@oyasmi/pipiclaw 0.9.6-beta.2 → 0.9.6-beta.3

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.
Files changed (78) hide show
  1. package/README.en.md +2 -2
  2. package/README.md +2 -2
  3. package/dist/agent/channel-runner.js +2 -0
  4. package/dist/agent/maintenance-context.js +21 -2
  5. package/dist/agent/turn/turn-context.js +10 -6
  6. package/dist/config/settings.d.ts +0 -2
  7. package/dist/config/settings.js +0 -2
  8. package/dist/config/tools.d.ts +8 -2
  9. package/dist/config/tools.js +35 -19
  10. package/dist/memory/anchors.d.ts +5 -0
  11. package/dist/memory/anchors.js +49 -0
  12. package/dist/memory/boundary-work.d.ts +1 -0
  13. package/dist/memory/boundary-work.js +1 -0
  14. package/dist/memory/commands.js +23 -6
  15. package/dist/memory/index-budget.d.ts +16 -0
  16. package/dist/memory/index-budget.js +24 -2
  17. package/dist/memory/journal.d.ts +7 -12
  18. package/dist/memory/journal.js +9 -41
  19. package/dist/memory/lifecycle.d.ts +1 -0
  20. package/dist/memory/lifecycle.js +2 -0
  21. package/dist/memory/reflect-job.d.ts +1 -0
  22. package/dist/memory/reflect-job.js +1 -0
  23. package/dist/memory/reflect.d.ts +10 -0
  24. package/dist/memory/reflect.js +103 -20
  25. package/dist/memory/render.d.ts +4 -1
  26. package/dist/memory/render.js +6 -4
  27. package/dist/memory/review-log.d.ts +2 -2
  28. package/dist/memory/scheduler.d.ts +7 -0
  29. package/dist/memory/scheduler.js +70 -3
  30. package/dist/memory/search-index-db.d.ts +15 -0
  31. package/dist/memory/search-index-db.js +247 -0
  32. package/dist/memory/search-index-files.d.ts +11 -0
  33. package/dist/memory/search-index-files.js +26 -0
  34. package/dist/memory/search-index-notes.d.ts +11 -0
  35. package/dist/memory/search-index-notes.js +155 -0
  36. package/dist/memory/search-index-sync.d.ts +19 -0
  37. package/dist/memory/search-index-sync.js +267 -0
  38. package/dist/memory/search-terms.d.ts +12 -0
  39. package/dist/memory/search-terms.js +185 -0
  40. package/dist/memory/search.d.ts +8 -28
  41. package/dist/memory/search.js +18 -278
  42. package/dist/memory/session-corpus.d.ts +15 -26
  43. package/dist/memory/session-corpus.js +120 -327
  44. package/dist/memory/session-search.d.ts +5 -3
  45. package/dist/memory/session-search.js +119 -171
  46. package/dist/memory/sidecar-worker.d.ts +2 -0
  47. package/dist/memory/sidecar-worker.js +25 -1
  48. package/dist/memory/store.d.ts +4 -0
  49. package/dist/memory/store.js +22 -1
  50. package/dist/memory/task-archive-digest.d.ts +61 -0
  51. package/dist/memory/task-archive-digest.js +366 -0
  52. package/dist/models/provider-login.d.ts +1 -1
  53. package/dist/models/provider-login.js +1 -1
  54. package/dist/playbooks/memory-operations.md +2 -2
  55. package/dist/shared/text-utils.d.ts +0 -1
  56. package/dist/shared/text-utils.js +0 -1
  57. package/dist/subagents/runs.d.ts +1 -1
  58. package/dist/subagents/task-envelope.js +6 -8
  59. package/dist/tools/memory-manage.d.ts +1 -0
  60. package/dist/tools/memory-manage.js +9 -23
  61. package/dist/tools/registry.js +2 -0
  62. package/dist/tools/session-search.d.ts +1 -0
  63. package/dist/tools/session-search.js +12 -6
  64. package/dist/tools/web-search.d.ts +13 -7
  65. package/dist/tools/web-search.js +62 -19
  66. package/dist/web/client.d.ts +2 -0
  67. package/dist/web/client.js +7 -3
  68. package/dist/web/config.d.ts +17 -4
  69. package/dist/web/config.js +42 -4
  70. package/dist/web/format.d.ts +2 -1
  71. package/dist/web/format.js +7 -3
  72. package/dist/web/search-providers.d.ts +15 -8
  73. package/dist/web/search-providers.js +208 -139
  74. package/dist/web/search.d.ts +13 -3
  75. package/dist/web/search.js +86 -40
  76. package/package.json +1 -1
  77. package/dist/memory/chinese-words.d.ts +0 -1
  78. package/dist/memory/chinese-words.js +0 -273
@@ -1,213 +1,9 @@
1
- import { parseLocalTime } from "../shared/local-time.js";
2
- import { HAN_REGEX } from "../shared/text-utils.js";
3
- import { COMMON_CHINESE_WORDS } from "./chinese-words.js";
4
- /**
5
- * Age-bucketed recency boost (≤1 day / ≤7 days / ≤30 days / older), scaled by `weights`. Kept
6
- * here (not `session-search.ts`) so both cold-storage callers share one scoring primitive; each
7
- * caller supplies its own weights since the two scoring systems live on different magnitudes.
8
- */
9
- export function recencyBoostByAge(timestamp, weights) {
10
- if (!timestamp)
11
- return 0;
12
- const timestampMs = parseLocalTime(timestamp);
13
- if (timestampMs === undefined)
14
- return 0;
15
- const ageMs = Date.now() - timestampMs;
16
- const dayMs = 24 * 60 * 60 * 1000;
17
- if (ageMs <= dayMs)
18
- return weights.day;
19
- if (ageMs <= 7 * dayMs)
20
- return weights.week;
21
- if (ageMs <= 30 * dayMs)
22
- return weights.month;
23
- return 0;
24
- }
25
- /**
26
- * Spec 050, D3/D4: the lexical tokenizer. It serves exactly two callers — `memory_search` and
27
- * `memory_save`'s deterministic near-duplicate guard.
28
- */
29
- const TOKEN_PART_REGEX = /[\p{Script=Han}]+|[\p{L}\p{N}_./-]+/gu;
30
- const ASCII_SPLIT_REGEX = /[._/-]+/g;
31
- const MAX_HAN_WORD_LENGTH = Array.from(COMMON_CHINESE_WORDS).reduce((max, word) => Math.max(max, word.length), 2);
32
- const LATIN_STOP_WORDS = new Set([
33
- "a",
34
- "an",
35
- "and",
36
- "are",
37
- "as",
38
- "at",
39
- "be",
40
- "been",
41
- "being",
42
- "by",
43
- "can",
44
- "could",
45
- "did",
46
- "do",
47
- "does",
48
- "doing",
49
- "for",
50
- "from",
51
- "had",
52
- "has",
53
- "have",
54
- "here",
55
- "how",
56
- "i",
57
- "in",
58
- "is",
59
- "it",
60
- "its",
61
- "me",
62
- "my",
63
- "of",
64
- "on",
65
- "or",
66
- "our",
67
- "please",
68
- "should",
69
- "that",
70
- "the",
71
- "their",
72
- "them",
73
- "there",
74
- "these",
75
- "they",
76
- "this",
77
- "to",
78
- "was",
79
- "we",
80
- "were",
81
- "what",
82
- "when",
83
- "where",
84
- "which",
85
- "who",
86
- "why",
87
- "with",
88
- "would",
89
- "you",
90
- "your",
91
- ]);
92
- const CHINESE_STOP_CHARS = new Set([
93
- "的",
94
- "了",
95
- "在",
96
- "是",
97
- "有",
98
- "不",
99
- "和",
100
- "与",
101
- "个",
102
- "把",
103
- "被",
104
- "从",
105
- "对",
106
- "而",
107
- "给",
108
- "将",
109
- "就",
110
- "让",
111
- "向",
112
- "也",
113
- "以",
114
- "因",
115
- "又",
116
- "于",
117
- "则",
118
- "之",
119
- "这",
120
- "那",
121
- "其",
122
- "它",
123
- "他",
124
- "她",
125
- "们",
126
- "都",
127
- "要",
128
- "会",
129
- "能",
130
- "很",
131
- "得",
132
- "地",
133
- "着",
134
- "过",
135
- "吗",
136
- "呢",
137
- "吧",
138
- "啊",
139
- "哦",
140
- "嗯",
141
- "呀",
142
- ]);
143
- function containsHanText(text) {
144
- return HAN_REGEX.test(text);
145
- }
146
- function tokenizeHanPart(part) {
147
- const chars = Array.from(part);
148
- const covered = new Uint8Array(chars.length);
149
- const tokens = [];
150
- for (let index = 0; index < chars.length; index++) {
151
- const maxLength = Math.min(MAX_HAN_WORD_LENGTH, chars.length - index);
152
- for (let size = maxLength; size >= 2; size--) {
153
- const candidate = chars.slice(index, index + size).join("");
154
- if (COMMON_CHINESE_WORDS.has(candidate)) {
155
- tokens.push(candidate);
156
- for (let c = index; c < index + size; c++) {
157
- covered[c] = 1;
158
- }
159
- break;
160
- }
161
- }
162
- }
163
- for (let index = 0; index <= chars.length - 2; index++) {
164
- if (covered[index] || covered[index + 1]) {
165
- continue;
166
- }
167
- tokens.push(chars.slice(index, index + 2).join(""));
168
- }
169
- for (let index = 0; index < chars.length; index++) {
170
- if (!covered[index] && !CHINESE_STOP_CHARS.has(chars[index])) {
171
- tokens.push(chars[index]);
172
- }
173
- }
174
- // Trigrams across the whole run — they survive greedy dictionary shredding of domain compounds
175
- // and match verbatim on both the query and the stored side.
176
- for (let index = 0; index + 3 <= chars.length; index++) {
177
- tokens.push(chars.slice(index, index + 3).join(""));
178
- }
179
- return Array.from(new Set(tokens));
180
- }
181
- function tokenizeAsciiPart(part) {
182
- const tokens = [];
183
- const normalized = part.toLowerCase();
184
- if (normalized.length >= 2 && !LATIN_STOP_WORDS.has(normalized)) {
185
- tokens.push(normalized);
186
- }
187
- for (const segment of normalized.split(ASCII_SPLIT_REGEX).filter(Boolean)) {
188
- if (segment.length >= 2 && !LATIN_STOP_WORDS.has(segment)) {
189
- tokens.push(segment);
190
- }
191
- }
192
- return tokens;
193
- }
194
- export function tokenizeRecallText(text) {
195
- const parts = text.toLowerCase().match(TOKEN_PART_REGEX) ?? [];
196
- const tokens = [];
197
- for (const part of parts) {
198
- if (containsHanText(part)) {
199
- tokens.push(...tokenizeHanPart(part));
200
- }
201
- else {
202
- tokens.push(...tokenizeAsciiPart(part));
203
- }
204
- }
205
- return Array.from(new Set(tokens));
206
- }
207
- // -------------------------------------------------------------------------------------------------
208
- // Near-duplicate detection for memory_save (spec 050, D3)
209
- // -------------------------------------------------------------------------------------------------
210
- export const NEAR_DUPLICATE_JACCARD = 0.6;
1
+ import { localDayKey } from "../shared/local-time.js";
2
+ import { getSearchIndexPath, withSearchIndex } from "./search-index-db.js";
3
+ import { queryNotes, syncNoteSources } from "./search-index-notes.js";
4
+ import { buildQueryTerms } from "./search-terms.js";
5
+ // Calibrated against the 050 duplicate guard on the spec 072 acceptance pairs.
6
+ const NEAR_DUPLICATE_JACCARD = 0.4;
211
7
  function normalizeForEquality(text) {
212
8
  return text.normalize("NFKC").replace(/\s+/g, " ").trim().toLowerCase();
213
9
  }
@@ -215,8 +11,10 @@ export function descriptionSimilarity(a, b) {
215
11
  if (normalizeForEquality(a) === normalizeForEquality(b)) {
216
12
  return 1;
217
13
  }
218
- const setA = new Set(tokenizeRecallText(a));
219
- const setB = new Set(tokenizeRecallText(b));
14
+ const aTerms = buildQueryTerms(a);
15
+ const bTerms = buildQueryTerms(b);
16
+ const setA = new Set([...aTerms.latin, ...aTerms.cjk]);
17
+ const setB = new Set([...bTerms.latin, ...bTerms.cjk]);
220
18
  if (setA.size === 0 || setB.size === 0) {
221
19
  return 0;
222
20
  }
@@ -236,71 +34,13 @@ export function findNearDuplicateEntries(description, entries) {
236
34
  .sort((a, b) => b.score - a.score)
237
35
  .map(({ entry }) => entry);
238
36
  }
239
- function scoreText(queryTokens, text) {
240
- const textTokens = new Set(tokenizeRecallText(text));
241
- let score = 0;
242
- for (const token of queryTokens) {
243
- if (textTokens.has(token)) {
244
- score++;
245
- }
246
- }
247
- const lower = text.toLowerCase();
248
- for (const token of queryTokens) {
249
- if (token.length >= 3 && lower.includes(token)) {
250
- score += 0.5;
251
- }
252
- }
253
- // Prefer the line that actually contains a query token (the hit may be in the body, not the
254
- // description); fall back to the first non-empty line only when nothing matches on a line.
255
- const nonEmptyLines = text
256
- .split("\n")
257
- .map((l) => l.trim())
258
- .filter((l) => l.length > 0);
259
- const matchingLine = nonEmptyLines.find((l) => {
260
- const low = l.toLowerCase();
261
- return [...queryTokens].some((token) => token.length >= 2 && low.includes(token));
262
- });
263
- const chosen = matchingLine ?? nonEmptyLines[0] ?? text.trim();
264
- return { score, line: chosen.length > 200 ? `${chosen.slice(0, 197)}…` : chosen };
265
- }
266
37
  export function searchMemory(input) {
267
- const queryTokens = new Set(tokenizeRecallText(input.query));
268
- if (queryTokens.size === 0) {
269
- return [];
270
- }
271
- const hits = [];
272
- for (const entry of input.entries) {
273
- const { score, line } = scoreText(queryTokens, `${entry.description}\n${entry.body}`);
274
- if (score > 0) {
275
- hits.push({ kind: "memory", label: entry.name, date: entry.updated, line, score });
276
- }
277
- }
278
- for (const day of input.journal ?? []) {
279
- for (const rawLine of day.content.split("\n")) {
280
- const line = rawLine.replace(/^[-*]\s*/, "").trim();
281
- if (!line || line.startsWith("#")) {
282
- continue;
283
- }
284
- const { score, line: snippet } = scoreText(queryTokens, line);
285
- if (score > 0) {
286
- hits.push({ kind: "journal", label: day.date, date: day.date, line: snippet, score });
287
- }
288
- }
289
- }
290
- if (input.workspaceMemory) {
291
- // Keep the preamble and empty headings searchable without changing the shared parser.
292
- for (const block of input.workspaceMemory.split(/(?=^## )/m)) {
293
- if (!block.trim())
294
- continue;
295
- const heading = /^## ([^\n]+)/.exec(block)?.[1]?.trim() ?? "Background";
296
- const { score, line } = scoreText(queryTokens, block);
297
- if (score > 0) {
298
- hits.push({ kind: "workspace", label: heading, line, score });
299
- }
300
- }
301
- }
302
- return hits
303
- .sort((a, b) => b.score - a.score || (b.date ?? "").localeCompare(a.date ?? ""))
304
- .slice(0, input.limit ?? 12)
305
- .map(({ score: _score, ...hit }) => hit);
38
+ const { appHomeDir, desk, deskDir, workspaceDir, query } = input;
39
+ const terms = buildQueryTerms(query);
40
+ const requested = input.limit ?? 12;
41
+ const limit = Number.isFinite(requested) ? Math.max(0, Math.min(12, Math.floor(requested))) : 12;
42
+ return withSearchIndex(getSearchIndexPath(appHomeDir, desk), async (handle) => {
43
+ await syncNoteSources(handle, deskDir, workspaceDir);
44
+ return queryNotes(handle, terms, localDayKey(), limit);
45
+ });
306
46
  }
@@ -1,32 +1,21 @@
1
1
  export type SessionSearchSource = "context" | "session" | "log" | "task";
2
2
  export type SessionSearchRole = "user" | "assistant" | "tool" | "system" | "unknown";
3
- export interface SessionSearchDocument {
4
- id: string;
5
- source: SessionSearchSource;
3
+ export interface TranscriptSourceFile {
6
4
  path: string;
7
- timestamp?: string;
8
- role: SessionSearchRole;
9
- /** The full scannable message text (up to {@link SESSION_SCAN_MAX_CHARS}) — matching runs on
10
- * this, display windows are cut from it at query time (fix plan §2.5). */
11
- text: string;
12
- /** True when even the scan text was clipped: a keyword past this point cannot be found here. */
13
- scanTruncated?: boolean;
14
- sessionId?: string;
15
- /** The conversation directory the document came from (`dm-1`, `group-2`, …). */
5
+ relPath: string;
6
+ kind: "session" | "log" | "task";
7
+ source: SessionSearchSource;
16
8
  conversation?: string;
17
9
  }
18
- /**
19
- * Ceiling on how much of one message is kept for *matching*. The old code clipped each message to
20
- * the small display window at ingest, so a keyword in the middle of a long message was gone from
21
- * the corpus entirely and widening the query could never bring it back. This is deliberately much
22
- * larger than the display window; a single pathological message still cannot blow out memory.
23
- */
24
- export declare const SESSION_SCAN_MAX_CHARS = 20000;
25
- export interface BuildSessionCorpusOptions {
26
- /** The desk whose conversations — every channel that has talked to it — form the corpus (spec 058, D12). */
27
- deskDir: string;
28
- maxFiles: number;
29
- maxCharsPerDocument?: number;
30
- maxDocumentsTotal?: number;
10
+ export interface TranscriptDoc {
11
+ role: SessionSearchRole;
12
+ text: string;
13
+ truncated: boolean;
14
+ speaker?: string;
15
+ ts?: string;
31
16
  }
32
- export declare function buildSessionCorpus(options: BuildSessionCorpusOptions): Promise<SessionSearchDocument[]>;
17
+ export declare function parseTranscriptLine(source: SessionSearchSource, value: unknown): TranscriptDoc | null;
18
+ export declare function dedupKey(text: string): string;
19
+ export declare function listDeskTranscriptSources(deskDir: string): Promise<TranscriptSourceFile[]>;
20
+ export declare function archivedTaskDoc(mdPath: string, markdown: string, logContent: string, mtimeMs: number): TranscriptDoc | null;
21
+ export declare function parseJsonLine(line: string): unknown;