metainsight-context-engine 0.0.1

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.
@@ -0,0 +1,221 @@
1
+ /**
2
+ * COS Bootstrap — Infrastructure Initialization
3
+ *
4
+ * Ensures all preconditions for the Cloud Context Engine are met before
5
+ * the engine starts processing requests:
6
+ *
7
+ * 1. COS SDK instance is initialized (SecretId / SecretKey auth)
8
+ * 2. Target bucket exists (headBucket → putBucket if missing)
9
+ * 3. For each dataset definition:
10
+ * a. Dataset exists (GET /dataset → POST /dataset if missing)
11
+ * b. Dataset ↔ Bucket binding exists (GET /datasetbinding → POST /datasetbinding if missing)
12
+ *
13
+ * The module supports **multiple datasets** per bucket — each dataset has its
14
+ * own COS path prefix and CI template. The default setup includes a "memory"
15
+ * dataset (DocSearch) and an "image" dataset (ImageSearch) for multimodal
16
+ * retrieval. Users can extend it with "knowledge" or any custom datasets.
17
+ *
18
+ * **Multi-agent isolation**: When `agentId` is set, the default cosPrefix
19
+ * becomes `openclaw-{agentId}/workspace/memory/`. This allows multiple
20
+ * agents to share the same bucket without data collisions.
21
+ *
22
+ * The module also exposes a generic `sendCIRequest()` helper for CI API
23
+ * calls (used by cos-operations.ts for hybridsearch, upload, etc.).
24
+ *
25
+ * Supported regions: ap-beijing, ap-shanghai, ap-chengdu
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * // Minimal — uses default datasets (memory)
30
+ * bootstrap({ secretId, secretKey, appId: '1253311026' }, logger);
31
+ *
32
+ * // Extended — add a custom knowledge dataset alongside defaults
33
+ * bootstrap({
34
+ * secretId, secretKey,
35
+ * appId: '1253311026',
36
+ * datasets: [
37
+ * { name: 'my-memory', cosPrefix: 'memory/', templateId: 'Official:DocSearch' },
38
+ * { name: 'my-knowledge', cosPrefix: 'knowledge/', templateId: 'Official:DocSearch' },
39
+ * ],
40
+ * }, logger);
41
+ * ```
42
+ */
43
+ import COS from 'cos-nodejs-sdk-v5';
44
+ /**
45
+ * Definition for a single dataset that should be bootstrapped.
46
+ *
47
+ * - `name` — CI dataset name (unique within the APPID scope)
48
+ * - `cosPrefix` — COS key prefix the binding should cover (e.g. "memory/")
49
+ * - `templateId` — CI dataset template (default: "Official:DocSearch")
50
+ * - `description` — Human-readable description used when auto-creating
51
+ */
52
+ export interface DatasetDefinition {
53
+ /** CI dataset name (e.g. "openclaw-memory"). */
54
+ name: string;
55
+ /**
56
+ * COS key prefix that the binding covers.
57
+ * Must end with "/" (e.g. "memory/").
58
+ */
59
+ cosPrefix: string;
60
+ /** CI dataset template (default: "Official:DocSearch"). */
61
+ templateId?: string;
62
+ /** Description applied when auto-creating the dataset. */
63
+ description?: string;
64
+ }
65
+ export interface CosBootstrapConfig {
66
+ secretId: string;
67
+ secretKey: string;
68
+ /**
69
+ * Tencent Cloud APPID (e.g. "1253311026").
70
+ * Obtain from https://console.cloud.tencent.com/cam/capi
71
+ */
72
+ appId: string;
73
+ /**
74
+ * Agent ID for multi-agent isolation within a single bucket.
75
+ *
76
+ * When set, the default cosPrefix becomes:
77
+ * `openclaw-{agentId}/workspace/memory/`
78
+ *
79
+ * Typically resolved by the plugin entry point (index.ts) with priority:
80
+ * 1. User-configured cfg.agentId
81
+ * 2. Hook ctx.agentId (auto-detected at runtime)
82
+ * 3. Fallback: 'main'
83
+ *
84
+ * When omitted here, the legacy flat prefix `memory/` is used
85
+ * (only happens if the caller does not resolve agentId beforehand).
86
+ */
87
+ agentId?: string;
88
+ /** COS bucket short name without APPID suffix (e.g. "openclaw-metainsight"). */
89
+ bucket?: string;
90
+ /** COS region — only ap-beijing / ap-shanghai / ap-chengdu are supported. */
91
+ region?: string;
92
+ /**
93
+ * Dataset definitions to bootstrap.
94
+ *
95
+ * When omitted, three default datasets are created:
96
+ * ```
97
+ * [
98
+ * { name: "openclaw-memory", cosPrefix: "memory/", templateId: "Official:DocSearch" },
99
+ * { name: "openclaw-image", cosPrefix: "asset/", templateId: "Official:ImageSearch" },
100
+ * { name: "openclaw-document", cosPrefix: "asset/", templateId: "Official:DocSearch" },
101
+ * ]
102
+ * ```
103
+ *
104
+ * When `agentId` is set and datasets are omitted, the defaults become:
105
+ * ```
106
+ * [
107
+ * { name: "openclaw-memory-{agentId}", cosPrefix: "openclaw-{agentId}/workspace/", ... },
108
+ * { name: "openclaw-{agentId}-image", cosPrefix: "openclaw-{agentId}/asset/", ... },
109
+ * { name: "openclaw-{agentId}-document", cosPrefix: "openclaw-{agentId}/asset/", ... },
110
+ * ]
111
+ * ```
112
+ *
113
+ * To add more datasets (e.g. knowledge), supply the full array:
114
+ * ```
115
+ * [
116
+ * { name: "openclaw-memory", cosPrefix: "memory/" },
117
+ * { name: "openclaw-image", cosPrefix: "asset/", templateId: "Official:ImageSearch" },
118
+ * { name: "openclaw-document", cosPrefix: "asset/" },
119
+ * { name: "openclaw-knowledge", cosPrefix: "knowledge/" },
120
+ * ]
121
+ * ```
122
+ */
123
+ datasets?: DatasetDefinition[];
124
+ }
125
+ export interface ResolvedDataset {
126
+ /** CI dataset name. */
127
+ name: string;
128
+ /** COS key prefix the binding covers (e.g. "memory/"). */
129
+ cosPrefix: string;
130
+ /** CI dataset template. */
131
+ templateId: string;
132
+ /** Dataset description. */
133
+ description: string;
134
+ }
135
+ export interface ResolvedCosConfig {
136
+ secretId: string;
137
+ secretKey: string;
138
+ appId: string;
139
+ /** Agent ID for multi-agent isolation (undefined = legacy flat layout). */
140
+ agentId?: string;
141
+ /** Full bucket name in COS format: "{shortName}-{APPID}" */
142
+ bucket: string;
143
+ region: string;
144
+ /** All datasets to bootstrap (always ≥ 1). */
145
+ datasets: ResolvedDataset[];
146
+ }
147
+ /** Subset of Dataset info returned by CI API. */
148
+ export interface DatasetInfo {
149
+ DatasetName?: string;
150
+ Description?: string;
151
+ TemplateId?: string;
152
+ [key: string]: unknown;
153
+ }
154
+ /** Subset of Binding info returned by CI API. */
155
+ export interface DatasetBinding {
156
+ DatasetName?: string;
157
+ URI?: string;
158
+ State?: string;
159
+ [key: string]: unknown;
160
+ }
161
+ interface Logger {
162
+ info: (...args: unknown[]) => void;
163
+ warn: (...args: unknown[]) => void;
164
+ }
165
+ /**
166
+ * Get (or create) the COS SDK singleton.
167
+ */
168
+ export declare function getCOSInstance(secretId: string, secretKey: string): COS;
169
+ /**
170
+ * Merge user-provided config with defaults, validate region.
171
+ *
172
+ * The bucket name is automatically resolved to COS format: `{shortName}-{APPID}`.
173
+ * If the user-provided bucket already contains the APPID suffix, it is used as-is.
174
+ *
175
+ * When `datasets` is omitted, three default datasets are created (memory, image, document).
176
+ * Each dataset's `cosPrefix` is normalized to end with "/".
177
+ */
178
+ export declare function resolveConfig(input: CosBootstrapConfig): ResolvedCosConfig;
179
+ /**
180
+ * Send a request to the COS CI API with automatic SDK signing.
181
+ *
182
+ * @param cos COS SDK instance
183
+ * @param bucket Bucket name
184
+ * @param region COS region
185
+ * @param method HTTP method (GET / POST / PUT / DELETE)
186
+ * @param endpoint CI API endpoint path (e.g. "dataset", "datasetbinding", "datasetquery/hybridsearch")
187
+ * @param body Optional JSON request body
188
+ * @param query Optional query string parameters
189
+ * @returns Parsed JSON response
190
+ */
191
+ export declare function sendCIRequest(cos: COS, bucket: string, region: string, method: string, endpoint: string, body?: Record<string, unknown>, query?: Record<string, string>): Promise<unknown>;
192
+ /** Per-dataset bootstrap result. */
193
+ export interface DatasetOutcome {
194
+ name: string;
195
+ cosPrefix: string;
196
+ datasetCreated: boolean;
197
+ bindingCreated: boolean;
198
+ }
199
+ export interface BootstrapOutcome {
200
+ success: boolean;
201
+ cos: COS;
202
+ config: ResolvedCosConfig;
203
+ bucketCreated: boolean;
204
+ /** Per-dataset results (one entry per dataset in config.datasets). */
205
+ datasetOutcomes: DatasetOutcome[];
206
+ error?: string;
207
+ }
208
+ /**
209
+ * Run the full bootstrap sequence:
210
+ * 1. Initialize COS SDK
211
+ * 2. Ensure bucket exists
212
+ * 3. For each dataset in config.datasets:
213
+ * a. Ensure dataset exists (GET → POST if missing)
214
+ * b. Ensure binding exists (GET → POST if missing), bound to `cos://{bucket}/{cosPrefix}`
215
+ *
216
+ * Returns a {@link BootstrapOutcome} with the COS instance and resolved config,
217
+ * ready for use by `CloudClient`.
218
+ */
219
+ export declare function bootstrap(input: CosBootstrapConfig, logger: Logger): Promise<BootstrapOutcome>;
220
+ export {};
221
+ //# sourceMappingURL=cos-bootstrap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cos-bootstrap.d.ts","sourceRoot":"","sources":["../cos-bootstrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAGH,OAAO,GAAG,MAAM,mBAAmB,CAAC;AAMpC;;;;;;;GAOG;AACH,MAAM,WAAW,iBAAiB;IAChC,gDAAgD;IAChD,IAAI,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,SAAS,EAAE,MAAM,CAAC;IAClB,2DAA2D;IAC3D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0DAA0D;IAC1D,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;;;OAaG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,6EAA6E;IAC7E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACH,QAAQ,CAAC,EAAE,iBAAiB,EAAE,CAAC;CAChC;AAED,MAAM,WAAW,eAAe;IAC9B,uBAAuB;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,0DAA0D;IAC1D,SAAS,EAAE,MAAM,CAAC;IAClB,2BAA2B;IAC3B,UAAU,EAAE,MAAM,CAAC;IACnB,2BAA2B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