klaus-agent 0.2.2 → 0.3.1

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/core/agent.ts CHANGED
@@ -21,6 +21,7 @@ import type { SubagentConfig } from "../multi-agent/types.js";
21
21
  import type { SkillSource } from "../skills/types.js";
22
22
  import type { MCPServerConfig, MCPClient } from "../tools/mcp-adapter.js";
23
23
  import type { TaskFactory } from "../background/types.js";
24
+ import type { PlanningConfig } from "../planning/types.js";
24
25
  import { SessionManager } from "../session/session-manager.js";
25
26
  import { CheckpointManager } from "../checkpoint/checkpoint-manager.js";
26
27
  import { InjectionManager } from "../injection/injection-manager.js";
@@ -35,6 +36,9 @@ import { LLMSummarizer, agentMessagesToCompactionInput } from "../compaction/sum
35
36
  import { Wire } from "../wire/wire.js";
36
37
  import { BackgroundTaskManager } from "../background/task-manager.js";
37
38
  import { createBackgroundTaskTools } from "../background/tools.js";
39
+ import { PlanningManager } from "../planning/planning-manager.js";
40
+ import { createPlanningTools } from "../planning/tools.js";
41
+ import { PlanningNagProvider } from "../planning/nag-injection.js";
38
42
  import { runAgentLoop } from "./agent-loop.js";
39
43
 
40
44
  export interface AgentConfig {
@@ -60,6 +64,7 @@ export interface AgentConfig {
60
64
  mcp?: { servers: MCPServerConfig[]; clientFactory: (config: MCPServerConfig) => MCPClient };
61
65
  wire?: { bufferSize?: number };
62
66
  backgroundTasks?: { factories?: Record<string, TaskFactory> };
67
+ planning?: PlanningConfig;
63
68
  }
64
69
 
65
70
  export class Agent {
@@ -83,6 +88,7 @@ export class Agent {
83
88
  private _mcpAdapter: MCPAdapter | undefined;
84
89
  private _wire: Wire;
85
90
  private _backgroundTaskManager: BackgroundTaskManager | undefined;
91
+ private _planningManager: PlanningManager | undefined;
86
92
  private _initialized = false;
87
93
 
88
94
  constructor(config: AgentConfig) {
@@ -145,6 +151,11 @@ export class Agent {
145
151
  }
146
152
  });
147
153
  }
154
+
155
+ // Planning
156
+ if (config.planning) {
157
+ this._planningManager = new PlanningManager(config.planning);
158
+ }
148
159
  }
149
160
 
150
161
  // --- Public API ---
@@ -238,6 +249,10 @@ export class Agent {
238
249
  return this._backgroundTaskManager;
239
250
  }
240
251
 
252
+ get planning(): PlanningManager | undefined {
253
+ return this._planningManager;
254
+ }
255
+
241
256
  setSystemPrompt(prompt: string): void {
242
257
  this._state.systemPrompt = prompt;
243
258
  }
@@ -352,6 +367,19 @@ export class Agent {
352
367
  const bgTools = createBackgroundTaskTools(this._backgroundTaskManager, this._config.backgroundTasks?.factories);
353
368
  this._state.tools = [...this._state.tools, ...bgTools];
354
369
  }
370
+
371
+ // Planning tools + nag injection
372
+ if (this._planningManager) {
373
+ this._state.tools = [...this._state.tools, ...createPlanningTools(this._planningManager)];
374
+
375
+ // Register nag provider into injection manager (create one if needed)
376
+ const nagProvider = new PlanningNagProvider(this._planningManager);
377
+ if (this._injectionManager) {
378
+ this._injectionManager.addProvider(nagProvider);
379
+ } else {
380
+ this._injectionManager = new InjectionManager([nagProvider]);
381
+ }
382
+ }
355
383
  }
356
384
 
357
385
  private _normalizeInput(input: string | AgentMessage | AgentMessage[]): AgentMessage[] {
@@ -430,6 +458,7 @@ export class Agent {
430
458
  injectionManager: this._injectionManager,
431
459
  extensionRunner: this._extensionRunner,
432
460
  compaction: compactionWithSummarizer,
461
+ planningManager: this._planningManager,
433
462
  });
434
463
 
435
464
  this._state.messages = result;
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ import type { SubagentConfig } from "./multi-agent/types.js";
15
15
  import type { SkillSource } from "./skills/types.js";
16
16
  import type { MCPServerConfig, MCPClient } from "./tools/mcp-adapter.js";
17
17
  import type { TaskFactory } from "./background/types.js";
18
+ import type { PlanningConfig } from "./planning/types.js";
18
19
 
19
20
  export interface CreateAgentConfig {
20
21
  // Required
@@ -41,6 +42,7 @@ export interface CreateAgentConfig {
41
42
  mcp?: { servers: MCPServerConfig[]; clientFactory: (config: MCPServerConfig) => MCPClient };
42
43
  wire?: { bufferSize?: number };
43
44
  backgroundTasks?: { factories?: Record<string, TaskFactory> };
45
+ planning?: PlanningConfig;
44
46
 
45
47
  // Advanced: provide your own LLM provider
46
48
  provider?: LLMProvider;
@@ -71,6 +73,7 @@ export function createAgent(config: CreateAgentConfig): Agent {
71
73
  mcp: config.mcp,
72
74
  wire: config.wire,
73
75
  backgroundTasks: config.backgroundTasks,
76
+ planning: config.planning,
74
77
  });
75
78
  }
76
79
 
@@ -95,12 +98,15 @@ export { ExtensionRunner } from "./extensions/runner.js";
95
98
  export { discoverSkills } from "./skills/discovery.js";
96
99
  export { loadSkill, renderSkillTemplate } from "./skills/loader.js";
97
100
  export { MCPAdapter } from "./tools/mcp-adapter.js";
98
- export { estimateTokens, shouldCompact, findCutPoint } from "./compaction/compaction.js";
101
+ export { estimateTokens, shouldCompact, findCutPoint, microCompact } from "./compaction/compaction.js";
99
102
  export { calculateCost } from "./providers/shared.js";
100
103
  export { LLMSummarizer } from "./compaction/summarizer.js";
101
104
  export { Wire } from "./wire/wire.js";
102
105
  export { BackgroundTaskManager } from "./background/task-manager.js";
103
106
  export { createBackgroundTaskTools } from "./background/tools.js";
107
+ export { PlanningManager } from "./planning/planning-manager.js";
108
+ export { createPlanningTools } from "./planning/tools.js";
109
+ export { PlanningNagProvider } from "./planning/nag-injection.js";
104
110
 
105
111
  // Core types
106
112
  export type {
@@ -227,3 +233,13 @@ export type {
227
233
  BackgroundTaskEvent,
228
234
  TaskFactory,
229
235
  } from "./background/types.js";
236
+
237
+ // Planning types
238
+ export type {
239
+ PlanningConfig,
240
+ PlanPhase,
241
+ TodoItem,
242
+ TodoStatus,
243
+ } from "./planning/types.js";
244
+
245
+ export { PLANNING_TOOL_NAMES } from "./planning/types.js";
@@ -0,0 +1,24 @@
1
+ // Nag injection provider — reminds model to update todos when it hasn't for N rounds
2
+
3
+ import type { DynamicInjectionProvider, DynamicInjection } from "../injection/types.js";
4
+ import type { AgentMessage } from "../types.js";
5
+ import type { PlanningManager } from "./planning-manager.js";
6
+
7
+ export class PlanningNagProvider implements DynamicInjectionProvider {
8
+ constructor(private _manager: PlanningManager) {}
9
+
10
+ async getInjections(_history: AgentMessage[]): Promise<DynamicInjection[]> {
11
+ if (!this._manager.shouldNag()) return [];
12
+
13
+ // Reset after check so the next nag waits another N rounds.
14
+ // Kept here (not in shouldNag) so shouldNag() stays side-effect-free.
15
+ this._manager.resetRoundCounter();
16
+
17
+ return [
18
+ {
19
+ type: "planning-nag",
20
+ content: this._manager.getNagMessage(),
21
+ },
22
+ ];
23
+ }
24
+ }
@@ -0,0 +1,133 @@
1
+ // Planning manager — two-phase planning with structured todo tracking
2
+
3
+ import type { TodoItem, TodoStatus, PlanPhase, PlanningState, PlanningConfig } from "./types.js";
4
+ import { PLANNING_TOOL_NAMES } from "./types.js";
5
+ import { generateId } from "../utils/id.js";
6
+
7
+ export class PlanningManager {
8
+ private _state: PlanningState;
9
+ private _config: PlanningConfig;
10
+ private _allowedInPlanning: ReadonlySet<string>;
11
+
12
+ constructor(config: PlanningConfig = {}) {
13
+ this._config = config;
14
+ const allowed = new Set(config.readOnlyTools ?? []);
15
+ allowed.add(PLANNING_TOOL_NAMES.todo);
16
+ allowed.add(PLANNING_TOOL_NAMES.planMode);
17
+ this._allowedInPlanning = allowed;
18
+ this._state = {
19
+ phase: "planning",
20
+ todos: [],
21
+ roundsSinceTodoUpdate: 0,
22
+ };
23
+ }
24
+
25
+ get phase(): PlanPhase {
26
+ return this._state.phase;
27
+ }
28
+
29
+ get todos(): readonly Readonly<TodoItem>[] {
30
+ return this._state.todos;
31
+ }
32
+
33
+ get roundsSinceTodoUpdate(): number {
34
+ return this._state.roundsSinceTodoUpdate;
35
+ }
36
+
37
+ get config(): Readonly<PlanningConfig> {
38
+ return this._config;
39
+ }
40
+
41
+ /** Pre-built set of tool names allowed during planning phase. */
42
+ get allowedInPlanning(): ReadonlySet<string> {
43
+ return this._allowedInPlanning;
44
+ }
45
+
46
+ // --- Phase control ---
47
+
48
+ startExecution(): string {
49
+ if (this._state.todos.length === 0) {
50
+ throw new Error("Cannot start execution: no todos defined. Create a plan first.");
51
+ }
52
+ this._state.phase = "executing";
53
+ this.resetRoundCounter();
54
+ return `Switched to execution phase. ${this._state.todos.length} todo(s) to complete.\n\n${this.render()}`;
55
+ }
56
+
57
+ switchToPlanning(): string {
58
+ this._state.phase = "planning";
59
+ this.resetRoundCounter();
60
+ return `Switched to planning phase. Tools restricted to read-only.\n\n${this.render()}`;
61
+ }
62
+
63
+ // --- Todo CRUD ---
64
+
65
+ updateTodos(items: Array<{ id: string; text: string; status: TodoStatus }>): string {
66
+ const max = this._config.maxTodos ?? 50;
67
+ if (items.length > max) {
68
+ throw new Error(`Too many todos: ${items.length} exceeds limit of ${max}.`);
69
+ }
70
+
71
+ let inProgressCount = 0;
72
+ const validated: TodoItem[] = [];
73
+
74
+ for (const item of items) {
75
+ const status = item.status ?? "pending";
76
+ if (status === "in_progress") inProgressCount++;
77
+ validated.push({
78
+ id: item.id || generateId(),
79
+ text: item.text,
80
+ status,
81
+ });
82
+ }
83
+
84
+ if (inProgressCount > 1) {
85
+ throw new Error("Only one todo can be in_progress at a time.");
86
+ }
87
+
88
+ this._state.todos = validated;
89
+ this.resetRoundCounter();
90
+ return this.render();
91
+ }
92
+
93
+ // --- Render ---
94
+
95
+ render(): string {
96
+ if (this._state.todos.length === 0) {
97
+ return `[phase: ${this._state.phase}] No todos.`;
98
+ }
99
+
100
+ const lines = this._state.todos.map((t) => {
101
+ const icon = t.status === "completed" ? "[x]" : t.status === "in_progress" ? "[>]" : "[ ]";
102
+ return `${icon} ${t.id}: ${t.text}`;
103
+ });
104
+
105
+ const done = this._state.todos.filter((t) => t.status === "completed").length;
106
+ const total = this._state.todos.length;
107
+
108
+ return `[phase: ${this._state.phase}] Progress: ${done}/${total}\n${lines.join("\n")}`;
109
+ }
110
+
111
+ // --- Nag tracking ---
112
+
113
+ /** Call once per agent loop step (after tool execution). */
114
+ tickRound(): void {
115
+ this._state.roundsSinceTodoUpdate++;
116
+ }
117
+
118
+ /** Reset the round counter (called when the model updates todos). */
119
+ resetRoundCounter(): void {
120
+ this._state.roundsSinceTodoUpdate = 0;
121
+ }
122
+
123
+ shouldNag(): boolean {
124
+ if (this._state.phase !== "executing") return false;
125
+ if (this._state.todos.length === 0) return false;
126
+ const threshold = this._config.nagAfterRounds ?? 3;
127
+ return this._state.roundsSinceTodoUpdate >= threshold;
128
+ }
129
+
130
+ getNagMessage(): string {
131
+ return this._config.nagMessage ?? "<reminder>Update your todos to reflect current progress.</reminder>";
132
+ }
133
+ }
@@ -0,0 +1,71 @@
1
+ // Planning tools — todo management + phase switching
2
+
3
+ import { Type } from "@sinclair/typebox";
4
+ import type { AgentTool, AgentToolResult } from "../tools/types.js";
5
+ import type { PlanningManager } from "./planning-manager.js";
6
+ import { PLANNING_TOOL_NAMES } from "./types.js";
7
+ import type { TodoStatus } from "./types.js";
8
+
9
+ export function createPlanningTools(manager: PlanningManager): AgentTool[] {
10
+ return [
11
+ {
12
+ name: PLANNING_TOOL_NAMES.todo,
13
+ label: "Todo",
14
+ description:
15
+ "Manage your task list. Use this tool to plan work, track progress, and stay on track. " +
16
+ "Only one todo can be in_progress at a time. Update todos frequently to reflect your current state.",
17
+ parameters: Type.Object({
18
+ items: Type.Array(
19
+ Type.Object({
20
+ id: Type.String({ description: "Unique ID for the todo item." }),
21
+ text: Type.String({ description: "Description of the task." }),
22
+ status: Type.Union(
23
+ [Type.Literal("pending"), Type.Literal("in_progress"), Type.Literal("completed")],
24
+ { description: "Task status. Only one item can be in_progress at a time." },
25
+ ),
26
+ }),
27
+ { description: "The full updated todo list (replaces previous list)." },
28
+ ),
29
+ }),
30
+ async execute(
31
+ _toolCallId: string,
32
+ params: { items: Array<{ id: string; text: string; status: TodoStatus }> },
33
+ ): Promise<AgentToolResult> {
34
+ const result = manager.updateTodos(params.items);
35
+ return { content: [{ type: "text", text: result }] };
36
+ },
37
+ },
38
+ {
39
+ name: PLANNING_TOOL_NAMES.planMode,
40
+ label: "Plan Mode",
41
+ description:
42
+ "Switch between planning and execution phases. " +
43
+ "In planning phase, only read-only tools are available — use this time to analyze and create todos. " +
44
+ "In execution phase, all tools are available and nag reminders will prompt you to update todos.",
45
+ parameters: Type.Object({
46
+ action: Type.Union(
47
+ [Type.Literal("start_execution"), Type.Literal("switch_to_planning"), Type.Literal("status")],
48
+ { description: "Action to perform." },
49
+ ),
50
+ }),
51
+ async execute(
52
+ _toolCallId: string,
53
+ params: { action: "start_execution" | "switch_to_planning" | "status" },
54
+ ): Promise<AgentToolResult> {
55
+ let result: string;
56
+ switch (params.action) {
57
+ case "start_execution":
58
+ result = manager.startExecution();
59
+ break;
60
+ case "switch_to_planning":
61
+ result = manager.switchToPlanning();
62
+ break;
63
+ case "status":
64
+ result = manager.render();
65
+ break;
66
+ }
67
+ return { content: [{ type: "text", text: result }] };
68
+ },
69
+ },
70
+ ];
71
+ }
@@ -0,0 +1,40 @@
1
+ // Planning module types — two-phase planning + structured todo tracking
2
+
3
+ export const PLANNING_TOOL_NAMES = {
4
+ todo: "todo",
5
+ planMode: "plan_mode",
6
+ } as const;
7
+
8
+ export type TodoStatus = "pending" | "in_progress" | "completed";
9
+
10
+ export interface TodoItem {
11
+ id: string;
12
+ text: string;
13
+ status: TodoStatus;
14
+ }
15
+
16
+ export type PlanPhase = "planning" | "executing";
17
+
18
+ export interface PlanningState {
19
+ phase: PlanPhase;
20
+ todos: TodoItem[];
21
+ roundsSinceTodoUpdate: number;
22
+ }
23
+
24
+ export interface PlanningConfig {
25
+ /**
26
+ * Tool names allowed during the planning phase (read-only tools).
27
+ * If omitted or empty, all tools are available during planning
28
+ * (phase separation is advisory only via system prompt).
29
+ */
30
+ readOnlyTools?: string[];
31
+
32
+ /** Number of rounds without a todo update before injecting a nag reminder. Default: 3. */
33
+ nagAfterRounds?: number;
34
+
35
+ /** Custom nag reminder text. */
36
+ nagMessage?: string;
37
+
38
+ /** Maximum number of todo items. Default: 50. */
39
+ maxTodos?: number;
40
+ }