@causyne/agent 1.20.124 → 1.20.128

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