@rcrsr/rill-agent-chat 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andre Bremer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -62,7 +62,6 @@ export interface ChatMessage {
62
62
  content: string;
63
63
  }
64
64
  export interface ChatRequest {
65
- model?: string | undefined;
66
65
  messages: ChatMessage[];
67
66
  stream?: boolean | undefined;
68
67
  }
package/dist/index.js CHANGED
@@ -19,18 +19,33 @@ function createHarnessLifecycle(options) {
19
19
  if (server !== void 0) {
20
20
  throw new Error("Server is already listening");
21
21
  }
22
- return new Promise((resolve) => {
23
- server = serve({ fetch: app.fetch, port }, () => {
24
- options?.serverTweaks?.(server);
22
+ return new Promise((resolve, reject) => {
23
+ const started = serve({ fetch: app.fetch, port }, () => {
24
+ started.off("error", onError);
25
+ options?.serverTweaks?.(started);
25
26
  resolve();
26
27
  });
28
+ function onError(err) {
29
+ server = void 0;
30
+ reject(err);
31
+ }
32
+ started.once("error", onError);
33
+ server = started;
27
34
  });
28
35
  }
29
36
  async function close() {
30
- if (server !== void 0) {
31
- server.close();
32
- server = void 0;
37
+ if (server === void 0) return;
38
+ const current = server;
39
+ server = void 0;
40
+ if ("closeAllConnections" in current) {
41
+ current.closeAllConnections();
33
42
  }
43
+ await new Promise((resolve, reject) => {
44
+ current.close((err) => {
45
+ if (err) reject(err);
46
+ else resolve();
47
+ });
48
+ });
34
49
  }
35
50
  return { app, listen, close };
36
51
  }
@@ -165,12 +180,6 @@ function inspectChatHandler(handler) {
165
180
 
166
181
  // src/stream.ts
167
182
  var encoder = new TextEncoder();
168
- function toAsyncIterable(source) {
169
- if (Symbol.asyncIterator in source) {
170
- return source;
171
- }
172
- return source;
173
- }
174
183
  function fillChunk(chunk, defaults) {
175
184
  return {
176
185
  id: chunk.id ?? defaults.id,
@@ -198,7 +207,7 @@ function buildErrorChunk(defaults) {
198
207
  };
199
208
  }
200
209
  async function createChatStreamResponse(source, options) {
201
- const iterable = toAsyncIterable(source);
210
+ const iterable = source;
202
211
  const iter = iterable[Symbol.asyncIterator]();
203
212
  const streamId = `chatcmpl-${crypto.randomUUID()}`;
204
213
  const created = Math.floor(Date.now() / 1e3);
@@ -255,7 +264,7 @@ function checkChunkForError(chunk) {
255
264
  }
256
265
  }
257
266
  async function createChatCompletionResponse(source, options) {
258
- const iterable = toAsyncIterable(source);
267
+ const iterable = source;
259
268
  const iter = iterable[Symbol.asyncIterator]();
260
269
  const id = `chatcmpl-${crypto.randomUUID()}`;
261
270
  const created = Math.floor(Date.now() / 1e3);
@@ -372,6 +381,15 @@ function validateMessages(value) {
372
381
  function getHandlerFromRouter(router, name) {
373
382
  return router.manifest.agents.get(name);
374
383
  }
384
+ function isValidChatChunkShape(value) {
385
+ if (typeof value !== "object" || value === null) return false;
386
+ const choices = value["choices"];
387
+ if (!Array.isArray(choices) || choices.length === 0) return false;
388
+ const first = choices[0];
389
+ if (typeof first !== "object" || first === null) return false;
390
+ const delta = first["delta"];
391
+ return typeof delta === "object" && delta !== null;
392
+ }
375
393
  function invokeHandlerAsStream(handler, messages, abortController) {
376
394
  return new ReadableStream({
377
395
  start(controller) {
@@ -383,6 +401,14 @@ function invokeHandlerAsStream(handler, messages, abortController) {
383
401
  signal: abortController.signal,
384
402
  onChunk: async (chunk) => {
385
403
  if (abortController.signal.aborted) return;
404
+ if (!isValidChatChunkShape(chunk)) {
405
+ controller.error(
406
+ new ChatChunkError(
407
+ "Malformed chunk from handler: choices must be a non-empty array whose first entry has a delta object"
408
+ )
409
+ );
410
+ return;
411
+ }
386
412
  controller.enqueue(chunk);
387
413
  }
388
414
  }
@@ -458,26 +484,19 @@ function createChatHarness(router, options) {
458
484
  (async () => {
459
485
  const writer = writable.getWriter();
460
486
  try {
461
- if (Symbol.asyncIterator in source) {
462
- for await (const chunk of source) {
463
- if (abortController.signal.aborted) break;
464
- await writer.write(chunk);
465
- }
466
- } else {
467
- const reader = source.getReader();
468
- try {
469
- let result = await reader.read();
470
- while (!result.done) {
471
- if (abortController.signal.aborted) {
472
- await reader.cancel();
473
- break;
474
- }
475
- await writer.write(result.value);
476
- result = await reader.read();
487
+ const reader = source.getReader();
488
+ try {
489
+ let result = await reader.read();
490
+ while (!result.done) {
491
+ if (abortController.signal.aborted) {
492
+ await reader.cancel();
493
+ break;
477
494
  }
478
- } finally {
479
- reader.releaseLock();
495
+ await writer.write(result.value);
496
+ result = await reader.read();
480
497
  }
498
+ } finally {
499
+ reader.releaseLock();
481
500
  }
482
501
  if (abortController.signal.aborted) {
483
502
  await writer.abort();
@@ -578,6 +597,17 @@ function createChatHarness(router, options) {
578
597
  { error: { message: "No default agent configured" } },
579
598
  500
580
599
  );
600
+ } else if (router.agents().includes(agentName) && ineligibleReasons.has(agentName)) {
601
+ errors++;
602
+ requests++;
603
+ return c.json(
604
+ {
605
+ error: {
606
+ message: `Agent "${agentName}" is not chat-eligible: ${ineligibleReasons.get(agentName)}`
607
+ }
608
+ },
609
+ 403
610
+ );
581
611
  } else {
582
612
  errors++;
583
613
  requests++;
@@ -589,10 +619,17 @@ function createChatHarness(router, options) {
589
619
  }
590
620
  const chatReq = {
591
621
  messages: validation.messages,
592
- ...typeof body["model"] === "string" ? { model: body["model"] } : {},
593
622
  ...typeof body["stream"] === "boolean" ? { stream: body["stream"] } : {}
594
623
  };
595
624
  const abortController = new AbortController();
625
+ const requestSignal = c.req.raw.signal;
626
+ if (requestSignal.aborted) {
627
+ abortController.abort();
628
+ } else {
629
+ requestSignal.addEventListener("abort", () => abortController.abort(), {
630
+ once: true
631
+ });
632
+ }
596
633
  const handler = getHandlerFromRouter(router, resolvedAgent);
597
634
  if (handler === void 0) {
598
635
  errors++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rcrsr/rill-agent-chat",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "rill agent chat harness — Hono-based chat endpoint wrapping AgentRouter",
5
5
  "license": "MIT",
6
6
  "author": "Andre Bremer",
@@ -14,22 +14,15 @@
14
14
  "default": "./dist/index.js"
15
15
  }
16
16
  },
17
- "scripts": {
18
- "build": "tsup && dts-bundle-generator --config dts-bundle-generator.config.cjs && node -e \"const fs=require('fs');const walk=(d)=>fs.readdirSync(d,{withFileTypes:true}).flatMap(e=>e.isDirectory()?walk(d+'/'+e.name):[d+'/'+e.name]);const hits=walk('dist').filter(f=>fs.readFileSync(f,'utf8').includes('@rcrsr/rill-agent-hono-kit'));if(hits.length){console.error('hono-kit leak in dist:',hits);process.exit(1);}\"",
19
- "test": "vitest run",
20
- "typecheck": "tsc --noEmit",
21
- "lint": "oxlint --config ../../../.oxlintrc.json src/ tests/",
22
- "check": "pnpm run build && pnpm run test && pnpm run lint"
23
- },
24
17
  "dependencies": {
25
- "@rcrsr/rill-agent": "workspace:~",
26
- "@hono/node-server": "^2.0.1",
27
- "hono": "^4.12.16"
18
+ "@hono/node-server": "^2.1.1",
19
+ "@rcrsr/rill-agent": "~0.21.0",
20
+ "hono": "^4.13.7"
28
21
  },
29
22
  "devDependencies": {
30
- "@rcrsr/rill-agent-hono-kit": "workspace:^",
23
+ "@rcrsr/rill-agent-hono-kit": "^0.21.0",
31
24
  "dts-bundle-generator": "^9.5.1",
32
- "tsup": "^8.5.0"
25
+ "tsup": "^8.5.1"
33
26
  },
34
27
  "files": [
35
28
  "dist"
@@ -41,5 +34,12 @@
41
34
  "type": "git",
42
35
  "url": "git+https://github.com/rcrsr/rill-agent.git",
43
36
  "directory": "packages/agent/chat"
37
+ },
38
+ "scripts": {
39
+ "build": "tsup && dts-bundle-generator --config dts-bundle-generator.config.cjs && node -e \"const fs=require('fs');const walk=(d)=>fs.readdirSync(d,{withFileTypes:true}).flatMap(e=>e.isDirectory()?walk(d+'/'+e.name):[d+'/'+e.name]);const hits=walk('dist').filter(f=>fs.readFileSync(f,'utf8').includes('@rcrsr/rill-agent-hono-kit'));if(hits.length){console.error('hono-kit leak in dist:',hits);process.exit(1);}\"",
40
+ "test": "vitest run",
41
+ "typecheck": "tsc --noEmit",
42
+ "lint": "oxlint --config ../../../.oxlintrc.json src/ tests/",
43
+ "check": "pnpm run build && pnpm run test && pnpm run lint"
44
44
  }
45
- }
45
+ }