|
1 | | -import { Agent } from "@/agent" |
| 1 | +import { Agent } from "@/agent"; |
2 | 2 | import { RenderableContent } from "@/memory/observation"; |
3 | | -import { z, Schema, ZodTypeAny } from "zod" |
| 3 | +import { z, ZodTypeAny, Schema } from "zod/v3"; |
4 | 4 |
|
5 | 5 | export interface ActionDefinition<T> { |
6 | | - name: string; |
7 | | - description?: string; |
8 | | - schema: Schema<T>; |
9 | | - resolver: ({ input, agent }: { input: T, agent: Agent }) => Promise<void | RenderableContent>; |
10 | | - render: (action: T) => string |
| 6 | + name: string; |
| 7 | + description?: string; |
| 8 | + schema: Schema<T>; |
| 9 | + resolver: ({ |
| 10 | + input, |
| 11 | + agent, |
| 12 | + }: { |
| 13 | + input: T; |
| 14 | + agent: Agent; |
| 15 | + }) => Promise<void | RenderableContent>; |
| 16 | + render: (action: T) => string; |
11 | 17 | } |
12 | 18 |
|
13 | | -export function createAction<S extends ZodTypeAny>( |
14 | | - action: { |
15 | | - name: string; |
16 | | - description?: string; |
17 | | - schema?: S; |
18 | | - resolver: ({ input, agent }: { input: z.infer<S>; agent: Agent }) => Promise<void | RenderableContent>; |
19 | | - render?: (action: z.infer<S>) => string |
20 | | - } |
21 | | -): ActionDefinition<z.infer<S>> { |
22 | | - // Just a helper for automatic schema typing |
23 | | - return { |
24 | | - name: action.name, |
25 | | - description: action.description, |
26 | | - schema: action.schema ?? z.object({}), |
27 | | - resolver: action.resolver, |
28 | | - render: action.render ?? ((action) => JSON.stringify(action)) |
29 | | - }; |
| 19 | +export function createAction<S extends ZodTypeAny>(action: { |
| 20 | + name: string; |
| 21 | + description?: string; |
| 22 | + schema?: S; |
| 23 | + resolver: ({ |
| 24 | + input, |
| 25 | + agent, |
| 26 | + }: { |
| 27 | + input: z.infer<S>; |
| 28 | + agent: Agent; |
| 29 | + }) => Promise<void | RenderableContent>; |
| 30 | + render?: (action: z.infer<S>) => string; |
| 31 | +}): ActionDefinition<z.infer<S>> { |
| 32 | + // Just a helper for automatic schema typing |
| 33 | + return { |
| 34 | + name: action.name, |
| 35 | + description: action.description, |
| 36 | + schema: action.schema ?? z.object({}), |
| 37 | + resolver: action.resolver, |
| 38 | + render: action.render ?? ((action) => JSON.stringify(action)), |
| 39 | + }; |
30 | 40 | } |
31 | 41 |
|
32 | | - |
33 | | - |
34 | 42 | // 2. Create a helper type to extract the payload structure for a single action |
35 | 43 | // This payload combines the 'name' (as a literal type) and the inferred schema. |
36 | | -export type ActionPayload<A extends ActionDefinition<any>> = { name: A['name'] } & z.infer<A['schema']>; |
37 | | - |
| 44 | +export type ActionPayload<A extends ActionDefinition<any>> = { |
| 45 | + name: A["name"]; |
| 46 | +} & z.infer<A["schema"]>; |
0 commit comments