Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions typescript/src/agents/anthropicAgent.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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:
Expand All @@ -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<Anthropic.ToolUseBlock>(
Expand All @@ -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,
Expand All @@ -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"
);
});

Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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"
);
});

Expand Down
18 changes: 8 additions & 10 deletions typescript/src/agents/bedrockLLMAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
);
});

Expand Down Expand Up @@ -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,
Expand All @@ -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"
);
});

Expand Down
13 changes: 13 additions & 0 deletions typescript/src/agents/supervisorAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
},
};
}

Expand Down