@aliyunrds/ctxdb 1.0.6 → 1.0.8-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.internal.md +59 -0
- package/README.md +210 -23
- package/dist/{chunk-BHRWAU3E.js → chunk-24CNB3XJ.js} +2 -2
- package/dist/{chunk-ARZX2RLR.js → chunk-AAZLOCVB.js} +246 -34
- package/dist/{chunk-7EIJB5LA.js → chunk-BBTXHFMD.js} +1 -1
- package/dist/{chunk-L4GVGVOP.js → chunk-CAXRYH6E.js} +235 -43
- package/dist/{chunk-6BB65W5W.js → chunk-HF2CWTRU.js} +2 -2
- package/dist/{chunk-B7JCQL4O.js → chunk-NJJBT52C.js} +1 -1
- package/dist/chunk-R67JELM7.js +20 -0
- package/dist/{chunk-Z6OHSEF2.js → chunk-ULAWTBBC.js} +34 -16
- package/dist/chunk-VIG4SYLU.js +8 -0
- package/dist/{chunk-VHNHVMCA.js → chunk-WSMZOFZQ.js} +3 -3
- package/dist/cli/main.js +1627 -62
- package/dist/hooks/hermes-post-llm-call.js +6 -4
- package/dist/hooks/hermes-pre-llm-call.js +9 -7
- package/dist/hooks/pre-tool-use.js +2 -1
- package/dist/hooks/session-start.js +8 -6
- package/dist/hooks/stop.js +6 -4
- package/dist/hooks/user-prompt-submit.js +8 -6
- package/dist/opencode/index.js +58 -2
- package/dist/setup/skills/contextdb-knowledge/SKILL.md +46 -8
- package/dist/setup/skills/contextdb-memory/SKILL.md +1 -1
- package/dist/workers/version-check.js +3 -1
- package/package.json +6 -2
- /package/dist/{chunk-UEKR2Z3S.js → chunk-5ML4VSXN.js} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
CtxdbError
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-AAZLOCVB.js";
|
|
5
5
|
|
|
6
6
|
// src/lib/kb.ts
|
|
7
7
|
import {
|
|
@@ -94,6 +94,51 @@ async function createKb(client, kbName, description = "") {
|
|
|
94
94
|
return client.postJson(KB_COLLECTION, { name: kbName, description: description || "" });
|
|
95
95
|
}
|
|
96
96
|
async function uploadText(client, kbName, docName, text, mimeType = "text/plain", filePath) {
|
|
97
|
+
return client.postJson(
|
|
98
|
+
DOCUMENTS,
|
|
99
|
+
buildTextDocumentBody(kbName, docName, text, mimeType, filePath)
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
async function updateText(client, locator, text) {
|
|
103
|
+
const body = {
|
|
104
|
+
...buildUpdateLocatorFields(locator),
|
|
105
|
+
text
|
|
106
|
+
};
|
|
107
|
+
const response = await client.putJson(DOCUMENTS, body);
|
|
108
|
+
return readCanonicalUpdateDocument(response, "text update");
|
|
109
|
+
}
|
|
110
|
+
function buildUpdateLocatorFields(locator) {
|
|
111
|
+
if (!locator || typeof locator !== "object") {
|
|
112
|
+
throw new CtxdbError("update requires exactly one document locator");
|
|
113
|
+
}
|
|
114
|
+
const candidate = locator;
|
|
115
|
+
const hasDocumentId = Object.hasOwn(candidate, "documentId");
|
|
116
|
+
const hasLogicalField = ["kbName", "docName", "filePath"].some((field) => Object.hasOwn(candidate, field));
|
|
117
|
+
if (hasDocumentId === hasLogicalField) {
|
|
118
|
+
throw new CtxdbError("update requires exactly one document locator");
|
|
119
|
+
}
|
|
120
|
+
if (hasDocumentId) {
|
|
121
|
+
if (typeof candidate.documentId !== "string" || candidate.documentId.trim() === "") {
|
|
122
|
+
throw new CtxdbError("update requires a non-empty document ID");
|
|
123
|
+
}
|
|
124
|
+
return { document_id: candidate.documentId };
|
|
125
|
+
}
|
|
126
|
+
if (typeof candidate.kbName !== "string" || candidate.kbName.trim() === "" || typeof candidate.docName !== "string" || candidate.docName.trim() === "") {
|
|
127
|
+
throw new CtxdbError("logical update requires KB name and document name");
|
|
128
|
+
}
|
|
129
|
+
if (candidate.filePath !== void 0 && (typeof candidate.filePath !== "string" || candidate.filePath.trim() === "")) {
|
|
130
|
+
throw new CtxdbError("logical update file path must be a non-empty string");
|
|
131
|
+
}
|
|
132
|
+
const fields = {
|
|
133
|
+
knowledge_base_name: candidate.kbName,
|
|
134
|
+
name: candidate.docName
|
|
135
|
+
};
|
|
136
|
+
if (typeof candidate.filePath === "string") {
|
|
137
|
+
fields.file_path = candidate.filePath;
|
|
138
|
+
}
|
|
139
|
+
return fields;
|
|
140
|
+
}
|
|
141
|
+
function buildTextDocumentBody(kbName, docName, text, mimeType, filePath) {
|
|
97
142
|
const body = {
|
|
98
143
|
knowledge_base_name: kbName,
|
|
99
144
|
name: docName,
|
|
@@ -101,74 +146,133 @@ async function uploadText(client, kbName, docName, text, mimeType = "text/plain"
|
|
|
101
146
|
mime_type: mimeType
|
|
102
147
|
};
|
|
103
148
|
if (filePath !== void 0 && filePath !== "") body.file_path = filePath;
|
|
104
|
-
return
|
|
149
|
+
return body;
|
|
105
150
|
}
|
|
106
151
|
async function uploadFile(client, kbName, localPath, options = {}) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
152
|
+
return writeFile(
|
|
153
|
+
client,
|
|
154
|
+
{
|
|
155
|
+
mode: "create",
|
|
156
|
+
kbName,
|
|
157
|
+
docName: options.docName,
|
|
158
|
+
filePath: options.filePath
|
|
159
|
+
},
|
|
160
|
+
localPath,
|
|
161
|
+
options.timeoutMs
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
async function updateFile(client, locator, localPath, options = {}) {
|
|
165
|
+
buildUpdateLocatorFields(locator);
|
|
166
|
+
return writeFile(
|
|
167
|
+
client,
|
|
168
|
+
{ mode: "update", locator },
|
|
169
|
+
localPath,
|
|
170
|
+
options.timeoutMs
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
async function writeFile(client, target, localPath, requestedTimeoutMs) {
|
|
174
|
+
const { expanded, stat } = inspectLocalFile(localPath);
|
|
118
175
|
const filename = basename(expanded);
|
|
119
|
-
const docName = options.docName ?? filename;
|
|
120
176
|
const mime = guessMime(expanded);
|
|
121
177
|
const content = await openAsBlob(expanded, { type: mime });
|
|
122
|
-
const fields = {
|
|
123
|
-
knowledge_base_name: kbName,
|
|
124
|
-
name: docName
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
const timeoutMs = options.timeoutMs ?? DEFAULT_FILE_UPLOAD_TIMEOUT_MS;
|
|
178
|
+
const fields = target.mode === "create" ? {
|
|
179
|
+
knowledge_base_name: target.kbName,
|
|
180
|
+
name: target.docName ?? filename,
|
|
181
|
+
...target.filePath ? { file_path: target.filePath } : {}
|
|
182
|
+
} : buildUpdateLocatorFields(target.locator);
|
|
183
|
+
const timeoutMs = requestedTimeoutMs ?? DEFAULT_FILE_UPLOAD_TIMEOUT_MS;
|
|
130
184
|
try {
|
|
131
|
-
|
|
185
|
+
const response = target.mode === "create" ? await client.postMultipart(
|
|
186
|
+
FILES,
|
|
187
|
+
fields,
|
|
188
|
+
{ file: { filename, content, mimeType: mime } },
|
|
189
|
+
{ timeoutMs }
|
|
190
|
+
) : await client.putMultipart(
|
|
132
191
|
FILES,
|
|
133
192
|
fields,
|
|
134
193
|
{ file: { filename, content, mimeType: mime } },
|
|
135
194
|
{ timeoutMs }
|
|
136
195
|
);
|
|
196
|
+
return target.mode === "update" ? readCanonicalUpdateDocument(response, "file update") : response;
|
|
137
197
|
} catch (error) {
|
|
138
198
|
if (!shouldFallbackToChunk(error)) throw error;
|
|
139
199
|
}
|
|
140
|
-
return
|
|
141
|
-
|
|
200
|
+
return writeFileInChunks(client, {
|
|
201
|
+
target,
|
|
142
202
|
filename,
|
|
143
|
-
docName,
|
|
144
|
-
filePath: options.filePath,
|
|
145
203
|
mime,
|
|
146
204
|
content,
|
|
147
205
|
fileSize: stat.size,
|
|
148
206
|
timeoutMs
|
|
149
207
|
});
|
|
150
208
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
209
|
+
function validateLocalFile(localPath) {
|
|
210
|
+
inspectLocalFile(localPath);
|
|
211
|
+
}
|
|
212
|
+
function inspectLocalFile(localPath) {
|
|
213
|
+
const expanded = expandHome(localPath);
|
|
214
|
+
if (!existsSync(expanded)) throw new Error(`file not found: ${expanded}`);
|
|
215
|
+
const stat = statSync(expanded);
|
|
216
|
+
if (!stat.isFile()) throw new Error(`not a file: ${expanded}`);
|
|
217
|
+
try {
|
|
218
|
+
accessSync(expanded, fsConstants.R_OK);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
throw new Error(
|
|
221
|
+
`file is not readable: ${expanded}${error?.message ? ` (${error.message})` : ""}`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
return { expanded, stat };
|
|
225
|
+
}
|
|
226
|
+
async function writeFileInChunks(client, options) {
|
|
227
|
+
let expectedKnowledgeBaseId;
|
|
156
228
|
const initParams = {
|
|
157
|
-
knowledge_base_id: knowledgeBaseId,
|
|
158
229
|
file_name: options.filename,
|
|
159
|
-
name: options.docName,
|
|
160
230
|
mime_type: options.mime
|
|
161
231
|
};
|
|
162
|
-
if (options.
|
|
163
|
-
|
|
232
|
+
if (options.target.mode === "create") {
|
|
233
|
+
expectedKnowledgeBaseId = await resolveKnowledgeBaseId(
|
|
234
|
+
client,
|
|
235
|
+
options.target.kbName
|
|
236
|
+
);
|
|
237
|
+
initParams.knowledge_base_id = expectedKnowledgeBaseId;
|
|
238
|
+
initParams.name = options.target.docName ?? options.filename;
|
|
239
|
+
if (options.target.filePath) initParams.file_path = options.target.filePath;
|
|
240
|
+
} else if ("documentId" in options.target.locator) {
|
|
241
|
+
initParams.document_id = options.target.locator.documentId;
|
|
242
|
+
} else {
|
|
243
|
+
expectedKnowledgeBaseId = await resolveKnowledgeBaseId(
|
|
244
|
+
client,
|
|
245
|
+
options.target.locator.kbName
|
|
246
|
+
);
|
|
247
|
+
initParams.knowledge_base_id = expectedKnowledgeBaseId;
|
|
248
|
+
initParams.name = options.target.locator.docName;
|
|
249
|
+
if (options.target.locator.filePath) {
|
|
250
|
+
initParams.file_path = options.target.locator.filePath;
|
|
251
|
+
}
|
|
164
252
|
}
|
|
165
|
-
const initResponse = await client.postJson(
|
|
253
|
+
const initResponse = options.target.mode === "create" ? await client.postJson(
|
|
254
|
+
CHUNK_INIT,
|
|
255
|
+
{},
|
|
256
|
+
initParams,
|
|
257
|
+
{ timeoutMs: options.timeoutMs }
|
|
258
|
+
) : await client.putJson(
|
|
166
259
|
CHUNK_INIT,
|
|
167
260
|
{},
|
|
168
261
|
initParams,
|
|
169
262
|
{ timeoutMs: options.timeoutMs }
|
|
170
263
|
);
|
|
171
|
-
const
|
|
264
|
+
const normalizedInit = unwrapBareOrBox(initResponse, "chunk init");
|
|
265
|
+
const init = normalizedInit.value;
|
|
266
|
+
const initKnowledgeBaseId = readOptionalString(init, "knowledge_base_id");
|
|
267
|
+
if (!initKnowledgeBaseId) {
|
|
268
|
+
if (!(options.target.mode === "create" && normalizedInit.boxed)) {
|
|
269
|
+
throw new CtxdbError("chunk init returned invalid knowledge_base_id");
|
|
270
|
+
}
|
|
271
|
+
} else if (expectedKnowledgeBaseId && initKnowledgeBaseId !== expectedKnowledgeBaseId) {
|
|
272
|
+
throw new CtxdbError(
|
|
273
|
+
`chunk init returned mismatched knowledge_base_id ${initKnowledgeBaseId}`
|
|
274
|
+
);
|
|
275
|
+
}
|
|
172
276
|
const uploadTicket = readRequiredString(
|
|
173
277
|
init,
|
|
174
278
|
"upload_ticket",
|
|
@@ -237,19 +341,56 @@ async function uploadFileInChunks(client, options) {
|
|
|
237
341
|
headers: { "X-Upload-Ticket": uploadTicket }
|
|
238
342
|
}
|
|
239
343
|
);
|
|
240
|
-
const
|
|
344
|
+
const normalizedComplete = unwrapBareOrBox(
|
|
345
|
+
completeResponse,
|
|
346
|
+
"chunk complete"
|
|
347
|
+
);
|
|
348
|
+
const completed = normalizedComplete.value;
|
|
349
|
+
const completedKnowledgeBaseId = readOptionalString(
|
|
350
|
+
completed,
|
|
351
|
+
"knowledge_base_id"
|
|
352
|
+
);
|
|
353
|
+
if (!completedKnowledgeBaseId) {
|
|
354
|
+
if (!(options.target.mode === "create" && normalizedComplete.boxed)) {
|
|
355
|
+
throw new CtxdbError(
|
|
356
|
+
"chunk complete returned invalid knowledge_base_id"
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
} else if (initKnowledgeBaseId && completedKnowledgeBaseId !== initKnowledgeBaseId) {
|
|
360
|
+
throw new CtxdbError(
|
|
361
|
+
`chunk complete returned mismatched knowledge_base_id ${completedKnowledgeBaseId}`
|
|
362
|
+
);
|
|
363
|
+
}
|
|
241
364
|
const completedDocumentId = readRequiredString(
|
|
242
365
|
completed,
|
|
243
366
|
"document_id",
|
|
244
367
|
"chunk complete"
|
|
245
368
|
);
|
|
246
|
-
const
|
|
369
|
+
const completedStatus = readOptionalString(completed, "status");
|
|
370
|
+
if (!completedStatus && !normalizedComplete.boxed) {
|
|
371
|
+
throw new CtxdbError("chunk complete returned invalid status");
|
|
372
|
+
}
|
|
373
|
+
const reusedSucceededDocument = completedDocumentId !== documentId && completedStatus === "succeeded";
|
|
247
374
|
if (completedDocumentId !== documentId && !reusedSucceededDocument) {
|
|
248
375
|
throw new CtxdbError(
|
|
249
376
|
`chunk complete returned mismatched document_id ${completedDocumentId}`
|
|
250
377
|
);
|
|
251
378
|
}
|
|
252
|
-
|
|
379
|
+
if (options.target.mode === "update") {
|
|
380
|
+
const canonicalKnowledgeBaseId = completedKnowledgeBaseId ?? initKnowledgeBaseId;
|
|
381
|
+
if (!canonicalKnowledgeBaseId) {
|
|
382
|
+
throw new CtxdbError(
|
|
383
|
+
"chunk update returned invalid knowledge_base_id"
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
const detail = await getDocumentByKnowledgeBaseId(
|
|
387
|
+
client,
|
|
388
|
+
canonicalKnowledgeBaseId,
|
|
389
|
+
completedDocumentId
|
|
390
|
+
);
|
|
391
|
+
return readCanonicalUpdateDocument(detail, "chunk update detail");
|
|
392
|
+
}
|
|
393
|
+
return getDocument(client, options.target.kbName, completedDocumentId);
|
|
253
394
|
} catch (error) {
|
|
254
395
|
if (!completeStarted) {
|
|
255
396
|
try {
|
|
@@ -268,6 +409,10 @@ async function uploadFileInChunks(client, options) {
|
|
|
268
409
|
throw error;
|
|
269
410
|
}
|
|
270
411
|
}
|
|
412
|
+
async function resolveKnowledgeBaseId(client, kbName) {
|
|
413
|
+
const kb = await client.get(KB_DETAIL, { knowledge_base_name: kbName });
|
|
414
|
+
return readRequiredString(kb, "id", "knowledge base detail");
|
|
415
|
+
}
|
|
271
416
|
function shouldFallbackToChunk(error) {
|
|
272
417
|
if (!error || typeof error !== "object") return false;
|
|
273
418
|
const candidate = error;
|
|
@@ -303,6 +448,31 @@ function unwrapBox(response, operation) {
|
|
|
303
448
|
}
|
|
304
449
|
return box.data;
|
|
305
450
|
}
|
|
451
|
+
function unwrapBareOrBox(response, operation) {
|
|
452
|
+
if (!response || typeof response !== "object" || Array.isArray(response)) {
|
|
453
|
+
throw new CtxdbError(`${operation} returned an invalid response`);
|
|
454
|
+
}
|
|
455
|
+
const object = response;
|
|
456
|
+
if (Object.hasOwn(object, "data")) {
|
|
457
|
+
return { value: unwrapBox(response, operation), boxed: true };
|
|
458
|
+
}
|
|
459
|
+
return { value: object, boxed: false };
|
|
460
|
+
}
|
|
461
|
+
function readCanonicalUpdateDocument(response, operation) {
|
|
462
|
+
if (!response || typeof response !== "object" || Array.isArray(response)) {
|
|
463
|
+
throw new CtxdbError(`${operation} returned an invalid response`);
|
|
464
|
+
}
|
|
465
|
+
readRequiredString(response, "id", operation);
|
|
466
|
+
readRequiredString(response, "knowledge_base_id", operation);
|
|
467
|
+
return response;
|
|
468
|
+
}
|
|
469
|
+
function readOptionalString(value, field) {
|
|
470
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
471
|
+
return void 0;
|
|
472
|
+
}
|
|
473
|
+
const result = value[field];
|
|
474
|
+
return typeof result === "string" && result.trim() !== "" ? result : void 0;
|
|
475
|
+
}
|
|
306
476
|
function readRequiredString(value, field, source) {
|
|
307
477
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
308
478
|
throw new CtxdbError(`${source} returned an invalid response`);
|
|
@@ -334,6 +504,12 @@ async function getDocument(client, kbName, docId) {
|
|
|
334
504
|
document_id: docId
|
|
335
505
|
});
|
|
336
506
|
}
|
|
507
|
+
async function getDocumentByKnowledgeBaseId(client, knowledgeBaseId, docId) {
|
|
508
|
+
return client.get(DOCUMENT_DETAIL, {
|
|
509
|
+
knowledge_base_id: knowledgeBaseId,
|
|
510
|
+
document_id: docId
|
|
511
|
+
});
|
|
512
|
+
}
|
|
337
513
|
async function listDocuments(client, kbName) {
|
|
338
514
|
const resp = await client.get(DOCUMENTS, { knowledge_base_name: kbName });
|
|
339
515
|
if (Array.isArray(resp)) return resp;
|
|
@@ -344,12 +520,24 @@ async function listDocuments(client, kbName) {
|
|
|
344
520
|
return [];
|
|
345
521
|
}
|
|
346
522
|
async function pollIngest(client, kbName, docId, options = {}) {
|
|
523
|
+
return pollIngestWith(
|
|
524
|
+
() => getDocument(client, kbName, docId),
|
|
525
|
+
options
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
async function pollIngestByKnowledgeBaseId(client, knowledgeBaseId, docId, options = {}) {
|
|
529
|
+
return pollIngestWith(
|
|
530
|
+
() => getDocumentByKnowledgeBaseId(client, knowledgeBaseId, docId),
|
|
531
|
+
options
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
async function pollIngestWith(getLatest, options) {
|
|
347
535
|
const timeoutMs = options.timeoutMs ?? DEFAULT_INGEST_TIMEOUT_MS;
|
|
348
536
|
const intervalMs = options.intervalMs ?? DEFAULT_POLL_INTERVAL_MS;
|
|
349
537
|
const sleep = options.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
350
538
|
const now = options.now ?? (() => performance.now());
|
|
351
539
|
const deadline = now() + timeoutMs;
|
|
352
|
-
let doc = await
|
|
540
|
+
let doc = await getLatest();
|
|
353
541
|
let timedOut = false;
|
|
354
542
|
while (ingestInFlight(doc)) {
|
|
355
543
|
if (now() >= deadline) {
|
|
@@ -358,7 +546,7 @@ async function pollIngest(client, kbName, docId, options = {}) {
|
|
|
358
546
|
}
|
|
359
547
|
await sleep(intervalMs);
|
|
360
548
|
try {
|
|
361
|
-
doc = await
|
|
549
|
+
doc = await getLatest();
|
|
362
550
|
} catch {
|
|
363
551
|
break;
|
|
364
552
|
}
|
|
@@ -434,10 +622,14 @@ export {
|
|
|
434
622
|
listKnowledgeBases,
|
|
435
623
|
createKb,
|
|
436
624
|
uploadText,
|
|
625
|
+
updateText,
|
|
437
626
|
uploadFile,
|
|
627
|
+
updateFile,
|
|
628
|
+
validateLocalFile,
|
|
438
629
|
getDocument,
|
|
439
630
|
listDocuments,
|
|
440
631
|
pollIngest,
|
|
632
|
+
pollIngestByKnowledgeBaseId,
|
|
441
633
|
compactKbQueryResponse,
|
|
442
634
|
minimalKbQueryResponse
|
|
443
635
|
};
|
|
@@ -4,12 +4,12 @@ import {
|
|
|
4
4
|
isConnectionError,
|
|
5
5
|
resetCircuit,
|
|
6
6
|
tripCircuit
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-NJJBT52C.js";
|
|
8
8
|
import {
|
|
9
9
|
CtxdbError,
|
|
10
10
|
debug,
|
|
11
11
|
isDebug
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-AAZLOCVB.js";
|
|
13
13
|
|
|
14
14
|
// src/lib/capture-orchestrator.ts
|
|
15
15
|
import {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
define_CTXDB_DISTRIBUTION_MANIFEST_default
|
|
4
|
+
} from "./chunk-VIG4SYLU.js";
|
|
5
|
+
|
|
6
|
+
// src/lib/distribution-capabilities.ts
|
|
7
|
+
var PUBLIC_DISTRIBUTION = {
|
|
8
|
+
id: "public",
|
|
9
|
+
packageName: "@aliyunrds/ctxdb",
|
|
10
|
+
packageRegistry: null,
|
|
11
|
+
capabilities: {
|
|
12
|
+
interactiveLogin: false,
|
|
13
|
+
managedCredentials: false
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var DISTRIBUTION_MANIFEST = typeof define_CTXDB_DISTRIBUTION_MANIFEST_default === "undefined" ? PUBLIC_DISTRIBUTION : define_CTXDB_DISTRIBUTION_MANIFEST_default;
|
|
17
|
+
|
|
18
|
+
export {
|
|
19
|
+
DISTRIBUTION_MANIFEST
|
|
20
|
+
};
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
DISTRIBUTION_MANIFEST
|
|
4
|
+
} from "./chunk-R67JELM7.js";
|
|
2
5
|
|
|
3
6
|
// src/lib/self-update.ts
|
|
4
7
|
import { spawnSync } from "child_process";
|
|
5
8
|
import { fileURLToPath } from "url";
|
|
6
9
|
import { dirname } from "path";
|
|
7
10
|
import semver from "semver";
|
|
8
|
-
var PACKAGE_NAME = "@aliyunrds/ctxdb";
|
|
9
11
|
var NPM_VIEW_TIMEOUT_MS = 1e4;
|
|
10
12
|
function npmCommand(platform = process.platform) {
|
|
11
13
|
return platform === "win32" ? "npm.cmd" : "npm";
|
|
@@ -17,6 +19,9 @@ function detectInstallMethod() {
|
|
|
17
19
|
const dir = dirname(fileURLToPath(import.meta.url));
|
|
18
20
|
return dir.includes("/node_modules/") || dir.includes("\\node_modules\\") ? "npm" : "unknown";
|
|
19
21
|
}
|
|
22
|
+
function registryArgs(registry) {
|
|
23
|
+
return registry ? ["--registry", registry] : [];
|
|
24
|
+
}
|
|
20
25
|
function checkLatestVersion(currentVersion, options = {}) {
|
|
21
26
|
const current = semver.valid(currentVersion);
|
|
22
27
|
if (!current) {
|
|
@@ -29,12 +34,18 @@ function checkLatestVersion(currentVersion, options = {}) {
|
|
|
29
34
|
}
|
|
30
35
|
const platform = options.platform ?? process.platform;
|
|
31
36
|
const run = options.spawnSyncFn ?? spawnSync;
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
const packageName = options.packageName ?? DISTRIBUTION_MANIFEST.packageName;
|
|
38
|
+
const registry = options.registry === void 0 ? DISTRIBUTION_MANIFEST.packageRegistry : options.registry;
|
|
39
|
+
const result = run(
|
|
40
|
+
npmCommand(platform),
|
|
41
|
+
["view", packageName, "version", ...registryArgs(registry)],
|
|
42
|
+
{
|
|
43
|
+
encoding: "utf-8",
|
|
44
|
+
timeout: options.timeoutMs ?? NPM_VIEW_TIMEOUT_MS,
|
|
45
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
46
|
+
shell: platform === "win32"
|
|
47
|
+
}
|
|
48
|
+
);
|
|
38
49
|
if (result.status !== 0 || !result.stdout?.trim()) {
|
|
39
50
|
return {
|
|
40
51
|
latest: null,
|
|
@@ -82,7 +93,9 @@ function runSelfUpdate(currentVersion, passthroughArgs = [], options = {}) {
|
|
|
82
93
|
error: "ctxdb was not installed via npm. Update manually with your package manager."
|
|
83
94
|
};
|
|
84
95
|
}
|
|
85
|
-
const
|
|
96
|
+
const packageName = options.packageName ?? DISTRIBUTION_MANIFEST.packageName;
|
|
97
|
+
const registry = options.registry === void 0 ? DISTRIBUTION_MANIFEST.packageRegistry : options.registry;
|
|
98
|
+
const check = checkLatestVersion(currentVersion, { packageName, registry });
|
|
86
99
|
if (check.error) {
|
|
87
100
|
return {
|
|
88
101
|
ok: false,
|
|
@@ -98,13 +111,17 @@ function runSelfUpdate(currentVersion, passthroughArgs = [], options = {}) {
|
|
|
98
111
|
);
|
|
99
112
|
return { ok: true, updated: false, fromVersion: currentVersion };
|
|
100
113
|
}
|
|
101
|
-
process.stderr.write(`ctxdb: updating ${
|
|
114
|
+
process.stderr.write(`ctxdb: updating ${packageName} v${currentVersion} \u2192 v${check.latest}...
|
|
102
115
|
`);
|
|
103
|
-
const install = spawnSync(
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
116
|
+
const install = spawnSync(
|
|
117
|
+
npmCommand(),
|
|
118
|
+
["install", "-g", `${packageName}@${check.latest}`, ...registryArgs(registry)],
|
|
119
|
+
{
|
|
120
|
+
stdio: "inherit",
|
|
121
|
+
encoding: "utf-8",
|
|
122
|
+
shell: process.platform === "win32"
|
|
123
|
+
}
|
|
124
|
+
);
|
|
108
125
|
if (install.status !== 0) {
|
|
109
126
|
return {
|
|
110
127
|
ok: false,
|
|
@@ -153,10 +170,11 @@ var NOTIFICATION_THROTTLE_MS = 3 * SUCCESS_TTL_MS;
|
|
|
153
170
|
var LOCK_STALE_AFTER_MS = 10 * 60 * 1e3;
|
|
154
171
|
function versionCheckPaths(home = homedir()) {
|
|
155
172
|
const cacheDir = join(home, ".ctxdb", "cache");
|
|
173
|
+
const distributionSuffix = DISTRIBUTION_MANIFEST.id === "public" ? "" : `-${DISTRIBUTION_MANIFEST.id}`;
|
|
156
174
|
return {
|
|
157
175
|
cacheDir,
|
|
158
|
-
statePath: join(cacheDir,
|
|
159
|
-
lockPath: join(cacheDir,
|
|
176
|
+
statePath: join(cacheDir, `version-check${distributionSuffix}.json`),
|
|
177
|
+
lockPath: join(cacheDir, `version-check${distributionSuffix}.lock`)
|
|
160
178
|
};
|
|
161
179
|
}
|
|
162
180
|
function createInitialVersionCheckState(currentVersion) {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// <define:__CTXDB_DISTRIBUTION_MANIFEST__>
|
|
4
|
+
var define_CTXDB_DISTRIBUTION_MANIFEST_default = { id: "public", packageName: "@aliyunrds/ctxdb", packageRegistry: null, capabilities: { interactiveLogin: false, managedCredentials: false } };
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
define_CTXDB_DISTRIBUTION_MANIFEST_default
|
|
8
|
+
};
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
listKnowledgeBases
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-CAXRYH6E.js";
|
|
5
5
|
import {
|
|
6
6
|
isConnectionError,
|
|
7
7
|
resetCircuit,
|
|
8
8
|
tripCircuit
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-NJJBT52C.js";
|
|
10
10
|
import {
|
|
11
11
|
CtxdbError
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-AAZLOCVB.js";
|
|
13
13
|
|
|
14
14
|
// src/lib/kb-catalog.ts
|
|
15
15
|
function sanitizeKeyEntities(raw) {
|