@simulacra-ai/core 0.0.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.
Files changed (134) hide show
  1. package/README.md +232 -0
  2. package/dist/checkpoints/default-summarization-strategy.d.ts +13 -0
  3. package/dist/checkpoints/default-summarization-strategy.d.ts.map +1 -0
  4. package/dist/checkpoints/default-summarization-strategy.js +63 -0
  5. package/dist/checkpoints/default-summarization-strategy.js.map +1 -0
  6. package/dist/checkpoints/index.d.ts +3 -0
  7. package/dist/checkpoints/index.d.ts.map +1 -0
  8. package/dist/checkpoints/index.js +3 -0
  9. package/dist/checkpoints/index.js.map +1 -0
  10. package/dist/checkpoints/types.d.ts +32 -0
  11. package/dist/checkpoints/types.d.ts.map +1 -0
  12. package/dist/checkpoints/types.js +2 -0
  13. package/dist/checkpoints/types.js.map +1 -0
  14. package/dist/context-transformers/checkpoint-context-transformer.d.ts +13 -0
  15. package/dist/context-transformers/checkpoint-context-transformer.d.ts.map +1 -0
  16. package/dist/context-transformers/checkpoint-context-transformer.js +32 -0
  17. package/dist/context-transformers/checkpoint-context-transformer.js.map +1 -0
  18. package/dist/context-transformers/composite-context-transformer.d.ts +31 -0
  19. package/dist/context-transformers/composite-context-transformer.d.ts.map +1 -0
  20. package/dist/context-transformers/composite-context-transformer.js +41 -0
  21. package/dist/context-transformers/composite-context-transformer.js.map +1 -0
  22. package/dist/context-transformers/index.d.ts +6 -0
  23. package/dist/context-transformers/index.d.ts.map +1 -0
  24. package/dist/context-transformers/index.js +6 -0
  25. package/dist/context-transformers/index.js.map +1 -0
  26. package/dist/context-transformers/noop-context-transformer.d.ts +24 -0
  27. package/dist/context-transformers/noop-context-transformer.d.ts.map +1 -0
  28. package/dist/context-transformers/noop-context-transformer.js +26 -0
  29. package/dist/context-transformers/noop-context-transformer.js.map +1 -0
  30. package/dist/context-transformers/tool-context-transformer.d.ts +26 -0
  31. package/dist/context-transformers/tool-context-transformer.d.ts.map +1 -0
  32. package/dist/context-transformers/tool-context-transformer.js +52 -0
  33. package/dist/context-transformers/tool-context-transformer.js.map +1 -0
  34. package/dist/context-transformers/types.d.ts +65 -0
  35. package/dist/context-transformers/types.d.ts.map +1 -0
  36. package/dist/context-transformers/types.js +2 -0
  37. package/dist/context-transformers/types.js.map +1 -0
  38. package/dist/conversations/conversation.d.ts +146 -0
  39. package/dist/conversations/conversation.d.ts.map +1 -0
  40. package/dist/conversations/conversation.js +642 -0
  41. package/dist/conversations/conversation.js.map +1 -0
  42. package/dist/conversations/index.d.ts +5 -0
  43. package/dist/conversations/index.d.ts.map +1 -0
  44. package/dist/conversations/index.js +5 -0
  45. package/dist/conversations/index.js.map +1 -0
  46. package/dist/conversations/stream-listener.d.ts +37 -0
  47. package/dist/conversations/stream-listener.d.ts.map +1 -0
  48. package/dist/conversations/stream-listener.js +68 -0
  49. package/dist/conversations/stream-listener.js.map +1 -0
  50. package/dist/conversations/token-tracker.d.ts +59 -0
  51. package/dist/conversations/token-tracker.d.ts.map +1 -0
  52. package/dist/conversations/token-tracker.js +98 -0
  53. package/dist/conversations/token-tracker.js.map +1 -0
  54. package/dist/conversations/types.d.ts +346 -0
  55. package/dist/conversations/types.d.ts.map +1 -0
  56. package/dist/conversations/types.js +2 -0
  57. package/dist/conversations/types.js.map +1 -0
  58. package/dist/index.d.ts +10 -0
  59. package/dist/index.d.ts.map +1 -0
  60. package/dist/index.js +9 -0
  61. package/dist/index.js.map +1 -0
  62. package/dist/policies/composite-policy.d.ts +29 -0
  63. package/dist/policies/composite-policy.d.ts.map +1 -0
  64. package/dist/policies/composite-policy.js +77 -0
  65. package/dist/policies/composite-policy.js.map +1 -0
  66. package/dist/policies/default-policy.d.ts +7 -0
  67. package/dist/policies/default-policy.d.ts.map +1 -0
  68. package/dist/policies/default-policy.js +14 -0
  69. package/dist/policies/default-policy.js.map +1 -0
  70. package/dist/policies/index.d.ts +8 -0
  71. package/dist/policies/index.d.ts.map +1 -0
  72. package/dist/policies/index.js +8 -0
  73. package/dist/policies/index.js.map +1 -0
  74. package/dist/policies/noop-policy.d.ts +21 -0
  75. package/dist/policies/noop-policy.d.ts.map +1 -0
  76. package/dist/policies/noop-policy.js +36 -0
  77. package/dist/policies/noop-policy.js.map +1 -0
  78. package/dist/policies/rate-limit-policy.d.ts +44 -0
  79. package/dist/policies/rate-limit-policy.d.ts.map +1 -0
  80. package/dist/policies/rate-limit-policy.js +98 -0
  81. package/dist/policies/rate-limit-policy.js.map +1 -0
  82. package/dist/policies/retry-policy.d.ts +42 -0
  83. package/dist/policies/retry-policy.d.ts.map +1 -0
  84. package/dist/policies/retry-policy.js +114 -0
  85. package/dist/policies/retry-policy.js.map +1 -0
  86. package/dist/policies/token-limit-policy.d.ts +76 -0
  87. package/dist/policies/token-limit-policy.d.ts.map +1 -0
  88. package/dist/policies/token-limit-policy.js +134 -0
  89. package/dist/policies/token-limit-policy.js.map +1 -0
  90. package/dist/policies/types.d.ts +49 -0
  91. package/dist/policies/types.d.ts.map +1 -0
  92. package/dist/policies/types.js +9 -0
  93. package/dist/policies/types.js.map +1 -0
  94. package/dist/tools/index.d.ts +2 -0
  95. package/dist/tools/index.d.ts.map +1 -0
  96. package/dist/tools/index.js +2 -0
  97. package/dist/tools/index.js.map +1 -0
  98. package/dist/tools/types.d.ts +180 -0
  99. package/dist/tools/types.d.ts.map +1 -0
  100. package/dist/tools/types.js +2 -0
  101. package/dist/tools/types.js.map +1 -0
  102. package/dist/utils/async.d.ts +125 -0
  103. package/dist/utils/async.d.ts.map +1 -0
  104. package/dist/utils/async.js +217 -0
  105. package/dist/utils/async.js.map +1 -0
  106. package/dist/utils/index.d.ts +4 -0
  107. package/dist/utils/index.d.ts.map +1 -0
  108. package/dist/utils/index.js +4 -0
  109. package/dist/utils/index.js.map +1 -0
  110. package/dist/utils/object.d.ts +80 -0
  111. package/dist/utils/object.d.ts.map +1 -0
  112. package/dist/utils/object.js +189 -0
  113. package/dist/utils/object.js.map +1 -0
  114. package/dist/utils/types.d.ts +17 -0
  115. package/dist/utils/types.d.ts.map +1 -0
  116. package/dist/utils/types.js +2 -0
  117. package/dist/utils/types.js.map +1 -0
  118. package/dist/workflows/index.d.ts +4 -0
  119. package/dist/workflows/index.d.ts.map +1 -0
  120. package/dist/workflows/index.js +4 -0
  121. package/dist/workflows/index.js.map +1 -0
  122. package/dist/workflows/types.d.ts +70 -0
  123. package/dist/workflows/types.d.ts.map +1 -0
  124. package/dist/workflows/types.js +2 -0
  125. package/dist/workflows/types.js.map +1 -0
  126. package/dist/workflows/workflow-manager.d.ts +74 -0
  127. package/dist/workflows/workflow-manager.d.ts.map +1 -0
  128. package/dist/workflows/workflow-manager.js +165 -0
  129. package/dist/workflows/workflow-manager.js.map +1 -0
  130. package/dist/workflows/workflow.d.ts +116 -0
  131. package/dist/workflows/workflow.d.ts.map +1 -0
  132. package/dist/workflows/workflow.js +351 -0
  133. package/dist/workflows/workflow.js.map +1 -0
  134. package/package.json +20 -0
package/README.md ADDED
@@ -0,0 +1,232 @@
1
+ # Simulacra Core
2
+
3
+ Core conversation engine, tool system, workflow management, policies, and context transformers for Simulacra.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @simulacra-ai/core
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { Conversation } from "@simulacra-ai/core";
15
+ import { AnthropicProvider } from "@simulacra-ai/anthropic";
16
+ import Anthropic from "@anthropic-ai/sdk";
17
+
18
+ const provider = new AnthropicProvider(new Anthropic(), { model: MODEL_NAME });
19
+ const conversation = new Conversation(provider);
20
+
21
+ await conversation.prompt("Hello!");
22
+ console.log(conversation.messages);
23
+ ```
24
+
25
+ ## Conversation
26
+
27
+ `Conversation` is a concrete class that accepts a `ModelProvider` at construction time. Provider packages (`@simulacra-ai/anthropic`, `@simulacra-ai/openai`, `@simulacra-ai/google`) supply `ModelProvider` implementations. All model-specific configuration (model name, token limits, thinking, caching) lives in the provider, not the conversation.
28
+
29
+ ```typescript
30
+ const conversation = new Conversation(provider);
31
+ conversation.system = "You are a helpful assistant.";
32
+
33
+ const result = await conversation.prompt("Hello!");
34
+ console.log(conversation.messages);
35
+ ```
36
+
37
+ Properties include `id`, `state`, `system`, `toolkit`, and `messages`.
38
+
39
+ Methods include `prompt(text)`, `send_message(contents)`, `cancel_response()`, `clear()`, `load(messages)`, `spawn_child(fork?, id?, system?, is_checkpoint?)`, and `checkpoint(config?)`.
40
+
41
+ State transitions flow: `idle` → `awaiting_response` → `streaming_response` → `idle` (or `stopping` → `idle` on cancel, `disposed` on disposal).
42
+
43
+ ### Conversation Events
44
+
45
+ Key events for streaming: `message_start`, `content_update`, `message_complete`. For tool use: `content_start`, `content_complete`. For lifecycle: `state_change`, `dispose`.
46
+
47
+ See the [developer guide](DEVELOPER_GUIDE.md#events) for the full event reference.
48
+
49
+ ## Tool System
50
+
51
+ Tools are implemented via the `ToolClass` interface:
52
+
53
+ ```typescript
54
+ import type {
55
+ ToolClass,
56
+ ToolContext,
57
+ ToolDefinition,
58
+ ToolSuccessResult,
59
+ ToolErrorResult,
60
+ } from "@simulacra-ai/core";
61
+
62
+ class SearchTool {
63
+ #context: ToolContext;
64
+
65
+ static get_definition(): ToolDefinition {
66
+ return {
67
+ name: "search",
68
+ description: "Search the web for information",
69
+ parameters: [
70
+ { name: "query", type: "string", required: true, description: "Search query" },
71
+ { name: "max_results", type: "number", required: false, default: 5 },
72
+ ],
73
+ parallelizable: true,
74
+ };
75
+ }
76
+
77
+ constructor(context: ToolContext) {
78
+ this.#context = context;
79
+ }
80
+
81
+ async execute(params: {
82
+ query: string;
83
+ max_results?: number;
84
+ }): Promise<ToolSuccessResult | ToolErrorResult> {
85
+ const results = await doSearch(params.query, params.max_results ?? 5);
86
+ return { result: true, ...results };
87
+ }
88
+ }
89
+
90
+ conversation.toolkit = [SearchTool as ToolClass];
91
+ ```
92
+
93
+ ### ToolDefinition
94
+
95
+ ```typescript
96
+ interface ToolDefinition {
97
+ name: string;
98
+ description: string;
99
+ parameters: ToolParameterDefinition[];
100
+ parallelizable?: boolean; // default: true
101
+ }
102
+ ```
103
+
104
+ ### ToolContext
105
+
106
+ Every tool instance receives `{ conversation, workflow }` at construction time, plus any application-specific data passed via `context_data`.
107
+
108
+ ### Parameter Types
109
+
110
+ `ToolParameterDefinition` supports: `string`, `number`, `boolean`, `object` (with nested `properties`), `array` (with `items`). String parameters support `enum` arrays. All types have `name`, `description?`, `required?`, and `default?`.
111
+
112
+ ### Parallel Execution
113
+
114
+ The `parallelizable` flag (default `true`) controls batching in the workflow engine:
115
+
116
+ - **`true` (default)**: the workflow can batch this tool call with other parallelizable calls into `Promise.all`
117
+ - **`false`**: acts as a barrier; the workflow executes this tool alone, waiting for it to complete before continuing
118
+
119
+ This allows tools with side effects or ordering requirements to be marked as sequential while allowing read-only tools to run concurrently.
120
+
121
+ ## Workflows
122
+
123
+ `WorkflowManager` is the agentic loop. It listens for tool-use responses, executes tools, sends results back, and repeats until the model produces a final response.
124
+
125
+ ```typescript
126
+ import { WorkflowManager } from "@simulacra-ai/core";
127
+
128
+ const manager = new WorkflowManager(conversation);
129
+ await conversation.prompt("Use my tools to answer this question.");
130
+ ```
131
+
132
+ For lower-level control, `Workflow` can be used directly — see the [developer guide](DEVELOPER_GUIDE.md#workflows).
133
+
134
+ Key events: `workflow_end` (loop finished with reason `complete` | `cancel` | `error`), `workflow_update` (tool results sent), `state_change`. See the [developer guide](DEVELOPER_GUIDE.md#events) for the full event reference.
135
+
136
+ ## Policies
137
+
138
+ Policies wrap the LLM request with cross-cutting concerns. All policies implement `execute(cancellation_token, fn, ...args)`.
139
+
140
+ ### RetryPolicy
141
+
142
+ `RetryPolicy` retries failed requests with exponential backoff.
143
+
144
+ ```typescript
145
+ import { RetryPolicy } from "@simulacra-ai/core";
146
+
147
+ const policy = new RetryPolicy({
148
+ max_attempts: 3,
149
+ initial_backoff_ms: 1000,
150
+ backoff_factor: 2,
151
+ retryable: (error) => error.error?.status === 429, // optional filter
152
+ });
153
+ ```
154
+
155
+ ### RateLimitPolicy
156
+
157
+ `RateLimitPolicy` limits requests per time window. Pass it to the conversation constructor, then call `attach()` to subscribe to events.
158
+
159
+ ```typescript
160
+ import { Conversation, RateLimitPolicy } from "@simulacra-ai/core";
161
+
162
+ const policy = new RateLimitPolicy({ limit: 10, period_ms: 60_000 });
163
+ const conversation = new Conversation(provider, policy);
164
+ policy.attach(conversation);
165
+ ```
166
+
167
+ ### TokenLimitPolicy
168
+
169
+ `TokenLimitPolicy` limits tokens per time window. Like `RateLimitPolicy`, it requires both constructor injection and `attach()`.
170
+
171
+ ```typescript
172
+ import { Conversation, TokenLimitPolicy } from "@simulacra-ai/core";
173
+
174
+ const policy = new TokenLimitPolicy({
175
+ period_ms: 60_000,
176
+ total_tokens_per_period: 120_000,
177
+ });
178
+ const conversation = new Conversation(provider, policy);
179
+ policy.attach(conversation);
180
+ ```
181
+
182
+ ### CompositePolicy
183
+
184
+ `CompositePolicy` chains multiple policies together.
185
+
186
+ ```typescript
187
+ import { CompositePolicy } from "@simulacra-ai/core";
188
+
189
+ const policy = new CompositePolicy(
190
+ new RateLimitPolicy({ limit: 10, period_ms: 60_000 }),
191
+ new RetryPolicy({ max_attempts: 3, initial_backoff_ms: 1000, backoff_factor: 2 }),
192
+ );
193
+ ```
194
+
195
+ ## Context Transformers
196
+
197
+ Context transformers modify messages at two points: before sending (`transform_prompt`) and after receiving (`transform_completion`).
198
+
199
+ By default, `Conversation` uses `ToolContextTransformer` and `CheckpointContextTransformer`. Pass a custom transformer to override.
200
+
201
+ ### ToolContextTransformer (default)
202
+
203
+ Removes orphaned tool calls that lack corresponding tool results, preventing provider errors when tool execution was interrupted.
204
+
205
+ ### CheckpointContextTransformer (default)
206
+
207
+ Replaces pre-checkpoint messages with the checkpoint summary, reducing context size while preserving conversation continuity. See [Checkpoints](DEVELOPER_GUIDE.md#checkpoints) in the developer guide.
208
+
209
+ ### CompositeContextTransformer
210
+
211
+ Chains multiple transformers sequentially.
212
+
213
+ ### NoopContextTransformer
214
+
215
+ Pass-through that disables all conversation-level transformations.
216
+
217
+ ### Provider Context Transformers
218
+
219
+ Model providers can bundle their own transformers via `context_transformers`. These run before conversation-level transformers and handle provider-specific quirks automatically. The conversation reads them fresh on every request, supporting runtime provider swaps.
220
+
221
+ See the [developer guide](DEVELOPER_GUIDE.md#provider-context-transformers) for the `ProviderContextTransformer` interface.
222
+
223
+ ## Utilities
224
+
225
+ - **`TokenTracker`**: attach to a conversation to accumulate input/output token counts across requests
226
+ - **`CancellationTokenSource` / `CancellationToken`**: cooperative cancellation for async operations
227
+ - **`sleep(ms, cancellationToken?)`**: cancellable delay
228
+ - **`deep_merge(original, supplemental)`**: recursive object merge
229
+
230
+ ## License
231
+
232
+ MIT
@@ -0,0 +1,13 @@
1
+ import type { Message } from "../conversations/types.ts";
2
+ import type { SummarizationContext, SummarizationStrategy } from "./types.ts";
3
+ /**
4
+ * Default summarization strategy for checkpoints.
5
+ *
6
+ * Serializes the conversation context into a structured text block and appends
7
+ * an instruction asking the model to produce a condensed summary.
8
+ */
9
+ export declare class DefaultSummarizationStrategy implements SummarizationStrategy {
10
+ #private;
11
+ build_prompt(context: SummarizationContext): Message[];
12
+ }
13
+ //# sourceMappingURL=default-summarization-strategy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-summarization-strategy.d.ts","sourceRoot":"","sources":["../../src/checkpoints/default-summarization-strategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAE9E;;;;;GAKG;AACH,qBAAa,4BAA6B,YAAW,qBAAqB;;IACxE,YAAY,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,EAAE;CA8DvD"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Default summarization strategy for checkpoints.
3
+ *
4
+ * Serializes the conversation context into a structured text block and appends
5
+ * an instruction asking the model to produce a condensed summary.
6
+ */
7
+ export class DefaultSummarizationStrategy {
8
+ build_prompt(context) {
9
+ const sections = [];
10
+ if (context.previous_checkpoint) {
11
+ sections.push(`## Previous Checkpoint Summary\n${context.previous_checkpoint.summary}`);
12
+ }
13
+ if (context.system) {
14
+ sections.push(`## System Prompt\n${context.system}`);
15
+ }
16
+ if (context.messages.length > 0) {
17
+ sections.push(`## Conversation\n${this.#serialize_messages(context.messages)}`);
18
+ }
19
+ sections.push([
20
+ "## Instructions",
21
+ "Summarize the above conversation concisely. Preserve:",
22
+ "- Key decisions and their rationale",
23
+ "- Current state of any in-progress work",
24
+ "- Important facts, names, and values established",
25
+ "- Tool outcomes",
26
+ "- Any explicit user preferences or instructions",
27
+ "",
28
+ "Omit redundant back-and-forth and superseded plans.",
29
+ "Format as a structured briefing the model can use to continue the conversation seamlessly.",
30
+ ].join("\n"));
31
+ return [
32
+ {
33
+ role: "user",
34
+ content: [{ type: "text", text: sections.join("\n\n") }],
35
+ },
36
+ ];
37
+ }
38
+ #serialize_messages(messages) {
39
+ return messages
40
+ .map((m) => {
41
+ const role = m.role === "user" ? "User" : "Assistant";
42
+ const parts = m.content
43
+ .map((c) => {
44
+ switch (c.type) {
45
+ case "text":
46
+ return c.text;
47
+ case "thinking":
48
+ return `[Thinking: ${c.thought}]`;
49
+ case "tool":
50
+ return `[Called tool: ${c.tool}]`;
51
+ case "tool_result":
52
+ return `[Tool ${c.tool} returned: ${JSON.stringify(c.result)}]`;
53
+ default:
54
+ return undefined;
55
+ }
56
+ })
57
+ .filter(Boolean);
58
+ return `${role}: ${parts.join("\n")}`;
59
+ })
60
+ .join("\n\n");
61
+ }
62
+ }
63
+ //# sourceMappingURL=default-summarization-strategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-summarization-strategy.js","sourceRoot":"","sources":["../../src/checkpoints/default-summarization-strategy.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,OAAO,4BAA4B;IACvC,YAAY,CAAC,OAA6B;QACxC,MAAM,QAAQ,GAAa,EAAE,CAAC;QAE9B,IAAI,OAAO,CAAC,mBAAmB,EAAE,CAAC;YAChC,QAAQ,CAAC,IAAI,CAAC,mCAAmC,OAAO,CAAC,mBAAmB,CAAC,OAAO,EAAE,CAAC,CAAC;QAC1F,CAAC;QAED,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACnB,QAAQ,CAAC,IAAI,CAAC,qBAAqB,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QACvD,CAAC;QAED,IAAI,OAAO,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAChC,QAAQ,CAAC,IAAI,CAAC,oBAAoB,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;QAClF,CAAC;QAED,QAAQ,CAAC,IAAI,CACX;YACE,iBAAiB;YACjB,uDAAuD;YACvD,qCAAqC;YACrC,yCAAyC;YACzC,kDAAkD;YAClD,iBAAiB;YACjB,iDAAiD;YACjD,EAAE;YACF,qDAAqD;YACrD,4FAA4F;SAC7F,CAAC,IAAI,CAAC,IAAI,CAAC,CACb,CAAC;QAEF,OAAO;YACL;gBACE,IAAI,EAAE,MAAM;gBACZ,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;aACzD;SACF,CAAC;IACJ,CAAC;IAED,mBAAmB,CAAC,QAA4B;QAC9C,OAAO,QAAQ;aACZ,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;YACT,MAAM,IAAI,GAAG,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC;YACtD,MAAM,KAAK,GAAG,CAAC,CAAC,OAAO;iBACpB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;gBACT,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;oBACf,KAAK,MAAM;wBACT,OAAO,CAAC,CAAC,IAAI,CAAC;oBAChB,KAAK,UAAU;wBACb,OAAO,cAAc,CAAC,CAAC,OAAO,GAAG,CAAC;oBACpC,KAAK,MAAM;wBACT,OAAO,iBAAiB,CAAC,CAAC,IAAI,GAAG,CAAC;oBACpC,KAAK,aAAa;wBAChB,OAAO,SAAS,CAAC,CAAC,IAAI,cAAc,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC;oBAClE;wBACE,OAAO,SAAS,CAAC;gBACrB,CAAC;YACH,CAAC,CAAC;iBACD,MAAM,CAAC,OAAO,CAAC,CAAC;YACnB,OAAO,GAAG,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACxC,CAAC,CAAC;aACD,IAAI,CAAC,MAAM,CAAC,CAAC;IAClB,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ export * from "./types.ts";
2
+ export * from "./default-summarization-strategy.ts";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/checkpoints/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,qCAAqC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./types.js";
2
+ export * from "./default-summarization-strategy.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/checkpoints/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,qCAAqC,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { Message } from "../conversations/types.ts";
2
+ import type { CheckpointState } from "../context-transformers/types.ts";
3
+ /**
4
+ * Configuration for a checkpoint operation.
5
+ */
6
+ export interface CheckpointConfig {
7
+ /** Arbitrary context passed through to the prompt strategy. */
8
+ context?: Record<string, unknown>;
9
+ }
10
+ /**
11
+ * Strategy for generating a checkpoint summary of a conversation.
12
+ */
13
+ export interface SummarizationStrategy {
14
+ /** Builds the messages for the checkpoint conversation from the parent's context. */
15
+ build_prompt(context: SummarizationContext): Message[];
16
+ }
17
+ /**
18
+ * Context provided to the summarization strategy.
19
+ */
20
+ export interface SummarizationContext {
21
+ /** The session ID of the conversation being checkpointed. */
22
+ session_id: string;
23
+ /** The conversation messages since the last checkpoint (or all if first checkpoint). */
24
+ messages: readonly Message[];
25
+ /** The previous checkpoint state, if this is an incremental checkpoint. */
26
+ previous_checkpoint?: CheckpointState;
27
+ /** The system prompt of the conversation being checkpointed. */
28
+ system?: string;
29
+ /** Arbitrary context forwarded from CheckpointConfig. */
30
+ context?: Record<string, unknown>;
31
+ }
32
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/checkpoints/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAExE;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,+DAA+D;IAC/D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,qFAAqF;IACrF,YAAY,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,EAAE,CAAC;CACxD;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,6DAA6D;IAC7D,UAAU,EAAE,MAAM,CAAC;IACnB,wFAAwF;IACxF,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;IAC7B,2EAA2E;IAC3E,mBAAmB,CAAC,EAAE,eAAe,CAAC;IACtC,gEAAgE;IAChE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,yDAAyD;IACzD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/checkpoints/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import type { AssistantMessage, Message } from "../conversations/index.ts";
2
+ import type { ContextTransformer, TransformContext } from "./types.ts";
3
+ /**
4
+ * Context transformer that trims pre-checkpoint messages and inserts
5
+ * the checkpoint summary as a synthetic first user message. The boundary
6
+ * message (an assistant response) is retained as the natural response to
7
+ * the summary, maintaining proper message alternation.
8
+ */
9
+ export declare class CheckpointContextTransformer implements ContextTransformer {
10
+ transform_prompt(messages: Message[], context?: TransformContext): Promise<Message[]>;
11
+ transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
12
+ }
13
+ //# sourceMappingURL=checkpoint-context-transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkpoint-context-transformer.d.ts","sourceRoot":"","sources":["../../src/context-transformers/checkpoint-context-transformer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAC3E,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEvE;;;;;GAKG;AACH,qBAAa,4BAA6B,YAAW,kBAAkB;IAC/D,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAwB3F,oBAAoB,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAG3E"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Context transformer that trims pre-checkpoint messages and inserts
3
+ * the checkpoint summary as a synthetic first user message. The boundary
4
+ * message (an assistant response) is retained as the natural response to
5
+ * the summary, maintaining proper message alternation.
6
+ */
7
+ export class CheckpointContextTransformer {
8
+ async transform_prompt(messages, context) {
9
+ const checkpoint = context?.checkpoint;
10
+ if (!checkpoint) {
11
+ return messages;
12
+ }
13
+ const boundary = messages.findIndex((m) => m.id === checkpoint.message_id);
14
+ if (boundary === -1) {
15
+ return messages;
16
+ }
17
+ const summary_message = {
18
+ role: "user",
19
+ timestamp: Date.now(),
20
+ content: [{ type: "text", timestamp: Date.now(), text: checkpoint.summary }],
21
+ };
22
+ const boundary_message = messages[boundary];
23
+ if (boundary_message.role === "user") {
24
+ return [summary_message, ...messages.slice(boundary + 1)];
25
+ }
26
+ return [summary_message, ...messages.slice(boundary)];
27
+ }
28
+ transform_completion(message) {
29
+ return Promise.resolve(message);
30
+ }
31
+ }
32
+ //# sourceMappingURL=checkpoint-context-transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkpoint-context-transformer.js","sourceRoot":"","sources":["../../src/context-transformers/checkpoint-context-transformer.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,OAAO,4BAA4B;IACvC,KAAK,CAAC,gBAAgB,CAAC,QAAmB,EAAE,OAA0B;QACpE,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,CAAC;QACvC,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,MAAM,QAAQ,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,UAAU,CAAC,UAAU,CAAC,CAAC;QAC3E,IAAI,QAAQ,KAAK,CAAC,CAAC,EAAE,CAAC;YACpB,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,MAAM,eAAe,GAAY;YAC/B,IAAI,EAAE,MAAM;YACZ,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;YACrB,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,UAAU,CAAC,OAAO,EAAE,CAAC;SAC7E,CAAC;QAEF,MAAM,gBAAgB,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC5C,IAAI,gBAAgB,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YACrC,OAAO,CAAC,eAAe,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC;QAC5D,CAAC;QACD,OAAO,CAAC,eAAe,EAAE,GAAG,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;IACxD,CAAC;IAED,oBAAoB,CAAC,OAAyB;QAC5C,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;CACF"}
@@ -0,0 +1,31 @@
1
+ import { AssistantMessage, Message } from "../conversations/index.ts";
2
+ import { ContextTransformer, TransformContext } from "./types.ts";
3
+ /**
4
+ * Combines multiple context transformers into a single transformation pipeline.
5
+ *
6
+ * Transformers are applied in the order provided.
7
+ */
8
+ export declare class CompositeContextTransformer implements ContextTransformer {
9
+ #private;
10
+ /**
11
+ * Creates a new composite context transformer.
12
+ *
13
+ * @param transformers - The transformers to compose.
14
+ */
15
+ constructor(transformers: ContextTransformer[]);
16
+ /**
17
+ * Transforms prompt messages by applying all transformers in sequence.
18
+ *
19
+ * @param messages - The messages to transform.
20
+ * @returns A promise that resolves to the transformed messages.
21
+ */
22
+ transform_prompt(messages: Message[], context?: TransformContext): Promise<Message[]>;
23
+ /**
24
+ * Transforms a completion message by applying all transformers in sequence.
25
+ *
26
+ * @param message - The assistant message to transform.
27
+ * @returns A promise that resolves to the transformed message.
28
+ */
29
+ transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
30
+ }
31
+ //# sourceMappingURL=composite-context-transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"composite-context-transformer.d.ts","sourceRoot":"","sources":["../../src/context-transformers/composite-context-transformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAElE;;;;GAIG;AACH,qBAAa,2BAA4B,YAAW,kBAAkB;;IAGpE;;;;OAIG;gBACS,YAAY,EAAE,kBAAkB,EAAE;IAI9C;;;;;OAKG;IACG,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAO3F;;;;;OAKG;IACG,oBAAoB,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAMjF"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Combines multiple context transformers into a single transformation pipeline.
3
+ *
4
+ * Transformers are applied in the order provided.
5
+ */
6
+ export class CompositeContextTransformer {
7
+ #transformers;
8
+ /**
9
+ * Creates a new composite context transformer.
10
+ *
11
+ * @param transformers - The transformers to compose.
12
+ */
13
+ constructor(transformers) {
14
+ this.#transformers = transformers;
15
+ }
16
+ /**
17
+ * Transforms prompt messages by applying all transformers in sequence.
18
+ *
19
+ * @param messages - The messages to transform.
20
+ * @returns A promise that resolves to the transformed messages.
21
+ */
22
+ async transform_prompt(messages, context) {
23
+ for (const transformer of this.#transformers) {
24
+ messages = await transformer.transform_prompt(messages, context);
25
+ }
26
+ return messages;
27
+ }
28
+ /**
29
+ * Transforms a completion message by applying all transformers in sequence.
30
+ *
31
+ * @param message - The assistant message to transform.
32
+ * @returns A promise that resolves to the transformed message.
33
+ */
34
+ async transform_completion(message) {
35
+ for (const transformer of this.#transformers) {
36
+ message = await transformer.transform_completion(message);
37
+ }
38
+ return message;
39
+ }
40
+ }
41
+ //# sourceMappingURL=composite-context-transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"composite-context-transformer.js","sourceRoot":"","sources":["../../src/context-transformers/composite-context-transformer.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,OAAO,2BAA2B;IAC7B,aAAa,CAAuB;IAE7C;;;;OAIG;IACH,YAAY,YAAkC;QAC5C,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;IACpC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,gBAAgB,CAAC,QAAmB,EAAE,OAA0B;QACpE,KAAK,MAAM,WAAW,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YAC7C,QAAQ,GAAG,MAAM,WAAW,CAAC,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;QACnE,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,oBAAoB,CAAC,OAAyB;QAClD,KAAK,MAAM,WAAW,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YAC7C,OAAO,GAAG,MAAM,WAAW,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAC5D,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ export * from "./checkpoint-context-transformer.ts";
2
+ export * from "./composite-context-transformer.ts";
3
+ export * from "./noop-context-transformer.ts";
4
+ export * from "./tool-context-transformer.ts";
5
+ export * from "./types.ts";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context-transformers/index.ts"],"names":[],"mappings":"AAAA,cAAc,qCAAqC,CAAC;AACpD,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,YAAY,CAAC"}
@@ -0,0 +1,6 @@
1
+ export * from "./checkpoint-context-transformer.js";
2
+ export * from "./composite-context-transformer.js";
3
+ export * from "./noop-context-transformer.js";
4
+ export * from "./tool-context-transformer.js";
5
+ export * from "./types.js";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context-transformers/index.ts"],"names":[],"mappings":"AAAA,cAAc,qCAAqC,CAAC;AACpD,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,YAAY,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { AssistantMessage, Message } from "../conversations/index.ts";
2
+ import { ContextTransformer } from "./types.ts";
3
+ /**
4
+ * Context transformer that performs no transformations.
5
+ *
6
+ * Returns messages unchanged.
7
+ */
8
+ export declare class NoopContextTransformer implements ContextTransformer {
9
+ /**
10
+ * Returns the prompt messages unchanged.
11
+ *
12
+ * @param messages - The messages to pass through.
13
+ * @returns A promise that resolves to the same messages.
14
+ */
15
+ transform_prompt(messages: Message[]): Promise<Message[]>;
16
+ /**
17
+ * Returns the completion message unchanged.
18
+ *
19
+ * @param message - The assistant message to pass through.
20
+ * @returns A promise that resolves to the same message.
21
+ */
22
+ transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
23
+ }
24
+ //# sourceMappingURL=noop-context-transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"noop-context-transformer.d.ts","sourceRoot":"","sources":["../../src/context-transformers/noop-context-transformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;GAIG;AACH,qBAAa,sBAAuB,YAAW,kBAAkB;IAC/D;;;;;OAKG;IACH,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAIzD;;;;;OAKG;IACH,oBAAoB,CAAC,OAAO,EAAE,gBAAgB;CAG/C"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Context transformer that performs no transformations.
3
+ *
4
+ * Returns messages unchanged.
5
+ */
6
+ export class NoopContextTransformer {
7
+ /**
8
+ * Returns the prompt messages unchanged.
9
+ *
10
+ * @param messages - The messages to pass through.
11
+ * @returns A promise that resolves to the same messages.
12
+ */
13
+ transform_prompt(messages) {
14
+ return Promise.resolve(messages);
15
+ }
16
+ /**
17
+ * Returns the completion message unchanged.
18
+ *
19
+ * @param message - The assistant message to pass through.
20
+ * @returns A promise that resolves to the same message.
21
+ */
22
+ transform_completion(message) {
23
+ return Promise.resolve(message);
24
+ }
25
+ }
26
+ //# sourceMappingURL=noop-context-transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"noop-context-transformer.js","sourceRoot":"","sources":["../../src/context-transformers/noop-context-transformer.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,OAAO,sBAAsB;IACjC;;;;;OAKG;IACH,gBAAgB,CAAC,QAAmB;QAClC,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;IAED;;;;;OAKG;IACH,oBAAoB,CAAC,OAAyB;QAC5C,OAAO,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAClC,CAAC;CACF"}
@@ -0,0 +1,26 @@
1
+ import { AssistantMessage, Message } from "../conversations/index.ts";
2
+ import { ContextTransformer } from "./types.ts";
3
+ /**
4
+ * Context transformer that removes unreferenced tool invocations from message history.
5
+ *
6
+ * Filters out tool content blocks that don't have a corresponding tool_result,
7
+ * reducing context size when tools were invoked but their results are no longer
8
+ * relevant to the conversation.
9
+ */
10
+ export declare class ToolContextTransformer implements ContextTransformer {
11
+ /**
12
+ * Transforms prompt messages by removing unreferenced tool invocations.
13
+ *
14
+ * @param messages - The messages to transform.
15
+ * @returns A promise that resolves to the transformed messages.
16
+ */
17
+ transform_prompt(messages: Message[]): Promise<Message[]>;
18
+ /**
19
+ * Returns the completion message unchanged.
20
+ *
21
+ * @param message - The assistant message to pass through.
22
+ * @returns A promise that resolves to the same message.
23
+ */
24
+ transform_completion(message: AssistantMessage): Promise<AssistantMessage>;
25
+ }
26
+ //# sourceMappingURL=tool-context-transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-context-transformer.d.ts","sourceRoot":"","sources":["../../src/context-transformers/tool-context-transformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;;;GAMG;AACH,qBAAa,sBAAuB,YAAW,kBAAkB;IAC/D;;;;;OAKG;IACH,gBAAgB,CAAC,QAAQ,EAAE,OAAO,EAAE;IA+BpC;;;;;OAKG;IACH,oBAAoB,CAAC,OAAO,EAAE,gBAAgB;CAG/C"}