@opencompress/opencompress 1.5.1 → 1.6.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/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
@@ -6,11 +6,34 @@ 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.0";
10
10
  var DEFAULT_BASE_URL = "https://www.opencompress.ai/api";
11
11
  function getApiKey(api) {
12
12
  const auth = api.config.auth;
13
- 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;
14
37
  }
15
38
  var FALLBACK_MODELS = [
16
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 },
@@ -34,10 +57,14 @@ function readExistingModels(api) {
34
57
  const providerModels = providerConfig.models || [];
35
58
  for (const m of providerModels) {
36
59
  if (m.name?.includes("\u2192")) continue;
37
- if (seen.has(m.id)) continue;
38
- 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;
39
64
  models.push({
40
65
  ...m,
66
+ id: upstreamId,
67
+ name: m.name || upstreamId,
41
68
  // Zero out cost — billing handled by OpenCompress proxy
42
69
  cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
43
70
  });
@@ -45,10 +72,11 @@ function readExistingModels(api) {
45
72
  }
46
73
  return models.length > 0 ? models : null;
47
74
  }
48
- function buildProviderModels(baseUrl, upstreamKey, upstreamBaseUrl, models) {
75
+ function buildProviderModels(baseUrl, upstreamKey, upstreamBaseUrl, models, apiKey) {
49
76
  const config = {
50
77
  baseUrl: `${baseUrl}/v1`,
51
78
  api: "openai-completions",
79
+ apiKey: apiKey || void 0,
52
80
  models: models || FALLBACK_MODELS
53
81
  };
54
82
  if (upstreamKey || upstreamBaseUrl) {
@@ -85,6 +113,9 @@ function persistModelsConfig(providerModels) {
85
113
  api: providerModels.api || "openai-completions",
86
114
  models: configSafeModels
87
115
  };
116
+ if (providerModels.apiKey) {
117
+ configEntry.apiKey = providerModels.apiKey;
118
+ }
88
119
  if (providerModels.headers) {
89
120
  configEntry.headers = providerModels.headers;
90
121
  }
@@ -93,6 +124,79 @@ function persistModelsConfig(providerModels) {
93
124
  } catch {
94
125
  }
95
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
+ }
96
200
  function persistAuthProfile(apiKey) {
97
201
  try {
98
202
  const os = __require("os");
@@ -195,10 +299,12 @@ var opencompressProvider = {
195
299
  });
196
300
  } catch {
197
301
  }
198
- const onboardModels = buildProviderModels(DEFAULT_BASE_URL, llmKey, upstreamBaseUrl);
302
+ const onboardModels = buildProviderModels(DEFAULT_BASE_URL, llmKey, upstreamBaseUrl, void 0, data.apiKey);
199
303
  const modelCount = FALLBACK_MODELS.length;
200
304
  persistModelsConfig(onboardModels);
305
+ persistAgentModelsJson(onboardModels);
201
306
  persistAuthProfile(data.apiKey);
307
+ persistAgentAuthJson(data.apiKey);
202
308
  return {
203
309
  profiles: [
204
310
  {
@@ -238,8 +344,9 @@ var plugin = {
238
344
  const existingHeaders = api.config.models?.providers?.opencompress?.headers;
239
345
  const existingUpstreamKey = existingHeaders?.["X-Upstream-Key"];
240
346
  const existingUpstreamBaseUrl = existingHeaders?.["X-Upstream-Base-Url"];
347
+ const existingApiKey = api.config.models?.providers?.opencompress?.apiKey || getApiKey(api);
241
348
  const existingModels = readExistingModels(api);
242
- const providerModels = buildProviderModels(baseUrl, existingUpstreamKey, existingUpstreamBaseUrl, existingModels || void 0);
349
+ const providerModels = buildProviderModels(baseUrl, existingUpstreamKey, existingUpstreamBaseUrl, existingModels || void 0, existingApiKey);
243
350
  opencompressProvider.models = providerModels;
244
351
  api.registerProvider(opencompressProvider);
245
352
  if (!api.config.models) {
@@ -250,9 +357,11 @@ var plugin = {
250
357
  }
251
358
  api.config.models.providers.opencompress = providerModels;
252
359
  persistModelsConfig(providerModels);
360
+ persistAgentModelsJson(providerModels);
253
361
  const apiKey = getApiKey(api);
254
362
  if (apiKey) {
255
363
  persistAuthProfile(apiKey);
364
+ persistAgentAuthJson(apiKey);
256
365
  }
257
366
  const modelCount = existingModels ? existingModels.length : FALLBACK_MODELS.length;
258
367
  const source = existingModels ? "from existing providers" : "fallback";
@@ -343,6 +452,7 @@ var plugin = {
343
452
  api.config.models.providers.opencompress = cleanModels;
344
453
  }
345
454
  persistModelsConfig(cleanModels);
455
+ persistAgentModelsJson(cleanModels);
346
456
  try {
347
457
  await fetch(`${baseUrl}/v1/byok`, {
348
458
  method: "DELETE",
@@ -379,6 +489,7 @@ var plugin = {
379
489
  api.config.models.providers.opencompress = updatedModels;
380
490
  }
381
491
  persistModelsConfig(updatedModels);
492
+ persistAgentModelsJson(updatedModels);
382
493
  try {
383
494
  await fetch(`${baseUrl}/v1/byok`, {
384
495
  method: "POST",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencompress/opencompress",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
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",