askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
chat-service.ts · 11.6 KB · 255 lines
| 1 | /** |
| 2 | * The chat, main-process side: one conversation, one model, the page tools, and the person's say over every action. |
| 3 | * |
| 4 | * Privacy and safety rules (the point of the design): |
| 5 | * - Nothing goes to a model until the person sends a message, and the first message per model each session asks |
| 6 | * first, naming the model and where it is (a provider, or a machine on your network). DBB_AI_CONSENT=1 |
| 7 | * pre-answers yes, for scripted runs. |
| 8 | * - What the assistant reads from pages goes to the model; the panel says so for every read, with how much. Field lists |
| 9 | * leave out password, card and code values, but visible page text is visible to it, and the consent question says so. |
| 10 | * - Every action is announced; by default each one waits for a yes ("ask"). In "auto" only the risky ones do. |
| 11 | * - Off while the caller network is on (that mode makes no requests of its own, and a model call is one). |
| 12 | * - API keys stay in this process; the panel only ever sees provider and model names. |
| 13 | */ |
| 14 | |
| 15 | import { estimateCostUsd } from "./ai.js"; |
| 16 | import { describeAi, redact, type AiConfig } from "./ai-config.js"; |
| 17 | import { MAX_RESULT_CHARS, newBoundary, runChat, wrapResult, type ChatEvent, type ChatMsg, type ModelClient, type Policy, type ToolDef } from "./chat-core.js"; |
| 18 | import { chatAi, chatModelChoices } from "./chat-model-choice.js"; |
| 19 | import { chatModel } from "./chat-models.js"; |
| 20 | import { pageTools, type Runner } from "./chat-tools.js"; |
| 21 | |
| 22 | export type PanelEvent = |
| 23 | | ChatEvent |
| 24 | | { kind: "user"; text: string } |
| 25 | | { kind: "confirm"; id: string; name: string; summary: string; danger: boolean } |
| 26 | | { kind: "confirm-settled"; id: string; ok: boolean } |
| 27 | | { kind: "cleared" } |
| 28 | /** A command the person ran themselves (from the address bar), and what came back. */ |
| 29 | | { kind: "command"; typed: string; tool: string; readOnly: boolean; sentence: string; page?: string; items?: Array<{ title: string; page?: string }>; details: string }; |
| 30 | export type LoggedEvent = PanelEvent & { seq: number }; |
| 31 | |
| 32 | export interface ChatDeps { |
| 33 | getAi(): AiConfig | null; |
| 34 | /** Run a browser automation method (tabs.*, page.*) in this process. */ |
| 35 | run: Runner; |
| 36 | /** Send to the chrome UI. */ |
| 37 | toPanel(payload: LoggedEvent): void; |
| 38 | /** For the browser's event log: one line per finished run, never silent. */ |
| 39 | announce(payload: Record<string, unknown>): void; |
| 40 | callerNetworkOn(): boolean; |
| 41 | /** Ask the person whether the assistant may send what it reads to this model. */ |
| 42 | consent(what: { provider: string; model: string; where: string }): Promise<boolean>; |
| 43 | /** Tools from connected services (see mcp-service.ts), asked for at the start of each message. */ |
| 44 | extraTools?(): Promise<ToolDef[]>; |
| 45 | /** Tests and tools can supply the model. */ |
| 46 | modelFor?(cfg: AiConfig): ModelClient; |
| 47 | /** Where the model picked in the panel is remembered ("provider/model"). */ |
| 48 | modelChoice?: { load(): string | null; save(id: string): void }; |
| 49 | } |
| 50 | |
| 51 | export interface SendResult { |
| 52 | ok: boolean; |
| 53 | reason?: "caller-network-on" | "no-ai" | "busy" | "declined" | "empty"; |
| 54 | message?: string; |
| 55 | /** How the run ended, when it ran. */ |
| 56 | ended?: Extract<ChatEvent, { kind: "done" }>["reason"]; |
| 57 | reply?: string; |
| 58 | events?: LoggedEvent[]; |
| 59 | } |
| 60 | |
| 61 | const MAX_LOG = 500; |
| 62 | |
| 63 | export class ChatService { |
| 64 | private history: ChatMsg[] = []; |
| 65 | private log: LoggedEvent[] = []; |
| 66 | private seq = 0; |
| 67 | private running: AbortController | null = null; |
| 68 | private pending = new Map<string, (ok: boolean) => void>(); |
| 69 | private consented = new Set<string>(); |
| 70 | /** A random word for this conversation's untrusted tags; a new conversation gets a new one. */ |
| 71 | private boundary = newBoundary(); |
| 72 | mode: Policy["mode"] = "ask"; |
| 73 | /** The conversation has read private data from the person's accounts, so actions that could carry it out always ask. */ |
| 74 | private tainted = false; |
| 75 | /** Records of commands run while the assistant was busy, added to the conversation when it is done. */ |
| 76 | private queued: string[] = []; |
| 77 | |
| 78 | /** The model picked in the panel, "provider/model", or null for the default. */ |
| 79 | private choice: string | null; |
| 80 | |
| 81 | constructor(private deps: ChatDeps) { |
| 82 | this.choice = deps.modelChoice?.load() ?? null; |
| 83 | } |
| 84 | |
| 85 | /** The AI config the chat runs with: the env file's, with the model picked in the panel. */ |
| 86 | private ai(): AiConfig | null { |
| 87 | return chatAi(this.deps.getAi(), this.choice); |
| 88 | } |
| 89 | |
| 90 | /** Pick the chat's model ("provider/model", one of those on offer). Takes effect from the next message; remembered. */ |
| 91 | setModel(id: unknown): { ok: boolean; model: string | null } { |
| 92 | const c = chatModelChoices(this.deps.getAi()).find((x) => x.id === id); |
| 93 | if (!c) return { ok: false, model: this.ai()?.model ?? null }; |
| 94 | this.choice = c.id; |
| 95 | this.deps.modelChoice?.save(c.id); |
| 96 | return { ok: true, model: c.model }; |
| 97 | } |
| 98 | |
| 99 | private push(e: PanelEvent): LoggedEvent { |
| 100 | const le = { ...e, seq: ++this.seq } as LoggedEvent; |
| 101 | this.log.push(le); |
| 102 | if (this.log.length > MAX_LOG) this.log.splice(0, this.log.length - MAX_LOG); |
| 103 | this.deps.toPanel(le); |
| 104 | return le; |
| 105 | } |
| 106 | |
| 107 | state() { |
| 108 | const cfg = this.ai(); |
| 109 | const ai = describeAi(cfg); |
| 110 | return { |
| 111 | available: ai.configured && !this.deps.callerNetworkOn(), |
| 112 | callerNetworkOn: this.deps.callerNetworkOn(), |
| 113 | provider: ai.provider ?? null, |
| 114 | model: ai.model ?? null, |
| 115 | modelId: cfg ? `${cfg.provider}/${cfg.model}` : null, |
| 116 | models: chatModelChoices(this.deps.getAi()).map(({ id, label }) => ({ id, label })), |
| 117 | endpoint: ai.endpoint ?? null, |
| 118 | mode: this.mode, |
| 119 | running: this.running !== null, |
| 120 | pending: [...this.pending.keys()], |
| 121 | transcript: this.log.filter((e) => e.kind !== "usage"), |
| 122 | seq: this.seq, |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | eventsSince(n: number): { events: LoggedEvent[]; next: number } { |
| 127 | return { events: this.log.filter((e) => e.seq > n), next: this.seq }; |
| 128 | } |
| 129 | |
| 130 | setMode(m: unknown): Policy["mode"] { |
| 131 | if (m === "ask" || m === "auto") this.mode = m; |
| 132 | return this.mode; |
| 133 | } |
| 134 | |
| 135 | clear(): void { |
| 136 | this.stop(); |
| 137 | this.history = []; |
| 138 | this.queued = []; |
| 139 | this.tainted = false; |
| 140 | this.boundary = newBoundary(); |
| 141 | this.log = []; |
| 142 | this.push({ kind: "cleared" }); |
| 143 | } |
| 144 | |
| 145 | stop(): void { |
| 146 | this.running?.abort(); |
| 147 | for (const [id, resolve] of this.pending) { |
| 148 | this.pending.delete(id); |
| 149 | resolve(false); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** The person's answer to an action they were asked about. */ |
| 154 | confirm(id: string, ok: boolean): boolean { |
| 155 | const r = this.pending.get(id); |
| 156 | if (!r) return false; |
| 157 | this.pending.delete(id); |
| 158 | this.push({ kind: "confirm-settled", id, ok }); |
| 159 | r(ok); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Something the person ran themselves from the address bar: shown in the conversation, and kept in it so the next |
| 165 | * message can ask about it ("open the second one"). What came back is marked as from the person's account, never as |
| 166 | * instructions, like any tool result; and from here on, actions that could carry it out of the browser ask first. |
| 167 | */ |
| 168 | recordCommand(c: { typed: string; tool: string; readOnly: boolean; args: Record<string, unknown>; sentence: string; page?: string; items?: Array<{ title: string; page?: string }>; details: string; server: string }): void { |
| 169 | const details = c.details.slice(0, MAX_RESULT_CHARS); |
| 170 | this.push({ kind: "command", typed: c.typed.slice(0, 2000), tool: c.tool, readOnly: c.readOnly, sentence: c.sentence, ...(c.page ? { page: c.page } : {}), ...(c.items ? { items: c.items } : {}), details }); |
| 171 | const record = |
| 172 | `[Not a message to you. A record: the person ran a command themselves, from the browser's address bar. It already ran; do not run it again unless asked. It is here so you can answer questions about it.]\n` + |
| 173 | `They typed: ${c.typed.slice(0, 2000)}\n` + |
| 174 | `It called ${c.server}__${c.tool} with ${JSON.stringify(c.args).slice(0, 2000)}${c.page ? `; the thing made is at ${c.page}` : ""}. The answer:\n` + |
| 175 | wrapResult(c.details.slice(0, 8000), this.boundary, "account"); |
| 176 | this.tainted = true; |
| 177 | if (this.running) this.queued.push(record); |
| 178 | else this.addRecord(record); |
| 179 | } |
| 180 | private addRecord(record: string) { |
| 181 | const last = this.history[this.history.length - 1]; |
| 182 | if (last?.role === "user") last.text = `${last.text}\n\n${record}`; |
| 183 | else this.history.push({ role: "user", text: record }); |
| 184 | } |
| 185 | |
| 186 | /** One message from the person; resolves when the assistant is done. */ |
| 187 | async send(text: string): Promise<SendResult> { |
| 188 | const cfg = this.ai(); |
| 189 | if (this.deps.callerNetworkOn()) return { ok: false, reason: "caller-network-on", message: "The chat is switched off while the caller network is on." }; |
| 190 | if (!cfg) return { ok: false, reason: "no-ai", message: "No AI is set up. Put ANTHROPIC_API_KEY or OPENAI_API_KEY in ~/.dbbasic/env, or set DBB_AI_BASE_URL for a model on your network." }; |
| 191 | if (this.running) return { ok: false, reason: "busy", message: "The assistant is still working on the last message. Stop it first." }; |
| 192 | const t = String(text ?? "").trim(); |
| 193 | if (!t) return { ok: false, reason: "empty" }; |
| 194 | |
| 195 | const model = (this.deps.modelFor ?? chatModel)(cfg); |
| 196 | const where = cfg.provider === "openai" && cfg.baseUrl ? new URL(cfg.baseUrl).host : model.provider === "anthropic" ? "Anthropic" : "OpenAI"; |
| 197 | const key = `${model.provider}/${model.model}/${where}`; |
| 198 | if (!this.consented.has(key)) { |
| 199 | if (process.env.DBB_AI_CONSENT !== "1" && !(await this.deps.consent({ provider: model.provider, model: model.model, where }))) { |
| 200 | return { ok: false, reason: "declined", message: "Not sent. Nothing leaves the browser until you agree." }; |
| 201 | } |
| 202 | this.consented.add(key); |
| 203 | } |
| 204 | |
| 205 | const ctl = new AbortController(); |
| 206 | this.running = ctl; |
| 207 | const start = this.seq; |
| 208 | this.push({ kind: "user", text: t }); |
| 209 | let inTok = 0; |
| 210 | let outTok = 0; |
| 211 | let steps = 0; |
| 212 | let ended: SendResult["ended"] = "error"; |
| 213 | let reply = ""; |
| 214 | try { |
| 215 | await runChat({ |
| 216 | model, |
| 217 | tools: [...pageTools(this.deps.run), ...((await this.deps.extraTools?.().catch(() => [])) ?? [])], |
| 218 | history: this.history, |
| 219 | userText: t, |
| 220 | policy: { mode: this.mode }, |
| 221 | boundary: this.boundary, |
| 222 | signal: ctl.signal, |
| 223 | confirm: (a) => |
| 224 | new Promise<boolean>((resolve) => { |
| 225 | this.pending.set(a.id, resolve); |
| 226 | this.push({ kind: "confirm", id: a.id, name: a.name, summary: a.summary, danger: a.danger }); |
| 227 | }), |
| 228 | emit: (e) => { |
| 229 | if (e.kind === "usage") { |
| 230 | inTok += e.inputTokens; |
| 231 | outTok += e.outputTokens; |
| 232 | steps++; |
| 233 | } |
| 234 | if (e.kind === "text") reply = e.text; |
| 235 | if (e.kind === "done") ended = e.reason; |
| 236 | // Redact anything that looks like a key before it reaches the panel. |
| 237 | this.push(e.kind === "done" && e.message ? { ...e, message: redact(e.message, cfg) } : e.kind === "text" ? { ...e, text: redact(e.text, cfg) } : e); |
| 238 | }, |
| 239 | context: () => |
| 240 | `The person is looking at one tab at a time; tools act on the active tab unless you pass a tab id. Today is ${new Date().toLocaleDateString("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric" })} (their local date).`, |
| 241 | tainted: this.tainted, |
| 242 | onTainted: () => { |
| 243 | this.tainted = true; |
| 244 | }, |
| 245 | }); |
| 246 | } finally { |
| 247 | this.running = null; |
| 248 | this.stop(); |
| 249 | for (const r of this.queued.splice(0)) this.addRecord(r); |
| 250 | } |
| 251 | const cost = cfg.provider === "anthropic" ? estimateCostUsd(model.model, { inputTokens: inTok, outputTokens: outTok }) : null; |
| 252 | this.deps.announce({ kind: "ai-chat", provider: model.provider, model: model.model, where, modelCalls: steps, inputTokens: inTok, outputTokens: outTok, costUsd: cost, ended }); |
| 253 | return { ok: true, ended, reply: redact(reply, cfg), events: this.log.filter((e) => e.seq > start) }; |
| 254 | } |
| 255 | } |