@tiens.nguyen/gonext-local-worker 1.0.8 → 1.0.9
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/gonext-local-worker.mjs +79 -10
- package/package.json +1 -1
package/gonext-local-worker.mjs
CHANGED
|
@@ -1,18 +1,87 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* Usage (from api/):
|
|
8
|
-
* export GONEXT_API_BASE=https://xxxx.execute-api....amazonaws.com
|
|
9
|
-
* export GONEXT_WORKER_KEY=<plaintext secret from Settings → Worker API key>
|
|
10
|
-
* node scripts/local-llm-worker.mjs
|
|
11
|
-
*
|
|
12
|
-
* Requires Node 18+ (global fetch). Uses the OpenAI SDK from this package.
|
|
3
|
+
* GoNext local worker:
|
|
4
|
+
* - `gonext-local-worker set <workerKey> [--api-base URL] [--poll-ms 1500]`
|
|
5
|
+
* writes ~/.gonext/worker.env
|
|
6
|
+
* - `gonext-local-worker` starts polling loop
|
|
13
7
|
*/
|
|
8
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import dotenv from "dotenv";
|
|
14
12
|
import OpenAI from "openai";
|
|
15
13
|
|
|
14
|
+
const ENV_FILE = join(homedir(), ".gonext", "worker.env");
|
|
15
|
+
dotenv.config({ path: ENV_FILE });
|
|
16
|
+
dotenv.config();
|
|
17
|
+
|
|
18
|
+
const args = process.argv.slice(2);
|
|
19
|
+
|
|
20
|
+
function printHelp() {
|
|
21
|
+
console.log(`
|
|
22
|
+
gonext-local-worker
|
|
23
|
+
|
|
24
|
+
Usage:
|
|
25
|
+
gonext-local-worker
|
|
26
|
+
gonext-local-worker set <workerKey> [--api-base <url>] [--poll-ms <ms>]
|
|
27
|
+
|
|
28
|
+
Examples:
|
|
29
|
+
gonext-local-worker set abc123 --api-base https://hwohu56e8d.execute-api.ap-southeast-1.amazonaws.com
|
|
30
|
+
gonext-local-worker
|
|
31
|
+
`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseFlag(name) {
|
|
35
|
+
const idx = args.indexOf(name);
|
|
36
|
+
if (idx >= 0 && args[idx + 1]) {
|
|
37
|
+
return args[idx + 1];
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function setConfig() {
|
|
43
|
+
const workerKey = args[1]?.trim();
|
|
44
|
+
if (!workerKey) {
|
|
45
|
+
console.error("Missing worker key. Usage: gonext-local-worker set <workerKey>");
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
const currentRaw = await readFile(ENV_FILE, "utf8").catch(() => "");
|
|
49
|
+
const current = dotenv.parse(currentRaw);
|
|
50
|
+
const apiBaseFromFlag = parseFlag("--api-base");
|
|
51
|
+
const pollMsFromFlag = parseFlag("--poll-ms");
|
|
52
|
+
const next = {
|
|
53
|
+
GONEXT_API_BASE: (
|
|
54
|
+
apiBaseFromFlag ??
|
|
55
|
+
current.GONEXT_API_BASE ??
|
|
56
|
+
process.env.GONEXT_API_BASE ??
|
|
57
|
+
""
|
|
58
|
+
).replace(/\/+$/, ""),
|
|
59
|
+
GONEXT_WORKER_KEY: workerKey,
|
|
60
|
+
GONEXT_POLL_MS:
|
|
61
|
+
pollMsFromFlag ?? current.GONEXT_POLL_MS ?? process.env.GONEXT_POLL_MS ?? "1500",
|
|
62
|
+
};
|
|
63
|
+
await mkdir(join(homedir(), ".gonext"), { recursive: true });
|
|
64
|
+
await writeFile(
|
|
65
|
+
ENV_FILE,
|
|
66
|
+
`GONEXT_API_BASE=${next.GONEXT_API_BASE}\nGONEXT_WORKER_KEY=${next.GONEXT_WORKER_KEY}\nGONEXT_POLL_MS=${next.GONEXT_POLL_MS}\n`,
|
|
67
|
+
"utf8"
|
|
68
|
+
);
|
|
69
|
+
console.log(`Saved ${ENV_FILE}`);
|
|
70
|
+
if (!next.GONEXT_API_BASE) {
|
|
71
|
+
console.log("Tip: set API base too: gonext-local-worker set <workerKey> --api-base <https-url>");
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
77
|
+
printHelp();
|
|
78
|
+
process.exit(0);
|
|
79
|
+
}
|
|
80
|
+
if (args[0] === "set") {
|
|
81
|
+
await setConfig();
|
|
82
|
+
process.exit(0);
|
|
83
|
+
}
|
|
84
|
+
|
|
16
85
|
const apiBase = (process.env.GONEXT_API_BASE ?? "").replace(/\/+$/, "");
|
|
17
86
|
const workerKey = process.env.GONEXT_WORKER_KEY ?? "";
|
|
18
87
|
const pollMs = Number(process.env.GONEXT_POLL_MS ?? "1500") || 1500;
|
package/package.json
CHANGED