@vtstech/pi-openrouter-sync 1.1.7 → 1.1.8

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 (2) hide show
  1. package/openrouter-sync.js +59 -65
  2. package/package.json +2 -2
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  MODELS_JSON_PATH as MODELS_FILE,
4
4
  readModelsJson,
5
- writeModelsJson,
5
+ readModifyWriteModelsJson,
6
6
  BUILTIN_PROVIDERS,
7
7
  EXTENSION_VERSION
8
8
  } from "@vtstech/pi-shared/ollama";
@@ -37,6 +37,35 @@ function ensureProviderOrder(providers) {
37
37
  }
38
38
  return ordered;
39
39
  }
40
+ async function performSync(modelIds) {
41
+ let result;
42
+ await readModifyWriteModelsJson((data) => {
43
+ if (!data.providers["openrouter"]) {
44
+ data.providers["openrouter"] = {
45
+ baseUrl: OR_CONFIG.baseUrl,
46
+ api: OR_CONFIG.api,
47
+ models: []
48
+ };
49
+ }
50
+ const orProvider = data.providers["openrouter"];
51
+ if (!orProvider.models) orProvider.models = [];
52
+ const existingIds = new Set(orProvider.models.map((m) => m.id));
53
+ const added = [];
54
+ const skipped = [];
55
+ for (const modelId of modelIds) {
56
+ if (existingIds.has(modelId)) {
57
+ skipped.push(modelId);
58
+ continue;
59
+ }
60
+ orProvider.models.push({ id: modelId });
61
+ added.push(modelId);
62
+ }
63
+ data.providers = ensureProviderOrder(data.providers);
64
+ result = { added, skipped, totalModels: orProvider.models.length };
65
+ return data;
66
+ });
67
+ return result;
68
+ }
40
69
  function openrouter_sync_temp_default(pi) {
41
70
  pi.registerCommand("openrouter-sync", {
42
71
  description: "Add OpenRouter model(s) to models.json. Use: /or-sync <url-or-id> [url-or-id ...]",
@@ -51,33 +80,11 @@ function openrouter_sync_temp_default(pi) {
51
80
  }
52
81
  ctx.ui.setStatus("openrouter-sync", `Adding ${modelIds.length} model(s)...`);
53
82
  try {
54
- const existing = readModelsJson();
55
- if (!existing.providers["openrouter"]) {
56
- existing.providers["openrouter"] = {
57
- baseUrl: OR_CONFIG.baseUrl,
58
- api: OR_CONFIG.api,
59
- models: []
60
- };
61
- }
62
- const orProvider = existing.providers["openrouter"];
63
- if (!orProvider.models) orProvider.models = [];
64
- const existingIds = new Set(orProvider.models.map((m) => m.id));
65
- const added = [];
66
- const skipped = [];
67
- for (const modelId of modelIds) {
68
- if (existingIds.has(modelId)) {
69
- skipped.push(modelId);
70
- continue;
71
- }
72
- orProvider.models.push({ id: modelId });
73
- added.push(modelId);
74
- }
75
- existing.providers = ensureProviderOrder(existing.providers);
76
- writeModelsJson(existing);
83
+ const { added, skipped, totalModels } = await performSync(modelIds);
77
84
  const lines = [""];
78
85
  lines.push(` Provider: openrouter (built-in)`);
79
86
  lines.push(` Base URL: ${OR_CONFIG.baseUrl}`);
80
- lines.push(` Total models: ${orProvider.models.length}`);
87
+ lines.push(` Total models: ${totalModels}`);
81
88
  if (added.length > 0) {
82
89
  lines.push(section("Added"));
83
90
  for (const id of added) lines.push(ok(id));
@@ -131,48 +138,35 @@ function openrouter_sync_temp_default(pi) {
131
138
  };
132
139
  }
133
140
  const modelIds = parseModelIds(rawModels.join(" "));
134
- const existing = readModelsJson();
135
- if (!existing.providers["openrouter"]) {
136
- existing.providers["openrouter"] = {
137
- baseUrl: OR_CONFIG.baseUrl,
138
- api: OR_CONFIG.api,
139
- models: []
141
+ try {
142
+ const { added, skipped, totalModels } = await performSync(modelIds);
143
+ const config = readModelsJson();
144
+ const orProvider = config.providers["openrouter"];
145
+ const modelList = (orProvider?.models || []).map((m) => ` - ${m.id}`).join("\n");
146
+ const report = [
147
+ BRANDING,
148
+ "",
149
+ `Added ${added.length} model(s) to openrouter provider (${totalModels} total).`,
150
+ ...added.length > 0 ? ["\nAdded:"] : [],
151
+ ...added.map((id) => ` + ${id}`),
152
+ ...skipped.length > 0 ? ["\nSkipped (already present):"] : [],
153
+ ...skipped.map((id) => ` = ${id}`),
154
+ "",
155
+ `Written to ${MODELS_FILE}. Run /reload to pick up changes.`,
156
+ "",
157
+ "Current openrouter models:",
158
+ modelList
159
+ ].join("\n");
160
+ return {
161
+ content: [{ type: "text", text: report }],
162
+ details: { added: added.length, skipped: skipped.length, total: totalModels }
163
+ };
164
+ } catch (err) {
165
+ return {
166
+ content: [{ type: "text", text: `Error: ${err.message}` }],
167
+ details: {}
140
168
  };
141
169
  }
142
- const orProvider = existing.providers["openrouter"];
143
- if (!orProvider.models) orProvider.models = [];
144
- const existingIds = new Set(orProvider.models.map((m) => m.id));
145
- const added = [];
146
- const skipped = [];
147
- for (const modelId of modelIds) {
148
- if (existingIds.has(modelId)) {
149
- skipped.push(modelId);
150
- continue;
151
- }
152
- orProvider.models.push({ id: modelId });
153
- added.push(modelId);
154
- }
155
- existing.providers = ensureProviderOrder(existing.providers);
156
- writeModelsJson(existing);
157
- const modelList = orProvider.models.map((m) => ` - ${m.id}`).join("\n");
158
- const report = [
159
- BRANDING,
160
- "",
161
- `Added ${added.length} model(s) to openrouter provider (${orProvider.models.length} total).`,
162
- ...added.length > 0 ? ["\nAdded:"] : [],
163
- ...added.map((id) => ` + ${id}`),
164
- ...skipped.length > 0 ? ["\nSkipped (already present):"] : [],
165
- ...skipped.map((id) => ` = ${id}`),
166
- "",
167
- `Written to ${MODELS_FILE}. Run /reload to pick up changes.`,
168
- "",
169
- "Current openrouter models:",
170
- modelList
171
- ].join("\n");
172
- return {
173
- content: [{ type: "text", text: report }],
174
- details: { added: added.length, skipped: skipped.length, total: orProvider.models.length }
175
- };
176
170
  }
177
171
  });
178
172
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtstech/pi-openrouter-sync",
3
- "version": "1.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "OpenRouter model sync extension for Pi Coding Agent — add models from OpenRouter URLs or IDs",
5
5
  "main": "openrouter-sync.js",
6
6
  "keywords": ["pi-extensions"],
@@ -14,7 +14,7 @@
14
14
  "url": "https://github.com/VTSTech/pi-coding-agent"
15
15
  },
16
16
  "dependencies": {
17
- "@vtstech/pi-shared": "1.1.7"
17
+ "@vtstech/pi-shared": "1.1.8"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "@mariozechner/pi-coding-agent": ">=0.66"