@jini-ai/http-kit 0.3.1 → 0.3.3

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 (74) hide show
  1. package/LICENSE +201 -0
  2. package/dist/delegated-tools.d.ts +89 -0
  3. package/dist/delegated-tools.d.ts.map +1 -1
  4. package/dist/delegated-tools.js +106 -1
  5. package/dist/delegated-tools.js.map +1 -1
  6. package/package.json +15 -15
  7. package/dist/express/run-stream.d.ts +0 -15
  8. package/dist/express/run-stream.d.ts.map +0 -1
  9. package/dist/express/run-stream.js +0 -11
  10. package/dist/express/run-stream.js.map +0 -1
  11. package/dist/express-index.d.ts +0 -32
  12. package/dist/express-index.d.ts.map +0 -1
  13. package/dist/express-index.js +0 -14
  14. package/dist/express-index.js.map +0 -1
  15. package/dist/fastify/adapter.d.ts +0 -31
  16. package/dist/fastify/adapter.d.ts.map +0 -1
  17. package/dist/fastify/adapter.js +0 -64
  18. package/dist/fastify/adapter.js.map +0 -1
  19. package/dist/fastify/agents.d.ts +0 -13
  20. package/dist/fastify/agents.d.ts.map +0 -1
  21. package/dist/fastify/agents.js +0 -7
  22. package/dist/fastify/agents.js.map +0 -1
  23. package/dist/fastify/api-security-middleware.d.ts +0 -64
  24. package/dist/fastify/api-security-middleware.d.ts.map +0 -1
  25. package/dist/fastify/api-security-middleware.js +0 -139
  26. package/dist/fastify/api-security-middleware.js.map +0 -1
  27. package/dist/fastify/compat.d.ts +0 -22
  28. package/dist/fastify/compat.d.ts.map +0 -1
  29. package/dist/fastify/compat.js +0 -16
  30. package/dist/fastify/compat.js.map +0 -1
  31. package/dist/fastify/daemon-status.d.ts +0 -22
  32. package/dist/fastify/daemon-status.d.ts.map +0 -1
  33. package/dist/fastify/daemon-status.js +0 -9
  34. package/dist/fastify/daemon-status.js.map +0 -1
  35. package/dist/fastify/host-tools.d.ts +0 -13
  36. package/dist/fastify/host-tools.d.ts.map +0 -1
  37. package/dist/fastify/host-tools.js +0 -8
  38. package/dist/fastify/host-tools.js.map +0 -1
  39. package/dist/fastify/index.d.ts +0 -36
  40. package/dist/fastify/index.d.ts.map +0 -1
  41. package/dist/fastify/index.js +0 -18
  42. package/dist/fastify/index.js.map +0 -1
  43. package/dist/fastify/local-daemon-request.d.ts +0 -43
  44. package/dist/fastify/local-daemon-request.d.ts.map +0 -1
  45. package/dist/fastify/local-daemon-request.js +0 -155
  46. package/dist/fastify/local-daemon-request.js.map +0 -1
  47. package/dist/fastify/origin.d.ts +0 -21
  48. package/dist/fastify/origin.d.ts.map +0 -1
  49. package/dist/fastify/origin.js +0 -14
  50. package/dist/fastify/origin.js.map +0 -1
  51. package/dist/fastify/request.d.ts +0 -20
  52. package/dist/fastify/request.d.ts.map +0 -1
  53. package/dist/fastify/request.js +0 -25
  54. package/dist/fastify/request.js.map +0 -1
  55. package/dist/fastify/response.d.ts +0 -20
  56. package/dist/fastify/response.d.ts.map +0 -1
  57. package/dist/fastify/response.js +0 -41
  58. package/dist/fastify/response.js.map +0 -1
  59. package/dist/fastify/route-registration-guard.d.ts +0 -70
  60. package/dist/fastify/route-registration-guard.d.ts.map +0 -1
  61. package/dist/fastify/route-registration-guard.js +0 -69
  62. package/dist/fastify/route-registration-guard.js.map +0 -1
  63. package/dist/fastify/run-stream.d.ts +0 -18
  64. package/dist/fastify/run-stream.d.ts.map +0 -1
  65. package/dist/fastify/run-stream.js +0 -10
  66. package/dist/fastify/run-stream.js.map +0 -1
  67. package/dist/fastify/runs.d.ts +0 -17
  68. package/dist/fastify/runs.d.ts.map +0 -1
  69. package/dist/fastify/runs.js +0 -33
  70. package/dist/fastify/runs.js.map +0 -1
  71. package/dist/run-stream.d.ts +0 -60
  72. package/dist/run-stream.d.ts.map +0 -1
  73. package/dist/run-stream.js +0 -108
  74. package/dist/run-stream.js.map +0 -1
@@ -1,60 +0,0 @@
1
- /**
2
- * @module run-stream
3
- *
4
- * An encoder-driven SSE run-stream route: subscribes to a run via `@jini-ai/daemon`'s
5
- * `RunLifecycle.stream`, pipes every event through a composition-root-supplied protocol encoder,
6
- * and writes each non-null event to the SSE response opened by `createSseResponse`.
7
- * `handleRunStreamRequest` is the core
8
- * — it only ever touches `node:http`'s `IncomingMessage`/`ServerResponse` — and
9
- * `registerRunStreamRoute` below is Express's own thin mounting glue (Express's `Request`/
10
- * `Response` already *are* Node's raw `http.IncomingMessage`/`http.ServerResponse`, so resolving
11
- * `req.params.runId` and handing the request/response straight through is the whole job).
12
- */
13
- import type { IncomingMessage, ServerResponse } from 'node:http';
14
- import type { RunLifecycle } from '@jini-ai/daemon';
15
- import type { RunProtocolEvent } from '@jini-ai/protocol';
16
- import type { Express } from 'express';
17
- /** Backward-compatible route path used by the AG-UI composition. The handler itself is encoder-agnostic. */
18
- export declare const RUN_STREAM_ROUTE_PATH = "/api/runs/:runId/agui-stream";
19
- export interface RunStreamEncoder {
20
- encode(event: RunProtocolEvent, context: {
21
- readonly runId: string;
22
- }): unknown | null;
23
- }
24
- export interface RunStreamInternalErrorContext {
25
- readonly source: 'encoder' | 'lifecycle' | 'route';
26
- readonly runId: string;
27
- readonly error: unknown;
28
- }
29
- export interface RunStreamDeps {
30
- readonly lifecycle: RunLifecycle;
31
- /** Protocol adapter supplied by the composition root (for example `createGenUiEncoder()`). */
32
- readonly encoder: RunStreamEncoder;
33
- /** Host-owned sink for failures that must not escape an async Express handler. Defaults to `console.error`. */
34
- readonly onInternalError?: (context: RunStreamInternalErrorContext) => void;
35
- }
36
- /**
37
- * Handles one encoded SSE run-stream request end to end. Opens the SSE connection immediately (an
38
- * SSE endpoint commits to `text/event-stream` headers before it can know whether `runId` is
39
- * valid), then subscribes to the run: a replay-then-live-subscribe `unknown-run`/`replay-gap`/
40
- * `invalid-cursor` result is reported as one `{ error }` SSE data event before closing (there is
41
- * no JSON-status-code channel left once SSE headers are already committed). On the happy path,
42
- * every event `RunLifecycle` delivers is encoded and forwarded; the connection closes itself once
43
- * the run's own terminal `'end'` event has been forwarded (a driver's contract with
44
- * `RunLifecycle` guarantees no further events follow a run's `'end')`, so nothing is lost by
45
- * closing right after it). If the client disconnects first, `createSseResponse`'s own
46
- * `req.on('close', ...)` fires first, which (via `onClose` here) unsubscribes from the run so a
47
- * disconnected client never leaves a dangling `RunLifecycle` subscriber.
48
- *
49
- * @param req - The raw request `createSseResponse` opens the stream against.
50
- * @param res - The raw response `createSseResponse` opens the stream against.
51
- * @param runId - The run to stream, already resolved by the caller's transport-specific glue.
52
- * @param deps.lifecycle - The `RunLifecycle` to subscribe to.
53
- * @param deps.encoder - The protocol adapter chosen by the composition root.
54
- * @complexity O(1) plus the supplied encoder's per-event cost and `RunLifecycle.stream`'s replay cost.
55
- * @overallScore 100/100
56
- */
57
- export declare function handleRunStreamRequest(req: IncomingMessage, res: ServerResponse, runId: string, deps: RunStreamDeps): Promise<void>;
58
- /** Mounts the encoder-driven SSE run-stream route on `app`. */
59
- export declare function registerRunStreamRoute(app: Express, deps: RunStreamDeps): void;
60
- //# sourceMappingURL=run-stream.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"run-stream.d.ts","sourceRoot":"","sources":["../src/run-stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,KAAK,EAAE,YAAY,EAAyB,MAAM,iBAAiB,CAAC;AAC3E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,SAAS,CAAC;AAG1D,4GAA4G;AAC5G,eAAO,MAAM,qBAAqB,iCAAiC,CAAC;AAEpE,MAAM,WAAW,gBAAgB;IAC/B,MAAM,CAAC,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE;QAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,GAAG,IAAI,CAAC;CACtF;AAED,MAAM,WAAW,6BAA6B;IAC5C,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,WAAW,GAAG,OAAO,CAAC;IACnD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,SAAS,EAAE,YAAY,CAAC;IACjC,8FAA8F;IAC9F,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC;IACnC,+GAA+G;IAC/G,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,6BAA6B,KAAK,IAAI,CAAC;CAC7E;AAwBD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,eAAe,EACpB,GAAG,EAAE,cAAc,EACnB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,aAAa,GAClB,OAAO,CAAC,IAAI,CAAC,CAuCf;AAED,+DAA+D;AAC/D,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI,CAiB9E"}
@@ -1,108 +0,0 @@
1
- import { createSseResponse } from './raw-sse.js';
2
- /** Backward-compatible route path used by the AG-UI composition. The handler itself is encoder-agnostic. */
3
- export const RUN_STREAM_ROUTE_PATH = '/api/runs/:runId/agui-stream';
4
- function reportRunStreamInternalError(deps, source, runId, error) {
5
- const context = { source, runId, error };
6
- try {
7
- if (deps.onInternalError) {
8
- deps.onInternalError(context);
9
- }
10
- else {
11
- // eslint-disable-next-line no-console
12
- console.error(`[@jini-ai/http-kit] internal error (run-stream:${source}, runId=${runId})`, error);
13
- }
14
- }
15
- catch (sinkError) {
16
- // A diagnostic sink must never turn an already-contained stream failure into an unhandled
17
- // rejection of its own.
18
- // eslint-disable-next-line no-console
19
- console.error(`[@jini-ai/http-kit] internal error sink failed (run-stream:${source}, runId=${runId})`, sinkError);
20
- }
21
- }
22
- /**
23
- * Handles one encoded SSE run-stream request end to end. Opens the SSE connection immediately (an
24
- * SSE endpoint commits to `text/event-stream` headers before it can know whether `runId` is
25
- * valid), then subscribes to the run: a replay-then-live-subscribe `unknown-run`/`replay-gap`/
26
- * `invalid-cursor` result is reported as one `{ error }` SSE data event before closing (there is
27
- * no JSON-status-code channel left once SSE headers are already committed). On the happy path,
28
- * every event `RunLifecycle` delivers is encoded and forwarded; the connection closes itself once
29
- * the run's own terminal `'end'` event has been forwarded (a driver's contract with
30
- * `RunLifecycle` guarantees no further events follow a run's `'end')`, so nothing is lost by
31
- * closing right after it). If the client disconnects first, `createSseResponse`'s own
32
- * `req.on('close', ...)` fires first, which (via `onClose` here) unsubscribes from the run so a
33
- * disconnected client never leaves a dangling `RunLifecycle` subscriber.
34
- *
35
- * @param req - The raw request `createSseResponse` opens the stream against.
36
- * @param res - The raw response `createSseResponse` opens the stream against.
37
- * @param runId - The run to stream, already resolved by the caller's transport-specific glue.
38
- * @param deps.lifecycle - The `RunLifecycle` to subscribe to.
39
- * @param deps.encoder - The protocol adapter chosen by the composition root.
40
- * @complexity O(1) plus the supplied encoder's per-event cost and `RunLifecycle.stream`'s replay cost.
41
- * @overallScore 100/100
42
- */
43
- export async function handleRunStreamRequest(req, res, runId, deps) {
44
- let unsubscribe;
45
- const connection = createSseResponse(req, res, {
46
- onClose: () => unsubscribe?.(),
47
- });
48
- const encoder = deps.encoder;
49
- let result;
50
- try {
51
- result = await deps.lifecycle.stream(runId, (event) => {
52
- if (connection.closed)
53
- return;
54
- try {
55
- const encoded = encoder.encode(event, { runId });
56
- if (encoded != null)
57
- connection.send(encoded);
58
- if (event.kind === 'end')
59
- connection.close();
60
- }
61
- catch (error) {
62
- reportRunStreamInternalError(deps, 'encoder', runId, error);
63
- connection.close();
64
- }
65
- });
66
- }
67
- catch (error) {
68
- reportRunStreamInternalError(deps, 'lifecycle', runId, error);
69
- connection.close();
70
- return;
71
- }
72
- if (result.kind !== 'ok') {
73
- connection.send({ error: result.kind });
74
- connection.close();
75
- return;
76
- }
77
- // Assigned even when the run was already terminal (in which case the driver-facing 'end' event
78
- // was already replayed synchronously above, already closing the connection, and `unsubscribe`
79
- // here is `RunLifecycle`'s own no-op stub for that case) — keeping this assignment unconditional
80
- // avoids a branch whose two arms would otherwise do the same thing.
81
- unsubscribe = result.unsubscribe;
82
- // The raw request can close while RunLifecycle is still awaiting durable replay. In that
83
- // window `onClose` runs before `unsubscribe` exists, so clean up immediately once it arrives.
84
- if (connection.closed)
85
- unsubscribe();
86
- }
87
- /** Mounts the encoder-driven SSE run-stream route on `app`. */
88
- export function registerRunStreamRoute(app, deps) {
89
- app.get(RUN_STREAM_ROUTE_PATH, async (req, res) => {
90
- // `:runId` is a required path segment of RUN_STREAM_ROUTE_PATH — this handler is only ever
91
- // reached via a URL that already matched it, so the param is always present at runtime even
92
- // though @types/express types every param as possibly `undefined` in general.
93
- const runId = req.params.runId;
94
- try {
95
- await handleRunStreamRequest(req, res, runId, deps);
96
- }
97
- catch (error) {
98
- reportRunStreamInternalError(deps, 'route', runId, error);
99
- if (!res.headersSent) {
100
- res.status(500).json({ error: { code: 'INTERNAL_ERROR', message: 'an internal error occurred' } });
101
- }
102
- else if (!res.writableEnded) {
103
- res.end();
104
- }
105
- }
106
- });
107
- }
108
- //# sourceMappingURL=run-stream.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"run-stream.js","sourceRoot":"","sources":["../src/run-stream.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjD,4GAA4G;AAC5G,MAAM,CAAC,MAAM,qBAAqB,GAAG,8BAA8B,CAAC;AAoBpE,SAAS,4BAA4B,CACnC,IAAmB,EACnB,MAA+C,EAC/C,KAAa,EACb,KAAc;IAEd,MAAM,OAAO,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;IACzC,IAAI,CAAC;QACH,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YACzB,IAAI,CAAC,eA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