@aivax/cli 0.0.0-stage → 1.1.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/LICENSE +21 -0
- package/README.md +213 -2
- package/package.json +41 -5
- package/src/args.js +84 -0
- package/src/cli.js +124 -0
- package/src/commands/account.js +132 -0
- package/src/commands/analytics.js +30 -0
- package/src/commands/api.js +37 -0
- package/src/commands/auth.js +142 -0
- package/src/commands/batch.js +191 -0
- package/src/commands/chat-clients.js +157 -0
- package/src/commands/conversations.js +92 -0
- package/src/commands/crud.js +68 -0
- package/src/commands/docs.js +91 -0
- package/src/commands/gateways.js +85 -0
- package/src/commands/generate.js +197 -0
- package/src/commands/logs.js +41 -0
- package/src/commands/models.js +38 -0
- package/src/commands/ocr.js +370 -0
- package/src/commands/rag.js +452 -0
- package/src/commands/shell.js +46 -0
- package/src/commands/skills.js +48 -0
- package/src/commands/tests.js +429 -0
- package/src/commands/web.js +68 -0
- package/src/errors.js +19 -0
- package/src/http.js +175 -0
- package/src/input.js +120 -0
- package/src/pdf.js +209 -0
- package/src/profiles.js +67 -0
- package/src/prompt.js +67 -0
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
import { basename, extname } from "node:path";
|
|
2
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
3
|
+
import { UsageError } from "../errors.js";
|
|
4
|
+
import { call, fetchData, print, seg, VERSION } from "../http.js";
|
|
5
|
+
import { resolveSession } from "../profiles.js";
|
|
6
|
+
import { dataOption, openFile, payload, readJson, readSource } from "../input.js";
|
|
7
|
+
import { confirm } from "../prompt.js";
|
|
8
|
+
import { crud, idOption } from "./crud.js";
|
|
9
|
+
|
|
10
|
+
const MEDIA_CHUNK_SIZE = 50 * 1024 * 1024;
|
|
11
|
+
const IMPORT_FRAME_BYTES = 256 * 1024;
|
|
12
|
+
const IMPORT_BUFFER_LIMIT = 1024 * 1024;
|
|
13
|
+
|
|
14
|
+
const collection = { alias: "c", value: "<id>", required: true, description: "Collection identifier." };
|
|
15
|
+
|
|
16
|
+
const collectionPath = ctx => `/api/v1/collections/${seg(ctx.options.collection)}`;
|
|
17
|
+
|
|
18
|
+
const queryOptions = {
|
|
19
|
+
filter: { value: "<expr>", list: true, description: "Document filter, e.g. 'tags has \"faq\"'. Repeatable (combined with and)." },
|
|
20
|
+
minScore: { value: "<n>", number: true, description: "Minimum similarity from 0 to 0.99." },
|
|
21
|
+
sort: { value: "<field[:dir]>", description: "Order by score, createdAt, updatedAt, indexedAt or name, e.g. createdAt:desc." },
|
|
22
|
+
limit: { alias: "k", value: "<n>", number: true, description: "Results per page, from 1 to 128." },
|
|
23
|
+
cursor: { value: "<cursor>", description: "page.cursor from a previous response." },
|
|
24
|
+
include: { value: "<fields>", description: "Comma-separated fields: content, metadata, references." }
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function queryBody(ctx, extra) {
|
|
28
|
+
const { options } = ctx;
|
|
29
|
+
const [by, dir] = options.sort?.split(":") ?? [];
|
|
30
|
+
|
|
31
|
+
return payload(ctx, {
|
|
32
|
+
...extra,
|
|
33
|
+
filter: options.filter,
|
|
34
|
+
minScore: options.minScore,
|
|
35
|
+
sort: by ? { by, dir } : undefined,
|
|
36
|
+
limit: options.limit,
|
|
37
|
+
cursor: options.cursor,
|
|
38
|
+
include: options.include?.split(",").map(field => field.trim())
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const search = {
|
|
43
|
+
aliases: ["query"],
|
|
44
|
+
description: "Search one or more collections by meaning.",
|
|
45
|
+
options: {
|
|
46
|
+
collection: { ...collection, list: true, description: "Collection identifier. Repeatable." },
|
|
47
|
+
query: { alias: "q", value: "<text>", list: true, required: true, description: "Search text. Repeatable; each document scores by its best match." },
|
|
48
|
+
reranker: { value: "<name>", description: "smart (default), lexical, rrf, none or a full reranker name." },
|
|
49
|
+
candidates: { value: "<n>", number: true, description: "Candidate pool size reranked before paging." },
|
|
50
|
+
...queryOptions,
|
|
51
|
+
data: dataOption()
|
|
52
|
+
},
|
|
53
|
+
async run(ctx) {
|
|
54
|
+
const body = await queryBody(ctx, {
|
|
55
|
+
collections: ctx.options.collection,
|
|
56
|
+
terms: ctx.options.query,
|
|
57
|
+
reranker: ctx.options.reranker,
|
|
58
|
+
candidates: ctx.options.candidates
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
await call(ctx, "POST", "/api/v2/query", { body });
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const similar = {
|
|
66
|
+
description: "Find documents similar to an indexed document.",
|
|
67
|
+
options: {
|
|
68
|
+
document: { value: "<id>", required: true, description: "Source document identifier." },
|
|
69
|
+
collection: { ...collection, required: false, list: true, description: "Collections to search. Defaults to the source collection." },
|
|
70
|
+
...queryOptions,
|
|
71
|
+
data: dataOption()
|
|
72
|
+
},
|
|
73
|
+
async run(ctx) {
|
|
74
|
+
const body = await queryBody(ctx, { documentId: ctx.options.document, collections: ctx.options.collection });
|
|
75
|
+
await call(ctx, "POST", "/api/v2/query/similar", { body });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const answer = {
|
|
80
|
+
description: "Generate an answer grounded on collection documents.",
|
|
81
|
+
options: {
|
|
82
|
+
collection: { ...collection, list: true, description: "Collection identifier. Repeatable." },
|
|
83
|
+
query: { alias: "q", value: "<text>", list: true, required: true, description: "Question or search text." },
|
|
84
|
+
top: { alias: "k", value: "<n>", number: true, description: "Maximum number of documents used." },
|
|
85
|
+
minScore: { value: "<n>", number: true, description: "Minimum similarity score." },
|
|
86
|
+
reranker: { value: "<name>", description: "Reranker identifier." },
|
|
87
|
+
filter: queryOptions.filter,
|
|
88
|
+
references: { description: "Include referenced document chunks." },
|
|
89
|
+
data: dataOption()
|
|
90
|
+
},
|
|
91
|
+
async run(ctx) {
|
|
92
|
+
const { options } = ctx;
|
|
93
|
+
const body = await payload(ctx, {
|
|
94
|
+
collections: options.collection,
|
|
95
|
+
terms: options.query,
|
|
96
|
+
top: options.top,
|
|
97
|
+
minScore: options.minScore,
|
|
98
|
+
reranker: options.reranker,
|
|
99
|
+
filter: options.filter,
|
|
100
|
+
includeReferences: options.references
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
await call(ctx, "POST", "/api/v1/answer", { body });
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const documentId = { value: "<id>", required: true, description: "Document identifier." };
|
|
108
|
+
|
|
109
|
+
const documents = {
|
|
110
|
+
aliases: ["docs"],
|
|
111
|
+
description: "Collection document commands.",
|
|
112
|
+
commands: {
|
|
113
|
+
list: {
|
|
114
|
+
aliases: ["ls", "browse"],
|
|
115
|
+
description: "List indexed documents of one or more collections.",
|
|
116
|
+
options: {
|
|
117
|
+
collection: { ...collection, list: true, description: "Collection identifier. Repeatable." },
|
|
118
|
+
...queryOptions,
|
|
119
|
+
data: dataOption()
|
|
120
|
+
},
|
|
121
|
+
async run(ctx) {
|
|
122
|
+
await call(ctx, "POST", "/api/v2/query/browse", { body: await queryBody(ctx, { collections: ctx.options.collection }) });
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
get: {
|
|
126
|
+
description: "Show one document.",
|
|
127
|
+
options: { collection, id: documentId },
|
|
128
|
+
run: ctx => call(ctx, "GET", `${collectionPath(ctx)}/documents/${seg(ctx.options.id)}`)
|
|
129
|
+
},
|
|
130
|
+
upsert: {
|
|
131
|
+
aliases: ["put"],
|
|
132
|
+
description: "Create or update a document matched by name.",
|
|
133
|
+
options: {
|
|
134
|
+
collection,
|
|
135
|
+
name: { alias: "n", value: "<name>", description: "Document name (required by the API)." },
|
|
136
|
+
contents: { value: "<text>", description: "Document contents. Use @file or - to read them." },
|
|
137
|
+
file: { alias: "f", value: "<file>", description: "Text file used as contents. Name defaults to the file name." },
|
|
138
|
+
reference: { value: "<text>", description: "Optional reference." },
|
|
139
|
+
tag: { alias: "t", value: "<tag>", list: true, description: "Document tag. Repeatable." },
|
|
140
|
+
metadata: { value: "<json>", description: "Metadata object (JSON, @file or -)." },
|
|
141
|
+
data: dataOption()
|
|
142
|
+
},
|
|
143
|
+
async run(ctx) {
|
|
144
|
+
const { options } = ctx;
|
|
145
|
+
const body = await payload(ctx, {
|
|
146
|
+
name: options.name ?? (options.file && basename(options.file)),
|
|
147
|
+
contents: options.file ? await (await openFile(options.file)).text() : options.contents && await readSource(options.contents),
|
|
148
|
+
reference: options.reference,
|
|
149
|
+
tags: options.tag,
|
|
150
|
+
metadata: options.metadata && await readJson(options.metadata, "--metadata")
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
await call(ctx, "PUT", `${collectionPath(ctx)}/documents`, { body });
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
delete: {
|
|
157
|
+
aliases: ["rm"],
|
|
158
|
+
description: "Delete a document.",
|
|
159
|
+
options: { collection, id: documentId },
|
|
160
|
+
async run(ctx) {
|
|
161
|
+
await confirm(ctx, `Delete document ${ctx.options.id}?`);
|
|
162
|
+
await call(ctx, "DELETE", `${collectionPath(ctx)}/documents/${seg(ctx.options.id)}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
async function importMedia(ctx, path) {
|
|
169
|
+
const file = await openFile(path);
|
|
170
|
+
const name = basename(path);
|
|
171
|
+
const headers = {
|
|
172
|
+
"X-Import-Media-File-Name": encodeURIComponent(name.normalize("NFC")),
|
|
173
|
+
"X-Import-Media-File-Name-Encoding": "percent",
|
|
174
|
+
"X-Import-Media-Upload-Id": crypto.randomUUID()
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const chunks = Math.max(1, Math.ceil(file.size / MEDIA_CHUNK_SIZE));
|
|
178
|
+
|
|
179
|
+
for (let index = 0; index < chunks; index++) {
|
|
180
|
+
process.stderr.write(`Uploading ${name}: chunk ${index + 1}/${chunks}\n`);
|
|
181
|
+
|
|
182
|
+
const chunk = file.slice(index * MEDIA_CHUNK_SIZE, (index + 1) * MEDIA_CHUNK_SIZE);
|
|
183
|
+
await fetchData(ctx, "POST", `${collectionPath(ctx)}/documents/media/chunk`, { headers, body: chunk });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
await fetchData(ctx, "POST", `${collectionPath(ctx)}/documents/media/complete`, { headers, body: { context: ctx.options.context ?? "" } });
|
|
187
|
+
process.stderr.write(`Queued ${name} for processing.\n`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function* jsonlLines(path) {
|
|
191
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
192
|
+
let buffer = "";
|
|
193
|
+
let number = 0;
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
for await (const chunk of (await openFile(path)).stream()) {
|
|
197
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
198
|
+
|
|
199
|
+
let newline;
|
|
200
|
+
while ((newline = buffer.indexOf("\n")) >= 0) {
|
|
201
|
+
const line = buffer.slice(0, newline).replace(/\r$/, "");
|
|
202
|
+
buffer = buffer.slice(newline + 1);
|
|
203
|
+
number++;
|
|
204
|
+
|
|
205
|
+
if (line.trim()) yield { line, number };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
buffer += decoder.decode();
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
throw error instanceof TypeError ? new UsageError(`${path}: the file is not valid UTF-8.`) : error;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (buffer.trim()) yield { line: buffer.replace(/\r$/, ""), number: number + 1 };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function lineProblem(line) {
|
|
219
|
+
let record;
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
record = JSON.parse(line);
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
return `invalid JSON (${error.message})`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (!record || typeof record !== "object" || Array.isArray(record)) {
|
|
229
|
+
return "each line must be a JSON object";
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const docId = record.docId ?? record.docid ?? record.DocId;
|
|
233
|
+
const text = record.text ?? record.Text;
|
|
234
|
+
|
|
235
|
+
return typeof docId === "string" && docId.trim() && typeof text === "string" ? null : "each line must contain docId and text";
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function importJsonl(ctx, files) {
|
|
239
|
+
const { token, endpoint } = await resolveSession(ctx);
|
|
240
|
+
const url = new URL(`/api/v2/collections/${seg(ctx.options.collection)}/documents/index-jsonl`, endpoint);
|
|
241
|
+
url.protocol = url.protocol === "http:" ? "ws:" : "wss:";
|
|
242
|
+
if (ctx.options.sync) url.searchParams.set("insert-mode", "sync");
|
|
243
|
+
|
|
244
|
+
const socket = new WebSocket(url, { headers: { Authorization: `Bearer ${token}`, "User-Agent": `aivax-cli/${VERSION}` } });
|
|
245
|
+
let terminal = null;
|
|
246
|
+
let failure = null;
|
|
247
|
+
let sentFrames = 0;
|
|
248
|
+
|
|
249
|
+
const closed = new Promise(resolve => socket.addEventListener("close", event => {
|
|
250
|
+
if (!terminal) failure ??= new Error(`The import connection closed before completion (${event.code}${event.reason ? `: ${event.reason}` : ""}).`);
|
|
251
|
+
resolve();
|
|
252
|
+
}));
|
|
253
|
+
|
|
254
|
+
socket.addEventListener("message", event => {
|
|
255
|
+
let message;
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
message = JSON.parse(String(event.data));
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
failure = new Error("The server sent a message that is not JSON.");
|
|
262
|
+
socket.close();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (message.type === "progress") {
|
|
267
|
+
process.stderr.write(`Committed ${message.committed ?? 0} lines (updated ${message.updated ?? 0}, skipped ${message.skipped ?? 0})\n`);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (message.type === "completed" || message.type === "error") terminal = message;
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const stopped = () => terminal || failure || socket.readyState !== WebSocket.OPEN;
|
|
274
|
+
|
|
275
|
+
try {
|
|
276
|
+
await new Promise((resolve, reject) => {
|
|
277
|
+
socket.addEventListener("open", resolve, { once: true });
|
|
278
|
+
socket.addEventListener("error", () => reject(new Error("The import connection failed. Check the profile, the endpoint and the collection id.")), { once: true });
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
let frame = [];
|
|
282
|
+
let bytes = 0;
|
|
283
|
+
|
|
284
|
+
const flush = async () => {
|
|
285
|
+
if (!frame.length) return;
|
|
286
|
+
|
|
287
|
+
while (socket.readyState === WebSocket.OPEN && socket.bufferedAmount > IMPORT_BUFFER_LIMIT) await sleep(20);
|
|
288
|
+
if (stopped()) return;
|
|
289
|
+
|
|
290
|
+
socket.send(`${frame.join("\n")}\n`);
|
|
291
|
+
sentFrames++;
|
|
292
|
+
frame = [];
|
|
293
|
+
bytes = 0;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
for (const path of files) {
|
|
297
|
+
process.stderr.write(`Sending ${basename(path)}...\n`);
|
|
298
|
+
|
|
299
|
+
for await (const { line, number } of jsonlLines(path)) {
|
|
300
|
+
if (stopped()) break;
|
|
301
|
+
|
|
302
|
+
const problem = lineProblem(line);
|
|
303
|
+
if (problem) {
|
|
304
|
+
const kept = sentFrames ? " The import was aborted without END: lines already committed were kept and sync deletion did not run." : "";
|
|
305
|
+
throw new UsageError(`${path}:${number}: ${problem}.${kept}`);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
frame.push(line);
|
|
309
|
+
bytes += Buffer.byteLength(line) + 1;
|
|
310
|
+
if (bytes >= IMPORT_FRAME_BYTES) await flush();
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
await flush();
|
|
315
|
+
if (!stopped()) socket.send("END");
|
|
316
|
+
|
|
317
|
+
await Promise.race([closed, new Promise(resolve => {
|
|
318
|
+
const timer = setInterval(() => terminal && (clearInterval(timer), resolve()), 15);
|
|
319
|
+
})]);
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
if (socket.readyState <= WebSocket.OPEN) socket.close();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (terminal?.type === "completed") {
|
|
326
|
+
const { received = 0, updated = 0, skipped = 0, removed = 0 } = terminal;
|
|
327
|
+
print({ received, updated, skipped, removed });
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (terminal?.type === "error") {
|
|
332
|
+
const where = terminal.line == null ? "" : ` at line ${terminal.line}`;
|
|
333
|
+
const kept = terminal.committed == null ? "" : ` ${terminal.committed} lines were already committed.`;
|
|
334
|
+
throw new Error(`${terminal.message ?? "Import failed"}${where}.${kept}`);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
throw failure ?? new Error("The import ended without a completion message.");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const importCommand = {
|
|
341
|
+
description: "Import documents from JSONL files, or media files (PDF, DOCX, images, etc.) processed into documents.",
|
|
342
|
+
options: {
|
|
343
|
+
collection,
|
|
344
|
+
file: { alias: "f", value: "<file>", list: true, required: true, description: "JSONL or media file. Repeatable. All JSONL files are sent as one import." },
|
|
345
|
+
sync: { description: "JSONL only: delete documents whose names are not present in the uploaded JSONL files." },
|
|
346
|
+
context: { value: "<text>", description: "Media only: context used while processing the file." }
|
|
347
|
+
},
|
|
348
|
+
async run(ctx) {
|
|
349
|
+
const isJsonl = path => [".jsonl", ".ndjson"].includes(extname(path).toLowerCase());
|
|
350
|
+
const jsonl = ctx.options.file.filter(isJsonl);
|
|
351
|
+
|
|
352
|
+
if (jsonl.length) {
|
|
353
|
+
if (ctx.options.sync) {
|
|
354
|
+
await confirm(ctx, `Sync mode deletes collection documents missing from ${jsonl.map(path => basename(path)).join(", ")}. Continue?`);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
await importJsonl(ctx, jsonl);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
for (const path of ctx.options.file.filter(path => !isJsonl(path))) {
|
|
361
|
+
await importMedia(ctx, path);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
const id = { id: idOption("Collection") };
|
|
367
|
+
|
|
368
|
+
const destructive = (description, method, suffix, message) => ({
|
|
369
|
+
description,
|
|
370
|
+
options: id,
|
|
371
|
+
async run(ctx) {
|
|
372
|
+
await confirm(ctx, message(ctx.options.id));
|
|
373
|
+
await call(ctx, method, `/api/v1/collections/${seg(ctx.options.id)}${suffix}`);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
const collectionFields = {
|
|
378
|
+
name: { alias: "n", key: "collectionName", value: "<name>", description: "Collection name." },
|
|
379
|
+
context: { value: "<text>", source: true, description: "Collection context. Use @file or - to read it." },
|
|
380
|
+
tags: { value: "<tags>", description: "Contextual tags." }
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const transactions = {
|
|
384
|
+
description: "List RAG transactions of a collection.",
|
|
385
|
+
options: {
|
|
386
|
+
...id,
|
|
387
|
+
view: { value: "<view>", description: "recent, low-quality or high-quality." },
|
|
388
|
+
transaction: { value: "<id>", description: "Show one transaction instead of the list." },
|
|
389
|
+
export: { description: "Export the transactions as JSONL." },
|
|
390
|
+
output: { alias: "o", value: "<file>", description: "Write the export to a file." }
|
|
391
|
+
},
|
|
392
|
+
run(ctx) {
|
|
393
|
+
const base = `/api/v1/collections/${seg(ctx.options.id)}/transactions`;
|
|
394
|
+
const path = ctx.options.transaction ? `${base}/${seg(ctx.options.transaction)}` : ctx.options.export ? `${base}/export/jsonl` : base;
|
|
395
|
+
|
|
396
|
+
return call(ctx, "GET", path, { query: { view: ctx.options.view } });
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const dedup = {
|
|
401
|
+
aliases: ["deduplicate"],
|
|
402
|
+
description: "Estimate or start the removal of duplicate documents.",
|
|
403
|
+
options: {
|
|
404
|
+
...id,
|
|
405
|
+
delta: { value: "<level>", description: "Sensitivity: low, medium (default) or high." },
|
|
406
|
+
start: { description: "Start the de-duplication. Without it, only the cost estimate is shown." }
|
|
407
|
+
},
|
|
408
|
+
async run(ctx) {
|
|
409
|
+
const base = `/api/v1/collections/${seg(ctx.options.id)}/deduplication`;
|
|
410
|
+
const query = { delta: ctx.options.delta };
|
|
411
|
+
|
|
412
|
+
if (!ctx.options.start) {
|
|
413
|
+
await call(ctx, "GET", `${base}/estimate`, { query });
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
await confirm(ctx, `Remove duplicate documents from collection ${ctx.options.id}?`);
|
|
418
|
+
await call(ctx, "POST", base, { query });
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
const { list, get, create, patch, delete: remove } = crud({ base: "/api/v1/collections", noun: "collection", updateMethod: "PUT", fields: collectionFields });
|
|
423
|
+
|
|
424
|
+
export default {
|
|
425
|
+
description: "RAG-specific commands.",
|
|
426
|
+
commands: {
|
|
427
|
+
search,
|
|
428
|
+
similar,
|
|
429
|
+
answer,
|
|
430
|
+
collections: {
|
|
431
|
+
description: "Collection management commands.",
|
|
432
|
+
commands: {
|
|
433
|
+
list: { ...list, options: {} },
|
|
434
|
+
get,
|
|
435
|
+
create,
|
|
436
|
+
patch,
|
|
437
|
+
delete: remove,
|
|
438
|
+
export: {
|
|
439
|
+
description: "Export all documents as JSONL.",
|
|
440
|
+
options: { ...id, output: { alias: "o", value: "<file>", description: "Write the export to a file." } },
|
|
441
|
+
run: ctx => call(ctx, "GET", `/api/v1/collections/${seg(ctx.options.id)}/export.jsonl`, { agentOutput: false })
|
|
442
|
+
},
|
|
443
|
+
reset: destructive("Delete every document of a collection.", "DELETE", "/reset-only", id => `Delete every document of collection ${id}?`),
|
|
444
|
+
reindex: destructive("Rebuild the vectors of a collection.", "DELETE", "/vectors-only", id => `Rebuild all vectors of collection ${id}? This is billed again.`),
|
|
445
|
+
transactions,
|
|
446
|
+
dedup
|
|
447
|
+
}
|
|
448
|
+
},
|
|
449
|
+
documents,
|
|
450
|
+
import: importCommand
|
|
451
|
+
}
|
|
452
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { call } from "../http.js";
|
|
2
|
+
import { openFile } from "../input.js";
|
|
3
|
+
import { confirm } from "../prompt.js";
|
|
4
|
+
|
|
5
|
+
const user = { alias: "u", value: "<id>", required: true, description: "Shell user identifier (X-Shell-User-Id)." };
|
|
6
|
+
|
|
7
|
+
const path = { alias: "p", value: "<path>", required: true, description: "Path inside the shell sandbox." };
|
|
8
|
+
|
|
9
|
+
const shellCall = (ctx, method, route, options = {}) => call(ctx, method, `/api/v1/shell/io${route}`, {
|
|
10
|
+
...options,
|
|
11
|
+
query: { path: ctx.options.path },
|
|
12
|
+
headers: { "X-Shell-User-Id": ctx.options.user }
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const destructive = (noun, route) => ({
|
|
16
|
+
description: `Delete a ${noun}.`,
|
|
17
|
+
options: { user, path },
|
|
18
|
+
async run(ctx) {
|
|
19
|
+
await confirm(ctx, `Delete ${noun} ${ctx.options.path}?`);
|
|
20
|
+
await shellCall(ctx, "DELETE", route);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export default {
|
|
25
|
+
description: "Shell sandbox file commands.",
|
|
26
|
+
commands: {
|
|
27
|
+
ls: { description: "List a directory.", options: { user, path }, run: ctx => shellCall(ctx, "GET", "/listing") },
|
|
28
|
+
stat: { description: "Show file details.", options: { user, path }, run: ctx => shellCall(ctx, "GET", "/file/details") },
|
|
29
|
+
download: {
|
|
30
|
+
aliases: ["get"],
|
|
31
|
+
description: "Download a file.",
|
|
32
|
+
options: { user, path, output: { alias: "o", value: "<file>", description: "Local output file." } },
|
|
33
|
+
run: ctx => shellCall(ctx, "GET", "/file", { agentOutput: false })
|
|
34
|
+
},
|
|
35
|
+
upload: {
|
|
36
|
+
aliases: ["put"],
|
|
37
|
+
description: "Upload a local file.",
|
|
38
|
+
options: { user, path, file: { alias: "f", value: "<file>", required: true, description: "Local file to upload." } },
|
|
39
|
+
run: async ctx => shellCall(ctx, "POST", "/file", { body: await openFile(ctx.options.file) })
|
|
40
|
+
},
|
|
41
|
+
publish: { description: "Get a temporary public URL for a file.", options: { user, path }, run: ctx => shellCall(ctx, "GET", "/file/public-address") },
|
|
42
|
+
mkdir: { description: "Create a directory.", options: { user, path }, run: ctx => shellCall(ctx, "POST", "/directory") },
|
|
43
|
+
rm: destructive("file", "/file"),
|
|
44
|
+
rmdir: destructive("directory", "/directory")
|
|
45
|
+
}
|
|
46
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import { call } from "../http.js";
|
|
3
|
+
import { contentPart, openFile } from "../input.js";
|
|
4
|
+
import { confirm } from "../prompt.js";
|
|
5
|
+
import { crud } from "./crud.js";
|
|
6
|
+
|
|
7
|
+
const fields = {
|
|
8
|
+
slug: { value: "<slug>", description: "Skill slug." },
|
|
9
|
+
description: { value: "<text>", description: "Skill description." },
|
|
10
|
+
instructions: { alias: "i", value: "<text>", source: true, description: "Skill instructions. Use @file or - to read them." }
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
description: "Skills management commands.",
|
|
15
|
+
commands: {
|
|
16
|
+
...crud({ base: "/api/v1/skills", noun: "skill", updateMethod: "PUT", fields }),
|
|
17
|
+
import: {
|
|
18
|
+
description: "Import skills from a JSONL file. Existing skills with the same slug are overwritten.",
|
|
19
|
+
options: { file: { alias: "f", value: "<file>", required: true, description: "JSONL file with one skill per line." } },
|
|
20
|
+
async run(ctx) {
|
|
21
|
+
const form = new FormData();
|
|
22
|
+
form.append("skills", await openFile(ctx.options.file), basename(ctx.options.file));
|
|
23
|
+
|
|
24
|
+
await call(ctx, "POST", "/api/v1/skills/management/import", { body: form });
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
export: {
|
|
28
|
+
description: "Export every skill as JSONL.",
|
|
29
|
+
options: { output: { alias: "o", value: "<file>", description: "Write the export to a file." } },
|
|
30
|
+
run: ctx => call(ctx, "GET", "/api/v1/skills/management/export.jsonl", { agentOutput: false })
|
|
31
|
+
},
|
|
32
|
+
clear: {
|
|
33
|
+
description: "Delete every skill of the account.",
|
|
34
|
+
async run(ctx) {
|
|
35
|
+
await confirm(ctx, "Delete every skill of the account?");
|
|
36
|
+
await call(ctx, "DELETE", "/api/v1/skills/management/clear");
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
teach: {
|
|
40
|
+
description: "Generate skill instructions from tutorial videos.",
|
|
41
|
+
options: { file: { alias: "f", value: "<file|url>", list: true, required: true, description: "Tutorial video file or URL. Repeatable." } },
|
|
42
|
+
async run(ctx) {
|
|
43
|
+
const videos = await Promise.all(ctx.options.file.map(contentPart));
|
|
44
|
+
await call(ctx, "POST", "/api/v1/generations/teach-skill", { body: { videos } });
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|