@opencompress/opencompress 1.9.0 → 1.9.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.js +50 -108
- package/package.json +1 -1
- package/dist/index.d.ts +0 -144
package/dist/index.js
CHANGED
|
@@ -6,95 +6,38 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
// src/index.ts
|
|
9
|
-
var VERSION = "1.9.
|
|
9
|
+
var VERSION = "1.9.2";
|
|
10
10
|
var DEFAULT_BASE_URL = "https://www.opencompress.ai/api";
|
|
11
|
-
function getApiKey(
|
|
12
|
-
const auth =
|
|
11
|
+
function getApiKey(api2) {
|
|
12
|
+
const auth = api2.config.auth;
|
|
13
13
|
const fromConfig = auth?.profiles?.opencompress?.credentials?.["api-key"]?.apiKey;
|
|
14
14
|
if (fromConfig) return fromConfig;
|
|
15
15
|
if (process.env.OPENCOMPRESS_API_KEY) return process.env.OPENCOMPRESS_API_KEY;
|
|
16
|
-
|
|
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
|
-
}
|
|
16
|
+
if (api2.pluginConfig?.apiKey) return api2.pluginConfig.apiKey;
|
|
36
17
|
return void 0;
|
|
37
18
|
}
|
|
38
|
-
function
|
|
39
|
-
try {
|
|
40
|
-
const os = __require("os");
|
|
41
|
-
const fs = __require("fs");
|
|
42
|
-
const path = __require("path");
|
|
43
|
-
const agentsDir = path.join(os.homedir(), ".openclaw", "agents");
|
|
44
|
-
if (!fs.existsSync(agentsDir)) return;
|
|
45
|
-
const agentDirs = fs.readdirSync(agentsDir);
|
|
46
|
-
for (const agent of agentDirs) {
|
|
47
|
-
const authPath = path.join(agentsDir, agent, "agent", "auth-profiles.json");
|
|
48
|
-
const authDir = path.dirname(authPath);
|
|
49
|
-
if (!fs.existsSync(authDir)) {
|
|
50
|
-
fs.mkdirSync(authDir, { recursive: true });
|
|
51
|
-
}
|
|
52
|
-
let profiles = {
|
|
53
|
-
version: 1,
|
|
54
|
-
profiles: {}
|
|
55
|
-
};
|
|
56
|
-
if (fs.existsSync(authPath)) {
|
|
57
|
-
try {
|
|
58
|
-
profiles = JSON.parse(fs.readFileSync(authPath, "utf-8"));
|
|
59
|
-
} catch {
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
profiles.profiles["opencompress:default"] = {
|
|
63
|
-
type: "api_key",
|
|
64
|
-
provider: "opencompress",
|
|
65
|
-
key: apiKey
|
|
66
|
-
};
|
|
67
|
-
fs.writeFileSync(authPath, JSON.stringify(profiles, null, 2) + "\n");
|
|
68
|
-
}
|
|
69
|
-
} catch {
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function persistAgentAuthJson(apiKey) {
|
|
19
|
+
function persistApiKey(api2, apiKey) {
|
|
73
20
|
try {
|
|
74
|
-
const os = __require("os");
|
|
75
21
|
const fs = __require("fs");
|
|
76
22
|
const path = __require("path");
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
data = JSON.parse(fs.readFileSync(authPath, "utf-8"));
|
|
88
|
-
} catch {
|
|
89
|
-
data = {};
|
|
90
|
-
}
|
|
23
|
+
const agentDir = api2.resolvePath(".");
|
|
24
|
+
const authPath = path.join(agentDir, "auth-profiles.json");
|
|
25
|
+
let profiles = {
|
|
26
|
+
version: 1,
|
|
27
|
+
profiles: {}
|
|
28
|
+
};
|
|
29
|
+
if (fs.existsSync(authPath)) {
|
|
30
|
+
try {
|
|
31
|
+
profiles = JSON.parse(fs.readFileSync(authPath, "utf-8"));
|
|
32
|
+
} catch {
|
|
91
33
|
}
|
|
92
|
-
data.opencompress = {
|
|
93
|
-
type: "api_key",
|
|
94
|
-
key: apiKey
|
|
95
|
-
};
|
|
96
|
-
fs.writeFileSync(authPath, JSON.stringify(data, null, 2) + "\n");
|
|
97
34
|
}
|
|
35
|
+
profiles.profiles["opencompress:default"] = {
|
|
36
|
+
type: "api_key",
|
|
37
|
+
provider: "opencompress",
|
|
38
|
+
key: apiKey
|
|
39
|
+
};
|
|
40
|
+
fs.writeFileSync(authPath, JSON.stringify(profiles, null, 2) + "\n");
|
|
98
41
|
} catch {
|
|
99
42
|
}
|
|
100
43
|
}
|
|
@@ -191,10 +134,10 @@ function persistProviderToDisk(providerId, config) {
|
|
|
191
134
|
} catch {
|
|
192
135
|
}
|
|
193
136
|
}
|
|
194
|
-
function enableProxy(
|
|
195
|
-
const occKey = getApiKey(
|
|
137
|
+
function enableProxy(api2, baseUrl) {
|
|
138
|
+
const occKey = getApiKey(api2);
|
|
196
139
|
if (!occKey) return { proxied: [], skipped: [] };
|
|
197
|
-
let providers =
|
|
140
|
+
let providers = api2.config.models?.providers;
|
|
198
141
|
let source = "api.config";
|
|
199
142
|
if (!providers || Object.keys(providers).length === 0) {
|
|
200
143
|
providers = readProvidersFromDisk();
|
|
@@ -222,10 +165,10 @@ function enableProxy(api, baseUrl) {
|
|
|
222
165
|
if (pApi === "anthropic-messages") {
|
|
223
166
|
provider.headers = {
|
|
224
167
|
...provider.headers || {},
|
|
225
|
-
"x-
|
|
168
|
+
"x-upstream-key": provider.apiKey || ""
|
|
226
169
|
};
|
|
227
170
|
provider.baseUrl = `${baseUrl}/v1`;
|
|
228
|
-
provider.apiKey =
|
|
171
|
+
provider.apiKey = occKey;
|
|
229
172
|
} else {
|
|
230
173
|
provider.headers = {
|
|
231
174
|
...provider.headers || {},
|
|
@@ -242,10 +185,10 @@ function enableProxy(api, baseUrl) {
|
|
|
242
185
|
writeProxyState(state);
|
|
243
186
|
return { proxied, skipped, source };
|
|
244
187
|
}
|
|
245
|
-
function disableProxy(
|
|
188
|
+
function disableProxy(api2) {
|
|
246
189
|
const state = readProxyState();
|
|
247
190
|
if (!state.enabled) return [];
|
|
248
|
-
let providers =
|
|
191
|
+
let providers = api2.config.models?.providers;
|
|
249
192
|
if (!providers || Object.keys(providers).length === 0) {
|
|
250
193
|
providers = readProvidersFromDisk();
|
|
251
194
|
}
|
|
@@ -265,6 +208,7 @@ function disableProxy(api) {
|
|
|
265
208
|
delete h["X-Upstream-Key"];
|
|
266
209
|
delete h["X-Upstream-Base-Url"];
|
|
267
210
|
delete h["x-api-key"];
|
|
211
|
+
delete h["x-upstream-key"];
|
|
268
212
|
if (Object.keys(h).length === 0) delete provider.headers;
|
|
269
213
|
}
|
|
270
214
|
}
|
|
@@ -317,8 +261,7 @@ var opencompressProvider = {
|
|
|
317
261
|
}
|
|
318
262
|
const data = await res.json();
|
|
319
263
|
spinner.stop("Account created!");
|
|
320
|
-
|
|
321
|
-
persistAgentAuthJson(data.apiKey);
|
|
264
|
+
persistApiKey(api, data.apiKey);
|
|
322
265
|
const enableNow = await ctx.prompter.text({
|
|
323
266
|
message: "Ready to compress? This will route your existing LLM providers through OpenCompress. Enable now? (yes/no)",
|
|
324
267
|
validate: (v) => {
|
|
@@ -371,22 +314,21 @@ var plugin = {
|
|
|
371
314
|
name: "OpenCompress",
|
|
372
315
|
description: "Transparent prompt compression \u2014 save 40-70% on Claude, GPT, and any LLM provider",
|
|
373
316
|
version: VERSION,
|
|
374
|
-
register(
|
|
375
|
-
const baseUrl =
|
|
376
|
-
|
|
377
|
-
const apiKey = getApiKey(
|
|
317
|
+
register(api2) {
|
|
318
|
+
const baseUrl = api2.pluginConfig?.baseUrl || DEFAULT_BASE_URL;
|
|
319
|
+
api2.registerProvider(opencompressProvider);
|
|
320
|
+
const apiKey = getApiKey(api2);
|
|
378
321
|
if (apiKey) {
|
|
379
|
-
|
|
380
|
-
persistAgentAuthJson(apiKey);
|
|
322
|
+
persistApiKey(api2, apiKey);
|
|
381
323
|
}
|
|
382
|
-
|
|
383
|
-
|
|
324
|
+
api2.logger.info("OpenCompress registered (transparent proxy mode)");
|
|
325
|
+
api2.registerCommand({
|
|
384
326
|
name: "compress-stats",
|
|
385
327
|
description: "Show OpenCompress usage statistics and savings",
|
|
386
328
|
acceptsArgs: true,
|
|
387
329
|
requireAuth: false,
|
|
388
330
|
handler: async () => {
|
|
389
|
-
const apiKey2 = getApiKey(
|
|
331
|
+
const apiKey2 = getApiKey(api2);
|
|
390
332
|
if (!apiKey2) {
|
|
391
333
|
return {
|
|
392
334
|
text: "No API key found. Run `openclaw onboard opencompress` to set up."
|
|
@@ -436,8 +378,8 @@ var plugin = {
|
|
|
436
378
|
}
|
|
437
379
|
}
|
|
438
380
|
});
|
|
439
|
-
|
|
440
|
-
|
|
381
|
+
api2.logger.info("Registered /compress-stats command");
|
|
382
|
+
api2.registerCommand({
|
|
441
383
|
name: "compress",
|
|
442
384
|
description: "Toggle transparent compression for all LLM providers",
|
|
443
385
|
acceptsArgs: true,
|
|
@@ -445,11 +387,11 @@ var plugin = {
|
|
|
445
387
|
handler: async (ctx) => {
|
|
446
388
|
const arg = ctx.args?.trim().toLowerCase();
|
|
447
389
|
if (arg === "on" || arg === "enable") {
|
|
448
|
-
const occKey2 = getApiKey(
|
|
390
|
+
const occKey2 = getApiKey(api2);
|
|
449
391
|
if (!occKey2) {
|
|
450
392
|
return { text: "No API key found. Run `openclaw onboard opencompress` first." };
|
|
451
393
|
}
|
|
452
|
-
const result = enableProxy(
|
|
394
|
+
const result = enableProxy(api2, baseUrl);
|
|
453
395
|
if (result.proxied.length === 0 && result.skipped.length === 0) {
|
|
454
396
|
return { text: "No providers found to proxy. Add LLM providers first." };
|
|
455
397
|
}
|
|
@@ -468,7 +410,7 @@ var plugin = {
|
|
|
468
410
|
return { text: lines.join("\n") };
|
|
469
411
|
}
|
|
470
412
|
if (arg === "off" || arg === "disable") {
|
|
471
|
-
const restored = disableProxy(
|
|
413
|
+
const restored = disableProxy(api2);
|
|
472
414
|
if (restored.length === 0) {
|
|
473
415
|
return { text: "Compression proxy was not active." };
|
|
474
416
|
}
|
|
@@ -483,7 +425,7 @@ var plugin = {
|
|
|
483
425
|
};
|
|
484
426
|
}
|
|
485
427
|
const proxyState = readProxyState();
|
|
486
|
-
const occKey = getApiKey(
|
|
428
|
+
const occKey = getApiKey(api2);
|
|
487
429
|
const statusLines = [
|
|
488
430
|
"**OpenCompress Transparent Proxy**",
|
|
489
431
|
"",
|
|
@@ -503,14 +445,14 @@ var plugin = {
|
|
|
503
445
|
return { text: statusLines.join("\n") };
|
|
504
446
|
}
|
|
505
447
|
});
|
|
506
|
-
|
|
448
|
+
api2.logger.info("Registered /compress command");
|
|
507
449
|
setTimeout(() => {
|
|
508
450
|
const proxyState = readProxyState();
|
|
509
|
-
const autoEnable =
|
|
451
|
+
const autoEnable = api2.pluginConfig?._autoEnableProxy;
|
|
510
452
|
if (proxyState.enabled || autoEnable) {
|
|
511
|
-
const result = enableProxy(
|
|
453
|
+
const result = enableProxy(api2, baseUrl);
|
|
512
454
|
if (result.proxied.length > 0) {
|
|
513
|
-
|
|
455
|
+
api2.logger.info(`Compression active: ${result.proxied.length} providers (${result.proxied.join(", ")})`);
|
|
514
456
|
}
|
|
515
457
|
}
|
|
516
458
|
}, 2e3);
|
|
@@ -522,7 +464,7 @@ var plugin = {
|
|
|
522
464
|
fs.mkdirSync(logDir, { recursive: true });
|
|
523
465
|
} catch {
|
|
524
466
|
}
|
|
525
|
-
|
|
467
|
+
api2.on("llm_input", (...args) => {
|
|
526
468
|
try {
|
|
527
469
|
const ts = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
528
470
|
const event = args[0] || {};
|
|
@@ -564,7 +506,7 @@ args: ${JSON.stringify(args?.map((a) => typeof a))}
|
|
|
564
506
|
}
|
|
565
507
|
}
|
|
566
508
|
});
|
|
567
|
-
|
|
509
|
+
api2.logger.info(`LLM input logging enabled \u2192 ${logDir}`);
|
|
568
510
|
}
|
|
569
511
|
};
|
|
570
512
|
var index_default = plugin;
|
package/package.json
CHANGED
package/dist/index.d.ts
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenClaw Plugin Types (locally defined)
|
|
3
|
-
*
|
|
4
|
-
* OpenClaw's plugin SDK uses duck typing — these match the shapes
|
|
5
|
-
* expected by registerProvider() and the plugin system.
|
|
6
|
-
* Defined locally to avoid depending on internal OpenClaw paths.
|
|
7
|
-
*/
|
|
8
|
-
type ModelApi = "openai-completions" | "openai-responses" | "anthropic-messages" | "google-generative-ai";
|
|
9
|
-
type ModelDefinitionConfig = {
|
|
10
|
-
id: string;
|
|
11
|
-
name: string;
|
|
12
|
-
api?: ModelApi;
|
|
13
|
-
reasoning: boolean;
|
|
14
|
-
input: Array<"text" | "image">;
|
|
15
|
-
cost: {
|
|
16
|
-
input: number;
|
|
17
|
-
output: number;
|
|
18
|
-
cacheRead: number;
|
|
19
|
-
cacheWrite: number;
|
|
20
|
-
};
|
|
21
|
-
contextWindow: number;
|
|
22
|
-
maxTokens: number;
|
|
23
|
-
headers?: Record<string, string>;
|
|
24
|
-
};
|
|
25
|
-
type ModelProviderConfig = {
|
|
26
|
-
baseUrl: string;
|
|
27
|
-
apiKey?: string;
|
|
28
|
-
api?: ModelApi;
|
|
29
|
-
headers?: Record<string, string>;
|
|
30
|
-
authHeader?: boolean;
|
|
31
|
-
models: ModelDefinitionConfig[];
|
|
32
|
-
[key: string]: unknown;
|
|
33
|
-
};
|
|
34
|
-
type AuthProfileCredential = {
|
|
35
|
-
apiKey?: string;
|
|
36
|
-
type?: string;
|
|
37
|
-
[key: string]: unknown;
|
|
38
|
-
};
|
|
39
|
-
type ProviderAuthResult = {
|
|
40
|
-
profiles: Array<{
|
|
41
|
-
profileId: string;
|
|
42
|
-
credential: AuthProfileCredential;
|
|
43
|
-
}>;
|
|
44
|
-
configPatch?: Record<string, unknown>;
|
|
45
|
-
defaultModel?: string;
|
|
46
|
-
notes?: string[];
|
|
47
|
-
};
|
|
48
|
-
type WizardPrompter = {
|
|
49
|
-
text: (opts: {
|
|
50
|
-
message: string;
|
|
51
|
-
validate?: (value: string) => string | undefined;
|
|
52
|
-
}) => Promise<string | symbol>;
|
|
53
|
-
note: (message: string) => void;
|
|
54
|
-
progress: (message: string) => {
|
|
55
|
-
stop: (message?: string) => void;
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
type ProviderAuthContext = {
|
|
59
|
-
config: Record<string, unknown>;
|
|
60
|
-
agentDir?: string;
|
|
61
|
-
workspaceDir?: string;
|
|
62
|
-
prompter: WizardPrompter;
|
|
63
|
-
runtime: {
|
|
64
|
-
log: (message: string) => void;
|
|
65
|
-
};
|
|
66
|
-
isRemote: boolean;
|
|
67
|
-
openUrl: (url: string) => Promise<void>;
|
|
68
|
-
};
|
|
69
|
-
type ProviderAuthMethod = {
|
|
70
|
-
id: string;
|
|
71
|
-
label: string;
|
|
72
|
-
hint?: string;
|
|
73
|
-
kind: "oauth" | "api_key" | "token" | "device_code" | "custom";
|
|
74
|
-
run: (ctx: ProviderAuthContext) => Promise<ProviderAuthResult>;
|
|
75
|
-
};
|
|
76
|
-
type ProviderPlugin = {
|
|
77
|
-
id: string;
|
|
78
|
-
label: string;
|
|
79
|
-
docsPath?: string;
|
|
80
|
-
aliases?: string[];
|
|
81
|
-
envVars?: string[];
|
|
82
|
-
models?: ModelProviderConfig;
|
|
83
|
-
auth: ProviderAuthMethod[];
|
|
84
|
-
formatApiKey?: (cred: AuthProfileCredential) => string;
|
|
85
|
-
};
|
|
86
|
-
type PluginLogger = {
|
|
87
|
-
debug?: (message: string) => void;
|
|
88
|
-
info: (message: string) => void;
|
|
89
|
-
warn: (message: string) => void;
|
|
90
|
-
error: (message: string) => void;
|
|
91
|
-
};
|
|
92
|
-
type OpenClawPluginService = {
|
|
93
|
-
id: string;
|
|
94
|
-
start: () => void | Promise<void>;
|
|
95
|
-
stop?: () => void | Promise<void>;
|
|
96
|
-
};
|
|
97
|
-
type CommandHandler = {
|
|
98
|
-
name: string;
|
|
99
|
-
description: string;
|
|
100
|
-
acceptsArgs?: boolean;
|
|
101
|
-
requireAuth?: boolean;
|
|
102
|
-
handler: (ctx: {
|
|
103
|
-
args?: string;
|
|
104
|
-
}) => Promise<{
|
|
105
|
-
text: string;
|
|
106
|
-
}>;
|
|
107
|
-
};
|
|
108
|
-
type OpenClawPluginApi = {
|
|
109
|
-
id: string;
|
|
110
|
-
name: string;
|
|
111
|
-
version?: string;
|
|
112
|
-
description?: string;
|
|
113
|
-
source: string;
|
|
114
|
-
config: Record<string, unknown> & {
|
|
115
|
-
models?: {
|
|
116
|
-
providers?: Record<string, ModelProviderConfig>;
|
|
117
|
-
};
|
|
118
|
-
agents?: Record<string, unknown>;
|
|
119
|
-
};
|
|
120
|
-
pluginConfig?: Record<string, unknown>;
|
|
121
|
-
logger: PluginLogger;
|
|
122
|
-
registerProvider: (provider: ProviderPlugin) => void;
|
|
123
|
-
registerTool: (tool: unknown, opts?: unknown) => void;
|
|
124
|
-
registerHook: (events: string | string[], handler: unknown, opts?: unknown) => void;
|
|
125
|
-
registerHttpRoute: (params: {
|
|
126
|
-
path: string;
|
|
127
|
-
handler: unknown;
|
|
128
|
-
}) => void;
|
|
129
|
-
registerService: (service: OpenClawPluginService) => void;
|
|
130
|
-
registerCommand: (command: CommandHandler) => void;
|
|
131
|
-
resolvePath: (input: string) => string;
|
|
132
|
-
on: (hookName: string, handler: unknown, opts?: unknown) => void;
|
|
133
|
-
};
|
|
134
|
-
type OpenClawPluginDefinition = {
|
|
135
|
-
id?: string;
|
|
136
|
-
name?: string;
|
|
137
|
-
description?: string;
|
|
138
|
-
version?: string;
|
|
139
|
-
register?: (api: OpenClawPluginApi) => void | Promise<void>;
|
|
140
|
-
activate?: (api: OpenClawPluginApi) => void | Promise<void>;
|
|
141
|
-
};
|
|
142
|
-
declare const plugin: OpenClawPluginDefinition;
|
|
143
|
-
|
|
144
|
-
export { plugin as default };
|