@openanonymity/nanomem 0.1.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 +194 -0
- package/package.json +85 -0
- package/src/backends/BaseStorage.js +177 -0
- package/src/backends/filesystem.js +177 -0
- package/src/backends/indexeddb.js +208 -0
- package/src/backends/ram.js +113 -0
- package/src/backends/schema.js +42 -0
- package/src/bullets/bulletIndex.js +125 -0
- package/src/bullets/compaction.js +109 -0
- package/src/bullets/index.js +16 -0
- package/src/bullets/normalize.js +241 -0
- package/src/bullets/parser.js +199 -0
- package/src/bullets/scoring.js +53 -0
- package/src/cli/auth.js +323 -0
- package/src/cli/commands.js +411 -0
- package/src/cli/config.js +120 -0
- package/src/cli/diff.js +68 -0
- package/src/cli/help.js +84 -0
- package/src/cli/output.js +269 -0
- package/src/cli/spinner.js +54 -0
- package/src/cli.js +178 -0
- package/src/engine/compactor.js +247 -0
- package/src/engine/executors.js +152 -0
- package/src/engine/ingester.js +229 -0
- package/src/engine/retriever.js +414 -0
- package/src/engine/toolLoop.js +176 -0
- package/src/imports/chatgpt.js +160 -0
- package/src/imports/index.js +14 -0
- package/src/imports/markdown.js +104 -0
- package/src/imports/oaFastchat.js +124 -0
- package/src/index.js +199 -0
- package/src/llm/anthropic.js +264 -0
- package/src/llm/openai.js +179 -0
- package/src/prompt_sets/conversation/ingestion.js +51 -0
- package/src/prompt_sets/document/ingestion.js +43 -0
- package/src/prompt_sets/index.js +31 -0
- package/src/types.js +382 -0
- package/src/utils/portability.js +174 -0
- package/types/backends/BaseStorage.d.ts +42 -0
- package/types/backends/filesystem.d.ts +11 -0
- package/types/backends/indexeddb.d.ts +12 -0
- package/types/backends/ram.d.ts +8 -0
- package/types/backends/schema.d.ts +14 -0
- package/types/bullets/bulletIndex.d.ts +47 -0
- package/types/bullets/compaction.d.ts +10 -0
- package/types/bullets/index.d.ts +36 -0
- package/types/bullets/normalize.d.ts +95 -0
- package/types/bullets/parser.d.ts +31 -0
- package/types/bullets/scoring.d.ts +12 -0
- package/types/engine/compactor.d.ts +27 -0
- package/types/engine/executors.d.ts +46 -0
- package/types/engine/ingester.d.ts +29 -0
- package/types/engine/retriever.d.ts +50 -0
- package/types/engine/toolLoop.d.ts +9 -0
- package/types/imports/chatgpt.d.ts +14 -0
- package/types/imports/index.d.ts +3 -0
- package/types/imports/markdown.d.ts +31 -0
- package/types/imports/oaFastchat.d.ts +30 -0
- package/types/index.d.ts +21 -0
- package/types/llm/anthropic.d.ts +16 -0
- package/types/llm/openai.d.ts +16 -0
- package/types/prompt_sets/conversation/ingestion.d.ts +7 -0
- package/types/prompt_sets/document/ingestion.d.ts +7 -0
- package/types/prompt_sets/index.d.ts +11 -0
- package/types/types.d.ts +293 -0
- package/types/utils/portability.d.ts +33 -0
package/types/types.d.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
export type Tier = "working" | "long_term" | "history";
|
|
2
|
+
export type Status = "active" | "superseded" | "expired" | "uncertain";
|
|
3
|
+
export type Source = "user_statement" | "llm_infer" | "document" | "document_infer" | "assistant_summary" | "inference" | "system";
|
|
4
|
+
export type Confidence = "high" | "medium" | "low";
|
|
5
|
+
export type Bullet = {
|
|
6
|
+
text: string;
|
|
7
|
+
topic: string | null;
|
|
8
|
+
updatedAt: string | null;
|
|
9
|
+
expiresAt: string | null;
|
|
10
|
+
reviewAt: string | null;
|
|
11
|
+
tier: Tier;
|
|
12
|
+
status: Status;
|
|
13
|
+
source: Source | null;
|
|
14
|
+
confidence: Confidence | null;
|
|
15
|
+
explicitTier: boolean;
|
|
16
|
+
explicitStatus: boolean;
|
|
17
|
+
explicitSource: boolean;
|
|
18
|
+
explicitConfidence: boolean;
|
|
19
|
+
heading: string;
|
|
20
|
+
section: string;
|
|
21
|
+
lineIndex: number;
|
|
22
|
+
};
|
|
23
|
+
export type EnsureBulletMetadataOptions = {
|
|
24
|
+
defaultTopic?: string | undefined;
|
|
25
|
+
defaultTier?: Tier | undefined;
|
|
26
|
+
defaultStatus?: Status | undefined;
|
|
27
|
+
defaultSource?: Source | undefined;
|
|
28
|
+
defaultConfidence?: Confidence | undefined;
|
|
29
|
+
updatedAt?: string | undefined;
|
|
30
|
+
};
|
|
31
|
+
export type CompactionResult = {
|
|
32
|
+
working: Bullet[];
|
|
33
|
+
longTerm: Bullet[];
|
|
34
|
+
history: Bullet[];
|
|
35
|
+
active: Bullet[];
|
|
36
|
+
archive: Bullet[];
|
|
37
|
+
};
|
|
38
|
+
export type CompactBulletsOptions = {
|
|
39
|
+
today?: string | undefined;
|
|
40
|
+
maxActivePerTopic?: number | undefined;
|
|
41
|
+
defaultTopic?: string | undefined;
|
|
42
|
+
};
|
|
43
|
+
export type StorageMetadata = {
|
|
44
|
+
oneLiner?: string | undefined;
|
|
45
|
+
itemCount?: number | undefined;
|
|
46
|
+
titles?: string[] | undefined;
|
|
47
|
+
};
|
|
48
|
+
export type ExportRecord = {
|
|
49
|
+
path: string;
|
|
50
|
+
content?: string | undefined;
|
|
51
|
+
oneLiner?: string | undefined;
|
|
52
|
+
itemCount?: number | undefined;
|
|
53
|
+
titles?: string[] | undefined;
|
|
54
|
+
parentPath?: string | undefined;
|
|
55
|
+
createdAt?: number | undefined;
|
|
56
|
+
updatedAt?: number | undefined;
|
|
57
|
+
};
|
|
58
|
+
export type SearchResult = {
|
|
59
|
+
path: string;
|
|
60
|
+
lines: string[];
|
|
61
|
+
};
|
|
62
|
+
export type ListResult = {
|
|
63
|
+
files: string[];
|
|
64
|
+
dirs: string[];
|
|
65
|
+
};
|
|
66
|
+
export type ToolCallFunction = {
|
|
67
|
+
name: string;
|
|
68
|
+
arguments: string;
|
|
69
|
+
};
|
|
70
|
+
export type ToolCall = {
|
|
71
|
+
id: string;
|
|
72
|
+
type: "function";
|
|
73
|
+
function: ToolCallFunction;
|
|
74
|
+
};
|
|
75
|
+
export type ToolFunctionParameters = {
|
|
76
|
+
type: "object";
|
|
77
|
+
properties: Record<string, {
|
|
78
|
+
type: string;
|
|
79
|
+
description?: string;
|
|
80
|
+
}>;
|
|
81
|
+
required: string[];
|
|
82
|
+
};
|
|
83
|
+
export type ToolFunctionDef = {
|
|
84
|
+
name: string;
|
|
85
|
+
description: string;
|
|
86
|
+
parameters: ToolFunctionParameters;
|
|
87
|
+
};
|
|
88
|
+
export type ToolDefinition = {
|
|
89
|
+
type: "function";
|
|
90
|
+
function: ToolFunctionDef;
|
|
91
|
+
};
|
|
92
|
+
export type LLMMessage = {
|
|
93
|
+
role: "system" | "user" | "assistant" | "tool";
|
|
94
|
+
content?: string | null | undefined;
|
|
95
|
+
tool_calls?: ToolCall[] | undefined;
|
|
96
|
+
tool_call_id?: string | undefined;
|
|
97
|
+
};
|
|
98
|
+
export type ChatCompletionParams = {
|
|
99
|
+
model: string;
|
|
100
|
+
messages: LLMMessage[];
|
|
101
|
+
tools?: ToolDefinition[] | undefined;
|
|
102
|
+
max_tokens?: number | undefined;
|
|
103
|
+
temperature?: number | undefined;
|
|
104
|
+
};
|
|
105
|
+
export type StreamChatCompletionParams = {
|
|
106
|
+
model: string;
|
|
107
|
+
messages: LLMMessage[];
|
|
108
|
+
tools?: ToolDefinition[] | undefined;
|
|
109
|
+
max_tokens?: number | undefined;
|
|
110
|
+
temperature?: number | undefined;
|
|
111
|
+
onDelta?: ((text: string) => void) | undefined;
|
|
112
|
+
onReasoning?: ((text: string) => void) | undefined;
|
|
113
|
+
};
|
|
114
|
+
export type ChatCompletionResponse = {
|
|
115
|
+
content: string;
|
|
116
|
+
tool_calls: ToolCall[];
|
|
117
|
+
finish_reason?: string | undefined;
|
|
118
|
+
usage: {
|
|
119
|
+
prompt_tokens: number;
|
|
120
|
+
completion_tokens: number;
|
|
121
|
+
} | null;
|
|
122
|
+
};
|
|
123
|
+
export type LLMClient = {
|
|
124
|
+
createChatCompletion: (params: ChatCompletionParams) => Promise<ChatCompletionResponse>;
|
|
125
|
+
streamChatCompletion: (params: StreamChatCompletionParams) => Promise<ChatCompletionResponse>;
|
|
126
|
+
};
|
|
127
|
+
export type LLMClientOptions = {
|
|
128
|
+
apiKey: string;
|
|
129
|
+
baseUrl?: string | undefined;
|
|
130
|
+
headers?: Record<string, string> | undefined;
|
|
131
|
+
};
|
|
132
|
+
export type ProgressStage = "init" | "retrieval" | "fallback" | "tool_call" | "reasoning" | "loading" | "complete";
|
|
133
|
+
export type ProgressEvent = {
|
|
134
|
+
stage: ProgressStage;
|
|
135
|
+
message: string;
|
|
136
|
+
tool?: string | undefined;
|
|
137
|
+
args?: Record<string, any> | undefined;
|
|
138
|
+
result?: string | Record<string, any> | undefined;
|
|
139
|
+
paths?: string[] | undefined;
|
|
140
|
+
iteration?: number | undefined;
|
|
141
|
+
path?: string | undefined;
|
|
142
|
+
};
|
|
143
|
+
export type RetrievalResult = {
|
|
144
|
+
files: {
|
|
145
|
+
path: string;
|
|
146
|
+
content: string;
|
|
147
|
+
}[];
|
|
148
|
+
paths: string[];
|
|
149
|
+
assembledContext: string | null;
|
|
150
|
+
};
|
|
151
|
+
export type IngestOptions = {
|
|
152
|
+
updatedAt?: string | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* - Prompt set to use for extraction
|
|
155
|
+
*/
|
|
156
|
+
mode?: string | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* - Alias for mode (deprecated, use mode)
|
|
159
|
+
*/
|
|
160
|
+
extractionMode?: string | undefined;
|
|
161
|
+
sessionTitle?: string | undefined;
|
|
162
|
+
};
|
|
163
|
+
export type IngestResult = {
|
|
164
|
+
status: "processed" | "skipped" | "error";
|
|
165
|
+
writeCalls: number;
|
|
166
|
+
writes?: {
|
|
167
|
+
path: string;
|
|
168
|
+
before: string | null;
|
|
169
|
+
after: string | null;
|
|
170
|
+
}[] | undefined;
|
|
171
|
+
error?: string | undefined;
|
|
172
|
+
};
|
|
173
|
+
export type ToolCallLogEntry = {
|
|
174
|
+
name: string;
|
|
175
|
+
args: Record<string, any>;
|
|
176
|
+
result: string;
|
|
177
|
+
toolCallId: string;
|
|
178
|
+
};
|
|
179
|
+
export type ToolLoopResult = {
|
|
180
|
+
textResponse: string;
|
|
181
|
+
terminalToolResult: {
|
|
182
|
+
name: string;
|
|
183
|
+
arguments: Record<string, any>;
|
|
184
|
+
} | null;
|
|
185
|
+
messages: LLMMessage[];
|
|
186
|
+
iterations: number;
|
|
187
|
+
toolCallLog: ToolCallLogEntry[];
|
|
188
|
+
};
|
|
189
|
+
export type ToolLoopOptions = {
|
|
190
|
+
llmClient: LLMClient;
|
|
191
|
+
model: string;
|
|
192
|
+
tools: ToolDefinition[];
|
|
193
|
+
toolExecutors: Record<string, ToolExecutor>;
|
|
194
|
+
messages: LLMMessage[];
|
|
195
|
+
terminalTool?: string | null | undefined;
|
|
196
|
+
maxIterations?: number | undefined;
|
|
197
|
+
maxOutputTokens?: number | undefined;
|
|
198
|
+
temperature?: number | undefined;
|
|
199
|
+
onToolCall?: ((name: string, args: Record<string, any>, result: string) => void) | null | undefined;
|
|
200
|
+
onModelText?: ((text: string, iteration: number) => void) | null | undefined;
|
|
201
|
+
onReasoning?: ((chunk: string, iteration: number) => void) | null | undefined;
|
|
202
|
+
signal?: AbortSignal | null | undefined;
|
|
203
|
+
};
|
|
204
|
+
export type ExtractionExecutorHooks = {
|
|
205
|
+
normalizeContent?: ((content: string, path: string) => string) | undefined;
|
|
206
|
+
mergeWithExisting?: ((existing: string | null, incoming: string, path: string) => string) | undefined;
|
|
207
|
+
refreshIndex?: ((path: string) => Promise<void>) | undefined;
|
|
208
|
+
onWrite?: ((path: string, before: string, after: string) => void) | undefined;
|
|
209
|
+
};
|
|
210
|
+
export type ToolExecutor = (args: any) => Promise<string>;
|
|
211
|
+
export type StorageBackend = {
|
|
212
|
+
init: () => Promise<void>;
|
|
213
|
+
read: (path: string) => Promise<string | null>;
|
|
214
|
+
write: (path: string, content: string) => Promise<void>;
|
|
215
|
+
delete: (path: string) => Promise<void>;
|
|
216
|
+
exists: (path: string) => Promise<boolean>;
|
|
217
|
+
search: (query: string) => Promise<SearchResult[]>;
|
|
218
|
+
ls: (dirPath?: string) => Promise<ListResult>;
|
|
219
|
+
getTree: () => Promise<string | null>;
|
|
220
|
+
rebuildTree: () => Promise<void>;
|
|
221
|
+
exportAll: () => Promise<ExportRecord[]>;
|
|
222
|
+
clear: () => Promise<void>;
|
|
223
|
+
};
|
|
224
|
+
export type StorageFacade = {
|
|
225
|
+
read: (path: string) => Promise<string | null>;
|
|
226
|
+
write: (path: string, content: string) => Promise<void>;
|
|
227
|
+
delete: (path: string) => Promise<void>;
|
|
228
|
+
exists: (path: string) => Promise<boolean>;
|
|
229
|
+
search: (query: string) => Promise<SearchResult[]>;
|
|
230
|
+
ls: (dirPath?: string) => Promise<ListResult>;
|
|
231
|
+
getTree: () => Promise<string | null>;
|
|
232
|
+
rebuildTree: () => Promise<void>;
|
|
233
|
+
exportAll: () => Promise<ExportRecord[]>;
|
|
234
|
+
clear: () => Promise<void>;
|
|
235
|
+
};
|
|
236
|
+
export type MemoryBankLLMConfig = {
|
|
237
|
+
apiKey: string;
|
|
238
|
+
baseUrl?: string | undefined;
|
|
239
|
+
model?: string | undefined;
|
|
240
|
+
provider?: string | undefined;
|
|
241
|
+
headers?: Record<string, string> | undefined;
|
|
242
|
+
};
|
|
243
|
+
export type MemoryBankConfig = {
|
|
244
|
+
llm?: MemoryBankLLMConfig | undefined;
|
|
245
|
+
llmClient?: LLMClient | undefined;
|
|
246
|
+
model?: string | undefined;
|
|
247
|
+
storage?: StorageBackend | "ram" | "filesystem" | "indexeddb" | undefined;
|
|
248
|
+
storagePath?: string | undefined;
|
|
249
|
+
onProgress?: ((event: ProgressEvent) => void) | undefined;
|
|
250
|
+
onCompactProgress?: ((event: ProgressEvent) => void) | undefined;
|
|
251
|
+
onToolCall?: ((name: string, args: Record<string, any>, result: string) => void) | undefined;
|
|
252
|
+
onModelText?: ((text: string) => void) | undefined;
|
|
253
|
+
};
|
|
254
|
+
export type Message = {
|
|
255
|
+
role: "user" | "assistant";
|
|
256
|
+
content: string;
|
|
257
|
+
};
|
|
258
|
+
export type ChatGptSession = {
|
|
259
|
+
title: string | null;
|
|
260
|
+
messages: Message[];
|
|
261
|
+
updatedAt: string | null;
|
|
262
|
+
};
|
|
263
|
+
export type SessionSummary = {
|
|
264
|
+
id: string;
|
|
265
|
+
title: string;
|
|
266
|
+
createdAt: number | null;
|
|
267
|
+
updatedAt: number | null;
|
|
268
|
+
messageCount: number | null;
|
|
269
|
+
model: string | null;
|
|
270
|
+
};
|
|
271
|
+
export type SessionWithConversation = {
|
|
272
|
+
session: SessionSummary;
|
|
273
|
+
conversation: Message[];
|
|
274
|
+
};
|
|
275
|
+
export type BulletItem = {
|
|
276
|
+
path: string;
|
|
277
|
+
bullet: Bullet;
|
|
278
|
+
fileUpdatedAt: number;
|
|
279
|
+
};
|
|
280
|
+
export type MemoryBank = {
|
|
281
|
+
init: () => Promise<void>;
|
|
282
|
+
retrieve: (query: string, conversationText?: string) => Promise<RetrievalResult | null>;
|
|
283
|
+
ingest: (messages: Message[], options?: IngestOptions) => Promise<IngestResult>;
|
|
284
|
+
compact: () => Promise<{
|
|
285
|
+
filesChanged: number;
|
|
286
|
+
filesTotal: number;
|
|
287
|
+
} | undefined>;
|
|
288
|
+
storage: StorageFacade;
|
|
289
|
+
serialize: () => Promise<string>;
|
|
290
|
+
toZip: () => Promise<Uint8Array>;
|
|
291
|
+
_backend: StorageBackend;
|
|
292
|
+
_bulletIndex: import("./bullets/bulletIndex.js").MemoryBulletIndex;
|
|
293
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert an array of {path, content} records into a single portable string.
|
|
3
|
+
*
|
|
4
|
+
* Format:
|
|
5
|
+
* --- FILE: path/to/file.md
|
|
6
|
+
* <file content>
|
|
7
|
+
* --- FILE: other/file.md
|
|
8
|
+
* <file content>
|
|
9
|
+
*
|
|
10
|
+
* Note: file content must not contain a line that starts with "--- FILE: ".
|
|
11
|
+
* This is extremely unlikely for memory markdown but worth being aware of.
|
|
12
|
+
* @param {ExportRecord[]} records
|
|
13
|
+
* @returns {string}
|
|
14
|
+
*/
|
|
15
|
+
export function serialize(records: ExportRecord[]): string;
|
|
16
|
+
/**
|
|
17
|
+
* Reconstruct records from a serialized string produced by serialize().
|
|
18
|
+
* @param {string} str
|
|
19
|
+
* @returns {{ path: string, content: string }[]}
|
|
20
|
+
*/
|
|
21
|
+
export function deserialize(str: string): {
|
|
22
|
+
path: string;
|
|
23
|
+
content: string;
|
|
24
|
+
}[];
|
|
25
|
+
/**
|
|
26
|
+
* Produce a valid ZIP archive (STORE, no compression) from an array of records.
|
|
27
|
+
* Works in Node.js ≥ 18 and modern browsers (uses TextEncoder + Uint8Array only).
|
|
28
|
+
*
|
|
29
|
+
* @param {ExportRecord[]} records
|
|
30
|
+
* @returns {Uint8Array}
|
|
31
|
+
*/
|
|
32
|
+
export function toZip(records: ExportRecord[]): Uint8Array;
|
|
33
|
+
import type { ExportRecord } from '../types.js';
|