opencode-custom-api 0.0.0-stage → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +186 -2
- package/dist/autodiscovery.d.ts +16 -0
- package/dist/autodiscovery.d.ts.map +1 -0
- package/dist/autodiscovery.js +45 -0
- package/dist/autodiscovery.js.map +1 -0
- package/dist/cache.d.ts +11 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +40 -0
- package/dist/cache.js.map +1 -0
- package/dist/discovery.d.ts +6 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +62 -0
- package/dist/discovery.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/normalize.d.ts +5 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +47 -0
- package/dist/normalize.js.map +1 -0
- package/dist/plugin.d.ts +6 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +172 -0
- package/dist/plugin.js.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +49 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OpenCode community
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,187 @@
|
|
|
1
|
-
#
|
|
1
|
+
# opencode-custom-api
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An OpenCode V2 server plugin that discovers chat models from self-hosted or third-party OpenAI-compatible and Anthropic-compatible endpoints and registers them as native OpenCode providers.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- OpenCode with `@opencode/plugin` V2 (this release targets 2.3.x)
|
|
8
|
+
- Node.js 20+ when building or testing the npm package
|
|
9
|
+
- A model-list endpoint returning an array, `{ "data": [] }`, `{ "models": [] }`, or `{ "models": { "id": {...} } }`
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
After the package is published to npm, install it with:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install opencode-custom-api
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
For local development, clone this repository and run `npm install && npm run build`. Then use the plugin directory path in `opencode.jsonc`; paths may be relative to the config file, absolute, or `file://` URLs. A local directory with this package's `name` and `exports` is loaded through those exports:
|
|
20
|
+
|
|
21
|
+
```jsonc
|
|
22
|
+
{
|
|
23
|
+
"$schema": "https://opencode.ai/config.json",
|
|
24
|
+
"plugins": [
|
|
25
|
+
{
|
|
26
|
+
"package": "./path/to/opencode-custom-api",
|
|
27
|
+
"options": { "endpoints": [] }
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Configuration
|
|
34
|
+
|
|
35
|
+
Credentials are **references to environment variables**, never literal API keys:
|
|
36
|
+
|
|
37
|
+
With an empty Options field, the plugin automatically inspects OpenCode's merged provider registry. It discovers models for custom providers using `@opencode/ai/providers/openai-compatible`, `@opencode/ai/providers/anthropic-compatible`, or OpenChamber's legacy `aisdk:@ai-sdk/openai` runtime when `settings.baseURL` is present. Explicit plugin endpoints remain available and override an auto-detected provider with the same ID.
|
|
38
|
+
|
|
39
|
+
Zero-options example:
|
|
40
|
+
|
|
41
|
+
```jsonc
|
|
42
|
+
{
|
|
43
|
+
"$schema": "https://opencode.ai/config.json",
|
|
44
|
+
"plugins": ["opencode-custom-api"],
|
|
45
|
+
"providers": {
|
|
46
|
+
"acme": {
|
|
47
|
+
"name": "Acme",
|
|
48
|
+
"env": ["ACME_API_KEY"],
|
|
49
|
+
"package": "@opencode/ai/providers/openai-compatible",
|
|
50
|
+
"settings": {
|
|
51
|
+
"baseURL": "https://api.example.com/v1",
|
|
52
|
+
"apiKey": "{env:ACME_API_KEY}"
|
|
53
|
+
},
|
|
54
|
+
"models": {}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
`settings.apiKey` may use `{env:NAME}` or `{file:path}`. OpenCode resolves these references before exposing provider metadata to the plugin; the plugin does not read arbitrary files itself. If the provider has an active credential saved through `/connect`, that credential takes precedence and is resolved again on refresh. Secrets are used only in memory for discovery requests and are never written to plugin storage.
|
|
61
|
+
|
|
62
|
+
OpenChamber's provider connection UI stores saved OpenCode credentials in OpenCode's credential store (on Linux, normally `~/.local/share/opencode/opencode.db`). This plugin never opens that database; it uses the public `ctx.integration.connection.active()` and `resolve()` APIs so storage format and access control remain OpenCode's responsibility.
|
|
63
|
+
|
|
64
|
+
```jsonc
|
|
65
|
+
{
|
|
66
|
+
"$schema": "https://opencode.ai/config.json",
|
|
67
|
+
"plugins": [
|
|
68
|
+
{
|
|
69
|
+
"package": "opencode-custom-api",
|
|
70
|
+
"options": {
|
|
71
|
+
"endpoints": [
|
|
72
|
+
{
|
|
73
|
+
"id": "local-openai",
|
|
74
|
+
"name": "Local OpenAI API",
|
|
75
|
+
"baseURL": "http://127.0.0.1:8000/v1",
|
|
76
|
+
"protocol": "openai-compatible",
|
|
77
|
+
"apiKey": { "env": "LOCAL_OPENAI_API_KEY" },
|
|
78
|
+
"timeoutMs": 10000,
|
|
79
|
+
"cacheTtlMs": 300000,
|
|
80
|
+
"refreshIntervalMs": 0,
|
|
81
|
+
"headers": { "X-Tenant": { "env": "TENANT_ID" } },
|
|
82
|
+
"catalog": {
|
|
83
|
+
"my-model": {
|
|
84
|
+
"context": 131072,
|
|
85
|
+
"max_output_tokens": 8192,
|
|
86
|
+
"capabilities": {
|
|
87
|
+
"input": ["text", "image"],
|
|
88
|
+
"tools": true,
|
|
89
|
+
"reasoning": false
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
"id": "private-anthropic",
|
|
96
|
+
"baseURL": "https://gateway.example.com",
|
|
97
|
+
"protocol": "anthropic-compatible",
|
|
98
|
+
"apiKey": { "env": "PRIVATE_ANTHROPIC_KEY" }
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Endpoint options
|
|
108
|
+
|
|
109
|
+
| Option | Required | Default | Meaning |
|
|
110
|
+
|---|---:|---:|---|
|
|
111
|
+
| `id` | yes | — | Unique provider ID (`a-z`, digits, `.`, `_`, `-`) |
|
|
112
|
+
| `baseURL` | yes | — | HTTP(S) provider base URL |
|
|
113
|
+
| `protocol` | yes | — | `openai-compatible` or `anthropic-compatible` |
|
|
114
|
+
| `name` | no | `id` | Display name |
|
|
115
|
+
| `apiKey` | no | — | `{ "env": "VARIABLE_NAME" }` only |
|
|
116
|
+
| `headers` | no | `{}` | Static strings or env references; avoid literal secrets |
|
|
117
|
+
| `timeoutMs` | no | `10000` | Per discovery request and runtime provider timeout |
|
|
118
|
+
| `cacheTtlMs` | no | `300000` | Fresh-cache lifetime |
|
|
119
|
+
| `refreshIntervalMs` | no | `0` | Background refresh interval; zero/absent disables it |
|
|
120
|
+
| `catalog` | no | `{}` | Small, endpoint-specific metadata enrichment map keyed by model ID |
|
|
121
|
+
|
|
122
|
+
## Discovery and normalization
|
|
123
|
+
|
|
124
|
+
- If `baseURL` ends in `/v1`, discovery requests `/models` beneath it. Otherwise it tries `/v1/models`, then `/models`.
|
|
125
|
+
- OpenAI-compatible discovery uses `Authorization: Bearer ...`; Anthropic-compatible discovery uses `x-api-key` plus `anthropic-version`.
|
|
126
|
+
- Requests have a timeout and accept cancellation. Cleanup aborts in-flight scheduled refreshes.
|
|
127
|
+
- IDs/names and common context, output, modalities, tools, and reasoning fields are parsed.
|
|
128
|
+
- Image-generation-only entries are excluded; multimodal models that also produce text remain available.
|
|
129
|
+
- Metadata precedence is: explicit endpoint metadata (including explicit `false`) → exact or uniquely normalized endpoint catalog key → narrow conservative heuristic/default.
|
|
130
|
+
- Normalized catalog matching only lowercases, removes a leading `models/`, and normalizes spaces/underscores to hyphens. It does not use substring/fuzzy matching.
|
|
131
|
+
- Conservative defaults are 32,768 context tokens, 4,096 output tokens, text input, no tools, and no reasoning. Only IDs beginning with `o1`…`o9`, or containing the complete word `reasoner`/`reasoning`, infer reasoning.
|
|
132
|
+
|
|
133
|
+
OpenCode V2 already integrates models.dev. This plugin deliberately does **not** download or duplicate its full catalog because the plugin API does not expose a stable safe catalog lookup. The optional per-endpoint `catalog` supplies trusted enrichment. Internally each field records endpoint/catalog/heuristic/default provenance. V2 2.3.x has no `capabilities.reasoning` field, so reasoning is adapted through the official `compatibility.requireReasoning` model field; input and tools map directly to V2 capabilities.
|
|
134
|
+
|
|
135
|
+
## Cache and refresh
|
|
136
|
+
|
|
137
|
+
Last successful inventories are stored with a version and timestamp using `ctx.storage`; no filesystem path is hard-coded. Credentials and request headers are not cached. A valid fresh entry avoids HTTP. On an expired-cache refresh failure, the last successful inventory is used as stale fallback. An explicitly configured plugin endpoint fails setup when neither discovery nor cache is available; an auto-detected provider keeps its existing configured models so one unavailable endpoint does not prevent the plugin from loading.
|
|
138
|
+
|
|
139
|
+
Manual refresh interfaces:
|
|
140
|
+
|
|
141
|
+
- Command: `/custom-api-refresh` refreshes all endpoints.
|
|
142
|
+
- Tool: `custom_api_refresh` refreshes all endpoints or one optional endpoint ID and cooperates with session cancellation.
|
|
143
|
+
|
|
144
|
+
Refresh resolves the current active provider credential again, updates captured source data, then calls the official provider and model reload APIs. External HTTP work is always completed before synchronous transform callbacks. For auto-detected providers, discovered models are added while explicitly configured models win on ID collisions.
|
|
145
|
+
|
|
146
|
+
## Security
|
|
147
|
+
|
|
148
|
+
- Put secrets in environment variables and use `{ "env": "..." }`. The plugin never writes resolved keys to storage.
|
|
149
|
+
- Static header strings are supported for non-secret routing values, but config files may be committed; use env references for anything sensitive.
|
|
150
|
+
- Prefer HTTPS for remote endpoints. HTTP is allowed for loopback/private development.
|
|
151
|
+
- Discovery errors can contain URLs and HTTP statuses, but never include request headers.
|
|
152
|
+
|
|
153
|
+
## Limitations
|
|
154
|
+
|
|
155
|
+
- Compatibility endpoints vary; discovery requires one of the documented response shapes.
|
|
156
|
+
- Metadata names are not standardized. Unknown fields are ignored and conservative defaults apply.
|
|
157
|
+
- The plugin cannot safely query OpenCode's private models.dev internals and intentionally avoids private APIs or monkey patching.
|
|
158
|
+
- Saved `/connect` credentials are resolved again on refresh. Environment/file substitutions in provider config are resolved by OpenCode when it reloads configuration.
|
|
159
|
+
- The refresh command has no arguments; use the tool when refreshing a single endpoint.
|
|
160
|
+
|
|
161
|
+
## Development
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
npm test
|
|
165
|
+
npm run typecheck
|
|
166
|
+
npm run build
|
|
167
|
+
npm pack --dry-run
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Publishing
|
|
171
|
+
|
|
172
|
+
The GitHub Actions workflow at `.github/workflows/publish.yml` publishes the existing [`opencode-custom-api`](https://www.npmjs.com/package/opencode-custom-api) package through npm Trusted Publishing (OIDC). It uses no npm token or repository secret. For release-triggered runs, the release tag (for example `v0.1.0`) must match the version in `package.json`.
|
|
173
|
+
|
|
174
|
+
Configure the package once on npmjs.com under **Settings → Trusted Publisher → GitHub Actions**:
|
|
175
|
+
|
|
176
|
+
1. Set the organization or user to `6zev` and repository to `opencode-custom-api`.
|
|
177
|
+
2. Set the workflow filename to `publish.yml` (including the extension).
|
|
178
|
+
3. Leave the environment blank; this workflow does not use a GitHub environment.
|
|
179
|
+
4. Enable **Allow npm publish** rather than provenance-only staging.
|
|
180
|
+
|
|
181
|
+
To release, update the versions in `package.json` and `package-lock.json`, push the change, and publish a matching GitHub Release. The workflow can also be started manually with **Actions → Publish to npm → Run workflow**.
|
|
182
|
+
|
|
183
|
+
Every run uses a GitHub-hosted runner, requests `id-token: write`, installs an OIDC-capable npm CLI, tests, type-checks, builds, and publishes. npm generates provenance automatically for Trusted Publisher releases.
|
|
184
|
+
|
|
185
|
+
## License
|
|
186
|
+
|
|
187
|
+
MIT
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Model, Provider } from "@opencode/plugin";
|
|
2
|
+
import type { EndpointOptions } from "./types.js";
|
|
3
|
+
export interface AutoEndpoint {
|
|
4
|
+
endpoint: EndpointOptions;
|
|
5
|
+
integrationID?: string;
|
|
6
|
+
settingsApiKey?: string;
|
|
7
|
+
}
|
|
8
|
+
type ProviderCandidate = Pick<Provider.Info, "name" | "package" | "settings" | "headers"> & {
|
|
9
|
+
id: string;
|
|
10
|
+
integrationID?: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
export declare function autoEndpointFromProvider(provider: ProviderCandidate, excluded?: Set<string>): AutoEndpoint | undefined;
|
|
13
|
+
export declare function credentialSecret(value: unknown, fallback?: string): string | undefined;
|
|
14
|
+
export declare function mergeDiscoveredModels(discovered: readonly Model.Info[], existing: ReadonlyMap<string, Model.Info>): Model.Info[];
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=autodiscovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autodiscovery.d.ts","sourceRoot":"","sources":["../src/autodiscovery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,KAAK,EAAE,eAAe,EAAY,MAAM,YAAY,CAAA;AAQ3D,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,eAAe,CAAA;IACzB,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,cAAc,CAAC,EAAE,MAAM,CAAA;CACxB;AAED,KAAK,iBAAiB,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,UAAU,GAAG,SAAS,CAAC,GAAG;IAC1F,EAAE,EAAE,MAAM,CAAA;IACV,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACnC,CAAA;AAED,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,iBAAiB,EAAE,QAAQ,cAAoB,GAAG,YAAY,GAAG,SAAS,CAoB5H;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOtF;AAED,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,SAAS,KAAK,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,EAAE,CAIhI"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const PACKAGES = {
|
|
2
|
+
"@opencode/ai/providers/openai-compatible": "openai-compatible",
|
|
3
|
+
"@opencode/ai/providers/anthropic-compatible": "anthropic-compatible",
|
|
4
|
+
"aisdk:@ai-sdk/openai": "openai-compatible",
|
|
5
|
+
};
|
|
6
|
+
export function autoEndpointFromProvider(provider, excluded = new Set()) {
|
|
7
|
+
if (excluded.has(provider.id))
|
|
8
|
+
return undefined;
|
|
9
|
+
const protocol = PACKAGES[provider.package];
|
|
10
|
+
const baseURL = provider.settings?.baseURL;
|
|
11
|
+
if (!protocol || typeof baseURL !== "string" || !/^https?:\/\//.test(baseURL))
|
|
12
|
+
return undefined;
|
|
13
|
+
const timeout = provider.settings?.timeout;
|
|
14
|
+
const endpoint = {
|
|
15
|
+
id: provider.id,
|
|
16
|
+
name: provider.name,
|
|
17
|
+
baseURL,
|
|
18
|
+
protocol,
|
|
19
|
+
...(provider.headers ? { headers: provider.headers } : {}),
|
|
20
|
+
...(typeof timeout === "number" && Number.isFinite(timeout) && timeout > 0 ? { timeoutMs: timeout } : {}),
|
|
21
|
+
};
|
|
22
|
+
const apiKey = provider.settings?.apiKey;
|
|
23
|
+
return {
|
|
24
|
+
endpoint,
|
|
25
|
+
...(provider.integrationID ? { integrationID: provider.integrationID } : {}),
|
|
26
|
+
...(typeof apiKey === "string" && apiKey ? { settingsApiKey: apiKey } : {}),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export function credentialSecret(value, fallback) {
|
|
30
|
+
if (value && typeof value === "object") {
|
|
31
|
+
const credential = value;
|
|
32
|
+
if (credential.type === "key" && typeof credential.key === "string" && credential.key)
|
|
33
|
+
return credential.key;
|
|
34
|
+
if (credential.type === "oauth" && typeof credential.access === "string" && credential.access)
|
|
35
|
+
return credential.access;
|
|
36
|
+
}
|
|
37
|
+
return fallback || undefined;
|
|
38
|
+
}
|
|
39
|
+
export function mergeDiscoveredModels(discovered, existing) {
|
|
40
|
+
const merged = new Map(discovered.map(model => [model.id, model]));
|
|
41
|
+
for (const model of existing.values())
|
|
42
|
+
merged.set(model.id, model);
|
|
43
|
+
return [...merged.values()];
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=autodiscovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autodiscovery.js","sourceRoot":"","sources":["../src/autodiscovery.ts"],"names":[],"mappings":"AAGA,MAAM,QAAQ,GAA6B;IACzC,0CAA0C,EAAE,mBAAmB;IAC/D,6CAA6C,EAAE,sBAAsB;IACrE,sBAAsB,EAAE,mBAAmB;CAC5C,CAAA;AAaD,MAAM,UAAU,wBAAwB,CAAC,QAA2B,EAAE,WAAW,IAAI,GAAG,EAAU;IAChG,IAAI,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QAAE,OAAO,SAAS,CAAA;IAC/C,MAAM,QAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;IAC3C,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;IAC1C,IAAI,CAAC,QAAQ,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,OAAO,CAAC;QAAE,OAAO,SAAS,CAAA;IAC/F,MAAM,OAAO,GAAG,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAA;IAC1C,MAAM,QAAQ,GAAoB;QAChC,EAAE,EAAE,QAAQ,CAAC,EAAE;QACf,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,OAAO;QACP,QAAQ;QACR,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC1D,GAAG,CAAC,OAAO,OA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AC,CAAC,CAAC,CAAA;IAClE,KAAK,MAAM,KAAK,IAAI,QAAQ,CAAC,MAAM,EAAE;QAAE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,EAAE,KAAK,CAAC,CAAA;IAClE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAA;AAC7B,CAAC"}
|
package/dist/cache.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CacheEntry, DiscoveredModel } from "./types.js";
|
|
2
|
+
export interface Storage {
|
|
3
|
+
get(key: string): Promise<unknown>;
|
|
4
|
+
set(key: string, value: unknown): Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
export declare function validCache(value: unknown): value is CacheEntry;
|
|
7
|
+
export declare function loadWithCache(storage: Storage, key: string, ttl: number, loader: () => Promise<DiscoveredModel[]>, now?: number): Promise<{
|
|
8
|
+
models: DiscoveredModel[];
|
|
9
|
+
stale: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
//# sourceMappingURL=cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAE7D,MAAM,WAAW,OAAO;IAAG,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAAE;AAahH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAG9D;AACD,wBAAsB,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC,eAAe,EAAE,CAAC,EAAE,GAAG,SAAa,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,eAAe,EAAE,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC,CAYlM"}
|
package/dist/cache.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const object = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2
|
+
const positiveInteger = (value) => typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
3
|
+
const provenanceValues = new Set(["endpoint", "catalog", "heuristic", "default"]);
|
|
4
|
+
function validModel(value) {
|
|
5
|
+
if (!object(value))
|
|
6
|
+
return false;
|
|
7
|
+
if (typeof value.id !== "string" || !value.id || typeof value.name !== "string")
|
|
8
|
+
return false;
|
|
9
|
+
if (!positiveInteger(value.context) || !positiveInteger(value.output) || value.output > value.context)
|
|
10
|
+
return false;
|
|
11
|
+
if (!Array.isArray(value.input) || !value.input.every(item => typeof item === "string"))
|
|
12
|
+
return false;
|
|
13
|
+
if (typeof value.tools !== "boolean" || typeof value.reasoning !== "boolean")
|
|
14
|
+
return false;
|
|
15
|
+
if (value.type !== undefined && typeof value.type !== "string")
|
|
16
|
+
return false;
|
|
17
|
+
return object(value.provenance) && Object.values(value.provenance).every(item => typeof item === "string" && provenanceValues.has(item));
|
|
18
|
+
}
|
|
19
|
+
export function validCache(value) {
|
|
20
|
+
if (!object(value))
|
|
21
|
+
return false;
|
|
22
|
+
return value.version === 1 && typeof value.savedAt === "number" && Number.isFinite(value.savedAt) && value.savedAt >= 0 && Array.isArray(value.models) && value.models.every(validModel);
|
|
23
|
+
}
|
|
24
|
+
export async function loadWithCache(storage, key, ttl, loader, now = Date.now()) {
|
|
25
|
+
const cachedValue = await storage.get(key);
|
|
26
|
+
const cached = validCache(cachedValue) ? cachedValue : undefined;
|
|
27
|
+
if (cached && now - cached.savedAt < ttl)
|
|
28
|
+
return { models: cached.models, stale: false };
|
|
29
|
+
try {
|
|
30
|
+
const models = await loader();
|
|
31
|
+
await storage.set(key, { version: 1, savedAt: now, models });
|
|
32
|
+
return { models, stale: false };
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (cached)
|
|
36
|
+
return { models: cached.models, stale: true };
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,GAAG,CAAC,KAAc,EAAoC,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AACzI,MAAM,eAAe,GAAG,CAAC,KAAc,EAAmB,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAA;AAClI,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC,CAAA;AACjF,SAAS,UAAU,CAAC,KAAc;IAChC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IAChC,IAAI,OAAO,KAAK,CAAC,EAAE,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC7F,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,OAAO;QAAE,OAAO,KAAK,CAAA;IACnH,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,C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W,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAA;IAChE,IAAI,MAAM,IAAI,GAAG,GAAG,MAAM,CAAC,OAAO,GAAG,GAAG;QAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,CAAA;IACxF,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,MAAM,EAAE,CAAA;QAC7B,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAuB,CAAC,CAAA;QACjF,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,CAAA;IACjC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,MAAM;YAAE,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,CAAA;QACzD,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { EndpointOptions, ModelMetadata } from "./types.js";
|
|
2
|
+
export declare function discoveryURLs(baseURL: string): string[];
|
|
3
|
+
export declare function resolveHeaders(endpoint: EndpointOptions, credential?: string): Record<string, string>;
|
|
4
|
+
export declare function parseModels(payload: unknown): ModelMetadata[];
|
|
5
|
+
export declare function discover(endpoint: EndpointOptions, signal?: AbortSignal, fetcher?: typeof fetch, credential?: string): Promise<ModelMetadata[]>;
|
|
6
|
+
//# sourceMappingURL=discovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery.d.ts","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAIhE,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,CAGvD;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,eAAe,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAcrG;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,aAAa,EAAE,CAM7D;AAUD,wBAAsB,QAAQ,CAAC,QAAQ,EAAE,eAAe,EAAE,MAAM,CAAC,EAAE,WAAW,EAAE,OAAO,GAAE,OAAO,KAAa,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC,CAc5J"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const object = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2
|
+
export function discoveryURLs(baseURL) {
|
|
3
|
+
const base = baseURL.replace(/\/+$/, "");
|
|
4
|
+
return /\/v1$/i.test(base) ? [`${base}/models`] : [`${base}/v1/models`, `${base}/models`];
|
|
5
|
+
}
|
|
6
|
+
export function resolveHeaders(endpoint, credential) {
|
|
7
|
+
const result = { Accept: "application/json" };
|
|
8
|
+
for (const [key, source] of Object.entries(endpoint.headers ?? {})) {
|
|
9
|
+
const header = typeof source === "string" ? source : process.env[source.env];
|
|
10
|
+
if (header !== undefined)
|
|
11
|
+
result[key] = header;
|
|
12
|
+
}
|
|
13
|
+
const value = credential ?? (endpoint.apiKey ? process.env[endpoint.apiKey.env] : undefined);
|
|
14
|
+
if (value) {
|
|
15
|
+
if (endpoint.protocol === "anthropic-compatible") {
|
|
16
|
+
result["x-api-key"] = value;
|
|
17
|
+
result["anthropic-version"] = "2023-06-01";
|
|
18
|
+
}
|
|
19
|
+
else
|
|
20
|
+
result.Authorization = `Bearer ${value}`;
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
export function parseModels(payload) {
|
|
25
|
+
let value = payload;
|
|
26
|
+
if (object(payload))
|
|
27
|
+
value = payload.data ?? payload.models;
|
|
28
|
+
if (Array.isArray(value))
|
|
29
|
+
return value.filter(object);
|
|
30
|
+
if (object(value))
|
|
31
|
+
return Object.entries(value).flatMap(([id, item]) => object(item) ? [{ id, ...item }] : []);
|
|
32
|
+
throw new Error("Unsupported model-list response shape");
|
|
33
|
+
}
|
|
34
|
+
function combinedSignal(timeoutMs, signal) {
|
|
35
|
+
const controller = new AbortController();
|
|
36
|
+
const timeout = setTimeout(() => controller.abort(new Error(`Discovery timed out after ${timeoutMs}ms`)), timeoutMs);
|
|
37
|
+
const abort = () => controller.abort(signal?.reason);
|
|
38
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
39
|
+
return { signal: controller.signal, cleanup: () => { clearTimeout(timeout); signal?.removeEventListener("abort", abort); } };
|
|
40
|
+
}
|
|
41
|
+
export async function discover(endpoint, signal, fetcher = fetch, credential) {
|
|
42
|
+
const errors = [];
|
|
43
|
+
for (const url of discoveryURLs(endpoint.baseURL)) {
|
|
44
|
+
const linked = combinedSignal(endpoint.timeoutMs ?? 10_000, signal);
|
|
45
|
+
try {
|
|
46
|
+
const response = await fetcher(url, { headers: resolveHeaders(endpoint, credential), signal: linked.signal });
|
|
47
|
+
if (!response.ok)
|
|
48
|
+
throw new Error(`HTTP ${response.status}`);
|
|
49
|
+
return parseModels(await response.json());
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (signal?.aborted)
|
|
53
|
+
throw error;
|
|
54
|
+
errors.push(`${url}: ${error instanceof Error ? error.message : String(error)}`);
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
linked.cleanup();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
throw new Error(`Model discovery failed (${errors.join("; ")})`);
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=discovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../src/discovery.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,GAAG,CAAC,K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AM,IAAI,KAAK,CAAC,2BAA2B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAClE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default } from "./plugin.js";
|
|
2
|
+
export * from "./types.js";
|
|
3
|
+
export { discover, discoveryURLs, parseModels, resolveHeaders } from "./discovery.js";
|
|
4
|
+
export { normalizeModel, normalizeCatalogID, isImageOnly } from "./normalize.js";
|
|
5
|
+
export { loadWithCache, validCache } from "./cache.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AACrF,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAChF,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default } from "./plugin.js";
|
|
2
|
+
export * from "./types.js";
|
|
3
|
+
export { discover, discoveryURLs, parseModels, resolveHeaders } from "./discovery.js";
|
|
4
|
+
export { normalizeModel, normalizeCatalogID, isImageOnly } from "./normalize.js";
|
|
5
|
+
export { loadWithCache, validCache } from "./cache.js";
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AACrC,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AACrF,OAAO,EAAE,cAAc,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAChF,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DiscoveredModel, ModelMetadata } from "./types.js";
|
|
2
|
+
export declare const normalizeCatalogID: (id: string) => string;
|
|
3
|
+
export declare function isImageOnly(model: ModelMetadata): boolean;
|
|
4
|
+
export declare function normalizeModel(raw: ModelMetadata, catalog?: Record<string, ModelMetadata>): DiscoveredModel | undefined;
|
|
5
|
+
//# sourceMappingURL=normalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAKhE,eAAO,MAAM,kBAAkB,GAAI,IAAI,MAAM,WAAoE,CAAA;AAUjH,wBAAgB,WAAW,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAIzD;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,GAAG,eAAe,GAAG,SAAS,CA2BvH"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const own = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
|
2
|
+
const number = (...values) => values.find((v) => typeof v === "number" && Number.isSafeInteger(v) && v > 0);
|
|
3
|
+
const bool = (...values) => values.find((v) => typeof v === "boolean");
|
|
4
|
+
export const normalizeCatalogID = (id) => id.toLowerCase().replace(/^models\//, "").replace(/[_ ]/g, "-");
|
|
5
|
+
function catalogMatch(id, catalog) {
|
|
6
|
+
if (!catalog)
|
|
7
|
+
return undefined;
|
|
8
|
+
if (catalog[id])
|
|
9
|
+
return catalog[id];
|
|
10
|
+
const normalized = normalizeCatalogID(id);
|
|
11
|
+
const matches = Object.entries(catalog).filter(([key]) => normalizeCatalogID(key) === normalized);
|
|
12
|
+
return matches.length === 1 ? matches[0]?.[1] : undefined;
|
|
13
|
+
}
|
|
14
|
+
export function isImageOnly(model) {
|
|
15
|
+
const type = typeof model.type === "string" ? model.type.toLowerCase() : "";
|
|
16
|
+
const output = Array.isArray(model.output_modalities) ? model.output_modalities.map(String) : [];
|
|
17
|
+
return ((type === "image" || type === "image-generation") && !model.modalities?.includes("text")) || (output.length > 0 && output.includes("image") && !output.includes("text"));
|
|
18
|
+
}
|
|
19
|
+
export function normalizeModel(raw, catalog) {
|
|
20
|
+
const idValue = raw.id ?? raw.name;
|
|
21
|
+
if (typeof idValue !== "string" || !idValue || isImageOnly(raw))
|
|
22
|
+
return undefined;
|
|
23
|
+
const id = idValue;
|
|
24
|
+
const match = catalogMatch(id, catalog);
|
|
25
|
+
const endpointCaps = raw.capabilities ?? {};
|
|
26
|
+
const catalogCaps = match?.capabilities ?? {};
|
|
27
|
+
const explicitContext = number(raw.context, raw.context_length, raw.max_context_length);
|
|
28
|
+
const catalogContext = number(match?.context, match?.context_length, match?.max_context_length);
|
|
29
|
+
const explicitOutput = number(raw.output, raw.max_output_tokens);
|
|
30
|
+
const catalogOutput = number(match?.output, match?.max_output_tokens);
|
|
31
|
+
const explicitTools = bool(own(endpointCaps, "tools") ? endpointCaps.tools : undefined, own(raw, "tools") ? raw.tools : undefined, own(raw, "tool_calling") ? raw.tool_calling : undefined);
|
|
32
|
+
const catalogTools = bool(catalogCaps.tools, match?.tools, match?.tool_calling);
|
|
33
|
+
const explicitReasoning = bool(own(endpointCaps, "reasoning") ? endpointCaps.reasoning : undefined, own(raw, "reasoning") ? raw.reasoning : undefined);
|
|
34
|
+
const catalogReasoning = bool(catalogCaps.reasoning, match?.reasoning);
|
|
35
|
+
const explicitInput = Array.isArray(endpointCaps.input) ? endpointCaps.input : Array.isArray(raw.input) ? raw.input : undefined;
|
|
36
|
+
const catalogInput = Array.isArray(catalogCaps.input) ? catalogCaps.input : Array.isArray(match?.input) ? match.input : undefined;
|
|
37
|
+
const heuristicReasoning = /^(o[1-9](?:-|$)|.*\b(reasoner|reasoning)\b)/i.test(id);
|
|
38
|
+
const source = (explicit, catalogValue, fallback, heuristic) => explicit !== undefined ? [explicit, "endpoint"] : catalogValue !== undefined ? [catalogValue, "catalog"] : heuristic !== undefined ? [heuristic, "heuristic"] : [fallback, "default"];
|
|
39
|
+
const [context, contextSource] = source(explicitContext, catalogContext, 32_768);
|
|
40
|
+
const [discoveredOutput, outputSource] = source(explicitOutput, catalogOutput, 4_096);
|
|
41
|
+
const output = Math.min(discoveredOutput, context);
|
|
42
|
+
const [tools, toolsSource] = source(explicitTools, catalogTools, false);
|
|
43
|
+
const [reasoning, reasoningSource] = source(explicitReasoning, catalogReasoning, false, heuristicReasoning ? true : undefined);
|
|
44
|
+
const [input, inputSource] = source(explicitInput, catalogInput, ["text"]);
|
|
45
|
+
return { id, name: typeof raw.name === "string" ? raw.name : id, context, output, tools, reasoning, input, ...(typeof raw.type === "string" ? { type: raw.type } : {}), provenance: { context: contextSource, output: outputSource, tools: toolsSource, reasoning: reasoningSource, input: inputSource } };
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=normalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAEA,MAAM,GAAG,GAAG,CAAC,KAAa,EAAE,GAAW,EAAE,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;AAC5F,MAAM,MAAM,GAAG,CAAC,GAAG,MAAiB,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAe,E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E,CAAC,MAAM,CAAC,CAAC,CAAA;IAC1E,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,GAAG,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,CAAC,OAAO,GAAG,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,EAAE,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,CAAA;AAC5S,CAAC"}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { PluginOptions } from "./types.js";
|
|
3
|
+
export declare function parsePluginOptions(value: unknown): PluginOptions;
|
|
4
|
+
declare const _default: Plugin.Plugin;
|
|
5
|
+
export default _default;
|
|
6
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,MAAM,EAAY,MAAM,kBAAkB,CAAA;AAM1D,OAAO,KAAK,EAAoC,aAAa,EAAE,MAAM,YAAY,CAAA;AAYjF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CAmBhE;;AA6CD,wBA6FE"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Model, Plugin, Provider } from "@opencode/plugin";
|
|
2
|
+
import { discover } from "./discovery.js";
|
|
3
|
+
import { loadWithCache } from "./cache.js";
|
|
4
|
+
import { normalizeModel } from "./normalize.js";
|
|
5
|
+
import { autoEndpointFromProvider, credentialSecret, mergeDiscoveredModels } from "./autodiscovery.js";
|
|
6
|
+
const validDuration = (value, allowZero) => typeof value === "number" && Number.isFinite(value) && (allowZero ? value >= 0 : value > 0);
|
|
7
|
+
export function parsePluginOptions(value) {
|
|
8
|
+
if (value === undefined || value === null)
|
|
9
|
+
return { endpoints: [] };
|
|
10
|
+
if (typeof value !== "object")
|
|
11
|
+
throw new Error("custom-api: options must be an object");
|
|
12
|
+
if (!("endpoints" in value))
|
|
13
|
+
return { endpoints: [] };
|
|
14
|
+
if (!Array.isArray(value.endpoints))
|
|
15
|
+
throw new Error("custom-api: options.endpoints must be an array");
|
|
16
|
+
const endpoints = value.endpoints;
|
|
17
|
+
const ids = new Set();
|
|
18
|
+
for (const endpoint of endpoints) {
|
|
19
|
+
if (!endpoint || typeof endpoint.id !== "string" || !/^[a-z0-9][a-z0-9._-]*$/i.test(endpoint.id))
|
|
20
|
+
throw new Error("custom-api: each endpoint needs a valid id");
|
|
21
|
+
if (ids.has(endpoint.id))
|
|
22
|
+
throw new Error(`custom-api: duplicate endpoint id ${endpoint.id}`);
|
|
23
|
+
ids.add(endpoint.id);
|
|
24
|
+
if (typeof endpoint.baseURL !== "string" || !/^https?:\/\//.test(endpoint.baseURL))
|
|
25
|
+
throw new Error(`custom-api: ${endpoint.id}.baseURL must be an HTTP(S) URL`);
|
|
26
|
+
if (endpoint.protocol !== "openai-compatible" && endpoint.protocol !== "anthropic-compatible")
|
|
27
|
+
throw new Error(`custom-api: ${endpoint.id}.protocol is invalid`);
|
|
28
|
+
if (endpoint.apiKey && (typeof endpoint.apiKey.env !== "string" || !endpoint.apiKey.env))
|
|
29
|
+
throw new Error(`custom-api: ${endpoint.id}.apiKey must be { env: "NAME" }`);
|
|
30
|
+
if (endpoint.timeoutMs !== undefined && !validDuration(endpoint.timeoutMs, false))
|
|
31
|
+
throw new Error(`custom-api: ${endpoint.id}.timeoutMs must be finite and positive`);
|
|
32
|
+
if (endpoint.cacheTtlMs !== undefined && !validDuration(endpoint.cacheTtlMs, true))
|
|
33
|
+
throw new Error(`custom-api: ${endpoint.id}.cacheTtlMs must be finite and nonnegative`);
|
|
34
|
+
if (endpoint.refreshIntervalMs !== undefined && !validDuration(endpoint.refreshIntervalMs, true))
|
|
35
|
+
throw new Error(`custom-api: ${endpoint.id}.refreshIntervalMs must be finite and nonnegative`);
|
|
36
|
+
}
|
|
37
|
+
return { endpoints };
|
|
38
|
+
}
|
|
39
|
+
function runtimeHeaders(endpoint) {
|
|
40
|
+
const headers = {};
|
|
41
|
+
if (endpoint.apiKey) {
|
|
42
|
+
const key = process.env[endpoint.apiKey.env];
|
|
43
|
+
if (key)
|
|
44
|
+
endpoint.protocol === "anthropic-compatible" ? headers["x-api-key"] = key : headers.Authorization = `Bearer ${key}`;
|
|
45
|
+
}
|
|
46
|
+
for (const [name, reference] of Object.entries(endpoint.headers ?? {})) {
|
|
47
|
+
const value = typeof reference === "string" ? reference : process.env[reference.env];
|
|
48
|
+
if (value !== undefined)
|
|
49
|
+
headers[name] = value;
|
|
50
|
+
}
|
|
51
|
+
return headers;
|
|
52
|
+
}
|
|
53
|
+
function toModel(providerID, value) {
|
|
54
|
+
return {
|
|
55
|
+
...Model.Info.default(providerID, Model.ID.make(value.id)),
|
|
56
|
+
name: value.name,
|
|
57
|
+
type: value.type,
|
|
58
|
+
limit: { context: value.context, output: value.output },
|
|
59
|
+
capabilities: { tools: value.tools, input: value.input, output: ["text"] },
|
|
60
|
+
compatibility: value.reasoning ? { requireReasoning: true } : undefined,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async function readSource(ctx, endpoint, force = false, signal, credential) {
|
|
64
|
+
const load = async () => (await discover(endpoint, signal, fetch, credential)).map(raw => normalizeModel(raw, endpoint.catalog)).filter((item) => item !== undefined);
|
|
65
|
+
const cache = await loadWithCache({ get: key => ctx.storage.get(key), set: (key, value) => ctx.storage.set(key, value) }, `inventory/v1/${endpoint.id}`, force ? 0 : endpoint.cacheTtlMs ?? 300_000, load);
|
|
66
|
+
return { endpoint, ...cache };
|
|
67
|
+
}
|
|
68
|
+
async function resolveAutoCredential(ctx, source) {
|
|
69
|
+
if (!source.integrationID)
|
|
70
|
+
return source.settingsApiKey;
|
|
71
|
+
const connection = await ctx.integration.connection.active(source.integrationID);
|
|
72
|
+
if (!connection)
|
|
73
|
+
return source.settingsApiKey;
|
|
74
|
+
const credential = await ctx.integration.connection.resolve(connection);
|
|
75
|
+
return credentialSecret(credential, source.settingsApiKey);
|
|
76
|
+
}
|
|
77
|
+
export default Plugin.define({
|
|
78
|
+
id: "custom-api",
|
|
79
|
+
async setup(ctx) {
|
|
80
|
+
const config = parsePluginOptions(ctx.options);
|
|
81
|
+
const controllers = new Set();
|
|
82
|
+
const sources = [];
|
|
83
|
+
for (const endpoint of config.endpoints)
|
|
84
|
+
sources.push(await readSource(ctx, endpoint));
|
|
85
|
+
const configured = new Set(config.endpoints.map(endpoint => endpoint.id));
|
|
86
|
+
const providers = await ctx.provider.list();
|
|
87
|
+
for (const provider of providers.data) {
|
|
88
|
+
const auto = autoEndpointFromProvider(provider, configured);
|
|
89
|
+
if (!auto)
|
|
90
|
+
continue;
|
|
91
|
+
try {
|
|
92
|
+
const source = await readSource(ctx, auto.endpoint, false, undefined, await resolveAutoCredential(ctx, auto));
|
|
93
|
+
sources.push({ ...source, auto });
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
sources.push({ endpoint: auto.endpoint, models: [], stale: true, auto });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const providerRegistration = await ctx.provider.transform(editor => {
|
|
100
|
+
for (const source of sources) {
|
|
101
|
+
const endpoint = source.endpoint;
|
|
102
|
+
const id = Provider.ID.make(endpoint.id);
|
|
103
|
+
const discovered = source.models.map(model => toModel(id, model));
|
|
104
|
+
if (source.auto) {
|
|
105
|
+
editor.models.set(endpoint.id, mergeDiscoveredModels(discovered, editor.get(endpoint.id)?.models ?? new Map()));
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
editor.add({
|
|
109
|
+
info: {
|
|
110
|
+
...Provider.Info.empty(id),
|
|
111
|
+
name: endpoint.name ?? endpoint.id,
|
|
112
|
+
activation: "enabled",
|
|
113
|
+
package: endpoint.protocol === "anthropic-compatible" ? "@opencode/ai/providers/anthropic-compatible" : "@opencode/ai/providers/openai-compatible",
|
|
114
|
+
settings: { baseURL: endpoint.baseURL, timeout: endpoint.timeoutMs ?? 10_000 },
|
|
115
|
+
headers: runtimeHeaders(endpoint),
|
|
116
|
+
},
|
|
117
|
+
models: discovered,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
// V2 has no `capabilities.reasoning` field. Preserve discovered reasoning through
|
|
122
|
+
// the official compatibility flag in a model transform.
|
|
123
|
+
const modelRegistration = await ctx.model.transform(editor => {
|
|
124
|
+
for (const source of sources)
|
|
125
|
+
for (const model of source.models) {
|
|
126
|
+
if (!model.reasoning)
|
|
127
|
+
continue;
|
|
128
|
+
editor.update(source.endpoint.id, model.id, draft => { draft.compatibility = { ...draft.compatibility, requireReasoning: true }; });
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
const refresh = async (only, signal) => {
|
|
132
|
+
const selected = sources.filter(source => !only || source.endpoint.id === only);
|
|
133
|
+
if (only && selected.length === 0)
|
|
134
|
+
throw new Error(`Unknown endpoint: ${only}`);
|
|
135
|
+
await Promise.all(selected.map(async (source) => {
|
|
136
|
+
const credential = source.auto ? await resolveAutoCredential(ctx, source.auto) : undefined;
|
|
137
|
+
const next = await readSource(ctx, source.endpoint, true, signal, credential);
|
|
138
|
+
source.models = next.models;
|
|
139
|
+
source.stale = next.stale;
|
|
140
|
+
}));
|
|
141
|
+
await ctx.provider.reload();
|
|
142
|
+
await ctx.model.reload();
|
|
143
|
+
return selected.map(source => ({ endpoint: source.endpoint.id, models: source.models.length, stale: source.stale }));
|
|
144
|
+
};
|
|
145
|
+
const toolRegistration = await ctx.tool.transform(editor => {
|
|
146
|
+
editor.namespace({ name: "custom_api", description: "Custom API endpoint inventory management" });
|
|
147
|
+
editor.add({
|
|
148
|
+
name: "refresh",
|
|
149
|
+
description: "Refresh custom endpoint model inventories. Optionally restrict to one endpoint ID.",
|
|
150
|
+
input: { type: "object", properties: { endpoint: { type: "string" } }, additionalProperties: false },
|
|
151
|
+
options: { namespace: "custom_api", codemode: false },
|
|
152
|
+
execute: async (input, context) => ({ content: JSON.stringify(await refresh(input.endpoint, context.signal)) }),
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
const commandRegistration = await ctx.command.transform(editor => editor.add({
|
|
156
|
+
name: "custom-api-refresh",
|
|
157
|
+
description: "Refresh every custom API model inventory",
|
|
158
|
+
execute: async () => { await refresh(); },
|
|
159
|
+
}));
|
|
160
|
+
const timers = sources.flatMap(source => source.endpoint.refreshIntervalMs && source.endpoint.refreshIntervalMs > 0 ? [setInterval(() => {
|
|
161
|
+
const controller = new AbortController();
|
|
162
|
+
controllers.add(controller);
|
|
163
|
+
void refresh(source.endpoint.id, controller.signal).catch(console.error).finally(() => controllers.delete(controller));
|
|
164
|
+
}, source.endpoint.refreshIntervalMs)] : []);
|
|
165
|
+
return async () => {
|
|
166
|
+
timers.forEach(clearInterval);
|
|
167
|
+
controllers.forEach(controller => controller.abort());
|
|
168
|
+
await Promise.all([providerRegistration.dispose(), modelRegistration.dispose(), toolRegistration.dispose(), commandRegistration.dispose()]);
|
|
169
|
+
};
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAA;AAE1D,OAAO,EAAE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AAC,UAAU,CAAC,CAAC,CAAA;YACxH,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;QAE5C,OAAO,KAAK,IAAI,EAAE;YAChB,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,CAAA;YAC7B,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,CAAA;YACrD,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,oBAAoB,CAAC,OAAO,EAAE,EAAE,iBAAiB,CAAC,OAAO,EAAE,EAAE,gBAAgB,CAAC,OAAO,EAAE,EAAE,mBAAmB,CAAC,OAAO,EAAE,CAAC,CAAC,CAAA;QAC7I,CAAC,CAAA;IACH,CAAC;CACF,CAAC,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export type Protocol = "openai-compatible" | "anthropic-compatible";
|
|
2
|
+
export interface EnvReference {
|
|
3
|
+
env: string;
|
|
4
|
+
}
|
|
5
|
+
export interface CapabilityMetadata {
|
|
6
|
+
input?: string[];
|
|
7
|
+
tools?: boolean;
|
|
8
|
+
reasoning?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface ModelMetadata {
|
|
11
|
+
id?: string;
|
|
12
|
+
name?: string;
|
|
13
|
+
context?: number;
|
|
14
|
+
context_length?: number;
|
|
15
|
+
max_context_length?: number;
|
|
16
|
+
output?: number;
|
|
17
|
+
max_output_tokens?: number;
|
|
18
|
+
capabilities?: CapabilityMetadata;
|
|
19
|
+
input?: string[];
|
|
20
|
+
tools?: boolean;
|
|
21
|
+
tool_calling?: boolean;
|
|
22
|
+
reasoning?: boolean;
|
|
23
|
+
type?: string;
|
|
24
|
+
modalities?: string[];
|
|
25
|
+
output_modalities?: string[];
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
export interface EndpointOptions {
|
|
29
|
+
id: string;
|
|
30
|
+
name?: string;
|
|
31
|
+
baseURL: string;
|
|
32
|
+
protocol: Protocol;
|
|
33
|
+
apiKey?: EnvReference;
|
|
34
|
+
headers?: Record<string, string | EnvReference>;
|
|
35
|
+
timeoutMs?: number;
|
|
36
|
+
cacheTtlMs?: number;
|
|
37
|
+
refreshIntervalMs?: number;
|
|
38
|
+
catalog?: Record<string, ModelMetadata>;
|
|
39
|
+
}
|
|
40
|
+
export interface PluginOptions {
|
|
41
|
+
endpoints: EndpointOptions[];
|
|
42
|
+
}
|
|
43
|
+
export interface DiscoveredModel {
|
|
44
|
+
id: string;
|
|
45
|
+
name: string;
|
|
46
|
+
context: number;
|
|
47
|
+
output: number;
|
|
48
|
+
input: string[];
|
|
49
|
+
tools: boolean;
|
|
50
|
+
reasoning: boolean;
|
|
51
|
+
type?: string;
|
|
52
|
+
provenance: Record<string, "endpoint" | "catalog" | "heuristic" | "default">;
|
|
53
|
+
}
|
|
54
|
+
export interface CacheEntry {
|
|
55
|
+
version: 1;
|
|
56
|
+
savedAt: number;
|
|
57
|
+
models: DiscoveredModel[];
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAAG,mBAAmB,GAAG,sBAAsB,CAAA;AAEnE,MAAM,WAAW,YAAY;IAAG,GAAG,EAAE,MAAM,CAAA;CAAE;AAC7C,MAAM,WAAW,kBAAkB;IACjC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AACD,MAAM,WAAW,aAAa;IAC5B,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CA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O,CAAA;IACd,SAAS,EAAE,OAAO,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,GAAG,SAAS,GAAG,WAAW,GAAG,SAAS,CAAC,CAAA;CAC7E;AACD,MAAM,WAAW,UAAU;IAAG,OAAO,EAAE,CAAC,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,eAAe,EAAE,CAAA;CAAE"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-custom-api",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Discover custom OpenAI- and Anthropic-compatible model endpoints in OpenCode V2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "OpenCode community",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"opencode",
|
|
10
|
+
"plugin",
|
|
11
|
+
"openai",
|
|
12
|
+
"anthropic",
|
|
13
|
+
"models"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/6zev/opencode-custom-api.git"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public",
|
|
21
|
+
"registry": "https://registry.npmjs.org"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -p tsconfig.build.json",
|
|
36
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"prepublishOnly": "npm run test && npm run typecheck && npm run build"
|
|
39
|
+
},
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@opencode/plugin": "^2.3.2"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^24.10.1",
|
|
48
|
+
"typescript": "^5.9.3",
|
|
49
|
+
"vitest": "^4.0.14"
|
|
50
|
+
}
|
|
51
|
+
}
|