@jaypie/llm 1.1.27 → 1.1.30-rc.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/cjs/constants.d.ts +22 -1
- package/dist/cjs/index.cjs +2399 -988
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/operate/OperateLoop.d.ts +61 -0
- package/dist/cjs/operate/adapters/AnthropicAdapter.d.ts +32 -0
- package/dist/cjs/operate/adapters/GeminiAdapter.d.ts +37 -0
- package/dist/cjs/operate/adapters/OpenAiAdapter.d.ts +30 -0
- package/dist/cjs/operate/adapters/ProviderAdapter.interface.d.ts +177 -0
- package/dist/cjs/operate/adapters/index.d.ts +5 -0
- package/dist/cjs/operate/hooks/HookRunner.d.ts +78 -0
- package/dist/cjs/operate/hooks/index.d.ts +2 -0
- package/dist/cjs/operate/index.d.ts +14 -0
- package/dist/cjs/operate/input/InputProcessor.d.ts +40 -0
- package/dist/cjs/operate/input/index.d.ts +2 -0
- package/dist/cjs/operate/response/ResponseBuilder.d.ts +89 -0
- package/dist/cjs/operate/response/index.d.ts +2 -0
- package/dist/cjs/operate/retry/RetryExecutor.d.ts +39 -0
- package/dist/cjs/operate/retry/RetryPolicy.d.ts +35 -0
- package/dist/cjs/operate/retry/index.d.ts +4 -0
- package/dist/cjs/operate/types.d.ts +136 -0
- package/dist/cjs/providers/anthropic/AnthropicProvider.class.d.ts +2 -0
- package/dist/cjs/providers/gemini/GeminiProvider.class.d.ts +17 -0
- package/dist/cjs/providers/gemini/index.d.ts +3 -0
- package/dist/cjs/providers/gemini/types.d.ts +185 -0
- package/dist/cjs/providers/gemini/utils.d.ts +51 -0
- package/dist/cjs/providers/openai/OpenAiProvider.class.d.ts +2 -0
- package/dist/esm/constants.d.ts +22 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +2392 -982
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/operate/OperateLoop.d.ts +61 -0
- package/dist/esm/operate/adapters/AnthropicAdapter.d.ts +32 -0
- package/dist/esm/operate/adapters/GeminiAdapter.d.ts +37 -0
- package/dist/esm/operate/adapters/OpenAiAdapter.d.ts +30 -0
- package/dist/esm/operate/adapters/ProviderAdapter.interface.d.ts +177 -0
- package/dist/esm/operate/adapters/index.d.ts +5 -0
- package/dist/esm/operate/hooks/HookRunner.d.ts +78 -0
- package/dist/esm/operate/hooks/index.d.ts +2 -0
- package/dist/esm/operate/index.d.ts +14 -0
- package/dist/esm/operate/input/InputProcessor.d.ts +40 -0
- package/dist/esm/operate/input/index.d.ts +2 -0
- package/dist/esm/operate/response/ResponseBuilder.d.ts +89 -0
- package/dist/esm/operate/response/index.d.ts +2 -0
- package/dist/esm/operate/retry/RetryExecutor.d.ts +39 -0
- package/dist/esm/operate/retry/RetryPolicy.d.ts +35 -0
- package/dist/esm/operate/retry/index.d.ts +4 -0
- package/dist/esm/operate/types.d.ts +136 -0
- package/dist/esm/providers/anthropic/AnthropicProvider.class.d.ts +2 -0
- package/dist/esm/providers/gemini/GeminiProvider.class.d.ts +17 -0
- package/dist/esm/providers/gemini/index.d.ts +3 -0
- package/dist/esm/providers/gemini/types.d.ts +185 -0
- package/dist/esm/providers/gemini/utils.d.ts +51 -0
- package/dist/esm/providers/openai/OpenAiProvider.class.d.ts +2 -0
- package/package.json +11 -7
- package/LICENSE.txt +0 -21
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { LlmHistory, LlmInputMessage, LlmOperateOptions, LlmOperateResponse } from "../types/LlmProvider.interface.js";
|
|
2
|
+
import { ProviderAdapter } from "./adapters/ProviderAdapter.interface.js";
|
|
3
|
+
import { HookRunner } from "./hooks/index.js";
|
|
4
|
+
import { InputProcessor } from "./input/index.js";
|
|
5
|
+
import { RetryPolicy } from "./retry/index.js";
|
|
6
|
+
export interface OperateLoopConfig {
|
|
7
|
+
adapter: ProviderAdapter;
|
|
8
|
+
client: unknown;
|
|
9
|
+
hookRunner?: HookRunner;
|
|
10
|
+
inputProcessor?: InputProcessor;
|
|
11
|
+
maxRetries?: number;
|
|
12
|
+
retryPolicy?: RetryPolicy;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* OperateLoop implements the core multi-turn conversation loop.
|
|
16
|
+
* It orchestrates provider adapters, retry logic, hook execution, and tool calling.
|
|
17
|
+
*
|
|
18
|
+
* This class uses Template Method + Strategy patterns:
|
|
19
|
+
* - Template Method: The execute() method defines the algorithm skeleton
|
|
20
|
+
* - Strategy: Provider adapters handle provider-specific operations
|
|
21
|
+
*/
|
|
22
|
+
export declare class OperateLoop {
|
|
23
|
+
private readonly adapter;
|
|
24
|
+
private readonly client;
|
|
25
|
+
private readonly hookRunnerInstance;
|
|
26
|
+
private readonly inputProcessorInstance;
|
|
27
|
+
private readonly maxRetries;
|
|
28
|
+
private readonly retryPolicy;
|
|
29
|
+
constructor(config: OperateLoopConfig);
|
|
30
|
+
/**
|
|
31
|
+
* Execute the operate loop for multi-turn conversations with tool calling.
|
|
32
|
+
*/
|
|
33
|
+
execute(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
|
|
34
|
+
private initializeState;
|
|
35
|
+
private createContext;
|
|
36
|
+
private buildInitialRequest;
|
|
37
|
+
private executeOneTurn;
|
|
38
|
+
/**
|
|
39
|
+
* Sync the current input state from the updated provider request.
|
|
40
|
+
* This is necessary because appendToolResult modifies the provider-specific request,
|
|
41
|
+
* and we need to keep our state in sync.
|
|
42
|
+
*/
|
|
43
|
+
private syncInputFromRequest;
|
|
44
|
+
/**
|
|
45
|
+
* Convert Gemini contents format to internal history format.
|
|
46
|
+
*/
|
|
47
|
+
private convertGeminiContentsToHistory;
|
|
48
|
+
/**
|
|
49
|
+
* Append response items to the provider request.
|
|
50
|
+
* This adds all output items from a response (including reasoning, function_calls, etc.)
|
|
51
|
+
* to the request's input/messages array.
|
|
52
|
+
*
|
|
53
|
+
* This is critical for OpenAI which requires reasoning items to be present
|
|
54
|
+
* when function_call items reference them.
|
|
55
|
+
*/
|
|
56
|
+
private appendResponseItemsToRequest;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create an OperateLoop instance with the specified configuration.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createOperateLoop(config: OperateLoopConfig): OperateLoop;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Anthropic } from "@anthropic-ai/sdk";
|
|
2
|
+
import { JsonObject, NaturalSchema } from "@jaypie/types";
|
|
3
|
+
import { z } from "zod/v4";
|
|
4
|
+
import { Toolkit } from "../../tools/Toolkit.class.js";
|
|
5
|
+
import { LlmHistory, LlmOperateOptions, LlmUsageItem } from "../../types/LlmProvider.interface.js";
|
|
6
|
+
import { ClassifiedError, OperateRequest, ParsedResponse, ProviderToolDefinition, StandardToolCall, StandardToolResult } from "../types.js";
|
|
7
|
+
import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
8
|
+
/**
|
|
9
|
+
* AnthropicAdapter implements the ProviderAdapter interface for Anthropic's API.
|
|
10
|
+
* It handles request building, response parsing, and error classification
|
|
11
|
+
* specific to Anthropic's Messages API.
|
|
12
|
+
*/
|
|
13
|
+
export declare class AnthropicAdapter extends BaseProviderAdapter {
|
|
14
|
+
readonly name: "anthropic";
|
|
15
|
+
readonly defaultModel: "claude-opus-4-1";
|
|
16
|
+
buildRequest(request: OperateRequest): Anthropic.MessageCreateParams;
|
|
17
|
+
formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
|
|
18
|
+
formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
|
|
19
|
+
executeRequest(client: unknown, request: unknown): Promise<Anthropic.Message>;
|
|
20
|
+
parseResponse(response: unknown, _options?: LlmOperateOptions): ParsedResponse;
|
|
21
|
+
extractToolCalls(response: unknown): StandardToolCall[];
|
|
22
|
+
extractUsage(response: unknown, model: string): LlmUsageItem;
|
|
23
|
+
formatToolResult(toolCall: StandardToolCall, result: StandardToolResult): Anthropic.ToolResultBlockParam;
|
|
24
|
+
appendToolResult(request: unknown, toolCall: StandardToolCall, result: StandardToolResult): Anthropic.MessageCreateParams;
|
|
25
|
+
responseToHistoryItems(response: unknown): LlmHistory;
|
|
26
|
+
classifyError(error: unknown): ClassifiedError;
|
|
27
|
+
isComplete(response: unknown): boolean;
|
|
28
|
+
hasStructuredOutput(response: unknown): boolean;
|
|
29
|
+
extractStructuredOutput(response: unknown): JsonObject | undefined;
|
|
30
|
+
private extractContent;
|
|
31
|
+
}
|
|
32
|
+
export declare const anthropicAdapter: AnthropicAdapter;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { JsonObject, NaturalSchema } from "@jaypie/types";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { Toolkit } from "../../tools/Toolkit.class.js";
|
|
4
|
+
import { LlmHistory, LlmOperateOptions, LlmUsageItem } from "../../types/LlmProvider.interface.js";
|
|
5
|
+
import { ClassifiedError, OperateRequest, ParsedResponse, ProviderToolDefinition, StandardToolCall, StandardToolResult } from "../types.js";
|
|
6
|
+
import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
7
|
+
import { GeminiPart, GeminiRawResponse, GeminiRequest } from "../../providers/gemini/types.js";
|
|
8
|
+
/**
|
|
9
|
+
* GeminiAdapter implements the ProviderAdapter interface for Google's Gemini API.
|
|
10
|
+
* It handles request building, response parsing, and error classification
|
|
11
|
+
* specific to Gemini's generateContent API.
|
|
12
|
+
*/
|
|
13
|
+
export declare class GeminiAdapter extends BaseProviderAdapter {
|
|
14
|
+
readonly name: "gemini";
|
|
15
|
+
readonly defaultModel: "gemini-2.5-flash";
|
|
16
|
+
buildRequest(request: OperateRequest): GeminiRequest;
|
|
17
|
+
formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
|
|
18
|
+
formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
|
|
19
|
+
executeRequest(client: unknown, request: unknown): Promise<GeminiRawResponse>;
|
|
20
|
+
parseResponse(response: unknown, options?: LlmOperateOptions): ParsedResponse;
|
|
21
|
+
extractToolCalls(response: unknown): StandardToolCall[];
|
|
22
|
+
extractUsage(response: unknown, model: string): LlmUsageItem;
|
|
23
|
+
formatToolResult(toolCall: StandardToolCall, result: StandardToolResult): GeminiPart;
|
|
24
|
+
appendToolResult(request: unknown, toolCall: StandardToolCall, result: StandardToolResult): GeminiRequest;
|
|
25
|
+
responseToHistoryItems(response: unknown): LlmHistory;
|
|
26
|
+
classifyError(error: unknown): ClassifiedError;
|
|
27
|
+
isComplete(response: unknown): boolean;
|
|
28
|
+
hasStructuredOutput(response: unknown): boolean;
|
|
29
|
+
extractStructuredOutput(response: unknown): JsonObject | undefined;
|
|
30
|
+
private convertMessagesToContents;
|
|
31
|
+
private mapRole;
|
|
32
|
+
private hasToolCalls;
|
|
33
|
+
private getFinishReason;
|
|
34
|
+
private extractContent;
|
|
35
|
+
private generateCallId;
|
|
36
|
+
}
|
|
37
|
+
export declare const geminiAdapter: GeminiAdapter;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { JsonObject, NaturalSchema } from "@jaypie/types";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { Toolkit } from "../../tools/Toolkit.class.js";
|
|
4
|
+
import { LlmHistory, LlmOperateOptions, LlmToolResult, LlmUsageItem } from "../../types/LlmProvider.interface.js";
|
|
5
|
+
import { ClassifiedError, OperateRequest, ParsedResponse, ProviderToolDefinition, StandardToolCall, StandardToolResult } from "../types.js";
|
|
6
|
+
import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
7
|
+
/**
|
|
8
|
+
* OpenAiAdapter implements the ProviderAdapter interface for OpenAI's API.
|
|
9
|
+
* It handles request building, response parsing, and error classification
|
|
10
|
+
* specific to OpenAI's Responses API.
|
|
11
|
+
*/
|
|
12
|
+
export declare class OpenAiAdapter extends BaseProviderAdapter {
|
|
13
|
+
readonly name: "openai";
|
|
14
|
+
readonly defaultModel: "gpt-5";
|
|
15
|
+
buildRequest(request: OperateRequest): unknown;
|
|
16
|
+
formatTools(toolkit: Toolkit, _outputSchema?: JsonObject): ProviderToolDefinition[];
|
|
17
|
+
formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
|
|
18
|
+
executeRequest(client: unknown, request: unknown): Promise<unknown>;
|
|
19
|
+
parseResponse(response: unknown, options?: LlmOperateOptions): ParsedResponse;
|
|
20
|
+
extractToolCalls(response: unknown): StandardToolCall[];
|
|
21
|
+
extractUsage(response: unknown, model: string): LlmUsageItem;
|
|
22
|
+
formatToolResult(toolCall: StandardToolCall, result: StandardToolResult): LlmToolResult;
|
|
23
|
+
appendToolResult(request: unknown, toolCall: StandardToolCall, result: StandardToolResult): unknown;
|
|
24
|
+
responseToHistoryItems(response: unknown): LlmHistory;
|
|
25
|
+
classifyError(error: unknown): ClassifiedError;
|
|
26
|
+
isComplete(response: unknown): boolean;
|
|
27
|
+
private hasToolCalls;
|
|
28
|
+
private extractContent;
|
|
29
|
+
}
|
|
30
|
+
export declare const openAiAdapter: OpenAiAdapter;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { JsonObject, NaturalSchema } from "@jaypie/types";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { LlmHistory, LlmOperateOptions, LlmUsageItem } from "../../types/LlmProvider.interface.js";
|
|
4
|
+
import { Toolkit } from "../../tools/Toolkit.class.js";
|
|
5
|
+
import { ClassifiedError, OperateRequest, ParsedResponse, ProviderToolDefinition, StandardToolCall, StandardToolResult } from "../types.js";
|
|
6
|
+
/**
|
|
7
|
+
* ProviderAdapter defines the contract that each LLM provider must implement
|
|
8
|
+
* to work with the shared operate loop.
|
|
9
|
+
*
|
|
10
|
+
* The adapter pattern allows the core operate loop to be provider-agnostic
|
|
11
|
+
* while each provider handles its specific API format and quirks.
|
|
12
|
+
*/
|
|
13
|
+
export interface ProviderAdapter {
|
|
14
|
+
/**
|
|
15
|
+
* Unique identifier for this provider (e.g., "openai", "anthropic")
|
|
16
|
+
*/
|
|
17
|
+
readonly name: string;
|
|
18
|
+
/**
|
|
19
|
+
* Default model for this provider
|
|
20
|
+
*/
|
|
21
|
+
readonly defaultModel: string;
|
|
22
|
+
/**
|
|
23
|
+
* Build a provider-specific request from the standardized format
|
|
24
|
+
*
|
|
25
|
+
* @param request - Standardized request options
|
|
26
|
+
* @returns Provider-specific request object ready for the API
|
|
27
|
+
*/
|
|
28
|
+
buildRequest(request: OperateRequest): unknown;
|
|
29
|
+
/**
|
|
30
|
+
* Convert a Toolkit to provider-specific tool definitions
|
|
31
|
+
*
|
|
32
|
+
* @param toolkit - The toolkit containing tool definitions
|
|
33
|
+
* @param outputSchema - Optional JSON schema for structured output
|
|
34
|
+
* @returns Array of provider-specific tool definitions
|
|
35
|
+
*/
|
|
36
|
+
formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
|
|
37
|
+
/**
|
|
38
|
+
* Format a structured output schema for the provider
|
|
39
|
+
*
|
|
40
|
+
* @param schema - JSON schema, NaturalSchema, or Zod schema for the expected output
|
|
41
|
+
* @returns Provider-specific format configuration
|
|
42
|
+
*/
|
|
43
|
+
formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
|
|
44
|
+
/**
|
|
45
|
+
* Execute an API request to the provider
|
|
46
|
+
*
|
|
47
|
+
* @param client - The provider's SDK client instance
|
|
48
|
+
* @param request - Provider-specific request object (from buildRequest)
|
|
49
|
+
* @returns Raw provider response
|
|
50
|
+
*/
|
|
51
|
+
executeRequest(client: unknown, request: unknown): Promise<unknown>;
|
|
52
|
+
/**
|
|
53
|
+
* Parse a provider response into standardized format
|
|
54
|
+
*
|
|
55
|
+
* @param response - Raw provider response
|
|
56
|
+
* @param options - Original operate options (for context)
|
|
57
|
+
* @returns Parsed response with content, tool calls, usage, etc.
|
|
58
|
+
*/
|
|
59
|
+
parseResponse(response: unknown, options?: LlmOperateOptions): ParsedResponse;
|
|
60
|
+
/**
|
|
61
|
+
* Extract tool calls from a provider response
|
|
62
|
+
*
|
|
63
|
+
* @param response - Raw provider response
|
|
64
|
+
* @returns Array of standardized tool calls
|
|
65
|
+
*/
|
|
66
|
+
extractToolCalls(response: unknown): StandardToolCall[];
|
|
67
|
+
/**
|
|
68
|
+
* Extract usage information from a provider response
|
|
69
|
+
*
|
|
70
|
+
* @param response - Raw provider response
|
|
71
|
+
* @param model - The model used (for tracking)
|
|
72
|
+
* @returns Usage item with token counts
|
|
73
|
+
*/
|
|
74
|
+
extractUsage(response: unknown, model: string): LlmUsageItem;
|
|
75
|
+
/**
|
|
76
|
+
* Format a tool result to append to the conversation
|
|
77
|
+
*
|
|
78
|
+
* @param toolCall - The original tool call
|
|
79
|
+
* @param result - The standardized tool result
|
|
80
|
+
* @returns Provider-specific message/item to add to history
|
|
81
|
+
*/
|
|
82
|
+
formatToolResult(toolCall: StandardToolCall, result: StandardToolResult): unknown;
|
|
83
|
+
/**
|
|
84
|
+
* Append tool call and result to the request for the next turn
|
|
85
|
+
*
|
|
86
|
+
* @param request - Current provider request
|
|
87
|
+
* @param toolCall - The tool call that was made
|
|
88
|
+
* @param result - The result of the tool call
|
|
89
|
+
* @returns Updated request with tool result appended
|
|
90
|
+
*/
|
|
91
|
+
appendToolResult(request: unknown, toolCall: StandardToolCall, result: StandardToolResult): unknown;
|
|
92
|
+
/**
|
|
93
|
+
* Convert provider response items to LlmHistory format for storage
|
|
94
|
+
*
|
|
95
|
+
* @param response - Raw provider response
|
|
96
|
+
* @returns History items to append
|
|
97
|
+
*/
|
|
98
|
+
responseToHistoryItems(response: unknown): LlmHistory;
|
|
99
|
+
/**
|
|
100
|
+
* Classify an error for retry logic
|
|
101
|
+
*
|
|
102
|
+
* @param error - The error that occurred
|
|
103
|
+
* @returns Classified error with retry recommendation
|
|
104
|
+
*/
|
|
105
|
+
classifyError(error: unknown): ClassifiedError;
|
|
106
|
+
/**
|
|
107
|
+
* Check if an error is retryable
|
|
108
|
+
*
|
|
109
|
+
* @param error - The error to check
|
|
110
|
+
* @returns True if the error can be retried
|
|
111
|
+
*/
|
|
112
|
+
isRetryableError(error: unknown): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Check if an error is due to rate limiting
|
|
115
|
+
*
|
|
116
|
+
* @param error - The error to check
|
|
117
|
+
* @returns True if this is a rate limit error
|
|
118
|
+
*/
|
|
119
|
+
isRateLimitError(error: unknown): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Check if a response indicates the model wants to stop (vs tool use)
|
|
122
|
+
*
|
|
123
|
+
* @param response - Raw provider response
|
|
124
|
+
* @returns True if the model has finished responding
|
|
125
|
+
*/
|
|
126
|
+
isComplete(response: unknown): boolean;
|
|
127
|
+
/**
|
|
128
|
+
* Check if a response contains a structured output result
|
|
129
|
+
*
|
|
130
|
+
* @param response - Raw provider response
|
|
131
|
+
* @returns True if structured output was returned
|
|
132
|
+
*/
|
|
133
|
+
hasStructuredOutput(response: unknown): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Extract structured output from a response
|
|
136
|
+
*
|
|
137
|
+
* @param response - Raw provider response
|
|
138
|
+
* @returns Parsed JSON object
|
|
139
|
+
*/
|
|
140
|
+
extractStructuredOutput(response: unknown): JsonObject | undefined;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* BaseProviderAdapter provides default implementations for common adapter methods.
|
|
144
|
+
* Providers can extend this class to reduce boilerplate.
|
|
145
|
+
*/
|
|
146
|
+
export declare abstract class BaseProviderAdapter implements ProviderAdapter {
|
|
147
|
+
abstract readonly name: string;
|
|
148
|
+
abstract readonly defaultModel: string;
|
|
149
|
+
abstract buildRequest(request: OperateRequest): unknown;
|
|
150
|
+
abstract formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
|
|
151
|
+
abstract formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
|
|
152
|
+
abstract executeRequest(client: unknown, request: unknown): Promise<unknown>;
|
|
153
|
+
abstract parseResponse(response: unknown, options?: LlmOperateOptions): ParsedResponse;
|
|
154
|
+
abstract extractToolCalls(response: unknown): StandardToolCall[];
|
|
155
|
+
abstract extractUsage(response: unknown, model: string): LlmUsageItem;
|
|
156
|
+
abstract formatToolResult(toolCall: StandardToolCall, result: StandardToolResult): unknown;
|
|
157
|
+
abstract appendToolResult(request: unknown, toolCall: StandardToolCall, result: StandardToolResult): unknown;
|
|
158
|
+
abstract responseToHistoryItems(response: unknown): LlmHistory;
|
|
159
|
+
abstract classifyError(error: unknown): ClassifiedError;
|
|
160
|
+
abstract isComplete(response: unknown): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Default implementation checks if error is retryable via classifyError
|
|
163
|
+
*/
|
|
164
|
+
isRetryableError(error: unknown): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Default implementation checks error category via classifyError
|
|
167
|
+
*/
|
|
168
|
+
isRateLimitError(error: unknown): boolean;
|
|
169
|
+
/**
|
|
170
|
+
* Default implementation returns false - override for providers with native structured output
|
|
171
|
+
*/
|
|
172
|
+
hasStructuredOutput(_response: unknown): boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Default implementation returns undefined - override for providers with native structured output
|
|
175
|
+
*/
|
|
176
|
+
extractStructuredOutput(_response: unknown): JsonObject | undefined;
|
|
177
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
2
|
+
export type { ProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
3
|
+
export { AnthropicAdapter, anthropicAdapter } from "./AnthropicAdapter.js";
|
|
4
|
+
export { GeminiAdapter, geminiAdapter } from "./GeminiAdapter.js";
|
|
5
|
+
export { OpenAiAdapter, openAiAdapter } from "./OpenAiAdapter.js";
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { JsonObject } from "@jaypie/types";
|
|
2
|
+
import { LlmHistory, LlmInputMessage, LlmOperateOptions, LlmUsage } from "../../types/LlmProvider.interface.js";
|
|
3
|
+
export interface BeforeModelRequestContext {
|
|
4
|
+
input: string | LlmHistory | LlmInputMessage;
|
|
5
|
+
options?: LlmOperateOptions;
|
|
6
|
+
providerRequest: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface AfterModelResponseContext {
|
|
9
|
+
content: string | JsonObject;
|
|
10
|
+
input: string | LlmHistory | LlmInputMessage;
|
|
11
|
+
options?: LlmOperateOptions;
|
|
12
|
+
providerRequest: unknown;
|
|
13
|
+
providerResponse: unknown;
|
|
14
|
+
usage: LlmUsage;
|
|
15
|
+
}
|
|
16
|
+
export interface BeforeToolContext {
|
|
17
|
+
args: string;
|
|
18
|
+
toolName: string;
|
|
19
|
+
}
|
|
20
|
+
export interface AfterToolContext {
|
|
21
|
+
args: string;
|
|
22
|
+
result: unknown;
|
|
23
|
+
toolName: string;
|
|
24
|
+
}
|
|
25
|
+
export interface ToolErrorContext {
|
|
26
|
+
args: string;
|
|
27
|
+
error: Error;
|
|
28
|
+
toolName: string;
|
|
29
|
+
}
|
|
30
|
+
export interface RetryableErrorContext {
|
|
31
|
+
error: unknown;
|
|
32
|
+
input: string | LlmHistory | LlmInputMessage;
|
|
33
|
+
options?: LlmOperateOptions;
|
|
34
|
+
providerRequest: unknown;
|
|
35
|
+
}
|
|
36
|
+
export interface UnrecoverableErrorContext {
|
|
37
|
+
error: unknown;
|
|
38
|
+
input: string | LlmHistory | LlmInputMessage;
|
|
39
|
+
options?: LlmOperateOptions;
|
|
40
|
+
providerRequest: unknown;
|
|
41
|
+
}
|
|
42
|
+
export type LlmHooks = LlmOperateOptions["hooks"];
|
|
43
|
+
/**
|
|
44
|
+
* HookRunner provides a centralized, consistent way to execute lifecycle hooks
|
|
45
|
+
* during the LLM operate loop. It handles async resolution and provides error
|
|
46
|
+
* isolation for hook execution.
|
|
47
|
+
*/
|
|
48
|
+
export declare class HookRunner {
|
|
49
|
+
/**
|
|
50
|
+
* Execute the beforeEachModelRequest hook if defined
|
|
51
|
+
*/
|
|
52
|
+
runBeforeModelRequest(hooks: LlmHooks, context: BeforeModelRequestContext): Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Execute the afterEachModelResponse hook if defined
|
|
55
|
+
*/
|
|
56
|
+
runAfterModelResponse(hooks: LlmHooks, context: AfterModelResponseContext): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Execute the beforeEachTool hook if defined
|
|
59
|
+
*/
|
|
60
|
+
runBeforeTool(hooks: LlmHooks, context: BeforeToolContext): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Execute the afterEachTool hook if defined
|
|
63
|
+
*/
|
|
64
|
+
runAfterTool(hooks: LlmHooks, context: AfterToolContext): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Execute the onToolError hook if defined
|
|
67
|
+
*/
|
|
68
|
+
runOnToolError(hooks: LlmHooks, context: ToolErrorContext): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* Execute the onRetryableModelError hook if defined
|
|
71
|
+
*/
|
|
72
|
+
runOnRetryableError(hooks: LlmHooks, context: RetryableErrorContext): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Execute the onUnrecoverableModelError hook if defined
|
|
75
|
+
*/
|
|
76
|
+
runOnUnrecoverableError(hooks: LlmHooks, context: UnrecoverableErrorContext): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
export declare const hookRunner: HookRunner;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { AnthropicAdapter, anthropicAdapter, BaseProviderAdapter, GeminiAdapter, geminiAdapter, OpenAiAdapter, openAiAdapter, } from "./adapters/index.js";
|
|
2
|
+
export type { ProviderAdapter } from "./adapters/index.js";
|
|
3
|
+
export { createOperateLoop, OperateLoop } from "./OperateLoop.js";
|
|
4
|
+
export type { OperateLoopConfig } from "./OperateLoop.js";
|
|
5
|
+
export { HookRunner, hookRunner } from "./hooks/index.js";
|
|
6
|
+
export type { AfterModelResponseContext, AfterToolContext, BeforeModelRequestContext, BeforeToolContext, LlmHooks, RetryableErrorContext, ToolErrorContext, UnrecoverableErrorContext, } from "./hooks/index.js";
|
|
7
|
+
export { InputProcessor, inputProcessor } from "./input/index.js";
|
|
8
|
+
export type { ProcessedInput } from "./input/index.js";
|
|
9
|
+
export { createResponseBuilder, ResponseBuilder } from "./response/index.js";
|
|
10
|
+
export type { LlmError, ResponseBuilderConfig } from "./response/index.js";
|
|
11
|
+
export { DEFAULT_BACKOFF_FACTOR, DEFAULT_INITIAL_DELAY_MS, DEFAULT_MAX_DELAY_MS, DEFAULT_MAX_RETRIES, defaultRetryPolicy, MAX_RETRIES_ABSOLUTE_LIMIT, RetryExecutor, RetryPolicy, } from "./retry/index.js";
|
|
12
|
+
export type { ErrorClassifier, ExecuteOptions, RetryContext, RetryExecutorConfig, RetryPolicyConfig, } from "./retry/index.js";
|
|
13
|
+
export { ErrorCategory, LlmMessageType } from "./types.js";
|
|
14
|
+
export type { ClassifiedError, OperateContext, OperateLoopState, OperateRequest, ParsedResponse, ProviderToolDefinition, StandardToolCall, StandardToolResult, } from "./types.js";
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { LlmHistory, LlmInputMessage, LlmOperateOptions } from "../../types/LlmProvider.interface.js";
|
|
2
|
+
export interface ProcessedInput {
|
|
3
|
+
/** The processed history with all messages formatted */
|
|
4
|
+
history: LlmHistory;
|
|
5
|
+
/** Processed instructions with placeholders applied */
|
|
6
|
+
instructions?: string;
|
|
7
|
+
/** Processed system prompt with placeholders applied */
|
|
8
|
+
system?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* InputProcessor handles input normalization, placeholder substitution,
|
|
12
|
+
* and history merging for the operate loop.
|
|
13
|
+
*/
|
|
14
|
+
export declare class InputProcessor {
|
|
15
|
+
/**
|
|
16
|
+
* Process input with placeholders, history merging, and system message handling
|
|
17
|
+
*
|
|
18
|
+
* @param input - The raw input (string, message, or history)
|
|
19
|
+
* @param options - The operate options containing data, history, system, etc.
|
|
20
|
+
* @returns Processed input with all transformations applied
|
|
21
|
+
*/
|
|
22
|
+
process(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): ProcessedInput;
|
|
23
|
+
/**
|
|
24
|
+
* Format input and apply placeholders if data is provided
|
|
25
|
+
*/
|
|
26
|
+
private formatInputWithPlaceholders;
|
|
27
|
+
/**
|
|
28
|
+
* Process instructions with placeholder substitution
|
|
29
|
+
*/
|
|
30
|
+
private processInstructions;
|
|
31
|
+
/**
|
|
32
|
+
* Process system prompt with placeholder substitution
|
|
33
|
+
*/
|
|
34
|
+
private processSystem;
|
|
35
|
+
/**
|
|
36
|
+
* Prepend system message to history, handling duplicates
|
|
37
|
+
*/
|
|
38
|
+
private prependSystemMessage;
|
|
39
|
+
}
|
|
40
|
+
export declare const inputProcessor: InputProcessor;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { JsonObject, JsonReturn } from "@jaypie/types";
|
|
2
|
+
import { LlmHistory, LlmOperateResponse, LlmResponseStatus, LlmUsage, LlmUsageItem } from "../../types/LlmProvider.interface.js";
|
|
3
|
+
type LlmOutput = LlmOperateResponse["output"];
|
|
4
|
+
export interface ResponseBuilderConfig {
|
|
5
|
+
model: string;
|
|
6
|
+
provider: string;
|
|
7
|
+
}
|
|
8
|
+
export interface LlmError {
|
|
9
|
+
detail?: string;
|
|
10
|
+
status: number | string;
|
|
11
|
+
title: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* ResponseBuilder provides a fluent API for constructing LlmOperateResponse objects.
|
|
15
|
+
* It standardizes response construction across providers.
|
|
16
|
+
*/
|
|
17
|
+
export declare class ResponseBuilder {
|
|
18
|
+
private response;
|
|
19
|
+
constructor(config: ResponseBuilderConfig);
|
|
20
|
+
/**
|
|
21
|
+
* Set the response content
|
|
22
|
+
*/
|
|
23
|
+
setContent(content: string | JsonObject | undefined): this;
|
|
24
|
+
/**
|
|
25
|
+
* Set the response status
|
|
26
|
+
*/
|
|
27
|
+
setStatus(status: LlmResponseStatus): this;
|
|
28
|
+
/**
|
|
29
|
+
* Set an error on the response
|
|
30
|
+
*/
|
|
31
|
+
setError(error: LlmError): this;
|
|
32
|
+
/**
|
|
33
|
+
* Set the history
|
|
34
|
+
*/
|
|
35
|
+
setHistory(history: LlmHistory): this;
|
|
36
|
+
/**
|
|
37
|
+
* Append items to the history
|
|
38
|
+
*/
|
|
39
|
+
appendToHistory(...items: LlmHistory): this;
|
|
40
|
+
/**
|
|
41
|
+
* Set the output
|
|
42
|
+
*/
|
|
43
|
+
setOutput(output: LlmOutput): this;
|
|
44
|
+
/**
|
|
45
|
+
* Append items to the output
|
|
46
|
+
*/
|
|
47
|
+
appendToOutput(...items: LlmOutput): this;
|
|
48
|
+
/**
|
|
49
|
+
* Add a raw provider response
|
|
50
|
+
*/
|
|
51
|
+
addResponse(response: JsonReturn): this;
|
|
52
|
+
/**
|
|
53
|
+
* Add a usage entry for a single API call
|
|
54
|
+
*/
|
|
55
|
+
addUsage(usage: LlmUsageItem): this;
|
|
56
|
+
/**
|
|
57
|
+
* Set the entire usage array
|
|
58
|
+
*/
|
|
59
|
+
setUsage(usage: LlmUsage): this;
|
|
60
|
+
/**
|
|
61
|
+
* Get the current usage array for modifications
|
|
62
|
+
*/
|
|
63
|
+
getUsage(): LlmUsage;
|
|
64
|
+
/**
|
|
65
|
+
* Get the current history for modifications
|
|
66
|
+
*/
|
|
67
|
+
getHistory(): LlmHistory;
|
|
68
|
+
/**
|
|
69
|
+
* Get the current output for modifications
|
|
70
|
+
*/
|
|
71
|
+
getOutput(): LlmOutput;
|
|
72
|
+
/**
|
|
73
|
+
* Mark response as completed
|
|
74
|
+
*/
|
|
75
|
+
complete(): this;
|
|
76
|
+
/**
|
|
77
|
+
* Mark response as incomplete (e.g., max turns exceeded)
|
|
78
|
+
*/
|
|
79
|
+
incomplete(): this;
|
|
80
|
+
/**
|
|
81
|
+
* Build and return the final response object
|
|
82
|
+
*/
|
|
83
|
+
build(): LlmOperateResponse;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Factory function to create a new ResponseBuilder
|
|
87
|
+
*/
|
|
88
|
+
export declare function createResponseBuilder(config: ResponseBuilderConfig): ResponseBuilder;
|
|
89
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { HookRunner, LlmHooks } from "../hooks/HookRunner.js";
|
|
2
|
+
import { RetryPolicy } from "./RetryPolicy.js";
|
|
3
|
+
export interface RetryContext {
|
|
4
|
+
input: unknown;
|
|
5
|
+
options?: unknown;
|
|
6
|
+
providerRequest: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface ErrorClassifier {
|
|
9
|
+
isRetryable(error: unknown): boolean;
|
|
10
|
+
isKnownError(error: unknown): boolean;
|
|
11
|
+
}
|
|
12
|
+
export interface RetryExecutorConfig {
|
|
13
|
+
errorClassifier: ErrorClassifier;
|
|
14
|
+
hookRunner?: HookRunner;
|
|
15
|
+
policy?: RetryPolicy;
|
|
16
|
+
}
|
|
17
|
+
export interface ExecuteOptions {
|
|
18
|
+
context: RetryContext;
|
|
19
|
+
hooks?: LlmHooks;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* RetryExecutor handles the retry loop logic for LLM API calls.
|
|
23
|
+
* It provides exponential backoff, error classification, and hook execution.
|
|
24
|
+
*/
|
|
25
|
+
export declare class RetryExecutor {
|
|
26
|
+
private readonly policy;
|
|
27
|
+
private readonly hookRunner;
|
|
28
|
+
private readonly errorClassifier;
|
|
29
|
+
constructor(config: RetryExecutorConfig);
|
|
30
|
+
/**
|
|
31
|
+
* Execute an operation with retry logic
|
|
32
|
+
*
|
|
33
|
+
* @param operation - The async operation to execute
|
|
34
|
+
* @param options - Execution options including context and hooks
|
|
35
|
+
* @returns The result of the operation
|
|
36
|
+
* @throws BadGatewayError if all retries are exhausted or error is not retryable
|
|
37
|
+
*/
|
|
38
|
+
execute<T>(operation: () => Promise<T>, options: ExecuteOptions): Promise<T>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare const DEFAULT_INITIAL_DELAY_MS = 1000;
|
|
2
|
+
export declare const DEFAULT_MAX_DELAY_MS = 32000;
|
|
3
|
+
export declare const DEFAULT_BACKOFF_FACTOR = 2;
|
|
4
|
+
export declare const DEFAULT_MAX_RETRIES = 6;
|
|
5
|
+
export declare const MAX_RETRIES_ABSOLUTE_LIMIT = 72;
|
|
6
|
+
export interface RetryPolicyConfig {
|
|
7
|
+
/** Initial delay in milliseconds before first retry. Default: 1000 */
|
|
8
|
+
initialDelayMs?: number;
|
|
9
|
+
/** Maximum delay in milliseconds between retries. Default: 32000 */
|
|
10
|
+
maxDelayMs?: number;
|
|
11
|
+
/** Backoff multiplier for exponential backoff. Default: 2 */
|
|
12
|
+
backoffFactor?: number;
|
|
13
|
+
/** Maximum number of retries. Default: 6 */
|
|
14
|
+
maxRetries?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* RetryPolicy encapsulates retry configuration and delay calculation
|
|
18
|
+
* for the operate loop's retry logic.
|
|
19
|
+
*/
|
|
20
|
+
export declare class RetryPolicy {
|
|
21
|
+
readonly initialDelayMs: number;
|
|
22
|
+
readonly maxDelayMs: number;
|
|
23
|
+
readonly backoffFactor: number;
|
|
24
|
+
readonly maxRetries: number;
|
|
25
|
+
constructor(config?: RetryPolicyConfig);
|
|
26
|
+
/**
|
|
27
|
+
* Calculate the delay for a given attempt number (0-indexed)
|
|
28
|
+
*/
|
|
29
|
+
getDelayForAttempt(attempt: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* Check if another retry should be attempted
|
|
32
|
+
*/
|
|
33
|
+
shouldRetry(currentAttempt: number): boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare const defaultRetryPolicy: RetryPolicy;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { DEFAULT_BACKOFF_FACTOR, DEFAULT_INITIAL_DELAY_MS, DEFAULT_MAX_DELAY_MS, DEFAULT_MAX_RETRIES, defaultRetryPolicy, MAX_RETRIES_ABSOLUTE_LIMIT, RetryPolicy, } from "./RetryPolicy.js";
|
|
2
|
+
export type { RetryPolicyConfig } from "./RetryPolicy.js";
|
|
3
|
+
export { RetryExecutor } from "./RetryExecutor.js";
|
|
4
|
+
export type { ErrorClassifier, ExecuteOptions, RetryContext, RetryExecutorConfig, } from "./RetryExecutor.js";
|