copilotz 1.0.0-rc.7 → 1.0.0-rc.8
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 +34 -0
- package/README.md +2 -2
- package/package.json +1 -1
- package/plugins/agents/chat/collections/message.d.ts +45 -0
- package/plugins/agents/chat/collections/message.js +14 -0
- package/plugins/agents/chat/processors/turn.js +8 -2
- package/plugins/agents/chat/shared/delivery.d.ts +2 -1
- package/plugins/agents/chat/shared/delivery.js +2 -0
- package/plugins/agents/chat/shared/types.d.ts +13 -2
- package/plugins/agents/chat/views/message.d.ts +4 -1
- package/plugins/agents/chat/views/message.js +10 -2
- package/plugins/agents/chat/views/prompt.d.ts +3 -1
- package/plugins/agents/chat/views/prompt.js +12 -4
- package/plugins/agents/chat/views/terminal/agent-prompt.js +2 -2
- package/plugins/agents/history/collections/outline.d.ts +15 -0
- package/plugins/agents/history/collections/outline.js +2 -0
- package/plugins/agents/history/mod.d.ts +8 -6
- package/plugins/agents/history/mod.js +8 -6
- package/plugins/agents/history/processors/compact.d.ts +1 -1
- package/plugins/agents/history/processors/compact.js +52 -29
- package/plugins/agents/memory/collections/memory.js +15 -4
- package/plugins/agents/memory/resources/context.d.ts +9 -1
- package/plugins/agents/memory/resources/context.js +7 -1
- package/plugins/agents/memory/shared/search.d.ts +3 -1
- package/plugins/agents/memory/shared/search.js +10 -2
- package/plugins/agents/memory/views/context.d.ts +4 -1
- package/plugins/agents/memory/views/context.js +27 -11
- 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/plugins/interfaces/whatsapp/collections/phone.d.ts +1 -1
- package/plugins/models/adapters/llm/openai.js +57 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
+
## 1.0.0-rc.8 — 2026-10-31
|
|
2
|
+
|
|
3
|
+
- **Agents' prompts only grow, so the prompt cache keeps them whole.** A turn context (the
|
|
4
|
+
computers', the spaces elsewhere, memories, an app's) is sent after the conversation only when it
|
|
5
|
+
says something new: when its text isn't what the agent was last told there. The turn's first
|
|
6
|
+
message keeps what it was told (`told`, `[{ name, text }]`, a private field: people's reads come
|
|
7
|
+
without it), and later turns are sent it there. So each model call starts with the whole call
|
|
8
|
+
before it, tool steps and earlier contexts included, where before a turn's context, dropped the
|
|
9
|
+
next turn, cut the cached prefix at the turn's start. `Prompt.turn` is now `{ name, text }[]`:
|
|
10
|
+
what's new this turn.
|
|
11
|
+
- **OpenAI's newer models keep the prefix where told.** Since GPT-5.6, OpenAI caches only at
|
|
12
|
+
breakpoints: its implicit one falls at the end of the latest user message, the turn's context,
|
|
13
|
+
which the next turn didn't send, so a conversation reused only its system text. The Responses
|
|
14
|
+
adapter now marks a message with `cache` (the end of the history) with an explicit
|
|
15
|
+
`prompt_cache_breakpoint`, on the user's or tool's input at or before it. A model that refuses
|
|
16
|
+
breakpoints (earlier ones, other servers) is asked again without, and never again. What OpenAI
|
|
17
|
+
wrote to the cache is metered as `cacheWrite` (billed at a premium by models that report it).
|
|
18
|
+
- **Memory: the system text says how to remember; what's remembered is told.** The `memory` context
|
|
19
|
+
(system) is the guide alone, the same in every conversation, so the system text is cached across
|
|
20
|
+
all of an agent's conversations in a space. Core memories about who's here are the new
|
|
21
|
+
`remembered` turn context: those learned before the conversation began, told at its start and kept
|
|
22
|
+
there. Core memories learned since come up in `recall` when relevant (query `relevant` takes
|
|
23
|
+
`since`; `core` takes `before`), so a memory learned mid-conversation never rewrites its prompt.
|
|
24
|
+
- **History summarizes a message when it leaves the tail**, not when it comes: only then is its line
|
|
25
|
+
read (in the outline, or by `zoom`). A conversation that stays within the tail never calls the
|
|
26
|
+
summarizer; a longer one builds the same tree, in batches, as its tail shrinks. The outline keeps
|
|
27
|
+
the tail's message ids (`tailIds`); threads summarized before keep the lines they have.
|
|
28
|
+
- Measured on Compass's own plugins with real OpenAI calls (gpt-6.1-sol; a 12-turn conversation,
|
|
29
|
+
then a 4-turn one with tool steps, the same agent in the same space): the agent's calls read 95.5%
|
|
30
|
+
of their input from the prompt cache, up from 59.3%, and every model call in the run (memory
|
|
31
|
+
extraction included) 92.4%, up from 44.4%, with no summarizer call (101 before). A 24-turn
|
|
32
|
+
conversation that outgrew its tail: 92.4% for the agent, its one miss when the outline arrives
|
|
33
|
+
(the prompt is new from there).
|
|
34
|
+
|
|
1
35
|
## 1.0.0-rc.7 — 2026-10-30
|
|
2
36
|
|
|
3
37
|
- **Agents choose among a space's computers.** Every computer tool (the terminal, jobs, files, the
|
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ bun add copilotz # Bun
|
|
|
22
22
|
deno add jsr:@copilotz/copilotz # Deno (or `deno add npm:copilotz`)
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.
|
|
25
|
+
On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.8"`), and
|
|
26
26
|
the imports below are the same everywhere. Some pieces need a package of their own, installed beside
|
|
27
27
|
it: `esbuild` to bundle the web host on Node, `@electric-sql/pglite` for PostgreSQL in the process,
|
|
28
28
|
and `@types/react` for the front ends' types.
|
|
@@ -204,7 +204,7 @@ and an app serves it from there by default:
|
|
|
204
204
|
import { computerDownloads } from "copilotz/plugins/compute/downloads";
|
|
205
205
|
|
|
206
206
|
plugins: [...plugins, await computerDownloads()], // this Copilotz's release
|
|
207
|
-
plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.
|
|
207
|
+
plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.8" })], // its own: /computer/download/
|
|
208
208
|
plugins: [...plugins, await computerDownloads({ url: "https://downloads.example.com/computer/" })], // or elsewhere
|
|
209
209
|
```
|
|
210
210
|
|
package/package.json
CHANGED
|
@@ -113,6 +113,26 @@ declare const messageSchema: {
|
|
|
113
113
|
readonly state: {
|
|
114
114
|
readonly type: "object";
|
|
115
115
|
};
|
|
116
|
+
/**
|
|
117
|
+
* What its sender was told before writing it, at the start of its turn
|
|
118
|
+
* (its `turn` contexts that said something new): sent to it again here,
|
|
119
|
+
* in later turns. Only trusted code reads it.
|
|
120
|
+
*/
|
|
121
|
+
readonly told: {
|
|
122
|
+
readonly type: "array";
|
|
123
|
+
readonly items: {
|
|
124
|
+
readonly type: "object";
|
|
125
|
+
readonly properties: {
|
|
126
|
+
readonly name: {
|
|
127
|
+
readonly type: "string";
|
|
128
|
+
};
|
|
129
|
+
readonly text: {
|
|
130
|
+
readonly type: "string";
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
readonly required: readonly ["name", "text"];
|
|
134
|
+
};
|
|
135
|
+
};
|
|
116
136
|
readonly at: {
|
|
117
137
|
readonly type: "string";
|
|
118
138
|
};
|
|
@@ -233,6 +253,26 @@ export declare const message: {
|
|
|
233
253
|
readonly state: {
|
|
234
254
|
readonly type: "object";
|
|
235
255
|
};
|
|
256
|
+
/**
|
|
257
|
+
* What its sender was told before writing it, at the start of its turn
|
|
258
|
+
* (its `turn` contexts that said something new): sent to it again here,
|
|
259
|
+
* in later turns. Only trusted code reads it.
|
|
260
|
+
*/
|
|
261
|
+
readonly told: {
|
|
262
|
+
readonly type: "array";
|
|
263
|
+
readonly items: {
|
|
264
|
+
readonly type: "object";
|
|
265
|
+
readonly properties: {
|
|
266
|
+
readonly name: {
|
|
267
|
+
readonly type: "string";
|
|
268
|
+
};
|
|
269
|
+
readonly text: {
|
|
270
|
+
readonly type: "string";
|
|
271
|
+
};
|
|
272
|
+
};
|
|
273
|
+
readonly required: readonly ["name", "text"];
|
|
274
|
+
};
|
|
275
|
+
};
|
|
236
276
|
readonly at: {
|
|
237
277
|
readonly type: "string";
|
|
238
278
|
};
|
|
@@ -274,6 +314,11 @@ export declare const message: {
|
|
|
274
314
|
in?: string | undefined;
|
|
275
315
|
sources?: string[] | undefined;
|
|
276
316
|
stopped?: boolean | undefined;
|
|
317
|
+
told?: {
|
|
318
|
+
[x: string]: unknown;
|
|
319
|
+
name: string;
|
|
320
|
+
text: string;
|
|
321
|
+
}[] | undefined;
|
|
277
322
|
from: string;
|
|
278
323
|
to: string[];
|
|
279
324
|
at: string;
|
|
@@ -61,12 +61,26 @@ const messageSchema = {
|
|
|
61
61
|
stopped: { type: "boolean" },
|
|
62
62
|
/** A step's model state (its thinking), sent back to that model while its turn goes on. */
|
|
63
63
|
state: { type: "object" },
|
|
64
|
+
/**
|
|
65
|
+
* What its sender was told before writing it, at the start of its turn
|
|
66
|
+
* (its `turn` contexts that said something new): sent to it again here,
|
|
67
|
+
* in later turns. Only trusted code reads it.
|
|
68
|
+
*/
|
|
69
|
+
told: {
|
|
70
|
+
type: "array",
|
|
71
|
+
items: {
|
|
72
|
+
type: "object",
|
|
73
|
+
properties: { name: { type: "string" }, text: { type: "string" } },
|
|
74
|
+
required: ["name", "text"],
|
|
75
|
+
},
|
|
76
|
+
},
|
|
64
77
|
at: { type: "string" },
|
|
65
78
|
},
|
|
66
79
|
required: ["thread", "from", "to", "at"],
|
|
67
80
|
};
|
|
68
81
|
export const message = defineCollection({
|
|
69
82
|
schema: messageSchema,
|
|
83
|
+
private: ["told"],
|
|
70
84
|
queries: {
|
|
71
85
|
/**
|
|
72
86
|
* Everything in a thread, oldest first, agents' private steps included.
|
|
@@ -61,10 +61,13 @@ const turn = async (ctx, event) => {
|
|
|
61
61
|
const instructions = composed.system.join("\n\n");
|
|
62
62
|
// The conversation up to here repeats in the next calls: a cache point, before what's new each turn.
|
|
63
63
|
const history = composed.history.map((m, i, all) => i === all.length - 1 ? { ...m, cache: true } : m);
|
|
64
|
+
// What's new this turn, after it; kept on the turn's first message, to be sent there again.
|
|
65
|
+
const told = composed.turn;
|
|
64
66
|
const conversation = [
|
|
65
67
|
...history,
|
|
66
|
-
...(
|
|
68
|
+
...(told.length ? [{ role: "user", text: told.map((c) => c.text).join("\n\n") }] : []),
|
|
67
69
|
];
|
|
70
|
+
const first = step;
|
|
68
71
|
const offered = composed.tools;
|
|
69
72
|
const messaging = (agent.tools ?? []).includes(SEND);
|
|
70
73
|
// Whom it may message, read only when it messages someone: the agents
|
|
@@ -111,7 +114,7 @@ const turn = async (ctx, event) => {
|
|
|
111
114
|
}
|
|
112
115
|
if (!calls.length) {
|
|
113
116
|
await live.settle();
|
|
114
|
-
return await answer(ctx, t, run, text, {}, current);
|
|
117
|
+
return await answer(ctx, t, run, text, step === first ? { told } : {}, current);
|
|
115
118
|
}
|
|
116
119
|
// Ids derive from the run, so a retry rewrites nothing it already committed.
|
|
117
120
|
const id = `${run}:${step}`;
|
|
@@ -171,6 +174,8 @@ const turn = async (ctx, event) => {
|
|
|
171
174
|
const waitingFor = sends.filter((s) => s.message.expectReply !== false)
|
|
172
175
|
.map((s) => s.message._id);
|
|
173
176
|
const deliveries = await Promise.all(sends.map((s) => delivery(ctx, s.message, s.message.subthread)));
|
|
177
|
+
// The turn's first message keeps what it was told.
|
|
178
|
+
const kept = told.length && step === first ? { told } : {};
|
|
174
179
|
await live.settle();
|
|
175
180
|
await ctx.transaction(async (tx) => {
|
|
176
181
|
// What showed live becomes the step.
|
|
@@ -185,6 +190,7 @@ const turn = async (ctx, event) => {
|
|
|
185
190
|
...(text ? { text } : {}),
|
|
186
191
|
toolCalls: done,
|
|
187
192
|
...(state ? { state } : {}),
|
|
193
|
+
...kept,
|
|
188
194
|
turn: run,
|
|
189
195
|
at,
|
|
190
196
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Delivering a message: the turns it starts and the waits it checks off; and agents' answers. */
|
|
2
2
|
import type { Context, Resources, TransactionScope } from "../../../../runtime/mod.js";
|
|
3
3
|
import type { Thread } from "../collections/thread.js";
|
|
4
|
-
import { type Turn } from "./types.js";
|
|
4
|
+
import { type Told, type Turn } from "./types.js";
|
|
5
5
|
/** Whether a message after `hops` agent messages in a row may still start a turn. */
|
|
6
6
|
export declare const starts: (ctx: {
|
|
7
7
|
resources: Resources;
|
|
@@ -31,6 +31,7 @@ start?: boolean) => Promise<(tx: TransactionScope) => Promise<void>>;
|
|
|
31
31
|
export declare const answer: (ctx: Context, t: Turn, run: string, text: string, how?: {
|
|
32
32
|
failed?: boolean;
|
|
33
33
|
stopped?: boolean;
|
|
34
|
+
told?: Told[];
|
|
34
35
|
},
|
|
35
36
|
/** The turn's thread, when it has it already. */
|
|
36
37
|
known?: Pick<Thread, "in">) => Promise<void>;
|
|
@@ -64,6 +64,8 @@ known) => {
|
|
|
64
64
|
turn: run,
|
|
65
65
|
...(how.failed ? { failed: true } : {}),
|
|
66
66
|
...(how.stopped ? { stopped: true } : {}),
|
|
67
|
+
// What it was told this turn, when it's the turn's first message.
|
|
68
|
+
...(how.told?.length ? { told: how.told } : {}),
|
|
67
69
|
at: now(),
|
|
68
70
|
};
|
|
69
71
|
const deliver = await delivery(ctx, reply, reply.thread, !how.stopped);
|
|
@@ -146,13 +146,24 @@ export type Prompt = {
|
|
|
146
146
|
tools: LlmTool[];
|
|
147
147
|
system: string[];
|
|
148
148
|
history: LlmMessage[];
|
|
149
|
-
turn:
|
|
149
|
+
turn: Told[];
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* A `turn` context an agent was told: its name and its text. Kept on the
|
|
153
|
+
* first message its turn writes, so later turns are sent it where it was, and
|
|
154
|
+
* the conversation only grows (the prompt cache keeps all of it).
|
|
155
|
+
*/
|
|
156
|
+
export type Told = {
|
|
157
|
+
name: string;
|
|
158
|
+
text: string;
|
|
150
159
|
};
|
|
151
160
|
/**
|
|
152
161
|
* A `context` resource: where text a plugin or app adds to agents' prompts
|
|
153
162
|
* goes. The text is the `context:<name>` View of the thread. `system` text must
|
|
154
163
|
* be stable (it is cached across turns); `turn` text comes after the
|
|
155
|
-
* conversation
|
|
164
|
+
* conversation when it says something new (when it's not what the agent was
|
|
165
|
+
* last told there), and stays in the conversation where it came. It goes to
|
|
166
|
+
* every agent unless `agents` says.
|
|
156
167
|
*/
|
|
157
168
|
export type ContextEntry = {
|
|
158
169
|
place: "system" | "turn";
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { ViewContext } from "../../../../runtime/mod.js";
|
|
2
2
|
import type { LlmMessage } from "../../../models/contract.js";
|
|
3
3
|
import type { Message } from "../collections/message.js";
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* A message as the viewing agent sees it: its own as its turns (after what it
|
|
6
|
+
* was told then), others' as labelled input.
|
|
7
|
+
*/
|
|
5
8
|
export declare function messageView(m: Message, ctx: ViewContext): LlmMessage[];
|
|
@@ -35,7 +35,10 @@ function attached(m, ctx) {
|
|
|
35
35
|
});
|
|
36
36
|
return { text: lines.join(""), files };
|
|
37
37
|
}
|
|
38
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* A message as the viewing agent sees it: its own as its turns (after what it
|
|
40
|
+
* was told then), others' as labelled input.
|
|
41
|
+
*/
|
|
39
42
|
export function messageView(m, ctx) {
|
|
40
43
|
const me = ctx.viewer?.name;
|
|
41
44
|
if (m.from !== me) {
|
|
@@ -51,10 +54,15 @@ export function messageView(m, ctx) {
|
|
|
51
54
|
...(files.length ? { files } : {}),
|
|
52
55
|
}];
|
|
53
56
|
}
|
|
57
|
+
// What it was told at the start of the turn, where it was told it.
|
|
58
|
+
const told = m.told?.length
|
|
59
|
+
? [{ role: "user", text: m.told.map((c) => c.text).join("\n\n") }]
|
|
60
|
+
: [];
|
|
54
61
|
if (!m.toolCalls?.length)
|
|
55
|
-
return [{ role: "assistant", text: m.text ?? "" }];
|
|
62
|
+
return [...told, { role: "assistant", text: m.text ?? "" }];
|
|
56
63
|
const calls = m.toolCalls;
|
|
57
64
|
return [
|
|
65
|
+
...told,
|
|
58
66
|
{
|
|
59
67
|
role: "assistant",
|
|
60
68
|
text: m.text || undefined,
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The thread's `llm` View: what an agent is sent there. Its tools, then the
|
|
3
3
|
* zones the prompt cache wants: stable system text, the growing conversation,
|
|
4
|
-
* and what's new
|
|
4
|
+
* and what's new this turn: the `turn` contexts that say something else than
|
|
5
|
+
* what it was last told in the conversation (which keeps what it was told
|
|
6
|
+
* where it was, so it only grows).
|
|
5
7
|
*/
|
|
6
8
|
import type { ViewContext } from "../../../../runtime/mod.js";
|
|
7
9
|
import type { Thread } from "../collections/thread.js";
|
|
@@ -23,8 +23,16 @@ export function prompt(thread, ctx) {
|
|
|
23
23
|
const contexts = Object.entries((ctx.resources.context ?? {}))
|
|
24
24
|
.filter(([, entry]) => !entry.agents || entry.agents.includes(name));
|
|
25
25
|
const rendered = (place) => contexts.filter(([, entry]) => entry.place === place)
|
|
26
|
-
.map(([key, entry]) => ctx.show("context", { thread, key, entry }))
|
|
27
|
-
.filter((
|
|
26
|
+
.map(([key, entry]) => ({ name: key, text: ctx.show("context", { thread, key, entry }) }))
|
|
27
|
+
.filter((c) => typeof c.text === "string" && c.text !== "");
|
|
28
|
+
// What it was last told of each, in the conversation it's sent.
|
|
29
|
+
const last = new Map();
|
|
30
|
+
for (const m of history) {
|
|
31
|
+
if (m.from === name) {
|
|
32
|
+
for (const c of (m.told ?? []))
|
|
33
|
+
last.set(c.name, c.text);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
28
36
|
const resources = (ctx.resources.tools ?? {});
|
|
29
37
|
const offered = Object.keys(resources).filter((tool) => {
|
|
30
38
|
const offer = resources[tool].offer;
|
|
@@ -51,9 +59,9 @@ export function prompt(thread, ctx) {
|
|
|
51
59
|
agent?.instructions ?? "",
|
|
52
60
|
String(ctx.show("participants", { thread, history }) ?? ""),
|
|
53
61
|
...(contexts.length ? [DATA] : []),
|
|
54
|
-
...rendered("system"),
|
|
62
|
+
...rendered("system").map((c) => c.text),
|
|
55
63
|
].filter(Boolean),
|
|
56
64
|
history: (ctx.show("history", { thread, messages: history }) ?? []),
|
|
57
|
-
turn: rendered("turn"),
|
|
65
|
+
turn: rendered("turn").filter((c) => last.get(c.name) !== c.text),
|
|
58
66
|
};
|
|
59
67
|
}
|
|
@@ -13,6 +13,6 @@ export const agentPromptView = (p, _ctx) => [
|
|
|
13
13
|
: "";
|
|
14
14
|
return `${dim(m.role.padEnd(9))} ${short(text, 160)}`;
|
|
15
15
|
}),
|
|
16
|
-
accent("── turn (new
|
|
17
|
-
p.turn.length ? p.turn.join("\n") : dim("(nothing)"),
|
|
16
|
+
accent("── turn (what's new, then kept in the history)"),
|
|
17
|
+
p.turn.length ? p.turn.map((c) => c.text).join("\n") : dim("(nothing new)"),
|
|
18
18
|
].join("\n");
|
|
@@ -34,6 +34,13 @@ declare const outlineSchema: {
|
|
|
34
34
|
readonly type: "string";
|
|
35
35
|
};
|
|
36
36
|
};
|
|
37
|
+
/** And their ids: each is summarized as it leaves the tail. */
|
|
38
|
+
readonly tailIds: {
|
|
39
|
+
readonly type: "array";
|
|
40
|
+
readonly items: {
|
|
41
|
+
readonly type: "string";
|
|
42
|
+
};
|
|
43
|
+
};
|
|
37
44
|
/** The last message numbered, so a retried delivery knows it's done. */
|
|
38
45
|
readonly last: {
|
|
39
46
|
readonly type: "string";
|
|
@@ -79,6 +86,13 @@ export declare const outline: {
|
|
|
79
86
|
readonly type: "string";
|
|
80
87
|
};
|
|
81
88
|
};
|
|
89
|
+
/** And their ids: each is summarized as it leaves the tail. */
|
|
90
|
+
readonly tailIds: {
|
|
91
|
+
readonly type: "array";
|
|
92
|
+
readonly items: {
|
|
93
|
+
readonly type: "string";
|
|
94
|
+
};
|
|
95
|
+
};
|
|
82
96
|
/** The last message numbered, so a retried delivery knows it's done. */
|
|
83
97
|
readonly last: {
|
|
84
98
|
readonly type: "string";
|
|
@@ -95,6 +109,7 @@ export declare const outline: {
|
|
|
95
109
|
[x: string]: unknown;
|
|
96
110
|
last?: string | undefined;
|
|
97
111
|
since?: string | undefined;
|
|
112
|
+
tailIds?: string[] | undefined;
|
|
98
113
|
count: number;
|
|
99
114
|
lines: unknown[];
|
|
100
115
|
bytes: number;
|
|
@@ -14,6 +14,8 @@ const outlineSchema = {
|
|
|
14
14
|
/** The whole messages' sizes, from the boundary on. */
|
|
15
15
|
tail: { type: "array", items: { type: "integer" } },
|
|
16
16
|
tailAt: { type: "array", items: { type: "string" } },
|
|
17
|
+
/** And their ids: each is summarized as it leaves the tail. */
|
|
18
|
+
tailIds: { type: "array", items: { type: "string" } },
|
|
17
19
|
/** The last message numbered, so a retried delivery knows it's done. */
|
|
18
20
|
last: { type: "string" },
|
|
19
21
|
},
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
* History: chat history that never outgrows the prompt, after optchat's design ("the
|
|
3
3
|
* chat itself is the memory"):
|
|
4
4
|
*
|
|
5
|
-
* - Every shared message stays whole, forever.
|
|
6
|
-
* into one line (`summary`, level 0) and merges adjacent
|
|
7
|
-
* (levels 1, 2, …): a binary tree over the thread. A short
|
|
8
|
-
* own line,
|
|
5
|
+
* - Every shared message stays whole, forever. As it leaves the tail, a cheap
|
|
6
|
+
* model compresses it into one line (`summary`, level 0) and merges adjacent
|
|
7
|
+
* lines in pairs (levels 1, 2, …): a binary tree over the thread. A short
|
|
8
|
+
* message is its own line, two short lines are simply joined, and a
|
|
9
|
+
* conversation that stays within the tail never calls the model.
|
|
9
10
|
* - An agent is sent the recent messages whole (the tail) and, before them,
|
|
10
11
|
* the rest as summary lines (the outline): fine for what's recent, coarser
|
|
11
12
|
* with age. `zoom(id, n)` opens a line into the two it was made from, down
|
|
@@ -19,8 +20,9 @@ export type { HistorySettings } from "./shared/settings.js";
|
|
|
19
20
|
export type { Outline } from "./collections/outline.js";
|
|
20
21
|
export type { Summary } from "./collections/summary.js";
|
|
21
22
|
/**
|
|
22
|
-
* Summarizes each thread's older messages as they
|
|
23
|
-
* outline and the recent messages whole (it replaces chat's
|
|
23
|
+
* Summarizes each thread's older messages as they leave its tail, and sends
|
|
24
|
+
* agents the outline and the recent messages whole (it replaces chat's
|
|
25
|
+
* `history` View).
|
|
24
26
|
*/
|
|
25
27
|
export declare const history: Plugin;
|
|
26
28
|
export default history;
|
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
* History: chat history that never outgrows the prompt, after optchat's design ("the
|
|
3
3
|
* chat itself is the memory"):
|
|
4
4
|
*
|
|
5
|
-
* - Every shared message stays whole, forever.
|
|
6
|
-
* into one line (`summary`, level 0) and merges adjacent
|
|
7
|
-
* (levels 1, 2, …): a binary tree over the thread. A short
|
|
8
|
-
* own line,
|
|
5
|
+
* - Every shared message stays whole, forever. As it leaves the tail, a cheap
|
|
6
|
+
* model compresses it into one line (`summary`, level 0) and merges adjacent
|
|
7
|
+
* lines in pairs (levels 1, 2, …): a binary tree over the thread. A short
|
|
8
|
+
* message is its own line, two short lines are simply joined, and a
|
|
9
|
+
* conversation that stays within the tail never calls the model.
|
|
9
10
|
* - An agent is sent the recent messages whole (the tail) and, before them,
|
|
10
11
|
* the rest as summary lines (the outline): fine for what's recent, coarser
|
|
11
12
|
* with age. `zoom(id, n)` opens a line into the two it was made from, down
|
|
@@ -24,8 +25,9 @@ import { historyView } from "./views/history.js";
|
|
|
24
25
|
import { sinceView } from "./views/since.js";
|
|
25
26
|
import { summaryView } from "./views/summary.js";
|
|
26
27
|
/**
|
|
27
|
-
* Summarizes each thread's older messages as they
|
|
28
|
-
* outline and the recent messages whole (it replaces chat's
|
|
28
|
+
* Summarizes each thread's older messages as they leave its tail, and sends
|
|
29
|
+
* agents the outline and the recent messages whole (it replaces chat's
|
|
30
|
+
* `history` View).
|
|
29
31
|
*/
|
|
30
32
|
export const history = definePlugin({
|
|
31
33
|
name: "history",
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/** The compactor: every shared message, in each thread's order, gets its line
|
|
1
|
+
/** The compactor: every shared message, in each thread's order, is numbered and moves the outline; it gets its line when it leaves the tail. */
|
|
2
2
|
import type { ProcessorDef } from "../../../../runtime/mod.js";
|
|
3
3
|
export declare const compactor: ProcessorDef;
|
|
@@ -57,7 +57,43 @@ async function write(ctx, config, task, thread) {
|
|
|
57
57
|
}
|
|
58
58
|
return cut(best ?? "", config.line);
|
|
59
59
|
}
|
|
60
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* A message's line, and the merges it completes up the tree, once it leaves
|
|
62
|
+
* the tail: only then is its line ever read (in the outline, or by zoom), so a
|
|
63
|
+
* conversation that stays short never calls the summarizer.
|
|
64
|
+
*/
|
|
65
|
+
async function summarize(ctx, config, thread, index, m, get, put) {
|
|
66
|
+
if (await get(0, index))
|
|
67
|
+
return; // Summarized when it came (before summaries waited).
|
|
68
|
+
// People by name in what's kept.
|
|
69
|
+
const text = lineOf(m, await namesFor(ctx, [m.from, ...m.to]));
|
|
70
|
+
// The message's own line: itself when short, else compressed with the lines before it.
|
|
71
|
+
let own = text;
|
|
72
|
+
if (size(text) > config.line) {
|
|
73
|
+
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}`] : []);
|
|
74
|
+
own = await write(ctx, config, `Compaction: compress message ${index} into one line of at most ${config.line} bytes ` +
|
|
75
|
+
`(about ${Math.round(config.line / 7)} words), the length of this ruler:\n{ruler}\n` +
|
|
76
|
+
(before.length ? `<chat>\n${before.join("\n")}\n</chat>\n` : "") +
|
|
77
|
+
`<input>\n${text}\n</input>`, thread);
|
|
78
|
+
}
|
|
79
|
+
put(0, index, own, m._id);
|
|
80
|
+
// Each pair this message completes merges into its parent, up the tree.
|
|
81
|
+
for (let [level, i] = [0, index]; i % 2 === 1; [level, i] = [level + 1, (i - 1) / 2]) {
|
|
82
|
+
const [left, right] = [await get(level, i - 1), await get(level, i)];
|
|
83
|
+
if (!left || !right)
|
|
84
|
+
break;
|
|
85
|
+
const joined = `${left.text}\n${right.text}`;
|
|
86
|
+
const n = 2 ** level;
|
|
87
|
+
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 line of ` +
|
|
88
|
+
`at most ${config.line} bytes (about ${Math.round(config.line / 7)} words), the length ` +
|
|
89
|
+
`of this ruler:\n{ruler}\n<input>\n${left.text}\n${right.text}\n</input>`, thread);
|
|
90
|
+
put(level + 1, (i - 1) / 2, line);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Numbers a shared message and moves the outline: past the tail's maximum,
|
|
95
|
+
* the oldest messages leave it, each summarized as it does.
|
|
96
|
+
*/
|
|
61
97
|
async function compact(ctx, m, config) {
|
|
62
98
|
const outlines = ctx.db.collection("outline");
|
|
63
99
|
const nodes = ctx.db.collection("summary");
|
|
@@ -74,19 +110,8 @@ async function compact(ctx, m, config) {
|
|
|
74
110
|
return; // Committed before this delivery was retried.
|
|
75
111
|
const at = new Date().toISOString();
|
|
76
112
|
const index = state.count;
|
|
77
|
-
// People by name in what's kept.
|
|
78
|
-
const text = lineOf(m, await namesFor(ctx, [m.from, ...m.to]));
|
|
79
113
|
const made = new Map();
|
|
80
114
|
const get = async (level, i) => made.get(node(m.thread, level, i)) ?? await nodes.findOne({ _id: node(m.thread, level, i) });
|
|
81
|
-
// The message's own line: itself when short, else compressed with the lines before it.
|
|
82
|
-
let own = text;
|
|
83
|
-
if (size(text) > config.line) {
|
|
84
|
-
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}`] : []);
|
|
85
|
-
own = await write(ctx, config, `Compaction: compress message ${index} into one line of at most ${config.line} bytes ` +
|
|
86
|
-
`(about ${Math.round(config.line / 7)} words), the length of this ruler:\n{ruler}\n` +
|
|
87
|
-
(before.length ? `<chat>\n${before.join("\n")}\n</chat>\n` : "") +
|
|
88
|
-
`<input>\n${text}\n</input>`, m.thread);
|
|
89
|
-
}
|
|
90
115
|
const put = (level, i, line, message) => {
|
|
91
116
|
const doc = {
|
|
92
117
|
_id: node(m.thread, level, i),
|
|
@@ -101,23 +126,12 @@ async function compact(ctx, m, config) {
|
|
|
101
126
|
made.set(doc._id, doc);
|
|
102
127
|
return doc;
|
|
103
128
|
};
|
|
104
|
-
put(0, index, own, m._id);
|
|
105
|
-
// Each pair this message completes merges into its parent, up the tree.
|
|
106
|
-
for (let [level, i] = [0, index]; i % 2 === 1; [level, i] = [level + 1, (i - 1) / 2]) {
|
|
107
|
-
const [left, right] = [await get(level, i - 1), await get(level, i)];
|
|
108
|
-
if (!left || !right)
|
|
109
|
-
break;
|
|
110
|
-
const joined = `${left.text}\n${right.text}`;
|
|
111
|
-
const n = 2 ** level;
|
|
112
|
-
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 line of ` +
|
|
113
|
-
`at most ${config.line} bytes (about ${Math.round(config.line / 7)} words), the length ` +
|
|
114
|
-
`of this ruler:\n{ruler}\n<input>\n${left.text}\n${right.text}\n</input>`, m.thread);
|
|
115
|
-
put(level + 1, (i - 1) / 2, line);
|
|
116
|
-
}
|
|
117
129
|
// The tail grows by this message; past its maximum, the oldest become lines.
|
|
118
130
|
const count = index + 1;
|
|
119
|
-
const tail = [...state.tail, size(
|
|
131
|
+
const tail = [...state.tail, size(lineOf(m, await namesFor(ctx, [m.from, ...m.to])))];
|
|
120
132
|
const tailAt = [...state.tailAt, m.at];
|
|
133
|
+
// Which messages they are (before summaries waited, every one had its line already).
|
|
134
|
+
const tailIds = [...(state.tailIds ?? state.tail.map(() => "")), m._id];
|
|
121
135
|
let tailBytes = tail.reduce((a, b) => a + b, 0);
|
|
122
136
|
const lines = [...state.lines];
|
|
123
137
|
let bytes = state.bytes;
|
|
@@ -127,9 +141,17 @@ async function compact(ctx, m, config) {
|
|
|
127
141
|
while (tailBytes > config.tail[0] && tail.length > 1) {
|
|
128
142
|
tailBytes -= tail.shift();
|
|
129
143
|
tailAt.shift();
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
144
|
+
const id = tailIds.shift();
|
|
145
|
+
if (!await get(0, boundary)) {
|
|
146
|
+
const left = await ctx.db.collection("message").findOne({ _id: id });
|
|
147
|
+
if (left)
|
|
148
|
+
await summarize(ctx, config, m.thread, boundary, left, get, put);
|
|
149
|
+
}
|
|
150
|
+
const line = await get(0, boundary);
|
|
151
|
+
if (line) {
|
|
152
|
+
lines.push([0, boundary, line.bytes]);
|
|
153
|
+
bytes += line.bytes + 1;
|
|
154
|
+
}
|
|
133
155
|
boundary++;
|
|
134
156
|
}
|
|
135
157
|
}
|
|
@@ -175,6 +197,7 @@ async function compact(ctx, m, config) {
|
|
|
175
197
|
...(tailAt[0] ? { since: tailAt[0] } : {}),
|
|
176
198
|
tail,
|
|
177
199
|
tailAt,
|
|
200
|
+
tailIds,
|
|
178
201
|
last: m._id,
|
|
179
202
|
}, { upsert: true, ...w0 });
|
|
180
203
|
});
|
|
@@ -35,8 +35,12 @@ export const memoryCollection = defineCollection({
|
|
|
35
35
|
schema: memorySchema,
|
|
36
36
|
vectors: ["embedding"],
|
|
37
37
|
queries: {
|
|
38
|
-
/**
|
|
39
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Core memories a scope can read, oldest first: what agents always have
|
|
40
|
+
* in mind. With `before`, those learned by then (a conversation's start:
|
|
41
|
+
* what it begins knowing stays the same, so its prompt stays cached).
|
|
42
|
+
*/
|
|
43
|
+
core: (memories, scope) => memories.find({ ...readable(scope), core: true, ...(scope.before ? { at: { $lt: scope.before } } : {}) }, { sort: { at: 1 }, limit: LIMIT.core }),
|
|
40
44
|
/** Everything remembered (not forgotten), newest first: everywhere, or learned in a space (`in`). */
|
|
41
45
|
list: {
|
|
42
46
|
allow: (actor) => actor.kind === "person",
|
|
@@ -45,7 +49,14 @@ export const memoryCollection = defineCollection({
|
|
|
45
49
|
limit: p.limit ?? 50,
|
|
46
50
|
}),
|
|
47
51
|
},
|
|
48
|
-
/**
|
|
49
|
-
|
|
52
|
+
/**
|
|
53
|
+
* The memories most relevant to a text, for a scope: core ones aside,
|
|
54
|
+
* unless learned `since` (after a conversation's start: it has them only
|
|
55
|
+
* as they come up).
|
|
56
|
+
*/
|
|
57
|
+
relevant: (_memories, p, ctx) => search(ctx, p, p.text, LIMIT.relevant, {
|
|
58
|
+
least: RELEVANT,
|
|
59
|
+
...(p.since ? { coreSince: p.since } : {}),
|
|
60
|
+
}),
|
|
50
61
|
},
|
|
51
62
|
});
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Three contexts (for agents with memory): how to use it, in the system text;
|
|
3
|
+
* what it has in mind about who's here, told when a conversation begins (the
|
|
4
|
+
* same in every conversation's system text, so cached across them); and
|
|
5
|
+
* what's relevant to the latest message, each turn.
|
|
6
|
+
*/
|
|
2
7
|
export declare const contexts: {
|
|
3
8
|
memory: {
|
|
4
9
|
place: string;
|
|
5
10
|
};
|
|
11
|
+
remembered: {
|
|
12
|
+
place: string;
|
|
13
|
+
};
|
|
6
14
|
recall: {
|
|
7
15
|
place: string;
|
|
8
16
|
};
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Three contexts (for agents with memory): how to use it, in the system text;
|
|
3
|
+
* what it has in mind about who's here, told when a conversation begins (the
|
|
4
|
+
* same in every conversation's system text, so cached across them); and
|
|
5
|
+
* what's relevant to the latest message, each turn.
|
|
6
|
+
*/
|
|
2
7
|
export const contexts = {
|
|
3
8
|
memory: { place: "system" },
|
|
9
|
+
remembered: { place: "turn" },
|
|
4
10
|
recall: { place: "turn" },
|
|
5
11
|
};
|
|
@@ -8,10 +8,12 @@ type Scored = Memory & {
|
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
10
|
* The memories most relevant to `text`, best first: by meaning (with an
|
|
11
|
-
* embedder) and by shared words; `core` ones only when asked
|
|
11
|
+
* embedder) and by shared words; `core` ones only when asked (or those
|
|
12
|
+
* learned since `coreSince`).
|
|
12
13
|
*/
|
|
13
14
|
export declare function search(ctx: ReadContext | ToolContext, scope: Scope, text: string, limit: number, o?: {
|
|
14
15
|
core?: boolean;
|
|
16
|
+
coreSince?: string;
|
|
15
17
|
least?: number;
|
|
16
18
|
}): Promise<Scored[]>;
|
|
17
19
|
/**
|
|
@@ -2,11 +2,19 @@ import { vectors } from "./settings.js";
|
|
|
2
2
|
import { now, overlap, readable, SAME, shortId, words } from "./scope.js";
|
|
3
3
|
/**
|
|
4
4
|
* The memories most relevant to `text`, best first: by meaning (with an
|
|
5
|
-
* embedder) and by shared words; `core` ones only when asked
|
|
5
|
+
* embedder) and by shared words; `core` ones only when asked (or those
|
|
6
|
+
* learned since `coreSince`).
|
|
6
7
|
*/
|
|
7
8
|
export async function search(ctx, scope, text, limit, o = {}) {
|
|
8
9
|
const memories = ctx.db.collection("memory");
|
|
9
|
-
const filter = {
|
|
10
|
+
const filter = {
|
|
11
|
+
...readable(scope),
|
|
12
|
+
...(o.core
|
|
13
|
+
? {}
|
|
14
|
+
: o.coreSince
|
|
15
|
+
? { $or: [{ core: { $ne: true } }, { at: { $gte: o.coreSince } }] }
|
|
16
|
+
: { core: { $ne: true } }),
|
|
17
|
+
};
|
|
10
18
|
const query = words(text);
|
|
11
19
|
const [vector] = await vectors(ctx, [text]) ?? [];
|
|
12
20
|
const candidates = vector
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
/** The memory contexts' Views: what each agent has in mind, and what comes up each turn. */
|
|
1
|
+
/** The memory contexts' Views: how to remember, what each agent has in mind, and what comes up each turn. */
|
|
2
2
|
import type { ViewContext } from "../../../../runtime/mod.js";
|
|
3
3
|
import type { Thread } from "../../chat/contract.js";
|
|
4
4
|
export declare const contextViews: {
|
|
5
5
|
"context:memory": {
|
|
6
|
+
llm: (_thread: Thread, ctx: ViewContext) => string | undefined;
|
|
7
|
+
};
|
|
8
|
+
"context:remembered": {
|
|
6
9
|
llm: (thread: Thread, ctx: ViewContext) => string | undefined;
|
|
7
10
|
};
|
|
8
11
|
"context:recall": {
|
|
@@ -1,44 +1,60 @@
|
|
|
1
1
|
import { GUIDE, scopeOf } from "../shared/scope.js";
|
|
2
2
|
import { applies } from "../shared/settings.js";
|
|
3
|
+
/** When a conversation began: its first shared message's time (none yet: undefined; null while read). */
|
|
4
|
+
function began(thread, ctx) {
|
|
5
|
+
if (!thread.opened)
|
|
6
|
+
return undefined;
|
|
7
|
+
const first = ctx.db.collection("message").findOne({ _id: thread.opened });
|
|
8
|
+
return first.status === "ready" ? first.value?.at : null;
|
|
9
|
+
}
|
|
3
10
|
export const contextViews = {
|
|
4
|
-
// In the system text:
|
|
11
|
+
// In the system text: how to use memory. The same everywhere, so cached.
|
|
5
12
|
"context:memory": {
|
|
13
|
+
llm: (_thread, ctx) => applies(ctx, ctx.viewer?.name ?? "") ? GUIDE : undefined,
|
|
14
|
+
},
|
|
15
|
+
// Told when the conversation begins, and kept there: core memories about who's
|
|
16
|
+
// here, as it began knowing them. What's learned since comes up in recall.
|
|
17
|
+
"context:remembered": {
|
|
6
18
|
llm: (thread, ctx) => {
|
|
7
19
|
const scope = scopeOf(thread, ctx);
|
|
8
20
|
if (!scope || !applies(ctx, scope.agent))
|
|
9
21
|
return undefined;
|
|
22
|
+
const start = began(thread, ctx);
|
|
23
|
+
if (start === null)
|
|
24
|
+
return undefined;
|
|
10
25
|
const core = ctx.db.collection("memory").query("core", {
|
|
11
26
|
agent: scope.agent,
|
|
12
27
|
thread: scope.thread,
|
|
13
28
|
people: scope.people,
|
|
14
29
|
...(scope.in ? { in: scope.in } : {}),
|
|
30
|
+
...(start ? { before: start } : {}),
|
|
15
31
|
});
|
|
16
|
-
if (core.status !== "ready")
|
|
32
|
+
if (core.status !== "ready" || !core.value.length)
|
|
17
33
|
return undefined;
|
|
18
34
|
return [
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"What you remember about the people here, yourself and everyone (memories can be " +
|
|
23
|
-
"out of date: what's said in the conversation wins):",
|
|
24
|
-
...core.value.map((m) => String(ctx.show("memory", m))),
|
|
25
|
-
]
|
|
26
|
-
: []),
|
|
35
|
+
"What you remember about the people here, yourself and everyone (memories can be " +
|
|
36
|
+
"out of date: what's said in the conversation wins):",
|
|
37
|
+
...core.value.map((m) => String(ctx.show("memory", m))),
|
|
27
38
|
].join("\n");
|
|
28
39
|
},
|
|
29
40
|
},
|
|
30
|
-
// After the conversation, each turn: memories relevant to the latest message
|
|
41
|
+
// After the conversation, each turn: memories relevant to the latest message
|
|
42
|
+
// (core ones too, learned since the conversation began).
|
|
31
43
|
"context:recall": {
|
|
32
44
|
llm: (thread, ctx) => {
|
|
33
45
|
const scope = scopeOf(thread, ctx);
|
|
34
46
|
if (!scope?.latest?.text || !applies(ctx, scope.agent))
|
|
35
47
|
return undefined;
|
|
48
|
+
const start = began(thread, ctx);
|
|
49
|
+
if (start === null)
|
|
50
|
+
return undefined;
|
|
36
51
|
const relevant = ctx.db.collection("memory").query("relevant", {
|
|
37
52
|
agent: scope.agent,
|
|
38
53
|
thread: scope.thread,
|
|
39
54
|
people: scope.people,
|
|
40
55
|
...(scope.in ? { in: scope.in } : {}),
|
|
41
56
|
text: scope.latest.text,
|
|
57
|
+
...(start ? { since: start } : {}),
|
|
42
58
|
});
|
|
43
59
|
if (relevant.status !== "ready" || !relevant.value.length)
|
|
44
60
|
return undefined;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The computer program's version, as `computer.info` and `--version` say it, and its builds are named. */
|
|
2
|
-
export declare const VERSION = "1.0.0-rc.
|
|
2
|
+
export declare const VERSION = "1.0.0-rc.8";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** The computer program's version, as `computer.info` and `--version` say it, and its builds are named. */
|
|
2
|
-
export const VERSION = "1.0.0-rc.
|
|
2
|
+
export const VERSION = "1.0.0-rc.8";
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
*
|
|
15
15
|
* ```ts
|
|
16
16
|
* plugins: [...plugins, await computerDownloads()]
|
|
17
|
-
* plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.
|
|
17
|
+
* plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.8" })]
|
|
18
18
|
* plugins: [...plugins, await computerDownloads({ url: "https://downloads.example.com/computer/" })]
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
@@ -112,11 +112,11 @@ export declare const phone: {
|
|
|
112
112
|
[x: string]: unknown;
|
|
113
113
|
name?: string | undefined;
|
|
114
114
|
thread?: string | undefined;
|
|
115
|
+
told?: string | undefined;
|
|
115
116
|
person?: string | undefined;
|
|
116
117
|
since?: string | undefined;
|
|
117
118
|
space?: string | undefined;
|
|
118
119
|
linked?: string | undefined;
|
|
119
|
-
told?: string | undefined;
|
|
120
120
|
tries?: number | undefined;
|
|
121
121
|
number: string;
|
|
122
122
|
at: string;
|
|
@@ -9,16 +9,49 @@ const part = (file) => file.type === "application/pdf"
|
|
|
9
9
|
file_data: `data:${file.type};base64,${file.data}`,
|
|
10
10
|
}
|
|
11
11
|
: { type: "input_image", image_url: `data:${file.type};base64,${file.data}` };
|
|
12
|
-
|
|
12
|
+
/** Where the cache keeps a prefix: a breakpoint, on an input text (OpenAI's models since GPT-5.6). */
|
|
13
|
+
const BREAKPOINT = { prompt_cache_breakpoint: { mode: "explicit" } };
|
|
14
|
+
/**
|
|
15
|
+
* The messages that carry the breakpoints: each marked `cache`, or, when that
|
|
16
|
+
* one is the model's own (which can't), the user's or tool's before it.
|
|
17
|
+
*/
|
|
18
|
+
function pointsOf(messages) {
|
|
19
|
+
const points = new Set();
|
|
20
|
+
messages.forEach((message, i) => {
|
|
21
|
+
if (!message.cache)
|
|
22
|
+
return;
|
|
23
|
+
let k = i;
|
|
24
|
+
while (k >= 0 && messages[k].role === "assistant")
|
|
25
|
+
k--;
|
|
26
|
+
if (k >= 0)
|
|
27
|
+
points.add(k);
|
|
28
|
+
});
|
|
29
|
+
return points;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The Responses API's input. The messages in `points` (a user's text, a
|
|
33
|
+
* tool's output) carry a breakpoint: the prefix up to it is kept for the next
|
|
34
|
+
* call, and found there although what came after it this time doesn't come
|
|
35
|
+
* again.
|
|
36
|
+
*/
|
|
37
|
+
const inputOf = (messages, points) => messages.flatMap((message, i) => {
|
|
38
|
+
const point = points.has(i);
|
|
13
39
|
if (message.role === "tool") {
|
|
14
|
-
return [{
|
|
40
|
+
return [{
|
|
41
|
+
type: "function_call_output",
|
|
42
|
+
call_id: message.toolCallId,
|
|
43
|
+
output: point ? [{ type: "input_text", text: message.text, ...BREAKPOINT }] : message.text,
|
|
44
|
+
}];
|
|
15
45
|
}
|
|
16
46
|
if (message.role === "user") {
|
|
17
|
-
if (!message.files?.length)
|
|
47
|
+
if (!message.files?.length && !point)
|
|
18
48
|
return [{ role: "user", content: message.text }];
|
|
19
49
|
return [{
|
|
20
50
|
role: "user",
|
|
21
|
-
content: [
|
|
51
|
+
content: [
|
|
52
|
+
{ type: "input_text", text: message.text, ...(point ? BREAKPOINT : {}) },
|
|
53
|
+
...(message.files ?? []).map(part),
|
|
54
|
+
],
|
|
22
55
|
}];
|
|
23
56
|
}
|
|
24
57
|
return [
|
|
@@ -31,13 +64,18 @@ const inputOf = (messages) => messages.flatMap((message) => {
|
|
|
31
64
|
})),
|
|
32
65
|
];
|
|
33
66
|
});
|
|
67
|
+
/** Models that refused breakpoints (OpenAI's before GPT-5.6, other servers): sent none from then on. */
|
|
68
|
+
const pointless = new Set();
|
|
69
|
+
/** Whether a refusal is of the breakpoints themselves. */
|
|
70
|
+
const refusedPoints = (error) => error.status === 400 &&
|
|
71
|
+
String(error.message).includes("prompt_cache_breakpoint");
|
|
34
72
|
export const openaiResponses = {
|
|
35
73
|
media: ["image", "pdf"],
|
|
36
74
|
async *generate({ connection, effort, maxTokens, temperature, options, instructions, messages, tools = [], signal, }) {
|
|
37
|
-
const
|
|
75
|
+
const body = (input) => ({
|
|
38
76
|
model: connection.model,
|
|
39
77
|
...(instructions ? { instructions } : {}),
|
|
40
|
-
input
|
|
78
|
+
input,
|
|
41
79
|
...(tools.length
|
|
42
80
|
? {
|
|
43
81
|
tools: tools.map((tool) => ({
|
|
@@ -56,7 +94,17 @@ export const openaiResponses = {
|
|
|
56
94
|
...(maxTokens ? { max_output_tokens: maxTokens } : {}),
|
|
57
95
|
...(temperature !== undefined ? { temperature } : {}),
|
|
58
96
|
...options,
|
|
59
|
-
}
|
|
97
|
+
});
|
|
98
|
+
const request = (points) => post(connection, OPENAI, "/responses", body(inputOf(messages, points)), signal);
|
|
99
|
+
// Breakpoints, unless this model refused them once: then it's asked again without.
|
|
100
|
+
const where = `${connection.baseURL ?? OPENAI.baseURL} ${connection.model}`;
|
|
101
|
+
const points = pointless.has(where) ? new Set() : pointsOf(messages);
|
|
102
|
+
const response = await request(points).catch((error) => {
|
|
103
|
+
if (!points.size || !refusedPoints(error))
|
|
104
|
+
throw error;
|
|
105
|
+
pointless.add(where);
|
|
106
|
+
return request(new Set());
|
|
107
|
+
});
|
|
60
108
|
for await (const { data } of sse(response.body)) {
|
|
61
109
|
const event = JSON.parse(data);
|
|
62
110
|
if (event.type === "response.output_text.delta") {
|
|
@@ -81,6 +129,8 @@ export const openaiResponses = {
|
|
|
81
129
|
input: usage.input_tokens ?? 0,
|
|
82
130
|
output: usage.output_tokens ?? 0,
|
|
83
131
|
cached: usage.input_tokens_details?.cached_tokens,
|
|
132
|
+
// Billed apart (at a premium) by the models that report it.
|
|
133
|
+
cacheWrite: usage.input_tokens_details?.cache_write_tokens,
|
|
84
134
|
reasoning: usage.output_tokens_details?.reasoning_tokens,
|
|
85
135
|
}),
|
|
86
136
|
};
|