@omp-provider-capabilities/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.
Files changed (3) hide show
  1. package/package.json +34 -4
  2. package/src/index.ts +147 -0
  3. package/README.md +0 -3
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@omp-provider-capabilities/openrouter",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "OpenRouter provider extension: batch execution, server tools (shell, web search).",
5
+ "type": "module",
6
+ "main": "src/index.ts",
7
+ "types": "src/index.ts",
8
+ "scripts": {
9
+ "build": "tsc --noEmit"
10
+ },
11
+ "dependencies": {
12
+ "@omp-provider-capabilities/core": "^0.1.0"
13
+ },
14
+ "peerDependencies": {
15
+ "@oh-my-pi/pi-coding-agent": "*"
16
+ },
17
+ "omp": {
18
+ "extensions": [
19
+ "./src/index.ts"
20
+ ]
21
+ },
22
+ "files": [
23
+ "src/",
24
+ "!src/**/*.test.ts",
25
+ "package.json"
26
+ ],
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "keywords": [
31
+ "omp-package",
32
+ "omp-plugin",
33
+ "omp-extension",
34
+ "oh-my-pi"
35
+ ]
36
+ }
package/src/index.ts ADDED
@@ -0,0 +1,147 @@
1
+ /** OpenRouter provider extension for OMPv1. */
2
+ import type { ExtensionAPI, ExtensionContext } from "@oh-my-pi/pi-coding-agent";
3
+ import {
4
+ BatchStore,
5
+ CredentialResolver,
6
+ CapabilityRegistry,
7
+ BatchEligibility,
8
+ OpenAICompatibleBatchClient,
9
+ } from "@omp-provider-capabilities/core";
10
+
11
+ export interface OpenRouterServerToolConfig {
12
+ shell: boolean;
13
+ webSearch: boolean;
14
+ }
15
+
16
+ export const DEFAULT_OPENROUTER_CONFIG: OpenRouterServerToolConfig = {
17
+ shell: false,
18
+ webSearch: false,
19
+ };
20
+
21
+ export class OpenRouterDirectHttpClient {
22
+ private apiKey: string;
23
+ private config: OpenRouterServerToolConfig;
24
+
25
+ constructor(apiKey: string, config: OpenRouterServerToolConfig) {
26
+ this.apiKey = apiKey;
27
+ this.config = config;
28
+ }
29
+
30
+ async sendMessage(messages: Array<{ role: string; content: string }>, model: string): Promise<unknown> {
31
+ const body: Record<string, unknown> = { model, messages };
32
+ const tools: Array<Record<string, unknown>> = [];
33
+ if (this.config.shell) tools.push({ type: "shell" });
34
+ if (this.config.webSearch) tools.push({ type: "web_search" });
35
+ if (tools.length > 0) body.tools = tools;
36
+
37
+ const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
38
+ method: "POST",
39
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.apiKey}` },
40
+ body: JSON.stringify(body),
41
+ });
42
+ if (!response.ok) {
43
+ const errorText = await response.text();
44
+ throw new Error(`OpenRouter direct HTTP failed: ${response.status} ${errorText}`);
45
+ }
46
+ return response.json();
47
+ }
48
+ }
49
+
50
+ export default function openrouterExtension(pi: ExtensionAPI) {
51
+ const store = new BatchStore();
52
+ const credentialResolver = new CredentialResolver();
53
+ const registry = new CapabilityRegistry();
54
+ const eligibility = new BatchEligibility(registry);
55
+
56
+ let batchClient: OpenAICompatibleBatchClient | null = null;
57
+ let directClient: OpenRouterDirectHttpClient | null = null;
58
+ let serverToolConfig: OpenRouterServerToolConfig = DEFAULT_OPENROUTER_CONFIG;
59
+
60
+ pi.on("session_start", async (_event, ctx) => {
61
+ pi.logger.debug("OpenRouter provider extension loaded");
62
+ const creds = await credentialResolver.resolveForProvider("openrouter", ctx);
63
+ if (creds && credentialResolver.isBatchEligible(creds)) {
64
+ const apiKey = process.env.OPENROUTER_API_KEY || "";
65
+ if (apiKey) {
66
+ batchClient = new OpenAICompatibleBatchClient(
67
+ { baseUrl: "https://openrouter.ai/api", apiKey },
68
+ store,
69
+ "openrouter",
70
+ );
71
+ directClient = new OpenRouterDirectHttpClient(apiKey, serverToolConfig);
72
+ }
73
+ }
74
+ await store.restore();
75
+ });
76
+
77
+ pi.registerTool({
78
+ name: "openrouter_batch_submit",
79
+ label: "OpenRouter Batch Submit",
80
+ description: "Submit a batch job to OpenRouter.",
81
+ parameters: pi.zod.object({
82
+ requests: pi.zod
83
+ .array(
84
+ pi.zod.object({
85
+ id: pi.zod.string().describe("Unique request ID"),
86
+ model: pi.zod.string().describe("Model name"),
87
+ messages: pi.zod
88
+ .array(
89
+ pi.zod.object({
90
+ role: pi.zod.enum(["system", "user", "assistant"]).describe("Message role"),
91
+ content: pi.zod.string().describe("Message content"),
92
+ }),
93
+ )
94
+ .describe("Messages"),
95
+ }),
96
+ )
97
+ .describe("Batch requests"),
98
+ }),
99
+ async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
100
+ const p = params as { requests: Array<{ id: string; model: string; messages: Array<{ role: string; content: string }> }> };
101
+ if (!batchClient) {
102
+ return { content: [{ type: "text", text: "OpenRouter batch client not available." }], isError: true };
103
+ }
104
+ try {
105
+ const job = await batchClient.submit(p.requests);
106
+ return { content: [{ type: "text", text: `Batch submitted: ${job.id}` }], details: { job } };
107
+ } catch (error) {
108
+ const msg = error instanceof Error ? error.message : String(error);
109
+ return { content: [{ type: "text", text: `Batch submit failed: ${msg}` }], isError: true };
110
+ }
111
+ },
112
+ });
113
+
114
+ pi.registerTool({
115
+ name: "openrouter_server_side_call",
116
+ label: "OpenRouter Server-Side Call",
117
+ description: "Make a direct API call to OpenRouter with server tools (shell, web search).",
118
+ parameters: pi.zod.object({
119
+ model: pi.zod.string().describe("Model name"),
120
+ messages: pi.zod
121
+ .array(
122
+ pi.zod.object({
123
+ role: pi.zod.enum(["system", "user", "assistant"]).describe("Message role"),
124
+ content: pi.zod.string().describe("Message content"),
125
+ }),
126
+ )
127
+ .describe("Messages"),
128
+ }),
129
+ async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
130
+ const p = params as { model: string; messages: Array<{ role: string; content: string }> };
131
+ if (!directClient) {
132
+ return { content: [{ type: "text", text: "OpenRouter direct HTTP client not available." }], isError: true };
133
+ }
134
+ try {
135
+ const result = await directClient.sendMessage(p.messages, p.model);
136
+ return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], details: { result } };
137
+ } catch (error) {
138
+ const msg = error instanceof Error ? error.message : String(error);
139
+ return { content: [{ type: "text", text: `OpenRouter server-side call failed: ${msg}` }], isError: true };
140
+ }
141
+ },
142
+ });
143
+
144
+ pi.on("session_shutdown", async () => {
145
+ await store.save();
146
+ });
147
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.