diff --git a/typescript/src/agents/anthropicAgent.ts b/typescript/src/agents/anthropicAgent.ts index f7625719..09b6370a 100644 --- a/typescript/src/agents/anthropicAgent.ts +++ b/typescript/src/agents/anthropicAgent.ts @@ -1,6 +1,6 @@ import { Agent, AgentCallbacks, AgentOptions } from "./agent"; import { - ANTHROPIC_MODEL_ID_CLAUDE_3_5_SONNET, + ANTHROPIC_MODEL_ID_CLAUDE_SONNET_4, ANTHROPIC_MODEL_ID_CLAUDE_3_5_SONNET, ConversationMessage, ParticipantRole, TemplateVariables, @@ -112,17 +112,20 @@ export class AnthropicAgent extends Agent { this.streaming = options.streaming ?? false; - this.modelId = options.modelId || ANTHROPIC_MODEL_ID_CLAUDE_3_5_SONNET; + this.modelId = options.modelId || ANTHROPIC_MODEL_ID_CLAUDE_SONNET_4; this.thinking = options.thinking ?? null; - const defaultMaxTokens = 1000; // You can adjust this default value as needed + const defaultMaxTokens = 1000; this.inferenceConfig = { maxTokens: options.inferenceConfig?.maxTokens ?? defaultMaxTokens, temperature: options.inferenceConfig?.temperature ?? 0.1, - topP: options.inferenceConfig?.topP ?? 0.9, stopSequences: options.inferenceConfig?.stopSequences ?? [], }; + // Only set topP if explicitly provided — newer Anthropic models reject both temperature and topP + if (options.inferenceConfig?.topP !== undefined) { + this.inferenceConfig.topP = options.inferenceConfig.topP; + } this.retriever = options.retriever; @@ -294,13 +297,12 @@ export class AnthropicAgent extends Agent { this.toolConfig?.toolMaxRecursions || this.defaultMaxRecursions; do { // Call Anthropic - const llmInput = { + const llmInput: any = { model: this.modelId, max_tokens: this.inferenceConfig.maxTokens, messages: messages, system: systemPrompt, temperature: this.inferenceConfig.temperature, - top_p: this.inferenceConfig.topP, thinking: this.thinking, ...(this.toolConfig && { tools: @@ -309,6 +311,10 @@ export class AnthropicAgent extends Agent { : this.toolConfig.tool, }), }; + // Only pass top_p if explicitly set — newer Anthropic models reject both temperature and top_p + if (this.inferenceConfig.topP !== undefined) { + llmInput.top_p = this.inferenceConfig.topP; + } const response = await this.handleSingleResponse(llmInput); const toolUseBlocks = response.content.filter( @@ -325,7 +331,7 @@ export class AnthropicAgent extends Agent { const tools = this.toolConfig.tool; const toolHandler = this.toolConfig.useToolHandler ?? - (async (response, conversationHistory) => { + (async (response, _conversationHistory) => { if (this.isAgentTools(tools)) { return tools.toolHandler( response, @@ -335,10 +341,9 @@ export class AnthropicAgent extends Agent { this.getInputData.bind(this) ); } - // Only use legacy handler when it's not AgentTools - return this.toolConfig.useToolHandler( - response, - conversationHistory + // Legacy Tool[] requires an explicit useToolHandler + throw new Error( + "toolConfig.useToolHandler is required when using Tool[] instead of AgentTools" ); }); @@ -399,7 +404,7 @@ export class AnthropicAgent extends Agent { let recursions = this.toolConfig?.toolMaxRecursions || 5; do { - const stream = await this.client.messages.stream({ + const streamConfig: any = { model: this.modelId, max_tokens: this.inferenceConfig.maxTokens, messages: messages, @@ -409,13 +414,18 @@ export class AnthropicAgent extends Agent { type: this.thinking?.type === "enabled" ? "enabled" : "disabled", budget_tokens: this.thinking?.budget_tokens }, - top_p: this.inferenceConfig.topP, ...(this.toolConfig && { tools: this.toolConfig.tool instanceof AgentTools ? this.formatTools(this.toolConfig.tool) : this.toolConfig.tool, }), + }; + // Only pass top_p if explicitly set — newer Anthropic models reject both temperature and top_p + if (this.inferenceConfig.topP !== undefined) { + streamConfig.top_p = this.inferenceConfig.topP; + } + const stream = await this.client.messages.stream(streamConfig); }); let toolBlock: Anthropic.ToolUseBlock = { @@ -462,7 +472,7 @@ export class AnthropicAgent extends Agent { const tools = this.toolConfig.tool; const toolHandler = this.toolConfig.useToolHandler ?? - (async (response, conversationHistory) => { + (async (response, _conversationHistory) => { if (this.isAgentTools(tools)) { return tools.toolHandler( response, @@ -472,10 +482,9 @@ export class AnthropicAgent extends Agent { this.getInputData.bind(this) ); } - // Only use legacy handler when it's not AgentTools - return this.toolConfig.useToolHandler( - response, - conversationHistory + // Legacy Tool[] requires an explicit useToolHandler + throw new Error( + "toolConfig.useToolHandler is required when using Tool[] instead of AgentTools" ); }); diff --git a/typescript/src/agents/bedrockLLMAgent.ts b/typescript/src/agents/bedrockLLMAgent.ts index a413b52c..3aa832c7 100644 --- a/typescript/src/agents/bedrockLLMAgent.ts +++ b/typescript/src/agents/bedrockLLMAgent.ts @@ -357,7 +357,7 @@ export class BedrockLLMAgent extends Agent { const toolHandler = this.toolConfig.useToolHandler ?? - (async (response, conversationHistory) => { + (async (response, _conversationHistory) => { if (this.isAgentTools(tools)) { return tools.toolHandler( response, @@ -367,10 +367,9 @@ export class BedrockLLMAgent extends Agent { this.getInputData.bind(this) ); } - // Only use legacy handler when it's not AgentTools - return this.toolConfig.useToolHandler( - response, - conversationHistory + // Legacy Tool[] requires an explicit useToolHandler + throw new Error( + "toolConfig.useToolHandler is required when using Tool[] instead of AgentTools" ); }); @@ -473,7 +472,7 @@ export class BedrockLLMAgent extends Agent { const tools = this.toolConfig.tool; const toolHandler = this.toolConfig.useToolHandler ?? - (async (response, conversationHistory) => { + (async (response, _conversationHistory) => { if (this.isAgentTools(tools)) { return tools.toolHandler( response, @@ -483,10 +482,9 @@ export class BedrockLLMAgent extends Agent { this.getInputData.bind(this) ); } - // Only use legacy handler when it's not AgentTools - return this.toolConfig.useToolHandler( - response, - conversationHistory + // Legacy Tool[] requires an explicit useToolHandler + throw new Error( + "toolConfig.useToolHandler is required when using Tool[] instead of AgentTools" ); }); diff --git a/typescript/src/agents/supervisorAgent.ts b/typescript/src/agents/supervisorAgent.ts index 2a987fe3..b79d47de 100644 --- a/typescript/src/agents/supervisorAgent.ts +++ b/typescript/src/agents/supervisorAgent.ts @@ -108,9 +108,22 @@ export class SupervisorAgent extends Agent { this.supervisorTools.tools.push(...additionalTools); } + const self = this; this.leadAgent.toolConfig = { tool: this.supervisorTools, toolMaxRecursions: SupervisorAgent.DEFAULT_TOOL_MAX_RECURSIONS, + useToolHandler: async (response: any, _conversation: ConversationMessage[]) => { + // Delegate to AgentTools.toolHandler with the required getter methods + // This ensures proper tool dispatch for streaming and non-streaming paths + const leadAgent = self.leadAgent as any; + return self.supervisorTools.toolHandler( + response, + leadAgent.getToolUseBlock.bind(leadAgent), + leadAgent.getToolName.bind(leadAgent), + leadAgent.getToolId.bind(leadAgent), + leadAgent.getInputData.bind(leadAgent) + ); + }, }; }