aicodeman 0.6.9 → 0.6.10
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/dist/web/paste-image-gc.d.ts +7 -0
- package/dist/web/paste-image-gc.d.ts.map +1 -0
- package/dist/web/paste-image-gc.js +69 -0
- package/dist/web/paste-image-gc.js.map +1 -0
- package/dist/web/public/api-client.3adebdc2.js.gz +0 -0
- package/dist/web/public/{app.2353feb8.js → app.03ed8e4e.js} +2 -2
- package/dist/web/public/app.03ed8e4e.js.br +0 -0
- package/dist/web/public/app.03ed8e4e.js.gz +0 -0
- package/dist/web/public/{constants.35154472.js → constants.cb6426c4.js} +46 -0
- package/dist/web/public/constants.cb6426c4.js.br +0 -0
- package/dist/web/public/constants.cb6426c4.js.gz +0 -0
- package/dist/web/public/{image-input.2d862e9c.js → image-input.926911b4.js} +7 -2
- package/dist/web/public/image-input.926911b4.js.br +0 -0
- package/dist/web/public/image-input.926911b4.js.gz +0 -0
- package/dist/web/public/index.html +6 -6
- package/dist/web/public/index.html.br +0 -0
- package/dist/web/public/index.html.gz +0 -0
- package/dist/web/public/input-cjk.88082175.js.gz +0 -0
- package/dist/web/public/keyboard-accessory.29aebd9c.js.gz +0 -0
- package/dist/web/public/mobile-handlers.1e2a8ef8.js.gz +0 -0
- package/dist/web/public/mobile.37d62c06.css.gz +0 -0
- package/dist/web/public/notification-manager.9c984ac2.js.gz +0 -0
- package/dist/web/public/orchestrator-panel.js.gz +0 -0
- package/dist/web/public/panels-ui.cf998835.js.gz +0 -0
- package/dist/web/public/ralph-panel.61076370.js.gz +0 -0
- package/dist/web/public/ralph-wizard.6b0f0be7.js.gz +0 -0
- package/dist/web/public/respawn-ui.5377f958.js.gz +0 -0
- package/dist/web/public/session-ui.f1555cd1.js.gz +0 -0
- package/dist/web/public/settings-ui.25a18120.js.gz +0 -0
- package/dist/web/public/styles.1f5114f6.css.gz +0 -0
- package/dist/web/public/subagent-windows.a366a4ad.js.gz +0 -0
- package/dist/web/public/sw.js.gz +0 -0
- package/dist/web/public/terminal-ui.d019e248.js +3 -0
- package/dist/web/public/terminal-ui.d019e248.js.br +0 -0
- package/dist/web/public/terminal-ui.d019e248.js.gz +0 -0
- package/dist/web/public/upload.html.gz +0 -0
- package/dist/web/public/vendor/marked.min.js.gz +0 -0
- package/dist/web/public/vendor/xterm-addon-fit.min.js.gz +0 -0
- package/dist/web/public/vendor/xterm-addon-unicode11.min.js.gz +0 -0
- package/dist/web/public/vendor/xterm-addon-webgl.min.js.gz +0 -0
- package/dist/web/public/vendor/xterm-zerolag-input.137ad9f0.js.gz +0 -0
- package/dist/web/public/vendor/xterm.css.gz +0 -0
- package/dist/web/public/vendor/xterm.min.js.gz +0 -0
- package/dist/web/public/voice-input.085e9e73.js.gz +0 -0
- package/dist/web/routes/session-routes.d.ts +12 -0
- package/dist/web/routes/session-routes.d.ts.map +1 -1
- package/dist/web/routes/session-routes.js +183 -57
- package/dist/web/routes/session-routes.js.map +1 -1
- package/dist/web/server.d.ts +1 -0
- package/dist/web/server.d.ts.map +1 -1
- package/dist/web/server.js +27 -5
- package/dist/web/server.js.map +1 -1
- package/package.json +2 -1
- package/dist/web/public/app.2353feb8.js.br +0 -0
- package/dist/web/public/app.2353feb8.js.gz +0 -0
- package/dist/web/public/constants.35154472.js.br +0 -0
- package/dist/web/public/constants.35154472.js.gz +0 -0
- package/dist/web/public/image-input.2d862e9c.js.br +0 -0
- package/dist/web/public/image-input.2d862e9c.js.gz +0 -0
- package/dist/web/public/terminal-ui.d069d610.js +0 -3
- package/dist/web/public/terminal-ui.d069d610.js.br +0 -0
- package/dist/web/public/terminal-ui.d069d610.js.gz +0 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SessionPort } from './ports/index.js';
|
|
2
|
+
export declare function sweepPasteImagesOnce(ctx: Pick<SessionPort, 'sessions'>, now?: number): Promise<{
|
|
3
|
+
scanned: number;
|
|
4
|
+
deleted: number;
|
|
5
|
+
}>;
|
|
6
|
+
export declare function startPasteImageGc(ctx: Pick<SessionPort, 'sessions'>): () => void;
|
|
7
|
+
//# sourceMappingURL=paste-image-gc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paste-image-gc.d.ts","sourceRoot":"","sources":["../../src/web/paste-image-gc.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAMpD,wBAAsB,oBAAoB,CACxC,GAAG,EAAE,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,EAClC,GAAG,GAAE,MAAmB,GACvB,OAAO,CAAC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC,CA6B/C;AAED,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,GAAG,MAAM,IAAI,CAahF"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Periodic GC for paste-image files.
|
|
3
|
+
*
|
|
4
|
+
* Without cleanup, /api/sessions/:id/paste-image accumulates files indefinitely
|
|
5
|
+
* under {workingDir}/.claude-images/. The route only triggers cleanup on
|
|
6
|
+
* killMux=true session deletion, so long-lived sessions can fill disk under
|
|
7
|
+
* heavy pasting. This sweeper bounds disk use by deleting `paste-*` files
|
|
8
|
+
* older than MAX_AGE_MS from each live session's image dir on an interval.
|
|
9
|
+
*
|
|
10
|
+
* Conservative defaults — only files matching the `paste-` prefix are
|
|
11
|
+
* considered, and we lstat (not stat) so a planted symlink cannot escape the
|
|
12
|
+
* image dir.
|
|
13
|
+
*/
|
|
14
|
+
import fs from 'node:fs/promises';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
|
17
|
+
const SWEEP_INTERVAL_MS = 60 * 60 * 1000; // 1 hour
|
|
18
|
+
const INITIAL_DELAY_MS = 30 * 1000; // 30s after startup
|
|
19
|
+
export async function sweepPasteImagesOnce(ctx, now = Date.now()) {
|
|
20
|
+
const cutoff = now - MAX_AGE_MS;
|
|
21
|
+
let scanned = 0;
|
|
22
|
+
let deleted = 0;
|
|
23
|
+
for (const session of ctx.sessions.values()) {
|
|
24
|
+
const dir = join(session.workingDir, '.claude-images');
|
|
25
|
+
let entries;
|
|
26
|
+
try {
|
|
27
|
+
entries = await fs.readdir(dir);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
continue; // dir absent — nothing to do
|
|
31
|
+
}
|
|
32
|
+
for (const name of entries) {
|
|
33
|
+
if (!name.startsWith('paste-'))
|
|
34
|
+
continue;
|
|
35
|
+
const p = join(dir, name);
|
|
36
|
+
scanned += 1;
|
|
37
|
+
try {
|
|
38
|
+
const st = await fs.lstat(p);
|
|
39
|
+
if (!st.isFile())
|
|
40
|
+
continue;
|
|
41
|
+
if (st.mtimeMs < cutoff) {
|
|
42
|
+
await fs.unlink(p);
|
|
43
|
+
deleted += 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// best-effort: skip permission/race errors silently
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return { scanned, deleted };
|
|
52
|
+
}
|
|
53
|
+
export function startPasteImageGc(ctx) {
|
|
54
|
+
const initial = setTimeout(() => {
|
|
55
|
+
void sweepPasteImagesOnce(ctx);
|
|
56
|
+
}, INITIAL_DELAY_MS);
|
|
57
|
+
const interval = setInterval(() => {
|
|
58
|
+
void sweepPasteImagesOnce(ctx);
|
|
59
|
+
}, SWEEP_INTERVAL_MS);
|
|
60
|
+
if (typeof initial.unref === 'function')
|
|
61
|
+
initial.unref();
|
|
62
|
+
if (typeof interval.unref === 'function')
|
|
63
|
+
interval.unref();
|
|
64
|
+
return () => {
|
|
65
|
+
clearTimeout(initial);
|
|
66
|
+
clearInterval(interval);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=paste-image-gc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paste-image-gc.js","sourceRoot":"","sources":["../../src/web/paste-image-gc.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,MAAM,UAAU,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;AACrD,MAAM,iBAAiB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,SAAS;AACnD,MAAM,gBAAgB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,oBAAoB;AAExD,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,GAAkC,EAClC,MAAc,IAAI,CAAC,GAAG,EAAE;IAExB,MAAM,MAAM,GAAG,GAAG,GAAG,UAAU,CAAC;IAChC,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,MAAM,OAAO,IAAI,GAAG,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;QAC5C,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;QACvD,IAAI,OAAiB,CAAC;QACtB,IAAI,CAAC;YACH,OAAO,GAAG,MAAM,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAClC,CAAC;QAAC,MAAM,CAAC;YACP,SAAS,CAAC,6BAA6B;QACzC,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;YAC3B,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;gBAAE,SAAS;YACzC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YAC1B,OAAO,IAAI,CAAC,CAAC;YACb,IAAI,CAAC;gBACH,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAC7B,IAAI,CAAC,EAAE,CAAC,MAAM,EAAE;oBAAE,SAAS;gBAC3B,IAAI,EAAE,CAAC,OAAO,GAAG,MAAM,EAAE,CAAC;oBACxB,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;oBACnB,OAAO,IAAI,CAAC,CAAC;gBACf,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,oDAAoD;YACtD,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,GAAkC;IAClE,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE;QAC9B,KAAK,oBAAoB,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC,EAAE,gBAAgB,CAAC,CAAC;IACrB,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE;QAChC,KAAK,oBAAoB,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC,EAAE,iBAAiB,CAAC,CAAC;IACtB,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,UAAU;QAAE,OAAO,CAAC,KAAK,EAAE,CAAC;IACzD,IAAI,OAAO,QAAQ,CAAC,KAAK,KAAK,UAAU;QAAE,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC3D,OAAO,GAAS,EAAE;QAChB,YAAY,CAAC,OAAO,CAAC,CAAC;QACtB,aAAa,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC,CAAC;AACJ,CAAC"}
|
|
Binary file
|
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
`)}))}catch{}},2e3),window.addEventListener("error",l=>{_crashDiag.log(`ERROR: ${l.message} at ${l.filename}:${l.lineno}`),console.error("[CRASH-DIAG] Uncaught error:",l.message,`
|
|
5
5
|
File:`,l.filename,":",l.lineno,":",l.colno,`
|
|
6
6
|
Stack:`,l.error?.stack)}),window.addEventListener("unhandledrejection",l=>{_crashDiag.log(`UNHANDLED: ${l.reason?.message||l.reason}`),console.error("[CRASH-DIAG] Unhandled promise rejection:",l.reason?.message||l.reason,`
|
|
7
|
-
Stack:`,l.reason?.stack)}),typeof PerformanceObserver<"u")try{new PerformanceObserver(e=>{for(const t of e.getEntries())t.duration>200&&(_crashDiag.log(`LONG_TASK: ${t.duration.toFixed(0)}ms`),console.warn(`[CRASH-DIAG] Long task: ${t.duration.toFixed(0)}ms (type: ${t.entryType}, name: ${t.name})`))}).observe({type:"longtask",buffered:!0})}catch{}const _origGetContext=HTMLCanvasElement.prototype.getContext;HTMLCanvasElement.prototype.getContext=function(l,...e){const t=_origGetContext.call(this,l,...e);return(l==="webgl2"||l==="webgl")&&(this.addEventListener("webglcontextlost",s=>{_crashDiag.log(`WEBGL_LOST: ${this.width}x${this.height}`),console.error("[CRASH-DIAG] WebGL context LOST on canvas",this.width,"x",this.height,"\u2014 prevented:",s.defaultPrevented)}),this.addEventListener("webglcontextrestored",()=>{_crashDiag.log("WEBGL_RESTORED"),console.warn("[CRASH-DIAG] WebGL context restored")})),t};const _SSE_HANDLER_MAP=[[SSE_EVENTS.INIT,"_onInit"],[SSE_EVENTS.SESSION_CREATED,"_onSessionCreated"],[SSE_EVENTS.SESSION_UPDATED,"_onSessionUpdated"],[SSE_EVENTS.SESSION_DELETED,"_onSessionDeleted"],[SSE_EVENTS.SESSION_TERMINAL,"_onSSETerminal"],[SSE_EVENTS.SESSION_NEEDS_REFRESH,"_onSSENeedsRefresh"],[SSE_EVENTS.SESSION_CLEAR_TERMINAL,"_onSSEClearTerminal"],[SSE_EVENTS.SESSION_COMPLETION,"_onSessionCompletion"],[SSE_EVENTS.SESSION_ERROR,"_onSessionError"],[SSE_EVENTS.SESSION_EXIT,"_onSessionExit"],[SSE_EVENTS.SESSION_IDLE,"_onSessionIdle"],[SSE_EVENTS.SESSION_WORKING,"_onSessionWorking"],[SSE_EVENTS.SESSION_AUTO_CLEAR,"_onSessionAutoClear"],[SSE_EVENTS.SESSION_CLI_INFO,"_onSessionCliInfo"],[SSE_EVENTS.SCHEDULED_CREATED,"_onScheduledCreated"],[SSE_EVENTS.SCHEDULED_UPDATED,"_onScheduledUpdated"],[SSE_EVENTS.SCHEDULED_COMPLETED,"_onScheduledCompleted"],[SSE_EVENTS.SCHEDULED_STOPPED,"_onScheduledStopped"],[SSE_EVENTS.RESPAWN_STARTED,"_onRespawnStarted"],[SSE_EVENTS.RESPAWN_STOPPED,"_onRespawnStopped"],[SSE_EVENTS.RESPAWN_STATE_CHANGED,"_onRespawnStateChanged"],[SSE_EVENTS.RESPAWN_CYCLE_STARTED,"_onRespawnCycleStarted"],[SSE_EVENTS.RESPAWN_BLOCKED,"_onRespawnBlocked"],[SSE_EVENTS.RESPAWN_AUTO_ACCEPT_SENT,"_onRespawnAutoAcceptSent"],[SSE_EVENTS.RESPAWN_DETECTION_UPDATE,"_onRespawnDetectionUpdate"],[SSE_EVENTS.RESPAWN_TIMER_STARTED,"_onRespawnTimerStarted"],[SSE_EVENTS.RESPAWN_TIMER_CANCELLED,"_onRespawnTimerCancelled"],[SSE_EVENTS.RESPAWN_TIMER_COMPLETED,"_onRespawnTimerCompleted"],[SSE_EVENTS.RESPAWN_ERROR,"_onRespawnError"],[SSE_EVENTS.RESPAWN_ACTION_LOG,"_onRespawnActionLog"],[SSE_EVENTS.TASK_CREATED,"_onTaskCreated"],[SSE_EVENTS.TASK_COMPLETED,"_onTaskCompleted"],[SSE_EVENTS.TASK_FAILED,"_onTaskFailed"],[SSE_EVENTS.TASK_UPDATED,"_onTaskUpdated"],[SSE_EVENTS.MUX_CREATED,"_onMuxCreated"],[SSE_EVENTS.MUX_KILLED,"_onMuxKilled"],[SSE_EVENTS.MUX_DIED,"_onMuxDied"],[SSE_EVENTS.MUX_STATS_UPDATED,"_onMuxStatsUpdated"],[SSE_EVENTS.SESSION_RALPH_LOOP_UPDATE,"_onRalphLoopUpdate"],[SSE_EVENTS.SESSION_RALPH_TODO_UPDATE,"_onRalphTodoUpdate"],[SSE_EVENTS.SESSION_RALPH_COMPLETION_DETECTED,"_onRalphCompletionDetected"],[SSE_EVENTS.SESSION_RALPH_STATUS_UPDATE,"_onRalphStatusUpdate"],[SSE_EVENTS.SESSION_CIRCUIT_BREAKER_UPDATE,"_onCircuitBreakerUpdate"],[SSE_EVENTS.SESSION_EXIT_GATE_MET,"_onExitGateMet"],[SSE_EVENTS.SESSION_BASH_TOOL_START,"_onBashToolStart"],[SSE_EVENTS.SESSION_BASH_TOOL_END,"_onBashToolEnd"],[SSE_EVENTS.SESSION_BASH_TOOLS_UPDATE,"_onBashToolsUpdate"],[SSE_EVENTS.HOOK_IDLE_PROMPT,"_onHookIdlePrompt"],[SSE_EVENTS.HOOK_PERMISSION_PROMPT,"_onHookPermissionPrompt"],[SSE_EVENTS.HOOK_ELICITATION_DIALOG,"_onHookElicitationDialog"],[SSE_EVENTS.HOOK_STOP,"_onHookStop"],[SSE_EVENTS.HOOK_TEAMMATE_IDLE,"_onHookTeammateIdle"],[SSE_EVENTS.HOOK_TASK_COMPLETED,"_onHookTaskCompleted"],[SSE_EVENTS.SUBAGENT_DISCOVERED,"_onSubagentDiscovered"],[SSE_EVENTS.SUBAGENT_UPDATED,"_onSubagentUpdated"],[SSE_EVENTS.SUBAGENT_TOOL_CALL,"_onSubagentToolCall"],[SSE_EVENTS.SUBAGENT_PROGRESS,"_onSubagentProgress"],[SSE_EVENTS.SUBAGENT_MESSAGE,"_onSubagentMessage"],[SSE_EVENTS.SUBAGENT_TOOL_RESULT,"_onSubagentToolResult"],[SSE_EVENTS.SUBAGENT_COMPLETED,"_onSubagentCompleted"],[SSE_EVENTS.IMAGE_DETECTED,"_onImageDetected"],[SSE_EVENTS.TUNNEL_STARTED,"_onTunnelStarted"],[SSE_EVENTS.TUNNEL_STOPPED,"_onTunnelStopped"],[SSE_EVENTS.TUNNEL_PROGRESS,"_onTunnelProgress"],[SSE_EVENTS.TUNNEL_ERROR,"_onTunnelError"],[SSE_EVENTS.TUNNEL_QR_ROTATED,"_onTunnelQrRotated"],[SSE_EVENTS.TUNNEL_QR_REGENERATED,"_onTunnelQrRegenerated"],[SSE_EVENTS.TUNNEL_QR_AUTH_USED,"_onTunnelQrAuthUsed"],[SSE_EVENTS.PLAN_SUBAGENT,"_onPlanSubagent"],[SSE_EVENTS.PLAN_PROGRESS,"_onPlanProgress"],[SSE_EVENTS.PLAN_STARTED,"_onPlanStarted"],[SSE_EVENTS.PLAN_CANCELLED,"_onPlanCancelled"],[SSE_EVENTS.PLAN_COMPLETED,"_onPlanCompleted"],[SSE_EVENTS.ORCHESTRATOR_STATE_CHANGED,"_onOrchestratorStateChanged"],[SSE_EVENTS.ORCHESTRATOR_PLAN_PROGRESS,"_onOrchestratorPlanProgress"],[SSE_EVENTS.ORCHESTRATOR_PLAN_READY,"_onOrchestratorPlanReady"],[SSE_EVENTS.ORCHESTRATOR_PHASE_STARTED,"_onOrchestratorPhaseStarted"],[SSE_EVENTS.ORCHESTRATOR_PHASE_COMPLETED,"_onOrchestratorPhaseCompleted"],[SSE_EVENTS.ORCHESTRATOR_PHASE_FAILED,"_onOrchestratorPhaseFailed"],[SSE_EVENTS.ORCHESTRATOR_VERIFICATION,"_onOrchestratorVerification"],[SSE_EVENTS.ORCHESTRATOR_TASK_ASSIGNED,"_onOrchestratorTaskAssigned"],[SSE_EVENTS.ORCHESTRATOR_TASK_COMPLETED,"_onOrchestratorTaskCompleted"],[SSE_EVENTS.ORCHESTRATOR_TASK_FAILED,"_onOrchestratorTaskFailed"],[SSE_EVENTS.ORCHESTRATOR_COMPLETED,"_onOrchestratorCompleted"],[SSE_EVENTS.ORCHESTRATOR_ERROR,"_onOrchestratorError"],[SSE_EVENTS.CLIPBOARD_WRITE,"_onClipboardWrite"]];function parseSessionPrefix(l){if(!l)return null;const e=l.match(/^(w\d+-[a-zA-Z0-9_-]+|s\d+-[a-zA-Z0-9_-]+)/);if(!e)return null;const t=e[1],s=l.slice(t.length);return s===""?{prefix:t,suffix:""}:s.startsWith(": ")?{prefix:t,suffix:s.slice(2)}:null}class CodemanApp{constructor(){this.sessions=new Map,this._shortIdCache=new Map,this.sessionOrder=[],this.draggedTabId=null,this.cases=[],this.currentRun=null,this.totalTokens=0,this.globalStats=null,this.eventSource=null,this._clientId=typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():"c-"+Math.random().toString(36).slice(2)+Date.now().toString(36),this.terminal=null,this.fitAddon=null,this.activeSessionId=null,this._initGeneration=0,this._initFallbackTimer=null,this._selectGeneration=0,this.respawnStatus={},this.respawnTimers={},this.respawnCountdownTimers={},this.respawnActionLogs={},this.timerCountdownInterval=null,this.terminalBuffers=new Map,this.editingSessionId=null,this.pendingCloseSessionId=null,this.muxSessions=[],this.ralphStates=new Map,this.subagents=new Map,this.subagentActivity=new Map,this.subagentToolResults=new Map,this.activeSubagentId=null,this.subagentPanelVisible=!1,this.subagentWindows=new Map,this.subagentWindowZIndex=ZINDEX_SUBAGENT_BASE,this.minimizedSubagents=new Map,this._subagentHideTimeout=null,this.subagentParentMap=new Map,this.teams=new Map,this.teamTasks=new Map,this.teammateMap=new Map,this.teammatePanesByName=new Map,this.teammateTerminals=new Map,this.terminalBufferCache=new Map,this.ralphStatePanelCollapsed=!0,this.ralphClosedSessions=new Set,this.planSubagents=new Map,this.planSubagentWindowZIndex=ZINDEX_PLAN_SUBAGENT_BASE,this.planGenerationStopped=!1,this.planAgentsMinimized=!1,this.wizardDragState=null,this.wizardDragListeners=null,this.wizardPosition=null,this.projectInsights=new Map,this.logViewerWindows=new Map,this.logViewerWindowZIndex=ZINDEX_LOG_VIEWER_BASE,this.projectInsightsPanelVisible=!1,this.orchestratorState=null,this.orchestratorPanelVisible=!1,this.currentSessionWorkingDir=null,this.imagePopups=new Map,this.imagePopupZIndex=ZINDEX_IMAGE_POPUP_BASE,this.fileBrowserData=null,this.fileBrowserExpandedDirs=new Set,this.fileBrowserFilter="",this.fileBrowserAllExpanded=!1,this.fileBrowserDragListeners=null,this.filePreviewContent="",this._toastContainer=null,this._tunnelUrl=null,this.tabAlerts=new Map,this.pendingHooks=new Map,this._ws=null,this._wsSessionId=null,this._wsReady=!1,this.pendingWrites=[],this.writeFrameScheduled=!1,this._wasAtBottomBeforeWrite=!0,this.syncWaitTimeout=null,this._isLoadingBuffer=!1,this._loadBufferQueue=null,this.flickerFilterBuffer="",this.flickerFilterActive=!1,this.flickerFilterTimeout=null,this._debounceTimers=Object.create(null),this.systemStatsInterval=null,this.sseReconnectTimeout=null,this._sseListenerCleanup=null,this.reconnectAttempts=0,this.maxReconnectAttempts=10,this.isOnline=navigator.onLine,this._inputQueue=new Map,this._inputQueueMaxBytes=64*1024,this._connectionStatus="connected",this._inputSendChain=Promise.resolve(),this._localEchoOverlay=null,this._localEchoEnabled=!1,this._restoringFlushedState=!1,this.activeFocusTrap=null,this.notificationManager=new NotificationManager(this),this.idleTimers=new Map,this._elemCache={},this.init()}$(e){return this._elemCache[e]||(this._elemCache[e]=document.getElementById(e)),this._elemCache[e]}_clearTimer(e){this[e]&&(clearTimeout(this[e]),this[e]=null)}_isStaleSelect(e){return e!==this._selectGeneration?(this._isLoadingBuffer&&this._finishBufferLoad(),this._restoringFlushedState=!1,!0):!1}formatTokens(e){if(e>=1e6){const t=e/1e6;return t>=10?`${t.toFixed(1)}m`:`${t.toFixed(2)}m`}else if(e>=1e3){const t=e/1e3;return t>=100?`${t.toFixed(0)}k`:`${t.toFixed(1)}k`}return String(e)}estimateCost(e,t){const s=e/1e6*15,i=t/1e6*75;return s+i}setPendingHook(e,t){this.pendingHooks.has(e)||this.pendingHooks.set(e,new Set),this.pendingHooks.get(e).add(t),this.updateTabAlertFromHooks(e)}clearPendingHooks(e,t=null){const s=this.pendingHooks.get(e);s&&(t?s.delete(t):s.clear(),s.size===0&&this.pendingHooks.delete(e),this.updateTabAlertFromHooks(e))}updateTabAlertFromHooks(e){const t=this.pendingHooks.get(e);!t||t.size===0?this.tabAlerts.delete(e):t.has("permission_prompt")||t.has("elicitation_dialog")?this.tabAlerts.set(e,"action"):t.has("idle_prompt")&&this.tabAlerts.set(e,"idle"),this.renderSessionTabs()}init(){MobileDetection.init(),KeyboardHandler.init(),SwipeHandler.init(),VoiceInput.init(),KeyboardAccessoryBar.init(),this.loadAppSettingsFromStorage().extendedKeyboardBar&&KeyboardAccessoryBar.setMode("extended"),this.applyHeaderVisibilitySettings(),this.applyTabWrapSettings(),this.applyMonitorVisibility(),document.documentElement.classList.remove("mobile-init"),requestAnimationFrame(()=>{this.initTerminal(),this.loadFontSize(),this.connectSSE(),this._initFallbackTimer=setTimeout(()=>{this._initGeneration===0&&this.loadState()},3e3)}),this.registerServiceWorker(),this.loadTunnelStatus();const t=fetch("/api/settings").then(s=>s.ok?s.json():null).catch(()=>null);if(this.loadQuickStartCases(null,t),this._initRunMode(),this.setupEventListeners(),MobileDetection.isTouchDevice()){const s=i=>{i&&i.addEventListener("touchstart",n=>{if(!KeyboardHandler.keyboardVisible)return;const o=n.target.closest("button");o&&(n.preventDefault(),o.click(),typeof app<"u"&&app.terminal&&app.terminal.focus())},{passive:!1})};s(document.querySelector(".toolbar")),s(document.querySelector(".welcome-overlay"))}this.setupOnlineDetection(),this.loadAppSettingsFromServer(t).then(()=>{this.applyHeaderVisibilitySettings(),this.applyTabWrapSettings(),this.applyMonitorVisibility()}),document.body.classList.add("app-loaded")}_initWebGL(){if(!(typeof WebglAddon>"u"))try{this._webglAddon=new WebglAddon.WebglAddon,this._webglAddon.onContextLoss(()=>{console.error("[CRASH-DIAG] WebGL context LOST \u2014 falling back to canvas renderer"),_crashDiag.log("WEBGL_LOST"),this._disableWebGLSticky("context-lost"),this._webglAddon?.dispose(),this._webglAddon=null}),this.terminal.loadAddon(this._webglAddon),console.log("[CRASH-DIAG] WebGL renderer enabled"),this._installWebGLLongTaskGuard()}catch{}}_installWebGLLongTaskGuard(){if(typeof PerformanceObserver>"u"||this._webglLongTaskObserver)return;const e=performance.now(),t=[];try{this._webglLongTaskObserver=new PerformanceObserver(s=>{if(!this._webglAddon)return;const i=performance.now();if(!(i-e<5e3)){for(const n of s.getEntries())n.duration>=200&&t.push(n.startTime);for(;t.length&&i-t[0]>3e4;)t.shift();if(t.length>=3){console.warn(`[CRASH-DIAG] WebGL long-task threshold (${t.length} stalls/30s) \u2014 falling back to canvas renderer`),_crashDiag.log(`WEBGL_FALLBACK: ${t.length}`),this._disableWebGLSticky("long-tasks"),this._webglAddon?.dispose(),this._webglAddon=null;try{this._webglLongTaskObserver.disconnect()}catch{}this._webglLongTaskObserver=null;try{this.terminal.refresh(0,this.terminal.rows-1)}catch{}}}}),this._webglLongTaskObserver.observe({type:"longtask",buffered:!1})}catch{}}_disableWebGLSticky(e){try{localStorage.setItem("codeman-webgl-disabled",JSON.stringify({reason:e,at:Date.now()}))}catch{}}setupEventListeners(){const e=[{key:"?",altKey:"/",ctrl:!0,action:()=>this.showHelp()},{key:"w",ctrl:!0,action:()=>this.killActiveSession()},{key:"Tab",ctrl:!0,action:()=>this.nextSession()},{key:"l",ctrl:!0,action:()=>this.clearTerminal()},{key:"R",ctrl:!0,shift:!0,action:()=>this.restoreTerminalSize()},{key:"=",altKey:"+",ctrl:!0,action:()=>this.increaseFontSize()},{key:"-",ctrl:!0,action:()=>this.decreaseFontSize()},{key:"V",ctrl:!0,shift:!0,action:()=>VoiceInput.toggle()},{key:"{",ctrl:!0,shift:!0,action:()=>this.moveActiveTabLeft()},{key:"}",ctrl:!0,shift:!0,action:()=>this.moveActiveTabRight()}];document.addEventListener("keydown",s=>{if(!(s.isComposing||s.keyCode===229)){if(s.key==="Escape"&&(this.closeAllPanels(),this.closeHelp()),s.altKey&&!s.ctrlKey&&!s.shiftKey&&s.key>="1"&&s.key<="9"){const i=parseInt(s.key)-1;i<this.sessionOrder.length&&(s.preventDefault(),this.selectSession(this.sessionOrder[i]));return}for(const i of e){const n=s.key===i.key||i.altKey&&s.key===i.altKey,o=i.ctrl?s.ctrlKey||s.metaKey:!0,r=i.shift?s.shiftKey:!s.shiftKey;if(n&&o&&r){s.preventDefault(),i.action();return}}}},!0);const t=this.$("headerTokens");t&&!t._statsHandlerAttached&&(t.classList.add("clickable"),t._statsHandlerAttached=!0,t.addEventListener("click",()=>this.openTokenStats())),this.setupColorPicker()}_updateSseSubscription(e){try{const t=JSON.stringify({clientId:this._clientId,sessions:e?[e]:null});fetch("/api/events/subscribe",{method:"POST",headers:{"Content-Type":"application/json"},body:t,keepalive:!0}).catch(()=>{})}catch{}}connectSSE(){if(!navigator.onLine){this.setConnectionStatus("offline");return}this._clearTimer("sseReconnectTimeout"),this._sseListenerCleanup&&(this._sseListenerCleanup(),this._sseListenerCleanup=null),this.eventSource&&(this.eventSource.close(),this.eventSource=null),this.reconnectAttempts===0?this.setConnectionStatus("connecting"):this.setConnectionStatus("reconnecting");const e=new URLSearchParams({clientId:this._clientId});this.activeSessionId&&e.set("sessions",this.activeSessionId),this.eventSource=new EventSource(`/api/events?${e.toString()}`);const t=[],s=(i,n)=>{this.eventSource.addEventListener(i,n),t.push({event:i,handler:n})};if(this._sseListenerCleanup=()=>{for(const{event:i,handler:n}of t)this.eventSource&&this.eventSource.removeEventListener(i,n);t.length=0},this.eventSource.onopen=()=>{this.reconnectAttempts=0,this.setConnectionStatus("connected")},this.eventSource.onerror=()=>{this.reconnectAttempts++,this.reconnectAttempts>=this.maxReconnectAttempts?this.setConnectionStatus("disconnected"):this.setConnectionStatus("reconnecting"),this.eventSource&&(this.eventSource.close(),this.eventSource=null),this._clearTimer("sseReconnectTimeout");const i=this.reconnectAttempts<=1?200:Math.min(500*Math.pow(2,this.reconnectAttempts-2),3e4);this.sseReconnectTimeout=setTimeout(()=>this.connectSSE(),i)},!this._sseHandlerWrappers){this._sseHandlerWrappers=new Map;for(const[i,n]of _SSE_HANDLER_MAP){const o=this[n];this._sseHandlerWrappers.set(i,r=>{try{o.call(this,r.data?JSON.parse(r.data):{})}catch(a){console.error(`[SSE] Error handling ${i}:`,a)}})}}for(const[i]of _SSE_HANDLER_MAP)s(i,this._sseHandlerWrappers.get(i))}_onInit(e){_crashDiag.log(`INIT: ${e.sessions?.length||0} sessions`),this.handleInit(e)}_onSessionCreated(e){this.sessions.set(e.id,e),this.sessionOrder.includes(e.id)||(this.sessionOrder.push(e.id),this.saveSessionOrder()),this.renderSessionTabs(),this.updateCost(),this.sessions.size===1&&this.startSystemStatsPolling()}_onSessionUpdated(e){const t=e.session||e,s=this.sessions.get(t.id),i=t.claudeSessionId&&(!s||!s.claudeSessionId);this.sessions.set(t.id,t),this.renderSessionTabs(),this.updateCost(),t.id===this.activeSessionId&&t.tokens&&this.updateRespawnTokens(t.tokens),this.updateSubagentParentNames(t.id),i&&(this.recheckOrphanSubagents(),requestAnimationFrame(()=>{this.updateConnectionLines()}))}_onSessionDeleted(e){if(this._wsSessionId===e.id&&this._disconnectWs(),this._cleanupSessionData(e.id),this.activeSessionId===e.id){this.activeSessionId=null;try{localStorage.removeItem("codeman-active-session")}catch{}this.terminal.clear(),this.showWelcome()}this.renderSessionTabs(),this.renderRalphStatePanel(),this.renderProjectInsightsPanel(),this.sessions.size===0&&this.stopSystemStatsPolling()}_onSSETerminal(e){this._wsReady&&this._wsSessionId===e.id||this._onSessionTerminal(e)}_onSSENeedsRefresh(e){this._wsReady&&this._wsSessionId===e?.id||this._onSessionNeedsRefresh(e)}_onSSEClearTerminal(e){this._wsReady&&this._wsSessionId===e?.id||this._onSessionClearTerminal(e)}_onSessionTerminal(e){if(e.id===this.activeSessionId){if(e.data.length>32768&&_crashDiag.log(`TERMINAL: ${(e.data.length/1024).toFixed(0)}KB`),(this.pendingWrites?.reduce((s,i)=>s+i.length,0)||0)+(this.flickerFilterBuffer?.length||0)>131072){this._clientDropRecoveryTimer||(this._clientDropRecoveryTimer=setTimeout(()=>{this._clientDropRecoveryTimer=null,this._onSessionNeedsRefresh()},2e3));return}this.batchTerminalWrite(e.data)}}_sanitizeHtml(e){const t=document.createElement("template");t.innerHTML=e;const s=t.content;for(const n of s.querySelectorAll("script, iframe, object, embed, form, base, meta, link, style"))n.remove();for(const n of s.querySelectorAll("*"))for(const o of[...n.attributes]){const r=o.name.toLowerCase();if(r.startsWith("on"))n.removeAttribute(o.name);else if(["href","src","action","xlink:href","formaction"].includes(r)){const a=o.value.replace(/\s/g,"").toLowerCase();(a.startsWith("javascript:")||a.startsWith("vbscript:")||a.startsWith("data:text/html"))&&n.removeAttribute(o.name)}}const i=document.createElement("div");return i.appendChild(s),i.innerHTML}_cleanTerminalBuffer(e){const t=e.replace(/\x1b\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]/g,"").replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g,"").replace(/\x1b[PX^_][^\x1b]*\x1b\\/g,"").replace(/\x1b[NO()][A-Z0-9]?/g,"").replace(/\x1b[>=<78cDEHM]/g,"").replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g,"").replace(/\r\n/g,`
|
|
7
|
+
Stack:`,l.reason?.stack)}),typeof PerformanceObserver<"u")try{new PerformanceObserver(e=>{for(const t of e.getEntries())t.duration>200&&(_crashDiag.log(`LONG_TASK: ${t.duration.toFixed(0)}ms`),console.warn(`[CRASH-DIAG] Long task: ${t.duration.toFixed(0)}ms (type: ${t.entryType}, name: ${t.name})`))}).observe({type:"longtask",buffered:!0})}catch{}const _origGetContext=HTMLCanvasElement.prototype.getContext;HTMLCanvasElement.prototype.getContext=function(l,...e){const t=_origGetContext.call(this,l,...e);return(l==="webgl2"||l==="webgl")&&(this.addEventListener("webglcontextlost",s=>{_crashDiag.log(`WEBGL_LOST: ${this.width}x${this.height}`),console.error("[CRASH-DIAG] WebGL context LOST on canvas",this.width,"x",this.height,"\u2014 prevented:",s.defaultPrevented)}),this.addEventListener("webglcontextrestored",()=>{_crashDiag.log("WEBGL_RESTORED"),console.warn("[CRASH-DIAG] WebGL context restored")})),t};const _SSE_HANDLER_MAP=[[SSE_EVENTS.INIT,"_onInit"],[SSE_EVENTS.SESSION_CREATED,"_onSessionCreated"],[SSE_EVENTS.SESSION_UPDATED,"_onSessionUpdated"],[SSE_EVENTS.SESSION_DELETED,"_onSessionDeleted"],[SSE_EVENTS.SESSION_TERMINAL,"_onSSETerminal"],[SSE_EVENTS.SESSION_NEEDS_REFRESH,"_onSSENeedsRefresh"],[SSE_EVENTS.SESSION_CLEAR_TERMINAL,"_onSSEClearTerminal"],[SSE_EVENTS.SESSION_COMPLETION,"_onSessionCompletion"],[SSE_EVENTS.SESSION_ERROR,"_onSessionError"],[SSE_EVENTS.SESSION_EXIT,"_onSessionExit"],[SSE_EVENTS.SESSION_IDLE,"_onSessionIdle"],[SSE_EVENTS.SESSION_WORKING,"_onSessionWorking"],[SSE_EVENTS.SESSION_AUTO_CLEAR,"_onSessionAutoClear"],[SSE_EVENTS.SESSION_CLI_INFO,"_onSessionCliInfo"],[SSE_EVENTS.SCHEDULED_CREATED,"_onScheduledCreated"],[SSE_EVENTS.SCHEDULED_UPDATED,"_onScheduledUpdated"],[SSE_EVENTS.SCHEDULED_COMPLETED,"_onScheduledCompleted"],[SSE_EVENTS.SCHEDULED_STOPPED,"_onScheduledStopped"],[SSE_EVENTS.RESPAWN_STARTED,"_onRespawnStarted"],[SSE_EVENTS.RESPAWN_STOPPED,"_onRespawnStopped"],[SSE_EVENTS.RESPAWN_STATE_CHANGED,"_onRespawnStateChanged"],[SSE_EVENTS.RESPAWN_CYCLE_STARTED,"_onRespawnCycleStarted"],[SSE_EVENTS.RESPAWN_BLOCKED,"_onRespawnBlocked"],[SSE_EVENTS.RESPAWN_AUTO_ACCEPT_SENT,"_onRespawnAutoAcceptSent"],[SSE_EVENTS.RESPAWN_DETECTION_UPDATE,"_onRespawnDetectionUpdate"],[SSE_EVENTS.RESPAWN_TIMER_STARTED,"_onRespawnTimerStarted"],[SSE_EVENTS.RESPAWN_TIMER_CANCELLED,"_onRespawnTimerCancelled"],[SSE_EVENTS.RESPAWN_TIMER_COMPLETED,"_onRespawnTimerCompleted"],[SSE_EVENTS.RESPAWN_ERROR,"_onRespawnError"],[SSE_EVENTS.RESPAWN_ACTION_LOG,"_onRespawnActionLog"],[SSE_EVENTS.TASK_CREATED,"_onTaskCreated"],[SSE_EVENTS.TASK_COMPLETED,"_onTaskCompleted"],[SSE_EVENTS.TASK_FAILED,"_onTaskFailed"],[SSE_EVENTS.TASK_UPDATED,"_onTaskUpdated"],[SSE_EVENTS.MUX_CREATED,"_onMuxCreated"],[SSE_EVENTS.MUX_KILLED,"_onMuxKilled"],[SSE_EVENTS.MUX_DIED,"_onMuxDied"],[SSE_EVENTS.MUX_STATS_UPDATED,"_onMuxStatsUpdated"],[SSE_EVENTS.SESSION_RALPH_LOOP_UPDATE,"_onRalphLoopUpdate"],[SSE_EVENTS.SESSION_RALPH_TODO_UPDATE,"_onRalphTodoUpdate"],[SSE_EVENTS.SESSION_RALPH_COMPLETION_DETECTED,"_onRalphCompletionDetected"],[SSE_EVENTS.SESSION_RALPH_STATUS_UPDATE,"_onRalphStatusUpdate"],[SSE_EVENTS.SESSION_CIRCUIT_BREAKER_UPDATE,"_onCircuitBreakerUpdate"],[SSE_EVENTS.SESSION_EXIT_GATE_MET,"_onExitGateMet"],[SSE_EVENTS.SESSION_BASH_TOOL_START,"_onBashToolStart"],[SSE_EVENTS.SESSION_BASH_TOOL_END,"_onBashToolEnd"],[SSE_EVENTS.SESSION_BASH_TOOLS_UPDATE,"_onBashToolsUpdate"],[SSE_EVENTS.HOOK_IDLE_PROMPT,"_onHookIdlePrompt"],[SSE_EVENTS.HOOK_PERMISSION_PROMPT,"_onHookPermissionPrompt"],[SSE_EVENTS.HOOK_ELICITATION_DIALOG,"_onHookElicitationDialog"],[SSE_EVENTS.HOOK_STOP,"_onHookStop"],[SSE_EVENTS.HOOK_TEAMMATE_IDLE,"_onHookTeammateIdle"],[SSE_EVENTS.HOOK_TASK_COMPLETED,"_onHookTaskCompleted"],[SSE_EVENTS.SUBAGENT_DISCOVERED,"_onSubagentDiscovered"],[SSE_EVENTS.SUBAGENT_UPDATED,"_onSubagentUpdated"],[SSE_EVENTS.SUBAGENT_TOOL_CALL,"_onSubagentToolCall"],[SSE_EVENTS.SUBAGENT_PROGRESS,"_onSubagentProgress"],[SSE_EVENTS.SUBAGENT_MESSAGE,"_onSubagentMessage"],[SSE_EVENTS.SUBAGENT_TOOL_RESULT,"_onSubagentToolResult"],[SSE_EVENTS.SUBAGENT_COMPLETED,"_onSubagentCompleted"],[SSE_EVENTS.IMAGE_DETECTED,"_onImageDetected"],[SSE_EVENTS.TUNNEL_STARTED,"_onTunnelStarted"],[SSE_EVENTS.TUNNEL_STOPPED,"_onTunnelStopped"],[SSE_EVENTS.TUNNEL_PROGRESS,"_onTunnelProgress"],[SSE_EVENTS.TUNNEL_ERROR,"_onTunnelError"],[SSE_EVENTS.TUNNEL_QR_ROTATED,"_onTunnelQrRotated"],[SSE_EVENTS.TUNNEL_QR_REGENERATED,"_onTunnelQrRegenerated"],[SSE_EVENTS.TUNNEL_QR_AUTH_USED,"_onTunnelQrAuthUsed"],[SSE_EVENTS.PLAN_SUBAGENT,"_onPlanSubagent"],[SSE_EVENTS.PLAN_PROGRESS,"_onPlanProgress"],[SSE_EVENTS.PLAN_STARTED,"_onPlanStarted"],[SSE_EVENTS.PLAN_CANCELLED,"_onPlanCancelled"],[SSE_EVENTS.PLAN_COMPLETED,"_onPlanCompleted"],[SSE_EVENTS.ORCHESTRATOR_STATE_CHANGED,"_onOrchestratorStateChanged"],[SSE_EVENTS.ORCHESTRATOR_PLAN_PROGRESS,"_onOrchestratorPlanProgress"],[SSE_EVENTS.ORCHESTRATOR_PLAN_READY,"_onOrchestratorPlanReady"],[SSE_EVENTS.ORCHESTRATOR_PHASE_STARTED,"_onOrchestratorPhaseStarted"],[SSE_EVENTS.ORCHESTRATOR_PHASE_COMPLETED,"_onOrchestratorPhaseCompleted"],[SSE_EVENTS.ORCHESTRATOR_PHASE_FAILED,"_onOrchestratorPhaseFailed"],[SSE_EVENTS.ORCHESTRATOR_VERIFICATION,"_onOrchestratorVerification"],[SSE_EVENTS.ORCHESTRATOR_TASK_ASSIGNED,"_onOrchestratorTaskAssigned"],[SSE_EVENTS.ORCHESTRATOR_TASK_COMPLETED,"_onOrchestratorTaskCompleted"],[SSE_EVENTS.ORCHESTRATOR_TASK_FAILED,"_onOrchestratorTaskFailed"],[SSE_EVENTS.ORCHESTRATOR_COMPLETED,"_onOrchestratorCompleted"],[SSE_EVENTS.ORCHESTRATOR_ERROR,"_onOrchestratorError"],[SSE_EVENTS.CLIPBOARD_WRITE,"_onClipboardWrite"]];function parseSessionPrefix(l){if(!l)return null;const e=l.match(/^(w\d+-[a-zA-Z0-9_-]+|s\d+-[a-zA-Z0-9_-]+)/);if(!e)return null;const t=e[1],s=l.slice(t.length);return s===""?{prefix:t,suffix:""}:s.startsWith(": ")?{prefix:t,suffix:s.slice(2)}:null}class CodemanApp{constructor(){this.sessions=new Map,this._shortIdCache=new Map,this.sessionOrder=[],this.draggedTabId=null,this.cases=[],this.currentRun=null,this.totalTokens=0,this.globalStats=null,this.eventSource=null,this._clientId=typeof crypto<"u"&&crypto.randomUUID?crypto.randomUUID():"c-"+Math.random().toString(36).slice(2)+Date.now().toString(36),this.terminal=null,this.fitAddon=null,this.activeSessionId=null,this._initGeneration=0,this._initFallbackTimer=null,this._selectGeneration=0,this.respawnStatus={},this.respawnTimers={},this.respawnCountdownTimers={},this.respawnActionLogs={},this.timerCountdownInterval=null,this.terminalBuffers=new Map,this.editingSessionId=null,this.pendingCloseSessionId=null,this.muxSessions=[],this.ralphStates=new Map,this.subagents=new Map,this.subagentActivity=new Map,this.subagentToolResults=new Map,this.activeSubagentId=null,this.subagentPanelVisible=!1,this.subagentWindows=new Map,this.subagentWindowZIndex=ZINDEX_SUBAGENT_BASE,this.minimizedSubagents=new Map,this._subagentHideTimeout=null,this.subagentParentMap=new Map,this.teams=new Map,this.teamTasks=new Map,this.teammateMap=new Map,this.teammatePanesByName=new Map,this.teammateTerminals=new Map,this.terminalBufferCache=new Map,this.ralphStatePanelCollapsed=!0,this.ralphClosedSessions=new Set,this.planSubagents=new Map,this.planSubagentWindowZIndex=ZINDEX_PLAN_SUBAGENT_BASE,this.planGenerationStopped=!1,this.planAgentsMinimized=!1,this.wizardDragState=null,this.wizardDragListeners=null,this.wizardPosition=null,this.projectInsights=new Map,this.logViewerWindows=new Map,this.logViewerWindowZIndex=ZINDEX_LOG_VIEWER_BASE,this.projectInsightsPanelVisible=!1,this.orchestratorState=null,this.orchestratorPanelVisible=!1,this.currentSessionWorkingDir=null,this.imagePopups=new Map,this.imagePopupZIndex=ZINDEX_IMAGE_POPUP_BASE,this.fileBrowserData=null,this.fileBrowserExpandedDirs=new Set,this.fileBrowserFilter="",this.fileBrowserAllExpanded=!1,this.fileBrowserDragListeners=null,this.filePreviewContent="",this._toastContainer=null,this._tunnelUrl=null,this.tabAlerts=new Map,this.pendingHooks=new Map,this._ws=null,this._wsSessionId=null,this._wsReady=!1,this.pendingWrites=[],this.writeFrameScheduled=!1,this._wasAtBottomBeforeWrite=!0,this.syncWaitTimeout=null,this._isLoadingBuffer=!1,this._loadBufferQueue=null,this.flickerFilterBuffer="",this.flickerFilterActive=!1,this.flickerFilterTimeout=null,this._debounceTimers=Object.create(null),this.systemStatsInterval=null,this.sseReconnectTimeout=null,this._sseListenerCleanup=null,this.reconnectAttempts=0,this.maxReconnectAttempts=10,this.isOnline=navigator.onLine,this._inputQueue=new Map,this._inputQueueMaxBytes=64*1024,this._connectionStatus="connected",this._inputSendChain=Promise.resolve(),this._localEchoOverlay=null,this._localEchoEnabled=!1,this._restoringFlushedState=!1,this.activeFocusTrap=null,this.notificationManager=new NotificationManager(this),this.idleTimers=new Map,this._elemCache={},this.init()}$(e){return this._elemCache[e]||(this._elemCache[e]=document.getElementById(e)),this._elemCache[e]}_clearTimer(e){this[e]&&(clearTimeout(this[e]),this[e]=null)}_isStaleSelect(e){return e!==this._selectGeneration?(this._isLoadingBuffer&&this._finishBufferLoad(),this._restoringFlushedState=!1,!0):!1}formatTokens(e){if(e>=1e6){const t=e/1e6;return t>=10?`${t.toFixed(1)}m`:`${t.toFixed(2)}m`}else if(e>=1e3){const t=e/1e3;return t>=100?`${t.toFixed(0)}k`:`${t.toFixed(1)}k`}return String(e)}estimateCost(e,t){const s=e/1e6*15,i=t/1e6*75;return s+i}setPendingHook(e,t){this.pendingHooks.has(e)||this.pendingHooks.set(e,new Set),this.pendingHooks.get(e).add(t),this.updateTabAlertFromHooks(e)}clearPendingHooks(e,t=null){const s=this.pendingHooks.get(e);s&&(t?s.delete(t):s.clear(),s.size===0&&this.pendingHooks.delete(e),this.updateTabAlertFromHooks(e))}updateTabAlertFromHooks(e){const t=this.pendingHooks.get(e);!t||t.size===0?this.tabAlerts.delete(e):t.has("permission_prompt")||t.has("elicitation_dialog")?this.tabAlerts.set(e,"action"):t.has("idle_prompt")&&this.tabAlerts.set(e,"idle"),this.renderSessionTabs()}init(){MobileDetection.init(),KeyboardHandler.init(),SwipeHandler.init(),VoiceInput.init(),KeyboardAccessoryBar.init(),this.loadAppSettingsFromStorage().extendedKeyboardBar&&KeyboardAccessoryBar.setMode("extended"),this.applyHeaderVisibilitySettings(),this.applyTabWrapSettings(),this.applyMonitorVisibility(),document.documentElement.classList.remove("mobile-init"),requestAnimationFrame(()=>{this.initTerminal(),this.loadFontSize(),this.connectSSE(),this._initFallbackTimer=setTimeout(()=>{this._initGeneration===0&&this.loadState()},3e3)}),this.registerServiceWorker(),this.loadTunnelStatus();const t=fetch("/api/settings").then(s=>s.ok?s.json():null).catch(()=>null);if(this.loadQuickStartCases(null,t),this._initRunMode(),this.setupEventListeners(),MobileDetection.isTouchDevice()){const s=i=>{i&&i.addEventListener("touchstart",n=>{if(!KeyboardHandler.keyboardVisible)return;const o=n.target.closest("button");o&&(n.preventDefault(),o.click(),typeof app<"u"&&app.terminal&&app.terminal.focus())},{passive:!1})};s(document.querySelector(".toolbar")),s(document.querySelector(".welcome-overlay"))}this.setupOnlineDetection(),this.loadAppSettingsFromServer(t).then(()=>{this.applyHeaderVisibilitySettings(),this.applyTabWrapSettings(),this.applyMonitorVisibility()}),document.body.classList.add("app-loaded")}_initWebGL(){if(!(typeof WebglAddon>"u"))try{this._webglAddon=new WebglAddon.WebglAddon,this._webglAddon.onContextLoss(()=>{console.error("[CRASH-DIAG] WebGL context LOST \u2014 falling back to canvas renderer"),_crashDiag.log("WEBGL_LOST"),this._disableWebGLSticky("context-lost"),this._disposeWebGLObserver(),this._webglAddon?.dispose(),this._webglAddon=null}),this.terminal.loadAddon(this._webglAddon),console.log("[CRASH-DIAG] WebGL renderer enabled"),this._installWebGLLongTaskGuard()}catch{}}_installWebGLLongTaskGuard(){if(typeof PerformanceObserver>"u"||this._webglLongTaskObserver)return;const e=performance.now(),t=[];try{this._webglLongTaskObserver=new PerformanceObserver(s=>{if(!this._webglAddon)return;const i=performance.now();if(!(i-e<WEBGL_FALLBACK.GRACE_MS)&&evaluateWebGLLongTaskTrip(t,s.getEntries(),i)){console.warn(`[CRASH-DIAG] WebGL long-task threshold (${t.length} stalls/${WEBGL_FALLBACK.WINDOW_MS}ms) \u2014 falling back to canvas renderer`),_crashDiag.log(`WEBGL_FALLBACK: ${t.length}`),this._disableWebGLSticky("long-tasks"),this._disposeWebGLObserver(),this._webglAddon?.dispose(),this._webglAddon=null;try{this.terminal.refresh(0,this.terminal.rows-1)}catch{}}}),this._webglLongTaskObserver.observe({type:"longtask",buffered:!1})}catch{}}_disposeWebGLObserver(){if(this._webglLongTaskObserver){try{this._webglLongTaskObserver.disconnect()}catch{}this._webglLongTaskObserver=null}}_disableWebGLSticky(e){try{localStorage.setItem("codeman-webgl-disabled",JSON.stringify({reason:e,at:Date.now()}))}catch{}}setupEventListeners(){const e=[{key:"?",altKey:"/",ctrl:!0,action:()=>this.showHelp()},{key:"w",ctrl:!0,action:()=>this.killActiveSession()},{key:"Tab",ctrl:!0,action:()=>this.nextSession()},{key:"l",ctrl:!0,action:()=>this.clearTerminal()},{key:"R",ctrl:!0,shift:!0,action:()=>this.restoreTerminalSize()},{key:"=",altKey:"+",ctrl:!0,action:()=>this.increaseFontSize()},{key:"-",ctrl:!0,action:()=>this.decreaseFontSize()},{key:"V",ctrl:!0,shift:!0,action:()=>VoiceInput.toggle()},{key:"{",ctrl:!0,shift:!0,action:()=>this.moveActiveTabLeft()},{key:"}",ctrl:!0,shift:!0,action:()=>this.moveActiveTabRight()}];document.addEventListener("keydown",s=>{if(!(s.isComposing||s.keyCode===229)){if(s.key==="Escape"&&(this.closeAllPanels(),this.closeHelp()),s.altKey&&!s.ctrlKey&&!s.shiftKey&&s.key>="1"&&s.key<="9"){const i=parseInt(s.key)-1;i<this.sessionOrder.length&&(s.preventDefault(),this.selectSession(this.sessionOrder[i]));return}for(const i of e){const n=s.key===i.key||i.altKey&&s.key===i.altKey,o=i.ctrl?s.ctrlKey||s.metaKey:!0,r=i.shift?s.shiftKey:!s.shiftKey;if(n&&o&&r){s.preventDefault(),i.action();return}}}},!0);const t=this.$("headerTokens");t&&!t._statsHandlerAttached&&(t.classList.add("clickable"),t._statsHandlerAttached=!0,t.addEventListener("click",()=>this.openTokenStats())),this.setupColorPicker()}_updateSseSubscription(e){try{const t=JSON.stringify({clientId:this._clientId,sessions:e?[e]:null});fetch("/api/events/subscribe",{method:"POST",headers:{"Content-Type":"application/json"},body:t,keepalive:!0}).catch(()=>{})}catch{}}connectSSE(){if(!navigator.onLine){this.setConnectionStatus("offline");return}this._clearTimer("sseReconnectTimeout"),this._sseListenerCleanup&&(this._sseListenerCleanup(),this._sseListenerCleanup=null),this.eventSource&&(this.eventSource.close(),this.eventSource=null),this.reconnectAttempts===0?this.setConnectionStatus("connecting"):this.setConnectionStatus("reconnecting");const e=new URLSearchParams({clientId:this._clientId});this.activeSessionId&&e.set("sessions",this.activeSessionId),this.eventSource=new EventSource(`/api/events?${e.toString()}`);const t=[],s=(i,n)=>{this.eventSource.addEventListener(i,n),t.push({event:i,handler:n})};if(this._sseListenerCleanup=()=>{for(const{event:i,handler:n}of t)this.eventSource&&this.eventSource.removeEventListener(i,n);t.length=0},this.eventSource.onopen=()=>{this.reconnectAttempts=0,this.setConnectionStatus("connected")},this.eventSource.onerror=()=>{this.reconnectAttempts++,this.reconnectAttempts>=this.maxReconnectAttempts?this.setConnectionStatus("disconnected"):this.setConnectionStatus("reconnecting"),this.eventSource&&(this.eventSource.close(),this.eventSource=null),this._clearTimer("sseReconnectTimeout");const i=this.reconnectAttempts<=1?200:Math.min(500*Math.pow(2,this.reconnectAttempts-2),3e4);this.sseReconnectTimeout=setTimeout(()=>this.connectSSE(),i)},!this._sseHandlerWrappers){this._sseHandlerWrappers=new Map;for(const[i,n]of _SSE_HANDLER_MAP){const o=this[n];this._sseHandlerWrappers.set(i,r=>{try{o.call(this,r.data?JSON.parse(r.data):{})}catch(a){console.error(`[SSE] Error handling ${i}:`,a)}})}}for(const[i]of _SSE_HANDLER_MAP)s(i,this._sseHandlerWrappers.get(i))}_onInit(e){_crashDiag.log(`INIT: ${e.sessions?.length||0} sessions`),this.handleInit(e)}_onSessionCreated(e){this.sessions.set(e.id,e),this.sessionOrder.includes(e.id)||(this.sessionOrder.push(e.id),this.saveSessionOrder()),this.renderSessionTabs(),this.updateCost(),this.sessions.size===1&&this.startSystemStatsPolling()}_onSessionUpdated(e){const t=e.session||e,s=this.sessions.get(t.id),i=t.claudeSessionId&&(!s||!s.claudeSessionId);this.sessions.set(t.id,t),this.renderSessionTabs(),this.updateCost(),t.id===this.activeSessionId&&t.tokens&&this.updateRespawnTokens(t.tokens),this.updateSubagentParentNames(t.id),i&&(this.recheckOrphanSubagents(),requestAnimationFrame(()=>{this.updateConnectionLines()}))}_onSessionDeleted(e){if(this._wsSessionId===e.id&&this._disconnectWs(),this._cleanupSessionData(e.id),this.activeSessionId===e.id){this.activeSessionId=null;try{localStorage.removeItem("codeman-active-session")}catch{}this.terminal.clear(),this.showWelcome()}this.renderSessionTabs(),this.renderRalphStatePanel(),this.renderProjectInsightsPanel(),this.sessions.size===0&&this.stopSystemStatsPolling()}_onSSETerminal(e){this._wsReady&&this._wsSessionId===e.id||this._onSessionTerminal(e)}_onSSENeedsRefresh(e){this._wsReady&&this._wsSessionId===e?.id||this._onSessionNeedsRefresh(e)}_onSSEClearTerminal(e){this._wsReady&&this._wsSessionId===e?.id||this._onSessionClearTerminal(e)}_onSessionTerminal(e){if(e.id===this.activeSessionId){if(e.data.length>32768&&_crashDiag.log(`TERMINAL: ${(e.data.length/1024).toFixed(0)}KB`),(this.pendingWrites?.reduce((s,i)=>s+i.length,0)||0)+(this.flickerFilterBuffer?.length||0)>131072){this._clientDropRecoveryTimer||(this._clientDropRecoveryTimer=setTimeout(()=>{this._clientDropRecoveryTimer=null,this._onSessionNeedsRefresh()},2e3));return}this.batchTerminalWrite(e.data)}}_sanitizeHtml(e){const t=document.createElement("template");t.innerHTML=e;const s=t.content;for(const n of s.querySelectorAll("script, iframe, object, embed, form, base, meta, link, style"))n.remove();for(const n of s.querySelectorAll("*"))for(const o of[...n.attributes]){const r=o.name.toLowerCase();if(r.startsWith("on"))n.removeAttribute(o.name);else if(["href","src","action","xlink:href","formaction"].includes(r)){const a=o.value.replace(/\s/g,"").toLowerCase();(a.startsWith("javascript:")||a.startsWith("vbscript:")||a.startsWith("data:text/html"))&&n.removeAttribute(o.name)}}const i=document.createElement("div");return i.appendChild(s),i.innerHTML}_cleanTerminalBuffer(e){const t=e.replace(/\x1b\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]/g,"").replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g,"").replace(/\x1b[PX^_][^\x1b]*\x1b\\/g,"").replace(/\x1b[NO()][A-Z0-9]?/g,"").replace(/\x1b[>=<78cDEHM]/g,"").replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g,"").replace(/\r\n/g,`
|
|
8
8
|
`).replace(/\r/g,`
|
|
9
9
|
`),s=[/^\s*❯\s*/,/^\s*[⏵⏺⏸⏹]+\s*/,/^\s*✻\s*(Crunching|Crunched|Thinking)/i,/bypass permissions/i,/\bshift\+tab to cycle\b/i,/^\s*focus\s*$/,/^\s*new task\?/i,/\/clear to save/i,/^\s*─{5,}\s*$/,/\[(Opus|Sonnet|Haiku|GPT|Claude)[\s\S]*(tokens?|\$|¥|%|↑|↓)/i,/^\s*\[\d+[km]?\/\d+[km]?\]/i,/[█░▓▒]{3,}/,/^\s*\(.*\s*(tokens?|context).*\)\s*$/i];return t.split(`
|
|
10
10
|
`).filter(o=>o.trim()?!s.some(a=>a.test(o)):!0).join(`
|
|
11
11
|
`).replace(/[ \t]+$/gm,"").replace(/\n{4,}/g,`
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
`).trim()}_preprocessAsciiArt(e){const t=/[─-╿▀-▟]/,s=/```[\s\S]*?```/g,i=[];return e.replace(s,r=>(i.push(r),`\0FENCE${i.length-1}\0`)).split(/(\n{2,})/).map(r=>/^\n{2,}$/.test(r)||!r.trim()||r.includes("\0FENCE")?r:t.test(r)?"\n```\n"+r+"\n```\n":r).join("").replace(/FENCE(\d+)/g,(r,a)=>i[Number(a)])}_renderMarkdown(e){if(typeof marked<"u"&&marked.parse)try{const s=this._preprocessAsciiArt(e);let i=this._sanitizeHtml(marked.parse(s,{breaks:!0,gfm:!0}));i=i.replace(/<table>/g,'<div class="rv-table-wrap"><table>').replace(/<\/table>/g,"</table></div>");const n=/[─-╿▀-▟]/,o=document.createElement("template");return o.innerHTML=i,o.content.querySelectorAll("pre > code").forEach(r=>{if(!n.test(r.textContent||""))return;const a=r.parentElement;a.classList.add("rv-diagram");const h=document.createElement("div");h.className="rv-diagram-wrap";const c=document.createElement("button");c.className="rv-wrap-toggle",c.type="button",c.setAttribute("aria-label","Toggle line wrapping"),c.setAttribute("title","Toggle line wrapping"),a.parentNode.insertBefore(h,a),h.appendChild(c),h.appendChild(a)}),o.innerHTML}catch{}return`<pre style="white-space:pre-wrap;word-break:break-word">${e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}</pre>`}_bindResponseViewerInteractions(e){!e||e.dataset.rvBound==="1"||(e.dataset.rvBound="1",e.addEventListener("click",t=>{const s=t.target.closest(".rv-wrap-toggle");if(!s)return;t.preventDefault(),t.stopPropagation();const i=s.closest(".rv-diagram-wrap"),n=i?.querySelector("pre.rv-diagram");if(!n||!i)return;const o=n.classList.toggle("rv-nowrap");i.classList.toggle("rv-wrap-nowrap",o)}))}async toggleResponseViewer(){const e=document.getElementById("responseViewer"),t=document.getElementById("responseViewerBackdrop");if(!e)return;if(e.classList.contains("visible")){e.classList.remove("visible"),t.classList.remove("visible");return}if(this.activeSessionId)try{let o=(await(await fetch(`/api/sessions/${this.activeSessionId}/last-response`)).json()).text||"";if(!o){const d=await(await fetch(`/api/sessions/${this.activeSessionId}/terminal`)).json();d.terminalBuffer&&(o=this._cleanTerminalBuffer(d.terminalBuffer))}const r=document.getElementById("responseViewerBody");r.innerHTML=this._renderMarkdown(o),this._bindResponseViewerInteractions(r);const a=document.getElementById("responseViewerTitle"),h=document.getElementById("responseViewerMore");a&&(a.textContent="Last Response"),h&&(h.style.display="",h.textContent="More"),e.classList.add("visible"),t.classList.add("visible"),r.scrollTop=0}catch(i){console.error("Failed to load response:",i)}}async loadFullContext(){if(!this.activeSessionId)return;const e=document.getElementById("responseViewerMore");e&&(e.textContent="...");try{const i=(await(await fetch(`/api/sessions/${this.activeSessionId}/last-response?context=full`)).json()).messages||[],n=document.getElementById("responseViewerBody"),o=document.getElementById("responseViewerTitle");if(!n)return;if(i.length===0){n.textContent="No conversation history available";return}n.innerHTML="";for(const r of i){const a=document.createElement("div"),h=r.role==="user";a.className="rv-message "+(h?"rv-msg-user":"rv-msg-assistant");const c=document.createElement("div");c.className="rv-role "+(h?"rv-role-user":"rv-role-assistant"),c.textContent=h?"You":"Claude",a.appendChild(c);const d=document.createElement("div");d.className="rv-text",d.innerHTML=this._renderMarkdown(r.text),a.appendChild(d),n.appendChild(a)}this._bindResponseViewerInteractions(n),o&&(o.textContent=`Conversation (${i.length} messages)`),e&&(e.style.display="none"),n.scrollTop=n.scrollHeight}catch(t){console.error("Failed to load context:",t)}finally{e&&(e.textContent="More")}}async _onSessionNeedsRefresh(){if(!(!this.activeSessionId||!this.terminal)&&!this._isLoadingBuffer)try{const t=await(await fetch(`/api/sessions/${this.activeSessionId}/terminal?tail=${TERMINAL_TAIL_SIZE}`)).json();t.terminalBuffer&&(this.terminal.clear(),this.terminal.reset(),await this.chunkedTerminalWrite(t.terminalBuffer),this.terminal.scrollToBottom(),this._localEchoOverlay?.rerender(),this.activeSessionId&&this.sendResize(this.activeSessionId))}catch(e){console.error("needsRefresh reload failed:",e)}}async _onSessionClearTerminal(e){if(e.id===this.activeSessionId){if(this._isLoadingBuffer)return;try{const s=await(await fetch(`/api/sessions/${e.id}/terminal`)).json();if(this.terminal.clear(),this.terminal.reset(),s.terminalBuffer){const i=s.terminalBuffer.replace(DEC_SYNC_STRIP_RE,"");await this.chunkedTerminalWrite(i)}this.sendResize(e.id),this._localEchoOverlay?.rerender()}catch(t){console.error("clearTerminal refresh failed:",t)}}}_onSessionCompletion(e){this.totalCost+=e.cost||0,this.updateCost(),e.id===this.activeSessionId&&(this.terminal.writeln(""),this.terminal.writeln(`\x1B[1;32m Done (Cost: $${(e.cost||0).toFixed(4)})\x1B[0m`))}_onSessionError(e){e.id===this.activeSessionId&&this.terminal.writeln(`\x1B[1;31m Error: ${e.error}\x1B[0m`),this._notifySession(e.id,"critical","session-error","Session Error",e.error||"Unknown error")}_onSessionExit(e){this._wsSessionId===e.id&&this._disconnectWs();const t=this.sessions.get(e.id);t&&(t.status="stopped",this.renderSessionTabs(),e.id===this.activeSessionId&&this._updateLocalEchoState()),e.code&&e.code!==0&&this._notifySession(e.id,"critical","session-crash","Session Crashed",`Exited with code ${e.code}`)}_onSessionIdle(e){const t=this.sessions.get(e.id);if(t&&(t.status="idle",this.renderSessionTabs(),this.sendPendingCtrlL(e.id),e.id===this.activeSessionId&&this._updateLocalEchoState()),!this.respawnStatus[e.id]?.enabled){const s=this.notificationManager?.preferences?.stuckThresholdMs||6e5;clearTimeout(this.idleTimers.get(e.id)),this.idleTimers.set(e.id,setTimeout(()=>{this._notifySession(e.id,"warning","session-stuck","Session Idle",`Idle for ${Math.round(s/6e4)}+ minutes`),this.idleTimers.delete(e.id)},s))}}_onSessionWorking(e){const t=this.sessions.get(e.id);t&&(t.status="busy",this.pendingHooks.has(e.id)||this.tabAlerts.delete(e.id),this.renderSessionTabs(),this.sendPendingCtrlL(e.id),e.id===this.activeSessionId&&this._updateLocalEchoState());const s=this.idleTimers.get(e.id);s&&(clearTimeout(s),this.idleTimers.delete(e.id))}_onSessionAutoClear(e){e.sessionId===this.activeSessionId&&(this.showToast(`Auto-cleared at ${e.tokens.toLocaleString()} tokens`,"info"),this.updateRespawnTokens(0)),this._notifySession(e.sessionId,"info","auto-clear","Auto-Cleared",`Context reset at ${(e.tokens||0).toLocaleString()} tokens`)}_onSessionCliInfo(e){const t=this.sessions.get(e.sessionId);t&&(e.version&&(t.cliVersion=e.version),e.model&&(t.cliModel=e.model),e.accountType&&(t.cliAccountType=e.accountType),e.latestVersion&&(t.cliLatestVersion=e.latestVersion)),e.sessionId===this.activeSessionId&&this.updateCliInfoDisplay()}_onScheduledCreated(e){this.currentRun=e,this.showTimer()}_onScheduledUpdated(e){this.currentRun=e,this.updateTimer()}_onScheduledCompleted(e){this.currentRun=e,this.hideTimer(),this.showToast("Scheduled run completed!","success")}_onScheduledStopped(){this.currentRun=null,this.hideTimer()}setConnectionStatus(e){this._connectionStatus=e,this._updateConnectionIndicator(),e==="connected"&&this._inputQueue.size>0&&this._drainInputQueues()}_connectWs(e){this._disconnectWs();const s=`${location.protocol==="https:"?"wss:":"ws:"}//${location.host}/ws/sessions/${e}/terminal`,i=new WebSocket(s);this._ws=i,this._wsSessionId=e,i.onopen=()=>{this._ws===i&&(this._wsReady=!0,this._wsReconnectAttempts=0)},i.onmessage=n=>{if(this._ws===i)try{const o=JSON.parse(n.data);o.t==="o"?this._onSessionTerminal({id:e,data:o.d}):o.t==="c"?this._onSessionClearTerminal({id:e}):o.t==="r"&&this._onSessionNeedsRefresh({id:e})}catch{}},i.onclose=n=>{if(this._ws===i&&(this._ws=null,this._wsSessionId=null,this._wsReady=!1,n.code<4004&&this.activeSessionId===e)){const o=Math.min(1e3*Math.pow(2,this._wsReconnectAttempts||0),1e4);this._wsReconnectAttempts=(this._wsReconnectAttempts||0)+1,this._wsReconnectTimer=setTimeout(()=>{this._wsReconnectTimer=null,this.activeSessionId===e&&this._connectWs(e)},o)}},i.onerror=()=>{}}_disconnectWs(){this._clearTimer("_wsReconnectTimer"),this._wsReconnectAttempts=0,this._ws&&(this._ws.onclose=null,this._ws.close(),this._ws=null,this._wsSessionId=null,this._wsReady=!1)}_sendInputAsync(e,t){if(!this.isOnline||this._connectionStatus==="disconnected"){this._enqueueInput(e,t);return}if(this._wsReady&&this._wsSessionId===e)try{this._ws.send(JSON.stringify({t:"i",d:t})),this.clearPendingHooks(e);return}catch{}this._inputSendChain=this._inputSendChain.then(()=>{fetch(`/api/sessions/${e}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t}),keepalive:t.length<65536}).then(i=>{i.ok?this.clearPendingHooks(e):this._enqueueInput(e,t)}).catch(()=>{this._enqueueInput(e,t)})})}_enqueueInput(e,t){let i=(this._inputQueue.get(e)||"")+t;i.length>this._inputQueueMaxBytes&&(i=i.slice(i.length-this._inputQueueMaxBytes)),this._inputQueue.set(e,i),this._updateConnectionIndicator()}async _drainInputQueues(){if(this._inputQueue.size===0)return;const e=new Map(this._inputQueue);this._inputQueue.clear(),this._updateConnectionIndicator();for(const[t,s]of e)(await this._apiPost(`/api/sessions/${t}/input`,{input:s}))?.ok||this._enqueueInput(t,s);this._updateConnectionIndicator()}_updateConnectionIndicator(){const e=this.$("connectionIndicator"),t=this.$("connectionDot"),s=this.$("connectionText");if(!e||!t||!s)return;let i=0;for(const a of this._inputQueue.values())i+=a.length;const n=this._connectionStatus,o=i>0;if((n==="connected"||n==="connecting")&&!o){e.style.display="none";return}e.style.display="flex",t.className="connection-dot";const r=a=>a<1024?`${a}B`:`${(a/1024).toFixed(1)}KB`;n==="connected"&&o?(t.classList.add("draining"),s.textContent=`Sending ${r(i)}...`):n==="reconnecting"?(t.classList.add("reconnecting"),s.textContent=o?`Reconnecting (${r(i)} queued)`:"Reconnecting..."):(t.classList.add("offline"),s.textContent=o?`Offline (${r(i)} queued)`:"Offline")}setupOnlineDetection(){window.addEventListener("online",()=>{this.isOnline=!0,this.reconnectAttempts=0,this.connectSSE()}),window.addEventListener("offline",()=>{this.isOnline=!1,this.setConnectionStatus("offline")})}_updateCjkInputState(){const e=document.getElementById("cjkInput");if(!e)return;const t=this.loadAppSettingsFromStorage(),s=this._serverCjkOverride||t.cjkInputEnabled||!1;e.style.display=s?"block":"none",s||(window.cjkActive=!1)}_resetAllAppState(){this.sessions.clear(),this.ralphStates.clear(),this.terminalBuffers.clear(),this.terminalBufferCache.clear(),this.projectInsights.clear(),this.teams.clear(),this.teamTasks.clear();for(const e of this.idleTimers.values())clearTimeout(e);if(this.idleTimers.clear(),this._clearTimer("flickerFilterTimeout"),this.flickerFilterBuffer="",this.flickerFilterActive=!1,this._clearTimer("syncWaitTimeout"),this.pendingWrites=[],this.writeFrameScheduled=!1,this._isLoadingBuffer=!1,this._loadBufferQueue=null,this._chunkedWriteGen=(this._chunkedWriteGen||0)+1,this._localEchoOverlay?.rerender(),this.pendingHooks.clear(),this._parentNameCache&&this._parentNameCache.clear(),this.subagentActivity.clear(),this.subagentToolResults.clear(),MobileDetection.cleanup(),KeyboardHandler.cleanup(),MobileDetection.init(),KeyboardHandler.init(),this.tabAlerts.clear(),this._shownCompletions&&this._shownCompletions.clear(),this.notificationManager?.titleFlashInterval&&(clearInterval(this.notificationManager.titleFlashInterval),this.notificationManager.titleFlashInterval=null),this.notificationManager?.groupingMap){for(const{timeout:e}of this.notificationManager.groupingMap.values())clearTimeout(e);this.notificationManager.groupingMap.clear()}this.terminalResizeObserver&&(this.terminalResizeObserver.disconnect(),this.terminalResizeObserver=null),this.planLoadingTimer&&(clearInterval(this.planLoadingTimer),this.planLoadingTimer=null),this.timerCountdownInterval&&(clearInterval(this.timerCountdownInterval),this.timerCountdownInterval=null),this.runSummaryAutoRefreshTimer&&(clearInterval(this.runSummaryAutoRefreshTimer),this.runSummaryAutoRefreshTimer=null)}handleInit(e){this._clearTimer("_initFallbackTimer");const t=++this._initGeneration;if(this._serverCjkOverride=e.inputCjkForm||!1,this._updateCjkInputState(),e.version){const n=this.$("versionDisplay"),o=this.$("headerVersion");n&&(n.textContent=`v${e.version}`,n.title=`Codeman v${e.version}`),o&&(o.textContent=`v${e.version}`,o.title=`Codeman v${e.version}`)}VoiceInput.cleanup(),this._resetAllAppState(),e.sessions.forEach(n=>{this.sessions.set(n.id,n),(n.ralphLoop||n.ralphTodos)&&!this.ralphClosedSessions.has(n.id)&&this.ralphStates.set(n.id,{loop:n.ralphLoop||null,todos:n.ralphTodos||[]})}),this._restoreEndedTabs(),this.syncSessionOrder(),e.respawnStatus?this.respawnStatus=e.respawnStatus:this.respawnStatus={},this.respawnTimers={},this.respawnCountdownTimers={},this.respawnActionLogs={},e.globalStats&&(this.globalStats=e.globalStats),this.totalCost=e.sessions.reduce((n,o)=>n+(o.totalCost||0),0),this.totalCost+=e.scheduledRuns.reduce((n,o)=>n+(o.totalCost||0),0);const s=e.scheduledRuns.find(n=>n.status==="running");if(s&&(this.currentRun=s,this.showTimer()),this.updateCost(),this.renderSessionTabs(),this.sessions.size>0?this.startSystemStatsPolling():this.stopSystemStatsPolling(),this.cleanupAllFloatingWindows(),e.subagents&&(this.subagents.clear(),this.subagentActivity.clear(),this.subagentToolResults.clear(),e.subagents.forEach(n=>{this.subagents.set(n.agentId,n)}),this.renderSubagentPanel(),this.subagentParentMap.clear(),this.loadSubagentParentMap().then(()=>{for(const[n,o]of this.subagentParentMap){const r=this.subagents.get(n);if(r&&this.sessions.has(o)){r.parentSessionId=o;const a=this.sessions.get(o);a&&(r.parentSessionName=this.getSessionName(a)),this.subagents.set(n,r)}}for(const[n]of this.subagents)this.subagentParentMap.has(n)||this.findParentSessionForSubagent(n);this.restoreSubagentWindowStates()})),t!==this._initGeneration)return;const i=this.activeSessionId;if(this.activeSessionId=null,this.sessionOrder.length>0){let n=i;if(!n||!this.sessions.has(n))try{n=localStorage.getItem("codeman-active-session")}catch{}n&&this.sessions.has(n)?this.selectSession(n):this.selectSession(this.sessionOrder[0])}}async loadState(){try{const t=await(await fetch("/api/status")).json();this.handleInit(t)}catch(e){console.error("Failed to load state:",e)}}_debouncedCall(e,t,s=100){this._debounceTimers[e]&&clearTimeout(this._debounceTimers[e]),this._debounceTimers[e]=setTimeout(()=>{this._debounceTimers[e]=null,t.call(this)},s)}renderSessionTabs(){this._activeRename||this._debouncedCall("sessionTabs",this._renderSessionTabsImmediate)}_updateActiveTabImmediate(e){const t=this.$("sessionTabs");if(!t)return;const s=t.querySelectorAll(".session-tab[data-id]");for(const i of s)i.dataset.id===e?i.classList.add("active"):i.classList.remove("active")}_renderSessionTabsImmediate(){const e=this.$("sessionTabs"),t=e.querySelectorAll(".session-tab[data-id]"),s=new Set([...t].map(o=>o.dataset.id)),i=new Set(this.sessions.keys());if(s.size===i.size&&[...s].every(o=>i.has(o)))for(const[o,r]of this.sessions){const a=e.querySelector(`.session-tab[data-id="${o}"]`);if(!a)continue;const h=o===this.activeSessionId,c=r.status||"idle",d=this.getSessionName(r),u=r.taskStats||{running:0,total:0},p=u.running>0;h&&!a.classList.contains("active")?a.classList.add("active"):!h&&a.classList.contains("active")&&a.classList.remove("active");const T=this.tabAlerts.get(o),f=T==="action",m=T==="idle",b=a.classList.contains("tab-alert-action"),w=a.classList.contains("tab-alert-idle");if(f&&!b?(a.classList.add("tab-alert-action"),a.classList.remove("tab-alert-idle")):m&&!w?(a.classList.add("tab-alert-idle"),a.classList.remove("tab-alert-action")):!T&&(b||w)&&a.classList.remove("tab-alert-action","tab-alert-idle"),!a.querySelector(".tab-number")){const S=this.sessionOrder.indexOf(o);if(S>=0&&S<9){const _=document.createElement("span");_.className="tab-number",_.textContent=String(S+1),a.insertBefore(_,a.firstChild)}}const v=a.querySelector(".tab-status");v&&!v.classList.contains(c)&&(v.className=`tab-status ${c}`);const A=a.querySelector(".tab-name");if(A&&A.textContent!==d){const S=parseSessionPrefix(d);S&&S.suffix?A.innerHTML='<span class="tab-prefix">'+escapeHtml(S.prefix)+'</span><span class="tab-suffix">: '+escapeHtml(S.suffix)+"</span>":A.textContent=d}const C=a.querySelector(".tab-badge");if(p)if(C)C.textContent!==String(u.running)&&(C.textContent=u.running);else{this._fullRenderSessionTabs();return}else if(C){this._fullRenderSessionTabs();return}const E=a.querySelector(".tab-subagent-badge"),g=this.minimizedSubagents.get(o),y=g?.size||0;if(y>0&&E){const S=E.querySelector(".subagent-label"),_=y===1?"AGENT":`AGENTS (${y})`;S&&S.textContent!==_&&(S.textContent=_);const O=E.querySelector(".subagent-dropdown");if(O){const L=this.renderSubagentTabBadge(o,g),R=document.createElement("div");R.innerHTML=L;const I=R.querySelector(".subagent-dropdown");I&&(O.innerHTML=I.innerHTML)}}else if(y>0&&!E){const S=this.renderSubagentTabBadge(o,g),_=a.querySelector(".tab-gear");_&&_.insertAdjacentHTML("beforebegin",S)}else y===0&&E&&E.remove()}else this._fullRenderSessionTabs()}_fullRenderSessionTabs(){if(this._activeRename)return;const e=this.$("sessionTabs");document.querySelectorAll("body > .subagent-dropdown").forEach(n=>n.remove()),this.cancelHideSubagentDropdown();const t=[];let s=this.sessionOrder;MobileDetection.getDeviceType()==="mobile"&&this.activeSessionId&&(s=[this.activeSessionId,...this.sessionOrder.filter(n=>n!==this.activeSessionId)]);let i=0;for(const n of s){const o=this.sessions.get(n);if(!o)continue;const r=n===this.activeSessionId,a=o.status||"idle",h=this.getSessionName(o),c=o.mode||"claude",d=o.color||"default",u=o.taskStats||{running:0,total:0},p=u.running>0,T=this.tabAlerts.get(n),f=T==="action"?" tab-alert-action":T==="idle"?" tab-alert-idle":"",m=this.minimizedSubagents.get(n),w=(m?.size||0)>0?this.renderSubagentTabBadge(n,m):"",v=o.workingDir&&o.workingDir.split("/").pop()||"",C=(this._tallTabsEnabled??!1)&&o.name&&v&&v!==h,E=o._ended?' data-ended="1"':"";t.push(`<div class="session-tab ${r?"active":""}${f}" data-id="${n}" data-color="${d}"${E} onclick="app.selectSession('${escapeHtml(n)}')" oncontextmenu="event.preventDefault(); app.startInlineRename('${escapeHtml(n)}')" tabindex="0" role="tab" aria-selected="${r?"true":"false"}" aria-label="${escapeHtml(h)} session" ${o.workingDir?`title="${escapeHtml(o.workingDir)}"`:""}>
|
|
14
|
+
`).trim()}_preprocessAsciiArt(e){const t=/[─-╿▀-▟]/,s=/```[\s\S]*?```/g,i=[];return e.replace(s,r=>(i.push(r),`\0FENCE${i.length-1}\0`)).split(/(\n{2,})/).map(r=>/^\n{2,}$/.test(r)||!r.trim()||r.includes("\0FENCE")?r:t.test(r)?"\n```\n"+r+"\n```\n":r).join("").replace(/FENCE(\d+)/g,(r,a)=>i[Number(a)])}_renderMarkdown(e){if(typeof marked<"u"&&marked.parse)try{const s=this._preprocessAsciiArt(e);let i=this._sanitizeHtml(marked.parse(s,{breaks:!0,gfm:!0}));i=i.replace(/<table>/g,'<div class="rv-table-wrap"><table>').replace(/<\/table>/g,"</table></div>");const n=/[─-╿▀-▟]/,o=document.createElement("template");return o.innerHTML=i,o.content.querySelectorAll("pre > code").forEach(r=>{if(!n.test(r.textContent||""))return;const a=r.parentElement;a.classList.add("rv-diagram");const h=document.createElement("div");h.className="rv-diagram-wrap";const c=document.createElement("button");c.className="rv-wrap-toggle",c.type="button",c.setAttribute("aria-label","Toggle line wrapping"),c.setAttribute("title","Toggle line wrapping"),a.parentNode.insertBefore(h,a),h.appendChild(c),h.appendChild(a)}),o.innerHTML}catch{}return`<pre style="white-space:pre-wrap;word-break:break-word">${e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">")}</pre>`}_bindResponseViewerInteractions(e){!e||e.dataset.rvBound==="1"||(e.dataset.rvBound="1",e.addEventListener("click",t=>{const s=t.target.closest(".rv-wrap-toggle");if(!s)return;t.preventDefault(),t.stopPropagation();const i=s.closest(".rv-diagram-wrap"),n=i?.querySelector("pre.rv-diagram");if(!n||!i)return;const o=n.classList.toggle("rv-nowrap");i.classList.toggle("rv-wrap-nowrap",o)}))}async toggleResponseViewer(){const e=document.getElementById("responseViewer"),t=document.getElementById("responseViewerBackdrop");if(!e)return;if(e.classList.contains("visible")){e.classList.remove("visible"),t.classList.remove("visible");return}if(this.activeSessionId)try{let o=(await(await fetch(`/api/sessions/${this.activeSessionId}/last-response`)).json()).text||"";if(!o){const d=await(await fetch(`/api/sessions/${this.activeSessionId}/terminal`)).json();d.terminalBuffer&&(o=this._cleanTerminalBuffer(d.terminalBuffer))}const r=document.getElementById("responseViewerBody");r.innerHTML=this._renderMarkdown(o),this._bindResponseViewerInteractions(r);const a=document.getElementById("responseViewerTitle"),h=document.getElementById("responseViewerMore");a&&(a.textContent="Last Response"),h&&(h.style.display="",h.textContent="More"),e.classList.add("visible"),t.classList.add("visible"),r.scrollTop=0}catch(i){console.error("Failed to load response:",i)}}async loadFullContext(){if(!this.activeSessionId)return;const e=document.getElementById("responseViewerMore");e&&(e.textContent="...");try{const i=(await(await fetch(`/api/sessions/${this.activeSessionId}/last-response?context=full`)).json()).messages||[],n=document.getElementById("responseViewerBody"),o=document.getElementById("responseViewerTitle");if(!n)return;if(i.length===0){n.textContent="No conversation history available";return}n.innerHTML="";for(const r of i){const a=document.createElement("div"),h=r.role==="user";a.className="rv-message "+(h?"rv-msg-user":"rv-msg-assistant");const c=document.createElement("div");c.className="rv-role "+(h?"rv-role-user":"rv-role-assistant"),c.textContent=h?"You":"Claude",a.appendChild(c);const d=document.createElement("div");d.className="rv-text",d.innerHTML=this._renderMarkdown(r.text),a.appendChild(d),n.appendChild(a)}this._bindResponseViewerInteractions(n),o&&(o.textContent=`Conversation (${i.length} messages)`),e&&(e.style.display="none"),n.scrollTop=n.scrollHeight}catch(t){console.error("Failed to load context:",t)}finally{e&&(e.textContent="More")}}async _onSessionNeedsRefresh(){if(!(!this.activeSessionId||!this.terminal)&&!this._isLoadingBuffer)try{const t=await(await fetch(`/api/sessions/${this.activeSessionId}/terminal?tail=${TERMINAL_TAIL_SIZE}`)).json();t.terminalBuffer&&(this.terminal.clear(),this.terminal.reset(),await this.chunkedTerminalWrite(t.terminalBuffer),this.terminal.scrollToBottom(),this._localEchoOverlay?.rerender(),this.activeSessionId&&this.sendResize(this.activeSessionId))}catch(e){console.error("needsRefresh reload failed:",e)}}async _onSessionClearTerminal(e){if(e.id===this.activeSessionId){if(this._isLoadingBuffer)return;try{const s=await(await fetch(`/api/sessions/${e.id}/terminal`)).json();if(this.terminal.clear(),this.terminal.reset(),s.terminalBuffer){const i=s.terminalBuffer.replace(DEC_SYNC_STRIP_RE,"");await this.chunkedTerminalWrite(i)}this.sendResize(e.id),this._localEchoOverlay?.rerender()}catch(t){console.error("clearTerminal refresh failed:",t)}}}_onSessionCompletion(e){this.totalCost+=e.cost||0,this.updateCost(),e.id===this.activeSessionId&&(this.terminal.writeln(""),this.terminal.writeln(`\x1B[1;32m Done (Cost: $${(e.cost||0).toFixed(4)})\x1B[0m`))}_onSessionError(e){e.id===this.activeSessionId&&this.terminal.writeln(`\x1B[1;31m Error: ${e.error}\x1B[0m`),this._notifySession(e.id,"critical","session-error","Session Error",e.error||"Unknown error")}_onSessionExit(e){this._wsSessionId===e.id&&this._disconnectWs();const t=this.sessions.get(e.id);t&&(t.status="stopped",this.renderSessionTabs(),e.id===this.activeSessionId&&this._updateLocalEchoState()),e.code&&e.code!==0&&this._notifySession(e.id,"critical","session-crash","Session Crashed",`Exited with code ${e.code}`)}_onSessionIdle(e){const t=this.sessions.get(e.id);if(t&&(t.status="idle",this.renderSessionTabs(),this.sendPendingCtrlL(e.id),e.id===this.activeSessionId&&this._updateLocalEchoState()),!this.respawnStatus[e.id]?.enabled){const s=this.notificationManager?.preferences?.stuckThresholdMs||6e5;clearTimeout(this.idleTimers.get(e.id)),this.idleTimers.set(e.id,setTimeout(()=>{this._notifySession(e.id,"warning","session-stuck","Session Idle",`Idle for ${Math.round(s/6e4)}+ minutes`),this.idleTimers.delete(e.id)},s))}}_onSessionWorking(e){const t=this.sessions.get(e.id);t&&(t.status="busy",this.pendingHooks.has(e.id)||this.tabAlerts.delete(e.id),this.renderSessionTabs(),this.sendPendingCtrlL(e.id),e.id===this.activeSessionId&&this._updateLocalEchoState());const s=this.idleTimers.get(e.id);s&&(clearTimeout(s),this.idleTimers.delete(e.id))}_onSessionAutoClear(e){e.sessionId===this.activeSessionId&&(this.showToast(`Auto-cleared at ${e.tokens.toLocaleString()} tokens`,"info"),this.updateRespawnTokens(0)),this._notifySession(e.sessionId,"info","auto-clear","Auto-Cleared",`Context reset at ${(e.tokens||0).toLocaleString()} tokens`)}_onSessionCliInfo(e){const t=this.sessions.get(e.sessionId);t&&(e.version&&(t.cliVersion=e.version),e.model&&(t.cliModel=e.model),e.accountType&&(t.cliAccountType=e.accountType),e.latestVersion&&(t.cliLatestVersion=e.latestVersion)),e.sessionId===this.activeSessionId&&this.updateCliInfoDisplay()}_onScheduledCreated(e){this.currentRun=e,this.showTimer()}_onScheduledUpdated(e){this.currentRun=e,this.updateTimer()}_onScheduledCompleted(e){this.currentRun=e,this.hideTimer(),this.showToast("Scheduled run completed!","success")}_onScheduledStopped(){this.currentRun=null,this.hideTimer()}setConnectionStatus(e){this._connectionStatus=e,this._updateConnectionIndicator(),e==="connected"&&this._inputQueue.size>0&&this._drainInputQueues()}_connectWs(e){this._disconnectWs();const s=`${location.protocol==="https:"?"wss:":"ws:"}//${location.host}/ws/sessions/${e}/terminal`,i=new WebSocket(s);this._ws=i,this._wsSessionId=e,i.onopen=()=>{this._ws===i&&(this._wsReady=!0,this._wsReconnectAttempts=0)},i.onmessage=n=>{if(this._ws===i)try{const o=JSON.parse(n.data);o.t==="o"?this._onSessionTerminal({id:e,data:o.d}):o.t==="c"?this._onSessionClearTerminal({id:e}):o.t==="r"&&this._onSessionNeedsRefresh({id:e})}catch{}},i.onclose=n=>{if(this._ws===i&&(this._ws=null,this._wsSessionId=null,this._wsReady=!1,n.code<4004&&this.activeSessionId===e)){const o=Math.min(1e3*Math.pow(2,this._wsReconnectAttempts||0),1e4);this._wsReconnectAttempts=(this._wsReconnectAttempts||0)+1,this._wsReconnectTimer=setTimeout(()=>{this._wsReconnectTimer=null,this.activeSessionId===e&&this._connectWs(e)},o)}},i.onerror=()=>{}}_disconnectWs(){this._clearTimer("_wsReconnectTimer"),this._wsReconnectAttempts=0,this._ws&&(this._ws.onclose=null,this._ws.close(),this._ws=null,this._wsSessionId=null,this._wsReady=!1)}_sendInputAsync(e,t){if(!this.isOnline||this._connectionStatus==="disconnected"){this._enqueueInput(e,t);return}if(this._wsReady&&this._wsSessionId===e)try{this._ws.send(JSON.stringify({t:"i",d:t})),this.clearPendingHooks(e);return}catch{}this._inputSendChain=this._inputSendChain.then(()=>{fetch(`/api/sessions/${e}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t}),keepalive:t.length<65536}).then(i=>{i.ok?this.clearPendingHooks(e):this._enqueueInput(e,t)}).catch(()=>{this._enqueueInput(e,t)})})}_enqueueInput(e,t){let i=(this._inputQueue.get(e)||"")+t;i.length>this._inputQueueMaxBytes&&(i=i.slice(i.length-this._inputQueueMaxBytes)),this._inputQueue.set(e,i),this._updateConnectionIndicator()}async _drainInputQueues(){if(this._inputQueue.size===0)return;const e=new Map(this._inputQueue);this._inputQueue.clear(),this._updateConnectionIndicator();for(const[t,s]of e)(await this._apiPost(`/api/sessions/${t}/input`,{input:s}))?.ok||this._enqueueInput(t,s);this._updateConnectionIndicator()}_updateConnectionIndicator(){const e=this.$("connectionIndicator"),t=this.$("connectionDot"),s=this.$("connectionText");if(!e||!t||!s)return;let i=0;for(const a of this._inputQueue.values())i+=a.length;const n=this._connectionStatus,o=i>0;if((n==="connected"||n==="connecting")&&!o){e.style.display="none";return}e.style.display="flex",t.className="connection-dot";const r=a=>a<1024?`${a}B`:`${(a/1024).toFixed(1)}KB`;n==="connected"&&o?(t.classList.add("draining"),s.textContent=`Sending ${r(i)}...`):n==="reconnecting"?(t.classList.add("reconnecting"),s.textContent=o?`Reconnecting (${r(i)} queued)`:"Reconnecting..."):(t.classList.add("offline"),s.textContent=o?`Offline (${r(i)} queued)`:"Offline")}setupOnlineDetection(){window.addEventListener("online",()=>{this.isOnline=!0,this.reconnectAttempts=0,this.connectSSE()}),window.addEventListener("offline",()=>{this.isOnline=!1,this.setConnectionStatus("offline")})}_updateCjkInputState(){const e=document.getElementById("cjkInput");if(!e)return;const t=this.loadAppSettingsFromStorage(),s=this._serverCjkOverride||t.cjkInputEnabled||!1;e.style.display=s?"block":"none",s||(window.cjkActive=!1)}_resetAllAppState(){this.sessions.clear(),this.ralphStates.clear(),this.terminalBuffers.clear(),this.terminalBufferCache.clear(),this.projectInsights.clear(),this.teams.clear(),this.teamTasks.clear();for(const e of this.idleTimers.values())clearTimeout(e);if(this.idleTimers.clear(),this._clearTimer("flickerFilterTimeout"),this.flickerFilterBuffer="",this.flickerFilterActive=!1,this._clearTimer("syncWaitTimeout"),this.pendingWrites=[],this.writeFrameScheduled=!1,this._isLoadingBuffer=!1,this._loadBufferQueue=null,this._chunkedWriteGen=(this._chunkedWriteGen||0)+1,this._localEchoOverlay?.rerender(),this.pendingHooks.clear(),this._parentNameCache&&this._parentNameCache.clear(),this.subagentActivity.clear(),this.subagentToolResults.clear(),MobileDetection.cleanup(),KeyboardHandler.cleanup(),MobileDetection.init(),KeyboardHandler.init(),this.tabAlerts.clear(),this._shownCompletions&&this._shownCompletions.clear(),this.notificationManager?.titleFlashInterval&&(clearInterval(this.notificationManager.titleFlashInterval),this.notificationManager.titleFlashInterval=null),this.notificationManager?.groupingMap){for(const{timeout:e}of this.notificationManager.groupingMap.values())clearTimeout(e);this.notificationManager.groupingMap.clear()}this.terminalResizeObserver&&(this.terminalResizeObserver.disconnect(),this.terminalResizeObserver=null),this.planLoadingTimer&&(clearInterval(this.planLoadingTimer),this.planLoadingTimer=null),this.timerCountdownInterval&&(clearInterval(this.timerCountdownInterval),this.timerCountdownInterval=null),this.runSummaryAutoRefreshTimer&&(clearInterval(this.runSummaryAutoRefreshTimer),this.runSummaryAutoRefreshTimer=null)}handleInit(e){this._clearTimer("_initFallbackTimer");const t=++this._initGeneration;if(this._serverCjkOverride=e.inputCjkForm||!1,this._updateCjkInputState(),e.version){const n=this.$("versionDisplay"),o=this.$("headerVersion");n&&(n.textContent=`v${e.version}`,n.title=`Codeman v${e.version}`),o&&(o.textContent=`v${e.version}`,o.title=`Codeman v${e.version}`)}VoiceInput.cleanup(),this._resetAllAppState(),e.sessions.forEach(n=>{this.sessions.set(n.id,n),(n.ralphLoop||n.ralphTodos)&&!this.ralphClosedSessions.has(n.id)&&this.ralphStates.set(n.id,{loop:n.ralphLoop||null,todos:n.ralphTodos||[]})}),this._restoreEndedTabs(),this.syncSessionOrder(),e.respawnStatus?this.respawnStatus=e.respawnStatus:this.respawnStatus={},this.respawnTimers={},this.respawnCountdownTimers={},this.respawnActionLogs={},e.globalStats&&(this.globalStats=e.globalStats),this.totalCost=e.sessions.reduce((n,o)=>n+(o.totalCost||0),0),this.totalCost+=e.scheduledRuns.reduce((n,o)=>n+(o.totalCost||0),0);const s=e.scheduledRuns.find(n=>n.status==="running");if(s&&(this.currentRun=s,this.showTimer()),this.updateCost(),this.renderSessionTabs(),this.sessions.size>0?this.startSystemStatsPolling():this.stopSystemStatsPolling(),this.cleanupAllFloatingWindows(),e.subagents&&(this.subagents.clear(),this.subagentActivity.clear(),this.subagentToolResults.clear(),e.subagents.forEach(n=>{this.subagents.set(n.agentId,n)}),this.renderSubagentPanel(),this.subagentParentMap.clear(),this.loadSubagentParentMap().then(()=>{for(const[n,o]of this.subagentParentMap){const r=this.subagents.get(n);if(r&&this.sessions.has(o)){r.parentSessionId=o;const a=this.sessions.get(o);a&&(r.parentSessionName=this.getSessionName(a)),this.subagents.set(n,r)}}for(const[n]of this.subagents)this.subagentParentMap.has(n)||this.findParentSessionForSubagent(n);this.restoreSubagentWindowStates()})),t!==this._initGeneration)return;const i=this.activeSessionId;if(this.activeSessionId=null,this.sessionOrder.length>0){let n=i;if(!n||!this.sessions.has(n))try{n=localStorage.getItem("codeman-active-session")}catch{}n&&this.sessions.has(n)?this.selectSession(n):this.selectSession(this.sessionOrder[0])}}async loadState(){try{const t=await(await fetch("/api/status")).json();this.handleInit(t)}catch(e){console.error("Failed to load state:",e)}}_debouncedCall(e,t,s=100){this._debounceTimers[e]&&clearTimeout(this._debounceTimers[e]),this._debounceTimers[e]=setTimeout(()=>{this._debounceTimers[e]=null,t.call(this)},s)}renderSessionTabs(){this._activeRename||this._debouncedCall("sessionTabs",this._renderSessionTabsImmediate)}_updateActiveTabImmediate(e){const t=this.$("sessionTabs");if(!t)return;const s=t.querySelectorAll(".session-tab[data-id]");for(const i of s)i.dataset.id===e?i.classList.add("active"):i.classList.remove("active")}_renderSessionTabsImmediate(){const e=this.$("sessionTabs"),t=e.querySelectorAll(".session-tab[data-id]"),s=new Set([...t].map(o=>o.dataset.id)),i=new Set(this.sessions.keys());if(s.size===i.size&&[...s].every(o=>i.has(o)))for(const[o,r]of this.sessions){const a=e.querySelector(`.session-tab[data-id="${o}"]`);if(!a)continue;const h=o===this.activeSessionId,c=r.status||"idle",d=this.getSessionName(r),u=r.taskStats||{running:0,total:0},p=u.running>0;h&&!a.classList.contains("active")?a.classList.add("active"):!h&&a.classList.contains("active")&&a.classList.remove("active");const T=this.tabAlerts.get(o),f=T==="action",m=T==="idle",b=a.classList.contains("tab-alert-action"),w=a.classList.contains("tab-alert-idle");if(f&&!b?(a.classList.add("tab-alert-action"),a.classList.remove("tab-alert-idle")):m&&!w?(a.classList.add("tab-alert-idle"),a.classList.remove("tab-alert-action")):!T&&(b||w)&&a.classList.remove("tab-alert-action","tab-alert-idle"),!a.querySelector(".tab-number")){const S=this.sessionOrder.indexOf(o);if(S>=0&&S<9){const _=document.createElement("span");_.className="tab-number",_.textContent=String(S+1),a.insertBefore(_,a.firstChild)}}const v=a.querySelector(".tab-status");v&&!v.classList.contains(c)&&(v.className=`tab-status ${c}`);const A=a.querySelector(".tab-name");if(A&&A.textContent!==d){const S=parseSessionPrefix(d);S&&S.suffix?A.innerHTML='<span class="tab-prefix">'+escapeHtml(S.prefix)+'</span><span class="tab-suffix">: '+escapeHtml(S.suffix)+"</span>":A.textContent=d}const C=a.querySelector(".tab-badge");if(p)if(C)C.textContent!==String(u.running)&&(C.textContent=u.running);else{this._fullRenderSessionTabs();return}else if(C){this._fullRenderSessionTabs();return}const E=a.querySelector(".tab-subagent-badge"),g=this.minimizedSubagents.get(o),y=g?.size||0;if(y>0&&E){const S=E.querySelector(".subagent-label"),_=y===1?"AGENT":`AGENTS (${y})`;S&&S.textContent!==_&&(S.textContent=_);const O=E.querySelector(".subagent-dropdown");if(O){const I=this.renderSubagentTabBadge(o,g),R=document.createElement("div");R.innerHTML=I;const L=R.querySelector(".subagent-dropdown");L&&(O.innerHTML=L.innerHTML)}}else if(y>0&&!E){const S=this.renderSubagentTabBadge(o,g),_=a.querySelector(".tab-gear");_&&_.insertAdjacentHTML("beforebegin",S)}else y===0&&E&&E.remove()}else this._fullRenderSessionTabs()}_fullRenderSessionTabs(){if(this._activeRename)return;const e=this.$("sessionTabs");document.querySelectorAll("body > .subagent-dropdown").forEach(n=>n.remove()),this.cancelHideSubagentDropdown();const t=[];let s=this.sessionOrder;MobileDetection.getDeviceType()==="mobile"&&this.activeSessionId&&(s=[this.activeSessionId,...this.sessionOrder.filter(n=>n!==this.activeSessionId)]);let i=0;for(const n of s){const o=this.sessions.get(n);if(!o)continue;const r=n===this.activeSessionId,a=o.status||"idle",h=this.getSessionName(o),c=o.mode||"claude",d=o.color||"default",u=o.taskStats||{running:0,total:0},p=u.running>0,T=this.tabAlerts.get(n),f=T==="action"?" tab-alert-action":T==="idle"?" tab-alert-idle":"",m=this.minimizedSubagents.get(n),w=(m?.size||0)>0?this.renderSubagentTabBadge(n,m):"",v=o.workingDir&&o.workingDir.split("/").pop()||"",C=(this._tallTabsEnabled??!1)&&o.name&&v&&v!==h,E=o._ended?' data-ended="1"':"";t.push(`<div class="session-tab ${r?"active":""}${f}" data-id="${n}" data-color="${d}"${E} onclick="app.selectSession('${escapeHtml(n)}')" oncontextmenu="event.preventDefault(); app.startInlineRename('${escapeHtml(n)}')" tabindex="0" role="tab" aria-selected="${r?"true":"false"}" aria-label="${escapeHtml(h)} session" ${o.workingDir?`title="${escapeHtml(o.workingDir)}"`:""}>
|
|
15
15
|
${i<9?'<span class="tab-number">'+(i+1)+"</span>":""}
|
|
16
16
|
<span class="tab-status ${a}" aria-hidden="true"></span>
|
|
17
17
|
<span class="tab-info">
|
|
Binary file
|
|
Binary file
|
|
@@ -71,6 +71,52 @@ const WINDOW_MIN_WIDTH_PX = 200;
|
|
|
71
71
|
const WINDOW_MIN_HEIGHT_PX = 200;
|
|
72
72
|
const WINDOW_DEFAULT_WIDTH_PX = 300;
|
|
73
73
|
|
|
74
|
+
// WebGL renderer auto-fallback thresholds.
|
|
75
|
+
// _installWebGLLongTaskGuard() observes longtask entries and disables WebGL
|
|
76
|
+
// after LONGTASK_COUNT stalls of >= LONGTASK_MS within WINDOW_MS. GRACE_MS
|
|
77
|
+
// suppresses the noisy initial-load stalls. STICKY_EXPIRY_MS is how long
|
|
78
|
+
// localStorage's webgl-disabled marker survives before we retry WebGL on a
|
|
79
|
+
// fresh load (driver/Chrome may have been updated).
|
|
80
|
+
const WEBGL_FALLBACK = {
|
|
81
|
+
LONGTASK_MS: 200,
|
|
82
|
+
LONGTASK_COUNT: 3,
|
|
83
|
+
WINDOW_MS: 30000,
|
|
84
|
+
GRACE_MS: 5000,
|
|
85
|
+
STICKY_EXPIRY_MS: 7 * 24 * 60 * 60 * 1000,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Pure rolling-window trip evaluator for the WebGL longtask guard.
|
|
90
|
+
* Mutates `recent` in place (prunes entries older than `now - WINDOW_MS`)
|
|
91
|
+
* and appends each new duration's startTime that meets the threshold.
|
|
92
|
+
* Returns true when the count inside the window reaches `LONGTASK_COUNT`.
|
|
93
|
+
*
|
|
94
|
+
* Exposed on `window` for unit testing — the production guard in app.js
|
|
95
|
+
* inlines this same logic in its PerformanceObserver callback. Splitting it
|
|
96
|
+
* out keeps the threshold math testable without a real PerformanceObserver.
|
|
97
|
+
*
|
|
98
|
+
* @param {number[]} recent - mutable array of startTimes inside the window
|
|
99
|
+
* @param {{startTime: number, duration: number}[]} entries - new longtask entries
|
|
100
|
+
* @param {number} now - performance.now() at evaluation time
|
|
101
|
+
* @param {typeof WEBGL_FALLBACK} [config=WEBGL_FALLBACK] - thresholds
|
|
102
|
+
* @returns {boolean} true if the rolling window has reached the trip count
|
|
103
|
+
*/
|
|
104
|
+
function evaluateWebGLLongTaskTrip(recent, entries, now, config = WEBGL_FALLBACK) {
|
|
105
|
+
for (const entry of entries) {
|
|
106
|
+
if (entry.duration >= config.LONGTASK_MS) recent.push(entry.startTime);
|
|
107
|
+
}
|
|
108
|
+
while (recent.length && now - recent[0] > config.WINDOW_MS) recent.shift();
|
|
109
|
+
return recent.length >= config.LONGTASK_COUNT;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Expose for tests. `const` declarations at the top of a non-module script
|
|
113
|
+
// are global lexical bindings but not `window` properties, so explicit
|
|
114
|
+
// assignment is the test-visible API surface.
|
|
115
|
+
if (typeof window !== 'undefined') {
|
|
116
|
+
window.WEBGL_FALLBACK = WEBGL_FALLBACK;
|
|
117
|
+
window.evaluateWebGLLongTaskTrip = evaluateWebGLLongTaskTrip;
|
|
118
|
+
}
|
|
119
|
+
|
|
74
120
|
// Scheduler API — prioritize terminal writes over background UI updates.
|
|
75
121
|
// scheduler.postTask('background') defers non-critical work (connection lines, panel renders)
|
|
76
122
|
// so the main thread stays free for terminal rendering at 60fps.
|
|
Binary file
|
|
Binary file
|
|
@@ -87,10 +87,15 @@ Object.assign(CodemanApp.prototype, {
|
|
|
87
87
|
e.preventDefault();
|
|
88
88
|
self._uploadAndInsertImages(imageFiles);
|
|
89
89
|
} else {
|
|
90
|
-
// No image --
|
|
90
|
+
// No image -- route text through xterm's paste() so bracketed-paste
|
|
91
|
+
// markers (CSI 200~ ... CSI 201~) survive when the inner application
|
|
92
|
+
// has enabled bracketed-paste mode (Claude Code does). Sending text
|
|
93
|
+
// via raw sendInput() strips those markers and makes pasted input
|
|
94
|
+
// indistinguishable from typed input, weakening the CLI's
|
|
95
|
+
// prompt-injection defenses.
|
|
91
96
|
var text = e.clipboardData ? e.clipboardData.getData('text/plain') : '';
|
|
92
97
|
e.preventDefault();
|
|
93
|
-
if (text) self.
|
|
98
|
+
if (text && self.terminal) self.terminal.paste(text);
|
|
94
99
|
}
|
|
95
100
|
});
|
|
96
101
|
|
|
Binary file
|
|
Binary file
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
<!-- Preload critical resources — lets browser discover these during HTML parse
|
|
21
21
|
instead of waiting until <script> tags at bottom-of-body are reached. -->
|
|
22
22
|
<link rel="preload" href="vendor/xterm.min.js" as="script">
|
|
23
|
-
<link rel="preload" href="constants.
|
|
24
|
-
<link rel="preload" href="app.
|
|
23
|
+
<link rel="preload" href="constants.cb6426c4.js" as="script">
|
|
24
|
+
<link rel="preload" href="app.03ed8e4e.js" as="script">
|
|
25
25
|
<!-- Self-hosted xterm.js — eliminates CDN DNS/TLS latency (~100ms).
|
|
26
26
|
'defer' preserves execution order (xterm loads before fit addon). -->
|
|
27
27
|
<script defer src="vendor/xterm.min.js"></script>
|
|
@@ -1787,14 +1787,14 @@
|
|
|
1787
1787
|
<!-- Lines drawn dynamically -->
|
|
1788
1788
|
</svg>
|
|
1789
1789
|
|
|
1790
|
-
<script defer src="constants.
|
|
1790
|
+
<script defer src="constants.cb6426c4.js"></script>
|
|
1791
1791
|
<script defer src="mobile-handlers.1e2a8ef8.js"></script>
|
|
1792
1792
|
<script defer src="voice-input.085e9e73.js"></script>
|
|
1793
1793
|
<script defer src="notification-manager.9c984ac2.js"></script>
|
|
1794
1794
|
<script defer src="keyboard-accessory.29aebd9c.js"></script>
|
|
1795
1795
|
<script defer src="input-cjk.88082175.js"></script>
|
|
1796
|
-
<script defer src="app.
|
|
1797
|
-
<script defer src="terminal-ui.
|
|
1796
|
+
<script defer src="app.03ed8e4e.js"></script>
|
|
1797
|
+
<script defer src="terminal-ui.d019e248.js"></script>
|
|
1798
1798
|
<script defer src="respawn-ui.5377f958.js"></script>
|
|
1799
1799
|
<script defer src="ralph-panel.61076370.js"></script>
|
|
1800
1800
|
<script defer src="orchestrator-panel.js"></script>
|
|
@@ -1804,6 +1804,6 @@
|
|
|
1804
1804
|
<script defer src="ralph-wizard.6b0f0be7.js"></script>
|
|
1805
1805
|
<script defer src="api-client.3adebdc2.js"></script>
|
|
1806
1806
|
<script defer src="subagent-windows.a366a4ad.js"></script>
|
|
1807
|
-
<script defer src="image-input.
|
|
1807
|
+
<script defer src="image-input.926911b4.js"></script>
|
|
1808
1808
|
</body>
|
|
1809
1809
|
</html>
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/dist/web/public/sw.js.gz
CHANGED
|
Binary file
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.assign(CodemanApp.prototype,{initTerminal(){const e=parseInt(localStorage.getItem("codeman-scrollback")),i=Number.isFinite(e)&&e>0?Math.max(e,DEFAULT_SCROLLBACK):DEFAULT_SCROLLBACK;if(this.terminal=new Terminal({theme:{background:"#0d0d0d",foreground:"#e0e0e0",cursor:"#e0e0e0",cursorAccent:"#0d0d0d",selection:"rgba(255, 255, 255, 0.3)",black:"#0d0d0d",red:"#ff6b6b",green:"#51cf66",yellow:"#ffd43b",blue:"#339af0",magenta:"#cc5de8",cyan:"#22b8cf",white:"#e0e0e0",brightBlack:"#495057",brightRed:"#ff8787",brightGreen:"#69db7c",brightYellow:"#ffe066",brightBlue:"#5c7cfa",brightMagenta:"#da77f2",brightCyan:"#66d9e8",brightWhite:"#ffffff"},fontFamily:'"Fira Code", "Cascadia Code", "JetBrains Mono", "SF Mono", Monaco, monospace',fontSize:MobileDetection.getDeviceType()==="mobile"?10:14,lineHeight:1.2,cursorBlink:!1,cursorStyle:"block",scrollback:i,allowTransparency:!0,allowProposedApi:!0}),this.fitAddon=new FitAddon.FitAddon,this.terminal.loadAddon(this.fitAddon),typeof Unicode11Addon<"u")try{const t=new Unicode11Addon.Unicode11Addon;this.terminal.loadAddon(t),this.terminal.unicode.activeVersion="11"}catch{}const s=document.getElementById("terminalContainer");this.terminal.open(s),this.terminal.attachCustomKeyEventHandler(t=>{if(t.isComposing||t.keyCode===229||t.altKey&&t.key>="0"&&t.key<="9")return!1;if((t.ctrlKey||t.metaKey)&&t.key==="v"&&t.type==="keydown")return this.activeSessionId&&this._handleImagePaste&&this._handleImagePaste(),!1;if(t.key==="Enter"&&(t.shiftKey||t.ctrlKey)&&t.type==="keydown"){if(this.activeSessionId)if(this._localEchoEnabled){const a=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),a&&(this._pendingInput+=a,y()),setTimeout(()=>{fetch(`/api/sessions/${this.activeSessionId}/send-key`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:t.ctrlKey?"C-Enter":"S-Enter"})})},a?80:0)}else fetch(`/api/sessions/${this.activeSessionId}/send-key`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({key:t.ctrlKey?"C-Enter":"S-Enter"})});return!1}return!0});{const t=s.querySelector(".xterm-helper-textarea");if(t&&MobileDetection.isTouchDevice()){let a=!1,l=0;t.addEventListener("compositionstart",()=>{a=!0}),t.addEventListener("compositionend",()=>{a=!1}),t.addEventListener("keydown",r=>{!r.isComposing&&r.keyCode!==229&&(l=Date.now())}),t.addEventListener("input",r=>{if(a||r.isComposing||r.inputType!=="insertText"||!r.data||Date.now()-l<50)return;const u=r.data;Promise.resolve().then(()=>{const f=t.value;!f||f.trim()===""&&u!==" "||(this.terminal._core.coreService.triggerDataEvent(u,!0),t.value="")})})}}this._webglAddon=null;const n=new URLSearchParams(location.search);if(n.get("webgl")==="force")try{localStorage.removeItem("codeman-webgl-disabled")}catch{}const c=(()=>{try{const t=localStorage.getItem("codeman-webgl-disabled");if(!t)return!1;const{at:a}=JSON.parse(t);return Date.now()-a>WEBGL_FALLBACK.STICKY_EXPIRY_MS?(localStorage.removeItem("codeman-webgl-disabled"),!1):!0}catch{return!1}})(),m=MobileDetection.getDeviceType()!=="desktop"||n.has("nowebgl")||c;if(c&&console.log("[CRASH-DIAG] WebGL sticky-disabled from prior stalls \u2014 DOM renderer in use. Re-enable: ?webgl=force"),!m)if(typeof WebglAddon<"u")this._initWebGL();else{const t=document.createElement("script");t.src="vendor/xterm-addon-webgl.min.js",t.onload=()=>this._initWebGL(),t.onerror=()=>console.warn("[CRASH-DIAG] Failed to load WebGL addon \u2014 using canvas renderer"),document.head.appendChild(t)}this._localEchoOverlay=new LocalEchoOverlay(this.terminal),this._cjkInput=null,typeof CjkInput<"u"&&(this._cjkInput=CjkInput.init({send:t=>{this.activeSessionId&&this._sendInputAsync(this.activeSessionId,t)}})),MobileDetection.getDeviceType()==="mobile"&&document.body.classList.contains("safari-browser")?requestAnimationFrame(()=>{this.fitAddon.fit(),requestAnimationFrame(()=>this.fitAddon.fit())}):this.fitAddon.fit(),this.registerFilePathLinkProvider(),s.addEventListener("wheel",t=>{t.preventDefault();const a=Math.round(t.deltaY/25)||(t.deltaY>0?1:-1);this.terminal.scrollLines(a)},{passive:!1});{const t=()=>this.terminal._core?._renderService?.dimensions?.css?.cell?.height||13;let a=0,l=0,r=0,u=null,f=!1;const p=S=>{const v=r?(S-r)/16.67:1;if(r=S,!f&&Math.abs(l)>.3){const T=Math.round(l/t());T!==0&&this.terminal.scrollLines(T),l*=.92,u=requestAnimationFrame(p)}else f?u=requestAnimationFrame(p):(u=null,l=0)};let _=0,b=!1;s.addEventListener("touchstart",S=>{S.touches.length===1&&(a=S.touches[0].clientY,l=0,_=0,f=!0,b=!1,r=0,u&&(cancelAnimationFrame(u),u=null))},{passive:!0}),s.addEventListener("touchmove",S=>{if(S.touches.length===1&&f){b=!0;const v=S.touches[0].clientY,T=a-v;_+=T,l=T*1.2,a=v;const w=t(),I=Math.trunc(_/w);I!==0&&(this.terminal.scrollLines(I),_-=I*w)}},{passive:!0}),s.addEventListener("touchend",()=>{f=!1,!u&&Math.abs(l)>.3&&(u=requestAnimationFrame(p)),!b&&this.terminal&&this.terminal.focus()},{passive:!0}),s.addEventListener("touchcancel",()=>{f=!1,l=0,_=0},{passive:!0})}this.showWelcome(),this.initImageInput(),this._chunkedWriteGen=0,this._resizeTimeout=null,this._lastResizeDims=null;const d=40,h=10,g=()=>{this._resizeTimeout&&clearTimeout(this._resizeTimeout),this._resizeTimeout=setTimeout(()=>{this._resizeTimeout=null,this.fitAddon&&this.fitAddon.fit(),this.flickerFilterBuffer&&(this.flickerFilterTimeout&&(clearTimeout(this.flickerFilterTimeout),this.flickerFilterTimeout=null),this.flushFlickerBuffer());const t=typeof KeyboardHandler<"u"&&KeyboardHandler.keyboardVisible;if(this.activeSessionId&&!t){const a=this.fitAddon.proposeDimensions(),l=a?Math.max(a.cols,d):d,r=a?Math.max(a.rows,h):h;if(!this._lastResizeDims||l!==this._lastResizeDims.cols||r!==this._lastResizeDims.rows){const u=this.activeSessionId?this.sessions.get(this.activeSessionId):null;u&&u.mode!=="shell"&&!u._ended&&this.terminal&&this.isTerminalAtBottom()&&this.terminal.write("\x1B[3J\x1B[H\x1B[2J"),this._lastResizeDims={cols:l,rows:r},fetch(`/api/sessions/${this.activeSessionId}/resize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({cols:l,rows:r})}).catch(()=>{})}}this.updateConnectionLines(),this._localEchoOverlay?.hasPending&&this._localEchoOverlay.rerender()},300)};window.addEventListener("resize",g),this.terminalResizeObserver&&this.terminalResizeObserver.disconnect(),this.terminalResizeObserver=new ResizeObserver(g),this.terminalResizeObserver.observe(s),this._pendingInput="",this._inputFlushTimeout=null,this._lastKeystrokeTime=0;const y=()=>{if(this._inputFlushTimeout=null,this._pendingInput&&this.activeSessionId){const t=this._pendingInput,a=this.activeSessionId;this._pendingInput="",this._sendInputAsync(a,t)}};this.terminal.onData(t=>{if(!(window.cjkActive||document.activeElement?.id==="cjkInput")&&this.activeSessionId){if(/^\x1b\[[\?>=]?[\d;]*[cnR]$/.test(t))return;if(this._localEchoEnabled){if(t==="\x7F"){if(this._localEchoOverlay?.removeChar()==="flushed"){const{count:r,text:u}=this._localEchoOverlay.getFlushed();this._flushedOffsets?.has(this.activeSessionId)&&(r===0?(this._flushedOffsets.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId)):(this._flushedOffsets.set(this.activeSessionId,r),this._flushedTexts?.set(this.activeSessionId,u))),this._pendingInput+=t,y()}return}if(/^[\r\n]+$/.test(t)){const l=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),l&&(this._pendingInput+=l,y()),setTimeout(()=>{this._pendingInput+="\r",y()},80);return}if(t.length>1&&t.charCodeAt(0)>=32){this._localEchoOverlay?.appendText(t);return}if(t.charCodeAt(0)<32){if(t.length>1&&t.charCodeAt(0)===27){this._pendingInput+=t,y();return}if(this._restoringFlushedState){this._pendingInput+=t,y();return}if(t===" "){const r=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),r&&(this._pendingInput+=r),this._pendingInput+=t,this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null);let u="";try{const p=this._localEchoOverlay?.findPrompt?.();if(p){const _=this.terminal.buffer.active,b=_.getLine(_.viewportY+p.row);b&&(u=b.translateToString(!0).slice(p.col+2).trimEnd())}}catch{}this._tabCompletionBaseText=u,y(),this._tabCompletionSessionId=this.activeSessionId,this._tabCompletionRetries=0,this._tabCompletionFallback&&clearTimeout(this._tabCompletionFallback);const f=this;this._tabCompletionFallback=setTimeout(()=>{if(f._tabCompletionFallback=null,!f._tabCompletionSessionId||f._tabCompletionSessionId!==f.activeSessionId)return;const p=f._localEchoOverlay;!p||p.pendingText||f.terminal.write("",()=>{if(!f._tabCompletionSessionId)return;p.resetBufferDetection();const _=p.detectBufferText();_&&_!==f._tabCompletionBaseText&&(f._tabCompletionSessionId=null,f._tabCompletionRetries=0,f._tabCompletionBaseText=null,p.rerender())})},300);return}const l=this._localEchoOverlay?.pendingText||"";this._localEchoOverlay?.clear(),this._localEchoOverlay?.suppressBufferDetection(),this._flushedOffsets?.delete(this.activeSessionId),this._flushedTexts?.delete(this.activeSessionId),l&&(this._pendingInput+=l),this._pendingInput+=t,this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),y();return}if(t.length===1&&t.charCodeAt(0)>=32){this._localEchoOverlay?.addChar(t);return}}if(this._pendingInput+=t,t.charCodeAt(0)<32||t.length>1){this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),y();return}const a=performance.now();a-this._lastKeystrokeTime>50?(this._inputFlushTimeout&&(clearTimeout(this._inputFlushTimeout),this._inputFlushTimeout=null),this._lastKeystrokeTime=a,y()):(this._lastKeystrokeTime=a,this._inputFlushTimeout||(this._inputFlushTimeout=setTimeout(y,0)))}})},registerFilePathLinkProvider(){const e=this;let i=-1;this.terminal.registerLinkProvider({provideLinks(s,n){s!==i&&(i=s,console.debug("[LinkProvider] Checking line:",s));const m=e.terminal.buffer.active.getLine(s-1);if(!m){n(void 0);return}const o=m.translateToString(!0);if(!o||!o.includes("/")){n(void 0);return}const d=[],h=/https?:\/\/[^\s"'<>|;&)\]\x00-\x1f]+/g,g=(u,f)=>{const p=u.replace(/[.,;:!?)]+$/,""),_=o.indexOf(p,f);_!==-1&&(d.some(b=>b.range.start.x===_+1)||d.push({text:p,range:{start:{x:_+1,y:s},end:{x:_+p.length+1,y:s}},decorations:{pointerCursor:!0,underline:!0},activate(b,S){window.open(S,"_blank","noopener,noreferrer")}}))},y=/(tail|cat|head|less|grep|watch|vim|nano)\s+(?:[^\s\/]*\s+)*(\/[^\s"'<>|;&\n\x00-\x1f]+)/g,t=/(\/(?:home|tmp|var|etc|opt)[^\s"'<>|;&\n\x00-\x1f]*\.(?:log|txt|json|md|yaml|yml|csv|xml|sh|py|ts|js))\b/g,a=/Bash\([^)]*?(\/(?:home|tmp|var|etc|opt)[^\s"'<>|;&\)\n\x00-\x1f]+)/g,l=(u,f)=>{const p=o.indexOf(u,f);p!==-1&&(d.some(_=>_.range.start.x===p+1)||d.push({text:u,range:{start:{x:p+1,y:s},end:{x:p+u.length+1,y:s}},decorations:{pointerCursor:!0,underline:!0},activate(_,b){e.openLogViewerWindow(b,e.activeSessionId)}}))};let r;for(h.lastIndex=0;(r=h.exec(o))!==null;)g(r[0],r.index);for(y.lastIndex=0;(r=y.exec(o))!==null;)l(r[2],r.index);for(t.lastIndex=0;(r=t.exec(o))!==null;)l(r[1],r.index);for(a.lastIndex=0;(r=a.exec(o))!==null;)l(r[1],r.index);d.length>0&&console.debug("[LinkProvider] Found links:",d.map(u=>u.text)),n(d.length>0?d:void 0)}}),console.log("[LinkProvider] File path link provider registered")},showWelcome(){const e=document.getElementById("welcomeOverlay");e&&(e.classList.add("visible"),this.loadTunnelStatus(),this.loadHistorySessions())},hideWelcome(){const e=document.getElementById("welcomeOverlay");e&&e.classList.remove("visible");const i=document.getElementById("welcomeQr");i&&(clearTimeout(this._welcomeQrShrinkTimer),i.classList.remove("expanded"))},async _fetchHistorySessions(){const s=(await(await fetch("/api/history/sessions")).json()).sessions||[];if(s.length===0)return[];const n=new Map;for(const m of s){const o=m.projectKey||m.workingDir;n.has(o)||n.set(o,[]),n.get(o).push(m)}const c=[];for(const[,m]of n)c.push(...m.slice(0,3));return c.sort((m,o)=>new Date(o.lastModified)-new Date(m.lastModified)),c},_resolveCaseLabel(e,i){if(!e)return"";let s=null;for(const n of i||[])if(!(!n||!n.path)){if(e===n.path)return`#${n.name}`;if(e.startsWith(n.path+"/")){const c=n.path.length;(!s||c>s.len)&&(s={name:n.name,suffix:e.slice(c),len:c})}}return s?`#${s.name}${s.suffix}`:e.split("/").pop()||e},_shortenHomePath(e){return(e||"").replace(/^\/home\/[^/]+\//,"~/").replace(/^\/Users\/[^/]+\//,"~/")},_buildHistoryItem(e,i){const s=e.sizeBytes<1024?`${e.sizeBytes}B`:e.sizeBytes<1048576?`${(e.sizeBytes/1024).toFixed(0)}K`:`${(e.sizeBytes/1048576).toFixed(1)}M`,n=new Date(e.lastModified),c=n.toLocaleDateString("en",{month:"short",day:"numeric"})+" "+n.toLocaleTimeString("en",{hour:"2-digit",minute:"2-digit",hour12:!1}),m=this._shortenHomePath(e.workingDir),o=this._resolveCaseLabel(e.workingDir,i),d=document.createElement("div");d.className="history-item",d.title=e.workingDir;const h=document.createElement("div");h.className="history-item-main",h.addEventListener("click",()=>this.resumeHistorySession(e.sessionId,e.workingDir));const g=document.createElement("div");g.className="history-item-text";const y=document.createElement("span");y.className="history-item-title",y.textContent=e.firstPrompt||m;const t=document.createElement("span");t.className="history-item-subtitle",o.startsWith("#")&&t.classList.add("is-case"),t.textContent=o,g.append(y,t);const a=document.createElement("span");a.className="history-item-meta",a.textContent=c;const l=document.createElement("button");l.className="history-item-expand",l.type="button",l.setAttribute("aria-label","Show details"),l.setAttribute("aria-expanded","false"),l.textContent="\u22EF",h.append(g,a,l);const r=document.createElement("div");r.className="history-item-detail",r.hidden=!0;const u=document.createElement("div");u.className="history-detail-row";const f=document.createElement("span");f.className="history-detail-label",f.textContent="Prompt";const p=document.createElement("span");p.className="history-detail-value history-detail-prompt",p.textContent=e.firstPrompt||"(no prompt captured)",u.append(f,p);const _=document.createElement("div");_.className="history-detail-row";const b=document.createElement("span");b.className="history-detail-label",b.textContent="Path";const S=document.createElement("span");S.className="history-detail-value history-detail-path",S.textContent=m,_.append(b,S);const v=document.createElement("div");return v.className="history-detail-row history-detail-meta",v.textContent=`${c} \xB7 ${s} \xB7 ${e.sessionId.slice(0,8)}`,r.append(u,_,v),l.addEventListener("click",T=>{T.stopPropagation();const w=d.classList.toggle("expanded");r.hidden=!w,l.setAttribute("aria-expanded",w?"true":"false")}),d.append(h,r),d},_HISTORY_INITIAL_COUNT:4,async loadHistorySessions(){const e=document.getElementById("historySessions"),i=document.getElementById("historyList");if(!(!e||!i))try{const s=Array.isArray(this.cases)&&this.cases.length>0?Promise.resolve(this.cases):fetch("/api/cases").then(o=>o.ok?o.json():[]).catch(()=>[]),[n,c]=await Promise.all([this._fetchHistorySessions(30),s]);if(n.length===0){e.style.display="none";return}i.replaceChildren();const m=this._HISTORY_INITIAL_COUNT;for(let o=0;o<Math.min(m,n.length);o++)i.appendChild(this._buildHistoryItem(n[o],c));if(n.length>m){const o=document.createElement("button");o.className="history-show-more",o.textContent=`Show ${n.length-m} more`,o.addEventListener("click",()=>{for(let d=m;d<n.length;d++)i.insertBefore(this._buildHistoryItem(n[d],c),o);o.remove()}),i.appendChild(o)}e.style.display=""}catch(s){console.error("[loadHistorySessions]",s),e.style.display="none"}},async resumeHistorySession(e,i){document.getElementById("runModeMenu")?.classList.remove("active");try{this.terminal.clear(),this.terminal.writeln(`\x1B[1;32m Resuming conversation ${e.slice(0,8)}...\x1B[0m`);const s=i.split("/").pop()||"session";let n=1;for(const[,t]of this.sessions){const a=t.name&&t.name.match(/^w(\d+)-/);if(a){const l=parseInt(a[1]);l>=n&&(n=l+1)}}const c=`w${n}-${s}`,o=(this.cases||[]).find(t=>t.path===i)?.name||i.split("/").pop()||"",d=this.buildEnvOverrides(this.getCaseSettings(o),this.loadAppSettingsFromStorage()),g=await(await fetch("/api/sessions",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({workingDir:i,name:c,resumeSessionId:e,...Object.keys(d).length>0?{envOverrides:d}:{}})})).json();if(!g.success)throw new Error(g.error);const y=g.session.id;await fetch(`/api/sessions/${y}/interactive`,{method:"POST"}),this.terminal.writeln(`\x1B[90m Session ${c} ready\x1B[0m`),await this.selectSession(y),this.terminal.focus()}catch(s){this.terminal.writeln(`\x1B[1;31m Error: ${s.message}\x1B[0m`)}},isTerminalAtBottom(){if(!this.terminal)return!0;const e=this.terminal.buffer.active;return e.viewportY>=e.baseY-2},batchTerminalWrite(e){if(this._isLoadingBuffer){this._loadBufferQueue&&this._loadBufferQueue.push(e);return}if(this.writeFrameScheduled||(this._wasAtBottomBeforeWrite=this.isTerminalAtBottom()),(this.activeSessionId?this.sessions.get(this.activeSessionId):null)?.flickerFilterEnabled??!1){if(e.includes("\x1B[2J")||e.includes("\x1B[H\x1B[J")||e.includes("\x1B[H")&&e.includes("\x1B[?25l")){this.flickerFilterActive=!0,this.flickerFilterBuffer+=e,this.flickerFilterTimeout&&clearTimeout(this.flickerFilterTimeout),this.flickerFilterTimeout=setTimeout(()=>{this.flickerFilterTimeout=null,this.flushFlickerBuffer()},SYNC_WAIT_TIMEOUT_MS);return}if(this.flickerFilterActive){this.flickerFilterBuffer+=e;return}}this.pendingWrites.push(e),this.writeFrameScheduled||(this.writeFrameScheduled=!0,this._safeYield(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1}))},flushFlickerBuffer(){this.flickerFilterBuffer&&(this.pendingWrites.push(this.flickerFilterBuffer),this.flickerFilterBuffer="",this.flickerFilterActive=!1,this.writeFrameScheduled||(this.writeFrameScheduled=!0,this._safeYield(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1})))},_updateLocalEchoState(){const e=this.loadAppSettingsFromStorage(),i=this.activeSessionId?this.sessions.get(this.activeSessionId):null,n=!!((e.localEchoEnabled??MobileDetection.isTouchDevice())&&i);this._localEchoEnabled&&!n&&this._localEchoOverlay?.clear(),this._localEchoEnabled=n,this._localEchoOverlay&&i&&(i.mode==="opencode"?this._localEchoOverlay.setPrompt({type:"custom",offset:3,find:c=>{try{const m=c.buffer.active,o=m.cursorY,d=m.getLine(m.viewportY+o);if(!d)return null;const g=d.translateToString(!0).indexOf("\u2503");return g>=0?{row:o,col:g}:null}catch{return null}}}):i.mode==="shell"?(this._localEchoOverlay.clear(),this._localEchoEnabled=!1):this._localEchoOverlay.setPrompt({type:"character",char:"\u276F",offset:2}))},flushPendingWrites(){if(this.pendingWrites.length===0||!this.terminal)return;const e=performance.now(),i=this.pendingWrites.join("");this.pendingWrites=[];const s=i.length;s>16384&&_crashDiag.log(`FLUSH: ${(s/1024).toFixed(0)}KB`);const n=65536;let c=!1;s<=n?this.terminal.write(i):(this.terminal.write(i.slice(0,n)),this.pendingWrites.push(i.slice(n)),c=!0,this.writeFrameScheduled||(this.writeFrameScheduled=!0,this._safeYield(()=>{this.flushPendingWrites(),this.writeFrameScheduled=!1})));const m=c?n:s,o=performance.now()-e;if((o>100||c)&&console.warn(`[CRASH-DIAG] flushPendingWrites: ${o.toFixed(0)}ms, ${(m/1024).toFixed(0)}KB written${c?", rest deferred":""} (total ${(s/1024).toFixed(0)}KB)`),this._wasAtBottomBeforeWrite&&this.terminal.scrollToBottom(),this._localEchoOverlay?.hasPending&&this._localEchoOverlay.rerender(),this._tabCompletionSessionId&&this._tabCompletionSessionId===this.activeSessionId&&this._localEchoOverlay&&!this._localEchoOverlay.pendingText){const d=this._localEchoOverlay,h=this;this.terminal.write("",()=>{if(!h._tabCompletionSessionId)return;d.resetBufferDetection();const g=d.detectBufferText();g?g===h._tabCompletionBaseText?(d.undoDetection(),h._tabCompletionRetries=(h._tabCompletionRetries||0)+1,h._tabCompletionRetries>60&&(h._tabCompletionSessionId=null,h._tabCompletionRetries=0)):(h._tabCompletionSessionId=null,h._tabCompletionRetries=0,h._tabCompletionBaseText=null,h._tabCompletionFallback&&(clearTimeout(h._tabCompletionFallback),h._tabCompletionFallback=null),d.rerender()):(h._tabCompletionRetries=(h._tabCompletionRetries||0)+1,h._tabCompletionRetries>60&&(h._tabCompletionSessionId=null,h._tabCompletionRetries=0))})}},_safeYield(e){let i=!1;const s=()=>{i||(i=!0,e())};requestAnimationFrame(s),setTimeout(s,50),this._workerYield(s)},_workerYield(e){try{if(this._yieldWorker===void 0){const i="onmessage=()=>setTimeout(()=>postMessage(0),0);",s=new Blob([i],{type:"application/javascript"}),n=URL.createObjectURL(s);this._yieldWorker=new Worker(n),URL.revokeObjectURL(n),this._yieldQueue=[],this._yieldWorker.onmessage=()=>{const c=this._yieldQueue.shift();c&&c()}}if(!this._yieldWorker)return;this._yieldQueue.push(e),this._yieldWorker.postMessage(0)}catch{this._yieldWorker=null}},chunkedTerminalWrite(e,i=TERMINAL_CHUNK_SIZE){const s=++this._chunkedWriteGen;return new Promise(n=>{if(!e||e.length===0){this._finishBufferLoad(),n();return}this._isLoadingBuffer=!0,this._loadBufferQueue=[];const c=e.replace(DEC_SYNC_STRIP_RE,""),m=()=>{this._chunkedWriteGen===s&&this._finishBufferLoad(),n()};if(c.length<=i){this.terminal.write(c),m();return}let o=0;const d=performance.now();let h=0;const g=()=>{if(this._chunkedWriteGen!==s){n();return}if(o>=c.length){const l=performance.now()-d;console.log(`[CRASH-DIAG] chunkedTerminalWrite complete: ${c.length} bytes in ${h} chunks, ${l.toFixed(0)}ms total`),this._safeYield(m);return}const y=performance.now(),t=c.slice(o,o+i);this.terminal.write(t);const a=performance.now()-y;h++,a>50&&console.warn(`[CRASH-DIAG] chunk #${h} write took ${a.toFixed(0)}ms (${t.length} bytes at offset ${o})`),o+=i,this._safeYield(g)};this._safeYield(g)})},_finishBufferLoad(){this._isLoadingBuffer=!1,this._loadBufferQueue=null},clearTerminal(){this.terminal.clear()},async restoreTerminalSize(){if(!this.activeSessionId){this.showToast("No active session","warning");return}const e=this.getTerminalDimensions();if(!e){this.showToast("Could not determine terminal size","error");return}try{await this.sendResize(this.activeSessionId),await fetch(`/api/sessions/${this.activeSessionId}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:"\f"})}),this.showToast(`Terminal restored to ${e.cols}x${e.rows}`,"success")}catch(i){console.error("Failed to restore terminal size:",i),this.showToast("Failed to restore terminal size","error")}},sendPendingCtrlL(e){!this.pendingCtrlL||!this.pendingCtrlL.has(e)||(this.pendingCtrlL.delete(e),e===this.activeSessionId&&this.sendResize(e).then(()=>{fetch(`/api/sessions/${e}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:"\f"})})}))},async copyTerminal(){try{const e=this.terminal.buffer.active;let i="";for(let s=0;s<e.length;s++){const n=e.getLine(s);n&&(i+=n.translateToString(!0)+`
|
|
2
|
+
`)}await navigator.clipboard.writeText(i.replace(/\n+$/,`
|
|
3
|
+
`)),this.showToast("Copied to clipboard","success")}catch{this.showToast("Failed to copy","error")}},increaseFontSize(){const e=this.terminal.options.fontSize||14;this.setFontSize(Math.min(e+2,24))},decreaseFontSize(){const e=this.terminal.options.fontSize||14;this.setFontSize(Math.max(e-2,10))},setFontSize(e){this.terminal.options.fontSize=e,document.getElementById("fontSizeDisplay").textContent=e,this.fitAddon.fit(),localStorage.setItem("codeman-font-size",e),this._localEchoOverlay?.refreshFont()},loadFontSize(){const e=localStorage.getItem("codeman-font-size");if(e){const i=parseInt(e,10);i>=10&&i<=24&&(this.terminal.options.fontSize=i,document.getElementById("fontSizeDisplay").textContent=i)}},getTerminalDimensions(){const s=this.fitAddon?.proposeDimensions();return s?{cols:Math.max(s.cols,40),rows:Math.max(s.rows,10)}:null},async sendResize(e){this.fitAddon&&this.fitAddon.fit();const i=this.getTerminalDimensions();if(i){if(this._lastResizeDims={cols:i.cols,rows:i.rows},this._wsReady&&this._wsSessionId===e)try{this._ws.send(JSON.stringify({t:"z",c:i.cols,r:i.rows}));return}catch{}await fetch(`/api/sessions/${e}/resize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})}},async sendInput(e){this.activeSessionId&&await fetch(`/api/sessions/${this.activeSessionId}/input`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:e,useMux:!0})})},toggleDirInput(){const e=document.querySelector("#dirDisplay").parentElement,i=document.getElementById("dirInput");i.classList.contains("hidden")&&(i.classList.remove("hidden"),e.style.display="none",i.focus())},hideDirInput(){const e=document.querySelector("#dirDisplay").parentElement,i=document.getElementById("dirInput");setTimeout(()=>{i.classList.add("hidden"),e.style.display="";const s=i.value.trim();document.getElementById("dirDisplay").textContent=s||"No directory"},100)}});
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -20,5 +20,17 @@ import type { SessionPort, EventPort, ConfigPort, InfraPort, AuthPort } from '..
|
|
|
20
20
|
* conversation after Claude's first render — losing 100KB+ of legitimate scrollback.
|
|
21
21
|
*/
|
|
22
22
|
export declare function stripInkRedrawBloat(buffer: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Validate image bytes against a declared extension. Sniffs the first ~12 bytes
|
|
25
|
+
* for a known magic-number signature. Defends against polyglots (e.g. HTML or
|
|
26
|
+
* SVG disguised under a `Content-Type: image/png` header) and against simple
|
|
27
|
+
* extension-only spoofing — both the multipart filename and the Content-Type
|
|
28
|
+
* are attacker-controlled, the raw bytes are not.
|
|
29
|
+
*
|
|
30
|
+
* Signatures: https://en.wikipedia.org/wiki/List_of_file_signatures
|
|
31
|
+
*/
|
|
32
|
+
export declare function imageMagicMatchesExt(data: Buffer, ext: string): boolean;
|
|
33
|
+
export declare function consumePasteToken(key: string, now?: number): boolean;
|
|
34
|
+
export declare function _resetPasteRateBuckets(): void;
|
|
23
35
|
export declare function registerSessionRoutes(app: FastifyInstance, ctx: SessionPort & EventPort & ConfigPort & InfraPort & AuthPort): void;
|
|
24
36
|
//# sourceMappingURL=session-routes.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-routes.d.ts","sourceRoot":"","sources":["../../../src/web/routes/session-routes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"session-routes.d.ts","sourceRoot":"","sources":["../../../src/web/routes/session-routes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AA6C1C,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAgBjG;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CA0C1D;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CA0BvE;AAWD,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,GAAE,MAAmB,GAAG,OAAO,CAiBhF;AAGD,wBAAgB,sBAAsB,IAAI,IAAI,CAE7C;AAED,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,WAAW,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,GAAG,QAAQ,GAC/D,IAAI,CAg8CN"}
|