@opencompress/opencompress 1.5.0 → 1.5.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.
- package/dist/index.js +95 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
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";
|
|
@@ -51,6 +58,75 @@ function buildProviderModels(baseUrl, upstreamKey, upstreamBaseUrl, models) {
|
|
|
51
58
|
}
|
|
52
59
|
return config;
|
|
53
60
|
}
|
|
61
|
+
function persistModelsConfig(providerModels) {
|
|
62
|
+
try {
|
|
63
|
+
const os = __require("os");
|
|
64
|
+
const fs = __require("fs");
|
|
65
|
+
const path = __require("path");
|
|
66
|
+
const configPath = path.join(os.homedir(), ".openclaw", "openclaw.json");
|
|
67
|
+
if (!fs.existsSync(configPath)) return;
|
|
68
|
+
const raw = fs.readFileSync(configPath, "utf-8");
|
|
69
|
+
let config;
|
|
70
|
+
try {
|
|
71
|
+
config = JSON.parse(raw);
|
|
72
|
+
} catch {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!config.models) config.models = {};
|
|
76
|
+
const models = config.models;
|
|
77
|
+
if (!models.providers) models.providers = {};
|
|
78
|
+
const providers = models.providers;
|
|
79
|
+
const configSafeModels = providerModels.models.map((m) => ({
|
|
80
|
+
id: m.id,
|
|
81
|
+
name: m.name
|
|
82
|
+
}));
|
|
83
|
+
const configEntry = {
|
|
84
|
+
baseUrl: providerModels.baseUrl,
|
|
85
|
+
api: providerModels.api || "openai-completions",
|
|
86
|
+
models: configSafeModels
|
|
87
|
+
};
|
|
88
|
+
if (providerModels.headers) {
|
|
89
|
+
configEntry.headers = providerModels.headers;
|
|
90
|
+
}
|
|
91
|
+
providers.opencompress = configEntry;
|
|
92
|
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
93
|
+
} catch {
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function persistAuthProfile(apiKey) {
|
|
97
|
+
try {
|
|
98
|
+
const os = __require("os");
|
|
99
|
+
const fs = __require("fs");
|
|
100
|
+
const path = __require("path");
|
|
101
|
+
const agentsDir = path.join(os.homedir(), ".openclaw", "agents");
|
|
102
|
+
if (!fs.existsSync(agentsDir)) return;
|
|
103
|
+
const agentDirs = fs.readdirSync(agentsDir);
|
|
104
|
+
for (const agent of agentDirs) {
|
|
105
|
+
const authPath = path.join(agentsDir, agent, "agent", "auth-profiles.json");
|
|
106
|
+
const authDir = path.dirname(authPath);
|
|
107
|
+
if (!fs.existsSync(authDir)) {
|
|
108
|
+
fs.mkdirSync(authDir, { recursive: true });
|
|
109
|
+
}
|
|
110
|
+
let profiles = {
|
|
111
|
+
version: 1,
|
|
112
|
+
profiles: {}
|
|
113
|
+
};
|
|
114
|
+
if (fs.existsSync(authPath)) {
|
|
115
|
+
try {
|
|
116
|
+
profiles = JSON.parse(fs.readFileSync(authPath, "utf-8"));
|
|
117
|
+
} catch {
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
profiles.profiles["opencompress:default"] = {
|
|
121
|
+
type: "api_key",
|
|
122
|
+
provider: "opencompress",
|
|
123
|
+
key: apiKey
|
|
124
|
+
};
|
|
125
|
+
fs.writeFileSync(authPath, JSON.stringify(profiles, null, 2) + "\n");
|
|
126
|
+
}
|
|
127
|
+
} catch {
|
|
128
|
+
}
|
|
129
|
+
}
|
|
54
130
|
var opencompressProvider = {
|
|
55
131
|
id: "opencompress",
|
|
56
132
|
label: "OpenCompress",
|
|
@@ -119,7 +195,10 @@ var opencompressProvider = {
|
|
|
119
195
|
});
|
|
120
196
|
} catch {
|
|
121
197
|
}
|
|
198
|
+
const onboardModels = buildProviderModels(DEFAULT_BASE_URL, llmKey, upstreamBaseUrl);
|
|
122
199
|
const modelCount = FALLBACK_MODELS.length;
|
|
200
|
+
persistModelsConfig(onboardModels);
|
|
201
|
+
persistAuthProfile(data.apiKey);
|
|
123
202
|
return {
|
|
124
203
|
profiles: [
|
|
125
204
|
{
|
|
@@ -130,7 +209,7 @@ var opencompressProvider = {
|
|
|
130
209
|
configPatch: {
|
|
131
210
|
models: {
|
|
132
211
|
providers: {
|
|
133
|
-
opencompress:
|
|
212
|
+
opencompress: onboardModels
|
|
134
213
|
}
|
|
135
214
|
}
|
|
136
215
|
},
|
|
@@ -170,6 +249,11 @@ var plugin = {
|
|
|
170
249
|
api.config.models.providers = {};
|
|
171
250
|
}
|
|
172
251
|
api.config.models.providers.opencompress = providerModels;
|
|
252
|
+
persistModelsConfig(providerModels);
|
|
253
|
+
const apiKey = getApiKey(api);
|
|
254
|
+
if (apiKey) {
|
|
255
|
+
persistAuthProfile(apiKey);
|
|
256
|
+
}
|
|
173
257
|
const modelCount = existingModels ? existingModels.length : FALLBACK_MODELS.length;
|
|
174
258
|
const source = existingModels ? "from existing providers" : "fallback";
|
|
175
259
|
api.logger.info(`OpenCompress provider registered (${modelCount} models ${source}, 5-layer compression)`);
|
|
@@ -179,15 +263,15 @@ var plugin = {
|
|
|
179
263
|
acceptsArgs: true,
|
|
180
264
|
requireAuth: false,
|
|
181
265
|
handler: async () => {
|
|
182
|
-
const
|
|
183
|
-
if (!
|
|
266
|
+
const apiKey2 = getApiKey(api);
|
|
267
|
+
if (!apiKey2) {
|
|
184
268
|
return {
|
|
185
269
|
text: "No API key found. Run `openclaw onboard opencompress` to set up."
|
|
186
270
|
};
|
|
187
271
|
}
|
|
188
272
|
try {
|
|
189
273
|
const res = await fetch(`${baseUrl}/user/stats`, {
|
|
190
|
-
headers: { Authorization: `Bearer ${
|
|
274
|
+
headers: { Authorization: `Bearer ${apiKey2}` }
|
|
191
275
|
});
|
|
192
276
|
if (!res.ok) {
|
|
193
277
|
return { text: `Failed to fetch stats: HTTP ${res.status}` };
|
|
@@ -227,14 +311,14 @@ var plugin = {
|
|
|
227
311
|
acceptsArgs: true,
|
|
228
312
|
requireAuth: false,
|
|
229
313
|
handler: async (ctx) => {
|
|
230
|
-
const
|
|
231
|
-
if (!
|
|
314
|
+
const apiKey2 = getApiKey(api);
|
|
315
|
+
if (!apiKey2) {
|
|
232
316
|
return { text: "Not set up. Run `openclaw onboard opencompress` first." };
|
|
233
317
|
}
|
|
234
318
|
const upstreamKey = ctx.args?.trim();
|
|
235
319
|
if (!upstreamKey) {
|
|
236
320
|
const res = await fetch(`${baseUrl}/v1/topup`, {
|
|
237
|
-
headers: { Authorization: `Bearer ${
|
|
321
|
+
headers: { Authorization: `Bearer ${apiKey2}` }
|
|
238
322
|
});
|
|
239
323
|
const data = res.ok ? await res.json() : null;
|
|
240
324
|
return {
|
|
@@ -258,10 +342,11 @@ var plugin = {
|
|
|
258
342
|
if (api.config.models?.providers) {
|
|
259
343
|
api.config.models.providers.opencompress = cleanModels;
|
|
260
344
|
}
|
|
345
|
+
persistModelsConfig(cleanModels);
|
|
261
346
|
try {
|
|
262
347
|
await fetch(`${baseUrl}/v1/byok`, {
|
|
263
348
|
method: "DELETE",
|
|
264
|
-
headers: { Authorization: `Bearer ${
|
|
349
|
+
headers: { Authorization: `Bearer ${apiKey2}` }
|
|
265
350
|
});
|
|
266
351
|
} catch {
|
|
267
352
|
}
|
|
@@ -293,11 +378,12 @@ var plugin = {
|
|
|
293
378
|
if (api.config.models?.providers) {
|
|
294
379
|
api.config.models.providers.opencompress = updatedModels;
|
|
295
380
|
}
|
|
381
|
+
persistModelsConfig(updatedModels);
|
|
296
382
|
try {
|
|
297
383
|
await fetch(`${baseUrl}/v1/byok`, {
|
|
298
384
|
method: "POST",
|
|
299
385
|
headers: {
|
|
300
|
-
Authorization: `Bearer ${
|
|
386
|
+
Authorization: `Bearer ${apiKey2}`,
|
|
301
387
|
"Content-Type": "application/json"
|
|
302
388
|
},
|
|
303
389
|
body: JSON.stringify({ provider, passthrough: true })
|