@openbmb/clawxrouter 1.0.4
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/config.example.json +204 -0
- package/index.ts +398 -0
- package/openclaw.plugin.json +97 -0
- package/package.json +48 -0
- package/prompts/detection-system.md +50 -0
- package/prompts/token-saver-judge.md +25 -0
- package/src/config-schema.ts +210 -0
- package/src/dashboard-config-io.ts +25 -0
- package/src/detector.ts +230 -0
- package/src/guard-agent.ts +86 -0
- package/src/hooks.ts +1428 -0
- package/src/live-config.ts +75 -0
- package/src/llm-desensitize-worker.ts +7 -0
- package/src/llm-detect-worker.ts +7 -0
- package/src/local-model.ts +723 -0
- package/src/memory-isolation.ts +403 -0
- package/src/privacy-proxy.ts +683 -0
- package/src/prompt-loader.ts +101 -0
- package/src/provider.ts +268 -0
- package/src/router-pipeline.ts +380 -0
- package/src/routers/configurable.ts +208 -0
- package/src/routers/privacy.ts +102 -0
- package/src/routers/token-saver.ts +273 -0
- package/src/rules.ts +320 -0
- package/src/session-manager.ts +377 -0
- package/src/session-state.ts +471 -0
- package/src/stats-dashboard.ts +3402 -0
- package/src/sync-desensitize.ts +48 -0
- package/src/sync-detect.ts +49 -0
- package/src/token-stats.ts +358 -0
- package/src/types.ts +269 -0
- package/src/utils.ts +283 -0
- package/src/worker-loader.mjs +25 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { readFileSync, watch, type FSWatcher } from "node:fs";
|
|
2
|
+
import type { PrivacyConfig } from "./types.js";
|
|
3
|
+
import { defaultPrivacyConfig } from "./config-schema.js";
|
|
4
|
+
|
|
5
|
+
let liveConfig: PrivacyConfig = { ...defaultPrivacyConfig } as PrivacyConfig;
|
|
6
|
+
let configWatcher: FSWatcher | null = null;
|
|
7
|
+
|
|
8
|
+
/** Initialize live config from the plugin's startup config snapshot. */
|
|
9
|
+
export function initLiveConfig(pluginConfig: Record<string, unknown> | undefined): void {
|
|
10
|
+
const userConfig = (pluginConfig?.privacy ?? {}) as PrivacyConfig;
|
|
11
|
+
liveConfig = mergeConfig(userConfig);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Watch clawxrouter.json for external edits and hot-reload into liveConfig.
|
|
16
|
+
* Uses a debounce to avoid reloading multiple times on rapid writes.
|
|
17
|
+
*/
|
|
18
|
+
export function watchConfigFile(
|
|
19
|
+
configPath: string,
|
|
20
|
+
logger: { info: (msg: string) => void },
|
|
21
|
+
): void {
|
|
22
|
+
if (configWatcher) return;
|
|
23
|
+
let debounce: ReturnType<typeof setTimeout> | null = null;
|
|
24
|
+
try {
|
|
25
|
+
configWatcher = watch(configPath, () => {
|
|
26
|
+
if (debounce) clearTimeout(debounce);
|
|
27
|
+
debounce = setTimeout(() => {
|
|
28
|
+
try {
|
|
29
|
+
const raw = JSON.parse(readFileSync(configPath, "utf-8")) as Record<string, unknown>;
|
|
30
|
+
const privacy = (raw.privacy ?? {}) as PrivacyConfig;
|
|
31
|
+
liveConfig = mergeConfig(privacy);
|
|
32
|
+
logger.info("[ClawXrouter] clawxrouter.json changed — config hot-reloaded");
|
|
33
|
+
} catch { /* ignore parse errors from partial writes */ }
|
|
34
|
+
}, 300);
|
|
35
|
+
});
|
|
36
|
+
} catch { /* file may not exist yet — non-fatal */ }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Get the current live config (mutable, always up-to-date). */
|
|
40
|
+
export function getLiveConfig(): PrivacyConfig {
|
|
41
|
+
return liveConfig;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Hot-update the live config. Called from Dashboard save handler. */
|
|
45
|
+
export function updateLiveConfig(patch: Partial<PrivacyConfig>): void {
|
|
46
|
+
liveConfig = mergeConfig({ ...liveConfig, ...patch });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function mergeConfig(userConfig: PrivacyConfig): PrivacyConfig {
|
|
50
|
+
return {
|
|
51
|
+
...defaultPrivacyConfig,
|
|
52
|
+
...userConfig,
|
|
53
|
+
checkpoints: { ...defaultPrivacyConfig.checkpoints, ...userConfig.checkpoints },
|
|
54
|
+
rules: {
|
|
55
|
+
keywords: { ...defaultPrivacyConfig.rules?.keywords, ...userConfig.rules?.keywords },
|
|
56
|
+
patterns: { ...defaultPrivacyConfig.rules?.patterns, ...userConfig.rules?.patterns },
|
|
57
|
+
tools: {
|
|
58
|
+
S2: { ...defaultPrivacyConfig.rules?.tools?.S2, ...userConfig.rules?.tools?.S2 },
|
|
59
|
+
S3: { ...defaultPrivacyConfig.rules?.tools?.S3, ...userConfig.rules?.tools?.S3 },
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
localModel: { ...defaultPrivacyConfig.localModel, ...userConfig.localModel },
|
|
63
|
+
guardAgent: { ...defaultPrivacyConfig.guardAgent, ...userConfig.guardAgent },
|
|
64
|
+
session: { ...defaultPrivacyConfig.session, ...userConfig.session },
|
|
65
|
+
localProviders: [
|
|
66
|
+
...defaultPrivacyConfig.localProviders,
|
|
67
|
+
...(userConfig.localProviders ?? []),
|
|
68
|
+
],
|
|
69
|
+
modelPricing: {
|
|
70
|
+
...defaultPrivacyConfig.modelPricing,
|
|
71
|
+
...userConfig.modelPricing,
|
|
72
|
+
},
|
|
73
|
+
redaction: { ...defaultPrivacyConfig.redaction, ...userConfig.redaction },
|
|
74
|
+
} as PrivacyConfig;
|
|
75
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { runAsWorker } from "synckit";
|
|
2
|
+
import { desensitizeWithLocalModel } from "./local-model.js";
|
|
3
|
+
import type { PrivacyConfig } from "./types.js";
|
|
4
|
+
|
|
5
|
+
runAsWorker(async (content: string, config: PrivacyConfig, sessionKey?: string): Promise<{ desensitized: string; wasModelUsed: boolean; failed?: boolean }> => {
|
|
6
|
+
return await desensitizeWithLocalModel(content, config, sessionKey);
|
|
7
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { runAsWorker } from "synckit";
|
|
2
|
+
import { detectByLocalModel } from "./local-model.js";
|
|
3
|
+
import type { DetectionContext, DetectionResult, PrivacyConfig } from "./types.js";
|
|
4
|
+
|
|
5
|
+
runAsWorker(async (context: DetectionContext, config: PrivacyConfig): Promise<DetectionResult> => {
|
|
6
|
+
return await detectByLocalModel(context, config);
|
|
7
|
+
});
|