kimaki 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/bin.sh +28 -0
- package/dist/ai-tool-to-genai.js +207 -0
- package/dist/ai-tool-to-genai.test.js +267 -0
- package/dist/cli.js +357 -0
- package/dist/directVoiceStreaming.js +102 -0
- package/dist/discordBot.js +1740 -0
- package/dist/genai-worker-wrapper.js +104 -0
- package/dist/genai-worker.js +293 -0
- package/dist/genai.js +224 -0
- package/dist/logger.js +10 -0
- package/dist/markdown.js +199 -0
- package/dist/markdown.test.js +232 -0
- package/dist/openai-realtime.js +228 -0
- package/dist/plugin.js +1414 -0
- package/dist/tools.js +352 -0
- package/dist/utils.js +52 -0
- package/dist/voice.js +28 -0
- package/dist/worker-types.js +1 -0
- package/dist/xml.js +85 -0
- package/package.json +37 -56
- package/src/ai-tool-to-genai.test.ts +296 -0
- package/src/ai-tool-to-genai.ts +251 -0
- package/src/cli.ts +551 -0
- package/src/discordBot.ts +2350 -0
- package/src/genai-worker-wrapper.ts +152 -0
- package/src/genai-worker.ts +361 -0
- package/src/genai.ts +308 -0
- package/src/logger.ts +16 -0
- package/src/markdown.test.ts +314 -0
- package/src/markdown.ts +225 -0
- package/src/openai-realtime.ts +363 -0
- package/src/tools.ts +421 -0
- package/src/utils.ts +73 -0
- package/src/voice.ts +42 -0
- package/src/worker-types.ts +60 -0
- package/src/xml.ts +112 -0
- package/bin.js +0 -3
- package/dist/bin.d.ts +0 -3
- package/dist/bin.d.ts.map +0 -1
- package/dist/bin.js +0 -4
- package/dist/bin.js.map +0 -1
- package/dist/bundle.js +0 -3124
- package/dist/cli.d.ts.map +0 -1
package/README.md
CHANGED
package/bin.sh
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Restarts dist/cli.js if it exits non-zero.
|
|
3
|
+
# Throttles restarts to at most once every 5 seconds.
|
|
4
|
+
|
|
5
|
+
set -u -o pipefail
|
|
6
|
+
|
|
7
|
+
NODE_BIN="${NODE_BIN:-node}"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
last_start=0
|
|
11
|
+
|
|
12
|
+
while :; do
|
|
13
|
+
now=$(date +%s)
|
|
14
|
+
elapsed=$(( now - last_start ))
|
|
15
|
+
if (( elapsed < 5 )); then
|
|
16
|
+
sleep $(( 5 - elapsed ))
|
|
17
|
+
fi
|
|
18
|
+
last_start=$(date +%s)
|
|
19
|
+
|
|
20
|
+
"$NODE_BIN" "./dist/cli.js" "$@"
|
|
21
|
+
code=$?
|
|
22
|
+
|
|
23
|
+
# Exit cleanly if the app ended OK or via SIGINT/SIGTERM
|
|
24
|
+
if (( code == 0 || code == 130 || code == 143 || code == 64 )); then
|
|
25
|
+
exit "$code"
|
|
26
|
+
fi
|
|
27
|
+
# otherwise loop; the 5s throttle above will apply
|
|
28
|
+
done
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { Type } from '@google/genai';
|
|
2
|
+
import { z, toJSONSchema } from 'zod';
|
|
3
|
+
/**
|
|
4
|
+
* Convert JSON Schema to GenAI Schema format
|
|
5
|
+
* Based on the actual implementation used by the GenAI package:
|
|
6
|
+
* https://github.com/googleapis/js-genai/blob/027f09db662ce6b30f737b10b4d2efcb4282a9b6/src/_transformers.ts#L294
|
|
7
|
+
*/
|
|
8
|
+
function jsonSchemaToGenAISchema(jsonSchema) {
|
|
9
|
+
const schema = {};
|
|
10
|
+
// Map JSON Schema type to GenAI Type
|
|
11
|
+
if (jsonSchema.type) {
|
|
12
|
+
switch (jsonSchema.type) {
|
|
13
|
+
case 'string':
|
|
14
|
+
schema.type = Type.STRING;
|
|
15
|
+
break;
|
|
16
|
+
case 'number':
|
|
17
|
+
schema.type = Type.NUMBER;
|
|
18
|
+
schema.format = jsonSchema.format || 'float';
|
|
19
|
+
break;
|
|
20
|
+
case 'integer':
|
|
21
|
+
schema.type = Type.INTEGER;
|
|
22
|
+
schema.format = jsonSchema.format || 'int32';
|
|
23
|
+
break;
|
|
24
|
+
case 'boolean':
|
|
25
|
+
schema.type = Type.BOOLEAN;
|
|
26
|
+
break;
|
|
27
|
+
case 'array':
|
|
28
|
+
schema.type = Type.ARRAY;
|
|
29
|
+
if (jsonSchema.items) {
|
|
30
|
+
schema.items = jsonSchemaToGenAISchema(jsonSchema.items);
|
|
31
|
+
}
|
|
32
|
+
if (jsonSchema.minItems !== undefined) {
|
|
33
|
+
schema.minItems = jsonSchema.minItems;
|
|
34
|
+
}
|
|
35
|
+
if (jsonSchema.maxItems !== undefined) {
|
|
36
|
+
schema.maxItems = jsonSchema.maxItems;
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
case 'object':
|
|
40
|
+
schema.type = Type.OBJECT;
|
|
41
|
+
if (jsonSchema.properties) {
|
|
42
|
+
schema.properties = {};
|
|
43
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
44
|
+
schema.properties[key] = jsonSchemaToGenAISchema(value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (jsonSchema.required) {
|
|
48
|
+
schema.required = jsonSchema.required;
|
|
49
|
+
}
|
|
50
|
+
// Note: GenAI Schema doesn't have additionalProperties field
|
|
51
|
+
// We skip it for now
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
// For unknown types, keep as-is
|
|
55
|
+
schema.type = jsonSchema.type;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Copy over common properties
|
|
59
|
+
if (jsonSchema.description) {
|
|
60
|
+
schema.description = jsonSchema.description;
|
|
61
|
+
}
|
|
62
|
+
if (jsonSchema.enum) {
|
|
63
|
+
schema.enum = jsonSchema.enum.map(String);
|
|
64
|
+
}
|
|
65
|
+
if (jsonSchema.default !== undefined) {
|
|
66
|
+
schema.default = jsonSchema.default;
|
|
67
|
+
}
|
|
68
|
+
if (jsonSchema.example !== undefined) {
|
|
69
|
+
schema.example = jsonSchema.example;
|
|
70
|
+
}
|
|
71
|
+
if (jsonSchema.nullable) {
|
|
72
|
+
schema.nullable = true;
|
|
73
|
+
}
|
|
74
|
+
// Handle anyOf/oneOf as anyOf in GenAI
|
|
75
|
+
if (jsonSchema.anyOf) {
|
|
76
|
+
schema.anyOf = jsonSchema.anyOf.map((s) => jsonSchemaToGenAISchema(s));
|
|
77
|
+
}
|
|
78
|
+
else if (jsonSchema.oneOf) {
|
|
79
|
+
schema.anyOf = jsonSchema.oneOf.map((s) => jsonSchemaToGenAISchema(s));
|
|
80
|
+
}
|
|
81
|
+
// Handle number/string specific properties
|
|
82
|
+
if (jsonSchema.minimum !== undefined) {
|
|
83
|
+
schema.minimum = jsonSchema.minimum;
|
|
84
|
+
}
|
|
85
|
+
if (jsonSchema.maximum !== undefined) {
|
|
86
|
+
schema.maximum = jsonSchema.maximum;
|
|
87
|
+
}
|
|
88
|
+
if (jsonSchema.minLength !== undefined) {
|
|
89
|
+
schema.minLength = jsonSchema.minLength;
|
|
90
|
+
}
|
|
91
|
+
if (jsonSchema.maxLength !== undefined) {
|
|
92
|
+
schema.maxLength = jsonSchema.maxLength;
|
|
93
|
+
}
|
|
94
|
+
if (jsonSchema.pattern) {
|
|
95
|
+
schema.pattern = jsonSchema.pattern;
|
|
96
|
+
}
|
|
97
|
+
return schema;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Convert AI SDK Tool to GenAI FunctionDeclaration
|
|
101
|
+
*/
|
|
102
|
+
export function aiToolToGenAIFunction(tool) {
|
|
103
|
+
// Extract the input schema - assume it's a Zod schema
|
|
104
|
+
const inputSchema = tool.inputSchema;
|
|
105
|
+
// Get the tool name from the schema or generate one
|
|
106
|
+
let toolName = 'tool';
|
|
107
|
+
let jsonSchema = {};
|
|
108
|
+
if (inputSchema) {
|
|
109
|
+
// Convert Zod schema to JSON Schema
|
|
110
|
+
jsonSchema = toJSONSchema(inputSchema);
|
|
111
|
+
// Extract name from Zod description if available
|
|
112
|
+
const description = inputSchema.description;
|
|
113
|
+
if (description) {
|
|
114
|
+
const nameMatch = description.match(/name:\s*(\w+)/);
|
|
115
|
+
if (nameMatch) {
|
|
116
|
+
toolName = nameMatch[1] || '';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Convert JSON Schema to GenAI Schema
|
|
121
|
+
const genAISchema = jsonSchemaToGenAISchema(jsonSchema);
|
|
122
|
+
// Create the FunctionDeclaration
|
|
123
|
+
const functionDeclaration = {
|
|
124
|
+
name: toolName,
|
|
125
|
+
description: tool.description || jsonSchema.description || 'Tool function',
|
|
126
|
+
parameters: genAISchema,
|
|
127
|
+
};
|
|
128
|
+
return functionDeclaration;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Convert AI SDK Tool to GenAI CallableTool
|
|
132
|
+
*/
|
|
133
|
+
export function aiToolToCallableTool(tool, name) {
|
|
134
|
+
const toolName = name || 'tool';
|
|
135
|
+
return {
|
|
136
|
+
name,
|
|
137
|
+
async tool() {
|
|
138
|
+
const functionDeclaration = aiToolToGenAIFunction(tool);
|
|
139
|
+
if (name) {
|
|
140
|
+
functionDeclaration.name = name;
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
functionDeclarations: [functionDeclaration],
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
async callTool(functionCalls) {
|
|
147
|
+
const parts = [];
|
|
148
|
+
for (const functionCall of functionCalls) {
|
|
149
|
+
// Check if this function call matches our tool
|
|
150
|
+
if (functionCall.name !== toolName &&
|
|
151
|
+
name &&
|
|
152
|
+
functionCall.name !== name) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
// Execute the tool if it has an execute function
|
|
156
|
+
if (tool.execute) {
|
|
157
|
+
try {
|
|
158
|
+
const result = await tool.execute(functionCall.args || {}, {
|
|
159
|
+
toolCallId: functionCall.id || '',
|
|
160
|
+
messages: [],
|
|
161
|
+
});
|
|
162
|
+
// Convert the result to a Part
|
|
163
|
+
parts.push({
|
|
164
|
+
functionResponse: {
|
|
165
|
+
id: functionCall.id,
|
|
166
|
+
name: functionCall.name || toolName,
|
|
167
|
+
response: {
|
|
168
|
+
output: result,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
// Handle errors
|
|
175
|
+
parts.push({
|
|
176
|
+
functionResponse: {
|
|
177
|
+
id: functionCall.id,
|
|
178
|
+
name: functionCall.name || toolName,
|
|
179
|
+
response: {
|
|
180
|
+
error: error instanceof Error ? error.message : String(error),
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return parts;
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Helper to extract schema from AI SDK tool
|
|
193
|
+
*/
|
|
194
|
+
export function extractSchemaFromTool(tool) {
|
|
195
|
+
const inputSchema = tool.inputSchema;
|
|
196
|
+
if (!inputSchema) {
|
|
197
|
+
return {};
|
|
198
|
+
}
|
|
199
|
+
// Convert Zod schema to JSON Schema
|
|
200
|
+
return toJSONSchema(inputSchema);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Given an object of tools, creates an array of CallableTool
|
|
204
|
+
*/
|
|
205
|
+
export function callableToolsFromObject(tools) {
|
|
206
|
+
return Object.entries(tools).map(([name, tool]) => aiToolToCallableTool(tool, name));
|
|
207
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { tool } from 'ai';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { Type } from '@google/genai';
|
|
5
|
+
import { aiToolToGenAIFunction, aiToolToCallableTool, extractSchemaFromTool, } from './ai-tool-to-genai.js';
|
|
6
|
+
describe('AI Tool to GenAI Conversion', () => {
|
|
7
|
+
it('should convert a simple Zod-based tool', () => {
|
|
8
|
+
const weatherTool = tool({
|
|
9
|
+
description: 'Get the current weather for a location',
|
|
10
|
+
inputSchema: z.object({
|
|
11
|
+
location: z.string().describe('The city name'),
|
|
12
|
+
unit: z.enum(['celsius', 'fahrenheit']).optional(),
|
|
13
|
+
}),
|
|
14
|
+
execute: async ({ location, unit }) => {
|
|
15
|
+
return {
|
|
16
|
+
temperature: 72,
|
|
17
|
+
unit: unit || 'fahrenheit',
|
|
18
|
+
condition: 'sunny',
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
const genAIFunction = aiToolToGenAIFunction(weatherTool);
|
|
23
|
+
expect(genAIFunction).toMatchInlineSnapshot(`
|
|
24
|
+
{
|
|
25
|
+
"description": "Get the current weather for a location",
|
|
26
|
+
"name": "tool",
|
|
27
|
+
"parameters": {
|
|
28
|
+
"properties": {
|
|
29
|
+
"location": {
|
|
30
|
+
"description": "The city name",
|
|
31
|
+
"type": "STRING",
|
|
32
|
+
},
|
|
33
|
+
"unit": {
|
|
34
|
+
"enum": [
|
|
35
|
+
"celsius",
|
|
36
|
+
"fahrenheit",
|
|
37
|
+
],
|
|
38
|
+
"type": "STRING",
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
"required": [
|
|
42
|
+
"location",
|
|
43
|
+
],
|
|
44
|
+
"type": "OBJECT",
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
`);
|
|
48
|
+
});
|
|
49
|
+
it('should handle complex nested schemas', () => {
|
|
50
|
+
const complexTool = tool({
|
|
51
|
+
description: 'Process complex data',
|
|
52
|
+
inputSchema: z.object({
|
|
53
|
+
user: z.object({
|
|
54
|
+
name: z.string(),
|
|
55
|
+
age: z.number().int().min(0).max(150),
|
|
56
|
+
email: z.string().email(),
|
|
57
|
+
}),
|
|
58
|
+
preferences: z.array(z.string()),
|
|
59
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
60
|
+
}),
|
|
61
|
+
execute: async (input) => input,
|
|
62
|
+
});
|
|
63
|
+
const genAIFunction = aiToolToGenAIFunction(complexTool);
|
|
64
|
+
expect(genAIFunction.parameters).toMatchInlineSnapshot(`
|
|
65
|
+
{
|
|
66
|
+
"properties": {
|
|
67
|
+
"metadata": {
|
|
68
|
+
"type": "OBJECT",
|
|
69
|
+
},
|
|
70
|
+
"preferences": {
|
|
71
|
+
"items": {
|
|
72
|
+
"type": "STRING",
|
|
73
|
+
},
|
|
74
|
+
"type": "ARRAY",
|
|
75
|
+
},
|
|
76
|
+
"user": {
|
|
77
|
+
"properties": {
|
|
78
|
+
"age": {
|
|
79
|
+
"format": "int32",
|
|
80
|
+
"maximum": 150,
|
|
81
|
+
"minimum": 0,
|
|
82
|
+
"type": "INTEGER",
|
|
83
|
+
},
|
|
84
|
+
"email": {
|
|
85
|
+
"pattern": "^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$",
|
|
86
|
+
"type": "STRING",
|
|
87
|
+
},
|
|
88
|
+
"name": {
|
|
89
|
+
"type": "STRING",
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
"required": [
|
|
93
|
+
"name",
|
|
94
|
+
"age",
|
|
95
|
+
"email",
|
|
96
|
+
],
|
|
97
|
+
"type": "OBJECT",
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
"required": [
|
|
101
|
+
"user",
|
|
102
|
+
"preferences",
|
|
103
|
+
],
|
|
104
|
+
"type": "OBJECT",
|
|
105
|
+
}
|
|
106
|
+
`);
|
|
107
|
+
});
|
|
108
|
+
it('should extract schema from tool', () => {
|
|
109
|
+
const testTool = tool({
|
|
110
|
+
inputSchema: z.object({
|
|
111
|
+
test: z.string(),
|
|
112
|
+
}),
|
|
113
|
+
execute: async () => { },
|
|
114
|
+
});
|
|
115
|
+
const schema = extractSchemaFromTool(testTool);
|
|
116
|
+
expect(schema).toMatchInlineSnapshot(`
|
|
117
|
+
{
|
|
118
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
119
|
+
"additionalProperties": false,
|
|
120
|
+
"properties": {
|
|
121
|
+
"test": {
|
|
122
|
+
"type": "string",
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
"required": [
|
|
126
|
+
"test",
|
|
127
|
+
],
|
|
128
|
+
"type": "object",
|
|
129
|
+
}
|
|
130
|
+
`);
|
|
131
|
+
});
|
|
132
|
+
it('should handle tools with no input schema', () => {
|
|
133
|
+
const simpleTool = tool({
|
|
134
|
+
description: 'Simple tool with no inputs',
|
|
135
|
+
inputSchema: z.object({}),
|
|
136
|
+
execute: async () => ({ result: 'done' }),
|
|
137
|
+
});
|
|
138
|
+
const genAIFunction = aiToolToGenAIFunction(simpleTool);
|
|
139
|
+
expect(genAIFunction).toMatchInlineSnapshot(`
|
|
140
|
+
{
|
|
141
|
+
"description": "Simple tool with no inputs",
|
|
142
|
+
"name": "tool",
|
|
143
|
+
"parameters": {
|
|
144
|
+
"properties": {},
|
|
145
|
+
"type": "OBJECT",
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
`);
|
|
149
|
+
});
|
|
150
|
+
it('should handle union types', () => {
|
|
151
|
+
const unionTool = tool({
|
|
152
|
+
description: 'Tool with union types',
|
|
153
|
+
inputSchema: z.object({
|
|
154
|
+
value: z.union([z.string(), z.number(), z.boolean()]),
|
|
155
|
+
}),
|
|
156
|
+
execute: async ({ value }) => ({ received: value }),
|
|
157
|
+
});
|
|
158
|
+
const genAIFunction = aiToolToGenAIFunction(unionTool);
|
|
159
|
+
expect(genAIFunction.parameters?.properties?.value).toMatchInlineSnapshot(`
|
|
160
|
+
{
|
|
161
|
+
"anyOf": [
|
|
162
|
+
{
|
|
163
|
+
"type": "STRING",
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
"format": "float",
|
|
167
|
+
"type": "NUMBER",
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
"type": "BOOLEAN",
|
|
171
|
+
},
|
|
172
|
+
],
|
|
173
|
+
}
|
|
174
|
+
`);
|
|
175
|
+
});
|
|
176
|
+
it('should create a CallableTool', async () => {
|
|
177
|
+
const weatherTool = tool({
|
|
178
|
+
description: 'Get weather',
|
|
179
|
+
inputSchema: z.object({
|
|
180
|
+
location: z.string(),
|
|
181
|
+
}),
|
|
182
|
+
execute: async ({ location }) => ({
|
|
183
|
+
temperature: 72,
|
|
184
|
+
location,
|
|
185
|
+
}),
|
|
186
|
+
});
|
|
187
|
+
const callableTool = aiToolToCallableTool(weatherTool, 'weather');
|
|
188
|
+
// Test tool() method
|
|
189
|
+
const genAITool = await callableTool.tool();
|
|
190
|
+
expect(genAITool.functionDeclarations).toMatchInlineSnapshot(`
|
|
191
|
+
[
|
|
192
|
+
{
|
|
193
|
+
"description": "Get weather",
|
|
194
|
+
"name": "weather",
|
|
195
|
+
"parameters": {
|
|
196
|
+
"properties": {
|
|
197
|
+
"location": {
|
|
198
|
+
"type": "STRING",
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
"required": [
|
|
202
|
+
"location",
|
|
203
|
+
],
|
|
204
|
+
"type": "OBJECT",
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
]
|
|
208
|
+
`);
|
|
209
|
+
// Test callTool() method
|
|
210
|
+
const functionCall = {
|
|
211
|
+
id: 'call_123',
|
|
212
|
+
name: 'weather',
|
|
213
|
+
args: { location: 'San Francisco' },
|
|
214
|
+
};
|
|
215
|
+
const parts = await callableTool.callTool([functionCall]);
|
|
216
|
+
expect(parts).toMatchInlineSnapshot(`
|
|
217
|
+
[
|
|
218
|
+
{
|
|
219
|
+
"functionResponse": {
|
|
220
|
+
"id": "call_123",
|
|
221
|
+
"name": "weather",
|
|
222
|
+
"response": {
|
|
223
|
+
"output": {
|
|
224
|
+
"location": "San Francisco",
|
|
225
|
+
"temperature": 72,
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
},
|
|
230
|
+
]
|
|
231
|
+
`);
|
|
232
|
+
});
|
|
233
|
+
it('should handle tool execution errors', async () => {
|
|
234
|
+
const errorTool = tool({
|
|
235
|
+
description: 'Tool that throws',
|
|
236
|
+
inputSchema: z.object({
|
|
237
|
+
trigger: z.boolean(),
|
|
238
|
+
}),
|
|
239
|
+
execute: async ({ trigger }) => {
|
|
240
|
+
if (trigger) {
|
|
241
|
+
throw new Error('Tool execution failed');
|
|
242
|
+
}
|
|
243
|
+
return { success: true };
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
const callableTool = aiToolToCallableTool(errorTool, 'error_tool');
|
|
247
|
+
const functionCall = {
|
|
248
|
+
id: 'call_error',
|
|
249
|
+
name: 'error_tool',
|
|
250
|
+
args: { trigger: true },
|
|
251
|
+
};
|
|
252
|
+
const parts = await callableTool.callTool([functionCall]);
|
|
253
|
+
expect(parts).toMatchInlineSnapshot(`
|
|
254
|
+
[
|
|
255
|
+
{
|
|
256
|
+
"functionResponse": {
|
|
257
|
+
"id": "call_error",
|
|
258
|
+
"name": "error_tool",
|
|
259
|
+
"response": {
|
|
260
|
+
"error": "Tool execution failed",
|
|
261
|
+
},
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
]
|
|
265
|
+
`);
|
|
266
|
+
});
|
|
267
|
+
});
|