Skip to content

Commit 73604de

Browse files
committed
ensure that it is always objects returned
1 parent 3dfa8d1 commit 73604de

File tree

5 files changed

+57
-18
lines changed

5 files changed

+57
-18
lines changed

packages/agent/src/core/toolAgent.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ToolResultPart,
99
ToolSet,
1010
tool as makeTool,
11+
ToolCallPart,
1112
} from 'ai';
1213
import chalk from 'chalk';
1314

@@ -117,6 +118,11 @@ function processResponse(response: Anthropic.Message) {
117118
}
118119
*/
119120

121+
type ErrorResult = {
122+
errorMessage: string;
123+
errorType: string;
124+
};
125+
120126
async function executeTools(
121127
toolCalls: ToolUseContent[],
122128
tools: Tool[],
@@ -159,8 +165,12 @@ async function executeTools(
159165
tokenTracker: new TokenTracker(call.name, context.tokenTracker),
160166
});
161167
} catch (error: any) {
162-
toolResult = `Error: Exception thrown during tool execution. Type: ${error.constructor.name}, Message: ${error.message}`;
168+
toolResult = JSON.stringify({
169+
errorMessage: error.message,
170+
errorType: error.constructor.name,
171+
});
163172
}
173+
164174
return {
165175
type: 'tool-result',
166176
toolCallId: call.id,
@@ -173,7 +183,8 @@ async function executeTools(
173183
const sequenceCompletedTool = toolResults.find(
174184
(r) => r.toolName === 'sequenceComplete',
175185
);
176-
const completionResult = sequenceCompletedTool?.result as string;
186+
const completionResult = (sequenceCompletedTool?.result as { result: string })
187+
.result;
177188

178189
messages.push({
179190
role: 'tool',
@@ -292,7 +303,13 @@ export const toolAgent = async (
292303
messages: messagesWithCacheControl,
293304
tools: toolSet,
294305
};
295-
const { text, toolCalls } = await generateText(generateTextProps);
306+
const { text, toolCalls, ...other } = await generateText(generateTextProps);
307+
308+
//console.log(
309+
// 'providerMetadata',
310+
// JSON.stringify(other.providerMetadata, null, 2),
311+
//);
312+
//console.log('other data', JSON.stringify(other, null, 2));
296313

297314
const localToolCalls: ToolUseContent[] = toolCalls.map((call) => ({
298315
type: 'tool_use',
@@ -329,6 +346,20 @@ export const toolAgent = async (
329346
logger.info(text);
330347
}
331348

349+
if (toolCalls.length > 0) {
350+
const toolCallParts: Array<ToolCallPart> = toolCalls.map((toolCall) => ({
351+
type: 'tool-call',
352+
toolCallId: toolCall.toolCallId,
353+
toolName: toolCall.toolName,
354+
args: toolCall.args,
355+
}));
356+
357+
messages.push({
358+
role: 'assistant',
359+
content: toolCallParts,
360+
});
361+
}
362+
332363
/*logger.log(
333364
tokenTracker.logLevel,
334365
chalk.blue(`[Token Usage/Message] ${tokenUsagePerMessage.toString()}`),

packages/agent/src/tools/interaction/subAgent.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ const parameterSchema = z.object({
3636
.optional(),
3737
});
3838

39-
const returnSchema = z
40-
.string()
41-
.describe(
42-
'The response from the sub-agent including its reasoning and tool usage',
43-
);
39+
const returnSchema = z.object({
40+
response: z
41+
.string()
42+
.describe(
43+
'The response from the sub-agent including its reasoning and tool usage',
44+
),
45+
});
4446

4547
type Parameters = z.infer<typeof parameterSchema>;
4648
type ReturnType = z.infer<typeof returnSchema>;
@@ -109,7 +111,7 @@ export const subAgentTool: Tool<Parameters, ReturnType> = {
109111
workingDirectory:
110112
fileContext?.workingDirectory ?? context.workingDirectory,
111113
});
112-
return result.result; // Return the result string directly
114+
return { response: result.result };
113115
},
114116
logParameters: (input, { logger }) => {
115117
logger.info(`Delegating task "${input.description}"`);

packages/agent/src/tools/interaction/userPrompt.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const parameterSchema = z.object({
88
prompt: z.string().describe('The prompt message to display to the user'),
99
});
1010

11-
const returnSchema = z.string().describe("The user's response");
11+
const returnSchema = z.object({
12+
userText: z.string().describe("The user's response"),
13+
});
1214

1315
type Parameters = z.infer<typeof parameterSchema>;
1416
type ReturnType = z.infer<typeof returnSchema>;
@@ -28,7 +30,7 @@ export const userPromptTool: Tool<Parameters, ReturnType> = {
2830

2931
logger.verbose(`Received user response: ${response}`);
3032

31-
return response;
33+
return { userText: response };
3234
},
3335
logParameters: () => {},
3436
logReturns: () => {},

packages/agent/src/tools/system/respawn.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ const parameterSchema = z.object({
1111
respawnContext: z.string().describe('The context to keep after respawning'),
1212
});
1313

14-
const returnSchema = z
15-
.string()
16-
.describe('A message indicating that the respawn has been initiated');
14+
const returnSchema = z.object({
15+
result: z
16+
.string()
17+
.describe('A message indicating that the respawn has been initiated'),
18+
});
1719

1820
export const respawnTool: Tool = {
1921
name: 'respawn',

packages/agent/src/tools/system/sequenceComplete.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ const parameterSchema = z.object({
77
result: z.string().describe('The final result to return from the tool agent'),
88
});
99

10-
const returnSchema = z
11-
.string()
12-
.describe('This is returned to the caller of the tool agent.');
10+
const returnSchema = z.object({
11+
result: z
12+
.string()
13+
.describe('This is returned to the caller of the tool agent.'),
14+
});
1315

1416
type Parameters = z.infer<typeof parameterSchema>;
1517
type ReturnType = z.infer<typeof returnSchema>;
@@ -22,7 +24,7 @@ export const sequenceCompleteTool: Tool<Parameters, ReturnType> = {
2224
parametersJsonSchema: zodToJsonSchema(parameterSchema),
2325
returns: returnSchema,
2426
returnsJsonSchema: zodToJsonSchema(returnSchema),
25-
execute: ({ result }) => Promise.resolve(result),
27+
execute: ({ result }) => Promise.resolve({ result }),
2628
logParameters: () => {},
2729
logReturns: (output, { logger }) => {
2830
logger.info(`Completed: ${output}`);

0 commit comments

Comments
 (0)