@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/README.md +9 -16
- package/dist/browser.d.ts +2 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +825 -323
- package/dist/browser.js.map +4 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +821 -311
- package/dist/index.js.map +4 -4
- package/dist/lib/codec/codec.d.ts.map +1 -0
- package/dist/lib/codec/serialized-codec.d.ts +3 -0
- package/dist/lib/codec/serialized-codec.d.ts.map +1 -0
- package/dist/lib/codec/serialized-fragments.d.ts +114 -0
- package/dist/lib/codec/serialized-fragments.d.ts.map +1 -0
- package/dist/lib/engine.d.ts +2 -2
- package/dist/lib/engine.d.ts.map +1 -1
- package/dist/lib/fragments/domain.d.ts.map +1 -1
- package/dist/lib/fragments/message/user.d.ts.map +1 -1
- package/dist/lib/fragments/user.d.ts +1 -16
- package/dist/lib/fragments/user.d.ts.map +1 -1
- package/dist/lib/fragments.d.ts +3 -2
- package/dist/lib/fragments.d.ts.map +1 -1
- package/dist/lib/renderers/abstract.renderer.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/lib/codec.d.ts.map +0 -1
- /package/dist/lib/{codec.d.ts → codec/codec.d.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -15,7 +15,7 @@ import { createRepairToolCall } from "@deepagents/agent";
|
|
|
15
15
|
// packages/context/src/lib/fragments.ts
|
|
16
16
|
import { generateId } from "ai";
|
|
17
17
|
function isFragment(data) {
|
|
18
|
-
return typeof data === "object" && data !== null && "name" in data && "data" in data && typeof data.name === "string";
|
|
18
|
+
return typeof data === "object" && data !== null && "name" in data && ("data" in data || "codec" in data) && typeof data.name === "string";
|
|
19
19
|
}
|
|
20
20
|
function isFragmentObject(data) {
|
|
21
21
|
return typeof data === "object" && data !== null && !Array.isArray(data) && !isFragment(data);
|
|
@@ -23,6 +23,15 @@ function isFragmentObject(data) {
|
|
|
23
23
|
function isMessageFragment(fragment2) {
|
|
24
24
|
return fragment2.type === "message";
|
|
25
25
|
}
|
|
26
|
+
function getFragmentData(fragment2) {
|
|
27
|
+
if (fragment2.codec) {
|
|
28
|
+
return fragment2.codec.decode();
|
|
29
|
+
}
|
|
30
|
+
if ("data" in fragment2) {
|
|
31
|
+
return fragment2.data;
|
|
32
|
+
}
|
|
33
|
+
throw new Error(`Fragment "${fragment2.name}" is missing data and codec`);
|
|
34
|
+
}
|
|
26
35
|
function fragment(name, ...children) {
|
|
27
36
|
return {
|
|
28
37
|
name,
|
|
@@ -33,7 +42,6 @@ function assistant(message2) {
|
|
|
33
42
|
return {
|
|
34
43
|
id: message2.id,
|
|
35
44
|
name: "assistant",
|
|
36
|
-
data: "content",
|
|
37
45
|
type: "message",
|
|
38
46
|
persist: true,
|
|
39
47
|
codec: {
|
|
@@ -55,7 +63,6 @@ function message(content) {
|
|
|
55
63
|
return {
|
|
56
64
|
id: message2.id,
|
|
57
65
|
name: message2.role,
|
|
58
|
-
data: "content",
|
|
59
66
|
type: "message",
|
|
60
67
|
persist: true,
|
|
61
68
|
codec: {
|
|
@@ -420,7 +427,7 @@ function toMessageFragment(item) {
|
|
|
420
427
|
}
|
|
421
428
|
function chatMessageToUIMessage(item) {
|
|
422
429
|
if (isFragment(item) && isMessageFragment(item)) {
|
|
423
|
-
return item.codec.
|
|
430
|
+
return item.codec.encode();
|
|
424
431
|
}
|
|
425
432
|
return item;
|
|
426
433
|
}
|
|
@@ -447,73 +454,746 @@ async function chat(agent2, messages, options) {
|
|
|
447
454
|
await context.save();
|
|
448
455
|
assistantMsgId = options?.generateMessageId?.() ?? generateId3();
|
|
449
456
|
}
|
|
450
|
-
const uiMessages = messages.map(chatMessageToUIMessage);
|
|
451
|
-
const streamContextVariables = options?.contextVariables === void 0 ? {} : options.contextVariables;
|
|
452
|
-
const result = await agent2.stream(streamContextVariables, {
|
|
453
|
-
transform: options?.transform
|
|
454
|
-
});
|
|
455
|
-
const uiStream = result.toUIMessageStream({
|
|
456
|
-
onError: options?.onError ?? formatChatError,
|
|
457
|
-
sendStart: true,
|
|
458
|
-
sendFinish: true,
|
|
459
|
-
sendReasoning: true,
|
|
460
|
-
sendSources: true,
|
|
461
|
-
originalMessages: uiMessages,
|
|
462
|
-
generateMessageId: () => assistantMsgId,
|
|
463
|
-
messageMetadata: options?.messageMetadata
|
|
464
|
-
});
|
|
465
|
-
return createUIMessageStream2({
|
|
466
|
-
originalMessages: uiMessages,
|
|
467
|
-
generateId: () => assistantMsgId,
|
|
468
|
-
onStepFinish: async ({ responseMessage }) => {
|
|
469
|
-
const normalizedMessage = {
|
|
470
|
-
...responseMessage,
|
|
471
|
-
id: assistantMsgId
|
|
472
|
-
};
|
|
473
|
-
context.set(assistant(normalizedMessage));
|
|
474
|
-
await context.save({ branch: false });
|
|
475
|
-
},
|
|
476
|
-
onFinish: async ({ responseMessage }) => {
|
|
477
|
-
const normalizedMessage = {
|
|
478
|
-
...responseMessage,
|
|
479
|
-
id: assistantMsgId
|
|
480
|
-
};
|
|
481
|
-
const finalMetadata = await options?.finalAssistantMetadata?.(normalizedMessage);
|
|
482
|
-
const finalMessage = finalMetadata === void 0 ? normalizedMessage : {
|
|
483
|
-
...normalizedMessage,
|
|
484
|
-
metadata: {
|
|
485
|
-
...normalizedMessage.metadata ?? {},
|
|
486
|
-
...finalMetadata
|
|
487
|
-
}
|
|
488
|
-
};
|
|
489
|
-
context.set(assistant(finalMessage));
|
|
490
|
-
await context.save({ branch: false });
|
|
491
|
-
const totalUsage = await result.totalUsage;
|
|
492
|
-
await context.trackUsage(totalUsage);
|
|
493
|
-
},
|
|
494
|
-
execute: async ({ writer }) => {
|
|
495
|
-
writer.merge(uiStream);
|
|
457
|
+
const uiMessages = messages.map(chatMessageToUIMessage);
|
|
458
|
+
const streamContextVariables = options?.contextVariables === void 0 ? {} : options.contextVariables;
|
|
459
|
+
const result = await agent2.stream(streamContextVariables, {
|
|
460
|
+
transform: options?.transform
|
|
461
|
+
});
|
|
462
|
+
const uiStream = result.toUIMessageStream({
|
|
463
|
+
onError: options?.onError ?? formatChatError,
|
|
464
|
+
sendStart: true,
|
|
465
|
+
sendFinish: true,
|
|
466
|
+
sendReasoning: true,
|
|
467
|
+
sendSources: true,
|
|
468
|
+
originalMessages: uiMessages,
|
|
469
|
+
generateMessageId: () => assistantMsgId,
|
|
470
|
+
messageMetadata: options?.messageMetadata
|
|
471
|
+
});
|
|
472
|
+
return createUIMessageStream2({
|
|
473
|
+
originalMessages: uiMessages,
|
|
474
|
+
generateId: () => assistantMsgId,
|
|
475
|
+
onStepFinish: async ({ responseMessage }) => {
|
|
476
|
+
const normalizedMessage = {
|
|
477
|
+
...responseMessage,
|
|
478
|
+
id: assistantMsgId
|
|
479
|
+
};
|
|
480
|
+
context.set(assistant(normalizedMessage));
|
|
481
|
+
await context.save({ branch: false });
|
|
482
|
+
},
|
|
483
|
+
onFinish: async ({ responseMessage }) => {
|
|
484
|
+
const normalizedMessage = {
|
|
485
|
+
...responseMessage,
|
|
486
|
+
id: assistantMsgId
|
|
487
|
+
};
|
|
488
|
+
const finalMetadata = await options?.finalAssistantMetadata?.(normalizedMessage);
|
|
489
|
+
const finalMessage = finalMetadata === void 0 ? normalizedMessage : {
|
|
490
|
+
...normalizedMessage,
|
|
491
|
+
metadata: {
|
|
492
|
+
...normalizedMessage.metadata ?? {},
|
|
493
|
+
...finalMetadata
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
context.set(assistant(finalMessage));
|
|
497
|
+
await context.save({ branch: false });
|
|
498
|
+
const totalUsage = await result.totalUsage;
|
|
499
|
+
await context.trackUsage(totalUsage);
|
|
500
|
+
},
|
|
501
|
+
execute: async ({ writer }) => {
|
|
502
|
+
writer.merge(uiStream);
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
function formatChatError(error) {
|
|
507
|
+
if (NoSuchToolError.isInstance(error)) {
|
|
508
|
+
return "The model tried to call an unknown tool.";
|
|
509
|
+
}
|
|
510
|
+
if (InvalidToolInputError.isInstance(error)) {
|
|
511
|
+
return "The model called a tool with invalid arguments.";
|
|
512
|
+
}
|
|
513
|
+
if (ToolCallRepairError.isInstance(error)) {
|
|
514
|
+
return "The model tried to call a tool with invalid arguments, but it was repaired.";
|
|
515
|
+
}
|
|
516
|
+
if (APICallError.isInstance(error)) {
|
|
517
|
+
console.error("Upstream API call failed:", error);
|
|
518
|
+
return `Upstream API call failed with status ${error.statusCode}: ${error.message}`;
|
|
519
|
+
}
|
|
520
|
+
return JSON.stringify(error);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// packages/context/src/lib/codec/serialized-codec.ts
|
|
524
|
+
function encodeSerializedValue(value) {
|
|
525
|
+
if (isFragment(value)) {
|
|
526
|
+
if (isMessageFragment(value)) {
|
|
527
|
+
throw new Error(
|
|
528
|
+
"Message fragments are not supported by serialized fragment conversion"
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
if (!value.codec) {
|
|
532
|
+
throw new Error(`Fragment "${value.name}" is missing codec`);
|
|
533
|
+
}
|
|
534
|
+
return value.codec.encode();
|
|
535
|
+
}
|
|
536
|
+
if (Array.isArray(value)) {
|
|
537
|
+
return value.map((item) => encodeSerializedValue(item));
|
|
538
|
+
}
|
|
539
|
+
if (isFragmentObject(value)) {
|
|
540
|
+
return Object.fromEntries(
|
|
541
|
+
Object.entries(value).map(([key, entry]) => [
|
|
542
|
+
key,
|
|
543
|
+
encodeSerializedValue(entry)
|
|
544
|
+
])
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
return value;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// packages/context/src/lib/fragments/domain.ts
|
|
551
|
+
function term(name, definition) {
|
|
552
|
+
return {
|
|
553
|
+
name: "term",
|
|
554
|
+
data: { name, definition },
|
|
555
|
+
codec: {
|
|
556
|
+
encode() {
|
|
557
|
+
return { type: "term", name, definition };
|
|
558
|
+
},
|
|
559
|
+
decode() {
|
|
560
|
+
return { name, definition };
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
function hint(text) {
|
|
566
|
+
return {
|
|
567
|
+
name: "hint",
|
|
568
|
+
data: text,
|
|
569
|
+
codec: {
|
|
570
|
+
encode() {
|
|
571
|
+
return { type: "hint", text };
|
|
572
|
+
},
|
|
573
|
+
decode() {
|
|
574
|
+
return text;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
function guardrail(input) {
|
|
580
|
+
return {
|
|
581
|
+
name: "guardrail",
|
|
582
|
+
data: {
|
|
583
|
+
rule: input.rule,
|
|
584
|
+
...input.reason && { reason: input.reason },
|
|
585
|
+
...input.action && { action: input.action }
|
|
586
|
+
},
|
|
587
|
+
codec: {
|
|
588
|
+
encode() {
|
|
589
|
+
return {
|
|
590
|
+
type: "guardrail",
|
|
591
|
+
rule: input.rule,
|
|
592
|
+
...input.reason && { reason: input.reason },
|
|
593
|
+
...input.action && { action: input.action }
|
|
594
|
+
};
|
|
595
|
+
},
|
|
596
|
+
decode() {
|
|
597
|
+
return {
|
|
598
|
+
rule: input.rule,
|
|
599
|
+
...input.reason && { reason: input.reason },
|
|
600
|
+
...input.action && { action: input.action }
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
function explain(input) {
|
|
607
|
+
return {
|
|
608
|
+
name: "explain",
|
|
609
|
+
data: {
|
|
610
|
+
concept: input.concept,
|
|
611
|
+
explanation: input.explanation,
|
|
612
|
+
...input.therefore && { therefore: input.therefore }
|
|
613
|
+
},
|
|
614
|
+
codec: {
|
|
615
|
+
encode() {
|
|
616
|
+
return {
|
|
617
|
+
type: "explain",
|
|
618
|
+
concept: input.concept,
|
|
619
|
+
explanation: input.explanation,
|
|
620
|
+
...input.therefore && { therefore: input.therefore }
|
|
621
|
+
};
|
|
622
|
+
},
|
|
623
|
+
decode() {
|
|
624
|
+
return {
|
|
625
|
+
concept: input.concept,
|
|
626
|
+
explanation: input.explanation,
|
|
627
|
+
...input.therefore && { therefore: input.therefore }
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
function example(input) {
|
|
634
|
+
return {
|
|
635
|
+
name: "example",
|
|
636
|
+
data: {
|
|
637
|
+
question: input.question,
|
|
638
|
+
answer: input.answer,
|
|
639
|
+
...input.note && { note: input.note }
|
|
640
|
+
},
|
|
641
|
+
codec: {
|
|
642
|
+
encode() {
|
|
643
|
+
return {
|
|
644
|
+
type: "example",
|
|
645
|
+
question: input.question,
|
|
646
|
+
answer: input.answer,
|
|
647
|
+
...input.note && { note: input.note }
|
|
648
|
+
};
|
|
649
|
+
},
|
|
650
|
+
decode() {
|
|
651
|
+
return {
|
|
652
|
+
question: input.question,
|
|
653
|
+
answer: input.answer,
|
|
654
|
+
...input.note && { note: input.note }
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
function clarification(input) {
|
|
661
|
+
return {
|
|
662
|
+
name: "clarification",
|
|
663
|
+
data: {
|
|
664
|
+
when: input.when,
|
|
665
|
+
ask: input.ask,
|
|
666
|
+
reason: input.reason
|
|
667
|
+
},
|
|
668
|
+
codec: {
|
|
669
|
+
encode() {
|
|
670
|
+
return {
|
|
671
|
+
type: "clarification",
|
|
672
|
+
when: input.when,
|
|
673
|
+
ask: input.ask,
|
|
674
|
+
reason: input.reason
|
|
675
|
+
};
|
|
676
|
+
},
|
|
677
|
+
decode() {
|
|
678
|
+
return {
|
|
679
|
+
when: input.when,
|
|
680
|
+
ask: input.ask,
|
|
681
|
+
reason: input.reason
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
function workflow(input) {
|
|
688
|
+
return {
|
|
689
|
+
name: "workflow",
|
|
690
|
+
data: {
|
|
691
|
+
task: input.task,
|
|
692
|
+
steps: input.steps,
|
|
693
|
+
...input.triggers?.length && { triggers: input.triggers },
|
|
694
|
+
...input.notes && { notes: input.notes }
|
|
695
|
+
},
|
|
696
|
+
codec: {
|
|
697
|
+
encode() {
|
|
698
|
+
return {
|
|
699
|
+
type: "workflow",
|
|
700
|
+
task: input.task,
|
|
701
|
+
steps: input.steps,
|
|
702
|
+
...input.triggers?.length && { triggers: input.triggers },
|
|
703
|
+
...input.notes && { notes: input.notes }
|
|
704
|
+
};
|
|
705
|
+
},
|
|
706
|
+
decode() {
|
|
707
|
+
return {
|
|
708
|
+
task: input.task,
|
|
709
|
+
steps: input.steps,
|
|
710
|
+
...input.triggers?.length && { triggers: input.triggers },
|
|
711
|
+
...input.notes && { notes: input.notes }
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
function quirk(input) {
|
|
718
|
+
return {
|
|
719
|
+
name: "quirk",
|
|
720
|
+
data: {
|
|
721
|
+
issue: input.issue,
|
|
722
|
+
workaround: input.workaround
|
|
723
|
+
},
|
|
724
|
+
codec: {
|
|
725
|
+
encode() {
|
|
726
|
+
return {
|
|
727
|
+
type: "quirk",
|
|
728
|
+
issue: input.issue,
|
|
729
|
+
workaround: input.workaround
|
|
730
|
+
};
|
|
731
|
+
},
|
|
732
|
+
decode() {
|
|
733
|
+
return {
|
|
734
|
+
issue: input.issue,
|
|
735
|
+
workaround: input.workaround
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
function styleGuide(input) {
|
|
742
|
+
return {
|
|
743
|
+
name: "styleGuide",
|
|
744
|
+
data: {
|
|
745
|
+
prefer: input.prefer,
|
|
746
|
+
...input.never && { never: input.never },
|
|
747
|
+
...input.always && { always: input.always }
|
|
748
|
+
},
|
|
749
|
+
codec: {
|
|
750
|
+
encode() {
|
|
751
|
+
return {
|
|
752
|
+
type: "styleGuide",
|
|
753
|
+
prefer: input.prefer,
|
|
754
|
+
...input.never && { never: input.never },
|
|
755
|
+
...input.always && { always: input.always }
|
|
756
|
+
};
|
|
757
|
+
},
|
|
758
|
+
decode() {
|
|
759
|
+
return {
|
|
760
|
+
prefer: input.prefer,
|
|
761
|
+
...input.never && { never: input.never },
|
|
762
|
+
...input.always && { always: input.always }
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
function analogy(input) {
|
|
769
|
+
return {
|
|
770
|
+
name: "analogy",
|
|
771
|
+
data: {
|
|
772
|
+
concepts: input.concepts,
|
|
773
|
+
relationship: input.relationship,
|
|
774
|
+
...input.insight && { insight: input.insight },
|
|
775
|
+
...input.therefore && { therefore: input.therefore },
|
|
776
|
+
...input.pitfall && { pitfall: input.pitfall }
|
|
777
|
+
},
|
|
778
|
+
codec: {
|
|
779
|
+
encode() {
|
|
780
|
+
return {
|
|
781
|
+
type: "analogy",
|
|
782
|
+
concepts: input.concepts,
|
|
783
|
+
relationship: input.relationship,
|
|
784
|
+
...input.insight && { insight: input.insight },
|
|
785
|
+
...input.therefore && { therefore: input.therefore },
|
|
786
|
+
...input.pitfall && { pitfall: input.pitfall }
|
|
787
|
+
};
|
|
788
|
+
},
|
|
789
|
+
decode() {
|
|
790
|
+
return {
|
|
791
|
+
concepts: input.concepts,
|
|
792
|
+
relationship: input.relationship,
|
|
793
|
+
...input.insight && { insight: input.insight },
|
|
794
|
+
...input.therefore && { therefore: input.therefore },
|
|
795
|
+
...input.pitfall && { pitfall: input.pitfall }
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
function glossary(entries) {
|
|
802
|
+
return {
|
|
803
|
+
name: "glossary",
|
|
804
|
+
data: Object.entries(entries).map(([term2, expression]) => ({
|
|
805
|
+
term: term2,
|
|
806
|
+
expression
|
|
807
|
+
})),
|
|
808
|
+
codec: {
|
|
809
|
+
encode() {
|
|
810
|
+
return { type: "glossary", entries };
|
|
811
|
+
},
|
|
812
|
+
decode() {
|
|
813
|
+
return Object.entries(entries).map(([term2, expression]) => ({
|
|
814
|
+
term: term2,
|
|
815
|
+
expression
|
|
816
|
+
}));
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
function role(content) {
|
|
822
|
+
return {
|
|
823
|
+
name: "role",
|
|
824
|
+
data: content,
|
|
825
|
+
codec: {
|
|
826
|
+
encode() {
|
|
827
|
+
return { type: "role", content };
|
|
828
|
+
},
|
|
829
|
+
decode() {
|
|
830
|
+
return content;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
function principle(input) {
|
|
836
|
+
return {
|
|
837
|
+
name: "principle",
|
|
838
|
+
data: {
|
|
839
|
+
title: input.title,
|
|
840
|
+
description: input.description,
|
|
841
|
+
...input.policies?.length && { policies: input.policies }
|
|
842
|
+
},
|
|
843
|
+
codec: {
|
|
844
|
+
encode() {
|
|
845
|
+
return {
|
|
846
|
+
type: "principle",
|
|
847
|
+
title: input.title,
|
|
848
|
+
description: input.description,
|
|
849
|
+
...input.policies?.length && {
|
|
850
|
+
policies: input.policies.map((item) => encodeSerializedValue(item))
|
|
851
|
+
}
|
|
852
|
+
};
|
|
853
|
+
},
|
|
854
|
+
decode() {
|
|
855
|
+
return {
|
|
856
|
+
title: input.title,
|
|
857
|
+
description: input.description,
|
|
858
|
+
...input.policies?.length && { policies: input.policies }
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
function policy(input) {
|
|
865
|
+
return {
|
|
866
|
+
name: "policy",
|
|
867
|
+
data: {
|
|
868
|
+
rule: input.rule,
|
|
869
|
+
...input.before && { before: input.before },
|
|
870
|
+
...input.reason && { reason: input.reason },
|
|
871
|
+
...input.policies?.length && { policies: input.policies }
|
|
872
|
+
},
|
|
873
|
+
codec: {
|
|
874
|
+
encode() {
|
|
875
|
+
return {
|
|
876
|
+
type: "policy",
|
|
877
|
+
rule: input.rule,
|
|
878
|
+
...input.before && { before: input.before },
|
|
879
|
+
...input.reason && { reason: input.reason },
|
|
880
|
+
...input.policies?.length && {
|
|
881
|
+
policies: input.policies.map((item) => encodeSerializedValue(item))
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
},
|
|
885
|
+
decode() {
|
|
886
|
+
return {
|
|
887
|
+
rule: input.rule,
|
|
888
|
+
...input.before && { before: input.before },
|
|
889
|
+
...input.reason && { reason: input.reason },
|
|
890
|
+
...input.policies?.length && { policies: input.policies }
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
// packages/context/src/lib/fragments/user.ts
|
|
898
|
+
function identity(input) {
|
|
899
|
+
return {
|
|
900
|
+
name: "identity",
|
|
901
|
+
data: {
|
|
902
|
+
...input.name && { name: input.name },
|
|
903
|
+
...input.role && { role: input.role }
|
|
904
|
+
},
|
|
905
|
+
codec: {
|
|
906
|
+
encode() {
|
|
907
|
+
return {
|
|
908
|
+
type: "identity",
|
|
909
|
+
...input.name && { name: input.name },
|
|
910
|
+
...input.role && { role: input.role }
|
|
911
|
+
};
|
|
912
|
+
},
|
|
913
|
+
decode() {
|
|
914
|
+
return {
|
|
915
|
+
...input.name && { name: input.name },
|
|
916
|
+
...input.role && { role: input.role }
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
function persona(input) {
|
|
923
|
+
return {
|
|
924
|
+
name: "persona",
|
|
925
|
+
data: {
|
|
926
|
+
name: input.name,
|
|
927
|
+
...input.role && { role: input.role },
|
|
928
|
+
...input.objective && { objective: input.objective },
|
|
929
|
+
...input.tone && { tone: input.tone }
|
|
930
|
+
},
|
|
931
|
+
codec: {
|
|
932
|
+
encode() {
|
|
933
|
+
return {
|
|
934
|
+
type: "persona",
|
|
935
|
+
name: input.name,
|
|
936
|
+
...input.role && { role: input.role },
|
|
937
|
+
...input.objective && { objective: input.objective },
|
|
938
|
+
...input.tone && { tone: input.tone }
|
|
939
|
+
};
|
|
940
|
+
},
|
|
941
|
+
decode() {
|
|
942
|
+
return {
|
|
943
|
+
name: input.name,
|
|
944
|
+
...input.role && { role: input.role },
|
|
945
|
+
...input.objective && { objective: input.objective },
|
|
946
|
+
...input.tone && { tone: input.tone }
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
function alias(term2, meaning) {
|
|
953
|
+
return {
|
|
954
|
+
name: "alias",
|
|
955
|
+
data: { term: term2, meaning },
|
|
956
|
+
codec: {
|
|
957
|
+
encode() {
|
|
958
|
+
return { type: "alias", term: term2, meaning };
|
|
959
|
+
},
|
|
960
|
+
decode() {
|
|
961
|
+
return { term: term2, meaning };
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
function preference(aspect, value) {
|
|
967
|
+
return {
|
|
968
|
+
name: "preference",
|
|
969
|
+
data: { aspect, value },
|
|
970
|
+
codec: {
|
|
971
|
+
encode() {
|
|
972
|
+
return { type: "preference", aspect, value };
|
|
973
|
+
},
|
|
974
|
+
decode() {
|
|
975
|
+
return { aspect, value };
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
function correction(subject, clarification2) {
|
|
981
|
+
return {
|
|
982
|
+
name: "correction",
|
|
983
|
+
data: { subject, clarification: clarification2 },
|
|
984
|
+
codec: {
|
|
985
|
+
encode() {
|
|
986
|
+
return { type: "correction", subject, clarification: clarification2 };
|
|
987
|
+
},
|
|
988
|
+
decode() {
|
|
989
|
+
return { subject, clarification: clarification2 };
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
// packages/context/src/lib/codec/serialized-fragments.ts
|
|
996
|
+
function isSerializedFragmentLike(value) {
|
|
997
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
998
|
+
}
|
|
999
|
+
function toFragmentData(value, options) {
|
|
1000
|
+
if (isSerializedFragmentLike(value)) {
|
|
1001
|
+
return toFragment(value, options);
|
|
1002
|
+
}
|
|
1003
|
+
if (Array.isArray(value)) {
|
|
1004
|
+
return value.map((item) => toFragmentData(item, options));
|
|
1005
|
+
}
|
|
1006
|
+
if (isFragmentObject(value)) {
|
|
1007
|
+
return Object.fromEntries(
|
|
1008
|
+
Object.entries(value).map(([key, entry]) => [
|
|
1009
|
+
key,
|
|
1010
|
+
toFragmentData(entry, options)
|
|
1011
|
+
])
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
return value;
|
|
1015
|
+
}
|
|
1016
|
+
var builtInSerializedRegistry = {
|
|
1017
|
+
term: {
|
|
1018
|
+
toFragment: (input) => term(input.name, input.definition),
|
|
1019
|
+
fromFragment: () => void 0
|
|
1020
|
+
},
|
|
1021
|
+
hint: {
|
|
1022
|
+
toFragment: (input) => hint(input.text),
|
|
1023
|
+
fromFragment: () => void 0
|
|
1024
|
+
},
|
|
1025
|
+
guardrail: {
|
|
1026
|
+
toFragment: (input) => guardrail({
|
|
1027
|
+
rule: input.rule,
|
|
1028
|
+
reason: input.reason,
|
|
1029
|
+
action: input.action
|
|
1030
|
+
}),
|
|
1031
|
+
fromFragment: () => void 0
|
|
1032
|
+
},
|
|
1033
|
+
explain: {
|
|
1034
|
+
toFragment: (input) => explain({
|
|
1035
|
+
concept: input.concept,
|
|
1036
|
+
explanation: input.explanation,
|
|
1037
|
+
therefore: input.therefore
|
|
1038
|
+
}),
|
|
1039
|
+
fromFragment: () => void 0
|
|
1040
|
+
},
|
|
1041
|
+
example: {
|
|
1042
|
+
toFragment: (input) => example({
|
|
1043
|
+
question: input.question,
|
|
1044
|
+
answer: input.answer,
|
|
1045
|
+
note: input.note
|
|
1046
|
+
}),
|
|
1047
|
+
fromFragment: () => void 0
|
|
1048
|
+
},
|
|
1049
|
+
clarification: {
|
|
1050
|
+
toFragment: (input) => clarification({
|
|
1051
|
+
when: input.when,
|
|
1052
|
+
ask: input.ask,
|
|
1053
|
+
reason: input.reason
|
|
1054
|
+
}),
|
|
1055
|
+
fromFragment: () => void 0
|
|
1056
|
+
},
|
|
1057
|
+
workflow: {
|
|
1058
|
+
toFragment: (input) => workflow({
|
|
1059
|
+
task: input.task,
|
|
1060
|
+
steps: input.steps,
|
|
1061
|
+
triggers: input.triggers,
|
|
1062
|
+
notes: input.notes
|
|
1063
|
+
}),
|
|
1064
|
+
fromFragment: () => void 0
|
|
1065
|
+
},
|
|
1066
|
+
quirk: {
|
|
1067
|
+
toFragment: (input) => quirk({
|
|
1068
|
+
issue: input.issue,
|
|
1069
|
+
workaround: input.workaround
|
|
1070
|
+
}),
|
|
1071
|
+
fromFragment: () => void 0
|
|
1072
|
+
},
|
|
1073
|
+
styleGuide: {
|
|
1074
|
+
toFragment: (input) => styleGuide({
|
|
1075
|
+
prefer: input.prefer,
|
|
1076
|
+
never: input.never,
|
|
1077
|
+
always: input.always
|
|
1078
|
+
}),
|
|
1079
|
+
fromFragment: () => void 0
|
|
1080
|
+
},
|
|
1081
|
+
analogy: {
|
|
1082
|
+
toFragment: (input) => analogy({
|
|
1083
|
+
concepts: input.concepts,
|
|
1084
|
+
relationship: input.relationship,
|
|
1085
|
+
insight: input.insight,
|
|
1086
|
+
therefore: input.therefore,
|
|
1087
|
+
pitfall: input.pitfall
|
|
1088
|
+
}),
|
|
1089
|
+
fromFragment: () => void 0
|
|
1090
|
+
},
|
|
1091
|
+
glossary: {
|
|
1092
|
+
toFragment: (input) => glossary(input.entries),
|
|
1093
|
+
fromFragment: () => void 0
|
|
1094
|
+
},
|
|
1095
|
+
role: {
|
|
1096
|
+
toFragment: (input) => role(input.content),
|
|
1097
|
+
fromFragment: () => void 0
|
|
1098
|
+
},
|
|
1099
|
+
principle: {
|
|
1100
|
+
toFragment: (input, options) => principle({
|
|
1101
|
+
title: input.title,
|
|
1102
|
+
description: input.description,
|
|
1103
|
+
policies: input.policies?.map((item) => toFragmentData(item, options))
|
|
1104
|
+
}),
|
|
1105
|
+
fromFragment: () => void 0
|
|
1106
|
+
},
|
|
1107
|
+
policy: {
|
|
1108
|
+
toFragment: (input, options) => policy({
|
|
1109
|
+
rule: input.rule,
|
|
1110
|
+
before: input.before,
|
|
1111
|
+
reason: input.reason,
|
|
1112
|
+
policies: input.policies?.map((item) => toFragmentData(item, options))
|
|
1113
|
+
}),
|
|
1114
|
+
fromFragment: () => void 0
|
|
1115
|
+
},
|
|
1116
|
+
identity: {
|
|
1117
|
+
toFragment: (input) => identity({
|
|
1118
|
+
name: input.name,
|
|
1119
|
+
role: input.role
|
|
1120
|
+
}),
|
|
1121
|
+
fromFragment: () => void 0
|
|
1122
|
+
},
|
|
1123
|
+
persona: {
|
|
1124
|
+
toFragment: (input) => persona({
|
|
1125
|
+
name: input.name,
|
|
1126
|
+
role: input.role,
|
|
1127
|
+
objective: input.objective,
|
|
1128
|
+
tone: input.tone
|
|
1129
|
+
}),
|
|
1130
|
+
fromFragment: () => void 0
|
|
1131
|
+
},
|
|
1132
|
+
alias: {
|
|
1133
|
+
toFragment: (input) => alias(input.term, input.meaning),
|
|
1134
|
+
fromFragment: () => void 0
|
|
1135
|
+
},
|
|
1136
|
+
preference: {
|
|
1137
|
+
toFragment: (input) => preference(input.aspect, input.value),
|
|
1138
|
+
fromFragment: () => void 0
|
|
1139
|
+
},
|
|
1140
|
+
correction: {
|
|
1141
|
+
toFragment: (input) => correction(input.subject, input.clarification),
|
|
1142
|
+
fromFragment: () => void 0
|
|
1143
|
+
}
|
|
1144
|
+
};
|
|
1145
|
+
var messageLikeTypes = /* @__PURE__ */ new Set(["user", "assistant", "message"]);
|
|
1146
|
+
function findCustomSerializedFragment(fragment2, options) {
|
|
1147
|
+
if (!options?.registry) {
|
|
1148
|
+
return void 0;
|
|
1149
|
+
}
|
|
1150
|
+
for (const entry of Object.values(options.registry)) {
|
|
1151
|
+
const serialized = entry.fromFragment(fragment2, options);
|
|
1152
|
+
if (serialized !== void 0) {
|
|
1153
|
+
return serialized;
|
|
496
1154
|
}
|
|
497
|
-
}
|
|
1155
|
+
}
|
|
1156
|
+
return void 0;
|
|
498
1157
|
}
|
|
499
|
-
function
|
|
500
|
-
if (
|
|
501
|
-
|
|
1158
|
+
function toFragment(input, options) {
|
|
1159
|
+
if (messageLikeTypes.has(input.type)) {
|
|
1160
|
+
throw new Error(
|
|
1161
|
+
"Message fragments are not supported by serialized fragment conversion"
|
|
1162
|
+
);
|
|
502
1163
|
}
|
|
503
|
-
|
|
504
|
-
|
|
1164
|
+
const entry = options?.registry?.[input.type] ?? builtInSerializedRegistry[input.type];
|
|
1165
|
+
if (!entry) {
|
|
1166
|
+
throw new Error(`Unsupported serialized fragment type: ${input.type}`);
|
|
505
1167
|
}
|
|
506
|
-
|
|
507
|
-
|
|
1168
|
+
return entry.toFragment(input, options);
|
|
1169
|
+
}
|
|
1170
|
+
function fromFragment(fragment2, options) {
|
|
1171
|
+
if (isMessageFragment(fragment2)) {
|
|
1172
|
+
throw new Error(
|
|
1173
|
+
"Message fragments are not supported by serialized fragment conversion"
|
|
1174
|
+
);
|
|
508
1175
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
return
|
|
1176
|
+
const customSerialized = findCustomSerializedFragment(fragment2, options);
|
|
1177
|
+
if (customSerialized !== void 0) {
|
|
1178
|
+
return customSerialized;
|
|
512
1179
|
}
|
|
513
|
-
|
|
1180
|
+
if (fragment2.codec) {
|
|
1181
|
+
const encoded = fragment2.codec.encode();
|
|
1182
|
+
if (!isSerializedFragmentLike(encoded)) {
|
|
1183
|
+
throw new Error(
|
|
1184
|
+
`Fragment "${fragment2.name}" codec must encode to a serialized fragment object`
|
|
1185
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
return encoded;
|
|
1188
|
+
}
|
|
1189
|
+
if (!builtInSerializedRegistry[fragment2.name]) {
|
|
1190
|
+
throw new Error(`Unsupported fragment name: ${fragment2.name}`);
|
|
1191
|
+
}
|
|
1192
|
+
throw new Error(`Fragment "${fragment2.name}" is missing codec`);
|
|
514
1193
|
}
|
|
515
1194
|
|
|
516
1195
|
// packages/context/src/lib/engine.ts
|
|
1196
|
+
import { validateUIMessages } from "ai";
|
|
517
1197
|
import { mergeWith } from "lodash-es";
|
|
518
1198
|
|
|
519
1199
|
// packages/context/src/lib/estimate.ts
|
|
@@ -718,7 +1398,7 @@ var ContextRenderer = class {
|
|
|
718
1398
|
return sanitized;
|
|
719
1399
|
}
|
|
720
1400
|
sanitizeFragment(fragment2, seen) {
|
|
721
|
-
const data = this.sanitizeData(fragment2
|
|
1401
|
+
const data = this.sanitizeData(getFragmentData(fragment2), seen);
|
|
722
1402
|
if (data == null) {
|
|
723
1403
|
return null;
|
|
724
1404
|
}
|
|
@@ -795,12 +1475,13 @@ var XmlRenderer = class extends ContextRenderer {
|
|
|
795
1475
|
return sanitized.map((f) => this.#renderTopLevel(f)).filter(Boolean).join("\n");
|
|
796
1476
|
}
|
|
797
1477
|
#renderTopLevel(fragment2) {
|
|
798
|
-
|
|
799
|
-
|
|
1478
|
+
const data = getFragmentData(fragment2);
|
|
1479
|
+
if (this.isPrimitive(data)) {
|
|
1480
|
+
return this.#leafRoot(fragment2.name, String(data));
|
|
800
1481
|
}
|
|
801
|
-
if (Array.isArray(
|
|
802
|
-
if (
|
|
803
|
-
const single =
|
|
1482
|
+
if (Array.isArray(data)) {
|
|
1483
|
+
if (data.length === 1) {
|
|
1484
|
+
const single = data[0];
|
|
804
1485
|
if (this.isPrimitive(single)) {
|
|
805
1486
|
return this.#leafRoot(fragment2.name, String(single));
|
|
806
1487
|
}
|
|
@@ -818,19 +1499,15 @@ var XmlRenderer = class extends ContextRenderer {
|
|
|
818
1499
|
);
|
|
819
1500
|
}
|
|
820
1501
|
}
|
|
821
|
-
return this.#renderArray(fragment2.name,
|
|
1502
|
+
return this.#renderArray(fragment2.name, data, 0);
|
|
822
1503
|
}
|
|
823
|
-
if (isFragment(
|
|
824
|
-
return this.#renderFragmentContentsUnderParent(
|
|
825
|
-
fragment2.name,
|
|
826
|
-
fragment2.data,
|
|
827
|
-
0
|
|
828
|
-
);
|
|
1504
|
+
if (isFragment(data)) {
|
|
1505
|
+
return this.#renderFragmentContentsUnderParent(fragment2.name, data, 0);
|
|
829
1506
|
}
|
|
830
|
-
if (isFragmentObject(
|
|
1507
|
+
if (isFragmentObject(data)) {
|
|
831
1508
|
return this.#wrap(
|
|
832
1509
|
fragment2.name,
|
|
833
|
-
this.renderEntries(
|
|
1510
|
+
this.renderEntries(data, { depth: 1, path: [] })
|
|
834
1511
|
);
|
|
835
1512
|
}
|
|
836
1513
|
return "";
|
|
@@ -962,23 +1639,23 @@ ${this.#indent(safe, 2)}
|
|
|
962
1639
|
return `<${tag}>${safe}</${tag}>`;
|
|
963
1640
|
}
|
|
964
1641
|
renderFragment(fragment2, ctx) {
|
|
965
|
-
const
|
|
1642
|
+
const data = getFragmentData(fragment2);
|
|
966
1643
|
if (this.isPrimitive(data)) {
|
|
967
|
-
return this.#leaf(name, String(data), ctx.depth);
|
|
1644
|
+
return this.#leaf(fragment2.name, String(data), ctx.depth);
|
|
968
1645
|
}
|
|
969
1646
|
if (isFragment(data)) {
|
|
970
1647
|
const child = this.renderFragment(data, { ...ctx, depth: ctx.depth + 1 });
|
|
971
|
-
return this.#wrapIndented(name, [child], ctx.depth);
|
|
1648
|
+
return this.#wrapIndented(fragment2.name, [child], ctx.depth);
|
|
972
1649
|
}
|
|
973
1650
|
if (Array.isArray(data)) {
|
|
974
|
-
return this.#renderArrayIndented(name, data, ctx.depth);
|
|
1651
|
+
return this.#renderArrayIndented(fragment2.name, data, ctx.depth);
|
|
975
1652
|
}
|
|
976
1653
|
if (isFragmentObject(data)) {
|
|
977
1654
|
const children = this.renderEntries(data, {
|
|
978
1655
|
...ctx,
|
|
979
1656
|
depth: ctx.depth + 1
|
|
980
1657
|
});
|
|
981
|
-
return this.#wrapIndented(name, children, ctx.depth);
|
|
1658
|
+
return this.#wrapIndented(fragment2.name, children, ctx.depth);
|
|
982
1659
|
}
|
|
983
1660
|
return "";
|
|
984
1661
|
}
|
|
@@ -1095,21 +1772,22 @@ var MarkdownRenderer = class extends ContextRenderer {
|
|
|
1095
1772
|
render(fragments) {
|
|
1096
1773
|
return this.sanitizeFragments(fragments).map((f) => {
|
|
1097
1774
|
const title = `## ${titlecase(f.name)}`;
|
|
1098
|
-
|
|
1775
|
+
const data = getFragmentData(f);
|
|
1776
|
+
if (this.isPrimitive(data)) {
|
|
1099
1777
|
return `${title}
|
|
1100
|
-
${String(
|
|
1778
|
+
${String(data)}`;
|
|
1101
1779
|
}
|
|
1102
|
-
if (Array.isArray(
|
|
1780
|
+
if (Array.isArray(data)) {
|
|
1103
1781
|
return `${title}
|
|
1104
|
-
${this.#renderArray(
|
|
1782
|
+
${this.#renderArray(data, 0)}`;
|
|
1105
1783
|
}
|
|
1106
|
-
if (isFragment(
|
|
1784
|
+
if (isFragment(data)) {
|
|
1107
1785
|
return `${title}
|
|
1108
|
-
${this.renderFragment(
|
|
1786
|
+
${this.renderFragment(data, { depth: 0, path: [] })}`;
|
|
1109
1787
|
}
|
|
1110
|
-
if (isFragmentObject(
|
|
1788
|
+
if (isFragmentObject(data)) {
|
|
1111
1789
|
return `${title}
|
|
1112
|
-
${this.renderEntries(
|
|
1790
|
+
${this.renderEntries(data, { depth: 0, path: [] }).join("\n")}`;
|
|
1113
1791
|
}
|
|
1114
1792
|
return `${title}
|
|
1115
1793
|
`;
|
|
@@ -1159,10 +1837,10 @@ ${this.renderEntries(f.data, { depth: 0, path: [] }).join("\n")}`;
|
|
|
1159
1837
|
return `${this.#pad(depth)}- ${String(item)}`;
|
|
1160
1838
|
}
|
|
1161
1839
|
renderFragment(fragment2, ctx) {
|
|
1162
|
-
const
|
|
1163
|
-
const header = `${this.#pad(ctx.depth)}- **${name}**:`;
|
|
1840
|
+
const data = getFragmentData(fragment2);
|
|
1841
|
+
const header = `${this.#pad(ctx.depth)}- **${fragment2.name}**:`;
|
|
1164
1842
|
if (this.isPrimitive(data)) {
|
|
1165
|
-
return `${this.#pad(ctx.depth)}- **${name}**: ${String(data)}`;
|
|
1843
|
+
return `${this.#pad(ctx.depth)}- **${fragment2.name}**: ${String(data)}`;
|
|
1166
1844
|
}
|
|
1167
1845
|
if (isFragment(data)) {
|
|
1168
1846
|
const child = this.renderFragment(data, { ...ctx, depth: ctx.depth + 1 });
|
|
@@ -1201,21 +1879,22 @@ ${this.renderEntries(f.data, { depth: 0, path: [] }).join("\n")}`;
|
|
|
1201
1879
|
var TomlRenderer = class extends ContextRenderer {
|
|
1202
1880
|
render(fragments) {
|
|
1203
1881
|
const rendered = [];
|
|
1204
|
-
for (const
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1882
|
+
for (const it of this.sanitizeFragments(fragments)) {
|
|
1883
|
+
const data = getFragmentData(it);
|
|
1884
|
+
if (this.isPrimitive(data)) {
|
|
1885
|
+
rendered.push(`${it.name} = ${this.#formatValue(data)}`);
|
|
1886
|
+
} else if (Array.isArray(data)) {
|
|
1887
|
+
rendered.push(this.#renderTopLevelArray(it.name, data));
|
|
1888
|
+
} else if (isFragment(data)) {
|
|
1210
1889
|
rendered.push(
|
|
1211
1890
|
[
|
|
1212
|
-
`[${
|
|
1213
|
-
this.renderFragment(
|
|
1891
|
+
`[${it.name}]`,
|
|
1892
|
+
this.renderFragment(data, { depth: 0, path: [it.name] })
|
|
1214
1893
|
].join("\n")
|
|
1215
1894
|
);
|
|
1216
|
-
} else if (isFragmentObject(
|
|
1217
|
-
const entries = this.#renderObjectEntries(
|
|
1218
|
-
rendered.push([`[${
|
|
1895
|
+
} else if (isFragmentObject(data)) {
|
|
1896
|
+
const entries = this.#renderObjectEntries(data, [it.name]);
|
|
1897
|
+
rendered.push([`[${it.name}]`, ...entries].join("\n"));
|
|
1219
1898
|
}
|
|
1220
1899
|
}
|
|
1221
1900
|
return rendered.join("\n\n");
|
|
@@ -1285,10 +1964,10 @@ var TomlRenderer = class extends ContextRenderer {
|
|
|
1285
1964
|
}).filter(Boolean);
|
|
1286
1965
|
}
|
|
1287
1966
|
renderFragment(fragment2, ctx) {
|
|
1288
|
-
const
|
|
1289
|
-
const newPath = [...ctx.path, name];
|
|
1967
|
+
const data = getFragmentData(fragment2);
|
|
1968
|
+
const newPath = [...ctx.path, fragment2.name];
|
|
1290
1969
|
if (this.isPrimitive(data)) {
|
|
1291
|
-
return `${name} = ${this.#formatValue(data)}`;
|
|
1970
|
+
return `${fragment2.name} = ${this.#formatValue(data)}`;
|
|
1292
1971
|
}
|
|
1293
1972
|
if (isFragment(data)) {
|
|
1294
1973
|
return [
|
|
@@ -1310,7 +1989,7 @@ var TomlRenderer = class extends ContextRenderer {
|
|
|
1310
1989
|
return parts.join("\n");
|
|
1311
1990
|
}
|
|
1312
1991
|
const values = nonFragmentItems.map((item) => this.#formatValue(item));
|
|
1313
|
-
return `${name} = [${values.join(", ")}]`;
|
|
1992
|
+
return `${fragment2.name} = [${values.join(", ")}]`;
|
|
1314
1993
|
}
|
|
1315
1994
|
if (isFragmentObject(data)) {
|
|
1316
1995
|
const entries = this.#renderObjectEntries(data, newPath);
|
|
@@ -1340,27 +2019,27 @@ var ToonRenderer = class extends ContextRenderer {
|
|
|
1340
2019
|
return sanitized.map((f) => this.#renderTopLevel(f)).filter(Boolean).join("\n");
|
|
1341
2020
|
}
|
|
1342
2021
|
#renderTopLevel(fragment2) {
|
|
1343
|
-
const
|
|
2022
|
+
const data = getFragmentData(fragment2);
|
|
1344
2023
|
if (this.isPrimitive(data)) {
|
|
1345
|
-
return `${name}: ${this.#formatValue(data)}`;
|
|
2024
|
+
return `${fragment2.name}: ${this.#formatValue(data)}`;
|
|
1346
2025
|
}
|
|
1347
2026
|
if (Array.isArray(data)) {
|
|
1348
|
-
return this.#renderArrayField(name, data, 0);
|
|
2027
|
+
return this.#renderArrayField(fragment2.name, data, 0);
|
|
1349
2028
|
}
|
|
1350
2029
|
if (isFragment(data)) {
|
|
1351
2030
|
const child = this.renderFragment(data, { depth: 1, path: [] });
|
|
1352
|
-
return `${name}:
|
|
2031
|
+
return `${fragment2.name}:
|
|
1353
2032
|
${child}`;
|
|
1354
2033
|
}
|
|
1355
2034
|
if (isFragmentObject(data)) {
|
|
1356
2035
|
const entries = this.#renderObjectEntries(data, 1);
|
|
1357
2036
|
if (!entries) {
|
|
1358
|
-
return `${name}:`;
|
|
2037
|
+
return `${fragment2.name}:`;
|
|
1359
2038
|
}
|
|
1360
|
-
return `${name}:
|
|
2039
|
+
return `${fragment2.name}:
|
|
1361
2040
|
${entries}`;
|
|
1362
2041
|
}
|
|
1363
|
-
return `${name}:`;
|
|
2042
|
+
return `${fragment2.name}:`;
|
|
1364
2043
|
}
|
|
1365
2044
|
#renderArrayField(key, items, depth) {
|
|
1366
2045
|
const filtered = items.filter((item) => item != null);
|
|
@@ -1435,8 +2114,9 @@ ${entries}`;
|
|
|
1435
2114
|
depth: depth + 1,
|
|
1436
2115
|
path: []
|
|
1437
2116
|
});
|
|
1438
|
-
|
|
1439
|
-
|
|
2117
|
+
const itemData = getFragmentData(item);
|
|
2118
|
+
if (this.isPrimitive(itemData)) {
|
|
2119
|
+
return `${this.#pad(depth)}- ${item.name}: ${this.#formatValue(itemData)}`;
|
|
1440
2120
|
}
|
|
1441
2121
|
return `${this.#pad(depth)}- ${item.name}:
|
|
1442
2122
|
${rendered.split("\n").slice(1).join("\n")}`;
|
|
@@ -1479,30 +2159,30 @@ ${nested}`);
|
|
|
1479
2159
|
return lines.join("\n");
|
|
1480
2160
|
}
|
|
1481
2161
|
renderFragment(fragment2, ctx) {
|
|
1482
|
-
const
|
|
2162
|
+
const data = getFragmentData(fragment2);
|
|
1483
2163
|
if (this.isPrimitive(data)) {
|
|
1484
|
-
return `${this.#pad(ctx.depth)}${name}: ${this.#formatValue(data)}`;
|
|
2164
|
+
return `${this.#pad(ctx.depth)}${fragment2.name}: ${this.#formatValue(data)}`;
|
|
1485
2165
|
}
|
|
1486
2166
|
if (isFragment(data)) {
|
|
1487
2167
|
const child = this.renderFragment(data, {
|
|
1488
2168
|
...ctx,
|
|
1489
2169
|
depth: ctx.depth + 1
|
|
1490
2170
|
});
|
|
1491
|
-
return `${this.#pad(ctx.depth)}${name}:
|
|
2171
|
+
return `${this.#pad(ctx.depth)}${fragment2.name}:
|
|
1492
2172
|
${child}`;
|
|
1493
2173
|
}
|
|
1494
2174
|
if (Array.isArray(data)) {
|
|
1495
|
-
return this.#renderArrayField(name, data, ctx.depth);
|
|
2175
|
+
return this.#renderArrayField(fragment2.name, data, ctx.depth);
|
|
1496
2176
|
}
|
|
1497
2177
|
if (isFragmentObject(data)) {
|
|
1498
2178
|
const entries = this.#renderObjectEntries(data, ctx.depth + 1);
|
|
1499
2179
|
if (!entries) {
|
|
1500
|
-
return `${this.#pad(ctx.depth)}${name}:`;
|
|
2180
|
+
return `${this.#pad(ctx.depth)}${fragment2.name}:`;
|
|
1501
2181
|
}
|
|
1502
|
-
return `${this.#pad(ctx.depth)}${name}:
|
|
2182
|
+
return `${this.#pad(ctx.depth)}${fragment2.name}:
|
|
1503
2183
|
${entries}`;
|
|
1504
2184
|
}
|
|
1505
|
-
return `${this.#pad(ctx.depth)}${name}:`;
|
|
2185
|
+
return `${this.#pad(ctx.depth)}${fragment2.name}:`;
|
|
1506
2186
|
}
|
|
1507
2187
|
renderPrimitive(key, value, ctx) {
|
|
1508
2188
|
return `${this.#pad(ctx.depth)}${key}: ${this.#formatValue(value)}`;
|
|
@@ -1557,6 +2237,9 @@ var ContextStore = class {
|
|
|
1557
2237
|
};
|
|
1558
2238
|
|
|
1559
2239
|
// packages/context/src/lib/engine.ts
|
|
2240
|
+
function estimateMessageContent(data) {
|
|
2241
|
+
return typeof data === "string" ? data : JSON.stringify(data);
|
|
2242
|
+
}
|
|
1560
2243
|
var ContextEngine = class {
|
|
1561
2244
|
/** Non-message fragments (role, hints, etc.) - not persisted in graph */
|
|
1562
2245
|
#fragments = [];
|
|
@@ -1739,7 +2422,7 @@ var ContextEngine = class {
|
|
|
1739
2422
|
this.#branch.headMessageId
|
|
1740
2423
|
);
|
|
1741
2424
|
for (const msg of chain) {
|
|
1742
|
-
messages.push(
|
|
2425
|
+
messages.push(msg.data);
|
|
1743
2426
|
}
|
|
1744
2427
|
}
|
|
1745
2428
|
for (let i = 0; i < this.#pendingMessages.length; i++) {
|
|
@@ -1751,13 +2434,15 @@ var ContextEngine = class {
|
|
|
1751
2434
|
for (const fragment2 of this.#pendingMessages) {
|
|
1752
2435
|
if (!fragment2.codec) {
|
|
1753
2436
|
throw new Error(
|
|
1754
|
-
`Fragment "${fragment2.name}" is missing codec. Lazy fragments must be resolved before
|
|
2437
|
+
`Fragment "${fragment2.name}" is missing codec. Lazy fragments must be resolved before encode.`
|
|
1755
2438
|
);
|
|
1756
2439
|
}
|
|
1757
|
-
|
|
1758
|
-
messages.push(decoded);
|
|
2440
|
+
messages.push(fragment2.codec.encode());
|
|
1759
2441
|
}
|
|
1760
|
-
return {
|
|
2442
|
+
return {
|
|
2443
|
+
systemPrompt,
|
|
2444
|
+
messages: messages.length === 0 ? [] : await validateUIMessages({ messages })
|
|
2445
|
+
};
|
|
1761
2446
|
}
|
|
1762
2447
|
/**
|
|
1763
2448
|
* Save pending messages to the graph.
|
|
@@ -1904,7 +2589,7 @@ var ContextEngine = class {
|
|
|
1904
2589
|
this.#branch.headMessageId
|
|
1905
2590
|
);
|
|
1906
2591
|
for (const msg of chain) {
|
|
1907
|
-
const content =
|
|
2592
|
+
const content = estimateMessageContent(msg.data);
|
|
1908
2593
|
const tokens = tokenizer.count(content);
|
|
1909
2594
|
const cost = tokens / 1e6 * model.cost.input;
|
|
1910
2595
|
fragmentEstimates.push({
|
|
@@ -1916,7 +2601,9 @@ var ContextEngine = class {
|
|
|
1916
2601
|
}
|
|
1917
2602
|
}
|
|
1918
2603
|
for (const fragment2 of this.#pendingMessages) {
|
|
1919
|
-
const content =
|
|
2604
|
+
const content = estimateMessageContent(
|
|
2605
|
+
fragment2.codec ? fragment2.codec.encode() : getFragmentData(fragment2)
|
|
2606
|
+
);
|
|
1920
2607
|
const tokens = tokenizer.count(content);
|
|
1921
2608
|
const cost = tokens / 1e6 * model.cost.input;
|
|
1922
2609
|
fragmentEstimates.push({
|
|
@@ -2246,138 +2933,6 @@ var ContextEngine = class {
|
|
|
2246
2933
|
}
|
|
2247
2934
|
};
|
|
2248
2935
|
|
|
2249
|
-
// packages/context/src/lib/fragments/domain.ts
|
|
2250
|
-
function term(name, definition) {
|
|
2251
|
-
return {
|
|
2252
|
-
name: "term",
|
|
2253
|
-
data: { name, definition }
|
|
2254
|
-
};
|
|
2255
|
-
}
|
|
2256
|
-
function hint(text) {
|
|
2257
|
-
return {
|
|
2258
|
-
name: "hint",
|
|
2259
|
-
data: text
|
|
2260
|
-
};
|
|
2261
|
-
}
|
|
2262
|
-
function guardrail(input) {
|
|
2263
|
-
return {
|
|
2264
|
-
name: "guardrail",
|
|
2265
|
-
data: {
|
|
2266
|
-
rule: input.rule,
|
|
2267
|
-
...input.reason && { reason: input.reason },
|
|
2268
|
-
...input.action && { action: input.action }
|
|
2269
|
-
}
|
|
2270
|
-
};
|
|
2271
|
-
}
|
|
2272
|
-
function explain(input) {
|
|
2273
|
-
return {
|
|
2274
|
-
name: "explain",
|
|
2275
|
-
data: {
|
|
2276
|
-
concept: input.concept,
|
|
2277
|
-
explanation: input.explanation,
|
|
2278
|
-
...input.therefore && { therefore: input.therefore }
|
|
2279
|
-
}
|
|
2280
|
-
};
|
|
2281
|
-
}
|
|
2282
|
-
function example(input) {
|
|
2283
|
-
return {
|
|
2284
|
-
name: "example",
|
|
2285
|
-
data: {
|
|
2286
|
-
question: input.question,
|
|
2287
|
-
answer: input.answer,
|
|
2288
|
-
...input.note && { note: input.note }
|
|
2289
|
-
}
|
|
2290
|
-
};
|
|
2291
|
-
}
|
|
2292
|
-
function clarification(input) {
|
|
2293
|
-
return {
|
|
2294
|
-
name: "clarification",
|
|
2295
|
-
data: {
|
|
2296
|
-
when: input.when,
|
|
2297
|
-
ask: input.ask,
|
|
2298
|
-
reason: input.reason
|
|
2299
|
-
}
|
|
2300
|
-
};
|
|
2301
|
-
}
|
|
2302
|
-
function workflow(input) {
|
|
2303
|
-
return {
|
|
2304
|
-
name: "workflow",
|
|
2305
|
-
data: {
|
|
2306
|
-
task: input.task,
|
|
2307
|
-
steps: input.steps,
|
|
2308
|
-
...input.triggers?.length && { triggers: input.triggers },
|
|
2309
|
-
...input.notes && { notes: input.notes }
|
|
2310
|
-
}
|
|
2311
|
-
};
|
|
2312
|
-
}
|
|
2313
|
-
function quirk(input) {
|
|
2314
|
-
return {
|
|
2315
|
-
name: "quirk",
|
|
2316
|
-
data: {
|
|
2317
|
-
issue: input.issue,
|
|
2318
|
-
workaround: input.workaround
|
|
2319
|
-
}
|
|
2320
|
-
};
|
|
2321
|
-
}
|
|
2322
|
-
function styleGuide(input) {
|
|
2323
|
-
return {
|
|
2324
|
-
name: "styleGuide",
|
|
2325
|
-
data: {
|
|
2326
|
-
prefer: input.prefer,
|
|
2327
|
-
...input.never && { never: input.never },
|
|
2328
|
-
...input.always && { always: input.always }
|
|
2329
|
-
}
|
|
2330
|
-
};
|
|
2331
|
-
}
|
|
2332
|
-
function analogy(input) {
|
|
2333
|
-
return {
|
|
2334
|
-
name: "analogy",
|
|
2335
|
-
data: {
|
|
2336
|
-
concepts: input.concepts,
|
|
2337
|
-
relationship: input.relationship,
|
|
2338
|
-
...input.insight && { insight: input.insight },
|
|
2339
|
-
...input.therefore && { therefore: input.therefore },
|
|
2340
|
-
...input.pitfall && { pitfall: input.pitfall }
|
|
2341
|
-
}
|
|
2342
|
-
};
|
|
2343
|
-
}
|
|
2344
|
-
function glossary(entries) {
|
|
2345
|
-
return {
|
|
2346
|
-
name: "glossary",
|
|
2347
|
-
data: Object.entries(entries).map(([term2, expression]) => ({
|
|
2348
|
-
term: term2,
|
|
2349
|
-
expression
|
|
2350
|
-
}))
|
|
2351
|
-
};
|
|
2352
|
-
}
|
|
2353
|
-
function role(content) {
|
|
2354
|
-
return {
|
|
2355
|
-
name: "role",
|
|
2356
|
-
data: content
|
|
2357
|
-
};
|
|
2358
|
-
}
|
|
2359
|
-
function principle(input) {
|
|
2360
|
-
return {
|
|
2361
|
-
name: "principle",
|
|
2362
|
-
data: {
|
|
2363
|
-
title: input.title,
|
|
2364
|
-
description: input.description,
|
|
2365
|
-
...input.policies?.length && { policies: input.policies }
|
|
2366
|
-
}
|
|
2367
|
-
};
|
|
2368
|
-
}
|
|
2369
|
-
function policy(input) {
|
|
2370
|
-
return {
|
|
2371
|
-
name: "policy",
|
|
2372
|
-
data: {
|
|
2373
|
-
rule: input.rule,
|
|
2374
|
-
...input.before && { before: input.before },
|
|
2375
|
-
...input.reason && { reason: input.reason },
|
|
2376
|
-
...input.policies?.length && { policies: input.policies }
|
|
2377
|
-
}
|
|
2378
|
-
};
|
|
2379
|
-
}
|
|
2380
|
-
|
|
2381
2936
|
// packages/context/src/lib/fragments/message/user.ts
|
|
2382
2937
|
import { generateId as generateId4, isTextUIPart } from "ai";
|
|
2383
2938
|
var SYSTEM_REMINDER_OPEN_TAG = "<system-reminder>";
|
|
@@ -2551,7 +3106,6 @@ function user(content, ...reminders) {
|
|
|
2551
3106
|
return {
|
|
2552
3107
|
id: message2.id,
|
|
2553
3108
|
name: "user",
|
|
2554
|
-
data: "content",
|
|
2555
3109
|
type: "message",
|
|
2556
3110
|
persist: true,
|
|
2557
3111
|
codec: {
|
|
@@ -2565,52 +3119,6 @@ function user(content, ...reminders) {
|
|
|
2565
3119
|
};
|
|
2566
3120
|
}
|
|
2567
3121
|
|
|
2568
|
-
// packages/context/src/lib/fragments/user.ts
|
|
2569
|
-
function identity(input) {
|
|
2570
|
-
return {
|
|
2571
|
-
name: "identity",
|
|
2572
|
-
data: {
|
|
2573
|
-
...input.name && { name: input.name },
|
|
2574
|
-
...input.role && { role: input.role }
|
|
2575
|
-
}
|
|
2576
|
-
};
|
|
2577
|
-
}
|
|
2578
|
-
function persona(input) {
|
|
2579
|
-
return {
|
|
2580
|
-
name: "persona",
|
|
2581
|
-
data: {
|
|
2582
|
-
name: input.name,
|
|
2583
|
-
...input.role && { role: input.role },
|
|
2584
|
-
...input.objective && { objective: input.objective },
|
|
2585
|
-
...input.tone && { tone: input.tone }
|
|
2586
|
-
}
|
|
2587
|
-
};
|
|
2588
|
-
}
|
|
2589
|
-
function alias(term2, meaning) {
|
|
2590
|
-
return {
|
|
2591
|
-
name: "alias",
|
|
2592
|
-
data: { term: term2, meaning }
|
|
2593
|
-
};
|
|
2594
|
-
}
|
|
2595
|
-
function preference(aspect, value) {
|
|
2596
|
-
return {
|
|
2597
|
-
name: "preference",
|
|
2598
|
-
data: { aspect, value }
|
|
2599
|
-
};
|
|
2600
|
-
}
|
|
2601
|
-
function userContext(description) {
|
|
2602
|
-
return {
|
|
2603
|
-
name: "userContext",
|
|
2604
|
-
data: description
|
|
2605
|
-
};
|
|
2606
|
-
}
|
|
2607
|
-
function correction(subject, clarification2) {
|
|
2608
|
-
return {
|
|
2609
|
-
name: "correction",
|
|
2610
|
-
data: { subject, clarification: clarification2 }
|
|
2611
|
-
};
|
|
2612
|
-
}
|
|
2613
|
-
|
|
2614
3122
|
// packages/context/src/lib/guardrails/error-recovery.guardrail.ts
|
|
2615
3123
|
import chalk2 from "chalk";
|
|
2616
3124
|
var errorRecoveryGuardrail = {
|
|
@@ -6590,6 +7098,8 @@ export {
|
|
|
6590
7098
|
explain,
|
|
6591
7099
|
fail,
|
|
6592
7100
|
fragment,
|
|
7101
|
+
fromFragment,
|
|
7102
|
+
getFragmentData,
|
|
6593
7103
|
getModelsRegistry,
|
|
6594
7104
|
getReminderRanges,
|
|
6595
7105
|
glossary,
|
|
@@ -6630,10 +7140,10 @@ export {
|
|
|
6630
7140
|
structuredOutput,
|
|
6631
7141
|
styleGuide,
|
|
6632
7142
|
term,
|
|
7143
|
+
toFragment,
|
|
6633
7144
|
toMessageFragment,
|
|
6634
7145
|
useSandbox,
|
|
6635
7146
|
user,
|
|
6636
|
-
userContext,
|
|
6637
7147
|
visualizeGraph,
|
|
6638
7148
|
workflow
|
|
6639
7149
|
};
|