Add a model picker and support for the OpenAI API

This commit is contained in:
James Babcock committed 2026-07-26 13:09:28 -07:00
1 parent 1c2e445519
commit ccdeedd687
13 files changed
+688 -131

No files matched your search

+63 -30
View File
@@ -13,7 +13,13 @@ import {
type UiFontChoice,
} from "~/graphics/bdffont";
import { wrapText } from "~/graphics/textwrap";
import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, MenuLayer } from "./menu";
import {
ASSISTANT_MODEL_VALUES,
assistantModelLabel,
assistantModelProvider,
type AssistantModel,
} from "~/assistant/models";
import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, openModalMenu } from "./menu";
import { DashboardInputEvent, Layer, type LayerContext } from "./layers";
import { GrayImage } from "~/graphics/image";
@@ -109,15 +115,19 @@ export class ConfigSettingBoolean<TId extends string = string> extends ConfigSet
type ConfigSettingEnumOptions<TValue extends string, TId extends string> = ConfigSettingOptions<TValue, TId> & {
values: readonly TValue[];
normalize?: (value: string | null | undefined) => TValue;
/** Dynamic availability check used by enum picker rows. */
isDisabled?: (value: TValue) => boolean;
};
export class ConfigSettingEnum<TValue extends string, TId extends string = string> extends ConfigSetting<TValue, TId> {
readonly values: readonly TValue[];
private readonly normalizer: (value: string | null | undefined) => TValue;
private readonly disabledPredicate: (value: TValue) => boolean;
constructor(options: ConfigSettingEnumOptions<TValue, TId>) {
super(options);
this.values = options.values;
this.disabledPredicate = options.isDisabled ?? (() => false);
if (options.normalize) {
this.normalizer = (value: string | null | undefined) => {
const normalized = options.normalize(value) as TValue|undefined;
@@ -139,9 +149,17 @@ export class ConfigSettingEnum<TValue extends string, TId extends string = strin
return normalized;
}
isDisabled(value: TValue): boolean {
return this.disabledPredicate(value);
}
next(value = this.get()): TValue {
const index = this.values.indexOf(value);
return this.values[(index + 1) % this.values.length] ?? this.defaultValue;
for (let offset = 1; offset <= this.values.length; offset++) {
const candidate = this.values[(index + offset) % this.values.length];
if (candidate !== undefined && !this.isDisabled(candidate)) return candidate;
}
return value;
}
}
@@ -352,6 +370,22 @@ export const anthropicApiKeySetting = new ConfigSettingString({
description: "Anthropic API key, used when an Anthropic model is selected for the voice assistant.",
});
export const assistantModelSetting = new ConfigSettingEnum<AssistantModel>({
id: "assistant-model",
label: "Assistant model",
storageKey: "assistant.model",
defaultValue: "auto",
values: ASSISTANT_MODEL_VALUES,
formatValue: assistantModelLabel,
isDisabled: (value) => {
const provider = assistantModelProvider(value);
if (provider === "anthropic") return anthropicApiKeySetting.get().trim().length === 0;
if (provider === "openai") return openAiApiKeySetting.get().trim().length === 0;
return false;
},
description: "Model used by the voice assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set.",
});
export const mapboxApiKeySetting = new ConfigSettingString({
id: "mapbox-api-key",
label: "Mapbox token",
@@ -494,7 +528,6 @@ function maskToken(token: string): string {
type SettingsMenuOptions<T> = {
style?: "cycle"|"submenu"
onChange?: (ctx: LayerContext, newValue: T, oldValue: T) => void
}
@@ -502,36 +535,36 @@ export function enumSettingMenuItem<TValue extends string, TId extends string =
setting: ConfigSettingEnum<TValue, TId>,
opts?: SettingsMenuOptions<TValue>
): MenuItem {
const style = opts?.style ?? "cycle";
return {
label: setting.label,
description: setting.description,
onSelect: (ctx) => {
if (style === "cycle") {
const oldValue = setting.get();
const newValue = setting.next(oldValue);
setting.set(newValue);
opts?.onChange?.(ctx, newValue, oldValue);
} else {
const submenu = new MenuLayer(setting.label, setting.values.map((value) => ({
label: setting.displayValue(value),
onSelect: () => {
const oldValue = setting.get();
setting.set(value);
opts?.onChange?.(ctx, value, oldValue);
ctx.stack.pop();
},
render: ({ image, x, y }) => {
const selected = setting.get() === value ? " *" : "";
image.drawText(getDefaultSmallFont(), x, y + 3, `${setting.displayValue(value)}${selected}`, 200);
}
})));
ctx.stack.push(submenu);
}
},
render: ({ image, x, y, width }) => {
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, setting.label, setting.displayValue(setting.get()));
},
onSelect: (ctx) => {
const current = setting.get();
const items = setting.values.map((value): MenuItem => ({
label: setting.displayValue(value),
disabled: () => setting.isDisabled(value),
onSelect: () => {
const oldValue = setting.get();
setting.set(value);
opts?.onChange?.(ctx, value, oldValue);
ctx.stack.pop();
},
render: ({ image, x, y, disabled }) => {
const selected = setting.get() === value ? " *" : "";
image.drawText(
getDefaultSmallFont(),
x,
y + 3,
`${setting.displayValue(value)}${selected}`,
disabled ? 70 : 200,
);
},
}));
openModalMenu(ctx, setting.label, items, Math.max(0, setting.values.indexOf(current)));
},
render: ({ image, x, y, width }) => {
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, setting.label, setting.displayValue(setting.get()));
},
};
}
+3 -1
View File
@@ -4,6 +4,7 @@ import { getDashboardLogo } from "../../graphics/logo";
import { shell } from "../shell/shell";
import {
anthropicApiKeySetting,
assistantModelSetting,
assistantSkipConfirmationSetting,
batteryDisplayModeSetting,
brightnessSetting,
@@ -40,7 +41,7 @@ function settingsSections(): SettingsSection[] {
items: [
// Auto (ambient sensor) or an exact level; pushed to the glasses by
// the dashboard controller when changed and on each connect.
enumSettingMenuItem(brightnessSetting, { style: "submenu" }),
enumSettingMenuItem(brightnessSetting),
enumSettingMenuItem(screenTimeoutSetting, {
onChange: () => {
shell.noteUserActivity();
@@ -59,6 +60,7 @@ function settingsSections(): SettingsSection[] {
items: [
enumSettingMenuItem(wakeWordActionSetting),
enumSettingMenuItem(voiceProviderSetting),
enumSettingMenuItem(assistantModelSetting),
// When on, a wakeword utterance goes straight to the assistant with no
// Send/Type menu step.
toggleSettingMenuItem(assistantSkipConfirmationSetting),
+19 -15
View File
@@ -4,7 +4,7 @@ import { wrapText } from "../../graphics/textwrap";
import { clamp } from "../../util/numeric-util";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
import { DashboardInputEvent, Layer, LayerContext } from "../layers";
import { drawSelectionHighlight, MenuItem, MenuLayer, type MenuLayout } from "../menu";
import { drawSelectionHighlight, isMenuItemDisabled, MenuItem, MenuLayer, openModalMenu } from "../menu";
import { shell } from "../shell/shell";
/**
@@ -114,13 +114,24 @@ export class SettingsPanelLayer implements Layer {
// A selection only appears once focus is in the right column; before
// that the pane is a preview of what tapping would open.
const selected = this.focus === "right" && i === this.rightIndex;
const disabled = isMenuItemDisabled(item);
if (selected) {
drawSelectionHighlight(image, rightX - 2, rowY, rightW + 2, ROW_H - 2, appFocused, 6);
}
if (item.render) {
item.render({ image, x: rightX + 8, y: rowY, width: rightW - 16, height: ROW_H - 3, selected, text: item.label, ctx });
item.render({
image,
x: rightX + 8,
y: rowY,
width: rightW - 16,
height: ROW_H - 3,
selected,
disabled,
text: item.label,
ctx,
});
} else {
image.drawText(font, rightX + 8, rowY + 4, item.label, selected ? 255 : 200);
image.drawText(font, rightX + 8, rowY + 4, item.label, disabled ? 70 : selected ? 255 : 200);
}
}
if (descriptionLines.length) {
@@ -174,7 +185,10 @@ export class SettingsPanelLayer implements Layer {
if (items.length) this.rightIndex = (this.rightIndex + 1) % items.length;
return;
case "click":
if (items.length) await items[clamp(this.rightIndex, 0, items.length - 1)]!.onSelect(ctx, NO_MENU);
if (items.length) {
const item = items[clamp(this.rightIndex, 0, items.length - 1)]!;
if (!isMenuItemDisabled(item)) await item.onSelect(ctx, NO_MENU);
}
return;
case "double-click":
this.focus = "left";
@@ -195,17 +209,7 @@ export class SettingsPanelLayer implements Layer {
* MenuLayer (which draws the border and self-closes on double-click).
*/
export function openSettingsSubMenu(ctx: LayerContext, title: string, items: MenuItem[]): void {
const { width, height } = ctx.stack.getBaseSize();
const menuW = Math.min(320, width - 40);
const menuH = 16 + PAD + items.length * ROW_H + PAD;
const layout: MenuLayout = {
x: ((width - menuW) / 2) | 0,
y: ((height - menuH) / 2) | 0,
width: menuW,
minHeight: menuH,
maxHeight: menuH,
};
ctx.stack.push(new MenuLayer(title, items, layout));
openModalMenu(ctx, title, items);
}
function clampScroll(selected: number, scroll: number, visible: number, count: number): number {
+40 -2
View File
@@ -37,6 +37,7 @@ export type MenuItemRenderArgs = {
width: number;
height: number;
selected: boolean;
disabled: boolean;
text: string;
ctx: LayerContext;
};
@@ -45,6 +46,8 @@ export type MenuItem = {
label: string;
/** Extended description; the Settings panel shows it below the list while the item is selected. */
description?: string;
/** Disabled rows stay visible but render dimly and ignore clicks. */
disabled?: boolean | (() => boolean);
onSelect: (ctx: LayerContext, menu: MenuLayer) => Promise<void> | void;
render?: (args: MenuItemRenderArgs) => void;
};
@@ -181,6 +184,12 @@ export class MenuLayer implements Layer {
public readonly paintOverBase = false,
) {}
/** Start a newly opened picker on its current value. */
selectItem(index: number): this {
this.selectedIndex = clamp(index, 0, Math.max(0, this.items.length - 1));
return this;
}
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
const font = getDefaultSmallFont();
const { x, y, width } = this.layout;
@@ -216,6 +225,7 @@ export class MenuLayer implements Layer {
const item = this.items[index]!;
const rowY = bodyY + (index - this.scrollRow) * MENU_ROW_HEIGHT;
const selected = index === this.selectedIndex;
const disabled = isMenuItemDisabled(item);
if (selected) {
drawSelectionHighlight(
image,
@@ -235,11 +245,12 @@ export class MenuLayer implements Layer {
width: width - 44,
height: MENU_ROW_HEIGHT - 3,
selected,
disabled,
text: item.label,
ctx,
});
} else {
image.drawText(font, x + 22, rowY + 3, item.label, selected ? 255 : 200);
image.drawText(font, x + 22, rowY + 3, item.label, disabled ? 70 : selected ? 255 : 200);
}
}
@@ -276,7 +287,9 @@ export class MenuLayer implements Layer {
ctx.stack.pop();
return;
case "click":
await this.items[this.selectedIndex]!.onSelect(ctx, this);
if (!isMenuItemDisabled(this.items[this.selectedIndex]!)) {
await this.items[this.selectedIndex]!.onSelect(ctx, this);
}
return;
default:
return;
@@ -284,6 +297,31 @@ export class MenuLayer implements Layer {
}
}
/** Open a centered, bordered menu over the current layer. */
export function openModalMenu(
ctx: LayerContext,
title: string,
items: MenuItem[],
initialSelectedIndex = 0,
): void {
const { width, height } = ctx.stack.getBaseSize();
const menuWidth = Math.min(320, width - 40);
const naturalHeight = MENU_TITLE_HEIGHT + 2 * MENU_BODY_PADDING + items.length * MENU_ROW_HEIGHT;
const menuHeight = Math.min(naturalHeight, height - 40);
const layout: MenuLayout = {
x: ((width - menuWidth) / 2) | 0,
y: ((height - menuHeight) / 2) | 0,
width: menuWidth,
minHeight: menuHeight,
maxHeight: menuHeight,
};
ctx.stack.push(new MenuLayer(title, items, layout).selectItem(initialSelectedIndex));
}
export function isMenuItemDisabled(item: MenuItem): boolean {
return typeof item.disabled === "function" ? item.disabled() : item.disabled === true;
}
export class TextPageLayer implements Layer {
constructor(
private readonly title: string,
+21 -6
View File
@@ -7,13 +7,16 @@ import { MenuLayer, type MenuItem } from "../menu";
import { VoiceInputLayer, type VoiceSendTarget } from "../apps/voice-input";
import { AssistantLayer } from "../apps/assistant";
import { AssistantSession } from "../../assistant/session";
import { resolveAssistantModel, type ResolvedAssistantModel } from "../../assistant/models";
import type { AssistantContext } from "../../assistant/types";
import { SingleNotificationLayer } from "../notifications";
import {
anthropicApiKeySetting,
assistantModelSetting,
assistantSkipConfirmationSetting,
batteryDisplayModeSetting,
onAnySettingChanged,
openAiApiKeySetting,
timeFormatSetting,
wakeWordActionSetting,
} from "../dashboard-settings";
@@ -673,18 +676,30 @@ class Shell {
}
private isAssistantAvailable(): boolean {
return anthropicApiKeySetting.get().trim().length > 0;
return this.resolveAssistantConfiguration() !== null;
}
private ensureAssistantSession(): AssistantSession | null {
const apiKey = anthropicApiKeySetting.get().trim();
if (!apiKey) return null;
if (!this.assistantSession || this.assistantSession.isExpired()) {
this.assistantSession = new AssistantSession(apiKey);
const llm = this.resolveAssistantConfiguration();
if (!llm) return null;
if (
!this.assistantSession ||
this.assistantSession.isExpired() ||
!this.assistantSession.matchesConfiguration(llm)
) {
this.assistantSession?.cancel();
this.assistantSession = new AssistantSession(llm);
}
return this.assistantSession;
}
private resolveAssistantConfiguration(): ResolvedAssistantModel | null {
return resolveAssistantModel(assistantModelSetting.get(), {
anthropic: anthropicApiKeySetting.get(),
openai: openAiApiKeySetting.get(),
});
}
private buildAssistantContext(): AssistantContext {
const foreground = this.getForegroundApp();
return {
@@ -704,7 +719,7 @@ class Shell {
sendToAssistant(text: string): void {
const session = this.ensureAssistantSession();
if (!session) {
this.showAlert("Set an Anthropic API key in Settings to use the assistant.");
this.showAlert("Set an API key for the selected assistant model in Settings.");
return;
}
if (!this.screenOn) this.wake("sidebar");