Skip to content

Commit e6a74df

Browse files
authored
fix(js/plugin): added init function to plugin v2 (#3431)
1 parent c01b9bc commit e6a74df

File tree

4 files changed

+64
-50
lines changed

4 files changed

+64
-50
lines changed

js/ai/src/embedder.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const EmbedRequestSchema = z.object({
5959
});
6060

6161
export interface EmbedRequest<O = any> {
62-
input: DocumentData[];
62+
input: Document[];
6363
options?: O;
6464
}
6565

@@ -120,7 +120,13 @@ export function embedder<ConfigSchema extends z.ZodTypeAny = z.ZodTypeAny>(
120120
) => Promise<EmbedResponse>
121121
) {
122122
const embedder = action(embedderActionOptions(options), (i, opts) =>
123-
runner(i, opts)
123+
runner(
124+
{
125+
input: i.input.map((dd) => new Document(dd)),
126+
options: i.options,
127+
},
128+
opts
129+
)
124130
);
125131
const ewm = withMetadata(
126132
embedder as Action<typeof EmbedRequestSchema, typeof EmbedResponseSchema>,

js/genkit/src/genkit.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ import type { HasRegistry } from '@genkit-ai/core/registry';
125125
import type { BaseEvalDataPointSchema } from './evaluator.js';
126126
import { logger } from './logging.js';
127127
import {
128+
ResolvableAction,
128129
isPluginV2,
129130
type GenkitPlugin,
130131
type GenkitPluginV2,
@@ -929,32 +930,17 @@ export class Genkit implements HasRegistry {
929930
name: plugin.name,
930931
async initializer() {
931932
logger.debug(`Initializing plugin ${plugin.name}:`);
933+
if (!plugin.init) return;
934+
const resolvedActions = await plugin.init();
935+
resolvedActions?.forEach((resolvedAction) => {
936+
registerActionV2(activeRegistry, resolvedAction, plugin);
937+
});
932938
},
933939
async resolver(action: ActionType, target: string) {
940+
if (!plugin.resolve) return;
934941
const resolvedAction = await plugin.resolve(action, target);
935942
if (resolvedAction) {
936-
if (isBackgroundAction(resolvedAction)) {
937-
registerBackgroundAction(activeRegistry, resolvedAction);
938-
} else if (isAction(resolvedAction)) {
939-
if (!resolvedAction.__action.actionType) {
940-
throw new GenkitError({
941-
status: 'INVALID_ARGUMENT',
942-
message:
943-
'Action type is missing for ' +
944-
resolvedAction.__action.name,
945-
});
946-
}
947-
activeRegistry.registerAction(
948-
resolvedAction.__action.actionType,
949-
resolvedAction
950-
);
951-
} else {
952-
throw new GenkitError({
953-
status: 'INVALID_ARGUMENT',
954-
message:
955-
'Unkown action type returned from plugin ' + plugin.name,
956-
});
957-
}
943+
registerActionV2(activeRegistry, resolvedAction, plugin);
958944
}
959945
},
960946
async listActions() {
@@ -998,6 +984,29 @@ export class Genkit implements HasRegistry {
998984
}
999985
}
1000986

987+
function registerActionV2(
988+
registry: Registry,
989+
resolvedAction: ResolvableAction,
990+
plugin: GenkitPluginV2
991+
) {
992+
if (isBackgroundAction(resolvedAction)) {
993+
registerBackgroundAction(registry, resolvedAction);
994+
} else if (isAction(resolvedAction)) {
995+
if (!resolvedAction.__action.actionType) {
996+
throw new GenkitError({
997+
status: 'INVALID_ARGUMENT',
998+
message: 'Action type is missing for ' + resolvedAction.__action.name,
999+
});
1000+
}
1001+
registry.registerAction(resolvedAction.__action.actionType, resolvedAction);
1002+
} else {
1003+
throw new GenkitError({
1004+
status: 'INVALID_ARGUMENT',
1005+
message: 'Unkown action type returned from plugin ' + plugin.name,
1006+
});
1007+
}
1008+
}
1009+
10011010
/**
10021011
* Initializes Genkit with a set of options.
10031012
*

js/genkit/src/plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export type ResolvableAction = Action | BackgroundAction;
3737
export interface GenkitPluginV2 {
3838
version: 'v2';
3939
name: string;
40-
resolve: (
40+
init?: () => ResolvableAction[] | Promise<ResolvableAction[]>;
41+
resolve?: (
4142
actionType: ActionType,
4243
name: string
4344
) => ResolvableAction | undefined | Promise<ResolvableAction | undefined>;

js/genkit/tests/plugins_test.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,16 @@ import {
2929
const v1Plugin = genkitPlugin(
3030
'myV1Plugin',
3131
(ai) => {
32-
ai.defineModel(
33-
{
34-
name: 'myV1Plugin/model_eager',
35-
},
36-
async () => {
37-
return {};
38-
}
39-
);
32+
ai.defineModel({ name: 'myV1Plugin/model_eager' }, async () => {
33+
return {};
34+
});
4035
},
4136
async (ai, actionType, name) => {
4237
switch (actionType) {
4338
case 'model':
44-
ai.defineModel(
45-
{
46-
name: 'myV1Plugin/' + name,
47-
},
48-
async () => {
49-
return {};
50-
}
51-
);
39+
ai.defineModel({ name: 'myV1Plugin/' + name }, async () => {
40+
return {};
41+
});
5242
case 'background-model':
5343
ai.defineBackgroundModel({
5444
name: 'myV1Plugin/' + name,
@@ -78,17 +68,19 @@ const v1Plugin = genkitPlugin(
7868

7969
const v2Plugin = genkitPluginV2({
8070
name: 'myV2Plugin',
71+
init() {
72+
return [
73+
model({ name: 'myV2Plugin/model_eager' }, async () => {
74+
return {};
75+
}),
76+
];
77+
},
8178
resolve(actionType, name) {
8279
switch (actionType) {
8380
case 'model':
84-
return model(
85-
{
86-
name: 'myV2Plugin/' + name,
87-
},
88-
async () => {
89-
return {};
90-
}
91-
);
81+
return model({ name: 'myV2Plugin/' + name }, async () => {
82+
return {};
83+
});
9284
case 'background-model':
9385
return backgroundModel({
9486
name: 'myV2Plugin/' + name,
@@ -139,6 +131,7 @@ describe('session', () => {
139131
'/embedder/myV2Plugin/potential_embedder',
140132
'/model/myV1Plugin/potential_model',
141133
'/model/myV1Plugin/model_eager',
134+
'/model/myV2Plugin/model_eager',
142135
'/model/myV2Plugin/potential_model',
143136
])
144137
);
@@ -148,7 +141,10 @@ describe('session', () => {
148141
(a) => a.startsWith('/model') || a.startsWith('/embedder')
149142
)
150143
),
151-
new Set(['/model/myV1Plugin/model_eager'])
144+
new Set([
145+
'/model/myV1Plugin/model_eager',
146+
'/model/myV2Plugin/model_eager',
147+
])
152148
);
153149
});
154150

@@ -168,6 +164,7 @@ describe('session', () => {
168164
),
169165
new Set([
170166
'/model/myV1Plugin/model_eager',
167+
'/model/myV2Plugin/model_eager',
171168
'/model/myV1Plugin/potential_model',
172169
])
173170
);
@@ -214,6 +211,7 @@ describe('session', () => {
214211
),
215212
new Set([
216213
'/model/myV1Plugin/model_eager',
214+
'/model/myV2Plugin/model_eager',
217215
'/model/myV2Plugin/potential_model',
218216
])
219217
);

0 commit comments

Comments
 (0)