@stamn/agent 0.1.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/bin/run.js +4 -0
- package/bin/run.ts +4 -0
- package/dist/chunk-3DME5XG7.js +66 -0
- package/dist/chunk-3DME5XG7.js.map +1 -0
- package/dist/chunk-B67ZSZCD.js +48 -0
- package/dist/chunk-B67ZSZCD.js.map +1 -0
- package/dist/chunk-UG6D7BON.js +372 -0
- package/dist/chunk-UG6D7BON.js.map +1 -0
- package/dist/commands/config/get.d.ts +12 -0
- package/dist/commands/config/get.js +30 -0
- package/dist/commands/config/get.js.map +1 -0
- package/dist/commands/config/index.d.ts +8 -0
- package/dist/commands/config/index.js +24 -0
- package/dist/commands/config/index.js.map +1 -0
- package/dist/commands/config/set.d.ts +13 -0
- package/dist/commands/config/set.js +39 -0
- package/dist/commands/config/set.js.map +1 -0
- package/dist/commands/spend.d.ts +21 -0
- package/dist/commands/spend.js +98 -0
- package/dist/commands/spend.js.map +1 -0
- package/dist/commands/start.d.ts +16 -0
- package/dist/commands/start.js +102 -0
- package/dist/commands/start.js.map +1 -0
- package/dist/commands/status.d.ts +8 -0
- package/dist/commands/status.js +40 -0
- package/dist/commands/status.js.map +1 -0
- package/dist/commands/stop.d.ts +8 -0
- package/dist/commands/stop.js +29 -0
- package/dist/commands/stop.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/process-HCAG5EF5.js +12 -0
- package/dist/process-HCAG5EF5.js.map +1 -0
- package/package.json +60 -0
package/bin/run.js
ADDED
package/bin/run.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// src/daemon/daemon-manager.ts
|
|
4
|
+
import {
|
|
5
|
+
existsSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
writeFileSync,
|
|
8
|
+
unlinkSync,
|
|
9
|
+
mkdirSync
|
|
10
|
+
} from "fs";
|
|
11
|
+
import { join } from "path";
|
|
12
|
+
import { homedir } from "os";
|
|
13
|
+
var DaemonManager = class {
|
|
14
|
+
pidDir;
|
|
15
|
+
pidFile;
|
|
16
|
+
constructor() {
|
|
17
|
+
this.pidDir = join(homedir(), ".config", "stamn-agent");
|
|
18
|
+
this.pidFile = join(this.pidDir, "daemon.pid");
|
|
19
|
+
}
|
|
20
|
+
isRunning() {
|
|
21
|
+
if (!existsSync(this.pidFile)) {
|
|
22
|
+
return { running: false };
|
|
23
|
+
}
|
|
24
|
+
const raw = readFileSync(this.pidFile, "utf-8").trim();
|
|
25
|
+
const pid = parseInt(raw, 10);
|
|
26
|
+
if (isNaN(pid)) {
|
|
27
|
+
this.removePid();
|
|
28
|
+
return { running: false };
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
process.kill(pid, 0);
|
|
32
|
+
return { running: true, pid };
|
|
33
|
+
} catch {
|
|
34
|
+
this.removePid();
|
|
35
|
+
return { running: false };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
writePid(pid) {
|
|
39
|
+
mkdirSync(this.pidDir, { recursive: true });
|
|
40
|
+
writeFileSync(this.pidFile, String(pid), "utf-8");
|
|
41
|
+
}
|
|
42
|
+
removePid() {
|
|
43
|
+
try {
|
|
44
|
+
unlinkSync(this.pidFile);
|
|
45
|
+
} catch {
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
stop() {
|
|
49
|
+
const { running, pid } = this.isRunning();
|
|
50
|
+
if (!running || !pid) return false;
|
|
51
|
+
try {
|
|
52
|
+
process.kill(pid, "SIGTERM");
|
|
53
|
+
return true;
|
|
54
|
+
} catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
get pidFilePath() {
|
|
59
|
+
return this.pidFile;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
DaemonManager
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=chunk-3DME5XG7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/daemon/daemon-manager.ts"],"sourcesContent":["import {\n existsSync,\n readFileSync,\n writeFileSync,\n unlinkSync,\n mkdirSync,\n} from 'fs';\nimport { join } from 'path';\nimport { homedir } from 'os';\n\nexport class DaemonManager {\n private readonly pidDir: string;\n private readonly pidFile: string;\n\n constructor() {\n this.pidDir = join(homedir(), '.config', 'stamn-agent');\n this.pidFile = join(this.pidDir, 'daemon.pid');\n }\n\n isRunning(): { running: boolean; pid?: number } {\n if (!existsSync(this.pidFile)) {\n return { running: false };\n }\n\n const raw = readFileSync(this.pidFile, 'utf-8').trim();\n const pid = parseInt(raw, 10);\n\n if (isNaN(pid)) {\n this.removePid();\n return { running: false };\n }\n\n try {\n process.kill(pid, 0);\n return { running: true, pid };\n } catch {\n // Process not alive — stale PID file\n this.removePid();\n return { running: false };\n }\n }\n\n writePid(pid: number): void {\n mkdirSync(this.pidDir, { recursive: true });\n writeFileSync(this.pidFile, String(pid), 'utf-8');\n }\n\n removePid(): void {\n try {\n unlinkSync(this.pidFile);\n } catch {\n // Already gone\n }\n }\n\n stop(): boolean {\n const { running, pid } = this.isRunning();\n if (!running || !pid) return false;\n\n try {\n process.kill(pid, 'SIGTERM');\n return true;\n } catch {\n return false;\n }\n }\n\n get pidFilePath(): string {\n return this.pidFile;\n }\n}\n"],"mappings":";;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,YAAY;AACrB,SAAS,eAAe;AAEjB,IAAM,gBAAN,MAAoB;AAAA,EACR;AAAA,EACA;AAAA,EAEjB,cAAc;AACZ,SAAK,SAAS,KAAK,QAAQ,GAAG,WAAW,aAAa;AACtD,SAAK,UAAU,KAAK,KAAK,QAAQ,YAAY;AAAA,EAC/C;AAAA,EAEA,YAAgD;AAC9C,QAAI,CAAC,WAAW,KAAK,OAAO,GAAG;AAC7B,aAAO,EAAE,SAAS,MAAM;AAAA,IAC1B;AAEA,UAAM,MAAM,aAAa,KAAK,SAAS,OAAO,EAAE,KAAK;AACrD,UAAM,MAAM,SAAS,KAAK,EAAE;AAE5B,QAAI,MAAM,GAAG,GAAG;AACd,WAAK,UAAU;AACf,aAAO,EAAE,SAAS,MAAM;AAAA,IAC1B;AAEA,QAAI;AACF,cAAQ,KAAK,KAAK,CAAC;AACnB,aAAO,EAAE,SAAS,MAAM,IAAI;AAAA,IAC9B,QAAQ;AAEN,WAAK,UAAU;AACf,aAAO,EAAE,SAAS,MAAM;AAAA,IAC1B;AAAA,EACF;AAAA,EAEA,SAAS,KAAmB;AAC1B,cAAU,KAAK,QAAQ,EAAE,WAAW,KAAK,CAAC;AAC1C,kBAAc,KAAK,SAAS,OAAO,GAAG,GAAG,OAAO;AAAA,EAClD;AAAA,EAEA,YAAkB;AAChB,QAAI;AACF,iBAAW,KAAK,OAAO;AAAA,IACzB,QAAQ;AAAA,IAER;AAAA,EACF;AAAA,EAEA,OAAgB;AACd,UAAM,EAAE,SAAS,IAAI,IAAI,KAAK,UAAU;AACxC,QAAI,CAAC,WAAW,CAAC,IAAK,QAAO;AAE7B,QAAI;AACF,cAAQ,KAAK,KAAK,SAAS;AAC3B,aAAO;AAAA,IACT,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEA,IAAI,cAAsB;AACxB,WAAO,KAAK;AAAA,EACd;AACF;","names":[]}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// src/config/config-schema.ts
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
var configSchema = z.object({
|
|
6
|
+
serverUrl: z.string().url().default("http://localhost:3001"),
|
|
7
|
+
apiKey: z.string().min(1).optional(),
|
|
8
|
+
agentId: z.string().uuid().optional(),
|
|
9
|
+
logLevel: z.enum(["trace", "debug", "info", "warn", "error", "fatal"]).default("info"),
|
|
10
|
+
heartbeatIntervalMs: z.number().int().positive().default(3e4),
|
|
11
|
+
wsReconnectBaseMs: z.number().int().positive().default(1e3),
|
|
12
|
+
wsReconnectMaxMs: z.number().int().positive().default(3e4)
|
|
13
|
+
});
|
|
14
|
+
var CONFIG_DEFAULTS = configSchema.parse({});
|
|
15
|
+
|
|
16
|
+
// src/config/config-store.ts
|
|
17
|
+
import Conf from "conf";
|
|
18
|
+
var ConfigStore = class {
|
|
19
|
+
store;
|
|
20
|
+
constructor() {
|
|
21
|
+
this.store = new Conf({
|
|
22
|
+
projectName: "stamn-agent",
|
|
23
|
+
defaults: CONFIG_DEFAULTS
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
get(key) {
|
|
27
|
+
return this.store.get(key);
|
|
28
|
+
}
|
|
29
|
+
set(key, value) {
|
|
30
|
+
configSchema.partial().parse({ [key]: value });
|
|
31
|
+
this.store.set(key, value);
|
|
32
|
+
}
|
|
33
|
+
getAll() {
|
|
34
|
+
return configSchema.parse(this.store.store);
|
|
35
|
+
}
|
|
36
|
+
clear() {
|
|
37
|
+
this.store.clear();
|
|
38
|
+
}
|
|
39
|
+
get path() {
|
|
40
|
+
return this.store.path;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export {
|
|
45
|
+
configSchema,
|
|
46
|
+
ConfigStore
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=chunk-B67ZSZCD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/config/config-schema.ts","../src/config/config-store.ts"],"sourcesContent":["import { z } from 'zod';\n\nexport const configSchema = z.object({\n serverUrl: z.string().url().default('http://localhost:3001'),\n apiKey: z.string().min(1).optional(),\n agentId: z.string().uuid().optional(),\n logLevel: z\n .enum(['trace', 'debug', 'info', 'warn', 'error', 'fatal'])\n .default('info'),\n heartbeatIntervalMs: z.number().int().positive().default(30_000),\n wsReconnectBaseMs: z.number().int().positive().default(1_000),\n wsReconnectMaxMs: z.number().int().positive().default(30_000),\n});\n\nexport type AgentConfig = z.infer<typeof configSchema>;\n\nexport const CONFIG_DEFAULTS: AgentConfig = configSchema.parse({});\n","import Conf from 'conf';\nimport { configSchema, CONFIG_DEFAULTS, type AgentConfig } from './config-schema.js';\n\nexport class ConfigStore {\n private store: Conf<AgentConfig>;\n\n constructor() {\n this.store = new Conf<AgentConfig>({\n projectName: 'stamn-agent',\n defaults: CONFIG_DEFAULTS,\n });\n }\n\n get<K extends keyof AgentConfig>(key: K): AgentConfig[K] {\n return this.store.get(key);\n }\n\n set<K extends keyof AgentConfig>(key: K, value: AgentConfig[K]): void {\n configSchema.partial().parse({ [key]: value });\n this.store.set(key, value);\n }\n\n getAll(): AgentConfig {\n return configSchema.parse(this.store.store);\n }\n\n clear(): void {\n this.store.clear();\n }\n\n get path(): string {\n return this.store.path;\n }\n}\n"],"mappings":";;;AAAA,SAAS,SAAS;AAEX,IAAM,eAAe,EAAE,OAAO;AAAA,EACnC,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,uBAAuB;AAAA,EAC3D,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,SAAS;AAAA,EACnC,SAAS,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS;AAAA,EACpC,UAAU,EACP,KAAK,CAAC,SAAS,SAAS,QAAQ,QAAQ,SAAS,OAAO,CAAC,EACzD,QAAQ,MAAM;AAAA,EACjB,qBAAqB,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,GAAM;AAAA,EAC/D,mBAAmB,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,GAAK;AAAA,EAC5D,kBAAkB,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,GAAM;AAC9D,CAAC;AAIM,IAAM,kBAA+B,aAAa,MAAM,CAAC,CAAC;;;AChBjE,OAAO,UAAU;AAGV,IAAM,cAAN,MAAkB;AAAA,EACf;AAAA,EAER,cAAc;AACZ,SAAK,QAAQ,IAAI,KAAkB;AAAA,MACjC,aAAa;AAAA,MACb,UAAU;AAAA,IACZ,CAAC;AAAA,EACH;AAAA,EAEA,IAAiC,KAAwB;AACvD,WAAO,KAAK,MAAM,IAAI,GAAG;AAAA,EAC3B;AAAA,EAEA,IAAiC,KAAQ,OAA6B;AACpE,iBAAa,QAAQ,EAAE,MAAM,EAAE,CAAC,GAAG,GAAG,MAAM,CAAC;AAC7C,SAAK,MAAM,IAAI,KAAK,KAAK;AAAA,EAC3B;AAAA,EAEA,SAAsB;AACpB,WAAO,aAAa,MAAM,KAAK,MAAM,KAAK;AAAA,EAC5C;AAAA,EAEA,QAAc;AACZ,SAAK,MAAM,MAAM;AAAA,EACnB;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK,MAAM;AAAA,EACpB;AACF;","names":[]}
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// src/logging/logger.ts
|
|
4
|
+
import pino from "pino";
|
|
5
|
+
function createLogger(config) {
|
|
6
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
7
|
+
return pino({
|
|
8
|
+
level: config.logLevel,
|
|
9
|
+
transport: isDev ? {
|
|
10
|
+
target: "pino-pretty",
|
|
11
|
+
options: { colorize: true, translateTime: "SYS:HH:MM:ss" }
|
|
12
|
+
} : void 0
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/ws/ws-client.ts
|
|
17
|
+
import WebSocket from "ws";
|
|
18
|
+
|
|
19
|
+
// src/ws/heartbeat.ts
|
|
20
|
+
var Heartbeat = class {
|
|
21
|
+
constructor(sender, agentId, intervalMs, startTime) {
|
|
22
|
+
this.sender = sender;
|
|
23
|
+
this.agentId = agentId;
|
|
24
|
+
this.intervalMs = intervalMs;
|
|
25
|
+
this.startTime = startTime;
|
|
26
|
+
}
|
|
27
|
+
timer = null;
|
|
28
|
+
missedPongs = 0;
|
|
29
|
+
maxMissedPongs = 3;
|
|
30
|
+
start() {
|
|
31
|
+
this.missedPongs = 0;
|
|
32
|
+
this.timer = setInterval(() => {
|
|
33
|
+
if (this.missedPongs >= this.maxMissedPongs) {
|
|
34
|
+
this.sender.reconnect();
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const payload = {
|
|
38
|
+
agentId: this.agentId,
|
|
39
|
+
uptimeSeconds: Math.floor((Date.now() - this.startTime) / 1e3),
|
|
40
|
+
memoryUsageMb: Math.round(process.memoryUsage().rss / 1024 / 1024)
|
|
41
|
+
};
|
|
42
|
+
this.sender.send("agent:heartbeat", payload);
|
|
43
|
+
this.missedPongs++;
|
|
44
|
+
}, this.intervalMs);
|
|
45
|
+
}
|
|
46
|
+
onAck() {
|
|
47
|
+
this.missedPongs = 0;
|
|
48
|
+
}
|
|
49
|
+
stop() {
|
|
50
|
+
if (this.timer) {
|
|
51
|
+
clearInterval(this.timer);
|
|
52
|
+
this.timer = null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/ws/message-handler.ts
|
|
58
|
+
import { z } from "zod";
|
|
59
|
+
var wsMessageSchema = z.object({
|
|
60
|
+
event: z.string(),
|
|
61
|
+
data: z.unknown()
|
|
62
|
+
});
|
|
63
|
+
var commandPayloadSchema = z.object({
|
|
64
|
+
commandId: z.string(),
|
|
65
|
+
command: z.enum(["pause", "resume", "update_config", "shutdown"]),
|
|
66
|
+
params: z.record(z.unknown()).optional()
|
|
67
|
+
});
|
|
68
|
+
var MessageHandler = class {
|
|
69
|
+
constructor(logger, onCommand) {
|
|
70
|
+
this.logger = logger;
|
|
71
|
+
this.onCommand = onCommand;
|
|
72
|
+
}
|
|
73
|
+
handle(raw) {
|
|
74
|
+
let parsed;
|
|
75
|
+
try {
|
|
76
|
+
parsed = JSON.parse(raw);
|
|
77
|
+
} catch {
|
|
78
|
+
this.logger.warn({ raw }, "Invalid JSON received");
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const result = wsMessageSchema.safeParse(parsed);
|
|
82
|
+
if (!result.success) {
|
|
83
|
+
this.logger.warn({ error: result.error.message }, "Invalid WS message");
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const { event, data } = result.data;
|
|
87
|
+
switch (event) {
|
|
88
|
+
case "server:authenticated":
|
|
89
|
+
case "server:auth_error":
|
|
90
|
+
case "server:heartbeat_ack":
|
|
91
|
+
case "server:event":
|
|
92
|
+
case "server:spend_approved":
|
|
93
|
+
case "server:spend_denied":
|
|
94
|
+
return {
|
|
95
|
+
type: event,
|
|
96
|
+
payload: data
|
|
97
|
+
};
|
|
98
|
+
case "server:command": {
|
|
99
|
+
const cmdResult = commandPayloadSchema.safeParse(data);
|
|
100
|
+
if (!cmdResult.success) {
|
|
101
|
+
this.logger.warn(
|
|
102
|
+
{ error: cmdResult.error.message },
|
|
103
|
+
"Invalid command payload"
|
|
104
|
+
);
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
const cmd = cmdResult.data;
|
|
108
|
+
this.logger.info(
|
|
109
|
+
{ command: cmd.command, commandId: cmd.commandId },
|
|
110
|
+
"Received command"
|
|
111
|
+
);
|
|
112
|
+
this.onCommand(cmd.command, cmd.params);
|
|
113
|
+
return { type: "server:command", payload: cmd };
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
this.logger.debug({ type: event }, "Unknown message type");
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// src/ws/ws-client.ts
|
|
123
|
+
var WSClient = class {
|
|
124
|
+
constructor(options) {
|
|
125
|
+
this.options = options;
|
|
126
|
+
this.handler = new MessageHandler(options.logger, options.onCommand);
|
|
127
|
+
}
|
|
128
|
+
ws = null;
|
|
129
|
+
heartbeat = null;
|
|
130
|
+
handler;
|
|
131
|
+
reconnectAttempt = 0;
|
|
132
|
+
reconnectTimer = null;
|
|
133
|
+
isShuttingDown = false;
|
|
134
|
+
authenticated = false;
|
|
135
|
+
startTime = Date.now();
|
|
136
|
+
spendListeners = /* @__PURE__ */ new Map();
|
|
137
|
+
onSpendResult(requestId, callback) {
|
|
138
|
+
this.spendListeners.set(requestId, callback);
|
|
139
|
+
}
|
|
140
|
+
removeSpendListener(requestId) {
|
|
141
|
+
this.spendListeners.delete(requestId);
|
|
142
|
+
}
|
|
143
|
+
connect() {
|
|
144
|
+
if (this.isShuttingDown) return;
|
|
145
|
+
const config = this.options.config;
|
|
146
|
+
const wsUrl = config.serverUrl.replace(/^http:/, "ws:").replace(/^https:/, "wss:");
|
|
147
|
+
const url = `${wsUrl}/ws/agent`;
|
|
148
|
+
this.options.logger.info({ url }, "Connecting to server...");
|
|
149
|
+
this.ws = new WebSocket(url);
|
|
150
|
+
this.ws.on("open", () => {
|
|
151
|
+
this.options.logger.info("WebSocket connected, authenticating...");
|
|
152
|
+
this.reconnectAttempt = 0;
|
|
153
|
+
const payload = {
|
|
154
|
+
agentId: config.agentId,
|
|
155
|
+
apiKey: config.apiKey ?? ""
|
|
156
|
+
};
|
|
157
|
+
this.send("agent:authenticate", payload);
|
|
158
|
+
});
|
|
159
|
+
this.ws.on("message", (data) => {
|
|
160
|
+
const raw = data.toString();
|
|
161
|
+
const result = this.handler.handle(raw);
|
|
162
|
+
if (!result) return;
|
|
163
|
+
switch (result.type) {
|
|
164
|
+
case "server:authenticated": {
|
|
165
|
+
const payload = result.payload;
|
|
166
|
+
this.authenticated = true;
|
|
167
|
+
this.options.logger.info(
|
|
168
|
+
{ serverVersion: payload.serverVersion },
|
|
169
|
+
`Agent ${payload.agentId} authenticated`
|
|
170
|
+
);
|
|
171
|
+
this.heartbeat = new Heartbeat(
|
|
172
|
+
this,
|
|
173
|
+
config.agentId,
|
|
174
|
+
config.heartbeatIntervalMs,
|
|
175
|
+
this.startTime
|
|
176
|
+
);
|
|
177
|
+
this.heartbeat.start();
|
|
178
|
+
this.sendStatusReport("online");
|
|
179
|
+
this.options.onConnected();
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
case "server:auth_error": {
|
|
183
|
+
const payload = result.payload;
|
|
184
|
+
this.options.logger.error(
|
|
185
|
+
{ reason: payload.reason },
|
|
186
|
+
"Authentication failed"
|
|
187
|
+
);
|
|
188
|
+
this.isShuttingDown = true;
|
|
189
|
+
this.ws?.close(4003, "Auth failed");
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
case "server:heartbeat_ack":
|
|
193
|
+
this.heartbeat?.onAck();
|
|
194
|
+
break;
|
|
195
|
+
case "server:spend_approved": {
|
|
196
|
+
const sp = result.payload;
|
|
197
|
+
const cb = this.spendListeners.get(sp.requestId);
|
|
198
|
+
if (cb) {
|
|
199
|
+
cb("approved", sp);
|
|
200
|
+
this.spendListeners.delete(sp.requestId);
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "server:spend_denied": {
|
|
205
|
+
const sd = result.payload;
|
|
206
|
+
const dcb = this.spendListeners.get(sd.requestId);
|
|
207
|
+
if (dcb) {
|
|
208
|
+
dcb("denied", sd);
|
|
209
|
+
this.spendListeners.delete(sd.requestId);
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
case "server:event":
|
|
214
|
+
this.options.logger.debug({ event: result.payload }, "Server event");
|
|
215
|
+
break;
|
|
216
|
+
case "server:command":
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
this.ws.on("close", (code, reason) => {
|
|
221
|
+
this.authenticated = false;
|
|
222
|
+
this.heartbeat?.stop();
|
|
223
|
+
this.heartbeat = null;
|
|
224
|
+
if (this.isShuttingDown) {
|
|
225
|
+
this.options.logger.info("Connection closed");
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
this.options.logger.warn(
|
|
229
|
+
{ code, reason: reason.toString() },
|
|
230
|
+
"Connection lost"
|
|
231
|
+
);
|
|
232
|
+
this.options.onDisconnect();
|
|
233
|
+
this.scheduleReconnect();
|
|
234
|
+
});
|
|
235
|
+
this.ws.on("error", (err) => {
|
|
236
|
+
this.options.logger.error({ err: err.message }, "WebSocket error");
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
reconnect() {
|
|
240
|
+
this.heartbeat?.stop();
|
|
241
|
+
this.heartbeat = null;
|
|
242
|
+
this.ws?.close();
|
|
243
|
+
}
|
|
244
|
+
send(type, payload) {
|
|
245
|
+
if (this.ws?.readyState !== WebSocket.OPEN) return;
|
|
246
|
+
this.ws.send(JSON.stringify({ event: type, data: payload }));
|
|
247
|
+
}
|
|
248
|
+
disconnect() {
|
|
249
|
+
this.isShuttingDown = true;
|
|
250
|
+
if (this.reconnectTimer) {
|
|
251
|
+
clearTimeout(this.reconnectTimer);
|
|
252
|
+
this.reconnectTimer = null;
|
|
253
|
+
}
|
|
254
|
+
if (this.authenticated) {
|
|
255
|
+
this.sendStatusReport("shutting_down");
|
|
256
|
+
}
|
|
257
|
+
this.heartbeat?.stop();
|
|
258
|
+
this.ws?.close(1e3, "Client shutdown");
|
|
259
|
+
}
|
|
260
|
+
get isAuthenticated() {
|
|
261
|
+
return this.authenticated;
|
|
262
|
+
}
|
|
263
|
+
sendStatusReport(status) {
|
|
264
|
+
const payload = {
|
|
265
|
+
agentId: this.options.config.agentId,
|
|
266
|
+
status,
|
|
267
|
+
version: "0.0.0"
|
|
268
|
+
};
|
|
269
|
+
this.send("agent:status_report", payload);
|
|
270
|
+
}
|
|
271
|
+
scheduleReconnect() {
|
|
272
|
+
const { wsReconnectBaseMs, wsReconnectMaxMs } = this.options.config;
|
|
273
|
+
const delay = Math.min(
|
|
274
|
+
wsReconnectBaseMs * 2 ** this.reconnectAttempt,
|
|
275
|
+
wsReconnectMaxMs
|
|
276
|
+
);
|
|
277
|
+
const jitter = Math.random() * delay * 0.1;
|
|
278
|
+
const totalDelay = Math.round(delay + jitter);
|
|
279
|
+
this.reconnectAttempt++;
|
|
280
|
+
this.options.logger.info(
|
|
281
|
+
{ attempt: this.reconnectAttempt, delayMs: totalDelay },
|
|
282
|
+
"Reconnecting..."
|
|
283
|
+
);
|
|
284
|
+
this.reconnectTimer = setTimeout(() => {
|
|
285
|
+
this.reconnectTimer = null;
|
|
286
|
+
this.connect();
|
|
287
|
+
}, totalDelay);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
// src/spend/spend-client.ts
|
|
292
|
+
import { randomUUID } from "crypto";
|
|
293
|
+
var SPEND_TIMEOUT_MS = 3e4;
|
|
294
|
+
var SpendClient = class {
|
|
295
|
+
constructor(wsClient, logger) {
|
|
296
|
+
this.wsClient = wsClient;
|
|
297
|
+
this.logger = logger;
|
|
298
|
+
}
|
|
299
|
+
async request(params) {
|
|
300
|
+
const requestId = randomUUID();
|
|
301
|
+
const payload = {
|
|
302
|
+
requestId,
|
|
303
|
+
amountCents: params.amountCents,
|
|
304
|
+
currency: "USDC",
|
|
305
|
+
category: params.category,
|
|
306
|
+
rail: params.rail,
|
|
307
|
+
vendor: params.vendor,
|
|
308
|
+
description: params.description,
|
|
309
|
+
recipientAgentId: params.recipientAgentId,
|
|
310
|
+
recipientAddress: params.recipientAddress
|
|
311
|
+
};
|
|
312
|
+
this.logger.info(
|
|
313
|
+
{
|
|
314
|
+
requestId,
|
|
315
|
+
amountCents: params.amountCents,
|
|
316
|
+
vendor: params.vendor,
|
|
317
|
+
category: params.category
|
|
318
|
+
},
|
|
319
|
+
`Requesting spend: ${params.description}`
|
|
320
|
+
);
|
|
321
|
+
return new Promise((resolve) => {
|
|
322
|
+
const timeout = setTimeout(() => {
|
|
323
|
+
this.wsClient.removeSpendListener(requestId);
|
|
324
|
+
this.logger.error({ requestId }, "Spend request timed out");
|
|
325
|
+
resolve({
|
|
326
|
+
approved: false,
|
|
327
|
+
reason: "Request timed out",
|
|
328
|
+
code: "timeout"
|
|
329
|
+
});
|
|
330
|
+
}, SPEND_TIMEOUT_MS);
|
|
331
|
+
this.wsClient.onSpendResult(requestId, (type, result) => {
|
|
332
|
+
clearTimeout(timeout);
|
|
333
|
+
if (type === "approved") {
|
|
334
|
+
const approved = result;
|
|
335
|
+
this.logger.info(
|
|
336
|
+
{
|
|
337
|
+
requestId,
|
|
338
|
+
ledgerEntryId: approved.ledgerEntryId,
|
|
339
|
+
remainingBalanceCents: approved.remainingBalanceCents
|
|
340
|
+
},
|
|
341
|
+
"Spend approved"
|
|
342
|
+
);
|
|
343
|
+
resolve({
|
|
344
|
+
approved: true,
|
|
345
|
+
ledgerEntryId: approved.ledgerEntryId,
|
|
346
|
+
transactionHash: approved.transactionHash,
|
|
347
|
+
remainingBalanceCents: approved.remainingBalanceCents
|
|
348
|
+
});
|
|
349
|
+
} else {
|
|
350
|
+
const denied = result;
|
|
351
|
+
this.logger.warn(
|
|
352
|
+
{ requestId, code: denied.code, reason: denied.reason },
|
|
353
|
+
"Spend denied"
|
|
354
|
+
);
|
|
355
|
+
resolve({
|
|
356
|
+
approved: false,
|
|
357
|
+
reason: denied.reason,
|
|
358
|
+
code: denied.code
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
this.wsClient.send("agent:spend_request", payload);
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
export {
|
|
368
|
+
createLogger,
|
|
369
|
+
WSClient,
|
|
370
|
+
SpendClient
|
|
371
|
+
};
|
|
372
|
+
//# sourceMappingURL=chunk-UG6D7BON.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/logging/logger.ts","../src/ws/ws-client.ts","../src/ws/heartbeat.ts","../src/ws/message-handler.ts","../src/spend/spend-client.ts"],"sourcesContent":["import pino from 'pino';\nimport type { AgentConfig } from '../config/config-schema.js';\n\nexport function createLogger(\n config: Pick<AgentConfig, 'logLevel'>,\n): pino.Logger {\n const isDev = process.env.NODE_ENV !== 'production';\n\n return pino({\n level: config.logLevel,\n transport: isDev\n ? {\n target: 'pino-pretty',\n options: { colorize: true, translateTime: 'SYS:HH:MM:ss' },\n }\n : undefined,\n });\n}\n","import WebSocket from 'ws';\nimport type {\n AuthenticatePayload,\n AuthenticatedPayload,\n AuthErrorPayload,\n StatusReportPayload,\n SpendApprovedPayload,\n SpendDeniedPayload,\n} from '@repo/types';\nimport type { Logger } from 'pino';\nimport type { AgentConfig } from '../config/config-schema.js';\nimport { Heartbeat, type HeartbeatSender } from './heartbeat.js';\nimport { MessageHandler } from './message-handler.js';\n\nexport type SpendResultCallback = (\n type: 'approved' | 'denied',\n payload: SpendApprovedPayload | SpendDeniedPayload,\n) => void;\n\nexport interface WSClientOptions {\n config: AgentConfig;\n logger: Logger;\n onCommand: (command: string, params?: Record<string, unknown>) => void;\n onDisconnect: () => void;\n onConnected: () => void;\n}\n\nexport class WSClient implements HeartbeatSender {\n private ws: WebSocket | null = null;\n private heartbeat: Heartbeat | null = null;\n private handler: MessageHandler;\n private reconnectAttempt = 0;\n private reconnectTimer: ReturnType<typeof setTimeout> | null = null;\n private isShuttingDown = false;\n private authenticated = false;\n private readonly startTime = Date.now();\n private spendListeners = new Map<string, SpendResultCallback>();\n\n constructor(private options: WSClientOptions) {\n this.handler = new MessageHandler(options.logger, options.onCommand);\n }\n\n onSpendResult(requestId: string, callback: SpendResultCallback): void {\n this.spendListeners.set(requestId, callback);\n }\n\n removeSpendListener(requestId: string): void {\n this.spendListeners.delete(requestId);\n }\n\n connect(): void {\n if (this.isShuttingDown) return;\n\n const config = this.options.config;\n const wsUrl = config.serverUrl\n .replace(/^http:/, 'ws:')\n .replace(/^https:/, 'wss:');\n const url = `${wsUrl}/ws/agent`;\n\n this.options.logger.info({ url }, 'Connecting to server...');\n\n this.ws = new WebSocket(url);\n\n this.ws.on('open', () => {\n this.options.logger.info('WebSocket connected, authenticating...');\n this.reconnectAttempt = 0;\n\n const payload: AuthenticatePayload = {\n agentId: config.agentId!,\n apiKey: config.apiKey ?? '',\n };\n\n this.send('agent:authenticate', payload);\n });\n\n this.ws.on('message', (data: WebSocket.RawData) => {\n const raw = data.toString();\n const result = this.handler.handle(raw);\n if (!result) return;\n\n switch (result.type) {\n case 'server:authenticated': {\n const payload = result.payload as AuthenticatedPayload;\n this.authenticated = true;\n this.options.logger.info(\n { serverVersion: payload.serverVersion },\n `Agent ${payload.agentId} authenticated`,\n );\n\n this.heartbeat = new Heartbeat(\n this,\n config.agentId!,\n config.heartbeatIntervalMs,\n this.startTime,\n );\n this.heartbeat.start();\n\n this.sendStatusReport('online');\n this.options.onConnected();\n break;\n }\n\n case 'server:auth_error': {\n const payload = result.payload as AuthErrorPayload;\n this.options.logger.error(\n { reason: payload.reason },\n 'Authentication failed',\n );\n this.isShuttingDown = true;\n this.ws?.close(4003, 'Auth failed');\n break;\n }\n\n case 'server:heartbeat_ack':\n this.heartbeat?.onAck();\n break;\n\n case 'server:spend_approved': {\n const sp = result.payload as SpendApprovedPayload;\n const cb = this.spendListeners.get(sp.requestId);\n if (cb) {\n cb('approved', sp);\n this.spendListeners.delete(sp.requestId);\n }\n break;\n }\n\n case 'server:spend_denied': {\n const sd = result.payload as SpendDeniedPayload;\n const dcb = this.spendListeners.get(sd.requestId);\n if (dcb) {\n dcb('denied', sd);\n this.spendListeners.delete(sd.requestId);\n }\n break;\n }\n\n case 'server:event':\n this.options.logger.debug({ event: result.payload }, 'Server event');\n break;\n\n case 'server:command':\n // Already handled by MessageHandler → onCommand callback\n break;\n }\n });\n\n this.ws.on('close', (code: number, reason: Buffer) => {\n this.authenticated = false;\n this.heartbeat?.stop();\n this.heartbeat = null;\n\n if (this.isShuttingDown) {\n this.options.logger.info('Connection closed');\n return;\n }\n\n this.options.logger.warn(\n { code, reason: reason.toString() },\n 'Connection lost',\n );\n this.options.onDisconnect();\n this.scheduleReconnect();\n });\n\n this.ws.on('error', (err: Error) => {\n this.options.logger.error({ err: err.message }, 'WebSocket error');\n });\n }\n\n reconnect(): void {\n this.heartbeat?.stop();\n this.heartbeat = null;\n this.ws?.close();\n }\n\n send<T>(type: string, payload: T): void {\n if (this.ws?.readyState !== WebSocket.OPEN) return;\n\n // NestJS @SubscribeMessage expects {event, data} format\n this.ws.send(JSON.stringify({ event: type, data: payload }));\n }\n\n disconnect(): void {\n this.isShuttingDown = true;\n\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = null;\n }\n\n if (this.authenticated) {\n this.sendStatusReport('shutting_down');\n }\n\n this.heartbeat?.stop();\n this.ws?.close(1000, 'Client shutdown');\n }\n\n get isAuthenticated(): boolean {\n return this.authenticated;\n }\n\n private sendStatusReport(status: StatusReportPayload['status']): void {\n const payload: StatusReportPayload = {\n agentId: this.options.config.agentId!,\n status,\n version: '0.0.0',\n };\n this.send('agent:status_report', payload);\n }\n\n private scheduleReconnect(): void {\n const { wsReconnectBaseMs, wsReconnectMaxMs } = this.options.config;\n const delay = Math.min(\n wsReconnectBaseMs * 2 ** this.reconnectAttempt,\n wsReconnectMaxMs,\n );\n const jitter = Math.random() * delay * 0.1;\n const totalDelay = Math.round(delay + jitter);\n\n this.reconnectAttempt++;\n this.options.logger.info(\n { attempt: this.reconnectAttempt, delayMs: totalDelay },\n 'Reconnecting...',\n );\n\n this.reconnectTimer = setTimeout(() => {\n this.reconnectTimer = null;\n this.connect();\n }, totalDelay);\n }\n}\n","import type { HeartbeatPayload } from '@repo/types';\n\nexport interface HeartbeatSender {\n send<T>(type: string, payload: T): void;\n reconnect(): void;\n}\n\nexport class Heartbeat {\n private timer: ReturnType<typeof setInterval> | null = null;\n private missedPongs = 0;\n private readonly maxMissedPongs = 3;\n\n constructor(\n private sender: HeartbeatSender,\n private agentId: string,\n private intervalMs: number,\n private startTime: number,\n ) {}\n\n start(): void {\n this.missedPongs = 0;\n this.timer = setInterval(() => {\n if (this.missedPongs >= this.maxMissedPongs) {\n this.sender.reconnect();\n return;\n }\n\n const payload: HeartbeatPayload = {\n agentId: this.agentId,\n uptimeSeconds: Math.floor((Date.now() - this.startTime) / 1000),\n memoryUsageMb: Math.round(process.memoryUsage().rss / 1024 / 1024),\n };\n\n this.sender.send('agent:heartbeat', payload);\n this.missedPongs++;\n }, this.intervalMs);\n }\n\n onAck(): void {\n this.missedPongs = 0;\n }\n\n stop(): void {\n if (this.timer) {\n clearInterval(this.timer);\n this.timer = null;\n }\n }\n}\n","import type { Logger } from 'pino';\nimport { z } from 'zod';\nimport type { ServerToAgentMessageType, CommandPayload } from '@repo/types';\n\n// NestJS WS sends {event, data} format\nconst wsMessageSchema = z.object({\n event: z.string(),\n data: z.unknown(),\n});\n\nconst commandPayloadSchema = z.object({\n commandId: z.string(),\n command: z.enum(['pause', 'resume', 'update_config', 'shutdown']),\n params: z.record(z.unknown()).optional(),\n});\n\nexport class MessageHandler {\n constructor(\n private logger: Logger,\n private onCommand: (\n command: string,\n params?: Record<string, unknown>,\n ) => void,\n ) {}\n\n handle(\n raw: string,\n ): { type: ServerToAgentMessageType; payload: unknown } | null {\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch {\n this.logger.warn({ raw }, 'Invalid JSON received');\n return null;\n }\n\n const result = wsMessageSchema.safeParse(parsed);\n if (!result.success) {\n this.logger.warn({ error: result.error.message }, 'Invalid WS message');\n return null;\n }\n\n const { event, data } = result.data;\n\n switch (event) {\n case 'server:authenticated':\n case 'server:auth_error':\n case 'server:heartbeat_ack':\n case 'server:event':\n case 'server:spend_approved':\n case 'server:spend_denied':\n return {\n type: event as ServerToAgentMessageType,\n payload: data,\n };\n\n case 'server:command': {\n const cmdResult = commandPayloadSchema.safeParse(data);\n if (!cmdResult.success) {\n this.logger.warn(\n { error: cmdResult.error.message },\n 'Invalid command payload',\n );\n return null;\n }\n\n const cmd = cmdResult.data as CommandPayload;\n this.logger.info(\n { command: cmd.command, commandId: cmd.commandId },\n 'Received command',\n );\n this.onCommand(cmd.command, cmd.params);\n return { type: 'server:command', payload: cmd };\n }\n\n default:\n this.logger.debug({ type: event }, 'Unknown message type');\n return null;\n }\n }\n}\n","import { randomUUID } from 'crypto';\nimport type { Logger } from 'pino';\nimport type {\n SpendRequestPayload,\n SpendApprovedPayload,\n SpendDeniedPayload,\n LedgerCategory,\n LedgerRail,\n} from '@repo/types';\nimport type { WSClient } from '../ws/ws-client.js';\n\nexport type SpendResult =\n | {\n approved: true;\n ledgerEntryId: string;\n transactionHash?: string;\n remainingBalanceCents: number;\n }\n | { approved: false; reason: string; code: string };\n\nexport interface SpendParams {\n amountCents: number;\n category: LedgerCategory;\n rail: LedgerRail;\n vendor?: string;\n description: string;\n recipientAgentId?: string;\n recipientAddress?: string;\n}\n\nconst SPEND_TIMEOUT_MS = 30_000;\n\nexport class SpendClient {\n constructor(\n private wsClient: WSClient,\n private logger: Logger,\n ) {}\n\n async request(params: SpendParams): Promise<SpendResult> {\n const requestId = randomUUID();\n\n const payload: SpendRequestPayload = {\n requestId,\n amountCents: params.amountCents,\n currency: 'USDC',\n category: params.category,\n rail: params.rail,\n vendor: params.vendor,\n description: params.description,\n recipientAgentId: params.recipientAgentId,\n recipientAddress: params.recipientAddress,\n };\n\n this.logger.info(\n {\n requestId,\n amountCents: params.amountCents,\n vendor: params.vendor,\n category: params.category,\n },\n `Requesting spend: ${params.description}`,\n );\n\n return new Promise<SpendResult>((resolve) => {\n const timeout = setTimeout(() => {\n this.wsClient.removeSpendListener(requestId);\n this.logger.error({ requestId }, 'Spend request timed out');\n resolve({\n approved: false,\n reason: 'Request timed out',\n code: 'timeout',\n });\n }, SPEND_TIMEOUT_MS);\n\n this.wsClient.onSpendResult(requestId, (type, result) => {\n clearTimeout(timeout);\n\n if (type === 'approved') {\n const approved = result as SpendApprovedPayload;\n this.logger.info(\n {\n requestId,\n ledgerEntryId: approved.ledgerEntryId,\n remainingBalanceCents: approved.remainingBalanceCents,\n },\n 'Spend approved',\n );\n resolve({\n approved: true,\n ledgerEntryId: approved.ledgerEntryId,\n transactionHash: approved.transactionHash,\n remainingBalanceCents: approved.remainingBalanceCents,\n });\n } else {\n const denied = result as SpendDeniedPayload;\n this.logger.warn(\n { requestId, code: denied.code, reason: denied.reason },\n 'Spend denied',\n );\n resolve({\n approved: false,\n reason: denied.reason,\n code: denied.code,\n });\n }\n });\n\n this.wsClient.send('agent:spend_request', payload);\n });\n }\n}\n"],"mappings":";;;AAAA,OAAO,UAAU;AAGV,SAAS,aACd,QACa;AACb,QAAM,QAAQ,QAAQ,IAAI,aAAa;AAEvC,SAAO,KAAK;AAAA,IACV,OAAO,OAAO;AAAA,IACd,WAAW,QACP;AAAA,MACE,QAAQ;AAAA,MACR,SAAS,EAAE,UAAU,MAAM,eAAe,eAAe;AAAA,IAC3D,IACA;AAAA,EACN,CAAC;AACH;;;ACjBA,OAAO,eAAe;;;ACOf,IAAM,YAAN,MAAgB;AAAA,EAKrB,YACU,QACA,SACA,YACA,WACR;AAJQ;AACA;AACA;AACA;AAAA,EACP;AAAA,EATK,QAA+C;AAAA,EAC/C,cAAc;AAAA,EACL,iBAAiB;AAAA,EASlC,QAAc;AACZ,SAAK,cAAc;AACnB,SAAK,QAAQ,YAAY,MAAM;AAC7B,UAAI,KAAK,eAAe,KAAK,gBAAgB;AAC3C,aAAK,OAAO,UAAU;AACtB;AAAA,MACF;AAEA,YAAM,UAA4B;AAAA,QAChC,SAAS,KAAK;AAAA,QACd,eAAe,KAAK,OAAO,KAAK,IAAI,IAAI,KAAK,aAAa,GAAI;AAAA,QAC9D,eAAe,KAAK,MAAM,QAAQ,YAAY,EAAE,MAAM,OAAO,IAAI;AAAA,MACnE;AAEA,WAAK,OAAO,KAAK,mBAAmB,OAAO;AAC3C,WAAK;AAAA,IACP,GAAG,KAAK,UAAU;AAAA,EACpB;AAAA,EAEA,QAAc;AACZ,SAAK,cAAc;AAAA,EACrB;AAAA,EAEA,OAAa;AACX,QAAI,KAAK,OAAO;AACd,oBAAc,KAAK,KAAK;A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BAAqB,OAAO,WAAW;AAAA,IACzC;AAEA,WAAO,IAAI,QAAqB,CAAC,YAAY;AAC3C,YAAM,UAAU,WAAW,MAAM;AAC/B,aAAK,SAAS,oBAAoB,SAAS;AAC3C,aAAK,OAAO,MAAM,EAAE,UAAU,GAAG,yBAAyB;AAC1D,gBAAQ;AAAA,UACN,UAAU;AAAA,UACV,QAAQ;AAAA,UACR,MAAM;AAAA,QACR,CAAC;AAAA,MACH,GAAG,gBAAgB;AAEnB,WAAK,SAAS,cAAc,WAAW,CAAC,MAAM,WAAW;AACvD,qBAAa,OAAO;AAEpB,YAAI,SAAS,YAAY;AACvB,gBAAM,WAAW;AACjB,eAAK,OAAO;AAAA,YACV;AAAA,cACE;AAAA,cACA,eAAe,SAAS;AAAA,cACxB,uBAAuB,SAAS;AAAA,YAClC;AAAA,YACA;AAAA,UACF;AACA,kBAAQ;AAAA,YACN,UAAU;AAAA,YACV,eAAe,SAAS;AAAA,YACxB,iBAAiB,SAAS;AAAA,YAC1B,uBAAuB,SAAS;AAAA,UAClC,CAAC;AAAA,QACH,OAAO;AACL,gBAAM,SAAS;AACf,eAAK,OAAO;AAAA,YACV,EAAE,WAAW,MAAM,OAAO,MAAM,QAAQ,OAAO,OAAO;AAAA,YACtD;AAAA,UACF;AACA,kBAAQ;AAAA,YACN,UAAU;AAAA,YACV,QAAQ,OAAO;AAAA,YACf,MAAM,OAAO;AAAA,UACf,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAED,WAAK,SAAS,KAAK,uBAAuB,OAAO;AAAA,IACnD,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as _oclif_core_interfaces from '@oclif/core/interfaces';
|
|
2
|
+
import { Command } from '@oclif/core';
|
|
3
|
+
|
|
4
|
+
declare class ConfigGet extends Command {
|
|
5
|
+
static description: string;
|
|
6
|
+
static args: {
|
|
7
|
+
key: _oclif_core_interfaces.Arg<string, Record<string, unknown>>;
|
|
8
|
+
};
|
|
9
|
+
run(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { ConfigGet as default };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
ConfigStore,
|
|
4
|
+
configSchema
|
|
5
|
+
} from "../../chunk-B67ZSZCD.js";
|
|
6
|
+
|
|
7
|
+
// src/commands/config/get.ts
|
|
8
|
+
import { Args, Command } from "@oclif/core";
|
|
9
|
+
var VALID_KEYS = Object.keys(configSchema.shape);
|
|
10
|
+
var ConfigGet = class _ConfigGet extends Command {
|
|
11
|
+
static description = "Get a configuration value";
|
|
12
|
+
static args = {
|
|
13
|
+
key: Args.string({ description: "Config key", required: true })
|
|
14
|
+
};
|
|
15
|
+
async run() {
|
|
16
|
+
const { args } = await this.parse(_ConfigGet);
|
|
17
|
+
if (!VALID_KEYS.includes(args.key)) {
|
|
18
|
+
this.error(
|
|
19
|
+
`Invalid key "${args.key}". Valid keys: ${VALID_KEYS.join(", ")}`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
const store = new ConfigStore();
|
|
23
|
+
const value = store.get(args.key);
|
|
24
|
+
this.log(value != null ? String(value) : "(not set)");
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
export {
|
|
28
|
+
ConfigGet as default
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=get.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/config/get.ts"],"sourcesContent":["import { Args, Command } from '@oclif/core';\nimport { ConfigStore } from '../../config/config-store.js';\nimport { configSchema } from '../../config/config-schema.js';\n\nconst VALID_KEYS = Object.keys(configSchema.shape);\n\nexport default class ConfigGet extends Command {\n static override description = 'Get a configuration value';\n\n static override args = {\n key: Args.string({ description: 'Config key', required: true }),\n };\n\n async run(): Promise<void> {\n const { args } = await this.parse(ConfigGet);\n\n if (!VALID_KEYS.includes(args.key)) {\n this.error(\n `Invalid key \"${args.key}\". Valid keys: ${VALID_KEYS.join(', ')}`,\n );\n }\n\n const store = new ConfigStore();\n const value = store.get(args.key as keyof typeof configSchema.shape);\n this.log(value != null ? String(value) : '(not set)');\n }\n}\n"],"mappings":";;;;;;;AAAA,SAAS,MAAM,eAAe;AAI9B,IAAM,aAAa,OAAO,KAAK,aAAa,KAAK;AAEjD,IAAqB,YAArB,MAAqB,mBAAkB,QAAQ;AAAA,EAC7C,OAAgB,cAAc;AAAA,EAE9B,OAAgB,OAAO;AAAA,IACrB,KAAK,KAAK,OAAO,EAAE,aAAa,cAAc,UAAU,KAAK,CAAC;AAAA,EAChE;AAAA,EAEA,MAAM,MAAqB;AACzB,UAAM,EAAE,KAAK,IAAI,MAAM,KAAK,MAAM,UAAS;AAE3C,QAAI,CAAC,WAAW,SAAS,KAAK,GAAG,GAAG;AAClC,WAAK;AAAA,QACH,gBAAgB,KAAK,GAAG,kBAAkB,WAAW,KAAK,IAAI,CAAC;AAAA,MACjE;AAAA,IACF;AAEA,UAAM,QAAQ,IAAI,YAAY;AAC9B,UAAM,QAAQ,MAAM,IAAI,KAAK,GAAsC;AACnE,SAAK,IAAI,SAAS,OAAO,OAAO,KAAK,IAAI,WAAW;AAAA,EACtD;AACF;","names":[]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
ConfigStore
|
|
4
|
+
} from "../../chunk-B67ZSZCD.js";
|
|
5
|
+
|
|
6
|
+
// src/commands/config/index.ts
|
|
7
|
+
import { Command } from "@oclif/core";
|
|
8
|
+
var ConfigShow = class extends Command {
|
|
9
|
+
static description = "Show current configuration";
|
|
10
|
+
async run() {
|
|
11
|
+
const store = new ConfigStore();
|
|
12
|
+
const config = store.getAll();
|
|
13
|
+
this.log(`Config file: ${store.path}
|
|
14
|
+
`);
|
|
15
|
+
for (const [key, value] of Object.entries(config)) {
|
|
16
|
+
const display = key === "apiKey" && value ? `${String(value).slice(0, 4)}...` : value ?? "(not set)";
|
|
17
|
+
this.log(` ${key}: ${display}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
export {
|
|
22
|
+
ConfigShow as default
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/config/index.ts"],"sourcesContent":["import { Command } from '@oclif/core';\nimport { ConfigStore } from '../../config/config-store.js';\n\nexport default class ConfigShow extends Command {\n static override description = 'Show current configuration';\n\n async run(): Promise<void> {\n const store = new ConfigStore();\n const config = store.getAll();\n\n this.log(`Config file: ${store.path}\\n`);\n\n for (const [key, value] of Object.entries(config)) {\n const display =\n key === 'apiKey' && value\n ? `${String(value).slice(0, 4)}...`\n : (value ?? '(not set)');\n this.log(` ${key}: ${display}`);\n }\n }\n}\n"],"mappings":";;;;;;AAAA,SAAS,eAAe;AAGxB,IAAqB,aAArB,cAAwC,QAAQ;AAAA,EAC9C,OAAgB,cAAc;AAAA,EAE9B,MAAM,MAAqB;AACzB,UAAM,QAAQ,IAAI,YAAY;AAC9B,UAAM,SAAS,MAAM,OAAO;AAE5B,SAAK,IAAI,gBAAgB,MAAM,IAAI;AAAA,CAAI;AAEvC,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,GAAG;AACjD,YAAM,UACJ,QAAQ,YAAY,QAChB,GAAG,OAAO,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,QAC3B,SAAS;AAChB,WAAK,IAAI,KAAK,GAAG,KAAK,OAAO,EAAE;AAAA,IACjC;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as _oclif_core_interfaces from '@oclif/core/interfaces';
|
|
2
|
+
import { Command } from '@oclif/core';
|
|
3
|
+
|
|
4
|
+
declare class ConfigSet extends Command {
|
|
5
|
+
static description: string;
|
|
6
|
+
static args: {
|
|
7
|
+
key: _oclif_core_interfaces.Arg<string, Record<string, unknown>>;
|
|
8
|
+
value: _oclif_core_interfaces.Arg<string, Record<string, unknown>>;
|
|
9
|
+
};
|
|
10
|
+
run(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { ConfigSet as default };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
ConfigStore,
|
|
4
|
+
configSchema
|
|
5
|
+
} from "../../chunk-B67ZSZCD.js";
|
|
6
|
+
|
|
7
|
+
// src/commands/config/set.ts
|
|
8
|
+
import { Args, Command } from "@oclif/core";
|
|
9
|
+
var VALID_KEYS = Object.keys(configSchema.shape);
|
|
10
|
+
var NUMERIC_KEYS = /* @__PURE__ */ new Set([
|
|
11
|
+
"heartbeatIntervalMs",
|
|
12
|
+
"wsReconnectBaseMs",
|
|
13
|
+
"wsReconnectMaxMs"
|
|
14
|
+
]);
|
|
15
|
+
var ConfigSet = class _ConfigSet extends Command {
|
|
16
|
+
static description = "Set a configuration value";
|
|
17
|
+
static args = {
|
|
18
|
+
key: Args.string({ description: "Config key", required: true }),
|
|
19
|
+
value: Args.string({ description: "Config value", required: true })
|
|
20
|
+
};
|
|
21
|
+
async run() {
|
|
22
|
+
const { args } = await this.parse(_ConfigSet);
|
|
23
|
+
if (!VALID_KEYS.includes(args.key)) {
|
|
24
|
+
this.error(
|
|
25
|
+
`Invalid key "${args.key}". Valid keys: ${VALID_KEYS.join(", ")}`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
const store = new ConfigStore();
|
|
29
|
+
const parsed = NUMERIC_KEYS.has(args.key) ? Number(args.value) : args.value;
|
|
30
|
+
store.set(args.key, parsed);
|
|
31
|
+
this.log(
|
|
32
|
+
`Set ${args.key} = ${args.key === "apiKey" ? "****" : args.value}`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
export {
|
|
37
|
+
ConfigSet as default
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=set.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/commands/config/set.ts"],"sourcesContent":["import { Args, Command } from '@oclif/core';\nimport { ConfigStore } from '../../config/config-store.js';\nimport { configSchema } from '../../config/config-schema.js';\n\nconst VALID_KEYS = Object.keys(configSchema.shape);\n\nconst NUMERIC_KEYS = new Set([\n 'heartbeatIntervalMs',\n 'wsReconnectBaseMs',\n 'wsReconnectMaxMs',\n]);\n\nexport default class ConfigSet extends Command {\n static override description = 'Set a configuration value';\n\n static override args = {\n key: Args.string({ description: 'Config key', required: true }),\n value: Args.string({ description: 'Config value', required: true }),\n };\n\n async run(): Promise<void> {\n const { args } = await this.parse(ConfigSet);\n\n if (!VALID_KEYS.includes(args.key)) {\n this.error(\n `Invalid key \"${args.key}\". Valid keys: ${VALID_KEYS.join(', ')}`,\n );\n }\n\n const store = new ConfigStore();\n const parsed = NUMERIC_KEYS.has(args.key)\n ? Number(args.value)\n : args.value;\n\n store.set(args.key as keyof typeof configSchema.shape, parsed as never);\n this.log(\n `Set ${args.key} = ${args.key === 'apiKey' ? '****' : args.value}`,\n );\n }\n}\n"],"mappings":";;;;;;;AAAA,SAAS,MAAM,eAAe;AAI9B,IAAM,aAAa,OAAO,KAAK,aAAa,KAAK;AAEjD,IAAM,eAAe,oBAAI,IAAI;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAED,IAAqB,YAArB,MAAqB,mBAAkB,QAAQ;AAAA,EAC7C,OAAgB,cAAc;AAAA,EAE9B,OAAgB,OAAO;AAAA,IACrB,KAAK,KAAK,OAAO,EAAE,aAAa,cAAc,UAAU,KAAK,CAAC;AAAA,IAC9D,OAAO,KAAK,OAAO,EAAE,aAAa,gBAAgB,UAAU,KAAK,CAAC;AAAA,EACpE;AAAA,EAEA,MAAM,MAAqB;AACzB,UAAM,EAAE,KAAK,IAAI,MAAM,KAAK,MAAM,UAAS;AAE3C,QAAI,CAAC,WAAW,SAAS,KAAK,GAAG,GAAG;AAClC,WAAK;AAAA,QACH,gBAAgB,KAAK,GAAG,kBAAkB,WAAW,KAAK,IAAI,CAAC;AAAA,MACjE;AAAA,IACF;AAEA,UAAM,QAAQ,IAAI,YAAY;AAC9B,UAAM,SAAS,aAAa,IAAI,KAAK,GAAG,IACpC,OAAO,KAAK,KAAK,IACjB,KAAK;AAET,UAAM,IAAI,KAAK,KAAwC,MAAe;AACtE,SAAK;AAAA,MACH,OAAO,KAAK,GAAG,MAAM,KAAK,QAAQ,WAAW,SAAS,KAAK,KAAK;AAAA,IAClE;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as _oclif_core_interfaces from '@oclif/core/interfaces';
|
|
2
|
+
import { Command } from '@oclif/core';
|
|
3
|
+
|
|
4
|
+
declare class Spend extends Command {
|
|
5
|
+
static description: string;
|
|
6
|
+
static flags: {
|
|
7
|
+
amount: _oclif_core_interfaces.OptionFlag<number, _oclif_core_interfaces.CustomOptions>;
|
|
8
|
+
category: _oclif_core_interfaces.OptionFlag<string, _oclif_core_interfaces.CustomOptions>;
|
|
9
|
+
rail: _oclif_core_interfaces.OptionFlag<string, _oclif_core_interfaces.CustomOptions>;
|
|
10
|
+
vendor: _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
11
|
+
description: _oclif_core_interfaces.OptionFlag<string, _oclif_core_interfaces.CustomOptions>;
|
|
12
|
+
'recipient-agent': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
13
|
+
'recipient-address': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
14
|
+
'server-url': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
15
|
+
'agent-id': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
16
|
+
'api-key': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
17
|
+
};
|
|
18
|
+
run(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { Spend as default };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
SpendClient,
|
|
4
|
+
WSClient,
|
|
5
|
+
createLogger
|
|
6
|
+
} from "../chunk-UG6D7BON.js";
|
|
7
|
+
import {
|
|
8
|
+
ConfigStore
|
|
9
|
+
} from "../chunk-B67ZSZCD.js";
|
|
10
|
+
|
|
11
|
+
// src/commands/spend.ts
|
|
12
|
+
import { Command, Flags } from "@oclif/core";
|
|
13
|
+
var Spend = class _Spend extends Command {
|
|
14
|
+
static description = "Send a spend request through the connected agent";
|
|
15
|
+
static flags = {
|
|
16
|
+
amount: Flags.integer({
|
|
17
|
+
required: true,
|
|
18
|
+
description: "Amount in cents"
|
|
19
|
+
}),
|
|
20
|
+
category: Flags.string({
|
|
21
|
+
required: true,
|
|
22
|
+
description: "Spend category",
|
|
23
|
+
options: ["api", "compute", "contractor", "transfer"]
|
|
24
|
+
}),
|
|
25
|
+
rail: Flags.string({
|
|
26
|
+
required: true,
|
|
27
|
+
description: "Payment rail",
|
|
28
|
+
options: ["crypto_onchain", "x402", "internal"]
|
|
29
|
+
}),
|
|
30
|
+
vendor: Flags.string({ description: "Vendor name" }),
|
|
31
|
+
description: Flags.string({
|
|
32
|
+
required: true,
|
|
33
|
+
description: "Spend description"
|
|
34
|
+
}),
|
|
35
|
+
"recipient-agent": Flags.string({
|
|
36
|
+
description: "Recipient agent ID (for agent-to-agent)"
|
|
37
|
+
}),
|
|
38
|
+
"recipient-address": Flags.string({
|
|
39
|
+
description: "Recipient wallet address (for on-chain)"
|
|
40
|
+
}),
|
|
41
|
+
"server-url": Flags.string({ env: "STAMN_SERVER_URL" }),
|
|
42
|
+
"agent-id": Flags.string({ env: "STAMN_AGENT_ID" }),
|
|
43
|
+
"api-key": Flags.string({ env: "STAMN_API_KEY" })
|
|
44
|
+
};
|
|
45
|
+
async run() {
|
|
46
|
+
const { flags } = await this.parse(_Spend);
|
|
47
|
+
const configStore = new ConfigStore();
|
|
48
|
+
const config = { ...configStore.getAll() };
|
|
49
|
+
if (flags["server-url"]) config.serverUrl = flags["server-url"];
|
|
50
|
+
if (flags["agent-id"]) config.agentId = flags["agent-id"];
|
|
51
|
+
if (flags["api-key"]) config.apiKey = flags["api-key"];
|
|
52
|
+
if (!config.agentId) {
|
|
53
|
+
this.error("agentId is required. Run: stamn-agent config set agentId <uuid>");
|
|
54
|
+
}
|
|
55
|
+
const logger = createLogger({ logLevel: "info" });
|
|
56
|
+
this.log(`Connecting to ${config.serverUrl}...`);
|
|
57
|
+
const client = new WSClient({
|
|
58
|
+
config,
|
|
59
|
+
logger,
|
|
60
|
+
onCommand: () => {
|
|
61
|
+
},
|
|
62
|
+
onDisconnect: () => {
|
|
63
|
+
},
|
|
64
|
+
onConnected: async () => {
|
|
65
|
+
this.log("Authenticated. Sending spend request...\n");
|
|
66
|
+
const spendClient = new SpendClient(client, logger);
|
|
67
|
+
const result = await spendClient.request({
|
|
68
|
+
amountCents: flags.amount,
|
|
69
|
+
category: flags.category,
|
|
70
|
+
rail: flags.rail,
|
|
71
|
+
vendor: flags.vendor,
|
|
72
|
+
description: flags.description,
|
|
73
|
+
recipientAgentId: flags["recipient-agent"],
|
|
74
|
+
recipientAddress: flags["recipient-address"]
|
|
75
|
+
});
|
|
76
|
+
if (result.approved) {
|
|
77
|
+
this.log("Spend APPROVED");
|
|
78
|
+
this.log(` Ledger Entry: ${result.ledgerEntryId}`);
|
|
79
|
+
if (result.transactionHash) {
|
|
80
|
+
this.log(` Tx Hash: ${result.transactionHash}`);
|
|
81
|
+
}
|
|
82
|
+
this.log(` Remaining: ${result.remainingBalanceCents} cents`);
|
|
83
|
+
} else {
|
|
84
|
+
this.log("Spend DENIED");
|
|
85
|
+
this.log(` Reason: ${result.reason}`);
|
|
86
|
+
this.log(` Code: ${result.code}`);
|
|
87
|
+
}
|
|
88
|
+
client.disconnect();
|
|
89
|
+
process.exit(0);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
client.connect();
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
export {
|
|
96
|
+
Spend as default
|
|
97
|
+
};
|
|
98
|
+
//# sourceMappingURL=spend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/commands/spend.ts"],"sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport { ConfigStore } from '../config/config-store.js';\nimport { createLogger } from '../logging/logger.js';\nimport { WSClient } from '../ws/ws-client.js';\nimport { SpendClient } from '../spend/spend-client.js';\nimport type { LedgerCategory, LedgerRail } from '@repo/types';\n\nexport default class Spend extends Command {\n static override description =\n 'Send a spend request through the connected agent';\n\n static override flags = {\n amount: Flags.integer({\n required: true,\n description: 'Amount in cents',\n }),\n category: Flags.string({\n required: true,\n description: 'Spend category',\n options: ['api', 'compute', 'contractor', 'transfer'],\n }),\n rail: Flags.string({\n required: true,\n description: 'Payment rail',\n options: ['crypto_onchain', 'x402', 'internal'],\n }),\n vendor: Flags.string({ description: 'Vendor name' }),\n description: Flags.string({\n required: true,\n description: 'Spend description',\n }),\n 'recipient-agent': Flags.string({\n description: 'Recipient agent ID (for agent-to-agent)',\n }),\n 'recipient-address': Flags.string({\n description: 'Recipient wallet address (for on-chain)',\n }),\n 'server-url': Flags.string({ env: 'STAMN_SERVER_URL' }),\n 'agent-id': Flags.string({ env: 'STAMN_AGENT_ID' }),\n 'api-key': Flags.string({ env: 'STAMN_API_KEY' }),\n };\n\n async run(): Promise<void> {\n const { flags } = await this.parse(Spend);\n const configStore = new ConfigStore();\n const config = { ...configStore.getAll() };\n\n if (flags['server-url']) config.serverUrl = flags['server-url'];\n if (flags['agent-id']) config.agentId = flags['agent-id'];\n if (flags['api-key']) config.apiKey = flags['api-key'];\n\n if (!config.agentId) {\n this.error('agentId is required. Run: stamn-agent config set agentId <uuid>');\n }\n\n const logger = createLogger({ logLevel: 'info' });\n\n this.log(`Connecting to ${config.serverUrl}...`);\n\n const client = new WSClient({\n config,\n logger,\n onCommand: () => {},\n onDisconnect: () => {},\n onConnected: async () => {\n this.log('Authenticated. Sending spend request...\\n');\n\n const spendClient = new SpendClient(client, logger);\n const result = await spendClient.request({\n amountCents: flags.amount,\n category: flags.category as LedgerCategory,\n rail: flags.rail as LedgerRail,\n vendor: flags.vendor,\n description: flags.description,\n recipientAgentId: flags['recipient-agent'],\n recipientAddress: flags['recipient-address'],\n });\n\n if (result.approved) {\n this.log('Spend APPROVED');\n this.log(` Ledger Entry: ${result.ledgerEntryId}`);\n if (result.transactionHash) {\n this.log(` Tx Hash: ${result.transactionHash}`);\n }\n this.log(` Remaining: ${result.remainingBalanceCents} cents`);\n } else {\n this.log('Spend DENIED');\n this.log(` Reason: ${result.reason}`);\n this.log(` Code: ${result.code}`);\n }\n\n client.disconnect();\n process.exit(0);\n },\n });\n\n client.connect();\n }\n}\n"],"mappings":";;;;;;;;;;;AAAA,SAAS,SAAS,aAAa;AAO/B,IAAqB,QAArB,MAAqB,eAAc,QAAQ;AAAA,EACzC,OAAgB,cACd;AAAA,EAEF,OAAgB,QAAQ;AAAA,IACtB,QAAQ,MAAM,QAAQ;AAAA,MACpB,UAAU;AAAA,MACV,aAAa;AAAA,IACf,CAAC;AAAA,IACD,UAAU,MAAM,OAAO;AAAA,MACrB,UAAU;AAAA,MACV,aAAa;AAAA,MACb,SAAS,CAAC,OAAO,WAAW,cAAc,UAAU;AAAA,IACtD,CAAC;AAAA,IACD,MAAM,MAAM,OAAO;AAAA,MACjB,UAAU;AAAA,MACV,aAAa;AAAA,MACb,SAAS,CAAC,kBAAkB,QAAQ,UAAU;AAAA,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AAA,UACnB,UAAU,MAAM;AAAA,UAChB,MAAM,MAAM;AAAA,UACZ,QAAQ,MAAM;AAAA,UACd,aAAa,MAAM;AAAA,UACnB,kBAAkB,MAAM,iBAAiB;AAAA,UACzC,kBAAkB,MAAM,mBAAmB;AAAA,QAC7C,CAAC;AAED,YAAI,OAAO,UAAU;AACnB,eAAK,IAAI,gBAAgB;AACzB,eAAK,IAAI,mBAAmB,OAAO,aAAa,EAAE;AAClD,cAAI,OAAO,iBAAiB;AAC1B,iBAAK,IAAI,mBAAmB,OAAO,eAAe,EAAE;AAAA,UACtD;AACA,eAAK,IAAI,mBAAmB,OAAO,qBAAqB,QAAQ;AAAA,QAClE,OAAO;AACL,eAAK,IAAI,cAAc;AACvB,eAAK,IAAI,aAAa,OAAO,MAAM,EAAE;AACrC,eAAK,IAAI,aAAa,OAAO,IAAI,EAAE;AAAA,QACrC;AAEA,eAAO,WAAW;AAClB,gBAAQ,KAAK,CAAC;AAAA,MAChB;AAAA,IACF,CAAC;AAED,WAAO,QAAQ;AAAA,EACjB;AACF;","names":[]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as _oclif_core_interfaces from '@oclif/core/interfaces';
|
|
2
|
+
import { Command } from '@oclif/core';
|
|
3
|
+
|
|
4
|
+
declare class Start extends Command {
|
|
5
|
+
static description: string;
|
|
6
|
+
static flags: {
|
|
7
|
+
daemon: _oclif_core_interfaces.BooleanFlag<boolean>;
|
|
8
|
+
'server-url': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
9
|
+
'agent-id': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
10
|
+
'api-key': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
11
|
+
'log-level': _oclif_core_interfaces.OptionFlag<string | undefined, _oclif_core_interfaces.CustomOptions>;
|
|
12
|
+
};
|
|
13
|
+
run(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { Start as default };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
DaemonManager
|
|
4
|
+
} from "../chunk-3DME5XG7.js";
|
|
5
|
+
import {
|
|
6
|
+
SpendClient,
|
|
7
|
+
WSClient,
|
|
8
|
+
createLogger
|
|
9
|
+
} from "../chunk-UG6D7BON.js";
|
|
10
|
+
import {
|
|
11
|
+
ConfigStore
|
|
12
|
+
} from "../chunk-B67ZSZCD.js";
|
|
13
|
+
|
|
14
|
+
// src/commands/start.ts
|
|
15
|
+
import { Command, Flags } from "@oclif/core";
|
|
16
|
+
var Start = class _Start extends Command {
|
|
17
|
+
static description = "Start the Stamn agent daemon";
|
|
18
|
+
static flags = {
|
|
19
|
+
daemon: Flags.boolean({
|
|
20
|
+
char: "d",
|
|
21
|
+
description: "Run as background daemon",
|
|
22
|
+
default: false
|
|
23
|
+
}),
|
|
24
|
+
"server-url": Flags.string({
|
|
25
|
+
description: "Override server URL",
|
|
26
|
+
env: "STAMN_SERVER_URL"
|
|
27
|
+
}),
|
|
28
|
+
"agent-id": Flags.string({
|
|
29
|
+
description: "Override agent ID",
|
|
30
|
+
env: "STAMN_AGENT_ID"
|
|
31
|
+
}),
|
|
32
|
+
"api-key": Flags.string({
|
|
33
|
+
description: "Override API key",
|
|
34
|
+
env: "STAMN_API_KEY"
|
|
35
|
+
}),
|
|
36
|
+
"log-level": Flags.string({
|
|
37
|
+
description: "Override log level",
|
|
38
|
+
options: ["trace", "debug", "info", "warn", "error", "fatal"]
|
|
39
|
+
})
|
|
40
|
+
};
|
|
41
|
+
async run() {
|
|
42
|
+
const { flags } = await this.parse(_Start);
|
|
43
|
+
const configStore = new ConfigStore();
|
|
44
|
+
const config = { ...configStore.getAll() };
|
|
45
|
+
if (flags["server-url"]) config.serverUrl = flags["server-url"];
|
|
46
|
+
if (flags["agent-id"]) config.agentId = flags["agent-id"];
|
|
47
|
+
if (flags["api-key"]) config.apiKey = flags["api-key"];
|
|
48
|
+
if (flags["log-level"])
|
|
49
|
+
config.logLevel = flags["log-level"];
|
|
50
|
+
if (!config.agentId) {
|
|
51
|
+
this.error(
|
|
52
|
+
"agentId is required. Run: stamn-agent config set agentId <uuid>"
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
const dm = new DaemonManager();
|
|
56
|
+
const { running, pid } = dm.isRunning();
|
|
57
|
+
if (running) {
|
|
58
|
+
this.error(`Daemon already running (PID ${pid})`);
|
|
59
|
+
}
|
|
60
|
+
if (flags.daemon) {
|
|
61
|
+
const { daemonizeProcess } = await import("../process-HCAG5EF5.js");
|
|
62
|
+
await daemonizeProcess();
|
|
63
|
+
}
|
|
64
|
+
dm.writePid(process.pid);
|
|
65
|
+
const logger = createLogger(config);
|
|
66
|
+
logger.info(
|
|
67
|
+
{ agentId: config.agentId, serverUrl: config.serverUrl },
|
|
68
|
+
"Starting Stamn agent"
|
|
69
|
+
);
|
|
70
|
+
const client = new WSClient({
|
|
71
|
+
config,
|
|
72
|
+
logger,
|
|
73
|
+
onCommand: (command, params) => {
|
|
74
|
+
logger.info({ command, params }, "Received command");
|
|
75
|
+
if (command === "shutdown") {
|
|
76
|
+
shutdown();
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
onDisconnect: () => {
|
|
80
|
+
logger.warn("Disconnected from server");
|
|
81
|
+
},
|
|
82
|
+
onConnected: () => {
|
|
83
|
+
logger.info("Agent is online and ready \u2014 spend capability active");
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const spendClient = new SpendClient(client, logger);
|
|
87
|
+
globalThis.__stamnSpendClient = spendClient;
|
|
88
|
+
const shutdown = () => {
|
|
89
|
+
logger.info("Shutting down...");
|
|
90
|
+
client.disconnect();
|
|
91
|
+
dm.removePid();
|
|
92
|
+
process.exit(0);
|
|
93
|
+
};
|
|
94
|
+
process.on("SIGTERM", shutdown);
|
|
95
|
+
process.on("SIGINT", shutdown);
|
|
96
|
+
client.connect();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
export {
|
|
100
|
+
Start as default
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=start.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/commands/start.ts"],"sourcesContent":["import { Command, Flags } from '@oclif/core';\nimport { ConfigStore } from '../config/config-store.js';\nimport type { AgentConfig } from '../config/config-schema.js';\nimport { createLogger } from '../logging/logger.js';\nimport { WSClient } from '../ws/ws-client.js';\nimport { SpendClient } from '../spend/spend-client.js';\nimport { DaemonManager } from '../daemon/daemon-manager.js';\n\nexport default class Start extends Command {\n static override description = 'Start the Stamn agent daemon';\n\n static override flags = {\n daemon: Flags.boolean({\n char: 'd',\n description: 'Run as background daemon',\n default: false,\n }),\n 'server-url': Flags.string({\n description: 'Override server URL',\n env: 'STAMN_SERVER_URL',\n }),\n 'agent-id': Flags.string({\n description: 'Override agent ID',\n env: 'STAMN_AGENT_ID',\n }),\n 'api-key': Flags.string({\n description: 'Override API key',\n env: 'STAMN_API_KEY',\n }),\n 'log-level': Flags.string({\n description: 'Override log level',\n options: ['trace', 'debug', 'info', 'warn', 'error', 'fatal'],\n }),\n };\n\n async run(): Promise<void> {\n const { flags } = await this.parse(Start);\n const configStore = new ConfigStore();\n const config = { ...configStore.getAll() };\n\n // Apply flag overrides\n if (flags['server-url']) config.serverUrl = flags['server-url'];\n if (flags['agent-id']) config.agentId = flags['agent-id'];\n if (flags['api-key']) config.apiKey = flags['api-key'];\n if (flags['log-level'])\n config.logLevel = flags['log-level'] as AgentConfig['logLevel'];\n\n // Validate required fields\n if (!config.agentId) {\n this.error(\n 'agentId is required. Run: stamn-agent config set agentId <uuid>',\n );\n }\n\n // Check for existing daemon\n const dm = new DaemonManager();\n const { running, pid } = dm.isRunning();\n if (running) {\n this.error(`Daemon already running (PID ${pid})`);\n }\n\n // Daemonize if requested\n if (flags.daemon) {\n const { daemonizeProcess } = await import('../daemon/process.js');\n await daemonizeProcess();\n }\n\n // Write PID\n dm.writePid(process.pid);\n\n const logger = createLogger(config);\n logger.info(\n { agentId: config.agentId, serverUrl: config.serverUrl },\n 'Starting Stamn agent',\n );\n\n // Create WebSocket client\n const client = new WSClient({\n config,\n logger,\n onCommand: (command, params) => {\n logger.info({ command, params }, 'Received command');\n if (command === 'shutdown') {\n shutdown();\n }\n },\n onDisconnect: () => {\n logger.warn('Disconnected from server');\n },\n onConnected: () => {\n logger.info('Agent is online and ready — spend capability active');\n },\n });\n\n // Create spend client — available for plugin/task integration\n const spendClient = new SpendClient(client, logger);\n\n // Expose on process for external plugin access\n (globalThis as Record<string, unknown>).__stamnSpendClient = spendClient;\n\n // Graceful shutdown\n const shutdown = () => {\n logger.info('Shutting down...');\n client.disconnect();\n dm.removePid();\n process.exit(0);\n };\n\n process.on('SIGTERM', shutdown);\n process.on('SIGINT', shutdown);\n\n // Connect\n client.connect();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,SAAS,aAAa;AAQ/B,IAAqB,QAArB,MAAqB,eAAc,QAAQ;AAAA,EACzC,OAAgB,cAAc;AAAA,EAE9B,OAAgB,QAAQ;AAAA,IACtB,QAAQ,MAAM,QAAQ;AAAA,MACpB,MAAM;AAAA,MACN,aAAa;AAAA,MACb,SAAS;AAAA,IACX,CAAC;AAAA,IACD,cAAc,MAAM,OAAO;AAAA,MACzB,aAAa;AAAA,MACb,KAAK;AAAA,IACP,CAAC;AAAA,IACD,YAAY,MAAM,OAAO;AAAA,MACvB,aAAa;AAAA,MACb,KAAK;AAAA,IACP,CAAC;AAAA,IACD,WAAW,MAAM,OAAO;AAAA,MACtB,aAAa;AAAA,MACb,KAAK;AAAA,IACP,CAAC;AAAA,IACD,aAAa,MAAM,OAAO;AAAA,MACxB,aAAa;AAAA,MACb,SAAS,CAAC,SAAS,SAAS,QAAQ,QAAQ,SAAS,OAAO;AAAA,IAC9D,CAA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IAAI,YAAY,QAAQ,MAAM;AAGlD,IAAC,WAAuC,qBAAqB;AAG7D,UAAM,WAAW,MAAM;AACrB,aAAO,KAAK,kBAAkB;AAC9B,aAAO,WAAW;AAClB,SAAG,UAAU;AACb,cAAQ,KAAK,CAAC;AAAA,IAChB;AAEA,YAAQ,GAAG,WAAW,QAAQ;AAC9B,YAAQ,GAAG,UAAU,QAAQ;AAG7B,WAAO,QAAQ;AAAA,EACjB;AACF;","names":[]}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
DaemonManager
|
|
4
|
+
} from "../chunk-3DME5XG7.js";
|
|
5
|
+
import {
|
|
6
|
+
ConfigStore
|
|
7
|
+
} from "../chunk-B67ZSZCD.js";
|
|
8
|
+
|
|
9
|
+
// src/commands/status.ts
|
|
10
|
+
import { Command } from "@oclif/core";
|
|
11
|
+
var Status = class extends Command {
|
|
12
|
+
static description = "Show Stamn agent daemon status";
|
|
13
|
+
async run() {
|
|
14
|
+
const dm = new DaemonManager();
|
|
15
|
+
const configStore = new ConfigStore();
|
|
16
|
+
const config = configStore.getAll();
|
|
17
|
+
const { running, pid } = dm.isRunning();
|
|
18
|
+
this.log("Stamn Agent Status");
|
|
19
|
+
this.log("\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500");
|
|
20
|
+
this.log(`Daemon: ${running ? `running (PID ${pid})` : "stopped"}`);
|
|
21
|
+
this.log(`Agent ID: ${config.agentId ?? "(not set)"}`);
|
|
22
|
+
this.log(`Server URL: ${config.serverUrl}`);
|
|
23
|
+
this.log(`Log Level: ${config.logLevel}`);
|
|
24
|
+
this.log(`Config: ${configStore.path}`);
|
|
25
|
+
this.log(`PID File: ${dm.pidFilePath}`);
|
|
26
|
+
if (running && config.serverUrl) {
|
|
27
|
+
try {
|
|
28
|
+
const res = await fetch(`${config.serverUrl}/v1/health`);
|
|
29
|
+
const data = await res.json();
|
|
30
|
+
this.log(`Server: ${data.data?.status ?? "unknown"}`);
|
|
31
|
+
} catch {
|
|
32
|
+
this.log("Server: unreachable");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export {
|
|
38
|
+
Status as default
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=status.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/commands/status.ts"],"sourcesContent":["import { Command } from '@oclif/core';\nimport { DaemonManager } from '../daemon/daemon-manager.js';\nimport { ConfigStore } from '../config/config-store.js';\n\nexport default class Status extends Command {\n static override description = 'Show Stamn agent daemon status';\n\n async run(): Promise<void> {\n const dm = new DaemonManager();\n const configStore = new ConfigStore();\n const config = configStore.getAll();\n const { running, pid } = dm.isRunning();\n\n this.log('Stamn Agent Status');\n this.log('──────────────────');\n this.log(`Daemon: ${running ? `running (PID ${pid})` : 'stopped'}`);\n this.log(`Agent ID: ${config.agentId ?? '(not set)'}`);\n this.log(`Server URL: ${config.serverUrl}`);\n this.log(`Log Level: ${config.logLevel}`);\n this.log(`Config: ${configStore.path}`);\n this.log(`PID File: ${dm.pidFilePath}`);\n\n if (running && config.serverUrl) {\n try {\n const res = await fetch(`${config.serverUrl}/v1/health`);\n const data = (await res.json()) as { data?: { status?: string } };\n this.log(`Server: ${data.data?.status ?? 'unknown'}`);\n } catch {\n this.log('Server: unreachable');\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;AAAA,SAAS,eAAe;AAIxB,IAAqB,SAArB,cAAoC,QAAQ;AAAA,EAC1C,OAAgB,cAAc;AAAA,EAE9B,MAAM,MAAqB;AACzB,UAAM,KAAK,IAAI,cAAc;AAC7B,UAAM,cAAc,IAAI,YAAY;AACpC,UAAM,SAAS,YAAY,OAAO;AAClC,UAAM,EAAE,SAAS,IAAI,IAAI,GAAG,UAAU;AAEtC,SAAK,IAAI,oBAAoB;AAC7B,SAAK,IAAI,8GAAoB;AAC7B,SAAK,IAAI,eAAe,UAAU,gBAAgB,GAAG,MAAM,SAAS,EAAE;AACtE,SAAK,IAAI,eAAe,OAAO,WAAW,WAAW,EAAE;AACvD,SAAK,IAAI,eAAe,OAAO,SAAS,EAAE;AAC1C,SAAK,IAAI,eAAe,OAAO,QAAQ,EAAE;AACzC,SAAK,IAAI,eAAe,YAAY,IAAI,EAAE;AAC1C,SAAK,IAAI,eAAe,GAAG,WAAW,EAAE;AAExC,QAAI,WAAW,OAAO,WAAW;AAC/B,UAAI;AACF,cAAM,MAAM,MAAM,MAAM,GAAG,OAAO,SAAS,YAAY;AACvD,cAAM,OAAQ,MAAM,IAAI,KAAK;AAC7B,aAAK,IAAI,eAAe,KAAK,MAAM,UAAU,SAAS,EAAE;AAAA,MAC1D,QAAQ;AACN,aAAK,IAAI,yBAAyB;AAAA,MACpC;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
DaemonManager
|
|
4
|
+
} from "../chunk-3DME5XG7.js";
|
|
5
|
+
|
|
6
|
+
// src/commands/stop.ts
|
|
7
|
+
import { Command } from "@oclif/core";
|
|
8
|
+
var Stop = class extends Command {
|
|
9
|
+
static description = "Stop the running Stamn agent daemon";
|
|
10
|
+
async run() {
|
|
11
|
+
const dm = new DaemonManager();
|
|
12
|
+
const { running, pid } = dm.isRunning();
|
|
13
|
+
if (!running) {
|
|
14
|
+
this.log("No daemon is running.");
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
this.log(`Stopping daemon (PID ${pid})...`);
|
|
18
|
+
const stopped = dm.stop();
|
|
19
|
+
if (stopped) {
|
|
20
|
+
this.log("Daemon stopped.");
|
|
21
|
+
} else {
|
|
22
|
+
this.error("Failed to stop daemon.");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
export {
|
|
27
|
+
Stop as default
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=stop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/commands/stop.ts"],"sourcesContent":["import { Command } from '@oclif/core';\nimport { DaemonManager } from '../daemon/daemon-manager.js';\n\nexport default class Stop extends Command {\n static override description = 'Stop the running Stamn agent daemon';\n\n async run(): Promise<void> {\n const dm = new DaemonManager();\n const { running, pid } = dm.isRunning();\n\n if (!running) {\n this.log('No daemon is running.');\n return;\n }\n\n this.log(`Stopping daemon (PID ${pid})...`);\n const stopped = dm.stop();\n\n if (stopped) {\n this.log('Daemon stopped.');\n } else {\n this.error('Failed to stop daemon.');\n }\n }\n}\n"],"mappings":";;;;;;AAAA,SAAS,eAAe;AAGxB,IAAqB,OAArB,cAAkC,QAAQ;AAAA,EACxC,OAAgB,cAAc;AAAA,EAE9B,MAAM,MAAqB;AACzB,UAAM,KAAK,IAAI,cAAc;AAC7B,UAAM,EAAE,SAAS,IAAI,IAAI,GAAG,UAAU;AAEtC,QAAI,CAAC,SAAS;AACZ,WAAK,IAAI,uBAAuB;AAChC;AAAA,IACF;AAEA,SAAK,IAAI,wBAAwB,GAAG,MAAM;AAC1C,UAAM,UAAU,GAAG,KAAK;AAExB,QAAI,SAAS;AACX,WAAK,IAAI,iBAAiB;AAAA,IAC5B,OAAO;AACL,WAAK,MAAM,wBAAwB;AAAA,IACrC;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { run } from '@oclif/core';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { run } from '@oclif/core';\n"],"mappings":";;;AAAA,SAAS,WAAW;","names":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createRequire } from "module"; const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// src/daemon/process.ts
|
|
4
|
+
async function daemonizeProcess() {
|
|
5
|
+
const mod = await import("daemonize-process");
|
|
6
|
+
const daemonize = typeof mod.default === "function" ? mod.default : mod;
|
|
7
|
+
daemonize();
|
|
8
|
+
}
|
|
9
|
+
export {
|
|
10
|
+
daemonizeProcess
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=process-HCAG5EF5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/daemon/process.ts"],"sourcesContent":["export async function daemonizeProcess(): Promise<void> {\n const mod = await import('daemonize-process');\n const daemonize =\n typeof mod.default === 'function' ? mod.default : (mod as any);\n daemonize();\n}\n"],"mappings":";;;AAAA,eAAsB,mBAAkC;AACtD,QAAM,MAAM,MAAM,OAAO,mBAAmB;AAC5C,QAAM,YACJ,OAAO,IAAI,YAAY,aAAa,IAAI,UAAW;AACrD,YAAU;AACZ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stamn/agent",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Stamn Agent Daemon CLI",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/stamnhq/stamn",
|
|
10
|
+
"directory": "apps/agent"
|
|
11
|
+
},
|
|
12
|
+
"bin": {
|
|
13
|
+
"stamn-agent": "./bin/run.js"
|
|
14
|
+
},
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"types": "dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"bin",
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"oclif": {
|
|
25
|
+
"bin": "stamn-agent",
|
|
26
|
+
"dirname": "stamn-agent",
|
|
27
|
+
"commands": "./dist/commands",
|
|
28
|
+
"topicSeparator": " ",
|
|
29
|
+
"topics": {
|
|
30
|
+
"config": {
|
|
31
|
+
"description": "Manage daemon configuration"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup",
|
|
37
|
+
"dev": "tsup --watch",
|
|
38
|
+
"start": "node bin/run.js",
|
|
39
|
+
"clean": "rm -rf dist .turbo",
|
|
40
|
+
"type-check": "tsc --noEmit",
|
|
41
|
+
"lint": "eslint src/",
|
|
42
|
+
"prepublishOnly": "pnpm build"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@oclif/core": "^4.2.0",
|
|
46
|
+
"conf": "^13.0.1",
|
|
47
|
+
"daemonize-process": "^3.0.0",
|
|
48
|
+
"pino": "^9.6.0",
|
|
49
|
+
"pino-pretty": "^13.0.0",
|
|
50
|
+
"ws": "^8.18.0",
|
|
51
|
+
"zod": "^3.24.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@repo/types": "workspace:*",
|
|
55
|
+
"@types/ws": "^8.5.0",
|
|
56
|
+
"@types/node": "^22.19.0",
|
|
57
|
+
"tsup": "^8.4.0",
|
|
58
|
+
"typescript": "^5.7.0"
|
|
59
|
+
}
|
|
60
|
+
}
|