@velum-labs/routekit-gateway 0.9.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 +201 -0
- package/README.md +28 -0
- package/dist/acp-agent.d.ts +38 -0
- package/dist/acp-agent.js +142 -0
- package/dist/acp-registry.d.ts +36 -0
- package/dist/acp-registry.js +85 -0
- package/dist/adapters/anthropic.d.ts +131 -0
- package/dist/adapters/anthropic.js +1195 -0
- package/dist/adapters/chat.d.ts +14 -0
- package/dist/adapters/chat.js +34 -0
- package/dist/adapters/cursor.d.ts +34 -0
- package/dist/adapters/cursor.js +305 -0
- package/dist/adapters/dropped.d.ts +10 -0
- package/dist/adapters/dropped.js +24 -0
- package/dist/adapters/openai-chat-wire.d.ts +93 -0
- package/dist/adapters/openai-chat-wire.js +143 -0
- package/dist/adapters/responses-stream.d.ts +7 -0
- package/dist/adapters/responses-stream.js +597 -0
- package/dist/adapters/responses.d.ts +174 -0
- package/dist/adapters/responses.js +778 -0
- package/dist/adapters/server-tool-loop.d.ts +94 -0
- package/dist/adapters/server-tool-loop.js +477 -0
- package/dist/adapters/upstream-error.d.ts +14 -0
- package/dist/adapters/upstream-error.js +25 -0
- package/dist/adapters/validate.d.ts +27 -0
- package/dist/adapters/validate.js +180 -0
- package/dist/adapters/web-search.d.ts +46 -0
- package/dist/adapters/web-search.js +151 -0
- package/dist/auth.d.ts +10 -0
- package/dist/auth.js +28 -0
- package/dist/backend.d.ts +151 -0
- package/dist/backend.js +143 -0
- package/dist/capacity-pool.d.ts +31 -0
- package/dist/capacity-pool.js +99 -0
- package/dist/cost.d.ts +49 -0
- package/dist/cost.js +112 -0
- package/dist/endpoint-health.d.ts +54 -0
- package/dist/endpoint-health.js +123 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +23 -0
- package/dist/provenance.d.ts +31 -0
- package/dist/provenance.js +191 -0
- package/dist/provider-backends.d.ts +40 -0
- package/dist/provider-backends.js +1050 -0
- package/dist/provider-source.d.ts +40 -0
- package/dist/provider-source.js +293 -0
- package/dist/router.d.ts +168 -0
- package/dist/router.js +474 -0
- package/dist/server.d.ts +67 -0
- package/dist/server.js +930 -0
- package/dist/sse/chat-assembler.d.ts +45 -0
- package/dist/sse/chat-assembler.js +190 -0
- package/dist/sse/parse.d.ts +50 -0
- package/dist/sse/parse.js +149 -0
- package/dist/sse-wire.d.ts +10 -0
- package/dist/sse-wire.js +31 -0
- package/dist/switching-proxy.d.ts +15 -0
- package/dist/switching-proxy.js +232 -0
- package/dist/test/acp-agent.test.d.ts +1 -0
- package/dist/test/acp-agent.test.js +66 -0
- package/dist/test/acp-registry.test.d.ts +1 -0
- package/dist/test/acp-registry.test.js +70 -0
- package/dist/test/anthropic.test.d.ts +1 -0
- package/dist/test/anthropic.test.js +793 -0
- package/dist/test/auth.test.d.ts +1 -0
- package/dist/test/auth.test.js +25 -0
- package/dist/test/boundary.test.d.ts +1 -0
- package/dist/test/boundary.test.js +32 -0
- package/dist/test/chat.test.d.ts +1 -0
- package/dist/test/chat.test.js +418 -0
- package/dist/test/cost.test.d.ts +1 -0
- package/dist/test/cost.test.js +60 -0
- package/dist/test/cursor.test.d.ts +1 -0
- package/dist/test/cursor.test.js +100 -0
- package/dist/test/drain.test.d.ts +1 -0
- package/dist/test/drain.test.js +116 -0
- package/dist/test/dropped.test.d.ts +1 -0
- package/dist/test/dropped.test.js +80 -0
- package/dist/test/endpoint-health.test.d.ts +1 -0
- package/dist/test/endpoint-health.test.js +73 -0
- package/dist/test/provenance.test.d.ts +1 -0
- package/dist/test/provenance.test.js +176 -0
- package/dist/test/provider-backends.test.d.ts +1 -0
- package/dist/test/provider-backends.test.js +699 -0
- package/dist/test/responses.test.d.ts +1 -0
- package/dist/test/responses.test.js +813 -0
- package/dist/test/routed-backend.test.d.ts +1 -0
- package/dist/test/routed-backend.test.js +39 -0
- package/dist/test/router.test.d.ts +1 -0
- package/dist/test/router.test.js +297 -0
- package/dist/test/server-resilience.test.d.ts +1 -0
- package/dist/test/server-resilience.test.js +169 -0
- package/dist/test/sse-codec.test.d.ts +1 -0
- package/dist/test/sse-codec.test.js +186 -0
- package/dist/test/web-search-loop.test.d.ts +1 -0
- package/dist/test/web-search-loop.test.js +469 -0
- package/dist/test/wire-validation.test.d.ts +1 -0
- package/dist/test/wire-validation.test.js +140 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Velum Labs
|
|
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,28 @@
|
|
|
1
|
+
# @velum-labs/routekit-gateway
|
|
2
|
+
|
|
3
|
+
Product-neutral model routing and provider egress for RouteKit.
|
|
4
|
+
|
|
5
|
+
The package owns the `Backend` interface, HTTP gateway, OpenAI Chat,
|
|
6
|
+
Responses, Anthropic Messages and Cursor adapters, SSE handling, ACP support,
|
|
7
|
+
normalized call provenance, runtime-validated router configuration, model
|
|
8
|
+
catalogs, provider sources, and provider-native egress.
|
|
9
|
+
|
|
10
|
+
At startup `CatalogBackend` authenticates every explicitly configured provider,
|
|
11
|
+
performs live model discovery, and publishes source-qualified
|
|
12
|
+
`provider/model` IDs. Dispatch removes the source prefix before provider-native
|
|
13
|
+
egress. An unavailable provider fails startup rather than silently shrinking
|
|
14
|
+
the catalog.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
import {
|
|
18
|
+
CatalogBackend,
|
|
19
|
+
parseRouterConfig,
|
|
20
|
+
startGateway
|
|
21
|
+
} from "@velum-labs/routekit-gateway";
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
API-key providers use registry-defined credentials and URLs. Multi-account
|
|
25
|
+
subscription providers and relays are in `@velum-labs/routekit-accounts`; they expose the
|
|
26
|
+
same source interface with per-model account eligibility and quota-aware
|
|
27
|
+
selection.
|
|
28
|
+
Product-specific orchestration is in `@fusionkit/gateway`.
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP-compatible front door for the unified runner.
|
|
3
|
+
*
|
|
4
|
+
* Implements the minimal Agent Client Protocol (ACP) local-agent lifecycle over
|
|
5
|
+
* newline-delimited JSON-RPC on stdio: `initialize`, `authenticate`,
|
|
6
|
+
* `session/new`, and `session/prompt`. A `session/prompt` runs the unified
|
|
7
|
+
* injected runner and streams the final answer back as `session/update`
|
|
8
|
+
* notifications before returning the prompt
|
|
9
|
+
* turn's stop reason.
|
|
10
|
+
*
|
|
11
|
+
* The runner and input/output streams are injectable for deterministic
|
|
12
|
+
* testing.
|
|
13
|
+
*/
|
|
14
|
+
import type { Readable, Writable } from "node:stream";
|
|
15
|
+
export declare const ACP_PROTOCOL_VERSION = 1;
|
|
16
|
+
export type AcpRunnerInput = {
|
|
17
|
+
prompt: string;
|
|
18
|
+
sessionId: string;
|
|
19
|
+
requestId: string;
|
|
20
|
+
};
|
|
21
|
+
export type AcpRunnerResult = {
|
|
22
|
+
finalOutput: string;
|
|
23
|
+
runId: string;
|
|
24
|
+
status: "succeeded" | "failed" | "skipped";
|
|
25
|
+
evidence: string[];
|
|
26
|
+
};
|
|
27
|
+
export type AcpRunner = (input: AcpRunnerInput) => Promise<AcpRunnerResult>;
|
|
28
|
+
export type AcpAgentOptions = {
|
|
29
|
+
runner: AcpRunner;
|
|
30
|
+
input?: Readable;
|
|
31
|
+
output?: Writable;
|
|
32
|
+
protocolVersion?: number;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Run the ACP agent loop until the input stream ends. Resolves when input
|
|
36
|
+
* closes. Each line is one JSON-RPC message.
|
|
37
|
+
*/
|
|
38
|
+
export declare function runAcpAgent(options: AcpAgentOptions): Promise<void>;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP-compatible front door for the unified runner.
|
|
3
|
+
*
|
|
4
|
+
* Implements the minimal Agent Client Protocol (ACP) local-agent lifecycle over
|
|
5
|
+
* newline-delimited JSON-RPC on stdio: `initialize`, `authenticate`,
|
|
6
|
+
* `session/new`, and `session/prompt`. A `session/prompt` runs the unified
|
|
7
|
+
* injected runner and streams the final answer back as `session/update`
|
|
8
|
+
* notifications before returning the prompt
|
|
9
|
+
* turn's stop reason.
|
|
10
|
+
*
|
|
11
|
+
* The runner and input/output streams are injectable for deterministic
|
|
12
|
+
* testing.
|
|
13
|
+
*/
|
|
14
|
+
import { createInterface } from "node:readline";
|
|
15
|
+
export const ACP_PROTOCOL_VERSION = 1;
|
|
16
|
+
function textFromPromptParam(params) {
|
|
17
|
+
if (typeof params !== "object" || params === null)
|
|
18
|
+
return "";
|
|
19
|
+
const prompt = params.prompt;
|
|
20
|
+
if (typeof prompt === "string")
|
|
21
|
+
return prompt;
|
|
22
|
+
if (Array.isArray(prompt)) {
|
|
23
|
+
return prompt
|
|
24
|
+
.map((block) => {
|
|
25
|
+
const candidate = block;
|
|
26
|
+
return typeof candidate.text === "string" ? candidate.text : "";
|
|
27
|
+
})
|
|
28
|
+
.join("");
|
|
29
|
+
}
|
|
30
|
+
return "";
|
|
31
|
+
}
|
|
32
|
+
function sessionIdFromParams(params, fallback) {
|
|
33
|
+
if (typeof params === "object" && params !== null) {
|
|
34
|
+
const sessionId = params.sessionId;
|
|
35
|
+
if (typeof sessionId === "string" && sessionId.length > 0)
|
|
36
|
+
return sessionId;
|
|
37
|
+
}
|
|
38
|
+
return fallback;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Run the ACP agent loop until the input stream ends. Resolves when input
|
|
42
|
+
* closes. Each line is one JSON-RPC message.
|
|
43
|
+
*/
|
|
44
|
+
export async function runAcpAgent(options) {
|
|
45
|
+
const input = options.input ?? process.stdin;
|
|
46
|
+
const output = options.output ?? process.stdout;
|
|
47
|
+
const protocolVersion = options.protocolVersion ?? ACP_PROTOCOL_VERSION;
|
|
48
|
+
let sessionCounter = 0;
|
|
49
|
+
let requestCounter = 0;
|
|
50
|
+
const send = (message) => {
|
|
51
|
+
output.write(`${JSON.stringify({ jsonrpc: "2.0", ...message })}\n`);
|
|
52
|
+
};
|
|
53
|
+
const respond = (id, result) => {
|
|
54
|
+
send({ id, result });
|
|
55
|
+
};
|
|
56
|
+
const respondError = (id, code, message) => {
|
|
57
|
+
send({ id, error: { code, message } });
|
|
58
|
+
};
|
|
59
|
+
const handlePrompt = async (id, params) => {
|
|
60
|
+
const sessionId = sessionIdFromParams(params, `sess_${sessionCounter}`);
|
|
61
|
+
const prompt = textFromPromptParam(params);
|
|
62
|
+
requestCounter += 1;
|
|
63
|
+
const result = await options.runner({
|
|
64
|
+
prompt,
|
|
65
|
+
sessionId,
|
|
66
|
+
requestId: `acp_${requestCounter}`
|
|
67
|
+
});
|
|
68
|
+
send({
|
|
69
|
+
method: "session/update",
|
|
70
|
+
params: {
|
|
71
|
+
sessionId,
|
|
72
|
+
update: {
|
|
73
|
+
sessionUpdate: "agent_message_chunk",
|
|
74
|
+
content: { type: "text", text: result.finalOutput }
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
respond(id, {
|
|
79
|
+
stopReason: result.status === "succeeded" ? "end_turn" : "refusal",
|
|
80
|
+
_meta: { runId: result.runId, status: result.status, evidence: result.evidence }
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
const dispatch = async (message) => {
|
|
84
|
+
const { id, method } = message;
|
|
85
|
+
if (method === undefined || id === undefined)
|
|
86
|
+
return;
|
|
87
|
+
switch (method) {
|
|
88
|
+
case "initialize":
|
|
89
|
+
respond(id, {
|
|
90
|
+
protocolVersion,
|
|
91
|
+
agentCapabilities: {
|
|
92
|
+
loadSession: false,
|
|
93
|
+
promptCapabilities: { image: false, audio: false, embeddedContext: true }
|
|
94
|
+
},
|
|
95
|
+
authMethods: []
|
|
96
|
+
});
|
|
97
|
+
return;
|
|
98
|
+
case "authenticate":
|
|
99
|
+
respond(id, {});
|
|
100
|
+
return;
|
|
101
|
+
case "session/new":
|
|
102
|
+
sessionCounter += 1;
|
|
103
|
+
respond(id, { sessionId: `sess_${sessionCounter}` });
|
|
104
|
+
return;
|
|
105
|
+
case "session/load":
|
|
106
|
+
respond(id, {});
|
|
107
|
+
return;
|
|
108
|
+
case "session/cancel":
|
|
109
|
+
respond(id, {});
|
|
110
|
+
return;
|
|
111
|
+
case "session/prompt":
|
|
112
|
+
await handlePrompt(id, message.params);
|
|
113
|
+
return;
|
|
114
|
+
default:
|
|
115
|
+
respondError(id, -32601, `method not found: ${method}`);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const rl = createInterface({ input });
|
|
120
|
+
const pending = [];
|
|
121
|
+
rl.on("line", (line) => {
|
|
122
|
+
const trimmed = line.trim();
|
|
123
|
+
if (trimmed.length === 0)
|
|
124
|
+
return;
|
|
125
|
+
let message;
|
|
126
|
+
try {
|
|
127
|
+
message = JSON.parse(trimmed);
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
pending.push(dispatch(message).catch((error) => {
|
|
133
|
+
if (message.id !== undefined) {
|
|
134
|
+
respondError(message.id, -32603, error instanceof Error ? error.message : String(error));
|
|
135
|
+
}
|
|
136
|
+
}));
|
|
137
|
+
});
|
|
138
|
+
await new Promise((resolve) => {
|
|
139
|
+
rl.on("close", () => resolve());
|
|
140
|
+
});
|
|
141
|
+
await Promise.all(pending);
|
|
142
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP Registry integration. Resolves curated ACP-compatible agents (for
|
|
3
|
+
* example the registry-backed `Codex CLI` and `Claude Agent` adapters) from the
|
|
4
|
+
* ACP Registry so they can drive the generic ACP front door. The fetcher and
|
|
5
|
+
* install directory are injectable for deterministic testing.
|
|
6
|
+
*
|
|
7
|
+
* Registry source: https://cdn.agentclientprotocol.com/registry/v1/latest/registry.json
|
|
8
|
+
*/
|
|
9
|
+
export declare const ACP_REGISTRY_URL = "https://cdn.agentclientprotocol.com/registry/v1/latest/registry.json";
|
|
10
|
+
export type AcpRegistryAgent = {
|
|
11
|
+
id: string;
|
|
12
|
+
name?: string;
|
|
13
|
+
version?: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
distribution?: Record<string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
export type AcpRegistry = {
|
|
18
|
+
agents: AcpRegistryAgent[];
|
|
19
|
+
};
|
|
20
|
+
export type AcpRegistryFetcher = (url: string) => Promise<unknown>;
|
|
21
|
+
export type InstalledAcpAdapter = {
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
version: string;
|
|
25
|
+
distribution: Record<string, unknown>;
|
|
26
|
+
installedAt: string;
|
|
27
|
+
metadataPath: string;
|
|
28
|
+
};
|
|
29
|
+
export declare function fetchAcpRegistry(fetcher?: AcpRegistryFetcher, url?: string): Promise<AcpRegistry>;
|
|
30
|
+
export type InstallAcpAdaptersOptions = {
|
|
31
|
+
agentIds: string[];
|
|
32
|
+
installDir: string;
|
|
33
|
+
fetcher?: AcpRegistryFetcher;
|
|
34
|
+
url?: string;
|
|
35
|
+
};
|
|
36
|
+
export declare function installAcpAdapters(options: InstallAcpAdaptersOptions): Promise<InstalledAcpAdapter[]>;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ACP Registry integration. Resolves curated ACP-compatible agents (for
|
|
3
|
+
* example the registry-backed `Codex CLI` and `Claude Agent` adapters) from the
|
|
4
|
+
* ACP Registry so they can drive the generic ACP front door. The fetcher and
|
|
5
|
+
* install directory are injectable for deterministic testing.
|
|
6
|
+
*
|
|
7
|
+
* Registry source: https://cdn.agentclientprotocol.com/registry/v1/latest/registry.json
|
|
8
|
+
*/
|
|
9
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { join, resolve } from "node:path";
|
|
11
|
+
export const ACP_REGISTRY_URL = "https://cdn.agentclientprotocol.com/registry/v1/latest/registry.json";
|
|
12
|
+
function normalizeRegistry(raw) {
|
|
13
|
+
if (typeof raw !== "object" || raw === null) {
|
|
14
|
+
throw new Error("ACP registry payload must be an object");
|
|
15
|
+
}
|
|
16
|
+
const agentsValue = raw.agents;
|
|
17
|
+
if (!Array.isArray(agentsValue)) {
|
|
18
|
+
throw new Error("ACP registry payload is missing an agents array");
|
|
19
|
+
}
|
|
20
|
+
const agents = [];
|
|
21
|
+
for (const entry of agentsValue) {
|
|
22
|
+
if (typeof entry !== "object" || entry === null)
|
|
23
|
+
continue;
|
|
24
|
+
const id = entry.id;
|
|
25
|
+
if (typeof id !== "string" || id.length === 0)
|
|
26
|
+
continue;
|
|
27
|
+
const agent = { id };
|
|
28
|
+
const name = entry.name;
|
|
29
|
+
if (typeof name === "string")
|
|
30
|
+
agent.name = name;
|
|
31
|
+
const version = entry.version;
|
|
32
|
+
if (typeof version === "string")
|
|
33
|
+
agent.version = version;
|
|
34
|
+
const description = entry.description;
|
|
35
|
+
if (typeof description === "string")
|
|
36
|
+
agent.description = description;
|
|
37
|
+
const distribution = entry.distribution;
|
|
38
|
+
if (typeof distribution === "object" && distribution !== null) {
|
|
39
|
+
agent.distribution = distribution;
|
|
40
|
+
}
|
|
41
|
+
agents.push(agent);
|
|
42
|
+
}
|
|
43
|
+
return { agents };
|
|
44
|
+
}
|
|
45
|
+
const defaultFetcher = async (url) => {
|
|
46
|
+
const response = await fetch(url);
|
|
47
|
+
if (!response.ok) {
|
|
48
|
+
throw new Error(`ACP registry fetch failed: ${response.status}`);
|
|
49
|
+
}
|
|
50
|
+
return (await response.json());
|
|
51
|
+
};
|
|
52
|
+
export async function fetchAcpRegistry(fetcher = defaultFetcher, url = ACP_REGISTRY_URL) {
|
|
53
|
+
return normalizeRegistry(await fetcher(url));
|
|
54
|
+
}
|
|
55
|
+
export async function installAcpAdapters(options) {
|
|
56
|
+
if (options.agentIds.length === 0) {
|
|
57
|
+
throw new Error("at least one ACP agent id is required");
|
|
58
|
+
}
|
|
59
|
+
const registry = await fetchAcpRegistry(options.fetcher ?? defaultFetcher, options.url ?? ACP_REGISTRY_URL);
|
|
60
|
+
const byId = new Map(registry.agents.map((agent) => [agent.id, agent]));
|
|
61
|
+
const dir = resolve(options.installDir);
|
|
62
|
+
mkdirSync(dir, { recursive: true });
|
|
63
|
+
const installed = [];
|
|
64
|
+
for (const agentId of options.agentIds) {
|
|
65
|
+
const agent = byId.get(agentId);
|
|
66
|
+
if (agent === undefined) {
|
|
67
|
+
throw new Error(`ACP registry has no agent with id "${agentId}"`);
|
|
68
|
+
}
|
|
69
|
+
if (agent.distribution === undefined) {
|
|
70
|
+
throw new Error(`ACP agent "${agentId}" has no distribution metadata`);
|
|
71
|
+
}
|
|
72
|
+
const metadataPath = join(dir, `${agentId}.json`);
|
|
73
|
+
const record = {
|
|
74
|
+
id: agent.id,
|
|
75
|
+
name: agent.name ?? agent.id,
|
|
76
|
+
version: agent.version ?? "unknown",
|
|
77
|
+
distribution: agent.distribution,
|
|
78
|
+
installedAt: new Date().toISOString(),
|
|
79
|
+
metadataPath
|
|
80
|
+
};
|
|
81
|
+
writeFileSync(metadataPath, JSON.stringify(record, null, 2) + "\n");
|
|
82
|
+
installed.push(record);
|
|
83
|
+
}
|
|
84
|
+
return installed;
|
|
85
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic Messages adapter. Claude Code speaks the Anthropic Messages API to
|
|
3
|
+
* whatever `ANTHROPIC_BASE_URL` points at, so to back it with a local model we
|
|
4
|
+
* translate `/v1/messages` (and `/v1/messages/count_tokens`, and the
|
|
5
|
+
* `/v1/models` discovery probe) to and from the gateway's OpenAI Chat
|
|
6
|
+
* Completions core. The pure translation functions are exported for testing;
|
|
7
|
+
* the request handler wires them to a `Backend` and returns a `Response` the
|
|
8
|
+
* server pipes straight to the client (JSON or SSE).
|
|
9
|
+
*/
|
|
10
|
+
import type { Backend, BackendRequestOptions } from "../backend.js";
|
|
11
|
+
import { type AnthropicThinkingConfig, type OpenAiChoice } from "./openai-chat-wire.js";
|
|
12
|
+
import type { ExecutedSearch, ServerToolLoopEvent } from "./server-tool-loop.js";
|
|
13
|
+
type AnthropicTextBlock = {
|
|
14
|
+
type: "text";
|
|
15
|
+
text: string;
|
|
16
|
+
};
|
|
17
|
+
type AnthropicImageBlock = {
|
|
18
|
+
type: "image";
|
|
19
|
+
source: {
|
|
20
|
+
type: "base64";
|
|
21
|
+
media_type: string;
|
|
22
|
+
data: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
type AnthropicToolUseBlock = {
|
|
26
|
+
type: "tool_use";
|
|
27
|
+
id: string;
|
|
28
|
+
name: string;
|
|
29
|
+
input: unknown;
|
|
30
|
+
};
|
|
31
|
+
type AnthropicToolResultBlock = {
|
|
32
|
+
type: "tool_result";
|
|
33
|
+
tool_use_id: string;
|
|
34
|
+
content?: string | AnthropicContentBlock[];
|
|
35
|
+
is_error?: boolean;
|
|
36
|
+
};
|
|
37
|
+
type AnthropicContentBlock = AnthropicTextBlock | AnthropicImageBlock | AnthropicToolUseBlock | AnthropicToolResultBlock | {
|
|
38
|
+
type: string;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
};
|
|
41
|
+
type AnthropicThinking = AnthropicThinkingConfig | null;
|
|
42
|
+
type AnthropicOutputConfig = {
|
|
43
|
+
effort?: string | null;
|
|
44
|
+
[key: string]: unknown;
|
|
45
|
+
};
|
|
46
|
+
type AnthropicMessage = {
|
|
47
|
+
role: "user" | "assistant";
|
|
48
|
+
content: string | AnthropicContentBlock[];
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Optional object fields tolerate an explicit JSON `null` (some clients encode
|
|
52
|
+
* "unset" that way — Codex does on the Responses wire); reads must use
|
|
53
|
+
* null-tolerant guards so a null never crashes the turn.
|
|
54
|
+
*/
|
|
55
|
+
export type AnthropicRequest = {
|
|
56
|
+
model?: string;
|
|
57
|
+
system?: string | AnthropicTextBlock[] | null;
|
|
58
|
+
messages: AnthropicMessage[];
|
|
59
|
+
max_tokens?: number;
|
|
60
|
+
temperature?: number;
|
|
61
|
+
top_p?: number;
|
|
62
|
+
top_k?: number;
|
|
63
|
+
thinking?: AnthropicThinking | null;
|
|
64
|
+
output_config?: AnthropicOutputConfig | null;
|
|
65
|
+
metadata?: Record<string, unknown> | null;
|
|
66
|
+
stop_sequences?: string[];
|
|
67
|
+
stream?: boolean;
|
|
68
|
+
tools?: Array<{
|
|
69
|
+
type?: string;
|
|
70
|
+
name: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
input_schema?: unknown;
|
|
73
|
+
}>;
|
|
74
|
+
tool_choice?: {
|
|
75
|
+
type: "auto" | "any" | "tool" | "none";
|
|
76
|
+
name?: string;
|
|
77
|
+
disable_parallel_tool_use?: boolean;
|
|
78
|
+
} | null;
|
|
79
|
+
};
|
|
80
|
+
/** Options gating server-executed tool projection (on iff an executor exists). */
|
|
81
|
+
export type AnthropicTranslationOptions = {
|
|
82
|
+
serverTools?: boolean;
|
|
83
|
+
};
|
|
84
|
+
type OpenAiUsage = {
|
|
85
|
+
prompt_tokens?: number;
|
|
86
|
+
completion_tokens?: number;
|
|
87
|
+
};
|
|
88
|
+
type OpenAiResponse = {
|
|
89
|
+
id?: string;
|
|
90
|
+
choices?: OpenAiChoice[];
|
|
91
|
+
usage?: OpenAiUsage;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Translate an Anthropic Messages request to an OpenAI Chat Completions body.
|
|
95
|
+
* The upstream model is always the backend's own model (Claude Code sends a
|
|
96
|
+
* `claude-*` id the local server would not recognise); the requested id is
|
|
97
|
+
* only echoed back in the response.
|
|
98
|
+
*/
|
|
99
|
+
export declare function anthropicToChat(body: AnthropicRequest, backendModel: string | undefined, options?: AnthropicTranslationOptions): Record<string, unknown>;
|
|
100
|
+
export declare function mapStopReason(finishReason: string | null | undefined): string;
|
|
101
|
+
export declare function chatToAnthropicMessage(openai: OpenAiResponse, model: string, searches?: readonly ExecutedSearch[], events?: readonly ServerToolLoopEvent[]): Record<string, unknown>;
|
|
102
|
+
export declare function openAiSseToAnthropic(upstream: ReadableStream<Uint8Array>, model: string): ReadableStream<Uint8Array>;
|
|
103
|
+
export declare function countTokensEstimate(body: AnthropicRequest): number;
|
|
104
|
+
export declare function handleAnthropicMessages(backend: Backend, body: AnthropicRequest, modelCallId?: string, signal?: AbortSignal, backendOptions?: BackendRequestOptions): Promise<Response>;
|
|
105
|
+
export declare function handleCountTokens(body: AnthropicRequest): Response;
|
|
106
|
+
/** The `claude-` prefix used to alias non-Anthropic models past Claude's filter. */
|
|
107
|
+
export declare const CLAUDE_ALIAS_PREFIX = "claude-";
|
|
108
|
+
export type ClaudePickerModelRoute = {
|
|
109
|
+
publicId: string;
|
|
110
|
+
nativeId: string;
|
|
111
|
+
provider: string;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* The id a model is advertised under in Claude Code's `/model` picker. Claude
|
|
115
|
+
* only lists ids beginning with `claude`/`anthropic`, so non-Anthropic models
|
|
116
|
+
* are aliased with a `claude-` prefix; the gateway maps the alias back when
|
|
117
|
+
* routing (see `resolveAlias`), and the picker shows the real id via
|
|
118
|
+
* `display_name`. This is the claude-code-router trick: the `model` field is an
|
|
119
|
+
* identifier we control end-to-end, so any model can be made selectable.
|
|
120
|
+
*/
|
|
121
|
+
export declare function claudeModelAlias(id: string): string;
|
|
122
|
+
export declare function resolveClaudeModelAlias(requested: string | undefined, modelIds?: readonly string[]): string | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* Anthropic-shaped `/v1/models` discovery response. Every advertised model is
|
|
125
|
+
* listed so it appears in Claude Code's `/model` picker: Anthropic-family ids
|
|
126
|
+
* as-is, others under a `claude-`prefixed alias with the real id as
|
|
127
|
+
* `display_name`. `modelIds` is the full advertised set (default model first);
|
|
128
|
+
* when absent we fall back to the single backend default.
|
|
129
|
+
*/
|
|
130
|
+
export declare function anthropicModelsResponse(backendModel: string | undefined, modelIds?: readonly string[], modelRoutes?: readonly ClaudePickerModelRoute[]): Response;
|
|
131
|
+
export {};
|