Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Copy the whole code block a folded message shows only in part

Fei241175c71c8fd575
diff --git a/ds-bot/client.js b/ds-bot/client.js
index cc54a653..c5e44b4a 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -7295,20 +7295,9 @@ function moreLines(text, max) {
}
return false;
}
-function foldPreview(text, limit = FOLD_AT, markdown = true) {
- if (text.length <= limit.chars && !moreLines(text, limit.lines)) return null;
- const body = text.replace(/^(\s*\n)+/, "");
- if (body.trim() === "") return null;
- let head = body.split("\n", PREVIEW_LINES).join("\n");
- if (head.length > PREVIEW_CHARS) {
- const cut = head.lastIndexOf("\n", PREVIEW_CHARS);
- head = cutAt(head, cut > 0 ? cut : PREVIEW_CHARS);
- }
- if (head.trim() === "") head = cutAt(body, PREVIEW_CHARS);
- if (!markdown) return head;
+function openedFence(lines) {
let open = null;
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;
@@ -7317,32 +7306,51 @@ function foldPreview(text, limit = FOLD_AT, markdown = true) {
openLine = i;
} else if (marker[0] === open[0] && marker.length >= open.length) open = null;
}
- if (open !== null && lines.slice(openLine + 1).join("").trim() === "") {
+ return open === null ? null : { marker: open, line: openLine };
+}
+function blockCode(body, fenceLine, open) {
+ const lines = body.split("\n");
+ const code = [];
+ for (let i = fenceLine + 1; i < lines.length; i += 1) {
+ const marker = FENCE.exec(lines[i])?.[1];
+ if (marker !== void 0 && marker[0] === open[0] && marker.length >= open.length) break;
+ code.push(lines[i]);
+ }
+ return code.join("\n");
+}
+function foldView(text, limit = FOLD_AT, markdown = true) {
+ if (text.length <= limit.chars && !moreLines(text, limit.lines)) return null;
+ const body = text.replace(/^(\s*\n)+/, "");
+ if (body.trim() === "") return null;
+ let head = body.split("\n", PREVIEW_LINES).join("\n");
+ if (head.length > PREVIEW_CHARS) {
+ const cut = head.lastIndexOf("\n", PREVIEW_CHARS);
+ head = cutAt(head, cut > 0 ? cut : PREVIEW_CHARS);
+ }
+ if (head.trim() === "") head = cutAt(body, PREVIEW_CHARS);
+ if (!markdown) return { head };
+ let opened = openedFence(head.split("\n"));
+ if (opened !== null && head.split("\n").slice(opened.line + 1).join("").trim() === "") {
head = cutAt(body, 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;
- }
+ opened = openedFence(head.split("\n"));
}
- if (open === null) return head;
- lines[openLine] = open;
- return `${lines.join("\n")}
-${open}`;
+ if (opened === null) return { head };
+ const lines = head.split("\n");
+ lines[opened.line] = opened.marker;
+ return { head: `${lines.join("\n")}
+${opened.marker}`, code: blockCode(body, opened.line, opened.marker) };
+}
+function foldPreview(text, limit = FOLD_AT, markdown = true) {
+ return foldView(text, limit, markdown)?.head ?? null;
}
var stripLongNote = (text) => text.replace(/^\[A long message: \d+ characters\.[^\]\n]*\]\n/, "");
function useFold(text, { limit = FOLD_AT, whole, live = false, markdown = true } = {}) {
const [open, setOpen] = (0, import_react14.useState)(false);
const [read, setRead] = (0, import_react14.useState)();
const cut = whole !== void 0 && whole.length > text.length;
- const preview = (0, import_react14.useMemo)(() => live ? null : foldPreview(text, cut ? ALWAYS : limit, markdown), [text, live, limit, markdown, cut]);
- if (preview === null) return { shown: text, control: null };
+ const view = (0, import_react14.useMemo)(() => live ? null : foldView(text, cut ? ALWAYS : limit, markdown), [text, live, limit, markdown, cut]);
+ if (view === null) return { shown: text, control: null };
+ const code = cut ? void 0 : view.code;
const count = (cut ? whole.length : text.length).toLocaleString();
if (!open) {
const show2 = async () => {
@@ -7351,7 +7359,7 @@ function useFold(text, { limit = FOLD_AT, whole, live = false, markdown = true }
setRead("loading");
setRead(await whole.load() ?? { whole: false });
};
- return { shown: preview, folded: true, control: (0, import_react14.createElement)("span", { className: "bt-fold" }, (0, import_react14.createElement)("button", { type: "button", className: "bt-more", onClick: show2 }, t("Show all · {count} characters", { count }))) };
+ return { shown: view.head, folded: true, code, control: (0, import_react14.createElement)("span", { className: "bt-fold" }, (0, import_react14.createElement)("button", { type: "button", className: "bt-more", onClick: show2 }, t("Show all · {count} characters", { count }))) };
}
const loaded = typeof read?.text === "string" ? read : void 0;
const note = !cut || read === void 0 || read === "loading" || loaded?.whole ? null : loaded !== void 0 && loaded.text.length > text.length ? t("Showing the first {count} characters", { count: loaded.text.length.toLocaleString() }) : t("Only the start of this message is kept here");
@@ -7716,7 +7724,7 @@ function Bubbles({ texts: texts2, roster, selfId, actions, streaming, sessionId,
);
}
function Bubble({ text, index, roster, selfId, actions, sessionId, msgKey, mentions, fold, picked, live, lead, whole }) {
- const { shown, folded, control } = useFold(text, { limit: fold, whole, live });
+ const { shown, folded, control, code } = useFold(text, { limit: fold, whole, live });
const key = msgKey ? `${msgKey}:${index}` : void 0;
const body = (0, import_react16.createElement)(import_dsh_client_ui_primitives.MarkdownText, { text: linkMentions(shown, roster.bots, selfId), streaming: live, labels: mdLabels3(), variant: "compact", fileMentions: mentions });
const row = (0, import_react16.useRef)(null);
@@ -7726,9 +7734,22 @@ function Bubble({ text, index, roster, selfId, actions, sessionId, msgKey, menti
if (!noHover() || event.target instanceof Element && event.target.closest("a,button,input,textarea,select,pre,code,[role=button],[role=link]")) return;
setToolsOpen((value) => !value);
};
+ const copyFolded = (event) => {
+ if (code === void 0 || !(event.target instanceof Element)) return;
+ const button = event.target.closest("button");
+ const label = button?.getAttribute("aria-label");
+ if (!button || !/copyButton/i.test(button.className ?? "") && label !== t("Copy") && label !== t("Copied")) return;
+ let holder = button.parentElement;
+ while (holder !== null && holder !== row.current && holder.querySelector("pre") === null) holder = holder.parentElement;
+ const pres = row.current?.querySelectorAll("pre") ?? [];
+ if (holder?.querySelector("pre") !== pres[pres.length - 1] || pres.length === 0) return;
+ event.preventDefault();
+ event.stopPropagation();
+ void navigator.clipboard?.writeText(code);
+ };
return (0, import_react16.createElement)(
"div",
- { ref: row, className: "bt-msg", onClick: tapTools },
+ { ref: row, className: "bt-msg", onClick: tapTools, onClickCapture: copyFolded },
(0, import_react16.createElement)(
"div",
{ className: "bt-msg-line" },
@@ -8101,9 +8122,23 @@ var scrollParent = (node) => {
return null;
};
function EarlierUser({ row }) {
- const { shown, folded, control } = useFold(row.text ?? "");
+ const { shown, folded, control, code } = useFold(row.text ?? "");
const body = row.text ? (0, import_react16.createElement)(import_dsh_client_ui_primitives.MarkdownText, { text: shown, labels: mdLabels3(), variant: "compact" }) : null;
- return (0, import_react16.createElement)("div", { className: "bt-urow" }, (0, import_react16.createElement)(
+ const box = (0, import_react16.useRef)(null);
+ const copyFolded = (event) => {
+ if (code === void 0 || !(event.target instanceof Element)) return;
+ const button = event.target.closest("button");
+ const label = button?.getAttribute("aria-label");
+ if (!button || !/copyButton/i.test(button.className ?? "") && label !== t("Copy") && label !== t("Copied")) return;
+ let holder = button.parentElement;
+ while (holder !== null && holder !== box.current && holder.querySelector("pre") === null) holder = holder.parentElement;
+ const pres = box.current?.querySelectorAll("pre") ?? [];
+ if (holder?.querySelector("pre") !== pres[pres.length - 1] || pres.length === 0) return;
+ event.preventDefault();
+ event.stopPropagation();
+ void navigator.clipboard?.writeText(code);
+ };
+ return (0, import_react16.createElement)("div", { className: "bt-urow", ref: box, onClickCapture: copyFolded }, (0, import_react16.createElement)(
"div",
{ className: "bt-ububble", "data-folded": folded || void 0 },
control ? (0, import_react16.createElement)("div", { className: "bt-fold-body" }, body) : body,
diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.js
index 3555f0cf..712e90bb 100644
--- a/ds-bot/src/client/cells.js
+++ b/ds-bot/src/client/cells.js
@@ -54,7 +54,7 @@ export function Bubbles({ texts, roster, selfId, actions, streaming, sessionId,
}
function Bubble({ text, index, roster, selfId, actions, sessionId, msgKey, mentions, fold, picked, live, lead, whole }) {
- const { shown, folded, control } = useFold(text, { limit: fold, whole, live })
+ const { shown, folded, control, code } = useFold(text, { limit: fold, whole, live })
const key = msgKey ? `${msgKey}:${index}` : undefined
const body = h(MarkdownText, { text: linkMentions(shown, roster.bots, selfId), streaming: live, labels: mdLabels(), variant: 'compact', fileMentions: mentions })
const row = useRef(null)
@@ -66,7 +66,28 @@ function Bubble({ text, index, roster, selfId, actions, sessionId, msgKey, menti
if (!noHover() || event.target instanceof Element && event.target.closest('a,button,input,textarea,select,pre,code,[role=button],[role=link]')) return
setToolsOpen(value => !value)
}
- return h('div', { ref: row, className: 'bt-msg', onClick: tapTools },
+ // The copy button inside a folded message's last code block would copy only
+ // the part the preview kept; write the whole block instead. On capture, so
+ // the Markdown's own copy handler never sees the click.
+ const copyFolded = (event) => {
+ if (code === undefined || !(event.target instanceof Element)) return
+ const button = event.target.closest('button')
+ // The code block's copy control is the copyButton in its banner; its label
+ // varies by the DSH version, so match the class, with the label as a spare.
+ const label = button?.getAttribute('aria-label')
+ if (!button || (!/copyButton/i.test(button.className ?? '') && label !== t('Copy') && label !== t('Copied'))) return
+ // The copy control may sit inside the `pre` or in the chrome wrapping it:
+ // the nearest ancestor of the button that holds a `pre` is its block, and
+ // only the last block in a folded preview is the cut one.
+ let holder = button.parentElement
+ while (holder !== null && holder !== row.current && holder.querySelector('pre') === null) holder = holder.parentElement
+ const pres = row.current?.querySelectorAll('pre') ?? []
+ if (holder?.querySelector('pre') !== pres[pres.length - 1] || pres.length === 0) return
+ event.preventDefault()
+ event.stopPropagation()
+ void navigator.clipboard?.writeText(code)
+ }
+ return h('div', { ref: row, className: 'bt-msg', onClick: tapTools, onClickCapture: copyFolded },
h('div', { className: 'bt-msg-line' },
lead,
h('div', { className: 'bt-bubble', 'data-folded': folded || undefined },
@@ -408,9 +429,23 @@ const scrollParent = (node) => {
}
function EarlierUser({ row }) {
- const { shown, folded, control } = useFold(row.text ?? '')
+ const { shown, folded, control, code } = useFold(row.text ?? '')
const body = row.text ? h(MarkdownText, { text: shown, labels: mdLabels(), variant: 'compact' }) : null
- return h('div', { className: 'bt-urow' }, h('div', { className: 'bt-ububble', 'data-folded': folded || undefined },
+ const box = useRef(null)
+ const copyFolded = (event) => {
+ if (code === undefined || !(event.target instanceof Element)) return
+ const button = event.target.closest('button')
+ const label = button?.getAttribute('aria-label')
+ if (!button || (!/copyButton/i.test(button.className ?? '') && label !== t('Copy') && label !== t('Copied'))) return
+ let holder = button.parentElement
+ while (holder !== null && holder !== box.current && holder.querySelector('pre') === null) holder = holder.parentElement
+ const pres = box.current?.querySelectorAll('pre') ?? []
+ if (holder?.querySelector('pre') !== pres[pres.length - 1] || pres.length === 0) return
+ event.preventDefault()
+ event.stopPropagation()
+ void navigator.clipboard?.writeText(code)
+ }
+ return h('div', { className: 'bt-urow', ref: box, onClickCapture: copyFolded }, h('div', { className: 'bt-ububble', 'data-folded': folded || undefined },
control ? h('div', { className: 'bt-fold-body' }, body) : body,
control,
row.images ? h('span', { className: 'bt-uimages' }, row.images === 1 ? t('1 image') : t('{count} images', { count: row.images })) : null))
diff --git a/ds-bot/src/client/fold.js b/ds-bot/src/client/fold.js
index 2b49959d..3817e288 100644
--- a/ds-bot/src/client/fold.js
+++ b/ds-bot/src/client/fold.js
@@ -30,7 +30,36 @@ function moreLines(text, max) {
// The start of a long text, cut at a line end (or inside a very long first line). In
// Markdown a code fence left open is closed, so the preview does not run on as code.
// Null when `text` is within `limit`.
-export function foldPreview(text, limit = FOLD_AT, markdown = true) {
+// The fence a preview cut left open: the marker and its line index in `lines`.
+function openedFence(lines) {
+ let open = null
+ let 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
+ }
+ return open === null ? null : { marker: open, line: openLine }
+}
+
+// The block a preview's cut fence opens, read whole from the unfolded text: the
+// lines between the fence and its close (or the end when it never closes), so a
+// copy of the partly shown block does not stop at the fold.
+function blockCode(body, fenceLine, open) {
+ const lines = body.split('\n')
+ const code = []
+ for (let i = fenceLine + 1; i < lines.length; i += 1) {
+ const marker = FENCE.exec(lines[i])?.[1]
+ if (marker !== undefined && marker[0] === open[0] && marker.length >= open.length) break
+ code.push(lines[i])
+ }
+ return code.join('\n')
+}
+
+// The preview `head` plus `code`, the whole block its cut fence opens (null when
+// the cut does not sit inside a fence).
+export function foldView(text, limit = FOLD_AT, markdown = true) {
if (text.length <= limit.chars && !moreLines(text, limit.lines)) return null
// Blank lines first: the preview starts at the first visible character. An
// all-blank message has nothing to preview, so it does not fold at all.
@@ -42,37 +71,26 @@ export function foldPreview(text, limit = FOLD_AT, markdown = true) {
head = cutAt(head, cut > 0 ? cut : PREVIEW_CHARS)
}
if (head.trim() === '') head = cutAt(body, PREVIEW_CHARS)
- if (!markdown) return head
- let open = null
- 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; openLine = i }
- else if (marker[0] === open[0] && marker.length >= open.length) open = null
- }
- if (open !== null && lines.slice(openLine + 1).join('').trim() === '') {
+ if (!markdown) return { head }
+ let opened = openedFence(head.split('\n'))
+ if (opened !== null && head.split('\n').slice(opened.line + 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 = cutAt(body, 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
- }
+ opened = openedFence(head.split('\n'))
}
- if (open === null) return head
+ if (opened === 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}`
+ const lines = head.split('\n')
+ lines[opened.line] = opened.marker
+ return { head: `${lines.join('\n')}\n${opened.marker}`, code: blockCode(body, opened.line, opened.marker) }
+}
+
+export function foldPreview(text, limit = FOLD_AT, markdown = true) {
+ return foldView(text, limit, markdown)?.head ?? null
}
// The line a long message that went as a file starts with (the host's attachLong) is
@@ -88,8 +106,12 @@ export function useFold(text, { limit = FOLD_AT, whole, live = false, markdown =
const [read, setRead] = useState()
const cut = whole !== undefined && whole.length > text.length
// A message kept only in part folds whatever the length of its start.
- const preview = useMemo(() => (live ? null : foldPreview(text, cut ? ALWAYS : limit, markdown)), [text, live, limit, markdown, cut])
- if (preview === null) return { shown: text, control: null }
+ const view = useMemo(() => (live ? null : foldView(text, cut ? ALWAYS : limit, markdown)), [text, live, limit, markdown, cut])
+ if (view === null) return { shown: text, control: null }
+ // `code` is the whole block the preview's last code fence opens, for a copy
+ // that must not stop at the fold; only the full text can supply it, so a
+ // message kept only in part has none.
+ const code = cut ? undefined : view.code
const count = (cut ? whole.length : text.length).toLocaleString()
if (!open) {
const show = async () => {
@@ -98,7 +120,7 @@ export function useFold(text, { limit = FOLD_AT, whole, live = false, markdown =
setRead('loading')
setRead(await whole.load() ?? { whole: false })
}
- return { shown: preview, folded: true, control: h('span', { className: 'bt-fold' }, h('button', { type: 'button', className: 'bt-more', onClick: show }, t('Show all · {count} characters', { count }))) }
+ return { shown: view.head, folded: true, code, control: h('span', { className: 'bt-fold' }, h('button', { type: 'button', className: 'bt-more', onClick: show }, t('Show all · {count} characters', { count }))) }
}
const loaded = typeof read?.text === 'string' ? read : undefined
const note = !cut || read === undefined || read === 'loading' || loaded?.whole ? null
diff --git a/ds-bot/test/fold.test.js b/ds-bot/test/fold.test.js
index 331d986a..714e8580 100644
--- a/ds-bot/test/fold.test.js
+++ b/ds-bot/test/fold.test.js
@@ -35,7 +35,7 @@ runInNewContext(bundle.outputFiles[0].text, {
return react
},
})
-const { FOLD_AT, FOLD_HUGE, foldPreview, stripLongNote, useFold } = module.exports
+const { FOLD_AT, FOLD_HUGE, foldPreview, foldView, stripLongNote, useFold } = module.exports
const render = (text, options) => { at = 0; return useFold(text, options) }
const fresh = () => { slots = [] }
const buttons = node => (node === null || typeof node !== 'object' ? [] : [...(node.type === 'button' ? [node] : []), ...node.children.flatMap(buttons)])
@@ -146,6 +146,25 @@ test('a message kept only in part is read whole on request, and says when it is
assert.match(words(render(short, { whole: lost }).control), /^Only the start of this message is kept here/)
})
+// #718: the copy of a code block the preview cut must reach the whole block.
+test('a folded preview also yields the whole code block it cut', () => {
+ const inner = lines(30)
+ const view = foldView(`Here:\n\`\`\`js\n${inner}\n\`\`\`\nafter`)
+ assert.equal(view.head, `Here:\n\`\`\`\n${lines(8)}\n\`\`\``)
+ assert.equal(view.code, inner)
+ // A block that never closes yields to the end of the text.
+ assert.equal(foldView(`\`\`\`js\n${inner}`).code, inner)
+ // No fence is cut: nothing extra to copy.
+ assert.equal(foldView(lines(30)).code, undefined)
+ const closed = foldView(`\`\`\`\na\n\`\`\`\n${lines(30)}`)
+ assert.equal(closed.code, undefined)
+ // The folded state hands the code to the caller; a cut message cannot.
+ fresh()
+ assert.equal(render(`Here:\n\`\`\`js\n${inner}\n\`\`\``, {}).code, inner)
+ fresh()
+ assert.equal(render('short', { whole: { length: 300_000 } }).code, undefined)
+})
+
test('a streaming text never folds', () => {
fresh()
assert.equal(render(lines(40), { live: true }).control, null)