@opencompress/opencompress 1.5.2 → 1.6.1

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/dist/index.js +119 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
6
6
  });
7
7
 
8
8
  // src/index.ts
9
- var VERSION = "1.0.0";
9
+ var VERSION = "1.6.1";
10
10
  var DEFAULT_BASE_URL = "https://www.opencompress.ai/api";
11
11
  function getApiKey(api) {
12
12
  const auth = api.config.auth;
@@ -124,6 +124,117 @@ function persistModelsConfig(providerModels) {
124
124
  } catch {
125
125
  }
126
126
  }
127
+ function persistAgentModelsJson(providerModels) {
128
+ try {
129
+ const os = __require("os");
130
+ const fs = __require("fs");
131
+ const path = __require("path");
132
+ const agentsDir = path.join(os.homedir(), ".openclaw", "agents");
133
+ if (!fs.existsSync(agentsDir)) return;
134
+ const agentDirs = fs.readdirSync(agentsDir);
135
+ for (const agent of agentDirs) {
136
+ const modelsPath = path.join(agentsDir, agent, "agent", "models.json");
137
+ const modelsDir = path.dirname(modelsPath);
138
+ if (!fs.existsSync(modelsDir)) continue;
139
+ let data = { providers: {} };
140
+ if (fs.existsSync(modelsPath)) {
141
+ try {
142
+ data = JSON.parse(fs.readFileSync(modelsPath, "utf-8"));
143
+ if (!data.providers || typeof data.providers !== "object") {
144
+ data.providers = {};
145
+ }
146
+ } catch {
147
+ data = { providers: {} };
148
+ }
149
+ }
150
+ data.providers.opencompress = {
151
+ baseUrl: providerModels.baseUrl,
152
+ api: providerModels.api || "openai-completions",
153
+ apiKey: providerModels.apiKey || void 0,
154
+ models: providerModels.models.map((m) => ({
155
+ id: m.id,
156
+ name: m.name,
157
+ api: m.api || "openai-completions",
158
+ reasoning: m.reasoning ?? false,
159
+ input: m.input || ["text"],
160
+ cost: m.cost || { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
161
+ contextWindow: m.contextWindow || 2e5,
162
+ maxTokens: m.maxTokens || 8192
163
+ })),
164
+ ...providerModels.headers ? { headers: providerModels.headers } : {}
165
+ };
166
+ fs.writeFileSync(modelsPath, JSON.stringify(data, null, 2) + "\n");
167
+ }
168
+ } catch {
169
+ }
170
+ }
171
+ function persistAgentAuthJson(apiKey) {
172
+ try {
173
+ const os = __require("os");
174
+ const fs = __require("fs");
175
+ const path = __require("path");
176
+ const agentsDir = path.join(os.homedir(), ".openclaw", "agents");
177
+ if (!fs.existsSync(agentsDir)) return;
178
+ const agentDirs = fs.readdirSync(agentsDir);
179
+ for (const agent of agentDirs) {
180
+ const authPath = path.join(agentsDir, agent, "agent", "auth.json");
181
+ const authDir = path.dirname(authPath);
182
+ if (!fs.existsSync(authDir)) continue;
183
+ let data = {};
184
+ if (fs.existsSync(authPath)) {
185
+ try {
186
+ data = JSON.parse(fs.readFileSync(authPath, "utf-8"));
187
+ } catch {
188
+ data = {};
189
+ }
190
+ }
191
+ data.opencompress = {
192
+ type: "api_key",
193
+ key: apiKey
194
+ };
195
+ fs.writeFileSync(authPath, JSON.stringify(data, null, 2) + "\n");
196
+ }
197
+ } catch {
198
+ }
199
+ }
200
+ function injectModelsAllowlist(models) {
201
+ try {
202
+ const os = __require("os");
203
+ const fs = __require("fs");
204
+ const path = __require("path");
205
+ const configPath = path.join(os.homedir(), ".openclaw", "openclaw.json");
206
+ if (!fs.existsSync(configPath)) return;
207
+ const raw = fs.readFileSync(configPath, "utf-8");
208
+ let config;
209
+ try {
210
+ config = JSON.parse(raw);
211
+ } catch {
212
+ return;
213
+ }
214
+ if (!config.agents) config.agents = {};
215
+ const agents = config.agents;
216
+ if (!agents.defaults) agents.defaults = {};
217
+ const defaults = agents.defaults;
218
+ if (!defaults.models) defaults.models = {};
219
+ const allowlist = defaults.models;
220
+ const existingKeys = Object.keys(allowlist);
221
+ if (existingKeys.length === 0) return;
222
+ let changed = false;
223
+ for (const m of models) {
224
+ const fullId = `opencompress/${m.id}`;
225
+ if (!allowlist[fullId]) {
226
+ allowlist[fullId] = {};
227
+ changed = true;
228
+ }
229
+ }
230
+ if (changed) {
231
+ const tmpPath = `${configPath}.tmp.${process.pid}`;
232
+ fs.writeFileSync(tmpPath, JSON.stringify(config, null, 2) + "\n");
233
+ fs.renameSync(tmpPath, configPath);
234
+ }
235
+ } catch {
236
+ }
237
+ }
127
238
  function persistAuthProfile(apiKey) {
128
239
  try {
129
240
  const os = __require("os");
@@ -229,7 +340,9 @@ var opencompressProvider = {
229
340
  const onboardModels = buildProviderModels(DEFAULT_BASE_URL, llmKey, upstreamBaseUrl, void 0, data.apiKey);
230
341
  const modelCount = FALLBACK_MODELS.length;
231
342
  persistModelsConfig(onboardModels);
343
+ persistAgentModelsJson(onboardModels);
232
344
  persistAuthProfile(data.apiKey);
345
+ persistAgentAuthJson(data.apiKey);
233
346
  return {
234
347
  profiles: [
235
348
  {
@@ -282,9 +395,12 @@ var plugin = {
282
395
  }
283
396
  api.config.models.providers.opencompress = providerModels;
284
397
  persistModelsConfig(providerModels);
398
+ persistAgentModelsJson(providerModels);
399
+ injectModelsAllowlist(providerModels.models);
285
400
  const apiKey = getApiKey(api);
286
401
  if (apiKey) {
287
402
  persistAuthProfile(apiKey);
403
+ persistAgentAuthJson(apiKey);
288
404
  }
289
405
  const modelCount = existingModels ? existingModels.length : FALLBACK_MODELS.length;
290
406
  const source = existingModels ? "from existing providers" : "fallback";
@@ -375,6 +491,7 @@ var plugin = {
375
491
  api.config.models.providers.opencompress = cleanModels;
376
492
  }
377
493
  persistModelsConfig(cleanModels);
494
+ persistAgentModelsJson(cleanModels);
378
495
  try {
379
496
  await fetch(`${baseUrl}/v1/byok`, {
380
497
  method: "DELETE",
@@ -411,6 +528,7 @@ var plugin = {
411
528
  api.config.models.providers.opencompress = updatedModels;
412
529
  }
413
530
  persistModelsConfig(updatedModels);
531
+ persistAgentModelsJson(updatedModels);
414
532
  try {
415
533
  await fetch(`${baseUrl}/v1/byok`, {
416
534
  method: "POST",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencompress/opencompress",
3
- "version": "1.5.2",
3
+ "version": "1.6.1",
4
4
  "description": "OpenCompress plugin for OpenClaw — automatic 5-layer prompt compression for any LLM",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",