dependencies.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * System-reminder injected dependencies.
  3. *
  4. * The block assembly logic (registry / providers / blocks / service) lives in
  5. * this package and produces a `<system-reminder>` text block from a
  6. * `SystemReminderInput` payload. The host owns the IO-heavy data collection
  7. * (reading stores, scanning filesystem) and the cross-cutting runtime
  8. * collaborators (logger, request context, config, date formatting); it supplies
  9. * them through the small interfaces defined here.
  10. *
  11. * - {@link DataCollector}: satisfied by the host's data collector. The
  12. * service calls it once per `buildReminder` to gather the IO-heavy payload.
  13. * - {@link NudgeRegistry}: satisfied by the host's nudge registry. The
  14. * `boardNudgeProvider` reads + clears a pending nudge for the session.
  15. * - {@link Logger} / {@link LogContext}: the host logging surface, injected
  16. * so this package never imports a runtime logger.
  17. * - {@link ReminderConfig}: the small config slice the service reads.
  18. * - {@link DateFormatter}: host-local date formatting (timezone owned by host).
  19. *
  20. * No runtime fallback is exposed: dependencies are passed in directly via
  21. * constructor arguments or setters, so a missing wiring fails loudly at the
  22. * call site instead of silently no-opping. Hosts satisfy these shapes
  23. * structurally — no `implements` declaration required.
  24. */
  25. import type { SessionInfo, MessageRequest, SystemReminderInput } from './types.js';
  26. // ─── DataCollector ─────────────────────────────────────────────────────────
  27. /**
  28. * Subset of the host data collector consumed by `SystemReminderService`.
  29. *
  30. * The full host collector is IO-heavy (sqlite stores, filesystem scans,
  31. * agent template lookups) and stays in the host. The service only calls two
  32. * methods — gather data for a turn, and release per-session bookkeeping on
  33. * session removal.
  34. */
  35. export interface DataCollector {
  36. collect(
  37. session: SessionInfo,
  38. msg: MessageRequest,
  39. turnCount: number,
  40. ): Promise<SystemReminderInput>;
  41. onSessionRemoved(sessionId: string): void;
  42. }
  43. // ─── NudgeRegistry ───────────────────────────────────────────────────────────
  44. /** Pending nudge metadata returned by {@link NudgeRegistry.get}. */
  45. export interface NudgeInfo {
  46. /** Path to the board.md file the worker should update. */
  47. boardPath: string;
  48. /** Current consecutive nudge count (for logging / escalation context). */
  49. nudgeCount: number;
  50. /** Epoch-ms when the nudge was set. */
  51. createdAt: number;
  52. }
  53. /**
  54. * Subset of the host nudge registry consumed by `boardNudgeProvider`.
  55. *
  56. * The host registry satisfies this structurally — it carries additional
  57. * methods (`set`, `clearAll`, `size`) that we don't need on the read path.
  58. */
  59. export interface NudgeRegistry {
  60. get(sessionId: string): NudgeInfo | undefined;
  61. clear(sessionId: string): void;
  62. }
  63. // ─── Logging ───────────────────────────────────────────────────────────────
  64. /**
  65. * Per-call request context passed to the logger. Structurally compatible with
  66. * the host's `RequestContext` (carries at least a trace id); only `traceId` is
  67. * required by this package.
  68. */
  69. export interface LogContext {
  70. traceId: string;
  71. callerAgent?: string;
  72. callerSession?: string;
  73. locale?: string;
  74. }
  75. /**
  76. * Logging surface consumed by `SystemReminderService`. The host injects an
  77. * implementation; only the levels this package uses are declared.
  78. */
  79. export interface Logger {
  80. warn(ctx: LogContext, msg: string): void;
  81. info(ctx: LogContext, msg: string): void;
  82. error(ctx: LogContext, msg: string): void;
  83. }
  84. // ─── ReminderConfig ──────────────────────────────────────────────────────────
  85. /**
  86. * Config slice read by `SystemReminderService`. The host owns the full config;
  87. * this is the only field the SR service reads (gates the model-preview path
  88. * that suppresses the per-message reminder for selected models).
  89. */
  90. export interface ReminderConfig {
  91. disableModelPrefixes: readonly string[];
  92. }
  93. // ─── DateFormatter ─────────────────────────────────────────────────────────
  94. /**
  95. * Host-local datetime formatter. The host owns the timezone; this package only
  96. * needs to render a timestamp into a human-readable local string.
  97. */
  98. export type DateFormatter = (ts: number | Date) => string;
  99. // ─── Diagnostic shapes ─────────────────────────────────────────────────────
  100. /**
  101. * Per-provider diagnostic emitted alongside the assembled `<system-reminder>`
  102. * when diagnostics are explicitly requested via the `withDiagnostic` option.
  103. */
  104. export interface ProviderDiagnostic {
  105. /** Provider name, e.g. "agentContextProvider". */
  106. name: string;
  107. /** Whether the provider produced output. */
  108. fired: boolean;
  109. /** The block content produced (only when fired=true). */
  110. output?: string | undefined;
  111. /** Execution time in milliseconds. */
  112. durationMs: number;
  113. /** Whether this is a critical provider (runs even when full SR is disabled). */
  114. critical: boolean;
  115. }
  116. /**
  117. * Aggregate diagnostic returned from `SystemReminderService.buildReminder`
  118. * when diagnostics are explicitly requested.
  119. */
  120. export interface SystemReminderDiagnostic {
  121. fullText: string | null;
  122. criticalOnly: boolean;
  123. providers: ProviderDiagnostic[];
  124. collectDurationMs: number;
  125. totalDurationMs: number;
  126. }
  127. // ─── ModelSelector ───────────────────────────────────────────────────────────
  128. /** Resolved model identifier returned by {@link ModelSelector.resolveForSend}. */
  129. export interface ModelSelection {
  130. providerID: string;
  131. modelID: string;
  132. }
  133. /**
  134. * Subset of the host's model selection service consumed by
  135. * `SystemReminderService` for the SR-disable preview.
  136. *
  137. * The service calls `resolveForSend({ dryRun: true })` to learn which model
  138. * the bridge will commit to before actually dispatching, so it can decide
  139. * whether the per-message reminder block is suppressed for that model. The
  140. * host's model selection service satisfies the shape; cloud hosts can supply a
  141. * remote-resolution proxy.
  142. */
  143. export interface ModelSelector {
  144. resolveForSend(input: {
  145. sessionId: string;
  146. agentName: string;
  147. requestModel?: { providerID: string; modelID: string; variant?: string };
  148. dryRun?: boolean;
  149. }): Promise<ModelSelection>;
  150. }