@operad-io/chat 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -6
- package/src/views.ts +62 -22
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/chat",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/index.ts"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@operad-io/access-match": "0.3.
|
|
10
|
-
"@operad-io/engine": "0.3.
|
|
11
|
-
"@operad-io/harness-process": "0.3.
|
|
9
|
+
"@operad-io/access-match": "0.3.1",
|
|
10
|
+
"@operad-io/engine": "0.3.1",
|
|
11
|
+
"@operad-io/harness-process": "0.3.1"
|
|
12
12
|
},
|
|
13
13
|
"description": "operad: the chat plugin",
|
|
14
14
|
"repository": {
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"directory": "packages/chat"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@operad-io/harness-claude": "0.3.
|
|
21
|
-
"@operad-io/harness-pi": "0.3.
|
|
20
|
+
"@operad-io/harness-claude": "0.3.1",
|
|
21
|
+
"@operad-io/harness-pi": "0.3.1"
|
|
22
22
|
},
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"files": [
|
package/src/views.ts
CHANGED
|
@@ -635,20 +635,31 @@ export function replyData(
|
|
|
635
635
|
let sum = sizes.reduce((total, size) => total + size, 0);
|
|
636
636
|
const sizeOf = (count: number, inner: number): number => overhead + inner + Math.max(0, count - 1);
|
|
637
637
|
if (sizeOf(blanked.length, sum) <= maxBytes) return data(blanked);
|
|
638
|
-
//
|
|
639
|
-
//
|
|
640
|
-
//
|
|
638
|
+
// Shortening goes in three passes, each the oldest reply first and whole
|
|
639
|
+
// reply by whole reply, so the newest stays whole the longest. Pass 1
|
|
640
|
+
// shortens everything that is not an answer (thinking, text, tool input and
|
|
641
|
+
// the input messages, which keep a preview). Pass 2 shortens answers to a
|
|
642
|
+
// preview of about two lines. Pass 3 goes below the preview, and only then
|
|
643
|
+
// are replies dropped.
|
|
641
644
|
const cuts: ReplyBlock[] = [...blanked];
|
|
642
|
-
|
|
643
|
-
const original =
|
|
644
|
-
if (original === undefined)
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
645
|
+
const step = (at: number, caps: ReplyCaps): boolean => {
|
|
646
|
+
const original = cuts[at];
|
|
647
|
+
if (original === undefined) return false;
|
|
648
|
+
const cut = cutReply(original, caps);
|
|
649
|
+
const size = measure(cut);
|
|
650
|
+
sum += size - (sizes[at] ?? 0);
|
|
651
|
+
sizes[at] = size;
|
|
652
|
+
cuts[at] = cut;
|
|
653
|
+
return sizeOf(cuts.length, sum) <= maxBytes;
|
|
654
|
+
};
|
|
655
|
+
const passes: readonly (readonly ReplyCaps[])[] = [
|
|
656
|
+
FREE_CAPS.map((free) => ({ free, input: Math.max(free, PREVIEW_BYTES), answer: Infinity })),
|
|
657
|
+
[{ free: 0, input: PREVIEW_BYTES, answer: PREVIEW_BYTES }],
|
|
658
|
+
[64, 0].map((all) => ({ free: all, input: all, answer: all })),
|
|
659
|
+
];
|
|
660
|
+
for (const pass of passes) {
|
|
661
|
+
for (let at = 0; at < cuts.length; at += 1) {
|
|
662
|
+
for (const caps of pass) if (step(at, caps)) return data(cuts);
|
|
652
663
|
}
|
|
653
664
|
}
|
|
654
665
|
// Even empty text leaves too much structure: the oldest replies go.
|
|
@@ -662,35 +673,64 @@ export function replyData(
|
|
|
662
673
|
return (measure({ replies: [] }) <= maxBytes ? { replies: [] } : {}) as Readonly<Record<string, JsonValue>>;
|
|
663
674
|
}
|
|
664
675
|
|
|
665
|
-
/**
|
|
676
|
+
/** The preview an older reply's answer and input keep while anything else can still be cut: about two lines. */
|
|
677
|
+
const PREVIEW_BYTES = 200;
|
|
678
|
+
const FREE_CAPS: readonly number[] = [128, 0];
|
|
679
|
+
|
|
680
|
+
/** Byte caps for the three kinds of text in a reply; `Infinity` leaves a kind whole. */
|
|
681
|
+
interface ReplyCaps {
|
|
682
|
+
readonly free: number;
|
|
683
|
+
readonly input: number;
|
|
684
|
+
readonly answer: number;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* Text cut to fit a byte budget, ending with an ellipsis when it was longer.
|
|
689
|
+
* The cut is at a line or sentence end when one is in the last quarter of what
|
|
690
|
+
* fits, and never inside a code point.
|
|
691
|
+
*/
|
|
666
692
|
function shorten(text: string, capBytes: number): string {
|
|
667
693
|
if (capBytes <= 0) return "";
|
|
668
694
|
if (Buffer.byteLength(text, "utf8") <= capBytes) return text;
|
|
669
695
|
if (capBytes <= 3) return "";
|
|
670
696
|
let end = Math.min(text.length, capBytes);
|
|
671
697
|
while (end > 0 && Buffer.byteLength(text.slice(0, end), "utf8") + 3 > capBytes) end -= 1;
|
|
672
|
-
|
|
698
|
+
const last = text.charCodeAt(end - 1);
|
|
699
|
+
if (end > 0 && last >= 0xd800 && last <= 0xdbff) end -= 1;
|
|
700
|
+
if (end === 0) return "";
|
|
701
|
+
const head = text.slice(0, end);
|
|
702
|
+
const floor = Math.floor(end * 0.75);
|
|
703
|
+
const newline = head.lastIndexOf("\n");
|
|
704
|
+
if (newline > 0 && newline >= floor) {
|
|
705
|
+
const line = head.slice(0, newline).trimEnd();
|
|
706
|
+
if (line !== "") return `${line}…`;
|
|
707
|
+
}
|
|
708
|
+
const sentence = /[.!?。!?](?=\s|$)/g;
|
|
709
|
+
let stop = -1;
|
|
710
|
+
for (let found = sentence.exec(head); found !== null; found = sentence.exec(head)) stop = found.index + 1;
|
|
711
|
+
if (stop > 0 && stop >= floor) return `${head.slice(0, stop)}…`;
|
|
712
|
+
return `${head}…`;
|
|
673
713
|
}
|
|
674
714
|
|
|
675
715
|
/** One reply with its free text cut to a budget; every text that got shorter says `cut`. */
|
|
676
|
-
function cutReply(reply: ReplyBlock,
|
|
677
|
-
const text = (original: string): { readonly text: string; readonly cut?: true } => {
|
|
716
|
+
function cutReply(reply: ReplyBlock, caps: ReplyCaps): ReplyBlock {
|
|
717
|
+
const text = (original: string, capBytes: number): { readonly text: string; readonly cut?: true } => {
|
|
678
718
|
const shortened = shorten(original, capBytes);
|
|
679
719
|
return shortened === original ? { text: shortened } : { text: shortened, cut: true };
|
|
680
720
|
};
|
|
681
|
-
const message = (block: ReplyMessage): ReplyMessage => ({ ...block, ...text(block.text) });
|
|
721
|
+
const message = (block: ReplyMessage, capBytes: number): ReplyMessage => ({ ...block, ...text(block.text, capBytes) });
|
|
682
722
|
const item = (entry: ReplyItem): ReplyItem => {
|
|
683
723
|
if (entry.kind === "tool") {
|
|
684
724
|
if (entry.input === null) return entry;
|
|
685
|
-
const shortened = shorten(entry.input,
|
|
725
|
+
const shortened = shorten(entry.input, caps.free);
|
|
686
726
|
return shortened === entry.input ? entry : { ...entry, input: shortened, cut: true };
|
|
687
727
|
}
|
|
688
|
-
return { ...entry, ...text(entry.text) };
|
|
728
|
+
return { ...entry, ...text(entry.text, caps.free) };
|
|
689
729
|
};
|
|
690
730
|
return {
|
|
691
731
|
...reply,
|
|
692
|
-
input: reply.input.map(message),
|
|
693
|
-
...(reply.answer === undefined ? {} : { answer: message(reply.answer) }),
|
|
732
|
+
input: reply.input.map((block) => message(block, caps.input)),
|
|
733
|
+
...(reply.answer === undefined ? {} : { answer: message(reply.answer, caps.answer) }),
|
|
694
734
|
process: reply.process.map((group) => ({
|
|
695
735
|
...group,
|
|
696
736
|
items: group.items.map(item),
|