@omp-provider-capabilities/openrouter 0.1.1 → 0.2.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Enver Francisco
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 ADDED
@@ -0,0 +1,39 @@
1
+ # @omp-provider-capabilities/openrouter
2
+
3
+ OpenRouter provider features for OMP: batch execution and server tools.
4
+
5
+ ## Features
6
+
7
+ - **Batch API**: `/v1/batches` with 50% cost discount (OpenAI-compatible, multi-endpoint)
8
+ - **Server Tools**: Shell, Web Search (beta)
9
+ - **640+ Models**: Access to a wide range of models through OpenRouter
10
+
11
+ ## Installation
12
+
13
+ Install via OMP plugin manager:
14
+
15
+ ```sh
16
+ omp plugin install @omp-provider-capabilities/openrouter
17
+ ```
18
+
19
+ Install from GitHub:
20
+
21
+ ```sh
22
+ omp plugin install https://github.com/blyzer/omp-provider-capabilities/tree/main/packages/openrouter
23
+ ```
24
+
25
+ Link a local checkout:
26
+
27
+ ```sh
28
+ omp plugin link /path/to/omp-provider-capabilities/packages/openrouter
29
+ ```
30
+
31
+ Load the extension source for one invocation:
32
+
33
+ ```sh
34
+ omp -e /path/to/omp-provider-capabilities/packages/openrouter/src/index.ts
35
+ ```
36
+
37
+ ## License
38
+
39
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omp-provider-capabilities/openrouter",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "OpenRouter provider extension: batch execution, server tools (shell, web search).",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -17,10 +17,24 @@
17
17
  "omp": {
18
18
  "extensions": [
19
19
  "./src/index.ts"
20
- ]
20
+ ],
21
+ "settings": {
22
+ "batch.enabled": {
23
+ "type": "boolean",
24
+ "default": true,
25
+ "description": "Enable OpenRouter batch API"
26
+ },
27
+ "tools.enabled": {
28
+ "type": "boolean",
29
+ "default": true,
30
+ "description": "Enable OpenRouter server tools"
31
+ }
32
+ }
21
33
  },
22
34
  "files": [
35
+ "LICENSE",
23
36
  "src/",
37
+ "README.md",
24
38
  "!src/**/*.test.ts",
25
39
  "package.json"
26
40
  ],
@@ -32,5 +46,6 @@
32
46
  "omp-plugin",
33
47
  "omp-extension",
34
48
  "oh-my-pi"
35
- ]
49
+ ],
50
+ "license": "MIT"
36
51
  }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * OpenRouter Agent SDK wrapper.
3
+ *
4
+ * Wraps OpenRouter's API for multi-turn agent loops via direct HTTP.
5
+ */
6
+
7
+ import type {
8
+ AgentSDK,
9
+ AgentConfig,
10
+ AgentResult,
11
+ AgentMessage,
12
+ AgentTool,
13
+ } from "@omp-provider-capabilities/core";
14
+
15
+ export class OpenRouterAgentSDK implements AgentSDK {
16
+ public provider = "openrouter";
17
+ private apiKey: string;
18
+
19
+ constructor(apiKey: string) {
20
+ this.apiKey = apiKey;
21
+ }
22
+
23
+ isAvailable(): boolean {
24
+ return !!this.apiKey;
25
+ }
26
+
27
+ async run(prompt: string, config: AgentConfig): Promise<AgentResult> {
28
+ const history: AgentMessage[] = [...(config.history || [])];
29
+ const maxTurns = config.maxTurns || 10;
30
+ let toolCallCount = 0;
31
+ let totalInputTokens = 0;
32
+ let totalOutputTokens = 0;
33
+
34
+ const messages: Array<{ role: string; content: string }> = [
35
+ ...(config.history || []).map((m) => ({ role: m.role, content: m.content })),
36
+ { role: "user", content: prompt },
37
+ ];
38
+
39
+ if (config.instruction) {
40
+ messages.unshift({ role: "system", content: config.instruction });
41
+ }
42
+
43
+ for (let turn = 0; turn < maxTurns; turn++) {
44
+ const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
45
+ method: "POST",
46
+ headers: {
47
+ "Content-Type": "application/json",
48
+ Authorization: `Bearer ${this.apiKey}`,
49
+ "HTTP-Referer": "https://github.com/blyzer/omp-provider-capabilities",
50
+ "X-Title": "OMP Provider Capabilities",
51
+ },
52
+ body: JSON.stringify({
53
+ model: config.model || "anthropic/claude-sonnet-4",
54
+ messages,
55
+ max_tokens: 4096,
56
+ tools: config.tools?.map((t) => ({
57
+ type: "function",
58
+ function: { name: t.name, description: t.description, parameters: t.parameters },
59
+ })),
60
+ }),
61
+ });
62
+
63
+ if (!response.ok) {
64
+ const errorText = await response.text();
65
+ return { output: "", history, toolCallCount, error: `OpenRouter API error: ${response.status} ${errorText}` };
66
+ }
67
+
68
+ const data = await response.json();
69
+ if (data.usage) {
70
+ totalInputTokens += data.usage.prompt_tokens || 0;
71
+ totalOutputTokens += data.usage.completion_tokens || 0;
72
+ }
73
+
74
+ const choices = data.choices || [];
75
+ if (choices.length === 0) break;
76
+
77
+ const message = choices[0].message;
78
+ if (message.tool_calls) {
79
+ toolCallCount += message.tool_calls.length;
80
+ for (const tc of message.tool_calls) {
81
+ messages.push({ role: "assistant", content: JSON.stringify(tc) });
82
+ const tool = config.tools?.find((t) => t.name === tc.function?.name);
83
+ let result = "Tool not found";
84
+ if (tool) {
85
+ try {
86
+ result = await tool.execute(tc.function?.arguments ? JSON.parse(tc.function.arguments) : {});
87
+ } catch (e) {
88
+ result = `Tool error: ${e instanceof Error ? e.message : String(e)}`;
89
+ }
90
+ }
91
+ messages.push({ role: "tool", content: result });
92
+ history.push({ role: "tool", content: result, toolCallId: tc.id, toolName: tc.function?.name });
93
+ }
94
+ } else if (message.content) {
95
+ messages.push({ role: "assistant", content: message.content });
96
+ history.push({ role: "assistant", content: message.content });
97
+ return {
98
+ output: message.content, history, toolCallCount,
99
+ usage: { inputTokens: totalInputTokens, outputTokens: totalOutputTokens, totalTokens: totalInputTokens + totalOutputTokens },
100
+ };
101
+ }
102
+ }
103
+
104
+ return {
105
+ output: "", history, toolCallCount,
106
+ usage: { inputTokens: totalInputTokens, outputTokens: totalOutputTokens, totalTokens: totalInputTokens + totalOutputTokens },
107
+ };
108
+ }
109
+ }
package/src/index.ts CHANGED
@@ -145,3 +145,4 @@ export default function openrouterExtension(pi: ExtensionAPI) {
145
145
  await store.save();
146
146
  });
147
147
  }
148
+ export { OpenRouterAgentSDK } from './agent-sdk';