openclaw-cloudflare-vectorize-memory 0.1.3 → 0.1.4
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/cli-metadata.ts +26 -18
- package/dist/cli.js.map +1 -1
- package/dist/doctor.js.map +1 -1
- package/dist/service.js.map +1 -1
- package/dist/vectorize-client.js.map +1 -1
- package/package.json +1 -1
package/cli-metadata.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
|
2
|
-
|
|
3
|
-
export default definePluginEntry({
|
|
4
|
-
id: "memory-cloudflare-vectorize",
|
|
5
|
-
name: "Cloudflare Vectorize Memory",
|
|
6
|
-
description: "OpenClaw memory plugin backed by Cloudflare Vectorize and Workers AI embeddings.",
|
|
7
|
-
register(api) {
|
|
8
|
-
api.registerCli(() => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
|
2
|
+
|
|
3
|
+
export default definePluginEntry({
|
|
4
|
+
id: "memory-cloudflare-vectorize",
|
|
5
|
+
name: "Cloudflare Vectorize Memory",
|
|
6
|
+
description: "OpenClaw memory plugin backed by Cloudflare Vectorize and Workers AI embeddings.",
|
|
7
|
+
register(api) {
|
|
8
|
+
api.registerCli(async ({ program }) => {
|
|
9
|
+
const cliModulePath = "./dist/cli.js";
|
|
10
|
+
const { registerCloudflareMemoryCli }: typeof import("./src/cli.js") = await import(cliModulePath);
|
|
11
|
+
registerCloudflareMemoryCli(program, {
|
|
12
|
+
pluginConfig: api.pluginConfig,
|
|
13
|
+
openClawConfig: api.config,
|
|
14
|
+
resolvePath: api.resolvePath,
|
|
15
|
+
});
|
|
16
|
+
}, {
|
|
17
|
+
descriptors: [
|
|
18
|
+
{
|
|
19
|
+
name: "cf-memory",
|
|
20
|
+
description: "Manage Cloudflare Vectorize memory",
|
|
21
|
+
hasSubcommands: true,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
});
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","names":[],"sources":["../src/cli.ts"],"sourcesContent":["import type { OpenClawConfig } from \"openclaw/plugin-sdk/config-runtime\";\r\nimport type { DoctorReport, IndexInitializationReport, MetadataFilter, MigrationDuplicateStrategy, SmokeTestReport } from \"./types.js\";\r\nimport { formatMigrationSummary, runCloudflareMemoryMigration } from \"./migration.js\";\r\nimport { createCloudflareMemoryService } from \"./service-factory.js\";\r\n\r\ntype CliCommand = {\r\n\tcommand: (name: string) => CliCommand;\r\n\tdescription: (description: string) => CliCommand;\r\n\targument: (name: string, description: string) => CliCommand;\r\n\toption: (flags: string, description: string) => CliCommand;\r\n\taction: (handler: (...args: unknown[]) => Promise<void> | void) => CliCommand;\r\n\topts?: () => Record<string, unknown>;\r\n};\r\n\r\nfunction printJson(value: unknown): void {\r\n\tconsole.log(JSON.stringify(value, null, 2));\r\n}\r\n\r\nfunction printCheckReport(report: DoctorReport | IndexInitializationReport | SmokeTestReport): void {\r\n\tfor (const check of report.checks) {\r\n\t\tconsole.log(`[${check.status}] ${check.name}: ${check.message}`);\r\n\t}\r\n}\r\n\r\nfunction parseMetadataFlag(value: string | undefined): Record<string, string | number | boolean> | undefined {\r\n\tif (!value) {\r\n\t\treturn undefined;\r\n\t}\r\n\tconst parsed = JSON.parse(value) as unknown;\r\n\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\r\n\t\tthrow new Error(\"--metadata must be a JSON object.\");\r\n\t}\r\n\treturn parsed as Record<string, string | number | boolean>;\r\n}\r\n\r\nfunction parseFilterFlag(value: string | undefined): MetadataFilter | undefined {\r\n\tif (!value) {\r\n\t\treturn undefined;\r\n\t}\r\n\tconst parsed = JSON.parse(value) as unknown;\r\n\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\r\n\t\tthrow new Error(\"--filter must be a JSON object.\");\r\n\t}\r\n\treturn parsed as MetadataFilter;\r\n}\r\n\r\nfunction isCliCommand(value: unknown): value is CliCommand {\r\n\treturn Boolean(value) && typeof value === \"object\" && typeof (value as CliCommand).opts === \"function\";\r\n}\r\n\r\nfunction resolveInvocation(args: unknown[]): { positionals: unknown[]; options: Record<string, unknown> } {\r\n\tconst maybeCommand = args.at(-1);\r\n\tif (!isCliCommand(maybeCommand)) {\r\n\t\treturn {\r\n\t\t\tpositionals: args,\r\n\t\t\toptions: {},\r\n\t\t};\r\n\t}\r\n\treturn {\r\n\t\tpositionals: args.slice(0, -1),\r\n\t\toptions: maybeCommand.opts?.() ?? {},\r\n\t};\r\n}\r\n\r\nfunction parseDuplicateStrategy(value: unknown): MigrationDuplicateStrategy | undefined {\r\n\tif (value === undefined) {\r\n\t\treturn undefined;\r\n\t}\r\n\tif (value === \"overwrite\" || value === \"skip\" || value === \"fail\") {\r\n\t\treturn value;\r\n\t}\r\n\tthrow new Error(\"--if-exists must be overwrite, skip, or fail.\");\r\n}\r\n\r\nexport function registerCloudflareMemoryCli(\r\n\tprogram: {\r\n\t\tcommand: (name: string) => CliCommand;\r\n\t},\r\n\tparams: {\r\n\t\tpluginConfig: unknown;\r\n\t\topenClawConfig: OpenClawConfig;\r\n\t\tresolvePath?: (input: string) => string;\r\n\t},\r\n): void {\r\n\tconst root = program.command(\"cf-memory\").description(\"Manage Cloudflare memory records.\");\r\n\r\n\tfunction resolveOptions(args: unknown[]): Record<string, unknown> {\r\n\t\treturn resolveInvocation(args).options;\r\n\t}\r\n\r\n\troot\r\n\t\t.command(\"doctor\")\r\n\t\t.description(\"Validate Workers AI and Vectorize configuration.\")\r\n\t\t.option(\"--create-index\", \"Create the Vectorize index if missing.\")\r\n\t\t.option(\"--json\", \"Print structured JSON output.\")\r\n\t\t.action(async (...args) => {\r\n\t\t\tconst options = resolveOptions(args);\r\n\t\t\tconst service = await createCloudflareMemoryService({\r\n\t\t\t\tpluginConfig: params.pluginConfig,\r\n\t\t\t\topenClawConfig: params.openClawConfig,\r\n\t\t\t\tenv: process.env,\r\n\t\t\t\tresolvePath: params.resolvePath,\r\n\t\t\t});\r\n\t\t\tconst report = await service.doctor({\r\n\t\t\t\tcreateIndexIfMissing: Boolean(options.createIndex),\r\n\t\t\t});\r\n\t\t\tif (options.json) {\r\n\t\t\t\tprintJson(report);\r\n\t\t\t} else {\r\n\t\t\t\tprintCheckReport(report);\r\n\t\t\t}\r\n\t\t\tif (!report.ok) {\r\n\t\t\t\tprocess.exitCode = 1;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\troot\r\n\t\t.command(\"init\")\r\n\t\t.description(\"Initialize the Cloudflare Vectorize index for the configured embedding model.\")\r\n\t\t.option(\"--json\", \"Print structured JSON output.\")\r\n\t\t.action(async (...args) => {\r\n\t\t\tconst options = resolveOptions(args);\r\n\t\t\tconst service = await createCloudflareMemoryService({\r\n\t\t\t\tpluginConfig: params.pluginConfig,\r\n\t\t\t\topenClawConfig: params.openClawConfig,\r\n\t\t\t\tenv: process.env,\r\n\t\t\t\tresolvePath: params.resolvePath,\r\n\t\t\t});\r\n\t\t\tconst report = await service.initializeIndex();\r\n\t\t\tif (options.json) {\r\n\t\t\t\tprintJson(report);\r\n\t\t\t} else {\r\n\t\t\t\tprintCheckReport(report);\r\n\t\t\t}\r\n\t\t\tif (!report.ok) {\r\n\t\t\t\tprocess.exitCode = 1;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\troot\r\n\t\t.command(\"test\")\r\n\t\t.description(\"Run an end-to-end embedding and semantic-search smoke test.\")\r\n\t\t.option(\"--json\", \"Print structured JSON output.\")\r\n\t\t.action(async (...args) => {\r\n\t\t\tconst options = resolveOptions(args);\r\n\t\t\tconst service = await createCloudflareMemoryService({\r\n\t\t\t\tpluginConfig: params.pluginConfig,\r\n\t\t\t\topenClawConfig: params.openClawConfig,\r\n\t\t\t\tenv: process.env,\r\n\t\t\t\tresolvePath: params.resolvePath,\r\n\t\t\t});\r\n\t\t\tconst report = await service.runSmokeTest();\r\n\t\t\tif (options.json) {\r\n\t\t\t\tprintJson(report);\r\n\t\t\t} else {\r\n\t\t\t\tprintCheckReport(report);\r\n\t\t\t}\r\n\t\t\tif (!report.ok) {\r\n\t\t\t\tprocess.exitCode = 1;\r\n\t\t\t}\r\n\t\t});\r\n\r\n\troot\r\n\t\t.command(\"search\")\r\n\t\t.description(\"Search stored Cloudflare memory.\")\r\n\t\t.argument(\"<query>\", \"Semantic search query.\")\r\n\t\t.option(\"--namespace <namespace>\", \"Optional namespace override.\")\r\n\t\t.option(\"--limit <count>\", \"Maximum number of results.\")\r\n\t\t.option(\"--filter <json>\", \"Optional metadata filter JSON.\")\r\n\t\t.action(async (query, opts) => {\r\n\t\t\tconst options = opts as Record<string, unknown>;\r\n\t\t\tconst service = await createCloudflareMemoryService({\r\n\t\t\t\tpluginConfig: params.pluginConfig,\r\n\t\t\t\topenClawConfig: params.openClawConfig,\r\n\t\t\t\tenv: process.env,\r\n\t\t\t\tresolvePath: params.resolvePath,\r\n\t\t\t});\r\n\t\t\tconst results = await service.search({\r\n\t\t\t\tquery: String(query),\r\n\t\t\t\tnamespace: options.namespace as string | undefined,\r\n\t\t\t\tmaxResults: options.limit ? Number(options.limit) : undefined,\r\n\t\t\t\tfilter: parseFilterFlag(options.filter as string | undefined),\r\n\t\t\t});\r\n\t\t\tprintJson(results);\r\n\t\t});\r\n\r\n\troot\r\n\t\t.command(\"upsert\")\r\n\t\t.description(\"Insert or update a memory record.\")\r\n\t\t.argument(\"<text>\", \"Memory text.\")\r\n\t\t.option(\"--id <id>\", \"Stable logical id.\")\r\n\t\t.option(\"--title <title>\", \"Optional title.\")\r\n\t\t.option(\"--namespace <namespace>\", \"Optional namespace override.\")\r\n\t\t.option(\"--source <source>\", \"Optional source label.\")\r\n\t\t.option(\"--metadata <json>\", \"Optional metadata JSON object.\")\r\n\t\t.action(async (text, opts) => {\r\n\t\t\tconst options = opts as Record<string, unknown>;\r\n\t\t\tconst service = await createCloudflareMemoryService({\r\n\t\t\t\tpluginConfig: params.pluginConfig,\r\n\t\t\t\topenClawConfig: params.openClawConfig,\r\n\t\t\t\tenv: process.env,\r\n\t\t\t\tresolvePath: params.resolvePath,\r\n\t\t\t});\r\n\t\t\tconst result = await service.upsert({\r\n\t\t\t\tinput: {\r\n\t\t\t\t\tid: options.id as string | undefined,\r\n\t\t\t\t\ttitle: options.title as string | undefined,\r\n\t\t\t\t\ttext: String(text),\r\n\t\t\t\t\tnamespace: options.namespace as string | undefined,\r\n\t\t\t\t\tsource: options.source as string | undefined,\r\n\t\t\t\t\tmetadata: parseMetadataFlag(options.metadata as string | undefined),\r\n\t\t\t\t},\r\n\t\t\t});\r\n\t\t\tprintJson(result);\r\n\t\t});\r\n\r\n\troot\r\n\t\t.command(\"delete\")\r\n\t\t.description(\"Delete a memory record.\")\r\n\t\t.argument(\"<id>\", \"Logical memory record id.\")\r\n\t\t.option(\"--namespace <namespace>\", \"Optional namespace override.\")\r\n\t\t.action(async (id, opts) => {\r\n\t\t\tconst options = opts as Record<string, unknown>;\r\n\t\t\tconst service = await createCloudflareMemoryService({\r\n\t\t\t\tpluginConfig: params.pluginConfig,\r\n\t\t\t\topenClawConfig: params.openClawConfig,\r\n\t\t\t\tenv: process.env,\r\n\t\t\t\tresolvePath: params.resolvePath,\r\n\t\t\t});\r\n\t\t\tconst mutationId = await service.delete({\r\n\t\t\t\tid: String(id),\r\n\t\t\t\tnamespace: options.namespace as string | undefined,\r\n\t\t\t});\r\n\t\t\tprintJson({ id, mutationId });\r\n\t\t});\r\n\r\n\troot\r\n\t\t.command(\"migrate\")\r\n\t\t.description(\"Migrate legacy markdown memory into Cloudflare Vectorize.\")\r\n\t\t.argument(\"[sources...]\", \"Markdown files, directories, or glob patterns. Defaults to the current OpenClaw memory corpus when omitted.\")\r\n\t\t.option(\"--workspace <path>\", \"Workspace root used for default-provider discovery and relative path normalization.\")\r\n\t\t.option(\"--namespace <namespace>\", \"Target namespace override.\")\r\n\t\t.option(\"--derive-namespace-from-path\", \"Derive namespaces from the first relative path segment instead of using a single target namespace.\")\r\n\t\t.option(\"--if-exists <strategy>\", \"Duplicate handling: overwrite, skip, or fail.\")\r\n\t\t.option(\"--create-index\", \"Create the Vectorize index if missing.\")\r\n\t\t.option(\"--dry-run\", \"Plan the migration without writing records.\")\r\n\t\t.option(\"--json\", \"Print structured JSON output.\")\r\n\t\t.action(async (...args) => {\r\n\t\t\tconst { positionals, options } = resolveInvocation(args);\r\n\t\t\tconst rawSources = positionals[0];\r\n\t\t\tconst sourcePaths =\r\n\t\t\t\tpositionals.length === 0 ? [] : Array.isArray(rawSources) ? rawSources.map((value) => String(value)) : positionals.map((value) => String(value));\r\n\t\t\tconst service = await createCloudflareMemoryService({\r\n\t\t\t\tpluginConfig: params.pluginConfig,\r\n\t\t\t\topenClawConfig: params.openClawConfig,\r\n\t\t\t\tenv: process.env,\r\n\t\t\t\tresolvePath: params.resolvePath,\r\n\t\t\t});\r\n\t\t\tconst summary = await runCloudflareMemoryMigration({\r\n\t\t\t\tservice,\r\n\t\t\t\toptions: {\r\n\t\t\t\t\tsourcePaths,\r\n\t\t\t\t\tworkspaceDir: options.workspace as string | undefined,\r\n\t\t\t\t\tnamespace: options.namespace as string | undefined,\r\n\t\t\t\t\tnamespaceStrategy: options.deriveNamespaceFromPath ? \"path\" : \"single-target\",\r\n\t\t\t\t\tduplicateStrategy: parseDuplicateStrategy(options.ifExists),\r\n\t\t\t\t\tdryRun: Boolean(options.dryRun),\r\n\t\t\t\t\tcreateIndexIfMissing: Boolean(options.createIndex),\r\n\t\t\t\t},\r\n\t\t\t});\r\n\t\t\tif (options.json) {\r\n\t\t\t\tprintJson(summary);\r\n\t\t\t} else {\r\n\t\t\t\tconsole.log(formatMigrationSummary(summary));\r\n\t\t\t}\r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} from \"openclaw/plugin-sdk/config-runtime\";\nimport type { DoctorReport, IndexInitializationReport, MetadataFilter, MigrationDuplicateStrategy, SmokeTestReport } from \"./types.js\";\nimport { formatMigrationSummary, runCloudflareMemoryMigration } from \"./migration.js\";\nimport { createCloudflareMemoryService } from \"./service-factory.js\";\n\ntype CliCommand = {\n\tcommand: (name: string) => CliCommand;\n\tdescription: (description: string) => CliCommand;\n\targument: (name: string, description: string) => CliCommand;\n\toption: (flags: string, description: string) => CliCommand;\n\taction: (handler: (...args: unknown[]) => Promise<void> | void) => CliCommand;\n\topts?: () => Record<string, unknown>;\n};\n\nfunction printJson(value: unknown): void {\n\tconsole.log(JSON.stringify(value, null, 2));\n}\n\nfunction printCheckReport(report: DoctorReport | IndexInitializationReport | SmokeTestReport): void {\n\tfor (const check of report.checks) {\n\t\tconsole.log(`[${check.status}] ${check.name}: ${check.message}`);\n\t}\n}\n\nfunction parseMetadataFlag(value: string | undefined): Record<string, string | number | boolean> | undefined {\n\tif (!value) {\n\t\treturn undefined;\n\t}\n\tconst parsed = JSON.parse(value) as unknown;\n\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n\t\tthrow new Error(\"--metadata must be a JSON object.\");\n\t}\n\treturn parsed as Record<string, string | number | boolean>;\n}\n\nfunction parseFilterFlag(value: string | undefined): MetadataFilter | undefined {\n\tif (!value) {\n\t\treturn undefined;\n\t}\n\tconst parsed = JSON.parse(value) as unknown;\n\tif (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n\t\tthrow new Error(\"--filter must be a JSON object.\");\n\t}\n\treturn parsed as MetadataFilter;\n}\n\nfunction isCliCommand(value: unknown): value is CliCommand {\n\treturn Boolean(value) && typeof value === \"object\" && typeof (value as CliCommand).opts === \"function\";\n}\n\nfunction resolveInvocation(args: unknown[]): { positionals: unknown[]; options: Record<string, unknown> } {\n\tconst maybeCommand = args.at(-1);\n\tif (!isCliCommand(maybeCommand)) {\n\t\treturn {\n\t\t\tpositionals: args,\n\t\t\toptions: {},\n\t\t};\n\t}\n\treturn {\n\t\tpositionals: args.slice(0, -1),\n\t\toptions: maybeCommand.opts?.() ?? {},\n\t};\n}\n\nfunction parseDuplicateStrategy(value: unknown): MigrationDuplicateStrategy | undefined {\n\tif (value === undefined) {\n\t\treturn undefined;\n\t}\n\tif (value === \"overwrite\" || value === \"skip\" || value === \"fail\") {\n\t\treturn value;\n\t}\n\tthrow new Error(\"--if-exists must be overwrite, skip, or fail.\");\n}\n\nexport function registerCloudflareMemoryCli(\n\tprogram: {\n\t\tcommand: (name: string) => CliCommand;\n\t},\n\tparams: {\n\t\tpluginConfig: unknown;\n\t\topenClawConfig: OpenClawConfig;\n\t\tresolvePath?: (input: string) => string;\n\t},\n): void {\n\tconst root = program.command(\"cf-memory\").description(\"Manage Cloudflare memory records.\");\n\n\tfunction resolveOptions(args: unknown[]): Record<string, unknown> {\n\t\treturn resolveInvocation(args).options;\n\t}\n\n\troot\n\t\t.command(\"doctor\")\n\t\t.description(\"Validate Workers AI and Vectorize configuration.\")\n\t\t.option(\"--create-index\", \"Create the Vectorize index if missing.\")\n\t\t.option(\"--json\", \"Print structured JSON output.\")\n\t\t.action(async (...args) => {\n\t\t\tconst options = resolveOptions(args);\n\t\t\tconst service = await createCloudflareMemoryService({\n\t\t\t\tpluginConfig: params.pluginConfig,\n\t\t\t\topenClawConfig: params.openClawConfig,\n\t\t\t\tenv: process.env,\n\t\t\t\tresolvePath: params.resolvePath,\n\t\t\t});\n\t\t\tconst report = await service.doctor({\n\t\t\t\tcreateIndexIfMissing: Boolean(options.createIndex),\n\t\t\t});\n\t\t\tif (options.json) {\n\t\t\t\tprintJson(report);\n\t\t\t} else {\n\t\t\t\tprintCheckReport(report);\n\t\t\t}\n\t\t\tif (!report.ok) {\n\t\t\t\tprocess.exitCode = 1;\n\t\t\t}\n\t\t});\n\n\troot\n\t\t.command(\"init\")\n\t\t.description(\"Initialize the Cloudflare Vectorize index for the configured embedding model.\")\n\t\t.option(\"--json\", \"Print structured JSON output.\")\n\t\t.action(async (...args) => {\n\t\t\tconst options = resolveOptions(args);\n\t\t\tconst service = await createCloudflareMemoryService({\n\t\t\t\tpluginConfig: params.pluginConfig,\n\t\t\t\topenClawConfig: params.openClawConfig,\n\t\t\t\tenv: process.env,\n\t\t\t\tresolvePath: params.resolvePath,\n\t\t\t});\n\t\t\tconst report = await service.initializeIndex();\n\t\t\tif (options.json) {\n\t\t\t\tprintJson(report);\n\t\t\t} else {\n\t\t\t\tprintCheckReport(report);\n\t\t\t}\n\t\t\tif (!report.ok) {\n\t\t\t\tprocess.exitCode = 1;\n\t\t\t}\n\t\t});\n\n\troot\n\t\t.command(\"test\")\n\t\t.description(\"Run an end-to-end embedding and semantic-search smoke test.\")\n\t\t.option(\"--json\", \"Print structured JSON output.\")\n\t\t.action(async (...args) => {\n\t\t\tconst options = resolveOptions(args);\n\t\t\tconst service = await createCloudflareMemoryService({\n\t\t\t\tpluginConfig: params.pluginConfig,\n\t\t\t\topenClawConfig: params.openClawConfig,\n\t\t\t\tenv: process.env,\n\t\t\t\tresolvePath: params.resolvePath,\n\t\t\t});\n\t\t\tconst report = await service.runSmokeTest();\n\t\t\tif (options.json) {\n\t\t\t\tprintJson(report);\n\t\t\t} else {\n\t\t\t\tprintCheckReport(report);\n\t\t\t}\n\t\t\tif (!report.ok) {\n\t\t\t\tprocess.exitCode = 1;\n\t\t\t}\n\t\t});\n\n\troot\n\t\t.command(\"search\")\n\t\t.description(\"Search stored Cloudflare memory.\")\n\t\t.argument(\"<query>\", \"Semantic search query.\")\n\t\t.option(\"--namespace <namespace>\", \"Optional namespace override.\")\n\t\t.option(\"--limit <count>\", \"Maximum number of results.\")\n\t\t.option(\"--filter <json>\", \"Optional metadata filter JSON.\")\n\t\t.action(async (query, opts) => {\n\t\t\tconst options = opts as Record<string, unknown>;\n\t\t\tconst service = await createCloudflareMemoryService({\n\t\t\t\tpluginConfig: params.pluginConfig,\n\t\t\t\topenClawConfig: params.openClawConfig,\n\t\t\t\tenv: process.env,\n\t\t\t\tresolvePath: params.resolvePath,\n\t\t\t});\n\t\t\tconst results = await service.search({\n\t\t\t\tquery: String(query),\n\t\t\t\tnamespace: options.namespace as string | undefined,\n\t\t\t\tmaxResults: options.limit ? Number(options.limit) : undefined,\n\t\t\t\tfilter: parseFilterFlag(options.filter as string | undefined),\n\t\t\t});\n\t\t\tprintJson(results);\n\t\t});\n\n\troot\n\t\t.command(\"upsert\")\n\t\t.description(\"Insert or update a memory record.\")\n\t\t.argument(\"<text>\", \"Memory text.\")\n\t\t.option(\"--id <id>\", \"Stable logical id.\")\n\t\t.option(\"--title <title>\", \"Optional title.\")\n\t\t.option(\"--namespace <namespace>\", \"Optional namespace override.\")\n\t\t.option(\"--source <source>\", \"Optional source label.\")\n\t\t.option(\"--metadata <json>\", \"Optional metadata JSON object.\")\n\t\t.action(async (text, opts) => {\n\t\t\tconst options = opts as Record<string, unknown>;\n\t\t\tconst service = await createCloudflareMemoryService({\n\t\t\t\tpluginConfig: params.pluginConfig,\n\t\t\t\topenClawConfig: params.openClawConfig,\n\t\t\t\tenv: process.env,\n\t\t\t\tresolvePath: params.resolvePath,\n\t\t\t});\n\t\t\tconst result = await service.upsert({\n\t\t\t\tinput: {\n\t\t\t\t\tid: options.id as string | undefined,\n\t\t\t\t\ttitle: options.title as string | undefined,\n\t\t\t\t\ttext: String(text),\n\t\t\t\t\tnamespace: options.namespace as string | undefined,\n\t\t\t\t\tsource: options.source as string | undefined,\n\t\t\t\t\tmetadata: parseMetadataFlag(options.metadata as string | undefined),\n\t\t\t\t},\n\t\t\t});\n\t\t\tprintJson(result);\n\t\t});\n\n\troot\n\t\t.command(\"delete\")\n\t\t.description(\"Delete a memory record.\")\n\t\t.argument(\"<id>\", \"Logical memory record id.\")\n\t\t.option(\"--namespace <namespace>\", \"Optional namespace override.\")\n\t\t.action(async (id, opts) => {\n\t\t\tconst options = opts as Record<string, unknown>;\n\t\t\tconst service = await createCloudflareMemoryService({\n\t\t\t\tpluginConfig: params.pluginConfig,\n\t\t\t\topenClawConfig: params.openClawConfig,\n\t\t\t\tenv: process.env,\n\t\t\t\tresolvePath: params.resolvePath,\n\t\t\t});\n\t\t\tconst mutationId = await service.delete({\n\t\t\t\tid: String(id),\n\t\t\t\tnamespace: options.namespace as string | undefined,\n\t\t\t});\n\t\t\tprintJson({ id, mutationId });\n\t\t});\n\n\troot\n\t\t.command(\"migrate\")\n\t\t.description(\"Migrate legacy markdown memory into Cloudflare Vectorize.\")\n\t\t.argument(\"[sources...]\", \"Markdown files, directories, or glob patterns. Defaults to the current OpenClaw memory corpus when omitted.\")\n\t\t.option(\"--workspace <path>\", \"Workspace root used for default-provider discovery and relative path normalization.\")\n\t\t.option(\"--namespace <namespace>\", \"Target namespace override.\")\n\t\t.option(\"--derive-namespace-from-path\", \"Derive namespaces from the first relative path segment instead of using a single target namespace.\")\n\t\t.option(\"--if-exists <strategy>\", \"Duplicate handling: overwrite, skip, or fail.\")\n\t\t.option(\"--create-index\", \"Create the Vectorize index if missing.\")\n\t\t.option(\"--dry-run\", \"Plan the migration without writing records.\")\n\t\t.option(\"--json\", \"Print structured JSON output.\")\n\t\t.action(async (...args) => {\n\t\t\tconst { positionals, options } = resolveInvocation(args);\n\t\t\tconst rawSources = positionals[0];\n\t\t\tconst sourcePaths =\n\t\t\t\tpositionals.length === 0 ? [] : Array.isArray(rawSources) ? rawSources.map((value) => String(value)) : positionals.map((value) => String(value));\n\t\t\tconst service = await createCloudflareMemoryService({\n\t\t\t\tpluginConfig: params.pluginConfig,\n\t\t\t\topenClawConfig: params.openClawConfig,\n\t\t\t\tenv: process.env,\n\t\t\t\tresolvePath: params.resolvePath,\n\t\t\t});\n\t\t\tconst summary = await runCloudflareMemoryMigration({\n\t\t\t\tservice,\n\t\t\t\toptions: {\n\t\t\t\t\tsourcePaths,\n\t\t\t\t\tworkspaceDir: options.workspace as string | undefined,\n\t\t\t\t\tnamespace: options.namespace as string | undefined,\n\t\t\t\t\tnamespaceStrategy: options.deriveNamespaceFromPath ? \"path\" : \"single-target\",\n\t\t\t\t\tduplicateStrategy: parseDuplicateStrategy(options.ifExists),\n\t\t\t\t\tdryRun: Boolean(options.dryRun),\n\t\t\t\t\tcreateIndexIfMissing: Boolean(options.createIndex),\n\t\t\t\t},\n\t\t\t});\n\t\t\tif (options.json) {\n\t\t\t\tprintJson(summary);\n\t\t\t} else {\n\t\t\t\tconsole.log(formatMigrationSummary(summary));\n\t\t\t}\n\t\t\tif (summary.failed > 0) {\n\t\t\t\tprocess.exitCode = 1;\n\t\t\t}\n\t\t});\n}\n"],"mappings":";;;AAcA,SAAS,EAAU,GAAsB;AACxC,SAAQ,IAAI,KAAK,UAAU,GAAO,MAAM,EAAE,CAAC;;AAG5C,SAAS,EAAiB,GAA0E;AACnG,MAAK,IAAM,KAAS,EAAO,OAC1B,SAAQ,IAAI,IAAI,EAAM,OAAO,IAAI,EAAM,KAAK,IAAI,EAAM,UAAU;;AAIlE,SAAS,EAAkB,GAAkF;AAC5G,KAAI,CAAC,EACJ;CAED,IAAM,IAAS,KAAK,MAAM,EAAM;AAChC,KAAI,CAAC,KAAU,OAAO,KAAW,YAAY,MAAM,QAAQ,EAAO,CACjE,OAAU,MAAM,oCAAoC;AAErD,QAAO;;AAGR,SAAS,EAAgB,GAAuD;AAC/E,KAAI,CAAC,EACJ;CAED,IAAM,IAAS,KAAK,MAAM,EAAM;AAChC,KAAI,CAAC,KAAU,OAAO,KAAW,YAAY,MAAM,QAAQ,EAAO,CACjE,OAAU,MAAM,kCAAkC;AAEnD,QAAO;;AAGR,SAAS,EAAa,GAAqC;AAC1D,QAAO,EAAQ,KAAU,OAAO,KAAU,YAAY,OAAQ,EAAqB,QAAS;;AAG7F,SAAS,EAAkB,GAA+E;CACzG,IAAM,IAAe,EAAK,GAAG,GAAG;AAOhC,QANK,EAAa,EAAa,GAMxB;EACN,aAAa,EAAK,MAAM,GAAG,GAAG;EAC9B,SAAS,EAAa,QAAQ,IAAI,EAAE;EACpC,G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D,QAAQ,EAAQ,EAAQ;IACxB,sBAAsB,EAAQ,EAAQ;IACtC;GACD,CAAC;AAMF,EALI,EAAQ,OACX,EAAU,EAAQ,GAElB,QAAQ,IAAI,EAAuB,EAAQ,CAAC,EAEzC,EAAQ,SAAS,MACpB,QAAQ,WAAW;GAEnB"}
|
package/dist/doctor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"doctor.js","names":[],"sources":["../src/doctor.ts"],"sourcesContent":["import type { CloudflareMemoryService } from \"./service.js\";\
|
|
1
|
+
{"version":3,"file":"doctor.js","names":[],"sources":["../src/doctor.ts"],"sourcesContent":["import type { CloudflareMemoryService } from \"./service.js\";\nimport type { DoctorCheck, DoctorReport } from \"./types.js\";\n\nexport async function runDoctor(params: { service: CloudflareMemoryService; createIndexIfMissing: boolean }): Promise<DoctorReport> {\n\tconst checks: DoctorCheck[] = [];\n\n\tchecks.push({\n\t\tname: \"credentials\",\n\t\tstatus: \"pass\",\n\t\tmessage: `Using Cloudflare account ${params.service.config.accountId} and Vectorize index ${params.service.config.indexName}.`,\n\t});\n\n\tconst embedding = await params.service.inspectEmbeddingDimensions();\n\tchecks.push({\n\t\tname: \"workers-ai-embeddings\",\n\t\tstatus: \"pass\",\n\t\tmessage: `Workers AI model ${params.service.config.model} returned ${embedding.embeddingDimensions} dimensions.`,\n\t});\n\tif (embedding.configuredDimensions !== undefined) {\n\t\tchecks.push({\n\t\t\tname: \"create-index-dimensions\",\n\t\t\tstatus: embedding.configuredDimensionsMatchModel ? \"pass\" : \"warn\",\n\t\t\tmessage: embedding.configuredDimensionsMatchModel\n\t\t\t\t? `Configured createIndex.dimensions matches the embedding model (${embedding.embeddingDimensions}).`\n\t\t\t\t: `Configured createIndex.dimensions (${embedding.configuredDimensions}) does not match the embedding model (${embedding.embeddingDimensions}). Index creation uses the live embedding dimensions.`,\n\t\t});\n\t}\n\n\tlet indexResult:\n\t\t| {\n\t\t\t\tcreated: boolean;\n\t\t\t\tdimensions: number;\n\t\t }\n\t\t| undefined;\n\tif (params.createIndexIfMissing) {\n\t\tindexResult = await params.service.ensureIndexExists(true, embedding.targetDimensions);\n\t} else {\n\t\tconst existingIndex = await params.service.describeIndexIfExists();\n\t\tif (existingIndex) {\n\t\t\tindexResult = {\n\t\t\t\tcreated: false,\n\t\t\t\tdimensions: existingIndex.config.dimensions,\n\t\t\t};\n\t\t}\n\t}\n\n\tif (!indexResult) {\n\t\tchecks.push({\n\t\t\tname: \"vectorize-index\",\n\t\t\tstatus: \"fail\",\n\t\t\tmessage: `Vectorize index \"${params.service.config.indexName}\" was not found. Run \"openclaw cf-memory init\" or rerun doctor with --create-index.`,\n\t\t});\n\t\tchecks.push({\n\t\t\tname: \"dimension-match\",\n\t\t\tstatus: \"warn\",\n\t\t\tmessage: \"Skipped dimension comparison because the Vectorize index does not exist yet.\",\n\t\t});\n\t} else {\n\t\tchecks.push({\n\t\t\tname: \"vectorize-index\",\n\t\t\tstatus: \"pass\",\n\t\t\tmessage: indexResult.created\n\t\t\t\t? `Created Vectorize index \"${params.service.config.indexName}\" with ${indexResult.dimensions} dimensions.`\n\t\t\t\t: `Vectorize index \"${params.service.config.indexName}\" is reachable.`,\n\t\t});\n\t\tif (embedding.embeddingDimensions !== indexResult.dimensions) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"dimension-match\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: `Embedding dimensions (${embedding.embeddingDimensions}) do not match the Vectorize index dimensions (${indexResult.dimensions}).`,\n\t\t\t});\n\t\t} else {\n\t\t\tchecks.push({\n\t\t\t\tname: \"dimension-match\",\n\t\t\t\tstatus: \"pass\",\n\t\t\t\tmessage: \"Embedding dimensions match the Vectorize index.\",\n\t\t\t});\n\t\t}\n\t}\n\n\tchecks.push({\n\t\tname: \"metadata-filters\",\n\t\tstatus: params.service.config.metadataIndexedFields.length > 0 ? \"pass\" : \"warn\",\n\t\tmessage:\n\t\t\tparams.service.config.metadataIndexedFields.length > 0\n\t\t\t\t? `Configured metadata-index guidance for: ${params.service.config.metadataIndexedFields.join(\", \")}.`\n\t\t\t\t: \"No metadataIndexedFields configured. Add metadata indexes in Cloudflare before relying on filter-heavy queries.\",\n\t});\n\n\tconst ok = checks.every((check) => check.status !== \"fail\");\n\treturn { ok, checks };\n}\n"],"mappings":";AAGA,eAAsB,EAAU,GAAoG;CACnI,IAAM,IAAwB,EAAE;AAEhC,GAAO,KAAK;EACX,MAAM;EACN,QAAQ;EACR,SAAS,4BAA4B,EAAO,QAAQ,OAAO,UAAU,uBAAuB,EAAO,QAAQ,OAAO,UAAU;EAC5H,CAAC;CAEF,IAAM,IAAY,MAAM,EAAO,QAAQ,4BAA4B;AAMnE,CALA,EAAO,KAAK;EACX,MAAM;EACN,QAAQ;EACR,SAAS,oBAAoB,EAAO,QAAQ,OAAO,MAAM,YAAY,EAAU,oBAAoB;EACnG,CAAC,EACE,EAAU,yBAAyB,KAAA,KACtC,EAAO,KAAK;EACX,MAAM;EACN,QAAQ,EAAU,iCAAiC,SAAS;EAC5D,SAAS,EAAU,iCAChB,kEAAkE,EAAU,oBAAoB,MAChG,sCAAsC,EAAU,qBAAqB,wCAAwC,EAAU,oBAAoB;EAC9I,CAAC;CAGH,IAAI;AAMJ,KAAI,EAAO,qBACV,KAAc,MAAM,EAAO,QAAQ,kBAAkB,IAAM,EAAU,iBAAiB;MAChF;EACN,IAAM,IAAgB,MAAM,EAAO,QAAQ,uBAAuB;AAClE,EAAI,MACH,IAAc;GACb,SAAS;GACT,YAAY,EAAc,OAAO;GACjC;;AAgDH,QA5CK,KAYJ,EAAO,KAAK;EACX,MAAM;EACN,QAAQ;EACR,SAAS,EAAY,UAClB,4BAA4B,EAAO,QAAQ,OAAO,UAAU,SAAS,EAAY,WAAW,gBAC5F,oBAAoB,EAAO,QAAQ,OAAO,UAAU;EACvD,CAAC,EACE,EAAU,wBAAwB,EAAY,aAOjD,EAAO,KAAK;EACX,MAAM;EACN,QAAQ;EACR,SAAS;EACT,CAAC,GAVF,EAAO,KAAK;EACX,MAAM;EACN,QAAQ;EACR,SAAS,yBAAyB,EAAU,oBAAoB,iDAAiD,EAAY,WAAW;EACxI,CAAC,KAvBH,EAAO,KAAK;EACX,MAAM;EACN,QAAQ;EACR,SAAS,oBAAoB,EAAO,QAAQ,OAAO,UAAU;EAC7D,CAAC,EACF,EAAO,KAAK;EACX,MAAM;EACN,QAAQ;EACR,SAAS;EACT,CAAC,GAwBH,EAAO,KAAK;EACX,MAAM;EACN,QAAQ,EAAO,QAAQ,OAAO,sBAAsB,SAAS,IAAI,SAAS;EAC1E,SACC,EAAO,QAAQ,OAAO,sBAAsB,SAAS,IAClD,2CAA2C,EAAO,QAAQ,OAAO,sBAAsB,KAAK,KAAK,CAAC,KAClG;EACJ,CAAC,EAGK;EAAE,IADE,EAAO,OAAO,MAAU,EAAM,WAAW,OAAO;EAC9C;EAAQ"}
|
package/dist/service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","names":[],"sources":["../src/service.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\r\nimport type { OpenClawConfig } from \"openclaw/plugin-sdk/config-runtime\";\r\nimport { isCloudflareNotFoundError } from \"./cloudflare-api.js\";\r\nimport { CompanionStore } from \"./companion-store.js\";\r\nimport { runDoctor } from \"./doctor.js\";\r\nimport { WorkersAiEmbeddingsClient } from \"./embeddings-client.js\";\r\nimport { sanitizeNamespace, resolveDefaultNamespace } from \"./namespace.js\";\r\nimport { hydrateInlineRecord, mapRecordForUpsert } from \"./record-mapper.js\";\r\nimport type {\r\n\tCompanionRecord,\r\n\tDoctorCheck,\r\n\tDoctorReport,\r\n\tEmbeddingDimensionsInspection,\r\n\tHydratedMemoryRecord,\r\n\tIndexInitializationReport,\r\n\tMemoryRecordInput,\r\n\tMetadataFilter,\r\n\tResolvedPluginConfig,\r\n\tSmokeTestReport,\r\n\tUpsertedMemoryRecord,\r\n\tVectorizeIndexDescription,\r\n} from \"./types.js\";\r\nimport { VectorizeClient } from \"./vectorize-client.js\";\r\n\r\nconst SMOKE_TEST_TIMEOUT_MS = 12_000;\r\nconst SMOKE_TEST_POLL_INTERVAL_MS = 1_000;\r\n\r\nexport class CloudflareMemoryService {\r\n\treadonly embeddings: WorkersAiEmbeddingsClient;\r\n\treadonly vectorize: VectorizeClient;\r\n\treadonly companionStore: CompanionStore;\r\n\r\n\tconstructor(\r\n\t\treadonly config: ResolvedPluginConfig,\r\n\t\treadonly openClawConfig: OpenClawConfig,\r\n\t) {\r\n\t\tthis.embeddings = new WorkersAiEmbeddingsClient(config);\r\n\t\tthis.vectorize = new VectorizeClient(config);\r\n\t\tthis.companionStore = new CompanionStore(config.companionStorePath);\r\n\t}\r\n\r\n\tresolveNamespace(params: { namespace?: string; sessionKey?: string; agentId?: string; workspaceDir?: string }): string {\r\n\t\treturn resolveDefaultNamespace({\r\n\t\t\tfixedNamespace: params.namespace ?? this.config.fixedNamespace,\r\n\t\t\tsessionKey: params.sessionKey,\r\n\t\t\tagentId: params.agentId,\r\n\t\t\tworkspaceDir: params.workspaceDir,\r\n\t\t});\r\n\t}\r\n\r\n\tasync search(params: {\r\n\t\tquery: string;\r\n\t\tnamespace?: string;\r\n\t\tmaxResults?: number;\r\n\t\tminScore?: number;\r\n\t\tfilter?: MetadataFilter;\r\n\t\tsessionKey?: string;\r\n\t\tagentId?: string;\r\n\t\tworkspaceDir?: string;\r\n\t}): Promise<Array<HydratedMemoryRecord & { score: number }>> {\r\n\t\tconst namespace = this.resolveNamespace(params);\r\n\t\tconst vector = await this.embeddings.embedQuery(params.query);\r\n\t\tconst matches = await this.vectorize.query({\r\n\t\t\tvector,\r\n\t\t\tnamespace,\r\n\t\t\ttopK: params.maxResults ?? this.config.topK,\r\n\t\t\tfilter: params.filter,\r\n\t\t});\r\n\r\n\t\tconst hydrated = await Promise.all(\r\n\t\t\tmatches.map(async (match) => {\r\n\t\t\t\tconst base = hydrateInlineRecord(match);\r\n\t\t\t\tconst text = base.text ?? (await this.companionStore.get(base.namespace, base.logicalId))?.text ?? \"\";\r\n\t\t\t\treturn {\r\n\t\t\t\t\t...base,\r\n\t\t\t\t\ttext,\r\n\t\t\t\t\tscore: match.score ?? 0,\r\n\t\t\t\t};\r\n\t\t\t}),\r\n\t\t);\r\n\r\n\t\treturn hydrated.filter((record) => record.score >= (params.minScore ?? this.config.minScore));\r\n\t}\r\n\r\n\tasync get(params: { id: string; namespace?: string; sessionKey?: string; agentId?: string; workspaceDir?: string }): Promise<HydratedMemoryRecord | null> {\r\n\t\tconst namespace = this.resolveNamespace(params);\r\n\t\tconst vectorId = `${namespace}::${params.id}`;\r\n\t\tconst [match] = await this.vectorize.getByIds([vectorId]);\r\n\t\tif (!match) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\tconst base = hydrateInlineRecord(match);\r\n\t\tconst companion = await this.companionStore.get(base.namespace, base.logicalId);\r\n\t\treturn {\r\n\t\t\t...base,\r\n\t\t\ttext: base.text ?? companion?.text ?? \"\",\r\n\t\t};\r\n\t}\r\n\r\n\tasync upsert(params: { input: MemoryRecordInput; sessionKey?: string; agentId?: string; workspaceDir?: string }): Promise<UpsertedMemoryRecord> {\r\n\t\tconst namespace = this.resolveNamespace({\r\n\t\t\tnamespace: params.input.namespace,\r\n\t\t\tsessionKey: params.sessionKey,\r\n\t\t\tagentId: params.agentId,\r\n\t\t\tworkspaceDir: params.workspaceDir,\r\n\t\t});\r\n\t\tconst embedding = await this.embeddings.embedQuery(params.input.text);\r\n\t\tconst mapped = mapRecordForUpsert({\r\n\t\t\tinput: params.input,\r\n\t\t\tnamespace,\r\n\t\t\tembedding,\r\n\t\t\tconfig: this.config,\r\n\t\t});\r\n\r\n\t\tif (mapped.companionRecord) {\r\n\t\t\tawait this.companionStore.upsert(mapped.companionRecord);\r\n\t\t}\r\n\r\n\t\tconst mutationId = await this.vectorize.upsert([mapped.vector]);\r\n\t\tconst hydrated = await this.get({\r\n\t\t\tid: mapped.logicalId,\r\n\t\t\tnamespace,\r\n\t\t});\r\n\r\n\t\treturn {\r\n\t\t\t...(hydrated ?? this.fromCompanionFallback(mapped.companionRecord, mapped.logicalId, namespace, mapped.path)),\r\n\t\t\tmutationId,\r\n\t\t};\r\n\t}\r\n\r\n\tasync delete(params: { id: string; namespace?: string; sessionKey?: string; agentId?: string; workspaceDir?: string }): Promise<string | undefined> {\r\n\t\tconst namespace = this.resolveNamespace(params);\r\n\t\tawait this.companionStore.delete(namespace, params.id);\r\n\t\treturn this.vectorize.deleteByIds([`${namespace}::${params.id}`]);\r\n\t}\r\n\r\n\tasync doctor(options: { createIndexIfMissing?: boolean }): Promise<DoctorReport> {\r\n\t\treturn runDoctor({\r\n\t\t\tservice: this,\r\n\t\t\tcreateIndexIfMissing: options.createIndexIfMissing ?? false,\r\n\t\t});\r\n\t}\r\n\r\n\tasync inspectEmbeddingDimensions(): Promise<EmbeddingDimensionsInspection> {\r\n\t\tconst embeddingDimensions = await this.embeddings.probeDimensions();\r\n\t\tconst configuredDimensions = this.config.createIndex.dimensions;\r\n\t\treturn {\r\n\t\t\tembeddingDimensions,\r\n\t\t\tconfiguredDimensions,\r\n\t\t\tconfiguredDimensionsMatchModel: configuredDimensions === undefined || configuredDimensions === embeddingDimensions,\r\n\t\t\ttargetDimensions: embeddingDimensions,\r\n\t\t};\r\n\t}\r\n\r\n\tasync describeIndexIfExists(): Promise<VectorizeIndexDescription | null> {\r\n\t\ttry {\r\n\t\t\treturn await this.vectorize.describeIndex();\r\n\t\t} catch (error) {\r\n\t\t\tif (isCloudflareNotFoundError(error)) {\r\n\t\t\t\treturn null;\r\n\t\t\t}\r\n\t\t\tthrow error;\r\n\t\t}\r\n\t}\r\n\r\n\tasync initializeIndex(options?: { recreateIfDimensionMismatch?: boolean }): Promise<IndexInitializationReport> {\r\n\t\tconst recreateIfDimensionMismatch = options?.recreateIfDimensionMismatch ?? true;\r\n\t\tconst checks: DoctorCheck[] = [];\r\n\t\tchecks.push({\r\n\t\t\tname: \"credentials\",\r\n\t\t\tstatus: \"pass\",\r\n\t\t\tmessage: `Using Cloudflare account ${this.config.accountId} and Vectorize index ${this.config.indexName}.`,\r\n\t\t});\r\n\r\n\t\tconst embedding = await this.inspectEmbeddingDimensions();\r\n\t\tchecks.push({\r\n\t\t\tname: \"workers-ai-embeddings\",\r\n\t\t\tstatus: \"pass\",\r\n\t\t\tmessage: `Workers AI model ${this.config.model} returned ${embedding.embeddingDimensions} dimensions.`,\r\n\t\t});\r\n\t\tif (embedding.configuredDimensions !== undefined) {\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"create-index-dimensions\",\r\n\t\t\t\tstatus: embedding.configuredDimensionsMatchModel ? \"pass\" : \"warn\",\r\n\t\t\t\tmessage: embedding.configuredDimensionsMatchModel\r\n\t\t\t\t\t? `Configured createIndex.dimensions matches the embedding model (${embedding.embeddingDimensions}).`\r\n\t\t\t\t\t: `Configured createIndex.dimensions (${embedding.configuredDimensions}) does not match the embedding model (${embedding.embeddingDimensions}). Using the live embedding dimensions for initialization.`,\r\n\t\t\t});\r\n\t\t}\r\n\r\n\t\tconst existingIndex = await this.describeIndexIfExists();\r\n\t\tlet created = false;\r\n\t\tlet recreated = false;\r\n\t\tlet indexDimensions = existingIndex?.config.dimensions;\r\n\r\n\t\tif (!existingIndex) {\r\n\t\t\tconst createdIndex = await this.vectorize.createIndex(embedding.targetDimensions);\r\n\t\t\tcreated = true;\r\n\t\t\tindexDimensions = createdIndex.config.dimensions;\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"vectorize-index\",\r\n\t\t\t\tstatus: \"pass\",\r\n\t\t\t\tmessage: `Created Vectorize index \"${this.config.indexName}\" with ${indexDimensions} dimensions.`,\r\n\t\t\t});\r\n\t\t} else if (existingIndex.config.dimensions === embedding.targetDimensions) {\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"vectorize-index\",\r\n\t\t\t\tstatus: \"pass\",\r\n\t\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" already uses ${existingIndex.config.dimensions} dimensions.`,\r\n\t\t\t});\r\n\t\t} else if (!recreateIfDimensionMismatch) {\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"vectorize-index\",\r\n\t\t\t\tstatus: \"fail\",\r\n\t\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" uses ${existingIndex.config.dimensions} dimensions, but the embedding model requires ${embedding.targetDimensions}. Recreate the index or rerun init with recreation enabled.`,\r\n\t\t\t});\r\n\t\t} else {\r\n\t\t\tawait this.vectorize.deleteIndex();\r\n\t\t\tconst recreatedIndex = await this.vectorize.createIndex(embedding.targetDimensions);\r\n\t\t\trecreated = true;\r\n\t\t\tindexDimensions = recreatedIndex.config.dimensions;\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"vectorize-index\",\r\n\t\t\t\tstatus: \"pass\",\r\n\t\t\t\tmessage: `Recreated Vectorize index \"${this.config.indexName}\" from ${existingIndex.config.dimensions} to ${indexDimensions} dimensions.`,\r\n\t\t\t});\r\n\t\t}\r\n\r\n\t\tchecks.push({\r\n\t\t\tname: \"dimension-match\",\r\n\t\t\tstatus: indexDimensions === embedding.embeddingDimensions ? \"pass\" : \"fail\",\r\n\t\t\tmessage:\r\n\t\t\t\tindexDimensions === embedding.embeddingDimensions\r\n\t\t\t\t\t? \"Embedding dimensions match the Vectorize index.\"\r\n\t\t\t\t\t: `Embedding dimensions (${embedding.embeddingDimensions}) do not match the Vectorize index dimensions (${indexDimensions ?? \"unknown\"}).`,\r\n\t\t});\r\n\t\tchecks.push({\r\n\t\t\tname: \"metadata-filters\",\r\n\t\t\tstatus: this.config.metadataIndexedFields.length > 0 ? \"pass\" : \"warn\",\r\n\t\t\tmessage:\r\n\t\t\t\tthis.config.metadataIndexedFields.length > 0\r\n\t\t\t\t\t? `Configured metadata-index guidance for: ${this.config.metadataIndexedFields.join(\", \")}.`\r\n\t\t\t\t\t: \"No metadataIndexedFields configured. Add metadata indexes in Cloudflare before relying on filter-heavy queries.\",\r\n\t\t});\r\n\r\n\t\treturn {\r\n\t\t\tok: checks.every((check) => check.status !== \"fail\"),\r\n\t\t\tchecks,\r\n\t\t\tcreated,\r\n\t\t\trecreated,\r\n\t\t\tembeddingDimensions: embedding.embeddingDimensions,\r\n\t\t\tindexDimensions,\r\n\t\t};\r\n\t}\r\n\r\n\tasync runSmokeTest(options?: { timeoutMs?: number; pollIntervalMs?: number }): Promise<SmokeTestReport> {\r\n\t\tconst checks: DoctorCheck[] = [];\r\n\t\tchecks.push({\r\n\t\t\tname: \"credentials\",\r\n\t\t\tstatus: \"pass\",\r\n\t\t\tmessage: `Using Cloudflare account ${this.config.accountId} and Vectorize index ${this.config.indexName}.`,\r\n\t\t});\r\n\r\n\t\tconst embedding = await this.inspectEmbeddingDimensions();\r\n\t\tchecks.push({\r\n\t\t\tname: \"workers-ai-embeddings\",\r\n\t\t\tstatus: \"pass\",\r\n\t\t\tmessage: `Workers AI model ${this.config.model} returned ${embedding.embeddingDimensions} dimensions.`,\r\n\t\t});\r\n\t\tif (embedding.configuredDimensions !== undefined) {\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"create-index-dimensions\",\r\n\t\t\t\tstatus: embedding.configuredDimensionsMatchModel ? \"pass\" : \"warn\",\r\n\t\t\t\tmessage: embedding.configuredDimensionsMatchModel\r\n\t\t\t\t\t? `Configured createIndex.dimensions matches the embedding model (${embedding.embeddingDimensions}).`\r\n\t\t\t\t\t: `Configured createIndex.dimensions (${embedding.configuredDimensions}) does not match the embedding model (${embedding.embeddingDimensions}). Using the live embedding dimensions for validation.`,\r\n\t\t\t});\r\n\t\t}\r\n\r\n\t\tconst existingIndex = await this.describeIndexIfExists();\r\n\t\tif (!existingIndex) {\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"vectorize-index\",\r\n\t\t\t\tstatus: \"fail\",\r\n\t\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" was not found. Run \"openclaw cf-memory init\" before rerunning this test.`,\r\n\t\t\t});\r\n\t\t\treturn {\r\n\t\t\t\tok: false,\r\n\t\t\t\tchecks,\r\n\t\t\t\tnamespace: \"n/a\",\r\n\t\t\t\tlogicalId: \"n/a\",\r\n\t\t\t};\r\n\t\t}\r\n\r\n\t\tchecks.push({\r\n\t\t\tname: \"vectorize-index\",\r\n\t\t\tstatus: \"pass\",\r\n\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" is reachable.`,\r\n\t\t});\r\n\t\tif (existingIndex.config.dimensions !== embedding.embeddingDimensions) {\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"dimension-match\",\r\n\t\t\t\tstatus: \"fail\",\r\n\t\t\t\tmessage: `Embedding dimensions (${embedding.embeddingDimensions}) do not match the Vectorize index dimensions (${existingIndex.config.dimensions}). Run \"openclaw cf-memory init\" to repair the index.`,\r\n\t\t\t});\r\n\t\t\treturn {\r\n\t\t\t\tok: false,\r\n\t\t\t\tchecks,\r\n\t\t\t\tnamespace: \"n/a\",\r\n\t\t\t\tlogicalId: \"n/a\",\r\n\t\t\t};\r\n\t\t}\r\n\r\n\t\tchecks.push({\r\n\t\t\tname: \"dimension-match\",\r\n\t\t\tstatus: \"pass\",\r\n\t\t\tmessage: \"Embedding dimensions match the Vectorize index.\",\r\n\t\t});\r\n\r\n\t\tconst namespace = sanitizeNamespace(`cf-memory-test-${randomUUID()}`);\r\n\t\tconst logicalId = `cf-memory-test-${randomUUID()}`;\r\n\t\tconst probeText = `OpenClaw Cloudflare memory smoke test ${logicalId}`;\r\n\t\tlet probeUpserted = false;\r\n\r\n\t\ttry {\r\n\t\t\tawait this.upsert({\r\n\t\t\t\tinput: {\r\n\t\t\t\t\tid: logicalId,\r\n\t\t\t\t\tnamespace,\r\n\t\t\t\t\ttext: probeText,\r\n\t\t\t\t\tsource: \"cf-memory-test\",\r\n\t\t\t\t\tmetadata: {\r\n\t\t\t\t\t\tprobe: true,\r\n\t\t\t\t\t\tprobeId: logicalId,\r\n\t\t\t\t\t},\r\n\t\t\t\t},\r\n\t\t\t});\r\n\t\t\tprobeUpserted = true;\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"probe-upsert\",\r\n\t\t\t\tstatus: \"pass\",\r\n\t\t\t\tmessage: `Inserted smoke-test record ${logicalId} in namespace ${namespace}.`,\r\n\t\t\t});\r\n\r\n\t\t\tconst found = await this.waitForSearchHit({\r\n\t\t\t\tquery: probeText,\r\n\t\t\t\tnamespace,\r\n\t\t\t\tlogicalId,\r\n\t\t\t\ttimeoutMs: options?.timeoutMs ?? SMOKE_TEST_TIMEOUT_MS,\r\n\t\t\t\tpollIntervalMs: options?.pollIntervalMs ?? SMOKE_TEST_POLL_INTERVAL_MS,\r\n\t\t\t});\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: \"probe-search\",\r\n\t\t\t\tstatus: found ? \"pass\" : \"fail\",\r\n\t\t\t\tmessage: found\r\n\t\t\t\t\t? \"Semantic search returned the smoke-test record.\"\r\n\t\t\t\t\t: `Semantic search did not return the smoke-test record within ${(options?.timeoutMs ?? SMOKE_TEST_TIMEOUT_MS) / 1000} seconds.`,\r\n\t\t\t});\r\n\t\t} catch (error) {\r\n\t\t\tchecks.push({\r\n\t\t\t\tname: probeUpserted ? \"probe-search\" : \"probe-upsert\",\r\n\t\t\t\tstatus: \"fail\",\r\n\t\t\t\tmessage: error instanceof Error ? error.message : \"Smoke test failed with an unknown error.\",\r\n\t\t\t});\r\n\t\t} finally {\r\n\t\t\ttry {\r\n\t\t\t\tawait this.delete({ id: logicalId, namespace });\r\n\t\t\t\tchecks.push({\r\n\t\t\t\t\tname: \"probe-cleanup\",\r\n\t\t\t\t\tstatus: \"pass\",\r\n\t\t\t\t\tmessage: `Deleted smoke-test record ${logicalId}.`,\r\n\t\t\t\t});\r\n\t\t\t} catch (error) {\r\n\t\t\t\tchecks.push({\r\n\t\t\t\t\tname: \"probe-cleanup\",\r\n\t\t\t\t\tstatus: \"fail\",\r\n\t\t\t\t\tmessage: error instanceof Error ? error.message : `Failed to delete smoke-test record ${logicalId}.`,\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn {\r\n\t\t\tok: checks.every((check) => check.status !== \"fail\"),\r\n\t\t\tchecks,\r\n\t\t\tnamespace,\r\n\t\t\tlogicalId,\r\n\t\t};\r\n\t}\r\n\r\n\tasync ensureIndexExists(createIfMissing: boolean, targetDimensions?: number): Promise<{ created: boolean; dimensions: number }> {\r\n\t\ttry {\r\n\t\t\tconst description = await this.vectorize.describeIndex();\r\n\t\t\treturn {\r\n\t\t\t\tcreated: false,\r\n\t\t\t\tdimensions: description.config.dimensions,\r\n\t\t\t};\r\n\t\t} catch (error) {\r\n\t\t\tif (!createIfMissing || !isCloudflareNotFoundError(error)) {\r\n\t\t\t\tthrow error;\r\n\t\t\t}\r\n\t\t\tconst dimensions = targetDimensions ?? (await this.inspectEmbeddingDimensions()).targetDimensions;\r\n\t\t\tconst createdIndex = await this.vectorize.createIndex(dimensions);\r\n\t\t\treturn {\r\n\t\t\t\tcreated: true,\r\n\t\t\t\tdimensions: createdIndex.config.dimensions,\r\n\t\t\t};\r\n\t\t}\r\n\t}\r\n\r\n\tprivate fromCompanionFallback(companionRecord: CompanionRecord | undefined, logicalId: string, namespace: string, path: string): UpsertedMemoryRecord {\r\n\t\treturn {\r\n\t\t\tlogicalId,\r\n\t\t\tvectorId: `${namespace}::${logicalId}`,\r\n\t\t\tnamespace,\r\n\t\t\ttitle: companionRecord?.title,\r\n\t\t\ttext: companionRecord?.text ?? \"\",\r\n\t\t\tmetadata: companionRecord?.metadata ?? {},\r\n\t\t\tsource: companionRecord?.source,\r\n\t\t\tcreatedAt: companionRecord?.createdAt,\r\n\t\t\tupdatedAt: companionRecord?.updatedAt,\r\n\t\t\tpath,\r\n\t\t};\r\n\t}\r\n\r\n\tprivate async waitForSearchHit(params: {\r\n\t\tquery: string;\r\n\t\tnamespace: string;\r\n\t\tlogicalId: string;\r\n\t\ttimeoutMs: number;\r\n\t\tpollIntervalMs: number;\r\n\t}): Promise<boolean> {\r\n\t\tconst deadline = Date.now() + params.timeoutMs;\r\n\t\twhile (Date.now() <= deadline) {\r\n\t\t\tconst results = await this.search({\r\n\t\t\t\tquery: params.query,\r\n\t\t\t\tnamespace: params.namespace,\r\n\t\t\t\tmaxResults: 5,\r\n\t\t\t\tminScore: 0,\r\n\t\t\t});\r\n\t\t\tif (results.some((record) => record.logicalId === params.logicalId)) {\r\n\t\t\t\treturn true;\r\n\t\t\t}\r\n\t\t\tif (Date.now() + params.pollIntervalMs > deadline) {\r\n\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\tawait this.pause(params.pollIntervalMs);\r\n\t\t}\r\n\t\treturn false;\r\n\t}\r\n\r\n\tprivate async pause(ms: number): Promise<void> {\r\n\t\tawait new Promise((resolve) => setTimeout(resolve, ms));\r\n\t}\r\n}\r\n"],"mappings":";;;;;;;;;AAwBA,IAAM,IAAwB,MACxB,IAA8B,KAEvB,IAAb,MAAqC;CACpC;CACA;CACA;CAEA,YACC,GACA,GACC;AAGD,EALS,KAAA,SAAA,GACA,KAAA,iBAAA,GAET,KAAK,aAAa,IAAI,EAA0B,EAAO,EACvD,KAAK,YAAY,IAAI,EAAgB,EAAO,EAC5C,KAAK,iBAAiB,IAAI,EAAe,EAAO,mBAAmB;;CAGpE,iBAAiB,GAAsG;AACtH,SAAO,EAAwB;GAC9B,gBAAgB,EAAO,aAAa,KAAK,OAAO;GAChD,YAAY,EAAO;GACnB,SAAS,EAAO;GAChB,cAAc,EAAO;GACrB,CAAC;;CAGH,MAAM,OAAO,GASgD;EAC5D,IAAM,IAAY,KAAK,iBAAiB,EAAO,EACzC,IAAS,MAAM,KAAK,WAAW,WAAW,EAAO,MAAM,EACvD,IAAU,MAAM,KAAK,UAAU,MAAM;GAC1C;GACA;GACA,MAAM,EAAO,cAAc,KAAK,OAAO;GACvC,QAAQ,EAAO;GACf,CAAC;AAcF,UAZiB,MAAM,QAAQ,IAC9B,EAAQ,IAAI,OAAO,MAAU;GAC5B,IAAM,IAAO,EAA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rJ,SAAO;GACN;GACA,UAAU,GAAG,EAAU,IAAI;GAC3B;GACA,OAAO,GAAiB;GACxB,MAAM,GAAiB,QAAQ;GAC/B,UAAU,GAAiB,YAAY,EAAE;GACzC,QAAQ,GAAiB;GACzB,WAAW,GAAiB;GAC5B,WAAW,GAAiB;GAC5B;GACA;;CAGF,MAAc,iBAAiB,GAMV;EACpB,IAAM,IAAW,KAAK,KAAK,GAAG,EAAO;AACrC,SAAO,KAAK,KAAK,IAAI,IAAU;AAO9B,QANgB,MAAM,KAAK,OAAO;IACjC,OAAO,EAAO;IACd,WAAW,EAAO;IAClB,YAAY;IACZ,UAAU;IACV,CAAC,EACU,MAAM,MAAW,EAAO,cAAc,EAAO,UAAU,CAClE,QAAO;AAER,OAAI,KAAK,KAAK,GAAG,EAAO,iBAAiB,EACxC;AAED,SAAM,KAAK,MAAM,EAAO,eAAe;;AAExC,SAAO;;CAGR,MAAc,MAAM,GAA2B;AAC9C,QAAM,IAAI,SAAS,MAAY,WAAW,GAAS,EAAG,CAAC"}
|
|
1
|
+
{"version":3,"file":"service.js","names":[],"sources":["../src/service.ts"],"sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport type { OpenClawConfig } from \"openclaw/plugin-sdk/config-runtime\";\nimport { isCloudflareNotFoundError } from \"./cloudflare-api.js\";\nimport { CompanionStore } from \"./companion-store.js\";\nimport { runDoctor } from \"./doctor.js\";\nimport { WorkersAiEmbeddingsClient } from \"./embeddings-client.js\";\nimport { sanitizeNamespace, resolveDefaultNamespace } from \"./namespace.js\";\nimport { hydrateInlineRecord, mapRecordForUpsert } from \"./record-mapper.js\";\nimport type {\n\tCompanionRecord,\n\tDoctorCheck,\n\tDoctorReport,\n\tEmbeddingDimensionsInspection,\n\tHydratedMemoryRecord,\n\tIndexInitializationReport,\n\tMemoryRecordInput,\n\tMetadataFilter,\n\tResolvedPluginConfig,\n\tSmokeTestReport,\n\tUpsertedMemoryRecord,\n\tVectorizeIndexDescription,\n} from \"./types.js\";\nimport { VectorizeClient } from \"./vectorize-client.js\";\n\nconst SMOKE_TEST_TIMEOUT_MS = 12_000;\nconst SMOKE_TEST_POLL_INTERVAL_MS = 1_000;\n\nexport class CloudflareMemoryService {\n\treadonly embeddings: WorkersAiEmbeddingsClient;\n\treadonly vectorize: VectorizeClient;\n\treadonly companionStore: CompanionStore;\n\n\tconstructor(\n\t\treadonly config: ResolvedPluginConfig,\n\t\treadonly openClawConfig: OpenClawConfig,\n\t) {\n\t\tthis.embeddings = new WorkersAiEmbeddingsClient(config);\n\t\tthis.vectorize = new VectorizeClient(config);\n\t\tthis.companionStore = new CompanionStore(config.companionStorePath);\n\t}\n\n\tresolveNamespace(params: { namespace?: string; sessionKey?: string; agentId?: string; workspaceDir?: string }): string {\n\t\treturn resolveDefaultNamespace({\n\t\t\tfixedNamespace: params.namespace ?? this.config.fixedNamespace,\n\t\t\tsessionKey: params.sessionKey,\n\t\t\tagentId: params.agentId,\n\t\t\tworkspaceDir: params.workspaceDir,\n\t\t});\n\t}\n\n\tasync search(params: {\n\t\tquery: string;\n\t\tnamespace?: string;\n\t\tmaxResults?: number;\n\t\tminScore?: number;\n\t\tfilter?: MetadataFilter;\n\t\tsessionKey?: string;\n\t\tagentId?: string;\n\t\tworkspaceDir?: string;\n\t}): Promise<Array<HydratedMemoryRecord & { score: number }>> {\n\t\tconst namespace = this.resolveNamespace(params);\n\t\tconst vector = await this.embeddings.embedQuery(params.query);\n\t\tconst matches = await this.vectorize.query({\n\t\t\tvector,\n\t\t\tnamespace,\n\t\t\ttopK: params.maxResults ?? this.config.topK,\n\t\t\tfilter: params.filter,\n\t\t});\n\n\t\tconst hydrated = await Promise.all(\n\t\t\tmatches.map(async (match) => {\n\t\t\t\tconst base = hydrateInlineRecord(match);\n\t\t\t\tconst text = base.text ?? (await this.companionStore.get(base.namespace, base.logicalId))?.text ?? \"\";\n\t\t\t\treturn {\n\t\t\t\t\t...base,\n\t\t\t\t\ttext,\n\t\t\t\t\tscore: match.score ?? 0,\n\t\t\t\t};\n\t\t\t}),\n\t\t);\n\n\t\treturn hydrated.filter((record) => record.score >= (params.minScore ?? this.config.minScore));\n\t}\n\n\tasync get(params: { id: string; namespace?: string; sessionKey?: string; agentId?: string; workspaceDir?: string }): Promise<HydratedMemoryRecord | null> {\n\t\tconst namespace = this.resolveNamespace(params);\n\t\tconst vectorId = `${namespace}::${params.id}`;\n\t\tconst [match] = await this.vectorize.getByIds([vectorId]);\n\t\tif (!match) {\n\t\t\treturn null;\n\t\t}\n\t\tconst base = hydrateInlineRecord(match);\n\t\tconst companion = await this.companionStore.get(base.namespace, base.logicalId);\n\t\treturn {\n\t\t\t...base,\n\t\t\ttext: base.text ?? companion?.text ?? \"\",\n\t\t};\n\t}\n\n\tasync upsert(params: { input: MemoryRecordInput; sessionKey?: string; agentId?: string; workspaceDir?: string }): Promise<UpsertedMemoryRecord> {\n\t\tconst namespace = this.resolveNamespace({\n\t\t\tnamespace: params.input.namespace,\n\t\t\tsessionKey: params.sessionKey,\n\t\t\tagentId: params.agentId,\n\t\t\tworkspaceDir: params.workspaceDir,\n\t\t});\n\t\tconst embedding = await this.embeddings.embedQuery(params.input.text);\n\t\tconst mapped = mapRecordForUpsert({\n\t\t\tinput: params.input,\n\t\t\tnamespace,\n\t\t\tembedding,\n\t\t\tconfig: this.config,\n\t\t});\n\n\t\tif (mapped.companionRecord) {\n\t\t\tawait this.companionStore.upsert(mapped.companionRecord);\n\t\t}\n\n\t\tconst mutationId = await this.vectorize.upsert([mapped.vector]);\n\t\tconst hydrated = await this.get({\n\t\t\tid: mapped.logicalId,\n\t\t\tnamespace,\n\t\t});\n\n\t\treturn {\n\t\t\t...(hydrated ?? this.fromCompanionFallback(mapped.companionRecord, mapped.logicalId, namespace, mapped.path)),\n\t\t\tmutationId,\n\t\t};\n\t}\n\n\tasync delete(params: { id: string; namespace?: string; sessionKey?: string; agentId?: string; workspaceDir?: string }): Promise<string | undefined> {\n\t\tconst namespace = this.resolveNamespace(params);\n\t\tawait this.companionStore.delete(namespace, params.id);\n\t\treturn this.vectorize.deleteByIds([`${namespace}::${params.id}`]);\n\t}\n\n\tasync doctor(options: { createIndexIfMissing?: boolean }): Promise<DoctorReport> {\n\t\treturn runDoctor({\n\t\t\tservice: this,\n\t\t\tcreateIndexIfMissing: options.createIndexIfMissing ?? false,\n\t\t});\n\t}\n\n\tasync inspectEmbeddingDimensions(): Promise<EmbeddingDimensionsInspection> {\n\t\tconst embeddingDimensions = await this.embeddings.probeDimensions();\n\t\tconst configuredDimensions = this.config.createIndex.dimensions;\n\t\treturn {\n\t\t\tembeddingDimensions,\n\t\t\tconfiguredDimensions,\n\t\t\tconfiguredDimensionsMatchModel: configuredDimensions === undefined || configuredDimensions === embeddingDimensions,\n\t\t\ttargetDimensions: embeddingDimensions,\n\t\t};\n\t}\n\n\tasync describeIndexIfExists(): Promise<VectorizeIndexDescription | null> {\n\t\ttry {\n\t\t\treturn await this.vectorize.describeIndex();\n\t\t} catch (error) {\n\t\t\tif (isCloudflareNotFoundError(error)) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tasync initializeIndex(options?: { recreateIfDimensionMismatch?: boolean }): Promise<IndexInitializationReport> {\n\t\tconst recreateIfDimensionMismatch = options?.recreateIfDimensionMismatch ?? true;\n\t\tconst checks: DoctorCheck[] = [];\n\t\tchecks.push({\n\t\t\tname: \"credentials\",\n\t\t\tstatus: \"pass\",\n\t\t\tmessage: `Using Cloudflare account ${this.config.accountId} and Vectorize index ${this.config.indexName}.`,\n\t\t});\n\n\t\tconst embedding = await this.inspectEmbeddingDimensions();\n\t\tchecks.push({\n\t\t\tname: \"workers-ai-embeddings\",\n\t\t\tstatus: \"pass\",\n\t\t\tmessage: `Workers AI model ${this.config.model} returned ${embedding.embeddingDimensions} dimensions.`,\n\t\t});\n\t\tif (embedding.configuredDimensions !== undefined) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"create-index-dimensions\",\n\t\t\t\tstatus: embedding.configuredDimensionsMatchModel ? \"pass\" : \"warn\",\n\t\t\t\tmessage: embedding.configuredDimensionsMatchModel\n\t\t\t\t\t? `Configured createIndex.dimensions matches the embedding model (${embedding.embeddingDimensions}).`\n\t\t\t\t\t: `Configured createIndex.dimensions (${embedding.configuredDimensions}) does not match the embedding model (${embedding.embeddingDimensions}). Using the live embedding dimensions for initialization.`,\n\t\t\t});\n\t\t}\n\n\t\tconst existingIndex = await this.describeIndexIfExists();\n\t\tlet created = false;\n\t\tlet recreated = false;\n\t\tlet indexDimensions = existingIndex?.config.dimensions;\n\n\t\tif (!existingIndex) {\n\t\t\tconst createdIndex = await this.vectorize.createIndex(embedding.targetDimensions);\n\t\t\tcreated = true;\n\t\t\tindexDimensions = createdIndex.config.dimensions;\n\t\t\tchecks.push({\n\t\t\t\tname: \"vectorize-index\",\n\t\t\t\tstatus: \"pass\",\n\t\t\t\tmessage: `Created Vectorize index \"${this.config.indexName}\" with ${indexDimensions} dimensions.`,\n\t\t\t});\n\t\t} else if (existingIndex.config.dimensions === embedding.targetDimensions) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"vectorize-index\",\n\t\t\t\tstatus: \"pass\",\n\t\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" already uses ${existingIndex.config.dimensions} dimensions.`,\n\t\t\t});\n\t\t} else if (!recreateIfDimensionMismatch) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"vectorize-index\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" uses ${existingIndex.config.dimensions} dimensions, but the embedding model requires ${embedding.targetDimensions}. Recreate the index or rerun init with recreation enabled.`,\n\t\t\t});\n\t\t} else {\n\t\t\tawait this.vectorize.deleteIndex();\n\t\t\tconst recreatedIndex = await this.vectorize.createIndex(embedding.targetDimensions);\n\t\t\trecreated = true;\n\t\t\tindexDimensions = recreatedIndex.config.dimensions;\n\t\t\tchecks.push({\n\t\t\t\tname: \"vectorize-index\",\n\t\t\t\tstatus: \"pass\",\n\t\t\t\tmessage: `Recreated Vectorize index \"${this.config.indexName}\" from ${existingIndex.config.dimensions} to ${indexDimensions} dimensions.`,\n\t\t\t});\n\t\t}\n\n\t\tchecks.push({\n\t\t\tname: \"dimension-match\",\n\t\t\tstatus: indexDimensions === embedding.embeddingDimensions ? \"pass\" : \"fail\",\n\t\t\tmessage:\n\t\t\t\tindexDimensions === embedding.embeddingDimensions\n\t\t\t\t\t? \"Embedding dimensions match the Vectorize index.\"\n\t\t\t\t\t: `Embedding dimensions (${embedding.embeddingDimensions}) do not match the Vectorize index dimensions (${indexDimensions ?? \"unknown\"}).`,\n\t\t});\n\t\tchecks.push({\n\t\t\tname: \"metadata-filters\",\n\t\t\tstatus: this.config.metadataIndexedFields.length > 0 ? \"pass\" : \"warn\",\n\t\t\tmessage:\n\t\t\t\tthis.config.metadataIndexedFields.length > 0\n\t\t\t\t\t? `Configured metadata-index guidance for: ${this.config.metadataIndexedFields.join(\", \")}.`\n\t\t\t\t\t: \"No metadataIndexedFields configured. Add metadata indexes in Cloudflare before relying on filter-heavy queries.\",\n\t\t});\n\n\t\treturn {\n\t\t\tok: checks.every((check) => check.status !== \"fail\"),\n\t\t\tchecks,\n\t\t\tcreated,\n\t\t\trecreated,\n\t\t\tembeddingDimensions: embedding.embeddingDimensions,\n\t\t\tindexDimensions,\n\t\t};\n\t}\n\n\tasync runSmokeTest(options?: { timeoutMs?: number; pollIntervalMs?: number }): Promise<SmokeTestReport> {\n\t\tconst checks: DoctorCheck[] = [];\n\t\tchecks.push({\n\t\t\tname: \"credentials\",\n\t\t\tstatus: \"pass\",\n\t\t\tmessage: `Using Cloudflare account ${this.config.accountId} and Vectorize index ${this.config.indexName}.`,\n\t\t});\n\n\t\tconst embedding = await this.inspectEmbeddingDimensions();\n\t\tchecks.push({\n\t\t\tname: \"workers-ai-embeddings\",\n\t\t\tstatus: \"pass\",\n\t\t\tmessage: `Workers AI model ${this.config.model} returned ${embedding.embeddingDimensions} dimensions.`,\n\t\t});\n\t\tif (embedding.configuredDimensions !== undefined) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"create-index-dimensions\",\n\t\t\t\tstatus: embedding.configuredDimensionsMatchModel ? \"pass\" : \"warn\",\n\t\t\t\tmessage: embedding.configuredDimensionsMatchModel\n\t\t\t\t\t? `Configured createIndex.dimensions matches the embedding model (${embedding.embeddingDimensions}).`\n\t\t\t\t\t: `Configured createIndex.dimensions (${embedding.configuredDimensions}) does not match the embedding model (${embedding.embeddingDimensions}). Using the live embedding dimensions for validation.`,\n\t\t\t});\n\t\t}\n\n\t\tconst existingIndex = await this.describeIndexIfExists();\n\t\tif (!existingIndex) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"vectorize-index\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" was not found. Run \"openclaw cf-memory init\" before rerunning this test.`,\n\t\t\t});\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tchecks,\n\t\t\t\tnamespace: \"n/a\",\n\t\t\t\tlogicalId: \"n/a\",\n\t\t\t};\n\t\t}\n\n\t\tchecks.push({\n\t\t\tname: \"vectorize-index\",\n\t\t\tstatus: \"pass\",\n\t\t\tmessage: `Vectorize index \"${this.config.indexName}\" is reachable.`,\n\t\t});\n\t\tif (existingIndex.config.dimensions !== embedding.embeddingDimensions) {\n\t\t\tchecks.push({\n\t\t\t\tname: \"dimension-match\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: `Embedding dimensions (${embedding.embeddingDimensions}) do not match the Vectorize index dimensions (${existingIndex.config.dimensions}). Run \"openclaw cf-memory init\" to repair the index.`,\n\t\t\t});\n\t\t\treturn {\n\t\t\t\tok: false,\n\t\t\t\tchecks,\n\t\t\t\tnamespace: \"n/a\",\n\t\t\t\tlogicalId: \"n/a\",\n\t\t\t};\n\t\t}\n\n\t\tchecks.push({\n\t\t\tname: \"dimension-match\",\n\t\t\tstatus: \"pass\",\n\t\t\tmessage: \"Embedding dimensions match the Vectorize index.\",\n\t\t});\n\n\t\tconst namespace = sanitizeNamespace(`cf-memory-test-${randomUUID()}`);\n\t\tconst logicalId = `cf-memory-test-${randomUUID()}`;\n\t\tconst probeText = `OpenClaw Cloudflare memory smoke test ${logicalId}`;\n\t\tlet probeUpserted = false;\n\n\t\ttry {\n\t\t\tawait this.upsert({\n\t\t\t\tinput: {\n\t\t\t\t\tid: logicalId,\n\t\t\t\t\tnamespace,\n\t\t\t\t\ttext: probeText,\n\t\t\t\t\tsource: \"cf-memory-test\",\n\t\t\t\t\tmetadata: {\n\t\t\t\t\t\tprobe: true,\n\t\t\t\t\t\tprobeId: logicalId,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\t\t\tprobeUpserted = true;\n\t\t\tchecks.push({\n\t\t\t\tname: \"probe-upsert\",\n\t\t\t\tstatus: \"pass\",\n\t\t\t\tmessage: `Inserted smoke-test record ${logicalId} in namespace ${namespace}.`,\n\t\t\t});\n\n\t\t\tconst found = await this.waitForSearchHit({\n\t\t\t\tquery: probeText,\n\t\t\t\tnamespace,\n\t\t\t\tlogicalId,\n\t\t\t\ttimeoutMs: options?.timeoutMs ?? SMOKE_TEST_TIMEOUT_MS,\n\t\t\t\tpollIntervalMs: options?.pollIntervalMs ?? SMOKE_TEST_POLL_INTERVAL_MS,\n\t\t\t});\n\t\t\tchecks.push({\n\t\t\t\tname: \"probe-search\",\n\t\t\t\tstatus: found ? \"pass\" : \"fail\",\n\t\t\t\tmessage: found\n\t\t\t\t\t? \"Semantic search returned the smoke-test record.\"\n\t\t\t\t\t: `Semantic search did not return the smoke-test record within ${(options?.timeoutMs ?? SMOKE_TEST_TIMEOUT_MS) / 1000} seconds.`,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tchecks.push({\n\t\t\t\tname: probeUpserted ? \"probe-search\" : \"probe-upsert\",\n\t\t\t\tstatus: \"fail\",\n\t\t\t\tmessage: error instanceof Error ? error.message : \"Smoke test failed with an unknown error.\",\n\t\t\t});\n\t\t} finally {\n\t\t\ttry {\n\t\t\t\tawait this.delete({ id: logicalId, namespace });\n\t\t\t\tchecks.push({\n\t\t\t\t\tname: \"probe-cleanup\",\n\t\t\t\t\tstatus: \"pass\",\n\t\t\t\t\tmessage: `Deleted smoke-test record ${logicalId}.`,\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tchecks.push({\n\t\t\t\t\tname: \"probe-cleanup\",\n\t\t\t\t\tstatus: \"fail\",\n\t\t\t\t\tmessage: error instanceof Error ? error.message : `Failed to delete smoke-test record ${logicalId}.`,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\tok: checks.every((check) => check.status !== \"fail\"),\n\t\t\tchecks,\n\t\t\tnamespace,\n\t\t\tlogicalId,\n\t\t};\n\t}\n\n\tasync ensureIndexExists(createIfMissing: boolean, targetDimensions?: number): Promise<{ created: boolean; dimensions: number }> {\n\t\ttry {\n\t\t\tconst description = await this.vectorize.describeIndex();\n\t\t\treturn {\n\t\t\t\tcreated: false,\n\t\t\t\tdimensions: description.config.dimensions,\n\t\t\t};\n\t\t} catch (error) {\n\t\t\tif (!createIfMissing || !isCloudflareNotFoundError(error)) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\tconst dimensions = targetDimensions ?? (await this.inspectEmbeddingDimensions()).targetDimensions;\n\t\t\tconst createdIndex = await this.vectorize.createIndex(dimensions);\n\t\t\treturn {\n\t\t\t\tcreated: true,\n\t\t\t\tdimensions: createdIndex.config.dimensions,\n\t\t\t};\n\t\t}\n\t}\n\n\tprivate fromCompanionFallback(companionRecord: CompanionRecord | undefined, logicalId: string, namespace: string, path: string): UpsertedMemoryRecord {\n\t\treturn {\n\t\t\tlogicalId,\n\t\t\tvectorId: `${namespace}::${logicalId}`,\n\t\t\tnamespace,\n\t\t\ttitle: companionRecord?.title,\n\t\t\ttext: companionRecord?.text ?? \"\",\n\t\t\tmetadata: companionRecord?.metadata ?? {},\n\t\t\tsource: companionRecord?.source,\n\t\t\tcreatedAt: companionRecord?.createdAt,\n\t\t\tupdatedAt: companionRecord?.updatedAt,\n\t\t\tpath,\n\t\t};\n\t}\n\n\tprivate async waitForSearchHit(params: { query: string; namespace: string; logicalId: string; timeoutMs: number; pollIntervalMs: number }): Promise<boolean> {\n\t\tconst deadline = Date.now() + params.timeoutMs;\n\t\twhile (Date.now() <= deadline) {\n\t\t\tconst results = await this.search({\n\t\t\t\tquery: params.query,\n\t\t\t\tnamespace: params.namespace,\n\t\t\t\tmaxResults: 5,\n\t\t\t\tminScore: 0,\n\t\t\t});\n\t\t\tif (results.some((record) => record.logicalId === params.logicalId)) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tif (Date.now() + params.pollIntervalMs > deadline) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tawait this.pause(params.pollIntervalMs);\n\t\t}\n\t\treturn false;\n\t}\n\n\tprivate async pause(ms: number): Promise<void> {\n\t\tawait new Promise((resolve) => setTimeout(resolve, ms));\n\t}\n}\n"],"mappings":";;;;;;;;;AAwBA,IAAM,IAAwB,MACxB,IAA8B,KAEvB,IAAb,MAAqC;CACpC;CACA;CACA;CAEA,YACC,GACA,GACC;AAGD,EALS,KAAA,SAAA,GACA,KAAA,iBAAA,GAET,KAAK,aAAa,IAAI,EAA0B,EAAO,EACvD,KAAK,YAAY,IAAI,EAAgB,EAAO,EAC5C,KAAK,iBAAiB,IAAI,EAAe,EAAO,mBAAmB;;CAGpE,iBAAiB,GAAsG;AACtH,SAAO,EAAwB;GAC9B,gBAAgB,EAAO,aAAa,KAAK,OAAO;GAChD,YAAY,EAAO;GACnB,SAAS,EAAO;GAChB,cAAc,EAAO;GACrB,CAAC;;CAGH,MAAM,OAAO,GASgD;EAC5D,IAAM,IAAY,KAAK,iBAAiB,EAAO,EACzC,IAAS,MAAM,KAAK,WAAW,WAAW,EAAO,MAAM,EACvD,IAAU,MAAM,KAAK,UAAU,MAAM;GAC1C;GACA;GACA,MAAM,EA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AAEjF,UAAO;IACN,SAAS;IACT,aAHoB,MAAM,KAAK,UAAU,YAAY,EAAW,EAGvC,OAAO;IAChC;;;CAIH,sBAA8B,GAA8C,GAAmB,GAAmB,GAAoC;AACrJ,SAAO;GACN;GACA,UAAU,GAAG,EAAU,IAAI;GAC3B;GACA,OAAO,GAAiB;GACxB,MAAM,GAAiB,QAAQ;GAC/B,UAAU,GAAiB,YAAY,EAAE;GACzC,QAAQ,GAAiB;GACzB,WAAW,GAAiB;GAC5B,WAAW,GAAiB;GAC5B;GACA;;CAGF,MAAc,iBAAiB,GAA8H;EAC5J,IAAM,IAAW,KAAK,KAAK,GAAG,EAAO;AACrC,SAAO,KAAK,KAAK,IAAI,IAAU;AAO9B,QANgB,MAAM,KAAK,OAAO;IACjC,OAAO,EAAO;IACd,WAAW,EAAO;IAClB,YAAY;IACZ,UAAU;IACV,CAAC,EACU,MAAM,MAAW,EAAO,cAAc,EAAO,UAAU,CAClE,QAAO;AAER,OAAI,KAAK,KAAK,GAAG,EAAO,iBAAiB,EACxC;AAED,SAAM,KAAK,MAAM,EAAO,eAAe;;AAExC,SAAO;;CAGR,MAAc,MAAM,GAA2B;AAC9C,QAAM,IAAI,SAAS,MAAY,WAAW,GAAS,EAAG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vectorize-client.js","names":[],"sources":["../src/vectorize-client.ts"],"sourcesContent":["import { requestCloudflare } from \"./cloudflare-api.js\";\
|
|
1
|
+
{"version":3,"file":"vectorize-client.js","names":[],"sources":["../src/vectorize-client.ts"],"sourcesContent":["import { requestCloudflare } from \"./cloudflare-api.js\";\nimport type { MetadataFilter, ResolvedPluginConfig, VectorizeIndexDescription, VectorizeQueryMatch, VectorizeVector } from \"./types.js\";\n\ntype MutationResponse = {\n\tmutationId?: string;\n};\n\ntype QueryResponse = {\n\tcount?: number;\n\tmatches?: VectorizeQueryMatch[];\n};\n\nexport class VectorizeClient {\n\tconstructor(private readonly config: ResolvedPluginConfig) {}\n\n\tasync describeIndex(): Promise<VectorizeIndexDescription> {\n\t\treturn requestCloudflare<VectorizeIndexDescription>({\n\t\t\turl: this.config.vectorizeBaseUrl,\n\t\t\tapiToken: this.config.apiToken,\n\t\t\tmethod: \"GET\",\n\t\t});\n\t}\n\n\tasync createIndex(dimensions: number, metric = this.config.createIndex.metric): Promise<VectorizeIndexDescription> {\n\t\treturn requestCloudflare<VectorizeIndexDescription>({\n\t\t\turl: `${this.config.apiBaseUrl}/accounts/${this.config.accountId}/vectorize/v2/indexes`,\n\t\t\tapiToken: this.config.apiToken,\n\t\t\tbody: JSON.stringify({\n\t\t\t\tname: this.config.indexName,\n\t\t\t\tdescription: this.config.createIndex.description,\n\t\t\t\tconfig: {\n\t\t\t\t\tdimensions,\n\t\t\t\t\tmetric,\n\t\t\t\t},\n\t\t\t}),\n\t\t});\n\t}\n\n\tasync deleteIndex(): Promise<void> {\n\t\tawait requestCloudflare<unknown>({\n\t\t\turl: this.config.vectorizeBaseUrl,\n\t\t\tapiToken: this.config.apiToken,\n\t\t\tmethod: \"DELETE\",\n\t\t});\n\t}\n\n\tasync upsert(vectors: VectorizeVector[]): Promise<string | undefined> {\n\t\tconst body = vectors.map((vector) => JSON.stringify(vector)).join(\"\\n\");\n\t\tconst result = await requestCloudflare<MutationResponse>({\n\t\t\turl: `${this.config.vectorizeBaseUrl}/upsert`,\n\t\t\tapiToken: this.config.apiToken,\n\t\t\theaders: {\n\t\t\t\t\"Content-Type\": \"application/x-ndjson\",\n\t\t\t},\n\t\t\tbody,\n\t\t});\n\t\treturn result.mutationId;\n\t}\n\n\tasync query(params: {\n\t\tvector: number[];\n\t\tnamespace?: string;\n\t\ttopK?: number;\n\t\tfilter?: MetadataFilter;\n\t\treturnValues?: boolean;\n\t}): Promise<VectorizeQueryMatch[]> {\n\t\tconst result = await requestCloudflare<QueryResponse>({\n\t\t\turl: `${this.config.vectorizeBaseUrl}/query`,\n\t\t\tapiToken: this.config.apiToken,\n\t\t\tbody: JSON.stringify({\n\t\t\t\tvector: params.vector,\n\t\t\t\ttopK: params.topK ?? this.config.topK,\n\t\t\t\tfilter: params.filter,\n\t\t\t\tnamespace: params.namespace,\n\t\t\t\treturnValues: params.returnValues ?? false,\n\t\t\t}),\n\t\t});\n\t\treturn result.matches ?? [];\n\t}\n\n\tasync getByIds(ids: string[]): Promise<VectorizeQueryMatch[]> {\n\t\tif (ids.length === 0) {\n\t\t\treturn [];\n\t\t}\n\t\treturn requestCloudflare<VectorizeQueryMatch[]>({\n\t\t\turl: `${this.config.vectorizeBaseUrl}/get_by_ids`,\n\t\t\tapiToken: this.config.apiToken,\n\t\t\tbody: JSON.stringify({ ids }),\n\t\t});\n\t}\n\n\tasync deleteByIds(ids: string[]): Promise<string | undefined> {\n\t\tif (ids.length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\tconst result = await requestCloudflare<MutationResponse>({\n\t\t\turl: `${this.config.vectorizeBaseUrl}/delete_by_ids`,\n\t\t\tapiToken: this.config.apiToken,\n\t\t\tbody: JSON.stringify({ ids }),\n\t\t});\n\t\treturn result.mutationId;\n\t}\n}\n"],"mappings":";;AAYA,IAAa,IAAb,MAA6B;CAC5B,YAAY,GAA+C;AAA9B,OAAA,SAAA;;CAE7B,MAAM,gBAAoD;AACzD,SAAO,EAA6C;GACnD,KAAK,KAAK,OAAO;GACjB,UAAU,KAAK,OAAO;GACtB,QAAQ;GACR,CAAC;;CAGH,MAAM,YAAY,GAAoB,IAAS,KAAK,OAAO,YAAY,QAA4C;AAClH,SAAO,EAA6C;GACnD,KAAK,GAAG,KAAK,OAAO,WAAW,YAAY,KAAK,OAAO,UAAU;GACjE,UAAU,KAAK,OAAO;GACtB,MAAM,KAAK,UAAU;IACpB,MAAM,KAAK,OAAO;IAClB,aAAa,KAAK,OAAO,YAAY;IACrC,QAAQ;KACP;KACA;KACA;IACD,CAAC;GACF,CAAC;;CAGH,MAAM,cAA6B;AAClC,QAAM,EAA2B;GAChC,KAAK,KAAK,OAAO;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