mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 21:02:02 +00:00
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:
parent
f8a81b9453
commit
f29752ac82
3 changed files with 167 additions and 207 deletions
|
|
@ -6,17 +6,7 @@ import type {
|
|||
Tool,
|
||||
} from "@anthropic-ai/sdk/resources/messages.js";
|
||||
import { calculateCost } from "../models.js";
|
||||
import type {
|
||||
AssistantMessage,
|
||||
Context,
|
||||
LLM,
|
||||
LLMOptions,
|
||||
Message,
|
||||
Model,
|
||||
StopReason,
|
||||
ToolCall,
|
||||
Usage,
|
||||
} from "../types.js";
|
||||
import type { AssistantMessage, Context, LLM, LLMOptions, Message, Model, StopReason, Usage } from "../types.js";
|
||||
|
||||
export interface AnthropicLLMOptions extends LLMOptions {
|
||||
thinking?: {
|
||||
|
|
@ -130,63 +120,65 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
|
|||
);
|
||||
|
||||
let blockType: "text" | "thinking" | "other" = "other";
|
||||
let blockContent = "";
|
||||
for await (const event of stream) {
|
||||
if (event.type === "content_block_start") {
|
||||
if (event.content_block.type === "text") {
|
||||
blockType = "text";
|
||||
blockContent = "";
|
||||
options?.onEvent?.({ type: "text_start" });
|
||||
} else if (event.content_block.type === "thinking") {
|
||||
blockType = "thinking";
|
||||
blockContent = "";
|
||||
options?.onEvent?.({ type: "thinking_start" });
|
||||
} else {
|
||||
blockType = "other";
|
||||
blockContent = "";
|
||||
}
|
||||
}
|
||||
if (event.type === "content_block_delta") {
|
||||
if (event.delta.type === "text_delta") {
|
||||
options?.onText?.(event.delta.text, false);
|
||||
blockType = "text"; // Ensure block type is set
|
||||
options?.onEvent?.({ type: "text_delta", content: blockContent, delta: event.delta.text });
|
||||
blockContent += event.delta.text;
|
||||
}
|
||||
if (event.delta.type === "thinking_delta") {
|
||||
options?.onThinking?.(event.delta.thinking, false);
|
||||
blockType = "thinking"; // Ensure block type is set
|
||||
options?.onEvent?.({ type: "thinking_delta", content: blockContent, delta: event.delta.thinking });
|
||||
blockContent += event.delta.thinking;
|
||||
}
|
||||
}
|
||||
if (event.type === "content_block_stop") {
|
||||
if (blockType === "text") {
|
||||
options?.onText?.("", true);
|
||||
options?.onEvent?.({ type: "text_end", content: blockContent });
|
||||
} else if (blockType === "thinking") {
|
||||
options?.onThinking?.("", true);
|
||||
options?.onEvent?.({ type: "thinking_end", content: blockContent });
|
||||
}
|
||||
blockType = "other";
|
||||
}
|
||||
}
|
||||
const msg = await stream.finalMessage();
|
||||
const thinking = msg.content.some((block) => block.type === "thinking")
|
||||
? msg.content
|
||||
.filter((block) => block.type === "thinking")
|
||||
.map((block) => block.thinking)
|
||||
.join("\n")
|
||||
: undefined;
|
||||
// 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.
|
||||
const thinkingSignature = msg.content.some((block) => block.type === "thinking")
|
||||
? msg.content
|
||||
.filter((block) => block.type === "thinking")
|
||||
.map((block) => block.signature)
|
||||
.join("\n")
|
||||
: undefined;
|
||||
const content = msg.content.some((block) => block.type === "text")
|
||||
? msg.content
|
||||
.filter((block) => block.type === "text")
|
||||
.map((block) => block.text)
|
||||
.join("\n")
|
||||
: 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 blocks: AssistantMessage["content"] = [];
|
||||
for (const block of msg.content) {
|
||||
if (block.type === "text" && block.text) {
|
||||
blocks.push({
|
||||
type: "text",
|
||||
text: block.text,
|
||||
});
|
||||
} else if (block.type === "thinking" && block.thinking) {
|
||||
blocks.push({
|
||||
type: "thinking",
|
||||
thinking: block.thinking,
|
||||
thinkingSignature: block.signature,
|
||||
});
|
||||
} else if (block.type === "tool_use") {
|
||||
blocks.push({
|
||||
type: "toolCall",
|
||||
id: block.id,
|
||||
name: block.name,
|
||||
arguments: block.input as Record<string, any>,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const usage: Usage = {
|
||||
input: msg.usage.input_tokens,
|
||||
output: msg.usage.output_tokens,
|
||||
|
|
@ -204,10 +196,7 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
|
|||
|
||||
return {
|
||||
role: "assistant",
|
||||
content,
|
||||
thinking,
|
||||
thinkingSignature,
|
||||
toolCalls,
|
||||
content: blocks,
|
||||
provider: this.modelInfo.provider,
|
||||
model: this.modelInfo.id,
|
||||
usage,
|
||||
|
|
@ -216,6 +205,7 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
|
|||
} catch (error) {
|
||||
return {
|
||||
role: "assistant",
|
||||
content: [],
|
||||
provider: this.modelInfo.provider,
|
||||
model: this.modelInfo.id,
|
||||
usage: {
|
||||
|
|
@ -270,28 +260,24 @@ export class AnthropicLLM implements LLM<AnthropicLLMOptions> {
|
|||
} else if (msg.role === "assistant") {
|
||||
const blocks: ContentBlockParam[] = [];
|
||||
|
||||
if (msg.thinking && msg.thinkingSignature) {
|
||||
blocks.push({
|
||||
type: "thinking",
|
||||
thinking: msg.thinking,
|
||||
signature: msg.thinkingSignature,
|
||||
});
|
||||
}
|
||||
|
||||
if (msg.content) {
|
||||
blocks.push({
|
||||
type: "text",
|
||||
text: msg.content,
|
||||
});
|
||||
}
|
||||
|
||||
if (msg.toolCalls) {
|
||||
for (const toolCall of msg.toolCalls) {
|
||||
for (const block of msg.content) {
|
||||
if (block.type === "text") {
|
||||
blocks.push({
|
||||
type: "text",
|
||||
text: block.text,
|
||||
});
|
||||
} else if (block.type === "thinking") {
|
||||
blocks.push({
|
||||
type: "thinking",
|
||||
thinking: block.thinking,
|
||||
signature: block.thinkingSignature || "",
|
||||
});
|
||||
} else if (block.type === "toolCall") {
|
||||
blocks.push({
|
||||
type: "tool_use",
|
||||
id: toolCall.id,
|
||||
name: toolCall.name,
|
||||
input: toolCall.arguments,
|
||||
id: block.id,
|
||||
name: block.name,
|
||||
input: block.arguments,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
export interface LLMOptions {
|
||||
temperature?: number;
|
||||
maxTokens?: number;
|
||||
onText?: (text: string, complete: boolean) => void;
|
||||
onThinking?: (thinking: string, complete: boolean) => void;
|
||||
onEvent?: (event: AssistantMessageEvent) => void;
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
|
|
@ -14,6 +13,13 @@ export interface LLM<T extends LLMOptions> {
|
|||
export interface TextContent {
|
||||
type: "text";
|
||||
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 {
|
||||
|
|
@ -22,6 +28,29 @@ export interface ImageContent {
|
|||
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 {
|
||||
role: "user";
|
||||
content: string | (TextContent | ImageContent)[];
|
||||
|
|
@ -29,18 +58,7 @@ export interface UserMessage {
|
|||
|
||||
export interface AssistantMessage {
|
||||
role: "assistant";
|
||||
thinking?: string;
|
||||
// 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>;
|
||||
}[];
|
||||
content: (TextContent | ThinkingContent | ToolCall)[];
|
||||
provider: string;
|
||||
model: string;
|
||||
usage: Usage;
|
||||
|
|
@ -70,37 +88,19 @@ export interface Context {
|
|||
tools?: Tool[];
|
||||
}
|
||||
|
||||
export type Event =
|
||||
export type AssistantMessageEvent =
|
||||
| { type: "start"; model: string; provider: string }
|
||||
| { type: "text"; content: string; delta: string }
|
||||
| { type: "thinking"; content: string; delta: string }
|
||||
| { type: "text_start" }
|
||||
| { 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: "usage"; usage: Usage }
|
||||
| { type: "done"; reason: StopReason; message: AssistantMessage }
|
||||
| { 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
|
||||
export interface Model {
|
||||
id: string;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue