@opencompress/opencompress 1.5.0 → 1.5.2

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/dist/index.d.ts CHANGED
@@ -29,6 +29,7 @@ type ModelProviderConfig = {
29
29
  headers?: Record<string, string>;
30
30
  authHeader?: boolean;
31
31
  models: ModelDefinitionConfig[];
32
+ [key: string]: unknown;
32
33
  };
33
34
  type AuthProfileCredential = {
34
35
  apiKey?: string;
package/dist/index.js CHANGED
@@ -1,9 +1,39 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/index.ts
2
9
  var VERSION = "1.0.0";
3
10
  var DEFAULT_BASE_URL = "https://www.opencompress.ai/api";
4
11
  function getApiKey(api) {
5
12
  const auth = api.config.auth;
6
- return auth?.profiles?.opencompress?.credentials?.["api-key"]?.apiKey;
13
+ const fromConfig = auth?.profiles?.opencompress?.credentials?.["api-key"]?.apiKey;
14
+ if (fromConfig) return fromConfig;
15
+ if (process.env.OPENCOMPRESS_API_KEY) return process.env.OPENCOMPRESS_API_KEY;
16
+ try {
17
+ const os = __require("os");
18
+ const fs = __require("fs");
19
+ const path = __require("path");
20
+ const agentsDir = path.join(os.homedir(), ".openclaw", "agents");
21
+ if (!fs.existsSync(agentsDir)) return void 0;
22
+ const agentDirs = fs.readdirSync(agentsDir);
23
+ for (const agent of agentDirs) {
24
+ const authPath = path.join(agentsDir, agent, "agent", "auth-profiles.json");
25
+ if (!fs.existsSync(authPath)) continue;
26
+ try {
27
+ const profiles = JSON.parse(fs.readFileSync(authPath, "utf-8"));
28
+ const ocProfile = profiles?.profiles?.["opencompress:default"];
29
+ if (ocProfile?.key) return ocProfile.key;
30
+ } catch {
31
+ continue;
32
+ }
33
+ }
34
+ } catch {
35
+ }
36
+ return void 0;
7
37
  }
8
38
  var FALLBACK_MODELS = [
9
39
  { id: "gpt-4o", name: "GPT-4o", reasoning: false, input: ["text", "image"], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 128e3, maxTokens: 16384 },
@@ -27,10 +57,14 @@ function readExistingModels(api) {
27
57
  const providerModels = providerConfig.models || [];
28
58
  for (const m of providerModels) {
29
59
  if (m.name?.includes("\u2192")) continue;
30
- if (seen.has(m.id)) continue;
31
- seen.add(m.id);
60
+ const rawId = m.id.includes("/") ? m.id.split("/").slice(1).join("/") : m.id;
61
+ if (seen.has(rawId)) continue;
62
+ seen.add(rawId);
63
+ const upstreamId = m.id.startsWith(`${providerId}/`) ? rawId : m.id;
32
64
  models.push({
33
65
  ...m,
66
+ id: upstreamId,
67
+ name: m.name || upstreamId,
34
68
  // Zero out cost — billing handled by OpenCompress proxy
35
69
  cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
36
70
  });
@@ -38,10 +72,11 @@ function readExistingModels(api) {
38
72
  }
39
73
  return models.length > 0 ? models : null;
40
74
  }
41
- function buildProviderModels(baseUrl, upstreamKey, upstreamBaseUrl, models) {
75
+ function buildProviderModels(baseUrl, upstreamKey, upstreamBaseUrl, models, apiKey) {
42
76
  const config = {
43
77
  baseUrl: `${baseUrl}/v1`,
44
78
  api: "openai-completions",
79
+ apiKey: apiKey || void 0,
45
80
  models: models || FALLBACK_MODELS
46
81
  };
47
82
  if (upstreamKey || upstreamBaseUrl) {
@@ -51,6 +86,78 @@ function buildProviderModels(baseUrl, upstreamKey, upstreamBaseUrl, models) {
51
86
  }
52
87
  return config;
53
88
  }
89
+ function persistModelsConfig(providerModels) {
90
+ try {
91
+ const os = __require("os");
92
+ const fs = __require("fs");
93
+ const path = __require("path");
94
+ const configPath = path.join(os.homedir(), ".openclaw", "openclaw.json");
95
+ if (!fs.existsSync(configPath)) return;
96
+ const raw = fs.readFileSync(configPath, "utf-8");
97
+ let config;
98
+ try {
99
+ config = JSON.parse(raw);
100
+ } catch {
101
+ return;
102
+ }
103
+ if (!config.models) config.models = {};
104
+ const models = config.models;
105
+ if (!models.providers) models.providers = {};
106
+ const providers = models.providers;
107
+ const configSafeModels = providerModels.models.map((m) => ({
108
+ id: m.id,
109
+ name: m.name
110
+ }));
111
+ const configEntry = {
112
+ baseUrl: providerModels.baseUrl,
113
+ api: providerModels.api || "openai-completions",
114
+ models: configSafeModels
115
+ };
116
+ if (providerModels.apiKey) {
117
+ configEntry.apiKey = providerModels.apiKey;
118
+ }
119
+ if (providerModels.headers) {
120
+ configEntry.headers = providerModels.headers;
121
+ }
122
+ providers.opencompress = configEntry;
123
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
124
+ } catch {
125
+ }
126
+ }
127
+ function persistAuthProfile(apiKey) {
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 authPath = path.join(agentsDir, agent, "agent", "auth-profiles.json");
137
+ const authDir = path.dirname(authPath);
138
+ if (!fs.existsSync(authDir)) {
139
+ fs.mkdirSync(authDir, { recursive: true });
140
+ }
141
+ let profiles = {
142
+ version: 1,
143
+ profiles: {}
144
+ };
145
+ if (fs.existsSync(authPath)) {
146
+ try {
147
+ profiles = JSON.parse(fs.readFileSync(authPath, "utf-8"));
148
+ } catch {
149
+ }
150
+ }
151
+ profiles.profiles["opencompress:default"] = {
152
+ type: "api_key",
153
+ provider: "opencompress",
154
+ key: apiKey
155
+ };
156
+ fs.writeFileSync(authPath, JSON.stringify(profiles, null, 2) + "\n");
157
+ }
158
+ } catch {
159
+ }
160
+ }
54
161
  var opencompressProvider = {
55
162
  id: "opencompress",
56
163
  label: "OpenCompress",
@@ -119,7 +226,10 @@ var opencompressProvider = {
119
226
  });
120
227
  } catch {
121
228
  }
229
+ const onboardModels = buildProviderModels(DEFAULT_BASE_URL, llmKey, upstreamBaseUrl, void 0, data.apiKey);
122
230
  const modelCount = FALLBACK_MODELS.length;
231
+ persistModelsConfig(onboardModels);
232
+ persistAuthProfile(data.apiKey);
123
233
  return {
124
234
  profiles: [
125
235
  {
@@ -130,7 +240,7 @@ var opencompressProvider = {
130
240
  configPatch: {
131
241
  models: {
132
242
  providers: {
133
- opencompress: buildProviderModels(DEFAULT_BASE_URL, llmKey, upstreamBaseUrl)
243
+ opencompress: onboardModels
134
244
  }
135
245
  }
136
246
  },
@@ -159,8 +269,9 @@ var plugin = {
159
269
  const existingHeaders = api.config.models?.providers?.opencompress?.headers;
160
270
  const existingUpstreamKey = existingHeaders?.["X-Upstream-Key"];
161
271
  const existingUpstreamBaseUrl = existingHeaders?.["X-Upstream-Base-Url"];
272
+ const existingApiKey = api.config.models?.providers?.opencompress?.apiKey || getApiKey(api);
162
273
  const existingModels = readExistingModels(api);
163
- const providerModels = buildProviderModels(baseUrl, existingUpstreamKey, existingUpstreamBaseUrl, existingModels || void 0);
274
+ const providerModels = buildProviderModels(baseUrl, existingUpstreamKey, existingUpstreamBaseUrl, existingModels || void 0, existingApiKey);
164
275
  opencompressProvider.models = providerModels;
165
276
  api.registerProvider(opencompressProvider);
166
277
  if (!api.config.models) {
@@ -170,6 +281,11 @@ var plugin = {
170
281
  api.config.models.providers = {};
171
282
  }
172
283
  api.config.models.providers.opencompress = providerModels;
284
+ persistModelsConfig(providerModels);
285
+ const apiKey = getApiKey(api);
286
+ if (apiKey) {
287
+ persistAuthProfile(apiKey);
288
+ }
173
289
  const modelCount = existingModels ? existingModels.length : FALLBACK_MODELS.length;
174
290
  const source = existingModels ? "from existing providers" : "fallback";
175
291
  api.logger.info(`OpenCompress provider registered (${modelCount} models ${source}, 5-layer compression)`);
@@ -179,15 +295,15 @@ var plugin = {
179
295
  acceptsArgs: true,
180
296
  requireAuth: false,
181
297
  handler: async () => {
182
- const apiKey = getApiKey(api);
183
- if (!apiKey) {
298
+ const apiKey2 = getApiKey(api);
299
+ if (!apiKey2) {
184
300
  return {
185
301
  text: "No API key found. Run `openclaw onboard opencompress` to set up."
186
302
  };
187
303
  }
188
304
  try {
189
305
  const res = await fetch(`${baseUrl}/user/stats`, {
190
- headers: { Authorization: `Bearer ${apiKey}` }
306
+ headers: { Authorization: `Bearer ${apiKey2}` }
191
307
  });
192
308
  if (!res.ok) {
193
309
  return { text: `Failed to fetch stats: HTTP ${res.status}` };
@@ -227,14 +343,14 @@ var plugin = {
227
343
  acceptsArgs: true,
228
344
  requireAuth: false,
229
345
  handler: async (ctx) => {
230
- const apiKey = getApiKey(api);
231
- if (!apiKey) {
346
+ const apiKey2 = getApiKey(api);
347
+ if (!apiKey2) {
232
348
  return { text: "Not set up. Run `openclaw onboard opencompress` first." };
233
349
  }
234
350
  const upstreamKey = ctx.args?.trim();
235
351
  if (!upstreamKey) {
236
352
  const res = await fetch(`${baseUrl}/v1/topup`, {
237
- headers: { Authorization: `Bearer ${apiKey}` }
353
+ headers: { Authorization: `Bearer ${apiKey2}` }
238
354
  });
239
355
  const data = res.ok ? await res.json() : null;
240
356
  return {
@@ -258,10 +374,11 @@ var plugin = {
258
374
  if (api.config.models?.providers) {
259
375
  api.config.models.providers.opencompress = cleanModels;
260
376
  }
377
+ persistModelsConfig(cleanModels);
261
378
  try {
262
379
  await fetch(`${baseUrl}/v1/byok`, {
263
380
  method: "DELETE",
264
- headers: { Authorization: `Bearer ${apiKey}` }
381
+ headers: { Authorization: `Bearer ${apiKey2}` }
265
382
  });
266
383
  } catch {
267
384
  }
@@ -293,11 +410,12 @@ var plugin = {
293
410
  if (api.config.models?.providers) {
294
411
  api.config.models.providers.opencompress = updatedModels;
295
412
  }
413
+ persistModelsConfig(updatedModels);
296
414
  try {
297
415
  await fetch(`${baseUrl}/v1/byok`, {
298
416
  method: "POST",
299
417
  headers: {
300
- Authorization: `Bearer ${apiKey}`,
418
+ Authorization: `Bearer ${apiKey2}`,
301
419
  "Content-Type": "application/json"
302
420
  },
303
421
  body: JSON.stringify({ provider, passthrough: true })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencompress/opencompress",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
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",