@opencompress/opencompress 1.7.0 → 1.8.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 +10 -256
- 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.
|
|
9
|
+
var VERSION = "1.8.1";
|
|
10
10
|
var DEFAULT_BASE_URL = "https://www.opencompress.ai/api";
|
|
11
11
|
function getApiKey(api) {
|
|
12
12
|
const auth = api.config.auth;
|
|
@@ -72,19 +72,13 @@ function readExistingModels(api) {
|
|
|
72
72
|
}
|
|
73
73
|
return models.length > 0 ? models : null;
|
|
74
74
|
}
|
|
75
|
-
function buildProviderModels(baseUrl,
|
|
76
|
-
|
|
75
|
+
function buildProviderModels(baseUrl, models, apiKey) {
|
|
76
|
+
return {
|
|
77
77
|
baseUrl: `${baseUrl}/v1`,
|
|
78
78
|
api: "openai-completions",
|
|
79
79
|
apiKey: apiKey || void 0,
|
|
80
80
|
models: models || FALLBACK_MODELS
|
|
81
81
|
};
|
|
82
|
-
if (upstreamKey || upstreamBaseUrl) {
|
|
83
|
-
config.headers = {};
|
|
84
|
-
if (upstreamKey) config.headers["X-Upstream-Key"] = upstreamKey;
|
|
85
|
-
if (upstreamBaseUrl) config.headers["X-Upstream-Base-Url"] = upstreamBaseUrl;
|
|
86
|
-
}
|
|
87
|
-
return config;
|
|
88
82
|
}
|
|
89
83
|
function persistModelsConfig(providerModels) {
|
|
90
84
|
try {
|
|
@@ -116,9 +110,6 @@ function persistModelsConfig(providerModels) {
|
|
|
116
110
|
if (providerModels.apiKey) {
|
|
117
111
|
configEntry.apiKey = providerModels.apiKey;
|
|
118
112
|
}
|
|
119
|
-
if (providerModels.headers) {
|
|
120
|
-
configEntry.headers = providerModels.headers;
|
|
121
|
-
}
|
|
122
113
|
providers.opencompress = configEntry;
|
|
123
114
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
|
|
124
115
|
} catch {
|
|
@@ -160,8 +151,7 @@ function persistAgentModelsJson(providerModels) {
|
|
|
160
151
|
cost: m.cost || { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
161
152
|
contextWindow: m.contextWindow || 2e5,
|
|
162
153
|
maxTokens: m.maxTokens || 8192
|
|
163
|
-
}))
|
|
164
|
-
...providerModels.headers ? { headers: providerModels.headers } : {}
|
|
154
|
+
}))
|
|
165
155
|
};
|
|
166
156
|
fs.writeFileSync(modelsPath, JSON.stringify(data, null, 2) + "\n");
|
|
167
157
|
}
|
|
@@ -269,151 +259,6 @@ function persistAuthProfile(apiKey) {
|
|
|
269
259
|
} catch {
|
|
270
260
|
}
|
|
271
261
|
}
|
|
272
|
-
function proxyStatePath() {
|
|
273
|
-
const os = __require("os");
|
|
274
|
-
const path = __require("path");
|
|
275
|
-
return path.join(os.homedir(), ".openclaw", "opencompress-proxy.json");
|
|
276
|
-
}
|
|
277
|
-
function readProxyState() {
|
|
278
|
-
try {
|
|
279
|
-
const fs = __require("fs");
|
|
280
|
-
const p = proxyStatePath();
|
|
281
|
-
if (!fs.existsSync(p)) return { enabled: false, originals: {} };
|
|
282
|
-
return JSON.parse(fs.readFileSync(p, "utf-8"));
|
|
283
|
-
} catch {
|
|
284
|
-
return { enabled: false, originals: {} };
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
function writeProxyState(state) {
|
|
288
|
-
try {
|
|
289
|
-
const fs = __require("fs");
|
|
290
|
-
fs.writeFileSync(proxyStatePath(), JSON.stringify(state, null, 2) + "\n");
|
|
291
|
-
} catch {
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
function persistProviderToDisk(providerId, config) {
|
|
295
|
-
const os = __require("os");
|
|
296
|
-
const fs = __require("fs");
|
|
297
|
-
const path = __require("path");
|
|
298
|
-
try {
|
|
299
|
-
const configPath = path.join(os.homedir(), ".openclaw", "openclaw.json");
|
|
300
|
-
if (fs.existsSync(configPath)) {
|
|
301
|
-
const raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
302
|
-
if (!raw.models) raw.models = {};
|
|
303
|
-
if (!raw.models.providers) raw.models.providers = {};
|
|
304
|
-
raw.models.providers[providerId] = {
|
|
305
|
-
baseUrl: config.baseUrl,
|
|
306
|
-
api: config.api || "openai-completions",
|
|
307
|
-
apiKey: config.apiKey || void 0,
|
|
308
|
-
models: config.models?.map((m) => ({ id: m.id, name: m.name })) || [],
|
|
309
|
-
...config.headers ? { headers: config.headers } : {}
|
|
310
|
-
};
|
|
311
|
-
fs.writeFileSync(configPath, JSON.stringify(raw, null, 2) + "\n");
|
|
312
|
-
}
|
|
313
|
-
} catch {
|
|
314
|
-
}
|
|
315
|
-
try {
|
|
316
|
-
const agentsDir = path.join(os.homedir(), ".openclaw", "agents");
|
|
317
|
-
if (!fs.existsSync(agentsDir)) return;
|
|
318
|
-
for (const agent of fs.readdirSync(agentsDir)) {
|
|
319
|
-
const modelsPath = path.join(agentsDir, agent, "agent", "models.json");
|
|
320
|
-
if (!fs.existsSync(path.dirname(modelsPath))) continue;
|
|
321
|
-
let data = { providers: {} };
|
|
322
|
-
if (fs.existsSync(modelsPath)) {
|
|
323
|
-
try {
|
|
324
|
-
data = JSON.parse(fs.readFileSync(modelsPath, "utf-8"));
|
|
325
|
-
} catch {
|
|
326
|
-
}
|
|
327
|
-
if (!data.providers) data.providers = {};
|
|
328
|
-
}
|
|
329
|
-
data.providers[providerId] = {
|
|
330
|
-
baseUrl: config.baseUrl,
|
|
331
|
-
api: config.api || "openai-completions",
|
|
332
|
-
apiKey: config.apiKey || void 0,
|
|
333
|
-
models: config.models?.map((m) => ({
|
|
334
|
-
id: m.id,
|
|
335
|
-
name: m.name,
|
|
336
|
-
api: m.api || config.api || "openai-completions",
|
|
337
|
-
reasoning: m.reasoning ?? false,
|
|
338
|
-
input: m.input || ["text"],
|
|
339
|
-
cost: m.cost || { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
340
|
-
contextWindow: m.contextWindow || 2e5,
|
|
341
|
-
maxTokens: m.maxTokens || 8192
|
|
342
|
-
})) || [],
|
|
343
|
-
...config.headers ? { headers: config.headers } : {}
|
|
344
|
-
};
|
|
345
|
-
fs.writeFileSync(modelsPath, JSON.stringify(data, null, 2) + "\n");
|
|
346
|
-
}
|
|
347
|
-
} catch {
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
function enableProxy(api, baseUrl) {
|
|
351
|
-
const occKey = getApiKey(api);
|
|
352
|
-
if (!occKey) return { proxied: [], skipped: [] };
|
|
353
|
-
const providers = api.config.models?.providers;
|
|
354
|
-
if (!providers) return { proxied: [], skipped: [] };
|
|
355
|
-
const state = readProxyState();
|
|
356
|
-
const proxied = [];
|
|
357
|
-
const skipped = [];
|
|
358
|
-
for (const [id, raw] of Object.entries(providers)) {
|
|
359
|
-
if (id === "opencompress") continue;
|
|
360
|
-
const provider = raw;
|
|
361
|
-
if (provider.baseUrl?.includes("opencompress.ai")) continue;
|
|
362
|
-
const pApi = provider.api || "openai-completions";
|
|
363
|
-
if (pApi === "anthropic-messages" || pApi === "google-generative-ai") {
|
|
364
|
-
skipped.push(`${id} (${pApi})`);
|
|
365
|
-
continue;
|
|
366
|
-
}
|
|
367
|
-
state.originals[id] = {
|
|
368
|
-
baseUrl: provider.baseUrl,
|
|
369
|
-
apiKey: provider.apiKey,
|
|
370
|
-
api: provider.api,
|
|
371
|
-
headers: provider.headers ? { ...provider.headers } : void 0
|
|
372
|
-
};
|
|
373
|
-
provider.headers = {
|
|
374
|
-
...provider.headers || {},
|
|
375
|
-
"X-Upstream-Key": provider.apiKey || "",
|
|
376
|
-
"X-Upstream-Base-Url": provider.baseUrl
|
|
377
|
-
};
|
|
378
|
-
provider.baseUrl = `${baseUrl}/v1`;
|
|
379
|
-
provider.apiKey = occKey;
|
|
380
|
-
persistProviderToDisk(id, provider);
|
|
381
|
-
proxied.push(id);
|
|
382
|
-
}
|
|
383
|
-
state.enabled = true;
|
|
384
|
-
writeProxyState(state);
|
|
385
|
-
return { proxied, skipped };
|
|
386
|
-
}
|
|
387
|
-
function disableProxy(api) {
|
|
388
|
-
const state = readProxyState();
|
|
389
|
-
if (!state.enabled) return [];
|
|
390
|
-
const providers = api.config.models?.providers;
|
|
391
|
-
if (!providers) return [];
|
|
392
|
-
const restored = [];
|
|
393
|
-
for (const [id, orig] of Object.entries(state.originals)) {
|
|
394
|
-
const provider = providers[id];
|
|
395
|
-
if (!provider) continue;
|
|
396
|
-
provider.baseUrl = orig.baseUrl;
|
|
397
|
-
provider.apiKey = orig.apiKey;
|
|
398
|
-
provider.api = orig.api;
|
|
399
|
-
if (orig.headers) {
|
|
400
|
-
provider.headers = orig.headers;
|
|
401
|
-
} else {
|
|
402
|
-
const h = provider.headers;
|
|
403
|
-
if (h) {
|
|
404
|
-
delete h["X-Upstream-Key"];
|
|
405
|
-
delete h["X-Upstream-Base-Url"];
|
|
406
|
-
if (Object.keys(h).length === 0) delete provider.headers;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
persistProviderToDisk(id, provider);
|
|
410
|
-
restored.push(id);
|
|
411
|
-
}
|
|
412
|
-
state.enabled = false;
|
|
413
|
-
state.originals = {};
|
|
414
|
-
writeProxyState(state);
|
|
415
|
-
return restored;
|
|
416
|
-
}
|
|
417
262
|
var opencompressProvider = {
|
|
418
263
|
id: "opencompress",
|
|
419
264
|
label: "OpenCompress",
|
|
@@ -471,18 +316,7 @@ var opencompressProvider = {
|
|
|
471
316
|
}
|
|
472
317
|
const data = await res.json();
|
|
473
318
|
spinner.stop("Account created");
|
|
474
|
-
|
|
475
|
-
await fetch(`${DEFAULT_BASE_URL}/v1/byok`, {
|
|
476
|
-
method: "POST",
|
|
477
|
-
headers: {
|
|
478
|
-
Authorization: `Bearer ${data.apiKey}`,
|
|
479
|
-
"Content-Type": "application/json"
|
|
480
|
-
},
|
|
481
|
-
body: JSON.stringify({ provider, passthrough: true })
|
|
482
|
-
});
|
|
483
|
-
} catch {
|
|
484
|
-
}
|
|
485
|
-
const onboardModels = buildProviderModels(DEFAULT_BASE_URL, llmKey, upstreamBaseUrl, void 0, data.apiKey);
|
|
319
|
+
const onboardModels = buildProviderModels(DEFAULT_BASE_URL, void 0, data.apiKey);
|
|
486
320
|
const modelCount = FALLBACK_MODELS.length;
|
|
487
321
|
persistModelsConfig(onboardModels);
|
|
488
322
|
persistAgentModelsJson(onboardModels);
|
|
@@ -520,16 +354,13 @@ var opencompressProvider = {
|
|
|
520
354
|
var plugin = {
|
|
521
355
|
id: "opencompress",
|
|
522
356
|
name: "OpenCompress",
|
|
523
|
-
description: "5-layer prompt compression \u2014
|
|
357
|
+
description: "5-layer prompt compression \u2014 53% input reduction, 62% latency cut, 96% quality",
|
|
524
358
|
version: VERSION,
|
|
525
359
|
register(api) {
|
|
526
360
|
const baseUrl = api.pluginConfig?.baseUrl || DEFAULT_BASE_URL;
|
|
527
|
-
const existingHeaders = api.config.models?.providers?.opencompress?.headers;
|
|
528
|
-
const existingUpstreamKey = existingHeaders?.["X-Upstream-Key"];
|
|
529
|
-
const existingUpstreamBaseUrl = existingHeaders?.["X-Upstream-Base-Url"];
|
|
530
361
|
const existingApiKey = api.config.models?.providers?.opencompress?.apiKey || getApiKey(api);
|
|
531
362
|
const existingModels = readExistingModels(api);
|
|
532
|
-
const providerModels = buildProviderModels(baseUrl,
|
|
363
|
+
const providerModels = buildProviderModels(baseUrl, existingModels || void 0, existingApiKey);
|
|
533
364
|
opencompressProvider.models = providerModels;
|
|
534
365
|
api.registerProvider(opencompressProvider);
|
|
535
366
|
if (!api.config.models) {
|
|
@@ -685,7 +516,7 @@ var plugin = {
|
|
|
685
516
|
upstreamBaseUrl = "https://generativelanguage.googleapis.com/v1beta/openai";
|
|
686
517
|
}
|
|
687
518
|
const existingModels2 = readExistingModels(api);
|
|
688
|
-
const updatedModels = buildProviderModels(baseUrl,
|
|
519
|
+
const updatedModels = buildProviderModels(baseUrl, existingModels2 || void 0);
|
|
689
520
|
if (api.config.models?.providers) {
|
|
690
521
|
api.config.models.providers.opencompress = updatedModels;
|
|
691
522
|
}
|
|
@@ -698,7 +529,7 @@ var plugin = {
|
|
|
698
529
|
Authorization: `Bearer ${apiKey2}`,
|
|
699
530
|
"Content-Type": "application/json"
|
|
700
531
|
},
|
|
701
|
-
body: JSON.stringify({
|
|
532
|
+
body: JSON.stringify({ upstreamApiKey: upstreamKey, upstreamBaseUrl })
|
|
702
533
|
});
|
|
703
534
|
} catch {
|
|
704
535
|
}
|
|
@@ -708,8 +539,7 @@ var plugin = {
|
|
|
708
539
|
`Switched to **BYOK mode** (${provider}).`,
|
|
709
540
|
`Loaded **${modelCount2} models** from your ${provider} account.`,
|
|
710
541
|
"",
|
|
711
|
-
"Your key is stored
|
|
712
|
-
"It's passed through on each request via header and discarded immediately.",
|
|
542
|
+
"Your upstream key is stored securely on the server, associated with your API key.",
|
|
713
543
|
"",
|
|
714
544
|
"To switch back: `/compress-byok off`"
|
|
715
545
|
].join("\n")
|
|
@@ -717,82 +547,6 @@ var plugin = {
|
|
|
717
547
|
}
|
|
718
548
|
});
|
|
719
549
|
api.logger.info("Registered /compress-byok command");
|
|
720
|
-
api.registerCommand({
|
|
721
|
-
name: "compress",
|
|
722
|
-
description: "Toggle transparent compression for all LLM providers",
|
|
723
|
-
acceptsArgs: true,
|
|
724
|
-
requireAuth: false,
|
|
725
|
-
handler: async (ctx) => {
|
|
726
|
-
const arg = ctx.args?.trim().toLowerCase();
|
|
727
|
-
if (arg === "on" || arg === "enable") {
|
|
728
|
-
const occKey2 = getApiKey(api);
|
|
729
|
-
if (!occKey2) {
|
|
730
|
-
return { text: "No API key found. Run `openclaw onboard opencompress` first." };
|
|
731
|
-
}
|
|
732
|
-
const result = enableProxy(api, baseUrl);
|
|
733
|
-
if (result.proxied.length === 0 && result.skipped.length === 0) {
|
|
734
|
-
return { text: "No providers found to proxy. Add LLM providers first." };
|
|
735
|
-
}
|
|
736
|
-
const lines = [
|
|
737
|
-
"**Compression enabled** for all compatible providers.",
|
|
738
|
-
""
|
|
739
|
-
];
|
|
740
|
-
if (result.proxied.length > 0) {
|
|
741
|
-
lines.push(`Proxied (${result.proxied.length}): ${result.proxied.join(", ")}`);
|
|
742
|
-
}
|
|
743
|
-
if (result.skipped.length > 0) {
|
|
744
|
-
lines.push(`Skipped (incompatible format): ${result.skipped.join(", ")}`);
|
|
745
|
-
}
|
|
746
|
-
lines.push("", "All requests now route through OpenCompress for automatic compression.");
|
|
747
|
-
lines.push("To disable: `/compress off`");
|
|
748
|
-
return { text: lines.join("\n") };
|
|
749
|
-
}
|
|
750
|
-
if (arg === "off" || arg === "disable") {
|
|
751
|
-
const restored = disableProxy(api);
|
|
752
|
-
if (restored.length === 0) {
|
|
753
|
-
return { text: "Compression proxy was not active." };
|
|
754
|
-
}
|
|
755
|
-
return {
|
|
756
|
-
text: [
|
|
757
|
-
"**Compression disabled.** Restored original provider configs.",
|
|
758
|
-
"",
|
|
759
|
-
`Restored: ${restored.join(", ")}`,
|
|
760
|
-
"",
|
|
761
|
-
"To re-enable: `/compress on`"
|
|
762
|
-
].join("\n")
|
|
763
|
-
};
|
|
764
|
-
}
|
|
765
|
-
const proxyState = readProxyState();
|
|
766
|
-
const occKey = getApiKey(api);
|
|
767
|
-
const statusLines = [
|
|
768
|
-
"**OpenCompress Transparent Proxy**",
|
|
769
|
-
"",
|
|
770
|
-
`Status: ${proxyState.enabled ? "**ON**" : "**OFF**"}`,
|
|
771
|
-
`API key: ${occKey ? `${occKey.slice(0, 12)}...` : "not set"}`
|
|
772
|
-
];
|
|
773
|
-
if (proxyState.enabled && Object.keys(proxyState.originals).length > 0) {
|
|
774
|
-
statusLines.push(`Proxied providers: ${Object.keys(proxyState.originals).join(", ")}`);
|
|
775
|
-
}
|
|
776
|
-
statusLines.push(
|
|
777
|
-
"",
|
|
778
|
-
"**Usage:**",
|
|
779
|
-
" `/compress on` \u2014 Route all providers through OpenCompress",
|
|
780
|
-
" `/compress off` \u2014 Restore original provider configs",
|
|
781
|
-
" `/compress-stats` \u2014 View compression savings"
|
|
782
|
-
);
|
|
783
|
-
return { text: statusLines.join("\n") };
|
|
784
|
-
}
|
|
785
|
-
});
|
|
786
|
-
api.logger.info("Registered /compress command");
|
|
787
|
-
setTimeout(() => {
|
|
788
|
-
const proxyState = readProxyState();
|
|
789
|
-
if (proxyState.enabled) {
|
|
790
|
-
const result = enableProxy(api, baseUrl);
|
|
791
|
-
if (result.proxied.length > 0) {
|
|
792
|
-
api.logger.info(`Proxy auto-applied on startup: ${result.proxied.length} providers (${result.proxied.join(", ")})`);
|
|
793
|
-
}
|
|
794
|
-
}
|
|
795
|
-
}, 2e3);
|
|
796
550
|
const os = __require("os");
|
|
797
551
|
const fs = __require("fs");
|
|
798
552
|
const path = __require("path");
|