@votrix-ai/cli 0.0.0-stage → 0.2.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.md +189 -2
- package/SITE-TRACKING-CLI-DESIGN.md +117 -0
- package/dist/src/agent.js +366 -0
- package/dist/src/auth.js +197 -0
- package/dist/src/cli.js +148 -0
- package/dist/src/commands.js +117 -0
- package/dist/src/core.js +373 -0
- package/dist/src/integrations.js +143 -0
- package/dist/src/site.js +278 -0
- package/dist/src/tools.js +267 -0
- package/dist/src/zip.js +112 -0
- package/docs/API.md +95 -0
- package/docs/cli/account.md +76 -0
- package/docs/cli/agent.md +105 -0
- package/docs/cli/files.md +65 -0
- package/docs/cli/integrations.md +67 -0
- package/docs/cli/media.md +45 -0
- package/docs/cli/runs.md +44 -0
- package/docs/cli/scraper.md +45 -0
- package/docs/cli/seo.md +45 -0
- package/docs/cli/site.md +137 -0
- package/docs/cli/social.md +85 -0
- package/docs/cli/ui.md +45 -0
- package/package.json +30 -4
- package/production.json +5 -0
- package/skills/votrix/SKILL.md +97 -0
- package/skills/votrix/agents/openai.yaml +4 -0
package/dist/src/site.js
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
// `votrix tools site`: the workspace's websites — deploy, versions, status,
|
|
2
|
+
// environment variables, custom domains. One JSON object on stdout.
|
|
3
|
+
//
|
|
4
|
+
// A deploy packs a folder (or takes a zip already in the workspace), uploads it,
|
|
5
|
+
// and the backend deploys it as the site's next version. By default the command
|
|
6
|
+
// waits until the site answers on its address.
|
|
7
|
+
import { readdirSync, readFileSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
import { join, relative } from "node:path";
|
|
10
|
+
import { emit, note, readStdin, sleep, usage, uuidOf, VotrixError } from "./core.js";
|
|
11
|
+
import { zipFolder } from "./zip.js";
|
|
12
|
+
const WAIT_MS = 10 * 60_000;
|
|
13
|
+
const POLL_MS = Number(process.env.VOTRIX_SITE_POLL_MS) || 4000;
|
|
14
|
+
// Never part of a site: tooling, VCS, OS junk, and local secrets.
|
|
15
|
+
const SKIP_DIRS = new Set(["node_modules", ".git", ".svn", ".hg", "__MACOSX", ".vercel", ".votrix", ".cache"]);
|
|
16
|
+
const SKIP_FILES = new Set([".DS_Store", "Thumbs.db", "desktop.ini"]);
|
|
17
|
+
const isSecretFile = (name) => name === ".env" || name.startsWith(".env.");
|
|
18
|
+
// --- packing ------------------------------------------------------------------
|
|
19
|
+
export function siteFiles(root) {
|
|
20
|
+
const out = [];
|
|
21
|
+
const walk = (dir) => {
|
|
22
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
23
|
+
const path = join(dir, entry.name);
|
|
24
|
+
if (entry.isDirectory()) {
|
|
25
|
+
if (!SKIP_DIRS.has(entry.name))
|
|
26
|
+
walk(path);
|
|
27
|
+
}
|
|
28
|
+
else if (entry.isFile() && !SKIP_FILES.has(entry.name) && !isSecretFile(entry.name))
|
|
29
|
+
out.push(path);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
walk(root);
|
|
33
|
+
return out.sort();
|
|
34
|
+
}
|
|
35
|
+
// The folder's zip in a temp file; the caller removes it.
|
|
36
|
+
function packFolder(dir, name) {
|
|
37
|
+
const info = statSync(dir, { throwIfNoEntry: false });
|
|
38
|
+
if (!info)
|
|
39
|
+
throw usage(`Not found: ${dir}`);
|
|
40
|
+
if (info.isFile()) {
|
|
41
|
+
if (!dir.toLowerCase().endsWith(".zip"))
|
|
42
|
+
throw usage(`${dir} is a file; pass the site's folder or a .zip.`);
|
|
43
|
+
return { path: dir, count: -1 };
|
|
44
|
+
}
|
|
45
|
+
const files = siteFiles(dir);
|
|
46
|
+
if (!files.some((f) => relative(dir, f) === "index.html")) {
|
|
47
|
+
throw usage(`${dir} has no index.html at its top level. Pass the folder that holds index.html.`);
|
|
48
|
+
}
|
|
49
|
+
const path = join(tmpdir(), `${name}-${process.pid}-${Date.now()}.zip`);
|
|
50
|
+
writeFileSync(path, zipFolder(dir, files));
|
|
51
|
+
return { path, count: files.length };
|
|
52
|
+
}
|
|
53
|
+
// KEY=VALUE lines; blank lines, comments and `export ` are allowed, quotes are removed.
|
|
54
|
+
export function parseEnv(text) {
|
|
55
|
+
const out = {};
|
|
56
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
57
|
+
const line = raw.trim().replace(/^export\s+/, "");
|
|
58
|
+
if (!line || line.startsWith("#"))
|
|
59
|
+
continue;
|
|
60
|
+
const eq = line.indexOf("=");
|
|
61
|
+
if (eq <= 0)
|
|
62
|
+
throw usage(`Not a KEY=VALUE line: ${line.split("=")[0]}`);
|
|
63
|
+
let value = line.slice(eq + 1).trim();
|
|
64
|
+
if (value.length >= 2 && (value[0] === '"' || value[0] === "'") && value.endsWith(value[0]))
|
|
65
|
+
value = value.slice(1, -1);
|
|
66
|
+
out[line.slice(0, eq).trim()] = value;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
// --- commands -----------------------------------------------------------------
|
|
71
|
+
async function waitLive(ctx, site, started) {
|
|
72
|
+
const workspace = await ctx.workspace();
|
|
73
|
+
let answer = started;
|
|
74
|
+
const begin = Date.now();
|
|
75
|
+
let last = 0;
|
|
76
|
+
while (answer.status === "building") {
|
|
77
|
+
if (Date.now() - begin > WAIT_MS) {
|
|
78
|
+
return { ...answer, note: `Still deploying. votrix tools site status ${site} picks it up.` };
|
|
79
|
+
}
|
|
80
|
+
await sleep(POLL_MS);
|
|
81
|
+
try {
|
|
82
|
+
answer = await ctx.client.request("GET", `/tools/sites/${encodeURIComponent(site)}/status?version=${started.version}`, { workspace });
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (!(error instanceof VotrixError) || error.kind !== "network")
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
const seconds = Math.floor((Date.now() - begin) / 30_000) * 30;
|
|
89
|
+
if (answer.status === "building" && seconds > last) {
|
|
90
|
+
last = seconds;
|
|
91
|
+
note(`Deploying version ${started.version}${answer.waiting_for === "address" ? " (address coming up)" : ""}...`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return answer;
|
|
95
|
+
}
|
|
96
|
+
const BIG_BYTES = 30 * 1048576;
|
|
97
|
+
const START_TIMEOUT_MS = 20 * 60_000;
|
|
98
|
+
async function versionCount(ctx, site) {
|
|
99
|
+
try {
|
|
100
|
+
const listing = await ctx.client.request("GET", `/tools/sites/${encodeURIComponent(site)}/versions`, { workspace: await ctx.workspace() });
|
|
101
|
+
return Math.max(0, ...(listing.versions ?? []).map((v) => v.version));
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error instanceof VotrixError && error.kind === "not_found")
|
|
105
|
+
return 0;
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// A dropped connection does not tell whether the deploy started: look for the
|
|
110
|
+
// version before sending it again, so one deploy never becomes two.
|
|
111
|
+
async function startVersion(ctx, site, body) {
|
|
112
|
+
const workspace = await ctx.workspace();
|
|
113
|
+
const path = `/tools/sites/${encodeURIComponent(site)}/versions`;
|
|
114
|
+
const before = await versionCount(ctx, site);
|
|
115
|
+
for (let attempt = 1;; attempt++) {
|
|
116
|
+
try {
|
|
117
|
+
return await ctx.client.request("POST", path, { workspace, body, timeoutMs: START_TIMEOUT_MS });
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
if (!(error instanceof VotrixError) || error.kind !== "network" || attempt > 2)
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
note("The connection dropped while starting the deploy; checking whether it started...");
|
|
124
|
+
for (let i = 0; i < 12; i++) {
|
|
125
|
+
await sleep(5000);
|
|
126
|
+
const latest = await versionCount(ctx, site).catch(() => before);
|
|
127
|
+
if (latest > before)
|
|
128
|
+
return { site, version: latest, status: "building" };
|
|
129
|
+
}
|
|
130
|
+
note(`It did not start; trying again (${attempt + 1} of 3).`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function needSite(ctx, at = 0) {
|
|
134
|
+
const site = ctx.positional[at];
|
|
135
|
+
if (!site)
|
|
136
|
+
throw usage("A site name is required (votrix tools site list).");
|
|
137
|
+
return site;
|
|
138
|
+
}
|
|
139
|
+
function trackFilters(raw) {
|
|
140
|
+
if (raw === undefined)
|
|
141
|
+
return undefined;
|
|
142
|
+
const filters = new Map();
|
|
143
|
+
for (const entry of Array.isArray(raw) ? raw : [raw]) {
|
|
144
|
+
const [field, values] = typeof entry === "string" ? entry.split(/=(.*)/s) : [];
|
|
145
|
+
if (!field || !values || values.split(",").some((value) => !value)) {
|
|
146
|
+
throw usage("--where requires FIELD=VALUE[,VALUE...], e.g. --where source=utm:instagram.");
|
|
147
|
+
}
|
|
148
|
+
filters.set(field, [...(filters.get(field) ?? []), ...values.split(",")]);
|
|
149
|
+
}
|
|
150
|
+
return Object.fromEntries(filters);
|
|
151
|
+
}
|
|
152
|
+
function trackOption(ctx, name) {
|
|
153
|
+
const value = ctx.flags[name];
|
|
154
|
+
if (value === undefined)
|
|
155
|
+
return undefined;
|
|
156
|
+
if (typeof value !== "string" || !value.trim())
|
|
157
|
+
throw usage(`--${name} requires one value.`);
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
export async function runSite(ctx, action, sub) {
|
|
161
|
+
const workspace = await ctx.workspace();
|
|
162
|
+
const call = (method, path, body) => ctx.client.request(method, `/tools/sites${path}`, { workspace, body, timeoutMs: 360_000 });
|
|
163
|
+
const finish = (answer) => (emit(answer), answer.status === "failed" ? 1 : 0);
|
|
164
|
+
if (action === "list")
|
|
165
|
+
return emit(await call("GET", "")), 0;
|
|
166
|
+
if (action === "track") {
|
|
167
|
+
if (sub !== "enable" && sub !== "disable" && sub !== "events" && sub !== "funnel") {
|
|
168
|
+
throw usage("Choose track enable, disable, events or funnel.", "Run votrix tools site --help.");
|
|
169
|
+
}
|
|
170
|
+
const path = `/${encodeURIComponent(needSite(ctx))}/track`;
|
|
171
|
+
if (sub === "enable" || sub === "disable") {
|
|
172
|
+
return finish(await call("POST", path, { enabled: sub === "enable" }));
|
|
173
|
+
}
|
|
174
|
+
const common = { period: trackOption(ctx, "period"), by: trackOption(ctx, "by"), where: trackFilters(ctx.flags.where) };
|
|
175
|
+
if (sub === "events") {
|
|
176
|
+
const rawLimit = trackOption(ctx, "limit");
|
|
177
|
+
const limit = rawLimit === undefined ? undefined : Number(rawLimit);
|
|
178
|
+
if (limit !== undefined && (!Number.isInteger(limit) || limit < 1 || limit > 200)) {
|
|
179
|
+
throw usage("--limit must be an integer from 1 to 200.");
|
|
180
|
+
}
|
|
181
|
+
return emit(await call("POST", `${path}/events`, {
|
|
182
|
+
...common, event: trackOption(ctx, "event"), search: trackOption(ctx, "search"), limit,
|
|
183
|
+
})), 0;
|
|
184
|
+
}
|
|
185
|
+
const steps = trackOption(ctx, "steps")?.split(",").map((step) => step.trim());
|
|
186
|
+
if (!steps || steps.length < 2 || steps.length > 5 || steps.some((step) => !step)) {
|
|
187
|
+
throw usage("--steps requires 2-5 comma-separated event names, e.g. visit,login,signup_completed.");
|
|
188
|
+
}
|
|
189
|
+
return emit(await call("POST", `${path}/funnel`, { ...common, steps, window: trackOption(ctx, "window") })), 0;
|
|
190
|
+
}
|
|
191
|
+
if (action === "deploy") {
|
|
192
|
+
const site = ctx.flags.name;
|
|
193
|
+
if (!site || typeof site !== "string")
|
|
194
|
+
throw usage("--name is required: the site's name, e.g. --name acme-shop.");
|
|
195
|
+
const path = `/${encodeURIComponent(site)}`;
|
|
196
|
+
let started;
|
|
197
|
+
if (ctx.flags.version !== undefined) {
|
|
198
|
+
if (ctx.positional.length || ctx.flags.asset || ctx.flags["no-track"]) {
|
|
199
|
+
throw usage("--version puts an earlier version back; omit a folder, --asset and --no-track.");
|
|
200
|
+
}
|
|
201
|
+
started = await call("POST", `${path}/live`, { version: Number(ctx.flags.version) });
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
let asset = ctx.flags.asset ? uuidOf(ctx.flags.asset) : undefined;
|
|
205
|
+
if (ctx.flags.asset && !asset)
|
|
206
|
+
throw usage(`'${ctx.flags.asset}' is not an asset id.`);
|
|
207
|
+
if (!asset) {
|
|
208
|
+
const dir = ctx.positional[0];
|
|
209
|
+
if (!dir)
|
|
210
|
+
throw usage("Pass the site's folder (or a .zip), or --asset ID.");
|
|
211
|
+
const packed = packFolder(dir, site);
|
|
212
|
+
try {
|
|
213
|
+
const size = statSync(packed.path).size;
|
|
214
|
+
if (packed.count >= 0)
|
|
215
|
+
note(`Packed ${packed.count} files from ${dir} (${(size / 1048576).toFixed(1)} MB).`);
|
|
216
|
+
if (size > BIG_BYTES)
|
|
217
|
+
note(`The site is over ${BIG_BYTES / 1048576} MB; it deploys slower. Compress large videos and images to speed it up.`);
|
|
218
|
+
asset = (await ctx.client.upload(packed.path, workspace)).id;
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
if (packed.count >= 0)
|
|
222
|
+
unlinkSync(packed.path);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const body = { asset_id: asset };
|
|
226
|
+
if (ctx.flags.address)
|
|
227
|
+
body.address = ctx.flags.address;
|
|
228
|
+
if (ctx.flags["no-track"])
|
|
229
|
+
body.track = false;
|
|
230
|
+
started = await startVersion(ctx, site, body);
|
|
231
|
+
}
|
|
232
|
+
if (ctx.flags["no-wait"])
|
|
233
|
+
return finish(started);
|
|
234
|
+
note(`Deploying ${site} version ${started.version}...`);
|
|
235
|
+
const done = await waitLive(ctx, site, started);
|
|
236
|
+
// What the start said about the site stays in the answer.
|
|
237
|
+
const kept = Object.fromEntries(["new_site", "replaces_version", "undo", "tracking", "tracking_note", "tracking_help"]
|
|
238
|
+
.filter((k) => started[k] !== undefined).map((k) => [k, started[k]]));
|
|
239
|
+
return finish({ ...done, ...kept });
|
|
240
|
+
}
|
|
241
|
+
if (action === "versions")
|
|
242
|
+
return emit(await call("GET", `/${encodeURIComponent(needSite(ctx))}/versions`)), 0;
|
|
243
|
+
if (action === "status") {
|
|
244
|
+
const query = new URLSearchParams();
|
|
245
|
+
if (ctx.flags.version !== undefined)
|
|
246
|
+
query.set("version", String(ctx.flags.version));
|
|
247
|
+
if (ctx.flags.logs)
|
|
248
|
+
query.set("logs", "true");
|
|
249
|
+
const q = query.toString() ? `?${query}` : "";
|
|
250
|
+
return finish(await call("GET", `/${encodeURIComponent(needSite(ctx))}/status${q}`));
|
|
251
|
+
}
|
|
252
|
+
if (action === "env") {
|
|
253
|
+
const site = encodeURIComponent(needSite(ctx));
|
|
254
|
+
if (sub === "list")
|
|
255
|
+
return emit(await call("GET", `/${site}/env`)), 0;
|
|
256
|
+
if (sub === "set") {
|
|
257
|
+
const from = ctx.flags["from-file"];
|
|
258
|
+
if (!from)
|
|
259
|
+
throw usage("--from-file FILE is required (KEY=VALUE lines; - reads stdin). Values never go on the command line.");
|
|
260
|
+
const text = from === "-" ? await readStdin() : readFileSync(String(from), "utf8");
|
|
261
|
+
const body = { variables: parseEnv(text), target: ctx.flags.target ?? "all" };
|
|
262
|
+
return emit(await call("PUT", `/${site}/env`, body)), 0;
|
|
263
|
+
}
|
|
264
|
+
throw usage(`Unknown command 'votrix tools site env ${sub ?? ""}'.`, "Run votrix tools site --help.");
|
|
265
|
+
}
|
|
266
|
+
if (action === "domain") {
|
|
267
|
+
const site = encodeURIComponent(needSite(ctx));
|
|
268
|
+
const domain = ctx.positional[1];
|
|
269
|
+
if (!domain)
|
|
270
|
+
throw usage("A domain is required, e.g. example.com.");
|
|
271
|
+
if (sub === "add")
|
|
272
|
+
return emit(await call("POST", `/${site}/domains`, { domain })), 0;
|
|
273
|
+
if (sub === "status")
|
|
274
|
+
return emit(await call("GET", `/${site}/domains/${encodeURIComponent(domain)}`)), 0;
|
|
275
|
+
throw usage(`Unknown command 'votrix tools site domain ${sub ?? ""}'.`, "Run votrix tools site --help.");
|
|
276
|
+
}
|
|
277
|
+
throw usage(`Unknown command 'votrix tools site ${action ?? ""}'.`, "Run votrix tools site --help.");
|
|
278
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
// `votrix tools`: media, seo, scraper, ui (catalogs on the backend), social, site, files.
|
|
2
|
+
//
|
|
3
|
+
// What is inside a category (models, datasets, sources, recipes) is read from
|
|
4
|
+
// the backend every time, so a model swap needs no new CLI.
|
|
5
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
6
|
+
import { downloadCards } from "./agent.js";
|
|
7
|
+
import { emit, note, sleep, usage, uuidOf, VotrixError } from "./core.js";
|
|
8
|
+
import { runSite } from "./site.js";
|
|
9
|
+
import { runIntegrations } from "./integrations.js";
|
|
10
|
+
import { categories as CATEGORIES } from "./commands.js";
|
|
11
|
+
const FIXED_FLAGS = new Set(["mode", "input", "max-cost", "out", "no-wait", "workspace", "json"]);
|
|
12
|
+
const WAIT_SECONDS = 25;
|
|
13
|
+
// `--some-param value` -> input.some_param; JSON values parse; a repeated flag is
|
|
14
|
+
// a list; a value that is a local file is uploaded and passed as asset:<id>.
|
|
15
|
+
async function inputOf(ctx) {
|
|
16
|
+
let input = {};
|
|
17
|
+
const given = ctx.flags.input;
|
|
18
|
+
if (given !== undefined) {
|
|
19
|
+
const text = String(given).startsWith("@") ? readFileSync(String(given).slice(1), "utf8") : String(given);
|
|
20
|
+
try {
|
|
21
|
+
input = JSON.parse(text);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
throw usage("--input must be a JSON object, or @file.json.");
|
|
25
|
+
}
|
|
26
|
+
if (!input || typeof input !== "object" || Array.isArray(input))
|
|
27
|
+
throw usage("--input must be a JSON object.");
|
|
28
|
+
}
|
|
29
|
+
const parse = (value) => {
|
|
30
|
+
if (typeof value !== "string")
|
|
31
|
+
return value;
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(value);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
for (const [flag, value] of Object.entries(ctx.flags)) {
|
|
40
|
+
if (FIXED_FLAGS.has(flag))
|
|
41
|
+
continue;
|
|
42
|
+
input[flag.replace(/-/g, "_")] = Array.isArray(value) ? value.map(parse) : parse(value);
|
|
43
|
+
}
|
|
44
|
+
const workspace = await ctx.workspace();
|
|
45
|
+
const uploaded = new Map();
|
|
46
|
+
const upload = async (value) => {
|
|
47
|
+
if (typeof value === "string" && value.length < 1024 && !value.includes("://") && existsSync(value)
|
|
48
|
+
&& statSync(value).isFile()) {
|
|
49
|
+
if (!uploaded.has(value))
|
|
50
|
+
uploaded.set(value, `asset:${(await ctx.client.upload(value, workspace)).id}`);
|
|
51
|
+
return uploaded.get(value);
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(value))
|
|
54
|
+
return Promise.all(value.map(upload));
|
|
55
|
+
if (value && typeof value === "object") {
|
|
56
|
+
return Object.fromEntries(await Promise.all(Object.entries(value).map(async ([k, v]) => [k, await upload(v)])));
|
|
57
|
+
}
|
|
58
|
+
return value;
|
|
59
|
+
};
|
|
60
|
+
return upload(input);
|
|
61
|
+
}
|
|
62
|
+
async function finish(ctx, run) {
|
|
63
|
+
if (ctx.flags.out && run.files?.length) {
|
|
64
|
+
run.files = await downloadCards(ctx, run.files, ctx.flags.out);
|
|
65
|
+
}
|
|
66
|
+
emit(run);
|
|
67
|
+
return run.status === "failed" ? 1 : 0;
|
|
68
|
+
}
|
|
69
|
+
async function waitFor(ctx, run) {
|
|
70
|
+
const workspace = await ctx.workspace();
|
|
71
|
+
const started = Date.now();
|
|
72
|
+
let last = 0;
|
|
73
|
+
while (run.status === "running") {
|
|
74
|
+
try {
|
|
75
|
+
run = await ctx.client.request("GET", `/tools/runs/${run.run_id}?wait=${WAIT_SECONDS}`, { workspace, timeoutMs: (WAIT_SECONDS + 15) * 1000 });
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (!(error instanceof VotrixError) || error.kind !== "network")
|
|
79
|
+
throw error;
|
|
80
|
+
await sleep(3000);
|
|
81
|
+
}
|
|
82
|
+
const minutes = Math.floor((Date.now() - started) / 60_000);
|
|
83
|
+
if (run.status === "running" && minutes > last) {
|
|
84
|
+
last = minutes;
|
|
85
|
+
note(`Still running (${minutes} min), still waiting. Stopping the wait does not stop the run: votrix tools runs get ${run.run_id} picks it up later.`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return run;
|
|
89
|
+
}
|
|
90
|
+
async function runCategory(ctx, category, action) {
|
|
91
|
+
const workspace = await ctx.workspace();
|
|
92
|
+
const call = (method, path, body) => ctx.client.request(method, path, { workspace, body });
|
|
93
|
+
const name = ctx.positional[0];
|
|
94
|
+
if (action === "list")
|
|
95
|
+
return emit(await call("GET", `/tools/${category}`)), 0;
|
|
96
|
+
if (action === "explore") {
|
|
97
|
+
if (!name)
|
|
98
|
+
throw usage(`Usage: votrix tools ${category} explore <name> [--mode MODE]`);
|
|
99
|
+
const mode = ctx.flags.mode ? `?mode=${encodeURIComponent(ctx.flags.mode)}` : "";
|
|
100
|
+
return emit(await call("GET", `/tools/${category}/${encodeURIComponent(name)}${mode}`)), 0;
|
|
101
|
+
}
|
|
102
|
+
if (action === "run") {
|
|
103
|
+
if (!name)
|
|
104
|
+
throw usage(`Usage: votrix tools ${category} run <name> [--mode MODE] [--<parameter> VALUE ...] [--input JSON|@file] [--max-cost USD] [--out DIR] [--no-wait]`);
|
|
105
|
+
const body = { input: await inputOf(ctx) };
|
|
106
|
+
if (ctx.flags.mode)
|
|
107
|
+
body.mode = ctx.flags.mode;
|
|
108
|
+
if (ctx.flags["max-cost"] !== undefined)
|
|
109
|
+
body.max_cost_usd = Number(ctx.flags["max-cost"]);
|
|
110
|
+
let run = await call("POST", `/tools/${category}/${encodeURIComponent(name)}/runs`, body);
|
|
111
|
+
const estimate = run.estimate_usd != null ? ` · estimate ≈ $${run.estimate_usd}` : "";
|
|
112
|
+
note(`Started ${category} ${run.name}${run.mode ? ` ${run.mode}` : ""} · run ${run.run_id}${estimate}`);
|
|
113
|
+
if (ctx.flags["no-wait"])
|
|
114
|
+
return emit(run), 0;
|
|
115
|
+
return finish(ctx, await waitFor(ctx, run));
|
|
116
|
+
}
|
|
117
|
+
throw usage(`Unknown command 'votrix tools ${category} ${action ?? ""}'.`, "Run votrix tools --help.");
|
|
118
|
+
}
|
|
119
|
+
async function runRuns(ctx, action) {
|
|
120
|
+
const workspace = await ctx.workspace();
|
|
121
|
+
if (action === "list") {
|
|
122
|
+
const query = new URLSearchParams({ limit: String(ctx.flags.limit ?? 20) });
|
|
123
|
+
if (ctx.flags.category)
|
|
124
|
+
query.set("category", ctx.flags.category);
|
|
125
|
+
return emit(await ctx.client.request("GET", `/tools/runs?${query}`, { workspace })), 0;
|
|
126
|
+
}
|
|
127
|
+
const id = uuidOf(ctx.positional[0]);
|
|
128
|
+
if (!id)
|
|
129
|
+
throw usage("A run id is required.");
|
|
130
|
+
if (action === "get") {
|
|
131
|
+
const run = await ctx.client.request("GET", `/tools/runs/${id}`, { workspace });
|
|
132
|
+
return finish(ctx, ctx.flags["no-wait"] ? run : await waitFor(ctx, run));
|
|
133
|
+
}
|
|
134
|
+
if (action === "stop")
|
|
135
|
+
return emit(await ctx.client.request("DELETE", `/tools/runs/${id}`, { workspace })), 0;
|
|
136
|
+
throw usage(`Unknown command 'votrix tools runs ${action ?? ""}'.`, "Run votrix tools --help.");
|
|
137
|
+
}
|
|
138
|
+
// --- social -------------------------------------------------------------------
|
|
139
|
+
async function runSocial(ctx, action, sub) {
|
|
140
|
+
const workspace = await ctx.workspace();
|
|
141
|
+
const base = "/tools/social";
|
|
142
|
+
const call = (method, path, body) => ctx.client.request(method, base + path, { workspace, body, timeoutMs: 360_000 });
|
|
143
|
+
if (action === "accounts") {
|
|
144
|
+
const accounts = (await call("GET", "/accounts")).accounts ?? [];
|
|
145
|
+
const platform = ctx.flags.platform;
|
|
146
|
+
return emit({ accounts: platform ? accounts.filter((a) => a.platform === platform) : accounts }), 0;
|
|
147
|
+
}
|
|
148
|
+
if (action !== "posts")
|
|
149
|
+
throw usage(`Unknown command 'votrix tools social ${action ?? ""}'.`, "Run votrix tools --help.");
|
|
150
|
+
const id = ctx.positional[0];
|
|
151
|
+
const needId = () => { if (!id)
|
|
152
|
+
throw usage("A post id is required."); return encodeURIComponent(id); };
|
|
153
|
+
if (sub === "list") {
|
|
154
|
+
const query = new URLSearchParams({ limit: String(ctx.flags.limit ?? 50), offset: String(ctx.flags.offset ?? 0) });
|
|
155
|
+
for (const a of [].concat(ctx.flags.account ?? []))
|
|
156
|
+
query.append("account_id", a);
|
|
157
|
+
for (const s of [].concat(ctx.flags.status ?? []))
|
|
158
|
+
query.append("status", s);
|
|
159
|
+
return emit(await call("GET", `/posts?${query}`)), 0;
|
|
160
|
+
}
|
|
161
|
+
if (sub === "get")
|
|
162
|
+
return emit(await call("GET", `/posts/${needId()}`)), 0;
|
|
163
|
+
if (sub === "results")
|
|
164
|
+
return emit(await call("GET", `/posts/${needId()}/results`)), 0;
|
|
165
|
+
if (sub === "cancel")
|
|
166
|
+
return emit(await call("DELETE", `/posts/${needId()}`)), 0;
|
|
167
|
+
if (sub === "create" || sub === "update") {
|
|
168
|
+
let body;
|
|
169
|
+
if (ctx.flags.file) {
|
|
170
|
+
const text = ctx.flags.file === "-" ? await import("./core.js").then((m) => m.readStdin()) : readFileSync(ctx.flags.file, "utf8");
|
|
171
|
+
body = JSON.parse(text);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
const accounts = [].concat(ctx.flags.account ?? []);
|
|
175
|
+
if (!accounts.length)
|
|
176
|
+
throw usage("--account is required (see votrix tools social accounts).");
|
|
177
|
+
const media = [];
|
|
178
|
+
for (const item of [].concat(ctx.flags.media ?? [])) {
|
|
179
|
+
if (/^https?:\/\//.test(item))
|
|
180
|
+
media.push({ url: item });
|
|
181
|
+
else if (uuidOf(item) && !existsSync(item))
|
|
182
|
+
media.push({ asset_id: uuidOf(item) });
|
|
183
|
+
else
|
|
184
|
+
media.push({ asset_id: (await ctx.client.upload(item, workspace)).id });
|
|
185
|
+
}
|
|
186
|
+
if (ctx.flags["thumbnail"] && media.length === 1) {
|
|
187
|
+
const thumb = ctx.flags.thumbnail;
|
|
188
|
+
Object.assign(media[0], uuidOf(thumb) ? { thumbnail_asset_id: uuidOf(thumb) }
|
|
189
|
+
: /^https?:/.test(thumb) ? { thumbnail_url: thumb } : { thumbnail_asset_id: (await ctx.client.upload(thumb, workspace)).id });
|
|
190
|
+
}
|
|
191
|
+
const caption = ctx.flags["caption-file"] ? readFileSync(ctx.flags["caption-file"], "utf8") : ctx.flags.caption ?? "";
|
|
192
|
+
body = { account_ids: accounts, caption, media, draft: Boolean(ctx.flags.draft) };
|
|
193
|
+
if (ctx.flags["schedule-at"])
|
|
194
|
+
body.scheduled_at = ctx.flags["schedule-at"];
|
|
195
|
+
if (ctx.flags.title)
|
|
196
|
+
body.platform_configurations = { youtube: { title: ctx.flags.title } };
|
|
197
|
+
}
|
|
198
|
+
return emit(sub === "create" ? await call("POST", "/posts", body) : await call("PUT", `/posts/${needId()}`, body)), 0;
|
|
199
|
+
}
|
|
200
|
+
throw usage(`Unknown command 'votrix tools social posts ${sub ?? ""}'.`, "Run votrix tools --help.");
|
|
201
|
+
}
|
|
202
|
+
// --- files --------------------------------------------------------------------
|
|
203
|
+
async function runFiles(ctx, action) {
|
|
204
|
+
const workspace = await ctx.workspace();
|
|
205
|
+
if (action === "upload") {
|
|
206
|
+
if (!ctx.positional.length)
|
|
207
|
+
throw usage("Usage: votrix tools files upload PATH [PATH ...]");
|
|
208
|
+
const uploaded = [];
|
|
209
|
+
for (const path of ctx.positional)
|
|
210
|
+
uploaded.push(await ctx.client.upload(path, workspace));
|
|
211
|
+
return emit({ files: uploaded, public: false,
|
|
212
|
+
note: "Private to this workspace. votrix tools files publish <asset> --name N gives a permanent public URL." }), 0;
|
|
213
|
+
}
|
|
214
|
+
if (action === "get") {
|
|
215
|
+
const id = uuidOf(ctx.positional[0]);
|
|
216
|
+
if (!id)
|
|
217
|
+
throw usage("An asset id is required.");
|
|
218
|
+
return emit(await ctx.client.request("GET", `/files/${id}`, { workspace })), 0;
|
|
219
|
+
}
|
|
220
|
+
if (action === "pull") {
|
|
221
|
+
const cards = [];
|
|
222
|
+
for (const given of ctx.positional) {
|
|
223
|
+
const id = uuidOf(given);
|
|
224
|
+
if (!id)
|
|
225
|
+
throw usage(`'${given}' is not an asset id.`);
|
|
226
|
+
const row = await ctx.client.request("GET", `/files/${id}`, { workspace });
|
|
227
|
+
cards.push({ asset_id: id, filename: row.filename });
|
|
228
|
+
}
|
|
229
|
+
return emit({ files: await downloadCards(ctx, cards, ctx.flags.out ?? ".") }), 0;
|
|
230
|
+
}
|
|
231
|
+
if (action === "publish") {
|
|
232
|
+
const given = ctx.positional[0];
|
|
233
|
+
if (!given || !ctx.flags.name)
|
|
234
|
+
throw usage("Usage: votrix tools files publish <asset>|PATH --name <name.ext>");
|
|
235
|
+
// A local file is uploaded first; publishing is what makes it public.
|
|
236
|
+
const id = uuidOf(given) ?? (existsSync(given) ? (await ctx.client.upload(given, workspace)).id : undefined);
|
|
237
|
+
if (!id)
|
|
238
|
+
throw usage(`'${given}' is neither an asset id nor a file.`);
|
|
239
|
+
return emit(await ctx.client.request("POST", `/files/${id}/publish`, { workspace, body: { name: ctx.flags.name } })), 0;
|
|
240
|
+
}
|
|
241
|
+
if (action === "library") {
|
|
242
|
+
const query = new URLSearchParams({ limit: String(ctx.flags.limit ?? 50) });
|
|
243
|
+
if (ctx.flags.query)
|
|
244
|
+
query.set("q", ctx.flags.query);
|
|
245
|
+
for (const g of [].concat(ctx.flags["mime-group"] ?? []))
|
|
246
|
+
query.append("mime_group", g);
|
|
247
|
+
if (ctx.flags.before)
|
|
248
|
+
query.set("before", ctx.flags.before);
|
|
249
|
+
return emit(await ctx.client.request("GET", `/files/save/list?${query}`, { workspace })), 0;
|
|
250
|
+
}
|
|
251
|
+
throw usage(`Unknown command 'votrix tools files ${action ?? ""}'.`, "Run votrix tools --help.");
|
|
252
|
+
}
|
|
253
|
+
export async function runTools(ctx, group, action, sub) {
|
|
254
|
+
if (CATEGORIES.includes(group))
|
|
255
|
+
return runCategory(ctx, group, action);
|
|
256
|
+
if (group === "runs")
|
|
257
|
+
return runRuns(ctx, action);
|
|
258
|
+
if (group === "social")
|
|
259
|
+
return runSocial(ctx, action, sub);
|
|
260
|
+
if (group === "files")
|
|
261
|
+
return runFiles(ctx, action);
|
|
262
|
+
if (group === "site")
|
|
263
|
+
return runSite(ctx, action, sub);
|
|
264
|
+
if (group === "integrations")
|
|
265
|
+
return runIntegrations(ctx, action);
|
|
266
|
+
throw usage(`Unknown tools group '${group ?? ""}'.`, "Run votrix tools --help.");
|
|
267
|
+
}
|
package/dist/src/zip.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Plain zip files without dependencies: pack a folder (deflate), and unpack the
|
|
2
|
+
// website / video project archives employees hand over.
|
|
3
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { deflateRawSync, inflateRawSync } from "node:zlib";
|
|
6
|
+
import { VotrixError } from "./core.js";
|
|
7
|
+
const CRC_TABLE = (() => {
|
|
8
|
+
const table = new Uint32Array(256);
|
|
9
|
+
for (let n = 0; n < 256; n++) {
|
|
10
|
+
let c = n;
|
|
11
|
+
for (let k = 0; k < 8; k++)
|
|
12
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
13
|
+
table[n] = c >>> 0;
|
|
14
|
+
}
|
|
15
|
+
return table;
|
|
16
|
+
})();
|
|
17
|
+
function crc32(data) {
|
|
18
|
+
let c = 0xffffffff;
|
|
19
|
+
for (let i = 0; i < data.length; i++)
|
|
20
|
+
c = CRC_TABLE[(c ^ data[i]) & 0xff] ^ (c >>> 8);
|
|
21
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
22
|
+
}
|
|
23
|
+
// A plain zip (deflate), paths relative to `root` with forward slashes.
|
|
24
|
+
export function zipFolder(root, files) {
|
|
25
|
+
const locals = [];
|
|
26
|
+
const centrals = [];
|
|
27
|
+
let offset = 0;
|
|
28
|
+
for (const path of files) {
|
|
29
|
+
const name = Buffer.from(relative(root, path).split(sep).join("/"), "utf8");
|
|
30
|
+
const data = readFileSync(path);
|
|
31
|
+
const packed = deflateRawSync(data);
|
|
32
|
+
const crc = crc32(data);
|
|
33
|
+
const local = Buffer.alloc(30);
|
|
34
|
+
local.writeUInt32LE(0x04034b50, 0);
|
|
35
|
+
local.writeUInt16LE(20, 4);
|
|
36
|
+
local.writeUInt16LE(0x0800, 6);
|
|
37
|
+
local.writeUInt16LE(8, 8);
|
|
38
|
+
local.writeUInt32LE(0, 10);
|
|
39
|
+
local.writeUInt32LE(crc, 14);
|
|
40
|
+
local.writeUInt32LE(packed.length, 18);
|
|
41
|
+
local.writeUInt32LE(data.length, 22);
|
|
42
|
+
local.writeUInt16LE(name.length, 26);
|
|
43
|
+
local.writeUInt16LE(0, 28);
|
|
44
|
+
const central = Buffer.alloc(46);
|
|
45
|
+
central.writeUInt32LE(0x02014b50, 0);
|
|
46
|
+
central.writeUInt16LE(20, 4);
|
|
47
|
+
central.writeUInt16LE(20, 6);
|
|
48
|
+
central.writeUInt16LE(0x0800, 8);
|
|
49
|
+
central.writeUInt16LE(8, 10);
|
|
50
|
+
central.writeUInt32LE(0, 12);
|
|
51
|
+
central.writeUInt32LE(crc, 16);
|
|
52
|
+
central.writeUInt32LE(packed.length, 20);
|
|
53
|
+
central.writeUInt32LE(data.length, 24);
|
|
54
|
+
central.writeUInt16LE(name.length, 28);
|
|
55
|
+
central.writeUInt32LE(offset, 42);
|
|
56
|
+
locals.push(local, name, packed);
|
|
57
|
+
centrals.push(central, name);
|
|
58
|
+
offset += local.length + name.length + packed.length;
|
|
59
|
+
}
|
|
60
|
+
const centralSize = centrals.reduce((n, b) => n + b.length, 0);
|
|
61
|
+
const end = Buffer.alloc(22);
|
|
62
|
+
end.writeUInt32LE(0x06054b50, 0);
|
|
63
|
+
end.writeUInt16LE(files.length, 8);
|
|
64
|
+
end.writeUInt16LE(files.length, 10);
|
|
65
|
+
end.writeUInt32LE(centralSize, 12);
|
|
66
|
+
end.writeUInt32LE(offset, 16);
|
|
67
|
+
return Buffer.concat([...locals, ...centrals, end]);
|
|
68
|
+
}
|
|
69
|
+
// Unpack `zipPath` into `dir`; -> the number of files written. Entries that would
|
|
70
|
+
// land outside `dir` (absolute, or with ..) are refused.
|
|
71
|
+
export function unzipTo(zipPath, dir) {
|
|
72
|
+
const data = readFileSync(zipPath);
|
|
73
|
+
let end = -1;
|
|
74
|
+
for (let i = data.length - 22; i >= Math.max(0, data.length - 65_557); i--) {
|
|
75
|
+
if (data.readUInt32LE(i) === 0x06054b50) {
|
|
76
|
+
end = i;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (end < 0)
|
|
81
|
+
throw new VotrixError("server", "The project archive is not a zip file.");
|
|
82
|
+
const count = data.readUInt16LE(end + 10);
|
|
83
|
+
let at = data.readUInt32LE(end + 16);
|
|
84
|
+
const root = resolve(dir);
|
|
85
|
+
let written = 0;
|
|
86
|
+
for (let n = 0; n < count; n++) {
|
|
87
|
+
if (data.readUInt32LE(at) !== 0x02014b50)
|
|
88
|
+
throw new VotrixError("server", "The project archive is damaged.");
|
|
89
|
+
const method = data.readUInt16LE(at + 10);
|
|
90
|
+
const size = data.readUInt32LE(at + 20);
|
|
91
|
+
const nameLength = data.readUInt16LE(at + 28);
|
|
92
|
+
const extra = data.readUInt16LE(at + 30);
|
|
93
|
+
const comment = data.readUInt16LE(at + 32);
|
|
94
|
+
const local = data.readUInt32LE(at + 42);
|
|
95
|
+
const name = data.toString("utf8", at + 46, at + 46 + nameLength);
|
|
96
|
+
at += 46 + nameLength + extra + comment;
|
|
97
|
+
if (name.endsWith("/"))
|
|
98
|
+
continue;
|
|
99
|
+
const target = resolve(root, name);
|
|
100
|
+
if (!target.startsWith(root + sep))
|
|
101
|
+
throw new VotrixError("server", `The project archive has an unsafe path: ${name}`);
|
|
102
|
+
const start = local + 30 + data.readUInt16LE(local + 26) + data.readUInt16LE(local + 28);
|
|
103
|
+
const raw = data.subarray(start, start + size);
|
|
104
|
+
const body = method === 0 ? raw : method === 8 ? inflateRawSync(raw) : null;
|
|
105
|
+
if (!body)
|
|
106
|
+
throw new VotrixError("server", `Cannot unpack ${name} (compression ${method}).`);
|
|
107
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
108
|
+
writeFileSync(target, body);
|
|
109
|
+
written++;
|
|
110
|
+
}
|
|
111
|
+
return written;
|
|
112
|
+
}
|