@coder/ai-sdk-provider 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.
- package/LICENSE +202 -0
- package/README.md +123 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +65 -0
- package/dist/index.js.map +1 -0
- package/package.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
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 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 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 those 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. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# @coder/ai-sdk-provider
|
|
2
|
+
|
|
3
|
+
[](https://github.com/coder/ai-sdk/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@coder/ai-sdk-provider)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
|
|
7
|
+
A **Vercel AI SDK provider that routes requests through your Coder deployment's
|
|
8
|
+
[AI Gateway](https://coder.com/docs/ai-coder/ai-gateway)** (formerly "AI Bridge" — the URL path is still `aibridge`). Point it
|
|
9
|
+
at your deployment URL, hand it a Coder API token, and use any model your
|
|
10
|
+
deployment proxies with the AI SDK's `generateText` / `streamText` — the same way
|
|
11
|
+
you'd use [OpenRouter](https://ai-sdk.dev/providers/community-providers/openrouter)
|
|
12
|
+
or any other provider.
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { generateText } from "ai";
|
|
16
|
+
import { createCoder } from "@coder/ai-sdk-provider";
|
|
17
|
+
|
|
18
|
+
const coder = createCoder({
|
|
19
|
+
baseURL: "https://coder.example.com",
|
|
20
|
+
apiKey: process.env.CODER_API_TOKEN!,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const { text } = await generateText({
|
|
24
|
+
model: coder("claude-sonnet-4-6"),
|
|
25
|
+
prompt: "What is Coder?",
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Why
|
|
30
|
+
|
|
31
|
+
**AI Gateway** is Coder's LLM gateway: it sits between your AI tooling and the
|
|
32
|
+
upstream providers (Anthropic, OpenAI, Bedrock, Copilot, …), authenticates each
|
|
33
|
+
request against a Coder identity, injects the centrally-managed provider keys, and
|
|
34
|
+
audits usage per user. This package lets the Vercel AI SDK speak to it natively, so
|
|
35
|
+
your developers never handle raw provider keys — they authenticate with their Coder
|
|
36
|
+
token and the deployment decides which models and providers are available.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pnpm add @coder/ai-sdk-provider ai zod
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Requires Node ≥ 20, `ai` v6, and a Coder deployment with **AI Gateway enabled**
|
|
45
|
+
(stable since Coder **v2.29**, GA in v2.30, on by default in v2.34; requires the
|
|
46
|
+
AI Governance Add-On).
|
|
47
|
+
|
|
48
|
+
## The two surfaces
|
|
49
|
+
|
|
50
|
+
AI Gateway exposes **two provider-namespaced surfaces** on your deployment, and
|
|
51
|
+
routing is **by URL path, not by model id** — so each surface reaches a fixed set
|
|
52
|
+
of upstreams:
|
|
53
|
+
|
|
54
|
+
| Surface | Reaches | Accessor |
|
|
55
|
+
| ------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- | -------------------------------------------- |
|
|
56
|
+
| **OpenAI-compatible** (`/api/v2/aibridge/openai/v1`) | OpenAI, Azure, Google, OpenRouter, Vercel, openai-compat — and **Copilot** (incl. Claude via Copilot) | `coder.openai(id)` / `coder.chat(id)` |
|
|
57
|
+
| **Anthropic-compatible** (`/api/v2/aibridge/anthropic`) | **native Claude** + **Bedrock-hosted Claude** | `coder.anthropic(id)` / `coder.messages(id)` |
|
|
58
|
+
|
|
59
|
+
The bare call `coder(modelId)` picks a surface by heuristic — model ids starting
|
|
60
|
+
with `claude`/`anthropic` go to the Anthropic surface, everything else to the
|
|
61
|
+
OpenAI surface. Use the explicit accessors to override (e.g. to reach Claude
|
|
62
|
+
through a Copilot-typed provider on the OpenAI surface):
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
coder("gpt-4o"); // → OpenAI surface
|
|
66
|
+
coder("claude-sonnet-4-6"); // → Anthropic surface (heuristic)
|
|
67
|
+
coder.openai("claude-sonnet-4"); // → OpenAI surface (e.g. Copilot)
|
|
68
|
+
coder.anthropic("claude-opus-4-5"); // → Anthropic surface (explicit)
|
|
69
|
+
coder.textEmbeddingModel("text-embedding-3-small"); // → OpenAI surface
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Model ids are passed through **unchanged** to the upstream provider (no
|
|
73
|
+
`vendor/model` namespacing) — use whatever ids your deployment's providers accept.
|
|
74
|
+
|
|
75
|
+
## Authentication
|
|
76
|
+
|
|
77
|
+
**Centralized mode (default).** Pass your Coder API token as `apiKey`. AI Gateway
|
|
78
|
+
holds the upstream provider keys and brokers the call — this is all you need:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
createCoder({ baseURL: "https://coder.example.com", apiKey: coderToken });
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Bring-your-own-key (BYOK) mode.** Set `coderToken` (sent in the
|
|
85
|
+
`X-Coder-AI-Governance-Token` header to authenticate you to the gateway) and pass
|
|
86
|
+
your _upstream_ provider key as `apiKey` (forwarded to the upstream):
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
createCoder({
|
|
90
|
+
baseURL: "https://coder.example.com",
|
|
91
|
+
coderToken, // authenticates you to AI Gateway
|
|
92
|
+
apiKey: upstreamKey, // your own OpenAI/Anthropic key
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Configuration
|
|
97
|
+
|
|
98
|
+
| Option | Type | Default | Description |
|
|
99
|
+
| --------------- | ------------------------- | ---------------------- | ----------------------------------------------------------------------------------------------------- |
|
|
100
|
+
| `baseURL` | `string` | — (required) | Your Coder deployment URL, e.g. `https://coder.example.com`. The AI Gateway path is appended for you. |
|
|
101
|
+
| `apiKey` | `string` | — | Coder API token (centralized) or upstream key (BYOK). |
|
|
102
|
+
| `coderToken` | `string` | — | Enables BYOK mode; sent in `X-Coder-AI-Governance-Token`. |
|
|
103
|
+
| `headers` | `Record<string,string>` | — | Extra headers merged into every request. |
|
|
104
|
+
| `aiGatewayPath` | `string` | `/api/v2/aibridge` | Override if your deployment uses a different mount path. |
|
|
105
|
+
| `providers` | `{ openai?, anthropic? }` | `openai` / `anthropic` | Override the admin-configured provider path segments. |
|
|
106
|
+
| `fetch` | `typeof fetch` | global `fetch` | Custom fetch (testing / middleware). |
|
|
107
|
+
|
|
108
|
+
## Examples
|
|
109
|
+
|
|
110
|
+
Runnable scripts live in [`examples/`](./examples) (run against a real deployment via `tsx`):
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
export CODER_URL=https://coder.example.com
|
|
114
|
+
export CODER_API_TOKEN=$(coder tokens create --name ai-sdk-provider-example)
|
|
115
|
+
|
|
116
|
+
pnpm example:generate # non-streaming generateText
|
|
117
|
+
pnpm example:stream # streaming streamText
|
|
118
|
+
pnpm example:anthropic # the Anthropic surface (native Claude)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
[Apache-2.0](./LICENSE) © Coder Technologies, Inc.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { AnthropicProvider } from '@ai-sdk/anthropic';
|
|
2
|
+
import { OpenAICompatibleProvider } from '@ai-sdk/openai-compatible';
|
|
3
|
+
import { LanguageModelV3, EmbeddingModelV3 } from '@ai-sdk/provider';
|
|
4
|
+
|
|
5
|
+
interface CoderProviderSettings {
|
|
6
|
+
/**
|
|
7
|
+
* Base URL of your Coder deployment, e.g. `https://coder.example.com`. Do NOT
|
|
8
|
+
* include the AI Gateway path (`/api/v2/aibridge/...`) — it is appended for you.
|
|
9
|
+
*/
|
|
10
|
+
baseURL: string;
|
|
11
|
+
/**
|
|
12
|
+
* The credential sent to AI Gateway.
|
|
13
|
+
*
|
|
14
|
+
* - **Centralized mode (default):** your **Coder API token**. AI Gateway holds
|
|
15
|
+
* the upstream provider keys and brokers the call, so this is all you need.
|
|
16
|
+
* - **BYOK mode** (when {@link CoderProviderSettings.coderToken} is also set):
|
|
17
|
+
* your **upstream provider key**, which AI Gateway forwards to the upstream.
|
|
18
|
+
*/
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Enables bring-your-own-key (BYOK) mode. This Coder API token is sent in the
|
|
22
|
+
* `X-Coder-AI-Governance-Token` header to authenticate you to the gateway,
|
|
23
|
+
* while {@link CoderProviderSettings.apiKey} carries your upstream provider key.
|
|
24
|
+
* Leave unset for the default centralized mode.
|
|
25
|
+
*/
|
|
26
|
+
coderToken?: string;
|
|
27
|
+
/** Extra headers merged into every request to both surfaces. */
|
|
28
|
+
headers?: Record<string, string>;
|
|
29
|
+
/**
|
|
30
|
+
* Mount path of AI Gateway on the deployment. Defaults to `/api/v2/aibridge`.
|
|
31
|
+
* Exposed because the `aibridge` path segment may change in a future Coder
|
|
32
|
+
* release (the feature was renamed "AI Gateway").
|
|
33
|
+
*/
|
|
34
|
+
aiGatewayPath?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Override the provider path segments — the *admin-configured provider names*
|
|
37
|
+
* on the deployment (e.g. `anthropic-corp`). Default to `openai` / `anthropic`.
|
|
38
|
+
*/
|
|
39
|
+
providers?: {
|
|
40
|
+
openai?: string;
|
|
41
|
+
anthropic?: string;
|
|
42
|
+
};
|
|
43
|
+
/** Custom fetch implementation (useful for testing or middleware). */
|
|
44
|
+
fetch?: typeof globalThis.fetch;
|
|
45
|
+
}
|
|
46
|
+
interface CoderProvider {
|
|
47
|
+
/** Route a model id to a surface by heuristic (Claude ids → Anthropic surface). */
|
|
48
|
+
(modelId: string): LanguageModelV3;
|
|
49
|
+
/** Route a model id to a surface by heuristic (Claude ids → Anthropic surface). */
|
|
50
|
+
languageModel(modelId: string): LanguageModelV3;
|
|
51
|
+
/**
|
|
52
|
+
* AI Gateway's OpenAI-compatible surface (`/aibridge/openai/v1`). Reaches the
|
|
53
|
+
* OpenAI / Azure / Google / OpenRouter / Vercel / openai-compat and Copilot
|
|
54
|
+
* upstreams. Routing is by URL, so any model id sent here hits this surface.
|
|
55
|
+
*/
|
|
56
|
+
openai: OpenAICompatibleProvider;
|
|
57
|
+
/**
|
|
58
|
+
* AI Gateway's Anthropic-compatible surface (`/aibridge/anthropic`). Reaches
|
|
59
|
+
* native Claude and Bedrock-hosted Claude.
|
|
60
|
+
*/
|
|
61
|
+
anthropic: AnthropicProvider;
|
|
62
|
+
/** Shorthand for an {@link CoderProvider.openai} chat model. */
|
|
63
|
+
chat(modelId: string): LanguageModelV3;
|
|
64
|
+
/** Shorthand for an {@link CoderProvider.anthropic} messages model. */
|
|
65
|
+
messages(modelId: string): LanguageModelV3;
|
|
66
|
+
/** Text-embedding model via the OpenAI-compatible surface. */
|
|
67
|
+
textEmbeddingModel(modelId: string): EmbeddingModelV3;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Heuristic for the bare {@link CoderProvider} call: which AI Gateway surface
|
|
71
|
+
* should a model id route to? Claude / Anthropic ids go to the Anthropic
|
|
72
|
+
* surface; everything else to the OpenAI-compatible surface. Use the explicit
|
|
73
|
+
* `.openai(id)` / `.anthropic(id)` accessors to override (e.g. to reach Claude
|
|
74
|
+
* through a Copilot-typed provider on the OpenAI surface).
|
|
75
|
+
*/
|
|
76
|
+
declare function isAnthropicModelId(modelId: string): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Create a {@link CoderProvider} that routes Vercel AI SDK calls through a Coder
|
|
79
|
+
* deployment's AI Gateway (formerly "AI Bridge"). AI Gateway exposes two
|
|
80
|
+
* provider-namespaced surfaces; this provider fronts both and selects between
|
|
81
|
+
* them per model.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { generateText } from "ai";
|
|
86
|
+
* import { createCoder } from "@coder/ai-sdk-provider";
|
|
87
|
+
*
|
|
88
|
+
* const coder = createCoder({
|
|
89
|
+
* baseURL: "https://coder.example.com",
|
|
90
|
+
* apiKey: process.env.CODER_API_TOKEN!,
|
|
91
|
+
* });
|
|
92
|
+
*
|
|
93
|
+
* await generateText({ model: coder("gpt-4o"), prompt: "Hi" }); // OpenAI surface
|
|
94
|
+
* await generateText({ model: coder("claude-sonnet-4-6"), prompt: "Hi" }); // Anthropic surface
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
declare function createCoder(settings: CoderProviderSettings): CoderProvider;
|
|
98
|
+
/** Convenience alias mirroring other AI SDK providers' `createX` naming. */
|
|
99
|
+
declare const coder: typeof createCoder;
|
|
100
|
+
|
|
101
|
+
export { type CoderProvider, type CoderProviderSettings, coder, createCoder, isAnthropicModelId };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// src/provider.ts
|
|
2
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
3
|
+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
4
|
+
var DEFAULT_AI_GATEWAY_PATH = "/api/v2/aibridge";
|
|
5
|
+
var DEFAULT_OPENAI_PROVIDER = "openai";
|
|
6
|
+
var DEFAULT_ANTHROPIC_PROVIDER = "anthropic";
|
|
7
|
+
var CODER_TOKEN_HEADER = "X-Coder-AI-Governance-Token";
|
|
8
|
+
function isAnthropicModelId(modelId) {
|
|
9
|
+
return /^(?:claude|anthropic[./:])/i.test(modelId);
|
|
10
|
+
}
|
|
11
|
+
function trimTrailingSlash(url) {
|
|
12
|
+
return url.replace(/\/+$/, "");
|
|
13
|
+
}
|
|
14
|
+
function createCoder(settings) {
|
|
15
|
+
if (!settings.baseURL) {
|
|
16
|
+
throw new Error(
|
|
17
|
+
"createCoder: `baseURL` is required (your Coder deployment URL, e.g. https://coder.example.com)."
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
const deployment = trimTrailingSlash(settings.baseURL);
|
|
21
|
+
const gatewayPath = settings.aiGatewayPath ?? DEFAULT_AI_GATEWAY_PATH;
|
|
22
|
+
const openaiName = settings.providers?.openai ?? DEFAULT_OPENAI_PROVIDER;
|
|
23
|
+
const anthropicName = settings.providers?.anthropic ?? DEFAULT_ANTHROPIC_PROVIDER;
|
|
24
|
+
const openaiBaseURL = `${deployment}${gatewayPath}/${openaiName}/v1`;
|
|
25
|
+
const anthropicBaseURL = `${deployment}${gatewayPath}/${anthropicName}/v1`;
|
|
26
|
+
const byok = settings.coderToken !== void 0;
|
|
27
|
+
const headers = {
|
|
28
|
+
...byok ? { [CODER_TOKEN_HEADER]: settings.coderToken } : {},
|
|
29
|
+
...settings.headers
|
|
30
|
+
};
|
|
31
|
+
const openai = createOpenAICompatible({
|
|
32
|
+
name: "coder.openai",
|
|
33
|
+
baseURL: openaiBaseURL,
|
|
34
|
+
apiKey: settings.apiKey,
|
|
35
|
+
// → `Authorization: Bearer <apiKey>`
|
|
36
|
+
headers,
|
|
37
|
+
fetch: settings.fetch,
|
|
38
|
+
includeUsage: true
|
|
39
|
+
});
|
|
40
|
+
const anthropic = createAnthropic({
|
|
41
|
+
name: "coder.anthropic",
|
|
42
|
+
baseURL: anthropicBaseURL,
|
|
43
|
+
// Centralized: send the Coder token via `Authorization: Bearer` (the
|
|
44
|
+
// documented path). BYOK: send the upstream key via `x-api-key`.
|
|
45
|
+
...byok ? { apiKey: settings.apiKey } : { authToken: settings.apiKey },
|
|
46
|
+
headers,
|
|
47
|
+
fetch: settings.fetch
|
|
48
|
+
});
|
|
49
|
+
const languageModel = (modelId) => isAnthropicModelId(modelId) ? anthropic(modelId) : openai(modelId);
|
|
50
|
+
return Object.assign(languageModel, {
|
|
51
|
+
languageModel,
|
|
52
|
+
openai,
|
|
53
|
+
anthropic,
|
|
54
|
+
chat: (modelId) => openai(modelId),
|
|
55
|
+
messages: (modelId) => anthropic(modelId),
|
|
56
|
+
textEmbeddingModel: (modelId) => openai.textEmbeddingModel(modelId)
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
var coder = createCoder;
|
|
60
|
+
export {
|
|
61
|
+
coder,
|
|
62
|
+
createCoder,
|
|
63
|
+
isAnthropicModelId
|
|
64
|
+
};
|
|
65
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/provider.ts"],"sourcesContent":["import { type AnthropicProvider, createAnthropic } from \"@ai-sdk/anthropic\";\nimport { createOpenAICompatible, type OpenAICompatibleProvider } from \"@ai-sdk/openai-compatible\";\nimport type { EmbeddingModelV3, LanguageModelV3 } from \"@ai-sdk/provider\";\n\n/** Default mount path of AI Gateway on a Coder deployment. */\nconst DEFAULT_AI_GATEWAY_PATH = \"/api/v2/aibridge\";\n/** Default provider path segments (the admin-configured provider names). */\nconst DEFAULT_OPENAI_PROVIDER = \"openai\";\nconst DEFAULT_ANTHROPIC_PROVIDER = \"anthropic\";\n/** Header that carries the Coder token to AI Gateway in bring-your-own-key mode. */\nconst CODER_TOKEN_HEADER = \"X-Coder-AI-Governance-Token\";\n\nexport interface CoderProviderSettings {\n /**\n * Base URL of your Coder deployment, e.g. `https://coder.example.com`. Do NOT\n * include the AI Gateway path (`/api/v2/aibridge/...`) — it is appended for you.\n */\n baseURL: string;\n /**\n * The credential sent to AI Gateway.\n *\n * - **Centralized mode (default):** your **Coder API token**. AI Gateway holds\n * the upstream provider keys and brokers the call, so this is all you need.\n * - **BYOK mode** (when {@link CoderProviderSettings.coderToken} is also set):\n * your **upstream provider key**, which AI Gateway forwards to the upstream.\n */\n apiKey?: string;\n /**\n * Enables bring-your-own-key (BYOK) mode. This Coder API token is sent in the\n * `X-Coder-AI-Governance-Token` header to authenticate you to the gateway,\n * while {@link CoderProviderSettings.apiKey} carries your upstream provider key.\n * Leave unset for the default centralized mode.\n */\n coderToken?: string;\n /** Extra headers merged into every request to both surfaces. */\n headers?: Record<string, string>;\n /**\n * Mount path of AI Gateway on the deployment. Defaults to `/api/v2/aibridge`.\n * Exposed because the `aibridge` path segment may change in a future Coder\n * release (the feature was renamed \"AI Gateway\").\n */\n aiGatewayPath?: string;\n /**\n * Override the provider path segments — the *admin-configured provider names*\n * on the deployment (e.g. `anthropic-corp`). Default to `openai` / `anthropic`.\n */\n providers?: {\n openai?: string;\n anthropic?: string;\n };\n /** Custom fetch implementation (useful for testing or middleware). */\n fetch?: typeof globalThis.fetch;\n}\n\nexport interface CoderProvider {\n /** Route a model id to a surface by heuristic (Claude ids → Anthropic surface). */\n (modelId: string): LanguageModelV3;\n /** Route a model id to a surface by heuristic (Claude ids → Anthropic surface). */\n languageModel(modelId: string): LanguageModelV3;\n /**\n * AI Gateway's OpenAI-compatible surface (`/aibridge/openai/v1`). Reaches the\n * OpenAI / Azure / Google / OpenRouter / Vercel / openai-compat and Copilot\n * upstreams. Routing is by URL, so any model id sent here hits this surface.\n */\n openai: OpenAICompatibleProvider;\n /**\n * AI Gateway's Anthropic-compatible surface (`/aibridge/anthropic`). Reaches\n * native Claude and Bedrock-hosted Claude.\n */\n anthropic: AnthropicProvider;\n /** Shorthand for an {@link CoderProvider.openai} chat model. */\n chat(modelId: string): LanguageModelV3;\n /** Shorthand for an {@link CoderProvider.anthropic} messages model. */\n messages(modelId: string): LanguageModelV3;\n /** Text-embedding model via the OpenAI-compatible surface. */\n textEmbeddingModel(modelId: string): EmbeddingModelV3;\n}\n\n/**\n * Heuristic for the bare {@link CoderProvider} call: which AI Gateway surface\n * should a model id route to? Claude / Anthropic ids go to the Anthropic\n * surface; everything else to the OpenAI-compatible surface. Use the explicit\n * `.openai(id)` / `.anthropic(id)` accessors to override (e.g. to reach Claude\n * through a Copilot-typed provider on the OpenAI surface).\n */\nexport function isAnthropicModelId(modelId: string): boolean {\n return /^(?:claude|anthropic[./:])/i.test(modelId);\n}\n\nfunction trimTrailingSlash(url: string): string {\n return url.replace(/\\/+$/, \"\");\n}\n\n/**\n * Create a {@link CoderProvider} that routes Vercel AI SDK calls through a Coder\n * deployment's AI Gateway (formerly \"AI Bridge\"). AI Gateway exposes two\n * provider-namespaced surfaces; this provider fronts both and selects between\n * them per model.\n *\n * @example\n * ```ts\n * import { generateText } from \"ai\";\n * import { createCoder } from \"@coder/ai-sdk-provider\";\n *\n * const coder = createCoder({\n * baseURL: \"https://coder.example.com\",\n * apiKey: process.env.CODER_API_TOKEN!,\n * });\n *\n * await generateText({ model: coder(\"gpt-4o\"), prompt: \"Hi\" }); // OpenAI surface\n * await generateText({ model: coder(\"claude-sonnet-4-6\"), prompt: \"Hi\" }); // Anthropic surface\n * ```\n */\nexport function createCoder(settings: CoderProviderSettings): CoderProvider {\n if (!settings.baseURL) {\n throw new Error(\n \"createCoder: `baseURL` is required (your Coder deployment URL, e.g. https://coder.example.com).\",\n );\n }\n\n const deployment = trimTrailingSlash(settings.baseURL);\n const gatewayPath = settings.aiGatewayPath ?? DEFAULT_AI_GATEWAY_PATH;\n const openaiName = settings.providers?.openai ?? DEFAULT_OPENAI_PROVIDER;\n const anthropicName = settings.providers?.anthropic ?? DEFAULT_ANTHROPIC_PROVIDER;\n\n // Both sub-providers append their route to a baseURL that INCLUDES `/v1`:\n // openai-compatible POSTs `${baseURL}/chat/completions`, and @ai-sdk/anthropic\n // POSTs `${baseURL}/messages`. AI Gateway's intercepted routes are\n // `/aibridge/<name>/v1/chat/completions` and `/aibridge/<name>/v1/messages`.\n const openaiBaseURL = `${deployment}${gatewayPath}/${openaiName}/v1`;\n const anthropicBaseURL = `${deployment}${gatewayPath}/${anthropicName}/v1`;\n\n // BYOK mode: the Coder token authenticates via a dedicated header and `apiKey`\n // carries the upstream key. Centralized mode (default): `apiKey` is the Coder\n // token itself, sent in the standard provider auth header.\n const byok = settings.coderToken !== undefined;\n const headers: Record<string, string> = {\n ...(byok ? { [CODER_TOKEN_HEADER]: settings.coderToken as string } : {}),\n ...settings.headers,\n };\n\n const openai = createOpenAICompatible({\n name: \"coder.openai\",\n baseURL: openaiBaseURL,\n apiKey: settings.apiKey, // → `Authorization: Bearer <apiKey>`\n headers,\n fetch: settings.fetch,\n includeUsage: true,\n });\n\n const anthropic = createAnthropic({\n name: \"coder.anthropic\",\n baseURL: anthropicBaseURL,\n // Centralized: send the Coder token via `Authorization: Bearer` (the\n // documented path). BYOK: send the upstream key via `x-api-key`.\n ...(byok ? { apiKey: settings.apiKey } : { authToken: settings.apiKey }),\n headers,\n fetch: settings.fetch,\n });\n\n const languageModel = (modelId: string): LanguageModelV3 =>\n isAnthropicModelId(modelId) ? anthropic(modelId) : openai(modelId);\n\n return Object.assign(languageModel, {\n languageModel,\n openai,\n anthropic,\n chat: (modelId: string): LanguageModelV3 => openai(modelId),\n messages: (modelId: string): LanguageModelV3 => anthropic(modelId),\n textEmbeddingModel: (modelId: string): EmbeddingModelV3 => openai.textEmbeddingModel(modelId),\n });\n}\n\n/** Convenience alias mirroring other AI SDK providers' `createX` naming. */\nexport const coder = createCoder;\n"],"mappings":";AAAA,SAAiC,uBAAuB;AACxD,SAAS,8BAA6D;AAItE,IAAM,0BAA0B;AAEhC,IAAM,0BAA0B;AAChC,IAAM,6BAA6B;AAEnC,IAAM,qBAAqB;AA2EpB,SAAS,mBAAmB,SAA0B;AAC3D,SAAO,8BAA8B,KAAK,OAAO;AACnD;AAEA,SAAS,kBAAkB,KAAqB;AAC9C,SAAO,IAAI,QAAQ,QAAQ,EAAE;AAC/B;AAsBO,SAAS,YAAY,UAAgD;AAC1E,MAAI,CAAC,SAAS,SAAS;AACrB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,QAAM,aAAa,kBAAkB,SAAS,OAAO;AACrD,QAAM,cAAc,SAAS,iBAAiB;AAC9C,QAAM,aAAa,SAAS,WAAW,UAAU;AACjD,QAAM,gBAAgB,SAAS,WAAW,aAAa;AAMvD,QAAM,gBAAgB,GAAG,UAAU,GAAG,WAAW,IAAI,UAAU;AAC/D,QAAM,mBAAmB,GAAG,UAAU,GAAG,WAAW,IAAI,aAAa;AAKrE,QAAM,OAAO,SAAS,eAAe;AACrC,QAAM,UAAkC;AAAA,IACtC,GAAI,OAAO,EAAE,CAAC,kBAAkB,GAAG,SAAS,WAAqB,IAAI,CAAC;AAAA,IACtE,GAAG,SAAS;AAAA,EACd;AAEA,QAAM,SAAS,uBAAuB;AAAA,IACpC,MAAM;AAAA,IACN,SAAS;AAAA,IACT,QAAQ,SAAS;AAAA;AAAA,IACjB;AAAA,IACA,OAAO,SAAS;AAAA,IAChB,cAAc;AAAA,EAChB,CAAC;AAED,QAAM,YAAY,gBAAgB;AAAA,IAChC,MAAM;AAAA,IACN,SAAS;AAAA;AAAA;AAAA,IAGT,GAAI,OAAO,EAAE,QAAQ,SAAS,OAAO,IAAI,EAAE,WAAW,SAAS,OAAO;AAAA,IACtE;AAAA,IACA,OAAO,SAAS;AAAA,EAClB,CAAC;AAED,QAAM,gBAAgB,CAAC,YACrB,mBAAmB,OAAO,IAAI,UAAU,OAAO,IAAI,OAAO,OAAO;AAEnE,SAAO,OAAO,OAAO,eAAe;AAAA,IAClC;AAAA,IACA;AAAA,IACA;AAAA,IACA,MAAM,CAAC,YAAqC,OAAO,OAAO;AAAA,IAC1D,UAAU,CAAC,YAAqC,UAAU,OAAO;AAAA,IACjE,oBAAoB,CAAC,YAAsC,OAAO,mBAAmB,OAAO;AAAA,EAC9F,CAAC;AACH;AAGO,IAAM,QAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coder/ai-sdk-provider",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Vercel AI SDK provider for Coder AI Gateway. Point the AI SDK at your Coder deployment with a Coder API token and use any model your deployment proxies — across AI Gateway's OpenAI- and Anthropic-compatible surfaces.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"ai",
|
|
7
|
+
"ai-gateway",
|
|
8
|
+
"ai-sdk",
|
|
9
|
+
"aibridge",
|
|
10
|
+
"anthropic",
|
|
11
|
+
"coder",
|
|
12
|
+
"llm",
|
|
13
|
+
"openai",
|
|
14
|
+
"provider",
|
|
15
|
+
"vercel-ai-sdk"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/coder/ai-sdk/tree/main/packages/provider#readme",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/coder/ai-sdk/issues"
|
|
20
|
+
},
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"author": "Coder Technologies, Inc. (https://coder.com)",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/coder/ai-sdk.git",
|
|
26
|
+
"directory": "packages/provider"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"main": "./dist/index.js",
|
|
35
|
+
"module": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"exports": {
|
|
38
|
+
"./package.json": "./package.json",
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"import": "./dist/index.js",
|
|
42
|
+
"default": "./dist/index.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public",
|
|
47
|
+
"provenance": true
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@ai-sdk/anthropic": "^3.0.85",
|
|
51
|
+
"@ai-sdk/openai-compatible": "^2.0.51",
|
|
52
|
+
"@ai-sdk/provider": "3.0.10"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@arethetypeswrong/cli": "^0.18.0",
|
|
56
|
+
"@types/node": "^25.9.3",
|
|
57
|
+
"@vitest/coverage-v8": "^4.1.9",
|
|
58
|
+
"ai": "^6.0.206",
|
|
59
|
+
"publint": "^0.3.0",
|
|
60
|
+
"tsup": "^8.5.1",
|
|
61
|
+
"tsx": "^4.22.4",
|
|
62
|
+
"typescript": "^6.0.3",
|
|
63
|
+
"vitest": "^4.1.9",
|
|
64
|
+
"zod": "4.4.3"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"ai": "^6.0.0",
|
|
68
|
+
"zod": "^3.25.76 || ^4.1.8"
|
|
69
|
+
},
|
|
70
|
+
"peerDependenciesMeta": {
|
|
71
|
+
"zod": {
|
|
72
|
+
"optional": true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
"engines": {
|
|
76
|
+
"node": ">=20"
|
|
77
|
+
},
|
|
78
|
+
"scripts": {
|
|
79
|
+
"build": "tsup",
|
|
80
|
+
"typecheck": "tsc --noEmit",
|
|
81
|
+
"test": "vitest run",
|
|
82
|
+
"test:watch": "vitest",
|
|
83
|
+
"test:coverage": "vitest run --coverage",
|
|
84
|
+
"attw": "attw --pack --ignore-rules cjs-resolves-to-esm",
|
|
85
|
+
"example:generate": "tsx examples/01-generate.ts",
|
|
86
|
+
"example:stream": "tsx examples/02-stream.ts",
|
|
87
|
+
"example:anthropic": "tsx examples/03-anthropic.ts"
|
|
88
|
+
}
|
|
89
|
+
}
|