@cogitator-ai/hono 0.8.6 → 0.10.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 (50) hide show
  1. package/README.md +86 -32
  2. package/dist/app.d.ts +5 -0
  3. package/dist/app.d.ts.map +1 -1
  4. package/dist/app.js +16 -4
  5. package/dist/app.js.map +1 -1
  6. package/dist/index.d.ts +2 -2
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/middleware/auth.d.ts +6 -0
  11. package/dist/middleware/auth.d.ts.map +1 -1
  12. package/dist/middleware/auth.js +15 -1
  13. package/dist/middleware/auth.js.map +1 -1
  14. package/dist/middleware/body-limit.d.ts.map +1 -1
  15. package/dist/middleware/body-limit.js +3 -1
  16. package/dist/middleware/body-limit.js.map +1 -1
  17. package/dist/middleware/context.d.ts +5 -0
  18. package/dist/middleware/context.d.ts.map +1 -1
  19. package/dist/middleware/context.js +10 -1
  20. package/dist/middleware/context.js.map +1 -1
  21. package/dist/middleware/error-handler.d.ts.map +1 -1
  22. package/dist/middleware/error-handler.js +3 -7
  23. package/dist/middleware/error-handler.js.map +1 -1
  24. package/dist/routes/agents.d.ts.map +1 -1
  25. package/dist/routes/agents.js +84 -44
  26. package/dist/routes/agents.js.map +1 -1
  27. package/dist/routes/swarms.d.ts.map +1 -1
  28. package/dist/routes/swarms.js +24 -17
  29. package/dist/routes/swarms.js.map +1 -1
  30. package/dist/routes/threads.d.ts.map +1 -1
  31. package/dist/routes/threads.js +17 -16
  32. package/dist/routes/threads.js.map +1 -1
  33. package/dist/routes/workflows.d.ts.map +1 -1
  34. package/dist/routes/workflows.js +15 -18
  35. package/dist/routes/workflows.js.map +1 -1
  36. package/dist/types.d.ts +42 -2
  37. package/dist/types.d.ts.map +1 -1
  38. package/dist/utils/errors.d.ts +6 -9
  39. package/dist/utils/errors.d.ts.map +1 -1
  40. package/dist/utils/errors.js +10 -19
  41. package/dist/utils/errors.js.map +1 -1
  42. package/dist/utils/request.d.ts +14 -1
  43. package/dist/utils/request.d.ts.map +1 -1
  44. package/dist/utils/request.js +36 -7
  45. package/dist/utils/request.js.map +1 -1
  46. package/dist/websocket/handler.d.ts +5 -1
  47. package/dist/websocket/handler.d.ts.map +1 -1
  48. package/dist/websocket/handler.js +43 -19
  49. package/dist/websocket/handler.js.map +1 -1
  50. package/package.json +8 -8
package/README.md CHANGED
@@ -44,21 +44,25 @@ Creates a Hono sub-application with all Cogitator endpoints.
44
44
 
45
45
  **Options:**
46
46
 
47
- | Option | Type | Description |
48
- | -------------------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
49
- | `cogitator` | `Cogitator` | **Required.** Cogitator runtime instance |
50
- | `agents` | `Record<string, Agent>` | Named agents to expose |
51
- | `workflows` | `Record<string, Workflow>` | Named workflows |
52
- | `swarms` | `Record<string, SwarmConfig>` | Named swarms |
53
- | `auth` | `AuthFunction` | `(c) => AuthContext \| undefined` (sync or async), receives the Hono Context, throw to answer `401 UNAUTHORIZED` |
54
- | `enableSwagger` | `boolean` | Serve `/openapi.json` and Swagger UI at `/docs` |
55
- | `swagger` | `SwaggerConfig` | Swagger configuration |
56
- | `enableWebSocket` | `boolean` | Enable the WebSocket endpoint |
57
- | `websocket` | `WebSocketConfig` | `{ path?, maxPayloadSize?, upgradeWebSocket? }` (see below) |
58
- | `bodyLimit` | `number` | Max request body size in bytes (default 1 MiB) |
59
- | `sseHeartbeatMs` | `number` | How often SSE streams write a `: keep-alive` comment while a run is silent (default `5000`, `0` turns it off) |
60
- | `acceptContext` | `boolean \| string[]` | Keys of a run `context` clients may send: none by default, a list, or `true` for any (the model reads it as data from the application) |
61
- | `threadMessageRoles` | `ThreadMessageRole[]` | Roles `POST /threads/:id/messages` accepts (default `user` and `assistant`) |
47
+ | Option | Type | Description |
48
+ | -------------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
49
+ | `cogitator` | `Cogitator` | **Required.** Cogitator runtime instance |
50
+ | `agents` | `Record<string, Agent>` | Named agents to expose |
51
+ | `workflows` | `Record<string, Workflow>` | Named workflows |
52
+ | `swarms` | `Record<string, SwarmConfig>` | Named swarms |
53
+ | `auth` | `AuthFunction` | `(c) => AuthContext \| undefined` (sync or async), receives the Hono Context, throw to answer `401 UNAUTHORIZED` |
54
+ | `enableSwagger` | `boolean` | Serve `/openapi.json` and Swagger UI at `/docs` |
55
+ | `swagger` | `SwaggerConfig` | Swagger configuration |
56
+ | `enableWebSocket` | `boolean` | Enable the WebSocket endpoint |
57
+ | `websocket` | `WebSocketConfig` | `{ path?, maxPayloadSize?, upgradeWebSocket? }` (see below) |
58
+ | `bodyLimit` | `number` | Max request body size in bytes (default 1 MiB) |
59
+ | `sseHeartbeatMs` | `number` | How often SSE streams write a `: keep-alive` comment while a run is silent (default `5000`, `0` turns it off) |
60
+ | `acceptContext` | `boolean \| string[]` | Keys of a run `context` clients may send: none by default, a list, or `true` for any (the model reads it as data from the application) |
61
+ | `threadMessageRoles` | `ThreadMessageRole[]` | Roles `POST /threads/:id/messages` accepts (default `user` and `assistant`) |
62
+ | `limits` | `RunLimits` | `{ swarmTimeout?, workflowMaxIterations?, workflowMaxConcurrency? }`: caps on what a request may ask a swarm or workflow run for (see [Run limits](#run-limits)) |
63
+ | `anonymous` | `'allow' \| 'deny'` | Whether callers without a `userId` may use the API (default `'allow'`), `'deny'` answers `401 UNAUTHORIZED` |
64
+ | `authorizeThread` | `(auth, threadId) => boolean \| Promise<boolean>` | Decides whether a caller may touch a thread, on top of the owner check, `false` answers `403 THREAD_FORBIDDEN` |
65
+ | `deps` | `(c) => unknown \| Promise<unknown>` | Builds the `deps` agent runs pass to their tools, from the Hono Context (see [Run deps](#run-deps)) |
62
66
 
63
67
  ## Request Handling
64
68
 
@@ -73,7 +77,8 @@ Creates a Hono sub-application with all Cogitator endpoints.
73
77
  - Swarm routes close each swarm they build once its run ends, so a distributed swarm leaves no Redis connections behind and its state expires. `GET /swarms` lists every agent, the router and pipeline stages included.
74
78
  - Thread routes use `cogitator.getMemory()`, which connects the configured memory adapter on first use, so threads can be read on a fresh server before any agent has run. Only a `Cogitator` without `memory` configured answers `503 UNAVAILABLE`.
75
79
  - Bodies above `bodyLimit` return `413 PAYLOAD_TOO_LARGE`.
76
- - `CogitatorError`s are returned with their HTTP status and code (for example `429 LLM_RATE_LIMITED`). Any other error is logged and returned as `500 Internal server error` (code `INTERNAL_ERROR`) without internal details; the same masking applies to SSE `error` events, `node_error`/`agent_error` stream events and WebSocket errors. A workflow that finishes with an error is answered like a thrown error (its `CogitatorError` status, otherwise `500`), never as a successful result. Missing optional packages (`@cogitator-ai/workflows`, `@cogitator-ai/swarms`) answer `501 UNIMPLEMENTED`.
80
+ - `GET /threads/:id` answers the whole thread, or one page of it with `?limit=50`: the newest 50 messages (oldest first within the page) and a `nextCursor`. Pass it back as `?limit=50&cursor=<nextCursor>` for the 50 before them, until an answer has no `nextCursor`. Messages written in the same millisecond are neither skipped nor repeated across pages. A `limit` outside 1 to 1000 or a cursor the server did not give answers `400 INVALID_INPUT`.
81
+ - Every error answer has the same envelope, `{ error: { message, code } }`, the one Express, Fastify, Koa and Next use. A `CogitatorError` is answered with its HTTP status and code (for example `429 LLM_RATE_LIMITED`), plus a `Retry-After` header in whole seconds when the provider said when to retry. A swarm that ran out of time answers `504 RUN_TIMEOUT`. Any other error is logged and answered as `500 Internal server error` (code `INTERNAL_ERROR`) without internal details, and the same masking applies to SSE `error` events, `node_error` and `agent_error` stream events and WebSocket errors. A workflow that finishes with an error is answered like a thrown error (its `CogitatorError` status, otherwise `500`), never as a successful result. Missing optional packages (`@cogitator-ai/workflows`, `@cogitator-ai/swarms`) answer `501 UNIMPLEMENTED`.
77
82
  - When the client disconnects, the running agent, workflow or swarm is aborted, for both JSON and SSE endpoints.
78
83
  - `GET /agents` returns each agent's `description` and never exposes its `instructions`.
79
84
  - `GET /tools` returns tool parameters as JSON Schema.
@@ -82,20 +87,21 @@ Creates a Hono sub-application with all Cogitator endpoints.
82
87
 
83
88
  ### Agents
84
89
 
85
- | Method | Path | Description |
86
- | ------ | ---------------------- | ------------------------- |
87
- | `GET` | `/agents` | List all agents |
88
- | `POST` | `/agents/:name/run` | Run agent (JSON response) |
89
- | `POST` | `/agents/:name/stream` | Run agent (SSE stream) |
90
- | `POST` | `/agents/:name/resume` | Resume a paused run |
90
+ | Method | Path | Description |
91
+ | ------ | ----------------------------- | -------------------------------- |
92
+ | `GET` | `/agents` | List all agents |
93
+ | `POST` | `/agents/:name/run` | Run agent (JSON response) |
94
+ | `POST` | `/agents/:name/stream` | Run agent (SSE stream) |
95
+ | `POST` | `/agents/:name/resume` | Resume a paused run |
96
+ | `POST` | `/agents/:name/resume/stream` | Resume a paused run (SSE stream) |
91
97
 
92
98
  ### Threads (Memory)
93
99
 
94
- | Method | Path | Description |
95
- | -------- | ----------------------- | --------------------- |
96
- | `GET` | `/threads/:id` | Get thread messages |
97
- | `POST` | `/threads/:id/messages` | Add message to thread |
98
- | `DELETE` | `/threads/:id` | Delete thread |
100
+ | Method | Path | Description |
101
+ | -------- | ----------------------- | ------------------------------------------------ |
102
+ | `GET` | `/threads/:id` | Get thread messages, a page with `?limit&cursor` |
103
+ | `POST` | `/threads/:id/messages` | Add message to thread |
104
+ | `DELETE` | `/threads/:id` | Delete thread |
99
105
 
100
106
  ### Workflows
101
107
 
@@ -153,6 +159,51 @@ When `auth` returns a `userId`, everything a caller does with threads is scoped
153
159
  - Another user's thread answers `403` with code `THREAD_ACCESS_DENIED`, and its messages are neither returned nor changed.
154
160
  - Threads created earlier without an owner (no `userId`) stay open only to callers without a `userId`, such as servers with no `auth` configured.
155
161
 
162
+ ### Anonymous callers
163
+
164
+ By default a caller without a `userId` (no `auth`, or `auth` returned none) may use the API as an anonymous user. With `anonymous: 'deny'` such a caller gets `401 UNAUTHORIZED` on every API route, the WebSocket upgrade included. `/health`, `/ready`, `/openapi.json` and `/docs` stay open, so probes and API docs keep working. A path the app does not serve answers `401` under `'deny'` too, so an anonymous caller cannot tell which paths exist, and `404 NOT_FOUND` in the error envelope under `'allow'`.
165
+
166
+ ### Thread authorization
167
+
168
+ Core lets a caller touch only threads they own. `authorizeThread` adds your own rule on top, for example a tenant check:
169
+
170
+ ```typescript
171
+ const api = cogitatorApp({
172
+ cogitator,
173
+ agents: { chat: chatAgent },
174
+ auth,
175
+ authorizeThread: (auth, threadId) => tenants.ownsThread(auth?.metadata?.tenant, threadId),
176
+ });
177
+ ```
178
+
179
+ It is called with what `auth` returned and the thread a request names: the body `threadId` of agent runs, streams and resumes and of swarm runs and streams, the `:id` of `GET`, `DELETE` and `POST /threads/:id/messages`, and the `threadId` of WebSocket runs and resumes. It runs after the body is validated and before the owner check, and `false` answers `403 THREAD_FORBIDDEN` (a WebSocket `error` message) without starting a run.
180
+
181
+ ### Run deps
182
+
183
+ `deps` builds what an agent's tools get as `context.deps` for one request: a database handle, the tenant, a client with the caller's credentials. The model never sees them, and they never come from the request body.
184
+
185
+ ```typescript
186
+ const api = cogitatorApp({
187
+ cogitator,
188
+ agents: { chat: chatAgent },
189
+ deps: async (c) => ({ db: await pool.connect(), tenant: c.req.header('x-tenant') }),
190
+ });
191
+ ```
192
+
193
+ Agent runs, streams and resumes call it once per run. WebSocket runs call it with the upgrade request's Context.
194
+
195
+ ### Run limits
196
+
197
+ `limits` caps what a request may ask a run for: `swarmTimeout` (ms) for swarm runs, `workflowMaxIterations` and `workflowMaxConcurrency` for workflow runs. A request that asks for more is held to the cap, and one that asks for nothing gets the cap, over HTTP, SSE and WebSocket alike. Each limit must be a positive integer, or `cogitatorApp` throws. The OpenAPI spec shows each limit as the default of its field.
198
+
199
+ ```typescript
200
+ const api = cogitatorApp({
201
+ cogitator,
202
+ swarms: { research: researchSwarm },
203
+ limits: { swarmTimeout: 120_000, workflowMaxIterations: 50, workflowMaxConcurrency: 4 },
204
+ });
205
+ ```
206
+
156
207
  ### Approvals
157
208
 
158
209
  A tool with `requiresApproval` (`true`, or a function of the arguments) pauses the run before that turn executes:
@@ -175,6 +226,8 @@ await fetch('/agents/support/resume', {
175
226
 
176
227
  The body is `{ threadId, decisions?, defaultDecision? }`, where a decision is `{ approved: boolean, reason?: string }`; malformed bodies get `400 INVALID_INPUT`. The response has the same shape as `/run` and may pause again for calls left without a decision. The run resumes as the authenticated `userId`: a thread with no paused run answers `409 RUN_NOT_PAUSED`, and another user's paused run `403 THREAD_ACCESS_DENIED`. Over WebSocket, send `resume` with `{ name, threadId, decisions?, defaultDecision? }`; a paused run's `complete` event carries `status` and `pendingApprovals`.
177
228
 
229
+ `POST /agents/:name/resume/stream` takes the same body and streams the rest of the run like `/stream`. `start` and `finish` name the resumed thread, and a body, agent or thread refused before the stream opens is answered as JSON, just like `/resume`. A thread with no paused run ends the open stream with an `error` event, `code: 'RUN_NOT_PAUSED'`, and disconnecting aborts the resume.
230
+
178
231
  ## Multi-Runtime
179
232
 
180
233
  ```typescript
@@ -265,11 +318,11 @@ On Bun use `upgradeWebSocket` from `hono/bun`, on Deno from `hono/deno`, on Clou
265
318
  | `stop` | Cancels the current run |
266
319
  | `ping` | Answered with `pong` (echoes `id`) |
267
320
 
268
- | Server sends | Description |
269
- | ------------ | ------------------------------------------------------------------------------------------------------ |
270
- | `event` | `token`, `reasoning`, `tool-call`, `tool-result`, `complete` (with the serialized result), `cancelled` |
271
- | `error` | Invalid message, unknown resource, run already in progress, or a masked run failure |
272
- | `pong` | Heartbeat reply |
321
+ | Server sends | Description |
322
+ | ------------ | ----------------------------------------------------------------------------------------------------------------------- |
323
+ | `event` | `token`, `reasoning`, `tool-call`, `tool-result`, `complete` (with the serialized result), `cancelled` |
324
+ | `error` | Invalid message, unknown resource, a thread `authorizeThread` refused, run already in progress, or a masked run failure |
325
+ | `pong` | Heartbeat reply |
273
326
 
274
327
  Messages larger than `maxPayloadSize` are rejected and the socket is closed with code `1009`. Closing the socket aborts the active run. `handleWebSocketMessage` and `createClientState` are exported for custom WebSocket integrations.
275
328
 
@@ -287,6 +340,7 @@ import {
287
340
  createWebSocketRoutes,
288
341
  createContextMiddleware,
289
342
  createAuthMiddleware,
343
+ createAnonymousMiddleware,
290
344
  createBodyLimitMiddleware,
291
345
  errorHandler,
292
346
  } from '@cogitator-ai/hono';
package/dist/app.d.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  import { Hono } from 'hono';
2
2
  import type { HonoEnv, CogitatorAppOptions } from './types.js';
3
+ /**
4
+ * The Cogitator API as a Hono app.
5
+ *
6
+ * @throws Error when `opts.limits` holds a limit that is not a positive integer
7
+ */
3
8
  export declare function cogitatorApp(opts: CogitatorAppOptions): Hono<HonoEnv>;
4
9
  //# sourceMappingURL=app.d.ts.map
package/dist/app.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAgB/D,wBAAgB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,IAAI,CAAC,OAAO,CAAC,CA+BrE"}
1
+ {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAiB/D;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,mBAAmB,GAAG,IAAI,CAAC,OAAO,CAAC,CAuCrE"}
package/dist/app.js CHANGED
@@ -1,11 +1,17 @@
1
1
  import { Hono } from 'hono';
2
2
  import { createContextMiddleware } from './middleware/context.js';
3
- import { createAuthMiddleware } from './middleware/auth.js';
3
+ import { createAnonymousMiddleware, createAuthMiddleware } from './middleware/auth.js';
4
4
  import { createBodyLimitMiddleware } from './middleware/body-limit.js';
5
5
  import { errorHandler } from './middleware/error-handler.js';
6
6
  import { createHealthRoutes, createAgentRoutes, createThreadRoutes, createToolRoutes, createWorkflowRoutes, createSwarmRoutes, } from './routes/index.js';
7
7
  import { createSwaggerRoutes } from './swagger/index.js';
8
8
  import { createWebSocketRoutes } from './websocket/handler.js';
9
+ import { notFound } from './utils/request.js';
10
+ /**
11
+ * The Cogitator API as a Hono app.
12
+ *
13
+ * @throws Error when `opts.limits` holds a limit that is not a positive integer
14
+ */
9
15
  export function cogitatorApp(opts) {
10
16
  const app = new Hono();
11
17
  app.use('*', createContextMiddleware(opts));
@@ -14,17 +20,23 @@ export function cogitatorApp(opts) {
14
20
  app.use('*', createAuthMiddleware(opts.auth));
15
21
  }
16
22
  app.route('/', createHealthRoutes());
23
+ if (opts.enableSwagger) {
24
+ app.route('/', createSwaggerRoutes({
25
+ ...opts.swagger,
26
+ auth: opts.swagger?.auth ?? Boolean(opts.auth),
27
+ ...(opts.limits && { limits: opts.limits }),
28
+ }));
29
+ }
30
+ app.use('*', createAnonymousMiddleware(opts.anonymous));
17
31
  app.route('/', createAgentRoutes());
18
32
  app.route('/', createThreadRoutes());
19
33
  app.route('/', createToolRoutes());
20
34
  app.route('/', createWorkflowRoutes());
21
35
  app.route('/', createSwarmRoutes());
22
- if (opts.enableSwagger) {
23
- app.route('/', createSwaggerRoutes({ ...opts.swagger, auth: opts.swagger?.auth ?? Boolean(opts.auth) }));
24
- }
25
36
  if (opts.enableWebSocket) {
26
37
  app.route('/', createWebSocketRoutes(opts.websocket));
27
38
  }
39
+ app.notFound((c) => notFound(c, 'Not found'));
28
40
  app.onError(errorHandler);
29
41
  return app;
30
42
  }
package/dist/app.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAE5B,OAAO,EAAE,uBAAuB,EAAE,MAAM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AAI,CAAC,SAAS,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC;IAC9C,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IAE1B,OAAO,GAAG,CAAC;AACb,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  export { cogitatorApp } from './app.js';
2
- export type { CogitatorAppOptions, CogitatorContext, HonoEnv, AuthContext, AuthFunction, WebSocketConfig, AgentListResponse, AgentRunRequest, AgentRunResponse, AgentResumeRequest, PendingApproval, ThreadResponse, AddMessageRequest, ToolListResponse, WorkflowListResponse, WorkflowRunRequest, WorkflowRunResponse, WorkflowStatusResponse, SwarmListResponse, SwarmRunRequest, SwarmRunResponse, BlackboardResponse, HealthResponse, ReadinessResponse, ReadinessCheckStatus, ErrorResponse, SwaggerConfig, WebSocketMessage, WebSocketResponse, WebSocketRunPayload, WebSocketResumePayload, WebSocketClientState, WebSocketLike, } from './types.js';
2
+ export type { CogitatorAppOptions, CogitatorContext, HonoEnv, AuthContext, AuthFunction, DepsFunction, RunLimits, AnonymousPolicy, AuthorizeThread, WebSocketConfig, AgentListResponse, AgentRunRequest, AgentRunResponse, AgentResumeRequest, PendingApproval, ThreadResponse, AddMessageRequest, ToolListResponse, WorkflowListResponse, WorkflowRunRequest, WorkflowRunResponse, WorkflowStatusResponse, SwarmListResponse, SwarmRunRequest, SwarmRunResponse, BlackboardResponse, HealthResponse, ReadinessResponse, ReadinessCheckStatus, ErrorResponse, SwaggerConfig, WebSocketMessage, WebSocketResponse, WebSocketRunPayload, WebSocketResumePayload, WebSocketClientState, WebSocketLike, } from './types.js';
3
3
  export { HonoStreamWriter } from './streaming/hono-stream-writer.js';
4
4
  export type { HonoStreamWriterOptions } from './streaming/hono-stream-writer.js';
5
5
  export { createContextMiddleware } from './middleware/context.js';
6
- export { createAuthMiddleware } from './middleware/auth.js';
6
+ export { createAnonymousMiddleware, createAuthMiddleware } from './middleware/auth.js';
7
7
  export { errorHandler } from './middleware/error-handler.js';
8
8
  export { createBodyLimitMiddleware } from './middleware/body-limit.js';
9
9
  export { createHealthRoutes, createAgentRoutes, createThreadRoutes, createToolRoutes, createWorkflowRoutes, createSwarmRoutes, } from './routes/index.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,YAAY,EACV,mBAAmB,EACnB,gBAAgB,EAChB,OAAO,EACP,WAAW,EACX,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,EACtB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,sBAAsB,EACtB,oBAAoB,EACpB,aAAa,GACd,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,YAAY,EAAE,uBAAuB,EAAE,MAAM,mCAAmC,CAAC;AAEjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAEvE,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,wBAAwB,CAAC;AAEhC,YAAY,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAEtE,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,kBAAkB,EAClB,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,2BAA2B,EAC3B,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,GACX,MAAM,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,YAAY,EACV,mBAAmB,EACnB,gBAAgB,EAChB,OAAO,EACP,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,eAAe,EACf,eAAe,EACf,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,iBAAiB,EACjB,gBAAgB,EAChB,oBAAoB,EACpB,kBAAkB,EAClB,mBAAmB,EACnB,sBAAsB,EACtB,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,sBAAsB,EACtB,oBAAoB,EACpB,aAAa,GACd,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,YAAY,EAAE,uBAAuB,EAAE,MAAM,mCAAmC,CAAC;AAEjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACvF,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAEvE,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,wBAAwB,CAAC;AAEhC,YAAY,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAEtE,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,kBAAkB,EAClB,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,2BAA2B,EAC3B,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,GACX,MAAM,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  export { cogitatorApp } from './app.js';
2
2
  export { HonoStreamWriter } from './streaming/hono-stream-writer.js';
3
3
  export { createContextMiddleware } from './middleware/context.js';
4
- export { createAuthMiddleware } from './middleware/auth.js';
4
+ export { createAnonymousMiddleware, createAuthMiddleware } from './middleware/auth.js';
5
5
  export { errorHandler } from './middleware/error-handler.js';
6
6
  export { createBodyLimitMiddleware } from './middleware/body-limit.js';
7
7
  export { createHealthRoutes, createAgentRoutes, createThreadRoutes, createToolRoutes, createWorkflowRoutes, createSwarmRoutes, } from './routes/index.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAsCxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAGrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAEvE,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,wBAAwB,CAAC;AAIhC,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,kBAAkB,EAClB,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,2BAA2B,EAC3B,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,GACX,MAAM,6BAA6B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AA0CxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAGrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACvF,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,4BAA4B,CAAC;AAEvE,OAAO,EACL,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,GAClB,MAAM,wBAAwB,CAAC;AAIhC,OAAO,EACL,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EACpB,kBAAkB,EAClB,yBAAyB,EACzB,yBAAyB,EACzB,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,2BAA2B,EAC3B,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,UAAU,GACX,MAAM,6BAA6B,CAAC"}
@@ -1,3 +1,9 @@
1
+ import { type AnonymousPolicy } from '@cogitator-ai/server-shared';
1
2
  import type { HonoEnv, AuthFunction } from '../types.js';
2
3
  export declare function createAuthMiddleware(authFn: AuthFunction): import("hono").MiddlewareHandler<HonoEnv, string, {}, Response>;
4
+ /**
5
+ * Turns away callers without a `userId` when `anonymous` is `'deny'`, with
6
+ * `401 UNAUTHORIZED`. Routes registered before it, such as health and docs, stay open.
7
+ */
8
+ export declare function createAnonymousMiddleware(anonymous: AnonymousPolicy | undefined): import("hono").MiddlewareHandler<HonoEnv, string, {}, Response>;
3
9
  //# sourceMappingURL=auth.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEzD,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,mEAUxD"}
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AACA,OAAO,EAA6B,KAAK,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9F,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGzD,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,mEAUxD;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,SAAS,EAAE,eAAe,GAAG,SAAS,mEAM/E"}
@@ -1,4 +1,6 @@
1
1
  import { createMiddleware } from 'hono/factory';
2
+ import { UNAUTHORIZED, admitCaller } from '@cogitator-ai/server-shared';
3
+ import { sendError } from '../utils/request.js';
2
4
  export function createAuthMiddleware(authFn) {
3
5
  return createMiddleware(async (c, next) => {
4
6
  try {
@@ -6,9 +8,21 @@ export function createAuthMiddleware(authFn) {
6
8
  c.set('cogitatorAuth', auth);
7
9
  }
8
10
  catch {
9
- return c.json({ error: { message: 'Unauthorized', code: 'UNAUTHORIZED' } }, 401);
11
+ return sendError(c, { ...UNAUTHORIZED, message: 'Unauthorized' });
10
12
  }
11
13
  await next();
12
14
  });
13
15
  }
16
+ /**
17
+ * Turns away callers without a `userId` when `anonymous` is `'deny'`, with
18
+ * `401 UNAUTHORIZED`. Routes registered before it, such as health and docs, stay open.
19
+ */
20
+ export function createAnonymousMiddleware(anonymous) {
21
+ return createMiddleware(async (c, next) => {
22
+ const refusal = admitCaller(c.get('cogitatorAuth')?.userId, anonymous);
23
+ if (refusal)
24
+ return sendError(c, refusal);
25
+ await next();
26
+ });
27
+ }
14
28
  //# sourceMappingURL=auth.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAGhD,MAAM,UAAU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AAgB,CAAU,KAAK,EAAE,CAAC,EAAE,IAAI,EAA4B,EAAE;QAC3E,MAAM,OAAO,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,eAAe,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,CAAC;QACvE,IAAI,OAAO;YAAE,OAAO,SAAS,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;QAC1C,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"body-limit.d.ts","sourceRoot":"","sources":["../../src/middleware/body-limit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAC9C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,eAAO,MAAM,kBAAkB,QAAc,CAAC;AAE9C,wBAAgB,yBAAyB,CACvC,OAAO,GAAE,MAA2B,GACnC,iBAAiB,CAAC,OAAO,CAAC,CAM5B"}
1
+ {"version":3,"file":"body-limit.d.ts","sourceRoot":"","sources":["../../src/middleware/body-limit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAE9C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,eAAO,MAAM,kBAAkB,QAAc,CAAC;AAE9C,wBAAgB,yBAAyB,CACvC,OAAO,GAAE,MAA2B,GACnC,iBAAiB,CAAC,OAAO,CAAC,CAK5B"}
@@ -1,9 +1,11 @@
1
1
  import { bodyLimit } from 'hono/body-limit';
2
+ import { PAYLOAD_TOO_LARGE } from '@cogitator-ai/server-shared';
3
+ import { sendError } from '../utils/request.js';
2
4
  export const DEFAULT_BODY_LIMIT = 1024 * 1024;
3
5
  export function createBodyLimitMiddleware(maxSize = DEFAULT_BODY_LIMIT) {
4
6
  return bodyLimit({
5
7
  maxSize,
6
- onError: (c) => c.json({ error: { message: 'Payload too large', code: 'PAYLOAD_TOO_LARGE' } }, 413),
8
+ onError: (c) => sendError(c, PAYLOAD_TOO_LARGE),
7
9
  });
8
10
  }
9
11
  //# sourceMappingURL=body-limit.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"body-limit.js","sourceRoot":"","sources":["../../src/middleware/body-limit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,GAAG,IAAI,CAAC;AAE9C,MAAM,UAAU,yBAAyB,CACvC,UAAkB,kBAAkB;IAEpC,OAAO,SAAS,CAAC;QACf,OAAO;QACP,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CACb,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,mBAAmB,EAAE,IAAI,EAAE,mBAAmB,EAAE,EAAE,EAAE,GAAG,CAAC;KACtF,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"file":"body-limit.js","sourceRoot":"","sources":["../../src/middleware/body-limit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAEhE,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,GAAG,IAAI,CAAC;AAE9C,MAAM,UAAU,yBAAyB,CACvC,UAAkB,kBAAkB;IAEpC,OAAO,SAAS,CAAC;QACf,OAAO;QACP,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,iBAAiB,CAAC;KAChD,CAAC,CAAC;AACL,CAAC"}
@@ -1,3 +1,8 @@
1
1
  import type { HonoEnv, CogitatorAppOptions } from '../types.js';
2
+ /**
3
+ * Sets the Cogitator context of each request.
4
+ *
5
+ * @throws Error when `opts.limits` holds a limit that is not a positive integer
6
+ */
2
7
  export declare function createContextMiddleware(opts: CogitatorAppOptions): import("hono").MiddlewareHandler<HonoEnv, string, {}, Response>;
3
8
  //# sourceMappingURL=context.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/middleware/context.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEhE,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,mBAAmB,mEAiBhE"}
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/middleware/context.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEhE;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,mBAAmB,mEAqBhE"}
@@ -1,7 +1,13 @@
1
1
  import { createMiddleware } from 'hono/factory';
2
- import { DEFAULT_THREAD_MESSAGE_ROLES, generateId, resolveSseHeartbeatMs, } from '@cogitator-ai/server-shared';
2
+ import { DEFAULT_THREAD_MESSAGE_ROLES, checkRunLimits, generateId, resolveSseHeartbeatMs, } from '@cogitator-ai/server-shared';
3
+ /**
4
+ * Sets the Cogitator context of each request.
5
+ *
6
+ * @throws Error when `opts.limits` holds a limit that is not a positive integer
7
+ */
3
8
  export function createContextMiddleware(opts) {
4
9
  const sseHeartbeatMs = resolveSseHeartbeatMs(opts.sseHeartbeatMs);
10
+ const limits = checkRunLimits(opts.limits);
5
11
  return createMiddleware(async (c, next) => {
6
12
  c.set('cogitator', {
7
13
  runtime: opts.cogitator,
@@ -11,6 +17,9 @@ export function createContextMiddleware(opts) {
11
17
  sseHeartbeatMs,
12
18
  acceptContext: opts.acceptContext ?? false,
13
19
  threadMessageRoles: opts.threadMessageRoles ?? DEFAULT_THREAD_MESSAGE_ROLES,
20
+ limits,
21
+ authorizeThread: opts.authorizeThread,
22
+ deps: opts.deps,
14
23
  });
15
24
  c.set('cogitatorRequestId', generateId('req'));
16
25
  c.set('cogitatorStartTime', Date.now());
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/middleware/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EACL,4BAA4B,EAC5B,UAAU,EACV,qBAAqB,GACtB,MAAM,6BAA6B,CAAC;AAGrC,MAAM,UAAU,uBAAuB,CAAC,IAAyB;IAC/D,MAAM,cAAc,GAAG,qBAAqB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IAClE,OAAO,gBAAgB,CAAU,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE;QACjD,CAAC,CAAC,GAAG,CAAC,WAAW,EAAE;YACjB,OAAO,EAAE,IAAI,CAAC,SAAS;YACvB,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,EAAE;YACzB,SAAS,EAAE,IAAI,CAAC,SAAS,IAAI,EAAE;YAC/B,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,EAAE;YACzB,cAAc;YACd,aAAa,EAAE,IAAI,CAAC,aAAa,IAAI,KAAK;YAC1C,kBAAkB,EAAE,IAAI,CAAC,kBAAkB,IAAI,4BAA4B;SAC5E,CAAC,CAAC;QACH,CAAC,CAAC,GAAG,CAAC,oBAAoB,EAAE,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/C,CAAC,CAAC,GAAG,CAAC,oBAAoB,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;QAExC,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../src/middleware/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EACL,4BAA4B,EAC5B,cAAc,EACd,UAAU,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"}
1
+ {"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../../src/middleware/error-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAC;AAEzC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAI3C,eAAO,MAAM,YAAY,EAAE,YAAY,CAAC,OAAO,CAM9C,CAAC"}
@@ -1,14 +1,10 @@
1
1
  import { HTTPException } from 'hono/http-exception';
2
- import { CogitatorError, ErrorCode } from '@cogitator-ai/types';
3
- import { cogitatorErrorStatus } from '../utils/errors.js';
2
+ import { describeError } from '../utils/errors.js';
3
+ import { sendError } from '../utils/request.js';
4
4
  export const errorHandler = (err, c) => {
5
5
  if (err instanceof HTTPException) {
6
6
  return err.getResponse();
7
7
  }
8
- if (CogitatorError.isCogitatorError(err)) {
9
- return c.json({ error: { message: err.message, code: err.code } }, cogitatorErrorStatus(err));
10
- }
11
- console.error('[CogitatorHono] Unhandled error:', err);
12
- return c.json({ error: { message: 'Internal server error', code: ErrorCode.INTERNAL_ERROR } }, 500);
8
+ return sendError(c, describeError(err, 'Unhandled error'));
13
9
  };
14
10
  //# sourceMappingURL=error-handler.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../src/middleware/error-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhE,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAE1D,MAAM,CAAC,MAAM,YAAY,GAA0B,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE;IAC5D,IAAI,GAAG,YAAY,aAAa,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,WAAW,EAAE,CAAC;IAC3B,CAAC;IAED,IAAI,cAAc,CAAC,gBAAgB,CAAC,GAAG,CAAC,EAAE,CAAC;QACzC,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,EAAE,EAAE,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC;IAChG,CAAC;IAED,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,GAAG,CAAC,CAAC;IAEvD,OAAO,CAAC,CAAC,IAAI,CACX,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,uBAAuB,EAAE,IAAI,EAAE,SAAS,CAAC,cAAc,EAAE,EAAE,EAC/E,GAAG,CACJ,CAAC;AACJ,CAAC,CAAC"}
1
+ {"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../src/middleware/error-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAEhD,MAAM,CAAC,MAAM,YAAY,GAA0B,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE;IAC5D,IAAI,GAAG,YAAY,aAAa,EAAE,CAAC;QACjC,OAAO,GAAG,CAAC,WAAW,EAAE,CAAC;IAC3B,CAAC;IAED,OAAO,SAAS,CAAC,CAAC,EAAE,aAAa,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC;AAC7D,CAAC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"agents.d.ts","sourceRoot":"","sources":["../../src/routes/agents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAS5B,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,aAAa,CAAC;AAa9D,wBAAgB,iBAAiB,IAAI,IAAI,CAAC,OAAO,CAAC,CAoIjD"}
1
+ {"version":3,"file":"agents.d.ts","sourceRoot":"","sources":["../../src/routes/agents.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAW5B,OAAO,KAAK,EAAE,OAAO,EAAqB,MAAM,aAAa,CAAC;AAgD9D,wBAAgB,iBAAiB,IAAI,IAAI,CAAC,OAAO,CAAC,CAqJjD"}