A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

9 downloads

dbbasic-browser/apps/browser/src/chat-models.ts
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 toolbar: Back and Forward are greyed out when there's nowhere to go cc08c7a 1 week, 2 days ago History
chat-models.ts · 6.5 KB · 130 lines
1/**
2 * Model adapters for the chat: Anthropic (Messages API with tool use) and anything that speaks the OpenAI chat
3 * API, which includes a model running on this machine or on your network (set DBB_AI_BASE_URL). Both turn the
4 * chat's provider-neutral messages into the provider's shape and back. Main process only (it holds API keys).
5 * Replies arrive whole; the loop shows each tool step as it happens.
6 */
7
8import Anthropic from "@anthropic-ai/sdk";
9import OpenAI from "openai";
10import type { AiConfig } from "./ai-config.js";
11import type { ChatMsg, ModelClient, ModelTurn, ToolCall, ToolSpec } from "./chat-core.js";
12
13const MAX_TOKENS = 4096;
14
15export interface AnthropicChatLike {
16 messages: { create(params: Record<string, unknown>, opts?: { signal?: AbortSignal }): Promise<AnthropicChatResponse> };
17}
18export interface AnthropicChatResponse {
19 content: Array<{ type: string; text?: string; id?: string; name?: string; input?: unknown }>;
20 stop_reason?: string | null;
21 usage?: { input_tokens?: number; output_tokens?: number };
22}
23export interface OpenAIChatLike {
24 chat: { completions: { create(params: Record<string, unknown>, opts?: { signal?: AbortSignal }): Promise<OpenAIChatResponse> } };
25}
26export interface OpenAIChatResponse {
27 choices: Array<{
28 message?: { content?: string | null; refusal?: string | null; tool_calls?: Array<{ id: string; type?: string; function: { name: string; arguments: string } }> };
29 finish_reason?: string | null;
30 }>;
31 usage?: { prompt_tokens?: number; completion_tokens?: number };
32}
33
34const asObject = (v: unknown): Record<string, unknown> => (v && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {});
35
36// ---- Anthropic --------------------------------------------------------------------------------------
37
38export function toAnthropicMessages(messages: ChatMsg[]): Array<Record<string, unknown>> {
39 return messages.map((m) => {
40 if (m.role === "user") return { role: "user", content: m.text };
41 if (m.role === "assistant") {
42 const content: Array<Record<string, unknown>> = [];
43 if (m.text.trim()) content.push({ type: "text", text: m.text });
44 for (const c of m.calls) content.push({ type: "tool_use", id: c.id, name: c.name, input: c.input });
45 return { role: "assistant", content: content.length ? content : [{ type: "text", text: "(no reply)" }] };
46 }
47 return { role: "user", content: m.results.map((r) => ({ type: "tool_result", tool_use_id: r.id, content: r.text, ...(r.isError ? { is_error: true } : {}) })) };
48 });
49}
50
51export function anthropicClient(cfg: AiConfig, inject?: AnthropicChatLike): ModelClient {
52 const client = (): AnthropicChatLike => {
53 if (inject) return inject;
54 if (!cfg.keys.anthropic) throw new Error("no Anthropic API key is configured");
55 return new Anthropic({ apiKey: cfg.keys.anthropic, timeout: 90_000, maxRetries: 1 }) as unknown as AnthropicChatLike;
56 };
57 return {
58 provider: "anthropic",
59 model: cfg.model,
60 async turn({ system, messages, tools, signal }): Promise<ModelTurn> {
61 const res = await client().messages.create(
62 { model: cfg.model, max_tokens: MAX_TOKENS, system, messages: toAnthropicMessages(messages), tools: tools.map(toAnthropicTool) },
63 signal ? { signal } : undefined,
64 );
65 const text = res.content.filter((b) => b.type === "text").map((b) => b.text ?? "").join("");
66 const calls: ToolCall[] = res.content.filter((b) => b.type === "tool_use").map((b) => ({ id: String(b.id), name: String(b.name), input: asObject(b.input) }));
67 const stop = res.stop_reason === "refusal" ? "refusal" : res.stop_reason === "max_tokens" ? "length" : calls.length ? "tools" : "end";
68 return { text, calls, stop, usage: { inputTokens: res.usage?.input_tokens ?? 0, outputTokens: res.usage?.output_tokens ?? 0 } };
69 },
70 };
71}
72const toAnthropicTool = (t: ToolSpec) => ({ name: t.name, description: t.description, input_schema: t.input_schema });
73
74// ---- OpenAI and compatible --------------------------------------------------------------------------
75
76export function toOpenAIMessages(system: string, messages: ChatMsg[]): Array<Record<string, unknown>> {
77 const out: Array<Record<string, unknown>> = [{ role: "system", content: system }];
78 for (const m of messages) {
79 if (m.role === "user") out.push({ role: "user", content: m.text });
80 else if (m.role === "assistant") {
81 out.push({
82 role: "assistant",
83 content: m.text || null,
84 ...(m.calls.length ? { tool_calls: m.calls.map((c) => ({ id: c.id, type: "function", function: { name: c.name, arguments: JSON.stringify(c.input) } })) } : {}),
85 });
86 } else for (const r of m.results) out.push({ role: "tool", tool_call_id: r.id, content: r.text });
87 }
88 return out;
89}
90
91export function openaiClient(cfg: AiConfig, inject?: OpenAIChatLike): ModelClient {
92 const client = (): OpenAIChatLike => {
93 if (inject) return inject;
94 const key = cfg.keys.openai ?? (cfg.baseUrl ? "none" : undefined);
95 if (!key) throw new Error("no OpenAI API key is configured");
96 return new OpenAI({ apiKey: key, ...(cfg.baseUrl ? { baseURL: cfg.baseUrl } : {}), timeout: 120_000, maxRetries: 1 }) as unknown as OpenAIChatLike;
97 };
98 return {
99 provider: "openai",
100 model: cfg.model,
101 async turn({ system, messages, tools, signal }): Promise<ModelTurn> {
102 const res = await client().chat.completions.create(
103 {
104 model: cfg.model,
105 max_completion_tokens: MAX_TOKENS,
106 messages: toOpenAIMessages(system, messages),
107 tools: tools.map((t) => ({ type: "function", function: { name: t.name, description: t.description, parameters: t.input_schema } })),
108 },
109 signal ? { signal } : undefined,
110 );
111 const choice = res.choices[0];
112 const msg = choice?.message;
113 const calls: ToolCall[] = (msg?.tool_calls ?? []).map((c) => {
114 let input: Record<string, unknown> = {};
115 try {
116 input = asObject(JSON.parse(c.function.arguments || "{}"));
117 } catch {
118 /* the model sent broken arguments: the tool will say what is missing */
119 }
120 return { id: c.id, name: c.function.name, input };
121 });
122 const stop = msg?.refusal ? "refusal" : choice?.finish_reason === "length" ? "length" : calls.length ? "tools" : "end";
123 return { text: msg?.content ?? msg?.refusal ?? "", calls, stop, usage: { inputTokens: res.usage?.prompt_tokens ?? 0, outputTokens: res.usage?.completion_tokens ?? 0 } };
124 },
125 };
126}
127
128export function chatModel(cfg: AiConfig): ModelClient {
129 return cfg.provider === "anthropic" ? anthropicClient(cfg) : openaiClient(cfg);
130}