pi-optchat 0.7.1 → 0.8.0
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/README.md +34 -21
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/docs/victor-recipe.md +5 -5
- package/package.json +2 -1
- package/src/agents.ts +32 -4
- package/src/browser.ts +5 -3
- package/src/cache.ts +14 -10
- package/src/checkpoint.ts +1 -1
- package/src/compactor.ts +19 -10
- package/src/images.ts +44 -0
- package/src/import/job.ts +12 -7
- package/src/import/sources.ts +108 -14
- package/src/import/ui.ts +6 -4
- package/src/inbox.ts +15 -4
- package/src/index.ts +99 -42
- package/src/inspector.ts +19 -2
- package/src/memory.ts +197 -79
- package/src/profiles.ts +51 -32
- package/src/prompts.ts +4 -4
- package/src/runs.ts +9 -2
- package/src/settings-page.ts +35 -10
- package/src/settings.ts +3 -3
- package/src/tools.ts +13 -4
- package/src/transcript.ts +26 -9
- package/src/window-bridge.ts +76 -17
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ A Pi extension that implements [Victor Taelin's OptChat recipe](https://gist.git
|
|
|
7
7
|
- **Memory**: every message is logged and summarized into a binary tree. Each turn starts from a fresh context holding a bounded memory view; the agent uses `zoom` and `date` to read originals.
|
|
8
8
|
- **Profiles**: separate memories and instructions, such as `work` and `personal`.
|
|
9
9
|
- **Subagents**: delegate tasks to background agents, inspect them live, and send them guidance.
|
|
10
|
-
- **Import**: bring in history from Claude Code (conversations and memories), Codex, or ChatGPT.
|
|
10
|
+
- **Import**: bring in history from Claude Code (conversations and memories), Codex, Pi and [OMP](https://github.com/can1357/oh-my-pi), or ChatGPT.
|
|
11
11
|
- **Connected windows**: a second Pi window on the same profile becomes a subagent you talk to directly.
|
|
12
12
|
|
|
13
13
|
It runs inside ordinary Pi, with no fork or separate launcher.
|
|
@@ -21,7 +21,7 @@ pi install npm:pi-web-access # optional, for web search and page fetching
|
|
|
21
21
|
|
|
22
22
|
Or from GitHub: `pi install git:github.com/jonaslsaa/pi-optchat`.
|
|
23
23
|
|
|
24
|
-
Requirements: Pi 1.0.2 or compatible, Node.js 22.19+, and Git. Tested on macOS; the offline tests also run on Linux.
|
|
24
|
+
Requirements: Pi 1.0.2 or compatible, Node.js 22.19+, and Git. Tested on macOS; the offline tests also run on Linux and Windows.
|
|
25
25
|
|
|
26
26
|
Web access is not bundled. Subagents load the Pi extensions you have installed (except pi-optchat itself), so installing `pi-web-access` gives web tools to the main agent and every subagent.
|
|
27
27
|
|
|
@@ -35,7 +35,9 @@ To uninstall, run `pi remove git:github.com/jonaslsaa/pi-optchat`. Profile data
|
|
|
35
35
|
|
|
36
36
|
The footer shows the active profile. Memory follows the profile across directories and Pi sessions. New sessions show the profile picker with the last-used profile first; resumed sessions restore their profile.
|
|
37
37
|
|
|
38
|
-
For headless use, pass `--optchat-profile work`.
|
|
38
|
+
For headless use (`pi -p`, `--mode rpc`, other extensions' runners), pass `--optchat-profile work`. Without it, and without a saved profile in a resumed session, a headless run is plain Pi with no OptChat memory. If the requested profile can't open (misspelled, deleted, or open in another Pi), the run reports the error and doesn't answer.
|
|
39
|
+
|
|
40
|
+
If that profile is open in another Pi, `pi -p` joins it like a connected window: a subagent in that Pi answers, only its final reply goes to stdout, and the main agent gets the handoff. `--optchat-connect auto` (default) joins only when the profile is busy, `join` always joins (and fails if no Pi has the profile open), `off` reports the busy profile as an error. A failed join exits non-zero.
|
|
39
41
|
|
|
40
42
|
## Commands
|
|
41
43
|
|
|
@@ -44,9 +46,9 @@ For headless use, pass `--optchat-profile work`.
|
|
|
44
46
|
| `/optchat` | Status and actions menu. |
|
|
45
47
|
| `/optchat profile` | Select or create a profile. Switching starts a fresh Pi session. |
|
|
46
48
|
| `/optchat settings` | This profile's settings: models, subagent levels and limits, previous exchange, summary size tolerance. |
|
|
47
|
-
| `/optchat model` | Compactor model and effort for this profile. |
|
|
49
|
+
| `/optchat model` | Compactor model and effort for this profile. Type to filter the models you are logged in to; the current one is marked. |
|
|
48
50
|
| `/optchat agents` | Live agent tree and saved run history. |
|
|
49
|
-
| `/optchat agents model` | Subagent model and effort for this profile. |
|
|
51
|
+
| `/optchat agents model` | Subagent model and effort for this profile, picked the same way. |
|
|
50
52
|
| `/optchat usage` | Token usage and cost estimates. |
|
|
51
53
|
| `/optchat activity` | Memory gauge: view size, summaries catching up, running agents. |
|
|
52
54
|
| `/optchat instructions` | Edit this profile's `AGENTS.md`. |
|
|
@@ -69,15 +71,15 @@ Compression and subagents make extra model requests with your provider credentia
|
|
|
69
71
|
|
|
70
72
|
## Settings
|
|
71
73
|
|
|
72
|
-
`/optchat settings` opens this profile's settings. Each row shows its value and default, the selected row says what it does and when a change applies, and a change is saved right away to the profile's `config.json`. Defaults follow Victor's recipe, except Previous exchange
|
|
74
|
+
`/optchat settings` opens this profile's settings. Each row shows its value and default, the selected row says what it does and when a change applies, and a change is saved right away to the profile's `config.json`. Defaults follow Victor's recipe, except Previous exchange, Summary size tolerance and Group subagent reports, which keep OptChat's earlier behaviour.
|
|
73
75
|
|
|
74
76
|
| Setting | Default | What it does |
|
|
75
77
|
| --- | --- | --- |
|
|
76
78
|
| Compactor model | Sonnet 5.5, medium | Same as `/optchat model`. Applies to the next summary. |
|
|
77
79
|
| Subagent model | Opus 5.5, high | Same as `/optchat agents model`. Applies to new subagents. |
|
|
78
|
-
| Subagent levels | 1 | 1: only the main agent starts subagents
|
|
80
|
+
| Subagent levels | 1 | 1: only the main agent starts subagents. 2 or more: subagents may start their own, that many levels deep. Applies to subagents started or resumed after the change. |
|
|
79
81
|
| Max active agents | 8 | Subagents running at once in the profile, all levels together, so it also caps how deep a chain can go. |
|
|
80
|
-
| Group subagent reports |
|
|
82
|
+
| Group subagent reports | off | Off: each subagent reports as soon as it finishes (the recipe). On: the subagents started by one spawn report together, in one message once the last of them finishes. Applies to the next spawn. |
|
|
81
83
|
| Previous exchange | on | Replays your last request and answer in full with the next turn (see below). Off is the recipe. |
|
|
82
84
|
| Previous exchange limit | 16 KB | A larger last exchange is left out. |
|
|
83
85
|
| Memory search | off | Gives the agent, and subagents started or resumed after the change, a `search` tool over your original messages (see below). Off is the recipe: zoom and date only. Applies from the next turn. |
|
|
@@ -95,8 +97,8 @@ Ask in plain words, for example: "Spawn an agent to investigate this repository
|
|
|
95
97
|
|
|
96
98
|
- Children get the profile's memory view (frozen at launch), its instructions, read-only `zoom`/`date`, and normal coding tools plus your installed extensions.
|
|
97
99
|
- Children also get the Pi built-in extensions the main session loaded: MCP, codemode and tool search. Your MCP servers (`~/.pi/agent/mcp.json`, the project's `.pi/mcp.json`) work in subagents, with the sign-ins you made in Pi. If MCP is off in the main session (`--no-mcp`, `-builtin:mcp` in settings, or an extension that replaces `/mcp`), it is off in subagents too. Each subagent opens its own server connections (stdio servers start once per subagent) and closes them when it ends.
|
|
98
|
-
-
|
|
99
|
-
- In the main chat, subagent messages and reports appear in a dark grey box labelled `↳ subagent <id> · still running` or `· report`, so they don't look like something you typed. The model still receives them as ordinary user messages. (One exception: reports recovered at startup, before your first message in the session, still show as plain user messages.)
|
|
100
|
+
- Each child reports to its parent as soon as it finishes, as one message starting `[id]`. A stopped or failed child reports its stop or error text. Turn **Group subagent reports** on to have the children of one spawn report together: when the last of them finishes, their reports reach the parent as one message, `[id] report` each, in spawn order. Each finished report is journaled right away, so if Pi dies before the rest finish, the reports it already has are delivered at the next start. Messages sent with `tell_parent`, connected windows and their handoffs are never held back, and a child resumed with `tell` reports on its own. The parent stays alive to receive reports; it never polls.
|
|
101
|
+
- In the main chat, subagent messages and reports appear in a dark grey box labelled `↳ subagent <id> · still running` or `· report`, so they don't look like something you typed. The model still receives them as ordinary user messages; memory logs them as kind `work` (older logs hold them as `user` messages starting `[id]`). (One exception: reports recovered at startup, before your first message in the session, still show as plain user messages.)
|
|
100
102
|
- The parent can send a running child guidance with `tell`, and the child can message its parent mid-run with `tell_parent` (a question, an early finding). It reaches the parent like a report, marked "still running": between tool calls if the parent is busy, or waking it if it's waiting.
|
|
101
103
|
- `tell` to a finished child resumes it: the same agent (ID, parent, model, directory) reopens its saved transcript, gets the message as a new prompt, and sends a new report. This also works for children from earlier Pi sessions. Only the agent that started the child can resume it, and the resumed child takes one active slot. Connected windows can't be resumed, and a child whose transcript is missing must be spawned fresh.
|
|
102
104
|
- By default only the main agent starts subagents. Set **Subagent levels** in `/optchat settings` to let them delegate further (3 means child, grandchild, great-grandchild).
|
|
@@ -118,7 +120,7 @@ An **Agents | Usage | Activity** bar sits below the input.
|
|
|
118
120
|
|
|
119
121
|
Set a different shortcut with `OPTCHAT_INSPECT_KEY=ctrl+shift+a pi`. If another extension supplies a custom editor, OptChat leaves its Down key alone; use the shortcut or commands instead.
|
|
120
122
|
|
|
121
|
-
**Agents** lists runs as a tree with state, elapsed time, current tool, and last activity. Navigate with **Up/Down**, **Page Up/Down**, **Home/End**, and press **M** to pick the subagent model.
|
|
123
|
+
**Agents** lists runs as a tree with state, elapsed time, current tool, and last activity. Navigate with **Up/Down**, **Page Up/Down**, **Home/End**, and press **M** to pick the subagent model. Press **D** twice to delete the selected agent and the agents it started, with their transcripts. An agent that is still working is stopped first, and its stop report reaches its parent before it is deleted. The report tells the parent that you dropped the task, so it does not delegate it again. The main agent can no longer resume a deleted agent. Deleting a connected window that is still open stops it like closing the window; its handoff still reaches the main agent, but the transcript the handoff points to is deleted.
|
|
122
124
|
|
|
123
125
|
**Enter** swaps the screen to that agent's conversation, drawn with Pi's own chat components, so it reads like the main chat: its task, replies, and collapsed tool calls, following live output. Typing and **Enter** send it guidance. Guidance from the main agent and reports from the agent's own agents show in labelled boxes, so only your own messages look typed. **Escape** goes back to the main chat.
|
|
124
126
|
|
|
@@ -137,7 +139,7 @@ Guidance shows as queued until delivered, or undelivered if the child stops firs
|
|
|
137
139
|
|
|
138
140
|

|
|
139
141
|
|
|
140
|
-
**Activity** is a memory gauge: how many messages the profile holds and how much of the 128 KB view they fill, then either **Settled** or **Catching up · 12 of 40 summaries** with a progress bar counted from when the backlog last grew from empty. If summarizing keeps failing, the last error and the retry countdown show under it. It also counts running agents, and interrupted ones waiting for you; their list is on Agents. While summaries or agents are at work, the bar's Activity item gets a **●**.
|
|
142
|
+
**Activity** is a memory gauge: how many messages the profile holds and how much of the 128 KB view they fill, then either **Settled** or **Catching up · 12 of 40 summaries** with a progress bar counted from when the backlog last grew from empty. If summarizing keeps failing, the last error and the retry countdown show under it. A turn waiting for summaries shows the same error next to its spinner, usually a summarizer model you aren't logged in to (`/optchat model`). Once every summary it waits for has failed, the turn goes on, with "(not summarized yet: zoom it)" in place of the missing lines, and the failed summaries keep retrying in the background. It also counts running agents, and interrupted ones waiting for you; their list is on Agents. While summaries or agents are at work, the bar's Activity item gets a **●**.
|
|
141
143
|
|
|
142
144
|

|
|
143
145
|
|
|
@@ -150,14 +152,14 @@ Guidance shows as queued until delivered, or undelivered if the child stops firs
|
|
|
150
152
|
|
|
151
153
|
Pick the destination profile, then run `/optchat import`.
|
|
152
154
|
|
|
153
|
-
1. **Source**: Claude Code (`~/.claude/projects`), Claude Code memories, Codex (`~/.codex/sessions`, `~/.codex/archived_sessions`), or a ChatGPT export (ZIP, folder, or `conversations.json`; ZIP
|
|
154
|
-
2. **Select**: for Claude Code, its memories, and
|
|
155
|
+
1. **Source**: Claude Code (`~/.claude/projects`), Claude Code memories, Codex (`~/.codex/sessions`, `~/.codex/archived_sessions`), Pi / OMP (`~/.pi/agent/sessions`, `~/.omp/agent/sessions`, and each OMP profile's `~/.omp/profiles/<name>/agent/sessions`; both write the same session format), or a ChatGPT export (ZIP, folder, or `conversations.json`; a ZIP is read with `unzip` on macOS/Linux and the built-in `tar.exe` on Windows). Scanning is local and makes no model calls.
|
|
156
|
+
2. **Select**: for Claude Code, its memories, Codex, and Pi / OMP, pick projects (busiest first), optionally filter by start date, then take all conversations or pick some. **Tab** toggles (and **Space** when the filter is empty), **Enter** continues, type to filter, **Ctrl+A**/**Ctrl+D** select/clear matches, **Esc** cancels. Nothing is classified as work or personal for you.
|
|
155
157
|
3. **Mode** (only if the profile already has history):
|
|
156
158
|
- **Append**: keep existing summaries and add the import. Faster and cheaper.
|
|
157
159
|
- **Rebuild by conversation start date**: regenerate the whole tree, ordered by conversation start.
|
|
158
160
|
4. **Preview**: destination, new and duplicate counts, text size, rough token estimate, and compactor. This is not a price quote: a big import costs about 3x that estimate in compactor input, because every message is summarized and then re-read in merges, each with the memory view as (mostly cached) context.
|
|
159
161
|
|
|
160
|
-
**What gets imported**: user messages and final assistant replies, with original dates and source labels, as in Victor's recipe. Tool calls and results, intermediate commentary, reasoning, subagent transcripts, replayed context, and image/audio/file bytes are left out. So is the output Claude Code logs for slash and shell commands; the command itself stays as typed (`/name args` or `!command`). So are the context messages Codex injects, such as the AGENTS.md instructions and the environment context. Dropped text never becomes a conversation's title. ChatGPT alternate branches are labelled as alternatives. Imported records are marked as historical so old requests are not treated as new instructions.
|
|
162
|
+
**What gets imported**: user messages and final assistant replies, with original dates and source labels, as in Victor's recipe. Tool calls and results, intermediate commentary, reasoning, subagent transcripts, replayed context, and image/audio/file bytes are left out. So is the output Claude Code logs for slash and shell commands; the command itself stays as typed (`/name args` or `!command`). So are the context messages Codex injects, such as the AGENTS.md instructions and the environment context, and the messages OMP injects (reminders, background job notices). Pi / OMP keep `/skill:name args`, `!command` and `$code` as typed, without the skill's text or the output; `!!` and `$$` commands, kept from the model, stay out. Dropped text never becomes a conversation's title. ChatGPT alternate branches are labelled as alternatives, and so are Pi / OMP branches left by a rewind (`[alternate branch]`). Pi sessions that ran under OptChat are skipped: their messages are already in a profile's memory. A forked session adds only its new messages. Imported records are marked as historical so old requests are not treated as new instructions.
|
|
161
163
|
|
|
162
164
|
**Claude Code memories**: the auto-memory topic files in `~/.claude/projects/*/memory/` (not `MEMORY.md`, which only indexes them), picked by project. Each file becomes one dated note in the memory tree, not part of the prompt. An edited file comes in again as a newer note.
|
|
163
165
|
|
|
@@ -208,28 +210,39 @@ Each profile folder is a local Git repository, committed after each turn and on
|
|
|
208
210
|
|
|
209
211
|
To delete a profile, delete its folder. Your original Pi sessions are kept in Pi's normal session directory.
|
|
210
212
|
|
|
213
|
+
## Windows
|
|
214
|
+
|
|
215
|
+
The same install and commands work on Windows, and the offline tests run there too. What differs:
|
|
216
|
+
|
|
217
|
+
- **Named pipes, not socket files.** The profile lock and the connected-window bridge listen on `\\.\pipe\optchat-<hash>-lock` and `optchat-<hash>-windows`, where the hash comes from the profile path: a Windows pipe has no filesystem entry inside the profile, so the hash is the only thing separating profiles. Pipes are machine-wide and carry no file permissions, so a Windows profile is not permission-protected the way a POSIX socket file (mode 0600) is.
|
|
218
|
+
- **ZIP exports** are read with Windows' own `tar.exe` (in System32 since Windows 10) instead of `unzip`; nothing is extracted to disk.
|
|
219
|
+
- **Home paths.** `~` in a subagent task directory is your home directory; `~\project` is read as a home path only on Windows.
|
|
220
|
+
- **File modes and fsync** (profile folder 0700, socket 0600, directory fsync after a rename) are POSIX-only and are skipped on Windows.
|
|
221
|
+
- Files are opened with `explorer.exe` instead of `open` or `xdg-open`.
|
|
222
|
+
|
|
211
223
|
## Good to know
|
|
212
224
|
|
|
213
225
|
- **Profiles separate memory and instructions only.** Agents keep full filesystem access and share provider credentials.
|
|
214
226
|
- **Tab title**: the terminal tab shows the profile and what it is doing: `π personal` while waiting for you, `● π personal` while the agent works, plus `· 2 agents` while subagents run. A connected window shows `↳ personal`, `● ↳ personal`, then `↳ personal · done` or `↳ personal · disconnected`. OptChat replaces Pi's default title and puts its own back when Pi resets it (new session, reload, rename).
|
|
215
227
|
- **Use worktrees** when parallel agents edit the same repository; they share the filesystem.
|
|
216
228
|
- **Instructions**: the main agent and subagents get Pi's usual global and repository `AGENTS.md` files and your skills, followed by the profile's `AGENTS.md`, which comes last and wins. OptChat replaces only Pi's opening prompt. Prompt templates work in the main session only.
|
|
217
|
-
- **Images**
|
|
229
|
+
- **Images** you paste, or a tool returns, are kept in the profile's memory folder under `images/` (once each, shrunk to 2,048 px on the long side and about 1.5 MB with Pi's own resizer; smaller ones stay as they are). The log names each as `[image <hash>]`, and `zoom(id, 1)` returns a message's images with its text, so the agent can look again in a later turn. Summaries stay text. Imported conversations keep a text placeholder.
|
|
218
230
|
- **Pi's auto-compaction is off.** A single very long run can still hit the model's context limit; stop it and continue in a new turn.
|
|
219
231
|
- **Restarts**: unsent inputs are recovered into memory, and pending subagent reports are delivered. Interrupted subagents are not restarted.
|
|
220
232
|
- **Prompt-template inputs** can be saved twice: expanded, and later in their original form as an unanswered input, because Pi expands them after the input journal records them. Skill commands (`/skill:name`) are matched back to their journaled input and don't have this problem. Plain text chat is unaffected.
|
|
221
233
|
|
|
222
234
|
## How it differs from the recipe
|
|
223
235
|
|
|
224
|
-
|
|
236
|
+
`src/prompts.ts` holds the recipe's prompts, lightly adapted. The main agent's turn rules are the revised recipe's (2026-10-08) word for word, without its paragraph on computers, and with reports starting "[id] " instead of "[Name]"; the view doc keeps the original's description of the view, drops its "No message appears in full" line (untrue with Previous exchange on) and its list of when to zoom, adds one sentence on zooming out from a message to the summaries above it, and ends with the revised recipe's rule that the view is the truth and zoom the only way through it; the compactor prompt is the revised recipe's intro, view and Compactions sections, with OptChat's kinds (`talk` for replies, subagent reports as `work`) and without its turn-only sections; unlike the recipe, the compactor doesn't get your instructions, since an A/B found they added nothing (#112); and the subagent prompt is the original's. OptChat keeps the recipe's numbers: 512-byte summary nodes (by default summaries up to 640 bytes are accepted without a retry, as long as they are smaller than what they replace; see Summary size tolerance), a memory view that grows a line per message and, past 128,000 bytes, merges in one batch down to 64,000 (so between batches it only grows at its end and each turn reads the last one's view from the cache; the view is saved and reloaded as it was), binary merges, 8 compression workers, fixed retry delays, 5 shortening attempts, and a 30,000-character tool output cap. Compactions get their own view, as in the 2026-10-08 revision of the recipe: the memory view merged further, to between 16,000 and 32,000 bytes with the same sawtooth, ending at the line being built. A message's summary starts once fewer than 8 lines before it are unsummarized, so up to 8 compactions run at once on one cached view. An import logs its messages the same way, waiting only while 8 messages and due merges are unbuilt, so the merges keep up. Where the recipe ends a compaction's view at the first line not summarized yet, OptChat shows that line as a placeholder and keeps the lines after it: cutting there also hid the short lines that follow, such as an echo's own tool call, and an A/B on real turns scored those summaries lower (see #101). The compactor's task and its "Too long" retry are the recipe's, verbatim, with a 512-dash ruler for the size. Anthropic requests split the view into blocks of 4 lines with cache marks on the last whole one and on the blocks 20 and 40 before it, so a turn that adds up to 240 lines of tool calls and results still reads the last view from the cache, and when that view is not cached yet, one compactor call goes first and the others wait until it starts answering, so they read the cache instead of each writing it. See `docs/victor-recipe.md` for notes.
|
|
225
237
|
|
|
226
238
|
Each run's context is the memory view, the previous exchange, and your new message. Deliberate additions (the ones that change the recipe's behaviour are settings, see [Settings](#settings)):
|
|
227
239
|
|
|
228
240
|
1. **Previous exchange kept verbatim.** Your last request (with any steering) and the final answer are included in full, so "why is that?" refers to what you actually read. Tool calls and reasoning are not carried over. It comes on top of the 128,000-byte view. If it is over 16,000 bytes (about 4,000 tokens, usually a big paste; Previous exchange limit) it is left out entirely, and the model relies on the view and zoom as in Victor's recipe. A new Pi session starts with the memory view only.
|
|
229
241
|
2. **Subagents** are built in with Pi's SDK rather than a separate package. With Subagent levels above 1 they can delegate further.
|
|
230
|
-
3. **Memory search** (off by default). With the setting on, the agent also gets `search(text, before?)`: plain, case-insensitive text matching over the original messages, never the summaries (a summary can be wrong, and one fact repeats at every level of the tree), skipping logged zoom and search results. It returns 20 hits at a time, newest first, each with its id, the view line that holds it when that is a summary (`1234 (in 1024+256)`), its date and a snippet; `before: id` pages back, and `zoom(id, 1)` reads a hit. One line about it is added to the system prompt. Turning it on or off changes the cached prompt once.
|
|
231
|
-
4. **
|
|
232
|
-
5. **
|
|
242
|
+
3. **Memory search** (off by default). With the setting on, the agent also gets `search(text, before?)`: plain, case-insensitive text matching over the original messages, never the summaries (a summary can be wrong, and one fact repeats at every level of the tree), skipping logged zoom and search results. It returns 20 hits at a time, newest first, each with its id, the view line that holds it when that is a summary (`1234 (in 1024+256)`), its date and a snippet; `before: id` pages back, and `zoom(id, 1)` reads a hit. One line about it is added to the system prompt, and the rule that zoom is the only way through memory names search too. Turning it on or off changes the cached prompt once.
|
|
243
|
+
4. **Large messages.** `zoom(id, 1)` returns a message over 25,000 characters in pages, `[showing characters 0-25000 of 92000; next page: offset 25000]`, and `offset`/`limit` read any part of one (used on a shorter message, the page adds a note that it fits in one zoom); the recipe returns it whole, where the 30,000-character tool output cap cut out its middle.
|
|
244
|
+
5. **Profiles**, the **inspector**, the **usage ledger**, **import**, and **connected windows** are additions. Import adds historical-record guidance to the prompts.
|
|
245
|
+
6. **Not done**: computer use and hosting on an always-on machine.
|
|
233
246
|
|
|
234
247
|
## Development
|
|
235
248
|
|
|
@@ -261,6 +274,6 @@ The workflow checks that the tag matches `package.json`, runs the type check and
|
|
|
261
274
|
|
|
262
275
|
## Credits and license
|
|
263
276
|
|
|
264
|
-
Based on [Victor Taelin's OptChat recipe](https://gist.github.com/VictorTaelin/91837951a5ce5b38f341ec1ba1df6449) and [OptMem](https://github.com/VictorTaelin/OptMem). This is an independent Pi implementation, not Victor's official OptChat.
|
|
277
|
+
Based on [Victor Taelin's OptChat recipe](https://gist.github.com/VictorTaelin/91837951a5ce5b38f341ec1ba1df6449) and [OptMem](https://github.com/VictorTaelin/OptMem). His revised gist (2026-10-08) calls his own version UniiChat. This is an independent Pi implementation, not Victor's official OptChat.
|
|
265
278
|
|
|
266
279
|
MIT licensed. See [LICENSE](LICENSE) and [third-party notices](THIRD_PARTY_NOTICES.md).
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Victor Taelin's OptChat recipe
|
|
4
4
|
|
|
5
|
-
The OptChat memory design and the
|
|
6
|
-
come from Victor Taelin's publicly shared implementation recipe:
|
|
5
|
+
The OptChat memory design and the prompt strings in `src/prompts.ts`
|
|
6
|
+
(adapted) come from Victor Taelin's publicly shared implementation recipe:
|
|
7
7
|
|
|
8
8
|
https://gist.github.com/VictorTaelin/91837951a5ce5b38f341ec1ba1df6449
|
|
9
9
|
|
package/docs/victor-recipe.md
CHANGED
|
@@ -6,16 +6,16 @@ Source: https://gist.github.com/VictorTaelin/91837951a5ce5b38f341ec1ba1df6449
|
|
|
6
6
|
|
|
7
7
|
The implementation was compared with the recipe fetched on 2026-10-04. The reference content SHA-256 was `8f6997e8944d85e4df53b5704bf7c4e393e4da361071181e9cc2f7d9d1b6e430`.
|
|
8
8
|
|
|
9
|
-
The source remains upstream rather than duplicating the full article here. Its
|
|
9
|
+
The source remains upstream rather than duplicating the full article here. Its prompt strings, from the original and the 2026-10-08 revision, are in `src/prompts.ts` with the small adaptations the README lists, and attribution in `THIRD_PARTY_NOTICES.md`.
|
|
10
10
|
|
|
11
11
|
## Implementation mapping
|
|
12
12
|
|
|
13
|
-
- `src/memory.ts`: append-only log, binary summary tree, compression scheduling,
|
|
14
|
-
- `src/compactor.ts`: contextual compression and size retries.
|
|
15
|
-
- `src/cache.ts`:
|
|
13
|
+
- `src/memory.ts`: append-only log, binary summary tree, compression scheduling (§4 of the 2026-10-08 revision: a message's summary starts once fewer than 8 lines before it are unbuilt, a merge once both halves are built, kept in a queue so the tree is never scanned for work), the compactions' own view (§4: the view merged further to 16,000-32,000 bytes, ending at the node; unlike the recipe, a line not built yet shows as a placeholder instead of ending it, so a summary still sees the built lines after it), the view (§3.2: the most due pair, measured from its last message, merges in one batch from 128,000 down to 64,000 bytes, and the view is saved to `view.json` rather than refolded at start), zoom/date, and the opt-in text search over original messages (`src/tools.ts` has the tool; not in the recipe, off by default).
|
|
14
|
+
- `src/compactor.ts`: contextual compression and size retries, with the 2026-10-08 revision's task (a 512-dash ruler for the size) and its "Too long" retry, verbatim. The compactions' view shows each line under its `id+n|` head, as the task's line ids need, so a reply that copies a head has it removed.
|
|
15
|
+
- `src/cache.ts`: Anthropic cache marks: the view in blocks of 4 lines, one mark on the last whole block and one at the request's end, as in recipe §3.3, plus two marks 20 and 40 blocks before the last. Anthropic looks back only 20 blocks from a mark, so with the recipe's one view mark a turn that added more than 80 lines, such as 40 tool calls and their results, made the next turn rewrite the whole view. OpenAI requests get no marks: GPT-5.6 and GPT-6 Luna reject the recipe's `prompt_cache_breakpoint` with a 400 (found by @aaaxn), so they rely on implicit prefix caching. OptChat doesn't set `reasoning.context` either: GPT-5.6 already defaults to the recipe's `"all_turns"`, and OpenAI documents it only for GPT-5.6 and GPT-6.1 Sol.
|
|
16
16
|
- `src/transcript.ts`: fresh context per parent run, current-run tool loop retained. The previous completed exchange is also retained in full text (left out if over 16,000 bytes by default), an intentional addition to the summary-only recipe for conversational continuity.
|
|
17
17
|
- `src/agents.ts`: asynchronous Pi SDK children and automatic completion reports.
|
|
18
|
-
- `src/settings.ts`: per-profile settings for the departures from the recipe. Defaults are the recipe's (
|
|
18
|
+
- `src/settings.ts`: per-profile settings for the departures from the recipe. Defaults are the recipe's (no memory search), except the previous exchange (on), the summary size tolerance (640 bytes, against the recipe's strict 512) and grouped subagent reports (on, where the revised recipe reports each subagent on its own).
|
|
19
19
|
- `src/import/`: profile-scoped historical imports retain user messages and final assistant replies, following the lighter history described in recipe section 10. An import hands `Memory` one message at a time, each once the one before it is summarized, so the compactor sees the view a live chat would have shown it. Source adapters, final-reply detection, replay filtering, and ChatGPT branch labels are integration choices. Live-chat tool logging remains unchanged.
|
|
20
20
|
|
|
21
21
|
Profiles, native Pi UI, conversation import, and local Git checkpoints are integration choices described in the README.
|
package/package.json
CHANGED
package/src/agents.ts
CHANGED
|
@@ -7,7 +7,7 @@ import * as sdk from '@earendil-works/pi-coding-agent';
|
|
|
7
7
|
import type { AgentMessage } from '@earendil-works/pi-agent-core';
|
|
8
8
|
import type { Api, Model } from '@earendil-works/pi-ai';
|
|
9
9
|
import { SUBAGENT, VIEW_DOC } from './prompts.ts';
|
|
10
|
-
import { memoryTools, SEARCH_DOC, searchTool } from './tools.ts';
|
|
10
|
+
import { allowSearch, memoryTools, SEARCH_DOC, searchTool } from './tools.ts';
|
|
11
11
|
import { type Memory } from './memory.ts';
|
|
12
12
|
import type { ModelChoice } from './compactor.ts';
|
|
13
13
|
import { cachePayload } from './cache.ts';
|
|
@@ -77,6 +77,7 @@ export class Children {
|
|
|
77
77
|
private closing = false;
|
|
78
78
|
private launching = 0;
|
|
79
79
|
private readonly resuming = new Set<string>();
|
|
80
|
+
private readonly deleting = new Set<string>();
|
|
80
81
|
private settling = 0;
|
|
81
82
|
private readonly launches = new Set<Promise<unknown>>();
|
|
82
83
|
private readonly completions = new Set<Promise<void>>();
|
|
@@ -229,7 +230,7 @@ export class Children {
|
|
|
229
230
|
if (provider) pi.registerProvider(o.provider, provider);
|
|
230
231
|
// Same prompt as the main agent (AGENTS.md files, skills, cwd); only the OptChat preamble differs.
|
|
231
232
|
pi.on('before_agent_start', event => {
|
|
232
|
-
event.systemPromptOptions.customPrompt = `${SUBAGENT}\n\n${VIEW_DOC}${memorySearch ? SEARCH_DOC : ''}
|
|
233
|
+
event.systemPromptOptions.customPrompt = allowSearch(`${SUBAGENT}\n\n${VIEW_DOC}${memorySearch ? SEARCH_DOC : ''}`, memorySearch);
|
|
233
234
|
event.systemPromptOptions.sections.instructions = instructions;
|
|
234
235
|
});
|
|
235
236
|
pi.on('before_provider_request', (event, ctx) => ctx.model?.api === 'anthropic-messages' ? cachePayload(event.payload) : event.payload);
|
|
@@ -368,6 +369,8 @@ export class Children {
|
|
|
368
369
|
await this.deliverHandoff(info);
|
|
369
370
|
return;
|
|
370
371
|
}
|
|
372
|
+
// Said plainly, so the parent does not take the user's choice for a failure and delegate the task again.
|
|
373
|
+
if (this.deleting.has(info.id)) info.report = 'Deleted by the user while it was working. The user dropped this task on purpose: do not redo it or delegate it again unless they ask.';
|
|
371
374
|
// A metadata failure must not suppress delivery of the actual result.
|
|
372
375
|
try { this.save(info); } catch (error) { this.warn(`Could not save run metadata: ${String(error)}`); }
|
|
373
376
|
let text = `[${info.id}] ${info.report}`;
|
|
@@ -384,16 +387,17 @@ export class Children {
|
|
|
384
387
|
if (parent && parent.info.state !== 'stopping') { parent.pendingReports.push(text); this.changed(); }
|
|
385
388
|
return;
|
|
386
389
|
}
|
|
387
|
-
if (this.closing) this.memory.append('
|
|
390
|
+
if (this.closing) this.memory.append('work', text);
|
|
388
391
|
else try { await this.report(text, { count }); }
|
|
389
392
|
catch (error) {
|
|
390
|
-
this.memory.append('
|
|
393
|
+
this.memory.append('work', text);
|
|
391
394
|
this.warn(`Subagent report saved but could not wake the parent: ${String(error)}`);
|
|
392
395
|
}
|
|
393
396
|
if (batch && batch.ids.length > 1) this.options.hold?.(batch.ids[0], []);
|
|
394
397
|
}
|
|
395
398
|
/** `caller` is the agent sending a manager message: undefined for the main agent, else the parent subagent's ID. */
|
|
396
399
|
async tell(id: string, message: string, source: 'manager' | 'user' = 'manager', caller?: string) {
|
|
400
|
+
if (this.deleting.has(id)) throw new Error(`${id} is being deleted by the user.`);
|
|
397
401
|
const live = this.running.get(id);
|
|
398
402
|
if (!live && source === 'manager' && this.history.records.has(id)) return this.track(this.resume(id, message, caller));
|
|
399
403
|
if (live && !['running', 'waiting', 'paused'].includes(live.info.state)) throw new Error(`${id} is finishing. Its report will arrive on its own${live.info.connected || source === 'user' ? '' : '; tell it again after that to resume it'}.`);
|
|
@@ -565,6 +569,30 @@ export class Children {
|
|
|
565
569
|
}
|
|
566
570
|
await Promise.allSettled(stopping.map(run => run.session.abort()));
|
|
567
571
|
}
|
|
572
|
+
/** The user's delete: stops the run and its descendants if they still work, lets their reports reach their parents, then deletes their files. */
|
|
573
|
+
async remove(id: string) {
|
|
574
|
+
if (!this.history.records.has(id)) throw new Error(`No subagent ${id}.`);
|
|
575
|
+
if (this.deleting.has(id)) throw new Error(`${id} is already being deleted.`);
|
|
576
|
+
const runs = () => [id, ...this.history.descendants(id).map(run => run.id)], marked = new Set<string>();
|
|
577
|
+
// An overlapping delete of a descendant keeps its own marks, so neither delete clears the other's.
|
|
578
|
+
const mark = () => { for (const run of runs()) if (!this.deleting.has(run)) { marked.add(run); this.deleting.add(run); } };
|
|
579
|
+
mark(); this.changed();
|
|
580
|
+
try {
|
|
581
|
+
// Launches and resumes register their runs before they settle, and a stopped parent can still have children launching.
|
|
582
|
+
for (;;) {
|
|
583
|
+
await Promise.allSettled(this.launches);
|
|
584
|
+
const live = runs().flatMap(run => this.running.get(run) ?? []);
|
|
585
|
+
if (!live.length) break;
|
|
586
|
+
mark();
|
|
587
|
+
await Promise.allSettled(live.map(run => this.stop(run.info.id)));
|
|
588
|
+
await Promise.allSettled(live.map(run => run.completion));
|
|
589
|
+
}
|
|
590
|
+
mark();
|
|
591
|
+
const records = runs().flatMap(run => this.history.records.get(run) ?? []);
|
|
592
|
+
if (records.some(run => run.connected && !run.handoff?.delivered)) throw new Error(`${id} has a connected conversation whose handoff has not reached the main agent yet, so it was kept.`);
|
|
593
|
+
for (const run of records.reverse()) this.history.remove(run.id); // Children first, so a failure never leaves one without its parent.
|
|
594
|
+
} finally { for (const run of marked) this.deleting.delete(run); this.changed(); }
|
|
595
|
+
}
|
|
568
596
|
async close() {
|
|
569
597
|
this.closing = true;
|
|
570
598
|
await Promise.allSettled([...this.running.keys()].map(id => this.stop(id)));
|
package/src/browser.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { join } from 'node:path';
|
|
2
|
-
import { atomicWrite } from './
|
|
2
|
+
import { atomicWrite } from './memory.ts';
|
|
3
3
|
import { type Memory } from './memory.ts';
|
|
4
4
|
|
|
5
5
|
// One self-contained, read-only HTML snapshot of a profile's memory.
|
|
@@ -82,12 +82,14 @@ function when(a, b) {
|
|
|
82
82
|
if (lo === hi) return day(x) + ', ' + time(x);
|
|
83
83
|
return x.toDateString() === y.toDateString() ? day(x) + ', ' + time(x) + '–' + time(y) : day(x) + ' ' + time(x) + ' – ' + day(y) + ' ' + time(y);
|
|
84
84
|
}
|
|
85
|
-
const SOURCE = { claude: 'Claude Code', 'claude-memory': 'Claude Code memory', codex: 'Codex', chatgpt: 'ChatGPT' };
|
|
85
|
+
const SOURCE = { claude: 'Claude Code', 'claude-memory': 'Claude Code memory', codex: 'Codex', pi: 'Pi / OMP', chatgpt: 'ChatGPT' };
|
|
86
86
|
// Imported text starts with a "[Historical …]" line for the model; the meta line already says it.
|
|
87
87
|
const body = e => { const j = e.origin && e.text.startsWith('[Historical ') ? e.text.indexOf(']\n') : -1; return j < 0 ? e.text : e.text.slice(j + 2); };
|
|
88
88
|
const short = (s, n) => { s = s.replace(/\s+/g, ' ').trim(); return s.length > n ? s.slice(0, n - 1) + '…' : s; };
|
|
89
89
|
function who(e) {
|
|
90
|
-
|
|
90
|
+
// Logs from before the work kind held reports as user messages starting "[id] "; another extension's shown custom message is tagged the same way.
|
|
91
|
+
if (e.kind === 'work' || e.kind === 'user' && /^\[[\w.:-]+\] /.test(e.text)) return ['Subagent', 'work', 'subagent report'];
|
|
92
|
+
if (e.kind === 'user') return ['You', 'you', 'from you'];
|
|
91
93
|
if (e.kind === 'talk') return ['OptChat', 'talk', 'reply'];
|
|
92
94
|
if (e.kind === 'tool') return ['Tool call', 'tool', 'tool step'];
|
|
93
95
|
if (e.kind === 'echo') return ['Tool result', 'tool', 'tool step'];
|
package/src/cache.ts
CHANGED
|
@@ -4,20 +4,24 @@ export function record(value: unknown): value is Record<string, unknown> {
|
|
|
4
4
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
/**
|
|
7
|
+
/** The view in blocks of 4 lines counted from its start (recipe §3.3), byte for byte. While the view only grows at its end,
|
|
8
|
+
* every block but the last is the same in the next call. */
|
|
8
9
|
export function splitView(text: string) {
|
|
9
10
|
const pieces: string[] = [];
|
|
10
11
|
let offset = 0;
|
|
11
|
-
for (
|
|
12
|
-
if (
|
|
13
|
-
const cut = text.lastIndexOf('\n', mark) + 1;
|
|
14
|
-
if (cut > offset) { pieces.push(text.slice(offset, cut)); offset = cut; }
|
|
15
|
-
}
|
|
12
|
+
for (let count = 0, at = text.indexOf('\n'); at >= 0; at = text.indexOf('\n', at + 1))
|
|
13
|
+
if (++count % 4 === 0) { pieces.push(text.slice(offset, at + 1)); offset = at + 1; }
|
|
16
14
|
pieces.push(text.slice(offset));
|
|
17
15
|
return pieces;
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
/**
|
|
18
|
+
/** How many blocks before the view's last whole one each mark sits. Anthropic looks back only 20 blocks from a mark for an
|
|
19
|
+
* earlier entry, so the marks 20 and 40 blocks back still find the last call's entry after a turn of tool calls adds up to
|
|
20
|
+
* 60 blocks (240 lines). With the end of the request, that is Anthropic's limit of 4 marks. */
|
|
21
|
+
const MARKS = [0, 20, 40];
|
|
22
|
+
|
|
23
|
+
/** Anthropic: marks on the view (see MARKS) plus automatic end-of-request caching, so the next call finds the last one's entry
|
|
24
|
+
* and pays only for the lines after it. */
|
|
21
25
|
export function cachePayload(payload: unknown): unknown {
|
|
22
26
|
if (!record(payload) || !Array.isArray(payload.messages)) return payload;
|
|
23
27
|
const messages = payload.messages;
|
|
@@ -26,14 +30,14 @@ export function cachePayload(payload: unknown): unknown {
|
|
|
26
30
|
if (!record(message) || message.role !== 'user' || !Array.isArray(message.content)) continue;
|
|
27
31
|
const at = message.content.findIndex((block: unknown) => record(block) && block.type === 'text' && typeof block.text === 'string' && isView(block.text));
|
|
28
32
|
if (at < 0) continue;
|
|
29
|
-
const pieces = splitView(message.content[at].text);
|
|
30
|
-
const blocks = pieces.map((text, j) => ({ type: 'text', text, ...(j
|
|
33
|
+
const pieces = splitView(message.content[at].text), marked = new Set(MARKS.map(back => pieces.length - 2 - back));
|
|
34
|
+
const blocks = pieces.map((text, j) => ({ type: 'text', text, ...(marked.has(j) ? { cache_control: { type: 'ephemeral' } } : {}) }));
|
|
31
35
|
message.content.splice(at, 1, ...blocks);
|
|
32
36
|
for (const block of blocks) view.add(block);
|
|
33
37
|
break;
|
|
34
38
|
}
|
|
35
39
|
if (!view.size) return payload;
|
|
36
|
-
// Pi's
|
|
40
|
+
// These marks only: Pi's own system, tool and recent-message marks are dropped.
|
|
37
41
|
for (const section of [payload.system, payload.tools]) {
|
|
38
42
|
if (Array.isArray(section)) for (const item of section) if (record(item)) delete item.cache_control;
|
|
39
43
|
}
|
package/src/checkpoint.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { execFile } from 'node:child_process';
|
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
3
|
import { existsSync } from 'node:fs';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { atomicWrite } from './
|
|
5
|
+
import { atomicWrite } from './memory.ts';
|
|
6
6
|
const exec = promisify(execFile);
|
|
7
7
|
|
|
8
8
|
export async function checkpoint(directory: string) {
|
package/src/compactor.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { clampThinkingLevel, type Api, type AssistantMessage, type Message, type
|
|
|
2
2
|
import type { ThinkingLevel } from '@earendil-works/pi-agent-core';
|
|
3
3
|
import type { ModelRegistry } from '@earendil-works/pi-coding-agent';
|
|
4
4
|
import { COMPACT } from './prompts.ts';
|
|
5
|
-
import { bytes, NODE, type Compressor } from './memory.ts';
|
|
5
|
+
import { bytes, NODE, start, type Compressor, type Part } from './memory.ts';
|
|
6
6
|
import { cachePayload, splitView } from './cache.ts';
|
|
7
7
|
import { IMPORT_GUIDANCE } from './import/guidance.ts';
|
|
8
8
|
import { DEFAULT_SETTINGS } from './settings.ts';
|
|
@@ -13,16 +13,25 @@ export const reasoningFor = (model: Model<Api>, level: ThinkingLevel) => {
|
|
|
13
13
|
const thinking = clampThinkingLevel(model, level);
|
|
14
14
|
return thinking === 'off' ? undefined : thinking;
|
|
15
15
|
};
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
/** Models can't count bytes, so the task shows the limit as a ruler; a real sample line got its content copied (recipe §4). */
|
|
17
|
+
const RULER = '-'.repeat(NODE);
|
|
18
|
+
const label = (part: Part) => `${start(part)}+${2 ** part.l}`;
|
|
19
|
+
/** The recipe's compaction task, verbatim. */
|
|
20
|
+
export function task({ source, part }: { source: string; part: Part }) {
|
|
21
|
+
if (!part.l) return `Compaction: compress message ${part.i} into one line of at most 512 bytes\n(about 70 words), the length of this ruler:\n${RULER}\n<input>\n${source}\n</input>`;
|
|
22
|
+
const a = { l: part.l - 1, i: 2 * part.i }, b = { l: part.l - 1, i: 2 * part.i + 1 };
|
|
23
|
+
return `Compaction: merge lines ${label(a)} and ${label(b)}, adjacent, into one line of at most\n512 bytes (about 70 words), the length of this ruler:\n${RULER}\n`
|
|
24
|
+
+ `<chat> may hold their messages, ${start(part)} to ${start(part) + 2 ** part.l - 1}, in more detail: take details\nof them from there too.\n<input>\n${source}\n</input>`;
|
|
25
|
+
}
|
|
19
26
|
const WARM_MS = 4 * 60_000; // Anthropic's short cache lives 5 minutes from its last use.
|
|
20
27
|
|
|
21
|
-
/** Parallel calls can't read a cache entry that isn't written yet, so one call primes a cold prefix and the rest wait until it answers.
|
|
28
|
+
/** Parallel calls can't read a cache entry that isn't written yet, so one call primes a cold prefix and the rest wait until it answers.
|
|
29
|
+
* Parallel compactions end their views at different messages, so a call also waits for a primer of a shorter prefix of its own view. */
|
|
22
30
|
function primeFirst() {
|
|
23
31
|
const warm = new Map<string, number | Promise<void>>();
|
|
24
32
|
return async (prefix: string, signal: AbortSignal) => {
|
|
25
|
-
for (
|
|
33
|
+
const priming = () => { for (const [k, state] of warm) if (typeof state !== 'number' && prefix.startsWith(k)) return state; };
|
|
34
|
+
for (let state = warm.get(prefix) ?? priming(); state !== undefined; state = warm.get(prefix) ?? priming()) {
|
|
26
35
|
if (typeof state === 'number') { if (Date.now() - state < WARM_MS) break; warm.delete(prefix); continue; }
|
|
27
36
|
// A cancelled waiter leaves at once instead of waiting for someone else's primer.
|
|
28
37
|
signal.throwIfAborted();
|
|
@@ -52,8 +61,7 @@ export function createCompressor(registry: ModelRegistry, choice: () => ModelCho
|
|
|
52
61
|
const model = registry.find(selected.provider, selected.model);
|
|
53
62
|
if (!model) throw new Error(`Compactor model unavailable: ${selected.provider}/${selected.model}. Use /optchat model.`);
|
|
54
63
|
const thinking = reasoningFor(model, selected.thinking);
|
|
55
|
-
|
|
56
|
-
const step = `${input.historical ? IMPORT_GUIDANCE + '\n\n' : ''}For scale only, here is an example line, not from this chat; it is exactly 512 bytes and is never part of your input or your line:\n<example>${SCALE}</example>\n\n${input.merge ? 'Merge these two lines into one' : 'Compress this message into one line'}, in at most 512 bytes:\n<input>\n${input.source}\n</input>`;
|
|
64
|
+
const step = `${input.historical ? IMPORT_GUIDANCE + '\n\n' : ''}${task(input)}`;
|
|
57
65
|
const messages: Message[] = [{ role: 'user', content: [{ type: 'text', text: input.context }, { type: 'text', text: step }], timestamp: Date.now() }];
|
|
58
66
|
const view = splitView(input.context);
|
|
59
67
|
const prefix = model.api === 'anthropic-messages' && view.length > 1 ? `${model.provider}/${model.id}/${thinking ?? 'off'}\n${view.slice(0, -1).join('')}` : undefined;
|
|
@@ -74,14 +82,15 @@ export function createCompressor(registry: ModelRegistry, choice: () => ModelCho
|
|
|
74
82
|
} finally { warmed(false); }
|
|
75
83
|
onUsage(reply);
|
|
76
84
|
if (reply.stopReason === 'error' || reply.stopReason === 'aborted') throw new Error(reply.errorMessage ?? `Compactor ${reply.stopReason}`);
|
|
77
|
-
|
|
85
|
+
// The compactions' view shows each line under its id+n| head, which a line can copy.
|
|
86
|
+
const line = reply.content.filter(c => c.type === 'text').map(c => c.text).join('').trim().replace(/^\d+\+\d+\|\s*/, '');
|
|
78
87
|
if (!line) throw new Error('Compactor returned no text.');
|
|
79
88
|
tries.push(line);
|
|
80
89
|
// A merge of two short lines can come back nearly as big as both, so a line must also shrink what it replaces.
|
|
81
90
|
if (bytes(line) <= accepted() && bytes(line) < bytes(input.source)) break;
|
|
82
91
|
messages.push(reply);
|
|
83
92
|
const cut = Buffer.from(line).subarray(0, NODE).toString('utf8').replace(/\uFFFD$/, '');
|
|
84
|
-
messages.push({ role: 'user', content: `
|
|
93
|
+
messages.push({ role: 'user', content: `Too long: your line is ${bytes(line)} bytes, over the 512-byte limit. Write\nthe whole line again for the same <input>, cutting just enough of the\nleast valuable items to fit before this cut:\n${cut}| ← LIMIT`, timestamp: Date.now() });
|
|
85
94
|
}
|
|
86
95
|
return tries.reduce((a, b) => bytes(a) <= bytes(b) ? a : b);
|
|
87
96
|
};
|
package/src/images.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFileSync, readdirSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import type { ImageContent } from '@earendil-works/pi-ai';
|
|
5
|
+
import { resizeImage } from '@earendil-works/pi-coding-agent';
|
|
6
|
+
import { atomicWrite } from './memory.ts';
|
|
7
|
+
|
|
8
|
+
/** Kept images fit 2048 px on the long side and ~1.5 MB (2 MB as base64), re-encoded as JPEG 85 only when they don't already. */
|
|
9
|
+
export const IMAGE_LIMITS = { maxWidth: 2048, maxHeight: 2048, maxBytes: 2 * 1024 * 1024, jpegQuality: 85 };
|
|
10
|
+
const EXTENSIONS = { 'image/png': 'png', 'image/jpeg': 'jpg', 'image/gif': 'gif', 'image/webp': 'webp' } as const;
|
|
11
|
+
type Mime = keyof typeof EXTENSIONS;
|
|
12
|
+
const isMime = (mime: string): mime is Mime => Object.hasOwn(EXTENSIONS, mime);
|
|
13
|
+
const MIMES = new Map<string, Mime>((Object.keys(EXTENSIONS) as Mime[]).map(mime => [EXTENSIONS[mime], mime]));
|
|
14
|
+
|
|
15
|
+
const hash = (image: ImageContent) => createHash('sha256').update(image.data).digest('hex').slice(0, 16);
|
|
16
|
+
/** How a message's image reads in memory: named by the image as the message holds it, before any shrinking, so it always gets the same name. */
|
|
17
|
+
export const imageRef = (image: ImageContent) => `[image ${hash(image)}]`;
|
|
18
|
+
const REF = /\[image ([0-9a-f]{16})\]/g;
|
|
19
|
+
|
|
20
|
+
export const isImage = (part: unknown): part is ImageContent => typeof part === 'object' && part !== null
|
|
21
|
+
&& 'type' in part && part.type === 'image' && 'data' in part && typeof part.data === 'string' && 'mimeType' in part && typeof part.mimeType === 'string';
|
|
22
|
+
const stored = (directory: string, name: string) => {
|
|
23
|
+
try { return readdirSync(join(directory, 'images')).find(file => file.startsWith(`${name}.`) && MIMES.has(file.slice(name.length + 1))); } catch { return undefined; }
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Keeps each image of a message's content under `images/`, once per distinct image, shrunk to IMAGE_LIMITS. */
|
|
27
|
+
export async function saveImages(directory: string, content: unknown) {
|
|
28
|
+
if (!Array.isArray(content)) return;
|
|
29
|
+
for (const image of content.filter(isImage)) {
|
|
30
|
+
const name = hash(image);
|
|
31
|
+
if (stored(directory, name)) continue;
|
|
32
|
+
// Pi's own resizer (Photon, WASM), as its read tool uses: it returns small images untouched, and null if it can't decode.
|
|
33
|
+
const kept = await resizeImage(Buffer.from(image.data, 'base64'), image.mimeType, IMAGE_LIMITS) ?? image;
|
|
34
|
+
if (isMime(kept.mimeType)) atomicWrite(join(directory, 'images', `${name}.${EXTENSIONS[kept.mimeType]}`), Buffer.from(kept.data, 'base64'));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The kept images a text refers to, in order, each once; ones never kept are skipped. */
|
|
39
|
+
export function loadImages(directory: string, text: string): ImageContent[] {
|
|
40
|
+
return [...new Set(Array.from(text.matchAll(REF), match => match[1]))].flatMap(name => {
|
|
41
|
+
const file = stored(directory, name), mimeType = file && MIMES.get(file.slice(name.length + 1));
|
|
42
|
+
return file && mimeType ? [{ type: 'image' as const, data: readFileSync(join(directory, 'images', file)).toString('base64'), mimeType }] : [];
|
|
43
|
+
});
|
|
44
|
+
}
|
package/src/import/job.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { existsSync, readFileSync, rmSync, cpSync, mkdirSync } from 'node:fs';
|
|
2
|
-
import { join, relative } from 'node:path';
|
|
2
|
+
import { join, relative, sep } from 'node:path';
|
|
3
3
|
import { randomUUID } from 'node:crypto';
|
|
4
4
|
import { Memory, bytes, isEntry, type Entry, type Compressor } from '../memory.ts';
|
|
5
|
-
import { atomicWrite } from '../
|
|
5
|
+
import { atomicWrite } from '../memory.ts';
|
|
6
6
|
import { record } from '../cache.ts';
|
|
7
7
|
import { copyKey, type ImportedEntry } from './sources.ts';
|
|
8
8
|
|
|
@@ -76,8 +76,12 @@ export function prepareImport(dir: string, old: Memory, incoming: readonly Impor
|
|
|
76
76
|
for (const sub of ['main', 'tree']) mkdirSync(join(path, sub), { recursive: true, mode: 0o700 });
|
|
77
77
|
if (kept) atomicWrite(join(path, 'main', '000-import.jsonl'), entries.slice(0, kept).map(e => JSON.stringify(e)).join('\n') + '\n');
|
|
78
78
|
atomicWrite(join(path, STAGED), entries.map(e => JSON.stringify(e)).join('\n') + '\n');
|
|
79
|
-
if (mode === 'append')
|
|
80
|
-
|
|
79
|
+
if (mode === 'append') {
|
|
80
|
+
cpSync(join(old.directory, 'tree'), join(path, 'tree'), { recursive: true });
|
|
81
|
+
// The old messages keep their ids, so their saved view (and its prompt cache) still holds.
|
|
82
|
+
if (existsSync(join(old.directory, 'view.json'))) cpSync(join(old.directory, 'view.json'), join(path, 'view.json'));
|
|
83
|
+
}
|
|
84
|
+
const job: ImportJob = { id, mode, previous: relative(dir, old.directory).split(sep).join('/') || '.', target,
|
|
81
85
|
created: new Date().toISOString(), added: added.length, skipped, total: entries.length,
|
|
82
86
|
inputBytes: (mode === 'rebuild' ? entries : added).reduce((n, e) => n + bytes(e.text), 0) };
|
|
83
87
|
atomicWrite(pendingFile(dir), JSON.stringify(job, null, 2));
|
|
@@ -107,9 +111,10 @@ export async function runImport(dir: string, compress: Compressor, signal: Abort
|
|
|
107
111
|
try {
|
|
108
112
|
if (memory.root.length > plan.length || memory.root.some((e, i) => e.text !== plan[i].text)) throw new Error('Import staging data does not match its log; original memory remains intact.');
|
|
109
113
|
report();
|
|
110
|
-
// Recipe §10: imported messages are compressed like any other
|
|
111
|
-
|
|
112
|
-
await memory.settle(signal,
|
|
114
|
+
// Recipe §10: imported messages are compressed like any other. Each is logged once its node can start, as a burst of live
|
|
115
|
+
// messages is, so up to 8 summaries run at once. Unlike a turn, an import waits through failures (its progress shows the error).
|
|
116
|
+
for (const e of plan.slice(memory.root.length)) { await memory.settle(signal, 'ahead'); memory.append(e.kind, e.text, e.date, e.receipt, e.origin); }
|
|
117
|
+
await memory.settle(signal, 'tree'); signal.throwIfAborted();
|
|
113
118
|
// Close all writers before the single atomic pointer swap. The previous generation stays intact.
|
|
114
119
|
await memory.close();
|
|
115
120
|
atomicWrite(join(dir, 'imports', `${job.id}.json`), JSON.stringify({ ...job, completed: new Date().toISOString() }, null, 2));
|