forked from Blendman974/faceclaw
Add a model picker and support for the OpenAI API
This commit is contained in:
1 parent
1c2e445519
commit
ccdeedd687
13 files changed
+688
-131
No files matched your search
@@ -1,10 +1,13 @@
|
||||
import {
|
||||
streamAnthropicMessage,
|
||||
type AnthropicContentBlock,
|
||||
type AnthropicMessage,
|
||||
type AnthropicStreamHandle,
|
||||
type AnthropicToolDefinition,
|
||||
} from "../native/anthropic";
|
||||
import { streamAnthropicMessage } from "../native/anthropic";
|
||||
import { streamOpenAiResponse } from "../native/openai";
|
||||
import type {
|
||||
LlmContentBlock,
|
||||
LlmMessage,
|
||||
LlmStreamHandle,
|
||||
LlmStreamOptions,
|
||||
LlmToolDefinition,
|
||||
} from "./llm-protocol";
|
||||
import type { AssistantProvider } from "./models";
|
||||
import type { ToolRegistry } from "./tool-registry";
|
||||
import type { AssistantTurnCallbacks, AssistantTurnHandle } from "./types";
|
||||
|
||||
@@ -20,29 +23,30 @@ const MAX_ITERATIONS = 8;
|
||||
const MAX_TURN_MS = 2 * 60 * 1000;
|
||||
|
||||
export type DirectTurnOptions = {
|
||||
provider: AssistantProvider;
|
||||
apiKey: string;
|
||||
model: string;
|
||||
effort?: "low" | "medium" | "high";
|
||||
system: string;
|
||||
/** Owned by the session; this loop appends assistant + tool_result messages. */
|
||||
messages: AnthropicMessage[];
|
||||
messages: LlmMessage[];
|
||||
/**
|
||||
* 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[];
|
||||
buildTools: () => LlmToolDefinition[];
|
||||
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 {
|
||||
export class DirectAssistantBackend {
|
||||
runTurn(options: DirectTurnOptions): AssistantTurnHandle {
|
||||
const startMs = Date.now();
|
||||
let cancelled = false;
|
||||
let streamHandle: AnthropicStreamHandle | null = null;
|
||||
let streamHandle: LlmStreamHandle | null = null;
|
||||
|
||||
const finishError = (message: string) => {
|
||||
if (cancelled) return;
|
||||
@@ -61,7 +65,7 @@ export class DirectAnthropicBackend {
|
||||
return;
|
||||
}
|
||||
|
||||
streamHandle = streamAnthropicMessage({
|
||||
const streamOptions: LlmStreamOptions = {
|
||||
apiKey: options.apiKey,
|
||||
model: options.model,
|
||||
system: options.system,
|
||||
@@ -80,13 +84,13 @@ export class DirectAnthropicBackend {
|
||||
return;
|
||||
}
|
||||
// Record what the model produced this iteration.
|
||||
const assistantContent: AnthropicContentBlock[] = result.content.length
|
||||
const assistantContent: LlmContentBlock[] = 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): block is Extract<LlmContentBlock, { type: "tool_use" }> =>
|
||||
block.type === "tool_use",
|
||||
);
|
||||
if (result.stopReason !== "tool_use" || toolUses.length === 0) {
|
||||
@@ -110,7 +114,10 @@ export class DirectAnthropicBackend {
|
||||
streamHandle = null;
|
||||
finishError(message);
|
||||
},
|
||||
});
|
||||
};
|
||||
streamHandle = options.provider === "openai"
|
||||
? streamOpenAiResponse(streamOptions)
|
||||
: streamAnthropicMessage(streamOptions);
|
||||
};
|
||||
|
||||
runIteration(0);
|
||||
@@ -125,10 +132,10 @@ export class DirectAnthropicBackend {
|
||||
}
|
||||
|
||||
private async runToolCalls(
|
||||
toolUses: Array<Extract<AnthropicContentBlock, { type: "tool_use" }>>,
|
||||
toolUses: Array<Extract<LlmContentBlock, { type: "tool_use" }>>,
|
||||
options: DirectTurnOptions,
|
||||
): Promise<AnthropicContentBlock[]> {
|
||||
const results: AnthropicContentBlock[] = [];
|
||||
): Promise<LlmContentBlock[]> {
|
||||
const results: LlmContentBlock[] = [];
|
||||
for (const toolUse of toolUses) {
|
||||
const canonicalName = options.resolveToolName(toolUse.name);
|
||||
options.callbacks.onToolActivity(canonicalName);
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Provider-neutral shapes used by the direct assistant loop. Each native LLM
|
||||
* client translates these messages and tools to its provider's wire format.
|
||||
*/
|
||||
|
||||
export type LlmContentBlock =
|
||||
| { type: "text"; text: string }
|
||||
| {
|
||||
type: "tool_use";
|
||||
id: string;
|
||||
name: string;
|
||||
input: any;
|
||||
/** Exact provider output item to replay during stateless continuation. */
|
||||
provider_item?: { provider: "openai"; item: any };
|
||||
}
|
||||
| { type: "tool_result"; tool_use_id: string; content: string; is_error?: boolean }
|
||||
/** Opaque state required to continue a provider's stateless conversation. */
|
||||
| { type: "provider_item"; provider: "openai"; item: any };
|
||||
|
||||
export type LlmMessage = {
|
||||
role: "user" | "assistant";
|
||||
content: string | LlmContentBlock[];
|
||||
};
|
||||
|
||||
export type LlmToolDefinition = {
|
||||
name: string;
|
||||
description: string;
|
||||
input_schema: object;
|
||||
};
|
||||
|
||||
export type LlmStreamResult = {
|
||||
/** Concatenated text of all text blocks. */
|
||||
text: string;
|
||||
/** The assembled assistant content blocks (text + any tool calls). */
|
||||
content: LlmContentBlock[];
|
||||
/** Provider-normalized stop reason: end_turn, tool_use, refusal, etc. */
|
||||
stopReason: string | null;
|
||||
};
|
||||
|
||||
export type LlmStreamOptions = {
|
||||
apiKey: string;
|
||||
model: string;
|
||||
system?: string;
|
||||
messages: LlmMessage[];
|
||||
tools?: LlmToolDefinition[];
|
||||
maxTokens?: number;
|
||||
/** Thinking/latency tradeoff; voice flows want "low". */
|
||||
effort?: "low" | "medium" | "high";
|
||||
onTextDelta?: (delta: string, textSoFar: string) => void;
|
||||
onDone: (result: LlmStreamResult) => void;
|
||||
onError: (message: string) => void;
|
||||
};
|
||||
|
||||
export type LlmStreamHandle = {
|
||||
cancel(): void;
|
||||
};
|
||||
@@ -0,0 +1,88 @@
|
||||
export const ASSISTANT_MODEL_VALUES = [
|
||||
"auto",
|
||||
"hauku",
|
||||
"sonnet",
|
||||
"opus",
|
||||
"fable",
|
||||
"luna",
|
||||
"terra",
|
||||
"sol",
|
||||
] as const;
|
||||
|
||||
export type AssistantModel = (typeof ASSISTANT_MODEL_VALUES)[number];
|
||||
export type AssistantProvider = "anthropic" | "openai";
|
||||
|
||||
export type AssistantApiKeys = {
|
||||
anthropic: string;
|
||||
openai: string;
|
||||
};
|
||||
|
||||
export type ResolvedAssistantModel = {
|
||||
selection: AssistantModel;
|
||||
provider: AssistantProvider;
|
||||
model: string;
|
||||
apiKey: string;
|
||||
effort?: "low" | "medium" | "high";
|
||||
};
|
||||
|
||||
type ModelDefinition = {
|
||||
label: string;
|
||||
provider: AssistantProvider;
|
||||
model: string;
|
||||
effort?: "low" | "medium" | "high";
|
||||
};
|
||||
|
||||
const MODEL_DEFINITIONS: Record<Exclude<AssistantModel, "auto">, ModelDefinition> = {
|
||||
// Keep the user-facing spelling requested for the picker while using
|
||||
// Anthropic's canonical Haiku alias on the wire.
|
||||
hauku: { label: "Hauku", provider: "anthropic", model: "claude-haiku-4-5" },
|
||||
sonnet: { label: "Sonnet", provider: "anthropic", model: "claude-sonnet-5", effort: "low" },
|
||||
opus: { label: "Opus", provider: "anthropic", model: "claude-opus-5", effort: "low" },
|
||||
fable: { label: "Fable", provider: "anthropic", model: "claude-fable-5", effort: "low" },
|
||||
luna: { label: "Luna", provider: "openai", model: "gpt-5.6-luna", effort: "low" },
|
||||
terra: { label: "Terra", provider: "openai", model: "gpt-5.6-terra", effort: "low" },
|
||||
sol: { label: "Sol", provider: "openai", model: "gpt-5.6-sol", effort: "low" },
|
||||
};
|
||||
|
||||
export function assistantModelLabel(value: AssistantModel): string {
|
||||
return value === "auto" ? "Auto" : MODEL_DEFINITIONS[value].label;
|
||||
}
|
||||
|
||||
export function assistantModelProvider(value: AssistantModel): AssistantProvider | null {
|
||||
return value === "auto" ? null : MODEL_DEFINITIONS[value].provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve Auto and attach the credential needed by the chosen provider.
|
||||
* Auto intentionally prefers Terra over Sonnet when both keys are available.
|
||||
*/
|
||||
export function resolveAssistantModel(
|
||||
selection: AssistantModel,
|
||||
keys: AssistantApiKeys,
|
||||
): ResolvedAssistantModel | null {
|
||||
const normalizedKeys: AssistantApiKeys = {
|
||||
anthropic: keys.anthropic.trim(),
|
||||
openai: keys.openai.trim(),
|
||||
};
|
||||
let resolvedSelection = selection;
|
||||
if (selection === "auto") {
|
||||
if (normalizedKeys.openai) {
|
||||
resolvedSelection = "terra";
|
||||
} else if (normalizedKeys.anthropic) {
|
||||
resolvedSelection = "sonnet";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const definition = MODEL_DEFINITIONS[resolvedSelection as Exclude<AssistantModel, "auto">];
|
||||
const apiKey = normalizedKeys[definition.provider];
|
||||
if (!apiKey) return null;
|
||||
return {
|
||||
selection,
|
||||
provider: definition.provider,
|
||||
model: definition.model,
|
||||
apiKey,
|
||||
effort: definition.effort,
|
||||
};
|
||||
}
|
||||
+24
-15
@@ -1,12 +1,13 @@
|
||||
import { DEFAULT_LLM_MODEL, type AnthropicMessage, type AnthropicToolDefinition } from "../native/anthropic";
|
||||
import { buildAssistantSystemPrompt } from "../prompts";
|
||||
import { DirectAnthropicBackend } from "./direct-backend";
|
||||
import { DirectAssistantBackend } from "./direct-backend";
|
||||
import type { LlmMessage, LlmToolDefinition } from "./llm-protocol";
|
||||
import type { ResolvedAssistantModel } from "./models";
|
||||
import { toolRegistry, type ToolRegistry } from "./tool-registry";
|
||||
import type { AssistantContext, AssistantTurnCallbacks, AssistantTurnHandle } from "./types";
|
||||
|
||||
/**
|
||||
* One assistant conversation. Owns the message history and turn state and
|
||||
* drives a backend (direct Anthropic loop for phase 1). The UI talks only to
|
||||
* drives a direct provider backend. The UI talks only to
|
||||
* this surface, so swapping in the external-agent backend later doesn't touch
|
||||
* AssistantLayer.
|
||||
*
|
||||
@@ -21,20 +22,27 @@ const SESSION_IDLE_MS = 10 * 60 * 1000;
|
||||
const MAX_HISTORY_MESSAGES = 40;
|
||||
|
||||
export class AssistantSession {
|
||||
private readonly backend = new DirectAnthropicBackend();
|
||||
private readonly messages: AnthropicMessage[] = [];
|
||||
private readonly backend = new DirectAssistantBackend();
|
||||
private readonly messages: LlmMessage[] = [];
|
||||
private turnHandle: AssistantTurnHandle | null = null;
|
||||
private lastActivityMs = Date.now();
|
||||
// API-safe tool name -> canonical registry name. The Anthropic API only
|
||||
// allows [a-zA-Z0-9_-] in tool names, so dotted registry names ("glasses.
|
||||
// get_state") are sanitized for the request and mapped back on tool calls.
|
||||
// API-safe tool name -> canonical registry name. Provider APIs restrict tool
|
||||
// names, so dotted registry names are sanitized and mapped back on calls.
|
||||
private readonly toolNameMap = new Map<string, string>();
|
||||
|
||||
constructor(
|
||||
private readonly apiKey: string,
|
||||
private readonly llm: ResolvedAssistantModel,
|
||||
private readonly registry: ToolRegistry = toolRegistry,
|
||||
) {}
|
||||
|
||||
matchesConfiguration(llm: ResolvedAssistantModel): boolean {
|
||||
return (
|
||||
this.llm.provider === llm.provider &&
|
||||
this.llm.model === llm.model &&
|
||||
this.llm.apiKey === llm.apiKey
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether this session has been idle long enough to retire. */
|
||||
isExpired(nowMs = Date.now()): boolean {
|
||||
return nowMs - this.lastActivityMs > SESSION_IDLE_MS;
|
||||
@@ -62,9 +70,10 @@ export class AssistantSession {
|
||||
};
|
||||
|
||||
this.turnHandle = this.backend.runTurn({
|
||||
apiKey: this.apiKey,
|
||||
model: DEFAULT_LLM_MODEL,
|
||||
effort: "low",
|
||||
provider: this.llm.provider,
|
||||
apiKey: this.llm.apiKey,
|
||||
model: this.llm.model,
|
||||
effort: this.llm.effort,
|
||||
system,
|
||||
messages: this.messages,
|
||||
buildTools: () => this.buildToolDefinitions(),
|
||||
@@ -91,7 +100,7 @@ export class AssistantSession {
|
||||
this.lastActivityMs = Date.now();
|
||||
}
|
||||
|
||||
private buildToolDefinitions(): AnthropicToolDefinition[] {
|
||||
private buildToolDefinitions(): LlmToolDefinition[] {
|
||||
this.toolNameMap.clear();
|
||||
return this.registry.listTools().map((spec) => {
|
||||
const apiName = this.toApiToolName(spec.name);
|
||||
@@ -100,7 +109,7 @@ export class AssistantSession {
|
||||
});
|
||||
}
|
||||
|
||||
/** Convert a canonical registry name to an Anthropic-legal tool name. */
|
||||
/** Convert a canonical registry name to a provider-legal tool name. */
|
||||
private toApiToolName(name: string): string {
|
||||
let base = name.replace(/[^a-zA-Z0-9_-]/g, "_");
|
||||
// Disambiguate the rare case where two canonical names sanitize alike.
|
||||
@@ -125,7 +134,7 @@ export class AssistantSession {
|
||||
}
|
||||
}
|
||||
|
||||
function startsWithToolResult(message: AnthropicMessage): boolean {
|
||||
function startsWithToolResult(message: LlmMessage): boolean {
|
||||
return (
|
||||
Array.isArray(message.content) &&
|
||||
message.content.length > 0 &&
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* The assistant's tool registry: the single place that knows which tools exist,
|
||||
* whether each is currently callable, and how to invoke it. Both assistant
|
||||
* backends (direct Anthropic loop, external agent bridge) list and call tools
|
||||
* backends (direct provider loop, external agent bridge) list and call tools
|
||||
* through here, so gating and timeouts are enforced in one spot rather than
|
||||
* trusted to a model or a remote agent.
|
||||
*
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Shared assistant types. Kept backend-agnostic so the UI (AssistantLayer)
|
||||
* talks to the same session surface whether the turn runs on the phone
|
||||
* (direct Anthropic loop) or against the user's own agent (external bridge,
|
||||
* (direct provider loop) or against the user's own agent (external bridge,
|
||||
* later phases).
|
||||
*/
|
||||
|
||||
|
||||
Reference in new issue
Block a user