kagent-ts 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.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +395 -0
  3. package/dist/compression/interface.d.ts +26 -0
  4. package/dist/compression/interface.d.ts.map +1 -0
  5. package/dist/compression/interface.js +3 -0
  6. package/dist/compression/interface.js.map +1 -0
  7. package/dist/compression/sliding-window.d.ts +21 -0
  8. package/dist/compression/sliding-window.d.ts.map +1 -0
  9. package/dist/compression/sliding-window.js +55 -0
  10. package/dist/compression/sliding-window.js.map +1 -0
  11. package/dist/compression/types.d.ts +12 -0
  12. package/dist/compression/types.d.ts.map +1 -0
  13. package/dist/compression/types.js +3 -0
  14. package/dist/compression/types.js.map +1 -0
  15. package/dist/context/context-manager.d.ts +76 -0
  16. package/dist/context/context-manager.d.ts.map +1 -0
  17. package/dist/context/context-manager.js +132 -0
  18. package/dist/context/context-manager.js.map +1 -0
  19. package/dist/context/types.d.ts +35 -0
  20. package/dist/context/types.d.ts.map +1 -0
  21. package/dist/context/types.js +3 -0
  22. package/dist/context/types.js.map +1 -0
  23. package/dist/core/agent.d.ts +288 -0
  24. package/dist/core/agent.d.ts.map +1 -0
  25. package/dist/core/agent.js +398 -0
  26. package/dist/core/agent.js.map +1 -0
  27. package/dist/core/hooks.d.ts +34 -0
  28. package/dist/core/hooks.d.ts.map +1 -0
  29. package/dist/core/hooks.js +3 -0
  30. package/dist/core/hooks.js.map +1 -0
  31. package/dist/core/plan-solve-agent.d.ts +114 -0
  32. package/dist/core/plan-solve-agent.d.ts.map +1 -0
  33. package/dist/core/plan-solve-agent.js +450 -0
  34. package/dist/core/plan-solve-agent.js.map +1 -0
  35. package/dist/core/react-agent.d.ts +52 -0
  36. package/dist/core/react-agent.d.ts.map +1 -0
  37. package/dist/core/react-agent.js +266 -0
  38. package/dist/core/react-agent.js.map +1 -0
  39. package/dist/core/response-schema.d.ts +91 -0
  40. package/dist/core/response-schema.d.ts.map +1 -0
  41. package/dist/core/response-schema.js +292 -0
  42. package/dist/core/response-schema.js.map +1 -0
  43. package/dist/core/types.d.ts +6 -0
  44. package/dist/core/types.d.ts.map +1 -0
  45. package/dist/core/types.js +3 -0
  46. package/dist/core/types.js.map +1 -0
  47. package/dist/index.d.ts +39 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +67 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/llm/interface.d.ts +87 -0
  52. package/dist/llm/interface.d.ts.map +1 -0
  53. package/dist/llm/interface.js +3 -0
  54. package/dist/llm/interface.js.map +1 -0
  55. package/dist/llm/openai-provider.d.ts +92 -0
  56. package/dist/llm/openai-provider.d.ts.map +1 -0
  57. package/dist/llm/openai-provider.js +262 -0
  58. package/dist/llm/openai-provider.js.map +1 -0
  59. package/dist/messages/message.d.ts +50 -0
  60. package/dist/messages/message.d.ts.map +1 -0
  61. package/dist/messages/message.js +87 -0
  62. package/dist/messages/message.js.map +1 -0
  63. package/dist/messages/types.d.ts +31 -0
  64. package/dist/messages/types.d.ts.map +1 -0
  65. package/dist/messages/types.js +14 -0
  66. package/dist/messages/types.js.map +1 -0
  67. package/dist/preferences/preference-manager.d.ts +88 -0
  68. package/dist/preferences/preference-manager.d.ts.map +1 -0
  69. package/dist/preferences/preference-manager.js +196 -0
  70. package/dist/preferences/preference-manager.js.map +1 -0
  71. package/dist/preferences/types.d.ts +27 -0
  72. package/dist/preferences/types.d.ts.map +1 -0
  73. package/dist/preferences/types.js +3 -0
  74. package/dist/preferences/types.js.map +1 -0
  75. package/dist/session/session-manager.d.ts +56 -0
  76. package/dist/session/session-manager.d.ts.map +1 -0
  77. package/dist/session/session-manager.js +156 -0
  78. package/dist/session/session-manager.js.map +1 -0
  79. package/dist/session/session-types.d.ts +51 -0
  80. package/dist/session/session-types.d.ts.map +1 -0
  81. package/dist/session/session-types.js +3 -0
  82. package/dist/session/session-types.js.map +1 -0
  83. package/dist/skills/file-skill-loader.d.ts +88 -0
  84. package/dist/skills/file-skill-loader.d.ts.map +1 -0
  85. package/dist/skills/file-skill-loader.js +365 -0
  86. package/dist/skills/file-skill-loader.js.map +1 -0
  87. package/dist/skills/index.d.ts +4 -0
  88. package/dist/skills/index.d.ts.map +1 -0
  89. package/dist/skills/index.js +10 -0
  90. package/dist/skills/index.js.map +1 -0
  91. package/dist/skills/skill-manager.d.ts +133 -0
  92. package/dist/skills/skill-manager.d.ts.map +1 -0
  93. package/dist/skills/skill-manager.js +310 -0
  94. package/dist/skills/skill-manager.js.map +1 -0
  95. package/dist/skills/types.d.ts +42 -0
  96. package/dist/skills/types.d.ts.map +1 -0
  97. package/dist/skills/types.js +3 -0
  98. package/dist/skills/types.js.map +1 -0
  99. package/dist/tools/builtin/edit-file.d.ts +12 -0
  100. package/dist/tools/builtin/edit-file.d.ts.map +1 -0
  101. package/dist/tools/builtin/edit-file.js +123 -0
  102. package/dist/tools/builtin/edit-file.js.map +1 -0
  103. package/dist/tools/builtin/glob-search.d.ts +11 -0
  104. package/dist/tools/builtin/glob-search.d.ts.map +1 -0
  105. package/dist/tools/builtin/glob-search.js +264 -0
  106. package/dist/tools/builtin/glob-search.js.map +1 -0
  107. package/dist/tools/builtin/grep-search.d.ts +14 -0
  108. package/dist/tools/builtin/grep-search.d.ts.map +1 -0
  109. package/dist/tools/builtin/grep-search.js +264 -0
  110. package/dist/tools/builtin/grep-search.js.map +1 -0
  111. package/dist/tools/builtin/index.d.ts +21 -0
  112. package/dist/tools/builtin/index.d.ts.map +1 -0
  113. package/dist/tools/builtin/index.js +53 -0
  114. package/dist/tools/builtin/index.js.map +1 -0
  115. package/dist/tools/builtin/read-file.d.ts +11 -0
  116. package/dist/tools/builtin/read-file.d.ts.map +1 -0
  117. package/dist/tools/builtin/read-file.js +122 -0
  118. package/dist/tools/builtin/read-file.js.map +1 -0
  119. package/dist/tools/builtin/write-file.d.ts +10 -0
  120. package/dist/tools/builtin/write-file.d.ts.map +1 -0
  121. package/dist/tools/builtin/write-file.js +89 -0
  122. package/dist/tools/builtin/write-file.js.map +1 -0
  123. package/dist/tools/circuit-breaker.d.ts +77 -0
  124. package/dist/tools/circuit-breaker.d.ts.map +1 -0
  125. package/dist/tools/circuit-breaker.js +102 -0
  126. package/dist/tools/circuit-breaker.js.map +1 -0
  127. package/dist/tools/error-tracker.d.ts +116 -0
  128. package/dist/tools/error-tracker.d.ts.map +1 -0
  129. package/dist/tools/error-tracker.js +484 -0
  130. package/dist/tools/error-tracker.js.map +1 -0
  131. package/dist/tools/tool-registry.d.ts +87 -0
  132. package/dist/tools/tool-registry.d.ts.map +1 -0
  133. package/dist/tools/tool-registry.js +188 -0
  134. package/dist/tools/tool-registry.js.map +1 -0
  135. package/dist/tools/types.d.ts +95 -0
  136. package/dist/tools/types.d.ts.map +1 -0
  137. package/dist/tools/types.js +14 -0
  138. package/dist/tools/types.js.map +1 -0
  139. package/dist/utils/token-counter.d.ts +31 -0
  140. package/dist/utils/token-counter.d.ts.map +1 -0
  141. package/dist/utils/token-counter.js +105 -0
  142. package/dist/utils/token-counter.js.map +1 -0
  143. package/package.json +56 -0
@@ -0,0 +1,398 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Agent = void 0;
4
+ const context_manager_1 = require("../context/context-manager");
5
+ const tool_registry_1 = require("../tools/tool-registry");
6
+ const skill_manager_1 = require("../skills/skill-manager");
7
+ const session_manager_1 = require("../session/session-manager");
8
+ const preference_manager_1 = require("../preferences/preference-manager");
9
+ /**
10
+ * Abstract base Agent class.
11
+ *
12
+ * Provides the shared infrastructure:
13
+ * - LLM provider
14
+ * - Context window management
15
+ * - Tool registry (with circuit breaker support)
16
+ * - Skill manager (progressive disclosure)
17
+ * - User preferences (system-prompt injection)
18
+ * - Session manager (checkpoint persistence & resume)
19
+ * - Abort controller (cancellation via SIGINT)
20
+ *
21
+ * Subclasses implement the actual agent logic (e.g., ReActAgent).
22
+ */
23
+ class Agent {
24
+ llm;
25
+ contextManager;
26
+ toolRegistry;
27
+ skillManager;
28
+ /** The original core system prompt (before skill sections are appended). */
29
+ coreSystemPrompt;
30
+ /** User preferences — plain-text directives injected into the system prompt. */
31
+ preferences = {};
32
+ /** Preference manager for file-based persistence (optional). */
33
+ preferenceManager;
34
+ /** Lifecycle hooks for observing agent execution. */
35
+ hooks = {};
36
+ /**
37
+ * Trace ID of the most recent tool error waiting for LLM analysis.
38
+ * Set after a tool fails, cleared when the LLM's analysis is recorded. */
39
+ pendingErrorTraceId = null;
40
+ // ─── Session & Cancellation ─────────────────────────────────────────
41
+ /** Session manager for checkpoint persistence (optional). */
42
+ sessionManager;
43
+ /** Whether auto-checkpointing is enabled. */
44
+ checkpointingEnabled = false;
45
+ /** Abort controller for cancellation (SIGINT / user abort). */
46
+ abortController;
47
+ constructor(config) {
48
+ this.abortController = new AbortController();
49
+ this.llm = config.llm;
50
+ this.contextManager = config.contextManager ?? new context_manager_1.ContextManager();
51
+ // Prefer toolRegistry; fall back to plain tools array
52
+ if (config.toolRegistry) {
53
+ this.toolRegistry = config.toolRegistry;
54
+ }
55
+ else {
56
+ this.toolRegistry = new tool_registry_1.ToolRegistry(config.toolRetryCount, config.toolErrorTracker);
57
+ if (config.tools && config.tools.length > 0) {
58
+ this.toolRegistry.registerMany(config.tools);
59
+ }
60
+ }
61
+ // Skill manager — auto-bind the tool registry so skill tools register
62
+ if (config.skillManager) {
63
+ this.skillManager = config.skillManager;
64
+ }
65
+ else {
66
+ this.skillManager = new skill_manager_1.SkillManager();
67
+ }
68
+ this.skillManager.bindToolRegistry(this.toolRegistry);
69
+ // Register any skills passed directly
70
+ if (config.skills && config.skills.length > 0) {
71
+ this.skillManager.register(...config.skills);
72
+ }
73
+ // Register file-based skills if a directory is configured
74
+ if (config.skillsDir) {
75
+ this.skillManager.registerFromDirectory(config.skillsDir);
76
+ }
77
+ // Store core system prompt and set it
78
+ this.coreSystemPrompt = config.systemPrompt ?? "";
79
+ if (this.coreSystemPrompt) {
80
+ this.contextManager.setSystemMessage(this.coreSystemPrompt);
81
+ }
82
+ // Session manager — only created when session ID or session dir is provided
83
+ if (config.sessionId || config.sessionDir) {
84
+ this.sessionManager = new session_manager_1.SessionManager({
85
+ sessionId: config.sessionId,
86
+ sessionDir: config.sessionDir,
87
+ });
88
+ }
89
+ this.checkpointingEnabled = config.enableCheckpointing ?? false;
90
+ // ── Hooks ──────────────────────────────────────────────────────────────
91
+ this.hooks = config.hooks ?? {};
92
+ // ── User Preferences ─────────────────────────────────────────────────
93
+ this.preferenceManager = config.preferenceManager;
94
+ this.preferences = config.preferences ?? {};
95
+ // If a PreferenceManager is configured, load persisted prefs
96
+ // and merge with inline prefs (inline values take precedence)
97
+ if (this.preferenceManager) {
98
+ const persisted = this.preferenceManager.getAll();
99
+ this.preferences = { ...persisted, ...this.preferences };
100
+ }
101
+ // If preferences are non-empty, inject them into the system prompt
102
+ if (Object.keys(this.preferences).length > 0) {
103
+ this.rebuildSystemPrompt();
104
+ }
105
+ }
106
+ // ─── Tool Management ─────────────────────────────────────────────────
107
+ /**
108
+ * Add a tool to the agent's registry.
109
+ */
110
+ addTool(tool) {
111
+ this.toolRegistry.register(tool);
112
+ }
113
+ /**
114
+ * Get the tool registry (for advanced management).
115
+ */
116
+ getToolRegistry() {
117
+ return this.toolRegistry;
118
+ }
119
+ // ─── Skill Management ────────────────────────────────────────────────
120
+ /**
121
+ * Add skills to the agent's SkillManager.
122
+ */
123
+ addSkill(...skills) {
124
+ this.skillManager.register(...skills);
125
+ }
126
+ /**
127
+ * Get the SkillManager (for advanced management).
128
+ */
129
+ getSkillManager() {
130
+ return this.skillManager;
131
+ }
132
+ /**
133
+ * Activate a skill by name and rebuild the system prompt to include it.
134
+ *
135
+ * @returns true if the skill was newly activated.
136
+ */
137
+ activateSkill(name) {
138
+ const activated = this.skillManager.activate(name);
139
+ if (activated) {
140
+ this.rebuildSystemPrompt();
141
+ }
142
+ return activated;
143
+ }
144
+ /**
145
+ * Deactivate a skill and rebuild the system prompt.
146
+ */
147
+ deactivateSkill(name) {
148
+ const deactivated = this.skillManager.deactivate(name);
149
+ if (deactivated) {
150
+ this.rebuildSystemPrompt();
151
+ }
152
+ return deactivated;
153
+ }
154
+ /**
155
+ * Rebuild the system message from the core prompt + user preferences + active skills.
156
+ * Called automatically when skills are activated/deactivated or preferences change.
157
+ */
158
+ rebuildSystemPrompt() {
159
+ const prefsPrompt = preference_manager_1.PreferenceManager.toPrompt(this.preferences);
160
+ const skillsHint = this.skillManager.buildAvailableSkillsHint();
161
+ const skillsContent = this.skillManager.buildSkillsPrompt();
162
+ const fullPrompt = this.coreSystemPrompt + prefsPrompt + skillsHint + skillsContent;
163
+ this.contextManager.setSystemMessage(fullPrompt);
164
+ }
165
+ /**
166
+ * Pre-iteration maintenance:
167
+ * 1. Compress context window if needed.
168
+ * 2. Auto-reload preferences from disk if the file was manually edited.
169
+ */
170
+ checkAndCompress() {
171
+ if (this.contextManager.shouldCompress()) {
172
+ const { removedCount } = this.contextManager.compress();
173
+ console.log(`[Context] Compression triggered: removed ${removedCount} messages.`);
174
+ }
175
+ // Auto-reload preferences if the file was manually edited
176
+ if (this.preferenceManager?.hasFileChanged()) {
177
+ this.preferences = this.preferenceManager.reload();
178
+ this.rebuildSystemPrompt();
179
+ console.log("[Preferences] Reloaded from disk (file changed).");
180
+ }
181
+ }
182
+ // ─── Cancellation ────────────────────────────────────────────────────
183
+ /**
184
+ * Cancel the current run.
185
+ *
186
+ * This triggers the AbortSignal (passed to the LLM provider) and prevents
187
+ * any further checkpoint saves. Call this when the user presses SIGINT
188
+ * to discard the session rather than persisting it.
189
+ */
190
+ cancel() {
191
+ this.abortController.abort();
192
+ }
193
+ /**
194
+ * Whether the run has been cancelled by the user.
195
+ */
196
+ get isCancelled() {
197
+ return this.abortController.signal.aborted;
198
+ }
199
+ // ─── User Preferences ──────────────────────────────────────────────────
200
+ /**
201
+ * Set a single user preference and rebuild the system prompt.
202
+ * If a PreferenceManager is configured, the change is persisted to disk.
203
+ */
204
+ setPreference(key, value) {
205
+ this.preferences[key] = value;
206
+ this.preferenceManager?.set(key, value);
207
+ this.rebuildSystemPrompt();
208
+ }
209
+ /**
210
+ * Replace all user preferences and rebuild the system prompt.
211
+ * If a PreferenceManager is configured, the change is persisted to disk.
212
+ */
213
+ setPreferences(prefs) {
214
+ this.preferences = { ...prefs };
215
+ this.preferenceManager?.setAll(prefs);
216
+ this.rebuildSystemPrompt();
217
+ }
218
+ /**
219
+ * Get a single preference by key.
220
+ */
221
+ getPreference(key) {
222
+ return this.preferences[key];
223
+ }
224
+ /**
225
+ * Get all current preferences.
226
+ */
227
+ getPreferences() {
228
+ return { ...this.preferences };
229
+ }
230
+ /**
231
+ * Remove a single preference by key and rebuild the system prompt.
232
+ * If a PreferenceManager is configured, the change is persisted to disk.
233
+ */
234
+ removePreference(key) {
235
+ delete this.preferences[key];
236
+ this.preferenceManager?.delete(key);
237
+ this.rebuildSystemPrompt();
238
+ }
239
+ /**
240
+ * Clear all user preferences and rebuild the system prompt.
241
+ * If a PreferenceManager is configured, the change is persisted to disk.
242
+ */
243
+ clearPreferences() {
244
+ this.preferences = {};
245
+ this.preferenceManager?.clear();
246
+ this.rebuildSystemPrompt();
247
+ }
248
+ // ─── Session Persistence ─────────────────────────────────────────────
249
+ /**
250
+ * Build a base SessionState from the current agent state.
251
+ * Subclasses override this to include agent-specific state (e.g. plan).
252
+ */
253
+ buildBaseSessionState(status) {
254
+ return {
255
+ sessionId: this.sessionManager?.getSessionId() ?? "unknown",
256
+ agentType: this.getAgentType(),
257
+ systemPrompt: this.coreSystemPrompt,
258
+ messages: this.contextManager.getContextMessages(),
259
+ createdAt: new Date().toISOString(),
260
+ updatedAt: new Date().toISOString(),
261
+ status,
262
+ };
263
+ }
264
+ /**
265
+ * Save a session checkpoint to disk (if session manager is configured
266
+ * and the run has NOT been cancelled).
267
+ */
268
+ saveCheckpoint(status = "active") {
269
+ if (!this.sessionManager)
270
+ return;
271
+ if (this.isCancelled)
272
+ return; // Don't save on abort
273
+ const state = this.buildBaseSessionState(status);
274
+ this.sessionManager.saveCheckpoint(state);
275
+ }
276
+ /**
277
+ * Restore agent state from a previously saved session.
278
+ *
279
+ * Loads the session file, restores the system prompt and message history
280
+ * into the context manager.
281
+ *
282
+ * @throws if the session is not found or is corrupt.
283
+ */
284
+ loadAndRestoreSession(sessionId) {
285
+ if (!this.sessionManager) {
286
+ throw new Error("Cannot resume: no SessionManager configured. " +
287
+ "Pass `sessionId` or `sessionDir` to the agent constructor.");
288
+ }
289
+ const state = this.sessionManager.loadSession(sessionId);
290
+ if (!state) {
291
+ throw new Error(`Session not found or corrupt: "${sessionId}"`);
292
+ }
293
+ // Validate agent type
294
+ const expectedType = this.getAgentType();
295
+ if (state.agentType !== expectedType) {
296
+ throw new Error(`Session "${sessionId}" was created by a ${state.agentType} agent, ` +
297
+ `but this agent is type "${expectedType}". Cannot resume.`);
298
+ }
299
+ // Sync session manager to the restored session ID so subsequent
300
+ // checkpoints write to the correct file
301
+ this.sessionManager.setSessionId(state.sessionId);
302
+ // Restore system prompt
303
+ this.coreSystemPrompt = state.systemPrompt;
304
+ this.contextManager.clear();
305
+ this.contextManager.setSystemMessage(state.systemPrompt);
306
+ // Restore all messages into context
307
+ for (const msg of state.messages) {
308
+ this.contextManager.addMessage(msg);
309
+ }
310
+ return state;
311
+ }
312
+ // ─── Conversation Lifecycle ─────────────────────────────────────────
313
+ /**
314
+ * Clear the current conversation history and reset the context.
315
+ *
316
+ * The system prompt (core prompt + preferences + skills) is preserved
317
+ * so the agent can start a fresh conversation with the same setup.
318
+ * Use this to begin a new topic without creating a new agent instance.
319
+ */
320
+ clearConversation() {
321
+ this.contextManager.clear();
322
+ }
323
+ /**
324
+ * Reset the agent to its initial state.
325
+ *
326
+ * Clears conversation history, session, and all runtime state.
327
+ * After calling reset(), the agent behaves as if newly constructed.
328
+ */
329
+ reset() {
330
+ this.contextManager.clear();
331
+ this.coreSystemPrompt = "";
332
+ this.preferences = {};
333
+ this.pendingErrorTraceId = null;
334
+ if (this.sessionManager) {
335
+ this.sessionManager.deleteSession(this.sessionManager.getSessionId());
336
+ }
337
+ }
338
+ /**
339
+ * Return the agent type identifier for session metadata.
340
+ * Subclasses override this ("react" or "plan-solve").
341
+ */
342
+ getAgentType() {
343
+ return "react";
344
+ }
345
+ // ─── Error Trace & Analysis ──────────────────────────────────────────
346
+ /**
347
+ * Get the error tracker (if configured via the ToolRegistry).
348
+ */
349
+ get errorTracker() {
350
+ return this.toolRegistry.getErrorTracker();
351
+ }
352
+ /**
353
+ * Call after a tool returns an error string — checks if the error
354
+ * indicates a tool failure (with retry guidance) and saves the trace ID
355
+ * so the next LLM analysis thought can be captured.
356
+ *
357
+ * @param toolName The name of the tool that failed.
358
+ * @param result The string returned by ToolRegistry.execute().
359
+ */
360
+ trackToolErrorForAnalysis(toolName, result) {
361
+ const tracker = this.errorTracker;
362
+ if (!tracker)
363
+ return;
364
+ // Check if the result contains retry guidance (indicating a failure)
365
+ if (result.includes("[Retry Guidance]") || result.includes("has been automatically disabled")) {
366
+ const activeTraceId = tracker.getActiveTraceId(toolName);
367
+ if (activeTraceId) {
368
+ this.pendingErrorTraceId = activeTraceId;
369
+ }
370
+ }
371
+ }
372
+ /**
373
+ * Records the LLM's analysis thought against the pending tool error trace.
374
+ * Should be called each iteration after parsing the LLM's response.
375
+ *
376
+ * @param thought The LLM's thought/analysis content.
377
+ */
378
+ captureAnalysisFromThought(thought) {
379
+ if (this.pendingErrorTraceId && thought) {
380
+ const tracker = this.errorTracker;
381
+ if (tracker) {
382
+ tracker.recordAnalysis(this.pendingErrorTraceId, thought);
383
+ }
384
+ this.pendingErrorTraceId = null;
385
+ }
386
+ }
387
+ /**
388
+ * Generate a markdown report of all recorded tool error traces.
389
+ */
390
+ generateErrorReport() {
391
+ const tracker = this.errorTracker;
392
+ if (!tracker)
393
+ return "# Tool Error Trace Report\n\n*Error tracker not configured.*\n";
394
+ return tracker.generateMarkdownReport();
395
+ }
396
+ }
397
+ exports.Agent = Agent;
398
+ //# sourceMappingURL=agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent.js","sourceRoot":"","sources":["../../src/core/agent.ts"],"names":[],"mappings":";;;AACA,gEAA4D;AAE5D,0DAAsD;AAEtD,2DAAuD;AAEvD,gEAA4D;AAE5D,0EAAsE;AAmHtE;;;;;;;;;;;;;GAaG;AACH,MAAsB,KAAK;IACf,GAAG,CAAc;IACjB,cAAc,CAAiB;IAC/B,YAAY,CAAe;IAC3B,YAAY,CAAe;IAErC,4EAA4E;IAClE,gBAAgB,CAAS;IAEnC,gFAAgF;IACtE,WAAW,GAAgB,EAAE,CAAC;IAExC,gEAAgE;IACtD,iBAAiB,CAAqB;IAEhD,qDAAqD;IAC3C,KAAK,GAAe,EAAE,CAAC;IAEjC;;8EAE0E;IAChE,mBAAmB,GAAkB,IAAI,CAAC;IAEpD,uEAAuE;IAEvE,6DAA6D;IACnD,cAAc,CAAkB;IAE1C,6CAA6C;IACnC,oBAAoB,GAAG,KAAK,CAAC;IAEvC,+DAA+D;IACrD,eAAe,CAAkB;IAE3C,YAAY,MAAmB;QAC7B,IAAI,CAAC,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;QAC7C,IAAI,CAAC,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;QACtB,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,IAAI,IAAI,gCAAc,EAAE,CAAC;QAEpE,sDAAsD;QACtD,IAAI,MAAM,CAAC,YAAY,EAAE,CAAC;YACxB,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QAC1C,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,YAAY,GAAG,IAAI,4BAAY,CAClC,MAAM,CAAC,cAAc,EACrB,MAAM,CAAC,gBAAgB,CACxB,CAAC;YACF,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC5C,IAAI,C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AM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC;QAClC,IAAI,CAAC,OAAO;YAAE,OAAO;QAErB,qEAAqE;QACrE,IAAI,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,iCAAiC,CAAC,EAAE,CAAC;YAC9F,MAAM,aAAa,GAAG,OAAO,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC;YACzD,IAAI,aAAa,EAAE,CAAC;gBAClB,IAAI,CAAC,mBAAmB,GAAG,aAAa,CAAC;YAC3C,CAAC;QACH,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACO,0BAA0B,CAAC,OAAe;QAClD,IAAI,IAAI,CAAC,mBAAmB,IAAI,OAAO,EAAE,CAAC;YACxC,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC;YAClC,IAAI,OAAO,EAAE,CAAC;gBACZ,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,mBAAmB,EAAE,OAAO,CAAC,CAAC;YAC5D,CAAC;YACD,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC;QAClC,CAAC;IACH,CAAC;IAED;;OAEG;IACH,mBAAmB;QACjB,MAAM,OAAO,GAAG,IAAI,CAAC,YAAY,CAAC;QAClC,IAAI,CAAC,OAAO;YAAE,OAAO,gEAAgE,CAAC;QACtF,OAAO,OAAO,CAAC,sBAAsB,EAAE,CAAC;IAC1C,CAAC;CACF;AA9bD,sBA8bC"}
@@ -0,0 +1,34 @@
1
+ import { LLMResponse } from "../llm/interface";
2
+ import { LLMNetworkError } from "../llm/openai-provider";
3
+ /**
4
+ * Lifecycle hooks for agent execution.
5
+ *
6
+ * Each hook is called at a specific point in the agent loop, enabling
7
+ * consumers to add logging, tracing, metrics, or UI updates without
8
+ * subclassing the agent.
9
+ *
10
+ * All hooks are optional — only the ones you provide are called.
11
+ */
12
+ export interface AgentHooks {
13
+ /** Called before each LLM chat() call. */
14
+ onLLMStart?: () => void;
15
+ /** Called after a successful LLM response. */
16
+ onLLMEnd?: (response: LLMResponse) => void;
17
+ /** Called when a network error exhausts all retries. */
18
+ onLLMError?: (error: LLMNetworkError) => void;
19
+ /** Called before a tool begins execution. */
20
+ onToolStart?: (toolName: string, args: Record<string, unknown>) => void;
21
+ /** Called after a tool returns successfully. */
22
+ onToolEnd?: (toolName: string, result: string) => void;
23
+ /** Called when a tool throws or returns an error string. */
24
+ onToolError?: (toolName: string, error: string) => void;
25
+ /** Called when the LLM produces a reasoning thought. */
26
+ onThought?: (thought: string) => void;
27
+ /** Called when an initial plan is created. */
28
+ onPlanCreated?: (plan: string[]) => void;
29
+ /** Called when the plan is revised mid-execution. */
30
+ onPlanRevised?: (plan: string[]) => void;
31
+ /** Called when a final answer is produced. */
32
+ onFinish?: (answer: string) => void;
33
+ }
34
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/core/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD;;;;;;;;GAQG;AACH,MAAM,WAAW,UAAU;IAGzB,0CAA0C;IAC1C,UAAU,CAAC,EAAE,MAAM,IAAI,CAAC;IAExB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;IAE3C,wDAAwD;IACxD,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,CAAC;IAI9C,6CAA6C;IAC7C,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAExE,gDAAgD;IAChD,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;IAEvD,4DAA4D;IAC5D,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAIxD,wDAAwD;IACxD,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAItC,8CAA8C;IAC9C,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IAEzC,qDAAqD;IACrD,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IAIzC,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACrC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../../src/core/hooks.ts"],"names":[],"mappings":""}
@@ -0,0 +1,114 @@
1
+ import { Agent, AgentConfig } from "./agent";
2
+ import { SessionState, SessionStatus } from "../session/session-types";
3
+ /**
4
+ * Configuration specific to the Plan-and-Solve Agent.
5
+ */
6
+ export interface PlanSolveAgentConfig extends AgentConfig {
7
+ /** Maximum iterations for the Plan-Solve loop (default: 15). */
8
+ maxIterations?: number;
9
+ /** Maximum number of steps in a plan (default: 12). */
10
+ maxPlanSteps?: number;
11
+ /**
12
+ * Enable progressive skill disclosure.
13
+ * When true, skills are auto-detected from user input and loaded
14
+ * into the system prompt on demand (default: true).
15
+ */
16
+ enableSkillAutoDetect?: boolean;
17
+ /**
18
+ * Number of consecutive tool failures before a "replan hint" is injected
19
+ * into the system prompt, nudging the LLM to consider revising its plan.
20
+ * Set to 0 to disable auto-replan triggering (default: 2).
21
+ */
22
+ replanThreshold?: number;
23
+ }
24
+ /**
25
+ * Plan-and-Solve Agent implementing a structured Plan → Resolve → Final Answer loop.
26
+ *
27
+ * This paradigm separates high-level planning from step-by-step execution:
28
+ * - **Phase 1 (Plan):** The LLM analyzes the request and creates a numbered plan.
29
+ * - **Phase 2 (Resolve):** The LLM executes each step, using tools. The plan is
30
+ * injected into the system prompt to keep the LLM focused. If obstacles arise,
31
+ * the LLM can output a `revised_plan` to adjust remaining steps.
32
+ * - **Final:** When all steps are complete, the LLM provides the final answer.
33
+ *
34
+ * Compared to ReAct, Plan-and-Solve encourages more deliberate decomposition
35
+ * up front, reducing the chance of the agent getting lost mid-task.
36
+ *
37
+ * Session persistence:
38
+ * When `enableCheckpointing` is set, the agent auto-saves checkpoints that
39
+ * include full plan state (current plan, completed steps, replan info) so the
40
+ * session can be resumed after a network interruption.
41
+ */
42
+ export declare class PlanSolveAgent extends Agent {
43
+ private maxIterations;
44
+ private maxPlanSteps;
45
+ private enableSkillAutoDetect;
46
+ /** The current plan steps (empty until the plan is created). */
47
+ private currentPlan;
48
+ /** Whether a plan has been created in this run. */
49
+ private hasPlan;
50
+ /**
51
+ * How many consecutive tool failures before a replan hint is injected.
52
+ * 0 = disabled (no auto-replanning hints).
53
+ */
54
+ private replanThreshold;
55
+ /** Consecutive tool failures in the current run (resets on success). */
56
+ private consecutiveFailures;
57
+ /** How many plan steps have been completed (used for plan display). */
58
+ private completedSteps;
59
+ /** Internal flag: when true, run() skips plan state reset (used by resume()). */
60
+ private _skipPlanReset;
61
+ constructor(config: PlanSolveAgentConfig);
62
+ run(input: string): Promise<string>;
63
+ /**
64
+ * Agent type identifier for session metadata.
65
+ */
66
+ protected getAgentType(): "plan-solve";
67
+ /**
68
+ * Include plan state in session checkpoints.
69
+ */
70
+ protected buildBaseSessionState(status: SessionStatus): SessionState;
71
+ /**
72
+ * Restore plan state from a saved session.
73
+ */
74
+ protected loadAndRestoreSession(sessionId: string): SessionState;
75
+ /**
76
+ * Resume a previously interrupted session.
77
+ *
78
+ * Restores messages, system prompt, and plan state so the agent can
79
+ * continue from where it left off.
80
+ *
81
+ * @param sessionId The session ID to resume.
82
+ * @param input New user input to continue the conversation.
83
+ */
84
+ resume(sessionId: string, input: string): Promise<string>;
85
+ /**
86
+ * Handle an LLMNetworkError: save an interrupted checkpoint if
87
+ * checkpointing is enabled, and return a user-facing message with
88
+ * resume instructions.
89
+ */
90
+ private handleNetworkError;
91
+ /**
92
+ * Rebuild the system prompt to include the current plan context
93
+ * alongside the core prompt and active skill prompts.
94
+ *
95
+ * Called each iteration so the LLM always sees the up-to-date plan.
96
+ *
97
+ * @param replanHint Optional hint injected when consecutive tool
98
+ * failures exceed the threshold, nudging the LLM
99
+ * to consider revising its plan.
100
+ */
101
+ private rebuildContextWithPlan;
102
+ /**
103
+ * Compute a replan hint based on the current consecutive failure count.
104
+ *
105
+ * When consecutive failures reach the configured threshold, a message is
106
+ * returned that will be appended to the system prompt, nudging the LLM
107
+ * to consider revising its plan rather than continuing with a failing approach.
108
+ *
109
+ * Returns undefined when no hint is needed (failures below threshold
110
+ * or auto-replan is disabled).
111
+ */
112
+ private computeReplanHint;
113
+ }
114
+ //# sourceMappingURL=plan-solve-agent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plan-solve-agent.d.ts","sourceRoot":"","sources":["../../src/core/plan-solve-agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAQ7C,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAqCvE;;GAEG;AACH,MAAM,WAAW,oBAAqB,SAAQ,WAAW;IACvD,gEAAgE;IAChE,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB,uDAAuD;IACvD,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAEhC;;;;OAIG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,cAA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