agent-worker 0.18.0 → 0.19.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.
@@ -0,0 +1,171 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-wcPFST8Q.mjs";
2
+ import { a as isDaemonRunning } from "./daemon-CwaHgxs6.mjs";
3
+ //#region src/cli/client.ts
4
+ /**
5
+ * CLI client — HTTP client for daemon REST API.
6
+ *
7
+ * Talks to the 9-endpoint daemon:
8
+ * GET /health, POST /shutdown
9
+ * GET/POST /agents, GET/DELETE /agents/:name
10
+ * POST /run (SSE), POST /serve
11
+ * ALL /mcp
12
+ */
13
+ var client_exports = /* @__PURE__ */ __exportAll({
14
+ createAgent: () => createAgent,
15
+ deleteAgent: () => deleteAgent,
16
+ health: () => health,
17
+ isDaemonActive: () => isDaemonActive,
18
+ listAgents: () => listAgents,
19
+ run: () => run,
20
+ serve: () => serve,
21
+ shutdown: () => shutdown,
22
+ startWorkflow: () => startWorkflow,
23
+ stopWorkflow: () => stopWorkflow
24
+ });
25
+ const MAX_RETRIES = 3;
26
+ const BASE_DELAY_MS = 200;
27
+ function isRetryableError(error) {
28
+ if (error instanceof TypeError) return true;
29
+ if (error instanceof Error) {
30
+ const code = error.code;
31
+ return code === "ECONNREFUSED" || code === "ECONNRESET";
32
+ }
33
+ return false;
34
+ }
35
+ function getDaemonConnection() {
36
+ const daemon = isDaemonRunning();
37
+ if (!daemon) return null;
38
+ return {
39
+ url: `http://${daemon.host}:${daemon.port}`,
40
+ token: daemon.token
41
+ };
42
+ }
43
+ function requireDaemon() {
44
+ const conn = getDaemonConnection();
45
+ if (!conn) throw new Error("No daemon running. Start one with: agent-worker up");
46
+ return conn;
47
+ }
48
+ /** Build headers with auth token */
49
+ function authHeaders(token, extra) {
50
+ const headers = { ...extra };
51
+ if (token) headers["Authorization"] = `Bearer ${token}`;
52
+ return headers;
53
+ }
54
+ async function request(method, path, body) {
55
+ const { url: baseUrl, token } = requireDaemon();
56
+ let lastError;
57
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) try {
58
+ const init = {
59
+ method,
60
+ headers: authHeaders(token, body !== void 0 ? { "Content-Type": "application/json" } : void 0),
61
+ body: body !== void 0 ? JSON.stringify(body) : void 0,
62
+ signal: AbortSignal.timeout(6e4)
63
+ };
64
+ return await (await fetch(`${baseUrl}${path}`, init)).json();
65
+ } catch (error) {
66
+ lastError = error;
67
+ if (attempt < MAX_RETRIES && isRetryableError(error)) {
68
+ const delay = BASE_DELAY_MS * Math.pow(2, attempt);
69
+ await new Promise((resolve) => setTimeout(resolve, delay));
70
+ } else break;
71
+ }
72
+ return {
73
+ success: false,
74
+ error: `Connection failed: ${lastError instanceof Error ? lastError.message : String(lastError)}`
75
+ };
76
+ }
77
+ /** GET /health */
78
+ function health() {
79
+ return request("GET", "/health");
80
+ }
81
+ /** POST /shutdown */
82
+ function shutdown() {
83
+ return request("POST", "/shutdown");
84
+ }
85
+ /** GET /agents */
86
+ function listAgents() {
87
+ return request("GET", "/agents");
88
+ }
89
+ /** POST /agents */
90
+ function createAgent(body) {
91
+ return request("POST", "/agents", body);
92
+ }
93
+ /** DELETE /agents/:name */
94
+ function deleteAgent(name) {
95
+ return request("DELETE", `/agents/${encodeURIComponent(name)}`);
96
+ }
97
+ /** POST /serve (sync JSON response) */
98
+ function serve(body) {
99
+ return request("POST", "/serve", body);
100
+ }
101
+ /**
102
+ * POST /run (SSE stream).
103
+ * Calls onChunk for each chunk, returns final response.
104
+ */
105
+ async function run(body, onChunk) {
106
+ let baseUrl;
107
+ let token;
108
+ try {
109
+ const conn = requireDaemon();
110
+ baseUrl = conn.url;
111
+ token = conn.token;
112
+ } catch (error) {
113
+ return {
114
+ success: false,
115
+ error: error instanceof Error ? error.message : String(error)
116
+ };
117
+ }
118
+ try {
119
+ const res = await fetch(`${baseUrl}/run`, {
120
+ method: "POST",
121
+ headers: authHeaders(token, { "Content-Type": "application/json" }),
122
+ body: JSON.stringify(body)
123
+ });
124
+ if (!res.ok || !res.body) return await res.json();
125
+ const reader = res.body.getReader();
126
+ const decoder = new TextDecoder();
127
+ let buffer = "";
128
+ let finalResponse = { success: true };
129
+ while (true) {
130
+ const { value, done } = await reader.read();
131
+ if (done) break;
132
+ buffer += decoder.decode(value, { stream: true });
133
+ const lines = buffer.split("\n");
134
+ buffer = lines.pop() ?? "";
135
+ let currentEvent = "";
136
+ for (const line of lines) if (line.startsWith("event: ")) currentEvent = line.slice(7);
137
+ else if (line.startsWith("data: ")) {
138
+ const data = line.slice(6);
139
+ try {
140
+ const parsed = JSON.parse(data);
141
+ if (currentEvent === "chunk" && onChunk) onChunk(parsed);
142
+ else if (currentEvent === "done") finalResponse = parsed;
143
+ else if (currentEvent === "error") return {
144
+ success: false,
145
+ error: parsed.error
146
+ };
147
+ } catch {}
148
+ }
149
+ }
150
+ return finalResponse;
151
+ } catch (error) {
152
+ return {
153
+ success: false,
154
+ error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`
155
+ };
156
+ }
157
+ }
158
+ /** POST /workflows — start a workflow via daemon */
159
+ function startWorkflow(body) {
160
+ return request("POST", "/workflows", body);
161
+ }
162
+ /** DELETE /workflows/:name[/:tag] — stop a workspace */
163
+ function stopWorkflow(name, tag) {
164
+ return request("DELETE", tag ? `/workflows/${encodeURIComponent(name)}/${encodeURIComponent(tag)}` : `/workflows/${encodeURIComponent(name)}`);
165
+ }
166
+ /** Check if daemon is running */
167
+ function isDaemonActive() {
168
+ return getDaemonConnection() !== null;
169
+ }
170
+ //#endregion
171
+ export { isDaemonActive as a, serve as c, stopWorkflow as d, health as i, shutdown as l, createAgent as n, listAgents as o, deleteAgent as r, run as s, client_exports as t, startWorkflow as u };