@limo-labs/deity 0.1.0-alpha.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 (138) hide show
  1. package/README.md +519 -0
  2. package/dist/src/component.d.ts +19 -0
  3. package/dist/src/component.d.ts.map +1 -0
  4. package/dist/src/component.js +2 -0
  5. package/dist/src/component.js.map +1 -0
  6. package/dist/src/context/context-scope.d.ts +24 -0
  7. package/dist/src/context/context-scope.d.ts.map +1 -0
  8. package/dist/src/context/context-scope.js +19 -0
  9. package/dist/src/context/context-scope.js.map +1 -0
  10. package/dist/src/context/memory-view.d.ts +62 -0
  11. package/dist/src/context/memory-view.d.ts.map +1 -0
  12. package/dist/src/context/memory-view.js +104 -0
  13. package/dist/src/context/memory-view.js.map +1 -0
  14. package/dist/src/context/scoped-context.d.ts +51 -0
  15. package/dist/src/context/scoped-context.d.ts.map +1 -0
  16. package/dist/src/context/scoped-context.js +74 -0
  17. package/dist/src/context/scoped-context.js.map +1 -0
  18. package/dist/src/context/scoped-execution-context.d.ts +55 -0
  19. package/dist/src/context/scoped-execution-context.d.ts.map +1 -0
  20. package/dist/src/context/scoped-execution-context.js +78 -0
  21. package/dist/src/context/scoped-execution-context.js.map +1 -0
  22. package/dist/src/conversation/conversation-manager.d.ts +272 -0
  23. package/dist/src/conversation/conversation-manager.d.ts.map +1 -0
  24. package/dist/src/conversation/conversation-manager.js +11 -0
  25. package/dist/src/conversation/conversation-manager.js.map +1 -0
  26. package/dist/src/conversation/conversation-pruner.d.ts +190 -0
  27. package/dist/src/conversation/conversation-pruner.d.ts.map +1 -0
  28. package/dist/src/conversation/conversation-pruner.js +274 -0
  29. package/dist/src/conversation/conversation-pruner.js.map +1 -0
  30. package/dist/src/conversation/conversation-tree.d.ts +185 -0
  31. package/dist/src/conversation/conversation-tree.d.ts.map +1 -0
  32. package/dist/src/conversation/conversation-tree.js +288 -0
  33. package/dist/src/conversation/conversation-tree.js.map +1 -0
  34. package/dist/src/conversation/file-conversation-store.d.ts +93 -0
  35. package/dist/src/conversation/file-conversation-store.d.ts.map +1 -0
  36. package/dist/src/conversation/file-conversation-store.js +284 -0
  37. package/dist/src/conversation/file-conversation-store.js.map +1 -0
  38. package/dist/src/conversation/in-memory-conversation-store.d.ts +36 -0
  39. package/dist/src/conversation/in-memory-conversation-store.d.ts.map +1 -0
  40. package/dist/src/conversation/in-memory-conversation-store.js +146 -0
  41. package/dist/src/conversation/in-memory-conversation-store.js.map +1 -0
  42. package/dist/src/copilot-adapter.d.ts +33 -0
  43. package/dist/src/copilot-adapter.d.ts.map +1 -0
  44. package/dist/src/copilot-adapter.js +119 -0
  45. package/dist/src/copilot-adapter.js.map +1 -0
  46. package/dist/src/file-trace-enhanced.d.ts +123 -0
  47. package/dist/src/file-trace-enhanced.d.ts.map +1 -0
  48. package/dist/src/file-trace-enhanced.js +177 -0
  49. package/dist/src/file-trace-enhanced.js.map +1 -0
  50. package/dist/src/file-trace.d.ts +24 -0
  51. package/dist/src/file-trace.d.ts.map +1 -0
  52. package/dist/src/file-trace.js +60 -0
  53. package/dist/src/file-trace.js.map +1 -0
  54. package/dist/src/index.d.ts +63 -0
  55. package/dist/src/index.d.ts.map +1 -0
  56. package/dist/src/index.js +40 -0
  57. package/dist/src/index.js.map +1 -0
  58. package/dist/src/json-store.d.ts +27 -0
  59. package/dist/src/json-store.d.ts.map +1 -0
  60. package/dist/src/json-store.js +93 -0
  61. package/dist/src/json-store.js.map +1 -0
  62. package/dist/src/llm.d.ts +35 -0
  63. package/dist/src/llm.d.ts.map +1 -0
  64. package/dist/src/llm.js +2 -0
  65. package/dist/src/llm.js.map +1 -0
  66. package/dist/src/memory/cold-storage.d.ts +60 -0
  67. package/dist/src/memory/cold-storage.d.ts.map +1 -0
  68. package/dist/src/memory/cold-storage.js +132 -0
  69. package/dist/src/memory/cold-storage.js.map +1 -0
  70. package/dist/src/memory/compression.d.ts +161 -0
  71. package/dist/src/memory/compression.d.ts.map +1 -0
  72. package/dist/src/memory/compression.js +193 -0
  73. package/dist/src/memory/compression.js.map +1 -0
  74. package/dist/src/memory/hot-memory.d.ts +69 -0
  75. package/dist/src/memory/hot-memory.d.ts.map +1 -0
  76. package/dist/src/memory/hot-memory.js +116 -0
  77. package/dist/src/memory/hot-memory.js.map +1 -0
  78. package/dist/src/memory/memory-budget.d.ts +162 -0
  79. package/dist/src/memory/memory-budget.d.ts.map +1 -0
  80. package/dist/src/memory/memory-budget.js +241 -0
  81. package/dist/src/memory/memory-budget.js.map +1 -0
  82. package/dist/src/memory/memory-config.d.ts +419 -0
  83. package/dist/src/memory/memory-config.d.ts.map +1 -0
  84. package/dist/src/memory/memory-config.js +297 -0
  85. package/dist/src/memory/memory-config.js.map +1 -0
  86. package/dist/src/memory/prefetcher.d.ts +137 -0
  87. package/dist/src/memory/prefetcher.d.ts.map +1 -0
  88. package/dist/src/memory/prefetcher.js +186 -0
  89. package/dist/src/memory/prefetcher.js.map +1 -0
  90. package/dist/src/memory/tiered-memory.d.ts +116 -0
  91. package/dist/src/memory/tiered-memory.d.ts.map +1 -0
  92. package/dist/src/memory/tiered-memory.js +215 -0
  93. package/dist/src/memory/tiered-memory.js.map +1 -0
  94. package/dist/src/memory/warm-storage.d.ts +74 -0
  95. package/dist/src/memory/warm-storage.d.ts.map +1 -0
  96. package/dist/src/memory/warm-storage.js +207 -0
  97. package/dist/src/memory/warm-storage.js.map +1 -0
  98. package/dist/src/openai-adapter.d.ts +20 -0
  99. package/dist/src/openai-adapter.d.ts.map +1 -0
  100. package/dist/src/openai-adapter.js +73 -0
  101. package/dist/src/openai-adapter.js.map +1 -0
  102. package/dist/src/parser.d.ts +27 -0
  103. package/dist/src/parser.d.ts.map +1 -0
  104. package/dist/src/parser.js +76 -0
  105. package/dist/src/parser.js.map +1 -0
  106. package/dist/src/runtime.d.ts +172 -0
  107. package/dist/src/runtime.d.ts.map +1 -0
  108. package/dist/src/runtime.js +436 -0
  109. package/dist/src/runtime.js.map +1 -0
  110. package/dist/src/schema-utils.d.ts +7 -0
  111. package/dist/src/schema-utils.d.ts.map +1 -0
  112. package/dist/src/schema-utils.js +71 -0
  113. package/dist/src/schema-utils.js.map +1 -0
  114. package/dist/src/stage.d.ts +139 -0
  115. package/dist/src/stage.d.ts.map +1 -0
  116. package/dist/src/stage.js +2 -0
  117. package/dist/src/stage.js.map +1 -0
  118. package/dist/src/store.d.ts +51 -0
  119. package/dist/src/store.d.ts.map +1 -0
  120. package/dist/src/store.js +2 -0
  121. package/dist/src/store.js.map +1 -0
  122. package/dist/src/tool.d.ts +12 -0
  123. package/dist/src/tool.d.ts.map +1 -0
  124. package/dist/src/tool.js +2 -0
  125. package/dist/src/tool.js.map +1 -0
  126. package/dist/src/trace.d.ts +60 -0
  127. package/dist/src/trace.d.ts.map +1 -0
  128. package/dist/src/trace.js +2 -0
  129. package/dist/src/trace.js.map +1 -0
  130. package/dist/src/validator.d.ts +17 -0
  131. package/dist/src/validator.d.ts.map +1 -0
  132. package/dist/src/validator.js +21 -0
  133. package/dist/src/validator.js.map +1 -0
  134. package/dist/src/workflow.d.ts +192 -0
  135. package/dist/src/workflow.d.ts.map +1 -0
  136. package/dist/src/workflow.js +50 -0
  137. package/dist/src/workflow.js.map +1 -0
  138. package/package.json +62 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation-manager.js","sourceRoot":"","sources":["../../../src/conversation/conversation-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}
@@ -0,0 +1,190 @@
1
+ /**
2
+ * Pruning strategy factories for conversation management.
3
+ *
4
+ * This module provides common pruning strategies that can be used with
5
+ * ConversationManager.prune() to control conversation history growth.
6
+ *
7
+ * @module conversation/conversation-pruner
8
+ * @since 2.0.0
9
+ */
10
+ import type { ConversationMessage, PruneStrategy } from "./conversation-manager.js";
11
+ import type { LLMAdapter } from "../llm.js";
12
+ /**
13
+ * Factory for creating common conversation pruning strategies.
14
+ *
15
+ * @since 2.0.0
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Keep only the last 50 messages
20
+ * const strategy = ConversationPruner.keepRecent(50);
21
+ * await store.prune(strategy);
22
+ *
23
+ * // Keep system messages and recent 20 messages
24
+ * const strategy = ConversationPruner.combine([
25
+ * ConversationPruner.keepImportant((msg) => msg.message.role === "system"),
26
+ * ConversationPruner.keepRecent(20),
27
+ * ]);
28
+ * await store.prune(strategy);
29
+ * ```
30
+ */
31
+ export declare class ConversationPruner {
32
+ /**
33
+ * Keep only the most recent N messages.
34
+ *
35
+ * This is the simplest pruning strategy - just keep a fixed number
36
+ * of recent messages and discard older ones.
37
+ *
38
+ * @param count - Number of recent messages to keep
39
+ * @returns Pruning strategy
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * const strategy = ConversationPruner.keepRecent(100);
44
+ * await store.prune(strategy);
45
+ * ```
46
+ */
47
+ static keepRecent(count: number): PruneStrategy;
48
+ /**
49
+ * Keep messages that match a filter function.
50
+ *
51
+ * This allows you to preserve "important" messages based on custom logic.
52
+ * Common use cases:
53
+ * - Keep all system messages
54
+ * - Keep messages marked as important
55
+ * - Keep messages from specific stages
56
+ *
57
+ * @param filter - Function to determine if a message should be kept
58
+ * @returns Pruning strategy
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // Keep all system messages
63
+ * const strategy = ConversationPruner.keepImportant(
64
+ * (msg) => msg.message.role === "system"
65
+ * );
66
+ *
67
+ * // Keep messages marked as important
68
+ * const strategy = ConversationPruner.keepImportant(
69
+ * (msg) => msg.metadata.important === true
70
+ * );
71
+ * ```
72
+ */
73
+ static keepImportant(filter: (message: ConversationMessage) => boolean): PruneStrategy;
74
+ /**
75
+ * Keep messages within a time window (in milliseconds).
76
+ *
77
+ * Messages older than the specified age will be pruned.
78
+ *
79
+ * @param maxAge - Maximum age in milliseconds
80
+ * @returns Pruning strategy
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * // Keep messages from the last hour
85
+ * const strategy = ConversationPruner.byAge(60 * 60 * 1000);
86
+ *
87
+ * // Keep messages from the last 7 days
88
+ * const strategy = ConversationPruner.byAge(7 * 24 * 60 * 60 * 1000);
89
+ * ```
90
+ */
91
+ static byAge(maxAge: number): PruneStrategy;
92
+ /**
93
+ * Sliding window strategy: keep recent N messages plus important ones.
94
+ *
95
+ * This combines keepRecent and keepImportant for a balanced approach:
96
+ * - Always keeps the last N messages for context continuity
97
+ * - Also preserves important messages outside the window
98
+ *
99
+ * @param windowSize - Number of recent messages to keep
100
+ * @param importantFilter - Optional filter for important messages
101
+ * @returns Pruning strategy
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * // Keep last 50 messages + all system messages
106
+ * const strategy = ConversationPruner.slidingWindow(
107
+ * 50,
108
+ * (msg) => msg.message.role === "system"
109
+ * );
110
+ * ```
111
+ */
112
+ static slidingWindow(windowSize: number, importantFilter?: (message: ConversationMessage) => boolean): PruneStrategy;
113
+ /**
114
+ * Combine multiple pruning strategies.
115
+ *
116
+ * When multiple strategies are combined, a message is kept if it matches
117
+ * ANY of the strategies (OR logic).
118
+ *
119
+ * @param strategies - Array of strategies to combine
120
+ * @returns Combined pruning strategy
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * // Keep recent 20 messages OR system messages OR messages from last hour
125
+ * const strategy = ConversationPruner.combine([
126
+ * ConversationPruner.keepRecent(20),
127
+ * ConversationPruner.keepImportant((msg) => msg.message.role === "system"),
128
+ * ConversationPruner.byAge(60 * 60 * 1000),
129
+ * ]);
130
+ * ```
131
+ */
132
+ static combine(strategies: PruneStrategy[]): PruneStrategy;
133
+ /**
134
+ * Summarize old messages using an LLM (advanced strategy).
135
+ *
136
+ * This strategy uses an LLM to create a condensed summary of old messages,
137
+ * then replaces them with a single summary message. This preserves context
138
+ * while significantly reducing message count.
139
+ *
140
+ * **Note:** This is an advanced strategy that involves LLM calls and
141
+ * may have associated costs.
142
+ *
143
+ * @param llm - LLM adapter for generating summaries
144
+ * @param options - Summarization options
145
+ * @returns Async function that performs pruning with summarization
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * const llm = new OpenAIAdapter({ apiKey: "..." });
150
+ *
151
+ * // Keep last 20 messages, summarize older ones
152
+ * const pruneWithSummary = ConversationPruner.summarize(llm, {
153
+ * keepRecent: 20,
154
+ * maxTokens: 500,
155
+ * });
156
+ *
157
+ * // Apply the strategy
158
+ * const summary = await pruneWithSummary(await store.getHistory());
159
+ * // Replace history with summarized version
160
+ * await store.clear();
161
+ * for (const msg of summary) {
162
+ * await store.addMessage(msg.message, msg.metadata);
163
+ * }
164
+ * ```
165
+ */
166
+ static summarize(llm: LLMAdapter, options: {
167
+ keepRecent: number;
168
+ maxTokens?: number;
169
+ systemPrompt?: string;
170
+ }): (messages: ConversationMessage[]) => Promise<ConversationMessage[]>;
171
+ /**
172
+ * Create a custom pruning strategy.
173
+ *
174
+ * This is a helper for creating ad-hoc strategies with explicit options.
175
+ *
176
+ * @param options - Pruning options
177
+ * @returns Pruning strategy
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * const strategy = ConversationPruner.custom({
182
+ * keepRecent: 30,
183
+ * keepImportant: (msg) => msg.metadata.stageId === "critical-stage",
184
+ * maxAge: 24 * 60 * 60 * 1000, // 24 hours
185
+ * });
186
+ * ```
187
+ */
188
+ static custom(options: PruneStrategy): PruneStrategy;
189
+ }
190
+ //# sourceMappingURL=conversation-pruner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation-pruner.d.ts","sourceRoot":"","sources":["../../../src/conversation/conversation-pruner.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AACpF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAG5C;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,kBAAkB;IAC7B;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa;IAM/C;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,MAAM,CAAC,aAAa,CAClB,MAAM,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,OAAO,GAChD,aAAa;IAMhB;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,aAAa;IAM3C;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM,CAAC,aAAa,CAClB,UAAU,EAAE,MAAM,EAClB,eAAe,CAAC,EAAE,CAAC,OAAO,EAAE,mBAAmB,KAAK,OAAO,GAC1D,aAAa;IAOhB;;;;;;;;;;;;;;;;;;OAkBG;IACH,MAAM,CAAC,OAAO,CAAC,UAAU,EAAE,aAAa,EAAE,GAAG,aAAa;IAkC1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACH,MAAM,CAAC,SAAS,CACd,GAAG,EAAE,UAAU,EACf,OAAO,EAAE;QACP,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,GACA,CAAC,QAAQ,EAAE,mBAAmB,EAAE,KAAK,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAyDtE;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,aAAa;CAGrD"}
@@ -0,0 +1,274 @@
1
+ /**
2
+ * Pruning strategy factories for conversation management.
3
+ *
4
+ * This module provides common pruning strategies that can be used with
5
+ * ConversationManager.prune() to control conversation history growth.
6
+ *
7
+ * @module conversation/conversation-pruner
8
+ * @since 2.0.0
9
+ */
10
+ /**
11
+ * Factory for creating common conversation pruning strategies.
12
+ *
13
+ * @since 2.0.0
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * // Keep only the last 50 messages
18
+ * const strategy = ConversationPruner.keepRecent(50);
19
+ * await store.prune(strategy);
20
+ *
21
+ * // Keep system messages and recent 20 messages
22
+ * const strategy = ConversationPruner.combine([
23
+ * ConversationPruner.keepImportant((msg) => msg.message.role === "system"),
24
+ * ConversationPruner.keepRecent(20),
25
+ * ]);
26
+ * await store.prune(strategy);
27
+ * ```
28
+ */
29
+ export class ConversationPruner {
30
+ /**
31
+ * Keep only the most recent N messages.
32
+ *
33
+ * This is the simplest pruning strategy - just keep a fixed number
34
+ * of recent messages and discard older ones.
35
+ *
36
+ * @param count - Number of recent messages to keep
37
+ * @returns Pruning strategy
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const strategy = ConversationPruner.keepRecent(100);
42
+ * await store.prune(strategy);
43
+ * ```
44
+ */
45
+ static keepRecent(count) {
46
+ return {
47
+ keepRecent: count,
48
+ };
49
+ }
50
+ /**
51
+ * Keep messages that match a filter function.
52
+ *
53
+ * This allows you to preserve "important" messages based on custom logic.
54
+ * Common use cases:
55
+ * - Keep all system messages
56
+ * - Keep messages marked as important
57
+ * - Keep messages from specific stages
58
+ *
59
+ * @param filter - Function to determine if a message should be kept
60
+ * @returns Pruning strategy
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * // Keep all system messages
65
+ * const strategy = ConversationPruner.keepImportant(
66
+ * (msg) => msg.message.role === "system"
67
+ * );
68
+ *
69
+ * // Keep messages marked as important
70
+ * const strategy = ConversationPruner.keepImportant(
71
+ * (msg) => msg.metadata.important === true
72
+ * );
73
+ * ```
74
+ */
75
+ static keepImportant(filter) {
76
+ return {
77
+ keepImportant: filter,
78
+ };
79
+ }
80
+ /**
81
+ * Keep messages within a time window (in milliseconds).
82
+ *
83
+ * Messages older than the specified age will be pruned.
84
+ *
85
+ * @param maxAge - Maximum age in milliseconds
86
+ * @returns Pruning strategy
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * // Keep messages from the last hour
91
+ * const strategy = ConversationPruner.byAge(60 * 60 * 1000);
92
+ *
93
+ * // Keep messages from the last 7 days
94
+ * const strategy = ConversationPruner.byAge(7 * 24 * 60 * 60 * 1000);
95
+ * ```
96
+ */
97
+ static byAge(maxAge) {
98
+ return {
99
+ maxAge,
100
+ };
101
+ }
102
+ /**
103
+ * Sliding window strategy: keep recent N messages plus important ones.
104
+ *
105
+ * This combines keepRecent and keepImportant for a balanced approach:
106
+ * - Always keeps the last N messages for context continuity
107
+ * - Also preserves important messages outside the window
108
+ *
109
+ * @param windowSize - Number of recent messages to keep
110
+ * @param importantFilter - Optional filter for important messages
111
+ * @returns Pruning strategy
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * // Keep last 50 messages + all system messages
116
+ * const strategy = ConversationPruner.slidingWindow(
117
+ * 50,
118
+ * (msg) => msg.message.role === "system"
119
+ * );
120
+ * ```
121
+ */
122
+ static slidingWindow(windowSize, importantFilter) {
123
+ return {
124
+ keepRecent: windowSize,
125
+ keepImportant: importantFilter,
126
+ };
127
+ }
128
+ /**
129
+ * Combine multiple pruning strategies.
130
+ *
131
+ * When multiple strategies are combined, a message is kept if it matches
132
+ * ANY of the strategies (OR logic).
133
+ *
134
+ * @param strategies - Array of strategies to combine
135
+ * @returns Combined pruning strategy
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * // Keep recent 20 messages OR system messages OR messages from last hour
140
+ * const strategy = ConversationPruner.combine([
141
+ * ConversationPruner.keepRecent(20),
142
+ * ConversationPruner.keepImportant((msg) => msg.message.role === "system"),
143
+ * ConversationPruner.byAge(60 * 60 * 1000),
144
+ * ]);
145
+ * ```
146
+ */
147
+ static combine(strategies) {
148
+ // Merge all strategies
149
+ const combined = {};
150
+ // Take the maximum keepRecent value
151
+ const keepRecentValues = strategies
152
+ .map((s) => s.keepRecent)
153
+ .filter((v) => v !== undefined);
154
+ if (keepRecentValues.length > 0) {
155
+ combined.keepRecent = Math.max(...keepRecentValues);
156
+ }
157
+ // Combine all keepImportant filters with OR logic
158
+ const keepImportantFilters = strategies
159
+ .map((s) => s.keepImportant)
160
+ .filter((f) => f !== undefined);
161
+ if (keepImportantFilters.length > 0) {
162
+ combined.keepImportant = (msg) => keepImportantFilters.some((filter) => filter(msg));
163
+ }
164
+ // Take the maximum maxAge value (keep messages for longer)
165
+ const maxAgeValues = strategies
166
+ .map((s) => s.maxAge)
167
+ .filter((v) => v !== undefined);
168
+ if (maxAgeValues.length > 0) {
169
+ combined.maxAge = Math.max(...maxAgeValues);
170
+ }
171
+ return combined;
172
+ }
173
+ /**
174
+ * Summarize old messages using an LLM (advanced strategy).
175
+ *
176
+ * This strategy uses an LLM to create a condensed summary of old messages,
177
+ * then replaces them with a single summary message. This preserves context
178
+ * while significantly reducing message count.
179
+ *
180
+ * **Note:** This is an advanced strategy that involves LLM calls and
181
+ * may have associated costs.
182
+ *
183
+ * @param llm - LLM adapter for generating summaries
184
+ * @param options - Summarization options
185
+ * @returns Async function that performs pruning with summarization
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * const llm = new OpenAIAdapter({ apiKey: "..." });
190
+ *
191
+ * // Keep last 20 messages, summarize older ones
192
+ * const pruneWithSummary = ConversationPruner.summarize(llm, {
193
+ * keepRecent: 20,
194
+ * maxTokens: 500,
195
+ * });
196
+ *
197
+ * // Apply the strategy
198
+ * const summary = await pruneWithSummary(await store.getHistory());
199
+ * // Replace history with summarized version
200
+ * await store.clear();
201
+ * for (const msg of summary) {
202
+ * await store.addMessage(msg.message, msg.metadata);
203
+ * }
204
+ * ```
205
+ */
206
+ static summarize(llm, options) {
207
+ return async (messages) => {
208
+ if (messages.length <= options.keepRecent) {
209
+ return messages; // Nothing to summarize
210
+ }
211
+ // Split messages into old (to summarize) and recent (to keep)
212
+ const oldMessages = messages.slice(0, -options.keepRecent);
213
+ const recentMessages = messages.slice(-options.keepRecent);
214
+ // Build summarization prompt
215
+ const summarizationMessages = [
216
+ {
217
+ role: "system",
218
+ content: options.systemPrompt ||
219
+ "You are a conversation summarizer. Create a concise summary of the conversation history that preserves key information and context.",
220
+ },
221
+ {
222
+ role: "user",
223
+ content: `Summarize the following conversation history:\n\n${oldMessages
224
+ .map((msg) => `[${msg.message.role}]: ${JSON.stringify(msg.message.content)}`)
225
+ .join("\n")}\n\nProvide a concise summary that captures the key points and context.`,
226
+ },
227
+ ];
228
+ // Generate summary
229
+ const response = await llm.generate(summarizationMessages, undefined, {
230
+ maxTokens: options.maxTokens || 500,
231
+ temperature: 0.3, // Lower temperature for consistent summaries
232
+ });
233
+ // Create summary message
234
+ const summaryMessage = {
235
+ message: {
236
+ role: "system",
237
+ content: `[Conversation Summary]: ${response.content}`,
238
+ },
239
+ metadata: {
240
+ id: `summary-${Date.now()}`,
241
+ timestamp: Date.now(),
242
+ important: true,
243
+ custom: {
244
+ isSummary: true,
245
+ originalMessageCount: oldMessages.length,
246
+ },
247
+ },
248
+ };
249
+ // Return summary + recent messages
250
+ return [summaryMessage, ...recentMessages];
251
+ };
252
+ }
253
+ /**
254
+ * Create a custom pruning strategy.
255
+ *
256
+ * This is a helper for creating ad-hoc strategies with explicit options.
257
+ *
258
+ * @param options - Pruning options
259
+ * @returns Pruning strategy
260
+ *
261
+ * @example
262
+ * ```typescript
263
+ * const strategy = ConversationPruner.custom({
264
+ * keepRecent: 30,
265
+ * keepImportant: (msg) => msg.metadata.stageId === "critical-stage",
266
+ * maxAge: 24 * 60 * 60 * 1000, // 24 hours
267
+ * });
268
+ * ```
269
+ */
270
+ static custom(options) {
271
+ return options;
272
+ }
273
+ }
274
+ //# sourceMappingURL=conversation-pruner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation-pruner.js","sourceRoot":"","sources":["../../../src/conversation/conversation-pruner.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAMH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,kBAAkB;IAC7B;;;;;;;;;;;;;;OAcG;IACH,MAAM,CAAC,UAAU,CAAC,KAAa;QAC7B,OAAO;YACL,UAAU,EAAE,KAAK;SAClB,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;OAwBG;IACH,MAAM,CAAC,aAAa,CAClB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CAAC,yEAAyE;iBACvF;aACF,CAAC;YAEF,mBAAmB;YACnB,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,QAAQ,CAAC,qBAAqB,EAAE,SAAS,EAAE;gBACpE,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,GAAG;gBACnC,WAAW,EAAE,GAAG,EAAE,6CAA6C;aAChE,CAAC,CAAC;YAEH,yBAAyB;YACzB,MAAM,cAAc,GAAwB;gBAC1C,OAAO,EAAE;oBACP,IAAI,EAAE,QAAQ;oBACd,OAAO,EAAE,2BAA2B,QAAQ,CAAC,OAAO,EAAE;iBACvD;gBACD,QAAQ,EAAE;oBACR,EAAE,EAAE,WAAW,IAAI,CAAC,GAAG,EAAE,EAAE;oBAC3B,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;oBACrB,SAAS,EAAE,IAAI;oBACf,MAAM,EAAE;wBACN,SAAS,EAAE,IAAI;wBACf,oBAAoB,EAAE,WAAW,CAAC,MAAM;qBACzC;iBACF;aACF,CAAC;YAEF,mCAAmC;YACnC,OAAO,CAAC,cAAc,EAAE,GAAG,cAAc,CAAC,CAAC;QAC7C,CAAC,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,MAAM,CAAC,OAAsB;QAClC,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Conversation tree with branching support.
3
+ *
4
+ * Enables creating branches for retries, exploring alternatives,
5
+ * and managing complex conversation histories.
6
+ *
7
+ * @module conversation/conversation-tree
8
+ * @since 2.0.0
9
+ */
10
+ import type { ConversationMessage, MessageMetadata } from "./conversation-manager.js";
11
+ import type { Message } from "../component.js";
12
+ /**
13
+ * A branch in the conversation tree.
14
+ */
15
+ export interface ConversationBranch {
16
+ /** Unique branch identifier */
17
+ id: string;
18
+ /** Parent branch ID (undefined for root) */
19
+ parentId?: string;
20
+ /** Human-readable branch name */
21
+ name: string;
22
+ /** Branch point: index in parent where this branch diverged */
23
+ branchPoint: number;
24
+ /** Messages specific to this branch */
25
+ messages: ConversationMessage[];
26
+ /** Timestamp when branch was created */
27
+ createdAt: number;
28
+ }
29
+ /**
30
+ * Conversation tree with branching support.
31
+ *
32
+ * Useful for:
33
+ * - Creating branches during retries
34
+ * - Exploring alternative conversation paths
35
+ * - Rolling back to previous states
36
+ * - A/B testing different prompts
37
+ *
38
+ * @since 2.0.0
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * const tree = new ConversationTree();
43
+ *
44
+ * // Add messages to main branch
45
+ * await tree.addMessage({ role: "user", content: "Hello" });
46
+ * await tree.addMessage({ role: "assistant", content: "Hi!" });
47
+ *
48
+ * // Create a branch for retry
49
+ * const retryBranch = await tree.createBranch("retry-1");
50
+ * await tree.addMessage({ role: "user", content: "Try again" });
51
+ *
52
+ * // Switch back to main
53
+ * await tree.switchBranch("main");
54
+ *
55
+ * // Get full history for current branch
56
+ * const history = await tree.getHistory();
57
+ * ```
58
+ */
59
+ export declare class ConversationTree {
60
+ private branches;
61
+ private currentBranchId;
62
+ constructor();
63
+ /**
64
+ * Get the current branch ID.
65
+ */
66
+ getCurrentBranchId(): string;
67
+ /**
68
+ * Get a branch by ID.
69
+ */
70
+ getBranch(branchId: string): ConversationBranch | undefined;
71
+ /**
72
+ * Get all branches.
73
+ */
74
+ getAllBranches(): ConversationBranch[];
75
+ /**
76
+ * Create a new branch from the current branch.
77
+ *
78
+ * The new branch starts at the current point in the conversation.
79
+ * All previous messages are inherited from the parent branch.
80
+ *
81
+ * @param name - Human-readable name for the branch
82
+ * @returns The new branch ID
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * // Create a retry branch
87
+ * const retryBranch = await tree.createBranch("retry-1");
88
+ * tree.switchBranch(retryBranch);
89
+ * // Add messages to retry branch...
90
+ * ```
91
+ */
92
+ createBranch(name: string): string;
93
+ /**
94
+ * Switch to a different branch.
95
+ *
96
+ * Future addMessage calls will add to the specified branch.
97
+ *
98
+ * @param branchId - ID of the branch to switch to
99
+ */
100
+ switchBranch(branchId: string): void;
101
+ /**
102
+ * Add a message to the current branch.
103
+ *
104
+ * @param message - The message to add
105
+ * @param metadata - Optional metadata (id and timestamp will be auto-generated)
106
+ */
107
+ addMessage(message: Message, metadata?: Partial<Omit<MessageMetadata, "id" | "timestamp">>): void;
108
+ /**
109
+ * Get the full history for the current branch.
110
+ *
111
+ * Includes all messages from parent branches up to the branch point,
112
+ * plus messages specific to this branch.
113
+ *
114
+ * @returns Array of messages in chronological order
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * const history = tree.getHistory();
119
+ * console.log(`${history.length} messages in current branch`);
120
+ * ```
121
+ */
122
+ getHistory(): ConversationMessage[];
123
+ /**
124
+ * Get the history for a specific branch.
125
+ *
126
+ * @param branchId - ID of the branch
127
+ * @returns Array of messages in chronological order
128
+ */
129
+ getBranchHistory(branchId: string): ConversationMessage[];
130
+ /**
131
+ * Get child branches of a specific branch.
132
+ *
133
+ * @param branchId - ID of the parent branch
134
+ * @returns Array of child branches
135
+ */
136
+ getChildBranches(branchId: string): ConversationBranch[];
137
+ /**
138
+ * Delete a branch and all its children.
139
+ *
140
+ * Cannot delete the main branch.
141
+ *
142
+ * @param branchId - ID of the branch to delete
143
+ */
144
+ deleteBranch(branchId: string): void;
145
+ /**
146
+ * Merge a branch into its parent.
147
+ *
148
+ * Copies all branch-specific messages to the parent branch
149
+ * and deletes the child branch.
150
+ *
151
+ * @param branchId - ID of the branch to merge
152
+ * @returns The parent branch ID
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * // Try an alternative approach
157
+ * const tryBranch = tree.createBranch("alternative");
158
+ * tree.switchBranch(tryBranch);
159
+ * tree.addMessage({ role: "user", content: "Alternative prompt" });
160
+ *
161
+ * // If successful, merge back to main
162
+ * tree.mergeBranch(tryBranch);
163
+ * ```
164
+ */
165
+ mergeBranch(branchId: string): string;
166
+ /**
167
+ * Export the tree structure.
168
+ *
169
+ * @returns Object containing all branches
170
+ */
171
+ export(): {
172
+ branches: ConversationBranch[];
173
+ };
174
+ /**
175
+ * Import a tree structure.
176
+ *
177
+ * Replaces the current tree.
178
+ *
179
+ * @param data - Exported tree data
180
+ */
181
+ import(data: {
182
+ branches: ConversationBranch[];
183
+ }): void;
184
+ }
185
+ //# sourceMappingURL=conversation-tree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation-tree.d.ts","sourceRoot":"","sources":["../../../src/conversation/conversation-tree.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EAChB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAG/C;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,+BAA+B;IAC/B,EAAE,EAAE,MAAM,CAAC;IACX,4CAA4C;IAC5C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,WAAW,EAAE,MAAM,CAAC;IACpB,uCAAuC;IACvC,QAAQ,EAAE,mBAAmB,EAAE,CAAC;IAChC,wCAAwC;IACxC,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAA8C;IAC9D,OAAO,CAAC,eAAe,CAAkB;;IAczC;;OAEG;IACH,kBAAkB,IAAI,MAAM;IAI5B;;OAEG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,kBAAkB,GAAG,SAAS;IAI3D;;OAEG;IACH,cAAc,IAAI,kBAAkB,EAAE;IAItC;;;;;;;;;;;;;;;;OAgBG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IAwBlC;;;;;;OAMG;IACH,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IA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