Skip to content

Commit

Permalink
ensure that it is always objects returned
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Mar 4, 2025
1 parent 3dfa8d1 commit 73604de
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
37 changes: 34 additions & 3 deletions packages/agent/src/core/toolAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ToolResultPart,
ToolSet,
tool as makeTool,
ToolCallPart,
} from 'ai';
import chalk from 'chalk';

Expand Down Expand Up @@ -117,6 +118,11 @@ function processResponse(response: Anthropic.Message) {
}
*/

type ErrorResult = {
errorMessage: string;
errorType: string;
};

async function executeTools(
toolCalls: ToolUseContent[],
tools: Tool[],
Expand Down Expand Up @@ -159,8 +165,12 @@ async function executeTools(
tokenTracker: new TokenTracker(call.name, context.tokenTracker),
});
} catch (error: any) {
toolResult = `Error: Exception thrown during tool execution. Type: ${error.constructor.name}, Message: ${error.message}`;
toolResult = JSON.stringify({
errorMessage: error.message,
errorType: error.constructor.name,
});
}

return {
type: 'tool-result',
toolCallId: call.id,
Expand All @@ -173,7 +183,8 @@ async function executeTools(
const sequenceCompletedTool = toolResults.find(
(r) => r.toolName === 'sequenceComplete',
);
const completionResult = sequenceCompletedTool?.result as string;
const completionResult = (sequenceCompletedTool?.result as { result: string })
.result;

messages.push({
role: 'tool',
Expand Down Expand Up @@ -292,7 +303,13 @@ export const toolAgent = async (
messages: messagesWithCacheControl,
tools: toolSet,
};
const { text, toolCalls } = await generateText(generateTextProps);
const { text, toolCalls, ...other } = await generateText(generateTextProps);

//console.log(
// 'providerMetadata',
// JSON.stringify(other.providerMetadata, null, 2),
//);
//console.log('other data', JSON.stringify(other, null, 2));

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

if (toolCalls.length > 0) {
const toolCallParts: Array<ToolCallPart> = toolCalls.map((toolCall) => ({
type: 'tool-call',
toolCallId: toolCall.toolCallId,
toolName: toolCall.toolName,
args: toolCall.args,
}));

messages.push({
role: 'assistant',
content: toolCallParts,
});
}

/*logger.log(
tokenTracker.logLevel,
chalk.blue(`[Token Usage/Message] ${tokenUsagePerMessage.toString()}`),
Expand Down
14 changes: 8 additions & 6 deletions packages/agent/src/tools/interaction/subAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ const parameterSchema = z.object({
.optional(),
});

const returnSchema = z
.string()
.describe(
'The response from the sub-agent including its reasoning and tool usage',
);
const returnSchema = z.object({
response: z
.string()
.describe(
'The response from the sub-agent including its reasoning and tool usage',
),
});

type Parameters = z.infer<typeof parameterSchema>;
type ReturnType = z.infer<typeof returnSchema>;
Expand Down Expand Up @@ -109,7 +111,7 @@ export const subAgentTool: Tool<Parameters, ReturnType> = {
workingDirectory:
fileContext?.workingDirectory ?? context.workingDirectory,
});
return result.result; // Return the result string directly
return { response: result.result };
},
logParameters: (input, { logger }) => {
logger.info(`Delegating task "${input.description}"`);
Expand Down
6 changes: 4 additions & 2 deletions packages/agent/src/tools/interaction/userPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const parameterSchema = z.object({
prompt: z.string().describe('The prompt message to display to the user'),
});

const returnSchema = z.string().describe("The user's response");
const returnSchema = z.object({
userText: z.string().describe("The user's response"),
});

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

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

return response;
return { userText: response };
},
logParameters: () => {},
logReturns: () => {},
Expand Down
8 changes: 5 additions & 3 deletions packages/agent/src/tools/system/respawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const parameterSchema = z.object({
respawnContext: z.string().describe('The context to keep after respawning'),
});

const returnSchema = z
.string()
.describe('A message indicating that the respawn has been initiated');
const returnSchema = z.object({
result: z
.string()
.describe('A message indicating that the respawn has been initiated'),
});

export const respawnTool: Tool = {
name: 'respawn',
Expand Down
10 changes: 6 additions & 4 deletions packages/agent/src/tools/system/sequenceComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ const parameterSchema = z.object({
result: z.string().describe('The final result to return from the tool agent'),
});

const returnSchema = z
.string()
.describe('This is returned to the caller of the tool agent.');
const returnSchema = z.object({
result: z
.string()
.describe('This is returned to the caller of the tool agent.'),
});

type Parameters = z.infer<typeof parameterSchema>;
type ReturnType = z.infer<typeof returnSchema>;
Expand All @@ -22,7 +24,7 @@ export const sequenceCompleteTool: Tool<Parameters, ReturnType> = {
parametersJsonSchema: zodToJsonSchema(parameterSchema),
returns: returnSchema,
returnsJsonSchema: zodToJsonSchema(returnSchema),
execute: ({ result }) => Promise.resolve(result),
execute: ({ result }) => Promise.resolve({ result }),
logParameters: () => {},
logReturns: (output, { logger }) => {
logger.info(`Completed: ${output}`);
Expand Down

0 comments on commit 73604de

Please sign in to comment.