svamp-cli 0.1.74 → 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.
- package/dist/agentCommands-NVZzP_Vo.mjs +298 -0
- package/dist/cli.mjs +188 -164
- package/dist/{commands-dKkUOvUb.mjs → commands-7Iw1nFwf.mjs} +3 -3
- package/dist/{commands-BLjcT1Vl.mjs → commands-CADr1mQg.mjs} +66 -2
- package/dist/{commands-UFi0_ESV.mjs → commands-DJoYOM_1.mjs} +25 -25
- package/dist/{commands-ClMc3nSg.mjs → commands-lJ8V7MJE.mjs} +77 -141
- package/dist/index.mjs +1 -1
- package/dist/{package-CCjeil_X.mjs → package-Dpz1MLO4.mjs} +4 -3
- package/dist/{run-BgwhZgkT.mjs → run-B29grSMh.mjs} +1 -1
- package/dist/{run-6QgabuQN.mjs → run-BnFGIK0c.mjs} +321 -329
- package/dist/staticServer-B-S9sl6E.mjs +198 -0
- package/package.json +4 -3
- package/dist/agentCommands-7GGmL2zY.mjs +0 -157
|
@@ -0,0 +1,298 @@
|
|
|
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 connectToSessionService(sessionId) {
|
|
148
|
+
const ENV_FILE = join(SVAMP_HOME, ".env");
|
|
149
|
+
if (existsSync(ENV_FILE)) {
|
|
150
|
+
const lines = readFileSync(ENV_FILE, "utf-8").split("\n");
|
|
151
|
+
for (const line of lines) {
|
|
152
|
+
const m = line.match(/^([A-Z_]+)=(.*)/);
|
|
153
|
+
if (m && !process.env[m[1]]) process.env[m[1]] = m[2].replace(/^["']|["']$/g, "");
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const serverUrl = process.env.HYPHA_SERVER_URL;
|
|
157
|
+
const token = process.env.HYPHA_TOKEN;
|
|
158
|
+
if (!serverUrl || !token) {
|
|
159
|
+
console.error('No Hypha credentials. Run "svamp login" first.');
|
|
160
|
+
process.exit(1);
|
|
161
|
+
}
|
|
162
|
+
const origLog = console.log;
|
|
163
|
+
const origWarn = console.warn;
|
|
164
|
+
const origInfo = console.info;
|
|
165
|
+
console.log = () => {
|
|
166
|
+
};
|
|
167
|
+
console.warn = () => {
|
|
168
|
+
};
|
|
169
|
+
console.info = () => {
|
|
170
|
+
};
|
|
171
|
+
let server;
|
|
172
|
+
try {
|
|
173
|
+
const mod = await import('hypha-rpc');
|
|
174
|
+
const connectToServer = mod.connectToServer || mod.default?.connectToServer;
|
|
175
|
+
server = await connectToServer({ server_url: serverUrl, token, name: "svamp-agent-inbox" });
|
|
176
|
+
} catch (err) {
|
|
177
|
+
console.log = origLog;
|
|
178
|
+
console.warn = origWarn;
|
|
179
|
+
console.info = origInfo;
|
|
180
|
+
console.error(`Failed to connect to Hypha: ${err.message}`);
|
|
181
|
+
process.exit(1);
|
|
182
|
+
}
|
|
183
|
+
console.log = origLog;
|
|
184
|
+
console.warn = origWarn;
|
|
185
|
+
console.info = origInfo;
|
|
186
|
+
const svc = await server.getService(`svamp-session-${sessionId}`);
|
|
187
|
+
return { server, svc };
|
|
188
|
+
}
|
|
189
|
+
async function inboxSend(targetSessionId, opts) {
|
|
190
|
+
const sessionId = process.env.SVAMP_SESSION_ID;
|
|
191
|
+
if (!sessionId) {
|
|
192
|
+
console.error("SVAMP_SESSION_ID not set. This command must be run inside a Svamp session.");
|
|
193
|
+
process.exit(1);
|
|
194
|
+
}
|
|
195
|
+
const body = opts?.body || "";
|
|
196
|
+
if (!body) {
|
|
197
|
+
console.error("Message body is required.");
|
|
198
|
+
process.exit(1);
|
|
199
|
+
}
|
|
200
|
+
const { server, svc } = await connectToSessionService(targetSessionId);
|
|
201
|
+
try {
|
|
202
|
+
const { randomUUID } = await import('node:crypto');
|
|
203
|
+
const message = {
|
|
204
|
+
messageId: randomUUID(),
|
|
205
|
+
body,
|
|
206
|
+
timestamp: Date.now(),
|
|
207
|
+
read: false,
|
|
208
|
+
from: `agent:${sessionId}`,
|
|
209
|
+
fromSession: sessionId,
|
|
210
|
+
to: targetSessionId,
|
|
211
|
+
subject: opts?.subject,
|
|
212
|
+
urgency: opts?.urgency || "normal"
|
|
213
|
+
};
|
|
214
|
+
const result = await svc.sendInboxMessage(message);
|
|
215
|
+
console.log(`Inbox message sent to ${targetSessionId.slice(0, 8)} (id: ${result.messageId.slice(0, 8)})`);
|
|
216
|
+
} finally {
|
|
217
|
+
await server.disconnect();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
async function inboxList(opts) {
|
|
221
|
+
const sessionId = process.env.SVAMP_SESSION_ID;
|
|
222
|
+
if (!sessionId) {
|
|
223
|
+
console.error("SVAMP_SESSION_ID not set. This command must be run inside a Svamp session.");
|
|
224
|
+
process.exit(1);
|
|
225
|
+
}
|
|
226
|
+
const { server, svc } = await connectToSessionService(sessionId);
|
|
227
|
+
try {
|
|
228
|
+
const result = await svc.getInbox({ unread: opts?.unread, limit: opts?.limit });
|
|
229
|
+
const messages = result.messages;
|
|
230
|
+
if (opts?.json) {
|
|
231
|
+
console.log(JSON.stringify({ messages }, null, 2));
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
if (messages.length === 0) {
|
|
235
|
+
console.log("Inbox is empty.");
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
for (const msg of messages) {
|
|
239
|
+
const status = msg.read ? " " : "\u25CF";
|
|
240
|
+
const from = msg.from ? ` from ${msg.from}` : "";
|
|
241
|
+
const subject = msg.subject ? ` \u2014 ${msg.subject}` : "";
|
|
242
|
+
const urgencyTag = msg.urgency === "urgent" ? " [URGENT]" : "";
|
|
243
|
+
const preview = msg.body.length > 100 ? msg.body.slice(0, 97) + "..." : msg.body;
|
|
244
|
+
console.log(`${status} ${msg.messageId.slice(0, 8)}${urgencyTag}${from}${subject}: ${preview}`);
|
|
245
|
+
}
|
|
246
|
+
} finally {
|
|
247
|
+
await server.disconnect();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async function inboxReply(messageId, body) {
|
|
251
|
+
const sessionId = process.env.SVAMP_SESSION_ID;
|
|
252
|
+
if (!sessionId) {
|
|
253
|
+
console.error("SVAMP_SESSION_ID not set. This command must be run inside a Svamp session.");
|
|
254
|
+
process.exit(1);
|
|
255
|
+
}
|
|
256
|
+
const { server, svc } = await connectToSessionService(sessionId);
|
|
257
|
+
try {
|
|
258
|
+
const result = await svc.getInbox();
|
|
259
|
+
const original = result.messages.find((m) => m.messageId === messageId || m.messageId.startsWith(messageId));
|
|
260
|
+
if (!original) {
|
|
261
|
+
console.error(`Message ${messageId} not found in inbox.`);
|
|
262
|
+
process.exit(1);
|
|
263
|
+
}
|
|
264
|
+
if (!original.fromSession) {
|
|
265
|
+
console.error("Cannot reply: original message has no fromSession.");
|
|
266
|
+
process.exit(1);
|
|
267
|
+
}
|
|
268
|
+
const targetSvc = await server.getService(`svamp-session-${original.fromSession}`);
|
|
269
|
+
const { randomUUID } = await import('node:crypto');
|
|
270
|
+
const reply = {
|
|
271
|
+
messageId: randomUUID(),
|
|
272
|
+
body,
|
|
273
|
+
timestamp: Date.now(),
|
|
274
|
+
read: false,
|
|
275
|
+
from: `agent:${sessionId}`,
|
|
276
|
+
fromSession: sessionId,
|
|
277
|
+
to: original.fromSession,
|
|
278
|
+
subject: original.subject ? `Re: ${original.subject}` : void 0,
|
|
279
|
+
urgency: "normal",
|
|
280
|
+
replyTo: original.messageId,
|
|
281
|
+
threadId: original.threadId || original.messageId
|
|
282
|
+
};
|
|
283
|
+
const sendResult = await targetSvc.sendInboxMessage(reply);
|
|
284
|
+
console.log(`Reply sent to ${original.fromSession.slice(0, 8)} (id: ${sendResult.messageId.slice(0, 8)})`);
|
|
285
|
+
} finally {
|
|
286
|
+
await server.disconnect();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
async function machineNotify(message, level = "info") {
|
|
290
|
+
const machineId = process.env.SVAMP_MACHINE_ID;
|
|
291
|
+
await connectAndEmit({
|
|
292
|
+
type: "svamp:machine-notify",
|
|
293
|
+
data: { machineId, message, level, timestamp: Date.now() }
|
|
294
|
+
});
|
|
295
|
+
console.log(`Machine notification sent [${level}]: ${message}`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export { inboxList, inboxReply, inboxSend, machineNotify, sessionBroadcast, sessionNotify, sessionSetLink, sessionSetTitle };
|