@framers/agentos 0.2.6 → 0.2.8

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.
Files changed (93) hide show
  1. package/dist/ingest-router/IngestRouter.d.ts +72 -0
  2. package/dist/ingest-router/IngestRouter.d.ts.map +1 -0
  3. package/dist/ingest-router/IngestRouter.js +98 -0
  4. package/dist/ingest-router/IngestRouter.js.map +1 -0
  5. package/dist/ingest-router/classifier.d.ts +63 -0
  6. package/dist/ingest-router/classifier.d.ts.map +1 -0
  7. package/dist/ingest-router/classifier.js +111 -0
  8. package/dist/ingest-router/classifier.js.map +1 -0
  9. package/dist/ingest-router/costs.d.ts +48 -0
  10. package/dist/ingest-router/costs.d.ts.map +1 -0
  11. package/dist/ingest-router/costs.js +63 -0
  12. package/dist/ingest-router/costs.js.map +1 -0
  13. package/dist/ingest-router/dispatcher.d.ts +35 -0
  14. package/dist/ingest-router/dispatcher.d.ts.map +1 -0
  15. package/dist/ingest-router/dispatcher.js +32 -0
  16. package/dist/ingest-router/dispatcher.js.map +1 -0
  17. package/dist/ingest-router/index.d.ts +43 -0
  18. package/dist/ingest-router/index.d.ts.map +1 -0
  19. package/dist/ingest-router/index.js +37 -0
  20. package/dist/ingest-router/index.js.map +1 -0
  21. package/dist/ingest-router/routing-tables.d.ts +122 -0
  22. package/dist/ingest-router/routing-tables.d.ts.map +1 -0
  23. package/dist/ingest-router/routing-tables.js +145 -0
  24. package/dist/ingest-router/routing-tables.js.map +1 -0
  25. package/dist/ingest-router/select-strategy.d.ts +67 -0
  26. package/dist/ingest-router/select-strategy.d.ts.map +1 -0
  27. package/dist/ingest-router/select-strategy.js +100 -0
  28. package/dist/ingest-router/select-strategy.js.map +1 -0
  29. package/dist/memory-router/MemoryRouter.d.ts +195 -0
  30. package/dist/memory-router/MemoryRouter.d.ts.map +1 -0
  31. package/dist/memory-router/MemoryRouter.js +155 -0
  32. package/dist/memory-router/MemoryRouter.js.map +1 -0
  33. package/dist/memory-router/adaptive.d.ts +142 -0
  34. package/dist/memory-router/adaptive.d.ts.map +1 -0
  35. package/dist/memory-router/adaptive.js +202 -0
  36. package/dist/memory-router/adaptive.js.map +1 -0
  37. package/dist/memory-router/backend-costs.d.ts +67 -0
  38. package/dist/memory-router/backend-costs.d.ts.map +1 -0
  39. package/dist/memory-router/backend-costs.js +136 -0
  40. package/dist/memory-router/backend-costs.js.map +1 -0
  41. package/dist/memory-router/classifier.d.ts +169 -0
  42. package/dist/memory-router/classifier.d.ts.map +1 -0
  43. package/dist/memory-router/classifier.js +193 -0
  44. package/dist/memory-router/classifier.js.map +1 -0
  45. package/dist/memory-router/dispatcher.d.ts +115 -0
  46. package/dist/memory-router/dispatcher.d.ts.map +1 -0
  47. package/dist/memory-router/dispatcher.js +84 -0
  48. package/dist/memory-router/dispatcher.js.map +1 -0
  49. package/dist/memory-router/index.d.ts +126 -0
  50. package/dist/memory-router/index.d.ts.map +1 -0
  51. package/dist/memory-router/index.js +122 -0
  52. package/dist/memory-router/index.js.map +1 -0
  53. package/dist/memory-router/routing-tables.d.ts +125 -0
  54. package/dist/memory-router/routing-tables.d.ts.map +1 -0
  55. package/dist/memory-router/routing-tables.js +137 -0
  56. package/dist/memory-router/routing-tables.js.map +1 -0
  57. package/dist/memory-router/select-backend.d.ts +136 -0
  58. package/dist/memory-router/select-backend.d.ts.map +1 -0
  59. package/dist/memory-router/select-backend.js +210 -0
  60. package/dist/memory-router/select-backend.js.map +1 -0
  61. package/dist/multi-stage-guardrails/index.d.ts +190 -0
  62. package/dist/multi-stage-guardrails/index.d.ts.map +1 -0
  63. package/dist/multi-stage-guardrails/index.js +186 -0
  64. package/dist/multi-stage-guardrails/index.js.map +1 -0
  65. package/dist/read-router/ReadRouter.d.ts +58 -0
  66. package/dist/read-router/ReadRouter.d.ts.map +1 -0
  67. package/dist/read-router/ReadRouter.js +91 -0
  68. package/dist/read-router/ReadRouter.js.map +1 -0
  69. package/dist/read-router/classifier.d.ts +54 -0
  70. package/dist/read-router/classifier.d.ts.map +1 -0
  71. package/dist/read-router/classifier.js +104 -0
  72. package/dist/read-router/classifier.js.map +1 -0
  73. package/dist/read-router/costs.d.ts +23 -0
  74. package/dist/read-router/costs.d.ts.map +1 -0
  75. package/dist/read-router/costs.js +51 -0
  76. package/dist/read-router/costs.js.map +1 -0
  77. package/dist/read-router/dispatcher.d.ts +33 -0
  78. package/dist/read-router/dispatcher.d.ts.map +1 -0
  79. package/dist/read-router/dispatcher.js +29 -0
  80. package/dist/read-router/dispatcher.js.map +1 -0
  81. package/dist/read-router/index.d.ts +23 -0
  82. package/dist/read-router/index.d.ts.map +1 -0
  83. package/dist/read-router/index.js +17 -0
  84. package/dist/read-router/index.js.map +1 -0
  85. package/dist/read-router/routing-tables.d.ts +85 -0
  86. package/dist/read-router/routing-tables.d.ts.map +1 -0
  87. package/dist/read-router/routing-tables.js +79 -0
  88. package/dist/read-router/routing-tables.js.map +1 -0
  89. package/dist/read-router/select-strategy.d.ts +42 -0
  90. package/dist/read-router/select-strategy.d.ts.map +1 -0
  91. package/dist/read-router/select-strategy.js +92 -0
  92. package/dist/read-router/select-strategy.js.map +1 -0
  93. package/package.json +21 -1
@@ -0,0 +1,190 @@
1
+ /**
2
+ * AgentOS MultiStageGuardrails Module
3
+ *
4
+ * Composition primitive that wires the LLM-as-judge stages of the
5
+ * agentos pipeline into one orchestrator. Each stage is independent and
6
+ * optional — wire only the stages you need, or compose all four.
7
+ *
8
+ * **The four stages:**
9
+ *
10
+ * ```
11
+ * Content Query Query
12
+ * │ │ │
13
+ * ▼ ▼ ▼
14
+ * ┌─────────┐ ┌─────────────┐ ┌─────────────┐
15
+ * │ Ingest │ │ Recall │ │ Read │
16
+ * │ Stage │ │ Stage │ │ Stage │
17
+ * │ (input) │ │ (memory) │ │ (reader) │
18
+ * └─────────┘ └─────────────┘ └─────────────┘
19
+ * │ │ │
20
+ * ▼ ▼ ▼
21
+ * Memory state Retrieved traces Final answer
22
+ * ```
23
+ *
24
+ * Plus the existing output-stage `core/guardrails` and
25
+ * `agentos-ext-grounding-guard` which validate the answer post-generation.
26
+ *
27
+ * **Why a composition primitive:** each individual router is independently
28
+ * useful and ships with its own classifier + dispatcher + table machinery.
29
+ * MultiStageGuardrails gives consumers a single object to coordinate the
30
+ * stages without duplicating per-stage wiring. It does NOT add new
31
+ * routing logic — it's a thin facade over the four primitives.
32
+ *
33
+ * The interfaces below ({@link IngestStage}, {@link RecallStage},
34
+ * {@link ReadStage}) are deliberately minimal so consumers can wire any
35
+ * implementation:
36
+ *
37
+ * - The shipping `IngestRouter` / `MemoryRouter` / `ReadRouter` classes
38
+ * each satisfy the corresponding stage interface via thin adapters
39
+ * (see `agentosStageAdapters` below).
40
+ * - Custom implementations (rule-based, ML-driven, mock for tests) can
41
+ * satisfy the same interfaces and slot in.
42
+ *
43
+ * @module @framers/agentos/multi-stage-guardrails
44
+ */
45
+ /**
46
+ * Generic decision metadata bundled with each stage's result. Stages
47
+ * report enough information for downstream telemetry without leaking
48
+ * stage-specific types into the orchestrator.
49
+ */
50
+ export interface IngestStageResult {
51
+ readonly writtenTraces: number;
52
+ readonly strategy: string;
53
+ readonly ingestRouterDecision: unknown;
54
+ }
55
+ export interface RecallStageResult<TTrace> {
56
+ readonly traces: TTrace[];
57
+ readonly backend: string;
58
+ readonly memoryRouterDecision: unknown;
59
+ }
60
+ export interface ReadStageResult<TOutcome> {
61
+ readonly outcome: TOutcome;
62
+ readonly strategy: string;
63
+ readonly readRouterDecision: unknown;
64
+ }
65
+ /**
66
+ * Pluggable input-stage. Wrap an {@link IngestRouter} or any equivalent
67
+ * implementation that turns content into stored traces.
68
+ */
69
+ export interface IngestStage {
70
+ ingest(content: string, payload?: unknown): Promise<IngestStageResult>;
71
+ }
72
+ /**
73
+ * Pluggable recall-stage. Wrap a {@link MemoryRouter} or any equivalent
74
+ * implementation that turns a query into retrieved traces.
75
+ */
76
+ export interface RecallStage<TTrace> {
77
+ recall(query: string, payload?: unknown): Promise<RecallStageResult<TTrace>>;
78
+ }
79
+ /**
80
+ * Pluggable read-stage. Wrap a {@link ReadRouter} or any equivalent
81
+ * implementation that turns a query+evidence pair into a final answer.
82
+ */
83
+ export interface ReadStage<TTrace, TOutcome> {
84
+ read(query: string, traces: TTrace[], payload?: unknown): Promise<ReadStageResult<TOutcome>>;
85
+ }
86
+ export interface RecallAndReadResult<TTrace, TOutcome> {
87
+ readonly recallStage: RecallStageResult<TTrace>;
88
+ readonly readStage: ReadStageResult<TOutcome>;
89
+ readonly outcome: TOutcome;
90
+ }
91
+ export interface MultiStageGuardrailsOptions<TTrace, TOutcome> {
92
+ readonly ingest?: IngestStage;
93
+ readonly recall?: RecallStage<TTrace>;
94
+ readonly read?: ReadStage<TTrace, TOutcome>;
95
+ }
96
+ export declare class MissingStageError extends Error {
97
+ constructor(stage: 'IngestStage' | 'RecallStage' | 'ReadStage');
98
+ }
99
+ /**
100
+ * Top-level pipeline composition. One instance per agent / endpoint;
101
+ * reuse across requests.
102
+ *
103
+ * @example Full pipeline
104
+ * ```ts
105
+ * import { MultiStageGuardrails } from '../multi-stage-guardrails';
106
+ * import { ingestRouterAsStage } from '../multi-stage-guardrails';
107
+ *
108
+ * const guardrails = new MultiStageGuardrails({
109
+ * ingest: ingestRouterAsStage(myIngestRouter),
110
+ * recall: memoryRouterAsStage(myMemoryRouter),
111
+ * read: readRouterAsStage(myReadRouter),
112
+ * });
113
+ *
114
+ * await guardrails.ingest(newContent);
115
+ * const { outcome } = await guardrails.recallAndRead("what's my latest job?");
116
+ * ```
117
+ */
118
+ export declare class MultiStageGuardrails<TTrace, TOutcome> {
119
+ private readonly ingestStage;
120
+ private readonly recallStage;
121
+ private readonly readStage;
122
+ constructor(options: MultiStageGuardrailsOptions<TTrace, TOutcome>);
123
+ get hasIngestStage(): boolean;
124
+ get hasRecallStage(): boolean;
125
+ get hasReadStage(): boolean;
126
+ ingest(content: string, payload?: unknown): Promise<IngestStageResult>;
127
+ recall(query: string, payload?: unknown): Promise<RecallStageResult<TTrace>>;
128
+ read(query: string, traces: TTrace[], payload?: unknown): Promise<ReadStageResult<TOutcome>>;
129
+ /**
130
+ * Recall traces for the query, then run the reader on those traces.
131
+ * Standard "ask a question, get an answer" path.
132
+ */
133
+ recallAndRead(query: string, recallPayload?: unknown, readPayload?: unknown): Promise<RecallAndReadResult<TTrace, TOutcome>>;
134
+ }
135
+ /**
136
+ * Wrap an {@link IngestRouter} as an {@link IngestStage}. The IngestRouter
137
+ * must have been constructed with a dispatcher; otherwise the stage will
138
+ * throw on every call.
139
+ */
140
+ export declare function ingestRouterAsStage<TOutcome extends {
141
+ writtenTraces?: number;
142
+ }>(router: {
143
+ decideAndDispatch: (content: string, payload?: unknown) => Promise<{
144
+ decision: {
145
+ routing: {
146
+ chosenStrategy: string;
147
+ };
148
+ };
149
+ outcome: TOutcome;
150
+ }>;
151
+ }): IngestStage;
152
+ /**
153
+ * Wrap a {@link MemoryRouter} as a {@link RecallStage}. The MemoryRouter
154
+ * must have been constructed with a dispatcher.
155
+ */
156
+ export declare function memoryRouterAsStage<TTrace>(router: {
157
+ decideAndDispatch: <T = TTrace>(query: string, payload?: unknown) => Promise<{
158
+ decision: {
159
+ routing: {
160
+ chosenBackend: string;
161
+ };
162
+ };
163
+ traces: T[];
164
+ backend: string;
165
+ }>;
166
+ }): RecallStage<TTrace>;
167
+ /**
168
+ * Wrap a {@link ReadRouter} as a {@link ReadStage}. The ReadRouter must
169
+ * have been constructed with a dispatcher.
170
+ */
171
+ export declare function readRouterAsStage<TTrace extends {
172
+ id?: string;
173
+ text?: string;
174
+ }, TOutcome>(router: {
175
+ decideAndDispatch: <T = TOutcome>(query: string, evidence: readonly string[], payload?: unknown) => Promise<{
176
+ decision: {
177
+ routing: {
178
+ chosenStrategy: string;
179
+ };
180
+ };
181
+ outcome: T;
182
+ }>;
183
+ },
184
+ /**
185
+ * Optional adapter to turn a TTrace into a string for the read-router's
186
+ * evidence array. Default: serialize `{id, text}`-shaped traces by
187
+ * concatenating text fields.
188
+ */
189
+ traceToString?: (trace: TTrace) => string): ReadStage<TTrace, TOutcome>;
190
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/multi-stage-guardrails/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAMH;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,oBAAoB,EAAE,OAAO,CAAC;CACxC;AAED,MAAM,WAAW,iBAAiB,CAAC,MAAM;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,oBAAoB,EAAE,OAAO,CAAC;CACxC;AAED,MAAM,WAAW,eAAe,CAAC,QAAQ;IACvC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,kBAAkB,EAAE,OAAO,CAAC;CACtC;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CACxE;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW,CAAC,MAAM;IACjC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC,CAAC;CAC9E;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,MAAM,EAAE,QAAQ;IACzC,IAAI,CACF,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,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AAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,QAAQ,EACvF,MAAM,EAAE;IACN,iBAAiB,EAAE,CAAC,CAAC,GAAG,QAAQ,EAC9B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,OAAO,CAAC,EAAE,OAAO,KACd,OAAO,CAAC;QACX,QAAQ,EAAE;YAAE,OAAO,EAAE;gBAAE,cAAc,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAC;QAClD,OAAO,EAAE,CAAC,CAAC;KACZ,CAAC,CAAC;CACJ;AACD;;;;GAIG;AACH,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,GACxC,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAuB7B"}
@@ -0,0 +1,186 @@
1
+ /**
2
+ * AgentOS MultiStageGuardrails Module
3
+ *
4
+ * Composition primitive that wires the LLM-as-judge stages of the
5
+ * agentos pipeline into one orchestrator. Each stage is independent and
6
+ * optional — wire only the stages you need, or compose all four.
7
+ *
8
+ * **The four stages:**
9
+ *
10
+ * ```
11
+ * Content Query Query
12
+ * │ │ │
13
+ * ▼ ▼ ▼
14
+ * ┌─────────┐ ┌─────────────┐ ┌─────────────┐
15
+ * │ Ingest │ │ Recall │ │ Read │
16
+ * │ Stage │ │ Stage │ │ Stage │
17
+ * │ (input) │ │ (memory) │ │ (reader) │
18
+ * └─────────┘ └─────────────┘ └─────────────┘
19
+ * │ │ │
20
+ * ▼ ▼ ▼
21
+ * Memory state Retrieved traces Final answer
22
+ * ```
23
+ *
24
+ * Plus the existing output-stage `core/guardrails` and
25
+ * `agentos-ext-grounding-guard` which validate the answer post-generation.
26
+ *
27
+ * **Why a composition primitive:** each individual router is independently
28
+ * useful and ships with its own classifier + dispatcher + table machinery.
29
+ * MultiStageGuardrails gives consumers a single object to coordinate the
30
+ * stages without duplicating per-stage wiring. It does NOT add new
31
+ * routing logic — it's a thin facade over the four primitives.
32
+ *
33
+ * The interfaces below ({@link IngestStage}, {@link RecallStage},
34
+ * {@link ReadStage}) are deliberately minimal so consumers can wire any
35
+ * implementation:
36
+ *
37
+ * - The shipping `IngestRouter` / `MemoryRouter` / `ReadRouter` classes
38
+ * each satisfy the corresponding stage interface via thin adapters
39
+ * (see `agentosStageAdapters` below).
40
+ * - Custom implementations (rule-based, ML-driven, mock for tests) can
41
+ * satisfy the same interfaces and slot in.
42
+ *
43
+ * @module @framers/agentos/multi-stage-guardrails
44
+ */
45
+ // ============================================================================
46
+ // Errors
47
+ // ============================================================================
48
+ export class MissingStageError extends Error {
49
+ constructor(stage) {
50
+ super(`MultiStageGuardrails: ${stage} is not configured. ` +
51
+ `Pass it in options at construction.`);
52
+ this.name = 'MissingStageError';
53
+ }
54
+ }
55
+ // ============================================================================
56
+ // Orchestrator
57
+ // ============================================================================
58
+ /**
59
+ * Top-level pipeline composition. One instance per agent / endpoint;
60
+ * reuse across requests.
61
+ *
62
+ * @example Full pipeline
63
+ * ```ts
64
+ * import { MultiStageGuardrails } from '../multi-stage-guardrails/index.js';
65
+ * import { ingestRouterAsStage } from '../multi-stage-guardrails';
66
+ *
67
+ * const guardrails = new MultiStageGuardrails({
68
+ * ingest: ingestRouterAsStage(myIngestRouter),
69
+ * recall: memoryRouterAsStage(myMemoryRouter),
70
+ * read: readRouterAsStage(myReadRouter),
71
+ * });
72
+ *
73
+ * await guardrails.ingest(newContent);
74
+ * const { outcome } = await guardrails.recallAndRead("what's my latest job?");
75
+ * ```
76
+ */
77
+ export class MultiStageGuardrails {
78
+ constructor(options) {
79
+ this.ingestStage = options.ingest ?? null;
80
+ this.recallStage = options.recall ?? null;
81
+ this.readStage = options.read ?? null;
82
+ }
83
+ get hasIngestStage() {
84
+ return this.ingestStage !== null;
85
+ }
86
+ get hasRecallStage() {
87
+ return this.recallStage !== null;
88
+ }
89
+ get hasReadStage() {
90
+ return this.readStage !== null;
91
+ }
92
+ async ingest(content, payload) {
93
+ if (!this.ingestStage)
94
+ throw new MissingStageError('IngestStage');
95
+ return this.ingestStage.ingest(content, payload);
96
+ }
97
+ async recall(query, payload) {
98
+ if (!this.recallStage)
99
+ throw new MissingStageError('RecallStage');
100
+ return this.recallStage.recall(query, payload);
101
+ }
102
+ async read(query, traces, payload) {
103
+ if (!this.readStage)
104
+ throw new MissingStageError('ReadStage');
105
+ return this.readStage.read(query, traces, payload);
106
+ }
107
+ /**
108
+ * Recall traces for the query, then run the reader on those traces.
109
+ * Standard "ask a question, get an answer" path.
110
+ */
111
+ async recallAndRead(query, recallPayload, readPayload) {
112
+ if (!this.recallStage)
113
+ throw new MissingStageError('RecallStage');
114
+ if (!this.readStage)
115
+ throw new MissingStageError('ReadStage');
116
+ const recallStage = await this.recallStage.recall(query, recallPayload);
117
+ const readStage = await this.readStage.read(query, recallStage.traces, readPayload);
118
+ return {
119
+ recallStage,
120
+ readStage,
121
+ outcome: readStage.outcome,
122
+ };
123
+ }
124
+ }
125
+ // ============================================================================
126
+ // Adapters: shipping routers → stage interfaces
127
+ // ============================================================================
128
+ /**
129
+ * Wrap an {@link IngestRouter} as an {@link IngestStage}. The IngestRouter
130
+ * must have been constructed with a dispatcher; otherwise the stage will
131
+ * throw on every call.
132
+ */
133
+ export function ingestRouterAsStage(router) {
134
+ return {
135
+ async ingest(content, payload) {
136
+ const result = await router.decideAndDispatch(content, payload);
137
+ return {
138
+ writtenTraces: result.outcome.writtenTraces ?? 0,
139
+ strategy: result.decision.routing.chosenStrategy,
140
+ ingestRouterDecision: result.decision,
141
+ };
142
+ },
143
+ };
144
+ }
145
+ /**
146
+ * Wrap a {@link MemoryRouter} as a {@link RecallStage}. The MemoryRouter
147
+ * must have been constructed with a dispatcher.
148
+ */
149
+ export function memoryRouterAsStage(router) {
150
+ return {
151
+ async recall(query, payload) {
152
+ const result = await router.decideAndDispatch(query, payload);
153
+ return {
154
+ traces: result.traces,
155
+ backend: result.backend,
156
+ memoryRouterDecision: result.decision,
157
+ };
158
+ },
159
+ };
160
+ }
161
+ /**
162
+ * Wrap a {@link ReadRouter} as a {@link ReadStage}. The ReadRouter must
163
+ * have been constructed with a dispatcher.
164
+ */
165
+ export function readRouterAsStage(router,
166
+ /**
167
+ * Optional adapter to turn a TTrace into a string for the read-router's
168
+ * evidence array. Default: serialize `{id, text}`-shaped traces by
169
+ * concatenating text fields.
170
+ */
171
+ traceToString) {
172
+ const toString = traceToString ??
173
+ ((t) => t.text ?? JSON.stringify(t));
174
+ return {
175
+ async read(query, traces, payload) {
176
+ const evidenceStrings = traces.map(toString);
177
+ const result = await router.decideAndDispatch(query, evidenceStrings, payload);
178
+ return {
179
+ outcome: result.outcome,
180
+ strategy: result.decision.routing.chosenStrategy,
181
+ readRouterDecision: result.decision,
182
+ };
183
+ },
184
+ };
185
+ }
186
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/multi-stage-guardrails/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AA6EH,+EAA+E;AAC/E,SAAS;AACT,+EAA+E;AAE/E,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YAAY,KAAkD;QAC5D,KAAK,CACH,yBAAyB,KAAK,sBAAsB;YAClD,qCAAqC,CACxC,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IAClC,CAAC;CACF;AAED,+EAA+E;AAC/E,eAAe;AACf,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,oBAAoB;IAK/B,YAAY,OAAsD;QAChE,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC;QAC1C,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC;QAC1C,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC;IACxC,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CAAS,EAAU,EAAE,CAAE,CAAuB,CAAC,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9E,OAAO;QACL,KAAK,CAAC,IAAI,CACR,KAAa,EACb,MAAgB,EAChB,OAAiB;YAEjB,MAAM,eAAe,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YAC7C,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,iBAAiB,CAC3C,KAAK,EACL,eAAe,EACf,OAAO,CACR,CAAC;YACF,OAAO;gBACL,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,cAAc;gBAChD,kBAAkB,EAAE,MAAM,CAAC,QAAQ;aACpC,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @file ReadRouter.ts
3
+ * @description Top-level read-stage orchestrator composing the read-intent
4
+ * classifier with the pure {@link selectReadStrategy} into a single
5
+ * per-query routing call.
6
+ *
7
+ * @module @framers/agentos/read-router/ReadRouter
8
+ */
9
+ import type { IReadIntentClassifier, ReadIntentClassifierResult } from './classifier.js';
10
+ import { type ReadStrategyCostPoint } from './costs.js';
11
+ import { type ReadIntent, type ReadRouterPreset, type ReadRoutingTable, type ReadStrategyId } from './routing-tables.js';
12
+ import { type ReadBudgetMode, type ReadRoutingDecision } from './select-strategy.js';
13
+ import type { IReadDispatcher } from './dispatcher.js';
14
+ export interface ReadBudgetPolicy {
15
+ readonly perReadUsd?: number;
16
+ readonly mode?: ReadBudgetMode;
17
+ }
18
+ export interface ReadRouterOptions {
19
+ readonly classifier: IReadIntentClassifier;
20
+ readonly preset?: ReadRouterPreset;
21
+ readonly routingTable?: ReadRoutingTable;
22
+ readonly mapping?: Partial<Record<ReadIntent, ReadStrategyId>>;
23
+ readonly budget?: ReadBudgetPolicy;
24
+ readonly strategyCosts?: Readonly<Record<ReadStrategyId, ReadStrategyCostPoint>>;
25
+ readonly useFewShotPrompt?: boolean;
26
+ readonly dispatcher?: IReadDispatcher<unknown, unknown>;
27
+ }
28
+ export interface ReadRouterDecideOptions {
29
+ readonly manualIntent?: ReadIntent;
30
+ readonly groundTruthIntent?: ReadIntent | null;
31
+ readonly useFewShotPrompt?: boolean;
32
+ }
33
+ export interface ReadRouterDecision {
34
+ readonly classifier: ReadIntentClassifierResult;
35
+ readonly routing: ReadRoutingDecision;
36
+ }
37
+ export interface ReadRouterDispatchedResult<TOutcome> {
38
+ readonly decision: ReadRouterDecision;
39
+ readonly outcome: TOutcome;
40
+ readonly strategy: ReadStrategyId;
41
+ }
42
+ export declare class ReadRouterDispatcherMissingError extends Error {
43
+ constructor();
44
+ }
45
+ export declare class ReadRouter {
46
+ private readonly classifier;
47
+ private readonly preset;
48
+ private readonly routingTable;
49
+ private readonly budgetPerReadUsd;
50
+ private readonly budgetMode;
51
+ private readonly strategyCosts;
52
+ private readonly defaultUseFewShotPrompt;
53
+ private readonly dispatcher;
54
+ constructor(options: ReadRouterOptions);
55
+ decide(query: string, evidence: readonly string[], options?: ReadRouterDecideOptions): Promise<ReadRouterDecision>;
56
+ decideAndDispatch<TOutcome, TPayload = undefined>(query: string, evidence: readonly string[], dispatchPayload?: TPayload, options?: ReadRouterDecideOptions): Promise<ReadRouterDispatchedResult<TOutcome>>;
57
+ }
58
+ //# sourceMappingURL=ReadRouter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReadRouter.d.ts","sourceRoot":"","sources":["../../src/read-router/ReadRouter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EACV,qBAAqB,EACrB,0BAA0B,EAC3B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAsB,KAAK,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAC5E,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK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D,QAAQ,CAAC,QAAQ,EAAE,kBAAkB,CAAC;IACtC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,cAAc,CAAC;CACnC;AAED,qBAAa,gCAAiC,SAAQ,KAAK;;CAQ1D;AAED,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAwB;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmB;IAC1C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAmB;IAChD,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAgB;IACjD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAiB;IAC5C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAE5B;IACF,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAU;IAClD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA2C;gBAE1D,OAAO,EAAE,iBAAiB;IA4BhC,MAAM,CACV,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,OAAO,CAAC,EAAE,uBAAuB,GAChC,OAAO,CAAC,kBAAkB,CAAC;IAiCxB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,EACpD,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,eAAe,CAAC,EAAE,QAAQ,EAC1B,OAAO,CAAC,EAAE,uBAAuB,GAChC,OAAO,CAAC,0BAA0B,CAAC,QAAQ,CAAC,CAAC;CAmBjD"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * @file ReadRouter.ts
3
+ * @description Top-level read-stage orchestrator composing the read-intent
4
+ * classifier with the pure {@link selectReadStrategy} into a single
5
+ * per-query routing call.
6
+ *
7
+ * @module @framers/agentos/read-router/ReadRouter
8
+ */
9
+ import { DEFAULT_READ_COSTS } from './costs.js';
10
+ import { PRESET_READ_TABLES, } from './routing-tables.js';
11
+ import { selectReadStrategy, } from './select-strategy.js';
12
+ export class ReadRouterDispatcherMissingError extends Error {
13
+ constructor() {
14
+ super('ReadRouter.decideAndDispatch requires a dispatcher. ' +
15
+ 'Either pass a dispatcher in options or call `decide` and dispatch yourself.');
16
+ this.name = 'ReadRouterDispatcherMissingError';
17
+ }
18
+ }
19
+ export class ReadRouter {
20
+ constructor(options) {
21
+ this.classifier = options.classifier;
22
+ this.preset = options.preset ?? 'precise-fact';
23
+ this.dispatcher = options.dispatcher ?? null;
24
+ const baseTable = options.routingTable ?? PRESET_READ_TABLES[this.preset];
25
+ if (options.mapping) {
26
+ const patched = {
27
+ ...baseTable.defaultMapping,
28
+ };
29
+ for (const key of Object.keys(options.mapping)) {
30
+ const ov = options.mapping[key];
31
+ if (ov)
32
+ patched[key] = ov;
33
+ }
34
+ this.routingTable = Object.freeze({
35
+ preset: baseTable.preset,
36
+ defaultMapping: Object.freeze(patched),
37
+ });
38
+ }
39
+ else {
40
+ this.routingTable = baseTable;
41
+ }
42
+ this.budgetPerReadUsd = options.budget?.perReadUsd ?? null;
43
+ this.budgetMode = options.budget?.mode ?? 'cheapest-fallback';
44
+ this.strategyCosts = options.strategyCosts ?? DEFAULT_READ_COSTS;
45
+ this.defaultUseFewShotPrompt = options.useFewShotPrompt ?? false;
46
+ }
47
+ async decide(query, evidence, options) {
48
+ let classifier;
49
+ if (options?.manualIntent) {
50
+ classifier = {
51
+ intent: options.manualIntent,
52
+ tokensIn: 0,
53
+ tokensOut: 0,
54
+ model: 'manual',
55
+ };
56
+ }
57
+ else {
58
+ const useFewShot = options?.useFewShotPrompt ?? this.defaultUseFewShotPrompt;
59
+ classifier = await this.classifier.classify(query, evidence, useFewShot ? { useFewShotPrompt: true } : undefined);
60
+ }
61
+ const routing = selectReadStrategy({
62
+ predictedIntent: classifier.intent,
63
+ groundTruthIntent: options?.groundTruthIntent ?? null,
64
+ config: {
65
+ table: this.routingTable,
66
+ budgetPerReadUsd: this.budgetPerReadUsd,
67
+ budgetMode: this.budgetMode,
68
+ strategyCosts: this.strategyCosts,
69
+ },
70
+ });
71
+ return { classifier, routing };
72
+ }
73
+ async decideAndDispatch(query, evidence, dispatchPayload, options) {
74
+ if (!this.dispatcher) {
75
+ throw new ReadRouterDispatcherMissingError();
76
+ }
77
+ const decision = await this.decide(query, evidence, options);
78
+ const dispatched = (await this.dispatcher.dispatch({
79
+ strategy: decision.routing.chosenStrategy,
80
+ query,
81
+ evidence,
82
+ payload: dispatchPayload,
83
+ }));
84
+ return {
85
+ decision,
86
+ outcome: dispatched.outcome,
87
+ strategy: dispatched.strategy,
88
+ };
89
+ }
90
+ }
91
+ //# sourceMappingURL=ReadRouter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReadRouter.js","sourceRoot":"","sources":["../../src/read-router/ReadRouter.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,OAAO,EAAE,kBAAkB,EAA8B,MAAM,YAAY,CAAC;AAC5E,OAAO,EACL,kBAAkB,GAKnB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,kBAAkB,GAGnB,MAAM,sBAAsB,CAAC;AAyC9B,MAAM,OAAO,gCAAiC,SAAQ,KAAK;IACzD;QACE,KAAK,CACH,sDAAsD;YACpD,6EAA6E,CAChF,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,kCAAkC,CAAC;IACjD,CAAC;CACF;AAED,MAAM,OAAO,UAAU;IAYrB,YAAY,OAA0B;QACpC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QACrC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,cAAc,CAAC;QAC/C,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,IAAI,CAAC;QAE7C,MAAM,SAAS,GAAG,OAAO,CAAC,YAAY,IAAI,kBAAkB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1E,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;YACpB,MAAM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as-judge classifier that maps a query+evidence pair
4
+ * to one of the five {@link ReadIntent} values.
5
+ *
6
+ * @module @framers/agentos/read-router/classifier
7
+ */
8
+ import { type ReadIntent } from './routing-tables.js';
9
+ export interface ReadIntentClassifierLLMRequest {
10
+ readonly system: string;
11
+ readonly user: string;
12
+ readonly maxTokens: number;
13
+ readonly temperature: number;
14
+ }
15
+ export interface ReadIntentClassifierLLMResponse {
16
+ readonly text: string;
17
+ readonly tokensIn: number;
18
+ readonly tokensOut: number;
19
+ readonly model: string;
20
+ }
21
+ export interface IReadIntentClassifierLLM {
22
+ invoke(request: ReadIntentClassifierLLMRequest): Promise<ReadIntentClassifierLLMResponse>;
23
+ }
24
+ export interface ReadIntentClassifierClassifyOptions {
25
+ readonly useFewShotPrompt?: boolean;
26
+ }
27
+ export interface ReadIntentClassifierResult {
28
+ readonly intent: ReadIntent;
29
+ readonly tokensIn: number;
30
+ readonly tokensOut: number;
31
+ readonly model: string;
32
+ }
33
+ export interface IReadIntentClassifier {
34
+ classify(query: string, evidence: readonly string[], options?: ReadIntentClassifierClassifyOptions): Promise<ReadIntentClassifierResult>;
35
+ }
36
+ export declare const READ_INTENT_CLASSIFIER_SYSTEM_PROMPT = "You are classifying a user query (with retrieved evidence) into one of five read intents.\n\nReturn ONLY the intent token (no explanation, no quotes, no punctuation).\n\nIntents:\n- precise-fact: the user wants a specific named entity, number, date, or fact. Examples: \"What is X's email?\", \"When was the last release?\"\n- multi-source-synthesis: the answer requires combining information from multiple evidence chunks. Examples: \"Summarize all the topics we discussed\", \"Aggregate counts across sessions.\"\n- time-interval: the query asks about durations, orderings, or \"how long ago / before / after\" timing. Examples: \"How many days since X?\", \"In what order did Y, Z, W happen?\"\n- preference-recommendation: the user wants tips, suggestions, advice, or recommendations grounded in their own context. Examples: \"Any tips for X?\", \"Can you suggest Y?\"\n- abstention-candidate: the question may be unanswerable from the evidence. Adversarial or off-topic. Examples: \"Where did I move to last year?\" when no move is in evidence.";
37
+ export declare const READ_INTENT_CLASSIFIER_SYSTEM_PROMPT_FEWSHOT = "You are classifying a user query (with retrieved evidence) into one of five read intents.\n\nReturn ONLY the intent token (no explanation, no quotes, no punctuation).\n\nIntents:\n- precise-fact: a specific named entity, number, date, or fact.\n- multi-source-synthesis: combining information across multiple evidence chunks.\n- time-interval: durations, orderings, or \"how long ago\".\n- preference-recommendation: tips / suggestions / advice grounded in user context.\n- abstention-candidate: question may be unanswerable from evidence.\n\nExamples:\n\nQuestion: What was my final boss's name in the game I played last week?\nIntent: precise-fact\n\nQuestion: What topics did we discuss across our last five conversations?\nIntent: multi-source-synthesis\n\nQuestion: How many weeks have passed since I started the new job?\nIntent: time-interval\n\nQuestion: Any tips for improving my morning routine?\nIntent: preference-recommendation\n\nQuestion: Where did I move to four years ago?\nIntent: abstention-candidate";
38
+ export declare const SAFE_READ_INTENT_FALLBACK: ReadIntent;
39
+ export declare function normalizeReadIntentClassifierOutput(raw: string): string;
40
+ export declare function parseReadIntentClassifierOutput(raw: string): ReadIntent;
41
+ export interface LLMReadIntentClassifierOptions {
42
+ readonly llm: IReadIntentClassifierLLM;
43
+ readonly maxTokens?: number;
44
+ /** Max evidence-preview chars forwarded to the classifier. Default 600. */
45
+ readonly maxEvidenceChars?: number;
46
+ }
47
+ export declare class LLMReadIntentClassifier implements IReadIntentClassifier {
48
+ private readonly llm;
49
+ private readonly maxTokens;
50
+ private readonly maxEvidenceChars;
51
+ constructor(options: LLMReadIntentClassifierOptions);
52
+ classify(query: string, evidence: readonly string[], options?: ReadIntentClassifierClassifyOptions): Promise<ReadIntentClassifierResult>;
53
+ }
54
+ //# sourceMappingURL=classifier.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classifier.d.ts","sourceRoot":"","sources":["../../src/read-router/classifier.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAMpE,MAAM,WAAW,8BAA8B;IAC7C,QAAQ,CAAC,MA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