@tansy/ai-openrouter 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 +9 -0
- package/index.js +65 -0
- package/package.json +29 -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-openrouter
|
|
4
|
+
|
|
5
|
+
OpenRouter for @tansy/ai: hundreds of models, free ones included, with one key.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tansy/ai-openrouter
|
|
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,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenRouter, through its OpenAI-compatible API. Models are named `vendor/model`, such as
|
|
3
|
+
* `anthropic/claude-sonnet-5-5`. The default, `openrouter/free`, routes each request to a
|
|
4
|
+
* free model that supports tool calls, so the assistant works with no credit on the account.
|
|
5
|
+
*/
|
|
6
|
+
export declare const openrouter: (options?: {
|
|
7
|
+
defaultModel?: string;
|
|
8
|
+
appName?: string;
|
|
9
|
+
}) => import("@tansy/ai/provider").AiProvider;
|
package/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
2
|
+
import { chatImageModel, defineAiProvider, modalities, openAiModels, str } from "@tansy/ai/provider";
|
|
3
|
+
const BASE_URL = "https://openrouter.ai/api/v1";
|
|
4
|
+
/**
|
|
5
|
+
* OpenRouter, through its OpenAI-compatible API. Models are named `vendor/model`, such as
|
|
6
|
+
* `anthropic/claude-sonnet-5-5`. The default, `openrouter/free`, routes each request to a
|
|
7
|
+
* free model that supports tool calls, so the assistant works with no credit on the account.
|
|
8
|
+
*/
|
|
9
|
+
export const openrouter = (options = {}) => defineAiProvider({
|
|
10
|
+
id: "openrouter",
|
|
11
|
+
label: "OpenRouter",
|
|
12
|
+
defaultModel: options.defaultModel ?? "openrouter/free",
|
|
13
|
+
// Its settings in other interface languages. English is each setting's own label and description.
|
|
14
|
+
messages: {
|
|
15
|
+
ar: {
|
|
16
|
+
settings: {
|
|
17
|
+
openrouterApiKey: {
|
|
18
|
+
label: "مفتاح API لـ OpenRouter",
|
|
19
|
+
description: "من openrouter.ai ← Keys."
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
settings: () => [
|
|
25
|
+
{ name: "openrouterApiKey", type: "secret", env: "OPENROUTER_API_KEY", label: "OpenRouter API key", description: "From openrouter.ai → Keys." },
|
|
26
|
+
],
|
|
27
|
+
ready: (settings) => Boolean(str(settings.openrouterApiKey)),
|
|
28
|
+
model: (settings, modelId) => createOpenAICompatible({
|
|
29
|
+
name: "openrouter",
|
|
30
|
+
baseURL: BASE_URL,
|
|
31
|
+
apiKey: str(settings.openrouterApiKey),
|
|
32
|
+
// How OpenRouter names the app in its usage pages.
|
|
33
|
+
headers: { "X-Title": options.appName ?? "tansy" },
|
|
34
|
+
})(modelId),
|
|
35
|
+
// Models that draw pictures answer through the chat API with the "image" modality.
|
|
36
|
+
imageModel: (settings, modelId) => chatImageModel({ provider: "openrouter", baseUrl: BASE_URL, apiKey: str(settings.openrouterApiKey), modelId, headers: { "X-Title": options.appName ?? "tansy" } }),
|
|
37
|
+
// Free models first. What each reads and writes, and whether it calls tools, as OpenRouter reports it.
|
|
38
|
+
models: async (settings) => {
|
|
39
|
+
const free = (model) => {
|
|
40
|
+
const pricing = model.pricing;
|
|
41
|
+
return Number(pricing?.prompt) === 0 && Number(pricing?.completion) === 0;
|
|
42
|
+
};
|
|
43
|
+
const seen = new Map();
|
|
44
|
+
const models = await openAiModels(BASE_URL, str(settings.openrouterApiKey) || undefined, {
|
|
45
|
+
label: (model) => {
|
|
46
|
+
seen.set(model.id, model);
|
|
47
|
+
const name = String(model.name ?? model.id);
|
|
48
|
+
return free(model) && !/\bfree\b/i.test(name) ? `${name} (free)` : name;
|
|
49
|
+
},
|
|
50
|
+
capabilities: (model) => {
|
|
51
|
+
const architecture = (model.architecture ?? {});
|
|
52
|
+
const parameters = Array.isArray(model.supported_parameters) ? model.supported_parameters : null;
|
|
53
|
+
return {
|
|
54
|
+
input: modalities(architecture.input_modalities),
|
|
55
|
+
output: modalities(architecture.output_modalities),
|
|
56
|
+
...(parameters ? { tools: parameters.includes("tools"), reasoning: parameters.includes("reasoning"), structured: parameters.includes("structured_outputs") || parameters.includes("response_format") } : {}),
|
|
57
|
+
reported: true,
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
// OpenRouter's own router of free models, the default, leads.
|
|
62
|
+
const rank = (model) => (model.value === "openrouter/free" ? 0 : free(seen.get(model.value)) ? 1 : 2);
|
|
63
|
+
return models.sort((a, b) => rank(a) - rank(b));
|
|
64
|
+
},
|
|
65
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tansy/ai-openrouter",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OpenRouter for @tansy/ai: hundreds of models, free ones included, with one 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-openrouter"
|
|
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/openai-compatible": "^3.0.92"
|
|
30
|
+
}
|
|
31
|
+
}
|