Files
faceclaw/app/assistant/direct-backend.ts
T

147 lines
5.1 KiB
TypeScript

import {
streamAnthropicMessage,
type AnthropicContentBlock,
type AnthropicMessage,
type AnthropicStreamHandle,
type AnthropicToolDefinition,
} from "../native/anthropic";
import type { ToolRegistry } from "./tool-registry";
import type { AssistantTurnCallbacks, AssistantTurnHandle } from "./types";
/**
* The on-phone agent loop: stream a reply, and while the model asks for tools,
* execute them through the registry and feed the results back until it stops.
* History lives in the AssistantSession; this backend appends the assistant
* and tool-result messages to the array it is handed.
*/
/** Safety caps so a tool-thrashing model can't loop or run forever. */
const MAX_ITERATIONS = 8;
const MAX_TURN_MS = 2 * 60 * 1000;
export type DirectTurnOptions = {
apiKey: string;
model: string;
effort?: "low" | "medium" | "high";
system: string;
/** Owned by the session; this loop appends assistant + tool_result messages. */
messages: AnthropicMessage[];
/**
* Build the tool list for the next request. Called once per loop iteration so
* the set reflects apps opening/closing and foreground changes mid-turn; it
* also refreshes the session's api-name map that resolveToolName reads.
*/
buildTools: () => AnthropicToolDefinition[];
registry: ToolRegistry;
/** Map an API tool name (as the model calls it) back to its registry name. */
resolveToolName: (apiName: string) => string;
callbacks: AssistantTurnCallbacks;
};
export class DirectAnthropicBackend {
runTurn(options: DirectTurnOptions): AssistantTurnHandle {
const startMs = Date.now();
let cancelled = false;
let streamHandle: AnthropicStreamHandle | null = null;
const finishError = (message: string) => {
if (cancelled) return;
cancelled = true;
options.callbacks.onError(message);
};
const runIteration = (iteration: number): void => {
if (cancelled) return;
if (iteration >= MAX_ITERATIONS) {
finishError("Assistant stopped: too many tool steps");
return;
}
if (Date.now() - startMs > MAX_TURN_MS) {
finishError("Assistant stopped: turn took too long");
return;
}
streamHandle = streamAnthropicMessage({
apiKey: options.apiKey,
model: options.model,
system: options.system,
effort: options.effort,
messages: options.messages,
tools: options.buildTools(),
onTextDelta: (delta, textSoFar) => {
if (cancelled) return;
options.callbacks.onTextDelta(delta, textSoFar);
},
onDone: (result) => {
streamHandle = null;
if (cancelled) return;
if (result.stopReason === "refusal") {
finishError("The assistant declined this request");
return;
}
// Record what the model produced this iteration.
const assistantContent: AnthropicContentBlock[] = result.content.length
? result.content
: [{ type: "text", text: result.text }];
options.messages.push({ role: "assistant", content: assistantContent });
const toolUses = assistantContent.filter(
(block): block is Extract<AnthropicContentBlock, { type: "tool_use" }> =>
block.type === "tool_use",
);
if (result.stopReason !== "tool_use" || toolUses.length === 0) {
options.callbacks.onTurnDone({ stopReason: result.stopReason });
return;
}
void this.runToolCalls(toolUses, options)
.then((toolResults) => {
// Always record the results, even if cancelled mid-flight: an
// assistant tool_use with no matching tool_result would make the
// next turn's request invalid. Only the loop continuation is gated.
options.messages.push({ role: "user", content: toolResults });
if (cancelled) return;
runIteration(iteration + 1);
})
.catch((error) => {
finishError(`Tool execution failed: ${String((error as Error)?.message ?? error)}`);
});
},
onError: (message) => {
streamHandle = null;
finishError(message);
},
});
};
runIteration(0);
return {
cancel: () => {
cancelled = true;
streamHandle?.cancel();
streamHandle = null;
},
};
}
private async runToolCalls(
toolUses: Array<Extract<AnthropicContentBlock, { type: "tool_use" }>>,
options: DirectTurnOptions,
): Promise<AnthropicContentBlock[]> {
const results: AnthropicContentBlock[] = [];
for (const toolUse of toolUses) {
const canonicalName = options.resolveToolName(toolUse.name);
options.callbacks.onToolActivity(canonicalName);
const result = await options.registry.callTool(canonicalName, toolUse.input);
const content = result.ok ? result.content ?? "" : result.error ?? "Tool error";
results.push({
type: "tool_result",
tool_use_id: toolUse.id,
content,
is_error: !result.ok,
});
}
return results;
}
}