llmist 2.6.0 → 3.1.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/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ZodType, ZodTypeAny } from 'zod';
2
2
  export { z } from 'zod';
3
- import { A as AgentHooks, s as ModelRegistry, t as LLMist, C as CompactionConfig, I as IConversationManager, u as CompactionEvent, L as LLMMessage, v as CompactionStats, w as CompactionStrategy, R as ResolvedCompactionConfig, x as CompactionContext, y as CompactionResult, z as MessageContent, G as GadgetMediaOutput, H as HintTemplate, S as StreamEvent, T as TokenUsage, D as GadgetRegistry, E as MediaStore, b as LLMStreamChunk, F as ExecutionContext, J as GadgetExecuteReturn, K as GadgetExample, B as BaseGadget, P as ParsedGadgetCall, N as GadgetExecutionResult, O as MediaKind, Q as MediaMetadata, U as GadgetExecuteResultWithMedia, V as ProviderAdapter, W as ModelDescriptor, X as ModelSpec, Y as LLMGenerationOptions, a as LLMStream, Z as ImageModelSpec, _ as ImageGenerationOptions, $ as ImageGenerationResult, a0 as SpeechModelSpec, a1 as SpeechGenerationOptions, a2 as SpeechGenerationResult } from './mock-stream-Jgg5u6Uf.js';
4
- export { ab as AfterGadgetExecutionAction, ac as AfterGadgetExecutionControllerContext, ad as AfterLLMCallAction, ae as AfterLLMCallControllerContext, af as AfterLLMErrorAction, a6 as AgentBuilder, ag as AgentOptions, aE as AudioContentPart, aF as AudioMimeType, aG as AudioSource, ah as BeforeGadgetExecutionAction, ai as BeforeLLMCallAction, aj as ChunkInterceptorContext, aH as ContentPart, ak as Controllers, b4 as CostEstimate, bq as CostReportingLLMist, aB as DEFAULT_COMPACTION_CONFIG, bg as DEFAULT_HINTS, bh as DEFAULT_PROMPTS, aC as DEFAULT_SUMMARIZATION_PROMPT, a7 as EventHandlers, bo as GadgetClass, br as GadgetExecuteResult, al as GadgetExecutionControllerContext, bp as GadgetOrClass, am as GadgetParameterInterceptorContext, an as GadgetResultInterceptorContext, bs as GadgetSkippedEvent, bc as HintContext, a3 as HistoryMessage, aI as ImageBase64Source, aJ as ImageContentPart, aK as ImageMimeType, aL as ImageSource, aM as ImageUrlSource, ao as Interceptors, ap as LLMCallControllerContext, aq as LLMErrorControllerContext, b2 as LLMMessageBuilder, b0 as LLMRole, aD as LLMistOptions, ar as MessageInterceptorContext, as as MessageTurn, d as MockBuilder, f as MockManager, l as MockMatcher, n as MockMatcherContext, o as MockOptions, M as MockProviderAdapter, p as MockRegistration, q as MockResponse, r as MockStats, b5 as ModelFeatures, bb as ModelIdentifierParser, b6 as ModelLimits, b7 as ModelPricing, at as ObserveChunkContext, au as ObserveCompactionContext, av as ObserveGadgetCompleteContext, aw as ObserveGadgetStartContext, ax as ObserveLLMCallContext, ay as ObserveLLMCompleteContext, az as ObserveLLMErrorContext, aA as Observers, bd as PromptConfig, be as PromptContext, bf as PromptTemplate, ba as ProviderIdentifier, bl as QuickOptions, bt as StoredMedia, aN as TextContentPart, bu as TextOnlyAction, bv as TextOnlyContext, bw as TextOnlyCustomHandler, bx as TextOnlyGadgetConfig, by as TextOnlyHandler, bz as TextOnlyStrategy, a4 as TrailingMessage, a5 as TrailingMessageContext, b8 as VisionAnalyzeOptions, b9 as VisionAnalyzeResult, aO as audioFromBase64, aP as audioFromBuffer, a8 as collectEvents, a9 as collectText, bm as complete, c as createMockAdapter, e as createMockClient, h as createMockStream, i as createTextMockStream, aQ as detectAudioMimeType, aR as detectImageMimeType, b1 as extractText, g as getMockManager, aS as imageFromBase64, aT as imageFromBuffer, aU as imageFromUrl, aV as isAudioPart, aW as isDataUrl, aX as isImagePart, aY as isTextPart, m as mockLLM, b3 as normalizeContent, aZ as parseDataUrl, bi as resolveHintTemplate, bj as resolvePromptTemplate, bk as resolveRulesTemplate, aa as runWithHandlers, bn as stream, a_ as text, a$ as toBase64 } from './mock-stream-Jgg5u6Uf.js';
3
+ import { s as AgentHooks, t as ModelRegistry, u as LLMist, C as CompactionConfig, I as IConversationManager, v as CompactionEvent, L as LLMMessage, w as CompactionStats, x as CompactionStrategy, R as ResolvedCompactionConfig, y as CompactionContext, z as CompactionResult, B as MessageContent, G as GadgetMediaOutput, H as HintTemplate, S as StreamEvent, T as TokenUsage, D as GadgetRegistry, E as MediaStore, F as AgentContextConfig, J as SubagentConfigMap, N as NestedAgentEvent, b as LLMStreamChunk, K as ExecutionContext, O as GadgetExecuteReturn, P as GadgetExample, A as AbstractGadget, Q as ParsedGadgetCall, U as GadgetExecutionResult, V as MediaKind, W as MediaMetadata, X as GadgetExecuteResultWithMedia, Y as ProviderAdapter, Z as ModelDescriptor, _ as ModelSpec, $ as LLMGenerationOptions, a as LLMStream, a0 as ImageModelSpec, a1 as ImageGenerationOptions, a2 as ImageGenerationResult, a3 as SpeechModelSpec, a4 as SpeechGenerationOptions, a5 as SpeechGenerationResult } from './mock-stream-CTLm00_q.js';
4
+ export { ae as AfterGadgetExecutionAction, af as AfterGadgetExecutionControllerContext, ag as AfterLLMCallAction, ah as AfterLLMCallControllerContext, ai as AfterLLMErrorAction, a9 as AgentBuilder, aj as AgentOptions, aH as AudioContentPart, aI as AudioMimeType, aJ as AudioSource, ak as BeforeGadgetExecutionAction, al as BeforeLLMCallAction, am as ChunkInterceptorContext, aK as ContentPart, an as Controllers, b7 as CostEstimate, bt as CostReportingLLMist, aE as DEFAULT_COMPACTION_CONFIG, bj as DEFAULT_HINTS, bk as DEFAULT_PROMPTS, aF as DEFAULT_SUMMARIZATION_PROMPT, aa as EventHandlers, br as GadgetClass, bu as GadgetExecuteResult, ao as GadgetExecutionControllerContext, bs as GadgetOrClass, ap as GadgetParameterInterceptorContext, aq as GadgetResultInterceptorContext, bv as GadgetSkippedEvent, bf as HintContext, a6 as HistoryMessage, aL as ImageBase64Source, aM as ImageContentPart, aN as ImageMimeType, aO as ImageSource, aP as ImageUrlSource, ar as Interceptors, as as LLMCallControllerContext, at as LLMErrorControllerContext, b5 as LLMMessageBuilder, aG as LLMistOptions, au as MessageInterceptorContext, b3 as MessageRole, av as MessageTurn, d as MockBuilder, f as MockManager, l as MockMatcher, n as MockMatcherContext, o as MockOptions, M as MockProviderAdapter, p as MockRegistration, q as MockResponse, r as MockStats, b8 as ModelFeatures, be as ModelIdentifierParser, b9 as ModelLimits, ba as ModelPricing, aw as ObserveChunkContext, ax as ObserveCompactionContext, ay as ObserveGadgetCompleteContext, az as ObserveGadgetStartContext, aA as ObserveLLMCallContext, aB as ObserveLLMCompleteContext, aC as ObserveLLMErrorContext, aD as Observers, bg as PromptContext, bh as PromptTemplate, bi as PromptTemplateConfig, bd as ProviderIdentifier, bw as StoredMedia, aQ as TextContentPart, bo as TextGenerationOptions, bx as TextOnlyAction, by as TextOnlyContext, bz as TextOnlyCustomHandler, bA as TextOnlyGadgetConfig, bB as TextOnlyHandler, bC as TextOnlyStrategy, a7 as TrailingMessage, a8 as TrailingMessageContext, bb as VisionAnalyzeOptions, bc as VisionAnalyzeResult, aR as audioFromBase64, aS as audioFromBuffer, ab as collectEvents, ac as collectText, bp as complete, c as createMockAdapter, e as createMockClient, h as createMockStream, i as createTextMockStream, aT as detectAudioMimeType, aU as detectImageMimeType, b4 as extractMessageText, g as getMockManager, aV as imageFromBase64, aW as imageFromBuffer, aX as imageFromUrl, aY as isAudioPart, aZ as isDataUrl, a_ as isImagePart, a$ as isTextPart, m as mockLLM, b6 as normalizeMessageContent, b0 as parseDataUrl, bl as resolveHintTemplate, bm as resolvePromptTemplate, bn as resolveRulesTemplate, ad as runWithHandlers, bq as stream, b1 as text, b2 as toBase64 } from './mock-stream-CTLm00_q.js';
5
5
  import { Logger, ILogObj } from 'tslog';
6
6
  import { MessageCreateParamsStreaming, MessageStreamEvent } from '@anthropic-ai/sdk/resources/messages';
7
7
  import OpenAI from 'openai';
@@ -888,7 +888,7 @@ declare class ConversationManager implements IConversationManager {
888
888
  constructor(baseMessages: LLMMessage[], initialMessages: LLMMessage[], options?: ConversationManagerOptions);
889
889
  addUserMessage(content: MessageContent): void;
890
890
  addAssistantMessage(content: string): void;
891
- addGadgetCall(gadgetName: string, parameters: Record<string, unknown>, result: string, media?: GadgetMediaOutput[], mediaIds?: string[]): void;
891
+ addGadgetCallResult(gadgetName: string, parameters: Record<string, unknown>, result: string, media?: GadgetMediaOutput[], mediaIds?: string[]): void;
892
892
  getMessages(): LLMMessage[];
893
893
  getHistoryMessages(): LLMMessage[];
894
894
  getBaseMessages(): LLMMessage[];
@@ -1178,12 +1178,12 @@ interface StreamProcessorOptions {
1178
1178
  hooks?: AgentHooks;
1179
1179
  /** Logger instance */
1180
1180
  logger?: Logger<ILogObj>;
1181
- /** Callback for human input */
1182
- onHumanInputRequired?: (question: string) => Promise<string>;
1181
+ /** Callback for requesting human input during execution */
1182
+ requestHumanInput?: (question: string) => Promise<string>;
1183
1183
  /** Whether to stop on gadget errors */
1184
1184
  stopOnGadgetError?: boolean;
1185
- /** Custom error continuation logic */
1186
- shouldContinueAfterError?: (context: {
1185
+ /** Custom error recovery logic */
1186
+ canRecoverFromGadgetError?: (context: {
1187
1187
  error: string;
1188
1188
  gadgetName: string;
1189
1189
  errorType: "parse" | "validation" | "execution";
@@ -1195,6 +1195,12 @@ interface StreamProcessorOptions {
1195
1195
  client?: LLMist;
1196
1196
  /** MediaStore for storing gadget media outputs */
1197
1197
  mediaStore?: MediaStore;
1198
+ /** Parent agent configuration for subagents to inherit */
1199
+ agentConfig?: AgentContextConfig;
1200
+ /** Subagent-specific configuration overrides */
1201
+ subagentConfig?: SubagentConfigMap;
1202
+ /** Callback for subagent gadgets to report nested events to parent */
1203
+ onNestedEvent?: (event: NestedAgentEvent) => void;
1198
1204
  }
1199
1205
  /**
1200
1206
  * Result of stream processing.
@@ -1245,25 +1251,38 @@ declare class StreamProcessor {
1245
1251
  private readonly parser;
1246
1252
  private readonly executor;
1247
1253
  private readonly stopOnGadgetError;
1248
- private readonly shouldContinueAfterError?;
1249
- private accumulatedText;
1250
- private shouldStopExecution;
1254
+ private readonly canRecoverFromGadgetError?;
1255
+ private responseText;
1256
+ private executionHalted;
1251
1257
  private observerFailureCount;
1252
1258
  /** Gadgets waiting for their dependencies to complete */
1253
- private pendingGadgets;
1259
+ private gadgetsAwaitingDependencies;
1254
1260
  /** Completed gadget results, keyed by invocation ID */
1255
1261
  private completedResults;
1256
1262
  /** Invocation IDs of gadgets that have failed (error or skipped due to dependency) */
1257
1263
  private failedInvocations;
1258
1264
  constructor(options: StreamProcessorOptions);
1259
1265
  /**
1260
- * Process an LLM stream and return structured results.
1266
+ * Process an LLM stream and yield events in real-time.
1267
+ *
1268
+ * This is an async generator that yields events immediately as they occur:
1269
+ * - Text events are yielded as text is streamed from the LLM
1270
+ * - gadget_call events are yielded immediately when a gadget call is parsed
1271
+ * - gadget_result events are yielded when gadget execution completes
1272
+ *
1273
+ * The final event is always a StreamCompletionEvent containing metadata.
1261
1274
  */
1262
- process(stream: AsyncIterable<LLMStreamChunk>): Promise<StreamProcessingResult>;
1275
+ process(stream: AsyncIterable<LLMStreamChunk>): AsyncGenerator<StreamEvent>;
1263
1276
  /**
1264
1277
  * Process a single parsed event (text or gadget call).
1278
+ * @deprecated Use processEventGenerator for real-time streaming
1265
1279
  */
1266
1280
  private processEvent;
1281
+ /**
1282
+ * Process a single parsed event, yielding events in real-time.
1283
+ * Generator version of processEvent for streaming support.
1284
+ */
1285
+ private processEventGenerator;
1267
1286
  /**
1268
1287
  * Process a text event through interceptors.
1269
1288
  */
@@ -1276,11 +1295,24 @@ declare class StreamProcessor {
1276
1295
  * After each execution, pending gadgets are checked to see if they can now run.
1277
1296
  */
1278
1297
  private processGadgetCall;
1298
+ /**
1299
+ * Process a gadget call, yielding events in real-time.
1300
+ *
1301
+ * Key difference from processGadgetCall: yields gadget_call event IMMEDIATELY
1302
+ * when parsed (before execution), enabling real-time UI feedback.
1303
+ */
1304
+ private processGadgetCallGenerator;
1279
1305
  /**
1280
1306
  * Execute a gadget through the full hook lifecycle.
1281
1307
  * This is the core execution logic, extracted from processGadgetCall.
1308
+ * @deprecated Use executeGadgetGenerator for real-time streaming
1282
1309
  */
1283
1310
  private executeGadgetWithHooks;
1311
+ /**
1312
+ * Execute a gadget and yield the result event.
1313
+ * Generator version that yields gadget_result immediately when execution completes.
1314
+ */
1315
+ private executeGadgetGenerator;
1284
1316
  /**
1285
1317
  * Handle a gadget that cannot execute because a dependency failed.
1286
1318
  * Calls the onDependencySkipped controller to allow customization.
@@ -1291,6 +1323,14 @@ declare class StreamProcessor {
1291
1323
  * Executes ready gadgets in parallel and continues until no more can be triggered.
1292
1324
  */
1293
1325
  private processPendingGadgets;
1326
+ /**
1327
+ * Process pending gadgets, yielding events in real-time.
1328
+ * Generator version that yields events as gadgets complete.
1329
+ *
1330
+ * Note: Gadgets are still executed in parallel for efficiency,
1331
+ * but results are yielded as they become available.
1332
+ */
1333
+ private processPendingGadgetsGenerator;
1294
1334
  /**
1295
1335
  * Safely execute an observer, catching and logging any errors.
1296
1336
  * Observers are non-critical, so errors are logged but don't crash the system.
@@ -1302,17 +1342,17 @@ declare class StreamProcessor {
1302
1342
  */
1303
1343
  private runObserversInParallel;
1304
1344
  /**
1305
- * Check if execution should continue after an error.
1345
+ * Check if execution can recover from an error.
1306
1346
  *
1307
1347
  * Returns true if we should continue processing subsequent gadgets, false if we should stop.
1308
1348
  *
1309
1349
  * Logic:
1310
- * - If custom shouldContinueAfterError is provided, use it
1350
+ * - If custom canRecoverFromGadgetError is provided, use it
1311
1351
  * - Otherwise, use stopOnGadgetError config:
1312
1352
  * - stopOnGadgetError=true → return false (stop execution)
1313
1353
  * - stopOnGadgetError=false → return true (continue execution)
1314
1354
  */
1315
- private checkContinueAfterError;
1355
+ private checkCanRecoverFromError;
1316
1356
  /**
1317
1357
  * Determine the type of error from a gadget execution.
1318
1358
  */
@@ -1541,14 +1581,14 @@ interface CreateGadgetConfig<TSchema extends ZodType> {
1541
1581
  * .ask("What's the weather in Paris and what's 10 + 5?");
1542
1582
  * ```
1543
1583
  */
1544
- declare function createGadget<TSchema extends ZodType>(config: CreateGadgetConfig<TSchema>): BaseGadget;
1584
+ declare function createGadget<TSchema extends ZodType>(config: CreateGadgetConfig<TSchema>): AbstractGadget;
1545
1585
 
1546
1586
  /**
1547
- * Exception that gadgets can throw to signal the agent loop should terminate.
1587
+ * Signal that a gadget throws to indicate task completion and agent termination.
1548
1588
  *
1549
- * When a gadget throws this exception, the agent loop will:
1589
+ * When a gadget throws this signal, the agent loop will:
1550
1590
  * 1. Complete the current iteration
1551
- * 2. Return the exception message as the gadget's result
1591
+ * 2. Return the signal message as the gadget's result
1552
1592
  * 3. Exit the loop instead of continuing to the next iteration
1553
1593
  *
1554
1594
  * @example
@@ -1564,12 +1604,12 @@ declare function createGadget<TSchema extends ZodType>(config: CreateGadgetConfi
1564
1604
  * }) {
1565
1605
  * execute(params: this['params']): string {
1566
1606
  * const message = params.message || 'Task completed';
1567
- * throw new BreakLoopException(message);
1607
+ * throw new TaskCompletionSignal(message);
1568
1608
  * }
1569
1609
  * }
1570
1610
  * ```
1571
1611
  */
1572
- declare class BreakLoopException extends Error {
1612
+ declare class TaskCompletionSignal extends Error {
1573
1613
  constructor(message?: string);
1574
1614
  }
1575
1615
  /**
@@ -1577,7 +1617,7 @@ declare class BreakLoopException extends Error {
1577
1617
  *
1578
1618
  * When a gadget throws this exception, the agent loop will:
1579
1619
  * 1. Pause execution and wait for human input
1580
- * 2. If `onHumanInputRequired` callback is provided, call it and await the answer
1620
+ * 2. If `requestHumanInput` callback is provided, call it and await the answer
1581
1621
  * 3. Return the user's answer as the gadget's result
1582
1622
  * 4. Continue the loop with the answer added to conversation history
1583
1623
  *
@@ -1596,15 +1636,49 @@ declare class BreakLoopException extends Error {
1596
1636
  * }),
1597
1637
  * }) {
1598
1638
  * execute(params: this['params']): string {
1599
- * throw new HumanInputException(params.question);
1639
+ * throw new HumanInputRequiredException(params.question);
1600
1640
  * }
1601
1641
  * }
1602
1642
  * ```
1603
1643
  */
1604
- declare class HumanInputException extends Error {
1644
+ declare class HumanInputRequiredException extends Error {
1605
1645
  readonly question: string;
1606
1646
  constructor(question: string);
1607
1647
  }
1648
+ /**
1649
+ * Exception thrown when a gadget execution exceeds its timeout limit.
1650
+ *
1651
+ * When a gadget's execution time exceeds either:
1652
+ * - The gadget's own `timeoutMs` property, or
1653
+ * - The global `defaultGadgetTimeoutMs` configured in runtime/agent loop options
1654
+ *
1655
+ * The executor will automatically throw this exception and return it as an error.
1656
+ *
1657
+ * @example
1658
+ * ```typescript
1659
+ * import { z } from 'zod';
1660
+ *
1661
+ * class SlowApiGadget extends Gadget({
1662
+ * name: 'SlowApi',
1663
+ * description: 'Calls a slow external API',
1664
+ * timeoutMs: 5000, // 5 second timeout
1665
+ * schema: z.object({
1666
+ * endpoint: z.string(),
1667
+ * }),
1668
+ * }) {
1669
+ * async execute(params: this['params']): Promise<string> {
1670
+ * // If this takes longer than 5 seconds, execution will be aborted
1671
+ * const response = await fetch(params.endpoint);
1672
+ * return await response.text();
1673
+ * }
1674
+ * }
1675
+ * ```
1676
+ */
1677
+ declare class TimeoutException extends Error {
1678
+ readonly timeoutMs: number;
1679
+ readonly gadgetName: string;
1680
+ constructor(gadgetName: string, timeoutMs: number);
1681
+ }
1608
1682
  /**
1609
1683
  * Exception thrown when gadget execution is aborted.
1610
1684
  *
@@ -1620,7 +1694,7 @@ declare class HumanInputException extends Error {
1620
1694
  * schema: z.object({ data: z.string() }),
1621
1695
  * }) {
1622
1696
  * async execute(params: this['params'], ctx: ExecutionContext): Promise<string> {
1623
- * // Check at key points - throws AbortError if aborted
1697
+ * // Check at key points - throws AbortException if aborted
1624
1698
  * this.throwIfAborted(ctx);
1625
1699
  *
1626
1700
  * await this.doPartOne(params.data);
@@ -1634,7 +1708,7 @@ declare class HumanInputException extends Error {
1634
1708
  * }
1635
1709
  * ```
1636
1710
  */
1637
- declare class AbortError extends Error {
1711
+ declare class AbortException extends Error {
1638
1712
  constructor(message?: string);
1639
1713
  }
1640
1714
 
@@ -1649,25 +1723,28 @@ interface ErrorFormatterOptions {
1649
1723
 
1650
1724
  declare class GadgetExecutor {
1651
1725
  private readonly registry;
1652
- private readonly onHumanInputRequired?;
1726
+ private readonly requestHumanInput?;
1653
1727
  private readonly defaultGadgetTimeoutMs?;
1654
1728
  private readonly client?;
1655
1729
  private readonly mediaStore?;
1730
+ private readonly agentConfig?;
1731
+ private readonly subagentConfig?;
1732
+ private readonly onNestedEvent?;
1656
1733
  private readonly logger;
1657
1734
  private readonly errorFormatter;
1658
1735
  private readonly argPrefix;
1659
- constructor(registry: GadgetRegistry, onHumanInputRequired?: ((question: string) => Promise<string>) | undefined, logger?: Logger<ILogObj>, defaultGadgetTimeoutMs?: number | undefined, errorFormatterOptions?: ErrorFormatterOptions, client?: LLMist | undefined, mediaStore?: MediaStore | undefined);
1736
+ constructor(registry: GadgetRegistry, requestHumanInput?: ((question: string) => Promise<string>) | undefined, logger?: Logger<ILogObj>, defaultGadgetTimeoutMs?: number | undefined, errorFormatterOptions?: ErrorFormatterOptions, client?: LLMist | undefined, mediaStore?: MediaStore | undefined, agentConfig?: AgentContextConfig | undefined, subagentConfig?: SubagentConfigMap | undefined, onNestedEvent?: ((event: NestedAgentEvent) => void) | undefined);
1660
1737
  /**
1661
1738
  * Creates a promise that rejects with a TimeoutException after the specified timeout.
1662
1739
  * Aborts the provided AbortController before rejecting, allowing gadgets to clean up.
1663
1740
  */
1664
1741
  private createTimeoutPromise;
1665
1742
  /**
1666
- * Normalizes gadget execute result to consistent format.
1743
+ * Unify gadget execute result to consistent internal format.
1667
1744
  * Handles string returns (backwards compat), object returns with cost,
1668
1745
  * and object returns with media.
1669
1746
  */
1670
- private normalizeExecuteResult;
1747
+ private unifyExecuteResult;
1671
1748
  execute(call: ParsedGadgetCall): Promise<GadgetExecutionResult>;
1672
1749
  executeAll(calls: ParsedGadgetCall[]): Promise<GadgetExecutionResult[]>;
1673
1750
  /**
@@ -1694,7 +1771,7 @@ declare class GadgetExecutor {
1694
1771
  * registry.register("GadgetOutputViewer", viewer);
1695
1772
  * ```
1696
1773
  */
1697
- declare function createGadgetOutputViewer(store: GadgetOutputStore, maxOutputChars?: number): BaseGadget;
1774
+ declare function createGadgetOutputViewer(store: GadgetOutputStore, maxOutputChars?: number): AbstractGadget;
1698
1775
 
1699
1776
  interface StreamParserOptions {
1700
1777
  startPrefix?: string;
@@ -1702,16 +1779,24 @@ interface StreamParserOptions {
1702
1779
  /** Prefix for block format arguments. Default: "!!!ARG:" */
1703
1780
  argPrefix?: string;
1704
1781
  }
1705
- declare class StreamParser {
1782
+ /**
1783
+ * Parser for extracting gadget invocations from LLM text output.
1784
+ * Processes text chunks incrementally and emits events for text and gadget calls.
1785
+ */
1786
+ declare class GadgetCallParser {
1706
1787
  private buffer;
1707
- private lastReportedTextLength;
1788
+ private lastEmittedTextOffset;
1708
1789
  private readonly startPrefix;
1709
1790
  private readonly endPrefix;
1710
1791
  private readonly argPrefix;
1711
1792
  constructor(options?: StreamParserOptions);
1712
- private takeTextUntil;
1713
1793
  /**
1714
- * Parse gadget name with optional invocation ID and dependencies.
1794
+ * Extract and consume text up to the given index.
1795
+ * Returns undefined if no meaningful text to emit.
1796
+ */
1797
+ private extractTextSegment;
1798
+ /**
1799
+ * Parse gadget invocation metadata from the header line.
1715
1800
  *
1716
1801
  * Supported formats:
1717
1802
  * - `GadgetName` - Auto-generate ID, no dependencies
@@ -1720,7 +1805,7 @@ declare class StreamParser {
1720
1805
  *
1721
1806
  * Dependencies must be comma-separated invocation IDs.
1722
1807
  */
1723
- private parseGadgetName;
1808
+ private parseInvocationMetadata;
1724
1809
  /**
1725
1810
  * Extract the error message from a parse error.
1726
1811
  * Preserves full message since the error formatter adds contextual help
@@ -1925,7 +2010,7 @@ declare function Gadget<TSchema extends ZodType>(config: GadgetConfig<TSchema>):
1925
2010
  * @param options - Optional description, metadata, and fileName
1926
2011
  * @returns A GadgetMediaOutput ready to include in results
1927
2012
  */
1928
- declare function createMedia(kind: MediaKind, data: Buffer | Uint8Array, mimeType: string, options?: {
2013
+ declare function createMediaOutput(kind: MediaKind, data: Buffer | Uint8Array, mimeType: string, options?: {
1929
2014
  description?: string;
1930
2015
  metadata?: MediaMetadata;
1931
2016
  fileName?: string;
@@ -1944,8 +2029,8 @@ declare function createMedia(kind: MediaKind, data: Buffer | Uint8Array, mimeTyp
1944
2029
  * return resultWithMedia(
1945
2030
  * "Generated 2 charts",
1946
2031
  * [
1947
- * createMedia("image", barChartPng, "image/png", { description: "Bar chart" }),
1948
- * createMedia("image", pieChartPng, "image/png", { description: "Pie chart" }),
2032
+ * createMediaOutput("image", barChartPng, "image/png", { description: "Bar chart" }),
2033
+ * createMediaOutput("image", pieChartPng, "image/png", { description: "Pie chart" }),
1949
2034
  * ],
1950
2035
  * 0.002
1951
2036
  * );
@@ -2183,7 +2268,7 @@ declare function validateAndApplyDefaults<T = Record<string, unknown>>(schema: Z
2183
2268
  * }
2184
2269
  * ```
2185
2270
  */
2186
- declare function validateGadgetParams(gadget: BaseGadget, params: Record<string, unknown>): ValidationResult;
2271
+ declare function validateGadgetParams(gadget: AbstractGadget, params: Record<string, unknown>): ValidationResult;
2187
2272
 
2188
2273
  /**
2189
2274
  * Logger configuration options for the library.
@@ -2275,16 +2360,16 @@ declare abstract class BaseProviderAdapter implements ProviderAdapter {
2275
2360
  */
2276
2361
  protected prepareMessages(messages: LLMMessage[]): LLMMessage[];
2277
2362
  /**
2278
- * Build the provider-specific request payload.
2363
+ * Build the provider-specific API request.
2279
2364
  * This method must be implemented by each concrete provider.
2280
2365
  *
2281
2366
  * @param options - The generation options
2282
2367
  * @param descriptor - The model descriptor
2283
2368
  * @param spec - Optional model specification with metadata
2284
2369
  * @param messages - The prepared messages
2285
- * @returns Provider-specific payload ready for the API call
2370
+ * @returns Provider-specific request object ready for the API call
2286
2371
  */
2287
- protected abstract buildRequestPayload(options: LLMGenerationOptions, descriptor: ModelDescriptor, spec: ModelSpec | undefined, messages: LLMMessage[]): unknown;
2372
+ protected abstract buildApiRequest(options: LLMGenerationOptions, descriptor: ModelDescriptor, spec: ModelSpec | undefined, messages: LLMMessage[]): unknown;
2288
2373
  /**
2289
2374
  * Execute the stream request using the provider's SDK.
2290
2375
  * This method must be implemented by each concrete provider.
@@ -2295,13 +2380,13 @@ declare abstract class BaseProviderAdapter implements ProviderAdapter {
2295
2380
  */
2296
2381
  protected abstract executeStreamRequest(payload: unknown, signal?: AbortSignal): Promise<AsyncIterable<unknown>>;
2297
2382
  /**
2298
- * Wrap the provider-specific stream into the universal LLMStream format.
2383
+ * Normalize the provider-specific stream into the universal LLMStream format.
2299
2384
  * This method must be implemented by each concrete provider.
2300
2385
  *
2301
2386
  * @param rawStream - The provider-specific stream
2302
2387
  * @returns Universal LLMStream
2303
2388
  */
2304
- protected abstract wrapStream(rawStream: AsyncIterable<unknown>): LLMStream;
2389
+ protected abstract normalizeProviderStream(rawStream: AsyncIterable<unknown>): LLMStream;
2305
2390
  }
2306
2391
 
2307
2392
  declare class AnthropicMessagesProvider extends BaseProviderAdapter {
@@ -2312,7 +2397,7 @@ declare class AnthropicMessagesProvider extends BaseProviderAdapter {
2312
2397
  generateImage(): Promise<never>;
2313
2398
  supportsSpeechGeneration(_modelId: string): boolean;
2314
2399
  generateSpeech(): Promise<never>;
2315
- protected buildRequestPayload(options: LLMGenerationOptions, descriptor: ModelDescriptor, spec: ModelSpec | undefined, messages: LLMMessage[]): MessageCreateParamsStreaming;
2400
+ protected buildApiRequest(options: LLMGenerationOptions, descriptor: ModelDescriptor, spec: ModelSpec | undefined, messages: LLMMessage[]): MessageCreateParamsStreaming;
2316
2401
  /**
2317
2402
  * Convert llmist content to Anthropic's content block format.
2318
2403
  * Handles text, images (base64 only), and applies cache_control.
@@ -2323,7 +2408,7 @@ declare class AnthropicMessagesProvider extends BaseProviderAdapter {
2323
2408
  */
2324
2409
  private convertImagePart;
2325
2410
  protected executeStreamRequest(payload: MessageCreateParamsStreaming, signal?: AbortSignal): Promise<AsyncIterable<MessageStreamEvent>>;
2326
- protected wrapStream(iterable: AsyncIterable<unknown>): LLMStream;
2411
+ protected normalizeProviderStream(iterable: AsyncIterable<unknown>): LLMStream;
2327
2412
  /**
2328
2413
  * Count tokens in messages using Anthropic's native token counting API.
2329
2414
  *
@@ -2398,7 +2483,7 @@ declare class GeminiGenerativeProvider extends BaseProviderAdapter {
2398
2483
  getSpeechModelSpecs(): SpeechModelSpec[];
2399
2484
  supportsSpeechGeneration(modelId: string): boolean;
2400
2485
  generateSpeech(options: SpeechGenerationOptions): Promise<SpeechGenerationResult>;
2401
- protected buildRequestPayload(options: LLMGenerationOptions, descriptor: ModelDescriptor, _spec: ModelSpec | undefined, messages: LLMMessage[]): {
2486
+ protected buildApiRequest(options: LLMGenerationOptions, descriptor: ModelDescriptor, _spec: ModelSpec | undefined, messages: LLMMessage[]): {
2402
2487
  model: string;
2403
2488
  contents: GeminiContent[];
2404
2489
  config: Record<string, unknown>;
@@ -2434,8 +2519,8 @@ declare class GeminiGenerativeProvider extends BaseProviderAdapter {
2434
2519
  */
2435
2520
  private convertToGeminiParts;
2436
2521
  private buildGenerationConfig;
2437
- protected wrapStream(iterable: AsyncIterable<unknown>): LLMStream;
2438
- private extractText;
2522
+ protected normalizeProviderStream(iterable: AsyncIterable<unknown>): LLMStream;
2523
+ private extractMessageText;
2439
2524
  private extractFinishReason;
2440
2525
  private extractUsage;
2441
2526
  /**
@@ -2475,7 +2560,7 @@ declare class OpenAIChatProvider extends BaseProviderAdapter {
2475
2560
  getSpeechModelSpecs(): SpeechModelSpec[];
2476
2561
  supportsSpeechGeneration(modelId: string): boolean;
2477
2562
  generateSpeech(options: SpeechGenerationOptions): Promise<SpeechGenerationResult>;
2478
- protected buildRequestPayload(options: LLMGenerationOptions, descriptor: ModelDescriptor, spec: ModelSpec | undefined, messages: LLMMessage[]): Parameters<OpenAI["chat"]["completions"]["create"]>[0];
2563
+ protected buildApiRequest(options: LLMGenerationOptions, descriptor: ModelDescriptor, spec: ModelSpec | undefined, messages: LLMMessage[]): Parameters<OpenAI["chat"]["completions"]["create"]>[0];
2479
2564
  /**
2480
2565
  * Convert an LLMMessage to OpenAI's ChatCompletionMessageParam.
2481
2566
  * Handles role-specific content type requirements:
@@ -2494,7 +2579,7 @@ declare class OpenAIChatProvider extends BaseProviderAdapter {
2494
2579
  */
2495
2580
  private convertImagePart;
2496
2581
  protected executeStreamRequest(payload: Parameters<OpenAI["chat"]["completions"]["create"]>[0], signal?: AbortSignal): Promise<AsyncIterable<ChatCompletionChunk>>;
2497
- protected wrapStream(iterable: AsyncIterable<unknown>): LLMStream;
2582
+ protected normalizeProviderStream(iterable: AsyncIterable<unknown>): LLMStream;
2498
2583
  /**
2499
2584
  * Count tokens in messages using OpenAI's tiktoken library.
2500
2585
  *
@@ -2522,4 +2607,4 @@ declare class OpenAIChatProvider extends BaseProviderAdapter {
2522
2607
  }
2523
2608
  declare function createOpenAIProviderFromEnv(): OpenAIChatProvider | null;
2524
2609
 
2525
- export { AbortError, AgentHooks, AnthropicMessagesProvider, BaseGadget, BreakLoopException, CompactionConfig, CompactionContext, CompactionEvent, CompactionManager, CompactionResult, CompactionStats, CompactionStrategy, ConversationManager, type CreateGadgetConfig, ExecutionContext, Gadget, type GadgetConfig, GadgetExample, GadgetExecuteResultWithMedia, GadgetExecuteReturn, GadgetExecutionResult, GadgetExecutor, GadgetMediaOutput, GadgetOutputStore, GadgetRegistry, GeminiGenerativeProvider, HintTemplate, type HintsConfig, HookPresets, HumanInputException, HybridStrategy, IConversationManager, type IterationHintOptions, LLMGenerationOptions, LLMMessage, LLMStream, LLMStreamChunk, LLMist, type LoggerOptions, type LoggingOptions, MODEL_ALIASES, MediaKind, MediaMetadata, MediaStore, MessageContent, ModelDescriptor, ModelRegistry, ModelSpec, OpenAIChatProvider, type ParallelGadgetHintOptions, ParsedGadgetCall, ProviderAdapter, ResolvedCompactionConfig, SlidingWindowStrategy, type StoredOutput, StreamEvent, StreamParser, type StreamProcessingResult, StreamProcessor, type StreamProcessorOptions, SummarizationStrategy, TokenUsage, type ValidationIssue, type ValidationResult, createAnthropicProviderFromEnv, createGadget, createGadgetOutputViewer, createGeminiProviderFromEnv, createHints, createLogger, createMedia, createOpenAIProviderFromEnv, defaultLogger, discoverProviderAdapters, getModelId, getProvider, hasProviderPrefix, iterationProgressHint, parallelGadgetHint, resolveModel, resultWithAudio, resultWithFile, resultWithImage, resultWithImages, resultWithMedia, validateAndApplyDefaults, validateGadgetParams };
2610
+ export { AbortException, AbstractGadget, AgentHooks, AnthropicMessagesProvider, CompactionConfig, CompactionContext, CompactionEvent, CompactionManager, CompactionResult, CompactionStats, CompactionStrategy, ConversationManager, type CreateGadgetConfig, ExecutionContext, Gadget, GadgetCallParser, type GadgetConfig, GadgetExample, GadgetExecuteResultWithMedia, GadgetExecuteReturn, GadgetExecutionResult, GadgetExecutor, GadgetMediaOutput, GadgetOutputStore, GadgetRegistry, GeminiGenerativeProvider, HintTemplate, type HintsConfig, HookPresets, HumanInputRequiredException, HybridStrategy, IConversationManager, type IterationHintOptions, LLMGenerationOptions, LLMMessage, LLMStream, LLMStreamChunk, LLMist, type LoggerOptions, type LoggingOptions, MODEL_ALIASES, MediaKind, MediaMetadata, MediaStore, MessageContent, ModelDescriptor, ModelRegistry, ModelSpec, OpenAIChatProvider, type ParallelGadgetHintOptions, ParsedGadgetCall, ProviderAdapter, ResolvedCompactionConfig, SlidingWindowStrategy, type StoredOutput, StreamEvent, type StreamProcessingResult, StreamProcessor, type StreamProcessorOptions, SummarizationStrategy, TaskCompletionSignal, TimeoutException, TokenUsage, type ValidationIssue, type ValidationResult, createAnthropicProviderFromEnv, createGadget, createGadgetOutputViewer, createGeminiProviderFromEnv, createHints, createLogger, createMediaOutput, createOpenAIProviderFromEnv, defaultLogger, discoverProviderAdapters, getModelId, getProvider, hasProviderPrefix, iterationProgressHint, parallelGadgetHint, resolveModel, resultWithAudio, resultWithFile, resultWithImage, resultWithImages, resultWithMedia, validateAndApplyDefaults, validateGadgetParams };
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  Gadget,
3
3
  HookPresets,
4
4
  createHints,
5
- createMedia,
5
+ createMediaOutput,
6
6
  iterationProgressHint,
7
7
  parallelGadgetHint,
8
8
  resultWithAudio,
@@ -11,24 +11,24 @@ import {
11
11
  resultWithImages,
12
12
  resultWithMedia,
13
13
  z
14
- } from "./chunk-4IHLIYW5.js";
14
+ } from "./chunk-LFI4WQVV.js";
15
15
  import {
16
- AbortError,
16
+ AbortException,
17
+ AbstractGadget,
17
18
  AgentBuilder,
18
19
  AnthropicMessagesProvider,
19
- BaseGadget,
20
- BreakLoopException,
21
20
  CompactionManager,
22
21
  ConversationManager,
23
22
  DEFAULT_COMPACTION_CONFIG,
24
23
  DEFAULT_HINTS,
25
24
  DEFAULT_PROMPTS,
26
25
  DEFAULT_SUMMARIZATION_PROMPT,
26
+ GadgetCallParser,
27
27
  GadgetExecutor,
28
28
  GadgetOutputStore,
29
29
  GadgetRegistry,
30
30
  GeminiGenerativeProvider,
31
- HumanInputException,
31
+ HumanInputRequiredException,
32
32
  HybridStrategy,
33
33
  LLMMessageBuilder,
34
34
  LLMist,
@@ -41,9 +41,10 @@ import {
41
41
  ModelRegistry,
42
42
  OpenAIChatProvider,
43
43
  SlidingWindowStrategy,
44
- StreamParser,
45
44
  StreamProcessor,
46
45
  SummarizationStrategy,
46
+ TaskCompletionSignal,
47
+ TimeoutException,
47
48
  audioFromBase64,
48
49
  audioFromBuffer,
49
50
  collectEvents,
@@ -63,7 +64,7 @@ import {
63
64
  detectAudioMimeType,
64
65
  detectImageMimeType,
65
66
  discoverProviderAdapters,
66
- extractText,
67
+ extractMessageText,
67
68
  getMockManager,
68
69
  getModelId,
69
70
  getProvider,
@@ -76,7 +77,7 @@ import {
76
77
  isImagePart,
77
78
  isTextPart,
78
79
  mockLLM,
79
- normalizeContent,
80
+ normalizeMessageContent,
80
81
  parseDataUrl,
81
82
  resolveHintTemplate,
82
83
  resolveModel,
@@ -88,13 +89,12 @@ import {
88
89
  toBase64,
89
90
  validateAndApplyDefaults,
90
91
  validateGadgetParams
91
- } from "./chunk-364PEMVT.js";
92
+ } from "./chunk-JCFPJMRQ.js";
92
93
  export {
93
- AbortError,
94
+ AbortException,
95
+ AbstractGadget,
94
96
  AgentBuilder,
95
97
  AnthropicMessagesProvider,
96
- BaseGadget,
97
- BreakLoopException,
98
98
  CompactionManager,
99
99
  ConversationManager,
100
100
  DEFAULT_COMPACTION_CONFIG,
@@ -102,12 +102,13 @@ export {
102
102
  DEFAULT_PROMPTS,
103
103
  DEFAULT_SUMMARIZATION_PROMPT,
104
104
  Gadget,
105
+ GadgetCallParser,
105
106
  GadgetExecutor,
106
107
  GadgetOutputStore,
107
108
  GadgetRegistry,
108
109
  GeminiGenerativeProvider,
109
110
  HookPresets,
110
- HumanInputException,
111
+ HumanInputRequiredException,
111
112
  HybridStrategy,
112
113
  LLMMessageBuilder,
113
114
  LLMist,
@@ -120,9 +121,10 @@ export {
120
121
  ModelRegistry,
121
122
  OpenAIChatProvider,
122
123
  SlidingWindowStrategy,
123
- StreamParser,
124
124
  StreamProcessor,
125
125
  SummarizationStrategy,
126
+ TaskCompletionSignal,
127
+ TimeoutException,
126
128
  audioFromBase64,
127
129
  audioFromBuffer,
128
130
  collectEvents,
@@ -134,7 +136,7 @@ export {
134
136
  createGeminiProviderFromEnv,
135
137
  createHints,
136
138
  createLogger,
137
- createMedia,
139
+ createMediaOutput,
138
140
  createMockAdapter,
139
141
  createMockClient,
140
142
  createMockStream,
@@ -144,7 +146,7 @@ export {
144
146
  detectAudioMimeType,
145
147
  detectImageMimeType,
146
148
  discoverProviderAdapters,
147
- extractText,
149
+ extractMessageText,
148
150
  getMockManager,
149
151
  getModelId,
150
152
  getProvider,
@@ -158,7 +160,7 @@ export {
158
160
  isTextPart,
159
161
  iterationProgressHint,
160
162
  mockLLM,
161
- normalizeContent,
163
+ normalizeMessageContent,
162
164
  parallelGadgetHint,
163
165
  parseDataUrl,
164
166
  resolveHintTemplate,