kongbrain 0.1.0
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/LICENSE +21 -0
- package/README.md +385 -0
- package/openclaw.plugin.json +66 -0
- package/package.json +65 -0
- package/src/acan.ts +309 -0
- package/src/causal.ts +237 -0
- package/src/cognitive-check.ts +330 -0
- package/src/config.ts +64 -0
- package/src/context-engine.ts +487 -0
- package/src/daemon-manager.ts +148 -0
- package/src/daemon-types.ts +65 -0
- package/src/embeddings.ts +77 -0
- package/src/errors.ts +43 -0
- package/src/graph-context.ts +989 -0
- package/src/hooks/after-tool-call.ts +99 -0
- package/src/hooks/before-prompt-build.ts +44 -0
- package/src/hooks/before-tool-call.ts +86 -0
- package/src/hooks/llm-output.ts +173 -0
- package/src/identity.ts +218 -0
- package/src/index.ts +435 -0
- package/src/intent.ts +190 -0
- package/src/memory-daemon.ts +495 -0
- package/src/orchestrator.ts +348 -0
- package/src/prefetch.ts +200 -0
- package/src/reflection.ts +280 -0
- package/src/retrieval-quality.ts +266 -0
- package/src/schema.surql +387 -0
- package/src/skills.ts +343 -0
- package/src/soul.ts +936 -0
- package/src/state.ts +119 -0
- package/src/surreal.ts +1371 -0
- package/src/tools/core-memory.ts +120 -0
- package/src/tools/introspect.ts +329 -0
- package/src/tools/recall.ts +102 -0
- package/src/wakeup.ts +318 -0
- package/src/workspace-migrate.ts +752 -0
package/src/state.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { PluginCompleteParams, PluginCompleteResult } from "openclaw/plugin-sdk";
|
|
2
|
+
import type { KongBrainConfig } from "./config.js";
|
|
3
|
+
import type { SurrealStore } from "./surreal.js";
|
|
4
|
+
import type { EmbeddingService } from "./embeddings.js";
|
|
5
|
+
import type { AdaptiveConfig } from "./orchestrator.js";
|
|
6
|
+
import type { MemoryDaemon } from "./daemon-manager.js";
|
|
7
|
+
|
|
8
|
+
/** Provider-agnostic LLM completion function. */
|
|
9
|
+
export type CompleteFn = (params: PluginCompleteParams) => Promise<PluginCompleteResult>;
|
|
10
|
+
|
|
11
|
+
// --- Per-session mutable state ---
|
|
12
|
+
|
|
13
|
+
const DEFAULT_TOOL_LIMIT = 10;
|
|
14
|
+
|
|
15
|
+
export class SessionState {
|
|
16
|
+
readonly sessionId: string;
|
|
17
|
+
readonly sessionKey: string;
|
|
18
|
+
|
|
19
|
+
// Turn tracking
|
|
20
|
+
lastUserTurnId = "";
|
|
21
|
+
lastUserText = "";
|
|
22
|
+
lastAssistantText = "";
|
|
23
|
+
toolCallCount = 0;
|
|
24
|
+
toolLimit = DEFAULT_TOOL_LIMIT;
|
|
25
|
+
turnTextLength = 0;
|
|
26
|
+
toolCallsSinceLastText = 0;
|
|
27
|
+
softInterrupted = false;
|
|
28
|
+
turnStartMs = Date.now();
|
|
29
|
+
userTurnCount = 0;
|
|
30
|
+
|
|
31
|
+
// Thinking capture
|
|
32
|
+
readonly pendingThinking: string[] = [];
|
|
33
|
+
|
|
34
|
+
// Memory daemon
|
|
35
|
+
daemon: MemoryDaemon | null = null;
|
|
36
|
+
newContentTokens = 0;
|
|
37
|
+
readonly DAEMON_TOKEN_THRESHOLD = 12000;
|
|
38
|
+
|
|
39
|
+
// Current adaptive config (set by orchestrator preflight each turn)
|
|
40
|
+
currentConfig: AdaptiveConfig | null = null;
|
|
41
|
+
|
|
42
|
+
// Pending tool args for artifact tracking
|
|
43
|
+
readonly pendingToolArgs = new Map<string, unknown>();
|
|
44
|
+
|
|
45
|
+
// 5-pillar IDs (populated at bootstrap)
|
|
46
|
+
agentId = "";
|
|
47
|
+
projectId = "";
|
|
48
|
+
taskId = "";
|
|
49
|
+
|
|
50
|
+
constructor(sessionId: string, sessionKey: string) {
|
|
51
|
+
this.sessionId = sessionId;
|
|
52
|
+
this.sessionKey = sessionKey;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Reset per-turn counters at the start of each prompt. */
|
|
56
|
+
resetTurn(): void {
|
|
57
|
+
this.toolCallCount = 0;
|
|
58
|
+
this.toolLimit = DEFAULT_TOOL_LIMIT;
|
|
59
|
+
this.turnTextLength = 0;
|
|
60
|
+
this.toolCallsSinceLastText = 0;
|
|
61
|
+
this.softInterrupted = false;
|
|
62
|
+
this.turnStartMs = Date.now();
|
|
63
|
+
this.pendingThinking.length = 0;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// --- Global plugin state (shared across all sessions) ---
|
|
68
|
+
|
|
69
|
+
/** Function to enqueue a system event visible to the user. */
|
|
70
|
+
export type EnqueueSystemEventFn = (text: string, options: { sessionKey: string }) => boolean;
|
|
71
|
+
|
|
72
|
+
export class GlobalPluginState {
|
|
73
|
+
readonly config: KongBrainConfig;
|
|
74
|
+
readonly store: SurrealStore;
|
|
75
|
+
readonly embeddings: EmbeddingService;
|
|
76
|
+
readonly complete: CompleteFn;
|
|
77
|
+
workspaceDir?: string;
|
|
78
|
+
enqueueSystemEvent?: EnqueueSystemEventFn;
|
|
79
|
+
private sessions = new Map<string, SessionState>();
|
|
80
|
+
|
|
81
|
+
constructor(
|
|
82
|
+
config: KongBrainConfig,
|
|
83
|
+
store: SurrealStore,
|
|
84
|
+
embeddings: EmbeddingService,
|
|
85
|
+
complete: CompleteFn,
|
|
86
|
+
) {
|
|
87
|
+
this.config = config;
|
|
88
|
+
this.store = store;
|
|
89
|
+
this.embeddings = embeddings;
|
|
90
|
+
this.complete = complete;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Get or create a SessionState for the given session key. */
|
|
94
|
+
getOrCreateSession(sessionKey: string, sessionId: string): SessionState {
|
|
95
|
+
let session = this.sessions.get(sessionKey);
|
|
96
|
+
if (!session) {
|
|
97
|
+
session = new SessionState(sessionId, sessionKey);
|
|
98
|
+
this.sessions.set(sessionKey, session);
|
|
99
|
+
}
|
|
100
|
+
return session;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Get an existing session by key. */
|
|
104
|
+
getSession(sessionKey: string): SessionState | undefined {
|
|
105
|
+
return this.sessions.get(sessionKey);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Remove a session from the map (after dispose/cleanup). */
|
|
109
|
+
removeSession(sessionKey: string): void {
|
|
110
|
+
this.sessions.delete(sessionKey);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Shut down all shared resources. */
|
|
114
|
+
async shutdown(): Promise<void> {
|
|
115
|
+
this.sessions.clear();
|
|
116
|
+
await this.embeddings.dispose();
|
|
117
|
+
await this.store.dispose();
|
|
118
|
+
}
|
|
119
|
+
}
|