optio-api 0.1.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 (79) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +244 -0
  3. package/dist/_generated/optio-engine.d.ts +69 -0
  4. package/dist/_generated/optio-engine.d.ts.map +1 -0
  5. package/dist/_generated/optio-engine.js +31 -0
  6. package/dist/_generated/optio-engine.js.map +1 -0
  7. package/dist/adapters/express.d.ts +24 -0
  8. package/dist/adapters/express.d.ts.map +1 -0
  9. package/dist/adapters/express.js +186 -0
  10. package/dist/adapters/express.js.map +1 -0
  11. package/dist/adapters/fastify.d.ts +38 -0
  12. package/dist/adapters/fastify.d.ts.map +1 -0
  13. package/dist/adapters/fastify.js +473 -0
  14. package/dist/adapters/fastify.js.map +1 -0
  15. package/dist/adapters/nextjs-app.d.ts +29 -0
  16. package/dist/adapters/nextjs-app.d.ts.map +1 -0
  17. package/dist/adapters/nextjs-app.js +220 -0
  18. package/dist/adapters/nextjs-app.js.map +1 -0
  19. package/dist/adapters/nextjs-pages.d.ts +29 -0
  20. package/dist/adapters/nextjs-pages.d.ts.map +1 -0
  21. package/dist/adapters/nextjs-pages.js +189 -0
  22. package/dist/adapters/nextjs-pages.js.map +1 -0
  23. package/dist/auth.d.ts +10 -0
  24. package/dist/auth.d.ts.map +1 -0
  25. package/dist/auth.js +9 -0
  26. package/dist/auth.js.map +1 -0
  27. package/dist/context.d.ts +14 -0
  28. package/dist/context.d.ts.map +1 -0
  29. package/dist/context.js +13 -0
  30. package/dist/context.js.map +1 -0
  31. package/dist/discovery.d.ts +10 -0
  32. package/dist/discovery.d.ts.map +1 -0
  33. package/dist/discovery.js +48 -0
  34. package/dist/discovery.js.map +1 -0
  35. package/dist/handlers.d.ts +110 -0
  36. package/dist/handlers.d.ts.map +1 -0
  37. package/dist/handlers.js +188 -0
  38. package/dist/handlers.js.map +1 -0
  39. package/dist/index.d.ts +10 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +22 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/metadata-filter-query.d.ts +13 -0
  44. package/dist/metadata-filter-query.d.ts.map +1 -0
  45. package/dist/metadata-filter-query.js +36 -0
  46. package/dist/metadata-filter-query.js.map +1 -0
  47. package/dist/optio-transports.d.ts +8 -0
  48. package/dist/optio-transports.d.ts.map +1 -0
  49. package/dist/optio-transports.js +37 -0
  50. package/dist/optio-transports.js.map +1 -0
  51. package/dist/process-id-resolver.d.ts +26 -0
  52. package/dist/process-id-resolver.d.ts.map +1 -0
  53. package/dist/process-id-resolver.js +31 -0
  54. package/dist/process-id-resolver.js.map +1 -0
  55. package/dist/resolve.d.ts +25 -0
  56. package/dist/resolve.d.ts.map +1 -0
  57. package/dist/resolve.js +16 -0
  58. package/dist/resolve.js.map +1 -0
  59. package/dist/sse-options.d.ts +28 -0
  60. package/dist/sse-options.d.ts.map +1 -0
  61. package/dist/sse-options.js +82 -0
  62. package/dist/sse-options.js.map +1 -0
  63. package/dist/stream-poller.d.ts +21 -0
  64. package/dist/stream-poller.d.ts.map +1 -0
  65. package/dist/stream-poller.js +146 -0
  66. package/dist/stream-poller.js.map +1 -0
  67. package/dist/types.d.ts +2 -0
  68. package/dist/types.d.ts.map +1 -0
  69. package/dist/types.js +2 -0
  70. package/dist/types.js.map +1 -0
  71. package/dist/widget-proxy-core.d.ts +7 -0
  72. package/dist/widget-proxy-core.d.ts.map +1 -0
  73. package/dist/widget-proxy-core.js +56 -0
  74. package/dist/widget-proxy-core.js.map +1 -0
  75. package/dist/widget-upstream-registry.d.ts +27 -0
  76. package/dist/widget-upstream-registry.d.ts.map +1 -0
  77. package/dist/widget-upstream-registry.js +28 -0
  78. package/dist/widget-upstream-registry.js.map +1 -0
  79. package/package.json +100 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to the Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by the Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding any notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. Please also get an
186
+ appropriate open source header file from
187
+ http://www.apache.org/legal/apply-license.html
188
+
189
+ Copyright 2026 Optio contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,244 @@
1
+ # optio-api
2
+
3
+ REST API handlers and SSE streams for Optio process management.
4
+ Framework-agnostic handlers with ready-to-use adapters for Fastify, Express, and Next.js.
5
+
6
+ ## Install
7
+
8
+ ```bash
9
+ npm install optio-api optio-contracts
10
+ ```
11
+
12
+ Then install the adapter dependencies for your framework:
13
+
14
+ ```bash
15
+ # Fastify
16
+ npm install fastify @ts-rest/fastify
17
+
18
+ # Express
19
+ npm install express @ts-rest/express
20
+
21
+ # Next.js (Pages Router)
22
+ npm install next @ts-rest/next
23
+
24
+ # Next.js (App Router)
25
+ npm install next @ts-rest/serverless
26
+ ```
27
+
28
+ ## Entry Points
29
+
30
+ - `optio-api` — framework-agnostic handlers and stream pollers
31
+ - `optio-api/fastify` — Fastify adapter
32
+ - `optio-api/express` — Express adapter
33
+ - `optio-api/nextjs/pages` — Next.js Pages Router adapter
34
+ - `optio-api/nextjs/app` — Next.js App Router adapter
35
+
36
+ ## Internal structure
37
+
38
+ The package has three layers:
39
+
40
+ 1. **Adapter layer** (`src/adapters/`): one file per supported web framework
41
+ (`fastify`, `express`, `nextjs-app`, `nextjs-pages`). Owns only framework
42
+ integration — route registration, request/response wrangling, lifecycle
43
+ hooks. Framework-agnostic code is forbidden here; see `AGENTS.md` for the
44
+ binding rules.
45
+ 2. **Handler layer** (`src/handlers.ts` and collaborators): framework-agnostic
46
+ functions taking `OptioContext` + per-request data. Owns read-path Mongo
47
+ queries, write-path RPC calls, request → response shaping.
48
+ 3. **Context layer** (`src/context.ts`): owns durable per-app resources
49
+ (`dbOpts`, `transports` cache, `redis`, `closeAll`). Constructed once at
50
+ adapter registration via `createOptioContext`. The `transports` cache
51
+ (Layer 1, in `src/optio-transports.ts`) is contract-agnostic — wraps
52
+ `RpcClient` per `(database, prefix)` — and is exposed to RPC-only
53
+ consumers via `createOptioTransports`.
54
+
55
+ When extending the package, the test for placing code in an adapter is:
56
+ *"Would I write this same code in the other three adapters?"* If yes, the
57
+ code belongs in the handler or context layer, not the adapter.
58
+
59
+ ## Quick Setup
60
+
61
+ ### Fastify
62
+
63
+ ```typescript
64
+ import Fastify from 'fastify';
65
+ import { MongoClient } from 'mongodb';
66
+ import { Redis } from 'ioredis';
67
+ import { registerOptioApi } from 'optio-api/fastify';
68
+
69
+ const app = Fastify();
70
+ const db = (await new MongoClient(process.env.MONGO_URL!).connect()).db();
71
+ const redis = new Redis(process.env.REDIS_URL!);
72
+
73
+ registerOptioApi(app, { db, redis });
74
+
75
+ await app.listen({ port: 3000 });
76
+ ```
77
+
78
+ ### Express
79
+
80
+ ```typescript
81
+ import express from 'express';
82
+ import { MongoClient } from 'mongodb';
83
+ import { Redis } from 'ioredis';
84
+ import { registerOptioApi } from 'optio-api/express';
85
+
86
+ const app = express();
87
+ app.use(express.json());
88
+ const db = (await new MongoClient(process.env.MONGO_URL!).connect()).db();
89
+ const redis = new Redis(process.env.REDIS_URL!);
90
+
91
+ registerOptioApi(app, { db, redis });
92
+
93
+ app.listen(3000);
94
+ ```
95
+
96
+ ### Next.js Pages Router
97
+
98
+ ```typescript
99
+ // pages/api/processes/[...optio].ts
100
+ import { MongoClient } from 'mongodb';
101
+ import { Redis } from 'ioredis';
102
+ import { createOptioHandler } from 'optio-api/nextjs/pages';
103
+
104
+ const db = (await new MongoClient(process.env.MONGO_URL!).connect()).db();
105
+ const redis = new Redis(process.env.REDIS_URL!);
106
+
107
+ export default createOptioHandler({ db, redis });
108
+ ```
109
+
110
+ ### Next.js App Router
111
+
112
+ ```typescript
113
+ // app/api/processes/[...optio]/route.ts
114
+ import { MongoClient } from 'mongodb';
115
+ import { Redis } from 'ioredis';
116
+ import { createOptioRouteHandlers } from 'optio-api/nextjs/app';
117
+
118
+ const db = (await new MongoClient(process.env.MONGO_URL!).connect()).db();
119
+ const redis = new Redis(process.env.REDIS_URL!);
120
+
121
+ export const { GET, POST } = createOptioRouteHandlers({ db, redis });
122
+ ```
123
+
124
+ ## REST Endpoints
125
+
126
+ All adapters mount the same endpoints under `/api/processes/:prefix/...`:
127
+
128
+ | Method | Path | Description |
129
+ |--------|------|-------------|
130
+ | GET | `/api/processes/:prefix` | List processes (cursor pagination, filtering) |
131
+ | GET | `/api/processes/:prefix/:id` | Get single process |
132
+ | GET | `/api/processes/:prefix/:id/tree` | Get process subtree |
133
+ | GET | `/api/processes/:prefix/:id/log` | Get process log |
134
+ | GET | `/api/processes/:prefix/:id/tree/log` | Get merged subtree log |
135
+ | POST | `/api/processes/:prefix/:id/launch` | Forward launch to engine. 200 on success; 404/409 with `{reason, message}` per `LaunchFailureReason`. |
136
+ | POST | `/api/processes/:prefix/:id/cancel` | Forward cancel to engine. 200 on success; 404/409 per `CancelFailureReason`. |
137
+ | POST | `/api/processes/:prefix/:id/dismiss` | Forward dismiss to engine. 200 on success; 404/409 per `DismissFailureReason`. |
138
+ | POST | `/api/processes/:prefix/resync` | 202 Accepted; engine handles resync asynchronously. |
139
+
140
+ Command endpoints do not validate state in this package. The engine owns all command-acceptance rules; the API translates the engine's discriminated-union result into HTTP status + body. See the architectural rule at the top of `AGENTS.md`.
141
+
142
+ ### List Query Parameters
143
+
144
+ The list endpoint (`GET /api/processes/:prefix`) accepts an optional `metadataFilter` query
145
+ parameter for metadata-based filtering. Pass it as a URL-encoded JSON string, e.g.
146
+ `?metadataFilter=%7B%22project%22%3A%22x%22%7D`.
147
+
148
+ ## Breaking Changes
149
+
150
+ ### Metadata filter query parameter
151
+
152
+ The legacy `?metadata.<key>=<value>` query param style has been replaced by
153
+ `?metadataFilter=<URL-encoded JSON>`. Requests using the legacy form return 400
154
+ with an explicit migration message.
155
+
156
+ ## SSE Streams
157
+
158
+ - `GET /api/processes/:prefix/stream` — live flat process list, polls every 1 s. Accepts the same optional `?metadataFilter=<URL-encoded JSON>` query param as the REST list endpoint; the legacy `?metadata.<key>=<value>` form returns 400.
159
+ - `GET /api/processes/:prefix/:id/tree/stream` — live process tree with log deltas, polls every 1 s
160
+
161
+ ## Building Custom Adapters
162
+
163
+ Import handler functions, stream pollers, and the context factory directly
164
+ from `optio-api`:
165
+
166
+ ```typescript
167
+ import {
168
+ createOptioContext, type OptioContext,
169
+ listProcesses, getProcess, getProcessTree,
170
+ getProcessLog, getProcessTreeLog,
171
+ launchProcess, cancelProcess, dismissProcess, resyncProcesses,
172
+ createListPoller, createTreePoller,
173
+ type ListQuery, type PaginationQuery, type TreeLogQuery,
174
+ type LaunchCommandResult, type CancelCommandResult, type DismissCommandResult,
175
+ type StreamPollerOptions, type TreePollerOptions, type ListPollerHandle,
176
+ } from 'optio-api';
177
+ ```
178
+
179
+ Handler functions take an `OptioContext` (constructed once via
180
+ `createOptioContext({ dbOpts, redis })`) as the first argument, a per-request
181
+ `query` object as the second, and an optional `id` / additional parameters as
182
+ later positional args. Command handlers (`launchProcess`, `cancelProcess`,
183
+ `dismissProcess`) return per-command result unions
184
+ (`LaunchCommandResult` / `CancelCommandResult` / `DismissCommandResult`)
185
+ whose 404/409 bodies are `{ reason, message }` — map these to HTTP responses
186
+ directly.
187
+
188
+ Stream pollers expose a `{ start(), stop() }` handle; call `start()` when the
189
+ client connects and `stop()` when they disconnect.
190
+
191
+ ## Return value
192
+
193
+ `registerOptioApi`, `createOptioRouteHandlers`, and `createOptioHandler`
194
+ accept two forms:
195
+
196
+ - **Sugar form** (`{ db | mongoClient, redis, authenticate, ... }`): the adapter constructs an `OptioContext` internally. Return shape:
197
+ - fastify/express: returns the constructed `OptioContext`.
198
+ - nextjs-app: returns `{ GET, POST, ctx }`.
199
+ - nextjs-pages: returns `{ handler, ctx }`.
200
+
201
+ Fastify additionally wires its `onClose` lifecycle hook to `ctx.closeAll()` automatically.
202
+
203
+ - **Explicit form** (`{ ctx, authenticate, ... }`): caller supplies a pre-built `OptioContext` and owns its lifecycle. Return shape:
204
+ - fastify/express: returns `void`.
205
+ - nextjs-app: returns `{ GET, POST }` (no `ctx`).
206
+ - nextjs-pages: returns `{ handler }`.
207
+
208
+ Construct the context explicitly via `createOptioContext({ dbOpts, redis })` and call `ctx.closeAll()` on shutdown:
209
+
210
+ ```typescript
211
+ // Express:
212
+ import { registerOptioApi } from 'optio-api/express';
213
+
214
+ const ctx = registerOptioApi(app, { db, redis, authenticate })!; // sugar form returns ctx
215
+ const server = app.listen(3000);
216
+ process.on('SIGTERM', async () => {
217
+ server.close();
218
+ await ctx.closeAll();
219
+ });
220
+ ```
221
+
222
+ Next.js: same pattern — sugar form returns `{ GET, POST, ctx }` (app router) or `{ handler, ctx }` (pages router). Wire `ctx.closeAll()` into whatever shutdown hook your deployment provides (typically not needed for serverless, since process death drops the Redis socket).
223
+
224
+ Programmatic engine access for the host's own non-HTTP code:
225
+
226
+ ```typescript
227
+ import { resolveOptioEngine } from 'optio-api';
228
+ const engine = resolveOptioEngine(ctx, {});
229
+ await engine.launch({ processId: 'my-task' });
230
+ ```
231
+
232
+ RPC-only consumers (no Mongo, no HTTP — e.g., Excavator) use the Layer-1 transport cache directly:
233
+
234
+ ```typescript
235
+ import { createOptioTransports, OptioEngineClient } from 'optio-api';
236
+ const transports = createOptioTransports(redis);
237
+ const optioEngine = new OptioEngineClient(transports.get('mydb', 'optio'));
238
+ // Plus any other clamator contract:
239
+ const myDomain = new MyDomainClient(transports.get('mydb', 'mydomain'));
240
+ ```
241
+
242
+ ## See Also
243
+
244
+ - [Optio Overview](../../README.md)
@@ -0,0 +1,69 @@
1
+ import { optioEngineContract } from 'optio-contracts/optio-engine';
2
+ import type { ClamatorClient } from '@clamator/protocol';
3
+ import type { z } from 'zod';
4
+ export type BlockLaunchesParams = z.infer<typeof optioEngineContract.methods.blockLaunches.params>;
5
+ export type BlockLaunchesResult = z.infer<typeof optioEngineContract.methods.blockLaunches.result>;
6
+ export type CancelParams = z.infer<typeof optioEngineContract.methods.cancel.params>;
7
+ export type CancelResult = z.infer<typeof optioEngineContract.methods.cancel.result>;
8
+ export type DismissParams = z.infer<typeof optioEngineContract.methods.dismiss.params>;
9
+ export type DismissResult = z.infer<typeof optioEngineContract.methods.dismiss.result>;
10
+ export type GroupCancelParams = z.infer<typeof optioEngineContract.methods.groupCancel.params>;
11
+ export type GroupCancelResult = z.infer<typeof optioEngineContract.methods.groupCancel.result>;
12
+ export type GroupCancelAndWaitParams = z.infer<typeof optioEngineContract.methods.groupCancelAndWait.params>;
13
+ export type GroupCancelAndWaitResult = z.infer<typeof optioEngineContract.methods.groupCancelAndWait.result>;
14
+ export type LaunchParams = z.infer<typeof optioEngineContract.methods.launch.params>;
15
+ export type LaunchResult = z.infer<typeof optioEngineContract.methods.launch.result>;
16
+ export type ResyncParams = z.infer<typeof optioEngineContract.methods.resync.params>;
17
+ export type UnblockLaunchesParams = z.infer<typeof optioEngineContract.methods.unblockLaunches.params>;
18
+ export type UnblockLaunchesResult = z.infer<typeof optioEngineContract.methods.unblockLaunches.result>;
19
+ export declare class OptioEngineClient {
20
+ private client;
21
+ constructor(client: ClamatorClient);
22
+ blockLaunches(params: BlockLaunchesParams, opts?: {
23
+ timeoutMs?: number;
24
+ }): Promise<BlockLaunchesResult>;
25
+ cancel(params: CancelParams, opts?: {
26
+ timeoutMs?: number;
27
+ }): Promise<CancelResult>;
28
+ dismiss(params: DismissParams, opts?: {
29
+ timeoutMs?: number;
30
+ }): Promise<DismissResult>;
31
+ groupCancel(params: GroupCancelParams, opts?: {
32
+ timeoutMs?: number;
33
+ }): Promise<GroupCancelResult>;
34
+ groupCancelAndWait(params: GroupCancelAndWaitParams, opts?: {
35
+ timeoutMs?: number;
36
+ }): Promise<GroupCancelAndWaitResult>;
37
+ launch(params: LaunchParams, opts?: {
38
+ timeoutMs?: number;
39
+ }): Promise<LaunchResult>;
40
+ resync(params: ResyncParams): Promise<void>;
41
+ unblockLaunches(params: UnblockLaunchesParams, opts?: {
42
+ timeoutMs?: number;
43
+ }): Promise<UnblockLaunchesResult>;
44
+ }
45
+ export interface OptioEngineService {
46
+ blockLaunches(params: BlockLaunchesParams, opts?: {
47
+ timeoutMs?: number;
48
+ }): Promise<BlockLaunchesResult>;
49
+ cancel(params: CancelParams, opts?: {
50
+ timeoutMs?: number;
51
+ }): Promise<CancelResult>;
52
+ dismiss(params: DismissParams, opts?: {
53
+ timeoutMs?: number;
54
+ }): Promise<DismissResult>;
55
+ groupCancel(params: GroupCancelParams, opts?: {
56
+ timeoutMs?: number;
57
+ }): Promise<GroupCancelResult>;
58
+ groupCancelAndWait(params: GroupCancelAndWaitParams, opts?: {
59
+ timeoutMs?: number;
60
+ }): Promise<GroupCancelAndWaitResult>;
61
+ launch(params: LaunchParams, opts?: {
62
+ timeoutMs?: number;
63
+ }): Promise<LaunchResult>;
64
+ resync(params: ResyncParams): Promise<void>;
65
+ unblockLaunches(params: UnblockLaunchesParams, opts?: {
66
+ timeoutMs?: number;
67
+ }): Promise<UnblockLaunchesResult>;
68
+ }
69
+ //# sourceMappingURL=optio-engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optio-engine.d.ts","sourceRoot":"","sources":["../../src/_generated/optio-engine.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;AACnG,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;AACnG,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACrF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACrF,MAAM,MAAM,aAAa,GAAG,C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AE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC;IACxG,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACnF,OAAO,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACtF,WAAW,CAAC,MAAM,EAAE,iBAAiB,EAAE,IAAI,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAClG,kBAAkB,CAAC,MAAM,EAAE,wBAAwB,EAAE,IAAI,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;IACvH,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;IACnF,MAAM,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,eAAe,CAAC,MAAM,EAAE,qBAAqB,EAAE,IAAI,CAAC,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;CAC/G"}
@@ -0,0 +1,31 @@
1
+ export class OptioEngineClient {
2
+ client;
3
+ constructor(client) {
4
+ this.client = client;
5
+ }
6
+ blockLaunches(params, opts) {
7
+ return this.client.call('optio-engine', 'blockLaunches', params, opts);
8
+ }
9
+ cancel(params, opts) {
10
+ return this.client.call('optio-engine', 'cancel', params, opts);
11
+ }
12
+ dismiss(params, opts) {
13
+ return this.client.call('optio-engine', 'dismiss', params, opts);
14
+ }
15
+ groupCancel(params, opts) {
16
+ return this.client.call('optio-engine', 'groupCancel', params, opts);
17
+ }
18
+ groupCancelAndWait(params, opts) {
19
+ return this.client.call('optio-engine', 'groupCancelAndWait', params, opts);
20
+ }
21
+ launch(params, opts) {
22
+ return this.client.call('optio-engine', 'launch', params, opts);
23
+ }
24
+ resync(params) {
25
+ return this.client.notify('optio-engine', 'resync', params);
26
+ }
27
+ unblockLaunches(params, opts) {
28
+ return this.client.call('optio-engine', 'unblockLaunches', params, opts);
29
+ }
30
+ }
31
+ //# sourceMappingURL=optio-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"optio-engine.js","sourceRoot":"","sources":["../../src/_generated/optio-engine.ts"],"names":[],"mappings":"AAsBA,MAAM,OAAO,iBAAiB;IACR;IAApB,YAAoB,MAAsB;QAAtB,WAAM,GAAN,MAAM,CAAgB;IAAG,CAAC;IAC9C,aAAa,CAAC,MAA2B,EAAE,IAA6B;QACtE,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,eAAe,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IACzE,CAAC;IACD,MAAM,CAAC,MAAoB,EAAE,IAA6B;QACxD,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IAClE,CAAC;IACD,OAAO,CAAC,MAAqB,EAAE,IAA6B;QAC1D,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IACnE,CAAC;IACD,WAAW,CAAC,MAAyB,EAAE,IAA6B;QAClE,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,aAAa,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IACvE,CAAC;IACD,kBAAkB,CAAC,MAAgC,EAAE,IAA6B;QAChF,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,oBAAoB,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IAC9E,CAAC;IACD,MAAM,CAAC,MAAoB,EAAE,IAA6B;QACxD,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IAClE,CAAC;IACD,MAAM,CAAC,MAAoB;QACzB,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IAC9D,CAAC;IACD,eAAe,CAAC,MAA6B,EAAE,IAA6B;QAC1E,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,iBAAiB,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC;IAC3E,CAAC;CACF"}
@@ -0,0 +1,24 @@
1
+ import type { Express } from 'express';
2
+ import type { Db } from 'mongodb';
3
+ import type { MongoClient } from 'mongodb';
4
+ import type { Redis } from 'ioredis';
5
+ import type { AuthCallback } from '../auth.js';
6
+ import { type OptioContext } from '../context.js';
7
+ export type OptioApiOptions = (BaseOptioApiOptions & {
8
+ ctx: OptioContext;
9
+ }) | (BaseOptioApiOptions & {
10
+ redis: Redis;
11
+ } & ({
12
+ db: Db;
13
+ mongoClient?: never;
14
+ } | {
15
+ mongoClient: MongoClient;
16
+ db?: never;
17
+ }));
18
+ interface BaseOptioApiOptions {
19
+ prefix?: string;
20
+ authenticate: AuthCallback<import('express').Request>;
21
+ }
22
+ export declare function registerOptioApi(app: Express, opts: OptioApiOptions): OptioContext | void;
23
+ export {};
24
+ //# sourceMappingURL=express.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"express.d.ts","sourceRoot":"","sources":["../../src/adapters/express.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAQrC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG/C,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,eAAe,CAAC;AAEtE,MAAM,MAAM,eAAe,GACvB,CAAC,mBAAmB,GAAG;IAAE,GAAG,EAAE,YAAY,CAAA;CAAE,CAAC,GAC7C,CAAC,mBAAmB,GAAG;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,GAAG,CACtC;IAAE,EAAE,EAAE,EAAE,CAAC;IAAC,WAAW,CAAC,EAAE,KAAK,CAAA;CAAE,GAC/B;IAAE,WAAW,EAAE,WAAW,CAAC;IAAC,EAAE,CAAC,EAAE,KAAK,CAAA;CAAE,CAC3C,CAAC,CAAC;AAEP,UAAU,mBAAmB;IAC3B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,YAAY,CAAC,OAAO,SAAS,EAAE,OAAO,CAAC,CAAC;CACvD;AAKD,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,eAAe,GAAG,YAAY,GAAG,IAAI,CA6KzF"}