@deepagents/context 0.10.2 → 0.12.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 (50) hide show
  1. package/README.md +114 -119
  2. package/dist/example-error-recovery.d.ts +2 -0
  3. package/dist/example-error-recovery.d.ts.map +1 -0
  4. package/dist/index.d.ts +18 -388
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +2765 -1091
  7. package/dist/index.js.map +4 -4
  8. package/dist/lib/agent.d.ts +87 -12
  9. package/dist/lib/agent.d.ts.map +1 -1
  10. package/dist/lib/engine.d.ts +325 -0
  11. package/dist/lib/engine.d.ts.map +1 -0
  12. package/dist/lib/estimate.d.ts +1 -1
  13. package/dist/lib/estimate.d.ts.map +1 -1
  14. package/dist/lib/fragments/domain.d.ts +537 -0
  15. package/dist/lib/fragments/domain.d.ts.map +1 -0
  16. package/dist/lib/fragments/user.d.ts +122 -0
  17. package/dist/lib/fragments/user.d.ts.map +1 -0
  18. package/dist/lib/fragments.d.ts +103 -0
  19. package/dist/lib/fragments.d.ts.map +1 -0
  20. package/dist/lib/guardrail.d.ts +138 -0
  21. package/dist/lib/guardrail.d.ts.map +1 -0
  22. package/dist/lib/guardrails/error-recovery.guardrail.d.ts +3 -0
  23. package/dist/lib/guardrails/error-recovery.guardrail.d.ts.map +1 -0
  24. package/dist/lib/render.d.ts +21 -0
  25. package/dist/lib/render.d.ts.map +1 -0
  26. package/dist/lib/renderers/abstract.renderer.d.ts +11 -3
  27. package/dist/lib/renderers/abstract.renderer.d.ts.map +1 -1
  28. package/dist/lib/sandbox/binary-bridges.d.ts +31 -0
  29. package/dist/lib/sandbox/binary-bridges.d.ts.map +1 -0
  30. package/dist/lib/sandbox/container-tool.d.ts +134 -0
  31. package/dist/lib/sandbox/container-tool.d.ts.map +1 -0
  32. package/dist/lib/sandbox/docker-sandbox.d.ts +471 -0
  33. package/dist/lib/sandbox/docker-sandbox.d.ts.map +1 -0
  34. package/dist/lib/sandbox/index.d.ts +4 -0
  35. package/dist/lib/sandbox/index.d.ts.map +1 -0
  36. package/dist/lib/skills/fragments.d.ts +24 -0
  37. package/dist/lib/skills/fragments.d.ts.map +1 -0
  38. package/dist/lib/skills/index.d.ts +31 -0
  39. package/dist/lib/skills/index.d.ts.map +1 -0
  40. package/dist/lib/skills/loader.d.ts +28 -0
  41. package/dist/lib/skills/loader.d.ts.map +1 -0
  42. package/dist/lib/skills/types.d.ts +40 -0
  43. package/dist/lib/skills/types.d.ts.map +1 -0
  44. package/dist/lib/store/sqlite.store.d.ts +4 -2
  45. package/dist/lib/store/sqlite.store.d.ts.map +1 -1
  46. package/dist/lib/store/store.d.ts +36 -2
  47. package/dist/lib/store/store.d.ts.map +1 -1
  48. package/package.json +8 -4
  49. package/dist/lib/context.d.ts +0 -56
  50. package/dist/lib/context.d.ts.map +0 -1
package/README.md CHANGED
@@ -4,7 +4,7 @@ A domain-agnostic context management system for formatting context fragments int
4
4
 
5
5
  ## Overview
6
6
 
7
- This package provides a simple and flexible way to render context data in multiple formats (XML, Markdown, TOML). Context fragments are simple data structures that can be transformed into different representations suitable for various LLM prompt styles.
7
+ This package provides a flexible way to compose and render context data in multiple formats (XML, Markdown, TOML, TOON). Context fragments are simple data structures that can be transformed into different representations suitable for various LLM prompt styles.
8
8
 
9
9
  ## Installation
10
10
 
@@ -15,106 +15,156 @@ npm install @deepagents/context
15
15
  ## Basic Usage
16
16
 
17
17
  ```typescript
18
- import {
19
- MarkdownRenderer,
20
- TomlRenderer,
21
- XmlRenderer,
22
- } from '@deepagents/context';
23
- import type { ContextFragment } from '@deepagents/context';
24
-
25
- // Define your context fragments
26
- const fragments: ContextFragment[] = [
27
- {
28
- name: 'styleGuide',
29
- data: {
30
- prefer: 'CTEs',
31
- never: 'subqueries',
32
- indentation: 2,
33
- },
34
- },
18
+ import { XmlRenderer, guardrail, hint, term } from '@deepagents/context';
19
+
20
+ // Create fragments using builder functions
21
+ const fragments = [
22
+ term('MRR', 'monthly recurring revenue'),
23
+ hint('Always exclude test accounts'),
24
+ guardrail({
25
+ rule: 'Never expose PII',
26
+ reason: 'Privacy compliance',
27
+ action: 'Aggregate data instead',
28
+ }),
35
29
  ];
36
30
 
37
- // Render in different formats
38
- const xmlRenderer = new XmlRenderer();
39
- console.log(xmlRenderer.render(fragments));
31
+ // Render to XML
32
+ const renderer = new XmlRenderer({ groupFragments: true });
33
+ console.log(renderer.render(fragments));
34
+ ```
40
35
 
41
- const mdRenderer = new MarkdownRenderer();
42
- console.log(mdRenderer.render(fragments));
36
+ **Output:**
43
37
 
44
- const tomlRenderer = new TomlRenderer();
45
- console.log(tomlRenderer.render(fragments));
46
- ```
38
+ ```xml
39
+ <terms>
40
+ <term>
41
+ <name>MRR</name>
42
+ <definition>monthly recurring revenue</definition>
43
+ </term>
44
+ </terms>
45
+ <hints>
46
+ <hint>Always exclude test accounts</hint>
47
+ </hints>
48
+ <guardrails>
49
+ <guardrail>
50
+ <rule>Never expose PII</rule>
51
+ <reason>Privacy compliance</reason>
52
+ <action>Aggregate data instead</action>
53
+ </guardrail>
54
+ </guardrails>
55
+ ```
56
+
57
+ ## Fragment Builders
58
+
59
+ ### Domain Fragments
60
+
61
+ Builder functions for injecting domain knowledge into prompts:
62
+
63
+ | Function | Description | Example |
64
+ | --------------------------------------------- | -------------------------------- | --------------------------------------------------- |
65
+ | `term(name, definition)` | Define business vocabulary | `term('NPL', 'non-performing loan')` |
66
+ | `hint(text)` | Behavioral rules and constraints | `hint('Always filter by status')` |
67
+ | `guardrail({rule, reason?, action?})` | Safety rules and boundaries | `guardrail({ rule: 'No PII' })` |
68
+ | `explain({concept, explanation, therefore?})` | Rich concept explanations | `explain({ concept: 'churn', explanation: '...' })` |
69
+ | `example({question, answer, note?})` | Question-answer pairs | `example({ question: '...', answer: '...' })` |
70
+ | `clarification({when, ask, reason})` | When to ask for more info | `clarification({ when: '...', ask: '...' })` |
71
+ | `workflow({task, steps, triggers?, notes?})` | Multi-step processes | `workflow({ task: '...', steps: [...] })` |
72
+ | `quirk({issue, workaround})` | Data edge cases | `quirk({ issue: '...', workaround: '...' })` |
73
+ | `styleGuide({prefer, never?, always?})` | Style preferences | `styleGuide({ prefer: 'CTEs' })` |
74
+ | `analogy({concepts, relationship, ...})` | Concept comparisons | `analogy({ concepts: [...], relationship: '...' })` |
75
+ | `glossary(entries)` | Term-to-expression mapping | `glossary({ revenue: 'SUM(amount)' })` |
76
+
77
+ ### User Fragments
78
+
79
+ Builder functions for user-specific context:
80
+
81
+ | Function | Description | Example |
82
+ | ------------------------------------ | ---------------------------- | ---------------------------------------------- |
83
+ | `identity({name?, role?})` | User identity | `identity({ role: 'VP Sales' })` |
84
+ | `persona({name, role, tone?})` | AI persona definition | `persona({ name: 'Freya', role: '...' })` |
85
+ | `alias(term, meaning)` | User-specific vocabulary | `alias('revenue', 'gross revenue')` |
86
+ | `preference(aspect, value)` | Output preferences | `preference('date format', 'YYYY-MM-DD')` |
87
+ | `userContext(description)` | Current working focus | `userContext('Q4 analysis')` |
88
+ | `correction(subject, clarification)` | Corrections to understanding | `correction('status', '1=active, 0=inactive')` |
89
+
90
+ ### Core Utilities
91
+
92
+ | Function | Description |
93
+ | ----------------------------- | ---------------------------------------------- |
94
+ | `fragment(name, ...children)` | Create a wrapper fragment with nested children |
95
+ | `role(content)` | System role/instructions fragment |
47
96
 
48
97
  ## Renderers
49
98
 
99
+ All renderers support the `groupFragments` option which groups same-named fragments under a pluralized parent tag.
100
+
50
101
  ### XmlRenderer
51
102
 
52
103
  Renders fragments as XML with proper nesting and escaping:
53
104
 
105
+ ```typescript
106
+ const renderer = new XmlRenderer({ groupFragments: true });
107
+ ```
108
+
54
109
  ```xml
55
110
  <styleGuide>
56
111
  <prefer>CTEs</prefer>
57
112
  <never>subqueries</never>
58
- <indentation>2</indentation>
59
113
  </styleGuide>
60
114
  ```
61
115
 
62
- **Features:**
63
-
64
- - Automatic XML escaping for special characters
65
- - Nested object support
66
- - Array handling with singular form tags
67
- - Proper indentation
68
-
69
116
  ### MarkdownRenderer
70
117
 
71
118
  Renders fragments as Markdown with bullet points:
72
119
 
120
+ ```typescript
121
+ const renderer = new MarkdownRenderer();
122
+ ```
123
+
73
124
  ```markdown
74
125
  ## Style Guide
75
126
 
76
127
  - **prefer**: CTEs
77
128
  - **never**: subqueries
78
- - **indentation**: 2
79
129
  ```
80
130
 
81
- **Features:**
82
-
83
- - Automatic title case conversion for fragment names
84
- - Nested structures with proper indentation
85
- - Array items as bullet points
86
- - Bold keys for readability
87
-
88
131
  ### TomlRenderer
89
132
 
90
133
  Renders fragments as TOML-like format:
91
134
 
135
+ ```typescript
136
+ const renderer = new TomlRenderer();
137
+ ```
138
+
92
139
  ```toml
93
140
  [styleGuide]
94
141
  prefer = "CTEs"
95
142
  never = "subqueries"
96
- indentation = 2
97
143
  ```
98
144
 
99
- **Features:**
145
+ ### ToonRenderer
146
+
147
+ Token-efficient format with CSV-style tables for uniform arrays:
148
+
149
+ ```typescript
150
+ const renderer = new ToonRenderer();
151
+ ```
100
152
 
101
- - TOML section headers
102
- - Nested objects as subsections
103
- - Array support with proper formatting
104
- - String escaping for quotes and backslashes
153
+ ```yaml
154
+ styleGuide:
155
+ prefer: CTEs
156
+ never: subqueries
157
+ ```
105
158
 
106
159
  ## Handling Complex Data
107
160
 
108
161
  ### Arrays
109
162
 
110
163
  ```typescript
111
- const fragment = {
112
- name: 'workflow',
113
- data: {
114
- task: 'Analysis',
115
- steps: ['step1', 'step2', 'step3'],
116
- },
117
- };
164
+ const fragment = workflow({
165
+ task: 'Analysis',
166
+ steps: ['step1', 'step2', 'step3'],
167
+ });
118
168
  ```
119
169
 
120
170
  **XML Output:**
@@ -130,26 +180,6 @@ const fragment = {
130
180
  </workflow>
131
181
  ```
132
182
 
133
- **Markdown Output:**
134
-
135
- ```markdown
136
- ## Workflow
137
-
138
- - **task**: Analysis
139
- - **steps**:
140
- - step1
141
- - step2
142
- - step3
143
- ```
144
-
145
- **TOML Output:**
146
-
147
- ```toml
148
- [workflow]
149
- task = "Analysis"
150
- steps = ["step1", "step2", "step3"]
151
- ```
152
-
153
183
  ### Nested Objects
154
184
 
155
185
  ```typescript
@@ -177,42 +207,9 @@ const fragment = {
177
207
  </database>
178
208
  ```
179
209
 
180
- **Markdown Output:**
181
-
182
- ```markdown
183
- ## Database
184
-
185
- - **host**: localhost
186
- - **settings**:
187
- - **timeout**: 30
188
- - **retry**: true
189
- ```
190
-
191
- **TOML Output:**
192
-
193
- ```toml
194
- [database]
195
- host = "localhost"
196
-
197
- [settings]
198
- timeout = 30
199
- retry = true
200
- ```
201
-
202
210
  ### Null and Undefined Values
203
211
 
204
- All renderers automatically skip `null` and `undefined` values:
205
-
206
- ```typescript
207
- const fragment = {
208
- name: 'config',
209
- data: {
210
- enabled: true,
211
- disabled: null, // Will be skipped
212
- missing: undefined, // Will be skipped
213
- },
214
- };
215
- ```
212
+ All renderers automatically skip `null` and `undefined` values.
216
213
 
217
214
  ## API Reference
218
215
 
@@ -223,31 +220,29 @@ const fragment = {
223
220
  ```typescript
224
221
  interface ContextFragment {
225
222
  name: string;
226
- data: Record<string, unknown>;
223
+ data: FragmentData;
224
+ type?: 'fragment' | 'message';
225
+ persist?: boolean;
226
+ codec?: FragmentCodec;
227
227
  }
228
228
  ```
229
229
 
230
230
  #### ContextRenderer
231
231
 
232
232
  ```typescript
233
- interface ContextRenderer {
234
- render(fragments: ContextFragment[]): string;
233
+ abstract class ContextRenderer {
234
+ abstract render(fragments: ContextFragment[]): string;
235
235
  }
236
236
  ```
237
237
 
238
238
  ### Classes
239
239
 
240
- All renderer classes implement the `ContextRenderer` interface:
240
+ All renderer classes extend `ContextRenderer`:
241
241
 
242
242
  - `XmlRenderer` - Renders as XML
243
243
  - `MarkdownRenderer` - Renders as Markdown
244
244
  - `TomlRenderer` - Renders as TOML
245
-
246
- Each class has a single public method:
247
-
248
- ```typescript
249
- render(fragments: ContextFragment[]): string
250
- ```
245
+ - `ToonRenderer` - Token-efficient format
251
246
 
252
247
  ## License
253
248
 
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=example-error-recovery.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"example-error-recovery.d.ts","sourceRoot":"","sources":["../src/example-error-recovery.ts"],"names":[],"mappings":""}