Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Preserve dashboard source when rendering fails

wyte4cfc9ecb94f
diff --git a/src/renderer/modules/utils.js b/src/renderer/modules/utils.js
index 06f1c665..a0621416 100644
--- a/src/renderer/modules/utils.js
+++ b/src/renderer/modules/utils.js
@@ -289,7 +289,7 @@ function _renderMarkdownFullInner(md, blockCache) {
const placeholder = _protectedDashboardPlaceholder(body, protectedBlocks);
if (placeholder) return placeholder;
const spec = _parseDashboardSpec(body);
- if (spec !== undefined) return protect(renderDashboard(spec));
+ if (spec !== undefined) return protect(_renderDashboardOrSource(spec, body));
return protect(`<pre class="code-view dashboard-parse-error"><code>${escapeHtml(body.trim())}</code></pre>`);
});
@@ -707,7 +707,7 @@ function _renderDashboardFromJsonBlock(lang, code) {
if (body == null) return '';
const spec = _parseDashboardSpec(body);
if (!_looksLikeDashboardSpec(spec)) return '';
- return renderDashboard(spec);
+ return _renderDashboardOrSource(spec, body);
}
function _protectedDashboardPlaceholder(body, protectedBlocks) {
@@ -765,7 +765,7 @@ function _replaceStandaloneDashboardJsonBlocks(md, protect) {
startRe.lastIndex = start + 1;
continue;
}
- out += text.slice(cursor, start) + protect(renderDashboard(spec));
+ out += text.slice(cursor, start) + protect(_renderDashboardOrSource(spec, candidate));
cursor = end;
startRe.lastIndex = end;
}
@@ -1011,6 +1011,17 @@ function renderDashboard(spec) {
return `<div class="dashboard" data-theme-color="${themeColor}" data-theme-style="${themeStyle}">${inner}</div>`;
}
+function _renderDashboardOrSource(spec, source) {
+ try {
+ return renderDashboard(spec);
+ } catch (error) {
+ if (!(error instanceof RangeError || error instanceof TypeError)) throw error;
+ // Deep trees and uncoercible JSON props can fail during rendering. Retain
+ // their complete source, as with parse failures, without losing the reply.
+ return `<pre class="code-view dashboard-render-error"><code>${escapeHtml(source.trim())}</code></pre>`;
+ }
+}
+
function _renderDbNode(node) {
if (!node || typeof node !== 'object') return '';
const props = (node.props && typeof node.props === 'object') ? node.props : {};
diff --git a/test/renderer/utils-streaming-blocks.test.ts b/test/renderer/utils-streaming-blocks.test.ts
index a5dc2d1a..7c933b06 100644
--- a/test/renderer/utils-streaming-blocks.test.ts
+++ b/test/renderer/utils-streaming-blocks.test.ts
@@ -19,6 +19,31 @@ const scenarios = [
];
describe('streamed Markdown block compatibility', () => {
+ it.each(['directive', 'json-fence', 'standalone'])('keeps the source and surrounding reply when a %s dashboard cannot be rendered', kind => {
+ const sources = [
+ '{"root":' + '{"type":"Stack","children":['.repeat(10_000)
+ + '{"type":"Markdown","props":{"text":"<deep & leaf>"}}' + ']}'.repeat(10_000) + '}',
+ '{"root":{"type":"Grid","props":{"columns":{"toString":null,"valueOf":null}}}}',
+ '{"root":{"type":"Metric","props":{"value":{"toString":null}}}}',
+ ];
+ for (const source of sources) {
+ const body = kind === 'directive' ? `:::dashboard\n${source}\n:::`
+ : kind === 'json-fence' ? `\`\`\`json\n${source}\n\`\`\`` : source;
+ const markdown = `Before\n\n${body}\n\nAfter`;
+ const cache: any = {};
+ for (const blockCache of [undefined, cache]) {
+ let html = '';
+ expect(() => { html = renderMarkdown(markdown, blockCache); }).not.toThrow();
+ expect(html).toContain('<p>Before</p>');
+ expect(html).toContain('<p>After</p>');
+ expect(html).toContain('dashboard-render-error');
+ expect(html).toContain(source.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'));
+ if (blockCache) expect(blockCache.blocks.join('')).toBe(html);
+ }
+ expect(renderMarkdown('Recovered `$x$` and $y$', cache)).toBe(renderMarkdown('Recovered `$x$` and $y$'));
+ }
+ });
+
it.each(scenarios)('preserves %s through every split and replacement', (_name, source) => {
const cache: any = {};
for (let end = 1; end <= source.length; end++) {