@causyne/agent 1.20.124 → 1.20.127
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/causyne.js +1 -1
- package/package.json +4 -4
- package/src/acp/protocol.ts +19 -1
- package/src/acp/server.js +282 -8
- package/src/acp/server.ts +284 -8
- package/src/agent/abortableDelay.js +2 -2
- package/src/agent/abortableDelay.ts +2 -2
- package/src/agent/errorClassifier.js +9 -1
- package/src/agent/errorClassifier.ts +8 -1
- package/src/agentLoop.js +83 -27
- package/src/agentLoop.ts +92 -26
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +30 -3
- package/src/browser/causyneBrowser.ts +33 -3
- package/src/browser/urlPolicy.js +14 -1
- package/src/browser/urlPolicy.ts +12 -1
- package/src/cliMode.js +21 -9
- package/src/cliMode.ts +20 -8
- package/src/commands.js +188 -32
- package/src/commands.ts +209 -32
- package/src/config.js +19 -4
- package/src/config.ts +18 -4
- package/src/core/contextCompactor.js +54 -7
- package/src/core/contextCompactor.ts +63 -7
- package/src/core/goalLoop.js +257 -0
- package/src/core/goalLoop.ts +310 -0
- package/src/entrypoint.js +135 -3
- package/src/entrypoint.ts +136 -3
- package/src/game/causyneCoreBridge.js +686 -0
- package/src/game/causyneCoreBridge.ts +702 -0
- package/src/game/gameLauncher.js +287 -0
- package/src/game/gameLauncher.ts +352 -0
- package/src/game/gameProvisioner.js +425 -0
- package/src/game/gameProvisioner.ts +488 -0
- package/src/headlessCli.js +19 -12
- package/src/headlessCli.ts +22 -8
- package/src/infra/atomicWriter.js +40 -4
- package/src/infra/atomicWriter.ts +36 -4
- package/src/launchIntent.js +73 -3
- package/src/launchIntent.ts +76 -3
- package/src/lsp/lspClient.js +83 -12
- package/src/lsp/lspClient.ts +75 -12
- package/src/mcp/mcpRegistry.js +75 -8
- package/src/mcp/mcpRegistry.ts +70 -7
- package/src/memory/contextBudget.js +2 -0
- package/src/memory/contextBudget.ts +1 -0
- package/src/modelVariants.js +45 -0
- package/src/modelVariants.ts +54 -0
- package/src/outputRecovery.js +3 -2
- package/src/outputRecovery.ts +4 -2
- package/src/permissionPresets.js +327 -0
- package/src/permissionPresets.ts +361 -0
- package/src/providers/antigravity.js +62 -9
- package/src/providers/antigravity.ts +73 -9
- package/src/providers/byokHttp.js +7 -1
- package/src/providers/byokHttp.ts +6 -1
- package/src/providers/byokStream.js +21 -8
- package/src/providers/byokStream.ts +17 -7
- package/src/providers/dashscope-byok.js +21 -5
- package/src/providers/dashscope-byok.ts +29 -11
- package/src/providers/moonshot.js +26 -7
- package/src/providers/moonshot.ts +34 -13
- package/src/providers/muse.js +6 -0
- package/src/providers/muse.ts +8 -0
- package/src/providers/ollama.js +39 -7
- package/src/providers/ollama.ts +36 -7
- package/src/providers/openai-compatible.js +16 -3
- package/src/providers/openai-compatible.ts +13 -3
- package/src/providers/openai.js +62 -4
- package/src/providers/openai.ts +50 -4
- package/src/providers/openrouter.js +19 -4
- package/src/providers/openrouter.ts +21 -4
- package/src/providers/qwen-byok.js +21 -5
- package/src/providers/qwen-byok.ts +29 -11
- package/src/providers/xai.js +26 -10
- package/src/providers/xai.ts +34 -16
- package/src/reporting.js +4 -2
- package/src/reporting.ts +4 -2
- package/src/sandbox.js +34 -0
- package/src/sandbox.ts +43 -0
- package/src/scheduler/scheduler.js +372 -0
- package/src/scheduler/scheduler.ts +433 -0
- package/src/sessionSearchIndex.js +267 -0
- package/src/sessionSearchIndex.ts +311 -0
- package/src/skillsManager.js +2 -3
- package/src/skillsManager.ts +2 -3
- package/src/toolPacks.js +6 -0
- package/src/toolPacks.ts +6 -0
- package/src/tools/applyPatch.js +1 -1
- package/src/tools/applyPatch.ts +1 -1
- package/src/tools/generateMediaViaApi.js +4 -3
- package/src/tools/generateMediaViaApi.ts +4 -3
- package/src/tools/nativeEngine.js +2 -2
- package/src/tools/nativeEngine.ts +2 -2
- package/src/tools/openSourceToolProvisioner.js +2 -2
- package/src/tools/openSourceToolProvisioner.ts +2 -2
- package/src/tools/payloadSanitizer.js +0 -1
- package/src/tools/payloadSanitizer.ts +0 -1
- package/src/tools/pdfReader.js +122 -20
- package/src/tools/pdfReader.ts +111 -20
- package/src/tools/presentDeliverable.js +103 -0
- package/src/tools/presentDeliverable.ts +127 -0
- package/src/tools/ptcRuntime.js +444 -0
- package/src/tools/ptcRuntime.ts +490 -0
- package/src/tools/ptyTerminal.js +1096 -0
- package/src/tools/ptyTerminal.ts +1231 -0
- package/src/tools/registry.js +21 -12
- package/src/tools/registry.ts +21 -12
- package/src/tools/toolOutputStore.js +26 -4
- package/src/tools/toolOutputStore.ts +22 -4
- package/src/tools/webFetch.js +158 -11
- package/src/tools/webFetch.ts +145 -11
- package/src/tools/webSearch.js +123 -36
- package/src/tools/webSearch.ts +124 -33
- package/src/tui/banner.js +5 -1
- package/src/tui/banner.ts +6 -1
- package/src/tui/chatRenderer.js +1 -2
- package/src/tui/chatRenderer.ts +1 -2
- package/src/tui/completion.js +1 -0
- package/src/tui/completion.ts +1 -0
- package/src/tui/composerControls.js +111 -27
- package/src/tui/composerControls.ts +124 -28
- package/src/tui/dialogs.js +3 -16
- package/src/tui/dialogs.ts +3 -16
- package/src/tui/home.js +1 -0
- package/src/tui/home.ts +1 -0
- package/src/tui/invocation.js +1 -2
- package/src/tui/invocation.ts +1 -2
- package/src/tui/modelPicker.js +22 -12
- package/src/tui/modelPicker.ts +22 -12
- package/src/tui/overlay.js +2 -5
- package/src/tui/overlay.ts +2 -5
- package/src/tui/prompt.js +6 -4
- package/src/tui/prompt.ts +6 -4
- package/src/tui/selectList.js +4 -12
- package/src/tui/selectList.ts +4 -12
- package/src/tui/setup.js +22 -9
- package/src/tui/setup.ts +26 -9
- package/src/tui/statusLine.ts +2 -0
- package/src/web/client/browserPanel.js +702 -0
- package/src/web/client/browserPanel.ts +663 -0
- package/src/web/client/chatStream.js +309 -0
- package/src/web/client/chatStream.ts +289 -0
- package/src/web/client/cliPanel.js +165 -0
- package/src/web/client/cliPanel.ts +171 -0
- package/src/web/client/composer.js +204 -0
- package/src/web/client/composer.ts +209 -0
- package/src/web/client/diffPanel.js +176 -0
- package/src/web/client/diffPanel.ts +203 -0
- package/src/web/client/dom.js +220 -0
- package/src/web/client/dom.ts +221 -0
- package/src/web/client/filesAndCanvas.js +231 -0
- package/src/web/client/filesAndCanvas.ts +238 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +301 -0
- package/src/web/client/main.ts +368 -0
- package/src/web/client/markdown.js +364 -0
- package/src/web/client/markdown.ts +373 -0
- package/src/web/client/modelPicker.js +455 -0
- package/src/web/client/modelPicker.ts +451 -0
- package/src/web/client/permissionPresetsUi.js +307 -0
- package/src/web/client/permissionPresetsUi.ts +333 -0
- package/src/web/client/sessions.js +353 -0
- package/src/web/client/sessions.ts +372 -0
- package/src/web/client/settingsModal.js +191 -0
- package/src/web/client/settingsModal.ts +176 -0
- package/src/web/client/state.js +250 -0
- package/src/web/client/state.ts +233 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +54 -0
- package/src/web/client/studioLayout.ts +60 -0
- package/src/web/client/studioShell.js +327 -0
- package/src/web/client/studioShell.ts +340 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +357 -0
- package/src/web/client/terminalPanel.ts +345 -0
- package/src/web/client/turnView.js +934 -0
- package/src/web/client/turnView.ts +981 -0
- package/src/web/client/types.js +1 -0
- package/src/web/client/types.ts +319 -0
- package/src/web/client/validators.js +106 -0
- package/src/web/client/validators.ts +96 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +355 -0
- package/src/web/server/routesBrowser.ts +386 -0
- package/src/web/server/routesChat.js +304 -0
- package/src/web/server/routesChat.ts +347 -0
- package/src/web/server/routesCore.js +472 -0
- package/src/web/server/routesCore.ts +522 -0
- package/src/web/server/routesTerminal.js +162 -0
- package/src/web/server/routesTerminal.ts +182 -0
- package/src/web/server/routesWorkspace.js +510 -0
- package/src/web/server/routesWorkspace.ts +546 -0
- package/src/web/server/security.js +98 -0
- package/src/web/server/security.ts +108 -0
- package/src/web/server/sessionTranscript.js +586 -0
- package/src/web/server/sessionTranscript.ts +566 -0
- package/src/web/server/staticAssets.js +80 -0
- package/src/web/server/staticAssets.ts +91 -0
- package/src/web/server/types.js +1 -0
- package/src/web/server/types.ts +135 -0
- package/src/web/server/workspaceFiles.js +368 -0
- package/src/web/server/workspaceFiles.ts +363 -0
- package/src/web/skybridgeServer.js +76 -2449
- package/src/web/skybridgeServer.ts +118 -2600
- package/src/web/skybridgeUi.js +16 -450
- package/src/web/skybridgeUi.ts +16 -454
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +436 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +723 -0
- package/src/web/styles/thread.css +221 -0
- package/src/web/styles/tokens.css +94 -0
- package/src/web/ui/brandMark.js +73 -0
- package/src/web/ui/brandMark.ts +82 -0
- package/src/web/ui/modalsTemplate.js +139 -0
- package/src/web/ui/modalsTemplate.ts +139 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +258 -0
- package/src/web/ui/studioTemplate.ts +258 -0
- package/src/web/ui/workspaceTemplate.js +72 -0
- package/src/web/ui/workspaceTemplate.ts +73 -0
- package/src/worktreeManager.js +16 -1
- package/src/worktreeManager.ts +22 -1
- package/src/web/skybridgeClientScript.js +0 -3951
- package/src/web/skybridgeClientScript.ts +0 -3952
- package/src/web/skybridgeStyles.js +0 -827
- package/src/web/skybridgeStyles.ts +0 -828
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { copyText } from './dom.js';
|
|
2
|
+
import { state } from './state.js';
|
|
3
|
+
import { parseWorkspaceFileHref } from './validators.js';
|
|
4
|
+
import type { WorkspaceFileHrefInfo } from './types.js';
|
|
5
|
+
|
|
6
|
+
function toSafeExternalHref(raw: string): string | null {
|
|
7
|
+
const trimmed = String(raw || '').trim();
|
|
8
|
+
if (!trimmed) return null;
|
|
9
|
+
let parsed: URL;
|
|
10
|
+
try {
|
|
11
|
+
parsed = new URL(trimmed);
|
|
12
|
+
} catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') return null;
|
|
16
|
+
const normalized = parsed.href;
|
|
17
|
+
if (!normalized.startsWith('https://') && !normalized.startsWith('http://')) return null;
|
|
18
|
+
try {
|
|
19
|
+
return encodeURI(decodeURI(normalized));
|
|
20
|
+
} catch {
|
|
21
|
+
return encodeURI(normalized);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function createSafeExternalAnchor(rawHref: string, labelText: string): HTMLAnchorElement | null {
|
|
26
|
+
const safeHref = toSafeExternalHref(rawHref);
|
|
27
|
+
if (!safeHref) return null;
|
|
28
|
+
const a = document.createElement('a');
|
|
29
|
+
if (safeHref.startsWith('https://')) {
|
|
30
|
+
a.href = `https://${safeHref.slice(8)}`;
|
|
31
|
+
} else if (safeHref.startsWith('http://')) {
|
|
32
|
+
a.href = `http://${safeHref.slice(7)}`;
|
|
33
|
+
} else {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
a.target = '_blank';
|
|
37
|
+
a.rel = 'noopener noreferrer';
|
|
38
|
+
a.textContent = labelText;
|
|
39
|
+
return a;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface MarkdownCallbacks {
|
|
43
|
+
openFile(relPath: string): void | Promise<void>;
|
|
44
|
+
openInCanvas(suggestedPath: string, content: string, preferPreview: boolean): void;
|
|
45
|
+
openBrowserArtifact(title: string, htmlOrSvg: string): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let markdownCallbacks: MarkdownCallbacks = {
|
|
49
|
+
openFile: () => {},
|
|
50
|
+
openInCanvas: () => {},
|
|
51
|
+
openBrowserArtifact: () => {}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export function configureMarkdownCallbacks(cbs: MarkdownCallbacks): void {
|
|
55
|
+
markdownCallbacks = cbs;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createInlineFileChip(label: string, fileInfo: WorkspaceFileHrefInfo): HTMLButtonElement {
|
|
59
|
+
const btn = document.createElement('button');
|
|
60
|
+
btn.type = 'button';
|
|
61
|
+
btn.className = `md-file-chip${fileInfo.relPath ? '' : ' is-static'}`;
|
|
62
|
+
btn.title = fileInfo.fullPath + fileInfo.lineBadge;
|
|
63
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
64
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
65
|
+
const p1 = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
66
|
+
p1.setAttribute('d', 'M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8Z');
|
|
67
|
+
const p2 = document.createElementNS('http://www.w3.org/2000/svg', 'polyline');
|
|
68
|
+
p2.setAttribute('points', '14 2 14 8 20 8');
|
|
69
|
+
svg.appendChild(p1);
|
|
70
|
+
svg.appendChild(p2);
|
|
71
|
+
btn.appendChild(svg);
|
|
72
|
+
|
|
73
|
+
const nameEl = document.createElement('span');
|
|
74
|
+
nameEl.className = 'md-file-chip-name';
|
|
75
|
+
const cleanLabel = String(label || '').replace(/^`|`$/g, '').trim();
|
|
76
|
+
const showLabel = (cleanLabel && !cleanLabel.startsWith('/Users/') && !cleanLabel.startsWith('/home/'))
|
|
77
|
+
? cleanLabel
|
|
78
|
+
: fileInfo.display;
|
|
79
|
+
nameEl.textContent = showLabel;
|
|
80
|
+
btn.appendChild(nameEl);
|
|
81
|
+
|
|
82
|
+
if (fileInfo.lineBadge) {
|
|
83
|
+
const ln = document.createElement('span');
|
|
84
|
+
ln.className = 'md-file-chip-line';
|
|
85
|
+
ln.textContent = fileInfo.lineBadge;
|
|
86
|
+
btn.appendChild(ln);
|
|
87
|
+
}
|
|
88
|
+
if (fileInfo.relPath) {
|
|
89
|
+
const targetRel = fileInfo.relPath;
|
|
90
|
+
btn.addEventListener('click', () => { void markdownCallbacks.openFile(targetRel); });
|
|
91
|
+
}
|
|
92
|
+
return btn;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function appendInlineNodes(parent: HTMLElement, text: string): void {
|
|
96
|
+
let i = 0;
|
|
97
|
+
const len = text.length;
|
|
98
|
+
let buf = '';
|
|
99
|
+
const flush = (): void => {
|
|
100
|
+
if (buf) {
|
|
101
|
+
parent.appendChild(document.createTextNode(buf));
|
|
102
|
+
buf = '';
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
while (i < len) {
|
|
106
|
+
if (text[i] === '`') {
|
|
107
|
+
const end = text.indexOf('`', i + 1);
|
|
108
|
+
if (end > i + 1) {
|
|
109
|
+
flush();
|
|
110
|
+
const c = document.createElement('code');
|
|
111
|
+
c.className = 'inline-code';
|
|
112
|
+
c.textContent = text.slice(i + 1, end);
|
|
113
|
+
parent.appendChild(c);
|
|
114
|
+
i = end + 1;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (text[i] === '*' && text[i + 1] === '*') {
|
|
119
|
+
const end = text.indexOf('**', i + 2);
|
|
120
|
+
if (end > i + 2) {
|
|
121
|
+
flush();
|
|
122
|
+
const s = document.createElement('strong');
|
|
123
|
+
appendInlineNodes(s, text.slice(i + 2, end));
|
|
124
|
+
parent.appendChild(s);
|
|
125
|
+
i = end + 2;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (text[i] === '*' && text[i + 1] !== ' ' && text[i + 1] !== '*') {
|
|
130
|
+
const end = text.indexOf('*', i + 1);
|
|
131
|
+
if (end > i + 1 && text[end - 1] !== ' ') {
|
|
132
|
+
flush();
|
|
133
|
+
const em = document.createElement('em');
|
|
134
|
+
em.textContent = text.slice(i + 1, end);
|
|
135
|
+
parent.appendChild(em);
|
|
136
|
+
i = end + 1;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (text[i] === '[') {
|
|
141
|
+
const mid = text.indexOf('](', i + 1);
|
|
142
|
+
const end = mid > -1 ? text.indexOf(')', mid + 2) : -1;
|
|
143
|
+
if (mid > i + 1 && end > mid + 2) {
|
|
144
|
+
const label = text.slice(i + 1, mid);
|
|
145
|
+
const href = text.slice(mid + 2, end).trim();
|
|
146
|
+
const extAnchor = createSafeExternalAnchor(href, label.replace(/^`|`$/g, ''));
|
|
147
|
+
if (extAnchor) {
|
|
148
|
+
flush();
|
|
149
|
+
parent.appendChild(extAnchor);
|
|
150
|
+
i = end + 1;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const fileInfo = parseWorkspaceFileHref(href, state.workspaceRoot);
|
|
154
|
+
if (fileInfo) {
|
|
155
|
+
flush();
|
|
156
|
+
parent.appendChild(createInlineFileChip(label, fileInfo));
|
|
157
|
+
i = end + 1;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (text[i] === 'h' && (text.startsWith('http://', i) || text.startsWith('https://', i))) {
|
|
163
|
+
let j = i;
|
|
164
|
+
while (j < len && !/[\s<>"'`\[\]]/.test(text[j] || '')) j += 1;
|
|
165
|
+
let rawUrl = text.slice(i, j);
|
|
166
|
+
let trail = '';
|
|
167
|
+
while (rawUrl.length > 0 && /[.,;:!?)]/.test(rawUrl[rawUrl.length - 1] || '')) {
|
|
168
|
+
trail = (rawUrl[rawUrl.length - 1] || '') + trail;
|
|
169
|
+
rawUrl = rawUrl.slice(0, -1);
|
|
170
|
+
}
|
|
171
|
+
const rawAnchor = createSafeExternalAnchor(rawUrl, rawUrl);
|
|
172
|
+
if (rawAnchor) {
|
|
173
|
+
flush();
|
|
174
|
+
parent.appendChild(rawAnchor);
|
|
175
|
+
if (trail) buf += trail;
|
|
176
|
+
i = j;
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
buf += text[i] || '';
|
|
181
|
+
i += 1;
|
|
182
|
+
}
|
|
183
|
+
flush();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function createCodeCard(lang: string, codeText: string): HTMLDivElement {
|
|
187
|
+
const normLang = String(lang || '').toLowerCase();
|
|
188
|
+
const card = document.createElement('div');
|
|
189
|
+
card.className = 'code-card';
|
|
190
|
+
const head = document.createElement('div');
|
|
191
|
+
head.className = 'code-head';
|
|
192
|
+
const langSpan = document.createElement('span');
|
|
193
|
+
const lineCount = codeText ? codeText.split('\n').length : 0;
|
|
194
|
+
langSpan.textContent = `${lang || 'code'} · ${lineCount} lines`;
|
|
195
|
+
const actions = document.createElement('div');
|
|
196
|
+
actions.className = 'code-head-actions';
|
|
197
|
+
|
|
198
|
+
if (normLang === 'html' || normLang === 'svg') {
|
|
199
|
+
const prevBtn = document.createElement('button');
|
|
200
|
+
prevBtn.type = 'button';
|
|
201
|
+
prevBtn.className = 'code-btn';
|
|
202
|
+
prevBtn.textContent = 'Live Preview';
|
|
203
|
+
prevBtn.addEventListener('click', () => markdownCallbacks.openBrowserArtifact(`snippet.${normLang}`, codeText));
|
|
204
|
+
actions.appendChild(prevBtn);
|
|
205
|
+
}
|
|
206
|
+
const canvasBtn = document.createElement('button');
|
|
207
|
+
canvasBtn.type = 'button';
|
|
208
|
+
canvasBtn.className = 'code-btn';
|
|
209
|
+
canvasBtn.textContent = 'Open in Canvas';
|
|
210
|
+
canvasBtn.addEventListener('click', () => {
|
|
211
|
+
const ext = normLang && /^[a-z0-9]{1,8}$/.test(normLang) ? normLang : 'txt';
|
|
212
|
+
markdownCallbacks.openInCanvas(
|
|
213
|
+
`snippet.${ext}`,
|
|
214
|
+
codeText,
|
|
215
|
+
normLang === 'md' || normLang === 'markdown' || normLang === 'html' || normLang === 'svg'
|
|
216
|
+
);
|
|
217
|
+
});
|
|
218
|
+
const copyBtn = document.createElement('button');
|
|
219
|
+
copyBtn.type = 'button';
|
|
220
|
+
copyBtn.className = 'code-btn';
|
|
221
|
+
copyBtn.textContent = 'Copy';
|
|
222
|
+
copyBtn.addEventListener('click', () => copyText(codeText, copyBtn));
|
|
223
|
+
actions.appendChild(canvasBtn);
|
|
224
|
+
actions.appendChild(copyBtn);
|
|
225
|
+
head.appendChild(langSpan);
|
|
226
|
+
head.appendChild(actions);
|
|
227
|
+
|
|
228
|
+
const pre = document.createElement('pre');
|
|
229
|
+
pre.className = 'code-pre';
|
|
230
|
+
const codeEl = document.createElement('code');
|
|
231
|
+
codeEl.textContent = codeText;
|
|
232
|
+
pre.appendChild(codeEl);
|
|
233
|
+
card.appendChild(head);
|
|
234
|
+
card.appendChild(pre);
|
|
235
|
+
return card;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function parseTableRow(line: string): string[] {
|
|
239
|
+
let trimmed = line.trim();
|
|
240
|
+
if (trimmed.startsWith('|')) trimmed = trimmed.slice(1);
|
|
241
|
+
if (trimmed.endsWith('|')) trimmed = trimmed.slice(0, -1);
|
|
242
|
+
return trimmed.split('|').map((c) => c.trim());
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function isTableDivider(line: string): boolean {
|
|
246
|
+
const cells = parseTableRow(line);
|
|
247
|
+
if (cells.length === 0) return false;
|
|
248
|
+
for (const c of cells) {
|
|
249
|
+
if (!/^:?-{2,}:?$/.test(c)) return false;
|
|
250
|
+
}
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function renderMarkdownInto(container: HTMLElement, source: string, streaming: boolean): void {
|
|
255
|
+
container.textContent = '';
|
|
256
|
+
const lines = String(source || '').replace(/\r\n/g, '\n').split('\n');
|
|
257
|
+
let i = 0;
|
|
258
|
+
while (i < lines.length) {
|
|
259
|
+
const line = lines[i] ?? '';
|
|
260
|
+
if (!line.trim()) { i += 1; continue; }
|
|
261
|
+
if (line.trimStart().startsWith('```')) {
|
|
262
|
+
const lang = line.trimStart().slice(3).trim().split(/\s+/)[0] || '';
|
|
263
|
+
const codeLines: string[] = [];
|
|
264
|
+
i += 1;
|
|
265
|
+
while (i < lines.length && !(lines[i] ?? '').trimStart().startsWith('```')) {
|
|
266
|
+
codeLines.push(lines[i] ?? '');
|
|
267
|
+
i += 1;
|
|
268
|
+
}
|
|
269
|
+
if (i < lines.length) i += 1;
|
|
270
|
+
container.appendChild(createCodeCard(lang, codeLines.join('\n')));
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (/^#{1,4}\s+/.test(line)) {
|
|
274
|
+
const match = line.match(/^#+/);
|
|
275
|
+
const level = Math.min(4, match ? match[0].length : 1);
|
|
276
|
+
const h = document.createElement(`h${level}`);
|
|
277
|
+
appendInlineNodes(h, line.slice(level).trim());
|
|
278
|
+
container.appendChild(h);
|
|
279
|
+
i += 1;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (/^\s*([-*_])\1{2,}\s*$/.test(line)) {
|
|
283
|
+
container.appendChild(document.createElement('hr'));
|
|
284
|
+
i += 1;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (line.trimStart().startsWith('> ')) {
|
|
288
|
+
const bq = document.createElement('blockquote');
|
|
289
|
+
const parts: string[] = [];
|
|
290
|
+
while (i < lines.length && (lines[i] ?? '').trimStart().startsWith('> ')) {
|
|
291
|
+
parts.push((lines[i] ?? '').trimStart().slice(2));
|
|
292
|
+
i += 1;
|
|
293
|
+
}
|
|
294
|
+
appendInlineNodes(bq, parts.join(' '));
|
|
295
|
+
container.appendChild(bq);
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (line.includes('|') && i + 1 < lines.length && isTableDivider(lines[i + 1] ?? '')) {
|
|
299
|
+
const wrap = document.createElement('div');
|
|
300
|
+
wrap.className = 'md-table-wrap';
|
|
301
|
+
const table = document.createElement('table');
|
|
302
|
+
table.className = 'md-table';
|
|
303
|
+
const thead = document.createElement('thead');
|
|
304
|
+
const hr = document.createElement('tr');
|
|
305
|
+
for (const hCell of parseTableRow(line)) {
|
|
306
|
+
const th = document.createElement('th');
|
|
307
|
+
appendInlineNodes(th, hCell);
|
|
308
|
+
hr.appendChild(th);
|
|
309
|
+
}
|
|
310
|
+
thead.appendChild(hr);
|
|
311
|
+
table.appendChild(thead);
|
|
312
|
+
const tbody = document.createElement('tbody');
|
|
313
|
+
i += 2;
|
|
314
|
+
while (i < lines.length && (lines[i] ?? '').includes('|') && (lines[i] ?? '').trim()) {
|
|
315
|
+
const tr = document.createElement('tr');
|
|
316
|
+
for (const bCell of parseTableRow(lines[i] ?? '')) {
|
|
317
|
+
const td = document.createElement('td');
|
|
318
|
+
appendInlineNodes(td, bCell);
|
|
319
|
+
tr.appendChild(td);
|
|
320
|
+
}
|
|
321
|
+
tbody.appendChild(tr);
|
|
322
|
+
i += 1;
|
|
323
|
+
}
|
|
324
|
+
table.appendChild(tbody);
|
|
325
|
+
wrap.appendChild(table);
|
|
326
|
+
container.appendChild(wrap);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
if (/^\s*[-*+]\s+/.test(line)) {
|
|
330
|
+
const ul = document.createElement('ul');
|
|
331
|
+
while (i < lines.length && /^\s*[-*+]\s+/.test(lines[i] ?? '')) {
|
|
332
|
+
const li = document.createElement('li');
|
|
333
|
+
appendInlineNodes(li, (lines[i] ?? '').replace(/^\s*[-*+]\s+/, ''));
|
|
334
|
+
ul.appendChild(li);
|
|
335
|
+
i += 1;
|
|
336
|
+
}
|
|
337
|
+
container.appendChild(ul);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (/^\s*\d+\.\s+/.test(line)) {
|
|
341
|
+
const ol = document.createElement('ol');
|
|
342
|
+
while (i < lines.length && /^\s*\d+\.\s+/.test(lines[i] ?? '')) {
|
|
343
|
+
const li = document.createElement('li');
|
|
344
|
+
appendInlineNodes(li, (lines[i] ?? '').replace(/^\s*\d+\.\s+/, ''));
|
|
345
|
+
ol.appendChild(li);
|
|
346
|
+
i += 1;
|
|
347
|
+
}
|
|
348
|
+
container.appendChild(ol);
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const paraLines: string[] = [];
|
|
352
|
+
while (
|
|
353
|
+
i < lines.length
|
|
354
|
+
&& (lines[i] ?? '').trim()
|
|
355
|
+
&& !(lines[i] ?? '').trimStart().startsWith('```')
|
|
356
|
+
&& !/^#{1,4}\s+/.test(lines[i] ?? '')
|
|
357
|
+
&& !/^\s*[-*+]\s+/.test(lines[i] ?? '')
|
|
358
|
+
&& !/^\s*\d+\.\s+/.test(lines[i] ?? '')
|
|
359
|
+
&& !(lines[i] ?? '').trimStart().startsWith('> ')
|
|
360
|
+
) {
|
|
361
|
+
paraLines.push(lines[i] ?? '');
|
|
362
|
+
i += 1;
|
|
363
|
+
}
|
|
364
|
+
const p = document.createElement('p');
|
|
365
|
+
appendInlineNodes(p, paraLines.join(' '));
|
|
366
|
+
container.appendChild(p);
|
|
367
|
+
}
|
|
368
|
+
if (streaming) {
|
|
369
|
+
const cursor = document.createElement('span');
|
|
370
|
+
cursor.className = 'streaming-cursor';
|
|
371
|
+
container.appendChild(cursor);
|
|
372
|
+
}
|
|
373
|
+
}
|