forked from Blendman974/faceclaw
Add "AI Chat" app.
This commit is contained in:
1 parent
78c56ffc4e
commit
0765e943fb
13 files changed
+616
-87
No files matched your search
@@ -0,0 +1,95 @@
|
||||
import { AssistantSession, type AssistantBackendConfig, type AssistantSessionHistory } from "./session";
|
||||
import { ASSISTANT_MODEL_VALUES, type AssistantModel } from "./models";
|
||||
|
||||
export type ReasoningLevel = "default" | "low" | "medium" | "high";
|
||||
export type Conversation = {
|
||||
id: string;
|
||||
model: AssistantModel;
|
||||
reasoning: ReasoningLevel;
|
||||
session: AssistantSession | null;
|
||||
history?: AssistantSessionHistory;
|
||||
};
|
||||
type SavedConversation = Omit<Conversation, "session">;
|
||||
type SavedConversations = { selectedId: string; conversations: SavedConversation[] };
|
||||
|
||||
/** Shared selection and history for every assistant entry point. No credentials are saved. */
|
||||
export class AssistantConversations {
|
||||
private records: Conversation[] = [];
|
||||
private selectedId = "";
|
||||
private readonly listeners = new Set<() => void>();
|
||||
|
||||
constructor(
|
||||
private readonly resolve: (model: AssistantModel, reasoning: ReasoningLevel) => AssistantBackendConfig | null,
|
||||
private readonly defaultModel: () => AssistantModel,
|
||||
private readonly persist: (value: string) => void,
|
||||
saved = "",
|
||||
) {
|
||||
try {
|
||||
const data: SavedConversations = JSON.parse(saved);
|
||||
if (Array.isArray(data.conversations)) {
|
||||
this.records = data.conversations.filter((record) =>
|
||||
typeof record.id === "string" && ASSISTANT_MODEL_VALUES.includes(record.model) &&
|
||||
["default", "low", "medium", "high"].includes(record.reasoning) &&
|
||||
Array.isArray(record.history?.messages) && Array.isArray(record.history?.transcript),
|
||||
).map((record) => ({ ...record, session: null }));
|
||||
this.selectedId = data.selectedId;
|
||||
}
|
||||
} catch { /* First launch or invalid saved state. */ }
|
||||
if (!this.records.length) this.records.push(this.makeConversation());
|
||||
if (!this.records.some((record) => record.id === this.selectedId)) this.selectedId = this.records[0]!.id;
|
||||
}
|
||||
|
||||
current(): Conversation { return this.records.find((record) => record.id === this.selectedId)!; }
|
||||
list(): readonly Conversation[] { return this.records; }
|
||||
title(record = this.current()): string {
|
||||
const transcript = record.session?.transcript ?? record.history?.transcript ?? [];
|
||||
return transcript.find((entry) => entry.role === "user")?.text.replace(/\s+/g, " ").slice(0, 60) || "New session";
|
||||
}
|
||||
onChanged(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
private changed(save = true): void {
|
||||
if (save) this.persist(JSON.stringify({ selectedId: this.selectedId, conversations: this.records.map(
|
||||
({ session, ...record }) => ({ ...record, history: session?.history() ?? record.history ?? { messages: [], transcript: [] } }),
|
||||
) }));
|
||||
for (const listener of this.listeners) listener();
|
||||
}
|
||||
private makeConversation(): Conversation {
|
||||
return { id: `${Date.now()}-${Math.random().toString(36).slice(2)}`, model: this.defaultModel(), reasoning: "default", session: null };
|
||||
}
|
||||
create(): boolean {
|
||||
if (this.current().session?.isTurnActive()) return false;
|
||||
const record = this.makeConversation();
|
||||
this.records.push(record);
|
||||
this.selectedId = record.id;
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
select(id: string): boolean {
|
||||
if (this.current().session?.isTurnActive() || !this.records.some((record) => record.id === id)) return false;
|
||||
this.selectedId = id;
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
configure(model: AssistantModel, reasoning: ReasoningLevel): boolean {
|
||||
const record = this.current();
|
||||
if (record.session?.isTurnActive() || !this.resolve(model, reasoning)) return false;
|
||||
record.model = model;
|
||||
record.reasoning = reasoning;
|
||||
this.ensureSession();
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
available(model: AssistantModel, reasoning: ReasoningLevel): boolean { return this.resolve(model, reasoning) !== null; }
|
||||
ensureSession(): AssistantSession | null {
|
||||
const record = this.current();
|
||||
const config = this.resolve(record.model, record.reasoning);
|
||||
if (!config) return null;
|
||||
if (!record.session) {
|
||||
record.session = new AssistantSession(config, undefined, record.history);
|
||||
record.session.onChanged(() => this.changed(!record.session!.isTurnActive()));
|
||||
} else record.session.configure(config);
|
||||
return record.session;
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,7 @@ export class DirectAssistantBackend {
|
||||
const startMs = Date.now();
|
||||
let cancelled = false;
|
||||
let streamHandle: LlmStreamHandle | null = null;
|
||||
let priorText = "";
|
||||
|
||||
const finishError = (message: string) => {
|
||||
if (cancelled) return;
|
||||
@@ -78,7 +79,7 @@ export class DirectAssistantBackend {
|
||||
tools: options.buildTools(),
|
||||
onTextDelta: (delta, textSoFar) => {
|
||||
if (cancelled) return;
|
||||
options.callbacks.onTextDelta(delta, textSoFar);
|
||||
options.callbacks.onTextDelta(delta, priorText + textSoFar);
|
||||
},
|
||||
onDone: (result) => {
|
||||
streamHandle = null;
|
||||
@@ -87,6 +88,9 @@ export class DirectAssistantBackend {
|
||||
finishError("The assistant declined this request");
|
||||
return;
|
||||
}
|
||||
const fullText = priorText + result.text;
|
||||
options.callbacks.onTextDelta("", fullText);
|
||||
priorText = fullText ? `${fullText}\n\n` : "";
|
||||
// Record what the model produced this iteration.
|
||||
const assistantContent: LlmContentBlock[] = result.content.length
|
||||
? result.content
|
||||
|
||||
+113
-47
@@ -15,26 +15,20 @@ import type {
|
||||
AssistantTurnHandle,
|
||||
} from "./types";
|
||||
|
||||
/**
|
||||
* One assistant conversation. Owns the message history and turn state and
|
||||
* drives either the on-phone provider loop (direct) or the external agent
|
||||
* bridge. The UI talks only to this surface, so AssistantLayer is
|
||||
* backend-agnostic.
|
||||
*
|
||||
* Sessions are cheap and idle-expire (see isExpired): the shell starts a fresh
|
||||
* one for a new wakeword and reuses the current one for a follow-up. In
|
||||
* external mode the conversation history lives on the agent's machine (one
|
||||
* long-lived session there), so expiry here only affects the overlay UI.
|
||||
*/
|
||||
/** One shared conversation, independent of the voice overlay or chat window. */
|
||||
export type AssistantTranscriptEntry = { role: "user" | "assistant"; text: string };
|
||||
export type AssistantSessionHistory = {
|
||||
messages: LlmMessage[];
|
||||
transcript: AssistantTranscriptEntry[];
|
||||
/** Model identity for safe replay after Auto resolves differently on restart. */
|
||||
engine?: string;
|
||||
};
|
||||
|
||||
/** Which engine answers utterances, plus what it needs to do so. */
|
||||
export type AssistantBackendConfig =
|
||||
| { kind: "direct"; llm: ResolvedAssistantModel }
|
||||
| { kind: "external"; bridge: AssistantBridgeConfig };
|
||||
|
||||
/** A new PTT after this much idle starts a fresh conversation. */
|
||||
const SESSION_IDLE_MS = 10 * 60 * 1000;
|
||||
|
||||
/** Trim history from the head once it grows past this many messages. */
|
||||
const MAX_HISTORY_MESSAGES = 40;
|
||||
|
||||
@@ -42,38 +36,61 @@ export class AssistantSession {
|
||||
private readonly directBackend = new DirectAssistantBackend();
|
||||
private readonly messages: LlmMessage[] = [];
|
||||
private turnHandle: AssistantTurnHandle | null = null;
|
||||
private lastActivityMs = Date.now();
|
||||
readonly transcript: AssistantTranscriptEntry[] = [];
|
||||
status = "";
|
||||
private readonly listeners = new Set<() => void>();
|
||||
private turnGeneration = 0;
|
||||
// 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 config: AssistantBackendConfig,
|
||||
private config: AssistantBackendConfig,
|
||||
private readonly registry: ToolRegistry = toolRegistry,
|
||||
) {}
|
||||
|
||||
matchesConfiguration(config: AssistantBackendConfig): boolean {
|
||||
if (this.config.kind !== config.kind) return false;
|
||||
if (this.config.kind === "direct" && config.kind === "direct") {
|
||||
return (
|
||||
this.config.llm.provider === config.llm.provider &&
|
||||
this.config.llm.model === config.llm.model &&
|
||||
this.config.llm.apiKey === config.llm.apiKey
|
||||
);
|
||||
history?: AssistantSessionHistory,
|
||||
) {
|
||||
if (history) {
|
||||
this.messages.push(...history.messages);
|
||||
this.transcript.push(...history.transcript);
|
||||
if (history.engine !== this.engine()) this.rebuildTextHistory();
|
||||
}
|
||||
if (this.config.kind === "external" && config.kind === "external") {
|
||||
return (
|
||||
this.config.bridge.host === config.bridge.host &&
|
||||
this.config.bridge.port === config.bridge.port &&
|
||||
this.config.bridge.token === config.bridge.token
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Whether this session has been idle long enough to retire. */
|
||||
isExpired(nowMs = Date.now()): boolean {
|
||||
return nowMs - this.lastActivityMs > SESSION_IDLE_MS;
|
||||
onChanged(listener: () => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
private changed(): void {
|
||||
for (const listener of this.listeners) {
|
||||
try { listener(); } catch (error) { console.warn("assistant history listener failed", error); }
|
||||
}
|
||||
}
|
||||
|
||||
history(): AssistantSessionHistory {
|
||||
return { messages: this.messages, transcript: this.transcript, engine: this.engine() };
|
||||
}
|
||||
|
||||
configure(config: AssistantBackendConfig): void {
|
||||
if (this.isTurnActive()) return;
|
||||
// Opaque reasoning/tool provider items are only replayable on their
|
||||
// original model. Preserve visible conversation when switching engines.
|
||||
if (this.config.kind !== config.kind ||
|
||||
(this.config.kind === "direct" && config.kind === "direct" &&
|
||||
(this.config.llm.model !== config.llm.model || this.config.llm.provider !== config.llm.provider))) {
|
||||
this.rebuildTextHistory();
|
||||
}
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
private engine(): string {
|
||||
return this.config.kind === "direct" ? `${this.config.llm.provider}:${this.config.llm.model}` : "external";
|
||||
}
|
||||
|
||||
private rebuildTextHistory(): void {
|
||||
this.messages.splice(0, this.messages.length, ...this.transcript
|
||||
.filter((entry) => entry.text.trim())
|
||||
.map((entry) => ({ role: entry.role, content: entry.text })));
|
||||
}
|
||||
|
||||
isTurnActive(): boolean {
|
||||
@@ -86,20 +103,44 @@ export class AssistantSession {
|
||||
callbacks.onError("The assistant is still working on the previous request");
|
||||
return;
|
||||
}
|
||||
this.lastActivityMs = Date.now();
|
||||
text = text.trim();
|
||||
if (!text) return;
|
||||
const generation = ++this.turnGeneration;
|
||||
const reply: AssistantTranscriptEntry = { role: "assistant", text: "" };
|
||||
this.transcript.push({ role: "user", text }, reply);
|
||||
this.status = "Thinking...";
|
||||
// Install a sentinel before calling providers: some failures are synchronous.
|
||||
this.turnHandle = { cancel: () => {} };
|
||||
this.changed();
|
||||
|
||||
const finish = () => {
|
||||
++this.turnGeneration;
|
||||
this.turnHandle = null;
|
||||
this.lastActivityMs = Date.now();
|
||||
this.changed();
|
||||
};
|
||||
const wrappedCallbacks: AssistantTurnCallbacks = {
|
||||
onTextDelta: callbacks.onTextDelta,
|
||||
onToolActivity: callbacks.onToolActivity,
|
||||
onTextDelta: (delta, full) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
reply.text = full;
|
||||
this.status = "Thinking...";
|
||||
this.changed();
|
||||
callbacks.onTextDelta(delta, full);
|
||||
},
|
||||
onToolActivity: (label) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = `→ ${label}`;
|
||||
this.changed();
|
||||
callbacks.onToolActivity(label);
|
||||
},
|
||||
onTurnDone: (result) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = "";
|
||||
finish();
|
||||
callbacks.onTurnDone(result);
|
||||
},
|
||||
onError: (message) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.status = message;
|
||||
finish();
|
||||
callbacks.onError(message);
|
||||
},
|
||||
@@ -108,7 +149,8 @@ export class AssistantSession {
|
||||
if (this.config.kind === "external") {
|
||||
// History and the agent loop live on the agent's machine; the phone just
|
||||
// streams this turn. The overlay keeps its own display state.
|
||||
this.turnHandle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
|
||||
const handle = assistantBridge.sendUtterance(text, ctx, wrappedCallbacks);
|
||||
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -123,24 +165,48 @@ export class AssistantSession {
|
||||
content: isLocal ? `${text}\n\n(${describeAssistantContext(ctx)})` : text,
|
||||
});
|
||||
this.trimHistory();
|
||||
this.turnHandle = this.directBackend.runTurn({
|
||||
// Work on a turn-local array: cancelled tools may finish later, and must
|
||||
// never append orphan results into a subsequent turn's context.
|
||||
const turnMessages = this.messages.slice();
|
||||
const handle = this.directBackend.runTurn({
|
||||
provider: llm.provider,
|
||||
apiKey: llm.apiKey,
|
||||
model: llm.model,
|
||||
effort: llm.effort,
|
||||
system: isLocal ? ASSISTANT_SYSTEM_PROMPT_BASE : buildAssistantSystemPrompt(ctx),
|
||||
messages: this.messages,
|
||||
messages: turnMessages,
|
||||
buildTools: () => this.buildToolDefinitions(),
|
||||
registry: this.registry,
|
||||
resolveToolName: (apiName) => this.toolNameMap.get(apiName) ?? apiName,
|
||||
callbacks: wrappedCallbacks,
|
||||
callbacks: {
|
||||
...wrappedCallbacks,
|
||||
onTurnDone: (result) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
this.messages.splice(0, this.messages.length, ...turnMessages);
|
||||
wrappedCallbacks.onTurnDone(result);
|
||||
},
|
||||
onError: (message) => {
|
||||
if (generation !== this.turnGeneration) return;
|
||||
// Retain the visible partial answer, without incomplete tool pairs.
|
||||
if (reply.text) this.messages.push({ role: "assistant", content: reply.text });
|
||||
wrappedCallbacks.onError(message);
|
||||
},
|
||||
},
|
||||
});
|
||||
if (generation === this.turnGeneration && this.turnHandle) this.turnHandle = handle;
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.turnHandle?.cancel();
|
||||
if (!this.turnHandle) return;
|
||||
++this.turnGeneration;
|
||||
this.turnHandle.cancel();
|
||||
this.turnHandle = null;
|
||||
this.lastActivityMs = Date.now();
|
||||
const reply = this.transcript[this.transcript.length - 1];
|
||||
if (reply?.role === "assistant" && reply.text) {
|
||||
this.messages.push({ role: "assistant", content: reply.text });
|
||||
}
|
||||
this.status = "Cancelled";
|
||||
this.changed();
|
||||
}
|
||||
|
||||
private buildToolDefinitions(): LlmToolDefinition[] {
|
||||
|
||||
Reference in new issue
Block a user