@jaypie/llm 1.1.30-rc.0 → 1.1.30
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/LICENSE.txt +21 -0
- package/dist/cjs/constants.d.ts +17 -1
- package/dist/cjs/index.cjs +627 -49
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/operate/adapters/OpenRouterAdapter.d.ts +96 -0
- package/dist/cjs/operate/adapters/index.d.ts +1 -0
- package/dist/cjs/operate/index.d.ts +1 -1
- package/dist/cjs/providers/openrouter/OpenRouterProvider.class.d.ts +17 -0
- package/dist/cjs/providers/openrouter/index.d.ts +2 -0
- package/dist/cjs/providers/openrouter/utils.d.ts +49 -0
- package/dist/esm/constants.d.ts +17 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +627 -50
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/operate/adapters/OpenRouterAdapter.d.ts +96 -0
- package/dist/esm/operate/adapters/index.d.ts +1 -0
- package/dist/esm/operate/index.d.ts +1 -1
- package/dist/esm/providers/openrouter/OpenRouterProvider.class.d.ts +17 -0
- package/dist/esm/providers/openrouter/index.d.ts +2 -0
- package/dist/esm/providers/openrouter/utils.d.ts +49 -0
- package/package.json +4 -2
- package/dist/cjs/providers/anthropic/operate.d.ts +0 -16
- package/dist/cjs/providers/openai/operate.d.ts +0 -26
- package/dist/esm/providers/anthropic/operate.d.ts +0 -16
- package/dist/esm/providers/openai/operate.d.ts +0 -26
package/dist/cjs/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export { LlmMessageRole, LlmMessageType, } from "./types/LlmProvider.interface.j
|
|
|
5
5
|
export type { LlmTool } from "./types/LlmTool.interface.js";
|
|
6
6
|
export { JaypieToolkit, toolkit, Toolkit, tools } from "./tools/index.js";
|
|
7
7
|
export { GeminiProvider } from "./providers/gemini/index.js";
|
|
8
|
+
export { OpenRouterProvider } from "./providers/openrouter/index.js";
|
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
interface OpenRouterMessage {
|
|
8
|
+
role: "system" | "user" | "assistant" | "tool";
|
|
9
|
+
content?: string | null;
|
|
10
|
+
toolCalls?: OpenRouterToolCall[];
|
|
11
|
+
toolCallId?: string;
|
|
12
|
+
}
|
|
13
|
+
interface OpenRouterToolCall {
|
|
14
|
+
id: string;
|
|
15
|
+
type: "function";
|
|
16
|
+
function: {
|
|
17
|
+
name: string;
|
|
18
|
+
arguments: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
interface OpenRouterResponseMessage {
|
|
22
|
+
role: "assistant";
|
|
23
|
+
content?: string | null;
|
|
24
|
+
toolCalls?: OpenRouterToolCall[];
|
|
25
|
+
refusal?: string | null;
|
|
26
|
+
reasoning?: string | null;
|
|
27
|
+
}
|
|
28
|
+
interface OpenRouterChoice {
|
|
29
|
+
index: number;
|
|
30
|
+
message: OpenRouterResponseMessage;
|
|
31
|
+
finishReason: string | null;
|
|
32
|
+
finish_reason?: string | null;
|
|
33
|
+
}
|
|
34
|
+
interface OpenRouterUsage {
|
|
35
|
+
promptTokens: number;
|
|
36
|
+
completionTokens: number;
|
|
37
|
+
totalTokens: number;
|
|
38
|
+
prompt_tokens?: number;
|
|
39
|
+
completion_tokens?: number;
|
|
40
|
+
total_tokens?: number;
|
|
41
|
+
}
|
|
42
|
+
interface OpenRouterResponse {
|
|
43
|
+
id: string;
|
|
44
|
+
object: string;
|
|
45
|
+
created: number;
|
|
46
|
+
model: string;
|
|
47
|
+
choices: OpenRouterChoice[];
|
|
48
|
+
usage?: OpenRouterUsage;
|
|
49
|
+
}
|
|
50
|
+
interface OpenRouterTool {
|
|
51
|
+
type: "function";
|
|
52
|
+
function: {
|
|
53
|
+
name: string;
|
|
54
|
+
description: string;
|
|
55
|
+
parameters: JsonObject;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
interface OpenRouterRequest {
|
|
59
|
+
model: string;
|
|
60
|
+
messages: OpenRouterMessage[];
|
|
61
|
+
tools?: OpenRouterTool[];
|
|
62
|
+
tool_choice?: "auto" | "none" | "required";
|
|
63
|
+
response_format?: {
|
|
64
|
+
type: "json_object" | "text";
|
|
65
|
+
};
|
|
66
|
+
user?: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* OpenRouterAdapter implements the ProviderAdapter interface for OpenRouter's API.
|
|
70
|
+
* OpenRouter provides a unified API to access hundreds of AI models through OpenAI-compatible endpoints.
|
|
71
|
+
* It handles request building, response parsing, and error classification
|
|
72
|
+
* specific to OpenRouter's Chat Completions API.
|
|
73
|
+
*/
|
|
74
|
+
export declare class OpenRouterAdapter extends BaseProviderAdapter {
|
|
75
|
+
readonly name: "openrouter";
|
|
76
|
+
readonly defaultModel: "openai/gpt-4o";
|
|
77
|
+
buildRequest(request: OperateRequest): OpenRouterRequest;
|
|
78
|
+
formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
|
|
79
|
+
formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
|
|
80
|
+
executeRequest(client: unknown, request: unknown): Promise<OpenRouterResponse>;
|
|
81
|
+
parseResponse(response: unknown, _options?: LlmOperateOptions): ParsedResponse;
|
|
82
|
+
extractToolCalls(response: unknown): StandardToolCall[];
|
|
83
|
+
extractUsage(response: unknown, model: string): LlmUsageItem;
|
|
84
|
+
formatToolResult(toolCall: StandardToolCall, result: StandardToolResult): OpenRouterMessage;
|
|
85
|
+
appendToolResult(request: unknown, toolCall: StandardToolCall, result: StandardToolResult): OpenRouterRequest;
|
|
86
|
+
responseToHistoryItems(response: unknown): LlmHistory;
|
|
87
|
+
classifyError(error: unknown): ClassifiedError;
|
|
88
|
+
isComplete(response: unknown): boolean;
|
|
89
|
+
hasStructuredOutput(response: unknown): boolean;
|
|
90
|
+
extractStructuredOutput(response: unknown): JsonObject | undefined;
|
|
91
|
+
private hasToolCalls;
|
|
92
|
+
private extractContent;
|
|
93
|
+
private convertMessagesToOpenRouter;
|
|
94
|
+
}
|
|
95
|
+
export declare const openRouterAdapter: OpenRouterAdapter;
|
|
96
|
+
export {};
|
|
@@ -3,3 +3,4 @@ export type { ProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
|
3
3
|
export { AnthropicAdapter, anthropicAdapter } from "./AnthropicAdapter.js";
|
|
4
4
|
export { GeminiAdapter, geminiAdapter } from "./GeminiAdapter.js";
|
|
5
5
|
export { OpenAiAdapter, openAiAdapter } from "./OpenAiAdapter.js";
|
|
6
|
+
export { OpenRouterAdapter, openRouterAdapter } from "./OpenRouterAdapter.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { AnthropicAdapter, anthropicAdapter, BaseProviderAdapter, GeminiAdapter, geminiAdapter, OpenAiAdapter, openAiAdapter, } from "./adapters/index.js";
|
|
1
|
+
export { AnthropicAdapter, anthropicAdapter, BaseProviderAdapter, GeminiAdapter, geminiAdapter, OpenAiAdapter, openAiAdapter, OpenRouterAdapter, openRouterAdapter, } from "./adapters/index.js";
|
|
2
2
|
export type { ProviderAdapter } from "./adapters/index.js";
|
|
3
3
|
export { createOperateLoop, OperateLoop } from "./OperateLoop.js";
|
|
4
4
|
export type { OperateLoopConfig } from "./OperateLoop.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { JsonObject } from "@jaypie/types";
|
|
2
|
+
import { LlmHistory, LlmInputMessage, LlmMessageOptions, LlmOperateOptions, LlmOperateResponse, LlmProvider } from "../../types/LlmProvider.interface.js";
|
|
3
|
+
export declare class OpenRouterProvider implements LlmProvider {
|
|
4
|
+
private model;
|
|
5
|
+
private _client?;
|
|
6
|
+
private _operateLoop?;
|
|
7
|
+
private apiKey?;
|
|
8
|
+
private log;
|
|
9
|
+
private conversationHistory;
|
|
10
|
+
constructor(model?: string, { apiKey }?: {
|
|
11
|
+
apiKey?: string;
|
|
12
|
+
});
|
|
13
|
+
private getClient;
|
|
14
|
+
private getOperateLoop;
|
|
15
|
+
send(message: string, options?: LlmMessageOptions): Promise<string | JsonObject>;
|
|
16
|
+
operate(input: string | LlmHistory | LlmInputMessage, options?: LlmOperateOptions): Promise<LlmOperateResponse>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { log as defaultLog } from "@jaypie/core";
|
|
2
|
+
import { OpenRouter } from "@openrouter/sdk";
|
|
3
|
+
import { LlmMessageOptions } from "../../types/LlmProvider.interface.js";
|
|
4
|
+
export declare const getLogger: () => {
|
|
5
|
+
debug: {
|
|
6
|
+
(...args: unknown[]): void;
|
|
7
|
+
var(key: string | Record<string, unknown>, value?: unknown): void;
|
|
8
|
+
};
|
|
9
|
+
error: {
|
|
10
|
+
(...args: unknown[]): void;
|
|
11
|
+
var(key: string | Record<string, unknown>, value?: unknown): void;
|
|
12
|
+
};
|
|
13
|
+
fatal: {
|
|
14
|
+
(...args: unknown[]): void;
|
|
15
|
+
var(key: string | Record<string, unknown>, value?: unknown): void;
|
|
16
|
+
};
|
|
17
|
+
info: {
|
|
18
|
+
(...args: unknown[]): void;
|
|
19
|
+
var(key: string | Record<string, unknown>, value?: unknown): void;
|
|
20
|
+
};
|
|
21
|
+
trace: {
|
|
22
|
+
(...args: unknown[]): void;
|
|
23
|
+
var(key: string | Record<string, unknown>, value?: unknown): void;
|
|
24
|
+
};
|
|
25
|
+
warn: {
|
|
26
|
+
(...args: unknown[]): void;
|
|
27
|
+
var(key: string | Record<string, unknown>, value?: unknown): void;
|
|
28
|
+
};
|
|
29
|
+
var(key: string | Record<string, unknown>, value?: unknown): void;
|
|
30
|
+
with(tags: Record<string, unknown>): typeof defaultLog;
|
|
31
|
+
tag(key: string | string[] | Record<string, unknown> | null, value?: string): void;
|
|
32
|
+
untag(tags: string | string[] | Record<string, unknown> | null): void;
|
|
33
|
+
lib(options: {
|
|
34
|
+
level?: string;
|
|
35
|
+
lib?: string;
|
|
36
|
+
tags?: Record<string, unknown>;
|
|
37
|
+
}): typeof defaultLog;
|
|
38
|
+
};
|
|
39
|
+
export declare function initializeClient({ apiKey, }?: {
|
|
40
|
+
apiKey?: string;
|
|
41
|
+
}): Promise<OpenRouter>;
|
|
42
|
+
export declare function getDefaultModel(): string;
|
|
43
|
+
export interface ChatMessage {
|
|
44
|
+
role: "system" | "user" | "assistant";
|
|
45
|
+
content: string;
|
|
46
|
+
}
|
|
47
|
+
export declare function formatSystemMessage(systemPrompt: string, { data, placeholders }?: Pick<LlmMessageOptions, "data" | "placeholders">): ChatMessage;
|
|
48
|
+
export declare function formatUserMessage(message: string, { data, placeholders }?: Pick<LlmMessageOptions, "data" | "placeholders">): ChatMessage;
|
|
49
|
+
export declare function prepareMessages(message: string, { system, data, placeholders }?: LlmMessageOptions): ChatMessage[];
|
package/dist/esm/constants.d.ts
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
1
|
export declare const PROVIDER: {
|
|
2
|
+
readonly OPENROUTER: {
|
|
3
|
+
readonly MODEL: {
|
|
4
|
+
readonly DEFAULT: "openai/gpt-4o";
|
|
5
|
+
readonly SMALL: "openai/gpt-4o-mini";
|
|
6
|
+
readonly LARGE: "anthropic/claude-3-opus";
|
|
7
|
+
readonly TINY: "openai/gpt-4o-mini";
|
|
8
|
+
};
|
|
9
|
+
readonly MODEL_MATCH_WORDS: readonly ["openrouter"];
|
|
10
|
+
readonly NAME: "openrouter";
|
|
11
|
+
readonly ROLE: {
|
|
12
|
+
readonly ASSISTANT: "assistant";
|
|
13
|
+
readonly SYSTEM: "system";
|
|
14
|
+
readonly TOOL: "tool";
|
|
15
|
+
readonly USER: "user";
|
|
16
|
+
};
|
|
17
|
+
};
|
|
2
18
|
readonly GEMINI: {
|
|
3
19
|
readonly MODEL: {
|
|
4
20
|
readonly DEFAULT: "gemini-2.5-flash";
|
|
@@ -88,7 +104,7 @@ export declare const PROVIDER: {
|
|
|
88
104
|
readonly NAME: "openai";
|
|
89
105
|
};
|
|
90
106
|
};
|
|
91
|
-
export type LlmProviderName = typeof PROVIDER.ANTHROPIC.NAME | typeof PROVIDER.GEMINI.NAME | typeof PROVIDER.OPENAI.NAME;
|
|
107
|
+
export type LlmProviderName = typeof PROVIDER.ANTHROPIC.NAME | typeof PROVIDER.GEMINI.NAME | typeof PROVIDER.OPENAI.NAME | typeof PROVIDER.OPENROUTER.NAME;
|
|
92
108
|
export declare const DEFAULT: {
|
|
93
109
|
readonly PROVIDER: {
|
|
94
110
|
readonly MODEL: {
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export { LlmMessageRole, LlmMessageType, } from "./types/LlmProvider.interface.j
|
|
|
5
5
|
export type { LlmTool } from "./types/LlmTool.interface.js";
|
|
6
6
|
export { JaypieToolkit, toolkit, Toolkit, tools } from "./tools/index.js";
|
|
7
7
|
export { GeminiProvider } from "./providers/gemini/index.js";
|
|
8
|
+
export { OpenRouterProvider } from "./providers/openrouter/index.js";
|