fix(cc-bridge): use idempotencyKey in prepend/append block ops

applyPrependSystemBlock and applyAppendSystemBlock were not using the
idempotencyKey when set — the old check used op.text as the idempotency
prefix and only examined the first/last block.

Fix: scan ALL existing text blocks; use idempotencyKey as prefix when set,
fall back to op.text (prepend) / exact match (append).
This commit is contained in:
Mourad Maatoug
2026-05-15 18:39:21 +02:00
parent 0f656571d5
commit 69f0735c7a

View File

@@ -427,19 +427,23 @@ function applyDropBlockIfContains(blocks: SystemBlock[], op: DropBlockIfContains
function applyPrependSystemBlock(blocks: SystemBlock[], op: PrependSystemBlockOp): SystemBlock[] {
if (!op.text) return blocks;
if (op.idempotencyKey || op.text) {
const firstText = blocks.find(isTextBlock)?.text ?? "";
if (firstText === op.text || firstText.startsWith(op.text)) {
return blocks;
}
}
// Idempotency: skip if any text block already starts with idempotencyKey (when
// set) or with op.text itself (default). Scans ALL blocks, not just the first.
const prefix = op.idempotencyKey ?? op.text;
const alreadyPresent = blocks.some((b) => isTextBlock(b) && b.text.startsWith(prefix));
if (alreadyPresent) return blocks;
return [{ type: "text", text: op.text }, ...blocks];
}
function applyAppendSystemBlock(blocks: SystemBlock[], op: AppendSystemBlockOp): SystemBlock[] {
if (!op.text) return blocks;
const last = [...blocks].reverse().find(isTextBlock)?.text ?? "";
if (last === op.text) return blocks;
// Idempotency: skip if any text block already starts with idempotencyKey (when
// set) or is an exact match of op.text (default). Scans ALL blocks.
const prefix = op.idempotencyKey;
const alreadyPresent = prefix
? blocks.some((b) => isTextBlock(b) && b.text.startsWith(prefix))
: blocks.some((b) => isTextBlock(b) && b.text === op.text);
if (alreadyPresent) return blocks;
return [...blocks, { type: "text", text: op.text }];
}