Refactor prompt construction into prompts.ts

This commit is contained in:
James Babcock committed 2026-07-26 12:33:10 -07:00
1 parent d07e22b458
commit 1c2e445519
3 files changed
+52 -31

No files matched your search

+2 -23
View File
@@ -1,4 +1,5 @@
import { DEFAULT_LLM_MODEL, type AnthropicMessage, type AnthropicToolDefinition } from "../native/anthropic";
import { buildAssistantSystemPrompt } from "../prompts";
import { DirectAnthropicBackend } from "./direct-backend";
import { toolRegistry, type ToolRegistry } from "./tool-registry";
import type { AssistantContext, AssistantTurnCallbacks, AssistantTurnHandle } from "./types";
@@ -19,13 +20,6 @@ const SESSION_IDLE_MS = 10 * 60 * 1000;
/** Trim history from the head once it grows past this many messages. */
const MAX_HISTORY_MESSAGES = 40;
const SYSTEM_PROMPT_BASE = [
"You are the voice assistant built into a pair of Even Realities G2 smart glasses.",
"Your reply is shown as text on a 576x288 monochrome heads-up display and may also be read aloud, so keep it short: 1-3 plain sentences, no markdown, no bullet lists unless the user explicitly asks for a list.",
"Prefer doing things with the tools you have over describing how the user could do them; when a tool can answer or act, use it.",
"If a tool fails or a capability is missing, say so briefly rather than inventing a result.",
].join(" ");
export class AssistantSession {
private readonly backend = new DirectAnthropicBackend();
private readonly messages: AnthropicMessage[] = [];
@@ -60,7 +54,7 @@ export class AssistantSession {
this.messages.push({ role: "user", content: text });
this.trimHistory();
const system = `${SYSTEM_PROMPT_BASE}\n\n${describeContext(ctx)}`;
const system = buildAssistantSystemPrompt(ctx);
const finish = () => {
this.turnHandle = null;
@@ -138,18 +132,3 @@ function startsWithToolResult(message: AnthropicMessage): boolean {
message.content[0]!.type === "tool_result"
);
}
function describeContext(ctx: AssistantContext): string {
const parts = [`Current time: ${ctx.localTime}.`];
parts.push(`The glasses display is currently ${ctx.screenOn ? "on" : "off"}.`);
if (ctx.foregroundApp) {
const title = ctx.foregroundTitle ? ` ("${ctx.foregroundTitle}")` : "";
parts.push(`The foreground app is ${ctx.foregroundApp}${title}.`);
} else {
parts.push("No app is in the foreground (the launcher is showing).");
}
if (ctx.headsetBattery !== null) {
parts.push(`Glasses battery: ${ctx.headsetBattery}%.`);
}
return `Context: ${parts.join(" ")}`;
}
+3 -8
View File
@@ -1,3 +1,5 @@
import { buildRefineUserMessage, REFINE_SYSTEM_PROMPT } from "../prompts";
declare const com: any;
/**
@@ -261,13 +263,6 @@ function describeHttpError(code: number, body: string): string {
}
}
const REFINE_SYSTEM_PROMPT =
"You edit dictated text. The user dictated a message, then dictated a follow-up. " +
"If the follow-up is additional content, append it to the message where it naturally fits. " +
"If it describes an edit (a correction, a deletion, or content to insert somewhere specific), apply that edit instead of appending the instruction itself. " +
"Fix only what the follow-up asks; keep the rest of the original wording. " +
"Output only the final text of the message, with no preamble, quotes, or commentary.";
export type RefineDictationOptions = {
apiKey: string;
original: string;
@@ -290,7 +285,7 @@ export function refineDictation(options: RefineDictationOptions): AnthropicStrea
messages: [
{
role: "user",
content: `Original dictation:\n${options.original}\n\nFollow-up dictation:\n${options.followup}`,
content: buildRefineUserMessage(options.original, options.followup),
},
],
onTextDelta: options.onTextDelta,
+47
View File
@@ -0,0 +1,47 @@
import type { AssistantContext } from "./assistant/types";
/**
* All hardcoded LLM prompt text lives here, so prompts can be reviewed and
* edited in one place without digging through the code that sends them.
*/
/** Base system prompt for the on-glasses voice assistant. */
export const ASSISTANT_SYSTEM_PROMPT_BASE = [
"You are the voice assistant built into a pair of Even Realities G2 smart glasses.",
"Your reply is shown as text on a 576x288 monochrome heads-up display and may also be read aloud, so keep it short: 1-3 plain sentences, no markdown, no bullet lists unless the user explicitly asks for a list.",
"Prefer doing things with the tools you have over describing how the user could do them; when a tool can answer or act, use it.",
"If a tool fails or a capability is missing, say so briefly rather than inventing a result.",
].join(" ");
/** Full assistant system prompt: the base plus the current device context. */
export function buildAssistantSystemPrompt(ctx: AssistantContext): string {
return `${ASSISTANT_SYSTEM_PROMPT_BASE}\n\n${describeContext(ctx)}`;
}
function describeContext(ctx: AssistantContext): string {
const parts = [`Current time: ${ctx.localTime}.`];
parts.push(`The glasses display is currently ${ctx.screenOn ? "on" : "off"}.`);
if (ctx.foregroundApp) {
const title = ctx.foregroundTitle ? ` ("${ctx.foregroundTitle}")` : "";
parts.push(`The foreground app is ${ctx.foregroundApp}${title}.`);
} else {
parts.push("No app is in the foreground (the launcher is showing).");
}
if (ctx.headsetBattery !== null) {
parts.push(`Glasses battery: ${ctx.headsetBattery}%.`);
}
return `Context: ${parts.join(" ")}`;
}
/** System prompt for the dictation-refinement (voice continuation) flow. */
export const REFINE_SYSTEM_PROMPT =
"You edit dictated text. The user dictated a message, then dictated a follow-up. " +
"If the follow-up is additional content, append it to the message where it naturally fits. " +
"If it describes an edit (a correction, a deletion, or content to insert somewhere specific), apply that edit instead of appending the instruction itself. " +
"Fix only what the follow-up asks; keep the rest of the original wording. " +
"Output only the final text of the message, with no preamble, quotes, or commentary.";
/** User message for the dictation-refinement flow. */
export function buildRefineUserMessage(original: string, followup: string): string {
return `Original dictation:\n${original}\n\nFollow-up dictation:\n${followup}`;
}