@prompty/core 2.0.0-alpha.5 → 2.0.0-alpha.7

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.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.
@@ -2138,19 +2148,108 @@ declare function clearConnections(): void;
2138
2148
  declare function load(path: string): Prompty;
2139
2149
 
2140
2150
  /**
2141
- * Four-step execution pipeline.
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
+
2225
+ /**
2226
+ * Execution pipeline — two top-level APIs plus building blocks.
2142
2227
  *
2143
2228
  * ```
2144
- * invoke(prompt, inputs) → top-level orchestrator
2229
+ * invoke(prompt, inputs) → one-shot: load + prepare + execute + process
2230
+ * ├── load(path) → file → agent (when path given)
2145
2231
  * ├── prepare(agent, inputs) → template → wire format
2146
2232
  * │ ├── render(agent, inputs) → template + inputs → rendered string
2147
2233
  * │ └── parse(agent, rendered) → rendered string → Message[]
2148
- * └── run(agent, messages) LLM call clean result
2149
- * ├── Executor.execute(...) messagesraw LLM response
2150
- * └── process(agent, response) → raw response → clean result
2234
+ * ├── Executor.execute(...) messagesraw LLM response
2235
+ * └── Processor.process(...) raw response clean result
2236
+ *
2237
+ * turn(agent, inputs, options?) → conversational round-trip
2238
+ * ├── prepare(agent, inputs) → template → wire format
2239
+ * ├── Executor.execute(...) → LLM call
2240
+ * ├── [toolCalls → Executor]* → agent loop (when tools provided)
2241
+ * └── Processor.process(...) → final result extraction
2242
+ *
2243
+ * run(agent, messages, options?) → standalone: execute + process
2244
+ * ├── Executor.execute(...) → messages → raw LLM response
2245
+ * └── Processor.process(...) → raw response → clean result
2151
2246
  * ```
2152
2247
  *
2153
- * Each leaf step is independently traced. Users can bring their own
2248
+ * `invoke` = "call this prompty like a function" (one-shot, embeddings, tool JSON).
2249
+ * `turn` = "one round of a conversation" (thread history, turn numbering, tool loops).
2250
+ * `run` = standalone building block for advanced users.
2251
+ *
2252
+ * Each step is independently traced. Users can bring their own
2154
2253
  * Renderer, Parser, Executor, Processor via the registry.
2155
2254
  *
2156
2255
  * @module
@@ -2188,12 +2287,95 @@ declare function prepare(agent: Prompty, inputs?: Record<string, unknown>): Prom
2188
2287
  declare function run(agent: Prompty, messages: Message[], options?: {
2189
2288
  raw?: boolean;
2190
2289
  }): Promise<unknown>;
2290
+ /** Options for {@link invoke}. */
2291
+ interface InvokeOptions {
2292
+ /** Return raw executor response without processing. */
2293
+ raw?: boolean;
2294
+ }
2191
2295
  /**
2192
- * Full pipeline: load → prepare → run.
2296
+ * One-shot pipeline: load → prepare → execute → process.
2297
+ *
2298
+ * Use `invoke` to call a prompty like a function — give inputs, get output.
2299
+ * No conversation context or turn numbering. Supports file paths or
2300
+ * pre-loaded agents.
2301
+ *
2302
+ * Trace structure (flat):
2303
+ * ```
2304
+ * invoke
2305
+ * load (only when path given)
2306
+ * prepare
2307
+ * Renderer
2308
+ * Parser
2309
+ * Executor
2310
+ * Processor
2311
+ * ```
2312
+ *
2313
+ * @overload Untyped — returns `unknown`.
2193
2314
  */
2194
- declare function invoke(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: {
2315
+ declare function invoke(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: InvokeOptions): Promise<unknown>;
2316
+ /**
2317
+ * One-shot pipeline with typed result: load → prepare → execute → process → cast.
2318
+ *
2319
+ * When a `validator` is provided the raw result is deserialized from JSON
2320
+ * and passed through the validator (e.g. a Zod `.parse` function), giving
2321
+ * you a fully typed return value.
2322
+ *
2323
+ * @overload Typed — returns `Promise<T>`.
2324
+ */
2325
+ declare function invoke<T>(prompt: string | Prompty, inputs: Record<string, unknown> | undefined, options: InvokeOptions & {
2326
+ validator: (data: unknown) => T;
2327
+ }): Promise<T>;
2328
+ /** Options for {@link turn}. */
2329
+ interface TurnOptions {
2330
+ /** Runtime tool handlers. When provided, triggers the agent loop. */
2331
+ tools?: Record<string, (...args: unknown[]) => unknown>;
2332
+ /** Turn number for trace labeling (e.g., "turn 3"). */
2333
+ turn?: number;
2334
+ /** Maximum agent-loop iterations before throwing (default: 10). */
2335
+ maxIterations?: number;
2336
+ /** Return raw executor response without processing. */
2195
2337
  raw?: boolean;
2196
- }): Promise<unknown>;
2338
+ /** Callback for agent loop events (token, tool_call, done, etc.). */
2339
+ onEvent?: EventCallback;
2340
+ /** Abort signal for cancellation (§13.2). */
2341
+ signal?: AbortSignal;
2342
+ /** Max character budget for context window trimming (§13.3). */
2343
+ contextBudget?: number;
2344
+ /** Input/output/tool guardrails (§13.4). */
2345
+ guardrails?: Guardrails;
2346
+ /** Steering queue for injecting messages mid-loop (§13.5). */
2347
+ steering?: Steering;
2348
+ /** Allow parallel tool execution within a single round (§13.6). */
2349
+ parallelToolCalls?: boolean;
2350
+ }
2351
+ /**
2352
+ * One conversational turn: prepare messages from inputs, then either execute a
2353
+ * single LLM call or enter the agent loop (when tools are provided).
2354
+ *
2355
+ * Trace structure (flat — no redundant wrappers):
2356
+ * ```
2357
+ * turn N
2358
+ * prepare → Renderer → Parser
2359
+ * Executor (each LLM call)
2360
+ * toolCalls → tool1, tool2 (if tools provided)
2361
+ * Executor (follow-up LLM call)
2362
+ * Processor (final result extraction)
2363
+ * ```
2364
+ *
2365
+ * @overload Untyped — returns `unknown`.
2366
+ */
2367
+ declare function turn(prompt: string | Prompty, inputs: Record<string, unknown>, options?: TurnOptions): Promise<unknown>;
2368
+ /**
2369
+ * One conversational turn with typed result.
2370
+ *
2371
+ * When a `validator` is provided the final result is deserialized from JSON
2372
+ * and passed through the validator (e.g. a Zod `.parse` function).
2373
+ *
2374
+ * @overload Typed — returns `Promise<T>`.
2375
+ */
2376
+ declare function turn<T>(prompt: string | Prompty, inputs: Record<string, unknown>, options: TurnOptions & {
2377
+ validator: (data: unknown) => T;
2378
+ }): Promise<T>;
2197
2379
  /**
2198
2380
  * Resolve tool bindings: inject values from parentInputs into tool arguments.
2199
2381
  *
@@ -2202,13 +2384,168 @@ declare function invoke(prompt: string | Prompty, inputs?: Record<string, unknow
2202
2384
  */
2203
2385
  declare function resolveBindings(agent: Prompty, toolName: string, args: Record<string, unknown>, parentInputs?: Record<string, unknown>): Record<string, unknown>;
2204
2386
  /**
2205
- * Run a prompt with automatic tool-call execution loop.
2387
+ * @deprecated Use {@link turn} with `tools` option instead.
2388
+ * Kept for backward compatibility — delegates to `turn()`.
2206
2389
  */
2207
- declare function invokeAgent(prompt: string | Prompty, inputs?: Record<string, unknown>, options?: {
2208
- tools?: Record<string, (...args: unknown[]) => unknown>;
2209
- maxIterations?: number;
2210
- raw?: boolean;
2211
- }): Promise<unknown>;
2390
+ declare const invokeAgent: typeof turn;
2391
+ /** @deprecated Use {@link TurnOptions} instead. */
2392
+ type InvokeAgentOptions = TurnOptions;
2393
+
2394
+ /**
2395
+ * §13.2 Cancellation — cooperative cancellation via AbortSignal.
2396
+ * @module
2397
+ */
2398
+ /**
2399
+ * Error thrown when the agent loop is cancelled.
2400
+ */
2401
+ declare class CancelledError extends Error {
2402
+ constructor(message?: string);
2403
+ }
2404
+ /**
2405
+ * Check if the signal is aborted, and throw CancelledError if so.
2406
+ */
2407
+ declare function checkCancellation(signal?: AbortSignal): void;
2408
+
2409
+ /**
2410
+ * §13.3 Context Window Management — trimming and summarization.
2411
+ * @module
2412
+ */
2413
+
2414
+ /**
2415
+ * Estimate the character cost of a message list.
2416
+ * Per spec §13.3: role + 4 overhead per message, text parts by length,
2417
+ * non-text parts at 200-char estimate.
2418
+ */
2419
+ declare function estimateChars(messages: Message[]): number;
2420
+ /**
2421
+ * Build a compact string summary from dropped messages.
2422
+ */
2423
+ declare function summarizeDropped(messages: Message[]): string;
2424
+ /**
2425
+ * Trim messages in-place to fit within a character budget.
2426
+ * Returns [droppedCount, droppedMessages].
2427
+ */
2428
+ declare function trimToContextWindow(messages: Message[], budgetChars: number): [number, Message[]];
2429
+
2430
+ /**
2431
+ * `tool()` wrapper for typed tool functions (spec §11.2).
2432
+ *
2433
+ * Creates a FunctionTool definition from a function's metadata and
2434
+ * auto-registers it in the global tool name registry.
2435
+ *
2436
+ * @module
2437
+ */
2438
+
2439
+ /** Options for the tool() wrapper. */
2440
+ interface ToolOptions {
2441
+ /** Override the tool name (defaults to fn.name). */
2442
+ name?: string;
2443
+ /** Override the description. */
2444
+ description?: string;
2445
+ /** Parameter definitions (since JS can't introspect type hints). */
2446
+ parameters?: ToolParameter[];
2447
+ /** If false, skip global registration. Default: true. */
2448
+ register?: boolean;
2449
+ }
2450
+ /** A parameter definition for a tool function. */
2451
+ interface ToolParameter {
2452
+ name: string;
2453
+ kind?: string;
2454
+ description?: string;
2455
+ required?: boolean;
2456
+ default?: unknown;
2457
+ }
2458
+ /** Extended function with __tool__ metadata. */
2459
+ interface ToolFunction<T extends (...args: unknown[]) => unknown = (...args: unknown[]) => unknown> {
2460
+ (...args: Parameters<T>): ReturnType<T>;
2461
+ __tool__: FunctionTool;
2462
+ }
2463
+ /**
2464
+ * Wrap a function as a typed tool.
2465
+ *
2466
+ * Unlike Python's @tool which can introspect type hints, the JS version
2467
+ * requires explicit parameter definitions. The function itself is returned
2468
+ * unchanged but with a `__tool__` property containing the FunctionTool.
2469
+ *
2470
+ * @example
2471
+ * ```ts
2472
+ * const getWeather = tool(
2473
+ * (city: string, units?: string) => `72°F in ${city}`,
2474
+ * {
2475
+ * name: "get_weather",
2476
+ * description: "Get the current weather",
2477
+ * parameters: [
2478
+ * { name: "city", kind: "string", required: true },
2479
+ * { name: "units", kind: "string", default: "celsius" },
2480
+ * ],
2481
+ * },
2482
+ * );
2483
+ *
2484
+ * getWeather.__tool__; // FunctionTool instance
2485
+ * getWeather("NYC"); // "72°F in NYC"
2486
+ * ```
2487
+ */
2488
+ declare function tool<T extends (...args: unknown[]) => unknown>(fn: T, options?: ToolOptions): ToolFunction<T>;
2489
+ /**
2490
+ * Validate tool handlers against an agent's tool declarations and return a handler record.
2491
+ *
2492
+ * Each function must have a `__tool__` property (set by `tool()`). `bindTools` matches
2493
+ * each handler's name against `kind: "function"` tools declared in `agent.tools`,
2494
+ * raising on mismatches and warning on missing handlers.
2495
+ *
2496
+ * @param agent - A loaded Prompty agent (has `.tools` property)
2497
+ * @param tools - Array of `tool()`-wrapped functions
2498
+ * @returns Handler record suitable for `turn(..., { tools: result })`
2499
+ * @throws Error if a handler has no `__tool__` property or no matching declaration
2500
+ */
2501
+ declare function bindTools(agent: {
2502
+ tools?: Array<{
2503
+ name: string;
2504
+ kind?: string;
2505
+ }>;
2506
+ }, tools: Array<ToolFunction>): Record<string, (...args: unknown[]) => unknown>;
2507
+
2508
+ /**
2509
+ * Structured result casting for typed LLM output.
2510
+ *
2511
+ * When a processor parses structured JSON from an LLM response, it wraps
2512
+ * the result in a `StructuredResult` that carries both the parsed data
2513
+ * (accessible as normal properties) and the raw JSON string (hidden behind
2514
+ * a Symbol). The `cast()` function lets callers deserialize directly from
2515
+ * the raw JSON, optionally running a validator (e.g., Zod `.parse`).
2516
+ *
2517
+ * @module
2518
+ */
2519
+ /**
2520
+ * Symbol used to store the raw JSON string on a StructuredResult.
2521
+ * Using a Symbol keeps the raw JSON invisible to normal property iteration.
2522
+ */
2523
+ declare const StructuredResultSymbol: unique symbol;
2524
+ /**
2525
+ * A plain object carrying structured output from an LLM.
2526
+ * Behaves like a normal Record<string, unknown> but also stores the raw JSON
2527
+ * string so that cast() can deserialize directly to typed objects.
2528
+ */
2529
+ interface StructuredResult extends Record<string, unknown> {
2530
+ readonly [StructuredResultSymbol]: string;
2531
+ }
2532
+ /**
2533
+ * Create a StructuredResult wrapping parsed data + raw JSON.
2534
+ */
2535
+ declare function createStructuredResult(data: Record<string, unknown>, rawJson: string): StructuredResult;
2536
+ /**
2537
+ * Type guard: is this value a StructuredResult?
2538
+ */
2539
+ declare function isStructuredResult(value: unknown): value is StructuredResult;
2540
+ /**
2541
+ * Cast a result to a typed object. When the result is a StructuredResult,
2542
+ * deserializes directly from the raw JSON (no intermediate round-trip).
2543
+ *
2544
+ * @param result - The result to cast (StructuredResult, string, or object)
2545
+ * @param validator - Optional runtime validator (e.g., Zod .parse)
2546
+ * @returns The typed result
2547
+ */
2548
+ declare function cast<T = Record<string, unknown>>(result: unknown, validator?: (data: unknown) => T): T;
2212
2549
 
2213
2550
  /**
2214
2551
  * Nunjucks renderer — Jinja2-compatible template rendering for TypeScript.
@@ -2469,4 +2806,4 @@ interface OtelTracerOptions {
2469
2806
  */
2470
2807
  declare function otelTracer(api: OtelApi, options?: OtelTracerOptions): TracerFactory;
2471
2808
 
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 };
2809
+ 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, type InvokeOptions, 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, type TurnOptions, 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, turn, validateInputs };