@yourgpt/llm-sdk 2.1.4-alpha.0 → 2.1.4-alpha.2
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/adapters/index.d.mts +3 -2
- package/dist/adapters/index.d.ts +3 -2
- package/dist/index.d.mts +59 -3
- package/dist/index.d.ts +59 -3
- package/dist/index.js +117 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +116 -3
- package/dist/index.mjs.map +1 -1
- package/dist/providers/anthropic/index.d.mts +2 -1
- package/dist/providers/anthropic/index.d.ts +2 -1
- package/dist/providers/azure/index.d.mts +2 -1
- package/dist/providers/azure/index.d.ts +2 -1
- package/dist/providers/google/index.d.mts +2 -1
- package/dist/providers/google/index.d.ts +2 -1
- package/dist/providers/ollama/index.d.mts +3 -2
- package/dist/providers/ollama/index.d.ts +3 -2
- package/dist/providers/openai/index.d.mts +2 -1
- package/dist/providers/openai/index.d.ts +2 -1
- package/dist/providers/openrouter/index.d.mts +2 -1
- package/dist/providers/openrouter/index.d.ts +2 -1
- package/dist/providers/xai/index.d.mts +2 -1
- package/dist/providers/xai/index.d.ts +2 -1
- package/dist/types-CR8mi9I0.d.mts +417 -0
- package/dist/types-CR8mi9I0.d.ts +417 -0
- package/dist/{types-COAOEe_y.d.mts → types-DoZX2k3v.d.mts} +4 -344
- package/dist/{types-COAOEe_y.d.ts → types-DtPQaoJi.d.ts} +4 -344
- package/dist/yourgpt/index.d.mts +77 -0
- package/dist/yourgpt/index.d.ts +77 -0
- package/dist/yourgpt/index.js +167 -0
- package/dist/yourgpt/index.js.map +1 -0
- package/dist/yourgpt/index.mjs +164 -0
- package/dist/yourgpt/index.mjs.map +1 -0
- package/package.json +6 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { L as LLMAdapter, W as WebSearchConfig,
|
|
2
|
-
export {
|
|
1
|
+
import { L as LLMAdapter, W as WebSearchConfig, C as ChatCompletionRequest, S as StreamEvent, N as CompletionResult, k as OllamaModelOptions } from '../types-DoZX2k3v.mjs';
|
|
2
|
+
export { f as AdapterFactory, a4 as AnthropicContentBlock, a5 as OpenAIContentBlock, a2 as attachmentToAnthropicDocument, a1 as attachmentToAnthropicImage, a3 as attachmentToOpenAIImage, Q as formatMessages, V as formatMessagesForAnthropic, Y as formatMessagesForOpenAI, R as formatTools, $ as hasImageAttachments, a0 as hasMediaAttachments, Z as messageToAnthropicContent, _ as messageToOpenAIContent } from '../types-DoZX2k3v.mjs';
|
|
3
|
+
import '../types-CR8mi9I0.mjs';
|
|
3
4
|
import 'zod';
|
|
4
5
|
|
|
5
6
|
/**
|
package/dist/adapters/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { L as LLMAdapter, W as WebSearchConfig,
|
|
2
|
-
export {
|
|
1
|
+
import { L as LLMAdapter, W as WebSearchConfig, C as ChatCompletionRequest, S as StreamEvent, N as CompletionResult, k as OllamaModelOptions } from '../types-DtPQaoJi.js';
|
|
2
|
+
export { f as AdapterFactory, a4 as AnthropicContentBlock, a5 as OpenAIContentBlock, a2 as attachmentToAnthropicDocument, a1 as attachmentToAnthropicImage, a3 as attachmentToOpenAIImage, Q as formatMessages, V as formatMessagesForAnthropic, Y as formatMessagesForOpenAI, R as formatTools, $ as hasImageAttachments, a0 as hasMediaAttachments, Z as messageToAnthropicContent, _ as messageToOpenAIContent } from '../types-DtPQaoJi.js';
|
|
3
|
+
import '../types-CR8mi9I0.js';
|
|
3
4
|
import 'zod';
|
|
4
5
|
|
|
5
6
|
/**
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool,
|
|
2
|
-
export {
|
|
1
|
+
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, d as StorageAdapter, e as StorageMessage } from './types-CR8mi9I0.mjs';
|
|
2
|
+
export { A as AssistantMessage, C as CoreMessage, w as DEFAULT_CAPABILITIES, D as DoGenerateParams, f as DoGenerateResult, E as ErrorChunk, F as FilePart, s as FinishChunk, u as FinishReason, m as GenerateStep, I as ImagePart, L as LanguageModel, M as ModelCapabilities, R as ResponseOptions, v as StorageFile, o as StreamChunk, n as StreamPart, g as SystemMessage, p as TextDeltaChunk, j as TextPart, t as TokenUsage, k as ToolCall, q as ToolCallChunk, h as ToolMessage, l as ToolResult, r as ToolResultChunk, i as UserContentPart, U as UserMessage } from './types-CR8mi9I0.mjs';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
+
import { A as AIProvider, a as ActionDefinition, T as ToolDefinition, b as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, L as LLMAdapter, D as DoneEventMessage, S as StreamEvent, c as ToolCallInfo, d as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, e as ToolResponse } from './types-DoZX2k3v.mjs';
|
|
5
|
+
export { f as AdapterFactory, h as AnthropicProviderConfig, I as AnthropicProviderToolOptions, m as AnthropicTool, o as AnthropicToolResult, E as AnthropicToolSelectionHints, n as AnthropicToolUse, i as AzureProviderConfig, B as BaseProviderConfig, C as ChatCompletionRequest, J as Citation, t as GeminiFunctionCall, s as GeminiFunctionDeclaration, u as GeminiFunctionResponse, G as GoogleProviderConfig, v as LLMConfig, k as OllamaModelOptions, j as OllamaProviderConfig, O as OpenAIProviderConfig, H as OpenAIProviderToolOptions, p as OpenAITool, q as OpenAIToolCall, r as OpenAIToolResult, z as OpenAIToolSelectionHints, g as ProviderCapabilities, l as ProviderFormatter, y as ToolExecution, w as ToolLocation, F as ToolNativeProviderHints, U as UnifiedToolCall, x as UnifiedToolResult, X as XAIProviderConfig } from './types-DoZX2k3v.mjs';
|
|
4
6
|
import * as hono from 'hono';
|
|
5
7
|
import { Hono } from 'hono';
|
|
6
8
|
|
|
@@ -257,6 +259,17 @@ interface RuntimeConfigWithAdapter {
|
|
|
257
259
|
* Set to true for defaults, or pass WebSearchConfig for customization.
|
|
258
260
|
*/
|
|
259
261
|
webSearch?: boolean | WebSearchConfig;
|
|
262
|
+
/**
|
|
263
|
+
* Storage adapter for automatic session creation and message persistence.
|
|
264
|
+
* When provided, runtime.chat() and runtime.stream() auto-save messages.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* import { createYourGPT } from '@yourgpt/llm-sdk/yourgpt'
|
|
269
|
+
* storage: createYourGPT({ apiKey, widgetUid })
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
storage?: StorageAdapter;
|
|
260
273
|
}
|
|
261
274
|
/**
|
|
262
275
|
* Runtime configuration with AIProvider
|
|
@@ -303,6 +316,17 @@ interface RuntimeConfigWithProvider {
|
|
|
303
316
|
* Set to true for defaults, or pass WebSearchConfig for customization.
|
|
304
317
|
*/
|
|
305
318
|
webSearch?: boolean | WebSearchConfig;
|
|
319
|
+
/**
|
|
320
|
+
* Storage adapter for automatic session creation and message persistence.
|
|
321
|
+
* When provided, runtime.chat() and runtime.stream() auto-save messages.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { createYourGPT } from '@yourgpt/llm-sdk/yourgpt'
|
|
326
|
+
* storage: createYourGPT({ apiKey, widgetUid })
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
storage?: StorageAdapter;
|
|
306
330
|
}
|
|
307
331
|
/**
|
|
308
332
|
* Runtime configuration - provide either a provider instance or a custom adapter
|
|
@@ -537,6 +561,8 @@ interface OnFinishResult {
|
|
|
537
561
|
completionTokens: number;
|
|
538
562
|
totalTokens: number;
|
|
539
563
|
};
|
|
564
|
+
/** Session ID — present when storage adapter created/resolved a session */
|
|
565
|
+
threadId?: string;
|
|
540
566
|
}
|
|
541
567
|
/**
|
|
542
568
|
* Options for StreamResult constructor
|
|
@@ -562,6 +588,8 @@ interface CollectedResult {
|
|
|
562
588
|
requiresAction: boolean;
|
|
563
589
|
/** Token usage for billing/tracking */
|
|
564
590
|
usage?: TokenUsageRaw;
|
|
591
|
+
/** Session ID — present when storage adapter created/resolved a session */
|
|
592
|
+
threadId?: string;
|
|
565
593
|
/** Raw events (for debugging) */
|
|
566
594
|
events: StreamEvent[];
|
|
567
595
|
}
|
|
@@ -870,6 +898,7 @@ type ToolSearchState = {
|
|
|
870
898
|
declare class Runtime {
|
|
871
899
|
private adapter;
|
|
872
900
|
private config;
|
|
901
|
+
private storage;
|
|
873
902
|
private actions;
|
|
874
903
|
private tools;
|
|
875
904
|
constructor(config: RuntimeConfig);
|
|
@@ -1034,6 +1063,7 @@ declare class Runtime {
|
|
|
1034
1063
|
*/
|
|
1035
1064
|
onFinish?: (result: {
|
|
1036
1065
|
messages: DoneEventMessage[];
|
|
1066
|
+
threadId?: string;
|
|
1037
1067
|
usage?: {
|
|
1038
1068
|
promptTokens: number;
|
|
1039
1069
|
completionTokens: number;
|
|
@@ -1464,6 +1494,32 @@ interface AgentLoopOptions {
|
|
|
1464
1494
|
*/
|
|
1465
1495
|
declare function runAgentLoop(options: AgentLoopOptions): AsyncGenerator<StreamEvent>;
|
|
1466
1496
|
|
|
1497
|
+
/**
|
|
1498
|
+
* Storage Helpers
|
|
1499
|
+
*
|
|
1500
|
+
* Extract input/output messages from request/response for storage adapters.
|
|
1501
|
+
* Used internally by Runtime when `storage` is configured.
|
|
1502
|
+
*/
|
|
1503
|
+
|
|
1504
|
+
/**
|
|
1505
|
+
* Extract new INPUT messages from a request's message array.
|
|
1506
|
+
*
|
|
1507
|
+
* The SDK sends the full conversation history on each request. This function
|
|
1508
|
+
* picks only the NEW messages the user just added:
|
|
1509
|
+
* - New user turn (last meaningful msg = "user"): returns [userMsg]
|
|
1510
|
+
* - Tool continuation (last meaningful msg = "tool"): returns tool results after last assistant
|
|
1511
|
+
* - Otherwise: returns []
|
|
1512
|
+
*
|
|
1513
|
+
* Skips empty assistant placeholders the SDK pushes before sending.
|
|
1514
|
+
*/
|
|
1515
|
+
declare function extractInputMessages(reqMessages: unknown[]): StorageMessage[];
|
|
1516
|
+
/**
|
|
1517
|
+
* Map LLM output messages (DoneEventMessage format) to StorageMessage format.
|
|
1518
|
+
*
|
|
1519
|
+
* Converts from snake_case API format to camelCase storage format.
|
|
1520
|
+
*/
|
|
1521
|
+
declare function mapOutputMessages(resultMessages: DoneEventMessage[]): StorageMessage[];
|
|
1522
|
+
|
|
1467
1523
|
/**
|
|
1468
1524
|
* Generate a message ID
|
|
1469
1525
|
*/
|
|
@@ -1477,4 +1533,4 @@ declare function generateThreadId(): string;
|
|
|
1477
1533
|
*/
|
|
1478
1534
|
declare function generateToolCallId(): string;
|
|
1479
1535
|
|
|
1480
|
-
export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };
|
|
1536
|
+
export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StorageAdapter, StorageMessage, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, extractInputMessages, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, mapOutputMessages, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool,
|
|
2
|
-
export {
|
|
1
|
+
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, d as StorageAdapter, e as StorageMessage } from './types-CR8mi9I0.js';
|
|
2
|
+
export { A as AssistantMessage, C as CoreMessage, w as DEFAULT_CAPABILITIES, D as DoGenerateParams, f as DoGenerateResult, E as ErrorChunk, F as FilePart, s as FinishChunk, u as FinishReason, m as GenerateStep, I as ImagePart, L as LanguageModel, M as ModelCapabilities, R as ResponseOptions, v as StorageFile, o as StreamChunk, n as StreamPart, g as SystemMessage, p as TextDeltaChunk, j as TextPart, t as TokenUsage, k as ToolCall, q as ToolCallChunk, h as ToolMessage, l as ToolResult, r as ToolResultChunk, i as UserContentPart, U as UserMessage } from './types-CR8mi9I0.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
+
import { A as AIProvider, a as ActionDefinition, T as ToolDefinition, b as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, L as LLMAdapter, D as DoneEventMessage, S as StreamEvent, c as ToolCallInfo, d as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, e as ToolResponse } from './types-DtPQaoJi.js';
|
|
5
|
+
export { f as AdapterFactory, h as AnthropicProviderConfig, I as AnthropicProviderToolOptions, m as AnthropicTool, o as AnthropicToolResult, E as AnthropicToolSelectionHints, n as AnthropicToolUse, i as AzureProviderConfig, B as BaseProviderConfig, C as ChatCompletionRequest, J as Citation, t as GeminiFunctionCall, s as GeminiFunctionDeclaration, u as GeminiFunctionResponse, G as GoogleProviderConfig, v as LLMConfig, k as OllamaModelOptions, j as OllamaProviderConfig, O as OpenAIProviderConfig, H as OpenAIProviderToolOptions, p as OpenAITool, q as OpenAIToolCall, r as OpenAIToolResult, z as OpenAIToolSelectionHints, g as ProviderCapabilities, l as ProviderFormatter, y as ToolExecution, w as ToolLocation, F as ToolNativeProviderHints, U as UnifiedToolCall, x as UnifiedToolResult, X as XAIProviderConfig } from './types-DtPQaoJi.js';
|
|
4
6
|
import * as hono from 'hono';
|
|
5
7
|
import { Hono } from 'hono';
|
|
6
8
|
|
|
@@ -257,6 +259,17 @@ interface RuntimeConfigWithAdapter {
|
|
|
257
259
|
* Set to true for defaults, or pass WebSearchConfig for customization.
|
|
258
260
|
*/
|
|
259
261
|
webSearch?: boolean | WebSearchConfig;
|
|
262
|
+
/**
|
|
263
|
+
* Storage adapter for automatic session creation and message persistence.
|
|
264
|
+
* When provided, runtime.chat() and runtime.stream() auto-save messages.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* import { createYourGPT } from '@yourgpt/llm-sdk/yourgpt'
|
|
269
|
+
* storage: createYourGPT({ apiKey, widgetUid })
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
storage?: StorageAdapter;
|
|
260
273
|
}
|
|
261
274
|
/**
|
|
262
275
|
* Runtime configuration with AIProvider
|
|
@@ -303,6 +316,17 @@ interface RuntimeConfigWithProvider {
|
|
|
303
316
|
* Set to true for defaults, or pass WebSearchConfig for customization.
|
|
304
317
|
*/
|
|
305
318
|
webSearch?: boolean | WebSearchConfig;
|
|
319
|
+
/**
|
|
320
|
+
* Storage adapter for automatic session creation and message persistence.
|
|
321
|
+
* When provided, runtime.chat() and runtime.stream() auto-save messages.
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { createYourGPT } from '@yourgpt/llm-sdk/yourgpt'
|
|
326
|
+
* storage: createYourGPT({ apiKey, widgetUid })
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
storage?: StorageAdapter;
|
|
306
330
|
}
|
|
307
331
|
/**
|
|
308
332
|
* Runtime configuration - provide either a provider instance or a custom adapter
|
|
@@ -537,6 +561,8 @@ interface OnFinishResult {
|
|
|
537
561
|
completionTokens: number;
|
|
538
562
|
totalTokens: number;
|
|
539
563
|
};
|
|
564
|
+
/** Session ID — present when storage adapter created/resolved a session */
|
|
565
|
+
threadId?: string;
|
|
540
566
|
}
|
|
541
567
|
/**
|
|
542
568
|
* Options for StreamResult constructor
|
|
@@ -562,6 +588,8 @@ interface CollectedResult {
|
|
|
562
588
|
requiresAction: boolean;
|
|
563
589
|
/** Token usage for billing/tracking */
|
|
564
590
|
usage?: TokenUsageRaw;
|
|
591
|
+
/** Session ID — present when storage adapter created/resolved a session */
|
|
592
|
+
threadId?: string;
|
|
565
593
|
/** Raw events (for debugging) */
|
|
566
594
|
events: StreamEvent[];
|
|
567
595
|
}
|
|
@@ -870,6 +898,7 @@ type ToolSearchState = {
|
|
|
870
898
|
declare class Runtime {
|
|
871
899
|
private adapter;
|
|
872
900
|
private config;
|
|
901
|
+
private storage;
|
|
873
902
|
private actions;
|
|
874
903
|
private tools;
|
|
875
904
|
constructor(config: RuntimeConfig);
|
|
@@ -1034,6 +1063,7 @@ declare class Runtime {
|
|
|
1034
1063
|
*/
|
|
1035
1064
|
onFinish?: (result: {
|
|
1036
1065
|
messages: DoneEventMessage[];
|
|
1066
|
+
threadId?: string;
|
|
1037
1067
|
usage?: {
|
|
1038
1068
|
promptTokens: number;
|
|
1039
1069
|
completionTokens: number;
|
|
@@ -1464,6 +1494,32 @@ interface AgentLoopOptions {
|
|
|
1464
1494
|
*/
|
|
1465
1495
|
declare function runAgentLoop(options: AgentLoopOptions): AsyncGenerator<StreamEvent>;
|
|
1466
1496
|
|
|
1497
|
+
/**
|
|
1498
|
+
* Storage Helpers
|
|
1499
|
+
*
|
|
1500
|
+
* Extract input/output messages from request/response for storage adapters.
|
|
1501
|
+
* Used internally by Runtime when `storage` is configured.
|
|
1502
|
+
*/
|
|
1503
|
+
|
|
1504
|
+
/**
|
|
1505
|
+
* Extract new INPUT messages from a request's message array.
|
|
1506
|
+
*
|
|
1507
|
+
* The SDK sends the full conversation history on each request. This function
|
|
1508
|
+
* picks only the NEW messages the user just added:
|
|
1509
|
+
* - New user turn (last meaningful msg = "user"): returns [userMsg]
|
|
1510
|
+
* - Tool continuation (last meaningful msg = "tool"): returns tool results after last assistant
|
|
1511
|
+
* - Otherwise: returns []
|
|
1512
|
+
*
|
|
1513
|
+
* Skips empty assistant placeholders the SDK pushes before sending.
|
|
1514
|
+
*/
|
|
1515
|
+
declare function extractInputMessages(reqMessages: unknown[]): StorageMessage[];
|
|
1516
|
+
/**
|
|
1517
|
+
* Map LLM output messages (DoneEventMessage format) to StorageMessage format.
|
|
1518
|
+
*
|
|
1519
|
+
* Converts from snake_case API format to camelCase storage format.
|
|
1520
|
+
*/
|
|
1521
|
+
declare function mapOutputMessages(resultMessages: DoneEventMessage[]): StorageMessage[];
|
|
1522
|
+
|
|
1467
1523
|
/**
|
|
1468
1524
|
* Generate a message ID
|
|
1469
1525
|
*/
|
|
@@ -1477,4 +1533,4 @@ declare function generateThreadId(): string;
|
|
|
1477
1533
|
*/
|
|
1478
1534
|
declare function generateToolCallId(): string;
|
|
1479
1535
|
|
|
1480
|
-
export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };
|
|
1536
|
+
export { AIProvider, ActionDefinition, type ActionRequest, type AgentLoopOptions, type ChatRequest, type CollectedResult, type CopilotChatResponse, DEFAULT_MAX_ITERATIONS, DoneEventMessage, GenerateResult, type GenerateResultData, GenerateTextParams, GenerateTextResult, LLMAdapter, Message, ProviderToolRuntimeOptions, type RequestContext, Runtime, type RuntimeConfig, StorageAdapter, StorageMessage, StreamEvent, StreamResult, type StreamResultOptions, StreamTextParams, StreamTextResult, TokenUsageRaw, Tool, ToolCallInfo, ToolContext, ToolDefinition, ToolProfile, ToolResponse, type ToolSearchMatch, WebSearchConfig, buildProviderToolOptions, createEventStream, createExpressHandler, createExpressMiddleware, createHonoApp, createNextHandler, createNodeHandler, createRuntime, createSSEHeaders, createSSEResponse, createStreamResult, createTextStreamHeaders, createTextStreamResponse, extractInputMessages, formatSSEData, formatToolsForAnthropic, formatToolsForGoogle, formatToolsForOpenAI, generateMessageId, generateText, generateThreadId, generateToolCallId, mapOutputMessages, pipeSSEToResponse, pipeTextToResponse, runAgentLoop, searchTools, selectTools, shouldExposeToolSearch, streamText, tool };
|
package/dist/index.js
CHANGED
|
@@ -553,6 +553,54 @@ function createMessage(partial) {
|
|
|
553
553
|
};
|
|
554
554
|
}
|
|
555
555
|
|
|
556
|
+
// src/server/storage-helpers.ts
|
|
557
|
+
function extractInputMessages(reqMessages) {
|
|
558
|
+
if (!reqMessages?.length) return [];
|
|
559
|
+
let lastMeaningful = null;
|
|
560
|
+
for (let i = reqMessages.length - 1; i >= 0; i--) {
|
|
561
|
+
const m = reqMessages[i];
|
|
562
|
+
if (m.role === "assistant" && (!m.content || m.content === "")) continue;
|
|
563
|
+
lastMeaningful = m;
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
if (!lastMeaningful) return [];
|
|
567
|
+
if (lastMeaningful.role === "user") {
|
|
568
|
+
const textContent = typeof lastMeaningful.content === "string" ? lastMeaningful.content : JSON.stringify(lastMeaningful.content);
|
|
569
|
+
const attachments = lastMeaningful.attachments;
|
|
570
|
+
const imageAtt = attachments?.find((a) => a.type === "image" && a.url);
|
|
571
|
+
const fileAtt = attachments?.find((a) => a.type === "file" && a.url);
|
|
572
|
+
const msg = { role: "user", content: textContent || "" };
|
|
573
|
+
if (imageAtt?.url) {
|
|
574
|
+
msg.contentType = "image";
|
|
575
|
+
msg.url = imageAtt.url;
|
|
576
|
+
} else if (fileAtt?.url) {
|
|
577
|
+
msg.contentType = "file";
|
|
578
|
+
msg.url = fileAtt.url;
|
|
579
|
+
}
|
|
580
|
+
return [msg];
|
|
581
|
+
}
|
|
582
|
+
if (lastMeaningful.role === "tool" || lastMeaningful.role === "function") {
|
|
583
|
+
const msgs = reqMessages;
|
|
584
|
+
const lastAssistantIdx = msgs.map((m) => m.role).lastIndexOf("assistant");
|
|
585
|
+
return msgs.slice(lastAssistantIdx + 1).filter(
|
|
586
|
+
(m) => !(m.role === "assistant" && (!m.content || m.content === ""))
|
|
587
|
+
).map((m) => ({
|
|
588
|
+
role: m.role,
|
|
589
|
+
content: typeof m.content === "string" ? m.content : JSON.stringify(m.content),
|
|
590
|
+
toolCallId: m.tool_call_id
|
|
591
|
+
}));
|
|
592
|
+
}
|
|
593
|
+
return [];
|
|
594
|
+
}
|
|
595
|
+
function mapOutputMessages(resultMessages) {
|
|
596
|
+
return resultMessages.map((m) => ({
|
|
597
|
+
role: m.role,
|
|
598
|
+
content: m.content ?? "",
|
|
599
|
+
toolCalls: m.tool_calls,
|
|
600
|
+
toolCallId: m.tool_call_id
|
|
601
|
+
}));
|
|
602
|
+
}
|
|
603
|
+
|
|
556
604
|
// src/server/streaming.ts
|
|
557
605
|
function createSSEHeaders() {
|
|
558
606
|
return {
|
|
@@ -951,6 +999,7 @@ var StreamResult = class {
|
|
|
951
999
|
toolCalls: [],
|
|
952
1000
|
requiresAction: false,
|
|
953
1001
|
usage: void 0,
|
|
1002
|
+
threadId: void 0,
|
|
954
1003
|
events: []
|
|
955
1004
|
};
|
|
956
1005
|
}
|
|
@@ -996,6 +1045,9 @@ var StreamResult = class {
|
|
|
996
1045
|
this.capturedUsage = event.usage;
|
|
997
1046
|
collected.usage = event.usage;
|
|
998
1047
|
}
|
|
1048
|
+
if (event.threadId) {
|
|
1049
|
+
collected.threadId = event.threadId;
|
|
1050
|
+
}
|
|
999
1051
|
break;
|
|
1000
1052
|
}
|
|
1001
1053
|
}
|
|
@@ -1008,6 +1060,7 @@ var StreamResult = class {
|
|
|
1008
1060
|
const usage = this.capturedUsage;
|
|
1009
1061
|
await this.onFinishCallback({
|
|
1010
1062
|
messages: collected.messages,
|
|
1063
|
+
threadId: collected.threadId,
|
|
1011
1064
|
usage: usage ? {
|
|
1012
1065
|
promptTokens: usage.prompt_tokens,
|
|
1013
1066
|
completionTokens: usage.completion_tokens,
|
|
@@ -1495,6 +1548,7 @@ var Runtime = class {
|
|
|
1495
1548
|
this.actions = /* @__PURE__ */ new Map();
|
|
1496
1549
|
this.tools = /* @__PURE__ */ new Map();
|
|
1497
1550
|
this.config = config;
|
|
1551
|
+
this.storage = config.storage;
|
|
1498
1552
|
if ("provider" in config && config.provider) {
|
|
1499
1553
|
this.adapter = config.provider.languageModel(config.model);
|
|
1500
1554
|
} else if ("adapter" in config && config.adapter) {
|
|
@@ -2744,8 +2798,67 @@ var Runtime = class {
|
|
|
2744
2798
|
* ```
|
|
2745
2799
|
*/
|
|
2746
2800
|
stream(request, options) {
|
|
2747
|
-
const
|
|
2748
|
-
|
|
2801
|
+
const storage = this.storage;
|
|
2802
|
+
if (!storage) {
|
|
2803
|
+
const generator = this.processChatWithLoop(request, options?.signal);
|
|
2804
|
+
return new StreamResult(generator, { onFinish: options?.onFinish });
|
|
2805
|
+
}
|
|
2806
|
+
let resolvedThreadId = request.threadId;
|
|
2807
|
+
const self = this;
|
|
2808
|
+
let storageHealthy = true;
|
|
2809
|
+
async function* storageWrappedGenerator() {
|
|
2810
|
+
if (!resolvedThreadId) {
|
|
2811
|
+
try {
|
|
2812
|
+
const session = await storage.createSession();
|
|
2813
|
+
resolvedThreadId = session.id;
|
|
2814
|
+
} catch (err) {
|
|
2815
|
+
console.error(
|
|
2816
|
+
"[Runtime] storage.createSession failed \u2014 generating fallback threadId:",
|
|
2817
|
+
err
|
|
2818
|
+
);
|
|
2819
|
+
resolvedThreadId = `local_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
2820
|
+
storageHealthy = false;
|
|
2821
|
+
}
|
|
2822
|
+
}
|
|
2823
|
+
if (resolvedThreadId && storageHealthy) {
|
|
2824
|
+
try {
|
|
2825
|
+
const inputMsgs = extractInputMessages(request.messages);
|
|
2826
|
+
if (inputMsgs.length) {
|
|
2827
|
+
await storage.saveMessages(resolvedThreadId, inputMsgs);
|
|
2828
|
+
}
|
|
2829
|
+
} catch (err) {
|
|
2830
|
+
console.error("[Runtime] storage.saveMessages (input) failed:", err);
|
|
2831
|
+
}
|
|
2832
|
+
}
|
|
2833
|
+
for await (const event of self.processChatWithLoop(
|
|
2834
|
+
request,
|
|
2835
|
+
options?.signal
|
|
2836
|
+
)) {
|
|
2837
|
+
if (event.type === "done" && resolvedThreadId) {
|
|
2838
|
+
yield { ...event, threadId: resolvedThreadId };
|
|
2839
|
+
} else {
|
|
2840
|
+
yield event;
|
|
2841
|
+
}
|
|
2842
|
+
}
|
|
2843
|
+
}
|
|
2844
|
+
return new StreamResult(storageWrappedGenerator(), {
|
|
2845
|
+
onFinish: async (result) => {
|
|
2846
|
+
if (resolvedThreadId && storageHealthy && result.messages.length > 0) {
|
|
2847
|
+
try {
|
|
2848
|
+
const outputMsgs = mapOutputMessages(result.messages);
|
|
2849
|
+
await storage.saveMessages(resolvedThreadId, outputMsgs);
|
|
2850
|
+
} catch (err) {
|
|
2851
|
+
console.error(
|
|
2852
|
+
"[Runtime] storage.saveMessages (output) failed:",
|
|
2853
|
+
err
|
|
2854
|
+
);
|
|
2855
|
+
}
|
|
2856
|
+
}
|
|
2857
|
+
if (options?.onFinish) {
|
|
2858
|
+
await options.onFinish({ ...result, threadId: resolvedThreadId });
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
});
|
|
2749
2862
|
}
|
|
2750
2863
|
/**
|
|
2751
2864
|
* Chat and collect the full response (non-streaming)
|
|
@@ -3703,6 +3816,7 @@ exports.createSSEResponse = createSSEResponse;
|
|
|
3703
3816
|
exports.createStreamResult = createStreamResult;
|
|
3704
3817
|
exports.createTextStreamHeaders = createTextStreamHeaders;
|
|
3705
3818
|
exports.createTextStreamResponse = createTextStreamResponse;
|
|
3819
|
+
exports.extractInputMessages = extractInputMessages;
|
|
3706
3820
|
exports.formatSSEData = formatSSEData;
|
|
3707
3821
|
exports.formatToolsForAnthropic = formatToolsForAnthropic;
|
|
3708
3822
|
exports.formatToolsForGoogle = formatToolsForGoogle;
|
|
@@ -3711,6 +3825,7 @@ exports.generateMessageId = generateMessageId;
|
|
|
3711
3825
|
exports.generateText = generateText;
|
|
3712
3826
|
exports.generateThreadId = generateThreadId;
|
|
3713
3827
|
exports.generateToolCallId = generateToolCallId;
|
|
3828
|
+
exports.mapOutputMessages = mapOutputMessages;
|
|
3714
3829
|
exports.pipeSSEToResponse = pipeSSEToResponse;
|
|
3715
3830
|
exports.pipeTextToResponse = pipeTextToResponse;
|
|
3716
3831
|
exports.runAgentLoop = runAgentLoop;
|