@causyne/agent 1.20.124 → 1.20.128
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/docs/THIRD_PARTY_NOTICES.md +36 -2
- package/package.json +6 -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 +142 -53
- package/src/agentLoop.ts +151 -50
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +61 -3
- package/src/browser/causyneBrowser.ts +65 -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 +365 -45
- package/src/commands.ts +403 -45
- 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/memory/episodicStore.js +40 -0
- package/src/memory/episodicStore.ts +46 -0
- package/src/memory.js +67 -0
- package/src/memory.ts +65 -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/personalization.js +365 -0
- package/src/personalization.ts +471 -0
- package/src/projectInstructions.js +30 -10
- package/src/projectInstructions.ts +31 -12
- package/src/providers/acp.js +3 -2
- package/src/providers/acp.ts +3 -2
- package/src/providers/antigravity.js +64 -9
- package/src/providers/antigravity.ts +75 -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/memoryWrite.js +15 -1
- package/src/tools/memoryWrite.ts +16 -1
- 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/osSandbox.js +1 -1
- package/src/tools/osSandbox.ts +1 -1
- 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 +1108 -0
- package/src/tools/ptyTerminal.ts +1245 -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 +905 -0
- package/src/web/client/browserPanel.ts +872 -0
- package/src/web/client/chatStream.js +321 -0
- package/src/web/client/chatStream.ts +297 -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 +271 -0
- package/src/web/client/dom.ts +272 -0
- package/src/web/client/filesAndCanvas.js +233 -0
- package/src/web/client/filesAndCanvas.ts +240 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +316 -0
- package/src/web/client/main.ts +391 -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 +824 -0
- package/src/web/client/settingsModal.ts +959 -0
- package/src/web/client/state.js +280 -0
- package/src/web/client/state.ts +259 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +89 -0
- package/src/web/client/studioLayout.ts +103 -0
- package/src/web/client/studioShell.js +660 -0
- package/src/web/client/studioShell.ts +672 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +606 -0
- package/src/web/client/terminalPanel.ts +632 -0
- package/src/web/client/terminalTheme.js +249 -0
- package/src/web/client/terminalTheme.ts +258 -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 +322 -0
- package/src/web/client/validators.js +147 -0
- package/src/web/client/validators.ts +140 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +409 -0
- package/src/web/server/routesBrowser.ts +435 -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/routesSettings.js +344 -0
- package/src/web/server/routesSettings.ts +397 -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 +104 -0
- package/src/web/server/staticAssets.ts +116 -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 +78 -2448
- package/src/web/skybridgeServer.ts +120 -2600
- package/src/web/skybridgeUi.js +20 -450
- package/src/web/skybridgeUi.ts +24 -455
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +935 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +951 -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 +388 -0
- package/src/web/ui/modalsTemplate.ts +388 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +256 -0
- package/src/web/ui/studioTemplate.ts +256 -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,280 @@
|
|
|
1
|
+
import { dom, maybeById } from './dom.js';
|
|
2
|
+
export const DRAFT_KEY = 'causyne/web/composerDraft/v1';
|
|
3
|
+
export const THEME_KEY = 'causyne/web/theme/v1';
|
|
4
|
+
export const DENSITY_KEY = 'causyne/web/density/v1';
|
|
5
|
+
export const AUTOSCROLL_KEY = 'causyne/web/autoScroll/v1';
|
|
6
|
+
export const PERMISSION_PRESET_KEY = 'causyne/web/permissionPreset/v1';
|
|
7
|
+
export const LEGACY_MODE_KEY = 'causyne/web/autonomyMode/v1';
|
|
8
|
+
export const STUDIO_WIDTH_KEY = 'causyne/web/studioWidth/v1';
|
|
9
|
+
export const DEFAULT_STUDIO_W = 480;
|
|
10
|
+
let autoScrollEnabled = true;
|
|
11
|
+
export function isAutoScrollEnabled() {
|
|
12
|
+
return autoScrollEnabled;
|
|
13
|
+
}
|
|
14
|
+
export function applyDensity(density) {
|
|
15
|
+
const next = density === 'compact' ? 'compact' : 'comfortable';
|
|
16
|
+
document.documentElement.setAttribute('data-density', next);
|
|
17
|
+
try {
|
|
18
|
+
window.localStorage.setItem(DENSITY_KEY, next);
|
|
19
|
+
}
|
|
20
|
+
catch { /* ignore */ }
|
|
21
|
+
}
|
|
22
|
+
export function applyAutoScrollPref(enabled) {
|
|
23
|
+
autoScrollEnabled = Boolean(enabled);
|
|
24
|
+
try {
|
|
25
|
+
window.localStorage.setItem(AUTOSCROLL_KEY, autoScrollEnabled ? '1' : '0');
|
|
26
|
+
}
|
|
27
|
+
catch { /* ignore */ }
|
|
28
|
+
}
|
|
29
|
+
const hashParams = new URLSearchParams((window.location.hash || '').replace(/^#/, ''));
|
|
30
|
+
export const TOKEN_STORE_KEY = `causyne/web/token:${window.location.origin}`;
|
|
31
|
+
let sessionToken = hashParams.get('token') || '';
|
|
32
|
+
if (sessionToken) {
|
|
33
|
+
try {
|
|
34
|
+
window.sessionStorage.setItem(TOKEN_STORE_KEY, sessionToken);
|
|
35
|
+
}
|
|
36
|
+
catch { /* ignore */ }
|
|
37
|
+
try {
|
|
38
|
+
window.history.replaceState(null, '', window.location.pathname + window.location.search);
|
|
39
|
+
}
|
|
40
|
+
catch { /* ignore */ }
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
try {
|
|
44
|
+
sessionToken = window.sessionStorage.getItem(TOKEN_STORE_KEY) || '';
|
|
45
|
+
}
|
|
46
|
+
catch { /* ignore */ }
|
|
47
|
+
}
|
|
48
|
+
let apiBase = '';
|
|
49
|
+
try {
|
|
50
|
+
const qs = new URLSearchParams(window.location.search || '');
|
|
51
|
+
const bridge = (qs.get('bridge') || '').trim();
|
|
52
|
+
if (bridge) {
|
|
53
|
+
const parsed = new URL(bridge);
|
|
54
|
+
const host = parsed.hostname.toLowerCase();
|
|
55
|
+
if (parsed.protocol === 'http:' && (host === '127.0.0.1' || host === 'localhost')) {
|
|
56
|
+
apiBase = parsed.origin;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Ignore invalid bridge parameter.
|
|
62
|
+
}
|
|
63
|
+
export function apiUrl(p) {
|
|
64
|
+
return apiBase + p;
|
|
65
|
+
}
|
|
66
|
+
export function authHeaders(extra) {
|
|
67
|
+
return Object.assign({ Authorization: `Bearer ${sessionToken}` }, extra || {});
|
|
68
|
+
}
|
|
69
|
+
export const state = {
|
|
70
|
+
sessionId: null,
|
|
71
|
+
sessions: [],
|
|
72
|
+
models: null,
|
|
73
|
+
modelTab: null,
|
|
74
|
+
modelQuery: '',
|
|
75
|
+
modelExpandedPeers: new Set(),
|
|
76
|
+
modelSelectedIndex: 0,
|
|
77
|
+
modelRefreshing: false,
|
|
78
|
+
modelLiveHydrated: false,
|
|
79
|
+
modelNotice: '',
|
|
80
|
+
activeProvider: 'unconfigured',
|
|
81
|
+
activeModel: 'default',
|
|
82
|
+
permissionPreset: 'workspace-write',
|
|
83
|
+
defaultPermissionPreset: 'workspace-write',
|
|
84
|
+
workspaceRoot: '',
|
|
85
|
+
lastScrollTop: 0,
|
|
86
|
+
attachments: [],
|
|
87
|
+
currentDir: '',
|
|
88
|
+
parentDir: '',
|
|
89
|
+
studioTab: 'start',
|
|
90
|
+
studioOpenTabs: ['start'],
|
|
91
|
+
studioSplit: false,
|
|
92
|
+
studioSecondaryTab: 'terminal',
|
|
93
|
+
studioSplitRatio: 0.5,
|
|
94
|
+
studioExpanded: false,
|
|
95
|
+
studioWidth: DEFAULT_STUDIO_W,
|
|
96
|
+
terminalSessions: [],
|
|
97
|
+
activeTerminalId: null,
|
|
98
|
+
availableShells: [],
|
|
99
|
+
canvasPreviewMode: false,
|
|
100
|
+
canvasDirty: false,
|
|
101
|
+
artifactVersions: new Map(),
|
|
102
|
+
sessionWorkspaces: new Map(),
|
|
103
|
+
sessionSwitchSeq: 0,
|
|
104
|
+
usageTokens: 0,
|
|
105
|
+
diffScope: 'session',
|
|
106
|
+
browserEngine: 'agent',
|
|
107
|
+
browserMode: 'empty',
|
|
108
|
+
browserUrl: '',
|
|
109
|
+
browserSrcdoc: '',
|
|
110
|
+
browserViewport: 'desktop',
|
|
111
|
+
browserFrameStrictSandbox: false,
|
|
112
|
+
liveBrowserActive: false,
|
|
113
|
+
liveBrowserVisible: false,
|
|
114
|
+
liveBrowserRuntime: null,
|
|
115
|
+
liveBrowserPref: 'auto',
|
|
116
|
+
liveStreamMode: 'idle',
|
|
117
|
+
liveStreamAbort: null,
|
|
118
|
+
liveStreamConnected: false,
|
|
119
|
+
liveFps: 0,
|
|
120
|
+
pointerDragging: false,
|
|
121
|
+
liveBrowserWidth: 1280,
|
|
122
|
+
liveBrowserHeight: 800,
|
|
123
|
+
controlOwner: 'human',
|
|
124
|
+
cliCommands: [],
|
|
125
|
+
mentionMode: 'file',
|
|
126
|
+
mentionItems: [],
|
|
127
|
+
mentionIndex: 0,
|
|
128
|
+
mentionStart: -1,
|
|
129
|
+
diffStaged: false,
|
|
130
|
+
isComposing: false,
|
|
131
|
+
stickToBottom: true,
|
|
132
|
+
activeController: null,
|
|
133
|
+
activeTurn: null,
|
|
134
|
+
activePrompt: '',
|
|
135
|
+
gameStatus: null,
|
|
136
|
+
gameLaunching: false
|
|
137
|
+
};
|
|
138
|
+
export function setLiveness(active, label) {
|
|
139
|
+
const isActive = Boolean(active);
|
|
140
|
+
dom.livenessPill.classList.toggle('active', isActive);
|
|
141
|
+
dom.appEl.classList.toggle('is-processing', isActive);
|
|
142
|
+
if (label)
|
|
143
|
+
dom.livenessText.textContent = label;
|
|
144
|
+
if (state.activeTurn && typeof state.activeTurn.setActivity === 'function') {
|
|
145
|
+
state.activeTurn.setActivity(isActive, label || 'Thinking...');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export function applyTheme(theme) {
|
|
149
|
+
const next = theme === 'light' ? 'light' : 'dark';
|
|
150
|
+
document.documentElement.setAttribute('data-theme', next);
|
|
151
|
+
if (dom.themeToggleBtn)
|
|
152
|
+
dom.themeToggleBtn.textContent = next === 'light' ? 'Dark' : 'Light';
|
|
153
|
+
try {
|
|
154
|
+
window.localStorage.setItem(THEME_KEY, next);
|
|
155
|
+
}
|
|
156
|
+
catch { /* ignore */ }
|
|
157
|
+
}
|
|
158
|
+
export function clampStudioWidth(raw) {
|
|
159
|
+
const n = Number(raw);
|
|
160
|
+
const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
|
|
161
|
+
const minW = 320;
|
|
162
|
+
const maxByChat = Math.max(minW, window.innerWidth - sidebarW - 360);
|
|
163
|
+
const maxW = Math.max(minW, Math.min(Math.floor(window.innerWidth * 0.72), maxByChat));
|
|
164
|
+
const base = Number.isFinite(n) && n > 0 ? Math.round(n) : DEFAULT_STUDIO_W;
|
|
165
|
+
return Math.max(minW, Math.min(maxW, base));
|
|
166
|
+
}
|
|
167
|
+
export function syncExpandedStudioWidth() {
|
|
168
|
+
const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
|
|
169
|
+
const available = Math.max(360, window.innerWidth - sidebarW - 380);
|
|
170
|
+
const expandedW = Math.min(1040, Math.max(360, available));
|
|
171
|
+
document.documentElement.style.setProperty('--studio-expanded-w', `${expandedW}px`);
|
|
172
|
+
}
|
|
173
|
+
export function applyStudioWidth(px, persist) {
|
|
174
|
+
const clamped = clampStudioWidth(px);
|
|
175
|
+
state.studioWidth = clamped;
|
|
176
|
+
document.documentElement.style.setProperty('--studio-w', `${clamped}px`);
|
|
177
|
+
syncExpandedStudioWidth();
|
|
178
|
+
if (dom.studioRoot) {
|
|
179
|
+
dom.studioRoot.style.flexBasis = '';
|
|
180
|
+
dom.studioRoot.style.width = '';
|
|
181
|
+
}
|
|
182
|
+
if (persist) {
|
|
183
|
+
try {
|
|
184
|
+
window.localStorage.setItem(STUDIO_WIDTH_KEY, String(clamped));
|
|
185
|
+
}
|
|
186
|
+
catch { /* ignore */ }
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export function setStageMode(hasMessages) {
|
|
190
|
+
dom.stageEl.classList.toggle('is-empty', !hasMessages);
|
|
191
|
+
dom.stageEl.classList.toggle('has-messages', hasMessages);
|
|
192
|
+
}
|
|
193
|
+
export function updateComposerMetrics() {
|
|
194
|
+
dom.promptInput.style.height = 'auto';
|
|
195
|
+
const nextH = Math.min(200, Math.max(26, dom.promptInput.scrollHeight));
|
|
196
|
+
dom.promptInput.style.height = `${nextH}px`;
|
|
197
|
+
const hasText = Boolean((dom.promptInput.value || '').trim());
|
|
198
|
+
const streaming = state.activeController !== null;
|
|
199
|
+
dom.sendBtn.classList.toggle('ready', !streaming && hasText);
|
|
200
|
+
dom.sendBtn.classList.toggle('stopping', streaming);
|
|
201
|
+
dom.sendBtn.setAttribute('aria-label', streaming ? 'Stop response' : 'Send message');
|
|
202
|
+
const iconSend = maybeById('icon-send');
|
|
203
|
+
const iconStop = maybeById('icon-stop');
|
|
204
|
+
if (iconSend)
|
|
205
|
+
iconSend.style.display = streaming ? 'none' : 'block';
|
|
206
|
+
if (iconStop)
|
|
207
|
+
iconStop.style.display = streaming ? 'block' : 'none';
|
|
208
|
+
try {
|
|
209
|
+
if (dom.promptInput.value)
|
|
210
|
+
window.localStorage.setItem(DRAFT_KEY, dom.promptInput.value);
|
|
211
|
+
else
|
|
212
|
+
window.localStorage.removeItem(DRAFT_KEY);
|
|
213
|
+
}
|
|
214
|
+
catch { /* ignore */ }
|
|
215
|
+
}
|
|
216
|
+
export function scrollToBottom(force, smooth = false) {
|
|
217
|
+
if (!dom.threadWrapEl)
|
|
218
|
+
return;
|
|
219
|
+
if (force || (autoScrollEnabled && state.stickToBottom)) {
|
|
220
|
+
if (smooth && typeof dom.threadWrapEl.scrollTo === 'function') {
|
|
221
|
+
dom.threadWrapEl.scrollTo({ top: dom.threadWrapEl.scrollHeight, behavior: 'smooth' });
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
dom.threadWrapEl.scrollTop = dom.threadWrapEl.scrollHeight;
|
|
225
|
+
}
|
|
226
|
+
state.lastScrollTop = dom.threadWrapEl.scrollTop;
|
|
227
|
+
state.stickToBottom = true;
|
|
228
|
+
dom.scrollBottomBtn.classList.remove('visible');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
export function syncUsagePill(tokens) {
|
|
232
|
+
state.usageTokens = Number(tokens) > 0 ? Number(tokens) : 0;
|
|
233
|
+
if (dom.usagePill) {
|
|
234
|
+
if (state.usageTokens > 0) {
|
|
235
|
+
dom.usagePill.textContent = `${state.usageTokens.toLocaleString()} tok`;
|
|
236
|
+
dom.usagePill.classList.add('visible');
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
dom.usagePill.textContent = '';
|
|
240
|
+
dom.usagePill.classList.remove('visible');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
export function restorePersistedPreferences() {
|
|
245
|
+
try {
|
|
246
|
+
const savedTheme = window.localStorage.getItem(THEME_KEY);
|
|
247
|
+
if (savedTheme === 'light' || savedTheme === 'dark')
|
|
248
|
+
applyTheme(savedTheme);
|
|
249
|
+
const savedDensity = window.localStorage.getItem(DENSITY_KEY);
|
|
250
|
+
if (savedDensity === 'compact' || savedDensity === 'comfortable')
|
|
251
|
+
applyDensity(savedDensity);
|
|
252
|
+
const savedAutoScroll = window.localStorage.getItem(AUTOSCROLL_KEY);
|
|
253
|
+
if (savedAutoScroll === '0' || savedAutoScroll === '1')
|
|
254
|
+
applyAutoScrollPref(savedAutoScroll === '1');
|
|
255
|
+
const savedPreset = window.localStorage.getItem(PERMISSION_PRESET_KEY);
|
|
256
|
+
if (savedPreset === 'read-only' || savedPreset === 'workspace-write' || savedPreset === 'danger-full-access') {
|
|
257
|
+
state.permissionPreset = savedPreset;
|
|
258
|
+
state.defaultPermissionPreset = savedPreset;
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
const legacyMode = window.localStorage.getItem(LEGACY_MODE_KEY);
|
|
262
|
+
if (legacyMode === 'A')
|
|
263
|
+
state.permissionPreset = 'read-only';
|
|
264
|
+
else if (legacyMode === 'C')
|
|
265
|
+
state.permissionPreset = 'danger-full-access';
|
|
266
|
+
else if (legacyMode === 'B')
|
|
267
|
+
state.permissionPreset = 'workspace-write';
|
|
268
|
+
state.defaultPermissionPreset = state.permissionPreset;
|
|
269
|
+
}
|
|
270
|
+
const savedDraft = window.localStorage.getItem(DRAFT_KEY);
|
|
271
|
+
if (savedDraft)
|
|
272
|
+
dom.promptInput.value = savedDraft;
|
|
273
|
+
const savedW = window.localStorage.getItem(STUDIO_WIDTH_KEY);
|
|
274
|
+
if (savedW)
|
|
275
|
+
applyStudioWidth(savedW, false);
|
|
276
|
+
}
|
|
277
|
+
catch { /* ignore */ }
|
|
278
|
+
if (window.innerWidth <= 960)
|
|
279
|
+
dom.appEl.classList.add('sidebar-collapsed');
|
|
280
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { dom, maybeById } from './dom.js';
|
|
2
|
+
import type { AppState } from './types.js';
|
|
3
|
+
|
|
4
|
+
export const DRAFT_KEY = 'causyne/web/composerDraft/v1';
|
|
5
|
+
export const THEME_KEY = 'causyne/web/theme/v1';
|
|
6
|
+
export const DENSITY_KEY = 'causyne/web/density/v1';
|
|
7
|
+
export const AUTOSCROLL_KEY = 'causyne/web/autoScroll/v1';
|
|
8
|
+
export const PERMISSION_PRESET_KEY = 'causyne/web/permissionPreset/v1';
|
|
9
|
+
export const LEGACY_MODE_KEY = 'causyne/web/autonomyMode/v1';
|
|
10
|
+
export const STUDIO_WIDTH_KEY = 'causyne/web/studioWidth/v1';
|
|
11
|
+
export const DEFAULT_STUDIO_W = 480;
|
|
12
|
+
|
|
13
|
+
let autoScrollEnabled = true;
|
|
14
|
+
|
|
15
|
+
export function isAutoScrollEnabled(): boolean {
|
|
16
|
+
return autoScrollEnabled;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function applyDensity(density: string): void {
|
|
20
|
+
const next = density === 'compact' ? 'compact' : 'comfortable';
|
|
21
|
+
document.documentElement.setAttribute('data-density', next);
|
|
22
|
+
try { window.localStorage.setItem(DENSITY_KEY, next); } catch { /* ignore */ }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function applyAutoScrollPref(enabled: boolean): void {
|
|
26
|
+
autoScrollEnabled = Boolean(enabled);
|
|
27
|
+
try { window.localStorage.setItem(AUTOSCROLL_KEY, autoScrollEnabled ? '1' : '0'); } catch { /* ignore */ }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const hashParams = new URLSearchParams((window.location.hash || '').replace(/^#/, ''));
|
|
31
|
+
export const TOKEN_STORE_KEY = `causyne/web/token:${window.location.origin}`;
|
|
32
|
+
|
|
33
|
+
let sessionToken = hashParams.get('token') || '';
|
|
34
|
+
if (sessionToken) {
|
|
35
|
+
try { window.sessionStorage.setItem(TOKEN_STORE_KEY, sessionToken); } catch { /* ignore */ }
|
|
36
|
+
try { window.history.replaceState(null, '', window.location.pathname + window.location.search); } catch { /* ignore */ }
|
|
37
|
+
} else {
|
|
38
|
+
try { sessionToken = window.sessionStorage.getItem(TOKEN_STORE_KEY) || ''; } catch { /* ignore */ }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let apiBase = '';
|
|
42
|
+
try {
|
|
43
|
+
const qs = new URLSearchParams(window.location.search || '');
|
|
44
|
+
const bridge = (qs.get('bridge') || '').trim();
|
|
45
|
+
if (bridge) {
|
|
46
|
+
const parsed = new URL(bridge);
|
|
47
|
+
const host = parsed.hostname.toLowerCase();
|
|
48
|
+
if (parsed.protocol === 'http:' && (host === '127.0.0.1' || host === 'localhost')) {
|
|
49
|
+
apiBase = parsed.origin;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
} catch {
|
|
53
|
+
// Ignore invalid bridge parameter.
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function apiUrl(p: string): string {
|
|
57
|
+
return apiBase + p;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function authHeaders(extra?: Record<string, string>): Record<string, string> {
|
|
61
|
+
return Object.assign({ Authorization: `Bearer ${sessionToken}` }, extra || {});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const state: AppState = {
|
|
65
|
+
sessionId: null,
|
|
66
|
+
sessions: [],
|
|
67
|
+
models: null,
|
|
68
|
+
modelTab: null,
|
|
69
|
+
modelQuery: '',
|
|
70
|
+
modelExpandedPeers: new Set<string>(),
|
|
71
|
+
modelSelectedIndex: 0,
|
|
72
|
+
modelRefreshing: false,
|
|
73
|
+
modelLiveHydrated: false,
|
|
74
|
+
modelNotice: '',
|
|
75
|
+
activeProvider: 'unconfigured',
|
|
76
|
+
activeModel: 'default',
|
|
77
|
+
permissionPreset: 'workspace-write',
|
|
78
|
+
defaultPermissionPreset: 'workspace-write',
|
|
79
|
+
workspaceRoot: '',
|
|
80
|
+
lastScrollTop: 0,
|
|
81
|
+
attachments: [],
|
|
82
|
+
currentDir: '',
|
|
83
|
+
parentDir: '',
|
|
84
|
+
studioTab: 'start',
|
|
85
|
+
studioOpenTabs: ['start'],
|
|
86
|
+
studioSplit: false,
|
|
87
|
+
studioSecondaryTab: 'terminal',
|
|
88
|
+
studioSplitRatio: 0.5,
|
|
89
|
+
studioExpanded: false,
|
|
90
|
+
studioWidth: DEFAULT_STUDIO_W,
|
|
91
|
+
terminalSessions: [],
|
|
92
|
+
activeTerminalId: null,
|
|
93
|
+
availableShells: [],
|
|
94
|
+
canvasPreviewMode: false,
|
|
95
|
+
canvasDirty: false,
|
|
96
|
+
artifactVersions: new Map(),
|
|
97
|
+
sessionWorkspaces: new Map(),
|
|
98
|
+
sessionSwitchSeq: 0,
|
|
99
|
+
usageTokens: 0,
|
|
100
|
+
diffScope: 'session',
|
|
101
|
+
browserEngine: 'agent',
|
|
102
|
+
browserMode: 'empty',
|
|
103
|
+
browserUrl: '',
|
|
104
|
+
browserSrcdoc: '',
|
|
105
|
+
browserViewport: 'desktop',
|
|
106
|
+
browserFrameStrictSandbox: false,
|
|
107
|
+
liveBrowserActive: false,
|
|
108
|
+
liveBrowserVisible: false,
|
|
109
|
+
liveBrowserRuntime: null,
|
|
110
|
+
liveBrowserPref: 'auto',
|
|
111
|
+
liveStreamMode: 'idle',
|
|
112
|
+
liveStreamAbort: null,
|
|
113
|
+
liveStreamConnected: false,
|
|
114
|
+
liveFps: 0,
|
|
115
|
+
pointerDragging: false,
|
|
116
|
+
liveBrowserWidth: 1280,
|
|
117
|
+
liveBrowserHeight: 800,
|
|
118
|
+
controlOwner: 'human',
|
|
119
|
+
cliCommands: [],
|
|
120
|
+
mentionMode: 'file',
|
|
121
|
+
mentionItems: [],
|
|
122
|
+
mentionIndex: 0,
|
|
123
|
+
mentionStart: -1,
|
|
124
|
+
diffStaged: false,
|
|
125
|
+
isComposing: false,
|
|
126
|
+
stickToBottom: true,
|
|
127
|
+
activeController: null,
|
|
128
|
+
activeTurn: null,
|
|
129
|
+
activePrompt: '',
|
|
130
|
+
gameStatus: null,
|
|
131
|
+
gameLaunching: false
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export function setLiveness(active: boolean, label: string): void {
|
|
135
|
+
const isActive = Boolean(active);
|
|
136
|
+
dom.livenessPill.classList.toggle('active', isActive);
|
|
137
|
+
dom.appEl.classList.toggle('is-processing', isActive);
|
|
138
|
+
if (label) dom.livenessText.textContent = label;
|
|
139
|
+
if (state.activeTurn && typeof state.activeTurn.setActivity === 'function') {
|
|
140
|
+
state.activeTurn.setActivity(isActive, label || 'Thinking...');
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function applyTheme(theme: string): void {
|
|
145
|
+
const next = theme === 'light' ? 'light' : 'dark';
|
|
146
|
+
document.documentElement.setAttribute('data-theme', next);
|
|
147
|
+
if (dom.themeToggleBtn) dom.themeToggleBtn.textContent = next === 'light' ? 'Dark' : 'Light';
|
|
148
|
+
try { window.localStorage.setItem(THEME_KEY, next); } catch { /* ignore */ }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function clampStudioWidth(raw: unknown): number {
|
|
152
|
+
const n = Number(raw);
|
|
153
|
+
const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
|
|
154
|
+
const minW = 320;
|
|
155
|
+
const maxByChat = Math.max(minW, window.innerWidth - sidebarW - 360);
|
|
156
|
+
const maxW = Math.max(minW, Math.min(Math.floor(window.innerWidth * 0.72), maxByChat));
|
|
157
|
+
const base = Number.isFinite(n) && n > 0 ? Math.round(n) : DEFAULT_STUDIO_W;
|
|
158
|
+
return Math.max(minW, Math.min(maxW, base));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function syncExpandedStudioWidth(): void {
|
|
162
|
+
const sidebarW = dom.appEl.classList.contains('sidebar-collapsed') || window.innerWidth <= 960 ? 0 : 256;
|
|
163
|
+
const available = Math.max(360, window.innerWidth - sidebarW - 380);
|
|
164
|
+
const expandedW = Math.min(1040, Math.max(360, available));
|
|
165
|
+
document.documentElement.style.setProperty('--studio-expanded-w', `${expandedW}px`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function applyStudioWidth(px: number | string, persist: boolean): void {
|
|
169
|
+
const clamped = clampStudioWidth(px);
|
|
170
|
+
state.studioWidth = clamped;
|
|
171
|
+
document.documentElement.style.setProperty('--studio-w', `${clamped}px`);
|
|
172
|
+
syncExpandedStudioWidth();
|
|
173
|
+
if (dom.studioRoot) {
|
|
174
|
+
dom.studioRoot.style.flexBasis = '';
|
|
175
|
+
dom.studioRoot.style.width = '';
|
|
176
|
+
}
|
|
177
|
+
if (persist) {
|
|
178
|
+
try { window.localStorage.setItem(STUDIO_WIDTH_KEY, String(clamped)); } catch { /* ignore */ }
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function setStageMode(hasMessages: boolean): void {
|
|
183
|
+
dom.stageEl.classList.toggle('is-empty', !hasMessages);
|
|
184
|
+
dom.stageEl.classList.toggle('has-messages', hasMessages);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function updateComposerMetrics(): void {
|
|
188
|
+
dom.promptInput.style.height = 'auto';
|
|
189
|
+
const nextH = Math.min(200, Math.max(26, dom.promptInput.scrollHeight));
|
|
190
|
+
dom.promptInput.style.height = `${nextH}px`;
|
|
191
|
+
const hasText = Boolean((dom.promptInput.value || '').trim());
|
|
192
|
+
const streaming = state.activeController !== null;
|
|
193
|
+
dom.sendBtn.classList.toggle('ready', !streaming && hasText);
|
|
194
|
+
dom.sendBtn.classList.toggle('stopping', streaming);
|
|
195
|
+
dom.sendBtn.setAttribute('aria-label', streaming ? 'Stop response' : 'Send message');
|
|
196
|
+
const iconSend = maybeById('icon-send');
|
|
197
|
+
const iconStop = maybeById('icon-stop');
|
|
198
|
+
if (iconSend) iconSend.style.display = streaming ? 'none' : 'block';
|
|
199
|
+
if (iconStop) iconStop.style.display = streaming ? 'block' : 'none';
|
|
200
|
+
try {
|
|
201
|
+
if (dom.promptInput.value) window.localStorage.setItem(DRAFT_KEY, dom.promptInput.value);
|
|
202
|
+
else window.localStorage.removeItem(DRAFT_KEY);
|
|
203
|
+
} catch { /* ignore */ }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function scrollToBottom(force: boolean, smooth = false): void {
|
|
207
|
+
if (!dom.threadWrapEl) return;
|
|
208
|
+
if (force || (autoScrollEnabled && state.stickToBottom)) {
|
|
209
|
+
if (smooth && typeof dom.threadWrapEl.scrollTo === 'function') {
|
|
210
|
+
dom.threadWrapEl.scrollTo({ top: dom.threadWrapEl.scrollHeight, behavior: 'smooth' });
|
|
211
|
+
} else {
|
|
212
|
+
dom.threadWrapEl.scrollTop = dom.threadWrapEl.scrollHeight;
|
|
213
|
+
}
|
|
214
|
+
state.lastScrollTop = dom.threadWrapEl.scrollTop;
|
|
215
|
+
state.stickToBottom = true;
|
|
216
|
+
dom.scrollBottomBtn.classList.remove('visible');
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function syncUsagePill(tokens: number): void {
|
|
221
|
+
state.usageTokens = Number(tokens) > 0 ? Number(tokens) : 0;
|
|
222
|
+
if (dom.usagePill) {
|
|
223
|
+
if (state.usageTokens > 0) {
|
|
224
|
+
dom.usagePill.textContent = `${state.usageTokens.toLocaleString()} tok`;
|
|
225
|
+
dom.usagePill.classList.add('visible');
|
|
226
|
+
} else {
|
|
227
|
+
dom.usagePill.textContent = '';
|
|
228
|
+
dom.usagePill.classList.remove('visible');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function restorePersistedPreferences(): void {
|
|
234
|
+
try {
|
|
235
|
+
const savedTheme = window.localStorage.getItem(THEME_KEY);
|
|
236
|
+
if (savedTheme === 'light' || savedTheme === 'dark') applyTheme(savedTheme);
|
|
237
|
+
const savedDensity = window.localStorage.getItem(DENSITY_KEY);
|
|
238
|
+
if (savedDensity === 'compact' || savedDensity === 'comfortable') applyDensity(savedDensity);
|
|
239
|
+
const savedAutoScroll = window.localStorage.getItem(AUTOSCROLL_KEY);
|
|
240
|
+
if (savedAutoScroll === '0' || savedAutoScroll === '1') applyAutoScrollPref(savedAutoScroll === '1');
|
|
241
|
+
const savedPreset = window.localStorage.getItem(PERMISSION_PRESET_KEY);
|
|
242
|
+
if (savedPreset === 'read-only' || savedPreset === 'workspace-write' || savedPreset === 'danger-full-access') {
|
|
243
|
+
state.permissionPreset = savedPreset;
|
|
244
|
+
state.defaultPermissionPreset = savedPreset;
|
|
245
|
+
} else {
|
|
246
|
+
const legacyMode = window.localStorage.getItem(LEGACY_MODE_KEY);
|
|
247
|
+
if (legacyMode === 'A') state.permissionPreset = 'read-only';
|
|
248
|
+
else if (legacyMode === 'C') state.permissionPreset = 'danger-full-access';
|
|
249
|
+
else if (legacyMode === 'B') state.permissionPreset = 'workspace-write';
|
|
250
|
+
state.defaultPermissionPreset = state.permissionPreset;
|
|
251
|
+
}
|
|
252
|
+
const savedDraft = window.localStorage.getItem(DRAFT_KEY);
|
|
253
|
+
if (savedDraft) dom.promptInput.value = savedDraft;
|
|
254
|
+
const savedW = window.localStorage.getItem(STUDIO_WIDTH_KEY);
|
|
255
|
+
if (savedW) applyStudioWidth(savedW, false);
|
|
256
|
+
} catch { /* ignore */ }
|
|
257
|
+
|
|
258
|
+
if (window.innerWidth <= 960) dom.appEl.classList.add('sidebar-collapsed');
|
|
259
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
function solveBarrierBisect(fn, lo, hi) {
|
|
2
|
+
if (lo >= hi)
|
|
3
|
+
return hi;
|
|
4
|
+
let a = lo;
|
|
5
|
+
let b = hi;
|
|
6
|
+
if (fn(a) >= 0)
|
|
7
|
+
return a;
|
|
8
|
+
if (fn(b) <= 0)
|
|
9
|
+
return b;
|
|
10
|
+
for (let k = 0; k < 22 && (b - a) > 0.05; k += 1) {
|
|
11
|
+
const mid = (a + b) * 0.5;
|
|
12
|
+
if (fn(mid) <= 0)
|
|
13
|
+
a = mid;
|
|
14
|
+
else
|
|
15
|
+
b = mid;
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
}
|
|
19
|
+
export function createStreamSmoother(onRender) {
|
|
20
|
+
let fullText = '';
|
|
21
|
+
let shownLen = 0;
|
|
22
|
+
let vel = 120;
|
|
23
|
+
let lastT = 0;
|
|
24
|
+
let startMs = 0;
|
|
25
|
+
const arrivals = [[-9999, 0]];
|
|
26
|
+
let done = false;
|
|
27
|
+
let rafId = 0;
|
|
28
|
+
const alpha = 0.99;
|
|
29
|
+
const gamma = 1e-5;
|
|
30
|
+
const reducedMotion = Boolean(window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches);
|
|
31
|
+
function step() {
|
|
32
|
+
rafId = 0;
|
|
33
|
+
const total = fullText.length;
|
|
34
|
+
if (reducedMotion || total === 0) {
|
|
35
|
+
shownLen = total;
|
|
36
|
+
onRender(fullText, !done, shownLen);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (shownLen >= total) {
|
|
40
|
+
if (done)
|
|
41
|
+
onRender(fullText, false, total);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const nowSec = (performance.now() - startMs) / 1000;
|
|
45
|
+
const dt = Math.max(nowSec - lastT, 1e-4);
|
|
46
|
+
const upper = total + (done ? 120 : 0);
|
|
47
|
+
const lagTarget = 0.9 * nowSec - 0.25;
|
|
48
|
+
let minChars = 0;
|
|
49
|
+
for (let idx = arrivals.length - 1; idx >= 0; idx -= 1) {
|
|
50
|
+
const pair = arrivals[idx];
|
|
51
|
+
if (pair[0] < lagTarget) {
|
|
52
|
+
minChars = pair[1];
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const lo = Math.max(shownLen, Math.min(total, done ? total : minChars));
|
|
57
|
+
const nextX = done && (total - shownLen) <= 18
|
|
58
|
+
? total
|
|
59
|
+
: solveBarrierBisect((x) => {
|
|
60
|
+
const d = (x - shownLen) / dt;
|
|
61
|
+
return 2 * (done ? gamma * 0.01 : gamma) * (1 / dt) * (d - vel)
|
|
62
|
+
- 1 / Math.max(1e-4, x - lo)
|
|
63
|
+
+ 1 / Math.max(1e-4, upper - x);
|
|
64
|
+
}, lo, upper);
|
|
65
|
+
const clamped = Math.min(total, Math.max(shownLen + 1, Math.round(nextX)));
|
|
66
|
+
const instV = (clamped - shownLen) / dt;
|
|
67
|
+
vel = Math.max(45, Math.min(2400, alpha * vel + (1 - alpha) * instV));
|
|
68
|
+
shownLen = clamped;
|
|
69
|
+
lastT = nowSec;
|
|
70
|
+
onRender(fullText.slice(0, shownLen), !done || shownLen < total, shownLen);
|
|
71
|
+
if (shownLen < total)
|
|
72
|
+
rafId = window.requestAnimationFrame(step);
|
|
73
|
+
else if (done)
|
|
74
|
+
onRender(fullText, false, total);
|
|
75
|
+
}
|
|
76
|
+
function schedule() {
|
|
77
|
+
if (!rafId)
|
|
78
|
+
rafId = window.requestAnimationFrame(step);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
push(delta) {
|
|
82
|
+
if (!delta)
|
|
83
|
+
return;
|
|
84
|
+
if (!startMs) {
|
|
85
|
+
startMs = performance.now();
|
|
86
|
+
lastT = 0;
|
|
87
|
+
}
|
|
88
|
+
fullText += delta;
|
|
89
|
+
const tSec = (performance.now() - startMs) / 1000;
|
|
90
|
+
arrivals.push([tSec, fullText.length]);
|
|
91
|
+
if (arrivals.length > 120)
|
|
92
|
+
arrivals.shift();
|
|
93
|
+
schedule();
|
|
94
|
+
},
|
|
95
|
+
setFull(text, streaming) {
|
|
96
|
+
fullText = String(text || '');
|
|
97
|
+
if (!streaming) {
|
|
98
|
+
if (rafId) {
|
|
99
|
+
window.cancelAnimationFrame(rafId);
|
|
100
|
+
rafId = 0;
|
|
101
|
+
}
|
|
102
|
+
done = true;
|
|
103
|
+
shownLen = fullText.length;
|
|
104
|
+
onRender(fullText, false, shownLen);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (!startMs) {
|
|
108
|
+
startMs = performance.now();
|
|
109
|
+
lastT = 0;
|
|
110
|
+
}
|
|
111
|
+
arrivals.push([(performance.now() - startMs) / 1000, fullText.length]);
|
|
112
|
+
schedule();
|
|
113
|
+
},
|
|
114
|
+
flush() {
|
|
115
|
+
done = true;
|
|
116
|
+
if (rafId) {
|
|
117
|
+
window.cancelAnimationFrame(rafId);
|
|
118
|
+
rafId = 0;
|
|
119
|
+
}
|
|
120
|
+
shownLen = fullText.length;
|
|
121
|
+
onRender(fullText, false, shownLen);
|
|
122
|
+
},
|
|
123
|
+
getText() {
|
|
124
|
+
return fullText;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|