1414 * limitations under the License.
1515 */
1616
17- import type { Action , ActionMetadata , BackgroundAction } from '@genkit-ai/core' ;
17+ import { type ModelAction } from '@genkit-ai/ai/model' ;
18+ import {
19+ GenkitError ,
20+ type Action ,
21+ type ActionMetadata ,
22+ type BackgroundAction ,
23+ } from '@genkit-ai/core' ;
1824import type { Genkit } from './genkit.js' ;
1925import type { ActionType } from './registry.js' ;
2026export { embedder , embedderActionMetadata } from '@genkit-ai/ai/embedder' ;
@@ -26,7 +32,6 @@ export {
2632} from '@genkit-ai/ai/model' ;
2733export { reranker } from '@genkit-ai/ai/reranker' ;
2834export { indexer , retriever } from '@genkit-ai/ai/retriever' ;
29-
3035export interface PluginProvider {
3136 name : string ;
3237 initializer : ( ) => void | Promise < void > ;
@@ -45,6 +50,9 @@ export interface GenkitPluginV2 {
4550 name : string
4651 ) => ResolvableAction | undefined | Promise < ResolvableAction | undefined > ;
4752 list ?: ( ) => ActionMetadata [ ] | Promise < ActionMetadata [ ] > ;
53+
54+ // A shortcut for resolving a model.
55+ model ( name : string ) : Promise < ModelAction > ;
4856}
4957
5058export type GenkitPlugin = ( genkit : Genkit ) => PluginProvider ;
@@ -85,10 +93,57 @@ export function genkitPlugin<T extends PluginInit>(
8593 } ) ;
8694}
8795
96+ export class GenkitPluginV2Instance implements Required < GenkitPluginV2 > {
97+ readonly version = 'v2' ;
98+ readonly name : string ;
99+
100+ private plugin : Omit < GenkitPluginV2 , 'version' | 'model' > ;
101+
102+ constructor ( plugin : Omit < GenkitPluginV2 , 'version' | 'model' > ) {
103+ this . name = plugin . name ;
104+ this . plugin = plugin ;
105+ }
106+
107+ init ( ) : ResolvableAction [ ] | Promise < ResolvableAction [ ] > {
108+ if ( ! this . plugin . init ) {
109+ return [ ] ;
110+ }
111+ return this . plugin . init ( ) ;
112+ }
113+
114+ list ( ) : ActionMetadata [ ] | Promise < ActionMetadata [ ] > {
115+ if ( ! this . plugin . list ) {
116+ return [ ] ;
117+ }
118+ return this . plugin . list ( ) ;
119+ }
120+
121+ resolve (
122+ actionType : ActionType ,
123+ name : string
124+ ) : ResolvableAction | undefined | Promise < ResolvableAction | undefined > {
125+ if ( ! this . plugin . resolve ) {
126+ return undefined ;
127+ }
128+ return this . plugin . resolve ( actionType , name ) ;
129+ }
130+
131+ async model ( name : string ) : Promise < ModelAction > {
132+ const model = await this . resolve ( 'model' , name ) ;
133+ if ( ! model ) {
134+ throw new GenkitError ( {
135+ message : `Failed to resolve model ${ name } for plugin ${ this . name } ` ,
136+ status : 'NOT_FOUND' ,
137+ } ) ;
138+ }
139+ return model as ModelAction ;
140+ }
141+ }
142+
88143export function genkitPluginV2 (
89- options : Omit < GenkitPluginV2 , 'version' >
90- ) : GenkitPluginV2 {
91- return { ... options , version : 'v2' } ;
144+ options : Omit < GenkitPluginV2 , 'version' | 'model' >
145+ ) : GenkitPluginV2Instance {
146+ return new GenkitPluginV2Instance ( options ) ;
92147}
93148
94149export function isPluginV2 ( plugin : unknown ) : plugin is GenkitPluginV2 {
0 commit comments