copilotz 1.0.0-rc.38 → 1.0.0-rc.39
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/CHANGELOG.md +46 -0
- package/README.md +1 -1
- package/package.json +1 -1
- package/plugins/agents/history/collections/outline.d.ts +28 -2
- package/plugins/agents/history/collections/outline.js +7 -1
- package/plugins/agents/history/collections/summary.d.ts +13 -1
- package/plugins/agents/history/collections/summary.js +6 -1
- package/plugins/agents/history/mod.d.ts +9 -5
- package/plugins/agents/history/mod.js +9 -5
- package/plugins/agents/history/processors/compact.d.ts +4 -3
- package/plugins/agents/history/processors/compact.js +173 -88
- package/plugins/agents/history/resources/tools.js +31 -19
- package/plugins/agents/history/shared/lines.d.ts +14 -2
- package/plugins/agents/history/shared/lines.js +26 -1
- package/plugins/agents/history/shared/summarized.d.ts +3 -0
- package/plugins/agents/history/shared/summarized.js +3 -1
- package/plugins/agents/history/views/history.d.ts +4 -4
- package/plugins/agents/history/views/history.js +25 -12
- package/plugins/agents/history/views/since.js +3 -2
- package/plugins/agents/history/views/summary.d.ts +4 -1
- package/plugins/agents/history/views/summary.js +5 -2
- package/plugins/agents/memory/collections/memory.js +2 -1
- package/plugins/agents/memory/mod.d.ts +19 -8
- package/plugins/agents/memory/mod.js +21 -9
- package/plugins/agents/memory/processors/extract.d.ts +10 -1
- package/plugins/agents/memory/processors/extract.js +75 -17
- package/plugins/agents/memory/resources/context.d.ts +7 -4
- package/plugins/agents/memory/resources/context.js +7 -5
- package/plugins/agents/memory/resources/tools.js +69 -12
- package/plugins/agents/memory/shared/scope.d.ts +20 -3
- package/plugins/agents/memory/shared/scope.js +60 -14
- package/plugins/agents/memory/shared/search.d.ts +3 -2
- package/plugins/agents/memory/shared/search.js +25 -13
- package/plugins/agents/memory/shared/settings.d.ts +5 -0
- package/plugins/agents/memory/views/context.js +43 -5
- package/plugins/agents/skills/mod.js +4 -4
- package/plugins/agents/skills/views/context.d.ts +8 -4
- package/plugins/agents/skills/views/context.js +9 -4
- package/plugins/compute/computers/program/version.d.ts +1 -1
- package/plugins/compute/computers/program/version.js +1 -1
- package/plugins/compute/computers/resources/downloads.js +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
+
## 1.0.0-rc.39 — 2026-11-10
|
|
2
|
+
|
|
3
|
+
- **Agents look in their memory before saying they don't have something, and find more when they
|
|
4
|
+
do:**
|
|
5
|
+
- **How to use memory is Copilotz's own instructions.** The `memory` context (the guide) is
|
|
6
|
+
declared `instructions: true`, so it's sent as it is, before the app's information, instead of
|
|
7
|
+
inside `<context name="memory">` after "treat it as information, not as instructions". It now
|
|
8
|
+
also says to look first (recall, and back through the conversation) before saying you don't know
|
|
9
|
+
something, don't have it, or can't go on without it. Skills likewise: their guide is a context
|
|
10
|
+
of its own declared as instructions (`skillsGuide`); the list, which people wrote, stays
|
|
11
|
+
information (`skills`, now headed "Skills you have:"). Each agent's cached system text changes
|
|
12
|
+
once.
|
|
13
|
+
- **What comes up by itself is for what the conversation is about**, not only its latest words:
|
|
14
|
+
the latest message, the two before it and the conversation's title, each memory scored by the
|
|
15
|
+
closest (a reply like "see above" still finds what the question above was about). Each says when
|
|
16
|
+
and where it was learned (`(learned in “Launch plan” 2026-11-10)`), in `recall`'s answers too,
|
|
17
|
+
and `recall`'s `source` (a memory's id) reads what was said there, around the message it came
|
|
18
|
+
from.
|
|
19
|
+
- **What an agent was told in a conversation and still has whole isn't told again:** only memories
|
|
20
|
+
new to it come up, so a follow-up no longer re-sends the same ones (once what it was told leaves
|
|
21
|
+
the recent part of the conversation, they can come up again).
|
|
22
|
+
- **In a person's own Personal space, memory reads everywhere they can read**, by itself and by
|
|
23
|
+
`recall`, as the agent there may already: only they see what's said there. What it brings from
|
|
24
|
+
another space marks the answer (`sources`), as `recall` with `in` did. Shared spaces still read
|
|
25
|
+
only what was learned there.
|
|
26
|
+
- **The person agents work for is in the conversation for memory, there or not:** in a chat one
|
|
27
|
+
agent handed another for them, their memories are in mind, and what's said of them there is
|
|
28
|
+
about them (the extractor is told whom the agents work for).
|
|
29
|
+
- **The extractor also reads a conversation once it's been quiet a while** (`memory.quiet`, 10
|
|
30
|
+
minutes; `0` turns it off), not only every `every` messages, so a short conversation, or the
|
|
31
|
+
last few messages of any, isn't left unread. Each message sets the conversation's timer again;
|
|
32
|
+
`memory.extract`, called by it, reads what's pending.
|
|
33
|
+
|
|
34
|
+
- **An agent's tool steps keep a line of their own when they leave the tail,** so what it read and
|
|
35
|
+
ran stays in reach in a long working chat. Until now a step (an agent's tool calls with what they
|
|
36
|
+
returned, which only that agent is sent) left the tail without a line: zoom couldn't reach it and
|
|
37
|
+
`read_conversation` leaves steps out, so what an agent learned from a tool and didn't repeat in a
|
|
38
|
+
reply was gone for it. Now each step that leaves is a line in a tree of its agent's own in that
|
|
39
|
+
thread: written as the thread's are (one as it is when short, else by the summarizer from the
|
|
40
|
+
step's start and end), merged in pairs, within the outline's budget, and committed with the
|
|
41
|
+
thread's lines, so a prompt's start still changes once a cut. Only that agent is sent its lines,
|
|
42
|
+
among the thread's by when they were written (`s12+4|…`), and `zoom` opens them the same way
|
|
43
|
+
(`zoom("s12", 4)`), down to the step whole; another agent's are no line of its own to open.
|
|
44
|
+
`summary` documents of a step's tree say whose (`agent`), and the `outline` keeps each agent's
|
|
45
|
+
lines (`steps`). Steps that left before this have no line.
|
|
46
|
+
|
|
1
47
|
## 1.0.0-rc.38 — 2026-11-10
|
|
2
48
|
|
|
3
49
|
- **MCP 2026-07-28 cacheable results, widget read schemas, cross-origin MCP lines, and
|
package/README.md
CHANGED
|
@@ -162,7 +162,7 @@ OPENAI_API_KEY=sk-… node salon.ts # or bun salon.ts, or deno run -A salon.t
|
|
|
162
162
|
```
|
|
163
163
|
|
|
164
164
|
On Deno, map the name `copilotz` in `deno.json`
|
|
165
|
-
(`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.
|
|
165
|
+
(`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.39"`) so the imports read the same everywhere.
|
|
166
166
|
Copilotz runs on Node, Deno and Bun, and it's built on web standards for Cloudflare Workers and the
|
|
167
167
|
browser.
|
|
168
168
|
|
package/package.json
CHANGED
|
@@ -41,13 +41,21 @@ declare const outlineSchema: {
|
|
|
41
41
|
readonly type: "string";
|
|
42
42
|
};
|
|
43
43
|
};
|
|
44
|
-
/** Which of them are agents' steps:
|
|
44
|
+
/** Which of them are agents' steps: each leaves as a line of its agent's own (`steps`). */
|
|
45
45
|
readonly tailSteps: {
|
|
46
46
|
readonly type: "array";
|
|
47
47
|
readonly items: {
|
|
48
48
|
readonly type: "boolean";
|
|
49
49
|
};
|
|
50
50
|
};
|
|
51
|
+
/**
|
|
52
|
+
* Per agent, the lines of its steps that left the tail, which only it is
|
|
53
|
+
* sent (`Steps`): a tree of its own, so nothing private merges with what
|
|
54
|
+
* others see.
|
|
55
|
+
*/
|
|
56
|
+
readonly steps: {
|
|
57
|
+
readonly type: "object";
|
|
58
|
+
};
|
|
51
59
|
/** The last message numbered, so a retried delivery knows it's done. */
|
|
52
60
|
readonly last: {
|
|
53
61
|
readonly type: "string";
|
|
@@ -72,6 +80,13 @@ declare const outlineSchema: {
|
|
|
72
80
|
};
|
|
73
81
|
export type Outline = Infer<typeof outlineSchema> & {
|
|
74
82
|
_id: string;
|
|
83
|
+
steps?: Record<string, Steps>;
|
|
84
|
+
};
|
|
85
|
+
/** An agent's steps in a thread: how many have a line, and the lines shown, as the outline's. */
|
|
86
|
+
export type Steps = {
|
|
87
|
+
count: number;
|
|
88
|
+
lines: Line[];
|
|
89
|
+
bytes: number;
|
|
75
90
|
};
|
|
76
91
|
/**
|
|
77
92
|
* A line shown: its level and index in the tree, its size, and when its
|
|
@@ -120,13 +135,21 @@ export declare const outline: {
|
|
|
120
135
|
readonly type: "string";
|
|
121
136
|
};
|
|
122
137
|
};
|
|
123
|
-
/** Which of them are agents' steps:
|
|
138
|
+
/** Which of them are agents' steps: each leaves as a line of its agent's own (`steps`). */
|
|
124
139
|
readonly tailSteps: {
|
|
125
140
|
readonly type: "array";
|
|
126
141
|
readonly items: {
|
|
127
142
|
readonly type: "boolean";
|
|
128
143
|
};
|
|
129
144
|
};
|
|
145
|
+
/**
|
|
146
|
+
* Per agent, the lines of its steps that left the tail, which only it is
|
|
147
|
+
* sent (`Steps`): a tree of its own, so nothing private merges with what
|
|
148
|
+
* others see.
|
|
149
|
+
*/
|
|
150
|
+
readonly steps: {
|
|
151
|
+
readonly type: "object";
|
|
152
|
+
};
|
|
130
153
|
/** The last message numbered, so a retried delivery knows it's done. */
|
|
131
154
|
readonly last: {
|
|
132
155
|
readonly type: "string";
|
|
@@ -158,6 +181,9 @@ export declare const outline: {
|
|
|
158
181
|
[x: string]: unknown;
|
|
159
182
|
since?: string | undefined;
|
|
160
183
|
complete?: boolean | undefined;
|
|
184
|
+
steps?: {
|
|
185
|
+
[x: string]: unknown;
|
|
186
|
+
} | undefined;
|
|
161
187
|
last?: string | undefined;
|
|
162
188
|
tailIds?: string[] | undefined;
|
|
163
189
|
tailSteps?: boolean[] | undefined;
|
|
@@ -16,8 +16,14 @@ const outlineSchema = {
|
|
|
16
16
|
tailAt: { type: "array", items: { type: "string" } },
|
|
17
17
|
/** And their ids: each is summarized as it leaves the tail. */
|
|
18
18
|
tailIds: { type: "array", items: { type: "string" } },
|
|
19
|
-
/** Which of them are agents' steps:
|
|
19
|
+
/** Which of them are agents' steps: each leaves as a line of its agent's own (`steps`). */
|
|
20
20
|
tailSteps: { type: "array", items: { type: "boolean" } },
|
|
21
|
+
/**
|
|
22
|
+
* Per agent, the lines of its steps that left the tail, which only it is
|
|
23
|
+
* sent (`Steps`): a tree of its own, so nothing private merges with what
|
|
24
|
+
* others see.
|
|
25
|
+
*/
|
|
26
|
+
steps: { type: "object" },
|
|
21
27
|
/** The last message numbered, so a retried delivery knows it's done. */
|
|
22
28
|
last: { type: "string" },
|
|
23
29
|
/**
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Summary nodes: a binary tree of lines over each thread's shared messages,
|
|
3
|
+
* and one over each agent's steps there, which only that agent reads.
|
|
4
|
+
*/
|
|
2
5
|
import { type Infer } from "../../../../runtime/mod.js";
|
|
3
6
|
declare const summarySchema: {
|
|
4
7
|
readonly type: "object";
|
|
@@ -23,6 +26,10 @@ declare const summarySchema: {
|
|
|
23
26
|
readonly message: {
|
|
24
27
|
readonly type: "string";
|
|
25
28
|
};
|
|
29
|
+
/** In an agent's tree of its steps: that agent, the only one it's shown to. */
|
|
30
|
+
readonly agent: {
|
|
31
|
+
readonly type: "string";
|
|
32
|
+
};
|
|
26
33
|
readonly at: {
|
|
27
34
|
readonly type: "string";
|
|
28
35
|
};
|
|
@@ -56,6 +63,10 @@ export declare const summary: {
|
|
|
56
63
|
readonly message: {
|
|
57
64
|
readonly type: "string";
|
|
58
65
|
};
|
|
66
|
+
/** In an agent's tree of its steps: that agent, the only one it's shown to. */
|
|
67
|
+
readonly agent: {
|
|
68
|
+
readonly type: "string";
|
|
69
|
+
};
|
|
59
70
|
readonly at: {
|
|
60
71
|
readonly type: "string";
|
|
61
72
|
};
|
|
@@ -69,6 +80,7 @@ export declare const summary: {
|
|
|
69
80
|
private?: string[];
|
|
70
81
|
queries?: Record<string, import("../../../../runtime/define.js").QueryDef<{
|
|
71
82
|
[x: string]: unknown;
|
|
83
|
+
agent?: string | undefined;
|
|
72
84
|
message?: string | undefined;
|
|
73
85
|
at: string;
|
|
74
86
|
bytes: number;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Summary nodes: a binary tree of lines over each thread's shared messages,
|
|
3
|
+
* and one over each agent's steps there, which only that agent reads.
|
|
4
|
+
*/
|
|
2
5
|
import { defineCollection } from "../../../../runtime/mod.js";
|
|
3
6
|
const summarySchema = {
|
|
4
7
|
type: "object",
|
|
@@ -11,6 +14,8 @@ const summarySchema = {
|
|
|
11
14
|
bytes: { type: "integer" },
|
|
12
15
|
/** Level 0: the message it summarizes. */
|
|
13
16
|
message: { type: "string" },
|
|
17
|
+
/** In an agent's tree of its steps: that agent, the only one it's shown to. */
|
|
18
|
+
agent: { type: "string" },
|
|
14
19
|
at: { type: "string" },
|
|
15
20
|
},
|
|
16
21
|
required: ["thread", "level", "index", "text", "bytes", "at"],
|
|
@@ -7,17 +7,21 @@
|
|
|
7
7
|
* lines in pairs (levels 1, 2, …): a binary tree over the thread. A short
|
|
8
8
|
* message is its own line, two short lines are simply joined, and a
|
|
9
9
|
* conversation that stays within the tail never calls the model.
|
|
10
|
+
* - An agent's tool steps (its calls and what they returned, which only it is
|
|
11
|
+
* sent) leave the tail the same way, into a tree of that agent's own in the
|
|
12
|
+
* thread, so nothing private ever merges with what others see.
|
|
10
13
|
* - An agent is sent the recent messages whole (the tail) and, before them,
|
|
11
|
-
* the rest as summary lines (the outline)
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
+
* the rest as summary lines (the outline), its own steps' among them by
|
|
15
|
+
* when they were written: fine for what's recent, coarser with age.
|
|
16
|
+
* `zoom(id, n)` opens a line into the two it was made from, down to the
|
|
17
|
+
* message or the step itself, so nothing is lost.
|
|
14
18
|
* - The tail and the outline grow at their end and shrink in batches (a
|
|
15
19
|
* sawtooth), so between batches each prompt starts with the last one and
|
|
16
20
|
* reads it from the provider's prompt cache.
|
|
17
21
|
* - The tail is measured by what a prompt carries for each message, in
|
|
18
22
|
* estimated tokens: its text, files' content, transcripts, images and PDFs,
|
|
19
|
-
* and agents' tool steps
|
|
20
|
-
*
|
|
23
|
+
* and agents' tool steps. Until it first overflows, every message is sent
|
|
24
|
+
* whole.
|
|
21
25
|
* - An agent with a window by age (`history: { maxAge }`) is sent only the
|
|
22
26
|
* lines whose newest message is in it: each line says when that was.
|
|
23
27
|
*/
|
|
@@ -7,17 +7,21 @@
|
|
|
7
7
|
* lines in pairs (levels 1, 2, …): a binary tree over the thread. A short
|
|
8
8
|
* message is its own line, two short lines are simply joined, and a
|
|
9
9
|
* conversation that stays within the tail never calls the model.
|
|
10
|
+
* - An agent's tool steps (its calls and what they returned, which only it is
|
|
11
|
+
* sent) leave the tail the same way, into a tree of that agent's own in the
|
|
12
|
+
* thread, so nothing private ever merges with what others see.
|
|
10
13
|
* - An agent is sent the recent messages whole (the tail) and, before them,
|
|
11
|
-
* the rest as summary lines (the outline)
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
+
* the rest as summary lines (the outline), its own steps' among them by
|
|
15
|
+
* when they were written: fine for what's recent, coarser with age.
|
|
16
|
+
* `zoom(id, n)` opens a line into the two it was made from, down to the
|
|
17
|
+
* message or the step itself, so nothing is lost.
|
|
14
18
|
* - The tail and the outline grow at their end and shrink in batches (a
|
|
15
19
|
* sawtooth), so between batches each prompt starts with the last one and
|
|
16
20
|
* reads it from the provider's prompt cache.
|
|
17
21
|
* - The tail is measured by what a prompt carries for each message, in
|
|
18
22
|
* estimated tokens: its text, files' content, transcripts, images and PDFs,
|
|
19
|
-
* and agents' tool steps
|
|
20
|
-
*
|
|
23
|
+
* and agents' tool steps. Until it first overflows, every message is sent
|
|
24
|
+
* whole.
|
|
21
25
|
* - An agent with a window by age (`history: { maxAge }`) is sent only the
|
|
22
26
|
* lines whose newest message is in it: each line says when that was.
|
|
23
27
|
*/
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The compactor: every message, in each thread's order, adds its weight to the
|
|
3
|
-
* tail (agents' steps too, which only their agent is sent)
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* tail (agents' steps too, which only their agent is sent). Past the tail's
|
|
4
|
+
* maximum, the oldest leave it, each as a line: a shared message in the
|
|
5
|
+
* thread's tree, numbered as it came; a step in its agent's own tree, numbered
|
|
6
|
+
* as it leaves, which only that agent is sent and opens.
|
|
6
7
|
*/
|
|
7
8
|
import type { ProcessorDef } from "../../../../runtime/mod.js";
|
|
8
9
|
export declare const compactor: ProcessorDef;
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { settingsOf } from "../shared/settings.js";
|
|
2
|
-
import { cut, lineOf, node, size } from "../shared/lines.js";
|
|
2
|
+
import { clip, cut, lineOf, node, size, stepOf } from "../shared/lines.js";
|
|
3
|
+
import { hasLines } from "../shared/summarized.js";
|
|
3
4
|
import { weightOf } from "../shared/weight.js";
|
|
5
|
+
import { namesFor } from "../../../access/spaces/contract.js";
|
|
4
6
|
/** Shared messages that always stay whole, however much they weigh: the newest ones. */
|
|
5
7
|
const KEPT = 4;
|
|
6
|
-
|
|
8
|
+
/** The most of a step the summarizer reads (bytes): its start and its end. zoom opens it whole. */
|
|
9
|
+
const READ = 24_000;
|
|
10
|
+
/** Steps summarized at once, for each agent. */
|
|
11
|
+
const AT_ONCE = 8;
|
|
7
12
|
const INSTRUCTIONS = `You write the memory of a conversation: one step of a summary tree,
|
|
8
13
|
compressing one message into a line or merging two adjacent lines into one. Your line stands in
|
|
9
14
|
for its messages for a long time. Agents open it only when its words show that what they need is
|
|
@@ -61,48 +66,119 @@ async function write(ctx, config, task, tags) {
|
|
|
61
66
|
}
|
|
62
67
|
return cut(best ?? "", config.line);
|
|
63
68
|
}
|
|
69
|
+
/** A summarizer call's tags: metered for the thread, in its space (the conversation's upkeep). */
|
|
70
|
+
const tagsOf = (m) => ({ thread: m.thread, ...(m.in ? { space: m.in } : {}) });
|
|
64
71
|
/**
|
|
65
|
-
* A message's line, and the merges it completes up the tree, once it
|
|
66
|
-
* the tail: only then is its line ever read (in the outline, or by
|
|
67
|
-
* conversation that stays short never calls the summarizer.
|
|
72
|
+
* A shared message's line, and the merges it completes up the tree, once it
|
|
73
|
+
* leaves the tail: only then is its line ever read (in the outline, or by
|
|
74
|
+
* zoom), so a conversation that stays short never calls the summarizer.
|
|
68
75
|
*/
|
|
69
|
-
async function summarize(ctx, config,
|
|
70
|
-
if (await get(0, index))
|
|
76
|
+
async function summarize(ctx, config, tree, index, m) {
|
|
77
|
+
if (await tree.get(0, index))
|
|
71
78
|
return; // Summarized when it came (before summaries waited).
|
|
72
|
-
const tags = { thread, ...(m.in ? { space: m.in } : {}) };
|
|
73
79
|
// People by name in what's kept.
|
|
74
80
|
const text = lineOf(m, await namesFor(ctx, [m.from, ...m.to]));
|
|
75
81
|
// The message's own line: itself when short, else compressed with the lines before it.
|
|
76
82
|
let own = text;
|
|
77
83
|
if (size(text) > config.line) {
|
|
78
|
-
const before = (await Promise.all([4, 3, 2, 1].filter((k) => index - k >= 0).map((k) => get(0, index - k)))).flatMap((n) => n ? [`${n.index}+1|${n.text}`] : []);
|
|
84
|
+
const before = (await Promise.all([4, 3, 2, 1].filter((k) => index - k >= 0).map((k) => tree.get(0, index - k)))).flatMap((n) => n ? [`${n.index}+1|${n.text}`] : []);
|
|
79
85
|
own = await write(ctx, config, `Compaction: compress message ${index} into one line of at most ${config.line} bytes ` +
|
|
80
86
|
`(about ${Math.round(config.line / 7)} words), the length of this ruler:\n{ruler}\n` +
|
|
81
87
|
(before.length ? `<chat>\n${before.join("\n")}\n</chat>\n` : "") +
|
|
82
|
-
`<input>\n${text}\n</input>`,
|
|
88
|
+
`<input>\n${text}\n</input>`, tagsOf(m));
|
|
83
89
|
}
|
|
84
|
-
put(0, index, own, m._id);
|
|
85
|
-
|
|
90
|
+
tree.put(0, index, own, m._id);
|
|
91
|
+
await merge(ctx, config, tree, index, m);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A step's line, in its agent's tree: what it did and found, from what that
|
|
95
|
+
* agent was sent of it (a long one's start and end). Its merges come after,
|
|
96
|
+
* in order.
|
|
97
|
+
*/
|
|
98
|
+
async function summarizeStep(ctx, config, tree, index, m) {
|
|
99
|
+
if (await tree.get(0, index))
|
|
100
|
+
return;
|
|
101
|
+
const viewer = { viewer: { kind: "agent", name: m.from } };
|
|
102
|
+
const sent = await ctx.render("llm.message", m, viewer).catch(() => undefined);
|
|
103
|
+
const text = Array.isArray(sent) ? stepOf(m.from, sent) : lineOf(m);
|
|
104
|
+
const own = size(text) <= config.line ? text : await write(ctx, config, `Compaction: compress step ${index}, ${m.from}'s tool calls and what they returned, into one ` +
|
|
105
|
+
`line of at most ${config.line} bytes (about ${Math.round(config.line / 7)} words), the ` +
|
|
106
|
+
`length of this ruler:\n{ruler}\nKeep what was done to what and what it found: names, ` +
|
|
107
|
+
`values, paths, ids, errors and outcomes.\n<input>\n${clip(text, READ)}\n</input>`, tagsOf(m));
|
|
108
|
+
tree.put(0, index, own, m._id);
|
|
109
|
+
}
|
|
110
|
+
/** Each pair a line completes merges into its parent, up its tree. */
|
|
111
|
+
async function merge(ctx, config, tree, index, m) {
|
|
112
|
+
const s = tree.agent ? "s" : "";
|
|
86
113
|
for (let [level, i] = [0, index]; i % 2 === 1; [level, i] = [level + 1, (i - 1) / 2]) {
|
|
87
|
-
const [left, right] = [await get(level, i - 1), await get(level, i)];
|
|
114
|
+
const [left, right] = [await tree.get(level, i - 1), await tree.get(level, i)];
|
|
88
115
|
if (!left || !right)
|
|
89
116
|
break;
|
|
90
117
|
const joined = `${left.text}\n${right.text}`;
|
|
91
118
|
const n = 2 ** level;
|
|
92
|
-
const line = size(joined) <= config.line ? joined : await write(ctx, config, `Compaction: merge lines ${(i - 1) * n}+${n} and ${i * n}+${n}, adjacent, into one
|
|
93
|
-
`at most ${config.line} bytes (about ${Math.round(config.line / 7)} words), the
|
|
94
|
-
`of this ruler:\n{ruler}\n<input>\n${left.text}\n${right.text}\n</input>`,
|
|
95
|
-
put(level + 1, (i - 1) / 2, line);
|
|
119
|
+
const line = size(joined) <= config.line ? joined : await write(ctx, config, `Compaction: merge lines ${s}${(i - 1) * n}+${n} and ${s}${i * n}+${n}, adjacent, into one ` +
|
|
120
|
+
`line of at most ${config.line} bytes (about ${Math.round(config.line / 7)} words), the ` +
|
|
121
|
+
`length of this ruler:\n{ruler}\n<input>\n${left.text}\n${right.text}\n</input>`, tagsOf(m));
|
|
122
|
+
tree.put(level + 1, (i - 1) / 2, line);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Lines shown, within the outline's budget: past its maximum, the most due
|
|
127
|
+
* pairs (whose parent is built) merge until it's back under its minimum, so
|
|
128
|
+
* older parts get coarser. `count`: the tree's messages so far.
|
|
129
|
+
*/
|
|
130
|
+
async function squeeze(config, shown, count, tree) {
|
|
131
|
+
if (shown.bytes <= config.outline[1])
|
|
132
|
+
return shown;
|
|
133
|
+
const lines = [...shown.lines];
|
|
134
|
+
let bytes = shown.bytes;
|
|
135
|
+
while (bytes > config.outline[0]) {
|
|
136
|
+
let best = -1;
|
|
137
|
+
let due = -Infinity;
|
|
138
|
+
for (let k = 0; k + 1 < lines.length; k++) {
|
|
139
|
+
const [level, a] = lines[k];
|
|
140
|
+
const [other, b] = lines[k + 1];
|
|
141
|
+
if (level !== other || a % 2 || b !== a + 1)
|
|
142
|
+
continue;
|
|
143
|
+
const last = (b + 1) * 2 ** level - 1;
|
|
144
|
+
const d = (count - last) / 2 ** level;
|
|
145
|
+
if (d > due && await tree.get(level + 1, a / 2))
|
|
146
|
+
[best, due] = [k, d];
|
|
147
|
+
}
|
|
148
|
+
if (best < 0)
|
|
149
|
+
break;
|
|
150
|
+
const [level, a, left] = lines[best];
|
|
151
|
+
const [, , right, until] = lines[best + 1];
|
|
152
|
+
const parent = (await tree.get(level + 1, a / 2));
|
|
153
|
+
// As new as its newer half.
|
|
154
|
+
const merged = until
|
|
155
|
+
? [level + 1, a / 2, parent.bytes, until]
|
|
156
|
+
: [level + 1, a / 2, parent.bytes];
|
|
157
|
+
lines.splice(best, 2, merged);
|
|
158
|
+
bytes += parent.bytes - left - right - 1;
|
|
96
159
|
}
|
|
160
|
+
return { lines, bytes };
|
|
161
|
+
}
|
|
162
|
+
/** Runs `fn` on each item, at most `limit` at once. */
|
|
163
|
+
async function atOnce(items, limit, fn) {
|
|
164
|
+
let next = 0;
|
|
165
|
+
const worker = async () => {
|
|
166
|
+
while (next < items.length) {
|
|
167
|
+
const i = next++;
|
|
168
|
+
await fn(items[i], i);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
97
172
|
}
|
|
98
173
|
/**
|
|
99
174
|
* Adds a message to the tail (numbering it, when shared) and moves the
|
|
100
|
-
* outline: past the tail's maximum, the oldest leave it, each
|
|
101
|
-
*
|
|
175
|
+
* outline: past the tail's maximum, the oldest leave it, each summarized as
|
|
176
|
+
* it does, shared ones in the thread's tree and steps in their agent's.
|
|
102
177
|
*/
|
|
103
178
|
async function compact(ctx, m, config) {
|
|
104
179
|
const outlines = ctx.db.collection("outline");
|
|
105
180
|
const nodes = ctx.db.collection("summary");
|
|
181
|
+
const messages = ctx.db.collection("message");
|
|
106
182
|
const state = await outlines.findOne({ _id: m.thread }) ?? {
|
|
107
183
|
_id: m.thread,
|
|
108
184
|
count: 0,
|
|
@@ -113,10 +189,7 @@ async function compact(ctx, m, config) {
|
|
|
113
189
|
tailAt: [],
|
|
114
190
|
// Made with the thread's first message, it has weighed them all; made later
|
|
115
191
|
// (history added to an app, a thread from before), it hasn't.
|
|
116
|
-
complete: !await
|
|
117
|
-
thread: m.thread,
|
|
118
|
-
at: { $lt: m.at },
|
|
119
|
-
}),
|
|
192
|
+
complete: !await messages.findOne({ thread: m.thread, at: { $lt: m.at } }),
|
|
120
193
|
};
|
|
121
194
|
if (state.last === m._id)
|
|
122
195
|
return; // Committed before this delivery was retried.
|
|
@@ -124,21 +197,27 @@ async function compact(ctx, m, config) {
|
|
|
124
197
|
const step = m.private === true;
|
|
125
198
|
const index = state.count;
|
|
126
199
|
const made = new Map();
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
_id: node(m.thread, level, i),
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
200
|
+
const treeOf = (agent) => ({
|
|
201
|
+
...(agent ? { agent } : {}),
|
|
202
|
+
get: async (level, i) => made.get(node(m.thread, level, i, agent)) ??
|
|
203
|
+
await nodes.findOne({ _id: node(m.thread, level, i, agent) }),
|
|
204
|
+
put: (level, i, line, message) => {
|
|
205
|
+
const doc = {
|
|
206
|
+
_id: node(m.thread, level, i, agent),
|
|
207
|
+
thread: m.thread,
|
|
208
|
+
level,
|
|
209
|
+
index: i,
|
|
210
|
+
text: line,
|
|
211
|
+
bytes: size(line),
|
|
212
|
+
...(message ? { message } : {}),
|
|
213
|
+
...(agent ? { agent } : {}),
|
|
214
|
+
at,
|
|
215
|
+
};
|
|
216
|
+
made.set(doc._id, doc);
|
|
217
|
+
return doc;
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
const shared = treeOf();
|
|
142
221
|
// The tail grows by what this message costs a prompt; past its maximum, the oldest leave it.
|
|
143
222
|
const count = step ? index : index + 1;
|
|
144
223
|
const tail = [...state.tail, await weightOf(ctx, m)];
|
|
@@ -147,75 +226,80 @@ async function compact(ctx, m, config) {
|
|
|
147
226
|
const tailIds = [...(state.tailIds ?? state.tail.map(() => "")), m._id];
|
|
148
227
|
const tailSteps = [...(state.tailSteps ?? state.tail.map(() => false)), step];
|
|
149
228
|
let tailTokens = tail.reduce((a, b) => a + b, 0);
|
|
150
|
-
const lines = [...state.lines];
|
|
151
|
-
let bytes = state.bytes;
|
|
152
|
-
let boundary = state.boundary;
|
|
153
229
|
// The newest shared messages stay whole (with a file a few answers might need), and so does
|
|
154
230
|
// what came after them.
|
|
155
231
|
const kept = () => tailSteps.filter((s) => !s).length <= KEPT && !tailSteps[0];
|
|
156
232
|
let cutAt = state.cutAt;
|
|
157
233
|
let gone = "";
|
|
234
|
+
// What leaves, oldest first.
|
|
235
|
+
const left = [];
|
|
158
236
|
if (tailTokens > config.tail[1]) {
|
|
159
237
|
while (tailTokens > config.tail[0] && tail.length > 1 && !kept()) {
|
|
160
238
|
// In messages' own time: what was written up to this one was thought with the old start.
|
|
161
239
|
cutAt = m.at;
|
|
162
240
|
tailTokens -= tail.shift();
|
|
163
|
-
const
|
|
164
|
-
if (
|
|
165
|
-
gone =
|
|
166
|
-
|
|
167
|
-
if (tailSteps.shift())
|
|
168
|
-
continue;
|
|
169
|
-
if (!await get(0, boundary)) {
|
|
170
|
-
const left = await ctx.db.collection("message").findOne({ _id: id });
|
|
171
|
-
if (left)
|
|
172
|
-
await summarize(ctx, config, m.thread, boundary, left, get, put);
|
|
173
|
-
}
|
|
174
|
-
const line = await get(0, boundary);
|
|
175
|
-
if (line) {
|
|
176
|
-
// When its message was written: an agent's window by age leaves out older lines.
|
|
177
|
-
lines.push([0, boundary, line.bytes, left]);
|
|
178
|
-
bytes += line.bytes + 1;
|
|
179
|
-
}
|
|
180
|
-
boundary++;
|
|
241
|
+
const when = tailAt.shift();
|
|
242
|
+
if (when > gone)
|
|
243
|
+
gone = when;
|
|
244
|
+
left.push({ id: tailIds.shift(), at: when, step: tailSteps.shift() });
|
|
181
245
|
}
|
|
182
246
|
// A step is dated before its tools ran, and may follow messages written meanwhile: one dated
|
|
183
247
|
// before what just left goes with it, so whole messages start after every line.
|
|
184
248
|
while (tail.length > 1 && tailSteps[0] && tailAt[0] < gone) {
|
|
185
249
|
tailTokens -= tail.shift();
|
|
186
|
-
tailAt.shift();
|
|
187
|
-
tailIds.shift();
|
|
188
|
-
tailSteps.shift();
|
|
250
|
+
left.push({ id: tailIds.shift(), at: tailAt.shift(), step: tailSteps.shift() });
|
|
189
251
|
}
|
|
190
252
|
}
|
|
191
|
-
//
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
if (
|
|
200
|
-
|
|
201
|
-
const last = (b + 1) * 2 ** level - 1;
|
|
202
|
-
const d = (count - last) / 2 ** level;
|
|
203
|
-
if (d > due && await get(level + 1, a / 2))
|
|
204
|
-
[best, due] = [k, d];
|
|
253
|
+
// Shared messages that left, in order: each its line (when it had none), numbered as it came.
|
|
254
|
+
let lines = [...state.lines];
|
|
255
|
+
let bytes = state.bytes;
|
|
256
|
+
let boundary = state.boundary;
|
|
257
|
+
const shares = async () => {
|
|
258
|
+
for (const out of left.filter((l) => !l.step)) {
|
|
259
|
+
if (!await shared.get(0, boundary)) {
|
|
260
|
+
const message = await messages.findOne({ _id: out.id });
|
|
261
|
+
if (message)
|
|
262
|
+
await summarize(ctx, config, shared, boundary, message);
|
|
205
263
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
? [level + 1, a / 2, parent.bytes, until]
|
|
214
|
-
: [level + 1, a / 2, parent.bytes];
|
|
215
|
-
lines.splice(best, 2, merged);
|
|
216
|
-
bytes += parent.bytes - left - right - 1;
|
|
264
|
+
const line = await shared.get(0, boundary);
|
|
265
|
+
if (line) {
|
|
266
|
+
// When its message was written: an agent's window by age leaves out older lines.
|
|
267
|
+
lines.push([0, boundary, line.bytes, out.at]);
|
|
268
|
+
bytes += line.bytes + 1;
|
|
269
|
+
}
|
|
270
|
+
boundary++;
|
|
217
271
|
}
|
|
272
|
+
({ lines, bytes } = await squeeze(config, { lines, bytes }, count, shared));
|
|
273
|
+
};
|
|
274
|
+
// Steps that left, by agent: each its line in its agent's tree, numbered as it leaves.
|
|
275
|
+
const ids = left.filter((l) => l.step).map((l) => l.id);
|
|
276
|
+
const found = new Map((ids.length ? await messages.find({ _id: { $in: ids } }) : []).map((s) => [s._id, s]));
|
|
277
|
+
const byAgent = new Map();
|
|
278
|
+
for (const id of ids) {
|
|
279
|
+
const s = found.get(id);
|
|
280
|
+
if (s)
|
|
281
|
+
byAgent.set(s.from, [...byAgent.get(s.from) ?? [], s]);
|
|
218
282
|
}
|
|
283
|
+
const steps = { ...state.steps };
|
|
284
|
+
const stepped = [...byAgent].map(async ([agent, list]) => {
|
|
285
|
+
const tree = treeOf(agent);
|
|
286
|
+
const before = steps[agent] ?? { count: 0, lines: [], bytes: 0 };
|
|
287
|
+
// Each its line, a few at once; then, in order, the pairs they complete.
|
|
288
|
+
await atOnce(list, AT_ONCE, (s, k) => summarizeStep(ctx, config, tree, before.count + k, s));
|
|
289
|
+
const own = { lines: [...before.lines], bytes: before.bytes };
|
|
290
|
+
for (const [k, s] of list.entries()) {
|
|
291
|
+
await merge(ctx, config, tree, before.count + k, s);
|
|
292
|
+
const line = await tree.get(0, before.count + k);
|
|
293
|
+
if (line) {
|
|
294
|
+
own.lines.push([0, before.count + k, line.bytes, s.at]);
|
|
295
|
+
own.bytes += line.bytes + 1;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const total = before.count + list.length;
|
|
299
|
+
steps[agent] = { count: total, ...await squeeze(config, own, total, tree) };
|
|
300
|
+
});
|
|
301
|
+
await Promise.all([shares(), ...stepped]);
|
|
302
|
+
// What the batch made commits together: a prompt's start changes once a cut.
|
|
219
303
|
await ctx.transaction(async (tx) => {
|
|
220
304
|
const w0 = { writeConcern: { w: 0 } };
|
|
221
305
|
for (const doc of made.values()) {
|
|
@@ -223,7 +307,7 @@ async function compact(ctx, m, config) {
|
|
|
223
307
|
await tx.db.collection("summary").replaceOne({ _id }, rest, { upsert: true, ...w0 });
|
|
224
308
|
}
|
|
225
309
|
// The thread says so once, so unsummarized threads never look for an outline.
|
|
226
|
-
if (boundary && !state
|
|
310
|
+
if (hasLines({ boundary, steps }) && !hasLines(state)) {
|
|
227
311
|
await tx.db.collection("thread").updateOne({ _id: m.thread }, { $set: { outlined: true } }, w0);
|
|
228
312
|
}
|
|
229
313
|
await tx.db.collection("outline").replaceOne({ _id: m.thread }, {
|
|
@@ -236,6 +320,7 @@ async function compact(ctx, m, config) {
|
|
|
236
320
|
tailAt,
|
|
237
321
|
tailIds,
|
|
238
322
|
tailSteps,
|
|
323
|
+
...(Object.keys(steps).length ? { steps } : {}),
|
|
239
324
|
last: m._id,
|
|
240
325
|
...(state.complete ? { complete: true } : {}),
|
|
241
326
|
...(cutAt ? { cutAt } : {}),
|