A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Fold a card message to a real preview, not an empty fence
Fei24111f8d0795c272
diff --git a/ds-bot/client.js b/ds-bot/client.jsindex 0c7d9bf8..6836ddea 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -6672,21 +6672,43 @@ function moreLines(text, max) {function foldPreview(text, limit = FOLD_AT, markdown = true) {if (text.length <= limit.chars && !moreLines(text, limit.lines)) return null;let head = text.split("\n", PREVIEW_LINES).join("\n");+ head = head.replace(/^(\s*\n)+/, "");if (head.length > PREVIEW_CHARS) {const cut = head.lastIndexOf("\n", PREVIEW_CHARS);const end = cut > 0 ? cut : PREVIEW_CHARS;const code = head.charCodeAt(end - 1);head = head.slice(0, code >= 55296 && code <= 56319 ? end - 1 : end);}+ if (head.trim() === "") return null;if (!markdown) return head;let open = null;- for (const line of head.split("\n")) {- const marker = FENCE.exec(line)?.[1];+ let openLine = -1;+ let lines = head.split("\n");+ for (let i = 0; i < lines.length; i += 1) {+ const marker = FENCE.exec(lines[i])?.[1];if (marker === void 0) continue;- if (open === null) open = marker;- else if (marker[0] === open[0] && marker.length >= open.length) open = null;+ if (open === null) {+ open = marker;+ openLine = i;+ } else if (marker[0] === open[0] && marker.length >= open.length) open = null;}- return open === null ? head : `${head}+ if (open !== null && lines.slice(openLine + 1).join("").trim() === "") {+ head = text.replace(/^(\s*\n)+/, "").slice(0, PREVIEW_CHARS);+ lines = head.split("\n");+ open = null;+ openLine = -1;+ for (let i = 0; i < lines.length; i += 1) {+ const marker = FENCE.exec(lines[i])?.[1];+ if (marker === void 0) continue;+ if (open === null) {+ open = marker;+ openLine = i;+ } else if (marker[0] === open[0] && marker.length >= open.length) open = null;+ }+ }+ if (open === null) return head;+ lines[openLine] = open;+ return `${lines.join("\n")}${open}`;}var stripLongNote = (text) => text.replace(/^\[A long message: \d+ characters\.[^\]\n]*\]\n/, "");diff --git a/ds-bot/src/client/fold.js b/ds-bot/src/client/fold.jsindex 9bc2eb6d..ad452dd0 100644--- a/ds-bot/src/client/fold.js+++ b/ds-bot/src/client/fold.js@@ -26,21 +26,46 @@ function moreLines(text, max) {export function foldPreview(text, limit = FOLD_AT, markdown = true) {if (text.length <= limit.chars && !moreLines(text, limit.lines)) return nulllet head = text.split('\n', PREVIEW_LINES).join('\n')+ // Blank lines first: the preview starts at the first visible character.+ head = head.replace(/^(\s*\n)+/, '')if (head.length > PREVIEW_CHARS) {const cut = head.lastIndexOf('\n', PREVIEW_CHARS)const end = cut > 0 ? cut : PREVIEW_CHARSconst code = head.charCodeAt(end - 1)head = head.slice(0, code >= 0xd800 && code <= 0xdbff ? end - 1 : end)}+ if (head.trim() === '') return nullif (!markdown) return headlet open = null- for (const line of head.split('\n')) {- const marker = FENCE.exec(line)?.[1]+ let openLine = -1+ let lines = head.split('\n')+ for (let i = 0; i < lines.length; i += 1) {+ const marker = FENCE.exec(lines[i])?.[1]if (marker === undefined) continue- if (open === null) open = marker+ if (open === null) { open = marker; openLine = i }else if (marker[0] === open[0] && marker.length >= open.length) open = null}- return open === null ? head : `${head}\n${open}`+ if (open !== null && lines.slice(openLine + 1).join('').trim() === '') {+ // The cut kept only the fence marker (a card opening like ```dsh-ui on+ // its own line): an empty fence renders nothing, so pull the start of+ // the fenced text in as the preview instead.+ head = text.replace(/^(\s*\n)+/, '').slice(0, PREVIEW_CHARS)+ lines = head.split('\n')+ open = null+ openLine = -1+ for (let i = 0; i < lines.length; i += 1) {+ const marker = FENCE.exec(lines[i])?.[1]+ if (marker === undefined) continue+ if (open === null) { open = marker; openLine = i }+ else if (marker[0] === open[0] && marker.length >= open.length) open = null+ }+ }+ if (open === null) return head+ // A fence the preview cut open loses its language tag: a downstream+ // renderer (dsh-ui cards) would otherwise claim the half message and the+ // expansion remounts its subtree looking like a no-op.+ lines[openLine] = open+ return `${lines.join('\n')}\n${open}`}// The line a long message that went as a file starts with (the host's attachLong) isdiff --git a/ds-bot/test/fold.test.js b/ds-bot/test/fold.test.jsindex 520a8219..a6856134 100644--- a/ds-bot/test/fold.test.js+++ b/ds-bot/test/fold.test.js@@ -49,8 +49,10 @@ test('a text folds past 1,500 characters or 25 lines, and shows its first ten li})test('the preview closes a code fence it cuts open, in Markdown only', () => {+ // A fence the cut opened loses its language tag, so a downstream renderer+ // (dsh-ui cards) does not claim a half message.const code = `Here:\n\`\`\`js\n${lines(30)}\n\`\`\``- assert.equal(foldPreview(code), `Here:\n\`\`\`js\n${lines(8)}\n\`\`\``)+ assert.equal(foldPreview(code), `Here:\n\`\`\`\n${lines(8)}\n\`\`\``)assert.equal(foldPreview(code, FOLD_AT, false), `Here:\n\`\`\`js\n${lines(8)}`)const tilde = `~~~~\n\`\`\`\n${lines(30)}`assert.equal(foldPreview(tilde), `~~~~\n\`\`\`\n${lines(8)}\n~~~~`)@@ -58,6 +60,21 @@ test('the preview closes a code fence it cuts open, in Markdown only', () => {assert.equal(foldPreview(closed), `\`\`\`\na\n\`\`\`\n${lines(7)}`)})+// #520: a card message is the fence marker, then one very long line — the+// newline cut left an empty fence, so no preview and nothing on open either.+test('a long message that opens with a card fence still previews its start', () => {+ const card = `\`\`\`dsh-ui\n${'x'.repeat(2000)}`+ const preview = foldPreview(card)+ assert.equal(preview, `\`\`\`\n${'x'.repeat(590)}\n\`\`\``)+})++// Leading blank lines are not part of the preview; an all-blank message does+// not fold at all.+test('the preview starts at the first visible character', () => {+ assert.equal(foldPreview(`\n\n\n${'x'.repeat(1500)}`), 'x'.repeat(600))+ assert.equal(foldPreview(`${' '.repeat(2000)}\n${'\n'.repeat(30)}`), null)+})+test('a Bot reply in its own chat folds only past 20,000 characters', () => {assert.equal(foldPreview(lines(500).slice(0, 5000), FOLD_HUGE), null)assert.equal(foldPreview('z'.repeat(20_001), FOLD_HUGE), 'z'.repeat(600))