refactor(ai): Update API to support multiple thinking and text blocks

BREAKING CHANGE: AssistantMessage now uses content array instead of separate fields
- Changed AssistantMessage.content from string to array of content blocks
- Removed separate thinking, toolCalls, and signature fields
- Content blocks can be TextContent, ThinkingContent, or ToolCall types
- Updated streaming events to include start/end events for text and thinking
- Fixed multiTurn test to handle new content structure

Note: Currently only Anthropic provider is updated to work with new API
Other providers need to be updated to match the new interface
This commit is contained in:
Mario Zechner 2025-08-31 19:32:12 +02:00
parent f8a81b9453
commit f29752ac82
3 changed files with 167 additions and 207 deletions

View file

@ -6,17 +6,7 @@ import type {
Tool, Tool,
} from "@anthropic-ai/sdk/resources/messages.js"; } from "@anthropic-ai/sdk/resources/messages.js";
import { calculateCost } from "../models.js"; import { calculateCost } from "../models.js";
import type { import type { AssistantMessage, Context, LLM, LLMOptions, Message, Model, StopReason, Usage } from "../types.js";
AssistantMessage,
Context,
LLM,
LLMOptions,
Message,
Model,
StopReason,
ToolCall,
Usage,
} from "../types.js";
export interface AnthropicLLMOptions extends LLMOptions { export interface AnthropicLLMOptions extends LLMOptions {
thinking?: { thinking?: {
@ -130,63 +120,65 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
); );
let blockType: "text" | "thinking" | "other" = "other"; let blockType: "text" | "thinking" | "other" = "other";
let blockContent = "";
for await (const event of stream) { for await (const event of stream) {
if (event.type === "content_block_start") { if (event.type === "content_block_start") {
if (event.content_block.type === "text") { if (event.content_block.type === "text") {
blockType = "text"; blockType = "text";
blockContent = "";
options?.onEvent?.({ type: "text_start" });
} else if (event.content_block.type === "thinking") { } else if (event.content_block.type === "thinking") {
blockType = "thinking"; blockType = "thinking";
blockContent = "";
options?.onEvent?.({ type: "thinking_start" });
} else { } else {
blockType = "other"; blockType = "other";
blockContent = "";
} }
} }
if (event.type === "content_block_delta") { if (event.type === "content_block_delta") {
if (event.delta.type === "text_delta") { if (event.delta.type === "text_delta") {
options?.onText?.(event.delta.text, false); options?.onEvent?.({ type: "text_delta", content: blockContent, delta: event.delta.text });
blockType = "text"; // Ensure block type is set blockContent += event.delta.text;
} }
if (event.delta.type === "thinking_delta") { if (event.delta.type === "thinking_delta") {
options?.onThinking?.(event.delta.thinking, false); options?.onEvent?.({ type: "thinking_delta", content: blockContent, delta: event.delta.thinking });
blockType = "thinking"; // Ensure block type is set blockContent += event.delta.thinking;
} }
} }
if (event.type === "content_block_stop") { if (event.type === "content_block_stop") {
if (blockType === "text") { if (blockType === "text") {
options?.onText?.("", true); options?.onEvent?.({ type: "text_end", content: blockContent });
} else if (blockType === "thinking") { } else if (blockType === "thinking") {
options?.onThinking?.("", true); options?.onEvent?.({ type: "thinking_end", content: blockContent });
} }
blockType = "other"; blockType = "other";
} }
} }
const msg = await stream.finalMessage(); const msg = await stream.finalMessage();
const thinking = msg.content.some((block) => block.type === "thinking") const blocks: AssistantMessage["content"] = [];
? msg.content for (const block of msg.content) {
.filter((block) => block.type === "thinking") if (block.type === "text" && block.text) {
.map((block) => block.thinking) blocks.push({
.join("\n") type: "text",
: undefined; text: block.text,
// This is kinda wrong if there is more than one thinking block. We do not use interleaved thinking though, so we should });
// always have a single thinking block. } else if (block.type === "thinking" && block.thinking) {
const thinkingSignature = msg.content.some((block) => block.type === "thinking") blocks.push({
? msg.content type: "thinking",
.filter((block) => block.type === "thinking") thinking: block.thinking,
.map((block) => block.signature) thinkingSignature: block.signature,
.join("\n") });
: undefined; } else if (block.type === "tool_use") {
const content = msg.content.some((block) => block.type === "text") blocks.push({
? msg.content type: "toolCall",
.filter((block) => block.type === "text") id: block.id,
.map((block) => block.text) name: block.name,
.join("\n") arguments: block.input as Record<string, any>,
: undefined; });
const toolCalls: ToolCall[] = msg.content }
.filter((block) => block.type === "tool_use") }
.map((block) => ({
id: block.id,
name: block.name,
arguments: block.input as Record<string, any>,
}));
const usage: Usage = { const usage: Usage = {
input: msg.usage.input_tokens, input: msg.usage.input_tokens,
output: msg.usage.output_tokens, output: msg.usage.output_tokens,
@ -204,10 +196,7 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
return { return {
role: "assistant", role: "assistant",
content, content: blocks,
thinking,
thinkingSignature,
toolCalls,
provider: this.modelInfo.provider, provider: this.modelInfo.provider,
model: this.modelInfo.id, model: this.modelInfo.id,
usage, usage,
@ -216,6 +205,7 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
} catch (error) { } catch (error) {
return { return {
role: "assistant", role: "assistant",
content: [],
provider: this.modelInfo.provider, provider: this.modelInfo.provider,
model: this.modelInfo.id, model: this.modelInfo.id,
usage: { usage: {
@ -270,28 +260,24 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
} else if (msg.role === "assistant") { } else if (msg.role === "assistant") {
const blocks: ContentBlockParam[] = []; const blocks: ContentBlockParam[] = [];
if (msg.thinking && msg.thinkingSignature) { for (const block of msg.content) {
blocks.push({ if (block.type === "text") {
type: "thinking", blocks.push({
thinking: msg.thinking, type: "text",
signature: msg.thinkingSignature, text: block.text,
}); });
} } else if (block.type === "thinking") {
blocks.push({
if (msg.content) { type: "thinking",
blocks.push({ thinking: block.thinking,
type: "text", signature: block.thinkingSignature || "",
text: msg.content, });
}); } else if (block.type === "toolCall") {
}
if (msg.toolCalls) {
for (const toolCall of msg.toolCalls) {
blocks.push({ blocks.push({
type: "tool_use", type: "tool_use",
id: toolCall.id, id: block.id,
name: toolCall.name, name: block.name,
input: toolCall.arguments, input: block.arguments,
}); });
} }
} }

View file

@ -1,8 +1,7 @@
export interface LLMOptions { export interface LLMOptions {
temperature?: number; temperature?: number;
maxTokens?: number; maxTokens?: number;
onText?: (text: string, complete: boolean) => void; onEvent?: (event: AssistantMessageEvent) => void;
onThinking?: (thinking: string, complete: boolean) => void;
signal?: AbortSignal; signal?: AbortSignal;
} }
@ -14,6 +13,13 @@ export interface LLM<T extends LLMOptions> {
export interface TextContent { export interface TextContent {
type: "text"; type: "text";
text: string; text: string;
textSignature?: string; // e.g., for OpenAI responses, the message ID
}
export interface ThinkingContent {
type: "thinking";
thinking: string;
thinkingSignature?: string; // e.g., for OpenAI responses, the reasoning item ID
} }
export interface ImageContent { export interface ImageContent {
@ -22,6 +28,29 @@ export interface ImageContent {
mimeType: string; // e.g., "image/jpeg", "image/png" mimeType: string; // e.g., "image/jpeg", "image/png"
} }
export interface ToolCall {
type: "toolCall";
id: string;
name: string;
arguments: Record<string, any>;
}
export interface Usage {
input: number;
output: number;
cacheRead: number;
cacheWrite: number;
cost: {
input: number;
output: number;
cacheRead: number;
cacheWrite: number;
total: number;
};
}
export type StopReason = "stop" | "length" | "toolUse" | "safety" | "error";
export interface UserMessage { export interface UserMessage {
role: "user"; role: "user";
content: string | (TextContent | ImageContent)[]; content: string | (TextContent | ImageContent)[];
@ -29,18 +58,7 @@ export interface UserMessage {
export interface AssistantMessage { export interface AssistantMessage {
role: "assistant"; role: "assistant";
thinking?: string; content: (TextContent | ThinkingContent | ToolCall)[];
// Leaky abstraction: provider specific, does not translate to other providers
thinkingSignature?: string;
content?: string;
// Leaky abstraction: provider specific, does not translate to other providers
// e.g. OpenAI responses must include id for assistant responses
contentSignature?: string;
toolCalls?: {
id: string;
name: string;
arguments: Record<string, any>;
}[];
provider: string; provider: string;
model: string; model: string;
usage: Usage; usage: Usage;
@ -70,37 +88,19 @@ export interface Context {
tools?: Tool[]; tools?: Tool[];
} }
export type Event = export type AssistantMessageEvent =
| { type: "start"; model: string; provider: string } | { type: "start"; model: string; provider: string }
| { type: "text"; content: string; delta: string } | { type: "text_start" }
| { type: "thinking"; content: string; delta: string } | { type: "text_delta"; content: string; delta: string }
| { type: "text_end"; content: string }
| { type: "thinking_start" }
| { type: "thinking_delta"; content: string; delta: string }
| { type: "thinking_end"; content: string }
| { type: "toolCall"; toolCall: ToolCall } | { type: "toolCall"; toolCall: ToolCall }
| { type: "usage"; usage: Usage } | { type: "usage"; usage: Usage }
| { type: "done"; reason: StopReason; message: AssistantMessage } | { type: "done"; reason: StopReason; message: AssistantMessage }
| { type: "error"; error: Error }; | { type: "error"; error: Error };
export interface ToolCall {
id: string;
name: string;
arguments: Record<string, any>;
}
export interface Usage {
input: number;
output: number;
cacheRead: number;
cacheWrite: number;
cost: {
input: number;
output: number;
cacheRead: number;
cacheWrite: number;
total: number;
};
}
export type StopReason = "stop" | "length" | "toolUse" | "safety" | "error";
// Model interface for the unified model system // Model interface for the unified model system
export interface Model { export interface Model {
id: string; id: string;

View file

@ -47,7 +47,7 @@ async function basicTextGeneration<T extends LLMOptions>(llm: LLM<T>) {
expect(response.usage.input).toBeGreaterThan(0); expect(response.usage.input).toBeGreaterThan(0);
expect(response.usage.output).toBeGreaterThan(0); expect(response.usage.output).toBeGreaterThan(0);
expect(response.error).toBeFalsy(); expect(response.error).toBeFalsy();
expect(response.content).toContain("Hello test successful"); expect(response.content.map(b => b.type == "text" ? b.text : "").join("\n")).toContain("Hello test successful");
context.messages.push(response); context.messages.push(response);
context.messages.push({ role: "user", content: "Now say 'Goodbye test successful'" }); context.messages.push({ role: "user", content: "Now say 'Goodbye test successful'" });
@ -59,7 +59,7 @@ async function basicTextGeneration<T extends LLMOptions>(llm: LLM<T>) {
expect(secondResponse.usage.input).toBeGreaterThan(0); expect(secondResponse.usage.input).toBeGreaterThan(0);
expect(secondResponse.usage.output).toBeGreaterThan(0); expect(secondResponse.usage.output).toBeGreaterThan(0);
expect(secondResponse.error).toBeFalsy(); expect(secondResponse.error).toBeFalsy();
expect(secondResponse.content).toContain("Goodbye test successful"); expect(secondResponse.content.map(b => b.type == "text" ? b.text : "").join("\n")).toContain("Goodbye test successful");
} }
async function handleToolCall<T extends LLMOptions>(llm: LLM<T>) { async function handleToolCall<T extends LLMOptions>(llm: LLM<T>) {
@ -74,14 +74,14 @@ async function handleToolCall<T extends LLMOptions>(llm: LLM<T>) {
const response = await llm.complete(context); const response = await llm.complete(context);
expect(response.stopReason).toBe("toolUse"); expect(response.stopReason).toBe("toolUse");
expect(response.toolCalls).toBeTruthy(); expect(response.content.some(b => b.type == "toolCall")).toBeTruthy();
expect(response.toolCalls!.length).toBeGreaterThan(0); const toolCall = response.content.find(b => b.type == "toolCall")!;
const toolCall = response.toolCalls![0];
expect(toolCall.name).toBe("calculator"); expect(toolCall.name).toBe("calculator");
expect(toolCall.id).toBeTruthy(); expect(toolCall.id).toBeTruthy();
} }
async function handleStreaming<T extends LLMOptions>(llm: LLM<T>) { async function handleStreaming<T extends LLMOptions>(llm: LLM<T>) {
let textStarted = false;
let textChunks = ""; let textChunks = "";
let textCompleted = false; let textCompleted = false;
@ -90,37 +90,50 @@ async function handleStreaming<T extends LLMOptions>(llm: LLM<T>) {
}; };
const response = await llm.complete(context, { const response = await llm.complete(context, {
onText: (chunk, complete) => { onEvent: (event) => {
textChunks += chunk; if (event.type === "text_start") {
if (complete) textCompleted = true; textStarted = true;
} else if (event.type === "text_delta") {
textChunks += event.delta;
} else if (event.type === "text_end") {
textCompleted = true;
}
} }
} as T); } as T);
expect(textStarted).toBe(true);
expect(textChunks.length).toBeGreaterThan(0); expect(textChunks.length).toBeGreaterThan(0);
expect(textCompleted).toBe(true); expect(textCompleted).toBe(true);
expect(response.content).toBeTruthy(); expect(response.content.some(b => b.type == "text")).toBeTruthy();
} }
async function handleThinking<T extends LLMOptions>(llm: LLM<T>, options: T, requireThinking: boolean = true) { async function handleThinking<T extends LLMOptions>(llm: LLM<T>, options: T) {
let thinkingStarted = false;
let thinkingChunks = ""; let thinkingChunks = "";
let thinkingCompleted = false;
const context: Context = { const context: Context = {
messages: [{ role: "user", content: "What is 15 + 27? Think step by step." }] messages: [{ role: "user", content: "What is 15 + 27? Think step by step." }]
}; };
const response = await llm.complete(context, { const response = await llm.complete(context, {
onThinking: (chunk) => { onEvent: (event) => {
thinkingChunks += chunk; if (event.type === "thinking_start") {
thinkingStarted = true;
} else if (event.type === "thinking_delta") {
thinkingChunks += event.delta;
} else if (event.type === "thinking_end") {
thinkingCompleted = true;
}
}, },
...options ...options
}); });
expect(response.content).toBeTruthy();
// For providers that should always return thinking when enabled expect(thinkingStarted).toBe(true);
if (requireThinking) { expect(thinkingChunks.length).toBeGreaterThan(0);
expect(thinkingChunks.length > 0 || !!response.thinking).toBe(true); expect(thinkingCompleted).toBe(true);
} expect(response.content.some(b => b.type == "thinking")).toBeTruthy();
} }
async function handleImage<T extends LLMOptions>(llm: LLM<T>) { async function handleImage<T extends LLMOptions>(llm: LLM<T>) {
@ -157,8 +170,8 @@ async function handleImage<T extends LLMOptions>(llm: LLM<T>) {
const response = await llm.complete(context); const response = await llm.complete(context);
// Check the response mentions red and circle // Check the response mentions red and circle
expect(response.content).toBeTruthy(); expect(response.content.length > 0).toBeTruthy();
const lowerContent = response.content?.toLowerCase() || ""; const lowerContent = response.content.find(b => b.type == "text")?.text || "";
expect(lowerContent).toContain("red"); expect(lowerContent).toContain("red");
expect(lowerContent).toContain("circle"); expect(lowerContent).toContain("circle");
} }
@ -175,74 +188,33 @@ async function multiTurn<T extends LLMOptions>(llm: LLM<T>, thinkingOptions: T)
tools: [calculatorTool] tools: [calculatorTool]
}; };
// First turn - should get thinking and/or tool calls // Collect all text content from all assistant responses
const firstResponse = await llm.complete(context, thinkingOptions); let allTextContent = "";
let hasSeenThinking = false;
// Verify we got either thinking content or tool calls (or both) let hasSeenToolCalls = false;
const hasThinking = firstResponse.thinking !== undefined && firstResponse.thinking.length > 0; const maxTurns = 5; // Prevent infinite loops
const hasToolCalls = firstResponse.toolCalls && firstResponse.toolCalls.length > 0;
expect(hasThinking || hasToolCalls).toBe(true);
// If we got tool calls, verify they're correct
if (hasToolCalls) {
expect(firstResponse.toolCalls).toBeTruthy();
expect(firstResponse.toolCalls!.length).toBeGreaterThan(0);
}
// If we have thinking with tool calls, we should have thinkingSignature for proper multi-turn context
// Note: Some providers may not return thinking when tools are used
if (firstResponse.thinking && hasToolCalls) {
// For now, we'll just check if it exists when both are present
// Some providers may not support thinkingSignature yet
if (firstResponse.thinkingSignature !== undefined) {
expect(firstResponse.thinkingSignature).toBeTruthy();
}
}
// Add the assistant response to context
context.messages.push(firstResponse);
// Process tool calls and add results
for (const toolCall of firstResponse.toolCalls || []) {
expect(toolCall.name).toBe("calculator");
expect(toolCall.id).toBeTruthy();
expect(toolCall.arguments).toBeTruthy();
const { a, b, operation } = toolCall.arguments;
let result: number;
switch (operation) {
case "add": result = a + b; break;
case "multiply": result = a * b; break;
default: result = 0;
}
context.messages.push({
role: "toolResult",
content: `${result}`,
toolCallId: toolCall.id,
isError: false
});
}
// Second turn - complete the conversation
// Keep processing until we get a response with content (not just tool calls)
let finalResponse: AssistantMessage | undefined;
const maxTurns = 3; // Prevent infinite loops
for (let turn = 0; turn < maxTurns; turn++) { for (let turn = 0; turn < maxTurns; turn++) {
const response = await llm.complete(context, thinkingOptions); const response = await llm.complete(context, thinkingOptions);
// Add the assistant response to context
context.messages.push(response); context.messages.push(response);
if (response.stopReason === "stop" && response.content) { // Process content blocks
finalResponse = response; for (const block of response.content) {
break; if (block.type === "text") {
} allTextContent += block.text + " ";
} else if (block.type === "thinking") {
hasSeenThinking = true;
} else if (block.type === "toolCall") {
hasSeenToolCalls = true;
// If we got more tool calls, process them // Process the tool call
if (response.toolCalls) { expect(block.name).toBe("calculator");
for (const toolCall of response.toolCalls) { expect(block.id).toBeTruthy();
const { a, b, operation } = toolCall.arguments; expect(block.arguments).toBeTruthy();
const { a, b, operation } = block.arguments;
let result: number; let result: number;
switch (operation) { switch (operation) {
case "add": result = a + b; break; case "add": result = a + b; break;
@ -250,24 +222,30 @@ async function multiTurn<T extends LLMOptions>(llm: LLM<T>, thinkingOptions: T)
default: result = 0; default: result = 0;
} }
// Add tool result to context
context.messages.push({ context.messages.push({
role: "toolResult", role: "toolResult",
content: `${result}`, content: `${result}`,
toolCallId: toolCall.id, toolCallId: block.id,
isError: false isError: false
}); });
} }
} }
// If we got a stop response with text content, we're likely done
expect(response.stopReason).not.toBe("error");
if (response.stopReason === "stop") {
break;
}
} }
expect(finalResponse).toBeTruthy(); // Verify we got either thinking content or tool calls (or both)
expect(finalResponse!.content).toBeTruthy(); expect(hasSeenThinking || hasSeenToolCalls).toBe(true);
expect(finalResponse!.role).toBe("assistant");
// The final response should reference the calculations // The accumulated text should reference both calculations
expect( expect(allTextContent).toBeTruthy();
finalResponse!.content!.includes("714") || finalResponse!.content!.includes("887") expect(allTextContent.includes("714")).toBe(true);
).toBe(true); expect(allTextContent.includes("887")).toBe(true);
} }
describe("AI Providers E2E Tests", () => { describe("AI Providers E2E Tests", () => {
@ -343,7 +321,7 @@ describe("AI Providers E2E Tests", () => {
}); });
it("should handle thinking mode", async () => { it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"}, false); await handleThinking(llm, {reasoningEffort: "medium"});
}); });
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
@ -407,7 +385,7 @@ describe("AI Providers E2E Tests", () => {
}); });
it("should handle thinking mode", async () => { it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"}, false); await handleThinking(llm, {reasoningEffort: "medium"});
}); });
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
@ -435,7 +413,7 @@ describe("AI Providers E2E Tests", () => {
}); });
it("should handle thinking mode", async () => { it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"}, false); await handleThinking(llm, {reasoningEffort: "medium"});
}); });
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
@ -463,7 +441,7 @@ describe("AI Providers E2E Tests", () => {
}); });
it("should handle thinking mode", async () => { it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"}, false); await handleThinking(llm, {reasoningEffort: "medium"});
}); });
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
@ -491,7 +469,7 @@ describe("AI Providers E2E Tests", () => {
}); });
it("should handle thinking mode", async () => { it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"}, false); await handleThinking(llm, {reasoningEffort: "medium"});
}); });
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
@ -589,7 +567,7 @@ describe("AI Providers E2E Tests", () => {
}); });
it("should handle thinking mode", async () => { it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"}, false); await handleThinking(llm, {reasoningEffort: "medium"});
}); });
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
@ -617,7 +595,7 @@ describe("AI Providers E2E Tests", () => {
}); });
it("should handle thinking mode", async () => { it("should handle thinking mode", async () => {
await handleThinking(llm, {reasoningEffort: "medium"}, false); await handleThinking(llm, {reasoningEffort: "medium"});
}); });
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
@ -644,10 +622,6 @@ describe("AI Providers E2E Tests", () => {
await handleStreaming(llm); await handleStreaming(llm);
}); });
it("should handle thinking mode", async () => {
await handleThinking(llm, {thinking: {enabled: true}}, false);
});
it("should handle multi-turn with thinking and tools", async () => { it("should handle multi-turn with thinking and tools", async () => {
await multiTurn(llm, {thinking: {enabled: true}}); await multiTurn(llm, {thinking: {enabled: true}});
}); });