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.
JavaScript agent-collaborationagent-harnessagent-orchestrationagent-teamagent-workflowai-assistantai-automationai-employeeai-productivityai-teamai-workflowautonomous-agentsclaude-code-guiclaude-coworkdesktop-agentlocal-firstmulti-agentmulti-agent-systemself-evolving-agentstask-automation
Parse dashboard JSON tolerantly
wyt303ce2a954d7
diff --git a/src/renderer/modules/utils.js b/src/renderer/modules/utils.jsindex 1c0d14a6..57c01a38 100644--- a/src/renderer/modules/utils.js+++ b/src/renderer/modules/utils.js@@ -114,15 +114,14 @@ function renderMarkdownFull(md) {});// :::dashboard directives — JSON component tree → recursive HTML render.- // Malformed JSON falls back to a code-view block so the user sees the raw- // body instead of a silently-dropped section.+ // `_parseDashboardSpec` is strict-first, then attempts bounded repairs for+ // the brace miscounts LLMs reliably produce. Anything it still can't parse+ // falls back to a code-view block so the user sees the raw body instead of+ // a silently-dropped section.md = md.replace(/:::dashboard\s*\n([\s\S]*?)\n\s*:::/g, (_, body) => {- try {- const spec = JSON.parse(body.trim());- return protect(renderDashboard(spec));- } catch (e) {- return protect(`<pre class="code-view dashboard-parse-error"><code>${escapeHtml(body.trim())}</code></pre>`);- }+ const spec = _parseDashboardSpec(body);+ if (spec !== undefined) return protect(renderDashboard(spec));+ return protect(`<pre class="code-view dashboard-parse-error"><code>${escapeHtml(body.trim())}</code></pre>`);});// Math blocks — protect so markdown phase 2 (emphasis, autolinking, html@@ -395,6 +394,59 @@ function _dbEnum(table, val, dflt) {return (val && Object.prototype.hasOwnProperty.call(table, val)) ? table[val] : dflt;}+// Tolerant parse for a `:::dashboard` JSON body. LLMs (DeepSeek especially,+// when hand-writing a deeply-nested tree) reliably miscount the trailing+// brackets — a single extra `}` after the root close, trailing prose, or a+// truncated tail with closers missing. Strict `JSON.parse` rejects all three+// and the whole dashboard collapses to a raw code-view block. We retry with+// two bounded repairs before giving up:+// 1. drop trailing garbage after the root value's balanced close+// (the common "one extra }" / trailing-prose case)+// 2. append the missing closers for an unclosed (truncated) tree+// Returns the parsed spec, or `undefined` if nothing parses — the caller then+// shows the parse-error fallback so the raw body is never silently dropped.+// The scan is string-aware: brackets inside string values (e.g. an Alert body+// containing `}`) never shift the depth count, and we never trim leading+// garbage (over-repair risk) — only the trailing tail is dropped.+function _parseDashboardSpec(body) {+ const text = String(body == null ? '' : body).trim();+ if (!text) return undefined;+ try { return JSON.parse(text); } catch (_) { /* fall through to repair */ }++ let inString = false;+ let escaped = false;+ let opened = false;+ let balancedEnd = -1;+ const stack = [];+ for (let i = 0; i < text.length; i++) {+ const c = text[i];+ if (inString) {+ if (escaped) escaped = false;+ else if (c === '\\') escaped = true;+ else if (c === '"') inString = false;+ continue;+ }+ if (c === '"') { inString = true; continue; }+ if (c === '{' || c === '[') { stack.push(c); opened = true; }+ else if (c === '}' || c === ']') {+ if (stack.length) stack.pop();+ // First time the root value closes: everything after is trailing tail.+ if (opened && stack.length === 0) { balancedEnd = i + 1; break; }+ }+ }++ // Repair 1: a complete root value followed by trailing garbage (extra `}`).+ if (balancedEnd > 0 && balancedEnd < text.length) {+ try { return JSON.parse(text.slice(0, balancedEnd)); } catch (_) { /* try next */ }+ }+ // Repair 2: unclosed tree — append the closers it still needs, innermost first.+ if (opened && stack.length && !inString) {+ const closers = stack.reverse().map((c) => (c === '{' ? '}' : ']')).join('');+ try { return JSON.parse(text + closers); } catch (_) { /* give up */ }+ }+ return undefined;+}+function renderDashboard(spec) {if (!spec || typeof spec !== 'object') return '';const theme = spec.theme || {};@@ -1089,6 +1141,7 @@ if (typeof module !== 'undefined' && typeof module.exports === 'object') {escapeHtml,renderMarkdown,renderDashboard,+ _parseDashboardSpec,sanitizeMathExpressionForMathJax,_aiSelectNextZIndex,};diff --git a/test/renderer/utils-dashboard.test.ts b/test/renderer/utils-dashboard.test.tsindex 8fba473a..83ab54ec 100644--- a/test/renderer/utils-dashboard.test.ts+++ b/test/renderer/utils-dashboard.test.ts@@ -24,9 +24,10 @@import { describe, it, expect } from 'vitest';// eslint-disable-next-line @typescript-eslint/no-var-requiresconst utils = require('../../src/renderer/modules/utils.js');-const { renderMarkdown, renderDashboard } = utils as {+const { renderMarkdown, renderDashboard, _parseDashboardSpec } = utils as {renderMarkdown: (md: string) => string;renderDashboard: (spec: unknown) => string;+ _parseDashboardSpec: (body: string) => unknown;};function fence(spec: unknown): string {@@ -208,6 +209,49 @@ describe('renderDashboard — defensive / unknown shapes (set A safety)', () =>});});+describe('_parseDashboardSpec — tolerant parse (matching + look-alike non-matching)', () => {+ it('strict-valid JSON parses unchanged', () => {+ expect(_parseDashboardSpec('{"root":{"type":"Separator"}}'))+ .toEqual({ root: { type: 'Separator' } });+ });++ it('repair 1: one extra trailing `}` after the root close', () => {+ expect(_parseDashboardSpec('{"a":1}\n}')).toEqual({ a: 1 });+ });++ it('repair 1: trailing prose after a complete object is dropped', () => {+ expect(_parseDashboardSpec('{"a":1}\n\nhope this helps!')).toEqual({ a: 1 });+ });++ it('repair 2: truncated tail gets the missing closers appended (innermost first)', () => {+ expect(_parseDashboardSpec('{"a":1,"b":[1,2')).toEqual({ a: 1, b: [1, 2] });+ });++ it('string-aware: a `}` inside a string value never shifts the depth count', () => {+ // Without string-awareness the brace in the body would be counted as a+ // close and the real extra `}` would be mis-located.+ expect(_parseDashboardSpec('{"body":"done 100% } ok"}\n}'))+ .toEqual({ body: 'done 100% } ok' });+ });++ it('escaped quote inside a string does not end the string early', () => {+ expect(_parseDashboardSpec('{"q":"a\\"b"}\n}')).toEqual({ q: 'a"b' });+ });++ it('non-matching: leading non-JSON garbage is NOT repaired → undefined', () => {+ expect(_parseDashboardSpec('not { valid: json }')).toBeUndefined();+ });++ it('non-matching: empty / whitespace body → undefined', () => {+ expect(_parseDashboardSpec(' ')).toBeUndefined();+ expect(_parseDashboardSpec('')).toBeUndefined();+ });++ it('non-matching: irreparable junk → undefined (caller shows fallback)', () => {+ expect(_parseDashboardSpec('{{{')).toBeUndefined();+ });+});+describe('renderMarkdown integration — set B (must not break existing surfaces)', () => {it(':::chart-bar still renders alongside :::dashboard', () => {const md = ':::chart-bar\n[{"label":"x","value":5}]\n:::\n\n:::dashboard\n' +@@ -238,6 +282,23 @@ describe('renderMarkdown integration — set B (must not break existing surfacesexpect(html).not.toMatch(/class="dashboard"/);});+ it('extra trailing `}` (the DeepSeek miscount) still renders the dashboard', () => {+ // Reproduces the field report: a structurally complete tree with one+ // surplus `}` after the root close. Strict JSON.parse rejects it; the+ // repair drops the trailing brace so the Alert renders instead of+ // collapsing to a raw code-view block.+ const md = ':::dashboard\n' ++ '{\n"schema_version": 1,\n' ++ '"root": { "type": "Stack", "props": { "gap": "md" }, "children": [\n' ++ '{ "type": "Alert", "props": { "level": "warning", "title": "Funding", "body": "x" } }\n' ++ '] } }\n}\n:::';+ const html = renderMarkdown(md);+ expect(html).not.toContain('dashboard-parse-error');+ expect(html).toMatch(/class="dashboard"/);+ expect(html).toMatch(/data-level="warning"/);+ expect(html).toContain('Funding');+ });+it('unclosed :::dashboard at end of doc → not a dashboard, body kept', () => {const md = ':::dashboard\n{ "root": null }\n';const html = renderMarkdown(md);