-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathutils.ts
More file actions
78 lines (71 loc) · 2.12 KB
/
utils.ts
File metadata and controls
78 lines (71 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import z from 'zod/v4'
import {
endsAgentStepParam,
endToolTag,
startToolTag,
toolNameParam,
} from '../constants'
import type { JSONValue } from '../../types/json'
import type { ToolResultOutput } from '../../types/messages/content-part'
/** Only used for generating tool call strings before all tools are defined.
*
* @param toolName - The name of the tool to call
* @param inputSchema - The zod schema for the tool. This is only used as type validation and is unused otherwise.
* @param input - The input to the tool
* @param endsAgentStep - Whether the agent should end its turn after this tool call
*/
export function $getToolCallString<Input>(params: {
toolName: string
inputSchema: z.ZodType<any, Input> | null
input: Input
endsAgentStep: boolean
}): string {
const { toolName, input, endsAgentStep } = params
const obj: Record<string, any> = {
[toolNameParam]: toolName,
...input,
}
if (endsAgentStep) {
obj[endsAgentStepParam] = endsAgentStep satisfies true
}
return [startToolTag, JSON.stringify(obj, null, 2), endToolTag].join('')
}
export function $getNativeToolCallExampleString<Input>(params: {
toolName: string
inputSchema: z.ZodType<any, Input> | null
input: Input
endsAgentStep?: boolean // unused
}): string {
const { toolName, input } = params
return [
`<${toolName}_params_example>\n`,
JSON.stringify(input, null, 2),
`\n</${toolName}_params_example>`,
].join('')
}
/** Generates the zod schema for a single JSON tool result. */
export function jsonToolResultSchema<T extends JSONValue>(
valueSchema: z.ZodType<T>,
) {
return z.tuple([
z.object({
type: z.literal('json'),
value: valueSchema,
}) satisfies z.ZodType<ToolResultOutput>,
])
}
/** Generates the zod schema for an empty tool result. */
export function emptyToolResultSchema() {
return z.tuple([])
}
/** Generates the zod schema for a simple text tool result. */
export function textToolResultSchema() {
return z.tuple([
z.object({
type: z.literal('json'),
value: z.object({
message: z.string(),
}),
}) satisfies z.ZodType<ToolResultOutput>,
])
}