@directive-run/ai 1.13.0 → 1.14.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/anthropic.d.cts +1 -1
- package/dist/anthropic.d.ts +1 -1
- package/dist/{chunk-XV2QSBBE.cjs → chunk-2TD4ZSGZ.cjs} +7 -7
- package/dist/{chunk-XV2QSBBE.cjs.map → chunk-2TD4ZSGZ.cjs.map} +1 -1
- package/dist/chunk-5JQ2A3JK.js +2 -0
- package/dist/chunk-5JQ2A3JK.js.map +1 -0
- package/dist/chunk-A22KLB23.cjs +67 -0
- package/dist/chunk-A22KLB23.cjs.map +1 -0
- package/dist/chunk-A5K77UDX.cjs +4 -0
- package/dist/chunk-A5K77UDX.cjs.map +1 -0
- package/dist/chunk-DN5NAJM6.js +6 -0
- package/dist/chunk-DN5NAJM6.js.map +1 -0
- package/dist/chunk-FABDFT74.cjs +6 -0
- package/dist/chunk-FABDFT74.cjs.map +1 -0
- package/dist/chunk-FBT73WFY.js +2 -0
- package/dist/chunk-FBT73WFY.js.map +1 -0
- package/dist/chunk-FFRWQNK7.cjs +7 -0
- package/dist/chunk-FFRWQNK7.cjs.map +1 -0
- package/dist/chunk-IR3IHBVQ.cjs +2 -0
- package/dist/chunk-IR3IHBVQ.cjs.map +1 -0
- package/dist/chunk-IUGSMTBE.js +16 -0
- package/dist/{chunk-Q3PQLWBR.js.map → chunk-IUGSMTBE.js.map} +1 -1
- package/dist/chunk-J2Q5KKPN.js +37 -0
- package/dist/chunk-J2Q5KKPN.js.map +1 -0
- package/dist/chunk-K64WKZ22.cjs +2 -0
- package/dist/chunk-K64WKZ22.cjs.map +1 -0
- package/dist/chunk-LKY4K5TV.cjs +11 -0
- package/dist/chunk-LKY4K5TV.cjs.map +1 -0
- package/dist/chunk-LXUMJKGJ.js +67 -0
- package/dist/chunk-LXUMJKGJ.js.map +1 -0
- package/dist/chunk-NNAQ4ZH2.js +11 -0
- package/dist/chunk-NNAQ4ZH2.js.map +1 -0
- package/dist/chunk-PD772MSE.cjs +30 -0
- package/dist/chunk-PD772MSE.cjs.map +1 -0
- package/dist/chunk-POBEEJR6.js +30 -0
- package/dist/chunk-POBEEJR6.js.map +1 -0
- package/dist/chunk-QRXWLD6H.js +4 -0
- package/dist/chunk-QRXWLD6H.js.map +1 -0
- package/dist/chunk-UR4ZGO7V.js +7 -0
- package/dist/chunk-UR4ZGO7V.js.map +1 -0
- package/dist/chunk-UR5BMWEN.js +2 -0
- package/dist/chunk-UR5BMWEN.js.map +1 -0
- package/dist/chunk-WOFIBIPW.cjs +2 -0
- package/dist/chunk-WOFIBIPW.cjs.map +1 -0
- package/dist/chunk-XN5LUOVS.cjs +37 -0
- package/dist/chunk-XN5LUOVS.cjs.map +1 -0
- package/dist/debug-timeline-DpnRMnLU.d.cts +87 -0
- package/dist/debug-timeline-L13P-U2I.d.ts +87 -0
- package/dist/devtools.cjs +2 -0
- package/dist/devtools.cjs.map +1 -0
- package/dist/devtools.d.cts +354 -0
- package/dist/devtools.d.ts +354 -0
- package/dist/devtools.js +2 -0
- package/dist/devtools.js.map +1 -0
- package/dist/evals.cjs +2 -0
- package/dist/evals.cjs.map +1 -0
- package/dist/evals.d.cts +361 -0
- package/dist/evals.d.ts +361 -0
- package/dist/evals.js +2 -0
- package/dist/evals.js.map +1 -0
- package/dist/gemini.d.cts +1 -1
- package/dist/gemini.d.ts +1 -1
- package/dist/guardrails.cjs +2 -0
- package/dist/guardrails.cjs.map +1 -0
- package/dist/guardrails.d.cts +618 -0
- package/dist/guardrails.d.ts +618 -0
- package/dist/guardrails.js +2 -0
- package/dist/guardrails.js.map +1 -0
- package/dist/health-monitor-C6xoXrQz.d.cts +55 -0
- package/dist/health-monitor-qL9RNMH3.d.ts +55 -0
- package/dist/index.cjs +21 -99
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1197 -4735
- package/dist/index.d.ts +1197 -4735
- package/dist/index.js +21 -99
- package/dist/index.js.map +1 -1
- package/dist/mcp.cjs +2 -0
- package/dist/mcp.cjs.map +1 -0
- package/dist/mcp.d.cts +450 -0
- package/dist/mcp.d.ts +450 -0
- package/dist/mcp.js +2 -0
- package/dist/mcp.js.map +1 -0
- package/dist/multi-agent-orchestrator-QWWQKKGX.js +2 -0
- package/dist/{multi-agent-orchestrator-4PXNYRHB.js.map → multi-agent-orchestrator-QWWQKKGX.js.map} +1 -1
- package/dist/multi-agent-orchestrator-Y5U4JCFO.cjs +2 -0
- package/dist/{multi-agent-orchestrator-KFGTEGE5.cjs.map → multi-agent-orchestrator-Y5U4JCFO.cjs.map} +1 -1
- package/dist/multi-agent.cjs +2 -0
- package/dist/multi-agent.cjs.map +1 -0
- package/dist/multi-agent.d.cts +1429 -0
- package/dist/multi-agent.d.ts +1429 -0
- package/dist/multi-agent.js +2 -0
- package/dist/multi-agent.js.map +1 -0
- package/dist/ollama.d.cts +1 -1
- package/dist/ollama.d.ts +1 -1
- package/dist/openai.d.cts +2 -2
- package/dist/openai.d.ts +2 -2
- package/dist/{orchestrator-types-CTfIKk0W.d.ts → orchestrator-types-DGBhL6mc.d.ts} +5 -138
- package/dist/{orchestrator-types-Bh8r3_Sq.d.cts → orchestrator-types-DeIMRLR7.d.cts} +5 -138
- package/dist/predicate.cjs +2 -0
- package/dist/predicate.cjs.map +1 -0
- package/dist/predicate.d.cts +371 -0
- package/dist/predicate.d.ts +371 -0
- package/dist/predicate.js +2 -0
- package/dist/predicate.js.map +1 -0
- package/dist/{semantic-cache-nBpQqILc.d.cts → semantic-cache-DM7ev7NQ.d.cts} +1 -1
- package/dist/{semantic-cache-nBpQqILc.d.ts → semantic-cache-DM7ev7NQ.d.ts} +1 -1
- package/dist/testing.cjs +1 -1
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +4 -2
- package/dist/testing.d.ts +4 -2
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/{types-CRmwFnVk.d.cts → types-DJ09LjZX.d.cts} +1 -1
- package/dist/{types-CRmwFnVk.d.ts → types-DJ09LjZX.d.ts} +1 -1
- package/package.json +32 -2
- package/dist/chunk-Q3PQLWBR.js +0 -16
- package/dist/chunk-RW4R3O5P.js +0 -72
- package/dist/chunk-RW4R3O5P.js.map +0 -1
- package/dist/chunk-X3VQ5F7D.cjs +0 -72
- package/dist/chunk-X3VQ5F7D.cjs.map +0 -1
- package/dist/multi-agent-orchestrator-4PXNYRHB.js +0 -2
- package/dist/multi-agent-orchestrator-KFGTEGE5.cjs +0 -2
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { FactPredicate, SchemaValidationError } from '@directive-run/core';
|
|
2
|
+
import { c as AgentRunner, b as AgentLike } from './types-DJ09LjZX.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* predicateFromIntent — let an LLM emit a typed FactPredicate as JSON,
|
|
6
|
+
* structurally + semantically validated before it ever reaches your
|
|
7
|
+
* constraint engine.
|
|
8
|
+
*
|
|
9
|
+
* Pipeline per call attempt:
|
|
10
|
+
*
|
|
11
|
+
* 1. Output-size check (reject before JSON.parse for DoS guard)
|
|
12
|
+
* 2. JSON.parse via extractJsonFromOutput (handles surrounding prose)
|
|
13
|
+
* 3. validatePredicate (structural: closed operator set, depth, JSON safety)
|
|
14
|
+
* 4. Operator-count check
|
|
15
|
+
* 5. validatePredicateAgainstSchema (semantic: operator-on-kind matrix)
|
|
16
|
+
*
|
|
17
|
+
* On any failure: a structured error message — including the offending
|
|
18
|
+
* clause's path, the allowed operators for that fact's kind, and the
|
|
19
|
+
* original schema kinds — is fed back to the LLM in the next attempt.
|
|
20
|
+
*
|
|
21
|
+
* Returns the validated FactPredicate. Throws PredicateFromIntentError
|
|
22
|
+
* on retry exhaustion. NEVER returns a partial / unvalidated predicate.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
interface PredicateFromIntentOptions<_F = Record<string, unknown>> {
|
|
26
|
+
/** Natural-language intent (untrusted user input — sanitize via `redact`). */
|
|
27
|
+
intent: string;
|
|
28
|
+
/**
|
|
29
|
+
* Module schema (must expose builders with `_typeName` or `_kind`).
|
|
30
|
+
* Pass either `{ facts: {...} }` or a bare `Record<string, builder>`.
|
|
31
|
+
*/
|
|
32
|
+
schema: unknown;
|
|
33
|
+
/** AgentRunner from `@directive-run/ai` adapters (createOpenAIRunner, etc.). */
|
|
34
|
+
runner: AgentRunner;
|
|
35
|
+
/**
|
|
36
|
+
* Optional agent override. Default is `{ name: "predicate-emitter" }`
|
|
37
|
+
* with our system prompt; pass `instructions` to append additional
|
|
38
|
+
* context.
|
|
39
|
+
*/
|
|
40
|
+
agent?: AgentLike;
|
|
41
|
+
/**
|
|
42
|
+
* Optional dotted-path namespace; useful for cross-module systems
|
|
43
|
+
* where the LLM should emit a predicate over `auth.token` (default:
|
|
44
|
+
* the schema's root facts).
|
|
45
|
+
*/
|
|
46
|
+
factPath?: string;
|
|
47
|
+
/** Max retries on validation failure. Default 3. */
|
|
48
|
+
maxRetries?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Hard byte cap on the LLM's raw output, BEFORE JSON.parse. Defaults
|
|
51
|
+
* to 64 KiB. A larger predicate is rejected outright; protects
|
|
52
|
+
* against multi-MB-payload DoS where the predicate is technically
|
|
53
|
+
* structurally valid.
|
|
54
|
+
*/
|
|
55
|
+
maxPredicateBytes?: number;
|
|
56
|
+
/**
|
|
57
|
+
* Hard cap on the number of operator clauses in the predicate.
|
|
58
|
+
* Defaults to 256. Protects against `{ $any: [{ x: 1 }, … x100,000 ] }`
|
|
59
|
+
* style operator-count exhaustion.
|
|
60
|
+
*/
|
|
61
|
+
maxOperatorCount?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Optional sanitizer applied to `intent` BEFORE it lands in the
|
|
64
|
+
* system prompt. Useful for stripping or redacting user-controlled
|
|
65
|
+
* content that looks like prompt-injection.
|
|
66
|
+
*/
|
|
67
|
+
redact?: (intent: string) => string;
|
|
68
|
+
/**
|
|
69
|
+
* Hard cap on the length of each `$in` / `$nin` array operand the LLM
|
|
70
|
+
* may emit. Defaults to 1000. Forwarded to
|
|
71
|
+
* `validatePredicateAgainstSchema`'s `maxArrayOperandLength`.
|
|
72
|
+
*/
|
|
73
|
+
maxArrayOperandLength?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Optional `AbortSignal` for cooperative cancellation. (N6)
|
|
76
|
+
*
|
|
77
|
+
* The retry loop checks `signal.aborted` between attempts AND forwards
|
|
78
|
+
* the signal into the runner call itself (`runner(agent, input, { signal })`).
|
|
79
|
+
* Whether the in-flight LLM call honors the signal depends on the runner:
|
|
80
|
+
* fetch-based adapters (the bundled OpenAI / Anthropic / Ollama runners)
|
|
81
|
+
* thread it through to `fetch`, so the network call aborts mid-stream.
|
|
82
|
+
* A custom runner that ignores the signal still delivers cancellation
|
|
83
|
+
* at the next retry boundary.
|
|
84
|
+
*
|
|
85
|
+
* On abort: throws `Error("aborted")`.
|
|
86
|
+
*
|
|
87
|
+
* NOTE: `predicateFromIntent` does NOT limit in-flight calls — callers
|
|
88
|
+
* MUST wrap with a concurrency limiter (e.g. `p-limit`) to bound
|
|
89
|
+
* fan-out under load.
|
|
90
|
+
*/
|
|
91
|
+
signal?: AbortSignal;
|
|
92
|
+
/**
|
|
93
|
+
* When `true`, the {@link PredicateFromIntentProvenance} returned by
|
|
94
|
+
* `predicateFromIntentWithProvenance` omits the raw `intent` string and
|
|
95
|
+
* stores only the SHA-256 `intentHash`. Use this in PII-sensitive
|
|
96
|
+
* contexts where the original intent must not be persisted. (M6)
|
|
97
|
+
*
|
|
98
|
+
* **Default `false` for back-compat.** For PII-sensitive deployments,
|
|
99
|
+
* ALWAYS set `redactIntent: true` — the raw intent may contain
|
|
100
|
+
* user-supplied content (names, emails, medical or financial details,
|
|
101
|
+
* customer messages) that becomes a permanent record in
|
|
102
|
+
* `provenance.intent`. The default is opt-in only because flipping it
|
|
103
|
+
* now would silently strip diagnostic data from existing callers; v2
|
|
104
|
+
* may flip this default (tracked in IDEAS.md).
|
|
105
|
+
*
|
|
106
|
+
* Pair with a `redact:` sanitizer when the intent itself must be
|
|
107
|
+
* scrubbed BEFORE it lands in the LLM prompt — `redactIntent` controls
|
|
108
|
+
* only what enters the provenance record, not what the LLM sees.
|
|
109
|
+
*/
|
|
110
|
+
redactIntent?: boolean;
|
|
111
|
+
}
|
|
112
|
+
interface PredicateFromIntentDiagnostics<F = Record<string, unknown>> {
|
|
113
|
+
/** The validated predicate (`null` if all retries failed). */
|
|
114
|
+
predicate: FactPredicate<F> | null;
|
|
115
|
+
/** Number of LLM calls actually made. */
|
|
116
|
+
attempts: number;
|
|
117
|
+
/** Errors encountered across all attempts (most recent last). */
|
|
118
|
+
errors: ReadonlyArray<{
|
|
119
|
+
attempt: number;
|
|
120
|
+
reason: string;
|
|
121
|
+
details?: readonly SchemaValidationError[];
|
|
122
|
+
}>;
|
|
123
|
+
/** The raw LLM output from the final attempt — useful for debugging. */
|
|
124
|
+
lastRawOutput?: string;
|
|
125
|
+
}
|
|
126
|
+
/** Thrown by `predicateFromIntent` on retry exhaustion. `predicateFromIntentRaw` returns these as a diagnostics payload instead. */
|
|
127
|
+
declare class PredicateFromIntentError extends Error {
|
|
128
|
+
readonly attempts: number;
|
|
129
|
+
readonly errors: ReadonlyArray<{
|
|
130
|
+
attempt: number;
|
|
131
|
+
reason: string;
|
|
132
|
+
details?: readonly SchemaValidationError[];
|
|
133
|
+
}>;
|
|
134
|
+
readonly lastRawOutput: string | undefined;
|
|
135
|
+
readonly name = "PredicateFromIntentError";
|
|
136
|
+
constructor(message: string, attempts: number, errors: ReadonlyArray<{
|
|
137
|
+
attempt: number;
|
|
138
|
+
reason: string;
|
|
139
|
+
details?: readonly SchemaValidationError[];
|
|
140
|
+
}>, lastRawOutput: string | undefined);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Ask an LLM to emit a FactPredicate matching the user's intent, then
|
|
144
|
+
* validate it structurally + semantically before returning. On validation
|
|
145
|
+
* failure, retries with structured error feedback in the next prompt.
|
|
146
|
+
*
|
|
147
|
+
* Throws {@link PredicateFromIntentError} on retry exhaustion. NEVER
|
|
148
|
+
* returns a partial / unvalidated predicate.
|
|
149
|
+
*
|
|
150
|
+
* **Rate limiting:** this function does NOT limit in-flight calls. Wrap
|
|
151
|
+
* with a concurrency limiter (e.g. `p-limit` / `Bottleneck`) before
|
|
152
|
+
* exposing it to user-driven traffic. Pass an `AbortSignal` via `opts.signal`
|
|
153
|
+
* for cooperative cancellation between retry attempts.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { createOpenAIRunner } from "@directive-run/ai/openai";
|
|
158
|
+
* import { predicateFromIntent } from "@directive-run/ai";
|
|
159
|
+
*
|
|
160
|
+
* const runner = createOpenAIRunner({ apiKey, model: "gpt-4o-mini" });
|
|
161
|
+
*
|
|
162
|
+
* const predicate = await predicateFromIntent({
|
|
163
|
+
* intent: "checkout is unblocked when the cart total is at least 50",
|
|
164
|
+
* schema: myModule.schema,
|
|
165
|
+
* runner,
|
|
166
|
+
* });
|
|
167
|
+
* // → { cartTotal: { $gte: 50 } }
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
declare function predicateFromIntent<F = Record<string, unknown>>(opts: PredicateFromIntentOptions<F>): Promise<FactPredicate<F>>;
|
|
171
|
+
/**
|
|
172
|
+
* Lower-level variant — returns the validated predicate (or null) plus
|
|
173
|
+
* full diagnostics. Use when you want to surface validation telemetry,
|
|
174
|
+
* preview the LLM's last raw output, or display per-attempt errors in
|
|
175
|
+
* a UI.
|
|
176
|
+
*/
|
|
177
|
+
declare function predicateFromIntentRaw<F = Record<string, unknown>>(opts: PredicateFromIntentOptions<F>): Promise<PredicateFromIntentDiagnostics<F>>;
|
|
178
|
+
interface PredicateToolSpecOptions {
|
|
179
|
+
/** Tool name. Default `"emit_predicate"`. */
|
|
180
|
+
name?: string;
|
|
181
|
+
/** Tool description. Default: a one-liner describing predicate emission. */
|
|
182
|
+
description?: string;
|
|
183
|
+
/** Optional dotted-path namespace to restrict the tool's scope. */
|
|
184
|
+
factPath?: string;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Anthropic Messages API tool shape. Drop into the `tools: [...]` array.
|
|
188
|
+
* Anthropic's API expects `input_schema` at the top level of the tool.
|
|
189
|
+
*/
|
|
190
|
+
interface PredicateToolSpecAnthropic {
|
|
191
|
+
name: string;
|
|
192
|
+
description: string;
|
|
193
|
+
input_schema: {
|
|
194
|
+
type: "object";
|
|
195
|
+
properties: {
|
|
196
|
+
predicate: {
|
|
197
|
+
type: "object";
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
required: ["predicate"];
|
|
201
|
+
};
|
|
202
|
+
/** Human-readable schema description — embed in your tool's "description" if your provider concatenates them. */
|
|
203
|
+
schemaSummary: string;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* OpenAI Chat Completions / Responses API tool shape. Drop into the
|
|
207
|
+
* `tools: [...]` array. OpenAI nests the function payload under
|
|
208
|
+
* `function: { name, description, parameters }`.
|
|
209
|
+
*/
|
|
210
|
+
interface PredicateToolSpecOpenAI {
|
|
211
|
+
type: "function";
|
|
212
|
+
function: {
|
|
213
|
+
name: string;
|
|
214
|
+
description: string;
|
|
215
|
+
parameters: {
|
|
216
|
+
type: "object";
|
|
217
|
+
properties: {
|
|
218
|
+
predicate: {
|
|
219
|
+
type: "object";
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
required: ["predicate"];
|
|
223
|
+
};
|
|
224
|
+
};
|
|
225
|
+
/** Human-readable schema description — embed in your tool's "description" if your provider concatenates them. */
|
|
226
|
+
schemaSummary: string;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* @deprecated Use {@link PredicateToolSpecAnthropic} (or
|
|
230
|
+
* {@link PredicateToolSpecOpenAI}) directly. Kept as an alias for the
|
|
231
|
+
* pre-split shape that v1.12.x callers depend on; identical to
|
|
232
|
+
* `PredicateToolSpecAnthropic`.
|
|
233
|
+
*/
|
|
234
|
+
type PredicateToolSpec = PredicateToolSpecAnthropic;
|
|
235
|
+
/**
|
|
236
|
+
* OpenAI Chat Completions / Responses API tool spec for predicate
|
|
237
|
+
* emission. Drop the result into your `tools: [...]` array; the model
|
|
238
|
+
* will be told to emit a predicate matching this schema.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* const tool = predicateToolSpecOpenAI(myModule.schema, { name: "set_checkout_rule" });
|
|
243
|
+
*
|
|
244
|
+
* await openai.chat.completions.create({
|
|
245
|
+
* model: "gpt-4o-mini",
|
|
246
|
+
* tools: [tool],
|
|
247
|
+
* messages: [...],
|
|
248
|
+
* });
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
declare function predicateToolSpecOpenAI(schema: unknown, opts?: PredicateToolSpecOptions): PredicateToolSpecOpenAI;
|
|
252
|
+
/**
|
|
253
|
+
* Anthropic Messages API tool spec for predicate emission. Drop the
|
|
254
|
+
* result into your `tools: [...]` array; the model will be told to emit
|
|
255
|
+
* a predicate matching this schema.
|
|
256
|
+
*
|
|
257
|
+
* @example
|
|
258
|
+
* ```ts
|
|
259
|
+
* const tool = predicateToolSpecAnthropic(myModule.schema, { name: "set_checkout_rule" });
|
|
260
|
+
*
|
|
261
|
+
* await anthropic.messages.create({
|
|
262
|
+
* model: "claude-3-5-sonnet-latest",
|
|
263
|
+
* tools: [tool],
|
|
264
|
+
* messages: [...],
|
|
265
|
+
* });
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
declare function predicateToolSpecAnthropic(schema: unknown, opts?: PredicateToolSpecOptions): PredicateToolSpecAnthropic;
|
|
269
|
+
/**
|
|
270
|
+
* @deprecated Use {@link predicateToolSpecAnthropic} (or
|
|
271
|
+
* {@link predicateToolSpecOpenAI}) directly. Back-compat alias for
|
|
272
|
+
* v1.12.x callers — emits the Anthropic shape, unchanged.
|
|
273
|
+
*/
|
|
274
|
+
declare function predicateToolSpec(schema: unknown, opts?: PredicateToolSpecOptions): PredicateToolSpec;
|
|
275
|
+
interface PredicateFromIntentProvenance {
|
|
276
|
+
/**
|
|
277
|
+
* Resolved model name when the caller passes `agent: { model }`,
|
|
278
|
+
* otherwise `"unknown"`.
|
|
279
|
+
*
|
|
280
|
+
* NOTE: most callers omit `agent` (the default `predicate-emitter`
|
|
281
|
+
* agent has no model field set), so `model` is `"unknown"` by default.
|
|
282
|
+
* v1 does NOT read provider-detected model strings from `RunResult` —
|
|
283
|
+
* that requires every adapter to surface `runResult.model`, which the
|
|
284
|
+
* current `AgentRunner` contract does not. Tracked for v2; callers who
|
|
285
|
+
* need provider attribution today should pass `agent: { name, model: "..." }`.
|
|
286
|
+
*/
|
|
287
|
+
readonly model: string;
|
|
288
|
+
/**
|
|
289
|
+
* Sanitized intent string actually sent to the LLM (post-redact). When
|
|
290
|
+
* `redactIntent: true` was passed, this field is omitted entirely — only
|
|
291
|
+
* `intentHash` is populated.
|
|
292
|
+
*/
|
|
293
|
+
readonly intent?: string;
|
|
294
|
+
/**
|
|
295
|
+
* SHA-256 hex hash of the sanitized intent string (or djb2 fallback
|
|
296
|
+
* when `crypto.subtle` is unavailable). Always present — even when
|
|
297
|
+
* `intent` is omitted via `redactIntent`. (M6)
|
|
298
|
+
*/
|
|
299
|
+
readonly intentHash: string;
|
|
300
|
+
/** Number of LLM calls that ran before the final predicate was accepted. */
|
|
301
|
+
readonly attemptCount: number;
|
|
302
|
+
/** ISO timestamp when the predicate was returned. */
|
|
303
|
+
readonly emittedAt: string;
|
|
304
|
+
/**
|
|
305
|
+
* Hash of the VALIDATED predicate (canonicalized via stableStringify
|
|
306
|
+
* before hashing). Two semantically-identical predicates emitted with
|
|
307
|
+
* different whitespace or key order produce the SAME `predicateHash`.
|
|
308
|
+
* Sufficient as a tamper-evident pointer alongside the persisted
|
|
309
|
+
* predicate. (N3)
|
|
310
|
+
*
|
|
311
|
+
* Renamed from `rawOutputHash` in v1.13.x — the old name hashed the
|
|
312
|
+
* raw LLM output string, which made two whitespace-different responses
|
|
313
|
+
* for the same logical predicate hash differently. Callers persisting
|
|
314
|
+
* the old `rawOutputHash` value should re-derive it from the stored
|
|
315
|
+
* predicate using `hashObject(predicate)` from `@directive-run/core/internals`.
|
|
316
|
+
*/
|
|
317
|
+
readonly predicateHash: string;
|
|
318
|
+
}
|
|
319
|
+
interface PredicateFromIntentWithProvenanceResult<F = Record<string, unknown>> {
|
|
320
|
+
readonly predicate: FactPredicate<F>;
|
|
321
|
+
readonly provenance: PredicateFromIntentProvenance;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Like {@link predicateFromIntent} but additionally returns a structured
|
|
325
|
+
* provenance record — the model name, sanitized intent (or its hash),
|
|
326
|
+
* attempt count, timestamp, and a canonical hash of the validated
|
|
327
|
+
* predicate.
|
|
328
|
+
*
|
|
329
|
+
* **Production deployments MUST persist the provenance record alongside
|
|
330
|
+
* the predicate.** Without it, auditing "where did this rule come from?"
|
|
331
|
+
* becomes guesswork.
|
|
332
|
+
*
|
|
333
|
+
* Throws {@link PredicateFromIntentError} on retry exhaustion — same
|
|
334
|
+
* semantics as the un-provenanced variant.
|
|
335
|
+
*
|
|
336
|
+
* **PII guidance (M6):** pass `redactIntent: true` to omit the raw
|
|
337
|
+
* intent from the provenance record and persist only the `intentHash`.
|
|
338
|
+
* Useful when the intent itself is sensitive (medical, financial,
|
|
339
|
+
* customer messages, etc.).
|
|
340
|
+
*
|
|
341
|
+
* **Hash semantics (N3):**
|
|
342
|
+
* - `predicateHash` hashes the VALIDATED predicate object via stable
|
|
343
|
+
* stringification — two whitespace-different LLM outputs that parse to
|
|
344
|
+
* the same predicate produce the same hash.
|
|
345
|
+
* - `intentHash` hashes the sanitized intent STRING (SHA-256 when
|
|
346
|
+
* available, djb2 fallback).
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* const { predicate, provenance } = await predicateFromIntentWithProvenance({
|
|
351
|
+
* intent: "block checkout when cart > 10k",
|
|
352
|
+
* schema: checkoutModule.schema,
|
|
353
|
+
* runner,
|
|
354
|
+
* agent: { name: "predicate-emitter", model: "gpt-4o-mini" },
|
|
355
|
+
* redactIntent: false, // default: store both intent + intentHash
|
|
356
|
+
* });
|
|
357
|
+
*
|
|
358
|
+
* await db.predicates.insert({
|
|
359
|
+
* predicate,
|
|
360
|
+
* model: provenance.model,
|
|
361
|
+
* intent: provenance.intent, // omitted when redactIntent: true
|
|
362
|
+
* intentHash: provenance.intentHash,
|
|
363
|
+
* emittedAt: provenance.emittedAt,
|
|
364
|
+
* predicateHash: provenance.predicateHash,
|
|
365
|
+
* attempts: provenance.attemptCount,
|
|
366
|
+
* });
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
declare function predicateFromIntentWithProvenance<F = Record<string, unknown>>(opts: PredicateFromIntentOptions<F>): Promise<PredicateFromIntentWithProvenanceResult<F>>;
|
|
370
|
+
|
|
371
|
+
export { type PredicateFromIntentDiagnostics, PredicateFromIntentError, type PredicateFromIntentOptions, type PredicateFromIntentProvenance, type PredicateFromIntentWithProvenanceResult, type PredicateToolSpec, type PredicateToolSpecAnthropic, type PredicateToolSpecOpenAI, type PredicateToolSpecOptions, predicateFromIntent, predicateFromIntentRaw, predicateFromIntentWithProvenance, predicateToolSpec, predicateToolSpecAnthropic, predicateToolSpecOpenAI };
|