@jaypie/llm 1.3.6 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  export { default as Llm } from "./Llm.js";
2
2
  export * as LLM from "./constants.js";
3
- export type { LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProvider, } from "./types/LlmProvider.interface.js";
4
- export { LlmMessageRole, LlmMessageType, } from "./types/LlmProvider.interface.js";
3
+ export type { LlmFallbackConfig, LlmHistory, LlmInputContent, LlmInputContentFile, LlmInputContentImage, LlmInputContentText, LlmInputMessage, LlmMessageOptions, LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage, LlmOperateOptions, LlmOperateResponse, LlmOptions, LlmProgressCallback, LlmProgressEvent, LlmProgressToolCall, LlmProvider, } from "./types/LlmProvider.interface.js";
4
+ export { LlmMessageRole, LlmMessageType, LlmProgressEventType, } from "./types/LlmProvider.interface.js";
5
5
  export { isLlmOperateInput, isLlmOperateInputContent, isLlmOperateInputFile, isLlmOperateInputImage, } from "./types/LlmOperateInput.guards.js";
6
6
  export type { LlmTool } from "./types/LlmTool.interface.js";
7
7
  export type { LlmStreamChunk, LlmStreamChunkDone, LlmStreamChunkError, LlmStreamChunkText, LlmStreamChunkToolCall, LlmStreamChunkToolResult, } from "./types/LlmStreamChunk.interface.js";
@@ -34,6 +34,11 @@ export declare class OperateLoop {
34
34
  private createContext;
35
35
  private buildInitialRequest;
36
36
  private executeOneTurn;
37
+ /**
38
+ * Draconian summary of a history item for trace logging: string message
39
+ * content is kept; everything else is reduced to its type.
40
+ */
41
+ private summarizeHistoryItem;
37
42
  /**
38
43
  * Reconcile structured output against the declared `format` contract. A
39
44
  * declared `format` is a schema contract: returned keys should match the
@@ -15,16 +15,19 @@ export interface AfterModelResponseContext {
15
15
  }
16
16
  export interface BeforeToolContext {
17
17
  args: string;
18
+ message?: string;
18
19
  toolName: string;
19
20
  }
20
21
  export interface AfterToolContext {
21
22
  args: string;
23
+ message?: string;
22
24
  result: unknown;
23
25
  toolName: string;
24
26
  }
25
27
  export interface ToolErrorContext {
26
28
  args: string;
27
29
  error: Error;
30
+ message?: string;
28
31
  toolName: string;
29
32
  }
30
33
  export interface RetryableErrorContext {
@@ -0,0 +1,10 @@
1
+ import { LlmProgressCallback, LlmProgressEvent } from "../../types/LlmProvider.interface.js";
2
+ /**
3
+ * Deliver a progress event to the caller's onProgress callback.
4
+ * Errors thrown by the callback are logged and swallowed — progress
5
+ * reporting must never interrupt the operate loop.
6
+ */
7
+ export declare function emitProgress({ event, onProgress, }: {
8
+ event: LlmProgressEvent;
9
+ onProgress?: LlmProgressCallback;
10
+ }): Promise<void>;
@@ -0,0 +1 @@
1
+ export { emitProgress } from "./emitProgress.js";
@@ -14,9 +14,21 @@ export declare class Toolkit {
14
14
  private readonly log;
15
15
  constructor(tools: LlmTool[], options?: ToolkitOptions);
16
16
  get tools(): Omit<LlmTool, "call">[];
17
- call({ name, arguments: args }: {
17
+ private parseArgs;
18
+ /**
19
+ * Resolve a tool's `message` (static string or function of args) without
20
+ * calling the tool. Returns undefined when the tool is missing or defines
21
+ * no message. Never throws; resolution errors log at warn.
22
+ */
23
+ resolveMessage({ name, arguments: args, }: {
18
24
  name: string;
19
25
  arguments: string;
26
+ }): Promise<string | undefined>;
27
+ call({ name, arguments: args, message: resolvedMessage, }: {
28
+ name: string;
29
+ arguments: string;
30
+ /** Pre-resolved tool message; skips re-resolving `tool.message` for logging */
31
+ message?: string;
20
32
  }): Promise<import("@jaypie/types").AnyValue>;
21
33
  extend(tools: LlmTool[], options?: {
22
34
  warn?: boolean;
@@ -166,6 +166,51 @@ export interface LlmMessageOptions {
166
166
  response?: NaturalSchema | z.ZodType;
167
167
  system?: string;
168
168
  }
169
+ export declare enum LlmProgressEventType {
170
+ Done = "done",
171
+ ModelRequest = "model_request",
172
+ ModelResponse = "model_response",
173
+ Retry = "retry",
174
+ Start = "start",
175
+ ToolCall = "tool_call",
176
+ ToolError = "tool_error",
177
+ ToolResult = "tool_result"
178
+ }
179
+ export interface LlmProgressToolCall {
180
+ /** JSON string of arguments (tool_call events only) */
181
+ arguments?: string;
182
+ /** Resolved `LlmTool.message`, when the tool defines one (tool_call events only) */
183
+ message?: string;
184
+ /** Tool name */
185
+ name: string;
186
+ }
187
+ /**
188
+ * Lightweight, serializable progress event emitted by the operate loop.
189
+ * Suitable for forwarding directly to websockets, queues, or UI updates.
190
+ */
191
+ export interface LlmProgressEvent {
192
+ /** Text or structured content (model_response, done) */
193
+ content?: string | JsonObject;
194
+ /** Error message (tool_error, retry) */
195
+ error?: string;
196
+ /** Maximum turns allowed for the loop (start) */
197
+ maxTurns?: number;
198
+ /** Model handling the request (start, model_request) */
199
+ model?: string;
200
+ /** Provider name (start) */
201
+ provider?: string;
202
+ /** The tool involved (tool_call, tool_result, tool_error) */
203
+ tool?: LlmProgressToolCall;
204
+ /** Tools the model requested (model_response) */
205
+ toolCalls?: LlmProgressToolCall[];
206
+ /** Current turn, 1-indexed */
207
+ turn?: number;
208
+ /** Event type */
209
+ type: LlmProgressEventType;
210
+ /** Usage: this turn for model_response, cumulative for done */
211
+ usage?: LlmUsage;
212
+ }
213
+ export type LlmProgressCallback = (event: LlmProgressEvent) => unknown | Promise<unknown>;
169
214
  export interface LlmOperateOptions {
170
215
  data?: NaturalMap;
171
216
  explain?: boolean;
@@ -182,19 +227,23 @@ export interface LlmOperateOptions {
182
227
  content: string | JsonObject;
183
228
  usage: LlmUsage;
184
229
  }) => unknown | Promise<unknown>;
185
- afterEachTool?: ({ result, toolName, args, }: {
230
+ afterEachTool?: ({ result, toolName, args, message, }: {
186
231
  result: unknown;
187
232
  toolName: string;
188
233
  args: string;
234
+ /** Resolved `LlmTool.message`, when the tool defines one */
235
+ message?: string;
189
236
  }) => unknown | Promise<unknown>;
190
237
  beforeEachModelRequest?: ({ input, options, providerRequest, }: {
191
238
  input: string | LlmHistory | LlmInputMessage;
192
239
  options?: LlmOperateOptions;
193
240
  providerRequest: any;
194
241
  }) => unknown | Promise<unknown>;
195
- beforeEachTool?: ({ toolName, args, }: {
242
+ beforeEachTool?: ({ toolName, args, message, }: {
196
243
  toolName: string;
197
244
  args: string;
245
+ /** Resolved `LlmTool.message`, when the tool defines one */
246
+ message?: string;
198
247
  }) => unknown | Promise<unknown>;
199
248
  onRetryableModelError?: ({ input, options, providerRequest, error, }: {
200
249
  input: string | LlmHistory | LlmInputMessage;
@@ -202,10 +251,12 @@ export interface LlmOperateOptions {
202
251
  providerRequest: any;
203
252
  error: any;
204
253
  }) => unknown | Promise<unknown>;
205
- onToolError?: ({ error, toolName, args, }: {
254
+ onToolError?: ({ error, toolName, args, message, }: {
206
255
  error: Error;
207
256
  toolName: string;
208
257
  args: string;
258
+ /** Resolved `LlmTool.message`, when the tool defines one */
259
+ message?: string;
209
260
  }) => unknown | Promise<unknown>;
210
261
  onUnrecoverableModelError?: ({ input, options, providerRequest, error, }: {
211
262
  input: string | LlmHistory | LlmInputMessage;
@@ -216,6 +267,13 @@ export interface LlmOperateOptions {
216
267
  };
217
268
  instructions?: string;
218
269
  model?: string;
270
+ /**
271
+ * Receives lightweight progress events as the operate loop runs:
272
+ * start, model_request, model_response, tool_call, tool_result,
273
+ * tool_error, retry, done. Errors thrown by the callback are logged
274
+ * and never interrupt the loop.
275
+ */
276
+ onProgress?: LlmProgressCallback;
219
277
  placeholders?: {
220
278
  input?: boolean;
221
279
  instructions?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/llm",
3
- "version": "1.3.6",
3
+ "version": "1.3.8",
4
4
  "description": "Large language model utilities",
5
5
  "repository": {
6
6
  "type": "git",