Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions genkit-tools/common/src/manager/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,32 @@ export class RuntimeManager {
return response.data as Record<string, Action>;
}

/**
* Retrieves all valuess.
*/
async listValues(
input: apis.ListValuesRequest
): Promise<Record<string, unknown>> {
const runtime = input.runtimeId
? this.getRuntimeById(input.runtimeId)
: this.getMostRecentRuntime();
if (!runtime) {
throw new Error(
input?.runtimeId
? `No runtime found with ID ${input.runtimeId}.`
: 'No runtimes found. Make sure your app is running using `genkit start -- ...`. See getting started documentation.'
);
}
const response = await axios
.get(`${runtime.reflectionServerUrl}/api/values`, {
params: {
type: input.type,
},
})
.catch((err) => this.httpErrorHandler(err, 'Error listing values.'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backwards compatibility consideration -- Go and python won't have this endpoint implemented, so will be getting 404. Should 404 be handled here and an empty record returned, or bubble up the error all the way to the Dev UI and let it deal with backwards compatibility?

I'm leaning towards handle 404 here...

return response.data as Record<string, unknown>;
}

/**
* Runs an action.
*/
Expand Down
7 changes: 7 additions & 0 deletions genkit-tools/common/src/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ export const TOOLS_SERVER_ROUTER = (manager: RuntimeManager) =>
return manager.listActions(input);
}),

/** Retrieves all values. */
listValues: loggedProcedure
.input(apis.ListValuesRequestSchema)
.query(async ({ input }): Promise<Record<string, unknown>> => {
return manager.listValues(input);
}),

/** Generate a .prompt file from messages and model config. */
createPrompt: loggedProcedure
.input(apis.CreatePromptRequestSchema)
Expand Down
16 changes: 16 additions & 0 deletions genkit-tools/common/src/types/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ export const ListActionsRequestSchema = z

export type ListActionsRequest = z.infer<typeof ListActionsRequestSchema>;

export const ListValuesRequestSchema = z.object({
runtimeId: z
.string()
.optional()
.describe(
'ID of the Genkit runtime to run the action on. Typically $pid-$port.'
),
type: z
.enum(['defaultModel'])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restricted to defaultModel only.

.describe(
"The type of values to fetch. Currently only supports 'defaultModel'"
),
});

export type ListValuesRequest = z.infer<typeof ListValuesRequestSchema>;

export const RunActionRequestSchema = z.object({
runtimeId: z
.string()
Expand Down
16 changes: 16 additions & 0 deletions js/core/src/reflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ export class ReflectionServer {
await this.stop();
});

server.get('/api/values', async (req, response, next) => {
logger.debug('Fetching values.');
try {
const type = req.query.type;
if (!type) {
response.status(400).send('Query parameter "type" is required.');
return;
}
const values = await this.registry.listValues(type as string);
response.send(values);
} catch (err) {
const { message, stack } = err as Error;
next({ message, stack });
}
});

server.get('/api/actions', async (_, response, next) => {
logger.debug('Fetching actions.');
try {
Expand Down
19 changes: 18 additions & 1 deletion js/genkit/src/genkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
type GenerationCommonConfigSchema,
type IndexerParams,
type ModelArgument,
type ModelReference,
type Part,
type PromptConfig,
type PromptGenerateOptions,
Expand Down Expand Up @@ -953,7 +954,7 @@ export class Genkit implements HasRegistry {
this.registry.registerValue(
'defaultModel',
'defaultModel',
this.options.model
modelName(this.options.model)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realized that having the conversion done at registry time is more convenient, since all types needed are available here. WDYT?

);
}
if (this.options.promptDir !== null) {
Expand Down Expand Up @@ -1086,3 +1087,19 @@ let disableReflectionApi = false;
export function __disableReflectionApi() {
disableReflectionApi = true;
}

/** Helper method to map ModelArgument to string */
function modelName(
modelArg: ModelArgument<any> | undefined
): string | undefined {
if (modelArg === undefined) {
return undefined;
}
if (typeof modelArg === 'string') {
return modelArg;
}
if ((modelArg as ModelReference<any>).name) {
return (modelArg as ModelReference<any>).name;
}
return (modelArg as ModelAction).__action.name;
}
Loading