system-reminder.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * `systemReminderExtension`: Installs `SystemReminderService` through the `@mavis/agent-runtime`
  3. * extension SPI. The design document §4.3 table maps system-reminder to
  4. * `pi.registerReminderProvider`.
  5. *
  6. * `SystemReminderService` already owns a `SystemReminderRegistry` chain; `buildReminder` assembles
  7. * one `<system-reminder>` block. This adapter registers that aggregated block as one reminder
  8. * emission rather than a user prompt prefix, honoring `Reminder.content`'s complete-block contract.
  9. * Hosts consuming `AssemblyResult.reminders` inject blocks in assembler order without adding
  10. * wrappers.
  11. *
  12. * Host resolver contract: `SystemReminderService.buildReminder` needs `SessionInfo` +
  13. * `MessageRequest`, while SPI `TurnAssemblyCtx` only exposes read-only projections such as
  14. * `sessionId / turnId / agentName / workspaceDir / userInput / ...` (§6.2). The host supplies
  15. * `resolveContext(ctx) => { session, msg } | null` to convert SPI context to service context:
  16. * - Return `null` when the host has no SR context (e.g. background tasks); skip reminder generation
  17. * for this turn.
  18. * - When SR context exists, the resolver supplies SessionInfo / MessageRequest from backend lookups
  19. * based on turn state. Exceptions propagate to `assembleTurn` (fail-fast).
  20. *
  21. * Service fail-open note: `SystemReminderService.buildReminder` catches errors internally and
  22. * returns `{ text: undefined }` instead of throwing; the extension relies on this contract. If the
  23. * service later becomes fail-fast, the extension propagates errors to the pi loop (see the
  24. * rejection propagation case in `system-reminder.test.ts`).
  25. *
  26. * The extension exposes reminders through `pi.registerReminderProvider`; it does not write
  27. * `AssemblyResult.userPromptPrefix`.
  28. */
  29. import type { AgentExtension, ExtensionAPI, TurnAssemblyCtx } from '@mavis/agent-runtime';
  30. import type {
  31. MessageRequest,
  32. SessionInfo,
  33. SystemReminderDiagnostic,
  34. SystemReminderService,
  35. } from '@mavis/system-reminder';
  36. export interface SystemReminderResolvedContext {
  37. readonly session: SessionInfo;
  38. readonly msg: MessageRequest;
  39. }
  40. export type SystemReminderContextResolver = (
  41. ctx: TurnAssemblyCtx,
  42. ) => SystemReminderResolvedContext | null | Promise<SystemReminderResolvedContext | null>;
  43. export interface SystemReminderExtensionOptions {
  44. readonly service: SystemReminderService;
  45. readonly resolveContext: SystemReminderContextResolver;
  46. /** Extension id override; default `'system-reminder'` per design doc §4.3. */
  47. readonly id?: string;
  48. readonly description?: string;
  49. /**
  50. * Optional diagnostic sink populated with per-turn `SystemReminderDiagnostic`.
  51. * Only invoked when the extension explicitly forces `withDiagnostic: true`
  52. * for this reason. Note: `SystemReminderService.buildReminder` also produces
  53. * diagnostics when its debug store is populated internally—those still go
  54. * to the debug store, not to this callback, so hosts wanting the callback
  55. * path must supply `onDiagnostic` here (they can supply both).
  56. */
  57. readonly onDiagnostic?: (
  58. diagnostic: SystemReminderDiagnostic,
  59. ctx: TurnAssemblyCtx,
  60. ) => void | Promise<void>;
  61. }
  62. export function systemReminderExtension(options: SystemReminderExtensionOptions): AgentExtension {
  63. const { service, resolveContext, onDiagnostic } = options;
  64. const id = options.id ?? 'system-reminder';
  65. const description =
  66. options.description ??
  67. 'Emit an aggregated <system-reminder> block each turn via SystemReminderService.';
  68. return {
  69. id,
  70. description,
  71. init(pi: ExtensionAPI): void {
  72. pi.registerReminderProvider({
  73. name: id,
  74. compute: async (ctx) => {
  75. const resolved = await resolveContext(ctx);
  76. if (resolved === null) return null;
  77. const withDiag = Boolean(onDiagnostic);
  78. const { text, diagnostic } = await service.buildReminder(
  79. resolved.session,
  80. resolved.msg,
  81. withDiag ? { withDiagnostic: true } : undefined,
  82. );
  83. if (onDiagnostic && diagnostic) {
  84. await onDiagnostic(diagnostic, ctx);
  85. }
  86. return text ? { content: text } : null;
  87. },
  88. });
  89. },
  90. };
  91. }