@tansy/ai-opencode 0.0.0-stage → 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 +21 -0
- package/README.md +12 -2
- package/index.d.ts +10 -0
- package/index.js +90 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mohamed Salah
|
|
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,13 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- Generated by scripts/package-metadata.mjs. Change that script, not this file. -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# @tansy/ai-opencode
|
|
4
|
+
|
|
5
|
+
OpenCode for @tansy/ai: Claude, GPT, Gemini and open models with one OpenCode key.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tansy/ai-opencode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Add it to `tansy.config.ts`. See [the ai plugin documentation](https://github.com/mhmdsalahsebai/tansy/blob/main/docs/plugins/ai.md) for what it adds and the options it takes.
|
|
12
|
+
|
|
13
|
+
A first-party plugin for [tansy](https://github.com/mhmdsalahsebai/tansy), a self-hosted, runtime-schema CMS for Next.js. MIT licensed.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenCode's pay-as-you-go models (opencode.ai/console), with one key. Each family is asked
|
|
3
|
+
* through its own API, as OpenCode's model list names it: Claude through Anthropic's
|
|
4
|
+
* Messages, GPT, Grok and Muse through OpenAI's Responses, Gemini through Google's, and the
|
|
5
|
+
* open models (GLM, Kimi, Qwen, DeepSeek, MiniMax…) through OpenAI's chat API, which
|
|
6
|
+
* OpenCode also translates for any text model.
|
|
7
|
+
*/
|
|
8
|
+
export declare const opencode: (options?: {
|
|
9
|
+
defaultModel?: string;
|
|
10
|
+
}) => import("@tansy/ai/provider").AiProvider;
|
package/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
2
|
+
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
3
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
4
|
+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
5
|
+
import { defineAiProvider, openAiModels, str } from "@tansy/ai/provider";
|
|
6
|
+
const BASE_URL = "https://opencode.ai/inference";
|
|
7
|
+
/** Media types as OpenCode lists them, such as "image/png" or "application/pdf", as modalities. */
|
|
8
|
+
const modalitiesOf = (value) => {
|
|
9
|
+
if (!Array.isArray(value))
|
|
10
|
+
return undefined;
|
|
11
|
+
const found = new Set();
|
|
12
|
+
for (const item of value) {
|
|
13
|
+
if (typeof item !== "string")
|
|
14
|
+
continue;
|
|
15
|
+
const kind = item.split("/")[0];
|
|
16
|
+
if (kind === "text" || kind === "image" || kind === "audio" || kind === "video")
|
|
17
|
+
found.add(kind);
|
|
18
|
+
else if (kind === "application")
|
|
19
|
+
found.add("file");
|
|
20
|
+
}
|
|
21
|
+
return [...found];
|
|
22
|
+
};
|
|
23
|
+
/** A model OpenCode prices at nothing, which only its own app may use. */
|
|
24
|
+
const isFree = (model) => {
|
|
25
|
+
const text = (model.cost ?? {}).text;
|
|
26
|
+
return /-free$/.test(String(model.id)) || (text?.input?.price === 0 && text?.output?.price === 0);
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* OpenCode's pay-as-you-go models (opencode.ai/console), with one key. Each family is asked
|
|
30
|
+
* through its own API, as OpenCode's model list names it: Claude through Anthropic's
|
|
31
|
+
* Messages, GPT, Grok and Muse through OpenAI's Responses, Gemini through Google's, and the
|
|
32
|
+
* open models (GLM, Kimi, Qwen, DeepSeek, MiniMax…) through OpenAI's chat API, which
|
|
33
|
+
* OpenCode also translates for any text model.
|
|
34
|
+
*/
|
|
35
|
+
export const opencode = (options = {}) => defineAiProvider({
|
|
36
|
+
id: "opencode",
|
|
37
|
+
label: "OpenCode",
|
|
38
|
+
icon: "opencode",
|
|
39
|
+
defaultModel: options.defaultModel ?? "claude-haiku-5-5",
|
|
40
|
+
// Its settings in other interface languages. English is each setting's own label and description.
|
|
41
|
+
messages: {
|
|
42
|
+
ar: {
|
|
43
|
+
settings: {
|
|
44
|
+
opencodeApiKey: {
|
|
45
|
+
label: "مفتاح API لـ OpenCode",
|
|
46
|
+
description: "من opencode.ai/console ← API keys."
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
settings: () => [
|
|
52
|
+
{ name: "opencodeApiKey", type: "secret", env: "OPENCODE_API_KEY", label: "OpenCode API key", description: "From opencode.ai/console → API keys." },
|
|
53
|
+
],
|
|
54
|
+
ready: (settings) => Boolean(str(settings.opencodeApiKey)),
|
|
55
|
+
model: (settings, modelId) => {
|
|
56
|
+
const apiKey = str(settings.opencodeApiKey);
|
|
57
|
+
if (/^claude-/.test(modelId))
|
|
58
|
+
return createAnthropic({ baseURL: `${BASE_URL}/anthropic/v1`, apiKey })(modelId);
|
|
59
|
+
if (/^(gpt-|grok-|muse-)/.test(modelId))
|
|
60
|
+
return createOpenAI({ baseURL: `${BASE_URL}/openai/v1`, apiKey }).responses(modelId);
|
|
61
|
+
if (/^gemini-/.test(modelId))
|
|
62
|
+
return createGoogleGenerativeAI({ baseURL: `${BASE_URL}/google/v1beta`, apiKey })(modelId);
|
|
63
|
+
return createOpenAICompatible({ name: "opencode", baseURL: `${BASE_URL}/openai/v1`, apiKey })(modelId);
|
|
64
|
+
},
|
|
65
|
+
// What each reads and writes, and whether it calls tools, as OpenCode's model list reports it.
|
|
66
|
+
// Its free models answer only the OpenCode app itself, so they're left out.
|
|
67
|
+
models: async (settings) => {
|
|
68
|
+
const free = new Set();
|
|
69
|
+
const models = await openAiModels(`${BASE_URL}/v1`, str(settings.opencodeApiKey) || undefined, {
|
|
70
|
+
label: (model) => {
|
|
71
|
+
if (isFree(model))
|
|
72
|
+
free.add(model.id);
|
|
73
|
+
return typeof model.name === "string" ? model.name : undefined;
|
|
74
|
+
},
|
|
75
|
+
capabilities: (model) => {
|
|
76
|
+
const capabilities = (model.capabilities ?? {});
|
|
77
|
+
const io = (model.modalities ?? {});
|
|
78
|
+
return {
|
|
79
|
+
input: modalitiesOf(io.input),
|
|
80
|
+
output: modalitiesOf(io.output),
|
|
81
|
+
tools: capabilities.tools?.supported === true,
|
|
82
|
+
reasoning: capabilities.reasoning?.supported === true,
|
|
83
|
+
structured: capabilities.structured_output === true,
|
|
84
|
+
reported: true,
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
return models.filter((model) => !free.has(model.value));
|
|
89
|
+
},
|
|
90
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/ai-opencode",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenCode for @tansy/ai: Claude, GPT, Gemini and open models with one OpenCode key.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tansy",
|
|
7
|
+
"tansy-ai-provider"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Mohamed Salah <mohamedsalahsebai@gmail.com>",
|
|
11
|
+
"homepage": "https://github.com/mhmdsalahsebai/tansy",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/mhmdsalahsebai/tansy.git",
|
|
15
|
+
"directory": "plugins/ai-opencode"
|
|
16
|
+
},
|
|
17
|
+
"bugs": "https://github.com/mhmdsalahsebai/tansy/issues",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./index.d.ts",
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"@tansy/ai": "^0.1.0",
|
|
26
|
+
"tansy": "^0.1.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@ai-sdk/anthropic": "^4.0.105",
|
|
30
|
+
"@ai-sdk/google": "^4.0.121",
|
|
31
|
+
"@ai-sdk/openai": "^4.0.118",
|
|
32
|
+
"@ai-sdk/openai-compatible": "^3.0.92"
|
|
33
|
+
}
|
|
34
|
+
}
|