sagadeck 1.5.0 → 1.7.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/src/ai/relay.js DELETED
@@ -1,45 +0,0 @@
1
- // Studio em Node: sobe o `modelrelay serve` junto, como o sagadeck do pip já faz (python/sagadeck/llm.py).
2
- // Só quando faz sentido: ninguém escutando na porta padrão, sem SAGADECK_LLM_URL e sem SAGADECK_NO_RELAY=1.
3
- // A configuração (provedor, modelos, chave) continua sendo do modelrelay (~/.modelrelay/config.toml).
4
- import net from "node:net";
5
- import { spawn } from "node:child_process";
6
-
7
- export const RELAY_PORT = 8765;
8
-
9
- export function portOpen(port, host = "127.0.0.1", timeoutMs = 300) {
10
- return new Promise((resolve) => {
11
- const s = net.connect({ port, host });
12
- const done = (ok) => { s.destroy(); resolve(ok); };
13
- s.setTimeout(timeoutMs, () => done(false));
14
- s.once("connect", () => done(true));
15
- s.once("error", () => done(false));
16
- });
17
- }
18
-
19
- // o comando `modelrelay` (script do pip) e, se ele não estiver no PATH, o módulo pelo Python
20
- const candidates = (port) => [
21
- ["modelrelay", ["serve", "--port", String(port)]],
22
- ["python", ["-m", "modelrelay.cli", "serve", "--port", String(port)]],
23
- ["python3", ["-m", "modelrelay.cli", "serve", "--port", String(port)]],
24
- ];
25
-
26
- export async function startRelay({ env = process.env, port = RELAY_PORT, spawnFn = spawn, timeoutMs = 8000 } = {}) {
27
- if (env.SAGADECK_LLM_URL || env.SAGADECK_NO_RELAY === "1") return null;
28
- if (await portOpen(port)) return null; // já tem um rodando: o motor acha sozinho
29
- for (const [cmd, args] of candidates(port)) {
30
- let child;
31
- try { child = spawnFn(cmd, args, { stdio: "ignore", windowsHide: true, env }); } catch { continue; }
32
- const failed = await new Promise((resolve) => {
33
- let over = false;
34
- const finish = (v) => { if (!over) { over = true; clearInterval(poll); clearTimeout(limit); resolve(v); } };
35
- child.once?.("error", () => finish(true)); // comando não existe
36
- child.once?.("exit", () => finish(true)); // existe, mas sem o modelrelay ou com config inválida
37
- const poll = setInterval(async () => { if (await portOpen(port)) finish(false); }, 150);
38
- const limit = setTimeout(() => finish(true), timeoutMs);
39
- });
40
- if (failed) { try { child.kill?.(); } catch {} continue; }
41
- const stop = () => { try { child.kill?.(); } catch {} };
42
- return { url: `http://127.0.0.1:${port}/v1`, cmd, stop };
43
- }
44
- return null;
45
- }