@yourgpt/llm-sdk 0.1.0 → 1.0.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/README.md +61 -40
- package/dist/adapters/index.d.mts +4 -258
- package/dist/adapters/index.d.ts +4 -258
- package/dist/adapters/index.js +0 -113
- package/dist/adapters/index.js.map +1 -1
- package/dist/adapters/index.mjs +1 -112
- package/dist/adapters/index.mjs.map +1 -1
- package/dist/base-D_FyHFKj.d.mts +235 -0
- package/dist/base-D_FyHFKj.d.ts +235 -0
- package/dist/index.d.mts +209 -451
- package/dist/index.d.ts +209 -451
- package/dist/index.js +1905 -311
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1895 -309
- package/dist/index.mjs.map +1 -1
- package/dist/providers/anthropic/index.d.mts +61 -0
- package/dist/providers/anthropic/index.d.ts +61 -0
- package/dist/providers/anthropic/index.js +939 -0
- package/dist/providers/anthropic/index.js.map +1 -0
- package/dist/providers/anthropic/index.mjs +934 -0
- package/dist/providers/anthropic/index.mjs.map +1 -0
- package/dist/providers/azure/index.d.mts +38 -0
- package/dist/providers/azure/index.d.ts +38 -0
- package/dist/providers/azure/index.js +380 -0
- package/dist/providers/azure/index.js.map +1 -0
- package/dist/providers/azure/index.mjs +377 -0
- package/dist/providers/azure/index.mjs.map +1 -0
- package/dist/providers/google/index.d.mts +72 -0
- package/dist/providers/google/index.d.ts +72 -0
- package/dist/providers/google/index.js +790 -0
- package/dist/providers/google/index.js.map +1 -0
- package/dist/providers/google/index.mjs +785 -0
- package/dist/providers/google/index.mjs.map +1 -0
- package/dist/providers/ollama/index.d.mts +24 -0
- package/dist/providers/ollama/index.d.ts +24 -0
- package/dist/providers/ollama/index.js +235 -0
- package/dist/providers/ollama/index.js.map +1 -0
- package/dist/providers/ollama/index.mjs +232 -0
- package/dist/providers/ollama/index.mjs.map +1 -0
- package/dist/providers/openai/index.d.mts +82 -0
- package/dist/providers/openai/index.d.ts +82 -0
- package/dist/providers/openai/index.js +679 -0
- package/dist/providers/openai/index.js.map +1 -0
- package/dist/providers/openai/index.mjs +674 -0
- package/dist/providers/openai/index.mjs.map +1 -0
- package/dist/providers/xai/index.d.mts +78 -0
- package/dist/providers/xai/index.d.ts +78 -0
- package/dist/providers/xai/index.js +671 -0
- package/dist/providers/xai/index.js.map +1 -0
- package/dist/providers/xai/index.mjs +666 -0
- package/dist/providers/xai/index.mjs.map +1 -0
- package/dist/types-BBCZ3Fxy.d.mts +308 -0
- package/dist/types-CdORv1Yu.d.mts +338 -0
- package/dist/types-CdORv1Yu.d.ts +338 -0
- package/dist/types-DcoCaVVC.d.ts +308 -0
- package/package.json +34 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,318 +1,181 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool } from './types-CdORv1Yu.js';
|
|
2
|
+
export { A as AssistantMessage, C as CoreMessage, t as DEFAULT_CAPABILITIES, D as DoGenerateParams, d as DoGenerateResult, E as ErrorChunk, F as FilePart, q as FinishChunk, s as FinishReason, k as GenerateStep, I as ImagePart, L as LanguageModel, M as ModelCapabilities, R as ResponseOptions, m as StreamChunk, l as StreamPart, e as SystemMessage, n as TextDeltaChunk, h as TextPart, r as TokenUsage, i as ToolCall, o as ToolCallChunk, f as ToolMessage, j as ToolResult, p as ToolResultChunk, g as UserContentPart, U as UserMessage } from './types-CdORv1Yu.js';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
export { createOpenAI, openai } from './providers/openai/index.js';
|
|
5
|
+
export { anthropic, createAnthropic } from './providers/anthropic/index.js';
|
|
6
|
+
export { createXAI, xai } from './providers/xai/index.js';
|
|
7
|
+
export { createGoogle, google } from './providers/google/index.js';
|
|
8
|
+
import { LLMConfig, ActionDefinition, ToolDefinition, AgentLoopConfig, KnowledgeBaseConfig, DoneEventMessage, StreamEvent, Message, AIProvider as AIProvider$1, ToolResponse } from '@yourgpt/copilot-sdk/core';
|
|
2
9
|
export { ActionDefinition, AgentLoopConfig, LLMConfig, LLMProvider, Message, StreamEvent, ToolDefinition, ToolExecution, ToolLocation, ToolResponse, UnifiedToolCall, UnifiedToolResult } from '@yourgpt/copilot-sdk/core';
|
|
3
|
-
import {
|
|
4
|
-
export {
|
|
10
|
+
import { A as AIProvider, P as ProviderCapabilities, d as ProviderFormatter } from './types-DcoCaVVC.js';
|
|
11
|
+
export { c as AnthropicProviderConfig, e as AnthropicTool, g as AnthropicToolResult, f as AnthropicToolUse, a as AzureProviderConfig, B as BaseProviderConfig, l as GeminiFunctionCall, k as GeminiFunctionDeclaration, m as GeminiFunctionResponse, G as GoogleProviderConfig, O as OllamaProviderConfig, b as OpenAIProviderConfig, h as OpenAITool, i as OpenAIToolCall, j as OpenAIToolResult, X as XAIProviderConfig } from './types-DcoCaVVC.js';
|
|
12
|
+
import { L as LLMAdapter } from './base-D_FyHFKj.js';
|
|
13
|
+
export { A as AdapterFactory, C as ChatCompletionRequest } from './base-D_FyHFKj.js';
|
|
5
14
|
import * as hono from 'hono';
|
|
6
15
|
import { Hono } from 'hono';
|
|
16
|
+
export { AnthropicAdapter, AnthropicAdapterConfig, AzureAdapter, AzureAdapterConfig, GoogleAdapter, GoogleAdapterConfig, OllamaAdapter, OllamaAdapterConfig, OpenAIAdapter, OpenAIAdapterConfig, XAIAdapter, XAIAdapterConfig, createAnthropicAdapter, createAzureAdapter, createGoogleAdapter, createOllamaAdapter, createOpenAIAdapter, createXAIAdapter } from './adapters/index.js';
|
|
17
|
+
export { createOllama } from './providers/ollama/index.js';
|
|
18
|
+
export { createAzure } from './providers/azure/index.js';
|
|
7
19
|
|
|
8
20
|
/**
|
|
9
|
-
*
|
|
21
|
+
* generateText - Generate text using a language model
|
|
10
22
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { generateText } from '@yourgpt/llm-sdk';
|
|
26
|
+
* import { openai } from '@yourgpt/llm-sdk/openai';
|
|
27
|
+
*
|
|
28
|
+
* // Simple usage
|
|
29
|
+
* const result = await generateText({
|
|
30
|
+
* model: openai('gpt-4o'),
|
|
31
|
+
* prompt: 'Explain quantum computing in simple terms.',
|
|
32
|
+
* });
|
|
33
|
+
* console.log(result.text);
|
|
34
|
+
*
|
|
35
|
+
* // With tools (agentic)
|
|
36
|
+
* const result = await generateText({
|
|
37
|
+
* model: openai('gpt-4o'),
|
|
38
|
+
* prompt: 'What is the weather in Tokyo?',
|
|
39
|
+
* tools: { weather: weatherTool },
|
|
40
|
+
* maxSteps: 5,
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
14
43
|
*/
|
|
15
44
|
|
|
16
45
|
/**
|
|
17
|
-
*
|
|
46
|
+
* Generate text using a language model
|
|
18
47
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* - Tool call parsing from responses
|
|
22
|
-
* - Tool result formatting
|
|
23
|
-
* - Stop reason detection
|
|
24
|
-
*/
|
|
25
|
-
interface ProviderFormatter {
|
|
26
|
-
/**
|
|
27
|
-
* Transform unified tool definitions to provider format
|
|
28
|
-
*/
|
|
29
|
-
transformTools(tools: ToolDefinition[]): unknown[];
|
|
30
|
-
/**
|
|
31
|
-
* Parse tool calls from provider response
|
|
32
|
-
*/
|
|
33
|
-
parseToolCalls(response: unknown): UnifiedToolCall[];
|
|
34
|
-
/**
|
|
35
|
-
* Format tool results for provider
|
|
36
|
-
*/
|
|
37
|
-
formatToolResults(results: UnifiedToolResult[]): unknown[];
|
|
38
|
-
/**
|
|
39
|
-
* Check if response indicates tool use is requested
|
|
40
|
-
*/
|
|
41
|
-
isToolUseStop(response: unknown): boolean;
|
|
42
|
-
/**
|
|
43
|
-
* Check if response indicates end of turn
|
|
44
|
-
*/
|
|
45
|
-
isEndTurnStop(response: unknown): boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Get stop reason string from response
|
|
48
|
-
*/
|
|
49
|
-
getStopReason(response: unknown): string;
|
|
50
|
-
/**
|
|
51
|
-
* Extract text content from response
|
|
52
|
-
*/
|
|
53
|
-
extractTextContent(response: unknown): string;
|
|
54
|
-
/**
|
|
55
|
-
* Build assistant message with tool calls for conversation history
|
|
56
|
-
*/
|
|
57
|
-
buildAssistantToolMessage(toolCalls: UnifiedToolCall[], textContent?: string): unknown;
|
|
58
|
-
/**
|
|
59
|
-
* Build user message with tool results for conversation history
|
|
60
|
-
*/
|
|
61
|
-
buildToolResultMessage(results: UnifiedToolResult[]): unknown;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Anthropic tool definition format
|
|
65
|
-
*/
|
|
66
|
-
interface AnthropicTool {
|
|
67
|
-
name: string;
|
|
68
|
-
description: string;
|
|
69
|
-
input_schema: {
|
|
70
|
-
type: "object";
|
|
71
|
-
properties: Record<string, unknown>;
|
|
72
|
-
required?: string[];
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Anthropic tool_use block from response
|
|
77
|
-
*/
|
|
78
|
-
interface AnthropicToolUse {
|
|
79
|
-
type: "tool_use";
|
|
80
|
-
id: string;
|
|
81
|
-
name: string;
|
|
82
|
-
input: Record<string, unknown>;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Anthropic tool_result block
|
|
86
|
-
*/
|
|
87
|
-
interface AnthropicToolResult {
|
|
88
|
-
type: "tool_result";
|
|
89
|
-
tool_use_id: string;
|
|
90
|
-
content: string;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* OpenAI tool definition format
|
|
94
|
-
*/
|
|
95
|
-
interface OpenAITool {
|
|
96
|
-
type: "function";
|
|
97
|
-
function: {
|
|
98
|
-
name: string;
|
|
99
|
-
description: string;
|
|
100
|
-
parameters: {
|
|
101
|
-
type: "object";
|
|
102
|
-
properties: Record<string, unknown>;
|
|
103
|
-
required?: string[];
|
|
104
|
-
};
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* OpenAI tool call from response
|
|
109
|
-
*/
|
|
110
|
-
interface OpenAIToolCall {
|
|
111
|
-
id: string;
|
|
112
|
-
type: "function";
|
|
113
|
-
function: {
|
|
114
|
-
name: string;
|
|
115
|
-
arguments: string;
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* OpenAI tool result message
|
|
120
|
-
*/
|
|
121
|
-
interface OpenAIToolResult {
|
|
122
|
-
role: "tool";
|
|
123
|
-
tool_call_id: string;
|
|
124
|
-
content: string;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Google Gemini function declaration
|
|
128
|
-
*/
|
|
129
|
-
interface GeminiFunctionDeclaration {
|
|
130
|
-
name: string;
|
|
131
|
-
description: string;
|
|
132
|
-
parameters?: {
|
|
133
|
-
type: "object";
|
|
134
|
-
properties: Record<string, unknown>;
|
|
135
|
-
required?: string[];
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Gemini function call from response
|
|
140
|
-
*/
|
|
141
|
-
interface GeminiFunctionCall {
|
|
142
|
-
name: string;
|
|
143
|
-
args: Record<string, unknown>;
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Gemini function response
|
|
147
|
-
*/
|
|
148
|
-
interface GeminiFunctionResponse {
|
|
149
|
-
name: string;
|
|
150
|
-
response: Record<string, unknown>;
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Capabilities of a model for UI feature flags
|
|
154
|
-
* UI components can use this to enable/disable features
|
|
48
|
+
* @param params - Generation parameters
|
|
49
|
+
* @returns Promise resolving to generation result with text, usage, and tool interactions
|
|
155
50
|
*/
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
supportsVision: boolean;
|
|
159
|
-
/** Supports tool/function calling */
|
|
160
|
-
supportsTools: boolean;
|
|
161
|
-
/** Supports extended thinking (Claude, DeepSeek) */
|
|
162
|
-
supportsThinking: boolean;
|
|
163
|
-
/** Supports streaming responses */
|
|
164
|
-
supportsStreaming: boolean;
|
|
165
|
-
/** Supports PDF document inputs */
|
|
166
|
-
supportsPDF: boolean;
|
|
167
|
-
/** Supports audio inputs */
|
|
168
|
-
supportsAudio: boolean;
|
|
169
|
-
/** Supports video inputs */
|
|
170
|
-
supportsVideo: boolean;
|
|
171
|
-
/** Maximum context tokens */
|
|
172
|
-
maxTokens: number;
|
|
173
|
-
/** Supported image MIME types */
|
|
174
|
-
supportedImageTypes: string[];
|
|
175
|
-
/** Supported audio MIME types */
|
|
176
|
-
supportedAudioTypes?: string[];
|
|
177
|
-
/** Supported video MIME types */
|
|
178
|
-
supportedVideoTypes?: string[];
|
|
179
|
-
/** Supports JSON mode / structured output */
|
|
180
|
-
supportsJsonMode?: boolean;
|
|
181
|
-
/** Supports system messages */
|
|
182
|
-
supportsSystemMessages?: boolean;
|
|
183
|
-
}
|
|
51
|
+
declare function generateText(params: GenerateTextParams): Promise<GenerateTextResult>;
|
|
52
|
+
|
|
184
53
|
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
* Wraps existing LLMAdapter with additional metadata:
|
|
188
|
-
* - Supported models list
|
|
189
|
-
* - Per-model capabilities
|
|
190
|
-
* - Provider name
|
|
54
|
+
* streamText - Stream text from a language model
|
|
191
55
|
*
|
|
192
56
|
* @example
|
|
193
|
-
* ```
|
|
194
|
-
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { streamText } from '@yourgpt/llm-sdk';
|
|
59
|
+
* import { openai } from '@yourgpt/llm-sdk/openai';
|
|
195
60
|
*
|
|
196
|
-
* //
|
|
197
|
-
* const
|
|
61
|
+
* // Simple streaming
|
|
62
|
+
* const result = await streamText({
|
|
63
|
+
* model: openai('gpt-4o'),
|
|
64
|
+
* prompt: 'Tell me a story.',
|
|
65
|
+
* });
|
|
198
66
|
*
|
|
199
|
-
* //
|
|
200
|
-
* const
|
|
201
|
-
*
|
|
202
|
-
* // Show image upload button
|
|
67
|
+
* // Option 1: Iterate text chunks
|
|
68
|
+
* for await (const chunk of result.textStream) {
|
|
69
|
+
* process.stdout.write(chunk);
|
|
203
70
|
* }
|
|
71
|
+
*
|
|
72
|
+
* // Option 2: Get full text
|
|
73
|
+
* const text = await result.text;
|
|
74
|
+
*
|
|
75
|
+
* // Option 3: Return as Response (for API routes)
|
|
76
|
+
* return result.toTextStreamResponse();
|
|
204
77
|
* ```
|
|
205
78
|
*/
|
|
206
|
-
|
|
207
|
-
/** Provider name (e.g., 'openai', 'anthropic') */
|
|
208
|
-
readonly name: string;
|
|
209
|
-
/** List of supported model IDs */
|
|
210
|
-
readonly supportedModels: string[];
|
|
211
|
-
/**
|
|
212
|
-
* Get a language model adapter for the given model ID
|
|
213
|
-
* Returns the existing LLMAdapter interface - no breaking changes
|
|
214
|
-
*/
|
|
215
|
-
languageModel(modelId: string): LLMAdapter;
|
|
216
|
-
/**
|
|
217
|
-
* Get capabilities for a specific model
|
|
218
|
-
* UI components use this to enable/disable features
|
|
219
|
-
*/
|
|
220
|
-
getCapabilities(modelId: string): ProviderCapabilities;
|
|
221
|
-
/**
|
|
222
|
-
* Optional: Get an embedding model (future expansion)
|
|
223
|
-
*/
|
|
224
|
-
embeddingModel?(modelId: string): EmbeddingModel;
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Embedding model interface (for future expansion)
|
|
228
|
-
*/
|
|
229
|
-
interface EmbeddingModel {
|
|
230
|
-
readonly provider: string;
|
|
231
|
-
readonly modelId: string;
|
|
232
|
-
embed(texts: string[]): Promise<number[][]>;
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Base provider configuration
|
|
236
|
-
*/
|
|
237
|
-
interface BaseProviderConfig {
|
|
238
|
-
/** API key (falls back to environment variable) */
|
|
239
|
-
apiKey?: string;
|
|
240
|
-
/** Custom base URL */
|
|
241
|
-
baseUrl?: string;
|
|
242
|
-
/** Request timeout in milliseconds */
|
|
243
|
-
timeout?: number;
|
|
244
|
-
/** Custom headers to include */
|
|
245
|
-
headers?: Record<string, string>;
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* OpenAI provider configuration
|
|
249
|
-
*/
|
|
250
|
-
interface OpenAIProviderConfig extends BaseProviderConfig {
|
|
251
|
-
/** OpenAI organization ID */
|
|
252
|
-
organization?: string;
|
|
253
|
-
/** OpenAI project ID */
|
|
254
|
-
project?: string;
|
|
255
|
-
/** Vision detail level for images */
|
|
256
|
-
imageDetail?: "auto" | "low" | "high";
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Anthropic provider configuration
|
|
260
|
-
*/
|
|
261
|
-
interface AnthropicProviderConfig extends BaseProviderConfig {
|
|
262
|
-
/** Extended thinking budget in tokens (minimum 1024) */
|
|
263
|
-
thinkingBudget?: number;
|
|
264
|
-
/** Enable prompt caching */
|
|
265
|
-
cacheControl?: boolean;
|
|
266
|
-
}
|
|
79
|
+
|
|
267
80
|
/**
|
|
268
|
-
*
|
|
81
|
+
* Stream text from a language model
|
|
82
|
+
*
|
|
83
|
+
* @param params - Stream parameters
|
|
84
|
+
* @returns Promise resolving to stream result with text stream and helpers
|
|
269
85
|
*/
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
safetySettings?: GoogleSafetySetting[];
|
|
273
|
-
/** Grounding configuration (for web search) */
|
|
274
|
-
groundingConfig?: GoogleGroundingConfig;
|
|
275
|
-
}
|
|
86
|
+
declare function streamText(params: StreamTextParams): Promise<StreamTextResult>;
|
|
87
|
+
|
|
276
88
|
/**
|
|
277
|
-
*
|
|
89
|
+
* Tool Helper
|
|
90
|
+
*
|
|
91
|
+
* Create type-safe tools with Zod schema validation.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { tool } from '@yourgpt/llm-sdk';
|
|
96
|
+
* import { z } from 'zod';
|
|
97
|
+
*
|
|
98
|
+
* const weatherTool = tool({
|
|
99
|
+
* description: 'Get current weather for a city',
|
|
100
|
+
* parameters: z.object({
|
|
101
|
+
* city: z.string().describe('City name'),
|
|
102
|
+
* unit: z.enum(['celsius', 'fahrenheit']).optional(),
|
|
103
|
+
* }),
|
|
104
|
+
* execute: async ({ city, unit }) => {
|
|
105
|
+
* const data = await fetchWeather(city);
|
|
106
|
+
* return { temperature: data.temp, condition: data.condition };
|
|
107
|
+
* },
|
|
108
|
+
* });
|
|
109
|
+
* ```
|
|
278
110
|
*/
|
|
279
|
-
|
|
280
|
-
category: "HARM_CATEGORY_HARASSMENT" | "HARM_CATEGORY_HATE_SPEECH" | "HARM_CATEGORY_SEXUALLY_EXPLICIT" | "HARM_CATEGORY_DANGEROUS_CONTENT";
|
|
281
|
-
threshold: "BLOCK_NONE" | "BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_HIGH_AND_ABOVE";
|
|
282
|
-
}
|
|
111
|
+
|
|
283
112
|
/**
|
|
284
|
-
*
|
|
113
|
+
* Configuration for creating a tool
|
|
285
114
|
*/
|
|
286
|
-
interface
|
|
287
|
-
/**
|
|
288
|
-
|
|
115
|
+
interface ToolConfig<TParams extends z.ZodType, TResult = unknown> {
|
|
116
|
+
/** Description of what the tool does (shown to LLM) */
|
|
117
|
+
description: string;
|
|
118
|
+
/** Zod schema defining the tool's parameters */
|
|
119
|
+
parameters: TParams;
|
|
120
|
+
/** Function to execute when the tool is called */
|
|
121
|
+
execute: (params: z.infer<TParams>, context: ToolContext) => Promise<TResult>;
|
|
289
122
|
}
|
|
290
123
|
/**
|
|
291
|
-
*
|
|
124
|
+
* Create a type-safe tool definition
|
|
125
|
+
*
|
|
126
|
+
* @param config - Tool configuration with description, parameters, and execute function
|
|
127
|
+
* @returns A Tool object that can be passed to generateText/streamText
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const searchTool = tool({
|
|
132
|
+
* description: 'Search for products in the database',
|
|
133
|
+
* parameters: z.object({
|
|
134
|
+
* query: z.string(),
|
|
135
|
+
* maxResults: z.number().optional().default(10),
|
|
136
|
+
* }),
|
|
137
|
+
* execute: async ({ query, maxResults }) => {
|
|
138
|
+
* return await db.products.search(query, maxResults);
|
|
139
|
+
* },
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* const result = await generateText({
|
|
143
|
+
* model: openai('gpt-4o'),
|
|
144
|
+
* prompt: 'Find me some headphones',
|
|
145
|
+
* tools: { search: searchTool },
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
292
148
|
*/
|
|
293
|
-
|
|
294
|
-
}
|
|
149
|
+
declare function tool<TParams extends z.ZodType, TResult = unknown>(config: ToolConfig<TParams, TResult>): Tool<z.infer<TParams>, TResult>;
|
|
295
150
|
/**
|
|
296
|
-
*
|
|
151
|
+
* Format tools for OpenAI API format
|
|
297
152
|
*/
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
153
|
+
declare function formatToolsForOpenAI(tools: Record<string, Tool>): Array<{
|
|
154
|
+
type: "function";
|
|
155
|
+
function: {
|
|
156
|
+
name: string;
|
|
157
|
+
description: string;
|
|
158
|
+
parameters: object;
|
|
159
|
+
};
|
|
160
|
+
}>;
|
|
306
161
|
/**
|
|
307
|
-
*
|
|
162
|
+
* Format tools for Anthropic API format
|
|
308
163
|
*/
|
|
309
|
-
|
|
310
|
-
|
|
164
|
+
declare function formatToolsForAnthropic(tools: Record<string, Tool>): Array<{
|
|
165
|
+
name: string;
|
|
166
|
+
description: string;
|
|
167
|
+
input_schema: object;
|
|
168
|
+
}>;
|
|
311
169
|
/**
|
|
312
|
-
*
|
|
170
|
+
* Format tools for Google Gemini API format
|
|
313
171
|
*/
|
|
314
|
-
|
|
315
|
-
|
|
172
|
+
declare function formatToolsForGoogle(tools: Record<string, Tool>): Array<{
|
|
173
|
+
functionDeclarations: Array<{
|
|
174
|
+
name: string;
|
|
175
|
+
description: string;
|
|
176
|
+
parameters?: object;
|
|
177
|
+
}>;
|
|
178
|
+
}>;
|
|
316
179
|
|
|
317
180
|
/**
|
|
318
181
|
* Runtime configuration with LLM config
|
|
@@ -503,6 +366,45 @@ interface RequestContext {
|
|
|
503
366
|
body: ChatRequest;
|
|
504
367
|
}
|
|
505
368
|
|
|
369
|
+
/**
|
|
370
|
+
* Result passed to onFinish callback after stream completes
|
|
371
|
+
*/
|
|
372
|
+
interface HandleRequestResult {
|
|
373
|
+
/** All messages from this request (assistant messages + tool results) */
|
|
374
|
+
messages: DoneEventMessage[];
|
|
375
|
+
/** Thread ID if provided in request */
|
|
376
|
+
threadId?: string;
|
|
377
|
+
/** Token usage information */
|
|
378
|
+
usage?: {
|
|
379
|
+
promptTokens: number;
|
|
380
|
+
completionTokens: number;
|
|
381
|
+
totalTokens: number;
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Options for handleRequest method
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* ```typescript
|
|
389
|
+
* runtime.handleRequest(request, {
|
|
390
|
+
* onFinish: async ({ messages, threadId }) => {
|
|
391
|
+
* await db.thread.upsert({
|
|
392
|
+
* where: { id: threadId },
|
|
393
|
+
* update: { messages, updatedAt: new Date() },
|
|
394
|
+
* create: { id: threadId, messages },
|
|
395
|
+
* });
|
|
396
|
+
* },
|
|
397
|
+
* });
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
interface HandleRequestOptions {
|
|
401
|
+
/**
|
|
402
|
+
* Called after the stream completes successfully.
|
|
403
|
+
* Use this for server-side persistence of messages.
|
|
404
|
+
*/
|
|
405
|
+
onFinish?: (result: HandleRequestResult) => Promise<void> | void;
|
|
406
|
+
}
|
|
407
|
+
|
|
506
408
|
/**
|
|
507
409
|
* Copilot SDK Runtime
|
|
508
410
|
*
|
|
@@ -524,8 +426,32 @@ declare class Runtime {
|
|
|
524
426
|
processChat(request: ChatRequest, signal?: AbortSignal): AsyncGenerator<StreamEvent>;
|
|
525
427
|
/**
|
|
526
428
|
* Handle HTTP request (for use with any framework)
|
|
429
|
+
*
|
|
430
|
+
* @param request - The HTTP request
|
|
431
|
+
* @param options - Optional configuration including onFinish callback for persistence
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```typescript
|
|
435
|
+
* // Basic usage
|
|
436
|
+
* return runtime.handleRequest(request);
|
|
437
|
+
*
|
|
438
|
+
* // With server-side persistence
|
|
439
|
+
* return runtime.handleRequest(request, {
|
|
440
|
+
* onFinish: async ({ messages, threadId }) => {
|
|
441
|
+
* await db.thread.upsert({
|
|
442
|
+
* where: { id: threadId },
|
|
443
|
+
* update: { messages, updatedAt: new Date() },
|
|
444
|
+
* create: { id: threadId, messages },
|
|
445
|
+
* });
|
|
446
|
+
* },
|
|
447
|
+
* });
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
450
|
+
handleRequest(request: Request, options?: HandleRequestOptions): Promise<Response>;
|
|
451
|
+
/**
|
|
452
|
+
* Wrap a generator to intercept the done event and call onFinish
|
|
527
453
|
*/
|
|
528
|
-
|
|
454
|
+
private wrapGeneratorWithOnFinish;
|
|
529
455
|
/**
|
|
530
456
|
* Handle non-streaming request - returns JSON instead of SSE
|
|
531
457
|
*/
|
|
@@ -795,174 +721,6 @@ declare function getAvailableProviders(): Array<{
|
|
|
795
721
|
*/
|
|
796
722
|
declare function getModelCapabilities(providerName: string, modelId: string): ProviderCapabilities | undefined;
|
|
797
723
|
|
|
798
|
-
/**
|
|
799
|
-
* OpenAI Provider
|
|
800
|
-
*
|
|
801
|
-
* Wraps the existing OpenAIAdapter with provider interface.
|
|
802
|
-
* Adds model capabilities and metadata.
|
|
803
|
-
*/
|
|
804
|
-
|
|
805
|
-
/**
|
|
806
|
-
* Create an OpenAI provider
|
|
807
|
-
*
|
|
808
|
-
* @example
|
|
809
|
-
* ```typescript
|
|
810
|
-
* const openai = createOpenAI({ apiKey: '...' });
|
|
811
|
-
* const adapter = openai.languageModel('gpt-4o');
|
|
812
|
-
* const caps = openai.getCapabilities('gpt-4o');
|
|
813
|
-
* ```
|
|
814
|
-
*/
|
|
815
|
-
declare function createOpenAI(config?: OpenAIProviderConfig): AIProvider;
|
|
816
|
-
|
|
817
|
-
/**
|
|
818
|
-
* Anthropic Provider
|
|
819
|
-
*
|
|
820
|
-
* Wraps the existing AnthropicAdapter with provider interface.
|
|
821
|
-
* Adds model capabilities and metadata including extended thinking support.
|
|
822
|
-
*/
|
|
823
|
-
|
|
824
|
-
/**
|
|
825
|
-
* Create an Anthropic provider
|
|
826
|
-
*
|
|
827
|
-
* @example
|
|
828
|
-
* ```typescript
|
|
829
|
-
* const anthropic = createAnthropic({
|
|
830
|
-
* apiKey: '...',
|
|
831
|
-
* thinkingBudget: 10000, // Enable extended thinking
|
|
832
|
-
* });
|
|
833
|
-
* const adapter = anthropic.languageModel('claude-sonnet-4-20250514');
|
|
834
|
-
* const caps = anthropic.getCapabilities('claude-sonnet-4-20250514');
|
|
835
|
-
* ```
|
|
836
|
-
*/
|
|
837
|
-
declare function createAnthropic(config?: AnthropicProviderConfig): AIProvider;
|
|
838
|
-
|
|
839
|
-
/**
|
|
840
|
-
* Groq Provider
|
|
841
|
-
*
|
|
842
|
-
* Wraps the existing GroqAdapter with provider interface.
|
|
843
|
-
* Groq provides fast inference for open-source models.
|
|
844
|
-
*/
|
|
845
|
-
|
|
846
|
-
/**
|
|
847
|
-
* Create a Groq provider
|
|
848
|
-
*
|
|
849
|
-
* @example
|
|
850
|
-
* ```typescript
|
|
851
|
-
* const groq = createGroq({ apiKey: '...' });
|
|
852
|
-
* const adapter = groq.languageModel('llama-3.3-70b-versatile');
|
|
853
|
-
* ```
|
|
854
|
-
*/
|
|
855
|
-
declare function createGroq(config?: GroqProviderConfig): AIProvider;
|
|
856
|
-
|
|
857
|
-
/**
|
|
858
|
-
* Ollama Provider
|
|
859
|
-
*
|
|
860
|
-
* Wraps the existing OllamaAdapter with provider interface.
|
|
861
|
-
* Ollama runs models locally on your machine.
|
|
862
|
-
*/
|
|
863
|
-
|
|
864
|
-
/**
|
|
865
|
-
* Create an Ollama provider
|
|
866
|
-
*
|
|
867
|
-
* @example
|
|
868
|
-
* ```typescript
|
|
869
|
-
* const ollama = createOllama({ baseUrl: 'http://localhost:11434' });
|
|
870
|
-
* const adapter = ollama.languageModel('llama3');
|
|
871
|
-
* ```
|
|
872
|
-
*/
|
|
873
|
-
declare function createOllama(config?: OllamaProviderConfig): AIProvider;
|
|
874
|
-
|
|
875
|
-
/**
|
|
876
|
-
* Google Provider
|
|
877
|
-
*
|
|
878
|
-
* Wraps the GoogleAdapter with provider interface.
|
|
879
|
-
* Adds model capabilities and metadata for multimodal support.
|
|
880
|
-
*
|
|
881
|
-
* Features:
|
|
882
|
-
* - Vision (images)
|
|
883
|
-
* - Audio input
|
|
884
|
-
* - Video input
|
|
885
|
-
* - PDF documents
|
|
886
|
-
* - Tools/Function calling
|
|
887
|
-
* - Massive context windows (up to 2M tokens)
|
|
888
|
-
*/
|
|
889
|
-
|
|
890
|
-
/**
|
|
891
|
-
* Create a Google provider
|
|
892
|
-
*
|
|
893
|
-
* @example
|
|
894
|
-
* ```typescript
|
|
895
|
-
* const google = createGoogle({
|
|
896
|
-
* apiKey: '...',
|
|
897
|
-
* });
|
|
898
|
-
* const adapter = google.languageModel('gemini-2.0-flash');
|
|
899
|
-
* const caps = google.getCapabilities('gemini-2.0-flash');
|
|
900
|
-
*
|
|
901
|
-
* if (caps.supportsVideo) {
|
|
902
|
-
* // Show video upload button
|
|
903
|
-
* }
|
|
904
|
-
* ```
|
|
905
|
-
*/
|
|
906
|
-
declare function createGoogle(config?: GoogleProviderConfig): AIProvider;
|
|
907
|
-
|
|
908
|
-
/**
|
|
909
|
-
* xAI Provider
|
|
910
|
-
*
|
|
911
|
-
* Wraps the XAIAdapter with provider interface.
|
|
912
|
-
* xAI's Grok models are cutting-edge AI models with vision and tool support.
|
|
913
|
-
*
|
|
914
|
-
* Features:
|
|
915
|
-
* - Vision (images)
|
|
916
|
-
* - Tools/Function calling
|
|
917
|
-
* - Real-time information (trained on X/Twitter data)
|
|
918
|
-
*/
|
|
919
|
-
|
|
920
|
-
/**
|
|
921
|
-
* Create an xAI provider
|
|
922
|
-
*
|
|
923
|
-
* @example
|
|
924
|
-
* ```typescript
|
|
925
|
-
* const xai = createXAI({
|
|
926
|
-
* apiKey: '...',
|
|
927
|
-
* });
|
|
928
|
-
* const adapter = xai.languageModel('grok-2');
|
|
929
|
-
* const caps = xai.getCapabilities('grok-2');
|
|
930
|
-
* ```
|
|
931
|
-
*/
|
|
932
|
-
declare function createXAI(config?: XAIProviderConfig): AIProvider;
|
|
933
|
-
|
|
934
|
-
/**
|
|
935
|
-
* Azure OpenAI Provider
|
|
936
|
-
*
|
|
937
|
-
* Wraps the AzureAdapter with provider interface.
|
|
938
|
-
* Azure OpenAI provides enterprise-grade OpenAI models with Azure security.
|
|
939
|
-
*
|
|
940
|
-
* Features:
|
|
941
|
-
* - Vision (for supported deployments)
|
|
942
|
-
* - Tools/Function calling
|
|
943
|
-
* - Enterprise security & compliance
|
|
944
|
-
* - Private networking options
|
|
945
|
-
*
|
|
946
|
-
* Note: Capabilities depend on which model is deployed, not a model ID.
|
|
947
|
-
* The provider attempts to detect capabilities from the deployment name.
|
|
948
|
-
*/
|
|
949
|
-
|
|
950
|
-
/**
|
|
951
|
-
* Create an Azure OpenAI provider
|
|
952
|
-
*
|
|
953
|
-
* @example
|
|
954
|
-
* ```typescript
|
|
955
|
-
* const azure = createAzure({
|
|
956
|
-
* apiKey: '...',
|
|
957
|
-
* resourceName: 'my-azure-resource',
|
|
958
|
-
* deploymentName: 'gpt-4o-deployment',
|
|
959
|
-
* });
|
|
960
|
-
* const adapter = azure.languageModel('gpt-4o-deployment');
|
|
961
|
-
* const caps = azure.getCapabilities('gpt-4o-deployment');
|
|
962
|
-
* ```
|
|
963
|
-
*/
|
|
964
|
-
declare function createAzure(config: AzureProviderConfig): AIProvider;
|
|
965
|
-
|
|
966
724
|
/**
|
|
967
725
|
* OpenAI Provider Formatter
|
|
968
726
|
*
|
|
@@ -1045,4 +803,4 @@ declare function isProviderSupported(provider: string): boolean;
|
|
|
1045
803
|
*/
|
|
1046
804
|
declare function getSupportedProviders(): string[];
|
|
1047
805
|
|
|
1048
|
-
export {
|
|
806
|
+
export { AIProvider, type ActionRequest, type AgentLoopOptions, type ChatRequest, DEFAULT_MAX_ITERATIONS, GenerateTextParams, GenerateTextResult, LLMAdapter, ProviderCapabilities, ProviderFormatter, type RequestContext, Runtime, type RuntimeConfig, StreamTextParams, StreamTextResult, Tool, ToolContext, anthropicFormatter, createEventStream, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, geminiFormatter, generateText, getAvailableProviders, getFormatter, getModelCapabilities, getProvider, getSupportedProviders, hasProvider, isProviderSupported, listProviders, openaiFormatter, registerProvider, runAgentLoop, streamText, tool };
|