prompt-read.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Host-neutral prompt template read contracts and bounded internal-turn handoff.
  3. *
  4. * Local runtime produces internal model turns while local-runtime-v2 consumes
  5. * them during preflight. Both packages already depend on agent-core.
  6. */
  7. declare const promptReadSnapshotBrand: unique symbol;
  8. export interface PromptReadSnapshot {
  9. readonly [promptReadSnapshotBrand]: never;
  10. }
  11. export type PromptTemplateRead =
  12. | { readonly kind: 'found'; readonly content: string }
  13. | { readonly kind: 'missing' }
  14. | { readonly kind: 'invalid' };
  15. /** Signals that the enclosing model call must be rebuilt from builtin prompts. */
  16. export class PromptSnapshotInvalidError extends Error {
  17. override readonly name = 'PromptSnapshotInvalidError';
  18. constructor(message = 'Prompt snapshot is invalid') {
  19. super(message);
  20. }
  21. }
  22. export function isPromptSnapshotInvalidError(error: unknown): error is PromptSnapshotInvalidError {
  23. return error instanceof PromptSnapshotInvalidError;
  24. }
  25. /** Production hosts own snapshot selection and consumers can only read it. */
  26. export interface PromptSnapshotSource {
  27. capture(): Promise<PromptReadSnapshot>;
  28. captureBuiltin(): Promise<PromptReadSnapshot>;
  29. read(snapshot: PromptReadSnapshot, key: string): Promise<PromptTemplateRead>;
  30. }
  31. /** A host-captured prompt source and immutable snapshot for one model call. */
  32. export interface PromptReadScope {
  33. readonly source: PromptSnapshotSource;
  34. readonly snapshot: PromptReadSnapshot;
  35. }
  36. /** Process-local handoff for prompts rendered before an internal Turn is admitted. */
  37. export interface InternalTurnPromptReadRegistry {
  38. reserve(turnId: string): boolean;
  39. remember(turnId: string, promptRead: PromptReadScope): void;
  40. rebind(fromTurnId: string, toTurnId: string): boolean;
  41. take(turnId: string): PromptReadScope | undefined;
  42. discard(turnId: string): void;
  43. close(): void;
  44. }
  45. export class BoundedInternalTurnPromptReadRegistry implements InternalTurnPromptReadRegistry {
  46. private readonly entries = new Map<string, PromptReadScope>();
  47. private readonly reservations = new Set<string>();
  48. private closed = false;
  49. constructor(private readonly capacity = 128) {}
  50. reserve(turnId: string): boolean {
  51. if (this.closed || this.entries.has(turnId) || this.reservations.has(turnId)) return false;
  52. if (this.entries.size + this.reservations.size >= this.capacity) return false;
  53. this.reservations.add(turnId);
  54. return true;
  55. }
  56. remember(turnId: string, promptRead: PromptReadScope): void {
  57. if (this.closed || !this.reservations.delete(turnId)) return;
  58. this.entries.set(turnId, promptRead);
  59. }
  60. rebind(fromTurnId: string, toTurnId: string): boolean {
  61. if (this.closed) return false;
  62. const promptRead = this.entries.get(fromTurnId);
  63. if (!promptRead) return false;
  64. if (fromTurnId === toTurnId) return true;
  65. if (this.entries.has(toTurnId) || this.reservations.has(toTurnId)) return false;
  66. this.entries.delete(fromTurnId);
  67. this.entries.set(toTurnId, promptRead);
  68. return true;
  69. }
  70. take(turnId: string): PromptReadScope | undefined {
  71. const promptRead = this.entries.get(turnId);
  72. this.entries.delete(turnId);
  73. this.reservations.delete(turnId);
  74. return promptRead;
  75. }
  76. discard(turnId: string): void {
  77. this.reservations.delete(turnId);
  78. this.entries.delete(turnId);
  79. }
  80. close(): void {
  81. this.closed = true;
  82. this.entries.clear();
  83. this.reservations.clear();
  84. }
  85. }