@deepagents/context 0.27.0 → 0.29.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
@@ -581,44 +581,38 @@ function toFragmentData(value, options) {
581
581
  }
582
582
  var builtInSerializedRegistry = {
583
583
  term: {
584
- toFragment: (input) => term(input.name, input.definition),
585
- fromFragment: () => void 0
584
+ toFragment: (input) => term(input.name, input.definition)
586
585
  },
587
586
  hint: {
588
- toFragment: (input) => hint(input.text),
589
- fromFragment: () => void 0
587
+ toFragment: (input) => hint(input.text)
590
588
  },
591
589
  guardrail: {
592
590
  toFragment: (input) => guardrail({
593
591
  rule: input.rule,
594
592
  reason: input.reason,
595
593
  action: input.action
596
- }),
597
- fromFragment: () => void 0
594
+ })
598
595
  },
599
596
  explain: {
600
597
  toFragment: (input) => explain({
601
598
  concept: input.concept,
602
599
  explanation: input.explanation,
603
600
  therefore: input.therefore
604
- }),
605
- fromFragment: () => void 0
601
+ })
606
602
  },
607
603
  example: {
608
604
  toFragment: (input) => example({
609
605
  question: input.question,
610
606
  answer: input.answer,
611
607
  note: input.note
612
- }),
613
- fromFragment: () => void 0
608
+ })
614
609
  },
615
610
  clarification: {
616
611
  toFragment: (input) => clarification({
617
612
  when: input.when,
618
613
  ask: input.ask,
619
614
  reason: input.reason
620
- }),
621
- fromFragment: () => void 0
615
+ })
622
616
  },
623
617
  workflow: {
624
618
  toFragment: (input) => workflow({
@@ -626,23 +620,20 @@ var builtInSerializedRegistry = {
626
620
  steps: input.steps,
627
621
  triggers: input.triggers,
628
622
  notes: input.notes
629
- }),
630
- fromFragment: () => void 0
623
+ })
631
624
  },
632
625
  quirk: {
633
626
  toFragment: (input) => quirk({
634
627
  issue: input.issue,
635
628
  workaround: input.workaround
636
- }),
637
- fromFragment: () => void 0
629
+ })
638
630
  },
639
631
  styleGuide: {
640
632
  toFragment: (input) => styleGuide({
641
633
  prefer: input.prefer,
642
634
  never: input.never,
643
635
  always: input.always
644
- }),
645
- fromFragment: () => void 0
636
+ })
646
637
  },
647
638
  analogy: {
648
639
  toFragment: (input) => analogy({
@@ -651,24 +642,20 @@ var builtInSerializedRegistry = {
651
642
  insight: input.insight,
652
643
  therefore: input.therefore,
653
644
  pitfall: input.pitfall
654
- }),
655
- fromFragment: () => void 0
645
+ })
656
646
  },
657
647
  glossary: {
658
- toFragment: (input) => glossary(input.entries),
659
- fromFragment: () => void 0
648
+ toFragment: (input) => glossary(input.entries)
660
649
  },
661
650
  role: {
662
- toFragment: (input) => role(input.content),
663
- fromFragment: () => void 0
651
+ toFragment: (input) => role(input.content)
664
652
  },
665
653
  principle: {
666
654
  toFragment: (input, options) => principle({
667
655
  title: input.title,
668
656
  description: input.description,
669
657
  policies: input.policies?.map((item) => toFragmentData(item, options))
670
- }),
671
- fromFragment: () => void 0
658
+ })
672
659
  },
673
660
  policy: {
674
661
  toFragment: (input, options) => policy({
@@ -676,15 +663,13 @@ var builtInSerializedRegistry = {
676
663
  before: input.before,
677
664
  reason: input.reason,
678
665
  policies: input.policies?.map((item) => toFragmentData(item, options))
679
- }),
680
- fromFragment: () => void 0
666
+ })
681
667
  },
682
668
  identity: {
683
669
  toFragment: (input) => identity({
684
670
  name: input.name,
685
671
  role: input.role
686
- }),
687
- fromFragment: () => void 0
672
+ })
688
673
  },
689
674
  persona: {
690
675
  toFragment: (input) => persona({
@@ -692,20 +677,16 @@ var builtInSerializedRegistry = {
692
677
  role: input.role,
693
678
  objective: input.objective,
694
679
  tone: input.tone
695
- }),
696
- fromFragment: () => void 0
680
+ })
697
681
  },
698
682
  alias: {
699
- toFragment: (input) => alias(input.term, input.meaning),
700
- fromFragment: () => void 0
683
+ toFragment: (input) => alias(input.term, input.meaning)
701
684
  },
702
685
  preference: {
703
- toFragment: (input) => preference(input.aspect, input.value),
704
- fromFragment: () => void 0
686
+ toFragment: (input) => preference(input.aspect, input.value)
705
687
  },
706
688
  correction: {
707
- toFragment: (input) => correction(input.subject, input.clarification),
708
- fromFragment: () => void 0
689
+ toFragment: (input) => correction(input.subject, input.clarification)
709
690
  }
710
691
  };
711
692
  var messageLikeTypes = /* @__PURE__ */ new Set(["user", "assistant", "message"]);
@@ -714,7 +695,7 @@ function findCustomSerializedFragment(fragment2, options) {
714
695
  return void 0;
715
696
  }
716
697
  for (const entry of Object.values(options.registry)) {
717
- const serialized = entry.fromFragment(fragment2, options);
698
+ const serialized = entry.fromFragment?.(fragment2, options);
718
699
  if (serialized !== void 0) {
719
700
  return serialized;
720
701
  }
@@ -920,6 +901,27 @@ async function estimate(modelId, renderer, ...fragments) {
920
901
 
921
902
  // packages/context/src/lib/fragments/message/user.ts
922
903
  import { generateId as generateId2, isTextUIPart } from "ai";
904
+ function everyNTurns(n) {
905
+ return (turn) => turn % n === 0;
906
+ }
907
+ function once() {
908
+ return (turn) => turn === 1;
909
+ }
910
+ function firstN(n) {
911
+ return (turn) => turn <= n;
912
+ }
913
+ function afterTurn(n) {
914
+ return (turn) => turn > n;
915
+ }
916
+ function and(...predicates) {
917
+ return (turn) => predicates.every((p) => p(turn));
918
+ }
919
+ function or(...predicates) {
920
+ return (turn) => predicates.some((p) => p(turn));
921
+ }
922
+ function not(predicate) {
923
+ return (turn) => !predicate(turn);
924
+ }
923
925
  var SYSTEM_REMINDER_OPEN_TAG = "<system-reminder>";
924
926
  var SYSTEM_REMINDER_CLOSE_TAG = "</system-reminder>";
925
927
  function getReminderRanges(metadata) {
@@ -1054,26 +1056,39 @@ function applyPartReminder(message2, value) {
1054
1056
  mode: "part"
1055
1057
  };
1056
1058
  }
1059
+ function hasSchedule(reminder2) {
1060
+ return reminder2.when !== void 0;
1061
+ }
1062
+ function shouldIncludeReminder(reminder2, turn) {
1063
+ if (reminder2.when && !reminder2.when(turn)) return false;
1064
+ return true;
1065
+ }
1057
1066
  function reminder(text, options) {
1058
1067
  if (typeof text === "string") {
1059
1068
  assertReminderText(text);
1060
1069
  }
1061
1070
  return {
1062
1071
  text,
1063
- asPart: options?.asPart ?? false
1072
+ asPart: options?.asPart ?? false,
1073
+ ...options?.when !== void 0 && { when: options.when }
1064
1074
  };
1065
1075
  }
1076
+ function resolveReminderText(item, ctx) {
1077
+ return typeof item.text === "function" ? item.text(ctx) : item.text;
1078
+ }
1066
1079
  function user(content, ...reminders) {
1067
1080
  const message2 = typeof content === "string" ? {
1068
1081
  id: generateId2(),
1069
1082
  role: "user",
1070
1083
  parts: [{ type: "text", text: content }]
1071
1084
  } : { ...content, role: "user", parts: [...content.parts] };
1072
- if (reminders.length > 0) {
1085
+ const immediateReminders = reminders.filter((r) => !hasSchedule(r));
1086
+ const scheduledReminders = reminders.filter((r) => hasSchedule(r));
1087
+ if (immediateReminders.length > 0) {
1073
1088
  const addedReminders = [];
1074
1089
  const plainText = extractPlainText(message2);
1075
- for (const item of reminders) {
1076
- const resolvedText = typeof item.text === "function" ? item.text(plainText) : item.text;
1090
+ for (const item of immediateReminders) {
1091
+ const resolvedText = resolveReminderText(item, { content: plainText });
1077
1092
  if (resolvedText.trim().length === 0) {
1078
1093
  continue;
1079
1094
  }
@@ -1088,6 +1103,7 @@ function user(content, ...reminders) {
1088
1103
  message2.metadata = metadata;
1089
1104
  }
1090
1105
  }
1106
+ const fragmentMetadata = scheduledReminders.length > 0 ? { scheduledReminders } : void 0;
1091
1107
  return {
1092
1108
  id: message2.id,
1093
1109
  name: "user",
@@ -1100,7 +1116,8 @@ function user(content, ...reminders) {
1100
1116
  encode() {
1101
1117
  return message2;
1102
1118
  }
1103
- }
1119
+ },
1120
+ metadata: fragmentMetadata
1104
1121
  };
1105
1122
  }
1106
1123
 
@@ -2166,17 +2183,23 @@ export {
2166
2183
  TomlRenderer,
2167
2184
  ToonRenderer,
2168
2185
  XmlRenderer,
2186
+ afterTurn,
2169
2187
  alias,
2170
2188
  analogy,
2189
+ and,
2190
+ applyInlineReminder,
2191
+ applyPartReminder,
2171
2192
  assistant,
2172
2193
  assistantText,
2173
2194
  clarification,
2174
2195
  correction,
2175
2196
  defaultTokenizer,
2176
2197
  estimate,
2198
+ everyNTurns,
2177
2199
  example,
2178
2200
  explain,
2179
2201
  fail,
2202
+ firstN,
2180
2203
  fragment,
2181
2204
  fromFragment,
2182
2205
  getFragmentData,
@@ -2184,6 +2207,7 @@ export {
2184
2207
  getReminderRanges,
2185
2208
  glossary,
2186
2209
  guardrail,
2210
+ hasSchedule,
2187
2211
  hint,
2188
2212
  identity,
2189
2213
  isFragment,
@@ -2192,6 +2216,9 @@ export {
2192
2216
  isMessageFragment,
2193
2217
  lastAssistantMessage,
2194
2218
  message,
2219
+ not,
2220
+ once,
2221
+ or,
2195
2222
  pass,
2196
2223
  persistedWriter,
2197
2224
  persona,
@@ -2201,8 +2228,10 @@ export {
2201
2228
  quirk,
2202
2229
  reminder,
2203
2230
  render,
2231
+ resolveReminderText,
2204
2232
  role,
2205
2233
  runGuardrailChain,
2234
+ shouldIncludeReminder,
2206
2235
  soul,
2207
2236
  stop,
2208
2237
  stripReminders,