@jaypie/llm 1.2.1 → 1.2.3

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.
Files changed (47) hide show
  1. package/dist/cjs/Llm.d.ts +5 -5
  2. package/dist/cjs/constants.d.ts +33 -77
  3. package/dist/cjs/index.cjs +725 -112
  4. package/dist/cjs/index.cjs.map +1 -1
  5. package/dist/cjs/index.d.ts +3 -1
  6. package/dist/cjs/operate/OperateLoop.d.ts +2 -2
  7. package/dist/cjs/operate/StreamLoop.d.ts +2 -2
  8. package/dist/cjs/operate/adapters/AnthropicAdapter.d.ts +1 -1
  9. package/dist/cjs/operate/adapters/GeminiAdapter.d.ts +2 -1
  10. package/dist/cjs/operate/adapters/OpenAiAdapter.d.ts +1 -1
  11. package/dist/cjs/operate/adapters/OpenRouterAdapter.d.ts +12 -2
  12. package/dist/cjs/operate/input/InputProcessor.d.ts +3 -3
  13. package/dist/cjs/operate/response/ResponseBuilder.d.ts +14 -1
  14. package/dist/cjs/types/LlmOperateInput.guards.d.ts +17 -0
  15. package/dist/cjs/types/LlmProvider.interface.d.ts +48 -2
  16. package/dist/cjs/upload/detectMimeType.d.ts +16 -0
  17. package/dist/cjs/upload/extractPdfPages.d.ts +13 -0
  18. package/dist/cjs/upload/index.d.ts +5 -0
  19. package/dist/cjs/upload/loadLocalFile.d.ts +6 -0
  20. package/dist/cjs/upload/loadS3File.d.ts +7 -0
  21. package/dist/cjs/upload/resolveOperateInput.d.ts +12 -0
  22. package/dist/cjs/util/extractReasoning.d.ts +14 -0
  23. package/dist/cjs/util/index.d.ts +1 -0
  24. package/dist/esm/Llm.d.ts +5 -5
  25. package/dist/esm/constants.d.ts +33 -77
  26. package/dist/esm/index.d.ts +3 -1
  27. package/dist/esm/index.js +722 -114
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/operate/OperateLoop.d.ts +2 -2
  30. package/dist/esm/operate/StreamLoop.d.ts +2 -2
  31. package/dist/esm/operate/adapters/AnthropicAdapter.d.ts +1 -1
  32. package/dist/esm/operate/adapters/GeminiAdapter.d.ts +2 -1
  33. package/dist/esm/operate/adapters/OpenAiAdapter.d.ts +1 -1
  34. package/dist/esm/operate/adapters/OpenRouterAdapter.d.ts +12 -2
  35. package/dist/esm/operate/input/InputProcessor.d.ts +3 -3
  36. package/dist/esm/operate/response/ResponseBuilder.d.ts +14 -1
  37. package/dist/esm/types/LlmOperateInput.guards.d.ts +17 -0
  38. package/dist/esm/types/LlmProvider.interface.d.ts +48 -2
  39. package/dist/esm/upload/detectMimeType.d.ts +16 -0
  40. package/dist/esm/upload/extractPdfPages.d.ts +13 -0
  41. package/dist/esm/upload/index.d.ts +5 -0
  42. package/dist/esm/upload/loadLocalFile.d.ts +6 -0
  43. package/dist/esm/upload/loadS3File.d.ts +7 -0
  44. package/dist/esm/upload/resolveOperateInput.d.ts +12 -0
  45. package/dist/esm/util/extractReasoning.d.ts +14 -0
  46. package/dist/esm/util/index.d.ts +1 -0
  47. package/package.json +2 -1
@@ -1,4 +1,4 @@
1
- import { LlmHistory, LlmInputMessage, LlmOperateOptions, LlmOperateResponse } from "../types/LlmProvider.interface.js";
1
+ import { LlmHistory, LlmInputMessage, LlmOperateInput, LlmOperateOptions, LlmOperateResponse } from "../types/LlmProvider.interface.js";
2
2
  import { ProviderAdapter } from "./adapters/ProviderAdapter.interface.js";
3
3
  import { HookRunner } from "./hooks/index.js";
4
4
  import { InputProcessor } from "./input/index.js";
@@ -28,7 +28,7 @@ export declare class OperateLoop {
28
28
  /**
29
29
  * Execute the operate loop for multi-turn conversations with tool calling.
30
30
  */
31
- execute(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
31
+ execute(input: string | LlmHistory | LlmInputMessage | LlmOperateInput, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
32
32
  private initializeState;
33
33
  private createContext;
34
34
  private buildInitialRequest;
@@ -1,4 +1,4 @@
1
- import { LlmHistory, LlmInputMessage, LlmOperateOptions } from "../types/LlmProvider.interface.js";
1
+ import { LlmHistory, LlmInputMessage, LlmOperateInput, LlmOperateOptions } from "../types/LlmProvider.interface.js";
2
2
  import { LlmStreamChunk } from "../types/LlmStreamChunk.interface.js";
3
3
  import { ProviderAdapter } from "./adapters/ProviderAdapter.interface.js";
4
4
  import { HookRunner } from "./hooks/index.js";
@@ -24,7 +24,7 @@ export declare class StreamLoop {
24
24
  * Execute the streaming loop for multi-turn conversations with tool calling.
25
25
  * Yields stream chunks as they become available.
26
26
  */
27
- execute(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): AsyncIterable<LlmStreamChunk>;
27
+ execute(input: string | LlmHistory | LlmInputMessage | LlmOperateInput, options?: LlmOperateOptions): AsyncIterable<LlmStreamChunk>;
28
28
  private initializeState;
29
29
  private createContext;
30
30
  private buildInitialRequest;
@@ -13,7 +13,7 @@ import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
13
13
  */
14
14
  export declare class AnthropicAdapter extends BaseProviderAdapter {
15
15
  readonly name: "anthropic";
16
- readonly defaultModel: "claude-opus-4-1";
16
+ readonly defaultModel: "claude-sonnet-4-5";
17
17
  buildRequest(request: OperateRequest): Anthropic.MessageCreateParams;
18
18
  formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
19
19
  formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
@@ -13,7 +13,7 @@ import { GeminiPart, GeminiRawResponse, GeminiRequest } from "../../providers/ge
13
13
  */
14
14
  export declare class GeminiAdapter extends BaseProviderAdapter {
15
15
  readonly name: "gemini";
16
- readonly defaultModel: "gemini-2.5-flash";
16
+ readonly defaultModel: "gemini-3-pro-preview";
17
17
  buildRequest(request: OperateRequest): GeminiRequest;
18
18
  formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
19
19
  formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
@@ -30,6 +30,7 @@ export declare class GeminiAdapter extends BaseProviderAdapter {
30
30
  hasStructuredOutput(response: unknown): boolean;
31
31
  extractStructuredOutput(response: unknown): JsonObject | undefined;
32
32
  private convertMessagesToContents;
33
+ private convertContentToParts;
33
34
  private mapRole;
34
35
  private hasToolCalls;
35
36
  private getFinishReason;
@@ -12,7 +12,7 @@ import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
12
12
  */
13
13
  export declare class OpenAiAdapter extends BaseProviderAdapter {
14
14
  readonly name: "openai";
15
- readonly defaultModel: "gpt-5";
15
+ readonly defaultModel: "gpt-5.1";
16
16
  buildRequest(request: OperateRequest): unknown;
17
17
  formatTools(toolkit: Toolkit, _outputSchema?: JsonObject): ProviderToolDefinition[];
18
18
  formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
@@ -7,7 +7,7 @@ import { ClassifiedError, OperateRequest, ParsedResponse, ProviderToolDefinition
7
7
  import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
8
8
  interface OpenRouterMessage {
9
9
  role: "system" | "user" | "assistant" | "tool";
10
- content?: string | null;
10
+ content?: string | OpenRouterContentPart[] | null;
11
11
  toolCalls?: OpenRouterToolCall[];
12
12
  toolCallId?: string;
13
13
  }
@@ -39,6 +39,9 @@ interface OpenRouterUsage {
39
39
  prompt_tokens?: number;
40
40
  completion_tokens?: number;
41
41
  total_tokens?: number;
42
+ completionTokensDetails?: {
43
+ reasoningTokens?: number;
44
+ };
42
45
  }
43
46
  interface OpenRouterResponse {
44
47
  id: string;
@@ -66,6 +69,13 @@ interface OpenRouterRequest {
66
69
  };
67
70
  user?: string;
68
71
  }
72
+ /**
73
+ * OpenRouter content part types (text only - images/files not supported)
74
+ */
75
+ type OpenRouterContentPart = {
76
+ type: "text";
77
+ text: string;
78
+ };
69
79
  /**
70
80
  * OpenRouterAdapter implements the ProviderAdapter interface for OpenRouter's API.
71
81
  * OpenRouter provides a unified API to access hundreds of AI models through OpenAI-compatible endpoints.
@@ -74,7 +84,7 @@ interface OpenRouterRequest {
74
84
  */
75
85
  export declare class OpenRouterAdapter extends BaseProviderAdapter {
76
86
  readonly name: "openrouter";
77
- readonly defaultModel: "openai/gpt-4o";
87
+ readonly defaultModel: "z-ai/glm-4.7";
78
88
  buildRequest(request: OperateRequest): OpenRouterRequest;
79
89
  formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
80
90
  formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
@@ -1,4 +1,4 @@
1
- import { LlmHistory, LlmInputMessage, LlmOperateOptions } from "../../types/LlmProvider.interface.js";
1
+ import { LlmHistory, LlmInputMessage, LlmOperateInput, LlmOperateOptions } from "../../types/LlmProvider.interface.js";
2
2
  export interface ProcessedInput {
3
3
  /** The processed history with all messages formatted */
4
4
  history: LlmHistory;
@@ -15,11 +15,11 @@ export declare class InputProcessor {
15
15
  /**
16
16
  * Process input with placeholders, history merging, and system message handling
17
17
  *
18
- * @param input - The raw input (string, message, or history)
18
+ * @param input - The raw input (string, message, history, or LlmOperateInput)
19
19
  * @param options - The operate options containing data, history, system, etc.
20
20
  * @returns Processed input with all transformations applied
21
21
  */
22
- process(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): ProcessedInput;
22
+ process(input: string | LlmHistory | LlmInputMessage | LlmOperateInput, options?: LlmOperateOptions): Promise<ProcessedInput>;
23
23
  /**
24
24
  * Format input and apply placeholders if data is provided
25
25
  */
@@ -45,6 +45,18 @@ export declare class ResponseBuilder {
45
45
  * Append items to the output
46
46
  */
47
47
  appendToOutput(...items: LlmOutput): this;
48
+ /**
49
+ * Set the reasoning array
50
+ */
51
+ setReasoning(reasoning: string[]): this;
52
+ /**
53
+ * Append reasoning text items
54
+ */
55
+ appendToReasoning(...items: string[]): this;
56
+ /**
57
+ * Get the current reasoning array
58
+ */
59
+ getReasoning(): string[];
48
60
  /**
49
61
  * Add a raw provider response
50
62
  */
@@ -78,7 +90,8 @@ export declare class ResponseBuilder {
78
90
  */
79
91
  incomplete(): this;
80
92
  /**
81
- * Build and return the final response object
93
+ * Build and return the final response object.
94
+ * Automatically extracts reasoning from history if not already set.
82
95
  */
83
96
  build(): LlmOperateResponse;
84
97
  }
@@ -0,0 +1,17 @@
1
+ import type { LlmOperateInput, LlmOperateInputContent, LlmOperateInputFile, LlmOperateInputImage } from "./LlmProvider.interface.js";
2
+ /**
3
+ * Type guard to check if an item is an LlmOperateInputFile
4
+ */
5
+ export declare function isLlmOperateInputFile(item: unknown): item is LlmOperateInputFile;
6
+ /**
7
+ * Type guard to check if an item is an LlmOperateInputImage
8
+ */
9
+ export declare function isLlmOperateInputImage(item: unknown): item is LlmOperateInputImage;
10
+ /**
11
+ * Type guard to check if an item is an LlmOperateInputContent
12
+ */
13
+ export declare function isLlmOperateInputContent(item: unknown): item is LlmOperateInputContent;
14
+ /**
15
+ * Type guard to check if input is an LlmOperateInput array
16
+ */
17
+ export declare function isLlmOperateInput(input: unknown): input is LlmOperateInput;
@@ -56,6 +56,51 @@ export interface LlmInputMessage {
56
56
  role: LlmMessageRole;
57
57
  type?: LlmMessageType.Message;
58
58
  }
59
+ /**
60
+ * File input that can be loaded from local filesystem, S3, or provided as base64 data.
61
+ * For PDFs, specific pages can be extracted via the `pages` property.
62
+ * If the file extension is an image type, it will be treated as an image internally.
63
+ */
64
+ export interface LlmOperateInputFile {
65
+ /** Path or filename (extension determines type via /\.(\w+)$/) */
66
+ file: string;
67
+ /** S3 bucket name (if present, load from S3) */
68
+ bucket?: string;
69
+ /** Specific pages to extract from PDF (omit = all pages) */
70
+ pages?: number[];
71
+ /** Base64 data (if present, use directly instead of loading from file) */
72
+ data?: string;
73
+ }
74
+ /**
75
+ * Image input that can be loaded from local filesystem, S3, or provided as base64 data.
76
+ */
77
+ export interface LlmOperateInputImage {
78
+ /** Path or filename */
79
+ image: string;
80
+ /** S3 bucket name (if present, load from S3) */
81
+ bucket?: string;
82
+ /** Base64 data (if present, use directly instead of loading from file) */
83
+ data?: string;
84
+ }
85
+ /**
86
+ * Content item in LlmOperateInput array.
87
+ * Can be a string (text), file object, or image object.
88
+ */
89
+ export type LlmOperateInputContent = string | LlmOperateInputFile | LlmOperateInputImage;
90
+ /**
91
+ * Simplified input format for operate() that supports automatic file resolution.
92
+ * Files/images are loaded from local filesystem, S3, or provided data.
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * const input: LlmOperateInput = [
97
+ * "Extract text from these documents",
98
+ * { file: "document.pdf", bucket: "my-bucket", pages: [1, 2, 3] },
99
+ * { image: "photo.png" }, // loads from local filesystem
100
+ * ];
101
+ * ```
102
+ */
103
+ export type LlmOperateInput = LlmOperateInputContent[];
59
104
  export interface LlmOutputContentText {
60
105
  annotations?: AnyValue[];
61
106
  text: string;
@@ -188,13 +233,14 @@ export interface LlmOperateResponse {
188
233
  model?: string;
189
234
  output: LlmOutput;
190
235
  provider?: string;
236
+ reasoning: string[];
191
237
  responses: JsonReturn[];
192
238
  status: LlmResponseStatus;
193
239
  usage: LlmUsage;
194
240
  }
195
241
  export interface LlmProvider {
196
- operate?(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
242
+ operate?(input: string | LlmHistory | LlmInputMessage | LlmOperateInput, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
197
243
  send(message: string, options?: LlmMessageOptions): Promise<string | JsonObject>;
198
- stream?(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): AsyncIterable<LlmStreamChunk>;
244
+ stream?(input: string | LlmHistory | LlmInputMessage | LlmOperateInput, options?: LlmOperateOptions): AsyncIterable<LlmStreamChunk>;
199
245
  }
200
246
  export {};
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Extract file extension from a path or filename
3
+ */
4
+ export declare function getFileExtension(filePath: string): string | undefined;
5
+ /**
6
+ * Determine MIME type from file extension
7
+ */
8
+ export declare function getMimeType(filePath: string): string | undefined;
9
+ /**
10
+ * Check if a file path has an image extension
11
+ */
12
+ export declare function isImageExtension(filePath: string): boolean;
13
+ /**
14
+ * Check if a file path has a PDF extension
15
+ */
16
+ export declare function isPdfExtension(filePath: string): boolean;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Extract specific pages from a PDF buffer
3
+ * @param pdfBytes - Buffer containing the PDF data
4
+ * @param pages - Array of page numbers to extract (1-indexed)
5
+ * @returns Buffer containing a new PDF with only the specified pages
6
+ */
7
+ export declare function extractPdfPages(pdfBytes: Buffer, pages: number[]): Promise<Buffer>;
8
+ /**
9
+ * Get the total number of pages in a PDF
10
+ * @param pdfBytes - Buffer containing the PDF data
11
+ * @returns Total number of pages
12
+ */
13
+ export declare function getPdfPageCount(pdfBytes: Buffer): Promise<number>;
@@ -0,0 +1,5 @@
1
+ export { getFileExtension, getMimeType, isImageExtension, isPdfExtension, } from "./detectMimeType.js";
2
+ export { extractPdfPages, getPdfPageCount } from "./extractPdfPages.js";
3
+ export { loadLocalFile } from "./loadLocalFile.js";
4
+ export { loadS3File } from "./loadS3File.js";
5
+ export { resolveOperateInput } from "./resolveOperateInput.js";
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Load a file from the local filesystem
3
+ * @param filePath - Path to the file (relative paths resolve from process.cwd())
4
+ * @returns Buffer containing the file contents
5
+ */
6
+ export declare function loadLocalFile(filePath: string): Promise<Buffer>;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Load a file from S3
3
+ * @param bucket - S3 bucket name
4
+ * @param key - S3 object key
5
+ * @returns Buffer containing the file contents
6
+ */
7
+ export declare function loadS3File(bucket: string, key: string): Promise<Buffer>;
@@ -0,0 +1,12 @@
1
+ import { LlmInputMessage, LlmOperateInput } from "../types/LlmProvider.interface.js";
2
+ /**
3
+ * Resolve LlmOperateInput to LlmInputMessage
4
+ *
5
+ * This function takes the simplified LlmOperateInput format and resolves
6
+ * all files/images from their sources (provided data, S3, or local filesystem),
7
+ * converting them to the standard LlmInputMessage format.
8
+ *
9
+ * @param input - LlmOperateInput array
10
+ * @returns LlmInputMessage ready for LLM providers
11
+ */
12
+ export declare function resolveOperateInput(input: LlmOperateInput): Promise<LlmInputMessage>;
@@ -0,0 +1,14 @@
1
+ import { LlmHistory } from "../types/LlmProvider.interface.js";
2
+ /**
3
+ * Extracts reasoning text from LLM history items.
4
+ *
5
+ * Reasoning items may have text in different locations depending on the provider:
6
+ * - OpenAI: `summary` array with objects containing `text` property (type: "reasoning")
7
+ * - OpenAI: `content` property on reasoning items (type: "reasoning")
8
+ * - Anthropic: `thinking` property on thinking items (type: "thinking")
9
+ * - OpenRouter/z-ai: `reasoning` property on message items
10
+ *
11
+ * @param history - The LLM history array to extract reasoning from
12
+ * @returns Array of reasoning text strings
13
+ */
14
+ export declare function extractReasoning(history: LlmHistory): string[];
@@ -1,4 +1,5 @@
1
1
  export * from "./determineModelProvider.js";
2
+ export * from "./extractReasoning.js";
2
3
  export * from "./formatOperateInput.js";
3
4
  export * from "./formatOperateMessage.js";
4
5
  export * from "./logger.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jaypie/llm",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "Large language model utilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,6 +36,7 @@
36
36
  "@jaypie/errors": "^1.2.1",
37
37
  "openai": "^6.9.1",
38
38
  "openmeteo": "^1.2.1",
39
+ "pdf-lib": "^1.17.1",
39
40
  "random": "^5.3.0",
40
41
  "z-schema": "^6.0.2",
41
42
  "zod": "^4.1.13"