@prompty/core 2.0.0-alpha.5 → 2.0.0-alpha.6
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/index.cjs +574 -236
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +286 -6
- package/dist/index.d.ts +286 -6
- package/dist/index.js +559 -236
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.d.cts
CHANGED
|
@@ -2055,6 +2055,16 @@ interface Parser {
|
|
|
2055
2055
|
*/
|
|
2056
2056
|
interface Executor {
|
|
2057
2057
|
execute(agent: Prompty, messages: Message[]): Promise<unknown>;
|
|
2058
|
+
/**
|
|
2059
|
+
* Format tool call results into provider-specific message format.
|
|
2060
|
+
* Called after tool dispatch; the pipeline provides extracted tool calls
|
|
2061
|
+
* and their string results.
|
|
2062
|
+
*/
|
|
2063
|
+
formatToolMessages(rawResponse: unknown, toolCalls: {
|
|
2064
|
+
id: string;
|
|
2065
|
+
name: string;
|
|
2066
|
+
arguments: string;
|
|
2067
|
+
}[], toolResults: string[], textContent?: string): Message[];
|
|
2058
2068
|
}
|
|
2059
2069
|
/**
|
|
2060
2070
|
* Extracts clean results from raw LLM responses.
|
|
@@ -2137,6 +2147,81 @@ declare function clearConnections(): void;
|
|
|
2137
2147
|
*/
|
|
2138
2148
|
declare function load(path: string): Prompty;
|
|
2139
2149
|
|
|
2150
|
+
/**
|
|
2151
|
+
* §13.1 Agent Loop Events — structured event callbacks.
|
|
2152
|
+
* @module
|
|
2153
|
+
*/
|
|
2154
|
+
/** Event types emitted during the agent loop. */
|
|
2155
|
+
type AgentEventType = "token" | "thinking" | "tool_call_start" | "tool_result" | "status" | "messages_updated" | "done" | "error" | "cancelled";
|
|
2156
|
+
/** Callback signature for agent loop events. */
|
|
2157
|
+
type EventCallback = (eventType: AgentEventType, data: Record<string, unknown>) => void;
|
|
2158
|
+
/**
|
|
2159
|
+
* Safely emit an event. Swallows errors from callback (spec §13.1:
|
|
2160
|
+
* event callbacks MUST NOT block the loop).
|
|
2161
|
+
*/
|
|
2162
|
+
declare function emitEvent(callback: EventCallback | undefined, eventType: AgentEventType, data: Record<string, unknown>): void;
|
|
2163
|
+
|
|
2164
|
+
/**
|
|
2165
|
+
* §13.4 Guardrails — optional validation hooks for the agent loop.
|
|
2166
|
+
* @module
|
|
2167
|
+
*/
|
|
2168
|
+
|
|
2169
|
+
/** Result of a guardrail check. */
|
|
2170
|
+
interface GuardrailResult {
|
|
2171
|
+
allowed: boolean;
|
|
2172
|
+
reason?: string;
|
|
2173
|
+
rewrite?: any;
|
|
2174
|
+
}
|
|
2175
|
+
/** Error thrown when a guardrail denies the operation. */
|
|
2176
|
+
declare class GuardrailError extends Error {
|
|
2177
|
+
reason: string;
|
|
2178
|
+
constructor(reason: string);
|
|
2179
|
+
}
|
|
2180
|
+
/** Input guardrail hook signature. */
|
|
2181
|
+
type InputGuardrail = (messages: Message[]) => GuardrailResult;
|
|
2182
|
+
/** Output guardrail hook signature. */
|
|
2183
|
+
type OutputGuardrail = (message: Message) => GuardrailResult;
|
|
2184
|
+
/** Tool guardrail hook signature. */
|
|
2185
|
+
type ToolGuardrail = (name: string, args: Record<string, unknown>) => GuardrailResult;
|
|
2186
|
+
/** Configuration for guardrail hooks. */
|
|
2187
|
+
interface GuardrailsOptions {
|
|
2188
|
+
input?: InputGuardrail;
|
|
2189
|
+
output?: OutputGuardrail;
|
|
2190
|
+
tool?: ToolGuardrail;
|
|
2191
|
+
}
|
|
2192
|
+
/**
|
|
2193
|
+
* Guardrails with input, output, and tool hooks.
|
|
2194
|
+
* All hooks are optional — when not set, execution proceeds normally.
|
|
2195
|
+
*/
|
|
2196
|
+
declare class Guardrails {
|
|
2197
|
+
private inputHook?;
|
|
2198
|
+
private outputHook?;
|
|
2199
|
+
private toolHook?;
|
|
2200
|
+
constructor(options?: GuardrailsOptions);
|
|
2201
|
+
checkInput(messages: Message[]): GuardrailResult;
|
|
2202
|
+
checkOutput(message: Message): GuardrailResult;
|
|
2203
|
+
checkTool(name: string, args: Record<string, unknown>): GuardrailResult;
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
/**
|
|
2207
|
+
* §13.5 Steering — inject user messages into a running agent loop.
|
|
2208
|
+
* @module
|
|
2209
|
+
*/
|
|
2210
|
+
|
|
2211
|
+
/**
|
|
2212
|
+
* A handle for injecting user messages into a running agent loop.
|
|
2213
|
+
* Thread-safe in the JS single-threaded model (no locking needed).
|
|
2214
|
+
*/
|
|
2215
|
+
declare class Steering {
|
|
2216
|
+
private queue;
|
|
2217
|
+
/** Enqueue a message to be injected at the next iteration. */
|
|
2218
|
+
send(message: string): void;
|
|
2219
|
+
/** Remove and return all queued messages as Message objects. */
|
|
2220
|
+
drain(): Message[];
|
|
2221
|
+
/** Whether there are pending messages without consuming them. */
|
|
2222
|
+
get hasPending(): boolean;
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2140
2225
|
/**
|
|
2141
2226
|
* Four-step execution pipeline.
|
|
2142
2227
|
*
|
|
@@ -2190,10 +2275,25 @@ declare function run(agent: Prompty, messages: Message[], options?: {
|
|
|
2190
2275
|
}): Promise<unknown>;
|
|
2191
2276
|
/**
|
|
2192
2277
|
* Full pipeline: load → prepare → run.
|
|
2278
|
+
*
|
|
2279
|
+
* @overload Untyped — returns `unknown`.
|
|
2193
2280
|
*/
|
|
2194
2281
|
declare function invoke(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: {
|
|
2195
2282
|
raw?: boolean;
|
|
2196
2283
|
}): Promise<unknown>;
|
|
2284
|
+
/**
|
|
2285
|
+
* Full pipeline with typed result: load → prepare → run → cast.
|
|
2286
|
+
*
|
|
2287
|
+
* When a `validator` is provided the raw result is deserialized from JSON
|
|
2288
|
+
* and passed through the validator (e.g. a Zod `.parse` function), giving
|
|
2289
|
+
* you a fully typed return value.
|
|
2290
|
+
*
|
|
2291
|
+
* @overload Typed — returns `Promise<T>`.
|
|
2292
|
+
*/
|
|
2293
|
+
declare function invoke<T>(prompt: string | Prompty, inputs: Record<string, unknown> | undefined, options: {
|
|
2294
|
+
raw?: boolean;
|
|
2295
|
+
validator: (data: unknown) => T;
|
|
2296
|
+
}): Promise<T>;
|
|
2197
2297
|
/**
|
|
2198
2298
|
* Resolve tool bindings: inject values from parentInputs into tool arguments.
|
|
2199
2299
|
*
|
|
@@ -2201,14 +2301,194 @@ declare function invoke(prompt: string | Prompty, inputs?: Record<string, unknow
|
|
|
2201
2301
|
* and sets `args[binding.name]` to that value. Returns a new args object.
|
|
2202
2302
|
*/
|
|
2203
2303
|
declare function resolveBindings(agent: Prompty, toolName: string, args: Record<string, unknown>, parentInputs?: Record<string, unknown>): Record<string, unknown>;
|
|
2204
|
-
/**
|
|
2205
|
-
|
|
2206
|
-
*/
|
|
2207
|
-
declare function invokeAgent(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: {
|
|
2304
|
+
/** Options for {@link invokeAgent}. */
|
|
2305
|
+
interface InvokeAgentOptions {
|
|
2208
2306
|
tools?: Record<string, (...args: unknown[]) => unknown>;
|
|
2209
2307
|
maxIterations?: number;
|
|
2210
2308
|
raw?: boolean;
|
|
2211
|
-
|
|
2309
|
+
onEvent?: EventCallback;
|
|
2310
|
+
signal?: AbortSignal;
|
|
2311
|
+
contextBudget?: number;
|
|
2312
|
+
guardrails?: Guardrails;
|
|
2313
|
+
steering?: Steering;
|
|
2314
|
+
parallelToolCalls?: boolean;
|
|
2315
|
+
}
|
|
2316
|
+
/**
|
|
2317
|
+
* Run a prompt with automatic tool-call execution loop.
|
|
2318
|
+
*
|
|
2319
|
+
* Supports §13 extensions: events, cancellation, context window
|
|
2320
|
+
* management, guardrails, steering, and parallel tool calls.
|
|
2321
|
+
*
|
|
2322
|
+
* @overload Untyped — returns `unknown`.
|
|
2323
|
+
*/
|
|
2324
|
+
declare function invokeAgent(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: InvokeAgentOptions): Promise<unknown>;
|
|
2325
|
+
/**
|
|
2326
|
+
* Run a prompt with automatic tool-call execution loop, returning a typed result.
|
|
2327
|
+
*
|
|
2328
|
+
* When a `validator` is provided in the options the final result is
|
|
2329
|
+
* deserialized from JSON and passed through the validator.
|
|
2330
|
+
*
|
|
2331
|
+
* @overload Typed — returns `Promise<T>`.
|
|
2332
|
+
*/
|
|
2333
|
+
declare function invokeAgent<T>(prompt: string | Prompty, inputs: Record<string, unknown> | undefined, options: InvokeAgentOptions & {
|
|
2334
|
+
validator: (data: unknown) => T;
|
|
2335
|
+
}): Promise<T>;
|
|
2336
|
+
|
|
2337
|
+
/**
|
|
2338
|
+
* §13.2 Cancellation — cooperative cancellation via AbortSignal.
|
|
2339
|
+
* @module
|
|
2340
|
+
*/
|
|
2341
|
+
/**
|
|
2342
|
+
* Error thrown when the agent loop is cancelled.
|
|
2343
|
+
*/
|
|
2344
|
+
declare class CancelledError extends Error {
|
|
2345
|
+
constructor(message?: string);
|
|
2346
|
+
}
|
|
2347
|
+
/**
|
|
2348
|
+
* Check if the signal is aborted, and throw CancelledError if so.
|
|
2349
|
+
*/
|
|
2350
|
+
declare function checkCancellation(signal?: AbortSignal): void;
|
|
2351
|
+
|
|
2352
|
+
/**
|
|
2353
|
+
* §13.3 Context Window Management — trimming and summarization.
|
|
2354
|
+
* @module
|
|
2355
|
+
*/
|
|
2356
|
+
|
|
2357
|
+
/**
|
|
2358
|
+
* Estimate the character cost of a message list.
|
|
2359
|
+
* Per spec §13.3: role + 4 overhead per message, text parts by length,
|
|
2360
|
+
* non-text parts at 200-char estimate.
|
|
2361
|
+
*/
|
|
2362
|
+
declare function estimateChars(messages: Message[]): number;
|
|
2363
|
+
/**
|
|
2364
|
+
* Build a compact string summary from dropped messages.
|
|
2365
|
+
*/
|
|
2366
|
+
declare function summarizeDropped(messages: Message[]): string;
|
|
2367
|
+
/**
|
|
2368
|
+
* Trim messages in-place to fit within a character budget.
|
|
2369
|
+
* Returns [droppedCount, droppedMessages].
|
|
2370
|
+
*/
|
|
2371
|
+
declare function trimToContextWindow(messages: Message[], budgetChars: number): [number, Message[]];
|
|
2372
|
+
|
|
2373
|
+
/**
|
|
2374
|
+
* `tool()` wrapper for typed tool functions (spec §11.2).
|
|
2375
|
+
*
|
|
2376
|
+
* Creates a FunctionTool definition from a function's metadata and
|
|
2377
|
+
* auto-registers it in the global tool name registry.
|
|
2378
|
+
*
|
|
2379
|
+
* @module
|
|
2380
|
+
*/
|
|
2381
|
+
|
|
2382
|
+
/** Options for the tool() wrapper. */
|
|
2383
|
+
interface ToolOptions {
|
|
2384
|
+
/** Override the tool name (defaults to fn.name). */
|
|
2385
|
+
name?: string;
|
|
2386
|
+
/** Override the description. */
|
|
2387
|
+
description?: string;
|
|
2388
|
+
/** Parameter definitions (since JS can't introspect type hints). */
|
|
2389
|
+
parameters?: ToolParameter[];
|
|
2390
|
+
/** If false, skip global registration. Default: true. */
|
|
2391
|
+
register?: boolean;
|
|
2392
|
+
}
|
|
2393
|
+
/** A parameter definition for a tool function. */
|
|
2394
|
+
interface ToolParameter {
|
|
2395
|
+
name: string;
|
|
2396
|
+
kind?: string;
|
|
2397
|
+
description?: string;
|
|
2398
|
+
required?: boolean;
|
|
2399
|
+
default?: unknown;
|
|
2400
|
+
}
|
|
2401
|
+
/** Extended function with __tool__ metadata. */
|
|
2402
|
+
interface ToolFunction<T extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown> {
|
|
2403
|
+
(...args: Parameters<T>): ReturnType<T>;
|
|
2404
|
+
__tool__: FunctionTool;
|
|
2405
|
+
}
|
|
2406
|
+
/**
|
|
2407
|
+
* Wrap a function as a typed tool.
|
|
2408
|
+
*
|
|
2409
|
+
* Unlike Python's @tool which can introspect type hints, the JS version
|
|
2410
|
+
* requires explicit parameter definitions. The function itself is returned
|
|
2411
|
+
* unchanged but with a `__tool__` property containing the FunctionTool.
|
|
2412
|
+
*
|
|
2413
|
+
* @example
|
|
2414
|
+
* ```ts
|
|
2415
|
+
* const getWeather = tool(
|
|
2416
|
+
* (city: string, units?: string) => `72°F in ${city}`,
|
|
2417
|
+
* {
|
|
2418
|
+
* name: "get_weather",
|
|
2419
|
+
* description: "Get the current weather",
|
|
2420
|
+
* parameters: [
|
|
2421
|
+
* { name: "city", kind: "string", required: true },
|
|
2422
|
+
* { name: "units", kind: "string", default: "celsius" },
|
|
2423
|
+
* ],
|
|
2424
|
+
* },
|
|
2425
|
+
* );
|
|
2426
|
+
*
|
|
2427
|
+
* getWeather.__tool__; // FunctionTool instance
|
|
2428
|
+
* getWeather("NYC"); // "72°F in NYC"
|
|
2429
|
+
* ```
|
|
2430
|
+
*/
|
|
2431
|
+
declare function tool<T extends (...args: unknown[]) => unknown>(fn: T, options?: ToolOptions): ToolFunction<T>;
|
|
2432
|
+
/**
|
|
2433
|
+
* Validate tool handlers against an agent's tool declarations and return a handler record.
|
|
2434
|
+
*
|
|
2435
|
+
* Each function must have a `__tool__` property (set by `tool()`). `bindTools` matches
|
|
2436
|
+
* each handler's name against `kind: "function"` tools declared in `agent.tools`,
|
|
2437
|
+
* raising on mismatches and warning on missing handlers.
|
|
2438
|
+
*
|
|
2439
|
+
* @param agent - A loaded Prompty agent (has `.tools` property)
|
|
2440
|
+
* @param tools - Array of `tool()`-wrapped functions
|
|
2441
|
+
* @returns Handler record suitable for `invokeAgent(..., { tools: result })`
|
|
2442
|
+
* @throws Error if a handler has no `__tool__` property or no matching declaration
|
|
2443
|
+
*/
|
|
2444
|
+
declare function bindTools(agent: {
|
|
2445
|
+
tools?: Array<{
|
|
2446
|
+
name: string;
|
|
2447
|
+
kind?: string;
|
|
2448
|
+
}>;
|
|
2449
|
+
}, tools: Array<ToolFunction>): Record<string, (...args: unknown[]) => unknown>;
|
|
2450
|
+
|
|
2451
|
+
/**
|
|
2452
|
+
* Structured result casting for typed LLM output.
|
|
2453
|
+
*
|
|
2454
|
+
* When a processor parses structured JSON from an LLM response, it wraps
|
|
2455
|
+
* the result in a `StructuredResult` that carries both the parsed data
|
|
2456
|
+
* (accessible as normal properties) and the raw JSON string (hidden behind
|
|
2457
|
+
* a Symbol). The `cast()` function lets callers deserialize directly from
|
|
2458
|
+
* the raw JSON, optionally running a validator (e.g., Zod `.parse`).
|
|
2459
|
+
*
|
|
2460
|
+
* @module
|
|
2461
|
+
*/
|
|
2462
|
+
/**
|
|
2463
|
+
* Symbol used to store the raw JSON string on a StructuredResult.
|
|
2464
|
+
* Using a Symbol keeps the raw JSON invisible to normal property iteration.
|
|
2465
|
+
*/
|
|
2466
|
+
declare const StructuredResultSymbol: unique symbol;
|
|
2467
|
+
/**
|
|
2468
|
+
* A plain object carrying structured output from an LLM.
|
|
2469
|
+
* Behaves like a normal Record<string, unknown> but also stores the raw JSON
|
|
2470
|
+
* string so that cast() can deserialize directly to typed objects.
|
|
2471
|
+
*/
|
|
2472
|
+
interface StructuredResult extends Record<string, unknown> {
|
|
2473
|
+
readonly [StructuredResultSymbol]: string;
|
|
2474
|
+
}
|
|
2475
|
+
/**
|
|
2476
|
+
* Create a StructuredResult wrapping parsed data + raw JSON.
|
|
2477
|
+
*/
|
|
2478
|
+
declare function createStructuredResult(data: Record<string, unknown>, rawJson: string): StructuredResult;
|
|
2479
|
+
/**
|
|
2480
|
+
* Type guard: is this value a StructuredResult?
|
|
2481
|
+
*/
|
|
2482
|
+
declare function isStructuredResult(value: unknown): value is StructuredResult;
|
|
2483
|
+
/**
|
|
2484
|
+
* Cast a result to a typed object. When the result is a StructuredResult,
|
|
2485
|
+
* deserializes directly from the raw JSON (no intermediate round-trip).
|
|
2486
|
+
*
|
|
2487
|
+
* @param result - The result to cast (StructuredResult, string, or object)
|
|
2488
|
+
* @param validator - Optional runtime validator (e.g., Zod .parse)
|
|
2489
|
+
* @returns The typed result
|
|
2490
|
+
*/
|
|
2491
|
+
declare function cast<T = Record<string, unknown>>(result: unknown, validator?: (data: unknown) => T): T;
|
|
2212
2492
|
|
|
2213
2493
|
/**
|
|
2214
2494
|
* Nunjucks renderer — Jinja2-compatible template rendering for TypeScript.
|
|
@@ -2469,4 +2749,4 @@ interface OtelTracerOptions {
|
|
|
2469
2749
|
*/
|
|
2470
2750
|
declare function otelTracer(api: OtelApi, options?: OtelTracerOptions): TracerFactory;
|
|
2471
2751
|
|
|
2472
|
-
export { Prompty as AgentDefinition, AnonymousConnection, ApiKeyConnection, ArrayProperty, type AudioPart, Binding, Connection, type ContentPart, CustomTool, type Executor, type FilePart, FormatConfig, FoundryConnection, FunctionTool, type ImagePart, InvokerError, LoadContext, McpApprovalMode, McpTool, Message, Model, ModelOptions, MustacheRenderer, NunjucksRenderer, OAuthConnection, ObjectProperty, OpenApiTool, type OtelApi, type OtelTracerOptions, type Parser, ParserConfig, type Processor, Prompty as PromptAgent, Prompty, PromptyChatParser, PromptyStream, PromptyTool, PromptyTracer, Property, RICH_KINDS, ROLES, ReferenceConnection, RemoteConnection, type Renderer, type Role, SaveContext, type SpanEmitter, Template, type TextPart, ThreadMarker, Tool, type ToolCall, Tracer, type TracerBackend, type TracerFactory, clearCache, clearConnections, consoleTracer, dictContentToPart, dictToMessage, getConnection, getExecutor, getParser, getProcessor, getRenderer, invoke, invokeAgent, load, otelTracer, parse, prepare, process, registerConnection, registerExecutor, registerParser, registerProcessor, registerRenderer, render, resolveBindings, run, sanitizeValue, text, textMessage, toSerializable, trace, traceMethod, traceSpan, validateInputs };
|
|
2752
|
+
export { Prompty as AgentDefinition, type AgentEventType, AnonymousConnection, ApiKeyConnection, ArrayProperty, type AudioPart, Binding, CancelledError, Connection, type ContentPart, CustomTool, type EventCallback, type Executor, type FilePart, FormatConfig, FoundryConnection, FunctionTool, GuardrailError, type GuardrailResult, Guardrails, type GuardrailsOptions, type ImagePart, type InputGuardrail, type InvokeAgentOptions, InvokerError, LoadContext, McpApprovalMode, McpTool, Message, Model, ModelOptions, MustacheRenderer, NunjucksRenderer, OAuthConnection, ObjectProperty, OpenApiTool, type OtelApi, type OtelTracerOptions, type OutputGuardrail, type Parser, ParserConfig, type Processor, Prompty as PromptAgent, Prompty, PromptyChatParser, PromptyStream, PromptyTool, PromptyTracer, Property, RICH_KINDS, ROLES, ReferenceConnection, RemoteConnection, type Renderer, type Role, SaveContext, type SpanEmitter, Steering, type StructuredResult, StructuredResultSymbol, Template, type TextPart, ThreadMarker, Tool, type ToolCall, type ToolFunction, type ToolGuardrail, type ToolOptions, type ToolParameter, Tracer, type TracerBackend, type TracerFactory, bindTools, cast, checkCancellation, clearCache, clearConnections, consoleTracer, createStructuredResult, dictContentToPart, dictToMessage, emitEvent, estimateChars, getConnection, getExecutor, getParser, getProcessor, getRenderer, invoke, invokeAgent, isStructuredResult, load, otelTracer, parse, prepare, process, registerConnection, registerExecutor, registerParser, registerProcessor, registerRenderer, render, resolveBindings, run, sanitizeValue, summarizeDropped, text, textMessage, toSerializable, tool, trace, traceMethod, traceSpan, trimToContextWindow, validateInputs };
|
package/dist/index.d.ts
CHANGED
|
@@ -2055,6 +2055,16 @@ interface Parser {
|
|
|
2055
2055
|
*/
|
|
2056
2056
|
interface Executor {
|
|
2057
2057
|
execute(agent: Prompty, messages: Message[]): Promise<unknown>;
|
|
2058
|
+
/**
|
|
2059
|
+
* Format tool call results into provider-specific message format.
|
|
2060
|
+
* Called after tool dispatch; the pipeline provides extracted tool calls
|
|
2061
|
+
* and their string results.
|
|
2062
|
+
*/
|
|
2063
|
+
formatToolMessages(rawResponse: unknown, toolCalls: {
|
|
2064
|
+
id: string;
|
|
2065
|
+
name: string;
|
|
2066
|
+
arguments: string;
|
|
2067
|
+
}[], toolResults: string[], textContent?: string): Message[];
|
|
2058
2068
|
}
|
|
2059
2069
|
/**
|
|
2060
2070
|
* Extracts clean results from raw LLM responses.
|
|
@@ -2137,6 +2147,81 @@ declare function clearConnections(): void;
|
|
|
2137
2147
|
*/
|
|
2138
2148
|
declare function load(path: string): Prompty;
|
|
2139
2149
|
|
|
2150
|
+
/**
|
|
2151
|
+
* §13.1 Agent Loop Events — structured event callbacks.
|
|
2152
|
+
* @module
|
|
2153
|
+
*/
|
|
2154
|
+
/** Event types emitted during the agent loop. */
|
|
2155
|
+
type AgentEventType = "token" | "thinking" | "tool_call_start" | "tool_result" | "status" | "messages_updated" | "done" | "error" | "cancelled";
|
|
2156
|
+
/** Callback signature for agent loop events. */
|
|
2157
|
+
type EventCallback = (eventType: AgentEventType, data: Record<string, unknown>) => void;
|
|
2158
|
+
/**
|
|
2159
|
+
* Safely emit an event. Swallows errors from callback (spec §13.1:
|
|
2160
|
+
* event callbacks MUST NOT block the loop).
|
|
2161
|
+
*/
|
|
2162
|
+
declare function emitEvent(callback: EventCallback | undefined, eventType: AgentEventType, data: Record<string, unknown>): void;
|
|
2163
|
+
|
|
2164
|
+
/**
|
|
2165
|
+
* §13.4 Guardrails — optional validation hooks for the agent loop.
|
|
2166
|
+
* @module
|
|
2167
|
+
*/
|
|
2168
|
+
|
|
2169
|
+
/** Result of a guardrail check. */
|
|
2170
|
+
interface GuardrailResult {
|
|
2171
|
+
allowed: boolean;
|
|
2172
|
+
reason?: string;
|
|
2173
|
+
rewrite?: any;
|
|
2174
|
+
}
|
|
2175
|
+
/** Error thrown when a guardrail denies the operation. */
|
|
2176
|
+
declare class GuardrailError extends Error {
|
|
2177
|
+
reason: string;
|
|
2178
|
+
constructor(reason: string);
|
|
2179
|
+
}
|
|
2180
|
+
/** Input guardrail hook signature. */
|
|
2181
|
+
type InputGuardrail = (messages: Message[]) => GuardrailResult;
|
|
2182
|
+
/** Output guardrail hook signature. */
|
|
2183
|
+
type OutputGuardrail = (message: Message) => GuardrailResult;
|
|
2184
|
+
/** Tool guardrail hook signature. */
|
|
2185
|
+
type ToolGuardrail = (name: string, args: Record<string, unknown>) => GuardrailResult;
|
|
2186
|
+
/** Configuration for guardrail hooks. */
|
|
2187
|
+
interface GuardrailsOptions {
|
|
2188
|
+
input?: InputGuardrail;
|
|
2189
|
+
output?: OutputGuardrail;
|
|
2190
|
+
tool?: ToolGuardrail;
|
|
2191
|
+
}
|
|
2192
|
+
/**
|
|
2193
|
+
* Guardrails with input, output, and tool hooks.
|
|
2194
|
+
* All hooks are optional — when not set, execution proceeds normally.
|
|
2195
|
+
*/
|
|
2196
|
+
declare class Guardrails {
|
|
2197
|
+
private inputHook?;
|
|
2198
|
+
private outputHook?;
|
|
2199
|
+
private toolHook?;
|
|
2200
|
+
constructor(options?: GuardrailsOptions);
|
|
2201
|
+
checkInput(messages: Message[]): GuardrailResult;
|
|
2202
|
+
checkOutput(message: Message): GuardrailResult;
|
|
2203
|
+
checkTool(name: string, args: Record<string, unknown>): GuardrailResult;
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
/**
|
|
2207
|
+
* §13.5 Steering — inject user messages into a running agent loop.
|
|
2208
|
+
* @module
|
|
2209
|
+
*/
|
|
2210
|
+
|
|
2211
|
+
/**
|
|
2212
|
+
* A handle for injecting user messages into a running agent loop.
|
|
2213
|
+
* Thread-safe in the JS single-threaded model (no locking needed).
|
|
2214
|
+
*/
|
|
2215
|
+
declare class Steering {
|
|
2216
|
+
private queue;
|
|
2217
|
+
/** Enqueue a message to be injected at the next iteration. */
|
|
2218
|
+
send(message: string): void;
|
|
2219
|
+
/** Remove and return all queued messages as Message objects. */
|
|
2220
|
+
drain(): Message[];
|
|
2221
|
+
/** Whether there are pending messages without consuming them. */
|
|
2222
|
+
get hasPending(): boolean;
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2140
2225
|
/**
|
|
2141
2226
|
* Four-step execution pipeline.
|
|
2142
2227
|
*
|
|
@@ -2190,10 +2275,25 @@ declare function run(agent: Prompty, messages: Message[], options?: {
|
|
|
2190
2275
|
}): Promise<unknown>;
|
|
2191
2276
|
/**
|
|
2192
2277
|
* Full pipeline: load → prepare → run.
|
|
2278
|
+
*
|
|
2279
|
+
* @overload Untyped — returns `unknown`.
|
|
2193
2280
|
*/
|
|
2194
2281
|
declare function invoke(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: {
|
|
2195
2282
|
raw?: boolean;
|
|
2196
2283
|
}): Promise<unknown>;
|
|
2284
|
+
/**
|
|
2285
|
+
* Full pipeline with typed result: load → prepare → run → cast.
|
|
2286
|
+
*
|
|
2287
|
+
* When a `validator` is provided the raw result is deserialized from JSON
|
|
2288
|
+
* and passed through the validator (e.g. a Zod `.parse` function), giving
|
|
2289
|
+
* you a fully typed return value.
|
|
2290
|
+
*
|
|
2291
|
+
* @overload Typed — returns `Promise<T>`.
|
|
2292
|
+
*/
|
|
2293
|
+
declare function invoke<T>(prompt: string | Prompty, inputs: Record<string, unknown> | undefined, options: {
|
|
2294
|
+
raw?: boolean;
|
|
2295
|
+
validator: (data: unknown) => T;
|
|
2296
|
+
}): Promise<T>;
|
|
2197
2297
|
/**
|
|
2198
2298
|
* Resolve tool bindings: inject values from parentInputs into tool arguments.
|
|
2199
2299
|
*
|
|
@@ -2201,14 +2301,194 @@ declare function invoke(prompt: string | Prompty, inputs?: Record<string, unknow
|
|
|
2201
2301
|
* and sets `args[binding.name]` to that value. Returns a new args object.
|
|
2202
2302
|
*/
|
|
2203
2303
|
declare function resolveBindings(agent: Prompty, toolName: string, args: Record<string, unknown>, parentInputs?: Record<string, unknown>): Record<string, unknown>;
|
|
2204
|
-
/**
|
|
2205
|
-
|
|
2206
|
-
*/
|
|
2207
|
-
declare function invokeAgent(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: {
|
|
2304
|
+
/** Options for {@link invokeAgent}. */
|
|
2305
|
+
interface InvokeAgentOptions {
|
|
2208
2306
|
tools?: Record<string, (...args: unknown[]) => unknown>;
|
|
2209
2307
|
maxIterations?: number;
|
|
2210
2308
|
raw?: boolean;
|
|
2211
|
-
|
|
2309
|
+
onEvent?: EventCallback;
|
|
2310
|
+
signal?: AbortSignal;
|
|
2311
|
+
contextBudget?: number;
|
|
2312
|
+
guardrails?: Guardrails;
|
|
2313
|
+
steering?: Steering;
|
|
2314
|
+
parallelToolCalls?: boolean;
|
|
2315
|
+
}
|
|
2316
|
+
/**
|
|
2317
|
+
* Run a prompt with automatic tool-call execution loop.
|
|
2318
|
+
*
|
|
2319
|
+
* Supports §13 extensions: events, cancellation, context window
|
|
2320
|
+
* management, guardrails, steering, and parallel tool calls.
|
|
2321
|
+
*
|
|
2322
|
+
* @overload Untyped — returns `unknown`.
|
|
2323
|
+
*/
|
|
2324
|
+
declare function invokeAgent(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: InvokeAgentOptions): Promise<unknown>;
|
|
2325
|
+
/**
|
|
2326
|
+
* Run a prompt with automatic tool-call execution loop, returning a typed result.
|
|
2327
|
+
*
|
|
2328
|
+
* When a `validator` is provided in the options the final result is
|
|
2329
|
+
* deserialized from JSON and passed through the validator.
|
|
2330
|
+
*
|
|
2331
|
+
* @overload Typed — returns `Promise<T>`.
|
|
2332
|
+
*/
|
|
2333
|
+
declare function invokeAgent<T>(prompt: string | Prompty, inputs: Record<string, unknown> | undefined, options: InvokeAgentOptions & {
|
|
2334
|
+
validator: (data: unknown) => T;
|
|
2335
|
+
}): Promise<T>;
|
|
2336
|
+
|
|
2337
|
+
/**
|
|
2338
|
+
* §13.2 Cancellation — cooperative cancellation via AbortSignal.
|
|
2339
|
+
* @module
|
|
2340
|
+
*/
|
|
2341
|
+
/**
|
|
2342
|
+
* Error thrown when the agent loop is cancelled.
|
|
2343
|
+
*/
|
|
2344
|
+
declare class CancelledError extends Error {
|
|
2345
|
+
constructor(message?: string);
|
|
2346
|
+
}
|
|
2347
|
+
/**
|
|
2348
|
+
* Check if the signal is aborted, and throw CancelledError if so.
|
|
2349
|
+
*/
|
|
2350
|
+
declare function checkCancellation(signal?: AbortSignal): void;
|
|
2351
|
+
|
|
2352
|
+
/**
|
|
2353
|
+
* §13.3 Context Window Management — trimming and summarization.
|
|
2354
|
+
* @module
|
|
2355
|
+
*/
|
|
2356
|
+
|
|
2357
|
+
/**
|
|
2358
|
+
* Estimate the character cost of a message list.
|
|
2359
|
+
* Per spec §13.3: role + 4 overhead per message, text parts by length,
|
|
2360
|
+
* non-text parts at 200-char estimate.
|
|
2361
|
+
*/
|
|
2362
|
+
declare function estimateChars(messages: Message[]): number;
|
|
2363
|
+
/**
|
|
2364
|
+
* Build a compact string summary from dropped messages.
|
|
2365
|
+
*/
|
|
2366
|
+
declare function summarizeDropped(messages: Message[]): string;
|
|
2367
|
+
/**
|
|
2368
|
+
* Trim messages in-place to fit within a character budget.
|
|
2369
|
+
* Returns [droppedCount, droppedMessages].
|
|
2370
|
+
*/
|
|
2371
|
+
declare function trimToContextWindow(messages: Message[], budgetChars: number): [number, Message[]];
|
|
2372
|
+
|
|
2373
|
+
/**
|
|
2374
|
+
* `tool()` wrapper for typed tool functions (spec §11.2).
|
|
2375
|
+
*
|
|
2376
|
+
* Creates a FunctionTool definition from a function's metadata and
|
|
2377
|
+
* auto-registers it in the global tool name registry.
|
|
2378
|
+
*
|
|
2379
|
+
* @module
|
|
2380
|
+
*/
|
|
2381
|
+
|
|
2382
|
+
/** Options for the tool() wrapper. */
|
|
2383
|
+
interface ToolOptions {
|
|
2384
|
+
/** Override the tool name (defaults to fn.name). */
|
|
2385
|
+
name?: string;
|
|
2386
|
+
/** Override the description. */
|
|
2387
|
+
description?: string;
|
|
2388
|
+
/** Parameter definitions (since JS can't introspect type hints). */
|
|
2389
|
+
parameters?: ToolParameter[];
|
|
2390
|
+
/** If false, skip global registration. Default: true. */
|
|
2391
|
+
register?: boolean;
|
|
2392
|
+
}
|
|
2393
|
+
/** A parameter definition for a tool function. */
|
|
2394
|
+
interface ToolParameter {
|
|
2395
|
+
name: string;
|
|
2396
|
+
kind?: string;
|
|
2397
|
+
description?: string;
|
|
2398
|
+
required?: boolean;
|
|
2399
|
+
default?: unknown;
|
|
2400
|
+
}
|
|
2401
|
+
/** Extended function with __tool__ metadata. */
|
|
2402
|
+
interface ToolFunction<T extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown> {
|
|
2403
|
+
(...args: Parameters<T>): ReturnType<T>;
|
|
2404
|
+
__tool__: FunctionTool;
|
|
2405
|
+
}
|
|
2406
|
+
/**
|
|
2407
|
+
* Wrap a function as a typed tool.
|
|
2408
|
+
*
|
|
2409
|
+
* Unlike Python's @tool which can introspect type hints, the JS version
|
|
2410
|
+
* requires explicit parameter definitions. The function itself is returned
|
|
2411
|
+
* unchanged but with a `__tool__` property containing the FunctionTool.
|
|
2412
|
+
*
|
|
2413
|
+
* @example
|
|
2414
|
+
* ```ts
|
|
2415
|
+
* const getWeather = tool(
|
|
2416
|
+
* (city: string, units?: string) => `72°F in ${city}`,
|
|
2417
|
+
* {
|
|
2418
|
+
* name: "get_weather",
|
|
2419
|
+
* description: "Get the current weather",
|
|
2420
|
+
* parameters: [
|
|
2421
|
+
* { name: "city", kind: "string", required: true },
|
|
2422
|
+
* { name: "units", kind: "string", default: "celsius" },
|
|
2423
|
+
* ],
|
|
2424
|
+
* },
|
|
2425
|
+
* );
|
|
2426
|
+
*
|
|
2427
|
+
* getWeather.__tool__; // FunctionTool instance
|
|
2428
|
+
* getWeather("NYC"); // "72°F in NYC"
|
|
2429
|
+
* ```
|
|
2430
|
+
*/
|
|
2431
|
+
declare function tool<T extends (...args: unknown[]) => unknown>(fn: T, options?: ToolOptions): ToolFunction<T>;
|
|
2432
|
+
/**
|
|
2433
|
+
* Validate tool handlers against an agent's tool declarations and return a handler record.
|
|
2434
|
+
*
|
|
2435
|
+
* Each function must have a `__tool__` property (set by `tool()`). `bindTools` matches
|
|
2436
|
+
* each handler's name against `kind: "function"` tools declared in `agent.tools`,
|
|
2437
|
+
* raising on mismatches and warning on missing handlers.
|
|
2438
|
+
*
|
|
2439
|
+
* @param agent - A loaded Prompty agent (has `.tools` property)
|
|
2440
|
+
* @param tools - Array of `tool()`-wrapped functions
|
|
2441
|
+
* @returns Handler record suitable for `invokeAgent(..., { tools: result })`
|
|
2442
|
+
* @throws Error if a handler has no `__tool__` property or no matching declaration
|
|
2443
|
+
*/
|
|
2444
|
+
declare function bindTools(agent: {
|
|
2445
|
+
tools?: Array<{
|
|
2446
|
+
name: string;
|
|
2447
|
+
kind?: string;
|
|
2448
|
+
}>;
|
|
2449
|
+
}, tools: Array<ToolFunction>): Record<string, (...args: unknown[]) => unknown>;
|
|
2450
|
+
|
|
2451
|
+
/**
|
|
2452
|
+
* Structured result casting for typed LLM output.
|
|
2453
|
+
*
|
|
2454
|
+
* When a processor parses structured JSON from an LLM response, it wraps
|
|
2455
|
+
* the result in a `StructuredResult` that carries both the parsed data
|
|
2456
|
+
* (accessible as normal properties) and the raw JSON string (hidden behind
|
|
2457
|
+
* a Symbol). The `cast()` function lets callers deserialize directly from
|
|
2458
|
+
* the raw JSON, optionally running a validator (e.g., Zod `.parse`).
|
|
2459
|
+
*
|
|
2460
|
+
* @module
|
|
2461
|
+
*/
|
|
2462
|
+
/**
|
|
2463
|
+
* Symbol used to store the raw JSON string on a StructuredResult.
|
|
2464
|
+
* Using a Symbol keeps the raw JSON invisible to normal property iteration.
|
|
2465
|
+
*/
|
|
2466
|
+
declare const StructuredResultSymbol: unique symbol;
|
|
2467
|
+
/**
|
|
2468
|
+
* A plain object carrying structured output from an LLM.
|
|
2469
|
+
* Behaves like a normal Record<string, unknown> but also stores the raw JSON
|
|
2470
|
+
* string so that cast() can deserialize directly to typed objects.
|
|
2471
|
+
*/
|
|
2472
|
+
interface StructuredResult extends Record<string, unknown> {
|
|
2473
|
+
readonly [StructuredResultSymbol]: string;
|
|
2474
|
+
}
|
|
2475
|
+
/**
|
|
2476
|
+
* Create a StructuredResult wrapping parsed data + raw JSON.
|
|
2477
|
+
*/
|
|
2478
|
+
declare function createStructuredResult(data: Record<string, unknown>, rawJson: string): StructuredResult;
|
|
2479
|
+
/**
|
|
2480
|
+
* Type guard: is this value a StructuredResult?
|
|
2481
|
+
*/
|
|
2482
|
+
declare function isStructuredResult(value: unknown): value is StructuredResult;
|
|
2483
|
+
/**
|
|
2484
|
+
* Cast a result to a typed object. When the result is a StructuredResult,
|
|
2485
|
+
* deserializes directly from the raw JSON (no intermediate round-trip).
|
|
2486
|
+
*
|
|
2487
|
+
* @param result - The result to cast (StructuredResult, string, or object)
|
|
2488
|
+
* @param validator - Optional runtime validator (e.g., Zod .parse)
|
|
2489
|
+
* @returns The typed result
|
|
2490
|
+
*/
|
|
2491
|
+
declare function cast<T = Record<string, unknown>>(result: unknown, validator?: (data: unknown) => T): T;
|
|
2212
2492
|
|
|
2213
2493
|
/**
|
|
2214
2494
|
* Nunjucks renderer — Jinja2-compatible template rendering for TypeScript.
|
|
@@ -2469,4 +2749,4 @@ interface OtelTracerOptions {
|
|
|
2469
2749
|
*/
|
|
2470
2750
|
declare function otelTracer(api: OtelApi, options?: OtelTracerOptions): TracerFactory;
|
|
2471
2751
|
|
|
2472
|
-
export { Prompty as AgentDefinition, AnonymousConnection, ApiKeyConnection, ArrayProperty, type AudioPart, Binding, Connection, type ContentPart, CustomTool, type Executor, type FilePart, FormatConfig, FoundryConnection, FunctionTool, type ImagePart, InvokerError, LoadContext, McpApprovalMode, McpTool, Message, Model, ModelOptions, MustacheRenderer, NunjucksRenderer, OAuthConnection, ObjectProperty, OpenApiTool, type OtelApi, type OtelTracerOptions, type Parser, ParserConfig, type Processor, Prompty as PromptAgent, Prompty, PromptyChatParser, PromptyStream, PromptyTool, PromptyTracer, Property, RICH_KINDS, ROLES, ReferenceConnection, RemoteConnection, type Renderer, type Role, SaveContext, type SpanEmitter, Template, type TextPart, ThreadMarker, Tool, type ToolCall, Tracer, type TracerBackend, type TracerFactory, clearCache, clearConnections, consoleTracer, dictContentToPart, dictToMessage, getConnection, getExecutor, getParser, getProcessor, getRenderer, invoke, invokeAgent, load, otelTracer, parse, prepare, process, registerConnection, registerExecutor, registerParser, registerProcessor, registerRenderer, render, resolveBindings, run, sanitizeValue, text, textMessage, toSerializable, trace, traceMethod, traceSpan, validateInputs };
|
|
2752
|
+
export { Prompty as AgentDefinition, type AgentEventType, AnonymousConnection, ApiKeyConnection, ArrayProperty, type AudioPart, Binding, CancelledError, Connection, type ContentPart, CustomTool, type EventCallback, type Executor, type FilePart, FormatConfig, FoundryConnection, FunctionTool, GuardrailError, type GuardrailResult, Guardrails, type GuardrailsOptions, type ImagePart, type InputGuardrail, type InvokeAgentOptions, InvokerError, LoadContext, McpApprovalMode, McpTool, Message, Model, ModelOptions, MustacheRenderer, NunjucksRenderer, OAuthConnection, ObjectProperty, OpenApiTool, type OtelApi, type OtelTracerOptions, type OutputGuardrail, type Parser, ParserConfig, type Processor, Prompty as PromptAgent, Prompty, PromptyChatParser, PromptyStream, PromptyTool, PromptyTracer, Property, RICH_KINDS, ROLES, ReferenceConnection, RemoteConnection, type Renderer, type Role, SaveContext, type SpanEmitter, Steering, type StructuredResult, StructuredResultSymbol, Template, type TextPart, ThreadMarker, Tool, type ToolCall, type ToolFunction, type ToolGuardrail, type ToolOptions, type ToolParameter, Tracer, type TracerBackend, type TracerFactory, bindTools, cast, checkCancellation, clearCache, clearConnections, consoleTracer, createStructuredResult, dictContentToPart, dictToMessage, emitEvent, estimateChars, getConnection, getExecutor, getParser, getProcessor, getRenderer, invoke, invokeAgent, isStructuredResult, load, otelTracer, parse, prepare, process, registerConnection, registerExecutor, registerParser, registerProcessor, registerRenderer, render, resolveBindings, run, sanitizeValue, summarizeDropped, text, textMessage, toSerializable, tool, trace, traceMethod, traceSpan, trimToContextWindow, validateInputs };
|