@rcrsr/rill-agent-http 0.20.0 → 0.21.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.
Files changed (2) hide show
  1. package/dist/index.js +45 -37
  2. package/package.json +6 -6
package/dist/index.js CHANGED
@@ -24,18 +24,33 @@ function createHarnessLifecycle(options) {
24
24
  if (server !== void 0) {
25
25
  throw new Error("Server is already listening");
26
26
  }
27
- return new Promise((resolve) => {
28
- server = serve({ fetch: app.fetch, port }, () => {
29
- options?.serverTweaks?.(server);
27
+ return new Promise((resolve, reject) => {
28
+ const started = serve({ fetch: app.fetch, port }, () => {
29
+ started.off("error", onError);
30
+ options?.serverTweaks?.(started);
30
31
  resolve();
31
32
  });
33
+ function onError(err) {
34
+ server = void 0;
35
+ reject(err);
36
+ }
37
+ started.once("error", onError);
38
+ server = started;
32
39
  });
33
40
  }
34
41
  async function close() {
35
- if (server !== void 0) {
36
- server.close();
37
- server = void 0;
42
+ if (server === void 0) return;
43
+ const current = server;
44
+ server = void 0;
45
+ if ("closeAllConnections" in current) {
46
+ current.closeAllConnections();
38
47
  }
48
+ await new Promise((resolve, reject) => {
49
+ current.close((err) => {
50
+ if (err) reject(err);
51
+ else resolve();
52
+ });
53
+ });
39
54
  }
40
55
  return { app, listen, close };
41
56
  }
@@ -82,8 +97,7 @@ function httpHarness(router) {
82
97
  }));
83
98
  return c.json({ agents });
84
99
  });
85
- app.post("/agents/:name/run", async (c) => {
86
- const name = c.req.param("name");
100
+ async function handleRun(c, name) {
87
101
  let body;
88
102
  try {
89
103
  const parsed = await c.req.json();
@@ -91,14 +105,32 @@ function httpHarness(router) {
91
105
  } catch {
92
106
  return c.json({ error: "Request body must be a JSON object" }, 400);
93
107
  }
94
- const params = body["params"] ?? {};
108
+ let params;
109
+ try {
110
+ params = body["params"] === void 0 ? {} : assertJsonObject(body["params"]);
111
+ } catch {
112
+ return c.json({ error: 'Parameter "params" must be a JSON object' }, 400);
113
+ }
95
114
  const validationError = validateParams(params, name, router);
96
115
  if (validationError !== null) {
97
116
  return c.json({ error: validationError }, 400);
98
117
  }
118
+ const rawTimeout = body["timeout"];
119
+ let timeout;
120
+ if (rawTimeout !== void 0) {
121
+ if (typeof rawTimeout !== "number" || !Number.isFinite(rawTimeout) || rawTimeout <= 0) {
122
+ return c.json(
123
+ {
124
+ error: 'Parameter "timeout" must be a finite number greater than 0'
125
+ },
126
+ 400
127
+ );
128
+ }
129
+ timeout = rawTimeout;
130
+ }
99
131
  const request = {
100
132
  params,
101
- ...typeof body["timeout"] === "number" ? { timeout: body["timeout"] } : {}
133
+ ...timeout !== void 0 ? { timeout } : {}
102
134
  };
103
135
  try {
104
136
  const response = await router.run(name, request);
@@ -108,33 +140,9 @@ function httpHarness(router) {
108
140
  const message = err instanceof Error ? err.message : String(err);
109
141
  return c.json({ error: message }, status);
110
142
  }
111
- });
112
- app.post("/run", async (c) => {
113
- let body;
114
- try {
115
- const parsed = await c.req.json();
116
- body = assertJsonObject(parsed);
117
- } catch {
118
- return c.json({ error: "Request body must be a JSON object" }, 400);
119
- }
120
- const params = body["params"] ?? {};
121
- const defaultName = router.defaultAgent();
122
- const validationError = validateParams(params, defaultName, router);
123
- if (validationError !== null) {
124
- return c.json({ error: validationError }, 400);
125
- }
126
- const request = {
127
- params,
128
- ...typeof body["timeout"] === "number" ? { timeout: body["timeout"] } : {}
129
- };
130
- try {
131
- const response = await router.run("", request);
132
- return c.json(response);
133
- } catch (err) {
134
- const message = err instanceof Error ? err.message : String(err);
135
- return c.json({ error: message }, 500);
136
- }
137
- });
143
+ }
144
+ app.post("/agents/:name/run", (c) => handleRun(c, c.req.param("name")));
145
+ app.post("/run", (c) => handleRun(c, ""));
138
146
  async function listen(port = 3e3) {
139
147
  return lifecycle.listen(port);
140
148
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rcrsr/rill-agent-http",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "rill agent HTTP harness — Hono-based HTTP server wrapping AgentRouter",
5
5
  "license": "MIT",
6
6
  "author": "Andre Bremer",
@@ -15,14 +15,14 @@
15
15
  }
16
16
  },
17
17
  "dependencies": {
18
- "@hono/node-server": "^2.0.1",
19
- "hono": "^4.12.16",
20
- "@rcrsr/rill-agent": "~0.20.0"
18
+ "@hono/node-server": "^2.1.1",
19
+ "@rcrsr/rill-agent": "~0.21.0",
20
+ "hono": "^4.13.7"
21
21
  },
22
22
  "devDependencies": {
23
+ "@rcrsr/rill-agent-hono-kit": "^0.21.0",
23
24
  "dts-bundle-generator": "^9.5.1",
24
- "tsup": "^8.5.0",
25
- "@rcrsr/rill-agent-hono-kit": "^0.20.0"
25
+ "tsup": "^8.5.1"
26
26
  },
27
27
  "files": [
28
28
  "dist"