@opencompress/opencompress 1.9.1 → 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 +47 -106
- 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
|
|
19
|
+
function persistApiKey(api2, apiKey) {
|
|
39
20
|
try {
|
|
40
|
-
const os = __require("os");
|
|
41
21
|
const fs = __require("fs");
|
|
42
22
|
const path = __require("path");
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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) {
|
|
73
|
-
try {
|
|
74
|
-
const os = __require("os");
|
|
75
|
-
const fs = __require("fs");
|
|
76
|
-
const path = __require("path");
|
|
77
|
-
const agentsDir = path.join(os.homedir(), ".openclaw", "agents");
|
|
78
|
-
if (!fs.existsSync(agentsDir)) return;
|
|
79
|
-
const agentDirs = fs.readdirSync(agentsDir);
|
|
80
|
-
for (const agent of agentDirs) {
|
|
81
|
-
const authPath = path.join(agentsDir, agent, "agent", "auth.json");
|
|
82
|
-
const authDir = path.dirname(authPath);
|
|
83
|
-
if (!fs.existsSync(authDir)) continue;
|
|
84
|
-
let data = {};
|
|
85
|
-
if (fs.existsSync(authPath)) {
|
|
86
|
-
try {
|
|
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();
|
|
@@ -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
|
}
|
|
@@ -318,8 +261,7 @@ var opencompressProvider = {
|
|
|
318
261
|
}
|
|
319
262
|
const data = await res.json();
|
|
320
263
|
spinner.stop("Account created!");
|
|
321
|
-
|
|
322
|
-
persistAgentAuthJson(data.apiKey);
|
|
264
|
+
persistApiKey(api, data.apiKey);
|
|
323
265
|
const enableNow = await ctx.prompter.text({
|
|
324
266
|
message: "Ready to compress? This will route your existing LLM providers through OpenCompress. Enable now? (yes/no)",
|
|
325
267
|
validate: (v) => {
|
|
@@ -372,22 +314,21 @@ var plugin = {
|
|
|
372
314
|
name: "OpenCompress",
|
|
373
315
|
description: "Transparent prompt compression \u2014 save 40-70% on Claude, GPT, and any LLM provider",
|
|
374
316
|
version: VERSION,
|
|
375
|
-
register(
|
|
376
|
-
const baseUrl =
|
|
377
|
-
|
|
378
|
-
const apiKey = getApiKey(
|
|
317
|
+
register(api2) {
|
|
318
|
+
const baseUrl = api2.pluginConfig?.baseUrl || DEFAULT_BASE_URL;
|
|
319
|
+
api2.registerProvider(opencompressProvider);
|
|
320
|
+
const apiKey = getApiKey(api2);
|
|
379
321
|
if (apiKey) {
|
|
380
|
-
|
|
381
|
-
persistAgentAuthJson(apiKey);
|
|
322
|
+
persistApiKey(api2, apiKey);
|
|
382
323
|
}
|
|
383
|
-
|
|
384
|
-
|
|
324
|
+
api2.logger.info("OpenCompress registered (transparent proxy mode)");
|
|
325
|
+
api2.registerCommand({
|
|
385
326
|
name: "compress-stats",
|
|
386
327
|
description: "Show OpenCompress usage statistics and savings",
|
|
387
328
|
acceptsArgs: true,
|
|
388
329
|
requireAuth: false,
|
|
389
330
|
handler: async () => {
|
|
390
|
-
const apiKey2 = getApiKey(
|
|
331
|
+
const apiKey2 = getApiKey(api2);
|
|
391
332
|
if (!apiKey2) {
|
|
392
333
|
return {
|
|
393
334
|
text: "No API key found. Run `openclaw onboard opencompress` to set up."
|
|
@@ -437,8 +378,8 @@ var plugin = {
|
|
|
437
378
|
}
|
|
438
379
|
}
|
|
439
380
|
});
|
|
440
|
-
|
|
441
|
-
|
|
381
|
+
api2.logger.info("Registered /compress-stats command");
|
|
382
|
+
api2.registerCommand({
|
|
442
383
|
name: "compress",
|
|
443
384
|
description: "Toggle transparent compression for all LLM providers",
|
|
444
385
|
acceptsArgs: true,
|
|
@@ -446,11 +387,11 @@ var plugin = {
|
|
|
446
387
|
handler: async (ctx) => {
|
|
447
388
|
const arg = ctx.args?.trim().toLowerCase();
|
|
448
389
|
if (arg === "on" || arg === "enable") {
|
|
449
|
-
const occKey2 = getApiKey(
|
|
390
|
+
const occKey2 = getApiKey(api2);
|
|
450
391
|
if (!occKey2) {
|
|
451
392
|
return { text: "No API key found. Run `openclaw onboard opencompress` first." };
|
|
452
393
|
}
|
|
453
|
-
const result = enableProxy(
|
|
394
|
+
const result = enableProxy(api2, baseUrl);
|
|
454
395
|
if (result.proxied.length === 0 && result.skipped.length === 0) {
|
|
455
396
|
return { text: "No providers found to proxy. Add LLM providers first." };
|
|
456
397
|
}
|
|
@@ -469,7 +410,7 @@ var plugin = {
|
|
|
469
410
|
return { text: lines.join("\n") };
|
|
470
411
|
}
|
|
471
412
|
if (arg === "off" || arg === "disable") {
|
|
472
|
-
const restored = disableProxy(
|
|
413
|
+
const restored = disableProxy(api2);
|
|
473
414
|
if (restored.length === 0) {
|
|
474
415
|
return { text: "Compression proxy was not active." };
|
|
475
416
|
}
|
|
@@ -484,7 +425,7 @@ var plugin = {
|
|
|
484
425
|
};
|
|
485
426
|
}
|
|
486
427
|
const proxyState = readProxyState();
|
|
487
|
-
const occKey = getApiKey(
|
|
428
|
+
const occKey = getApiKey(api2);
|
|
488
429
|
const statusLines = [
|
|
489
430
|
"**OpenCompress Transparent Proxy**",
|
|
490
431
|
"",
|
|
@@ -504,14 +445,14 @@ var plugin = {
|
|
|
504
445
|
return { text: statusLines.join("\n") };
|
|
505
446
|
}
|
|
506
447
|
});
|
|
507
|
-
|
|
448
|
+
api2.logger.info("Registered /compress command");
|
|
508
449
|
setTimeout(() => {
|
|
509
450
|
const proxyState = readProxyState();
|
|
510
|
-
const autoEnable =
|
|
451
|
+
const autoEnable = api2.pluginConfig?._autoEnableProxy;
|
|
511
452
|
if (proxyState.enabled || autoEnable) {
|
|
512
|
-
const result = enableProxy(
|
|
453
|
+
const result = enableProxy(api2, baseUrl);
|
|
513
454
|
if (result.proxied.length > 0) {
|
|
514
|
-
|
|
455
|
+
api2.logger.info(`Compression active: ${result.proxied.length} providers (${result.proxied.join(", ")})`);
|
|
515
456
|
}
|
|
516
457
|
}
|
|
517
458
|
}, 2e3);
|
|
@@ -523,7 +464,7 @@ var plugin = {
|
|
|
523
464
|
fs.mkdirSync(logDir, { recursive: true });
|
|
524
465
|
} catch {
|
|
525
466
|
}
|
|
526
|
-
|
|
467
|
+
api2.on("llm_input", (...args) => {
|
|
527
468
|
try {
|
|
528
469
|
const ts = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
|
|
529
470
|
const event = args[0] || {};
|
|
@@ -565,7 +506,7 @@ args: ${JSON.stringify(args?.map((a) => typeof a))}
|
|
|
565
506
|
}
|
|
566
507
|
}
|
|
567
508
|
});
|
|
568
|
-
|
|
509
|
+
api2.logger.info(`LLM input logging enabled \u2192 ${logDir}`);
|
|
569
510
|
}
|
|
570
511
|
};
|
|
571
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 };
|