forked from Blendman974/faceclaw
Merge branch 'pr-57'
This commit is contained in:
commit
7f3e3f2305
25 files changed
+1888
-41
No files matched your search
@@ -18,6 +18,7 @@ import {
|
||||
} from "~/assistant/models";
|
||||
import { isLocalModelReady } from "../native/local-llm";
|
||||
import { normalizeWhisperServerUrl } from "../native/whisper-server-stt";
|
||||
import { normalizeOpenAiCompatibleUrl } from "../native/openai-chat";
|
||||
import { isValidSpotifyClientId, SPOTIFY_CLIENT_ID_KEY } from "../native/spotify-auth";
|
||||
import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, openModalMenu } from "./menu";
|
||||
import { ANIMATION_SPEEDS, animationSpeedLabel, scaleAnimationDuration, type AnimationSpeed } from "./animation-speed";
|
||||
@@ -942,7 +943,7 @@ export const assistantBackendSetting = new ConfigSettingEnum<AssistantBackendKin
|
||||
values: global.isIOS ? ["direct"] : ["direct", "external"],
|
||||
formatValue: (value) => assistantBackendLabels[value] ?? value,
|
||||
description:
|
||||
global.isIOS ? "An LLM called from the phone: a cloud model with your OpenAI or Anthropic API key (Settings > API Keys), or Apple Intelligence's on-device model." : "Who answers assistant queries: an LLM called from the phone (a cloud API with your key, or the downloaded on-phone model), or your own long-running agent (e.g. OpenClaw) reached through the faceclaw-agent-bridge plugin.",
|
||||
global.isIOS ? "An LLM called from the phone: a cloud model with your OpenAI or Anthropic API key (Settings > API Keys) or your configured OpenAI-compatible API, or Apple Intelligence's on-device model." : "Who answers assistant queries: an LLM called from the phone (a cloud API with your key, or the downloaded on-phone model), or your own long-running agent (e.g. OpenClaw) reached through the faceclaw-agent-bridge plugin.",
|
||||
});
|
||||
|
||||
export const assistantHermesUrlSetting = new ConfigSettingString({
|
||||
@@ -1008,6 +1009,48 @@ export const assistantAllowProactiveSetting = new ConfigSettingBoolean({
|
||||
"Let the external agent use glasses tools outside of a conversation, e.g. showing an alert when a long-running job finishes. Rate-limited; only tools marked proactive-safe are allowed.",
|
||||
});
|
||||
|
||||
export const assistantOpenAiCompatibleUrlSetting = new ConfigSettingString({
|
||||
id: "assistant-openai-compatible-url",
|
||||
label: "Compatible API URL",
|
||||
storageKey: "assistant.openAiCompatibleUrl",
|
||||
defaultValue: "",
|
||||
editorTitle: "OpenAI-compatible API root",
|
||||
glassesEditTitle: "Edit compatible API URL",
|
||||
formatValue: (value) => value || "(not set)",
|
||||
validate: (value) => (!value.trim() || normalizeOpenAiCompatibleUrl(value) ? null : "Enter an http:// or https:// API root."),
|
||||
description: "Explicit API root for the OpenAI-compatible Chat Completions assistant. Venice: https://api.venice.ai/api/v1. Open WebUI: https://your-host/api. The provider's path is not guessed.",
|
||||
});
|
||||
|
||||
export const assistantOpenAiCompatibleKeySetting = new ConfigSettingString({
|
||||
id: "assistant-openai-compatible-key",
|
||||
label: "Compatible API key",
|
||||
storageKey: "assistant.openAiCompatibleKey",
|
||||
defaultValue: "",
|
||||
editorTitle: "OpenAI-compatible API key (optional)",
|
||||
glassesEditTitle: "Edit compatible API key",
|
||||
formatValue: (value) => (value ? `${value.slice(0, 6)}...` : "(none)"),
|
||||
description: "Bearer key sent only to the compatible assistant URL above. Optional for private servers that do not require authentication.",
|
||||
});
|
||||
|
||||
export const assistantOpenAiCompatibleModelSetting = new ConfigSettingString({
|
||||
id: "assistant-openai-compatible-model",
|
||||
label: "Compatible model ID",
|
||||
storageKey: "assistant.openAiCompatibleModel",
|
||||
defaultValue: "",
|
||||
editorTitle: "OpenAI-compatible model ID",
|
||||
glassesEditTitle: "Edit compatible model ID",
|
||||
formatValue: (value) => value || "(not set)",
|
||||
description: "Exact model ID sent to the compatible provider. For glasses tools, choose a model that supports function calling.",
|
||||
});
|
||||
|
||||
export const veniceWebSearchSetting = new ConfigSettingBoolean({
|
||||
id: "venice-web-search",
|
||||
label: "Venice web search",
|
||||
storageKey: "assistant.veniceWebSearch",
|
||||
defaultValue: false,
|
||||
description: "Allow supported Venice models to search the web for current information. Shown only for the official Venice API URL. Search queries are processed by Venice's search service and may incur additional charges.",
|
||||
});
|
||||
|
||||
export const elevenLabsApiKeySetting = new ConfigSettingString({
|
||||
id: "elevenlabs-api-key",
|
||||
label: "ElevenLabs key",
|
||||
@@ -1112,11 +1155,12 @@ export const assistantModelSetting = new ConfigSettingEnum<AssistantModel>({
|
||||
if (provider === "anthropic") return anthropicApiKeySetting.get().trim().length === 0;
|
||||
if (provider === "openai") return openAiApiKeySetting.get().trim().length === 0;
|
||||
if (provider === "local") return !isLocalModelReady();
|
||||
if (provider === "openai-compatible") return !assistantOpenAiCompatibleUrlSetting.get().trim() || !assistantOpenAiCompatibleModelSetting.get().trim();
|
||||
if (provider === "hermes") return assistantHermesUrlSetting.get().trim().length === 0;
|
||||
return false;
|
||||
},
|
||||
description:
|
||||
global.isIOS ? "Model used by the voice assistant. Auto prefers Terra with an OpenAI key, then Sonnet with an Anthropic key, then Apple Intelligence's on-device model." : "Model used by the voice assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set, then the downloaded on-phone model.",
|
||||
global.isIOS ? "Model used by the assistant. Auto prefers Terra with an OpenAI key, then Sonnet with an Anthropic key, then Apple Intelligence's on-device model. OpenAI-compatible uses the URL, model ID, and optional key below; Auto never picks it." : "Model used by the assistant. Auto prefers Terra when an OpenAI key is set, then Sonnet when an Anthropic key is set, then the downloaded on-phone model. OpenAI-compatible uses the URL, model ID, and optional key below; Auto never picks it.",
|
||||
});
|
||||
|
||||
export const mapboxApiKeySetting = new ConfigSettingString({
|
||||
|
||||
@@ -42,7 +42,11 @@ import {
|
||||
assistantBridgePortSetting,
|
||||
assistantBridgeTokenSetting,
|
||||
assistantModelSetting,
|
||||
assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting,
|
||||
assistantOpenAiCompatibleUrlSetting,
|
||||
assistantSkipConfirmationSetting,
|
||||
veniceWebSearchSetting,
|
||||
batteryDisplayModeSetting,
|
||||
brightnessSetting,
|
||||
autoBrightnessMinSetting,
|
||||
@@ -94,6 +98,8 @@ import {
|
||||
import { wearBridge } from "../../native/wear-bridge";
|
||||
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
||||
import { uiFontPickerMenuItem } from "../font-picker";
|
||||
import { compatibleModelDiscoveryMenuItem } from "../openai-compatible-model-picker";
|
||||
import { isVeniceApiBaseUrl } from "../../native/openai-chat";
|
||||
|
||||
/** The Settings app's master-detail panel (sections on the left, contents on the right). */
|
||||
export function createSettingsPanelLayer(): SettingsPanelLayer {
|
||||
@@ -142,22 +148,36 @@ function settingsSections(): SettingsSection[] {
|
||||
},
|
||||
{
|
||||
label: "Assistant",
|
||||
items: [
|
||||
// iOS's on-phone model is Apple Intelligence; Android downloads one. Only Android has the bridge.
|
||||
enumSettingMenuItem(assistantBackendSetting),
|
||||
enumSettingMenuItem(assistantModelSetting),
|
||||
textSettingMenuItem(assistantHermesUrlSetting),
|
||||
textSettingMenuItem(assistantHermesKeySetting),
|
||||
global.isIOS ? systemModelMenuItem() : localModelMenuItem(),
|
||||
toggleSettingMenuItem(assistantSkipConfirmationSetting),
|
||||
// iOS has no agent bridge yet.
|
||||
...(!global.isIOS ? [
|
||||
textSettingMenuItem(assistantBridgeHostSetting),
|
||||
textSettingMenuItem(assistantBridgePortSetting),
|
||||
textSettingMenuItem(assistantBridgeTokenSetting),
|
||||
toggleSettingMenuItem(assistantAllowProactiveSetting),
|
||||
] : []),
|
||||
],
|
||||
// Re-read on every paint so the Venice-only row follows URL edits.
|
||||
get items() {
|
||||
return [
|
||||
// iOS's on-phone model is Apple Intelligence; Android downloads one. Only Android has the bridge.
|
||||
enumSettingMenuItem(assistantBackendSetting),
|
||||
enumSettingMenuItem(assistantModelSetting),
|
||||
textSettingMenuItem(assistantHermesUrlSetting),
|
||||
textSettingMenuItem(assistantHermesKeySetting),
|
||||
textSettingMenuItem(assistantOpenAiCompatibleUrlSetting),
|
||||
textSettingMenuItem(assistantOpenAiCompatibleKeySetting),
|
||||
compatibleModelDiscoveryMenuItem({
|
||||
getBaseUrl: () => assistantOpenAiCompatibleUrlSetting.get(),
|
||||
getApiKey: () => assistantOpenAiCompatibleKeySetting.get(),
|
||||
getModel: () => assistantOpenAiCompatibleModelSetting.get(),
|
||||
setModel: (model) => assistantOpenAiCompatibleModelSetting.set(model),
|
||||
}, assistantOpenAiCompatibleModelSetting),
|
||||
textSettingMenuItem(assistantOpenAiCompatibleModelSetting),
|
||||
...(isVeniceApiBaseUrl(assistantOpenAiCompatibleUrlSetting.get())
|
||||
? [toggleSettingMenuItem(veniceWebSearchSetting)] : []),
|
||||
global.isIOS ? systemModelMenuItem() : localModelMenuItem(),
|
||||
toggleSettingMenuItem(assistantSkipConfirmationSetting),
|
||||
// iOS has no agent bridge yet.
|
||||
...(!global.isIOS ? [
|
||||
textSettingMenuItem(assistantBridgeHostSetting),
|
||||
textSettingMenuItem(assistantBridgePortSetting),
|
||||
textSettingMenuItem(assistantBridgeTokenSetting),
|
||||
toggleSettingMenuItem(assistantAllowProactiveSetting),
|
||||
] : []),
|
||||
];
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "API Keys",
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
import { MenuLayer, type MenuItem } from "./menu";
|
||||
import { EditTextSettingLayer } from "./dashboard-settings";
|
||||
import type { LayerContext } from "./layers";
|
||||
import {
|
||||
discoverOpenAiCompatibleModels,
|
||||
type DiscoveredCompatibleModel,
|
||||
} from "../native/openai-chat-models";
|
||||
|
||||
export type ModelDiscoverySettings = {
|
||||
getBaseUrl(): string;
|
||||
getApiKey(): string;
|
||||
getModel(): string;
|
||||
setModel(model: string): void;
|
||||
};
|
||||
|
||||
type Discover = typeof discoverOpenAiCompatibleModels;
|
||||
type PickerState = "loading" | "models" | "error";
|
||||
|
||||
const DISCOVERY_EXPLANATION =
|
||||
"Discover models queries only this server's /models endpoint. Choose an exact model ID, or keep using manual entry. This does not send an inference request.";
|
||||
|
||||
/** A cancellable, on-demand model picker embedded in Faceclaw's existing menus. */
|
||||
export class OpenAiCompatibleModelPicker extends MenuLayer {
|
||||
private state: PickerState = "loading";
|
||||
private models: DiscoveredCompatibleModel[] = [];
|
||||
private error = "";
|
||||
private showOffline = false;
|
||||
private requestGeneration = 0;
|
||||
private endpointKey = "";
|
||||
private removed = false;
|
||||
private readonly modelIdByRow = new Map<MenuItem, string>();
|
||||
|
||||
constructor(
|
||||
private readonly settings: ModelDiscoverySettings,
|
||||
private readonly modelSetting: ConstructorParameters<typeof EditTextSettingLayer>[0],
|
||||
private readonly discover: Discover = discoverOpenAiCompatibleModels,
|
||||
viewport: { width: number; height: number } = { width: 640, height: 480 },
|
||||
) {
|
||||
super("Discover models", [], discoveryLayout(viewport));
|
||||
}
|
||||
|
||||
open(ctx: LayerContext): void {
|
||||
ctx.stack.push(this);
|
||||
void this.refresh(ctx);
|
||||
}
|
||||
|
||||
private currentEndpoint(): string {
|
||||
return `${this.settings.getBaseUrl().trim()}\n${this.settings.getApiKey().trim()}`;
|
||||
}
|
||||
|
||||
private async refresh(ctx: LayerContext, forceRefresh = false): Promise<void> {
|
||||
const generation = ++this.requestGeneration;
|
||||
const endpointKey = this.currentEndpoint();
|
||||
this.endpointKey = endpointKey;
|
||||
this.state = "loading";
|
||||
this.error = "";
|
||||
this.updateRows();
|
||||
try {
|
||||
const models = await this.discover({
|
||||
baseUrl: this.settings.getBaseUrl(),
|
||||
apiKey: this.settings.getApiKey(),
|
||||
forceRefresh,
|
||||
});
|
||||
if (!this.isActive(generation)) return;
|
||||
if (endpointKey !== this.currentEndpoint()) {
|
||||
this.error = "API URL or key changed; discover again";
|
||||
this.state = "error";
|
||||
} else {
|
||||
this.models = models;
|
||||
this.state = "models";
|
||||
}
|
||||
} catch (error) {
|
||||
if (!this.isActive(generation)) return;
|
||||
this.error = endpointKey !== this.currentEndpoint()
|
||||
? "API URL or key changed; discover again"
|
||||
: error instanceof Error ? error.message : String(error);
|
||||
this.state = "error";
|
||||
}
|
||||
this.updateRows();
|
||||
ctx.actions.requestRender();
|
||||
}
|
||||
|
||||
private isActive(generation: number): boolean {
|
||||
// A temporary overlay may sit above this still-open picker. Removal, not
|
||||
// top-of-stack status, determines whether the pending result is obsolete.
|
||||
return !this.removed && generation === this.requestGeneration;
|
||||
}
|
||||
|
||||
private updateRows(): void {
|
||||
this.modelIdByRow.clear();
|
||||
const rows: MenuItem[] = [];
|
||||
if (this.state === "loading") {
|
||||
rows.push({ label: "Discovering models...", disabled: true, description: DISCOVERY_EXPLANATION, onSelect: () => {} });
|
||||
rows.push({
|
||||
label: "Cancel discovery",
|
||||
description: "Ignore the pending result and return to Assistant settings.",
|
||||
onSelect: (innerCtx) => innerCtx.stack.pop(),
|
||||
});
|
||||
} else if (this.state === "error") {
|
||||
rows.push({
|
||||
label: `Failed: ${truncateModelLabel(this.error || "Discovery failed", 40)}`,
|
||||
disabled: true,
|
||||
description: `${this.error}. Manual model entry is still available.`,
|
||||
onSelect: () => {},
|
||||
});
|
||||
rows.push({
|
||||
label: "Try again",
|
||||
description: "Query this server's /models endpoint again without changing the saved model.",
|
||||
onSelect: (innerCtx) => { void this.refresh(innerCtx, true); },
|
||||
});
|
||||
} else {
|
||||
const saved = this.settings.getModel().trim();
|
||||
const listed = this.models.filter((model) => this.showOffline || !model.offline || model.id === saved);
|
||||
const savedModel = saved ? this.models.find((model) => model.id === saved) : undefined;
|
||||
if (saved && !savedModel) {
|
||||
rows.push(this.modelRow({
|
||||
id: saved, name: saved, supportsTools: null, offline: false, deprecated: false, beta: false,
|
||||
}, "saved; not listed"));
|
||||
}
|
||||
if (!listed.length) {
|
||||
rows.push({
|
||||
label: this.models.length ? "No online models listed" : "No models listed",
|
||||
disabled: true,
|
||||
description: "Keep the saved model or use manual entry.",
|
||||
onSelect: () => {},
|
||||
});
|
||||
} else {
|
||||
rows.push(...listed.map((model) => this.modelRow(model)));
|
||||
}
|
||||
if (this.models.some((model) => model.offline)) {
|
||||
rows.push({
|
||||
label: this.showOffline ? "Hide offline models" : `Show offline (${this.models.filter((model) => model.offline).length})`,
|
||||
description: "Offline models normally fail inference until the provider restores them.",
|
||||
onSelect: (innerCtx) => {
|
||||
this.showOffline = !this.showOffline;
|
||||
this.updateRows();
|
||||
innerCtx.actions.requestRender();
|
||||
},
|
||||
});
|
||||
}
|
||||
rows.push({
|
||||
label: "Refresh models",
|
||||
description: "Fetch a fresh catalog. This never changes the selected model by itself.",
|
||||
onSelect: (innerCtx) => { void this.refresh(innerCtx, true); },
|
||||
});
|
||||
}
|
||||
rows.push({
|
||||
label: "Enter model manually",
|
||||
description: "Use an exact model ID when discovery is unavailable or the desired model is not listed.",
|
||||
onSelect: (innerCtx) => {
|
||||
this.requestGeneration++; // Any late discovery result belongs to the closed picker.
|
||||
void innerCtx.actions.startTextSettingEdit(this.modelSetting);
|
||||
innerCtx.stack.popThrough(this);
|
||||
innerCtx.stack.push(new EditTextSettingLayer(this.modelSetting));
|
||||
},
|
||||
});
|
||||
const current = this.settings.getModel().trim();
|
||||
const selected = Math.max(0, rows.findIndex((row) => this.modelIdByRow.get(row) === current));
|
||||
this.setItems(rows, selected);
|
||||
}
|
||||
|
||||
private modelRow(model: DiscoveredCompatibleModel, extraLabel?: string): MenuItem {
|
||||
const flags: string[] = [];
|
||||
if (model.supportsTools === true) flags.push("tools");
|
||||
else if (model.supportsTools === false) flags.push("no tools");
|
||||
else flags.push("tools unknown");
|
||||
if (model.offline) flags.push("offline");
|
||||
if (model.deprecated) flags.push("deprecated");
|
||||
if (model.beta) flags.push("beta");
|
||||
if (extraLabel) flags.push(extraLabel);
|
||||
const selected = this.settings.getModel().trim() === model.id ? " *" : "";
|
||||
const displayName = model.name === model.id ? model.id : `${model.name} (${model.id})`;
|
||||
const toolFlag = model.supportsTools === true ? "tools" : model.supportsTools === false ? "no tools" : "tools?";
|
||||
const visibleFlags = [toolFlag];
|
||||
if (model.offline) visibleFlags.push("offline");
|
||||
if (model.deprecated) visibleFlags.push("old");
|
||||
if (model.beta) visibleFlags.push("beta");
|
||||
if (extraLabel) visibleFlags.push("saved only");
|
||||
const flagSuffix = ` [${visibleFlags.join("/")}]`;
|
||||
const row: MenuItem = {
|
||||
label: `${truncateModelLabel(displayName, Math.max(12, 44 - flagSuffix.length))}${flagSuffix}${selected}`,
|
||||
description: `${model.name}: ${model.id} — ${flags.join(", ")}. Tool support unknown means the server did not advertise it; test before relying on glasses actions.`,
|
||||
onSelect: (innerCtx) => {
|
||||
// Re-read immediately before saving; a late catalog never overwrites a
|
||||
// selection made against a different endpoint.
|
||||
if (this.settings.getBaseUrl().trim() + "\n" + this.settings.getApiKey().trim() !== this.endpointKey) return;
|
||||
this.settings.setModel(model.id);
|
||||
innerCtx.stack.popThrough(this);
|
||||
},
|
||||
};
|
||||
this.modelIdByRow.set(row, model.id);
|
||||
return row;
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed = true;
|
||||
this.requestGeneration++; // Invalidate the pending result immediately.
|
||||
}
|
||||
}
|
||||
|
||||
/** Kept separate so tests can verify the normal Faceclaw modal sizing contract. */
|
||||
function discoveryLayout(viewport: { width: number; height: number }): ConstructorParameters<typeof MenuLayer>[2] {
|
||||
return {
|
||||
x: "center",
|
||||
y: 8,
|
||||
width: Math.min(320, viewport.width - 40),
|
||||
minHeight: Math.min(160, viewport.height - 40),
|
||||
maxHeight: Math.min(240, viewport.height - 40),
|
||||
depth: 4,
|
||||
};
|
||||
}
|
||||
|
||||
function truncateModelLabel(value: string, maxLength = 44): string {
|
||||
if (value.length <= maxLength) return value;
|
||||
const tailLength = Math.min(19, Math.max(4, maxLength - 8));
|
||||
const headLength = Math.max(1, maxLength - tailLength - 3);
|
||||
return `${value.slice(0, headLength)}...${value.slice(-tailLength)}`;
|
||||
}
|
||||
|
||||
/** One stable Assistant settings row; discovery state belongs to the pushed picker. */
|
||||
export function compatibleModelDiscoveryMenuItem(
|
||||
settings: ModelDiscoverySettings,
|
||||
modelSetting: ConstructorParameters<typeof EditTextSettingLayer>[0],
|
||||
discover: Discover = discoverOpenAiCompatibleModels,
|
||||
): MenuItem {
|
||||
return {
|
||||
label: "Discover models",
|
||||
description: DISCOVERY_EXPLANATION,
|
||||
disabled: () => !settings.getBaseUrl().trim(),
|
||||
onSelect: (ctx) => new OpenAiCompatibleModelPicker(settings, modelSetting, discover, ctx.stack.getBaseSize()).open(ctx),
|
||||
};
|
||||
}
|
||||
@@ -38,7 +38,11 @@ import {
|
||||
assistantBridgePortSetting,
|
||||
assistantBridgeTokenSetting,
|
||||
assistantModelSetting,
|
||||
assistantOpenAiCompatibleKeySetting,
|
||||
assistantOpenAiCompatibleModelSetting,
|
||||
assistantOpenAiCompatibleUrlSetting,
|
||||
assistantSkipConfirmationSetting,
|
||||
veniceWebSearchSetting,
|
||||
batteryIndicatorSettingsKey,
|
||||
brightnessSetting,
|
||||
onAnySettingChanged,
|
||||
@@ -1720,6 +1724,10 @@ class Shell {
|
||||
openai: openAiApiKeySetting.get(),
|
||||
hermesUrl: assistantHermesUrlSetting.get(),
|
||||
hermesKey: assistantHermesKeySetting.get(),
|
||||
openAiCompatibleUrl: assistantOpenAiCompatibleUrlSetting.get(),
|
||||
openAiCompatibleKey: assistantOpenAiCompatibleKeySetting.get(),
|
||||
openAiCompatibleModel: assistantOpenAiCompatibleModelSetting.get(),
|
||||
veniceWebSearch: veniceWebSearchSetting.get(),
|
||||
});
|
||||
if (llm && reasoning !== "default" && llm.effort) llm.effort = reasoning;
|
||||
return llm ? { kind: "direct", llm } : null;
|
||||
@@ -1749,7 +1757,7 @@ class Shell {
|
||||
this.showAlert(
|
||||
assistantBackendSetting.get() === "external"
|
||||
? "Configure the agent bridge host and token in Settings."
|
||||
: global.isIOS ? "Set an OpenAI or Anthropic API key in Settings." : "Set an API key or download the on-phone model in Settings.",
|
||||
: global.isIOS ? "Configure an assistant model in Settings." : "Set an API key or download the on-phone model in Settings.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user