Skip to content

Commit 34ab5bd

Browse files
committed
feat:(genkit-tools) support passing runtimeId to tools apis
1 parent 7a2d81a commit 34ab5bd

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

genkit-tools/common/src/manager/manager.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,17 @@ export class RuntimeManager {
149149
/**
150150
* Retrieves all runnable actions.
151151
*/
152-
async listActions(): Promise<Record<string, Action>> {
153-
// TODO: Allow selecting a runtime by pid.
154-
const runtime = this.getMostRecentRuntime();
152+
async listActions(
153+
input?: apis.ListActionsRequest
154+
): Promise<Record<string, Action>> {
155+
const runtime = input?.runtimeId
156+
? this.getRuntimeById(input.runtimeId)
157+
: this.getMostRecentRuntime();
155158
if (!runtime) {
156159
throw new Error(
157-
'No runtimes found. Make sure your app is running using `genkit start -- ...`. See getting started documentation.'
160+
input?.runtimeId
161+
? `No runtime found with ID ${input.runtimeId}.`
162+
: 'No runtimes found. Make sure your app is running using `genkit start -- ...`. See getting started documentation.'
158163
);
159164
}
160165
const response = await axios
@@ -170,11 +175,14 @@ export class RuntimeManager {
170175
input: apis.RunActionRequest,
171176
streamingCallback?: StreamingCallback<any>
172177
): Promise<RunActionResponse> {
173-
// TODO: Allow selecting a runtime by pid.
174-
const runtime = this.getMostRecentRuntime();
178+
const runtime = input.runtimeId
179+
? this.getRuntimeById(input.runtimeId)
180+
: this.getMostRecentRuntime();
175181
if (!runtime) {
176182
throw new Error(
177-
'No runtimes found. Make sure your app is running using `genkit start -- ...`. See getting started documentation.'
183+
input.runtimeId
184+
? `No runtime found with ID ${input.runtimeId}.`
185+
: 'No runtimes found. Make sure your app is running using `genkit start -- ...`. See getting started documentation.'
178186
);
179187
}
180188
if (streamingCallback) {

genkit-tools/common/src/server/router.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@ const loggedProcedure = t.procedure.use(async (opts) => {
125125
export const TOOLS_SERVER_ROUTER = (manager: RuntimeManager) =>
126126
t.router({
127127
/** Retrieves all runnable actions. */
128-
listActions: loggedProcedure.query(
129-
async (): Promise<Record<string, Action>> => {
130-
return manager.listActions();
131-
}
132-
),
128+
listActions: loggedProcedure
129+
.input(apis.ListActionsRequestSchema)
130+
.query(async ({ input }): Promise<Record<string, Action>> => {
131+
return manager.listActions(input);
132+
}),
133133

134134
/** Runs an action. */
135135
runAction: loggedProcedure

genkit-tools/common/src/types/apis.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,24 @@ export const GetTraceRequestSchema = z.object({
6161

6262
export type GetTraceRequest = z.infer<typeof GetTraceRequestSchema>;
6363

64+
export const ListActionsRequestSchema = z.object({
65+
runtimeId: z
66+
.string()
67+
.optional()
68+
.describe(
69+
'ID of the Genkit runtime to run the action on. Typically $pid-$port.'
70+
),
71+
});
72+
73+
export type ListActionsRequest = z.infer<typeof ListActionsRequestSchema>;
74+
6475
export const RunActionRequestSchema = z.object({
76+
runtimeId: z
77+
.string()
78+
.optional()
79+
.describe(
80+
'ID of the Genkit runtime to run the action on. Typically $pid-$port.'
81+
),
6582
key: z
6683
.string()
6784
.describe('Action key that consists of the action type and ID.'),

0 commit comments

Comments
 (0)