-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
344 lines (318 loc) · 11.7 KB
/
index.ts
File metadata and controls
344 lines (318 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { createHash } from "node:crypto";
import { basename, extname, join } from "node:path";
import { readFileSync } from "node:fs";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { Type } from "@sinclair/typebox";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { createMediaCli } from "./src/cli.js";
import { MultimodalEmbedder } from "./src/embedder.js";
import { extensionFromMimeType, inferMimeType, modalityFromMimeType, type MediaModality } from "./src/mime.js";
import { MediaStore } from "./src/store.js";
interface PluginConfig {
embedding: {
apiKey: string;
model?: string;
apiBase?: string;
dimensions?: number;
};
dbPath?: string;
blobPath?: string;
maxInlineBytes?: number;
modalities?: Partial<Record<MediaModality, boolean>>;
}
function asRecord(value: unknown): Record<string, unknown> | null {
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
return value as Record<string, unknown>;
}
function resolveEnvVars(value: string): string {
return value.replace(/\$\{([^}]+)\}/g, (_, key) => {
const envValue = process.env[key];
if (!envValue) throw new Error(`Environment variable ${key} is not set`);
return envValue;
});
}
function parseString(
source: Record<string, unknown>,
key: string,
fallback?: string,
): string | undefined {
const value = source[key];
if (typeof value === "string" && value.trim()) {
return resolveEnvVars(value.trim());
}
return fallback;
}
function parsePositiveInteger(
source: Record<string, unknown>,
key: string,
fallback: number,
): number {
const value = source[key];
if (typeof value === "number" && Number.isInteger(value) && value > 0) {
return value;
}
return fallback;
}
function parseMetadata(metadata: string | undefined): string {
const raw = metadata?.trim() || "{}";
try {
return JSON.stringify(JSON.parse(raw));
} catch (error) {
throw new Error(`metadata must be valid JSON: ${error instanceof Error ? error.message : String(error)}`);
}
}
function derivePreviewText(source: string): string {
if (/^https?:\/\//i.test(source)) {
try {
const url = new URL(source);
const filename = basename(url.pathname, extname(url.pathname));
return filename || url.hostname;
} catch {
return source;
}
}
return basename(source, extname(source));
}
function loadConfigCompatibilityFallback(): Record<string, unknown> | null {
try {
const cfgPath = process.env.OPENCLAW_CONFIG_PATH || `${process.env.HOME}/.openclaw/openclaw.json`;
const raw = JSON.parse(readFileSync(cfgPath, "utf8")) as Record<string, unknown>;
const plugins = asRecord(raw.plugins);
const entries = asRecord(plugins?.entries);
const entry = asRecord(entries?.["memory-multimodal-ingest"]);
return asRecord(entry?.config);
} catch {
return null;
}
}
function parsePluginConfig(value: unknown): PluginConfig {
const raw = asRecord(value);
const inlineCfg = asRecord(raw?.config) || raw;
const cfg = asRecord(inlineCfg?.embedding) ? inlineCfg : loadConfigCompatibilityFallback();
if (!cfg) {
throw new Error("memory-multimodal-ingest config is required");
}
const embedding = asRecord(cfg.embedding);
if (!embedding) {
throw new Error("embedding config is required");
}
const apiKey = parseString(embedding, "apiKey");
if (!apiKey) {
throw new Error("embedding.apiKey is required");
}
const model = parseString(embedding, "model", "gemini-embedding-2-preview");
if (!model) {
throw new Error("embedding.model is required");
}
return {
embedding: {
apiKey,
model,
apiBase: parseString(embedding, "apiBase", parseString(embedding, "baseURL", "https://generativelanguage.googleapis.com/v1beta")),
dimensions: parsePositiveInteger(embedding, "dimensions", 3072),
},
dbPath: parseString(cfg, "dbPath"),
blobPath: parseString(cfg, "blobPath"),
maxInlineBytes: parsePositiveInteger(cfg, "maxInlineBytes", 8 * 1024 * 1024),
modalities: asRecord(cfg.modalities) as Partial<Record<MediaModality, boolean>> | undefined,
};
}
async function readSourceBytes(source: string): Promise<{ bytes: Uint8Array; sourceUri: string; hintedMimeType?: string }> {
if (/^https?:\/\//i.test(source)) {
const res = await fetch(source);
if (!res.ok) {
throw new Error(`Failed to download ${source}: ${res.status} ${res.statusText}`);
}
return {
bytes: new Uint8Array(await res.arrayBuffer()),
sourceUri: source,
hintedMimeType: res.headers.get("content-type") || undefined
};
}
return {
bytes: new Uint8Array(await readFile(source)),
sourceUri: source
};
}
async function persistBlob(blobPath: string, id: string, mimeType: string, bytes: Uint8Array): Promise<string> {
await mkdir(blobPath, { recursive: true });
const filePath = join(blobPath, `${id}${extensionFromMimeType(mimeType)}`);
await writeFile(filePath, Buffer.from(bytes));
return filePath;
}
const plugin = {
id: "memory-multimodal-ingest",
register(api: OpenClawPluginApi) {
const config = parsePluginConfig(api.config);
const resolvedDbPath = api.resolvePath(config.dbPath || "~/.openclaw/memory/lancedb-multimodal");
const resolvedBlobPath = api.resolvePath(config.blobPath || "~/.openclaw/memory/blobs");
const embedder = new MultimodalEmbedder({
apiKey: config.embedding.apiKey,
model: config.embedding.model || "gemini-embedding-2-preview",
apiBase: config.embedding.apiBase || "https://generativelanguage.googleapis.com/v1beta",
dimensions: config.embedding.dimensions || 3072
});
const store = new MediaStore({
dbPath: resolvedDbPath,
vectorDim: embedder.dimensions
});
const ingest = async (
source: string,
options?: { previewText?: string; scope?: string; metadata?: string; modality?: MediaModality }
) => {
const loaded = await readSourceBytes(source);
if (loaded.bytes.byteLength > (config.maxInlineBytes || 8 * 1024 * 1024)) {
throw new Error(`File too large for current inline_data MVP: ${loaded.bytes.byteLength} bytes`);
}
const mimeType = inferMimeType(source, loaded.hintedMimeType);
if (!mimeType) {
throw new Error(`Could not infer MIME type for ${source}`);
}
const modality = options?.modality || modalityFromMimeType(mimeType);
if (!modality) {
throw new Error(`Unsupported modality for MIME type ${mimeType}`);
}
if (config.modalities?.[modality] === false) {
throw new Error(`Modality ${modality} is disabled by config`);
}
const previewText = options?.previewText?.trim() || derivePreviewText(loaded.sourceUri);
const vector = await embedder.embedInlineDataWithText(mimeType, loaded.bytes, previewText);
const digest = createHash("sha256").update(loaded.bytes).digest("hex");
const blobId = createHash("sha256").update(`${digest}:${Date.now()}`).digest("hex").slice(0, 16);
const savedBlobPath = await persistBlob(resolvedBlobPath, blobId, mimeType, loaded.bytes);
return store.store({
modality,
mimeType,
sourceUri: loaded.sourceUri,
blobPath: savedBlobPath,
previewText,
scope: options?.scope || "global",
metadata: parseMetadata(options?.metadata),
contentHash: digest,
vector
});
};
api.registerTool({
name: "memory_media_store",
label: "Memory Media Store",
description: "Store an image, video, audio file, or PDF in multimodal memory.",
parameters: Type.Object({
source: Type.String({ description: "Local file path or HTTP/HTTPS URL" }),
previewText: Type.Optional(Type.String()),
scope: Type.Optional(Type.String()),
metadata: Type.Optional(Type.String()),
modality: Type.Optional(Type.Union([
Type.Literal("image"),
Type.Literal("video"),
Type.Literal("audio"),
Type.Literal("pdf")
]))
}),
async execute(_toolCallId, params) {
try {
const entry = await ingest((params as any).source, {
previewText: (params as any).previewText,
scope: (params as any).scope,
metadata: (params as any).metadata,
modality: (params as any).modality
});
return {
content: [{ type: "text", text: `Stored ${entry.modality} memory ${entry.id.slice(0, 8)} from ${entry.sourceUri}` }],
details: entry
};
} catch (error) {
return {
content: [{ type: "text", text: `Multimodal memory store failed: ${error instanceof Error ? error.message : String(error)}` }],
details: { error: String(error) }
};
}
}
}, { name: "memory_media_store" });
api.registerTool({
name: "memory_media_search",
label: "Memory Media Search",
description: "Search multimodal memories with a text query.",
parameters: Type.Object({
query: Type.String(),
limit: Type.Optional(Type.Number()),
scope: Type.Optional(Type.String()),
modality: Type.Optional(Type.Union([
Type.Literal("image"),
Type.Literal("video"),
Type.Literal("audio"),
Type.Literal("pdf")
]))
}),
async execute(_toolCallId, params) {
try {
const vector = await embedder.embedText((params as any).query);
const results = await store.search(
vector,
Math.trunc((params as any).limit || 5),
(params as any).modality,
(params as any).scope,
(params as any).query,
);
return {
content: [{ type: "text", text: `Found ${results.length} multimodal memories` }],
details: results
};
} catch (error) {
return {
content: [{ type: "text", text: `Multimodal memory search failed: ${error instanceof Error ? error.message : String(error)}` }],
details: { error: String(error) }
};
}
}
}, { name: "memory_media_search" });
api.registerTool({
name: "memory_media_list",
label: "Memory Media List",
description: "List stored multimodal memories.",
parameters: Type.Object({
limit: Type.Optional(Type.Number()),
scope: Type.Optional(Type.String()),
modality: Type.Optional(Type.Union([
Type.Literal("image"),
Type.Literal("video"),
Type.Literal("audio"),
Type.Literal("pdf")
]))
}),
async execute(_toolCallId, params) {
const rows = await store.list(Math.trunc((params as any).limit || 20), (params as any).modality, (params as any).scope);
return {
content: [{ type: "text", text: `Listed ${rows.length} multimodal memories` }],
details: rows
};
}
}, { name: "memory_media_list" });
api.registerTool({
name: "memory_media_delete",
label: "Memory Media Delete",
description: "Delete a stored multimodal memory by id.",
parameters: Type.Object({
id: Type.String()
}),
async execute(_toolCallId, params) {
const deleted = await store.delete((params as any).id);
return {
content: [{ type: "text", text: deleted ? `Deleted ${(params as any).id}` : `Not found: ${(params as any).id}` }],
details: { deleted }
};
}
}, { name: "memory_media_delete" });
api.registerCli(
createMediaCli({
store,
embedder,
ingest
}),
{ commands: ["memory-media"] }
);
api.logger.info(
`memory-multimodal-ingest: plugin registered (db: ${resolvedDbPath}, model: ${config.embedding.model || "gemini-embedding-2-preview"})`
);
}
};
export default plugin;