@deepagents/context 0.26.0 → 0.27.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.
package/dist/browser.js CHANGED
@@ -1,3 +1,763 @@
1
+ // packages/context/src/lib/fragments.ts
2
+ import { generateId } from "ai";
3
+ function isFragment(data) {
4
+ return typeof data === "object" && data !== null && "name" in data && ("data" in data || "codec" in data) && typeof data.name === "string";
5
+ }
6
+ function isFragmentObject(data) {
7
+ return typeof data === "object" && data !== null && !Array.isArray(data) && !isFragment(data);
8
+ }
9
+ function isMessageFragment(fragment2) {
10
+ return fragment2.type === "message";
11
+ }
12
+ function getFragmentData(fragment2) {
13
+ if (fragment2.codec) {
14
+ return fragment2.codec.decode();
15
+ }
16
+ if ("data" in fragment2) {
17
+ return fragment2.data;
18
+ }
19
+ throw new Error(`Fragment "${fragment2.name}" is missing data and codec`);
20
+ }
21
+ function fragment(name, ...children) {
22
+ return {
23
+ name,
24
+ data: children
25
+ };
26
+ }
27
+ function assistant(message2) {
28
+ return {
29
+ id: message2.id,
30
+ name: "assistant",
31
+ type: "message",
32
+ persist: true,
33
+ codec: {
34
+ decode() {
35
+ return message2;
36
+ },
37
+ encode() {
38
+ return message2;
39
+ }
40
+ }
41
+ };
42
+ }
43
+ function message(content) {
44
+ const message2 = typeof content === "string" ? {
45
+ id: generateId(),
46
+ role: "user",
47
+ parts: [{ type: "text", text: content }]
48
+ } : content;
49
+ return {
50
+ id: message2.id,
51
+ name: message2.role,
52
+ type: "message",
53
+ persist: true,
54
+ codec: {
55
+ decode() {
56
+ return message2;
57
+ },
58
+ encode() {
59
+ return message2;
60
+ }
61
+ }
62
+ };
63
+ }
64
+ function assistantText(content, options) {
65
+ const id = options?.id ?? crypto.randomUUID();
66
+ return assistant({
67
+ id,
68
+ role: "assistant",
69
+ parts: [{ type: "text", text: content }]
70
+ });
71
+ }
72
+ var LAZY_ID = Symbol.for("@deepagents/context:lazy-id");
73
+ function isLazyFragment(fragment2) {
74
+ return LAZY_ID in fragment2;
75
+ }
76
+ function lastAssistantMessage(content) {
77
+ return {
78
+ name: "assistant",
79
+ type: "message",
80
+ persist: true,
81
+ data: "content",
82
+ [LAZY_ID]: {
83
+ type: "last-assistant",
84
+ content
85
+ }
86
+ };
87
+ }
88
+
89
+ // packages/context/src/lib/codec/serialized-codec.ts
90
+ function encodeSerializedValue(value) {
91
+ if (isFragment(value)) {
92
+ if (isMessageFragment(value)) {
93
+ throw new Error(
94
+ "Message fragments are not supported by serialized fragment conversion"
95
+ );
96
+ }
97
+ if (!value.codec) {
98
+ throw new Error(`Fragment "${value.name}" is missing codec`);
99
+ }
100
+ return value.codec.encode();
101
+ }
102
+ if (Array.isArray(value)) {
103
+ return value.map((item) => encodeSerializedValue(item));
104
+ }
105
+ if (isFragmentObject(value)) {
106
+ return Object.fromEntries(
107
+ Object.entries(value).map(([key, entry]) => [
108
+ key,
109
+ encodeSerializedValue(entry)
110
+ ])
111
+ );
112
+ }
113
+ return value;
114
+ }
115
+
116
+ // packages/context/src/lib/fragments/domain.ts
117
+ function term(name, definition) {
118
+ return {
119
+ name: "term",
120
+ data: { name, definition },
121
+ codec: {
122
+ encode() {
123
+ return { type: "term", name, definition };
124
+ },
125
+ decode() {
126
+ return { name, definition };
127
+ }
128
+ }
129
+ };
130
+ }
131
+ function hint(text) {
132
+ return {
133
+ name: "hint",
134
+ data: text,
135
+ codec: {
136
+ encode() {
137
+ return { type: "hint", text };
138
+ },
139
+ decode() {
140
+ return text;
141
+ }
142
+ }
143
+ };
144
+ }
145
+ function guardrail(input) {
146
+ return {
147
+ name: "guardrail",
148
+ data: {
149
+ rule: input.rule,
150
+ ...input.reason && { reason: input.reason },
151
+ ...input.action && { action: input.action }
152
+ },
153
+ codec: {
154
+ encode() {
155
+ return {
156
+ type: "guardrail",
157
+ rule: input.rule,
158
+ ...input.reason && { reason: input.reason },
159
+ ...input.action && { action: input.action }
160
+ };
161
+ },
162
+ decode() {
163
+ return {
164
+ rule: input.rule,
165
+ ...input.reason && { reason: input.reason },
166
+ ...input.action && { action: input.action }
167
+ };
168
+ }
169
+ }
170
+ };
171
+ }
172
+ function explain(input) {
173
+ return {
174
+ name: "explain",
175
+ data: {
176
+ concept: input.concept,
177
+ explanation: input.explanation,
178
+ ...input.therefore && { therefore: input.therefore }
179
+ },
180
+ codec: {
181
+ encode() {
182
+ return {
183
+ type: "explain",
184
+ concept: input.concept,
185
+ explanation: input.explanation,
186
+ ...input.therefore && { therefore: input.therefore }
187
+ };
188
+ },
189
+ decode() {
190
+ return {
191
+ concept: input.concept,
192
+ explanation: input.explanation,
193
+ ...input.therefore && { therefore: input.therefore }
194
+ };
195
+ }
196
+ }
197
+ };
198
+ }
199
+ function example(input) {
200
+ return {
201
+ name: "example",
202
+ data: {
203
+ question: input.question,
204
+ answer: input.answer,
205
+ ...input.note && { note: input.note }
206
+ },
207
+ codec: {
208
+ encode() {
209
+ return {
210
+ type: "example",
211
+ question: input.question,
212
+ answer: input.answer,
213
+ ...input.note && { note: input.note }
214
+ };
215
+ },
216
+ decode() {
217
+ return {
218
+ question: input.question,
219
+ answer: input.answer,
220
+ ...input.note && { note: input.note }
221
+ };
222
+ }
223
+ }
224
+ };
225
+ }
226
+ function clarification(input) {
227
+ return {
228
+ name: "clarification",
229
+ data: {
230
+ when: input.when,
231
+ ask: input.ask,
232
+ reason: input.reason
233
+ },
234
+ codec: {
235
+ encode() {
236
+ return {
237
+ type: "clarification",
238
+ when: input.when,
239
+ ask: input.ask,
240
+ reason: input.reason
241
+ };
242
+ },
243
+ decode() {
244
+ return {
245
+ when: input.when,
246
+ ask: input.ask,
247
+ reason: input.reason
248
+ };
249
+ }
250
+ }
251
+ };
252
+ }
253
+ function workflow(input) {
254
+ return {
255
+ name: "workflow",
256
+ data: {
257
+ task: input.task,
258
+ steps: input.steps,
259
+ ...input.triggers?.length && { triggers: input.triggers },
260
+ ...input.notes && { notes: input.notes }
261
+ },
262
+ codec: {
263
+ encode() {
264
+ return {
265
+ type: "workflow",
266
+ task: input.task,
267
+ steps: input.steps,
268
+ ...input.triggers?.length && { triggers: input.triggers },
269
+ ...input.notes && { notes: input.notes }
270
+ };
271
+ },
272
+ decode() {
273
+ return {
274
+ task: input.task,
275
+ steps: input.steps,
276
+ ...input.triggers?.length && { triggers: input.triggers },
277
+ ...input.notes && { notes: input.notes }
278
+ };
279
+ }
280
+ }
281
+ };
282
+ }
283
+ function quirk(input) {
284
+ return {
285
+ name: "quirk",
286
+ data: {
287
+ issue: input.issue,
288
+ workaround: input.workaround
289
+ },
290
+ codec: {
291
+ encode() {
292
+ return {
293
+ type: "quirk",
294
+ issue: input.issue,
295
+ workaround: input.workaround
296
+ };
297
+ },
298
+ decode() {
299
+ return {
300
+ issue: input.issue,
301
+ workaround: input.workaround
302
+ };
303
+ }
304
+ }
305
+ };
306
+ }
307
+ function styleGuide(input) {
308
+ return {
309
+ name: "styleGuide",
310
+ data: {
311
+ prefer: input.prefer,
312
+ ...input.never && { never: input.never },
313
+ ...input.always && { always: input.always }
314
+ },
315
+ codec: {
316
+ encode() {
317
+ return {
318
+ type: "styleGuide",
319
+ prefer: input.prefer,
320
+ ...input.never && { never: input.never },
321
+ ...input.always && { always: input.always }
322
+ };
323
+ },
324
+ decode() {
325
+ return {
326
+ prefer: input.prefer,
327
+ ...input.never && { never: input.never },
328
+ ...input.always && { always: input.always }
329
+ };
330
+ }
331
+ }
332
+ };
333
+ }
334
+ function analogy(input) {
335
+ return {
336
+ name: "analogy",
337
+ data: {
338
+ concepts: input.concepts,
339
+ relationship: input.relationship,
340
+ ...input.insight && { insight: input.insight },
341
+ ...input.therefore && { therefore: input.therefore },
342
+ ...input.pitfall && { pitfall: input.pitfall }
343
+ },
344
+ codec: {
345
+ encode() {
346
+ return {
347
+ type: "analogy",
348
+ concepts: input.concepts,
349
+ relationship: input.relationship,
350
+ ...input.insight && { insight: input.insight },
351
+ ...input.therefore && { therefore: input.therefore },
352
+ ...input.pitfall && { pitfall: input.pitfall }
353
+ };
354
+ },
355
+ decode() {
356
+ return {
357
+ concepts: input.concepts,
358
+ relationship: input.relationship,
359
+ ...input.insight && { insight: input.insight },
360
+ ...input.therefore && { therefore: input.therefore },
361
+ ...input.pitfall && { pitfall: input.pitfall }
362
+ };
363
+ }
364
+ }
365
+ };
366
+ }
367
+ function glossary(entries) {
368
+ return {
369
+ name: "glossary",
370
+ data: Object.entries(entries).map(([term2, expression]) => ({
371
+ term: term2,
372
+ expression
373
+ })),
374
+ codec: {
375
+ encode() {
376
+ return { type: "glossary", entries };
377
+ },
378
+ decode() {
379
+ return Object.entries(entries).map(([term2, expression]) => ({
380
+ term: term2,
381
+ expression
382
+ }));
383
+ }
384
+ }
385
+ };
386
+ }
387
+ function role(content) {
388
+ return {
389
+ name: "role",
390
+ data: content,
391
+ codec: {
392
+ encode() {
393
+ return { type: "role", content };
394
+ },
395
+ decode() {
396
+ return content;
397
+ }
398
+ }
399
+ };
400
+ }
401
+ function principle(input) {
402
+ return {
403
+ name: "principle",
404
+ data: {
405
+ title: input.title,
406
+ description: input.description,
407
+ ...input.policies?.length && { policies: input.policies }
408
+ },
409
+ codec: {
410
+ encode() {
411
+ return {
412
+ type: "principle",
413
+ title: input.title,
414
+ description: input.description,
415
+ ...input.policies?.length && {
416
+ policies: input.policies.map((item) => encodeSerializedValue(item))
417
+ }
418
+ };
419
+ },
420
+ decode() {
421
+ return {
422
+ title: input.title,
423
+ description: input.description,
424
+ ...input.policies?.length && { policies: input.policies }
425
+ };
426
+ }
427
+ }
428
+ };
429
+ }
430
+ function policy(input) {
431
+ return {
432
+ name: "policy",
433
+ data: {
434
+ rule: input.rule,
435
+ ...input.before && { before: input.before },
436
+ ...input.reason && { reason: input.reason },
437
+ ...input.policies?.length && { policies: input.policies }
438
+ },
439
+ codec: {
440
+ encode() {
441
+ return {
442
+ type: "policy",
443
+ rule: input.rule,
444
+ ...input.before && { before: input.before },
445
+ ...input.reason && { reason: input.reason },
446
+ ...input.policies?.length && {
447
+ policies: input.policies.map((item) => encodeSerializedValue(item))
448
+ }
449
+ };
450
+ },
451
+ decode() {
452
+ return {
453
+ rule: input.rule,
454
+ ...input.before && { before: input.before },
455
+ ...input.reason && { reason: input.reason },
456
+ ...input.policies?.length && { policies: input.policies }
457
+ };
458
+ }
459
+ }
460
+ };
461
+ }
462
+
463
+ // packages/context/src/lib/fragments/user.ts
464
+ function identity(input) {
465
+ return {
466
+ name: "identity",
467
+ data: {
468
+ ...input.name && { name: input.name },
469
+ ...input.role && { role: input.role }
470
+ },
471
+ codec: {
472
+ encode() {
473
+ return {
474
+ type: "identity",
475
+ ...input.name && { name: input.name },
476
+ ...input.role && { role: input.role }
477
+ };
478
+ },
479
+ decode() {
480
+ return {
481
+ ...input.name && { name: input.name },
482
+ ...input.role && { role: input.role }
483
+ };
484
+ }
485
+ }
486
+ };
487
+ }
488
+ function persona(input) {
489
+ return {
490
+ name: "persona",
491
+ data: {
492
+ name: input.name,
493
+ ...input.role && { role: input.role },
494
+ ...input.objective && { objective: input.objective },
495
+ ...input.tone && { tone: input.tone }
496
+ },
497
+ codec: {
498
+ encode() {
499
+ return {
500
+ type: "persona",
501
+ name: input.name,
502
+ ...input.role && { role: input.role },
503
+ ...input.objective && { objective: input.objective },
504
+ ...input.tone && { tone: input.tone }
505
+ };
506
+ },
507
+ decode() {
508
+ return {
509
+ name: input.name,
510
+ ...input.role && { role: input.role },
511
+ ...input.objective && { objective: input.objective },
512
+ ...input.tone && { tone: input.tone }
513
+ };
514
+ }
515
+ }
516
+ };
517
+ }
518
+ function alias(term2, meaning) {
519
+ return {
520
+ name: "alias",
521
+ data: { term: term2, meaning },
522
+ codec: {
523
+ encode() {
524
+ return { type: "alias", term: term2, meaning };
525
+ },
526
+ decode() {
527
+ return { term: term2, meaning };
528
+ }
529
+ }
530
+ };
531
+ }
532
+ function preference(aspect, value) {
533
+ return {
534
+ name: "preference",
535
+ data: { aspect, value },
536
+ codec: {
537
+ encode() {
538
+ return { type: "preference", aspect, value };
539
+ },
540
+ decode() {
541
+ return { aspect, value };
542
+ }
543
+ }
544
+ };
545
+ }
546
+ function correction(subject, clarification2) {
547
+ return {
548
+ name: "correction",
549
+ data: { subject, clarification: clarification2 },
550
+ codec: {
551
+ encode() {
552
+ return { type: "correction", subject, clarification: clarification2 };
553
+ },
554
+ decode() {
555
+ return { subject, clarification: clarification2 };
556
+ }
557
+ }
558
+ };
559
+ }
560
+
561
+ // packages/context/src/lib/codec/serialized-fragments.ts
562
+ function isSerializedFragmentLike(value) {
563
+ return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
564
+ }
565
+ function toFragmentData(value, options) {
566
+ if (isSerializedFragmentLike(value)) {
567
+ return toFragment(value, options);
568
+ }
569
+ if (Array.isArray(value)) {
570
+ return value.map((item) => toFragmentData(item, options));
571
+ }
572
+ if (isFragmentObject(value)) {
573
+ return Object.fromEntries(
574
+ Object.entries(value).map(([key, entry]) => [
575
+ key,
576
+ toFragmentData(entry, options)
577
+ ])
578
+ );
579
+ }
580
+ return value;
581
+ }
582
+ var builtInSerializedRegistry = {
583
+ term: {
584
+ toFragment: (input) => term(input.name, input.definition),
585
+ fromFragment: () => void 0
586
+ },
587
+ hint: {
588
+ toFragment: (input) => hint(input.text),
589
+ fromFragment: () => void 0
590
+ },
591
+ guardrail: {
592
+ toFragment: (input) => guardrail({
593
+ rule: input.rule,
594
+ reason: input.reason,
595
+ action: input.action
596
+ }),
597
+ fromFragment: () => void 0
598
+ },
599
+ explain: {
600
+ toFragment: (input) => explain({
601
+ concept: input.concept,
602
+ explanation: input.explanation,
603
+ therefore: input.therefore
604
+ }),
605
+ fromFragment: () => void 0
606
+ },
607
+ example: {
608
+ toFragment: (input) => example({
609
+ question: input.question,
610
+ answer: input.answer,
611
+ note: input.note
612
+ }),
613
+ fromFragment: () => void 0
614
+ },
615
+ clarification: {
616
+ toFragment: (input) => clarification({
617
+ when: input.when,
618
+ ask: input.ask,
619
+ reason: input.reason
620
+ }),
621
+ fromFragment: () => void 0
622
+ },
623
+ workflow: {
624
+ toFragment: (input) => workflow({
625
+ task: input.task,
626
+ steps: input.steps,
627
+ triggers: input.triggers,
628
+ notes: input.notes
629
+ }),
630
+ fromFragment: () => void 0
631
+ },
632
+ quirk: {
633
+ toFragment: (input) => quirk({
634
+ issue: input.issue,
635
+ workaround: input.workaround
636
+ }),
637
+ fromFragment: () => void 0
638
+ },
639
+ styleGuide: {
640
+ toFragment: (input) => styleGuide({
641
+ prefer: input.prefer,
642
+ never: input.never,
643
+ always: input.always
644
+ }),
645
+ fromFragment: () => void 0
646
+ },
647
+ analogy: {
648
+ toFragment: (input) => analogy({
649
+ concepts: input.concepts,
650
+ relationship: input.relationship,
651
+ insight: input.insight,
652
+ therefore: input.therefore,
653
+ pitfall: input.pitfall
654
+ }),
655
+ fromFragment: () => void 0
656
+ },
657
+ glossary: {
658
+ toFragment: (input) => glossary(input.entries),
659
+ fromFragment: () => void 0
660
+ },
661
+ role: {
662
+ toFragment: (input) => role(input.content),
663
+ fromFragment: () => void 0
664
+ },
665
+ principle: {
666
+ toFragment: (input, options) => principle({
667
+ title: input.title,
668
+ description: input.description,
669
+ policies: input.policies?.map((item) => toFragmentData(item, options))
670
+ }),
671
+ fromFragment: () => void 0
672
+ },
673
+ policy: {
674
+ toFragment: (input, options) => policy({
675
+ rule: input.rule,
676
+ before: input.before,
677
+ reason: input.reason,
678
+ policies: input.policies?.map((item) => toFragmentData(item, options))
679
+ }),
680
+ fromFragment: () => void 0
681
+ },
682
+ identity: {
683
+ toFragment: (input) => identity({
684
+ name: input.name,
685
+ role: input.role
686
+ }),
687
+ fromFragment: () => void 0
688
+ },
689
+ persona: {
690
+ toFragment: (input) => persona({
691
+ name: input.name,
692
+ role: input.role,
693
+ objective: input.objective,
694
+ tone: input.tone
695
+ }),
696
+ fromFragment: () => void 0
697
+ },
698
+ alias: {
699
+ toFragment: (input) => alias(input.term, input.meaning),
700
+ fromFragment: () => void 0
701
+ },
702
+ preference: {
703
+ toFragment: (input) => preference(input.aspect, input.value),
704
+ fromFragment: () => void 0
705
+ },
706
+ correction: {
707
+ toFragment: (input) => correction(input.subject, input.clarification),
708
+ fromFragment: () => void 0
709
+ }
710
+ };
711
+ var messageLikeTypes = /* @__PURE__ */ new Set(["user", "assistant", "message"]);
712
+ function findCustomSerializedFragment(fragment2, options) {
713
+ if (!options?.registry) {
714
+ return void 0;
715
+ }
716
+ for (const entry of Object.values(options.registry)) {
717
+ const serialized = entry.fromFragment(fragment2, options);
718
+ if (serialized !== void 0) {
719
+ return serialized;
720
+ }
721
+ }
722
+ return void 0;
723
+ }
724
+ function toFragment(input, options) {
725
+ if (messageLikeTypes.has(input.type)) {
726
+ throw new Error(
727
+ "Message fragments are not supported by serialized fragment conversion"
728
+ );
729
+ }
730
+ const entry = options?.registry?.[input.type] ?? builtInSerializedRegistry[input.type];
731
+ if (!entry) {
732
+ throw new Error(`Unsupported serialized fragment type: ${input.type}`);
733
+ }
734
+ return entry.toFragment(input, options);
735
+ }
736
+ function fromFragment(fragment2, options) {
737
+ if (isMessageFragment(fragment2)) {
738
+ throw new Error(
739
+ "Message fragments are not supported by serialized fragment conversion"
740
+ );
741
+ }
742
+ const customSerialized = findCustomSerializedFragment(fragment2, options);
743
+ if (customSerialized !== void 0) {
744
+ return customSerialized;
745
+ }
746
+ if (fragment2.codec) {
747
+ const encoded = fragment2.codec.encode();
748
+ if (!isSerializedFragmentLike(encoded)) {
749
+ throw new Error(
750
+ `Fragment "${fragment2.name}" codec must encode to a serialized fragment object`
751
+ );
752
+ }
753
+ return encoded;
754
+ }
755
+ if (!builtInSerializedRegistry[fragment2.name]) {
756
+ throw new Error(`Unsupported fragment name: ${fragment2.name}`);
757
+ }
758
+ throw new Error(`Fragment "${fragment2.name}" is missing codec`);
759
+ }
760
+
1
761
  // packages/context/src/lib/estimate.ts
2
762
  import { encode } from "gpt-tokenizer";
3
763
  var defaultTokenizer = {
@@ -158,219 +918,6 @@ async function estimate(modelId, renderer, ...fragments) {
158
918
  };
159
919
  }
160
920
 
161
- // packages/context/src/lib/fragments.ts
162
- import { generateId } from "ai";
163
- function isFragment(data) {
164
- return typeof data === "object" && data !== null && "name" in data && "data" in data && typeof data.name === "string";
165
- }
166
- function isFragmentObject(data) {
167
- return typeof data === "object" && data !== null && !Array.isArray(data) && !isFragment(data);
168
- }
169
- function isMessageFragment(fragment2) {
170
- return fragment2.type === "message";
171
- }
172
- function fragment(name, ...children) {
173
- return {
174
- name,
175
- data: children
176
- };
177
- }
178
- function assistant(message2) {
179
- return {
180
- id: message2.id,
181
- name: "assistant",
182
- data: "content",
183
- type: "message",
184
- persist: true,
185
- codec: {
186
- decode() {
187
- return message2;
188
- },
189
- encode() {
190
- return message2;
191
- }
192
- }
193
- };
194
- }
195
- function message(content) {
196
- const message2 = typeof content === "string" ? {
197
- id: generateId(),
198
- role: "user",
199
- parts: [{ type: "text", text: content }]
200
- } : content;
201
- return {
202
- id: message2.id,
203
- name: message2.role,
204
- data: "content",
205
- type: "message",
206
- persist: true,
207
- codec: {
208
- decode() {
209
- return message2;
210
- },
211
- encode() {
212
- return message2;
213
- }
214
- }
215
- };
216
- }
217
- function assistantText(content, options) {
218
- const id = options?.id ?? crypto.randomUUID();
219
- return assistant({
220
- id,
221
- role: "assistant",
222
- parts: [{ type: "text", text: content }]
223
- });
224
- }
225
- var LAZY_ID = Symbol.for("@deepagents/context:lazy-id");
226
- function isLazyFragment(fragment2) {
227
- return LAZY_ID in fragment2;
228
- }
229
- function lastAssistantMessage(content) {
230
- return {
231
- name: "assistant",
232
- type: "message",
233
- persist: true,
234
- data: "content",
235
- [LAZY_ID]: {
236
- type: "last-assistant",
237
- content
238
- }
239
- };
240
- }
241
-
242
- // packages/context/src/lib/fragments/domain.ts
243
- function term(name, definition) {
244
- return {
245
- name: "term",
246
- data: { name, definition }
247
- };
248
- }
249
- function hint(text) {
250
- return {
251
- name: "hint",
252
- data: text
253
- };
254
- }
255
- function guardrail(input) {
256
- return {
257
- name: "guardrail",
258
- data: {
259
- rule: input.rule,
260
- ...input.reason && { reason: input.reason },
261
- ...input.action && { action: input.action }
262
- }
263
- };
264
- }
265
- function explain(input) {
266
- return {
267
- name: "explain",
268
- data: {
269
- concept: input.concept,
270
- explanation: input.explanation,
271
- ...input.therefore && { therefore: input.therefore }
272
- }
273
- };
274
- }
275
- function example(input) {
276
- return {
277
- name: "example",
278
- data: {
279
- question: input.question,
280
- answer: input.answer,
281
- ...input.note && { note: input.note }
282
- }
283
- };
284
- }
285
- function clarification(input) {
286
- return {
287
- name: "clarification",
288
- data: {
289
- when: input.when,
290
- ask: input.ask,
291
- reason: input.reason
292
- }
293
- };
294
- }
295
- function workflow(input) {
296
- return {
297
- name: "workflow",
298
- data: {
299
- task: input.task,
300
- steps: input.steps,
301
- ...input.triggers?.length && { triggers: input.triggers },
302
- ...input.notes && { notes: input.notes }
303
- }
304
- };
305
- }
306
- function quirk(input) {
307
- return {
308
- name: "quirk",
309
- data: {
310
- issue: input.issue,
311
- workaround: input.workaround
312
- }
313
- };
314
- }
315
- function styleGuide(input) {
316
- return {
317
- name: "styleGuide",
318
- data: {
319
- prefer: input.prefer,
320
- ...input.never && { never: input.never },
321
- ...input.always && { always: input.always }
322
- }
323
- };
324
- }
325
- function analogy(input) {
326
- return {
327
- name: "analogy",
328
- data: {
329
- concepts: input.concepts,
330
- relationship: input.relationship,
331
- ...input.insight && { insight: input.insight },
332
- ...input.therefore && { therefore: input.therefore },
333
- ...input.pitfall && { pitfall: input.pitfall }
334
- }
335
- };
336
- }
337
- function glossary(entries) {
338
- return {
339
- name: "glossary",
340
- data: Object.entries(entries).map(([term2, expression]) => ({
341
- term: term2,
342
- expression
343
- }))
344
- };
345
- }
346
- function role(content) {
347
- return {
348
- name: "role",
349
- data: content
350
- };
351
- }
352
- function principle(input) {
353
- return {
354
- name: "principle",
355
- data: {
356
- title: input.title,
357
- description: input.description,
358
- ...input.policies?.length && { policies: input.policies }
359
- }
360
- };
361
- }
362
- function policy(input) {
363
- return {
364
- name: "policy",
365
- data: {
366
- rule: input.rule,
367
- ...input.before && { before: input.before },
368
- ...input.reason && { reason: input.reason },
369
- ...input.policies?.length && { policies: input.policies }
370
- }
371
- };
372
- }
373
-
374
921
  // packages/context/src/lib/fragments/message/user.ts
375
922
  import { generateId as generateId2, isTextUIPart } from "ai";
376
923
  var SYSTEM_REMINDER_OPEN_TAG = "<system-reminder>";
@@ -544,7 +1091,6 @@ function user(content, ...reminders) {
544
1091
  return {
545
1092
  id: message2.id,
546
1093
  name: "user",
547
- data: "content",
548
1094
  type: "message",
549
1095
  persist: true,
550
1096
  codec: {
@@ -558,52 +1104,6 @@ function user(content, ...reminders) {
558
1104
  };
559
1105
  }
560
1106
 
561
- // packages/context/src/lib/fragments/user.ts
562
- function identity(input) {
563
- return {
564
- name: "identity",
565
- data: {
566
- ...input.name && { name: input.name },
567
- ...input.role && { role: input.role }
568
- }
569
- };
570
- }
571
- function persona(input) {
572
- return {
573
- name: "persona",
574
- data: {
575
- name: input.name,
576
- ...input.role && { role: input.role },
577
- ...input.objective && { objective: input.objective },
578
- ...input.tone && { tone: input.tone }
579
- }
580
- };
581
- }
582
- function alias(term2, meaning) {
583
- return {
584
- name: "alias",
585
- data: { term: term2, meaning }
586
- };
587
- }
588
- function preference(aspect, value) {
589
- return {
590
- name: "preference",
591
- data: { aspect, value }
592
- };
593
- }
594
- function userContext(description) {
595
- return {
596
- name: "userContext",
597
- data: description
598
- };
599
- }
600
- function correction(subject, clarification2) {
601
- return {
602
- name: "correction",
603
- data: { subject, clarification: clarification2 }
604
- };
605
- }
606
-
607
1107
  // packages/context/src/lib/guardrail.ts
608
1108
  function pass(part) {
609
1109
  return { type: "pass", part };
@@ -668,7 +1168,7 @@ var ContextRenderer = class {
668
1168
  return sanitized;
669
1169
  }
670
1170
  sanitizeFragment(fragment2, seen) {
671
- const data = this.sanitizeData(fragment2.data, seen);
1171
+ const data = this.sanitizeData(getFragmentData(fragment2), seen);
672
1172
  if (data == null) {
673
1173
  return null;
674
1174
  }
@@ -745,12 +1245,13 @@ var XmlRenderer = class extends ContextRenderer {
745
1245
  return sanitized.map((f) => this.#renderTopLevel(f)).filter(Boolean).join("\n");
746
1246
  }
747
1247
  #renderTopLevel(fragment2) {
748
- if (this.isPrimitive(fragment2.data)) {
749
- return this.#leafRoot(fragment2.name, String(fragment2.data));
1248
+ const data = getFragmentData(fragment2);
1249
+ if (this.isPrimitive(data)) {
1250
+ return this.#leafRoot(fragment2.name, String(data));
750
1251
  }
751
- if (Array.isArray(fragment2.data)) {
752
- if (fragment2.data.length === 1) {
753
- const single = fragment2.data[0];
1252
+ if (Array.isArray(data)) {
1253
+ if (data.length === 1) {
1254
+ const single = data[0];
754
1255
  if (this.isPrimitive(single)) {
755
1256
  return this.#leafRoot(fragment2.name, String(single));
756
1257
  }
@@ -768,19 +1269,15 @@ var XmlRenderer = class extends ContextRenderer {
768
1269
  );
769
1270
  }
770
1271
  }
771
- return this.#renderArray(fragment2.name, fragment2.data, 0);
1272
+ return this.#renderArray(fragment2.name, data, 0);
772
1273
  }
773
- if (isFragment(fragment2.data)) {
774
- return this.#renderFragmentContentsUnderParent(
775
- fragment2.name,
776
- fragment2.data,
777
- 0
778
- );
1274
+ if (isFragment(data)) {
1275
+ return this.#renderFragmentContentsUnderParent(fragment2.name, data, 0);
779
1276
  }
780
- if (isFragmentObject(fragment2.data)) {
1277
+ if (isFragmentObject(data)) {
781
1278
  return this.#wrap(
782
1279
  fragment2.name,
783
- this.renderEntries(fragment2.data, { depth: 1, path: [] })
1280
+ this.renderEntries(data, { depth: 1, path: [] })
784
1281
  );
785
1282
  }
786
1283
  return "";
@@ -912,23 +1409,23 @@ ${this.#indent(safe, 2)}
912
1409
  return `<${tag}>${safe}</${tag}>`;
913
1410
  }
914
1411
  renderFragment(fragment2, ctx) {
915
- const { name, data } = fragment2;
1412
+ const data = getFragmentData(fragment2);
916
1413
  if (this.isPrimitive(data)) {
917
- return this.#leaf(name, String(data), ctx.depth);
1414
+ return this.#leaf(fragment2.name, String(data), ctx.depth);
918
1415
  }
919
1416
  if (isFragment(data)) {
920
1417
  const child = this.renderFragment(data, { ...ctx, depth: ctx.depth + 1 });
921
- return this.#wrapIndented(name, [child], ctx.depth);
1418
+ return this.#wrapIndented(fragment2.name, [child], ctx.depth);
922
1419
  }
923
1420
  if (Array.isArray(data)) {
924
- return this.#renderArrayIndented(name, data, ctx.depth);
1421
+ return this.#renderArrayIndented(fragment2.name, data, ctx.depth);
925
1422
  }
926
1423
  if (isFragmentObject(data)) {
927
1424
  const children = this.renderEntries(data, {
928
1425
  ...ctx,
929
1426
  depth: ctx.depth + 1
930
1427
  });
931
- return this.#wrapIndented(name, children, ctx.depth);
1428
+ return this.#wrapIndented(fragment2.name, children, ctx.depth);
932
1429
  }
933
1430
  return "";
934
1431
  }
@@ -1045,21 +1542,22 @@ var MarkdownRenderer = class extends ContextRenderer {
1045
1542
  render(fragments) {
1046
1543
  return this.sanitizeFragments(fragments).map((f) => {
1047
1544
  const title = `## ${titlecase(f.name)}`;
1048
- if (this.isPrimitive(f.data)) {
1545
+ const data = getFragmentData(f);
1546
+ if (this.isPrimitive(data)) {
1049
1547
  return `${title}
1050
- ${String(f.data)}`;
1548
+ ${String(data)}`;
1051
1549
  }
1052
- if (Array.isArray(f.data)) {
1550
+ if (Array.isArray(data)) {
1053
1551
  return `${title}
1054
- ${this.#renderArray(f.data, 0)}`;
1552
+ ${this.#renderArray(data, 0)}`;
1055
1553
  }
1056
- if (isFragment(f.data)) {
1554
+ if (isFragment(data)) {
1057
1555
  return `${title}
1058
- ${this.renderFragment(f.data, { depth: 0, path: [] })}`;
1556
+ ${this.renderFragment(data, { depth: 0, path: [] })}`;
1059
1557
  }
1060
- if (isFragmentObject(f.data)) {
1558
+ if (isFragmentObject(data)) {
1061
1559
  return `${title}
1062
- ${this.renderEntries(f.data, { depth: 0, path: [] }).join("\n")}`;
1560
+ ${this.renderEntries(data, { depth: 0, path: [] }).join("\n")}`;
1063
1561
  }
1064
1562
  return `${title}
1065
1563
  `;
@@ -1109,10 +1607,10 @@ ${this.renderEntries(f.data, { depth: 0, path: [] }).join("\n")}`;
1109
1607
  return `${this.#pad(depth)}- ${String(item)}`;
1110
1608
  }
1111
1609
  renderFragment(fragment2, ctx) {
1112
- const { name, data } = fragment2;
1113
- const header = `${this.#pad(ctx.depth)}- **${name}**:`;
1610
+ const data = getFragmentData(fragment2);
1611
+ const header = `${this.#pad(ctx.depth)}- **${fragment2.name}**:`;
1114
1612
  if (this.isPrimitive(data)) {
1115
- return `${this.#pad(ctx.depth)}- **${name}**: ${String(data)}`;
1613
+ return `${this.#pad(ctx.depth)}- **${fragment2.name}**: ${String(data)}`;
1116
1614
  }
1117
1615
  if (isFragment(data)) {
1118
1616
  const child = this.renderFragment(data, { ...ctx, depth: ctx.depth + 1 });
@@ -1151,21 +1649,22 @@ ${this.renderEntries(f.data, { depth: 0, path: [] }).join("\n")}`;
1151
1649
  var TomlRenderer = class extends ContextRenderer {
1152
1650
  render(fragments) {
1153
1651
  const rendered = [];
1154
- for (const f of this.sanitizeFragments(fragments)) {
1155
- if (this.isPrimitive(f.data)) {
1156
- rendered.push(`${f.name} = ${this.#formatValue(f.data)}`);
1157
- } else if (Array.isArray(f.data)) {
1158
- rendered.push(this.#renderTopLevelArray(f.name, f.data));
1159
- } else if (isFragment(f.data)) {
1652
+ for (const it of this.sanitizeFragments(fragments)) {
1653
+ const data = getFragmentData(it);
1654
+ if (this.isPrimitive(data)) {
1655
+ rendered.push(`${it.name} = ${this.#formatValue(data)}`);
1656
+ } else if (Array.isArray(data)) {
1657
+ rendered.push(this.#renderTopLevelArray(it.name, data));
1658
+ } else if (isFragment(data)) {
1160
1659
  rendered.push(
1161
1660
  [
1162
- `[${f.name}]`,
1163
- this.renderFragment(f.data, { depth: 0, path: [f.name] })
1661
+ `[${it.name}]`,
1662
+ this.renderFragment(data, { depth: 0, path: [it.name] })
1164
1663
  ].join("\n")
1165
1664
  );
1166
- } else if (isFragmentObject(f.data)) {
1167
- const entries = this.#renderObjectEntries(f.data, [f.name]);
1168
- rendered.push([`[${f.name}]`, ...entries].join("\n"));
1665
+ } else if (isFragmentObject(data)) {
1666
+ const entries = this.#renderObjectEntries(data, [it.name]);
1667
+ rendered.push([`[${it.name}]`, ...entries].join("\n"));
1169
1668
  }
1170
1669
  }
1171
1670
  return rendered.join("\n\n");
@@ -1235,10 +1734,10 @@ var TomlRenderer = class extends ContextRenderer {
1235
1734
  }).filter(Boolean);
1236
1735
  }
1237
1736
  renderFragment(fragment2, ctx) {
1238
- const { name, data } = fragment2;
1239
- const newPath = [...ctx.path, name];
1737
+ const data = getFragmentData(fragment2);
1738
+ const newPath = [...ctx.path, fragment2.name];
1240
1739
  if (this.isPrimitive(data)) {
1241
- return `${name} = ${this.#formatValue(data)}`;
1740
+ return `${fragment2.name} = ${this.#formatValue(data)}`;
1242
1741
  }
1243
1742
  if (isFragment(data)) {
1244
1743
  return [
@@ -1260,7 +1759,7 @@ var TomlRenderer = class extends ContextRenderer {
1260
1759
  return parts.join("\n");
1261
1760
  }
1262
1761
  const values = nonFragmentItems.map((item) => this.#formatValue(item));
1263
- return `${name} = [${values.join(", ")}]`;
1762
+ return `${fragment2.name} = [${values.join(", ")}]`;
1264
1763
  }
1265
1764
  if (isFragmentObject(data)) {
1266
1765
  const entries = this.#renderObjectEntries(data, newPath);
@@ -1290,27 +1789,27 @@ var ToonRenderer = class extends ContextRenderer {
1290
1789
  return sanitized.map((f) => this.#renderTopLevel(f)).filter(Boolean).join("\n");
1291
1790
  }
1292
1791
  #renderTopLevel(fragment2) {
1293
- const { name, data } = fragment2;
1792
+ const data = getFragmentData(fragment2);
1294
1793
  if (this.isPrimitive(data)) {
1295
- return `${name}: ${this.#formatValue(data)}`;
1794
+ return `${fragment2.name}: ${this.#formatValue(data)}`;
1296
1795
  }
1297
1796
  if (Array.isArray(data)) {
1298
- return this.#renderArrayField(name, data, 0);
1797
+ return this.#renderArrayField(fragment2.name, data, 0);
1299
1798
  }
1300
1799
  if (isFragment(data)) {
1301
1800
  const child = this.renderFragment(data, { depth: 1, path: [] });
1302
- return `${name}:
1801
+ return `${fragment2.name}:
1303
1802
  ${child}`;
1304
1803
  }
1305
1804
  if (isFragmentObject(data)) {
1306
1805
  const entries = this.#renderObjectEntries(data, 1);
1307
1806
  if (!entries) {
1308
- return `${name}:`;
1807
+ return `${fragment2.name}:`;
1309
1808
  }
1310
- return `${name}:
1809
+ return `${fragment2.name}:
1311
1810
  ${entries}`;
1312
1811
  }
1313
- return `${name}:`;
1812
+ return `${fragment2.name}:`;
1314
1813
  }
1315
1814
  #renderArrayField(key, items, depth) {
1316
1815
  const filtered = items.filter((item) => item != null);
@@ -1385,8 +1884,9 @@ ${entries}`;
1385
1884
  depth: depth + 1,
1386
1885
  path: []
1387
1886
  });
1388
- if (this.isPrimitive(item.data)) {
1389
- return `${this.#pad(depth)}- ${item.name}: ${this.#formatValue(item.data)}`;
1887
+ const itemData = getFragmentData(item);
1888
+ if (this.isPrimitive(itemData)) {
1889
+ return `${this.#pad(depth)}- ${item.name}: ${this.#formatValue(itemData)}`;
1390
1890
  }
1391
1891
  return `${this.#pad(depth)}- ${item.name}:
1392
1892
  ${rendered.split("\n").slice(1).join("\n")}`;
@@ -1429,30 +1929,30 @@ ${nested}`);
1429
1929
  return lines.join("\n");
1430
1930
  }
1431
1931
  renderFragment(fragment2, ctx) {
1432
- const { name, data } = fragment2;
1932
+ const data = getFragmentData(fragment2);
1433
1933
  if (this.isPrimitive(data)) {
1434
- return `${this.#pad(ctx.depth)}${name}: ${this.#formatValue(data)}`;
1934
+ return `${this.#pad(ctx.depth)}${fragment2.name}: ${this.#formatValue(data)}`;
1435
1935
  }
1436
1936
  if (isFragment(data)) {
1437
1937
  const child = this.renderFragment(data, {
1438
1938
  ...ctx,
1439
1939
  depth: ctx.depth + 1
1440
1940
  });
1441
- return `${this.#pad(ctx.depth)}${name}:
1941
+ return `${this.#pad(ctx.depth)}${fragment2.name}:
1442
1942
  ${child}`;
1443
1943
  }
1444
1944
  if (Array.isArray(data)) {
1445
- return this.#renderArrayField(name, data, ctx.depth);
1945
+ return this.#renderArrayField(fragment2.name, data, ctx.depth);
1446
1946
  }
1447
1947
  if (isFragmentObject(data)) {
1448
1948
  const entries = this.#renderObjectEntries(data, ctx.depth + 1);
1449
1949
  if (!entries) {
1450
- return `${this.#pad(ctx.depth)}${name}:`;
1950
+ return `${this.#pad(ctx.depth)}${fragment2.name}:`;
1451
1951
  }
1452
- return `${this.#pad(ctx.depth)}${name}:
1952
+ return `${this.#pad(ctx.depth)}${fragment2.name}:
1453
1953
  ${entries}`;
1454
1954
  }
1455
- return `${this.#pad(ctx.depth)}${name}:`;
1955
+ return `${this.#pad(ctx.depth)}${fragment2.name}:`;
1456
1956
  }
1457
1957
  renderPrimitive(key, value, ctx) {
1458
1958
  return `${this.#pad(ctx.depth)}${key}: ${this.#formatValue(value)}`;
@@ -1678,6 +2178,8 @@ export {
1678
2178
  explain,
1679
2179
  fail,
1680
2180
  fragment,
2181
+ fromFragment,
2182
+ getFragmentData,
1681
2183
  getModelsRegistry,
1682
2184
  getReminderRanges,
1683
2185
  glossary,
@@ -1707,8 +2209,8 @@ export {
1707
2209
  stripTextByRanges,
1708
2210
  styleGuide,
1709
2211
  term,
2212
+ toFragment,
1710
2213
  user,
1711
- userContext,
1712
2214
  visualizeGraph,
1713
2215
  workflow
1714
2216
  };