notesoup 0.59.2 → 0.60.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 +3 -17
- package/dist/plugins/claude/src/helper.js +1 -2
- package/dist/plugins/codex/src/mcp.js +1 -2
- package/dist/plugins/dsh/tools.js +1 -2
- package/dist/plugins/pi/notes/tools.d.ts +1 -1
- package/dist/plugins/pi/notes/tools.js +2 -3
- package/dist/src/history/history.d.ts +10 -11
- package/dist/src/history/history.js +8 -19
- package/dist/src/history/query.d.ts +3 -14
- package/dist/src/history/query.js +73 -85
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/notes/address.js +3 -3
- package/dist/src/notes/store.d.ts +2 -4
- package/dist/src/notes/store.js +1 -2
- package/dist/src/tools/history.d.ts +4 -6
- package/dist/src/tools/history.js +69 -29
- package/dist/src/tools/notes.d.ts +18 -78
- package/dist/src/tools/notes.js +39 -50
- package/dist/src/tools/schema.d.ts +0 -1
- package/dist/src/tools/schema.js +0 -1
- package/dist/test/claude-history.test.js +5 -6
- package/dist/test/claude-mod.test.js +6 -7
- package/dist/test/codex-history.test.js +1 -1
- package/dist/test/codex-plugin.test.js +1 -2
- package/dist/test/dependencies.test.js +2 -2
- package/dist/test/dsh.test.js +2 -2
- package/dist/test/history-corpus.test.js +21 -36
- package/dist/test/history-summary-cache.test.js +3 -3
- package/dist/test/history.integration.test.js +53 -106
- package/dist/test/host-neutral.test.js +1 -1
- package/dist/test/note-policy.test.js +5 -5
- package/dist/test/notes-cwd.test.js +5 -5
- package/dist/test/notes-query-namespaces.test.js +2 -6
- package/dist/test/notes.integration.test.js +30 -35
- package/dist/test/notes.test.js +12 -14
- package/dist/test/project-namespace.test.js +3 -3
- package/docs/architecture.md +3 -3
- package/docs/claude-code-mod.md +2 -2
- package/docs/codex.md +1 -1
- package/docs/dsh.md +2 -2
- package/docs/history-sessions.md +8 -9
- package/docs/notes-library.md +3 -3
- package/docs/tool-results.md +12 -6
- package/package.json +1 -1
- package/plugins/claude/dist/claude/helper.js +162 -153
- package/plugins/claude/skills/memory/SKILL.md +6 -6
- package/plugins/claude/src/helper.ts +1 -2
- package/plugins/codex/README.md +1 -1
- package/plugins/codex/dist/codex/notesoup.js +161 -152
- package/plugins/codex/src/mcp.ts +1 -2
- package/plugins/dsh/tools.ts +1 -2
- package/plugins/pi/notes/tools.ts +2 -3
- package/skills/memory/SKILL.md +6 -6
- package/src/history/history.ts +11 -16
- package/src/history/query.ts +67 -99
- package/src/index.ts +1 -1
- package/src/notes/address.ts +2 -2
- package/src/notes/store.ts +3 -6
- package/src/tools/history.ts +60 -29
- package/src/tools/notes.ts +39 -54
- package/src/tools/schema.ts +0 -1
package/README.md
CHANGED
|
@@ -31,23 +31,9 @@ pi -e npm:notesoup
|
|
|
31
31
|
- **Durable context windows.** `/fresh-window` (alias `/empty`) clears and stops, with any command text available for close-out notes. Separate human input received during emptying continues in the fresh window. The model can also request a window itself with `empty_brain`, and a budget close-out fires automatically before the window runs out. Abort and ordinary errors never count as completion; the raw conversation stays in the session, just out of the next context.
|
|
32
32
|
- **A boot block at every window head.** Pi keeps the window identity and notes snapshot in a hidden user-role boot; its fixed recovery protocol and pinned-page teaching live in a named system section. Every note body, including `@human` pins, stays user-role data. Claude, Codex and dsh keep their existing combined boot injection.
|
|
33
33
|
- **`get_context_remaining`** — how much room the brain has left before the hard-reserve wipe, including the warning runway.
|
|
34
|
-
- **
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
| --- | --- |
|
|
38
|
-
| `history.list_items` | `history_list` |
|
|
39
|
-
| `history.read_item` | `history_read` |
|
|
40
|
-
| `history.search_contents` | `history_list` (`query`) |
|
|
41
|
-
| `notes.write` | `notes_write` |
|
|
42
|
-
| `notes.update` | `notes_update` |
|
|
43
|
-
| `notes.rename` | `notes_update` (`rename_to`) |
|
|
44
|
-
| `notes.read` | `notes_read` |
|
|
45
|
-
| `notes.list` | `notes_list` |
|
|
46
|
-
| `notes.search` | `notes_search` |
|
|
47
|
-
|
|
48
|
-
- **Migration**: `history_search` is gone; pass `query` to `history_list`.
|
|
49
|
-
- **Cross-session history**: both history tools accept `session: "<id-or-prefix>"` or `"<harness>:<id>"` (`pi`, `claude`, `codex`, `dsh`). `history_list({ session: "*", query: "phrase" })` searches local transcripts across harnesses; hits carry the session to copy into `history_read`. Omit `session` for unchanged current-session behavior. See [cross-session history](docs/history-sessions.md) for discovery roots, refusals and offline limits.
|
|
50
|
-
- **Migration (0.40.0)**: parameters renamed: `order` → `from`, `before`/`after` → `before_seq`/`after_seq`, `limit_chars` → `max_chars`, `wastebasket` → `crumpled`, `pattern` → `glob` (the standalone library's options included), and `notes_update`'s `change_kind` value `file` → `moved`. Old names are refused.
|
|
34
|
+
- **Notes:** `notes_list` browses current notebooks by default. Add `glob` to select notebooks or paths and `query` for literal, case-insensitive matches with lines and read offsets. Open a page with `notes_read`, create or replace one with `notes_write`, and edit, move or crumple it with `notes_update`.
|
|
35
|
+
- **History:** `history_list` browses or searches with `query`. Page with exclusive `after` / `before` seqs or ISO 8601 times; add `context` to see neighboring events around hits. `history_read` opens one event by seq.
|
|
36
|
+
- **Cross-session history:** both history tools accept `session: "<id-or-prefix>"` or `"<harness>:<id>"` (`pi`, `claude`, `codex`, `dsh`). `history_list({ session: "*", query: "phrase" })` searches local transcripts across harnesses; hits carry the session to copy into `history_read`. Omit `session` to use the current session. See [cross-session history](docs/history-sessions.md) for discovery roots, refusals and offline limits.
|
|
51
37
|
|
|
52
38
|
- **Runtime toggle** — `/notesoup off` stops new automatic resets; `/notesoup on` re-enables them; bare `/notesoup` reports the state.
|
|
53
39
|
- **Context cap (Pi and Claude)** — `/notesoup cap 100k` limits the capacity used by notesoup's budget to `min(model window, 100000)`. Existing reserves and warning margins are deducted from that capacity; 100k is not the used-token reset point. `/notesoup cap` shows the configured cap and `/notesoup cap off` removes it. Whole tokens and decimal `k`/`m` suffixes are accepted; caps too small for the host's reserves are refused. The cap applies only to the running session, survives notesoup resets and model changes, and is lost on reload/restart/session switch. It never writes host settings or environment variables, changes the provider's advertised model window, or enables disabled automatic resets.
|
|
@@ -2,7 +2,7 @@ import { realpathSync } from "node:fs";
|
|
|
2
2
|
import { pathToFileURL } from "node:url";
|
|
3
3
|
import { Check } from "typebox/value";
|
|
4
4
|
import { paramProblem } from "../../../src/tools/schema.js";
|
|
5
|
-
import { notesList, notesRead,
|
|
5
|
+
import { notesList, notesRead, notesUpdate, notesWrite } from "../../../src/tools/notes.js";
|
|
6
6
|
import { historyList, historyRead } from "../../../src/tools/history.js";
|
|
7
7
|
import { localHistoryCorpus } from "../../../src/history/local.js";
|
|
8
8
|
import { readClaudeHistory } from "./history.js";
|
|
@@ -38,7 +38,6 @@ const OPERATIONS = {
|
|
|
38
38
|
notes_update: boundOperation(notesUpdate),
|
|
39
39
|
notes_read: boundOperation(notesRead),
|
|
40
40
|
notes_list: boundOperation(notesList),
|
|
41
|
-
notes_search: boundOperation(notesSearch),
|
|
42
41
|
};
|
|
43
42
|
const HISTORY_OPERATIONS = {
|
|
44
43
|
history_list: boundOperation(historyList),
|
|
@@ -4,7 +4,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
4
4
|
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
5
5
|
import { Check } from "typebox/value";
|
|
6
6
|
import { paramProblem } from "../../../src/tools/schema.js";
|
|
7
|
-
import { notesList, notesRead,
|
|
7
|
+
import { notesList, notesRead, notesUpdate, notesWrite } from "../../../src/tools/notes.js";
|
|
8
8
|
import { historyList, historyRead } from "../../../src/tools/history.js";
|
|
9
9
|
import { wireResult } from "../../../src/tools/result.js";
|
|
10
10
|
import { CodexIdentityError, notesIdentityFor, pluginDataRoot, readBinding, rootSessionIdOf, safeThreadId, turnModel } from "./identity.js";
|
|
@@ -17,7 +17,6 @@ const OPERATIONS = {
|
|
|
17
17
|
notes_update: { operation: notesUpdate, history: false },
|
|
18
18
|
notes_read: { operation: notesRead, history: false },
|
|
19
19
|
notes_list: { operation: notesList, history: false },
|
|
20
|
-
notes_search: { operation: notesSearch, history: false },
|
|
21
20
|
history_list: { operation: historyList, history: true },
|
|
22
21
|
history_read: { operation: historyRead, history: true },
|
|
23
22
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HarnessError } from "@deepseek-ai/dsh-llm";
|
|
2
2
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
3
3
|
import { Check, Errors } from "typebox/value";
|
|
4
|
-
import { notesList, notesRead,
|
|
4
|
+
import { notesList, notesRead, notesUpdate, notesWrite } from "../../src/tools/notes.js";
|
|
5
5
|
import { historyList, historyRead } from "../../src/tools/history.js";
|
|
6
6
|
import { success } from "../../src/result.js";
|
|
7
7
|
import { historyContext } from "./history.js";
|
|
@@ -78,7 +78,6 @@ export function registerTools(ctx, engine) {
|
|
|
78
78
|
register(ctx, notesUpdate, (agent) => [toolContext(agent)]);
|
|
79
79
|
register(ctx, notesRead, (agent) => [toolContext(agent)]);
|
|
80
80
|
register(ctx, notesList, (agent) => [toolContext(agent)]);
|
|
81
|
-
register(ctx, notesSearch, (agent) => [toolContext(agent)]);
|
|
82
81
|
register(ctx, historyList, (agent) => [historyContext(ctx, agent.session.id)]);
|
|
83
82
|
register(ctx, historyRead, (agent) => [historyContext(ctx, agent.session.id)]);
|
|
84
83
|
ctx.tools.register(defineTool({
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
/** Register the
|
|
2
|
+
/** Register the shared note operations and the three hidden dream tools; an open dream widens the note identity. */
|
|
3
3
|
export declare function registerNotesTools(pi: ExtensionAPI): void;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { generateDiffString } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import { notesWrite, notesUpdate, notesRead, notesList
|
|
2
|
+
import { notesWrite, notesUpdate, notesRead, notesList } from "../../../src/tools/notes.js";
|
|
3
3
|
import { abandonDream, dreamBegin, dreamEnd, dreamForget, dreamingIdentity } from "../../../src/tools/dream.js";
|
|
4
4
|
import { notesIdentityFromPi } from "./adapter.js";
|
|
5
5
|
import { notesReadRenderers, notesUpdateRenderers, notesWriteRenderers } from "../render.js";
|
|
@@ -8,7 +8,7 @@ import { notesToolContext } from "../../../src/tools/notes-context.js";
|
|
|
8
8
|
function renderDiff(change) {
|
|
9
9
|
return change.kind === "none" ? "" : generateDiffString(change.before, change.after, 1).diff;
|
|
10
10
|
}
|
|
11
|
-
/** Register the
|
|
11
|
+
/** Register the shared note operations and the three hidden dream tools; an open dream widens the note identity. */
|
|
12
12
|
export function registerNotesTools(pi) {
|
|
13
13
|
// One pending or open dream per session: shutdown releases ownership without the end commit.
|
|
14
14
|
const dreams = new Map();
|
|
@@ -34,7 +34,6 @@ export function registerNotesTools(pi) {
|
|
|
34
34
|
}, NOTES_NAMESPACE, notesUpdateRenderers);
|
|
35
35
|
registerOperation(pi, notesRead, (params, ctx) => notesRead.execute(params, toolContext(ctx)), NOTES_NAMESPACE, notesReadRenderers);
|
|
36
36
|
registerOperation(pi, notesList, (params, ctx) => notesList.execute(params, toolContext(ctx)), NOTES_NAMESPACE);
|
|
37
|
-
registerOperation(pi, notesSearch, (params, ctx) => notesSearch.execute(params, toolContext(ctx)), NOTES_NAMESPACE);
|
|
38
37
|
registerOperation(pi, dreamBegin, (params, ctx) => dreamBegin.execute(params, notesIdentityFromPi(ctx), dreamOf(ctx)), DREAM_NAMESPACE, undefined, "deferred");
|
|
39
38
|
registerOperation(pi, dreamForget, (params, ctx) => dreamForget.execute(params, notesIdentityFromPi(ctx), dreamOf(ctx)), DREAM_NAMESPACE, undefined, "deferred");
|
|
40
39
|
registerOperation(pi, dreamEnd, (params, ctx) => dreamEnd.execute(params, notesIdentityFromPi(ctx), dreamOf(ctx)), DREAM_NAMESPACE, undefined, "deferred");
|
|
@@ -52,12 +52,15 @@ export type HistoryProjection = {
|
|
|
52
52
|
pairedResults: Map<number, number>;
|
|
53
53
|
};
|
|
54
54
|
/** The filters one selection applies before paging: roles, seq bounds, and parsed epoch-ms time bounds. */
|
|
55
|
+
export type HistoryBound = {
|
|
56
|
+
seq: number;
|
|
57
|
+
} | {
|
|
58
|
+
at: number;
|
|
59
|
+
};
|
|
55
60
|
export type HistoryFilter = {
|
|
56
|
-
roles?: HistoryRole[]
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
sinceMs?: number;
|
|
60
|
-
untilMs?: number;
|
|
61
|
+
roles?: HistoryRole[];
|
|
62
|
+
after?: HistoryBound;
|
|
63
|
+
before?: HistoryBound;
|
|
61
64
|
};
|
|
62
65
|
/** Native entries after the adapter allocates seqs and selects the active branch. */
|
|
63
66
|
export type DecodedHistoryItem = {
|
|
@@ -136,6 +139,7 @@ export declare const HistoryPageItemSchema: Type.TObject<{
|
|
|
136
139
|
count: Type.TInteger;
|
|
137
140
|
tools: Type.TRecord<"^.*$", Type.TInteger>;
|
|
138
141
|
}>>;
|
|
142
|
+
context: Type.TOptional<Type.TLiteral<true>>;
|
|
139
143
|
seq: Type.TInteger;
|
|
140
144
|
role: Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"context">]>;
|
|
141
145
|
created_at: Type.TUnion<[Type.TString, Type.TNull]>;
|
|
@@ -148,11 +152,6 @@ export declare const MAX_FOLD_TOOL_NAME_CHARS = 120;
|
|
|
148
152
|
/** A bounded preview of one event from `matchOffset` on, plus its identity facts. */
|
|
149
153
|
export declare function visibleItem(event: HistoryEvent, maxChars: number, matchOffset?: number): HistoryPageItem;
|
|
150
154
|
export declare function allItems(projection: HistoryProjection): HistoryEvent[];
|
|
151
|
-
|
|
152
|
-
* Whether one event is inside the selection: its seq bounds, and a half-open
|
|
153
|
-
* [sinceMs, untilMs) window on created_at. A bounded time window drops events with no recorded
|
|
154
|
-
* time or a time that cannot be read.
|
|
155
|
-
*/
|
|
156
|
-
export declare function inSelection(event: HistoryEvent, params: HistoryFilter): boolean;
|
|
155
|
+
export declare function matchesFilter(event: HistoryEvent, { after, before, roles }: HistoryFilter): boolean;
|
|
157
156
|
/** Ordered events inside the requested seq/time range and optional roles, before folding. */
|
|
158
157
|
export declare function filteredItems(projection: HistoryProjection, params: HistoryFilter): HistoryEvent[];
|
|
@@ -146,6 +146,7 @@ export const HistoryFoldedRowSchema = Type.Object({
|
|
|
146
146
|
export const HistoryPageItemSchema = Type.Object({
|
|
147
147
|
session: Type.Optional(Type.String()),
|
|
148
148
|
folded_before: Type.Optional(HistoryFoldedRowSchema),
|
|
149
|
+
context: Type.Optional(Type.Literal(true, { description: "A neighbor of a search hit, not a hit in this page." })),
|
|
149
150
|
seq: Type.Integer({ minimum: 1 }),
|
|
150
151
|
role: HistoryRoleSchema,
|
|
151
152
|
created_at: Type.Union([Type.String(), Type.Null()]),
|
|
@@ -179,26 +180,14 @@ export function visibleItem(event, maxChars, matchOffset) {
|
|
|
179
180
|
export function allItems(projection) {
|
|
180
181
|
return projection.windows.flatMap((current) => current.items);
|
|
181
182
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (params.after_seq !== undefined && event.seq <= params.after_seq)
|
|
189
|
-
return false;
|
|
190
|
-
if (params.before_seq !== undefined && event.seq >= params.before_seq)
|
|
191
|
-
return false;
|
|
192
|
-
if (params.sinceMs === undefined && params.untilMs === undefined)
|
|
193
|
-
return true;
|
|
194
|
-
if (event.createdAt === undefined)
|
|
195
|
-
return false;
|
|
196
|
-
const at = Date.parse(event.createdAt);
|
|
197
|
-
return !Number.isNaN(at) && (params.sinceMs === undefined || at >= params.sinceMs) && (params.untilMs === undefined || at < params.untilMs);
|
|
183
|
+
function compareBound(event, bound) {
|
|
184
|
+
return "seq" in bound ? event.seq - bound.seq : Date.parse(event.createdAt ?? "") - bound.at;
|
|
185
|
+
}
|
|
186
|
+
export function matchesFilter(event, { after, before, roles }) {
|
|
187
|
+
return (!after || compareBound(event, after) > 0) && (!before || compareBound(event, before) < 0)
|
|
188
|
+
&& (!roles || roles.includes(event.role));
|
|
198
189
|
}
|
|
199
190
|
/** Ordered events inside the requested seq/time range and optional roles, before folding. */
|
|
200
191
|
export function filteredItems(projection, params) {
|
|
201
|
-
return allItems(projection)
|
|
202
|
-
.filter((item) => inSelection(item, params) && (!params.roles || params.roles.includes(item.role)))
|
|
203
|
-
.sort((a, b) => a.seq - b.seq);
|
|
192
|
+
return allItems(projection).filter(event => matchesFilter(event, params)).sort((a, b) => a.seq - b.seq);
|
|
204
193
|
}
|
|
@@ -4,21 +4,11 @@ import { type Outcome } from "../result.js";
|
|
|
4
4
|
import { type HistoryEvent, type HistoryFilter, type HistoryProjection } from "./history.js";
|
|
5
5
|
export declare function isMultiPage(page: HistoryPage): boolean;
|
|
6
6
|
export declare function markMultiPage(page: HistoryPage): HistoryPage;
|
|
7
|
-
/** Which end of one selection `limit` is taken from; page items always come back ascending. */
|
|
8
|
-
export type HistoryOrder = "newest" | "oldest";
|
|
9
|
-
/**
|
|
10
|
-
* What one list call selects. `before_seq`/`after_seq` choose a seq range and `since`/`until` a time
|
|
11
|
-
* range; `from` chooses the end `limit` counts from; a `query` keeps only matching events, across
|
|
12
|
-
* every role, with no folded rows.
|
|
13
|
-
*/
|
|
14
7
|
export type HistorySelection = HistoryFilter & {
|
|
15
8
|
session?: string;
|
|
16
|
-
from?: HistoryOrder;
|
|
17
9
|
limit?: number;
|
|
18
|
-
query?: string
|
|
19
|
-
|
|
20
|
-
until?: string;
|
|
21
|
-
max_chars_per_item?: number;
|
|
10
|
+
query?: string[];
|
|
11
|
+
context?: number;
|
|
22
12
|
};
|
|
23
13
|
export declare const HistoryPageSchema: Type.TObject<{
|
|
24
14
|
notice: Type.TOptional<Type.TString>;
|
|
@@ -38,6 +28,7 @@ export declare const HistoryPageSchema: Type.TObject<{
|
|
|
38
28
|
count: Type.TInteger;
|
|
39
29
|
tools: Type.TRecord<"^.*$", Type.TInteger>;
|
|
40
30
|
}>>;
|
|
31
|
+
context: Type.TOptional<Type.TLiteral<true>>;
|
|
41
32
|
seq: Type.TInteger;
|
|
42
33
|
role: Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"context">]>;
|
|
43
34
|
created_at: Type.TUnion<[Type.TString, Type.TNull]>;
|
|
@@ -63,8 +54,6 @@ export type HistoryPageResult = Outcome<HistoryPage> | {
|
|
|
63
54
|
};
|
|
64
55
|
};
|
|
65
56
|
export declare function selectPage(projection: HistoryProjection, params: HistorySelection, fits: (page: HistoryPage) => boolean, maxChars: number): HistoryPageResult;
|
|
66
|
-
/** Validate before corpus discovery or I/O: per-session seqs cannot bound a multi-session search. */
|
|
67
|
-
export declare function corpusSelection(params: HistorySelection): Outcome<HistorySelection>;
|
|
68
57
|
/** One topic per session, after role/time filtering; empty dialogue envelopes are not visible. */
|
|
69
58
|
export declare function sessionRepresentative(projection: HistoryProjection, scoped: HistorySelection): HistoryEvent | undefined;
|
|
70
59
|
/** Keep only the requested end of the globally ordered selection while sessions are released. */
|
|
@@ -1,37 +1,14 @@
|
|
|
1
1
|
/** Stable-anchor paging, folding and literal matching. The operation supplies the full-response budget check. */
|
|
2
2
|
import { Type } from "typebox";
|
|
3
|
-
import {
|
|
4
|
-
import { earliestMatchOffsetChars,
|
|
5
|
-
import { eventDocument, filteredItems, visibleItem, HistoryFoldedRowSchema, HistoryPageItemSchema, MAX_FOLD_TOOL_NAMES, MAX_FOLD_TOOL_NAME_CHARS } from "./history.js";
|
|
3
|
+
import { success } from "../result.js";
|
|
4
|
+
import { earliestMatchOffsetChars, prefixFit } from "../text-match.js";
|
|
5
|
+
import { eventDocument, filteredItems, matchesFilter, visibleItem, HistoryFoldedRowSchema, HistoryPageItemSchema, MAX_FOLD_TOOL_NAMES, MAX_FOLD_TOOL_NAME_CHARS } from "./history.js";
|
|
6
6
|
const multiPages = new WeakSet();
|
|
7
7
|
export function isMultiPage(page) { return multiPages.has(page); }
|
|
8
8
|
export function markMultiPage(page) {
|
|
9
9
|
multiPages.add(page);
|
|
10
10
|
return page;
|
|
11
11
|
}
|
|
12
|
-
/** ISO 8601 date or date-time; the offset is optional (group 1) and a missing one is read as UTC. */
|
|
13
|
-
const ISO_8601 = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?)?(Z|[+-]\d{2}:?\d{2})?$/;
|
|
14
|
-
/** Epoch ms for one optional time param, or a refusal naming it. */
|
|
15
|
-
function timeBound(name, value) {
|
|
16
|
-
if (value === undefined)
|
|
17
|
-
return success(undefined);
|
|
18
|
-
const match = ISO_8601.exec(value);
|
|
19
|
-
const at = match === null ? undefined : Date.parse(match[1] === undefined ? `${value}Z` : value);
|
|
20
|
-
return at === undefined || Number.isNaN(at) ? failure("invalid_time", `\`${name}\` isn't an ISO 8601 time: "${value}".`) : success(at);
|
|
21
|
-
}
|
|
22
|
-
/** Parse `since`/`until`; a malformed or inverted pair refuses here. */
|
|
23
|
-
function resolveTimeBounds(params) {
|
|
24
|
-
const since = timeBound("since", params.since);
|
|
25
|
-
if (!since.ok)
|
|
26
|
-
return since;
|
|
27
|
-
const until = timeBound("until", params.until);
|
|
28
|
-
if (!until.ok)
|
|
29
|
-
return until;
|
|
30
|
-
if (since.data !== undefined && until.data !== undefined && since.data >= until.data) {
|
|
31
|
-
return failure("invalid_time", "`since` must be earlier than `until`.");
|
|
32
|
-
}
|
|
33
|
-
return success({ ...(since.data === undefined ? {} : { sinceMs: since.data }), ...(until.data === undefined ? {} : { untilMs: until.data }) });
|
|
34
|
-
}
|
|
35
12
|
export const HistoryPageSchema = Type.Object({
|
|
36
13
|
notice: Type.Optional(Type.String()),
|
|
37
14
|
items: Type.Array(HistoryPageItemSchema),
|
|
@@ -72,26 +49,34 @@ function foldedRun(events, session) {
|
|
|
72
49
|
const tools = Object.fromEntries(entries);
|
|
73
50
|
return { ...(session === undefined ? {} : { session }), first_seq: events[0].seq, last_seq: events.at(-1).seq, count: events.length, tools };
|
|
74
51
|
}
|
|
75
|
-
|
|
52
|
+
// Tool/thinking-only envelopes are not dialogue, even with explicit roles.
|
|
53
|
+
const visible = (event) => (event.role !== "user" && event.role !== "assistant") || eventDocument(event) !== "";
|
|
54
|
+
function* queryHits(projection, params, session) {
|
|
55
|
+
const { query, context } = params;
|
|
56
|
+
const events = filteredItems(projection, context ? {} : params).filter(visible);
|
|
57
|
+
for (const [index, event] of events.entries()) {
|
|
58
|
+
if (context && !matchesFilter(event, params))
|
|
59
|
+
continue;
|
|
60
|
+
const matchOffset = earliestMatchOffsetChars(eventDocument(event), query);
|
|
61
|
+
if (matchOffset >= 0)
|
|
62
|
+
yield { event, session, matchOffset, neighbors: context ? { events, index } : undefined };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
76
65
|
function prepareSelection(projection, params) {
|
|
77
|
-
const
|
|
66
|
+
const { query, roles, session } = params;
|
|
67
|
+
if (query)
|
|
68
|
+
return { candidates: [...queryHits(projection, params, session)] };
|
|
78
69
|
const candidates = [];
|
|
79
70
|
let run = [];
|
|
80
|
-
for (const event of filteredItems(projection, params)) {
|
|
81
|
-
if (
|
|
71
|
+
for (const event of filteredItems(projection, params).filter(visible)) {
|
|
72
|
+
if (!roles && (event.role === "tool" || event.role === "context"))
|
|
82
73
|
run.push(event);
|
|
83
|
-
|
|
74
|
+
else {
|
|
75
|
+
candidates.push({ event, session, before: foldedRun(run, session) });
|
|
76
|
+
run = [];
|
|
84
77
|
}
|
|
85
|
-
// Tool/thinking-only envelopes are not dialogue, even with explicit roles.
|
|
86
|
-
if ((event.role === "user" || event.role === "assistant") && eventDocument(event) === "")
|
|
87
|
-
continue;
|
|
88
|
-
const matchOffset = params.query === undefined ? undefined : earliestMatchOffsetChars(eventDocument(event), params.query);
|
|
89
|
-
if (matchOffset !== undefined && matchOffset < 0)
|
|
90
|
-
continue;
|
|
91
|
-
candidates.push({ event, ...(params.session === undefined ? {} : { session: params.session }), matchOffset, before: foldedRun(run, params.session) });
|
|
92
|
-
run = [];
|
|
93
78
|
}
|
|
94
|
-
return { candidates,
|
|
79
|
+
return { candidates, tail: foldedRun(run, session) };
|
|
95
80
|
}
|
|
96
81
|
/** Assemble only the admitted interval. Edge folds belong only to a page reaching that edge. */
|
|
97
82
|
function pageData(prepared, start, end, maxChars, preview) {
|
|
@@ -105,52 +90,60 @@ function pageData(prepared, start, end, maxChars, preview) {
|
|
|
105
90
|
const page = {
|
|
106
91
|
items,
|
|
107
92
|
...(end === candidates.length && tail ? { folded_after: tail } : {}),
|
|
108
|
-
has_older: (start > 0 && start < end) || (!!prepared.discarded && prepared.
|
|
109
|
-
has_newer: (end < candidates.length && start < end) || (!!prepared.discarded && prepared.
|
|
93
|
+
has_older: (start > 0 && start < end) || (!!prepared.discarded && !prepared.forward),
|
|
94
|
+
has_newer: (end < candidates.length && start < end) || (!!prepared.discarded && !!prepared.forward),
|
|
110
95
|
};
|
|
111
96
|
return prepared.multi ? markMultiPage(page) : page;
|
|
112
97
|
}
|
|
113
|
-
/**
|
|
114
|
-
* Resolve the time bounds once, then filter and fold against the same [sinceMs, untilMs) window.
|
|
115
|
-
* Grow the page in selection order; shorten the first preview only when no full row fits.
|
|
116
|
-
*/
|
|
117
|
-
function resolveSelection(params) {
|
|
118
|
-
const bounds = resolveTimeBounds(params);
|
|
119
|
-
if (!bounds.ok)
|
|
120
|
-
return bounds;
|
|
121
|
-
try {
|
|
122
|
-
const queries = searchQueries(params.query);
|
|
123
|
-
return success({ ...params, ...bounds.data, query: queries.length ? queries : undefined });
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
if (error instanceof InvalidQueryError)
|
|
127
|
-
return failure("invalid_query", error.message);
|
|
128
|
-
throw error;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
98
|
export function selectPage(projection, params, fits, maxChars) {
|
|
132
|
-
|
|
133
|
-
if (!scoped.ok)
|
|
134
|
-
return scoped;
|
|
135
|
-
return selectPrepared(prepareSelection(projection, scoped.data), scoped.data, fits, maxChars);
|
|
136
|
-
}
|
|
137
|
-
/** Validate before corpus discovery or I/O: per-session seqs cannot bound a multi-session search. */
|
|
138
|
-
export function corpusSelection(params) {
|
|
139
|
-
if (params.before_seq !== undefined || params.after_seq !== undefined)
|
|
140
|
-
return failure("invalid_bounds", "`before_seq` and `after_seq` are per-session; omit them for wildcard session search.");
|
|
141
|
-
return resolveSelection(params);
|
|
99
|
+
return selectPrepared(prepareSelection(projection, params), params, fits, maxChars);
|
|
142
100
|
}
|
|
143
101
|
/** One topic per session, after role/time filtering; empty dialogue envelopes are not visible. */
|
|
144
102
|
export function sessionRepresentative(projection, scoped) {
|
|
145
|
-
const events = filteredItems(projection, scoped).filter(
|
|
103
|
+
const events = filteredItems(projection, scoped).filter(visible);
|
|
146
104
|
if (scoped.roles)
|
|
147
105
|
return events.at(-1);
|
|
148
106
|
return events.find((event) => event.role === "user")
|
|
149
107
|
?? events.filter((event) => event.role === "assistant").at(-1)
|
|
150
108
|
?? events.at(-1);
|
|
151
109
|
}
|
|
152
|
-
|
|
153
|
-
const
|
|
110
|
+
function eventTime(timestamp) {
|
|
111
|
+
const at = Date.parse(timestamp ?? "");
|
|
112
|
+
return Number.isNaN(at) ? -Infinity : at;
|
|
113
|
+
}
|
|
114
|
+
const compareCorpus = (a, b) => eventTime(a.event.createdAt) - eventTime(b.event.createdAt) || a.session.localeCompare(b.session) || a.event.seq - b.event.seq;
|
|
115
|
+
/** Fill the remaining budget nearest first. Shared session/seq keys merge overlapping windows. */
|
|
116
|
+
function addContext(page, hits, radius, fits, maxChars) {
|
|
117
|
+
const key = (session, seq) => `${session ?? ""}\0${seq}`;
|
|
118
|
+
const seen = new Set(page.items.map(item => key(item.session, item.seq)));
|
|
119
|
+
const compare = (a, b) => (isMultiPage(page) ? eventTime(a.created_at) - eventTime(b.created_at) || a.session.localeCompare(b.session) : 0) || a.seq - b.seq;
|
|
120
|
+
for (let distance = 1; distance <= radius; distance++) {
|
|
121
|
+
let found = false;
|
|
122
|
+
for (const { neighbors, session } of hits) {
|
|
123
|
+
if (!neighbors)
|
|
124
|
+
continue;
|
|
125
|
+
for (const index of [neighbors.index - distance, neighbors.index + distance]) {
|
|
126
|
+
const event = neighbors.events[index];
|
|
127
|
+
if (!event)
|
|
128
|
+
continue;
|
|
129
|
+
found = true;
|
|
130
|
+
const id = key(session, event.seq);
|
|
131
|
+
if (seen.has(id))
|
|
132
|
+
continue;
|
|
133
|
+
const item = { ...visibleItem(event, maxChars), ...(session === undefined ? {} : { session }), context: true };
|
|
134
|
+
const previous = page.items;
|
|
135
|
+
page.items = [...previous, item].sort(compare);
|
|
136
|
+
if (!fits(page)) {
|
|
137
|
+
page.items = previous;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
seen.add(id);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (!found)
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
154
147
|
/** Keep only the requested end of the globally ordered selection while sessions are released. */
|
|
155
148
|
export class CorpusCollector {
|
|
156
149
|
scoped;
|
|
@@ -173,7 +166,7 @@ export class CorpusCollector {
|
|
|
173
166
|
}
|
|
174
167
|
if (this.candidates.length === this.limit) {
|
|
175
168
|
this.discarded = true;
|
|
176
|
-
if (this.scoped.
|
|
169
|
+
if (this.scoped.after) {
|
|
177
170
|
if (low === this.limit)
|
|
178
171
|
return;
|
|
179
172
|
this.candidates.pop();
|
|
@@ -198,25 +191,18 @@ export class CorpusCollector {
|
|
|
198
191
|
add(session, projection) {
|
|
199
192
|
if (this.scoped.query === undefined)
|
|
200
193
|
return this.addRepresentative(session, sessionRepresentative(projection, this.scoped));
|
|
201
|
-
for (const
|
|
202
|
-
|
|
203
|
-
if ((event.role === "user" || event.role === "assistant") && document === "")
|
|
204
|
-
continue;
|
|
205
|
-
const matchOffset = earliestMatchOffsetChars(document, this.scoped.query);
|
|
206
|
-
if (matchOffset >= 0)
|
|
207
|
-
this.admit({ session, event, matchOffset });
|
|
208
|
-
}
|
|
194
|
+
for (const hit of queryHits(projection, this.scoped, session))
|
|
195
|
+
this.admit(hit);
|
|
209
196
|
return true;
|
|
210
197
|
}
|
|
211
198
|
page(fits, maxChars, unvisited = false) {
|
|
212
|
-
return selectPrepared({ candidates: this.candidates, multi: true, discarded: this.discarded || unvisited,
|
|
199
|
+
return selectPrepared({ candidates: this.candidates, multi: true, discarded: this.discarded || unvisited, forward: !!this.scoped.after }, this.scoped, fits, maxChars);
|
|
213
200
|
}
|
|
214
201
|
}
|
|
215
202
|
function selectPrepared(prepared, scoped, fits, maxChars) {
|
|
216
203
|
const { candidates } = prepared;
|
|
217
204
|
// Grow a contiguous interval from the requested end; even a byte-limited newest page stays ascending.
|
|
218
|
-
const
|
|
219
|
-
const newest = from === "newest";
|
|
205
|
+
const newest = scoped.after === undefined;
|
|
220
206
|
let start = newest ? candidates.length : 0;
|
|
221
207
|
let end = start;
|
|
222
208
|
const limit = Math.max(1, scoped.limit ?? 20);
|
|
@@ -239,5 +225,7 @@ function selectPrepared(prepared, scoped, fits, maxChars) {
|
|
|
239
225
|
const page = pageData(prepared, start, end, maxChars);
|
|
240
226
|
if (!fits(page))
|
|
241
227
|
return { ok: false, pageTooLarge: { count: page.items.length, multi: prepared.multi === true } };
|
|
228
|
+
if (scoped.context)
|
|
229
|
+
addContext(page, candidates.slice(start, end), scoped.context, fits, maxChars);
|
|
242
230
|
return success(page);
|
|
243
231
|
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export { renderBootBlock } from "./boot/render.js";
|
|
|
19
19
|
export type { BootRenderData, BootWindowTree } from "./boot/render.js";
|
|
20
20
|
export { DEFAULT_BOOT_TOOL_NAMES, renderProtocolBlock } from "./boot/text.js";
|
|
21
21
|
export type { BootToolNames } from "./boot/text.js";
|
|
22
|
-
export { notesList, notesRead,
|
|
22
|
+
export { notesList, notesRead, notesUpdate, notesWrite } from "./tools/notes.js";
|
|
23
23
|
export { notesIdentityForCall, notesToolContext, type NotesToolContext } from "./tools/notes-context.js";
|
|
24
24
|
export { historyList, historyRead } from "./tools/history.js";
|
|
25
25
|
export { projectHistory } from "./history/history.js";
|
package/dist/src/index.js
CHANGED
|
@@ -19,7 +19,7 @@ export { renderBootBlock } from "./boot/render.js";
|
|
|
19
19
|
export { DEFAULT_BOOT_TOOL_NAMES, renderProtocolBlock } from "./boot/text.js";
|
|
20
20
|
// Operations: the schemas and typed outcomes a host registers as its own tools, over a
|
|
21
21
|
// host-supplied notes tool context or decoded history projection/lazy corpus context.
|
|
22
|
-
export { notesList, notesRead,
|
|
22
|
+
export { notesList, notesRead, notesUpdate, notesWrite } from "./tools/notes.js";
|
|
23
23
|
export { notesIdentityForCall, notesToolContext } from "./tools/notes-context.js";
|
|
24
24
|
export { historyList, historyRead } from "./tools/history.js";
|
|
25
25
|
export { projectHistory } from "./history/history.js";
|
|
@@ -44,10 +44,10 @@ export function globToRegExp(pattern) {
|
|
|
44
44
|
}
|
|
45
45
|
/** Glob patterns are not virtual paths (`*` is legal), so they get their own guard. */
|
|
46
46
|
export function assertGlobPattern(value) {
|
|
47
|
-
if (value === undefined
|
|
47
|
+
if (value === undefined)
|
|
48
48
|
return undefined;
|
|
49
|
-
if (typeof value !== "string")
|
|
50
|
-
throw new NoteError("invalid_pattern", `glob ${shown(value)} must be a string.`);
|
|
49
|
+
if (typeof value !== "string" || !value)
|
|
50
|
+
throw new NoteError("invalid_pattern", `glob ${shown(value)} must be a non-empty string; omit it for the current notebooks.`);
|
|
51
51
|
if (value.includes("\0") || value.includes("\\"))
|
|
52
52
|
throw new NoteError("invalid_pattern", `glob ${shown(value)} can't contain "\\" or NUL.`);
|
|
53
53
|
return value;
|
|
@@ -38,11 +38,9 @@ export type EditOptions = NotePolicyFields & {
|
|
|
38
38
|
replaceAll?: boolean;
|
|
39
39
|
renameTo?: string;
|
|
40
40
|
};
|
|
41
|
-
/**
|
|
42
|
-
* A pattern without `@` or `/` matches filenames at any depth; relative paths with `/` match the current session.
|
|
43
|
-
*/
|
|
41
|
+
/** Omit glob for current notebooks. A pattern without `@` or `/` matches filenames at any depth. */
|
|
44
42
|
export type NotesQuery = {
|
|
45
|
-
glob?: string
|
|
43
|
+
glob?: string;
|
|
46
44
|
crumpled?: boolean;
|
|
47
45
|
};
|
|
48
46
|
/** A read exposes metadata and the pure body; the serialized frontmatter is never part of the body. */
|
package/dist/src/notes/store.js
CHANGED
|
@@ -471,8 +471,7 @@ export function createNotesStore(input) {
|
|
|
471
471
|
}));
|
|
472
472
|
}
|
|
473
473
|
async function* scan(options, status) {
|
|
474
|
-
const
|
|
475
|
-
const pattern = glob === "@default" ? undefined : glob;
|
|
474
|
+
const pattern = assertGlobPattern(options.glob);
|
|
476
475
|
const normalized = normalizePattern(pattern, identity);
|
|
477
476
|
const matcher = normalized === undefined ? undefined : globToRegExp(normalized);
|
|
478
477
|
const namespaces = await namespacesForPattern(pattern, identity, status);
|
|
@@ -27,13 +27,11 @@ type HistoryReadData = Static<typeof HistoryReadDataSchema>;
|
|
|
27
27
|
export type HistoryTextWrapper = (text: string) => string;
|
|
28
28
|
export declare const historyListParameters: Type.TObject<{
|
|
29
29
|
session: Type.TOptional<Type.TString>;
|
|
30
|
-
query: Type.TOptional<Type.TUnion<[Type.TString, Type.TArray<Type.TString>]>>;
|
|
31
30
|
roles: Type.TOptional<Type.TArray<Type.TUnion<[Type.TLiteral<"user">, Type.TLiteral<"assistant">, Type.TLiteral<"tool">, Type.TLiteral<"context">]>>>;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
from: Type.TOptional<Type.TUnion<[Type.TLiteral<"newest">, Type.TLiteral<"oldest">]>>;
|
|
31
|
+
query: Type.TOptional<Type.TUnion<[Type.TString, Type.TArray<Type.TString>]>>;
|
|
32
|
+
after: Type.TOptional<Type.TUnion<[Type.TString, Type.TNumber]>>;
|
|
33
|
+
before: Type.TOptional<Type.TUnion<[Type.TString, Type.TNumber]>>;
|
|
34
|
+
context: Type.TOptional<Type.TInteger>;
|
|
37
35
|
limit: Type.TOptional<Type.TInteger>;
|
|
38
36
|
max_chars_per_item: Type.TOptional<Type.TInteger>;
|
|
39
37
|
}>;
|