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/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
+ }