@deepagents/context 0.28.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/index.js CHANGED
@@ -591,6 +591,27 @@ async function estimate(modelId, renderer, ...fragments) {
591
591
 
592
592
  // packages/context/src/lib/fragments/message/user.ts
593
593
  import { generateId as generateId3, isTextUIPart } from "ai";
594
+ function everyNTurns(n) {
595
+ return (turn) => turn % n === 0;
596
+ }
597
+ function once() {
598
+ return (turn) => turn === 1;
599
+ }
600
+ function firstN(n) {
601
+ return (turn) => turn <= n;
602
+ }
603
+ function afterTurn(n) {
604
+ return (turn) => turn > n;
605
+ }
606
+ function and(...predicates) {
607
+ return (turn) => predicates.every((p) => p(turn));
608
+ }
609
+ function or(...predicates) {
610
+ return (turn) => predicates.some((p) => p(turn));
611
+ }
612
+ function not(predicate) {
613
+ return (turn) => !predicate(turn);
614
+ }
594
615
  var SYSTEM_REMINDER_OPEN_TAG = "<system-reminder>";
595
616
  var SYSTEM_REMINDER_CLOSE_TAG = "</system-reminder>";
596
617
  function getReminderRanges(metadata) {
@@ -726,15 +747,9 @@ function applyPartReminder(message2, value) {
726
747
  };
727
748
  }
728
749
  function hasSchedule(reminder2) {
729
- return reminder2.everyNTurns !== void 0 || reminder2.once !== void 0 || reminder2.firstN !== void 0 || reminder2.afterTurn !== void 0 || reminder2.when !== void 0;
750
+ return reminder2.when !== void 0;
730
751
  }
731
752
  function shouldIncludeReminder(reminder2, turn) {
732
- if (reminder2.once && turn !== 1) return false;
733
- if (reminder2.firstN !== void 0 && turn > reminder2.firstN) return false;
734
- if (reminder2.afterTurn !== void 0 && turn <= reminder2.afterTurn)
735
- return false;
736
- if (reminder2.everyNTurns !== void 0 && turn % reminder2.everyNTurns !== 0)
737
- return false;
738
753
  if (reminder2.when && !reminder2.when(turn)) return false;
739
754
  return true;
740
755
  }
@@ -745,12 +760,6 @@ function reminder(text, options) {
745
760
  return {
746
761
  text,
747
762
  asPart: options?.asPart ?? false,
748
- ...options?.everyNTurns !== void 0 && {
749
- everyNTurns: options.everyNTurns
750
- },
751
- ...options?.once !== void 0 && { once: options.once },
752
- ...options?.firstN !== void 0 && { firstN: options.firstN },
753
- ...options?.afterTurn !== void 0 && { afterTurn: options.afterTurn },
754
763
  ...options?.when !== void 0 && { when: options.when }
755
764
  };
756
765
  }
@@ -3426,25 +3435,29 @@ async function chat(agent2, messages, options) {
3426
3435
  } else {
3427
3436
  context.set(lastFragment);
3428
3437
  await context.save();
3429
- assistantMsgId = options?.generateMessageId?.() ?? generateId4();
3438
+ assistantMsgId = generateId4();
3430
3439
  }
3431
3440
  const uiMessages = messages.map(chatMessageToUIMessage);
3432
- let titlePromise = null;
3441
+ let title = null;
3433
3442
  if (!context.chat?.title) {
3434
3443
  const firstUserMsg = uiMessages.find((m) => m.role === "user");
3435
3444
  if (firstUserMsg) {
3436
- await context.updateChat({ title: staticChatTitle(firstUserMsg) });
3437
3445
  if (options?.generateTitle && agent2.model) {
3438
- titlePromise = generateChatTitle({
3446
+ title = await generateChatTitle({
3439
3447
  message: firstUserMsg,
3440
- model: agent2.model
3448
+ model: agent2.model,
3449
+ abortSignal: options?.abortSignal
3441
3450
  });
3451
+ } else {
3452
+ title = staticChatTitle(firstUserMsg);
3442
3453
  }
3454
+ await context.updateChat({ title });
3443
3455
  }
3444
3456
  }
3445
3457
  const streamContextVariables = options?.contextVariables === void 0 ? {} : options.contextVariables;
3446
3458
  const result = await agent2.stream(streamContextVariables, {
3447
- transform: options?.transform
3459
+ transform: options?.transform,
3460
+ abortSignal: options?.abortSignal
3448
3461
  });
3449
3462
  const uiStream = result.toUIMessageStream({
3450
3463
  onError: options?.onError ?? formatChatError,
@@ -3487,10 +3500,8 @@ async function chat(agent2, messages, options) {
3487
3500
  },
3488
3501
  execute: async ({ writer }) => {
3489
3502
  writer.merge(uiStream);
3490
- if (titlePromise) {
3491
- const title = await titlePromise;
3503
+ if (title) {
3492
3504
  writer.write({ type: "data-chat-title", data: title });
3493
- await context.updateChat({ title });
3494
3505
  }
3495
3506
  }
3496
3507
  });
@@ -7219,9 +7230,11 @@ export {
7219
7230
  TomlRenderer,
7220
7231
  ToonRenderer,
7221
7232
  XmlRenderer,
7233
+ afterTurn,
7222
7234
  agent,
7223
7235
  alias,
7224
7236
  analogy,
7237
+ and,
7225
7238
  applyInlineReminder,
7226
7239
  applyPartReminder,
7227
7240
  assistant,
@@ -7239,9 +7252,11 @@ export {
7239
7252
  encodeSerializedValue,
7240
7253
  errorRecoveryGuardrail,
7241
7254
  estimate,
7255
+ everyNTurns,
7242
7256
  example,
7243
7257
  explain,
7244
7258
  fail,
7259
+ firstN,
7245
7260
  fragment,
7246
7261
  fromFragment,
7247
7262
  generateChatTitle,
@@ -7265,6 +7280,9 @@ export {
7265
7280
  nextAdaptivePollingDelay,
7266
7281
  normalizeCancelPolling,
7267
7282
  normalizeWatchPolling,
7283
+ not,
7284
+ once,
7285
+ or,
7268
7286
  parseFrontmatter,
7269
7287
  pass,
7270
7288
  persistedWriter,