svamp-cli 0.1.75 → 0.1.76

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.
@@ -1,156 +0,0 @@
1
- import { existsSync, readFileSync, mkdirSync, writeFileSync, renameSync } from 'node:fs';
2
- import { join, dirname } from 'node:path';
3
- import os from 'node:os';
4
-
5
- const SVAMP_HOME = process.env.SVAMP_HOME || join(os.homedir(), ".svamp");
6
- function getConfigPath(sessionId) {
7
- const cwd = process.cwd();
8
- return join(cwd, ".svamp", sessionId, "config.json");
9
- }
10
- function readConfig(configPath) {
11
- try {
12
- if (existsSync(configPath)) return JSON.parse(readFileSync(configPath, "utf-8"));
13
- } catch {
14
- }
15
- return {};
16
- }
17
- function writeConfig(configPath, config) {
18
- mkdirSync(dirname(configPath), { recursive: true });
19
- const tmp = configPath + ".tmp";
20
- writeFileSync(tmp, JSON.stringify(config, null, 2));
21
- renameSync(tmp, configPath);
22
- }
23
- async function connectAndEmit(event) {
24
- const ENV_FILE = join(SVAMP_HOME, ".env");
25
- if (existsSync(ENV_FILE)) {
26
- const lines = readFileSync(ENV_FILE, "utf-8").split("\n");
27
- for (const line of lines) {
28
- const m = line.match(/^([A-Z_]+)=(.*)/);
29
- if (m && !process.env[m[1]]) process.env[m[1]] = m[2].replace(/^["']|["']$/g, "");
30
- }
31
- }
32
- const serverUrl = process.env.HYPHA_SERVER_URL;
33
- const token = process.env.HYPHA_TOKEN;
34
- if (!serverUrl || !token) {
35
- console.error('No Hypha credentials. Run "svamp login" first.');
36
- process.exit(1);
37
- }
38
- const origLog = console.log;
39
- const origWarn = console.warn;
40
- const origInfo = console.info;
41
- console.log = () => {
42
- };
43
- console.warn = () => {
44
- };
45
- console.info = () => {
46
- };
47
- let server;
48
- try {
49
- const mod = await import('hypha-rpc');
50
- const connectToServer = mod.connectToServer || mod.default?.connectToServer;
51
- server = await connectToServer({ server_url: serverUrl, token, name: "svamp-agent-emit" });
52
- } catch (err) {
53
- console.log = origLog;
54
- console.warn = origWarn;
55
- console.info = origInfo;
56
- console.error(`Failed to connect to Hypha: ${err.message}`);
57
- process.exit(1);
58
- }
59
- console.log = origLog;
60
- console.warn = origWarn;
61
- console.info = origInfo;
62
- await server.emit({ ...event, to: "*" });
63
- await server.disconnect();
64
- }
65
- async function sessionSetTitle(title) {
66
- const sessionId = process.env.SVAMP_SESSION_ID;
67
- if (!sessionId) {
68
- console.error("SVAMP_SESSION_ID not set. This command must be run inside a Svamp session.");
69
- process.exit(1);
70
- }
71
- const configPath = getConfigPath(sessionId);
72
- const config = readConfig(configPath);
73
- config.title = title.trim();
74
- writeConfig(configPath, config);
75
- console.log(`Session title set: "${title.trim()}"`);
76
- }
77
- async function sessionSetLink(url, label) {
78
- const sessionId = process.env.SVAMP_SESSION_ID;
79
- if (!sessionId) {
80
- console.error("SVAMP_SESSION_ID not set. This command must be run inside a Svamp session.");
81
- process.exit(1);
82
- }
83
- const resolvedLabel = label?.trim() || (() => {
84
- try {
85
- return new URL(url).hostname;
86
- } catch {
87
- return "View";
88
- }
89
- })();
90
- const configPath = getConfigPath(sessionId);
91
- const config = readConfig(configPath);
92
- config.session_link = { url: url.trim(), label: resolvedLabel };
93
- writeConfig(configPath, config);
94
- console.log(`Session link set: "${resolvedLabel}" \u2192 ${url.trim()}`);
95
- }
96
- async function sessionNotify(message, level = "info") {
97
- const sessionId = process.env.SVAMP_SESSION_ID;
98
- if (!sessionId) {
99
- console.error("SVAMP_SESSION_ID not set. This command must be run inside a Svamp session.");
100
- process.exit(1);
101
- }
102
- await connectAndEmit({
103
- type: "svamp:session-notify",
104
- data: { sessionId, message, level, timestamp: Date.now() }
105
- });
106
- console.log(`Notification sent [${level}]: ${message}`);
107
- }
108
- async function sessionBroadcast(action, args) {
109
- const sessionId = process.env.SVAMP_SESSION_ID;
110
- if (!sessionId) {
111
- console.error("SVAMP_SESSION_ID not set. This command must be run inside a Svamp session.");
112
- process.exit(1);
113
- }
114
- let payload = { action };
115
- if (action === "open-canvas") {
116
- const url = args[0];
117
- if (!url) {
118
- console.error("Usage: svamp session broadcast open-canvas <url> [label]");
119
- process.exit(1);
120
- }
121
- const label = args[1] || (() => {
122
- try {
123
- return new URL(url).hostname;
124
- } catch {
125
- return "View";
126
- }
127
- })();
128
- payload = { action, url, label };
129
- } else if (action === "close-canvas") ; else if (action === "toast") {
130
- const message = args[0];
131
- if (!message) {
132
- console.error("Usage: svamp session broadcast toast <message>");
133
- process.exit(1);
134
- }
135
- payload = { action, message };
136
- } else {
137
- console.error(`Unknown broadcast action: ${action}`);
138
- console.error("Available actions: open-canvas, close-canvas, toast");
139
- process.exit(1);
140
- }
141
- await connectAndEmit({
142
- type: "svamp:session-broadcast",
143
- data: { sessionId, ...payload }
144
- });
145
- console.log(`Broadcast sent: ${action}`);
146
- }
147
- async function machineNotify(message, level = "info") {
148
- const machineId = process.env.SVAMP_MACHINE_ID;
149
- await connectAndEmit({
150
- type: "svamp:machine-notify",
151
- data: { machineId, message, level, timestamp: Date.now() }
152
- });
153
- console.log(`Machine notification sent [${level}]: ${message}`);
154
- }
155
-
156
- export { machineNotify, sessionBroadcast, sessionNotify, sessionSetLink, sessionSetTitle };