@vainplex/openclaw-knowledge-engine 0.1.2 → 0.1.3

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.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import type { OpenClawPluginApi } from './src/types.js';
2
- declare const _default: (api: OpenClawPluginApi, context: {
3
- workspace: string;
4
- }) => void;
5
- export default _default;
2
+ declare const plugin: {
3
+ id: string;
4
+ name: string;
5
+ description: string;
6
+ version: string;
7
+ register(api: OpenClawPluginApi): void;
8
+ };
9
+ export default plugin;
package/dist/index.js CHANGED
@@ -1,29 +1,33 @@
1
- // index.ts
2
- import { resolveConfig } from './src/config.js';
1
+ // index.ts — OpenClaw Plugin Entry Point
2
+ import { loadConfig } from './src/config-loader.js';
3
3
  import { HookManager } from './src/hooks.js';
4
- // The main entry point for the OpenClaw plugin.
5
- // This function is called by the OpenClaw host during plugin loading.
6
- export default (api, context) => {
7
- const { pluginConfig, logger } = api;
8
- const { workspace: openClawWorkspace } = context;
9
- // 1. Resolve and validate the configuration
10
- const config = resolveConfig(pluginConfig, logger, openClawWorkspace);
11
- if (!config) {
12
- logger.error('Failed to initialize Knowledge Engine: Invalid configuration. The plugin will be disabled.');
13
- return;
14
- }
15
- if (!config.enabled) {
16
- logger.info('Knowledge Engine is disabled in the configuration.');
17
- return;
18
- }
19
- // 2. Initialize the Hook Manager with the resolved config
20
- try {
21
- const hookManager = new HookManager(api, config);
22
- // 3. Register all the event hooks
23
- hookManager.registerHooks();
24
- logger.info('Knowledge Engine plugin initialized successfully.');
25
- }
26
- catch (err) {
27
- logger.error('An unexpected error occurred during Knowledge Engine initialization.', err);
28
- }
4
+ const plugin = {
5
+ id: 'openclaw-knowledge-engine',
6
+ name: 'OpenClaw Knowledge Engine',
7
+ description: 'Real-time knowledge extraction — entities, facts, and relationships from conversations',
8
+ version: '0.1.2',
9
+ register(api) {
10
+ const { pluginConfig, logger } = api;
11
+ // 1. Resolve and validate the configuration
12
+ const { config } = loadConfig(pluginConfig, logger);
13
+ if (!config) {
14
+ logger.error('Knowledge Engine: Invalid configuration — plugin disabled.');
15
+ return;
16
+ }
17
+ if (!config.enabled) {
18
+ logger.info('[knowledge-engine] Disabled via config');
19
+ return;
20
+ }
21
+ // 2. Initialize the Hook Manager and register hooks
22
+ try {
23
+ logger.info('[knowledge-engine] Registering hooks...');
24
+ const hookManager = new HookManager(api, config);
25
+ hookManager.registerHooks();
26
+ logger.info('[knowledge-engine] Ready');
27
+ }
28
+ catch (err) {
29
+ logger.error('[knowledge-engine] Failed to initialize', err);
30
+ }
31
+ },
29
32
  };
33
+ export default plugin;
@@ -0,0 +1,22 @@
1
+ import type { KnowledgeConfig, Logger } from "./types.js";
2
+ /** Minimal inline config that lives in openclaw.json */
3
+ export interface InlineConfig {
4
+ readonly enabled?: boolean;
5
+ /** Override path to external config file */
6
+ readonly configPath?: string;
7
+ }
8
+ export interface ConfigLoadResult {
9
+ readonly config: KnowledgeConfig | null;
10
+ readonly source: "inline" | "file" | "defaults";
11
+ readonly filePath?: string;
12
+ }
13
+ /**
14
+ * Load knowledge-engine config with the following priority:
15
+ *
16
+ * 1. Legacy inline config (full config in openclaw.json) → use it directly
17
+ * 2. External file (configPath or default location) → read + resolve
18
+ * 3. Graceful defaults → everything enabled, fail-open
19
+ *
20
+ * NEVER throws. Worst case: returns defaults with a warning.
21
+ */
22
+ export declare function loadConfig(pluginConfig: Record<string, unknown> | undefined, logger: Logger): ConfigLoadResult;
@@ -0,0 +1,104 @@
1
+ import { readFileSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
2
+ import { join, dirname } from "node:path";
3
+ import { resolveConfig, DEFAULT_CONFIG } from "./config.js";
4
+ const DEFAULT_CONFIG_DIR = join(process.env["HOME"] ?? "/tmp", ".openclaw", "plugins", "openclaw-knowledge-engine");
5
+ const DEFAULT_CONFIG_FILENAME = "config.json";
6
+ function isRecord(v) {
7
+ return typeof v === "object" && v !== null && !Array.isArray(v);
8
+ }
9
+ /**
10
+ * Determine whether pluginConfig is a full (legacy) inline config
11
+ * or just the minimal inline pointer.
12
+ *
13
+ * Heuristic: if it has keys beyond `enabled` and `configPath`,
14
+ * treat it as legacy inline config.
15
+ */
16
+ function isLegacyInlineConfig(raw) {
17
+ const inlineOnlyKeys = new Set(["enabled", "configPath"]);
18
+ return Object.keys(raw).some((k) => !inlineOnlyKeys.has(k));
19
+ }
20
+ /**
21
+ * Read and parse a JSON file. Returns null on any error.
22
+ */
23
+ function readJsonFile(path, logger) {
24
+ try {
25
+ if (!existsSync(path))
26
+ return null;
27
+ const raw = readFileSync(path, "utf-8");
28
+ const parsed = JSON.parse(raw);
29
+ if (!isRecord(parsed)) {
30
+ logger.warn(`[knowledge-engine] Config file is not an object: ${path}`);
31
+ return null;
32
+ }
33
+ return parsed;
34
+ }
35
+ catch (e) {
36
+ logger.warn(`[knowledge-engine] Failed to read config file ${path}: ${e instanceof Error ? e.message : String(e)}`);
37
+ return null;
38
+ }
39
+ }
40
+ /**
41
+ * Apply inline `enabled` override to a file-loaded config (immutably).
42
+ */
43
+ function applyInlineOverrides(fileConfig, inline) {
44
+ if (typeof inline["enabled"] === "boolean") {
45
+ return { ...fileConfig, enabled: inline["enabled"] };
46
+ }
47
+ return fileConfig;
48
+ }
49
+ /**
50
+ * Bootstrap a default config file and return its contents.
51
+ * Returns null if writing or reading back fails.
52
+ */
53
+ function bootstrapConfig(path, logger) {
54
+ try {
55
+ const dir = dirname(path);
56
+ if (!existsSync(dir))
57
+ mkdirSync(dir, { recursive: true });
58
+ writeFileSync(path, JSON.stringify(DEFAULT_CONFIG, null, 2) + "\n", "utf-8");
59
+ logger.info(`[knowledge-engine] Created default config at ${path}`);
60
+ return readJsonFile(path, logger);
61
+ }
62
+ catch (e) {
63
+ logger.warn(`[knowledge-engine] Failed to write default config: ${e instanceof Error ? e.message : String(e)}`);
64
+ return null;
65
+ }
66
+ }
67
+ /**
68
+ * Load knowledge-engine config with the following priority:
69
+ *
70
+ * 1. Legacy inline config (full config in openclaw.json) → use it directly
71
+ * 2. External file (configPath or default location) → read + resolve
72
+ * 3. Graceful defaults → everything enabled, fail-open
73
+ *
74
+ * NEVER throws. Worst case: returns defaults with a warning.
75
+ */
76
+ export function loadConfig(pluginConfig, logger) {
77
+ const raw = pluginConfig ?? {};
78
+ // Priority 1: Legacy inline config (backward compatible)
79
+ if (isLegacyInlineConfig(raw)) {
80
+ logger.info("[knowledge-engine] Using inline config from openclaw.json");
81
+ return { config: resolveConfig(raw, logger), source: "inline" };
82
+ }
83
+ // Priority 2: External config file
84
+ const configPath = typeof raw["configPath"] === "string"
85
+ ? raw["configPath"]
86
+ : join(DEFAULT_CONFIG_DIR, DEFAULT_CONFIG_FILENAME);
87
+ const fileConfig = readJsonFile(configPath, logger);
88
+ if (fileConfig !== null) {
89
+ const merged = applyInlineOverrides(fileConfig, raw);
90
+ logger.info(`[knowledge-engine] Loaded config from ${configPath}`);
91
+ return { config: resolveConfig(merged, logger), source: "file", filePath: configPath };
92
+ }
93
+ // File missing → bootstrap with defaults
94
+ if (!existsSync(configPath)) {
95
+ const bootstrapped = bootstrapConfig(configPath, logger);
96
+ if (bootstrapped !== null) {
97
+ const merged = applyInlineOverrides(bootstrapped, raw);
98
+ return { config: resolveConfig(merged, logger), source: "file", filePath: configPath };
99
+ }
100
+ }
101
+ // Priority 3: Graceful defaults (file broken or unwritable)
102
+ logger.warn("[knowledge-engine] Falling back to default config");
103
+ return { config: resolveConfig(undefined, logger), source: "defaults" };
104
+ }
@@ -12,4 +12,4 @@ export declare const DEFAULT_CONFIG: Omit<KnowledgeConfig, 'workspace'>;
12
12
  * @param openClawWorkspace The root workspace directory provided by OpenClaw.
13
13
  * @returns A fully resolved KnowledgeConfig, or null if validation fails.
14
14
  */
15
- export declare function resolveConfig(userConfig: Record<string, unknown>, logger: Logger, openClawWorkspace: string): KnowledgeConfig | null;
15
+ export declare function resolveConfig(userConfig: Record<string, unknown> | undefined | null, logger: Logger, openClawWorkspace?: string): KnowledgeConfig | null;
@@ -78,8 +78,9 @@ function resolveTilde(ws, logger, fallback) {
78
78
  * @returns A fully resolved KnowledgeConfig, or null if validation fails.
79
79
  */
80
80
  export function resolveConfig(userConfig, logger, openClawWorkspace) {
81
- const config = mergeConfigDefaults(userConfig, openClawWorkspace);
82
- const fallbackWs = path.join(openClawWorkspace, 'knowledge-engine');
81
+ const ws = openClawWorkspace ?? process.cwd();
82
+ const config = mergeConfigDefaults(userConfig ?? {}, ws);
83
+ const fallbackWs = path.join(ws, 'knowledge-engine');
83
84
  config.workspace = resolveTilde(config.workspace, logger, fallbackWs);
84
85
  const errors = validateConfig(config);
85
86
  if (errors.length > 0) {
@@ -1,123 +1,127 @@
1
1
  {
2
- "id": "@vainplex/openclaw-knowledge-engine",
3
- "config": {
4
- "enabled": {
5
- "type": "boolean",
6
- "default": true,
7
- "description": "Whether the knowledge engine plugin is enabled."
8
- },
9
- "workspace": {
10
- "type": "string",
11
- "default": "~/.clawd/plugins/knowledge-engine",
12
- "description": "The directory to store knowledge files (entities.json, facts.json)."
13
- },
14
- "extraction": {
15
- "type": "object",
16
- "properties": {
17
- "regex": {
18
- "type": "object",
19
- "properties": {
20
- "enabled": {
21
- "type": "boolean",
22
- "default": true,
23
- "description": "Enable or disable high-speed regex-based entity extraction."
2
+ "id": "openclaw-knowledge-engine",
3
+ "configSchema": {
4
+ "type": "object",
5
+ "additionalProperties": true,
6
+ "properties": {
7
+ "enabled": {
8
+ "type": "boolean",
9
+ "default": true,
10
+ "description": "Whether the knowledge engine plugin is enabled."
11
+ },
12
+ "workspace": {
13
+ "type": "string",
14
+ "default": "~/.clawd/plugins/knowledge-engine",
15
+ "description": "The directory to store knowledge files (entities.json, facts.json)."
16
+ },
17
+ "extraction": {
18
+ "type": "object",
19
+ "properties": {
20
+ "regex": {
21
+ "type": "object",
22
+ "properties": {
23
+ "enabled": {
24
+ "type": "boolean",
25
+ "default": true,
26
+ "description": "Enable or disable high-speed regex-based entity extraction."
27
+ }
24
28
  }
25
- }
26
- },
27
- "llm": {
28
- "type": "object",
29
- "properties": {
30
- "enabled": {
31
- "type": "boolean",
32
- "default": true,
33
- "description": "Enable or disable high-fidelity LLM-based entity and fact extraction."
34
- },
35
- "model": {
36
- "type": "string",
37
- "default": "mistral:7b",
38
- "description": "The model name to use for the LLM API call (e.g., Ollama model)."
39
- },
40
- "endpoint": {
41
- "type": "string",
42
- "default": "http://localhost:11434/api/generate",
43
- "description": "The HTTP endpoint for the LLM generation API."
44
- },
45
- "batchSize": {
46
- "type": "number",
47
- "default": 10,
48
- "description": "Number of messages to batch together before sending to the LLM."
49
- },
50
- "cooldownMs": {
51
- "type": "number",
52
- "default": 30000,
53
- "description": "Milliseconds to wait after the last message before sending a batch to the LLM."
29
+ },
30
+ "llm": {
31
+ "type": "object",
32
+ "properties": {
33
+ "enabled": {
34
+ "type": "boolean",
35
+ "default": true,
36
+ "description": "Enable or disable high-fidelity LLM-based entity and fact extraction."
37
+ },
38
+ "model": {
39
+ "type": "string",
40
+ "default": "mistral:7b",
41
+ "description": "The model name to use for the LLM API call (e.g., Ollama model)."
42
+ },
43
+ "endpoint": {
44
+ "type": "string",
45
+ "default": "http://localhost:11434/api/generate",
46
+ "description": "The HTTP endpoint for the LLM generation API."
47
+ },
48
+ "batchSize": {
49
+ "type": "number",
50
+ "default": 10,
51
+ "description": "Number of messages to batch together before sending to the LLM."
52
+ },
53
+ "cooldownMs": {
54
+ "type": "number",
55
+ "default": 30000,
56
+ "description": "Milliseconds to wait after the last message before sending a batch to the LLM."
57
+ }
54
58
  }
55
59
  }
56
60
  }
57
- }
58
- },
59
- "decay": {
60
- "type": "object",
61
- "properties": {
62
- "enabled": {
63
- "type": "boolean",
64
- "default": true,
65
- "description": "Enable or disable the periodic decay of fact relevance."
66
- },
67
- "intervalHours": {
68
- "type": "number",
69
- "default": 24,
70
- "description": "How often (in hours) to run the decay process."
71
- },
72
- "rate": {
73
- "type": "number",
74
- "default": 0.02,
75
- "description": "The percentage of relevance to decay in each interval (e.g., 0.02 is 2%)."
61
+ },
62
+ "decay": {
63
+ "type": "object",
64
+ "properties": {
65
+ "enabled": {
66
+ "type": "boolean",
67
+ "default": true,
68
+ "description": "Enable or disable the periodic decay of fact relevance."
69
+ },
70
+ "intervalHours": {
71
+ "type": "number",
72
+ "default": 24,
73
+ "description": "How often (in hours) to run the decay process."
74
+ },
75
+ "rate": {
76
+ "type": "number",
77
+ "default": 0.02,
78
+ "description": "The percentage of relevance to decay in each interval (e.g., 0.02 is 2%)."
79
+ }
76
80
  }
77
- }
78
- },
79
- "embeddings": {
80
- "type": "object",
81
- "properties": {
82
- "enabled": {
83
- "type": "boolean",
84
- "default": false,
85
- "description": "Enable or disable syncing of facts to a vector database."
86
- },
87
- "endpoint": {
88
- "type": "string",
89
- "default": "http://localhost:8000/api/v1/collections/facts/add",
90
- "description": "The HTTP endpoint for the vector database's add API (ChromaDB compatible)."
91
- },
92
- "collectionName": {
93
- "type": "string",
94
- "default": "openclaw-facts",
95
- "description": "The name of the collection to use in the vector database."
96
- },
97
- "syncIntervalMinutes": {
98
- "type": "number",
99
- "default": 15,
100
- "description": "How often (in minutes) to sync new facts to the vector database."
81
+ },
82
+ "embeddings": {
83
+ "type": "object",
84
+ "properties": {
85
+ "enabled": {
86
+ "type": "boolean",
87
+ "default": false,
88
+ "description": "Enable or disable syncing of facts to a vector database."
89
+ },
90
+ "endpoint": {
91
+ "type": "string",
92
+ "default": "http://localhost:8000/api/v1/collections/facts/add",
93
+ "description": "The HTTP endpoint for the vector database's add API (ChromaDB compatible)."
94
+ },
95
+ "collectionName": {
96
+ "type": "string",
97
+ "default": "openclaw-facts",
98
+ "description": "The name of the collection to use in the vector database."
99
+ },
100
+ "syncIntervalMinutes": {
101
+ "type": "number",
102
+ "default": 15,
103
+ "description": "How often (in minutes) to sync new facts to the vector database."
104
+ }
101
105
  }
102
- }
103
- },
104
- "storage": {
105
- "type": "object",
106
- "properties": {
107
- "maxEntities": {
108
- "type": "number",
109
- "default": 5000,
110
- "description": "The maximum number of entities to store before pruning."
111
- },
112
- "maxFacts": {
113
- "type": "number",
114
- "default": 10000,
115
- "description": "The maximum number of facts to store before pruning."
116
- },
117
- "writeDebounceMs": {
118
- "type": "number",
119
- "default": 15000,
120
- "description": "Milliseconds to wait after a change before writing data to disk."
106
+ },
107
+ "storage": {
108
+ "type": "object",
109
+ "properties": {
110
+ "maxEntities": {
111
+ "type": "number",
112
+ "default": 5000,
113
+ "description": "The maximum number of entities to store before pruning."
114
+ },
115
+ "maxFacts": {
116
+ "type": "number",
117
+ "default": 10000,
118
+ "description": "The maximum number of facts to store before pruning."
119
+ },
120
+ "writeDebounceMs": {
121
+ "type": "number",
122
+ "default": 15000,
123
+ "description": "Milliseconds to wait after a change before writing data to disk."
124
+ }
121
125
  }
122
126
  }
123
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vainplex/openclaw-knowledge-engine",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "An OpenClaw plugin for real-time and batch knowledge extraction from conversational data.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -17,14 +17,18 @@
17
17
  "nlp",
18
18
  "entity-extraction"
19
19
  ],
20
- "author": "OpenClaw Community",
20
+ "author": "Albert Hild <a.hild@vainplex.de>",
21
21
  "license": "MIT",
22
22
  "repository": {
23
23
  "type": "git",
24
- "url": "https://github.com/alberthild/openclaw-knowledge-engine.git"
24
+ "url": "https://github.com/alberthild/vainplex-openclaw.git",
25
+ "directory": "packages/openclaw-knowledge-engine"
25
26
  },
26
27
  "openclaw": {
27
- "id": "@vainplex/openclaw-knowledge-engine"
28
+ "id": "openclaw-knowledge-engine",
29
+ "extensions": [
30
+ "./dist/index.js"
31
+ ]
28
32
  },
29
33
  "devDependencies": {
30
34
  "@types/node": "^20.11.24",
@@ -32,5 +36,11 @@
32
36
  "typescript": "^5.3.3",
33
37
  "tsx": "^4.7.1"
34
38
  },
35
- "type": "module"
39
+ "type": "module",
40
+ "files": [
41
+ "dist",
42
+ "openclaw.plugin.json",
43
+ "README.md",
44
+ "LICENSE"
45
+ ]
36
46
  }