disunday 1.0.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/dist/ai-tool-to-genai.js +208 -0
- package/dist/ai-tool-to-genai.test.js +267 -0
- package/dist/channel-management.js +96 -0
- package/dist/cli.js +1674 -0
- package/dist/commands/abort.js +89 -0
- package/dist/commands/add-project.js +117 -0
- package/dist/commands/agent.js +250 -0
- package/dist/commands/ask-question.js +219 -0
- package/dist/commands/compact.js +126 -0
- package/dist/commands/context-menu.js +171 -0
- package/dist/commands/context.js +89 -0
- package/dist/commands/cost.js +93 -0
- package/dist/commands/create-new-project.js +111 -0
- package/dist/commands/diff.js +77 -0
- package/dist/commands/export.js +100 -0
- package/dist/commands/files.js +73 -0
- package/dist/commands/fork.js +199 -0
- package/dist/commands/help.js +54 -0
- package/dist/commands/login.js +488 -0
- package/dist/commands/merge-worktree.js +165 -0
- package/dist/commands/model.js +325 -0
- package/dist/commands/permissions.js +140 -0
- package/dist/commands/ping.js +13 -0
- package/dist/commands/queue.js +133 -0
- package/dist/commands/remove-project.js +119 -0
- package/dist/commands/rename.js +70 -0
- package/dist/commands/restart-opencode-server.js +77 -0
- package/dist/commands/resume.js +276 -0
- package/dist/commands/run-config.js +79 -0
- package/dist/commands/run.js +240 -0
- package/dist/commands/schedule.js +170 -0
- package/dist/commands/session-info.js +58 -0
- package/dist/commands/session.js +191 -0
- package/dist/commands/settings.js +84 -0
- package/dist/commands/share.js +89 -0
- package/dist/commands/status.js +79 -0
- package/dist/commands/sync.js +119 -0
- package/dist/commands/theme.js +53 -0
- package/dist/commands/types.js +2 -0
- package/dist/commands/undo-redo.js +170 -0
- package/dist/commands/user-command.js +135 -0
- package/dist/commands/verbosity.js +59 -0
- package/dist/commands/worktree-settings.js +50 -0
- package/dist/commands/worktree.js +288 -0
- package/dist/config.js +139 -0
- package/dist/database.js +585 -0
- package/dist/discord-bot.js +700 -0
- package/dist/discord-utils.js +336 -0
- package/dist/discord-utils.test.js +20 -0
- package/dist/errors.js +193 -0
- package/dist/escape-backticks.test.js +429 -0
- package/dist/format-tables.js +96 -0
- package/dist/format-tables.test.js +418 -0
- package/dist/genai-worker-wrapper.js +109 -0
- package/dist/genai-worker.js +299 -0
- package/dist/genai.js +230 -0
- package/dist/image-utils.js +107 -0
- package/dist/interaction-handler.js +289 -0
- package/dist/limit-heading-depth.js +25 -0
- package/dist/limit-heading-depth.test.js +105 -0
- package/dist/logger.js +111 -0
- package/dist/markdown.js +323 -0
- package/dist/markdown.test.js +269 -0
- package/dist/message-formatting.js +447 -0
- package/dist/message-formatting.test.js +73 -0
- package/dist/openai-realtime.js +226 -0
- package/dist/opencode.js +224 -0
- package/dist/reaction-handler.js +128 -0
- package/dist/scheduler.js +93 -0
- package/dist/security.js +200 -0
- package/dist/session-handler.js +1436 -0
- package/dist/system-message.js +138 -0
- package/dist/tools.js +354 -0
- package/dist/unnest-code-blocks.js +117 -0
- package/dist/unnest-code-blocks.test.js +432 -0
- package/dist/utils.js +95 -0
- package/dist/voice-handler.js +569 -0
- package/dist/voice.js +344 -0
- package/dist/worker-types.js +4 -0
- package/dist/worktree-utils.js +134 -0
- package/dist/xml.js +90 -0
- package/dist/xml.test.js +32 -0
- package/package.json +84 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// AI SDK to Google GenAI tool converter.
|
|
2
|
+
// Transforms Vercel AI SDK tool definitions into Google GenAI CallableTool format
|
|
3
|
+
// for use with Gemini's function calling in the voice assistant.
|
|
4
|
+
import { Type } from '@google/genai';
|
|
5
|
+
import { z, toJSONSchema } from 'zod';
|
|
6
|
+
/**
|
|
7
|
+
* Convert JSON Schema to GenAI Schema format
|
|
8
|
+
* Based on the actual implementation used by the GenAI package:
|
|
9
|
+
* https://github.com/googleapis/js-genai/blob/027f09db662ce6b30f737b10b4d2efcb4282a9b6/src/_transformers.ts#L294
|
|
10
|
+
*/
|
|
11
|
+
function jsonSchemaToGenAISchema(jsonSchema) {
|
|
12
|
+
const schema = {};
|
|
13
|
+
// Map JSON Schema type to GenAI Type
|
|
14
|
+
if (jsonSchema.type) {
|
|
15
|
+
switch (jsonSchema.type) {
|
|
16
|
+
case 'string':
|
|
17
|
+
schema.type = Type.STRING;
|
|
18
|
+
break;
|
|
19
|
+
case 'number':
|
|
20
|
+
schema.type = Type.NUMBER;
|
|
21
|
+
schema.format = jsonSchema.format || 'float';
|
|
22
|
+
break;
|
|
23
|
+
case 'integer':
|
|
24
|
+
schema.type = Type.INTEGER;
|
|
25
|
+
schema.format = jsonSchema.format || 'int32';
|
|
26
|
+
break;
|
|
27
|
+
case 'boolean':
|
|
28
|
+
schema.type = Type.BOOLEAN;
|
|
29
|
+
break;
|
|
30
|
+
case 'array':
|
|
31
|
+
schema.type = Type.ARRAY;
|
|
32
|
+
if (jsonSchema.items) {
|
|
33
|
+
schema.items = jsonSchemaToGenAISchema(jsonSchema.items);
|
|
34
|
+
}
|
|
35
|
+
if (jsonSchema.minItems !== undefined) {
|
|
36
|
+
schema.minItems = jsonSchema.minItems;
|
|
37
|
+
}
|
|
38
|
+
if (jsonSchema.maxItems !== undefined) {
|
|
39
|
+
schema.maxItems = jsonSchema.maxItems;
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
case 'object':
|
|
43
|
+
schema.type = Type.OBJECT;
|
|
44
|
+
if (jsonSchema.properties) {
|
|
45
|
+
schema.properties = {};
|
|
46
|
+
for (const [key, value] of Object.entries(jsonSchema.properties)) {
|
|
47
|
+
schema.properties[key] = jsonSchemaToGenAISchema(value);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (jsonSchema.required) {
|
|
51
|
+
schema.required = jsonSchema.required;
|
|
52
|
+
}
|
|
53
|
+
// Note: GenAI Schema doesn't have additionalProperties field
|
|
54
|
+
// We skip it for now
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
// For unknown types, keep as-is
|
|
58
|
+
schema.type = jsonSchema.type;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// Copy over common properties
|
|
62
|
+
if (jsonSchema.description) {
|
|
63
|
+
schema.description = jsonSchema.description;
|
|
64
|
+
}
|
|
65
|
+
if (jsonSchema.enum) {
|
|
66
|
+
schema.enum = jsonSchema.enum.map(String);
|
|
67
|
+
}
|
|
68
|
+
if (jsonSchema.default !== undefined) {
|
|
69
|
+
schema.default = jsonSchema.default;
|
|
70
|
+
}
|
|
71
|
+
if (jsonSchema.example !== undefined) {
|
|
72
|
+
schema.example = jsonSchema.example;
|
|
73
|
+
}
|
|
74
|
+
if (jsonSchema.nullable) {
|
|
75
|
+
schema.nullable = true;
|
|
76
|
+
}
|
|
77
|
+
// Handle anyOf/oneOf as anyOf in GenAI
|
|
78
|
+
if (jsonSchema.anyOf) {
|
|
79
|
+
schema.anyOf = jsonSchema.anyOf.map((s) => jsonSchemaToGenAISchema(s));
|
|
80
|
+
}
|
|
81
|
+
else if (jsonSchema.oneOf) {
|
|
82
|
+
schema.anyOf = jsonSchema.oneOf.map((s) => jsonSchemaToGenAISchema(s));
|
|
83
|
+
}
|
|
84
|
+
// Handle number/string specific properties
|
|
85
|
+
if (jsonSchema.minimum !== undefined) {
|
|
86
|
+
schema.minimum = jsonSchema.minimum;
|
|
87
|
+
}
|
|
88
|
+
if (jsonSchema.maximum !== undefined) {
|
|
89
|
+
schema.maximum = jsonSchema.maximum;
|
|
90
|
+
}
|
|
91
|
+
if (jsonSchema.minLength !== undefined) {
|
|
92
|
+
schema.minLength = jsonSchema.minLength;
|
|
93
|
+
}
|
|
94
|
+
if (jsonSchema.maxLength !== undefined) {
|
|
95
|
+
schema.maxLength = jsonSchema.maxLength;
|
|
96
|
+
}
|
|
97
|
+
if (jsonSchema.pattern) {
|
|
98
|
+
schema.pattern = jsonSchema.pattern;
|
|
99
|
+
}
|
|
100
|
+
return schema;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Convert AI SDK Tool to GenAI FunctionDeclaration
|
|
104
|
+
*/
|
|
105
|
+
export function aiToolToGenAIFunction(tool) {
|
|
106
|
+
// Extract the input schema - assume it's a Zod schema
|
|
107
|
+
const inputSchema = tool.inputSchema;
|
|
108
|
+
// Get the tool name from the schema or generate one
|
|
109
|
+
let toolName = 'tool';
|
|
110
|
+
let jsonSchema = {};
|
|
111
|
+
if (inputSchema) {
|
|
112
|
+
// Convert Zod schema to JSON Schema
|
|
113
|
+
jsonSchema = toJSONSchema(inputSchema);
|
|
114
|
+
// Extract name from Zod description if available
|
|
115
|
+
const description = inputSchema.description;
|
|
116
|
+
if (description) {
|
|
117
|
+
const nameMatch = description.match(/name:\s*(\w+)/);
|
|
118
|
+
if (nameMatch) {
|
|
119
|
+
toolName = nameMatch[1] || '';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Convert JSON Schema to GenAI Schema
|
|
124
|
+
const genAISchema = jsonSchemaToGenAISchema(jsonSchema);
|
|
125
|
+
// Create the FunctionDeclaration
|
|
126
|
+
const functionDeclaration = {
|
|
127
|
+
name: toolName,
|
|
128
|
+
description: tool.description || jsonSchema.description || 'Tool function',
|
|
129
|
+
parameters: genAISchema,
|
|
130
|
+
};
|
|
131
|
+
return functionDeclaration;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Convert AI SDK Tool to GenAI CallableTool
|
|
135
|
+
*/
|
|
136
|
+
export function aiToolToCallableTool(tool, name) {
|
|
137
|
+
const toolName = name || 'tool';
|
|
138
|
+
return {
|
|
139
|
+
name,
|
|
140
|
+
async tool() {
|
|
141
|
+
const functionDeclaration = aiToolToGenAIFunction(tool);
|
|
142
|
+
if (name) {
|
|
143
|
+
functionDeclaration.name = name;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
functionDeclarations: [functionDeclaration],
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
async callTool(functionCalls) {
|
|
150
|
+
const parts = [];
|
|
151
|
+
for (const functionCall of functionCalls) {
|
|
152
|
+
// Check if this function call matches our tool
|
|
153
|
+
if (functionCall.name !== toolName && name && functionCall.name !== name) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
// Execute the tool if it has an execute function
|
|
157
|
+
if (tool.execute) {
|
|
158
|
+
try {
|
|
159
|
+
const result = await tool.execute(functionCall.args || {}, {
|
|
160
|
+
toolCallId: functionCall.id || '',
|
|
161
|
+
messages: [],
|
|
162
|
+
});
|
|
163
|
+
// Convert the result to a Part
|
|
164
|
+
parts.push({
|
|
165
|
+
functionResponse: {
|
|
166
|
+
id: functionCall.id,
|
|
167
|
+
name: functionCall.name || toolName,
|
|
168
|
+
response: {
|
|
169
|
+
output: result,
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
// Handle errors
|
|
176
|
+
parts.push({
|
|
177
|
+
functionResponse: {
|
|
178
|
+
id: functionCall.id,
|
|
179
|
+
name: functionCall.name || toolName,
|
|
180
|
+
response: {
|
|
181
|
+
error: error instanceof Error ? error.message : String(error),
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return parts;
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Helper to extract schema from AI SDK tool
|
|
194
|
+
*/
|
|
195
|
+
export function extractSchemaFromTool(tool) {
|
|
196
|
+
const inputSchema = tool.inputSchema;
|
|
197
|
+
if (!inputSchema) {
|
|
198
|
+
return {};
|
|
199
|
+
}
|
|
200
|
+
// Convert Zod schema to JSON Schema
|
|
201
|
+
return toJSONSchema(inputSchema);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Given an object of tools, creates an array of CallableTool
|
|
205
|
+
*/
|
|
206
|
+
export function callableToolsFromObject(tools) {
|
|
207
|
+
return Object.entries(tools).map(([name, tool]) => aiToolToCallableTool(tool, name));
|
|
208
|
+
}
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Discord channel and category management.
|
|
2
|
+
// Creates and manages Kimaki project channels (text + voice pairs),
|
|
3
|
+
// extracts channel metadata from topic tags, and ensures category structure.
|
|
4
|
+
import { ChannelType, } from 'discord.js';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { getDatabase, getChannelDirectory } from './database.js';
|
|
7
|
+
export async function ensureDisundayCategory(guild, botName) {
|
|
8
|
+
// Skip appending bot name if it's already "disunday" to avoid "Disunday disunday"
|
|
9
|
+
const isDisundayBot = botName?.toLowerCase() === 'disunday';
|
|
10
|
+
const categoryName = botName && !isDisundayBot ? `Disunday ${botName}` : 'Disunday';
|
|
11
|
+
const existingCategory = guild.channels.cache.find((channel) => {
|
|
12
|
+
if (channel.type !== ChannelType.GuildCategory) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return channel.name.toLowerCase() === categoryName.toLowerCase();
|
|
16
|
+
});
|
|
17
|
+
if (existingCategory) {
|
|
18
|
+
return existingCategory;
|
|
19
|
+
}
|
|
20
|
+
return guild.channels.create({
|
|
21
|
+
name: categoryName,
|
|
22
|
+
type: ChannelType.GuildCategory,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export async function ensureDisundayAudioCategory(guild, botName) {
|
|
26
|
+
// Skip appending bot name if it's already "disunday" to avoid "Disunday Audio disunday"
|
|
27
|
+
const isDisundayBot = botName?.toLowerCase() === 'disunday';
|
|
28
|
+
const categoryName = botName && !isDisundayBot ? `Disunday Audio ${botName}` : 'Disunday Audio';
|
|
29
|
+
const existingCategory = guild.channels.cache.find((channel) => {
|
|
30
|
+
if (channel.type !== ChannelType.GuildCategory) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return channel.name.toLowerCase() === categoryName.toLowerCase();
|
|
34
|
+
});
|
|
35
|
+
if (existingCategory) {
|
|
36
|
+
return existingCategory;
|
|
37
|
+
}
|
|
38
|
+
return guild.channels.create({
|
|
39
|
+
name: categoryName,
|
|
40
|
+
type: ChannelType.GuildCategory,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
export async function createProjectChannels({ guild, projectDirectory, appId, botName, enableVoiceChannels = false, }) {
|
|
44
|
+
const baseName = path.basename(projectDirectory);
|
|
45
|
+
const channelName = `${baseName}`
|
|
46
|
+
.toLowerCase()
|
|
47
|
+
.replace(/[^a-z0-9-]/g, '-')
|
|
48
|
+
.slice(0, 100);
|
|
49
|
+
const disundayCategory = await ensureDisundayCategory(guild, botName);
|
|
50
|
+
const textChannel = await guild.channels.create({
|
|
51
|
+
name: channelName,
|
|
52
|
+
type: ChannelType.GuildText,
|
|
53
|
+
parent: disundayCategory,
|
|
54
|
+
// Channel configuration is stored in SQLite, not in the topic
|
|
55
|
+
});
|
|
56
|
+
getDatabase()
|
|
57
|
+
.prepare('INSERT OR REPLACE INTO channel_directories (channel_id, directory, channel_type, app_id) VALUES (?, ?, ?, ?)')
|
|
58
|
+
.run(textChannel.id, projectDirectory, 'text', appId);
|
|
59
|
+
let voiceChannelId = null;
|
|
60
|
+
if (enableVoiceChannels) {
|
|
61
|
+
const disundayAudioCategory = await ensureDisundayAudioCategory(guild, botName);
|
|
62
|
+
const voiceChannel = await guild.channels.create({
|
|
63
|
+
name: channelName,
|
|
64
|
+
type: ChannelType.GuildVoice,
|
|
65
|
+
parent: disundayAudioCategory,
|
|
66
|
+
});
|
|
67
|
+
getDatabase()
|
|
68
|
+
.prepare('INSERT OR REPLACE INTO channel_directories (channel_id, directory, channel_type, app_id) VALUES (?, ?, ?, ?)')
|
|
69
|
+
.run(voiceChannel.id, projectDirectory, 'voice', appId);
|
|
70
|
+
voiceChannelId = voiceChannel.id;
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
textChannelId: textChannel.id,
|
|
74
|
+
voiceChannelId,
|
|
75
|
+
channelName,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export async function getChannelsWithDescriptions(guild) {
|
|
79
|
+
const channels = [];
|
|
80
|
+
guild.channels.cache
|
|
81
|
+
.filter((channel) => channel.isTextBased())
|
|
82
|
+
.forEach((channel) => {
|
|
83
|
+
const textChannel = channel;
|
|
84
|
+
const description = textChannel.topic || null;
|
|
85
|
+
// Get channel config from database instead of parsing XML from topic
|
|
86
|
+
const channelConfig = getChannelDirectory(textChannel.id);
|
|
87
|
+
channels.push({
|
|
88
|
+
id: textChannel.id,
|
|
89
|
+
name: textChannel.name,
|
|
90
|
+
description,
|
|
91
|
+
disundayDirectory: channelConfig?.directory,
|
|
92
|
+
disundayApp: channelConfig?.appId || undefined,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
return channels;
|
|
96
|
+
}
|