@framers/agentos 0.1.101 → 0.1.103
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 +16 -0
- package/dist/api/agency.js +1 -1
- package/dist/api/agency.js.map +1 -1
- package/dist/api/strategies/graph.d.ts.map +1 -1
- package/dist/api/strategies/graph.js +1 -0
- package/dist/api/strategies/graph.js.map +1 -1
- package/dist/api/strategies/sequential.d.ts.map +1 -1
- package/dist/api/strategies/sequential.js +1 -0
- package/dist/api/strategies/sequential.js.map +1 -1
- package/dist/memory/config.d.ts +39 -0
- package/dist/memory/config.d.ts.map +1 -1
- package/dist/memory/config.js.map +1 -1
- package/dist/memory/consolidation/ConsolidationLoop.d.ts +177 -0
- package/dist/memory/consolidation/ConsolidationLoop.d.ts.map +1 -0
- package/dist/memory/consolidation/ConsolidationLoop.js +517 -0
- package/dist/memory/consolidation/ConsolidationLoop.js.map +1 -0
- package/dist/memory/consolidation/ConsolidationPipeline.d.ts.map +1 -1
- package/dist/memory/consolidation/ConsolidationPipeline.js +7 -0
- package/dist/memory/consolidation/ConsolidationPipeline.js.map +1 -1
- package/dist/memory/consolidation/index.d.ts +8 -0
- package/dist/memory/consolidation/index.d.ts.map +1 -0
- package/dist/memory/consolidation/index.js +7 -0
- package/dist/memory/consolidation/index.js.map +1 -0
- package/dist/memory/decay/DecayModel.d.ts +33 -0
- package/dist/memory/decay/DecayModel.d.ts.map +1 -1
- package/dist/memory/decay/DecayModel.js +31 -0
- package/dist/memory/decay/DecayModel.js.map +1 -1
- package/dist/memory/facade/Memory.d.ts +228 -0
- package/dist/memory/facade/Memory.d.ts.map +1 -0
- package/dist/memory/facade/Memory.js +823 -0
- package/dist/memory/facade/Memory.js.map +1 -0
- package/dist/memory/facade/index.d.ts +13 -0
- package/dist/memory/facade/index.d.ts.map +1 -0
- package/dist/memory/facade/index.js +11 -0
- package/dist/memory/facade/index.js.map +1 -0
- package/dist/memory/facade/types.d.ts +606 -0
- package/dist/memory/facade/types.d.ts.map +1 -0
- package/dist/memory/facade/types.js +11 -0
- package/dist/memory/facade/types.js.map +1 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.d.ts +132 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.d.ts.map +1 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.js +178 -0
- package/dist/memory/feedback/RetrievalFeedbackSignal.js.map +1 -0
- package/dist/memory/feedback/index.d.ts +13 -0
- package/dist/memory/feedback/index.d.ts.map +1 -0
- package/dist/memory/feedback/index.js +12 -0
- package/dist/memory/feedback/index.js.map +1 -0
- package/dist/memory/index.d.ts +22 -0
- package/dist/memory/index.d.ts.map +1 -1
- package/dist/memory/index.js +24 -0
- package/dist/memory/index.js.map +1 -1
- package/dist/memory/ingestion/ChunkingEngine.d.ts +143 -0
- package/dist/memory/ingestion/ChunkingEngine.d.ts.map +1 -0
- package/dist/memory/ingestion/ChunkingEngine.js +508 -0
- package/dist/memory/ingestion/ChunkingEngine.js.map +1 -0
- package/dist/memory/ingestion/DoclingLoader.d.ts +44 -0
- package/dist/memory/ingestion/DoclingLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/DoclingLoader.js +228 -0
- package/dist/memory/ingestion/DoclingLoader.js.map +1 -0
- package/dist/memory/ingestion/DocxLoader.d.ts +37 -0
- package/dist/memory/ingestion/DocxLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/DocxLoader.js +111 -0
- package/dist/memory/ingestion/DocxLoader.js.map +1 -0
- package/dist/memory/ingestion/FolderScanner.d.ts +116 -0
- package/dist/memory/ingestion/FolderScanner.d.ts.map +1 -0
- package/dist/memory/ingestion/FolderScanner.js +127 -0
- package/dist/memory/ingestion/FolderScanner.js.map +1 -0
- package/dist/memory/ingestion/HtmlLoader.d.ts +49 -0
- package/dist/memory/ingestion/HtmlLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/HtmlLoader.js +202 -0
- package/dist/memory/ingestion/HtmlLoader.js.map +1 -0
- package/dist/memory/ingestion/IDocumentLoader.d.ts +63 -0
- package/dist/memory/ingestion/IDocumentLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/IDocumentLoader.js +11 -0
- package/dist/memory/ingestion/IDocumentLoader.js.map +1 -0
- package/dist/memory/ingestion/LoaderRegistry.d.ts +140 -0
- package/dist/memory/ingestion/LoaderRegistry.d.ts.map +1 -0
- package/dist/memory/ingestion/LoaderRegistry.js +229 -0
- package/dist/memory/ingestion/LoaderRegistry.js.map +1 -0
- package/dist/memory/ingestion/MarkdownLoader.d.ts +50 -0
- package/dist/memory/ingestion/MarkdownLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/MarkdownLoader.js +169 -0
- package/dist/memory/ingestion/MarkdownLoader.js.map +1 -0
- package/dist/memory/ingestion/MultimodalAggregator.d.ts +88 -0
- package/dist/memory/ingestion/MultimodalAggregator.d.ts.map +1 -0
- package/dist/memory/ingestion/MultimodalAggregator.js +96 -0
- package/dist/memory/ingestion/MultimodalAggregator.js.map +1 -0
- package/dist/memory/ingestion/OcrPdfLoader.d.ts +41 -0
- package/dist/memory/ingestion/OcrPdfLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/OcrPdfLoader.js +149 -0
- package/dist/memory/ingestion/OcrPdfLoader.js.map +1 -0
- package/dist/memory/ingestion/PdfLoader.d.ts +78 -0
- package/dist/memory/ingestion/PdfLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/PdfLoader.js +179 -0
- package/dist/memory/ingestion/PdfLoader.js.map +1 -0
- package/dist/memory/ingestion/TextLoader.d.ts +66 -0
- package/dist/memory/ingestion/TextLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/TextLoader.js +207 -0
- package/dist/memory/ingestion/TextLoader.js.map +1 -0
- package/dist/memory/ingestion/UrlLoader.d.ts +95 -0
- package/dist/memory/ingestion/UrlLoader.d.ts.map +1 -0
- package/dist/memory/ingestion/UrlLoader.js +174 -0
- package/dist/memory/ingestion/UrlLoader.js.map +1 -0
- package/dist/memory/io/ChatGptImporter.d.ts +85 -0
- package/dist/memory/io/ChatGptImporter.d.ts.map +1 -0
- package/dist/memory/io/ChatGptImporter.js +231 -0
- package/dist/memory/io/ChatGptImporter.js.map +1 -0
- package/dist/memory/io/JsonExporter.d.ts +67 -0
- package/dist/memory/io/JsonExporter.d.ts.map +1 -0
- package/dist/memory/io/JsonExporter.js +132 -0
- package/dist/memory/io/JsonExporter.js.map +1 -0
- package/dist/memory/io/JsonImporter.d.ts +84 -0
- package/dist/memory/io/JsonImporter.d.ts.map +1 -0
- package/dist/memory/io/JsonImporter.js +234 -0
- package/dist/memory/io/JsonImporter.js.map +1 -0
- package/dist/memory/io/MarkdownExporter.d.ts +95 -0
- package/dist/memory/io/MarkdownExporter.d.ts.map +1 -0
- package/dist/memory/io/MarkdownExporter.js +130 -0
- package/dist/memory/io/MarkdownExporter.js.map +1 -0
- package/dist/memory/io/MarkdownImporter.d.ts +84 -0
- package/dist/memory/io/MarkdownImporter.d.ts.map +1 -0
- package/dist/memory/io/MarkdownImporter.js +166 -0
- package/dist/memory/io/MarkdownImporter.js.map +1 -0
- package/dist/memory/io/ObsidianExporter.d.ts +80 -0
- package/dist/memory/io/ObsidianExporter.d.ts.map +1 -0
- package/dist/memory/io/ObsidianExporter.js +127 -0
- package/dist/memory/io/ObsidianExporter.js.map +1 -0
- package/dist/memory/io/ObsidianImporter.d.ts +93 -0
- package/dist/memory/io/ObsidianImporter.d.ts.map +1 -0
- package/dist/memory/io/ObsidianImporter.js +221 -0
- package/dist/memory/io/ObsidianImporter.js.map +1 -0
- package/dist/memory/io/SqliteExporter.d.ts +47 -0
- package/dist/memory/io/SqliteExporter.d.ts.map +1 -0
- package/dist/memory/io/SqliteExporter.js +56 -0
- package/dist/memory/io/SqliteExporter.js.map +1 -0
- package/dist/memory/io/SqliteImporter.d.ts +82 -0
- package/dist/memory/io/SqliteImporter.d.ts.map +1 -0
- package/dist/memory/io/SqliteImporter.js +232 -0
- package/dist/memory/io/SqliteImporter.js.map +1 -0
- package/dist/memory/io/index.d.ts +31 -0
- package/dist/memory/io/index.d.ts.map +1 -0
- package/dist/memory/io/index.js +31 -0
- package/dist/memory/io/index.js.map +1 -0
- package/dist/memory/store/SqliteBrain.d.ts +125 -0
- package/dist/memory/store/SqliteBrain.d.ts.map +1 -0
- package/dist/memory/store/SqliteBrain.js +407 -0
- package/dist/memory/store/SqliteBrain.js.map +1 -0
- package/dist/memory/store/SqliteKnowledgeGraph.d.ts +259 -0
- package/dist/memory/store/SqliteKnowledgeGraph.d.ts.map +1 -0
- package/dist/memory/store/SqliteKnowledgeGraph.js +1062 -0
- package/dist/memory/store/SqliteKnowledgeGraph.js.map +1 -0
- package/dist/memory/store/SqliteMemoryGraph.d.ts +251 -0
- package/dist/memory/store/SqliteMemoryGraph.d.ts.map +1 -0
- package/dist/memory/store/SqliteMemoryGraph.js +637 -0
- package/dist/memory/store/SqliteMemoryGraph.js.map +1 -0
- package/dist/memory/tools/MemoryAddTool.d.ts +98 -0
- package/dist/memory/tools/MemoryAddTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryAddTool.js +131 -0
- package/dist/memory/tools/MemoryAddTool.js.map +1 -0
- package/dist/memory/tools/MemoryDeleteTool.d.ts +83 -0
- package/dist/memory/tools/MemoryDeleteTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryDeleteTool.js +96 -0
- package/dist/memory/tools/MemoryDeleteTool.js.map +1 -0
- package/dist/memory/tools/MemoryMergeTool.d.ts +95 -0
- package/dist/memory/tools/MemoryMergeTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryMergeTool.js +164 -0
- package/dist/memory/tools/MemoryMergeTool.js.map +1 -0
- package/dist/memory/tools/MemoryReflectTool.d.ts +86 -0
- package/dist/memory/tools/MemoryReflectTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryReflectTool.js +102 -0
- package/dist/memory/tools/MemoryReflectTool.js.map +1 -0
- package/dist/memory/tools/MemorySearchTool.d.ts +117 -0
- package/dist/memory/tools/MemorySearchTool.d.ts.map +1 -0
- package/dist/memory/tools/MemorySearchTool.js +162 -0
- package/dist/memory/tools/MemorySearchTool.js.map +1 -0
- package/dist/memory/tools/MemoryUpdateTool.d.ts +92 -0
- package/dist/memory/tools/MemoryUpdateTool.d.ts.map +1 -0
- package/dist/memory/tools/MemoryUpdateTool.js +125 -0
- package/dist/memory/tools/MemoryUpdateTool.js.map +1 -0
- package/dist/memory/tools/index.d.ts +32 -0
- package/dist/memory/tools/index.d.ts.map +1 -0
- package/dist/memory/tools/index.js +26 -0
- package/dist/memory/tools/index.js.map +1 -0
- package/package.json +6 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview ChatGPT export importer for AgentOS memory brain.
|
|
3
|
+
*
|
|
4
|
+
* Parses the `conversations.json` file produced by ChatGPT's "Export data"
|
|
5
|
+
* feature and imports each conversation into the target `SqliteBrain`.
|
|
6
|
+
*
|
|
7
|
+
* ## Import strategy
|
|
8
|
+
*
|
|
9
|
+
* For each conversation object in `conversations.json`:
|
|
10
|
+
* 1. A row is inserted into the `conversations` table (deduped by title + created_at).
|
|
11
|
+
* 2. Every user/assistant message pair is encoded as an **episodic memory trace**
|
|
12
|
+
* whose content is `"[user]: {user_text}\n[assistant]: {assistant_text}"`.
|
|
13
|
+
* This preserves conversational context in a single retrievable unit.
|
|
14
|
+
* 3. System messages and tool messages are skipped (they are not episodic memories).
|
|
15
|
+
*
|
|
16
|
+
* ## ChatGPT export format
|
|
17
|
+
* ```json
|
|
18
|
+
* [
|
|
19
|
+
* {
|
|
20
|
+
* "title": "Conversation title",
|
|
21
|
+
* "create_time": 1711234567.89,
|
|
22
|
+
* "mapping": {
|
|
23
|
+
* "node-id": {
|
|
24
|
+
* "message": {
|
|
25
|
+
* "author": { "role": "user" },
|
|
26
|
+
* "content": { "parts": ["Hello!"] }
|
|
27
|
+
* },
|
|
28
|
+
* "children": ["next-node-id"]
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ]
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @module memory/io/ChatGptImporter
|
|
36
|
+
*/
|
|
37
|
+
import fs from 'node:fs/promises';
|
|
38
|
+
import crypto from 'node:crypto';
|
|
39
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// ChatGptImporter
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
/**
|
|
44
|
+
* Imports a ChatGPT `conversations.json` export into a `SqliteBrain`.
|
|
45
|
+
*
|
|
46
|
+
* **Usage:**
|
|
47
|
+
* ```ts
|
|
48
|
+
* const importer = new ChatGptImporter(brain);
|
|
49
|
+
* const result = await importer.import('/path/to/conversations.json');
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export class ChatGptImporter {
|
|
53
|
+
/**
|
|
54
|
+
* @param brain - The target `SqliteBrain` to import into.
|
|
55
|
+
*/
|
|
56
|
+
constructor(brain) {
|
|
57
|
+
this.brain = brain;
|
|
58
|
+
}
|
|
59
|
+
// -------------------------------------------------------------------------
|
|
60
|
+
// Public API
|
|
61
|
+
// -------------------------------------------------------------------------
|
|
62
|
+
/**
|
|
63
|
+
* Parse `conversations.json` and import all conversations and message pairs.
|
|
64
|
+
*
|
|
65
|
+
* @param sourcePath - Absolute path to the ChatGPT `conversations.json` file.
|
|
66
|
+
* @returns `ImportResult` with counts of imported traces, skipped duplicates,
|
|
67
|
+
* and any per-item error messages.
|
|
68
|
+
*/
|
|
69
|
+
async import(sourcePath) {
|
|
70
|
+
const result = { imported: 0, skipped: 0, errors: [] };
|
|
71
|
+
// ---- Load + parse ----
|
|
72
|
+
let raw;
|
|
73
|
+
try {
|
|
74
|
+
raw = await fs.readFile(sourcePath, 'utf8');
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
result.errors.push(`Failed to read file: ${String(err)}`);
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
let conversations;
|
|
81
|
+
try {
|
|
82
|
+
conversations = JSON.parse(raw);
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
result.errors.push(`Invalid JSON: ${String(err)}`);
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
if (!Array.isArray(conversations)) {
|
|
89
|
+
result.errors.push('Expected a JSON array at the top level of conversations.json');
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
// ---- Process each conversation ----
|
|
93
|
+
for (const convo of conversations) {
|
|
94
|
+
try {
|
|
95
|
+
this._importConversation(convo, result);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
result.errors.push(`Conversation import error: ${String(err)}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
// -------------------------------------------------------------------------
|
|
104
|
+
// Private helpers
|
|
105
|
+
// -------------------------------------------------------------------------
|
|
106
|
+
/**
|
|
107
|
+
* Import a single ChatGPT conversation object.
|
|
108
|
+
*
|
|
109
|
+
* Creates a `conversations` row and then iterates through the message
|
|
110
|
+
* mapping in tree order (BFS), pairing adjacent user/assistant messages
|
|
111
|
+
* into episodic memory traces.
|
|
112
|
+
*
|
|
113
|
+
* @param convo - Raw ChatGPT conversation object.
|
|
114
|
+
* @param result - Mutable result accumulator.
|
|
115
|
+
*/
|
|
116
|
+
_importConversation(convo, result) {
|
|
117
|
+
const db = this.brain.db;
|
|
118
|
+
const title = convo.title ?? 'Untitled';
|
|
119
|
+
const createdAt = convo.create_time ? Math.round(convo.create_time * 1000) : Date.now();
|
|
120
|
+
const updatedAt = convo.update_time ? Math.round(convo.update_time * 1000) : createdAt;
|
|
121
|
+
// Insert the conversation row (ignore duplicates by title + created_at).
|
|
122
|
+
const conversationId = `cv_${uuidv4()}`;
|
|
123
|
+
try {
|
|
124
|
+
db.prepare(`INSERT OR IGNORE INTO conversations (id, title, created_at, updated_at, metadata)
|
|
125
|
+
VALUES (?, ?, ?, ?, ?)`).run(conversationId, title, createdAt, updatedAt, JSON.stringify({ source: 'chatgpt_export' }));
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
result.errors.push(`Conversation insert error for "${title}": ${String(err)}`);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (!convo.mapping)
|
|
132
|
+
return;
|
|
133
|
+
// ---- Extract messages in tree order ----
|
|
134
|
+
// The mapping is a flat record of nodes keyed by ID. We use BFS starting
|
|
135
|
+
// from the root (node with no parent) to traverse in conversation order.
|
|
136
|
+
const nodes = convo.mapping;
|
|
137
|
+
// Find root node(s) — nodes with no parent or null parent.
|
|
138
|
+
const rootIds = Object.keys(nodes).filter((id) => !nodes[id]?.parent);
|
|
139
|
+
// Collect messages in BFS order.
|
|
140
|
+
const orderedMessages = [];
|
|
141
|
+
const queue = [...rootIds];
|
|
142
|
+
const visited = new Set();
|
|
143
|
+
while (queue.length > 0) {
|
|
144
|
+
const nodeId = queue.shift();
|
|
145
|
+
if (visited.has(nodeId))
|
|
146
|
+
continue;
|
|
147
|
+
visited.add(nodeId);
|
|
148
|
+
const node = nodes[nodeId];
|
|
149
|
+
if (!node)
|
|
150
|
+
continue;
|
|
151
|
+
const msg = node.message;
|
|
152
|
+
if (msg?.author?.role && msg.content?.parts) {
|
|
153
|
+
const textParts = msg.content.parts
|
|
154
|
+
.filter((p) => typeof p === 'string')
|
|
155
|
+
.join('');
|
|
156
|
+
if (textParts.trim()) {
|
|
157
|
+
orderedMessages.push({
|
|
158
|
+
role: msg.author.role,
|
|
159
|
+
text: textParts,
|
|
160
|
+
time: msg.create_time ? Math.round(msg.create_time * 1000) : createdAt,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// Enqueue children in order.
|
|
165
|
+
for (const childId of node.children ?? []) {
|
|
166
|
+
if (!visited.has(childId)) {
|
|
167
|
+
queue.push(childId);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// ---- Pair user + assistant messages into episodic traces ----
|
|
172
|
+
// We slide through the ordered messages and pair each user message with
|
|
173
|
+
// the immediately following assistant message.
|
|
174
|
+
for (let i = 0; i < orderedMessages.length; i++) {
|
|
175
|
+
const msg = orderedMessages[i];
|
|
176
|
+
// Skip system / tool messages.
|
|
177
|
+
if (msg.role === 'system' || msg.role === 'tool')
|
|
178
|
+
continue;
|
|
179
|
+
if (msg.role === 'user') {
|
|
180
|
+
// Look ahead for an assistant response.
|
|
181
|
+
const next = orderedMessages[i + 1];
|
|
182
|
+
const assistantText = next?.role === 'assistant' ? next.text : '';
|
|
183
|
+
const content = assistantText
|
|
184
|
+
? `[user]: ${msg.text}\n[assistant]: ${assistantText}`
|
|
185
|
+
: `[user]: ${msg.text}`;
|
|
186
|
+
this._insertEpisodicTrace(content, msg.time, conversationId, result);
|
|
187
|
+
// Skip the assistant message on the next iteration if we consumed it.
|
|
188
|
+
if (assistantText)
|
|
189
|
+
i++;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Insert a single episodic memory trace derived from a message pair.
|
|
195
|
+
*
|
|
196
|
+
* Deduplication is based on SHA-256 of the combined `content` string.
|
|
197
|
+
*
|
|
198
|
+
* @param content - The `[user]:...\n[assistant]:...` content string.
|
|
199
|
+
* @param createdAt - Unix timestamp (ms) of the user message.
|
|
200
|
+
* @param conversationId - ID of the parent conversation row.
|
|
201
|
+
* @param result - Mutable result accumulator.
|
|
202
|
+
*/
|
|
203
|
+
_insertEpisodicTrace(content, createdAt, conversationId, result) {
|
|
204
|
+
const hash = crypto.createHash('sha256').update(content, 'utf8').digest('hex');
|
|
205
|
+
// Dedup check.
|
|
206
|
+
const existing = this.brain.db
|
|
207
|
+
.prepare(`SELECT id FROM memory_traces WHERE json_extract(metadata, '$.import_hash') = ? LIMIT 1`)
|
|
208
|
+
.get(hash);
|
|
209
|
+
if (existing) {
|
|
210
|
+
result.skipped++;
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
this.brain.db
|
|
215
|
+
.prepare(`INSERT INTO memory_traces
|
|
216
|
+
(id, type, scope, content, embedding, strength, created_at, last_accessed,
|
|
217
|
+
retrieval_count, tags, emotions, metadata, deleted)
|
|
218
|
+
VALUES (?, 'episodic', 'user', ?, NULL, 1.0, ?, NULL, 0, '[]', '{}', ?, 0)`)
|
|
219
|
+
.run(`mt_${uuidv4()}`, content, createdAt, JSON.stringify({
|
|
220
|
+
import_hash: hash,
|
|
221
|
+
source: 'chatgpt_export',
|
|
222
|
+
conversation_id: conversationId,
|
|
223
|
+
}));
|
|
224
|
+
result.imported++;
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
result.errors.push(`Trace insert error: ${String(err)}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
//# sourceMappingURL=ChatGptImporter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatGptImporter.js","sourceRoot":"","sources":["../../../src/memory/io/ChatGptImporter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AA0CpC,8EAA8E;AAC9E,kBAAkB;AAClB,8EAA8E;AAE9E;;;;;;;;GAQG;AACH,MAAM,OAAO,eAAe;IAC1B;;OAEG;IACH,YAA6B,KAAkB;QAAlB,UAAK,GAAL,KAAK,CAAa;IAAG,CAAC;IAEnD,4EAA4E;IAC5E,aAAa;IACb,4EAA4E;IAE5E;;;;;;OAMG;IACH,KAAK,CAAC,MAAM,CAAC,UAAkB;QAC7B,MAAM,MAAM,GAAiB,EAAE,QAAQ,EAA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,CAAC,KAAK,CAAC,CAAC;QAE/E,eAAe;QACf,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE;aAC3B,OAAO,CACN,wFAAwF,CACzF;aACA,GAAG,CAAC,IAAI,CAAC,CAAC;QAEb,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO;QACT,CAAC;QAED,IAAI,CAAC;YACH,IAAI,CAAC,KAAK,CAAC,EAAE;iBACV,OAAO,CACN;;;sFAG4E,CAC7E;iBACA,GAAG,CACF,MAAM,MAAM,EAAE,EAAE,EAChB,OAAO,EACP,SAAS,EACT,IAAI,CAAC,SAAS,CAAC;gBACb,WAAW,EAAE,IAAI;gBACjB,MAAM,EAAE,gBAAgB;gBACxB,eAAe,EAAE,cAAc;aAChC,CAAC,CACH,CAAC;YAEJ,MAAM,CAAC,QAAQ,EAAE,CAAC;QACpB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,uBAAuB,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC3D,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview JSON exporter for AgentOS memory brain.
|
|
3
|
+
*
|
|
4
|
+
* Serialises all memory traces, knowledge graph nodes/edges, documents, and
|
|
5
|
+
* conversations from a `SqliteBrain` into a single structured JSON file.
|
|
6
|
+
* Optionally includes raw embedding vectors encoded as base64 strings.
|
|
7
|
+
*
|
|
8
|
+
* ## Output shape
|
|
9
|
+
* ```json
|
|
10
|
+
* {
|
|
11
|
+
* "meta": { "schema_version": "1", "exported_at": 1711234567890, ... },
|
|
12
|
+
* "traces": [...],
|
|
13
|
+
* "nodes": [...],
|
|
14
|
+
* "edges": [...],
|
|
15
|
+
* "documents": [...],
|
|
16
|
+
* "conversations": [...]
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @module memory/io/JsonExporter
|
|
21
|
+
*/
|
|
22
|
+
import type { ExportOptions } from '../facade/types.js';
|
|
23
|
+
import type { SqliteBrain } from '../store/SqliteBrain.js';
|
|
24
|
+
/**
|
|
25
|
+
* Exports a `SqliteBrain` to a structured JSON file.
|
|
26
|
+
*
|
|
27
|
+
* **Usage:**
|
|
28
|
+
* ```ts
|
|
29
|
+
* const exporter = new JsonExporter(brain);
|
|
30
|
+
* await exporter.export('/path/to/export.json', { includeEmbeddings: false });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare class JsonExporter {
|
|
34
|
+
private readonly brain;
|
|
35
|
+
/**
|
|
36
|
+
* @param brain - The `SqliteBrain` instance to read from.
|
|
37
|
+
*/
|
|
38
|
+
constructor(brain: SqliteBrain);
|
|
39
|
+
/**
|
|
40
|
+
* Export the full brain state to a JSON file at `outputPath`.
|
|
41
|
+
*
|
|
42
|
+
* @param outputPath - Absolute path to write the JSON output.
|
|
43
|
+
* @param options - Optional export configuration.
|
|
44
|
+
* @param options.includeEmbeddings - When `true`, raw embedding BLOBs are
|
|
45
|
+
* encoded as base64 strings and included in the output. Defaults to `false`.
|
|
46
|
+
* @param options.includeConversations - When `false`, conversation rows are
|
|
47
|
+
* omitted from the output. Defaults to `true`.
|
|
48
|
+
*/
|
|
49
|
+
export(outputPath: string, options?: ExportOptions): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Serialise a single `memory_traces` row, optionally stripping the embedding.
|
|
52
|
+
*
|
|
53
|
+
* @param row - Raw DB row.
|
|
54
|
+
* @param includeEmbeddings - Whether to encode the BLOB as base64.
|
|
55
|
+
* @returns Plain JS object safe for JSON serialisation.
|
|
56
|
+
*/
|
|
57
|
+
private _serializeTrace;
|
|
58
|
+
/**
|
|
59
|
+
* Serialise a single `knowledge_nodes` row, optionally stripping the embedding.
|
|
60
|
+
*
|
|
61
|
+
* @param row - Raw DB row.
|
|
62
|
+
* @param includeEmbeddings - Whether to encode the BLOB as base64.
|
|
63
|
+
* @returns Plain JS object safe for JSON serialisation.
|
|
64
|
+
*/
|
|
65
|
+
private _serializeNode;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=JsonExporter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonExporter.d.ts","sourceRoot":"","sources":["../../../src/memory/io/JsonExporter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAGH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAwE3D;;;;;;;;GAQG;AACH,qBAAa,YAAY;IAIX,OAAO,CAAC,QAAQ,CAAC,KAAK;IAHlC;;OAEG;gBAC0B,KAAK,EAAE,WAAW;IAM/C;;;;;;;;;OASG;IACG,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IA6DxE;;;;;;OAMG;IACH,OAAO,CAAC,eAAe;IAQvB;;;;;;OAMG;IACH,OAAO,CAAC,cAAc;CAOvB"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview JSON exporter for AgentOS memory brain.
|
|
3
|
+
*
|
|
4
|
+
* Serialises all memory traces, knowledge graph nodes/edges, documents, and
|
|
5
|
+
* conversations from a `SqliteBrain` into a single structured JSON file.
|
|
6
|
+
* Optionally includes raw embedding vectors encoded as base64 strings.
|
|
7
|
+
*
|
|
8
|
+
* ## Output shape
|
|
9
|
+
* ```json
|
|
10
|
+
* {
|
|
11
|
+
* "meta": { "schema_version": "1", "exported_at": 1711234567890, ... },
|
|
12
|
+
* "traces": [...],
|
|
13
|
+
* "nodes": [...],
|
|
14
|
+
* "edges": [...],
|
|
15
|
+
* "documents": [...],
|
|
16
|
+
* "conversations": [...]
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @module memory/io/JsonExporter
|
|
21
|
+
*/
|
|
22
|
+
import fs from 'node:fs/promises';
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// JsonExporter
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
/**
|
|
27
|
+
* Exports a `SqliteBrain` to a structured JSON file.
|
|
28
|
+
*
|
|
29
|
+
* **Usage:**
|
|
30
|
+
* ```ts
|
|
31
|
+
* const exporter = new JsonExporter(brain);
|
|
32
|
+
* await exporter.export('/path/to/export.json', { includeEmbeddings: false });
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class JsonExporter {
|
|
36
|
+
/**
|
|
37
|
+
* @param brain - The `SqliteBrain` instance to read from.
|
|
38
|
+
*/
|
|
39
|
+
constructor(brain) {
|
|
40
|
+
this.brain = brain;
|
|
41
|
+
}
|
|
42
|
+
// -------------------------------------------------------------------------
|
|
43
|
+
// Public API
|
|
44
|
+
// -------------------------------------------------------------------------
|
|
45
|
+
/**
|
|
46
|
+
* Export the full brain state to a JSON file at `outputPath`.
|
|
47
|
+
*
|
|
48
|
+
* @param outputPath - Absolute path to write the JSON output.
|
|
49
|
+
* @param options - Optional export configuration.
|
|
50
|
+
* @param options.includeEmbeddings - When `true`, raw embedding BLOBs are
|
|
51
|
+
* encoded as base64 strings and included in the output. Defaults to `false`.
|
|
52
|
+
* @param options.includeConversations - When `false`, conversation rows are
|
|
53
|
+
* omitted from the output. Defaults to `true`.
|
|
54
|
+
*/
|
|
55
|
+
async export(outputPath, options) {
|
|
56
|
+
const includeEmbeddings = options?.includeEmbeddings ?? false;
|
|
57
|
+
const includeConversations = options?.includeConversations ?? true;
|
|
58
|
+
const db = this.brain.db;
|
|
59
|
+
// Collect brain_meta as a plain object for the `meta` field.
|
|
60
|
+
const metaRows = db
|
|
61
|
+
.prepare('SELECT key, value FROM brain_meta')
|
|
62
|
+
.all();
|
|
63
|
+
const meta = {};
|
|
64
|
+
for (const row of metaRows) {
|
|
65
|
+
meta[row.key] = row.value;
|
|
66
|
+
}
|
|
67
|
+
meta['exported_at'] = String(Date.now());
|
|
68
|
+
// ---- memory_traces ----
|
|
69
|
+
const rawTraces = db
|
|
70
|
+
.prepare('SELECT * FROM memory_traces')
|
|
71
|
+
.all();
|
|
72
|
+
const traces = rawTraces.map((row) => this._serializeTrace(row, includeEmbeddings));
|
|
73
|
+
// ---- knowledge_nodes ----
|
|
74
|
+
const rawNodes = db
|
|
75
|
+
.prepare('SELECT * FROM knowledge_nodes')
|
|
76
|
+
.all();
|
|
77
|
+
const nodes = rawNodes.map((row) => this._serializeNode(row, includeEmbeddings));
|
|
78
|
+
// ---- knowledge_edges ----
|
|
79
|
+
const edges = db
|
|
80
|
+
.prepare('SELECT * FROM knowledge_edges')
|
|
81
|
+
.all();
|
|
82
|
+
// ---- documents ----
|
|
83
|
+
const documents = db
|
|
84
|
+
.prepare('SELECT * FROM documents')
|
|
85
|
+
.all();
|
|
86
|
+
// ---- conversations ----
|
|
87
|
+
const conversations = includeConversations
|
|
88
|
+
? db.prepare('SELECT * FROM conversations').all()
|
|
89
|
+
: [];
|
|
90
|
+
const payload = {
|
|
91
|
+
meta,
|
|
92
|
+
traces,
|
|
93
|
+
nodes,
|
|
94
|
+
edges,
|
|
95
|
+
documents,
|
|
96
|
+
conversations,
|
|
97
|
+
};
|
|
98
|
+
await fs.writeFile(outputPath, JSON.stringify(payload, null, 2), 'utf8');
|
|
99
|
+
}
|
|
100
|
+
// -------------------------------------------------------------------------
|
|
101
|
+
// Private helpers
|
|
102
|
+
// -------------------------------------------------------------------------
|
|
103
|
+
/**
|
|
104
|
+
* Serialise a single `memory_traces` row, optionally stripping the embedding.
|
|
105
|
+
*
|
|
106
|
+
* @param row - Raw DB row.
|
|
107
|
+
* @param includeEmbeddings - Whether to encode the BLOB as base64.
|
|
108
|
+
* @returns Plain JS object safe for JSON serialisation.
|
|
109
|
+
*/
|
|
110
|
+
_serializeTrace(row, includeEmbeddings) {
|
|
111
|
+
const out = { ...row };
|
|
112
|
+
if (row.embedding instanceof Buffer) {
|
|
113
|
+
out['embedding'] = includeEmbeddings ? row.embedding.toString('base64') : undefined;
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Serialise a single `knowledge_nodes` row, optionally stripping the embedding.
|
|
119
|
+
*
|
|
120
|
+
* @param row - Raw DB row.
|
|
121
|
+
* @param includeEmbeddings - Whether to encode the BLOB as base64.
|
|
122
|
+
* @returns Plain JS object safe for JSON serialisation.
|
|
123
|
+
*/
|
|
124
|
+
_serializeNode(row, includeEmbeddings) {
|
|
125
|
+
const out = { ...row };
|
|
126
|
+
if (row.embedding instanceof Buffer) {
|
|
127
|
+
out['embedding'] = includeEmbeddings ? row.embedding.toString('base64') : undefined;
|
|
128
|
+
}
|
|
129
|
+
return out;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=JsonExporter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonExporter.js","sourceRoot":"","sources":["../../../src/memory/io/JsonExporter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAsElC,8EAA8E;AAC9E,eAAe;AACf,8EAA8E;AAE9E;;;;;;;;GAQG;AACH,MAAM,OAAO,YAAY;IACvB;;OAEG;IACH,YAA6B,KAAkB;QAAlB,UAAK,GAAL,KAAK,CAAa;IAAG,CAAC;IAEnD,4EAA4E;IAC5E,aAAa;IACb,4EAA4E;IAE5E;;;;;;;;;OASG;IACH,KAAK,CAAC,MAAM,CAAC,UAAkB,EAAE,OAAuB;QACtD,MAAM,iBAAiB,GAAG,OAAO,EAAE,iBAAiB,IAAI,KAAK,CAAC;QAC9D,MAAM,oBAAoB,GAAG,OAAO,EAAE,oBAAoB,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,CAAC,CAAC,GAAG,EAAE;YACtE,CAAC,CAAC,EAAE,CAAC;QAEP,MAAM,OAAO,GAAG;YACd,IAAI;YACJ,MAAM;YACN,KAAK;YACL,KAAK;YACL,SAAS;YACT,aAAa;SACd,CAAC;QAEF,MAAM,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IAC3E,CAAC;IAED,4EAA4E;IAC5E,kBAAkB;IAClB,4EAA4E;IAE5E;;;;;;OAMG;IACK,eAAe,CAAC,GAAa,EAAE,iBAA0B;QAC/D,MAAM,GAAG,GAA4B,EAAE,GAAG,GAAG,EAAE,CAAC;QAChD,IAAI,GAAG,CAAC,SAAS,YAAY,MAAM,EAAE,CAAC;YACpC,GAAG,CAAC,WAAW,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACtF,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IAED;;;;;;OAMG;IACK,cAAc,CAAC,GAAY,EAAE,iBAA0B;QAC7D,MAAM,GAAG,GAA4B,EAAE,GAAG,GAAG,EAAE,CAAC;QAChD,IAAI,GAAG,CAAC,SAAS,YAAY,MAAM,EAAE,CAAC;YACpC,GAAG,CAAC,WAAW,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACtF,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview JSON importer for AgentOS memory brain.
|
|
3
|
+
*
|
|
4
|
+
* Reads a JSON file produced by `JsonExporter` (or a compatible schema) and
|
|
5
|
+
* merges its traces, knowledge nodes, and edges into a target `SqliteBrain`.
|
|
6
|
+
* Deduplication is performed via SHA-256 content hash — any trace whose hash
|
|
7
|
+
* already exists in the target brain is skipped rather than duplicated.
|
|
8
|
+
*
|
|
9
|
+
* @module memory/io/JsonImporter
|
|
10
|
+
*/
|
|
11
|
+
import type { ImportResult } from '../facade/types.js';
|
|
12
|
+
import type { SqliteBrain } from '../store/SqliteBrain.js';
|
|
13
|
+
/**
|
|
14
|
+
* Imports a `JsonExporter`-compatible JSON file into a `SqliteBrain`.
|
|
15
|
+
*
|
|
16
|
+
* **Usage:**
|
|
17
|
+
* ```ts
|
|
18
|
+
* const importer = new JsonImporter(brain);
|
|
19
|
+
* const result = await importer.import('/path/to/export.json');
|
|
20
|
+
* console.log(result.imported, result.skipped, result.errors);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class JsonImporter {
|
|
24
|
+
private readonly brain;
|
|
25
|
+
/**
|
|
26
|
+
* @param brain - The target `SqliteBrain` to import into.
|
|
27
|
+
*/
|
|
28
|
+
constructor(brain: SqliteBrain);
|
|
29
|
+
/**
|
|
30
|
+
* Read and merge a JSON export file into the target brain.
|
|
31
|
+
*
|
|
32
|
+
* Validation:
|
|
33
|
+
* - The file must be valid JSON.
|
|
34
|
+
* - The top-level object must contain a `traces` array.
|
|
35
|
+
*
|
|
36
|
+
* Deduplication:
|
|
37
|
+
* - For `memory_traces`: SHA-256 of `content` is used as the dedup key.
|
|
38
|
+
* Existing rows with the same hash are skipped.
|
|
39
|
+
* - For `knowledge_nodes`: SHA-256 of `label` + `type`.
|
|
40
|
+
* - For `knowledge_edges`: SHA-256 of `source_id` + `target_id` + `type`.
|
|
41
|
+
*
|
|
42
|
+
* @param sourcePath - Absolute path to the JSON file to import.
|
|
43
|
+
* @returns `ImportResult` with counts of imported, skipped, and errored items.
|
|
44
|
+
*/
|
|
45
|
+
import(sourcePath: string): Promise<ImportResult>;
|
|
46
|
+
/**
|
|
47
|
+
* Compute a SHA-256 hex digest of arbitrary string content.
|
|
48
|
+
* Used as a stable dedup key across import operations.
|
|
49
|
+
*
|
|
50
|
+
* @param content - The string to hash.
|
|
51
|
+
* @returns 64-character lowercase hex string.
|
|
52
|
+
*/
|
|
53
|
+
private _sha256;
|
|
54
|
+
/**
|
|
55
|
+
* Import memory trace records into `memory_traces`.
|
|
56
|
+
*
|
|
57
|
+
* Each trace is deduplicated by the SHA-256 of its `content` field.
|
|
58
|
+
* If a trace with the same content hash already exists, it is skipped.
|
|
59
|
+
*
|
|
60
|
+
* @param traces - Array of serialised trace objects from the export.
|
|
61
|
+
* @param result - Mutable `ImportResult` to accumulate counts.
|
|
62
|
+
*/
|
|
63
|
+
private _importTraces;
|
|
64
|
+
/**
|
|
65
|
+
* Import knowledge node records into `knowledge_nodes`.
|
|
66
|
+
*
|
|
67
|
+
* Dedup key: SHA-256 of `label` concatenated with `type`.
|
|
68
|
+
*
|
|
69
|
+
* @param nodes - Array of serialised node objects.
|
|
70
|
+
* @param result - Mutable `ImportResult` to accumulate counts.
|
|
71
|
+
*/
|
|
72
|
+
private _importNodes;
|
|
73
|
+
/**
|
|
74
|
+
* Import knowledge edge records into `knowledge_edges`.
|
|
75
|
+
*
|
|
76
|
+
* Dedup key: SHA-256 of `source_id + target_id + type`.
|
|
77
|
+
* Edges referencing non-existent nodes are silently skipped (FK constraint).
|
|
78
|
+
*
|
|
79
|
+
* @param edges - Array of serialised edge objects.
|
|
80
|
+
* @param result - Mutable `ImportResult` to accumulate counts.
|
|
81
|
+
*/
|
|
82
|
+
private _importEdges;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=JsonImporter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"JsonImporter.d.ts","sourceRoot":"","sources":["../../../src/memory/io/JsonImporter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAsF3D;;;;;;;;;GASG;AACH,qBAAa,YAAY;IAIX,OAAO,CAAC,QAAQ,CAAC,KAAK;IAHlC;;OAEG;gBAC0B,KAAK,EAAE,WAAW;IAM/C;;;;;;;;;;;;;;;OAeG;IACG,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAuCvD;;;;;;OAMG;IACH,OAAO,CAAC,OAAO;IAIf;;;;;;;;OAQG;IACH,OAAO,CAAC,aAAa;IAgErB;;;;;;;OAOG;IACH,OAAO,CAAC,YAAY;IAqDpB;;;;;;;;OAQG;IACH,OAAO,CAAC,YAAY;CAsDrB"}
|