@almadar/agent 1.1.3 → 1.2.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/dist/agent/index.d.ts +7 -245
- package/dist/agent/index.js +325 -2
- package/dist/agent/index.js.map +1 -1
- package/dist/{firestore-checkpointer-DxbQ10ve.d.ts → firestore-checkpointer-CkNKXoun.d.ts} +1 -1
- package/dist/index-D-Ahuo6F.d.ts +854 -0
- package/dist/index.d.ts +708 -56
- package/dist/index.js +2070 -4
- package/dist/index.js.map +1 -1
- package/dist/persistence/index.d.ts +2 -2
- package/package.json +17 -16
- package/LICENSE +0 -72
package/dist/agent/index.d.ts
CHANGED
|
@@ -1,251 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
import { S as SubagentEventCallback, O as OrbitalCompleteCallback, D as DomainOrbitalCompleteCallback } from '../orbital-subagent-cNfTLdXQ.js';
|
|
3
|
-
import { BaseCheckpointSaver } from '@langchain/langgraph-checkpoint';
|
|
4
|
-
import { P as PersistenceMode, b as FirestoreDb, d as SessionMetadata, e as SessionRecord } from '../firestore-checkpointer-DxbQ10ve.js';
|
|
1
|
+
export { E as EVENT_BUDGETS, S as SessionManager, e as SessionManagerOptions, f as Skill, g as SkillAgentOptions, h as SkillAgentResult, i as SkillLoader, j as SkillMeta, k as SkillRefLoader, m as createSkillAgent, p as getBudgetWarningMessage, q as getEventBudget, r as getInterruptConfig, t as resumeSkillAgent } from '../index-D-Ahuo6F.js';
|
|
5
2
|
export { Command } from '@langchain/langgraph';
|
|
3
|
+
export { P as PersistenceMode, S as SessionMetadata, e as SessionRecord } from '../firestore-checkpointer-CkNKXoun.js';
|
|
4
|
+
import '@almadar/llm';
|
|
5
|
+
import '../orbital-subagent-cNfTLdXQ.js';
|
|
6
6
|
import '@langchain/core/tools';
|
|
7
7
|
import 'zod';
|
|
8
8
|
import '../api-types-BW_58thJ.js';
|
|
9
9
|
import '@almadar/core/types';
|
|
10
|
+
import '@langchain/langgraph-checkpoint';
|
|
11
|
+
import '@almadar/core';
|
|
12
|
+
import '@langchain/core/messages';
|
|
10
13
|
import '@langchain/core/runnables';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Session Manager
|
|
14
|
-
*
|
|
15
|
-
* Unified session management API with pluggable persistence backends.
|
|
16
|
-
* Supports in-memory (development) and Firestore (production) backends.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
interface SessionManagerOptions {
|
|
20
|
-
/**
|
|
21
|
-
* Persistence mode.
|
|
22
|
-
* @default 'memory'
|
|
23
|
-
*/
|
|
24
|
-
mode?: PersistenceMode;
|
|
25
|
-
/**
|
|
26
|
-
* Firestore database instance. Required when mode is 'firestore'.
|
|
27
|
-
*/
|
|
28
|
-
firestoreDb?: FirestoreDb;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Unified session management for agent sessions.
|
|
32
|
-
*
|
|
33
|
-
* Handles both session metadata and LangGraph checkpointers.
|
|
34
|
-
*/
|
|
35
|
-
declare class SessionManager {
|
|
36
|
-
private mode;
|
|
37
|
-
private memoryBackend;
|
|
38
|
-
private memoryCheckpointers;
|
|
39
|
-
private firestoreCheckpointer;
|
|
40
|
-
private firestoreSessionStore;
|
|
41
|
-
constructor(options?: SessionManagerOptions);
|
|
42
|
-
/**
|
|
43
|
-
* Get the persistence mode.
|
|
44
|
-
*/
|
|
45
|
-
getMode(): PersistenceMode;
|
|
46
|
-
/**
|
|
47
|
-
* Get or create a checkpointer for a session.
|
|
48
|
-
*/
|
|
49
|
-
getCheckpointer(threadId: string): BaseCheckpointSaver<any>;
|
|
50
|
-
/**
|
|
51
|
-
* Store session metadata.
|
|
52
|
-
*/
|
|
53
|
-
store(threadId: string, metadata: SessionMetadata): void;
|
|
54
|
-
/**
|
|
55
|
-
* Get session metadata (sync, memory only).
|
|
56
|
-
*/
|
|
57
|
-
get(threadId: string): SessionMetadata | undefined;
|
|
58
|
-
/**
|
|
59
|
-
* Get session metadata (async, supports Firestore).
|
|
60
|
-
*/
|
|
61
|
-
getAsync(threadId: string): Promise<SessionMetadata | undefined>;
|
|
62
|
-
/**
|
|
63
|
-
* Clear a session's checkpointer and metadata.
|
|
64
|
-
*/
|
|
65
|
-
clear(threadId: string): boolean;
|
|
66
|
-
/**
|
|
67
|
-
* List all sessions (sync, memory only).
|
|
68
|
-
*/
|
|
69
|
-
list(): SessionRecord[];
|
|
70
|
-
/**
|
|
71
|
-
* List all sessions (async, supports Firestore).
|
|
72
|
-
*/
|
|
73
|
-
listAsync(): Promise<SessionRecord[]>;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Skill-Based DeepAgent Factory
|
|
78
|
-
*
|
|
79
|
-
* Creates DeepAgent instances that use skills as the primary prompt source.
|
|
80
|
-
* No custom system prompts - all agent behavior is defined through skills.
|
|
81
|
-
*
|
|
82
|
-
* Uses deepagents library primitives:
|
|
83
|
-
* - createDeepAgent() for agent creation
|
|
84
|
-
* - FilesystemBackend for file operations
|
|
85
|
-
*
|
|
86
|
-
* Skill loading is injected via SkillLoader functions, keeping this package
|
|
87
|
-
* independent of any specific skill registry location.
|
|
88
|
-
*/
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Skill definition loaded by the consumer.
|
|
92
|
-
*/
|
|
93
|
-
interface Skill {
|
|
94
|
-
name: string;
|
|
95
|
-
description: string;
|
|
96
|
-
content: string;
|
|
97
|
-
allowedTools?: string[];
|
|
98
|
-
version?: string;
|
|
99
|
-
references: string[];
|
|
100
|
-
path?: string;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Function to load a skill by name.
|
|
104
|
-
*/
|
|
105
|
-
type SkillLoader = (name: string) => Skill | null;
|
|
106
|
-
/**
|
|
107
|
-
* Function to load a skill reference by skill name and ref name.
|
|
108
|
-
*/
|
|
109
|
-
type SkillRefLoader = (skillName: string, refName: string) => string | null;
|
|
110
|
-
/**
|
|
111
|
-
* Options for creating a skill agent.
|
|
112
|
-
*/
|
|
113
|
-
interface SkillAgentOptions {
|
|
114
|
-
/** Required: The skill(s) to use. Can be a single skill or array of skills. */
|
|
115
|
-
skill: string | string[];
|
|
116
|
-
/** Required: Working directory for the agent */
|
|
117
|
-
workDir: string;
|
|
118
|
-
/** Required: Function to load skills */
|
|
119
|
-
skillLoader: SkillLoader;
|
|
120
|
-
/** Optional: Function to load skill references */
|
|
121
|
-
skillRefLoader?: SkillRefLoader;
|
|
122
|
-
/** Optional: Thread ID for session continuity */
|
|
123
|
-
threadId?: string;
|
|
124
|
-
/** Optional: LLM provider */
|
|
125
|
-
provider?: LLMProvider;
|
|
126
|
-
/** Optional: Model name */
|
|
127
|
-
model?: string;
|
|
128
|
-
/** Optional: Enable verbose logging */
|
|
129
|
-
verbose?: boolean;
|
|
130
|
-
/** Optional: Disable human-in-the-loop interrupts (for eval/testing) */
|
|
131
|
-
noInterrupt?: boolean;
|
|
132
|
-
/** Optional: Callback for subagent events (orbital generation) */
|
|
133
|
-
onSubagentEvent?: SubagentEventCallback;
|
|
134
|
-
/** Optional: Session manager instance (shared across requests) */
|
|
135
|
-
sessionManager?: SessionManager;
|
|
136
|
-
/** Optional: Extracted requirements from analysis phase (for orbital skill) */
|
|
137
|
-
requirements?: {
|
|
138
|
-
entities?: string[];
|
|
139
|
-
states?: string[];
|
|
140
|
-
events?: string[];
|
|
141
|
-
guards?: string[];
|
|
142
|
-
pages?: string[];
|
|
143
|
-
effects?: string[];
|
|
144
|
-
rawRequirements?: string[];
|
|
145
|
-
};
|
|
146
|
-
/** Optional: GitHub integration configuration */
|
|
147
|
-
githubConfig?: {
|
|
148
|
-
/** GitHub personal access token */
|
|
149
|
-
token: string;
|
|
150
|
-
/** Repository owner (e.g., 'octocat') */
|
|
151
|
-
owner?: string;
|
|
152
|
-
/** Repository name (e.g., 'hello-world') */
|
|
153
|
-
repo?: string;
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Result from creating a skill agent.
|
|
158
|
-
*/
|
|
159
|
-
interface SkillAgentResult {
|
|
160
|
-
/** The agent instance (from deepagents library) */
|
|
161
|
-
agent: any;
|
|
162
|
-
/** Thread ID for session resumption */
|
|
163
|
-
threadId: string;
|
|
164
|
-
/** The loaded skill(s) - primary skill when single, all skills when multiple */
|
|
165
|
-
skill: Skill;
|
|
166
|
-
/** All loaded skills (same as skill when single) */
|
|
167
|
-
skills: Skill[];
|
|
168
|
-
/** Working directory */
|
|
169
|
-
workDir: string;
|
|
170
|
-
/** Orbital tool setter for wiring SSE callback (if orbital tool enabled) */
|
|
171
|
-
setOrbitalEventCallback?: (callback: SubagentEventCallback) => void;
|
|
172
|
-
/** Orbital tool setter for wiring persistence callback */
|
|
173
|
-
setOrbitalCompleteCallback?: (callback: OrbitalCompleteCallback) => void;
|
|
174
|
-
/** Domain orbital callback for lean skill Firestore persistence */
|
|
175
|
-
setDomainCompleteCallback?: (callback: DomainOrbitalCompleteCallback) => void;
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Create a skill-based DeepAgent.
|
|
179
|
-
*
|
|
180
|
-
* Uses the specified skill's content as the system prompt.
|
|
181
|
-
* When multiple skills are provided, the agent sees all skill descriptions
|
|
182
|
-
* and can choose the appropriate one based on the user's request.
|
|
183
|
-
*
|
|
184
|
-
* @throws Error if any skill is not found
|
|
185
|
-
*/
|
|
186
|
-
declare function createSkillAgent(options: SkillAgentOptions): Promise<SkillAgentResult>;
|
|
187
|
-
/**
|
|
188
|
-
* Resume a skill agent session.
|
|
189
|
-
*
|
|
190
|
-
* Loads the skill from session metadata and creates agent with same threadId.
|
|
191
|
-
*
|
|
192
|
-
* @throws Error if session not found
|
|
193
|
-
*/
|
|
194
|
-
declare function resumeSkillAgent(threadId: string, options: {
|
|
195
|
-
verbose?: boolean;
|
|
196
|
-
noInterrupt?: boolean;
|
|
197
|
-
skillLoader: SkillLoader;
|
|
198
|
-
skillRefLoader?: SkillRefLoader;
|
|
199
|
-
sessionManager?: SessionManager;
|
|
200
|
-
}): Promise<SkillAgentResult>;
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Event Budget Configuration
|
|
204
|
-
*
|
|
205
|
-
* Prevents runaway agent loops with soft/hard event limits per skill.
|
|
206
|
-
*/
|
|
207
|
-
/**
|
|
208
|
-
* Event budget limits for different skills.
|
|
209
|
-
* - soft: Warning threshold (agent reminded to finish up)
|
|
210
|
-
* - hard: Maximum events before forced completion
|
|
211
|
-
*/
|
|
212
|
-
declare const EVENT_BUDGETS: Record<string, {
|
|
213
|
-
soft: number;
|
|
214
|
-
hard: number;
|
|
215
|
-
}>;
|
|
216
|
-
/**
|
|
217
|
-
* Get event budget for a skill.
|
|
218
|
-
*/
|
|
219
|
-
declare function getEventBudget(skillName: string): {
|
|
220
|
-
soft: number;
|
|
221
|
-
hard: number;
|
|
222
|
-
};
|
|
223
|
-
/**
|
|
224
|
-
* Generate a budget warning message to inject into the agent.
|
|
225
|
-
*/
|
|
226
|
-
declare function getBudgetWarningMessage(eventCount: number, budget: {
|
|
227
|
-
soft: number;
|
|
228
|
-
hard: number;
|
|
229
|
-
}): string | null;
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Interrupt Configuration
|
|
233
|
-
*
|
|
234
|
-
* Human-in-the-loop configuration for agent tools.
|
|
235
|
-
*/
|
|
236
|
-
/**
|
|
237
|
-
* Skill metadata (minimal interface for interrupt config).
|
|
238
|
-
*/
|
|
239
|
-
interface SkillMeta {
|
|
240
|
-
name: string;
|
|
241
|
-
allowedTools?: string[];
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Get interrupt configuration for a skill.
|
|
245
|
-
*
|
|
246
|
-
* Default: require approval for execute tool.
|
|
247
|
-
* Skills can override via frontmatter (future enhancement).
|
|
248
|
-
*/
|
|
249
|
-
declare function getInterruptConfig(_skill: SkillMeta): Record<string, boolean>;
|
|
250
|
-
|
|
251
|
-
export { EVENT_BUDGETS, PersistenceMode, SessionManager, type SessionManagerOptions, SessionMetadata, SessionRecord, type Skill, type SkillAgentOptions, type SkillAgentResult, type SkillLoader, type SkillMeta, type SkillRefLoader, createSkillAgent, getBudgetWarningMessage, getEventBudget, getInterruptConfig, resumeSkillAgent };
|
package/dist/agent/index.js
CHANGED
|
@@ -3258,9 +3258,13 @@ var SessionManager = class {
|
|
|
3258
3258
|
constructor(options = {}) {
|
|
3259
3259
|
this.firestoreCheckpointer = null;
|
|
3260
3260
|
this.firestoreSessionStore = null;
|
|
3261
|
+
this.memoryManager = null;
|
|
3262
|
+
this.compactionConfig = null;
|
|
3261
3263
|
this.mode = options.mode ?? "memory";
|
|
3262
3264
|
this.memoryBackend = new MemorySessionBackend();
|
|
3263
3265
|
this.memoryCheckpointers = /* @__PURE__ */ new Map();
|
|
3266
|
+
this.memoryManager = options.memoryManager ?? null;
|
|
3267
|
+
this.compactionConfig = options.compactionConfig ?? null;
|
|
3264
3268
|
if (this.mode === "firestore" && options.firestoreDb) {
|
|
3265
3269
|
this.firestoreCheckpointer = new FirestoreCheckpointer({ db: options.firestoreDb });
|
|
3266
3270
|
this.firestoreSessionStore = new FirestoreSessionStore({ db: options.firestoreDb });
|
|
@@ -3360,6 +3364,260 @@ var SessionManager = class {
|
|
|
3360
3364
|
}
|
|
3361
3365
|
return this.memoryBackend.list();
|
|
3362
3366
|
}
|
|
3367
|
+
// ============================================================================
|
|
3368
|
+
// Session → Memory Sync (GAP-002D)
|
|
3369
|
+
// ============================================================================
|
|
3370
|
+
/**
|
|
3371
|
+
* Sync a completed session to orbital memory.
|
|
3372
|
+
* This enables the agent to learn from past sessions.
|
|
3373
|
+
*
|
|
3374
|
+
* @param threadId - The session thread ID
|
|
3375
|
+
* @param userId - The user ID for memory association
|
|
3376
|
+
* @param sessionData - Additional session data to record
|
|
3377
|
+
* @returns Promise that resolves when sync is complete
|
|
3378
|
+
*/
|
|
3379
|
+
async syncSessionToMemory(threadId, userId, sessionData) {
|
|
3380
|
+
if (!this.memoryManager) {
|
|
3381
|
+
console.warn("[SessionManager] No memory manager configured, skipping session sync");
|
|
3382
|
+
return;
|
|
3383
|
+
}
|
|
3384
|
+
const metadata = this.get(threadId);
|
|
3385
|
+
if (!metadata) {
|
|
3386
|
+
console.warn(`[SessionManager] Session ${threadId} not found, skipping sync`);
|
|
3387
|
+
return;
|
|
3388
|
+
}
|
|
3389
|
+
try {
|
|
3390
|
+
await this.memoryManager.recordGeneration(userId, {
|
|
3391
|
+
threadId,
|
|
3392
|
+
prompt: sessionData.inputDescription,
|
|
3393
|
+
skill: metadata.skill,
|
|
3394
|
+
generatedSchema: sessionData.generatedOrbital ? { name: sessionData.generatedOrbital } : void 0,
|
|
3395
|
+
patterns: sessionData.patternsUsed ?? [],
|
|
3396
|
+
entities: sessionData.entities ?? [],
|
|
3397
|
+
success: sessionData.success,
|
|
3398
|
+
completedAt: /* @__PURE__ */ new Date()
|
|
3399
|
+
});
|
|
3400
|
+
if (sessionData.patternsUsed && sessionData.patternsUsed.length > 0) {
|
|
3401
|
+
await this.memoryManager.updateUserPreferences(userId, {
|
|
3402
|
+
preferredPatterns: sessionData.patternsUsed,
|
|
3403
|
+
commonEntities: sessionData.entities
|
|
3404
|
+
});
|
|
3405
|
+
}
|
|
3406
|
+
if (sessionData.entities && sessionData.entities.length > 0) {
|
|
3407
|
+
await this.memoryManager.updateProjectContext(sessionData.appId, {
|
|
3408
|
+
userId,
|
|
3409
|
+
existingEntities: sessionData.entities
|
|
3410
|
+
});
|
|
3411
|
+
}
|
|
3412
|
+
console.log(`[SessionManager] Session ${threadId} synced to memory for user ${userId}`);
|
|
3413
|
+
} catch (error) {
|
|
3414
|
+
console.error("[SessionManager] Failed to sync session to memory:", error);
|
|
3415
|
+
}
|
|
3416
|
+
}
|
|
3417
|
+
// ============================================================================
|
|
3418
|
+
// Interrupt → Memory Sync (GAP-003: Interrupt Memory)
|
|
3419
|
+
// ============================================================================
|
|
3420
|
+
/**
|
|
3421
|
+
* Record an interrupt decision to memory.
|
|
3422
|
+
* This enables learning from HITL (Human-in-the-Loop) decisions.
|
|
3423
|
+
*
|
|
3424
|
+
* @param sessionId - The session thread ID
|
|
3425
|
+
* @param userId - The user who made the decision
|
|
3426
|
+
* @param interruptData - The interrupt decision data
|
|
3427
|
+
* @returns Promise that resolves when sync is complete
|
|
3428
|
+
*/
|
|
3429
|
+
async recordInterruptDecision(sessionId, userId, interruptData) {
|
|
3430
|
+
if (!this.memoryManager) {
|
|
3431
|
+
console.warn("[SessionManager] No memory manager configured, skipping interrupt sync");
|
|
3432
|
+
return;
|
|
3433
|
+
}
|
|
3434
|
+
try {
|
|
3435
|
+
await this.memoryManager.recordInterruptDecision(sessionId, userId, interruptData);
|
|
3436
|
+
console.log(`[SessionManager] Interrupt recorded for user ${userId}: ${interruptData.toolName} ${interruptData.decision}`);
|
|
3437
|
+
} catch (error) {
|
|
3438
|
+
console.error("[SessionManager] Failed to record interrupt:", error);
|
|
3439
|
+
}
|
|
3440
|
+
}
|
|
3441
|
+
/**
|
|
3442
|
+
* Get interrupt history for a session.
|
|
3443
|
+
*/
|
|
3444
|
+
async getSessionInterrupts(sessionId) {
|
|
3445
|
+
if (!this.memoryManager) {
|
|
3446
|
+
return [];
|
|
3447
|
+
}
|
|
3448
|
+
return this.memoryManager.getSessionInterrupts(sessionId);
|
|
3449
|
+
}
|
|
3450
|
+
/**
|
|
3451
|
+
* Check if a tool should be auto-approved for a user.
|
|
3452
|
+
*/
|
|
3453
|
+
async shouldAutoApproveTool(userId, toolName) {
|
|
3454
|
+
if (!this.memoryManager) {
|
|
3455
|
+
return false;
|
|
3456
|
+
}
|
|
3457
|
+
return this.memoryManager.shouldAutoApproveTool(userId, toolName);
|
|
3458
|
+
}
|
|
3459
|
+
// ============================================================================
|
|
3460
|
+
// Checkpoint Management (GAP-004: Checkpoint → Memory)
|
|
3461
|
+
// ============================================================================
|
|
3462
|
+
/**
|
|
3463
|
+
* Record a checkpoint to memory for learning.
|
|
3464
|
+
*
|
|
3465
|
+
* @param userId - The user who owns this checkpoint
|
|
3466
|
+
* @param checkpointData - Checkpoint information
|
|
3467
|
+
* @returns Promise that resolves when checkpoint is recorded
|
|
3468
|
+
*/
|
|
3469
|
+
async recordCheckpoint(userId, checkpointData) {
|
|
3470
|
+
if (!this.memoryManager) {
|
|
3471
|
+
console.warn("[SessionManager] No memory manager configured, skipping checkpoint record");
|
|
3472
|
+
return;
|
|
3473
|
+
}
|
|
3474
|
+
try {
|
|
3475
|
+
await this.memoryManager.recordCheckpoint(userId, checkpointData);
|
|
3476
|
+
console.log(`[SessionManager] Checkpoint ${checkpointData.checkpointId} recorded for user ${userId}`);
|
|
3477
|
+
} catch (error) {
|
|
3478
|
+
console.error("[SessionManager] Failed to record checkpoint:", error);
|
|
3479
|
+
}
|
|
3480
|
+
}
|
|
3481
|
+
/**
|
|
3482
|
+
* Record a rollback to a checkpoint.
|
|
3483
|
+
*
|
|
3484
|
+
* @param userId - The user who performed the rollback
|
|
3485
|
+
* @param checkpointId - The checkpoint rolled back to
|
|
3486
|
+
* @param reason - Optional reason for rollback
|
|
3487
|
+
* @returns Promise that resolves when rollback is recorded
|
|
3488
|
+
*/
|
|
3489
|
+
async recordRollback(userId, checkpointId, reason) {
|
|
3490
|
+
if (!this.memoryManager) {
|
|
3491
|
+
console.warn("[SessionManager] No memory manager configured, skipping rollback record");
|
|
3492
|
+
return;
|
|
3493
|
+
}
|
|
3494
|
+
try {
|
|
3495
|
+
await this.memoryManager.recordRollback(userId, checkpointId, reason);
|
|
3496
|
+
console.log(`[SessionManager] Rollback to ${checkpointId} recorded for user ${userId}`);
|
|
3497
|
+
} catch (error) {
|
|
3498
|
+
console.error("[SessionManager] Failed to record rollback:", error);
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
/**
|
|
3502
|
+
* Mark a checkpoint as successful (terminal state).
|
|
3503
|
+
*
|
|
3504
|
+
* @param userId - The user who owns this checkpoint
|
|
3505
|
+
* @param checkpointId - The checkpoint that was successful
|
|
3506
|
+
* @returns Promise that resolves when success is recorded
|
|
3507
|
+
*/
|
|
3508
|
+
async markCheckpointSuccessful(userId, checkpointId) {
|
|
3509
|
+
if (!this.memoryManager) {
|
|
3510
|
+
console.warn("[SessionManager] No memory manager configured, skipping success mark");
|
|
3511
|
+
return;
|
|
3512
|
+
}
|
|
3513
|
+
try {
|
|
3514
|
+
await this.memoryManager.markCheckpointSuccessful(userId, checkpointId);
|
|
3515
|
+
console.log(`[SessionManager] Checkpoint ${checkpointId} marked as successful for user ${userId}`);
|
|
3516
|
+
} catch (error) {
|
|
3517
|
+
console.error("[SessionManager] Failed to mark checkpoint as successful:", error);
|
|
3518
|
+
}
|
|
3519
|
+
}
|
|
3520
|
+
/**
|
|
3521
|
+
* Get checkpoint history for a thread.
|
|
3522
|
+
*
|
|
3523
|
+
* @param threadId - The thread to get checkpoints for
|
|
3524
|
+
* @returns Array of checkpoint records
|
|
3525
|
+
*/
|
|
3526
|
+
async getThreadCheckpoints(threadId) {
|
|
3527
|
+
if (!this.memoryManager) {
|
|
3528
|
+
return [];
|
|
3529
|
+
}
|
|
3530
|
+
return this.memoryManager.getThreadCheckpoints(threadId);
|
|
3531
|
+
}
|
|
3532
|
+
/**
|
|
3533
|
+
* Get frequently rolled-back checkpoints (problem areas).
|
|
3534
|
+
*
|
|
3535
|
+
* @param userId - The user to get problem checkpoints for
|
|
3536
|
+
* @param minRollbackCount - Minimum rollback count (default: 2)
|
|
3537
|
+
* @returns Array of checkpoint records with rollback issues
|
|
3538
|
+
*/
|
|
3539
|
+
async getProblemCheckpoints(userId, minRollbackCount = 2) {
|
|
3540
|
+
if (!this.memoryManager) {
|
|
3541
|
+
return [];
|
|
3542
|
+
}
|
|
3543
|
+
return this.memoryManager.getProblemCheckpoints(userId, minRollbackCount);
|
|
3544
|
+
}
|
|
3545
|
+
// ============================================================================
|
|
3546
|
+
// Context Compaction (GAP-005)
|
|
3547
|
+
// ============================================================================
|
|
3548
|
+
/**
|
|
3549
|
+
* Get the context compaction configuration.
|
|
3550
|
+
* @returns The compaction configuration or null if not configured.
|
|
3551
|
+
*/
|
|
3552
|
+
getCompactionConfig() {
|
|
3553
|
+
return this.compactionConfig;
|
|
3554
|
+
}
|
|
3555
|
+
/**
|
|
3556
|
+
* Check if a session's messages need compaction based on token count.
|
|
3557
|
+
* Uses character-based estimation for quick checks.
|
|
3558
|
+
*
|
|
3559
|
+
* @param messages - Array of messages to check
|
|
3560
|
+
* @returns True if compaction is recommended
|
|
3561
|
+
*/
|
|
3562
|
+
shouldCompactMessages(messages) {
|
|
3563
|
+
if (!this.compactionConfig) {
|
|
3564
|
+
return false;
|
|
3565
|
+
}
|
|
3566
|
+
const totalChars = messages.reduce((sum, msg) => {
|
|
3567
|
+
const content = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
|
|
3568
|
+
return sum + content.length;
|
|
3569
|
+
}, 0);
|
|
3570
|
+
const estimatedTokens = totalChars / 4;
|
|
3571
|
+
const threshold = this.compactionConfig.maxTokens ?? 15e4;
|
|
3572
|
+
return estimatedTokens > threshold * 0.8;
|
|
3573
|
+
}
|
|
3574
|
+
/**
|
|
3575
|
+
* Record a compaction event for a session.
|
|
3576
|
+
* This helps track when and why compaction occurs.
|
|
3577
|
+
*
|
|
3578
|
+
* @param threadId - The session thread ID
|
|
3579
|
+
* @param originalMessageCount - Number of messages before compaction
|
|
3580
|
+
* @param compactedMessageCount - Number of messages after compaction
|
|
3581
|
+
* @param reason - Reason for compaction
|
|
3582
|
+
*/
|
|
3583
|
+
async recordCompaction(threadId, originalMessageCount, compactedMessageCount, reason) {
|
|
3584
|
+
if (!this.memoryManager) {
|
|
3585
|
+
console.warn("[SessionManager] No memory manager configured, skipping compaction record");
|
|
3586
|
+
return;
|
|
3587
|
+
}
|
|
3588
|
+
try {
|
|
3589
|
+
const metadata = this.get(threadId);
|
|
3590
|
+
if (metadata) {
|
|
3591
|
+
const compactionInfo = {
|
|
3592
|
+
timestamp: Date.now(),
|
|
3593
|
+
originalMessageCount,
|
|
3594
|
+
compactedMessageCount,
|
|
3595
|
+
reason: reason ?? "token_limit"
|
|
3596
|
+
};
|
|
3597
|
+
const meta = metadata;
|
|
3598
|
+
const existingCompactions = meta.compactions ?? [];
|
|
3599
|
+
meta.compactions = [...existingCompactions, compactionInfo];
|
|
3600
|
+
this.store(threadId, metadata);
|
|
3601
|
+
}
|
|
3602
|
+
console.log(`[SessionManager] Compaction recorded for ${threadId}: ${originalMessageCount} \u2192 ${compactedMessageCount} messages`);
|
|
3603
|
+
} catch (error) {
|
|
3604
|
+
console.error("[SessionManager] Failed to record compaction:", error);
|
|
3605
|
+
}
|
|
3606
|
+
}
|
|
3607
|
+
/**
|
|
3608
|
+
* Get compaction history for a session.
|
|
3609
|
+
*
|
|
3610
|
+
* @param threadId - The session thread ID
|
|
3611
|
+
* @returns Array of compaction events
|
|
3612
|
+
*/
|
|
3613
|
+
getCompactionHistory(threadId) {
|
|
3614
|
+
const metadata = this.get(threadId);
|
|
3615
|
+
if (!metadata) {
|
|
3616
|
+
return [];
|
|
3617
|
+
}
|
|
3618
|
+
const meta = metadata;
|
|
3619
|
+
return meta.compactions ?? [];
|
|
3620
|
+
}
|
|
3363
3621
|
};
|
|
3364
3622
|
|
|
3365
3623
|
// src/agent/interrupt-config.ts
|
|
@@ -3520,10 +3778,30 @@ ${skillSummaries}
|
|
|
3520
3778
|
|
|
3521
3779
|
${skillContents}`;
|
|
3522
3780
|
}
|
|
3781
|
+
let userPreferences = null;
|
|
3782
|
+
let projectContext = null;
|
|
3783
|
+
if (options.memoryManager && options.userId) {
|
|
3784
|
+
try {
|
|
3785
|
+
userPreferences = await options.memoryManager.getUserPreferences(options.userId);
|
|
3786
|
+
if (userPreferences && verbose) {
|
|
3787
|
+
console.log(`[SkillAgent] Loaded user preferences: ${userPreferences.namingConvention} naming, ${userPreferences.preferredPatterns.length} patterns`);
|
|
3788
|
+
}
|
|
3789
|
+
if (options.appId) {
|
|
3790
|
+
projectContext = await options.memoryManager.getProjectContext(options.appId);
|
|
3791
|
+
if (projectContext && verbose) {
|
|
3792
|
+
console.log(`[SkillAgent] Loaded project context: ${projectContext.existingEntities.length} entities`);
|
|
3793
|
+
}
|
|
3794
|
+
}
|
|
3795
|
+
} catch (error) {
|
|
3796
|
+
console.warn("[SkillAgent] Failed to load memory:", error);
|
|
3797
|
+
}
|
|
3798
|
+
}
|
|
3799
|
+
const memoryContext = buildMemoryContext(userPreferences, projectContext);
|
|
3523
3800
|
const systemPrompt = [
|
|
3524
3801
|
BASE_SYSTEM_PROMPT,
|
|
3525
3802
|
"\n## Skill Instructions\n\n" + skillInstructions,
|
|
3526
|
-
references ? "\n## Reference Documentation\n\n" + references : ""
|
|
3803
|
+
references ? "\n## Reference Documentation\n\n" + references : "",
|
|
3804
|
+
memoryContext ? "\n## User Context\n\n" + memoryContext : ""
|
|
3527
3805
|
].filter(Boolean).join("\n");
|
|
3528
3806
|
const threadId = providedThreadId || v4();
|
|
3529
3807
|
const llmClient = createLLMClient(provider, model, verbose);
|
|
@@ -3626,9 +3904,54 @@ ${skillContents}`;
|
|
|
3626
3904
|
workDir,
|
|
3627
3905
|
setOrbitalEventCallback,
|
|
3628
3906
|
setOrbitalCompleteCallback,
|
|
3629
|
-
setDomainCompleteCallback
|
|
3907
|
+
setDomainCompleteCallback,
|
|
3908
|
+
userPreferences,
|
|
3909
|
+
projectContext,
|
|
3910
|
+
memoryManager: options.memoryManager
|
|
3630
3911
|
};
|
|
3631
3912
|
}
|
|
3913
|
+
function buildMemoryContext(userPreferences, projectContext) {
|
|
3914
|
+
const sections = [];
|
|
3915
|
+
if (userPreferences) {
|
|
3916
|
+
const prefs = [];
|
|
3917
|
+
if (userPreferences.namingConvention) {
|
|
3918
|
+
prefs.push(`- Preferred naming: ${userPreferences.namingConvention}`);
|
|
3919
|
+
}
|
|
3920
|
+
if (userPreferences.validationStyle) {
|
|
3921
|
+
prefs.push(`- Validation style: ${userPreferences.validationStyle}`);
|
|
3922
|
+
}
|
|
3923
|
+
if (userPreferences.preferredPatterns.length > 0) {
|
|
3924
|
+
prefs.push(`- Preferred patterns: ${userPreferences.preferredPatterns.join(", ")}`);
|
|
3925
|
+
}
|
|
3926
|
+
if (userPreferences.commonEntities.length > 0) {
|
|
3927
|
+
prefs.push(`- Commonly used entities: ${userPreferences.commonEntities.join(", ")}`);
|
|
3928
|
+
}
|
|
3929
|
+
if (prefs.length > 0) {
|
|
3930
|
+
sections.push(`### User Preferences
|
|
3931
|
+
${prefs.join("\n")}`);
|
|
3932
|
+
}
|
|
3933
|
+
}
|
|
3934
|
+
if (projectContext) {
|
|
3935
|
+
const ctx = [];
|
|
3936
|
+
if (projectContext.projectName) {
|
|
3937
|
+
ctx.push(`- Project: ${projectContext.projectName}`);
|
|
3938
|
+
}
|
|
3939
|
+
if (projectContext.domain) {
|
|
3940
|
+
ctx.push(`- Domain: ${projectContext.domain}`);
|
|
3941
|
+
}
|
|
3942
|
+
if (projectContext.existingEntities.length > 0) {
|
|
3943
|
+
ctx.push(`- Existing entities: ${projectContext.existingEntities.join(", ")}`);
|
|
3944
|
+
}
|
|
3945
|
+
if (projectContext.conventions.length > 0) {
|
|
3946
|
+
ctx.push(`- Project conventions: ${projectContext.conventions.join(", ")}`);
|
|
3947
|
+
}
|
|
3948
|
+
if (ctx.length > 0) {
|
|
3949
|
+
sections.push(`### Project Context
|
|
3950
|
+
${ctx.join("\n")}`);
|
|
3951
|
+
}
|
|
3952
|
+
}
|
|
3953
|
+
return sections.length > 0 ? sections.join("\n\n") : null;
|
|
3954
|
+
}
|
|
3632
3955
|
async function resumeSkillAgent(threadId, options) {
|
|
3633
3956
|
const sessions = options.sessionManager ?? getDefaultSessionManager();
|
|
3634
3957
|
const metadata = sessions.get(threadId);
|