Skip to content
Merged
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
98 changes: 98 additions & 0 deletions src/__tests__/casing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,102 @@ describe("Key casing normalization", () => {
})
);
});

it("createAgentApi removes deprecated 'tools' field when 'tool_ids' is present", async () => {
const client = makeMockClient();
const conversation_config = {
conversation: {
client_events: ["audio"],
},
agent: {
prompt: {
prompt: "hi",
temperature: 0,
tools: [
{ type: "webhook", name: "test_tool", config: {} }
],
tool_ids: ["tool_123", "tool_456"]
}
},
} as unknown as Record<string, unknown>;

await createAgentApi(
client,
"Agent with Tools",
conversation_config,
undefined,
undefined,
[]
);

expect(client.conversationalAi.agents.create).toHaveBeenCalledTimes(1);
const payload = (client.conversationalAi.agents.create as jest.Mock).mock.calls[0][0];

// Verify that 'tools' field is removed but 'toolIds' is present
expect(payload.conversationConfig.agent.prompt).not.toHaveProperty("tools");
expect(payload.conversationConfig.agent.prompt).toHaveProperty("toolIds");
expect(payload.conversationConfig.agent.prompt.toolIds).toEqual(["tool_123", "tool_456"]);
});

it("updateAgentApi removes deprecated 'tools' field when 'tool_ids' is present", async () => {
const client = makeMockClient();
const conversation_config = {
agent: {
prompt: {
prompt: "updated",
tools: [
{ type: "system", name: "calendar" }
],
tool_ids: ["tool_789"]
}
},
} as unknown as Record<string, unknown>;

await updateAgentApi(
client,
"agent_123",
"Updated Agent",
conversation_config,
undefined,
undefined,
[]
);

expect(client.conversationalAi.agents.update).toHaveBeenCalledTimes(1);
const [, payload] = (client.conversationalAi.agents.update as jest.Mock).mock.calls[0];

// Verify that 'tools' field is removed but 'toolIds' is present
expect(payload.conversationConfig.agent.prompt).not.toHaveProperty("tools");
expect(payload.conversationConfig.agent.prompt).toHaveProperty("toolIds");
expect(payload.conversationConfig.agent.prompt.toolIds).toEqual(["tool_789"]);
});

it("createAgentApi preserves 'tools' field when 'tool_ids' is not present", async () => {
const client = makeMockClient();
const conversation_config = {
agent: {
prompt: {
prompt: "hi",
tools: [
{ type: "webhook", name: "legacy_tool" }
]
}
},
} as unknown as Record<string, unknown>;

await createAgentApi(
client,
"Agent with Legacy Tools",
conversation_config,
undefined,
undefined,
[]
);

const payload = (client.conversationalAi.agents.create as jest.Mock).mock.calls[0][0];

// When tool_ids is not present, tools should be preserved
expect(payload.conversationConfig.agent.prompt).toHaveProperty("tools");
expect(payload.conversationConfig.agent.prompt.tools).toHaveLength(1);
});
});
1 change: 0 additions & 1 deletion src/agents/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export function getDefaultAgentTemplate(name: string): AgentConfig {
llm: "gemini-2.0-flash",
temperature: 0.0,
max_tokens: -1,
tools: [],
tool_ids: [],
mcp_server_ids: [],
native_mcp_server_ids: [],
Expand Down
39 changes: 37 additions & 2 deletions src/shared/elevenlabs-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ function isConversationalConfig(config: unknown): config is ConversationalConfig
function isPlatformSettings(settings: unknown): settings is AgentPlatformSettingsRequestModel {
return typeof settings === 'object' && settings !== null;
}

/**
* Cleans conversation config before sending to API.
* Removes the deprecated 'tools' field if 'tool_ids' is present to avoid API conflicts.
* The API returns both fields, but only accepts one when creating/updating.
*/
function cleanConversationConfigForApi(config: Record<string, unknown>): Record<string, unknown> {
const cleaned = { ...config };

// Handle nested agent.prompt structure
if (cleaned.agent && typeof cleaned.agent === 'object') {
const agent = { ...(cleaned.agent as Record<string, unknown>) };

if (agent.prompt && typeof agent.prompt === 'object') {
const prompt = { ...(agent.prompt as Record<string, unknown>) };

// If tool_ids exists, remove tools (deprecated field) to avoid API error
if (prompt.tool_ids !== undefined || prompt.toolIds !== undefined) {
delete prompt.tools;
}

agent.prompt = prompt;
}

cleaned.agent = agent;
}

return cleaned;
}
/**
* Gets the API base URL based on residency configuration
*/
Expand Down Expand Up @@ -80,8 +109,11 @@ export async function createAgentApi(
throw new Error('Invalid conversation config provided');
}

// Clean config to remove deprecated 'tools' if 'tool_ids' exists
const cleanedConfig = cleanConversationConfigForApi(conversationConfigDict);

// Normalize to camelCase for API
const convConfig = toCamelCaseKeys(conversationConfigDict) as ConversationalConfig;
const convConfig = toCamelCaseKeys(cleanedConfig) as ConversationalConfig;
const platformSettings = platformSettingsDict && isPlatformSettings(platformSettingsDict) ? toCamelCaseKeys(platformSettingsDict) as AgentPlatformSettingsRequestModel : undefined;

const response = await client.conversationalAi.agents.create({
Expand Down Expand Up @@ -116,7 +148,10 @@ export async function updateAgentApi(
workflow?: unknown,
tags?: string[]
): Promise<string> {
const convConfig = conversationConfigDict && isConversationalConfig(conversationConfigDict) ? toCamelCaseKeys(conversationConfigDict) as ConversationalConfig : undefined;
// Clean config to remove deprecated 'tools' if 'tool_ids' exists
const cleanedConfig = conversationConfigDict ? cleanConversationConfigForApi(conversationConfigDict) : undefined;

const convConfig = cleanedConfig && isConversationalConfig(cleanedConfig) ? toCamelCaseKeys(cleanedConfig) as ConversationalConfig : undefined;
const platformSettings = platformSettingsDict && isPlatformSettings(platformSettingsDict) ? toCamelCaseKeys(platformSettingsDict) as AgentPlatformSettingsRequestModel : undefined;

const response = await client.conversationalAi.agents.update(agentId, {
Expand Down
Loading