feat: add zen model selection to settings (#415)

This commit is contained in:
gsxdsm
2026-02-13 05:37:28 +02:00
committed by GitHub
parent 2d529ddaaa
commit 2f09d375d3
14 changed files with 287 additions and 28 deletions
@@ -11,6 +11,11 @@ import { useConfigStore } from '@/stores/useConfigStore';
import { getRegisteredRuntimeAPIs } from '@/contexts/runtimeAPIRegistry'; import { getRegisteredRuntimeAPIs } from '@/contexts/runtimeAPIRegistry';
import { getModifierLabel } from '@/lib/utils'; import { getModifierLabel } from '@/lib/utils';
interface ZenModel {
id: string;
owned_by?: string;
}
const FALLBACK_PROVIDER_ID = 'opencode'; const FALLBACK_PROVIDER_ID = 'opencode';
const FALLBACK_MODEL_ID = 'big-pickle'; const FALLBACK_MODEL_ID = 'big-pickle';
@@ -48,12 +53,16 @@ export const DefaultsSettings: React.FC = () => {
const setSettingsDefaultAgent = useConfigStore((state) => state.setSettingsDefaultAgent); const setSettingsDefaultAgent = useConfigStore((state) => state.setSettingsDefaultAgent);
const settingsAutoCreateWorktree = useConfigStore((state) => state.settingsAutoCreateWorktree); const settingsAutoCreateWorktree = useConfigStore((state) => state.settingsAutoCreateWorktree);
const setSettingsAutoCreateWorktree = useConfigStore((state) => state.setSettingsAutoCreateWorktree); const setSettingsAutoCreateWorktree = useConfigStore((state) => state.setSettingsAutoCreateWorktree);
const settingsZenModel = useConfigStore((state) => state.settingsZenModel);
const setSettingsZenModel = useConfigStore((state) => state.setSettingsZenModel);
const providers = useConfigStore((state) => state.providers); const providers = useConfigStore((state) => state.providers);
const [defaultModel, setDefaultModel] = React.useState<string | undefined>(); const [defaultModel, setDefaultModel] = React.useState<string | undefined>();
const [defaultVariant, setDefaultVariant] = React.useState<string | undefined>(); const [defaultVariant, setDefaultVariant] = React.useState<string | undefined>();
const [defaultAgent, setDefaultAgent] = React.useState<string | undefined>(); const [defaultAgent, setDefaultAgent] = React.useState<string | undefined>();
const [isLoading, setIsLoading] = React.useState(true); const [isLoading, setIsLoading] = React.useState(true);
const [zenModels, setZenModels] = React.useState<ZenModel[]>([]);
const [zenModelsLoading, setZenModelsLoading] = React.useState(true);
const parsedModel = React.useMemo(() => { const parsedModel = React.useMemo(() => {
return getDisplayModel(defaultModel, providers); return getDisplayModel(defaultModel, providers);
@@ -61,11 +70,43 @@ export const DefaultsSettings: React.FC = () => {
const isVSCode = React.useMemo(() => isVSCodeRuntime(), []); const isVSCode = React.useMemo(() => isVSCodeRuntime(), []);
// Load zen models list
React.useEffect(() => {
const loadZenModels = async () => {
try {
const response = await fetch('/api/zen/models', {
method: 'GET',
headers: { Accept: 'application/json' },
});
if (response.ok) {
const data = await response.json() as { models?: ZenModel[] };
if (Array.isArray(data?.models)) {
setZenModels(data.models);
}
}
} catch (error) {
console.warn('Failed to load zen models:', error);
} finally {
setZenModelsLoading(false);
}
};
loadZenModels();
}, []);
// Resolve which zen model to display as selected
const selectedZenModel = React.useMemo(() => {
if (settingsZenModel && zenModels.some((m) => m.id === settingsZenModel)) {
return settingsZenModel;
}
// Default to first free model in the list
return zenModels[0]?.id ?? '';
}, [settingsZenModel, zenModels]);
// Load current settings // Load current settings
React.useEffect(() => { React.useEffect(() => {
const loadSettings = async () => { const loadSettings = async () => {
try { try {
let data: { defaultModel?: string; defaultVariant?: string; defaultAgent?: string } | null = null; let data: { defaultModel?: string; defaultVariant?: string; defaultAgent?: string; zenModel?: string } | null = null;
// 1. Runtime settings API (VSCode) // 1. Runtime settings API (VSCode)
if (!data) { if (!data) {
@@ -79,6 +120,7 @@ export const DefaultsSettings: React.FC = () => {
defaultModel: typeof settings.defaultModel === 'string' ? settings.defaultModel : undefined, defaultModel: typeof settings.defaultModel === 'string' ? settings.defaultModel : undefined,
defaultVariant: typeof (settings as Record<string, unknown>).defaultVariant === 'string' ? ((settings as Record<string, unknown>).defaultVariant as string) : undefined, defaultVariant: typeof (settings as Record<string, unknown>).defaultVariant === 'string' ? ((settings as Record<string, unknown>).defaultVariant as string) : undefined,
defaultAgent: typeof settings.defaultAgent === 'string' ? settings.defaultAgent : undefined, defaultAgent: typeof settings.defaultAgent === 'string' ? settings.defaultAgent : undefined,
zenModel: typeof (settings as Record<string, unknown>).zenModel === 'string' ? ((settings as Record<string, unknown>).zenModel as string) : undefined,
}; };
} }
} catch { } catch {
@@ -102,6 +144,7 @@ export const DefaultsSettings: React.FC = () => {
const model = typeof data.defaultModel === 'string' && data.defaultModel.trim().length > 0 ? data.defaultModel.trim() : undefined; const model = typeof data.defaultModel === 'string' && data.defaultModel.trim().length > 0 ? data.defaultModel.trim() : undefined;
const variant = typeof data.defaultVariant === 'string' && data.defaultVariant.trim().length > 0 ? data.defaultVariant.trim() : undefined; const variant = typeof data.defaultVariant === 'string' && data.defaultVariant.trim().length > 0 ? data.defaultVariant.trim() : undefined;
const agent = typeof data.defaultAgent === 'string' && data.defaultAgent.trim().length > 0 ? data.defaultAgent.trim() : undefined; const agent = typeof data.defaultAgent === 'string' && data.defaultAgent.trim().length > 0 ? data.defaultAgent.trim() : undefined;
const zen = typeof data.zenModel === 'string' && data.zenModel.trim().length > 0 ? data.zenModel.trim() : undefined;
if (model !== undefined) { if (model !== undefined) {
setDefaultModel(model); setDefaultModel(model);
@@ -112,6 +155,9 @@ export const DefaultsSettings: React.FC = () => {
if (agent !== undefined) { if (agent !== undefined) {
setDefaultAgent(agent); setDefaultAgent(agent);
} }
if (zen !== undefined) {
setSettingsZenModel(zen);
}
} }
} catch (error) { } catch (error) {
console.warn('Failed to load defaults settings:', error); console.warn('Failed to load defaults settings:', error);
@@ -120,7 +166,7 @@ export const DefaultsSettings: React.FC = () => {
} }
}; };
loadSettings(); loadSettings();
}, []); }, [setSettingsZenModel]);
const handleModelChange = React.useCallback(async (providerId: string, modelId: string) => { const handleModelChange = React.useCallback(async (providerId: string, modelId: string) => {
@@ -241,6 +287,16 @@ export const DefaultsSettings: React.FC = () => {
} }
}, [setSettingsAutoCreateWorktree]); }, [setSettingsAutoCreateWorktree]);
const handleZenModelChange = React.useCallback(async (modelId: string) => {
setSettingsZenModel(modelId);
try {
await updateDesktopSettings({
zenModel: modelId,
});
} catch (error) {
console.warn('Failed to save zen model setting:', error);
}
}, [setSettingsZenModel]);
if (isLoading) { if (isLoading) {
return null; return null;
@@ -333,6 +389,47 @@ export const DefaultsSettings: React.FC = () => {
</p> </p>
</div> </div>
)} )}
<div className="border-t border-border/40 pt-4 mt-4 space-y-3">
<div className="space-y-1">
<div className="flex items-center gap-2">
<h3 className="typography-ui-header font-semibold text-foreground">Zen Model</h3>
<Tooltip delayDuration={1000}>
<TooltipTrigger asChild>
<RiInformationLine className="h-3.5 w-3.5 text-muted-foreground/60 cursor-help" />
</TooltipTrigger>
<TooltipContent sideOffset={8} className="max-w-xs">
The free model used for lightweight internal tasks like commit message generation, PR descriptions, notification summarization, and TTS text summarization.
</TooltipContent>
</Tooltip>
</div>
<p className="typography-meta text-muted-foreground">
Used for commit messages, PR descriptions, and text summarization.
</p>
</div>
<div className="flex flex-col gap-1.5">
<label className="typography-ui-label text-muted-foreground">Model</label>
{zenModelsLoading ? (
<span className="typography-meta text-muted-foreground">Loading models...</span>
) : zenModels.length > 0 ? (
<Select value={selectedZenModel} onValueChange={handleZenModelChange}>
<SelectTrigger className="w-auto max-w-xs typography-meta text-foreground">
<SelectValue placeholder="Select model" />
</SelectTrigger>
<SelectContent>
{zenModels.map((model) => (
<SelectItem key={model.id} value={model.id} className="pr-2 [&>span:first-child]:hidden">
{model.id}
</SelectItem>
))}
</SelectContent>
</Select>
) : (
<span className="typography-meta text-muted-foreground">No free models available</span>
)}
</div>
</div>
</div> </div>
); );
}; };
@@ -38,7 +38,7 @@ const OPENCHAMBER_SECTION_GROUPS: SectionGroup[] = [
{ {
id: 'sessions', id: 'sessions',
label: 'Sessions', label: 'Sessions',
items: ['Defaults', 'Retention'], items: ['Defaults', 'Zen Model', 'Retention'],
}, },
{ {
id: 'git', id: 'git',
+3 -1
View File
@@ -839,9 +839,11 @@ export const GitView: React.FC<GitViewProps> = ({ mode = 'full' }) => {
setIsGeneratingMessage(true); setIsGeneratingMessage(true);
try { try {
const zenModel = useConfigStore.getState().settingsZenModel;
const { message } = await git.generateCommitMessage( const { message } = await git.generateCommitMessage(
currentDirectory, currentDirectory,
Array.from(selectedPaths) Array.from(selectedPaths),
zenModel ? { zenModel } : undefined
); );
const subject = message.subject?.trim() ?? ''; const subject = message.subject?.trim() ?? '';
const highlights = Array.isArray(message.highlights) ? message.highlights : []; const highlights = Array.isArray(message.highlights) ? message.highlights : [];
@@ -951,10 +951,12 @@ export const PullRequestSection: React.FC<{
if (!directory) return; if (!directory) return;
setIsGenerating(true); setIsGenerating(true);
try { try {
const zenModel = useConfigStore.getState().settingsZenModel;
const generated = await generatePullRequestDescription(directory, { const generated = await generatePullRequestDescription(directory, {
base: baseBranch, base: baseBranch,
head: branch, head: branch,
context: additionalContext, context: additionalContext,
...(zenModel ? { zenModel } : {}),
}); });
if (generated.title?.trim()) { if (generated.title?.trim()) {
+3 -2
View File
@@ -78,7 +78,7 @@ export function useServerTTS(): UseServerTTSReturn {
const abortControllerRef = useRef<AbortController | null>(null); const abortControllerRef = useRef<AbortController | null>(null);
// Get current model, threshold, and max length from config store for summarization // Get current model, threshold, and max length from config store for summarization
const { currentProviderId, currentModelId, summarizeCharacterThreshold, summarizeMaxLength, openaiApiKey } = useConfigStore(); const { currentProviderId, currentModelId, summarizeCharacterThreshold, summarizeMaxLength, openaiApiKey, settingsZenModel } = useConfigStore();
// Check if server TTS is available // Check if server TTS is available
const checkAvailability = useCallback(async (): Promise<boolean> => { const checkAvailability = useCallback(async (): Promise<boolean> => {
@@ -209,6 +209,7 @@ export function useServerTTS(): UseServerTTSReturn {
maxLength: summarizeMaxLength ?? 500, maxLength: summarizeMaxLength ?? 500,
// Send API key from settings if available // Send API key from settings if available
apiKey: openaiApiKey || undefined, apiKey: openaiApiKey || undefined,
...(settingsZenModel ? { zenModel: settingsZenModel } : {}),
}), }),
signal: abortControllerRef.current.signal, signal: abortControllerRef.current.signal,
}); });
@@ -258,7 +259,7 @@ export function useServerTTS(): UseServerTTSReturn {
options?.onError?.(errorMsg); options?.onError?.(errorMsg);
setIsPlaying(false); setIsPlaying(false);
} }
}, [stop, currentProviderId, currentModelId, summarizeCharacterThreshold, summarizeMaxLength, openaiApiKey]); }, [stop, currentProviderId, currentModelId, summarizeCharacterThreshold, summarizeMaxLength, openaiApiKey, settingsZenModel]);
// Cleanup on unmount // Cleanup on unmount
useEffect(() => { useEffect(() => {
+2 -2
View File
@@ -332,10 +332,10 @@ export interface GitAPI {
getGitBranches(directory: string): Promise<GitBranch>; getGitBranches(directory: string): Promise<GitBranch>;
deleteGitBranch(directory: string, payload: GitDeleteBranchPayload): Promise<{ success: boolean }>; deleteGitBranch(directory: string, payload: GitDeleteBranchPayload): Promise<{ success: boolean }>;
deleteRemoteBranch(directory: string, payload: GitDeleteRemoteBranchPayload): Promise<{ success: boolean }>; deleteRemoteBranch(directory: string, payload: GitDeleteRemoteBranchPayload): Promise<{ success: boolean }>;
generateCommitMessage(directory: string, files: string[]): Promise<{ message: GeneratedCommitMessage }>; generateCommitMessage(directory: string, files: string[], options?: { zenModel?: string }): Promise<{ message: GeneratedCommitMessage }>;
generatePullRequestDescription( generatePullRequestDescription(
directory: string, directory: string,
payload: { base: string; head: string; context?: string } payload: { base: string; head: string; context?: string; zenModel?: string }
): Promise<GeneratedPullRequestDescription>; ): Promise<GeneratedPullRequestDescription>;
listGitWorktrees(directory: string): Promise<GitWorktreeInfo[]>; listGitWorktrees(directory: string): Promise<GitWorktreeInfo[]>;
createGitCommit(directory: string, message: string, options?: CreateGitCommitOptions): Promise<GitCommitResult>; createGitCommit(directory: string, message: string, options?: CreateGitCommitOptions): Promise<GitCommitResult>;
+1
View File
@@ -91,6 +91,7 @@ export type DesktopSettings = {
autoCreateWorktree?: boolean; autoCreateWorktree?: boolean;
queueModeEnabled?: boolean; queueModeEnabled?: boolean;
gitmojiEnabled?: boolean; gitmojiEnabled?: boolean;
zenModel?: string;
toolCallExpansion?: 'collapsed' | 'activity' | 'detailed'; toolCallExpansion?: 'collapsed' | 'activity' | 'detailed';
fontSize?: number; fontSize?: number;
terminalFontSize?: number; terminalFontSize?: number;
+4 -3
View File
@@ -99,16 +99,17 @@ export async function deleteRemoteBranch(directory: string, payload: import('./a
export async function generateCommitMessage( export async function generateCommitMessage(
directory: string, directory: string,
files: string[] files: string[],
options?: { zenModel?: string }
): Promise<{ message: import('./api/types').GeneratedCommitMessage }> { ): Promise<{ message: import('./api/types').GeneratedCommitMessage }> {
const runtime = getRuntimeGit(); const runtime = getRuntimeGit();
if (runtime) return runtime.generateCommitMessage(directory, files); if (runtime) return runtime.generateCommitMessage(directory, files);
return gitHttp.generateCommitMessage(directory, files); return gitHttp.generateCommitMessage(directory, files, options);
} }
export async function generatePullRequestDescription( export async function generatePullRequestDescription(
directory: string, directory: string,
payload: { base: string; head: string; context?: string } payload: { base: string; head: string; context?: string; zenModel?: string }
): Promise<import('./api/types').GeneratedPullRequestDescription> { ): Promise<import('./api/types').GeneratedPullRequestDescription> {
const runtime = getRuntimeGit(); const runtime = getRuntimeGit();
if (runtime?.generatePullRequestDescription) { if (runtime?.generatePullRequestDescription) {
+14 -5
View File
@@ -205,16 +205,22 @@ export async function deleteRemoteBranch(directory: string, payload: GitDeleteRe
export async function generateCommitMessage( export async function generateCommitMessage(
directory: string, directory: string,
files: string[] files: string[],
options?: { zenModel?: string }
): Promise<{ message: GeneratedCommitMessage }> { ): Promise<{ message: GeneratedCommitMessage }> {
if (!Array.isArray(files) || files.length === 0) { if (!Array.isArray(files) || files.length === 0) {
throw new Error('No files provided to generate commit message'); throw new Error('No files provided to generate commit message');
} }
const body: Record<string, unknown> = { files };
if (options?.zenModel) {
body.zenModel = options.zenModel;
}
const response = await fetch(buildUrl(`${API_BASE}/commit-message`, directory), { const response = await fetch(buildUrl(`${API_BASE}/commit-message`, directory), {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ files }), body: JSON.stringify(body),
}); });
if (!response.ok) { if (!response.ok) {
@@ -249,17 +255,20 @@ export async function generateCommitMessage(
export async function generatePullRequestDescription( export async function generatePullRequestDescription(
directory: string, directory: string,
payload: { base: string; head: string; context?: string } payload: { base: string; head: string; context?: string; zenModel?: string }
): Promise<{ title: string; body: string }> { ): Promise<{ title: string; body: string }> {
const { base, head, context } = payload; const { base, head, context, zenModel } = payload;
if (!base || !head) { if (!base || !head) {
throw new Error('base and head are required'); throw new Error('base and head are required');
} }
const requestBody: { base: string; head: string; context?: string } = { base, head }; const requestBody: { base: string; head: string; context?: string; zenModel?: string } = { base, head };
if (context?.trim()) { if (context?.trim()) {
requestBody.context = context.trim(); requestBody.context = context.trim();
} }
if (zenModel) {
requestBody.zenModel = zenModel;
}
const response = await fetch(buildUrl(`${API_BASE}/pr-description`, directory), { const response = await fetch(buildUrl(`${API_BASE}/pr-description`, directory), {
method: 'POST', method: 'POST',
+2 -1
View File
@@ -33,12 +33,13 @@ export async function summarizeText(
} }
try { try {
const zenModel = store.settingsZenModel;
const response = await fetch('/api/tts/summarize', { const response = await fetch('/api/tts/summarize', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ text, threshold, maxLength }), body: JSON.stringify({ text, threshold, maxLength, ...(zenModel ? { zenModel } : {}) }),
}); });
if (!response.ok) { if (!response.ok) {
+14
View File
@@ -25,6 +25,7 @@ interface OpenChamberDefaults {
defaultAgent?: string; defaultAgent?: string;
autoCreateWorktree?: boolean; autoCreateWorktree?: boolean;
gitmojiEnabled?: boolean; gitmojiEnabled?: boolean;
zenModel?: string;
} }
const fetchOpenChamberDefaults = async (): Promise<OpenChamberDefaults> => { const fetchOpenChamberDefaults = async (): Promise<OpenChamberDefaults> => {
@@ -40,6 +41,7 @@ const fetchOpenChamberDefaults = async (): Promise<OpenChamberDefaults> => {
const defaultVariant = typeof data?.defaultVariant === 'string' ? data.defaultVariant.trim() : ''; const defaultVariant = typeof data?.defaultVariant === 'string' ? data.defaultVariant.trim() : '';
const defaultAgent = typeof data?.defaultAgent === 'string' ? data.defaultAgent.trim() : ''; const defaultAgent = typeof data?.defaultAgent === 'string' ? data.defaultAgent.trim() : '';
const gitmojiEnabled = typeof data?.gitmojiEnabled === 'boolean' ? data.gitmojiEnabled : undefined; const gitmojiEnabled = typeof data?.gitmojiEnabled === 'boolean' ? data.gitmojiEnabled : undefined;
const zenModel = typeof data?.zenModel === 'string' ? data.zenModel.trim() : '';
return { return {
defaultModel: defaultModel.length > 0 ? defaultModel : undefined, defaultModel: defaultModel.length > 0 ? defaultModel : undefined,
@@ -47,6 +49,7 @@ const fetchOpenChamberDefaults = async (): Promise<OpenChamberDefaults> => {
defaultAgent: defaultAgent.length > 0 ? defaultAgent : undefined, defaultAgent: defaultAgent.length > 0 ? defaultAgent : undefined,
autoCreateWorktree: typeof data?.autoCreateWorktree === 'boolean' ? data.autoCreateWorktree : undefined, autoCreateWorktree: typeof data?.autoCreateWorktree === 'boolean' ? data.autoCreateWorktree : undefined,
gitmojiEnabled, gitmojiEnabled,
zenModel: zenModel.length > 0 ? zenModel : undefined,
}; };
} }
} catch { } catch {
@@ -67,6 +70,7 @@ const fetchOpenChamberDefaults = async (): Promise<OpenChamberDefaults> => {
const defaultVariant = typeof data?.defaultVariant === 'string' ? data.defaultVariant.trim() : ''; const defaultVariant = typeof data?.defaultVariant === 'string' ? data.defaultVariant.trim() : '';
const defaultAgent = typeof data?.defaultAgent === 'string' ? data.defaultAgent.trim() : ''; const defaultAgent = typeof data?.defaultAgent === 'string' ? data.defaultAgent.trim() : '';
const gitmojiEnabled = typeof data?.gitmojiEnabled === 'boolean' ? data.gitmojiEnabled : undefined; const gitmojiEnabled = typeof data?.gitmojiEnabled === 'boolean' ? data.gitmojiEnabled : undefined;
const zenModel = typeof data?.zenModel === 'string' ? data.zenModel.trim() : '';
return { return {
defaultModel: defaultModel.length > 0 ? defaultModel : undefined, defaultModel: defaultModel.length > 0 ? defaultModel : undefined,
@@ -74,6 +78,7 @@ const fetchOpenChamberDefaults = async (): Promise<OpenChamberDefaults> => {
defaultAgent: defaultAgent.length > 0 ? defaultAgent : undefined, defaultAgent: defaultAgent.length > 0 ? defaultAgent : undefined,
autoCreateWorktree: typeof data?.autoCreateWorktree === 'boolean' ? data.autoCreateWorktree : undefined, autoCreateWorktree: typeof data?.autoCreateWorktree === 'boolean' ? data.autoCreateWorktree : undefined,
gitmojiEnabled, gitmojiEnabled,
zenModel: zenModel.length > 0 ? zenModel : undefined,
}; };
} catch { } catch {
return {}; return {};
@@ -366,6 +371,7 @@ interface ConfigStore {
settingsDefaultAgent: string | undefined; settingsDefaultAgent: string | undefined;
settingsAutoCreateWorktree: boolean; settingsAutoCreateWorktree: boolean;
settingsGitmojiEnabled: boolean; settingsGitmojiEnabled: boolean;
settingsZenModel: string | undefined;
// Voice provider preference ('browser', 'openai', or 'say' for macOS) // Voice provider preference ('browser', 'openai', or 'say' for macOS)
voiceProvider: 'browser' | 'openai' | 'say'; voiceProvider: 'browser' | 'openai' | 'say';
setVoiceProvider: (provider: 'browser' | 'openai' | 'say') => void; setVoiceProvider: (provider: 'browser' | 'openai' | 'say') => void;
@@ -414,6 +420,7 @@ interface ConfigStore {
setSettingsDefaultAgent: (agent: string | undefined) => void; setSettingsDefaultAgent: (agent: string | undefined) => void;
setSettingsAutoCreateWorktree: (enabled: boolean) => void; setSettingsAutoCreateWorktree: (enabled: boolean) => void;
setSettingsGitmojiEnabled: (enabled: boolean) => void; setSettingsGitmojiEnabled: (enabled: boolean) => void;
setSettingsZenModel: (model: string | undefined) => void;
saveAgentModelSelection: (agentName: string, providerId: string, modelId: string) => void; saveAgentModelSelection: (agentName: string, providerId: string, modelId: string) => void;
getAgentModelSelection: (agentName: string) => { providerId: string; modelId: string } | null; getAgentModelSelection: (agentName: string) => { providerId: string; modelId: string } | null;
checkConnection: () => Promise<boolean>; checkConnection: () => Promise<boolean>;
@@ -458,6 +465,7 @@ export const useConfigStore = create<ConfigStore>()(
settingsDefaultAgent: undefined, settingsDefaultAgent: undefined,
settingsAutoCreateWorktree: false, settingsAutoCreateWorktree: false,
settingsGitmojiEnabled: false, settingsGitmojiEnabled: false,
settingsZenModel: undefined,
// Voice provider preference - load from localStorage or default to 'browser' // Voice provider preference - load from localStorage or default to 'browser'
voiceProvider: (() => { voiceProvider: (() => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
@@ -1023,6 +1031,7 @@ export const useConfigStore = create<ConfigStore>()(
settingsDefaultAgent: openChamberDefaults.defaultAgent, settingsDefaultAgent: openChamberDefaults.defaultAgent,
settingsAutoCreateWorktree: openChamberDefaults.autoCreateWorktree ?? false, settingsAutoCreateWorktree: openChamberDefaults.autoCreateWorktree ?? false,
settingsGitmojiEnabled: openChamberDefaults.gitmojiEnabled ?? false, settingsGitmojiEnabled: openChamberDefaults.gitmojiEnabled ?? false,
settingsZenModel: openChamberDefaults.zenModel,
directoryScoped: { directoryScoped: {
...state.directoryScoped, ...state.directoryScoped,
[directoryKey]: nextSnapshot, [directoryKey]: nextSnapshot,
@@ -1447,6 +1456,10 @@ export const useConfigStore = create<ConfigStore>()(
set({ settingsGitmojiEnabled: enabled }); set({ settingsGitmojiEnabled: enabled });
}, },
setSettingsZenModel: (model: string | undefined) => {
set({ settingsZenModel: model });
},
setVoiceProvider: (provider: 'browser' | 'openai' | 'say') => { setVoiceProvider: (provider: 'browser' | 'openai' | 'say') => {
set({ voiceProvider: provider }); set({ voiceProvider: provider });
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
@@ -1656,6 +1669,7 @@ export const useConfigStore = create<ConfigStore>()(
settingsDefaultAgent: state.settingsDefaultAgent, settingsDefaultAgent: state.settingsDefaultAgent,
settingsAutoCreateWorktree: state.settingsAutoCreateWorktree, settingsAutoCreateWorktree: state.settingsAutoCreateWorktree,
settingsGitmojiEnabled: state.settingsGitmojiEnabled, settingsGitmojiEnabled: state.settingsGitmojiEnabled,
settingsZenModel: state.settingsZenModel,
speechRate: state.speechRate, speechRate: state.speechRate,
speechPitch: state.speechPitch, speechPitch: state.speechPitch,
speechVolume: state.speechVolume, speechVolume: state.speechVolume,
+4 -1
View File
@@ -2540,11 +2540,14 @@ export async function handleBridgeMessage(message: BridgeRequest, ctx?: BridgeCo
const prompt = `You are drafting a GitHub Pull Request title + description. Respond in JSON of the shape {"title": string, "body": string} (ONLY JSON in response, no markdown fences) with these rules:\n- title: concise, sentence case, <= 80 chars, no trailing punctuation, no commit-style prefixes (no "feat:", "fix:")\n- body: GitHub-flavored markdown with these sections in this order: Summary, Testing, Notes\n- Summary: 3-6 bullet points describing user-visible changes; avoid internal helper function names\n- Testing: bullet list ("- Not tested" allowed)\n- Notes: bullet list; include breaking/rollout notes only when relevant\n\nContext:\n- base branch: ${base}\n- head branch: ${head}\n\nDiff summary:\n${diffSummaries}`; const prompt = `You are drafting a GitHub Pull Request title + description. Respond in JSON of the shape {"title": string, "body": string} (ONLY JSON in response, no markdown fences) with these rules:\n- title: concise, sentence case, <= 80 chars, no trailing punctuation, no commit-style prefixes (no "feat:", "fix:")\n- body: GitHub-flavored markdown with these sections in this order: Summary, Testing, Notes\n- Summary: 3-6 bullet points describing user-visible changes; avoid internal helper function names\n- Testing: bullet list ("- Not tested" allowed)\n- Notes: bullet list; include breaking/rollout notes only when relevant\n\nContext:\n- base branch: ${base}\n- head branch: ${head}\n\nDiff summary:\n${diffSummaries}`;
try { try {
const zenSettings = readSettings(ctx) as Record<string, unknown>;
const zenModelRaw = typeof zenSettings?.zenModel === 'string' ? (zenSettings.zenModel as string).trim() : '';
const zenModel = zenModelRaw.length > 0 ? zenModelRaw : 'gpt-5-nano';
const response = await fetch('https://opencode.ai/zen/v1/responses', { const response = await fetch('https://opencode.ai/zen/v1/responses', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
model: 'gpt-5-nano', model: zenModel,
input: [{ role: 'user', content: prompt }], input: [{ role: 'user', content: prompt }],
max_output_tokens: 1200, max_output_tokens: 1200,
stream: false, stream: false,
+134 -8
View File
@@ -588,7 +588,76 @@ const shouldApplyResolvedTemplateMessage = (template, resolved, variables) => {
return true; return true;
}; };
const summarizeText = async (text, targetLength) => { const ZEN_DEFAULT_MODEL = 'gpt-5-nano';
/**
* Validated fallback zen model determined at startup by checking available free
* models from the zen API. When `null`, startup validation hasn't run yet (or
* failed), so `resolveZenModel` falls back to `ZEN_DEFAULT_MODEL`.
*/
let validatedZenFallback = null;
/** Cached free zen models response and timestamp (shared by startup + endpoint). */
let cachedZenModels = null;
let cachedZenModelsTimestamp = 0;
const ZEN_MODELS_CACHE_TTL = 5 * 60 * 1000; // 5 minutes
/**
* Fetch free models from the zen API with caching. Returns an array of
* `{ id, owned_by }` objects (may be empty on failure). Results are cached
* for `ZEN_MODELS_CACHE_TTL` ms.
*/
const fetchFreeZenModels = async () => {
const now = Date.now();
if (cachedZenModels && now - cachedZenModelsTimestamp < ZEN_MODELS_CACHE_TTL) {
return cachedZenModels.models;
}
const controller = typeof AbortController !== 'undefined' ? new AbortController() : null;
const timeout = controller ? setTimeout(() => controller.abort(), 8000) : null;
try {
const response = await fetch('https://opencode.ai/zen/v1/models', {
signal: controller?.signal,
headers: { Accept: 'application/json' },
});
if (!response.ok) {
throw new Error(`zen/v1/models responded with status ${response.status}`);
}
const data = await response.json();
const allModels = Array.isArray(data?.data) ? data.data : [];
const freeModels = allModels
.filter((m) => typeof m?.id === 'string' && m.id.endsWith('-free'))
.map((m) => ({ id: m.id, owned_by: m.owned_by }));
cachedZenModels = { models: freeModels };
cachedZenModelsTimestamp = Date.now();
return freeModels;
} finally {
if (timeout) clearTimeout(timeout);
}
};
/**
* Resolve the zen model to use. Checks the provided override first,
* then falls back to the stored zenModel setting, then to the validated
* startup fallback, then to the hardcoded default.
*/
const resolveZenModel = async (override) => {
if (typeof override === 'string' && override.trim().length > 0) {
return override.trim();
}
try {
const settings = await readSettingsFromDisk();
if (typeof settings?.zenModel === 'string' && settings.zenModel.trim().length > 0) {
return settings.zenModel.trim();
}
} catch {
// ignore
}
return validatedZenFallback || ZEN_DEFAULT_MODEL;
};
const summarizeText = async (text, targetLength, zenModel) => {
if (!text || typeof text !== 'string' || text.trim().length === 0) return text; if (!text || typeof text !== 'string' || text.trim().length === 0) return text;
try { try {
@@ -601,7 +670,7 @@ const summarizeText = async (text, targetLength) => {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
model: 'gpt-5-nano', model: zenModel || ZEN_DEFAULT_MODEL,
input: [{ role: 'user', content: prompt }], input: [{ role: 'user', content: prompt }],
max_output_tokens: 1000, max_output_tokens: 1000,
stream: false, stream: false,
@@ -1451,6 +1520,10 @@ const sanitizeSettingsUpdate = (payload) => {
if (typeof candidate.gitmojiEnabled === 'boolean') { if (typeof candidate.gitmojiEnabled === 'boolean') {
result.gitmojiEnabled = candidate.gitmojiEnabled; result.gitmojiEnabled = candidate.gitmojiEnabled;
} }
if (typeof candidate.zenModel === 'string') {
const trimmed = candidate.zenModel.trim();
result.zenModel = trimmed.length > 0 ? trimmed : undefined;
}
if (typeof candidate.toolCallExpansion === 'string') { if (typeof candidate.toolCallExpansion === 'string') {
const mode = candidate.toolCallExpansion.trim(); const mode = candidate.toolCallExpansion.trim();
if (mode === 'collapsed' || mode === 'activity' || mode === 'detailed') { if (mode === 'collapsed' || mode === 'activity' || mode === 'detailed') {
@@ -3901,10 +3974,11 @@ const maybeSendPushForTrigger = async (payload) => {
lastMessage = await fetchLastAssistantMessageText(sessionId, messageId); lastMessage = await fetchLastAssistantMessageText(sessionId, messageId);
} }
const notifZenModel = await resolveZenModel(settings?.zenModel);
variables.last_message = await prepareNotificationLastMessage({ variables.last_message = await prepareNotificationLastMessage({
message: lastMessage, message: lastMessage,
settings, settings,
summarize: summarizeText, summarize: (text, len) => summarizeText(text, len, notifZenModel),
}); });
const resolvedTitle = resolveNotificationTemplate(completionTemplate.title, variables); const resolvedTitle = resolveNotificationTemplate(completionTemplate.title, variables);
@@ -3961,10 +4035,11 @@ const maybeSendPushForTrigger = async (payload) => {
lastMessage = await fetchLastAssistantMessageText(sessionId, errorMessageId); lastMessage = await fetchLastAssistantMessageText(sessionId, errorMessageId);
} }
const errZenModel = await resolveZenModel(settings?.zenModel);
variables.last_message = await prepareNotificationLastMessage({ variables.last_message = await prepareNotificationLastMessage({
message: lastMessage, message: lastMessage,
settings, settings,
summarize: summarizeText, summarize: (text, len) => summarizeText(text, len, errZenModel),
}); });
const errorTemplate = (settings.notificationTemplates || {}).error || { title: 'Tool error', message: '{last_message}' }; const errorTemplate = (settings.notificationTemplates || {}).error || { title: 'Tool error', message: '{last_message}' };
@@ -5159,6 +5234,36 @@ async function main(options = {}) {
sayTTSCapability = { available: false, voices: [], reason: 'Not macOS' }; sayTTSCapability = { available: false, voices: [], reason: 'Not macOS' };
} }
// Validate stored zen model at startup – best-effort, never blocks startup
try {
const freeModels = await fetchFreeZenModels();
const freeModelIds = freeModels.map((m) => m.id);
if (freeModelIds.length > 0) {
// Set the validated fallback to the first available free model
validatedZenFallback = freeModelIds[0];
const settings = await readSettingsFromDisk();
const storedModel = typeof settings?.zenModel === 'string' ? settings.zenModel.trim() : '';
if (!storedModel || !freeModelIds.includes(storedModel)) {
const fallback = freeModelIds[0];
console.log(
storedModel
? `[zen] Stored model "${storedModel}" not found in free models, falling back to "${fallback}"`
: `[zen] No model configured, setting default to "${fallback}"`
);
await persistSettings({ zenModel: fallback });
} else {
console.log(`[zen] Stored model "${storedModel}" verified as available`);
}
} else {
console.warn('[zen] No free models returned from API, skipping validation');
}
} catch (error) {
console.warn('[zen] Startup model validation failed (non-blocking):', error?.message || error);
}
const app = express(); const app = express();
app.set('trust proxy', true); app.set('trust proxy', true);
expressApp = app; expressApp = app;
@@ -5417,7 +5522,8 @@ async function main(options = {}) {
if (summarize && textToSpeak.length > threshold) { if (summarize && textToSpeak.length > threshold) {
try { try {
const { summarizeText } = await import('./lib/summarization-service.js'); const { summarizeText } = await import('./lib/summarization-service.js');
const result = await summarizeText({ text: textToSpeak, threshold, maxLength }); const speakZenModel = await resolveZenModel(typeof req.body?.zenModel === 'string' ? req.body.zenModel : undefined);
const result = await summarizeText({ text: textToSpeak, threshold, maxLength, zenModel: speakZenModel });
if (result.summarized && result.summary) { if (result.summarized && result.summary) {
textToSpeak = result.summary; textToSpeak = result.summary;
@@ -5485,7 +5591,8 @@ async function main(options = {}) {
return res.status(400).json({ error: 'Text is required' }); return res.status(400).json({ error: 'Text is required' });
} }
const result = await summarizeText({ text, threshold, maxLength }); const sumZenModel = await resolveZenModel(typeof req.body?.zenModel === 'string' ? req.body.zenModel : undefined);
const result = await summarizeText({ text, threshold, maxLength, zenModel: sumZenModel });
return res.json(result); return res.json(result);
} catch (error) { } catch (error) {
@@ -5854,6 +5961,25 @@ async function main(options = {}) {
} }
}); });
// Zen models endpoint - returns available free models from the zen API
app.get('/api/zen/models', async (_req, res) => {
try {
const models = await fetchFreeZenModels();
res.setHeader('Cache-Control', 'public, max-age=300');
res.json({ models });
} catch (error) {
console.warn('Failed to fetch zen models:', error);
// Serve stale cache if available
if (cachedZenModels) {
res.setHeader('Cache-Control', 'public, max-age=60');
res.json(cachedZenModels);
} else {
const statusCode = error?.name === 'AbortError' ? 504 : 502;
res.status(statusCode).json({ error: 'Failed to retrieve zen models' });
}
}
});
app.get('/api/global/event', async (req, res) => { app.get('/api/global/event', async (req, res) => {
let targetUrl; let targetUrl;
try { try {
@@ -8811,7 +8937,7 @@ highlights:
Diff summary (may be truncated): Diff summary (may be truncated):
${diffSummaries}`; ${diffSummaries}`;
const model = 'gpt-5-nano'; const model = await resolveZenModel(typeof req.body?.zenModel === 'string' ? req.body.zenModel : undefined);
const completionTimeout = createTimeoutSignal(LONG_REQUEST_TIMEOUT_MS); const completionTimeout = createTimeoutSignal(LONG_REQUEST_TIMEOUT_MS);
let response; let response;
@@ -8924,7 +9050,7 @@ Context:
prompt += `\n\nDiff summary:\n${diffSummaries}`; prompt += `\n\nDiff summary:\n${diffSummaries}`;
const model = 'gpt-5-nano'; const model = await resolveZenModel(typeof req.body?.zenModel === 'string' ? req.body.zenModel : undefined);
const completionTimeout = createTimeoutSignal(LONG_REQUEST_TIMEOUT_MS); const completionTimeout = createTimeoutSignal(LONG_REQUEST_TIMEOUT_MS);
let response; let response;
@@ -78,18 +78,20 @@ function extractZenOutputText(data) {
} }
/** /**
* Summarize text using the opencode.ai zen API with gpt-5-nano * Summarize text using the opencode.ai zen API
* *
* @param {Object} options * @param {Object} options
* @param {string} options.text - The text to summarize * @param {string} options.text - The text to summarize
* @param {number} options.threshold - Character threshold (don't summarize if under this length) * @param {number} options.threshold - Character threshold (don't summarize if under this length)
* @param {number} options.maxLength - Maximum character length for the summary output (50-2000) * @param {number} options.maxLength - Maximum character length for the summary output (50-2000)
* @param {string} [options.zenModel] - Override zen model (defaults to gpt-5-nano)
* @returns {Promise<{summary: string, summarized: boolean, reason?: string}>} * @returns {Promise<{summary: string, summarized: boolean, reason?: string}>}
*/ */
export async function summarizeText({ export async function summarizeText({
text, text,
threshold = 200, threshold = 200,
maxLength = 500, maxLength = 500,
zenModel,
}) { }) {
// Don't summarize if text is under threshold // Don't summarize if text is under threshold
if (!text || text.length <= threshold) { if (!text || text.length <= threshold) {
@@ -110,7 +112,7 @@ export async function summarizeText({
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
model: 'gpt-5-nano', model: zenModel || 'gpt-5-nano',
input: [ input: [
{ role: 'user', content: `${prompt}\n\nText to summarize:\n${text}` }, { role: 'user', content: `${prompt}\n\nText to summarize:\n${text}` },
], ],