@causyne/agent 1.20.124 → 1.20.127
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/causyne.js +1 -1
- package/package.json +4 -4
- package/src/acp/protocol.ts +19 -1
- package/src/acp/server.js +282 -8
- package/src/acp/server.ts +284 -8
- package/src/agent/abortableDelay.js +2 -2
- package/src/agent/abortableDelay.ts +2 -2
- package/src/agent/errorClassifier.js +9 -1
- package/src/agent/errorClassifier.ts +8 -1
- package/src/agentLoop.js +83 -27
- package/src/agentLoop.ts +92 -26
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +30 -3
- package/src/browser/causyneBrowser.ts +33 -3
- package/src/browser/urlPolicy.js +14 -1
- package/src/browser/urlPolicy.ts +12 -1
- package/src/cliMode.js +21 -9
- package/src/cliMode.ts +20 -8
- package/src/commands.js +188 -32
- package/src/commands.ts +209 -32
- package/src/config.js +19 -4
- package/src/config.ts +18 -4
- package/src/core/contextCompactor.js +54 -7
- package/src/core/contextCompactor.ts +63 -7
- package/src/core/goalLoop.js +257 -0
- package/src/core/goalLoop.ts +310 -0
- package/src/entrypoint.js +135 -3
- package/src/entrypoint.ts +136 -3
- package/src/game/causyneCoreBridge.js +686 -0
- package/src/game/causyneCoreBridge.ts +702 -0
- package/src/game/gameLauncher.js +287 -0
- package/src/game/gameLauncher.ts +352 -0
- package/src/game/gameProvisioner.js +425 -0
- package/src/game/gameProvisioner.ts +488 -0
- package/src/headlessCli.js +19 -12
- package/src/headlessCli.ts +22 -8
- package/src/infra/atomicWriter.js +40 -4
- package/src/infra/atomicWriter.ts +36 -4
- package/src/launchIntent.js +73 -3
- package/src/launchIntent.ts +76 -3
- package/src/lsp/lspClient.js +83 -12
- package/src/lsp/lspClient.ts +75 -12
- package/src/mcp/mcpRegistry.js +75 -8
- package/src/mcp/mcpRegistry.ts +70 -7
- package/src/memory/contextBudget.js +2 -0
- package/src/memory/contextBudget.ts +1 -0
- package/src/modelVariants.js +45 -0
- package/src/modelVariants.ts +54 -0
- package/src/outputRecovery.js +3 -2
- package/src/outputRecovery.ts +4 -2
- package/src/permissionPresets.js +327 -0
- package/src/permissionPresets.ts +361 -0
- package/src/providers/antigravity.js +62 -9
- package/src/providers/antigravity.ts +73 -9
- package/src/providers/byokHttp.js +7 -1
- package/src/providers/byokHttp.ts +6 -1
- package/src/providers/byokStream.js +21 -8
- package/src/providers/byokStream.ts +17 -7
- package/src/providers/dashscope-byok.js +21 -5
- package/src/providers/dashscope-byok.ts +29 -11
- package/src/providers/moonshot.js +26 -7
- package/src/providers/moonshot.ts +34 -13
- package/src/providers/muse.js +6 -0
- package/src/providers/muse.ts +8 -0
- package/src/providers/ollama.js +39 -7
- package/src/providers/ollama.ts +36 -7
- package/src/providers/openai-compatible.js +16 -3
- package/src/providers/openai-compatible.ts +13 -3
- package/src/providers/openai.js +62 -4
- package/src/providers/openai.ts +50 -4
- package/src/providers/openrouter.js +19 -4
- package/src/providers/openrouter.ts +21 -4
- package/src/providers/qwen-byok.js +21 -5
- package/src/providers/qwen-byok.ts +29 -11
- package/src/providers/xai.js +26 -10
- package/src/providers/xai.ts +34 -16
- package/src/reporting.js +4 -2
- package/src/reporting.ts +4 -2
- package/src/sandbox.js +34 -0
- package/src/sandbox.ts +43 -0
- package/src/scheduler/scheduler.js +372 -0
- package/src/scheduler/scheduler.ts +433 -0
- package/src/sessionSearchIndex.js +267 -0
- package/src/sessionSearchIndex.ts +311 -0
- package/src/skillsManager.js +2 -3
- package/src/skillsManager.ts +2 -3
- package/src/toolPacks.js +6 -0
- package/src/toolPacks.ts +6 -0
- package/src/tools/applyPatch.js +1 -1
- package/src/tools/applyPatch.ts +1 -1
- package/src/tools/generateMediaViaApi.js +4 -3
- package/src/tools/generateMediaViaApi.ts +4 -3
- package/src/tools/nativeEngine.js +2 -2
- package/src/tools/nativeEngine.ts +2 -2
- package/src/tools/openSourceToolProvisioner.js +2 -2
- package/src/tools/openSourceToolProvisioner.ts +2 -2
- package/src/tools/payloadSanitizer.js +0 -1
- package/src/tools/payloadSanitizer.ts +0 -1
- package/src/tools/pdfReader.js +122 -20
- package/src/tools/pdfReader.ts +111 -20
- package/src/tools/presentDeliverable.js +103 -0
- package/src/tools/presentDeliverable.ts +127 -0
- package/src/tools/ptcRuntime.js +444 -0
- package/src/tools/ptcRuntime.ts +490 -0
- package/src/tools/ptyTerminal.js +1096 -0
- package/src/tools/ptyTerminal.ts +1231 -0
- package/src/tools/registry.js +21 -12
- package/src/tools/registry.ts +21 -12
- package/src/tools/toolOutputStore.js +26 -4
- package/src/tools/toolOutputStore.ts +22 -4
- package/src/tools/webFetch.js +158 -11
- package/src/tools/webFetch.ts +145 -11
- package/src/tools/webSearch.js +123 -36
- package/src/tools/webSearch.ts +124 -33
- package/src/tui/banner.js +5 -1
- package/src/tui/banner.ts +6 -1
- package/src/tui/chatRenderer.js +1 -2
- package/src/tui/chatRenderer.ts +1 -2
- package/src/tui/completion.js +1 -0
- package/src/tui/completion.ts +1 -0
- package/src/tui/composerControls.js +111 -27
- package/src/tui/composerControls.ts +124 -28
- package/src/tui/dialogs.js +3 -16
- package/src/tui/dialogs.ts +3 -16
- package/src/tui/home.js +1 -0
- package/src/tui/home.ts +1 -0
- package/src/tui/invocation.js +1 -2
- package/src/tui/invocation.ts +1 -2
- package/src/tui/modelPicker.js +22 -12
- package/src/tui/modelPicker.ts +22 -12
- package/src/tui/overlay.js +2 -5
- package/src/tui/overlay.ts +2 -5
- package/src/tui/prompt.js +6 -4
- package/src/tui/prompt.ts +6 -4
- package/src/tui/selectList.js +4 -12
- package/src/tui/selectList.ts +4 -12
- package/src/tui/setup.js +22 -9
- package/src/tui/setup.ts +26 -9
- package/src/tui/statusLine.ts +2 -0
- package/src/web/client/browserPanel.js +702 -0
- package/src/web/client/browserPanel.ts +663 -0
- package/src/web/client/chatStream.js +309 -0
- package/src/web/client/chatStream.ts +289 -0
- package/src/web/client/cliPanel.js +165 -0
- package/src/web/client/cliPanel.ts +171 -0
- package/src/web/client/composer.js +204 -0
- package/src/web/client/composer.ts +209 -0
- package/src/web/client/diffPanel.js +176 -0
- package/src/web/client/diffPanel.ts +203 -0
- package/src/web/client/dom.js +220 -0
- package/src/web/client/dom.ts +221 -0
- package/src/web/client/filesAndCanvas.js +231 -0
- package/src/web/client/filesAndCanvas.ts +238 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +301 -0
- package/src/web/client/main.ts +368 -0
- package/src/web/client/markdown.js +364 -0
- package/src/web/client/markdown.ts +373 -0
- package/src/web/client/modelPicker.js +455 -0
- package/src/web/client/modelPicker.ts +451 -0
- package/src/web/client/permissionPresetsUi.js +307 -0
- package/src/web/client/permissionPresetsUi.ts +333 -0
- package/src/web/client/sessions.js +353 -0
- package/src/web/client/sessions.ts +372 -0
- package/src/web/client/settingsModal.js +191 -0
- package/src/web/client/settingsModal.ts +176 -0
- package/src/web/client/state.js +250 -0
- package/src/web/client/state.ts +233 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +54 -0
- package/src/web/client/studioLayout.ts +60 -0
- package/src/web/client/studioShell.js +327 -0
- package/src/web/client/studioShell.ts +340 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +357 -0
- package/src/web/client/terminalPanel.ts +345 -0
- package/src/web/client/turnView.js +934 -0
- package/src/web/client/turnView.ts +981 -0
- package/src/web/client/types.js +1 -0
- package/src/web/client/types.ts +319 -0
- package/src/web/client/validators.js +106 -0
- package/src/web/client/validators.ts +96 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +355 -0
- package/src/web/server/routesBrowser.ts +386 -0
- package/src/web/server/routesChat.js +304 -0
- package/src/web/server/routesChat.ts +347 -0
- package/src/web/server/routesCore.js +472 -0
- package/src/web/server/routesCore.ts +522 -0
- package/src/web/server/routesTerminal.js +162 -0
- package/src/web/server/routesTerminal.ts +182 -0
- package/src/web/server/routesWorkspace.js +510 -0
- package/src/web/server/routesWorkspace.ts +546 -0
- package/src/web/server/security.js +98 -0
- package/src/web/server/security.ts +108 -0
- package/src/web/server/sessionTranscript.js +586 -0
- package/src/web/server/sessionTranscript.ts +566 -0
- package/src/web/server/staticAssets.js +80 -0
- package/src/web/server/staticAssets.ts +91 -0
- package/src/web/server/types.js +1 -0
- package/src/web/server/types.ts +135 -0
- package/src/web/server/workspaceFiles.js +368 -0
- package/src/web/server/workspaceFiles.ts +363 -0
- package/src/web/skybridgeServer.js +76 -2449
- package/src/web/skybridgeServer.ts +118 -2600
- package/src/web/skybridgeUi.js +16 -450
- package/src/web/skybridgeUi.ts +16 -454
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +436 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +723 -0
- package/src/web/styles/thread.css +221 -0
- package/src/web/styles/tokens.css +94 -0
- package/src/web/ui/brandMark.js +73 -0
- package/src/web/ui/brandMark.ts +82 -0
- package/src/web/ui/modalsTemplate.js +139 -0
- package/src/web/ui/modalsTemplate.ts +139 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +258 -0
- package/src/web/ui/studioTemplate.ts +258 -0
- package/src/web/ui/workspaceTemplate.js +72 -0
- package/src/web/ui/workspaceTemplate.ts +73 -0
- package/src/worktreeManager.js +16 -1
- package/src/worktreeManager.ts +22 -1
- package/src/web/skybridgeClientScript.js +0 -3951
- package/src/web/skybridgeClientScript.ts +0 -3952
- package/src/web/skybridgeStyles.js +0 -827
- package/src/web/skybridgeStyles.ts +0 -828
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import { announceStatus, dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, state } from './state.js';
|
|
3
|
+
import { closeAllPopovers, positionPopover } from './studioShell.js';
|
|
4
|
+
import type { AssistantTurnHandle, ModelPickerItem, ModelsResponse } from './types.js';
|
|
5
|
+
|
|
6
|
+
export interface ModelPickerCallbacks {
|
|
7
|
+
createErrorTurn(message: string): AssistantTurnHandle | void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let callbacks: ModelPickerCallbacks = {
|
|
11
|
+
createErrorTurn: () => {}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export function configureModelPickerCallbacks(cbs: ModelPickerCallbacks): void {
|
|
15
|
+
callbacks = cbs;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function loadHealth(): Promise<void> {
|
|
19
|
+
const res = await fetch(apiUrl('/api/health'), { headers: authHeaders() });
|
|
20
|
+
if (!res.ok) {
|
|
21
|
+
dom.wsNameEl.textContent = 'Auth required';
|
|
22
|
+
dom.wsMetaEl.textContent = 'Open URL printed by causyne web';
|
|
23
|
+
callbacks.createErrorTurn('Missing or invalid Skybridge token. Open the URL printed by `causyne web`.');
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const data = await res.json() as {
|
|
27
|
+
provider?: string;
|
|
28
|
+
model?: string;
|
|
29
|
+
workspaceRoot?: string;
|
|
30
|
+
workspaceName?: string;
|
|
31
|
+
version?: string;
|
|
32
|
+
};
|
|
33
|
+
state.activeProvider = data.provider || 'unconfigured';
|
|
34
|
+
state.activeModel = data.model || 'default';
|
|
35
|
+
state.workspaceRoot = data.workspaceRoot || '';
|
|
36
|
+
dom.wsNameEl.textContent = data.workspaceName || 'Workspace';
|
|
37
|
+
dom.wsMetaEl.textContent = `v${data.version || ''}`;
|
|
38
|
+
if (dom.settingsWsPath) dom.settingsWsPath.textContent = data.workspaceRoot || '';
|
|
39
|
+
const cleanModel = String(state.activeModel || '').split('/').pop() || state.activeProvider;
|
|
40
|
+
dom.modelTriggerName.textContent = cleanModel === 'default' ? state.activeProvider : cleanModel;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function updateModelRefreshUi(): void {
|
|
44
|
+
const btn = dom.modelPopover.querySelector<HTMLButtonElement>('.mp-refresh-btn');
|
|
45
|
+
if (!btn) return;
|
|
46
|
+
btn.disabled = state.modelRefreshing;
|
|
47
|
+
btn.textContent = state.modelRefreshing ? 'Refreshing...' : 'Refresh';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function selectModel(provider: string, model: string): Promise<void> {
|
|
51
|
+
closeAllPopovers();
|
|
52
|
+
const res = await fetch(apiUrl('/api/model'), {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
55
|
+
body: JSON.stringify({ provider, model })
|
|
56
|
+
});
|
|
57
|
+
if (!res.ok) return;
|
|
58
|
+
const data = await res.json() as { provider?: string; model?: string };
|
|
59
|
+
state.activeProvider = data.provider || provider;
|
|
60
|
+
state.activeModel = data.model || model;
|
|
61
|
+
const cleanModel = String(state.activeModel || '').split('/').pop() || state.activeProvider;
|
|
62
|
+
dom.modelTriggerName.textContent = cleanModel === 'default' ? state.activeProvider : cleanModel;
|
|
63
|
+
announceStatus(`Switched model to ${cleanModel}`);
|
|
64
|
+
void loadModels();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function disconnectOpenRouterAccount(): Promise<void> {
|
|
68
|
+
const res = await fetch(apiUrl('/api/model'), {
|
|
69
|
+
method: 'POST',
|
|
70
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
71
|
+
body: JSON.stringify({ action: 'disconnect', provider: 'openrouter' })
|
|
72
|
+
});
|
|
73
|
+
if (!res.ok) return;
|
|
74
|
+
const data = await res.json() as { provider?: string; model?: string };
|
|
75
|
+
state.activeProvider = data.provider || state.activeProvider;
|
|
76
|
+
state.activeModel = data.model || state.activeModel;
|
|
77
|
+
state.modelNotice = 'Disconnected OpenRouter account.';
|
|
78
|
+
await loadModels();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function fuzzyMatchModelItem(item: ModelPickerItem, query: string): boolean {
|
|
82
|
+
const q = String(query || '').trim().toLowerCase();
|
|
83
|
+
if (!q) return true;
|
|
84
|
+
const hay = [
|
|
85
|
+
item.label || '',
|
|
86
|
+
item.category || '',
|
|
87
|
+
item.id || '',
|
|
88
|
+
item.description || '',
|
|
89
|
+
item.value?.providerLabel || ''
|
|
90
|
+
].join(' ').toLowerCase();
|
|
91
|
+
const tokens = q.split(/\s+/).filter(Boolean);
|
|
92
|
+
for (const tok of tokens) {
|
|
93
|
+
if (hay.includes(tok)) continue;
|
|
94
|
+
let ti = 0;
|
|
95
|
+
for (let hi = 0; hi < hay.length && ti < tok.length; hi += 1) {
|
|
96
|
+
if (hay[hi] === tok[ti]) ti += 1;
|
|
97
|
+
}
|
|
98
|
+
if (ti < tok.length) return false;
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getVisibleModelItems(): ModelPickerItem[] {
|
|
104
|
+
if (!state.models || !state.models.tabs) return [];
|
|
105
|
+
const tab = state.modelTab || state.models.initialTab || 'acp';
|
|
106
|
+
const rawItems = Array.isArray(state.models.tabs[tab]) ? state.models.tabs[tab] : [];
|
|
107
|
+
const q = String(state.modelQuery || '').trim();
|
|
108
|
+
if (q) {
|
|
109
|
+
return rawItems.filter((item) => !(item.value && item.value.isToggle) && fuzzyMatchModelItem(item, q));
|
|
110
|
+
}
|
|
111
|
+
if (tab !== 'acp') return rawItems;
|
|
112
|
+
const MAX_ACP = 10;
|
|
113
|
+
const byPeer = new Map<string, ModelPickerItem[]>();
|
|
114
|
+
const orderedPeers: string[] = [];
|
|
115
|
+
for (const item of rawItems) {
|
|
116
|
+
const key = item.value?.providerId || item.category || 'peer';
|
|
117
|
+
let list = byPeer.get(key);
|
|
118
|
+
if (!list) {
|
|
119
|
+
list = [];
|
|
120
|
+
byPeer.set(key, list);
|
|
121
|
+
orderedPeers.push(key);
|
|
122
|
+
}
|
|
123
|
+
list.push(item);
|
|
124
|
+
}
|
|
125
|
+
const out: ModelPickerItem[] = [];
|
|
126
|
+
for (const key of orderedPeers) {
|
|
127
|
+
const peerItems = byPeer.get(key) || [];
|
|
128
|
+
const installedModels = peerItems.filter((it) => it.value?.installed && !it.value?.isToggle);
|
|
129
|
+
if (installedModels.length <= MAX_ACP) {
|
|
130
|
+
for (const it of peerItems) out.push(it);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
const expanded = state.modelExpandedPeers.has(key);
|
|
134
|
+
const slice = expanded ? installedModels : installedModels.slice(0, MAX_ACP);
|
|
135
|
+
for (const it of slice) out.push(it);
|
|
136
|
+
const first = installedModels[0];
|
|
137
|
+
out.push({
|
|
138
|
+
id: key + (expanded ? ':__load_less__' : ':__load_more__'),
|
|
139
|
+
label: expanded
|
|
140
|
+
? `Show fewer (top ${MAX_ACP})`
|
|
141
|
+
: `Show ${installedModels.length - MAX_ACP} more models...`,
|
|
142
|
+
category: first?.category || '',
|
|
143
|
+
description: expanded ? 'collapse' : 'expand',
|
|
144
|
+
value: {
|
|
145
|
+
type: 'toggle',
|
|
146
|
+
isToggle: true,
|
|
147
|
+
action: expanded ? 'collapse' : 'expand',
|
|
148
|
+
peerKey: key,
|
|
149
|
+
providerId: key,
|
|
150
|
+
modelId: '',
|
|
151
|
+
providerLabel: first?.value?.providerLabel || key,
|
|
152
|
+
installed: true,
|
|
153
|
+
isCurrent: false,
|
|
154
|
+
isConfigured: true
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function activateModelPickerItem(item: ModelPickerItem | undefined): void {
|
|
162
|
+
if (!item || !item.value) return;
|
|
163
|
+
const v = item.value;
|
|
164
|
+
if (v.isToggle && v.peerKey) {
|
|
165
|
+
if (state.modelExpandedPeers.has(v.peerKey)) state.modelExpandedPeers.delete(v.peerKey);
|
|
166
|
+
else state.modelExpandedPeers.add(v.peerKey);
|
|
167
|
+
renderModelListOnly(false);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (v.type === 'action' && v.action === 'disconnect' && v.providerId === 'openrouter') {
|
|
171
|
+
void disconnectOpenRouterAccount();
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (v.type === 'acp' && !v.installed) {
|
|
175
|
+
state.modelNotice = v.setupInstruction || `${v.providerLabel || 'Peer'} is not installed on PATH yet.`;
|
|
176
|
+
renderModelListOnly(false);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if ((v.type === 'byok' || v.type === 'free') && !v.isConfigured && v.providerId !== 'ollama') {
|
|
180
|
+
closeAllPopovers();
|
|
181
|
+
dom.settingsModal.classList.add('open');
|
|
182
|
+
if (dom.vaultProviderSelect && v.providerId) {
|
|
183
|
+
const opt = Array.from(dom.vaultProviderSelect.options).find((o) => o.value === v.providerId);
|
|
184
|
+
if (opt) dom.vaultProviderSelect.value = v.providerId;
|
|
185
|
+
}
|
|
186
|
+
dom.vaultStatusEl.textContent = `Add your ${v.envVar || `${v.providerLabel || v.providerId || 'provider'} API key`} to use ${v.modelId || 'this model'}.`;
|
|
187
|
+
dom.vaultKeyInput.focus();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
state.modelNotice = '';
|
|
191
|
+
if (v.providerId) void selectModel(v.providerId, v.modelId || '');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function switchModelTab(nextTab: 'acp' | 'byok' | 'free'): void {
|
|
195
|
+
if (state.modelTab === nextTab) return;
|
|
196
|
+
state.modelTab = nextTab;
|
|
197
|
+
state.modelSelectedIndex = 0;
|
|
198
|
+
state.modelNotice = '';
|
|
199
|
+
renderModelPopover();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function ensureModelPickerShell(): void {
|
|
203
|
+
if (dom.modelPopover.querySelector('.mp-head')) return;
|
|
204
|
+
dom.modelPopover.textContent = '';
|
|
205
|
+
const head = document.createElement('div');
|
|
206
|
+
head.className = 'mp-head';
|
|
207
|
+
const topRow = document.createElement('div');
|
|
208
|
+
topRow.className = 'mp-top-row';
|
|
209
|
+
const tabsEl = document.createElement('div');
|
|
210
|
+
tabsEl.className = 'mp-tabs';
|
|
211
|
+
tabsEl.setAttribute('role', 'tablist');
|
|
212
|
+
const tabDefs: ReadonlyArray<{ id: 'acp' | 'byok' | 'free'; label: string }> = [
|
|
213
|
+
{ id: 'acp', label: 'ACP' },
|
|
214
|
+
{ id: 'byok', label: 'BYOK' },
|
|
215
|
+
{ id: 'free', label: 'FREE' }
|
|
216
|
+
];
|
|
217
|
+
for (const def of tabDefs) {
|
|
218
|
+
const tb = document.createElement('button');
|
|
219
|
+
tb.type = 'button';
|
|
220
|
+
tb.className = 'mp-tab';
|
|
221
|
+
tb.setAttribute('role', 'tab');
|
|
222
|
+
tb.dataset.mptab = def.id;
|
|
223
|
+
tb.textContent = def.label;
|
|
224
|
+
tb.addEventListener('click', () => switchModelTab(def.id));
|
|
225
|
+
tabsEl.appendChild(tb);
|
|
226
|
+
}
|
|
227
|
+
const refreshBtn = document.createElement('button');
|
|
228
|
+
refreshBtn.type = 'button';
|
|
229
|
+
refreshBtn.className = 'mp-refresh-btn';
|
|
230
|
+
refreshBtn.textContent = 'Refresh';
|
|
231
|
+
refreshBtn.addEventListener('click', () => { void loadModels('refresh'); });
|
|
232
|
+
topRow.appendChild(tabsEl);
|
|
233
|
+
topRow.appendChild(refreshBtn);
|
|
234
|
+
|
|
235
|
+
const searchInput = document.createElement('input');
|
|
236
|
+
searchInput.type = 'search';
|
|
237
|
+
searchInput.className = 'mp-search';
|
|
238
|
+
searchInput.placeholder = 'Filter models (e.g. gemini, sonnet, deepseek)...';
|
|
239
|
+
searchInput.setAttribute('aria-label', 'Filter models');
|
|
240
|
+
searchInput.addEventListener('input', () => {
|
|
241
|
+
state.modelQuery = searchInput.value || '';
|
|
242
|
+
state.modelSelectedIndex = 0;
|
|
243
|
+
renderModelListOnly(false);
|
|
244
|
+
});
|
|
245
|
+
searchInput.addEventListener('keydown', (e) => {
|
|
246
|
+
const items = getVisibleModelItems();
|
|
247
|
+
const tabOrder: ReadonlyArray<'acp' | 'byok' | 'free'> = ['acp', 'byok', 'free'];
|
|
248
|
+
const curTabIdx = Math.max(0, tabOrder.indexOf(state.modelTab || 'acp'));
|
|
249
|
+
if (e.key === 'Tab') {
|
|
250
|
+
e.preventDefault();
|
|
251
|
+
const nextIdx = (curTabIdx + (e.shiftKey ? 2 : 1)) % 3;
|
|
252
|
+
switchModelTab(tabOrder[nextIdx] || 'acp');
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
if ((e.key === 'ArrowLeft' || e.key === 'ArrowRight') && !searchInput.value) {
|
|
256
|
+
e.preventDefault();
|
|
257
|
+
const nextIdx = e.key === 'ArrowRight' ? Math.min(2, curTabIdx + 1) : Math.max(0, curTabIdx - 1);
|
|
258
|
+
switchModelTab(tabOrder[nextIdx] || 'acp');
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (e.key === 'ArrowDown') {
|
|
262
|
+
e.preventDefault();
|
|
263
|
+
if (items.length > 0) {
|
|
264
|
+
state.modelSelectedIndex = (state.modelSelectedIndex + 1) % items.length;
|
|
265
|
+
renderModelListOnly(true);
|
|
266
|
+
}
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (e.key === 'ArrowUp') {
|
|
270
|
+
e.preventDefault();
|
|
271
|
+
if (items.length > 0) {
|
|
272
|
+
state.modelSelectedIndex = (state.modelSelectedIndex - 1 + items.length) % items.length;
|
|
273
|
+
renderModelListOnly(true);
|
|
274
|
+
}
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (e.key === 'Enter') {
|
|
278
|
+
e.preventDefault();
|
|
279
|
+
if (items.length > 0) {
|
|
280
|
+
const chosen = items[Math.min(state.modelSelectedIndex, items.length - 1)];
|
|
281
|
+
activateModelPickerItem(chosen);
|
|
282
|
+
}
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (e.key === 'Escape') {
|
|
286
|
+
e.preventDefault();
|
|
287
|
+
closeAllPopovers();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
head.appendChild(topRow);
|
|
292
|
+
head.appendChild(searchInput);
|
|
293
|
+
const noteEl = document.createElement('div');
|
|
294
|
+
noteEl.className = 'mp-note';
|
|
295
|
+
noteEl.style.display = 'none';
|
|
296
|
+
const listEl = document.createElement('div');
|
|
297
|
+
listEl.className = 'mp-list';
|
|
298
|
+
const footEl = document.createElement('div');
|
|
299
|
+
footEl.className = 'mp-foot';
|
|
300
|
+
const footLeft = document.createElement('span');
|
|
301
|
+
footLeft.textContent = 'Tab switch · ↑↓ navigate · Enter select';
|
|
302
|
+
const footCount = document.createElement('span');
|
|
303
|
+
footCount.className = 'mp-foot-count';
|
|
304
|
+
footEl.appendChild(footLeft);
|
|
305
|
+
footEl.appendChild(footCount);
|
|
306
|
+
dom.modelPopover.appendChild(head);
|
|
307
|
+
dom.modelPopover.appendChild(noteEl);
|
|
308
|
+
dom.modelPopover.appendChild(listEl);
|
|
309
|
+
dom.modelPopover.appendChild(footEl);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function renderModelListOnly(scrollFocused: boolean): void {
|
|
313
|
+
const listEl = dom.modelPopover.querySelector<HTMLElement>('.mp-list');
|
|
314
|
+
const noteEl = dom.modelPopover.querySelector<HTMLElement>('.mp-note');
|
|
315
|
+
const footCount = dom.modelPopover.querySelector<HTMLElement>('.mp-foot-count');
|
|
316
|
+
if (!listEl) return;
|
|
317
|
+
if (noteEl) {
|
|
318
|
+
if (state.modelNotice) {
|
|
319
|
+
noteEl.textContent = state.modelNotice;
|
|
320
|
+
noteEl.style.display = 'block';
|
|
321
|
+
} else {
|
|
322
|
+
noteEl.textContent = '';
|
|
323
|
+
noteEl.style.display = 'none';
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
listEl.textContent = '';
|
|
327
|
+
const items = getVisibleModelItems();
|
|
328
|
+
if (state.modelSelectedIndex >= items.length) state.modelSelectedIndex = Math.max(0, items.length - 1);
|
|
329
|
+
const selectableCount = items.filter((it) => !(it.value && (it.value.isToggle || it.value.type === 'action'))).length;
|
|
330
|
+
if (footCount) footCount.textContent = `${selectableCount}${selectableCount === 1 ? ' model' : ' models'}`;
|
|
331
|
+
if (items.length === 0) {
|
|
332
|
+
const empty = document.createElement('div');
|
|
333
|
+
empty.className = 'mp-empty';
|
|
334
|
+
empty.textContent = state.modelQuery ? 'No models matched your search in this tab.' : 'Loading models...';
|
|
335
|
+
listEl.appendChild(empty);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
let currentCategory = '';
|
|
339
|
+
let focusedBtn: HTMLButtonElement | null = null;
|
|
340
|
+
items.forEach((item, idx) => {
|
|
341
|
+
if (item.category && item.category !== currentCategory) {
|
|
342
|
+
currentCategory = item.category;
|
|
343
|
+
const cat = document.createElement('div');
|
|
344
|
+
cat.className = 'mp-cat';
|
|
345
|
+
cat.textContent = currentCategory;
|
|
346
|
+
listEl.appendChild(cat);
|
|
347
|
+
}
|
|
348
|
+
const v = item.value || {};
|
|
349
|
+
const btn = document.createElement('button');
|
|
350
|
+
btn.type = 'button';
|
|
351
|
+
const isFocused = idx === state.modelSelectedIndex;
|
|
352
|
+
const isCurrent = Boolean(v.isCurrent);
|
|
353
|
+
const isMuted = (v.type === 'acp' && !v.installed)
|
|
354
|
+
|| ((v.type === 'byok' || v.type === 'free') && !v.isConfigured && v.providerId !== 'ollama');
|
|
355
|
+
const isDanger = v.type === 'action' && v.action === 'disconnect';
|
|
356
|
+
btn.className = 'mp-row'
|
|
357
|
+
+ (isFocused ? ' focused' : '')
|
|
358
|
+
+ (isCurrent ? ' current' : '')
|
|
359
|
+
+ (v.isToggle ? ' toggle' : '')
|
|
360
|
+
+ (isMuted ? ' muted' : '')
|
|
361
|
+
+ (isDanger ? ' danger' : '');
|
|
362
|
+
btn.title = item.id || item.label || '';
|
|
363
|
+
const lbl = document.createElement('span');
|
|
364
|
+
lbl.className = 'mp-row-label';
|
|
365
|
+
lbl.textContent = item.label || '';
|
|
366
|
+
const st = document.createElement('span');
|
|
367
|
+
st.className = `mp-row-status${isCurrent ? ' is-active' : (v.installed && v.isConfigured ? ' is-ready' : '')}`;
|
|
368
|
+
st.textContent = item.description || '';
|
|
369
|
+
btn.appendChild(lbl);
|
|
370
|
+
btn.appendChild(st);
|
|
371
|
+
btn.addEventListener('mouseenter', () => { state.modelSelectedIndex = idx; });
|
|
372
|
+
btn.addEventListener('click', () => activateModelPickerItem(item));
|
|
373
|
+
listEl.appendChild(btn);
|
|
374
|
+
if (isFocused) focusedBtn = btn;
|
|
375
|
+
});
|
|
376
|
+
if (scrollFocused && focusedBtn) (focusedBtn as HTMLButtonElement).scrollIntoView({ block: 'nearest' });
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function renderModelPopover(): void {
|
|
380
|
+
ensureModelPickerShell();
|
|
381
|
+
const activeTab = state.modelTab || state.models?.initialTab || 'acp';
|
|
382
|
+
state.modelTab = activeTab;
|
|
383
|
+
const tabsData = state.models?.tabs || {};
|
|
384
|
+
dom.modelPopover.querySelectorAll<HTMLButtonElement>('.mp-tab').forEach((tb) => {
|
|
385
|
+
const id = tb.dataset.mptab || '';
|
|
386
|
+
const isSelected = id === activeTab;
|
|
387
|
+
tb.classList.toggle('active', isSelected);
|
|
388
|
+
tb.setAttribute('aria-selected', isSelected ? 'true' : 'false');
|
|
389
|
+
const list = Array.isArray(tabsData[id]) ? tabsData[id] : [];
|
|
390
|
+
const hasActive = list.some((it) => Boolean(it?.value?.isCurrent));
|
|
391
|
+
tb.textContent = '';
|
|
392
|
+
if (hasActive) {
|
|
393
|
+
const dot = document.createElement('span');
|
|
394
|
+
dot.className = 'mp-tab-dot';
|
|
395
|
+
tb.appendChild(dot);
|
|
396
|
+
}
|
|
397
|
+
tb.appendChild(document.createTextNode(id === 'acp' ? 'ACP' : (id === 'byok' ? 'BYOK' : 'FREE')));
|
|
398
|
+
});
|
|
399
|
+
const searchInput = dom.modelPopover.querySelector<HTMLInputElement>('.mp-search');
|
|
400
|
+
if (searchInput && searchInput.value !== state.modelQuery) searchInput.value = state.modelQuery;
|
|
401
|
+
updateModelRefreshUi();
|
|
402
|
+
renderModelListOnly(false);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export async function loadModels(mode?: string): Promise<void> {
|
|
406
|
+
const isRefresh = mode === 'refresh';
|
|
407
|
+
const isLive = mode === 'live';
|
|
408
|
+
if (isRefresh || isLive) {
|
|
409
|
+
state.modelRefreshing = true;
|
|
410
|
+
updateModelRefreshUi();
|
|
411
|
+
}
|
|
412
|
+
const qs = isRefresh ? '?refresh=1' : (isLive ? '?live=1' : '');
|
|
413
|
+
try {
|
|
414
|
+
const res = await fetch(apiUrl(`/api/models${qs}`), { headers: authHeaders() });
|
|
415
|
+
if (!res.ok) return;
|
|
416
|
+
const data = await res.json() as ModelsResponse;
|
|
417
|
+
state.models = data;
|
|
418
|
+
if (!state.modelTab) state.modelTab = data.initialTab || 'acp';
|
|
419
|
+
if (data.activeProvider) state.activeProvider = data.activeProvider;
|
|
420
|
+
if (data.activeModel) state.activeModel = data.activeModel;
|
|
421
|
+
renderModelPopover();
|
|
422
|
+
if (!isRefresh && !isLive && !state.modelLiveHydrated) {
|
|
423
|
+
state.modelLiveHydrated = true;
|
|
424
|
+
void loadModels('live');
|
|
425
|
+
}
|
|
426
|
+
} finally {
|
|
427
|
+
if (isRefresh || isLive) {
|
|
428
|
+
state.modelRefreshing = false;
|
|
429
|
+
updateModelRefreshUi();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export function bindModelPickerEvents(): void {
|
|
435
|
+
dom.modelTriggerBtn.addEventListener('click', (e) => {
|
|
436
|
+
e.stopPropagation();
|
|
437
|
+
const wasOpen = dom.modelPopover.classList.contains('open');
|
|
438
|
+
closeAllPopovers();
|
|
439
|
+
if (!wasOpen) {
|
|
440
|
+
state.modelQuery = '';
|
|
441
|
+
state.modelNotice = '';
|
|
442
|
+
state.modelSelectedIndex = 0;
|
|
443
|
+
dom.modelTriggerBtn.setAttribute('aria-expanded', 'true');
|
|
444
|
+
renderModelPopover();
|
|
445
|
+
positionPopover(dom.modelPopover, dom.modelTriggerBtn, false);
|
|
446
|
+
const searchInput = dom.modelPopover.querySelector<HTMLInputElement>('.mp-search');
|
|
447
|
+
if (searchInput) searchInput.focus();
|
|
448
|
+
void loadModels();
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|