@opencode/ai 2.2.5 → 2.2.7
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 +228 -72
- package/dist/ai-client.d.ts +2 -2
- package/dist/ai-client.js +2 -1
- package/dist/cache-policy.d.ts +1 -1
- package/dist/cache-policy.js +16 -4
- package/dist/decision-client.d.ts +3 -3
- package/dist/decision.d.ts +30 -125
- package/dist/decision.js +27 -15
- package/dist/effect.d.ts +10 -15
- package/dist/effect.js +6 -6
- package/dist/effort-updates.d.ts +3 -3
- package/dist/effort-updates.js +2 -2
- package/dist/embedding-client.d.ts +21 -0
- package/dist/embedding-client.js +30 -0
- package/dist/embedding.d.ts +111 -0
- package/dist/embedding.js +75 -0
- package/dist/experimental/cloudflare-run-extraction.d.ts +7 -3
- package/dist/experimental/cloudflare-run-extraction.js +4 -1
- package/dist/experimental/extraction.d.ts +10 -40
- package/dist/experimental/extraction.js +14 -13
- package/dist/experimental.d.ts +8 -8
- package/dist/experimental.js +4 -4
- package/dist/generation.d.ts +0 -2
- package/dist/generation.js +2 -2
- package/dist/image-client.d.ts +13 -7
- package/dist/image-client.js +1 -1
- package/dist/image.d.ts +357 -465
- package/dist/image.js +17 -18
- package/dist/index.d.ts +35 -467
- package/dist/index.js +23 -17
- package/dist/llm.d.ts +65 -266
- package/dist/llm.js +101 -92
- package/dist/loop.d.ts +38 -0
- package/dist/loop.js +136 -0
- package/dist/media-model.d.ts +1 -1
- package/dist/media-model.js +8 -6
- package/dist/media.d.ts +9 -0
- package/dist/media.js +37 -2
- package/dist/protocols/alibaba-chat.d.ts +9 -1
- package/dist/protocols/alibaba-chat.js +3 -3
- package/dist/protocols/alibaba-images.d.ts +2 -2
- package/dist/protocols/alibaba-messages.js +3 -3
- package/dist/protocols/alibaba-responses.js +8 -20
- package/dist/protocols/anthropic-messages.d.ts +1 -1
- package/dist/protocols/anthropic-messages.js +57 -176
- package/dist/protocols/assemblyai-transcription.d.ts +2 -2
- package/dist/protocols/bedrock-converse.js +10 -16
- package/dist/protocols/cartesia-speech.d.ts +2 -2
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +2 -2
- package/dist/protocols/cohere-chat.js +9 -12
- package/dist/protocols/deepgram-speech.d.ts +2 -2
- package/dist/protocols/deepgram-transcription.d.ts +2 -2
- package/dist/protocols/elevenlabs-speech.d.ts +2 -2
- package/dist/protocols/elevenlabs-transcription.d.ts +2 -2
- package/dist/protocols/gemini.d.ts +3 -0
- package/dist/protocols/gemini.js +54 -72
- package/dist/protocols/google-embed-content.d.ts +34 -0
- package/dist/protocols/google-embed-content.js +110 -0
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-interactions.d.ts +3 -4
- package/dist/protocols/google-interactions.js +44 -30
- package/dist/protocols/google-speech.d.ts +2 -2
- package/dist/protocols/google-transcription.d.ts +2 -2
- package/dist/protocols/google-vertex-predict-embeddings.d.ts +18 -0
- package/dist/protocols/google-vertex-predict-embeddings.js +74 -0
- package/dist/protocols/google-video.d.ts +2 -2
- package/dist/protocols/index.d.ts +3 -0
- package/dist/protocols/index.js +3 -0
- package/dist/protocols/meta-images.d.ts +2 -2
- package/dist/protocols/meta-responses.js +26 -23
- package/dist/protocols/minimax-images.d.ts +2 -2
- package/dist/protocols/mistral-chat.js +7 -10
- package/dist/protocols/open-responses-channel.js +6 -19
- package/dist/protocols/open-responses-continuation.d.ts +1 -5
- package/dist/protocols/open-responses-continuation.js +1 -3
- package/dist/protocols/open-responses.d.ts +2 -22
- package/dist/protocols/open-responses.js +41 -23
- package/dist/protocols/openai-chat.d.ts +27 -23
- package/dist/protocols/openai-chat.js +62 -57
- package/dist/protocols/openai-compatible-chat.d.ts +2 -1
- package/dist/protocols/openai-compatible-responses.d.ts +2 -2
- package/dist/protocols/openai-compatible-responses.js +1 -1
- package/dist/protocols/openai-decisions.js +15 -2
- package/dist/protocols/openai-embeddings.d.ts +16 -0
- package/dist/protocols/openai-embeddings.js +76 -0
- package/dist/protocols/openai-images.d.ts +2 -2
- package/dist/protocols/openai-responses.js +10 -29
- package/dist/protocols/openai-speech.d.ts +2 -2
- package/dist/protocols/openai-transcription.d.ts +2 -2
- package/dist/protocols/runway-video.d.ts +2 -2
- package/dist/protocols/shared.d.ts +14 -7
- package/dist/protocols/shared.js +40 -15
- package/dist/protocols/system-one.d.ts +4 -4
- package/dist/protocols/system-one.js +8 -1
- package/dist/protocols/utils/bedrock-cache.js +2 -5
- package/dist/protocols/utils/bedrock-media.d.ts +0 -3
- package/dist/protocols/utils/cache.d.ts +3 -0
- package/dist/protocols/utils/cache.js +15 -2
- package/dist/protocols/utils/image-sizing.d.ts +1 -1
- package/dist/protocols/utils/responses-checkpoint.js +1 -1
- package/dist/protocols/utils/responses-compaction.d.ts +1 -1
- package/dist/protocols/utils/responses-compaction.js +6 -5
- package/dist/protocols/utils/responses-hosted-tools.d.ts +97 -1
- package/dist/protocols/utils/responses-hosted-tools.js +31 -0
- package/dist/protocols/utils/tool-stream.d.ts +5 -271
- package/dist/protocols/utils/tool-stream.js +3 -13
- package/dist/protocols/venice-chat.d.ts +7 -1
- package/dist/protocols/venice-chat.js +4 -4
- package/dist/protocols/xai-images.d.ts +2 -2
- package/dist/protocols/xai-responses.js +5 -25
- package/dist/protocols/xai-video.d.ts +2 -2
- package/dist/protocols/zai-chat.d.ts +9 -1
- package/dist/protocols/zai-chat.js +1 -1
- package/dist/protocols/zai-images.d.ts +2 -2
- package/dist/protocols/zai-messages.js +3 -3
- package/dist/provider-error.d.ts +0 -1
- package/dist/provider-error.js +7 -80
- package/dist/providers/alibaba/embeddings.d.ts +2 -0
- package/dist/providers/alibaba/embeddings.js +1 -0
- package/dist/providers/alibaba.d.ts +70 -4
- package/dist/providers/alibaba.js +20 -2
- package/dist/providers/amazon-bedrock-mantle.d.ts +7 -3
- package/dist/providers/amazon-bedrock-mantle.js +6 -3
- package/dist/providers/amazon-bedrock.d.ts +6 -7
- package/dist/providers/amazon-bedrock.js +1 -1
- package/dist/providers/anthropic-compatible.d.ts +7 -8
- package/dist/providers/anthropic-compatible.js +2 -5
- package/dist/providers/anthropic.d.ts +8 -9
- package/dist/providers/anthropic.js +2 -7
- package/dist/providers/assemblyai.d.ts +2 -2
- package/dist/providers/assemblyai.js +2 -7
- package/dist/providers/azure/embeddings.d.ts +2 -0
- package/dist/providers/azure/embeddings.js +1 -0
- package/dist/providers/azure/messages.d.ts +2 -0
- package/dist/providers/azure/messages.js +1 -0
- package/dist/providers/azure.d.ts +325 -7
- package/dist/providers/azure.js +40 -9
- package/dist/providers/baseten.d.ts +7 -5
- package/dist/providers/baseten.js +7 -21
- package/dist/providers/cartesia.d.ts +1 -1
- package/dist/providers/cerebras.d.ts +7 -5
- package/dist/providers/cerebras.js +4 -15
- package/dist/providers/cloudflare-ai-gateway.d.ts +23 -13
- package/dist/providers/cloudflare-ai-gateway.js +28 -40
- package/dist/providers/cloudflare-shared.d.ts +9 -0
- package/dist/providers/cloudflare-shared.js +25 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +6 -4
- package/dist/providers/cloudflare-workers-ai.js +4 -20
- package/dist/providers/cohere/chat.js +2 -7
- package/dist/providers/cohere/embeddings.d.ts +2 -0
- package/dist/providers/cohere/embeddings.js +1 -0
- package/dist/providers/cohere.d.ts +14 -5
- package/dist/providers/cohere.js +23 -13
- package/dist/providers/deepgram.d.ts +2 -2
- package/dist/providers/deepgram.js +2 -7
- package/dist/providers/deepinfra/embeddings.d.ts +2 -0
- package/dist/providers/deepinfra/embeddings.js +1 -0
- package/dist/providers/deepinfra.d.ts +10 -5
- package/dist/providers/deepinfra.js +20 -14
- package/dist/providers/deepseek.d.ts +7 -5
- package/dist/providers/deepseek.js +3 -14
- package/dist/providers/digitalocean/embeddings.d.ts +2 -0
- package/dist/providers/digitalocean/embeddings.js +1 -0
- package/dist/providers/digitalocean.d.ts +18 -5
- package/dist/providers/digitalocean.js +28 -14
- package/dist/providers/elevenlabs.d.ts +2 -2
- package/dist/providers/elevenlabs.js +2 -7
- package/dist/providers/fireworks/embeddings.d.ts +2 -0
- package/dist/providers/fireworks/embeddings.js +1 -0
- package/dist/providers/fireworks.d.ts +10 -5
- package/dist/providers/fireworks.js +17 -14
- package/dist/providers/google/embeddings.d.ts +2 -0
- package/dist/providers/google/embeddings.js +1 -0
- package/dist/providers/google/interactions.d.ts +1 -1
- package/dist/providers/google/interactions.js +2 -7
- package/dist/providers/google-vertex/embeddings.d.ts +2 -0
- package/dist/providers/google-vertex/embeddings.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +4 -3
- package/dist/providers/google-vertex-chat.js +3 -29
- package/dist/providers/google-vertex-interactions.d.ts +3 -3
- package/dist/providers/google-vertex-messages.d.ts +3 -3
- package/dist/providers/google-vertex-messages.js +3 -29
- package/dist/providers/google-vertex-mistral.d.ts +3 -3
- package/dist/providers/google-vertex-mistral.js +3 -29
- package/dist/providers/google-vertex-responses.d.ts +3 -3
- package/dist/providers/google-vertex-responses.js +4 -30
- package/dist/providers/google-vertex-shared.d.ts +31 -0
- package/dist/providers/google-vertex-shared.js +25 -0
- package/dist/providers/google-vertex.d.ts +10 -3
- package/dist/providers/google-vertex.js +52 -0
- package/dist/providers/google.d.ts +21 -13
- package/dist/providers/google.js +23 -19
- package/dist/providers/groq.d.ts +10 -5
- package/dist/providers/groq.js +4 -15
- package/dist/providers/meta.d.ts +6 -5
- package/dist/providers/meta.js +10 -23
- package/dist/providers/minimax/chat.js +2 -7
- package/dist/providers/minimax/responses.js +2 -7
- package/dist/providers/minimax.d.ts +6 -5
- package/dist/providers/minimax.js +7 -22
- package/dist/providers/mistral.d.ts +10 -11
- package/dist/providers/mistral.js +3 -14
- package/dist/providers/moonshot/messages.js +2 -7
- package/dist/providers/moonshot/responses.js +2 -7
- package/dist/providers/moonshot.d.ts +6 -5
- package/dist/providers/moonshot.js +4 -16
- package/dist/providers/openai/embeddings.d.ts +2 -0
- package/dist/providers/openai/embeddings.js +1 -0
- package/dist/providers/openai-compatible/embeddings.d.ts +2 -0
- package/dist/providers/openai-compatible/embeddings.js +1 -0
- package/dist/providers/openai-compatible-responses.d.ts +6 -7
- package/dist/providers/openai-compatible-responses.js +2 -8
- package/dist/providers/openai-compatible.d.ts +10 -7
- package/dist/providers/openai-compatible.js +17 -10
- package/dist/providers/openai.d.ts +11 -4
- package/dist/providers/openai.js +17 -3
- package/dist/providers/opencode.d.ts +4 -4
- package/dist/providers/opencode.js +6 -2
- package/dist/providers/openrouter/embeddings.d.ts +2 -0
- package/dist/providers/openrouter/embeddings.js +1 -0
- package/dist/providers/openrouter.d.ts +24 -5
- package/dist/providers/openrouter.js +31 -24
- package/dist/providers/runway.d.ts +1 -1
- package/dist/providers/settings.d.ts +40 -0
- package/dist/providers/settings.js +19 -0
- package/dist/providers/togetherai.d.ts +7 -5
- package/dist/providers/togetherai.js +5 -15
- package/dist/providers/typesafe-ai.d.ts +4 -4
- package/dist/providers/typesafe-ai.js +6 -2
- package/dist/providers/venice.d.ts +5 -4
- package/dist/providers/venice.js +6 -2
- package/dist/providers/vercel-ai-gateway/embeddings.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/embeddings.js +1 -0
- package/dist/providers/vercel-ai-gateway.d.ts +9 -5
- package/dist/providers/vercel-ai-gateway.js +30 -29
- package/dist/providers/xai.d.ts +5 -4
- package/dist/providers/xai.js +4 -12
- package/dist/providers/zai-coding-plan/messages.js +2 -7
- package/dist/providers/zai-coding-plan/responses.js +2 -7
- package/dist/providers/zai-coding-plan.d.ts +6 -5
- package/dist/providers/zai-coding-plan.js +8 -15
- package/dist/providers/zai.d.ts +6 -5
- package/dist/providers/zai.js +10 -22
- package/dist/public.d.ts +14 -0
- package/dist/public.js +6 -0
- package/dist/route/auth-options.d.ts +9 -5
- package/dist/route/auth-options.js +10 -7
- package/dist/route/client.d.ts +50 -37
- package/dist/route/client.js +42 -53
- package/dist/route/decision.d.ts +8 -8
- package/dist/route/decision.js +3 -3
- package/dist/route/embedding.d.ts +61 -0
- package/dist/route/embedding.js +88 -0
- package/dist/route/executor.d.ts +5 -0
- package/dist/route/executor.js +10 -0
- package/dist/route/index.d.ts +5 -7
- package/dist/route/index.js +1 -1
- package/dist/route/media.d.ts +7 -11
- package/dist/route/media.js +22 -26
- package/dist/route/transport/http.js +6 -5
- package/dist/route/transport/websocket.js +7 -1
- package/dist/schema/errors.d.ts +16 -17
- package/dist/schema/errors.js +63 -12
- package/dist/schema/events.d.ts +2563 -2954
- package/dist/schema/events.js +54 -109
- package/dist/schema/messages.d.ts +194 -137
- package/dist/schema/messages.js +66 -83
- package/dist/schema/options.d.ts +112 -65
- package/dist/schema/options.js +52 -101
- package/dist/speech-client.d.ts +7 -7
- package/dist/speech.d.ts +306 -316
- package/dist/speech.js +16 -14
- package/dist/testing.d.ts +31 -175
- package/dist/testing.js +60 -5
- package/dist/tool-history.d.ts +1 -1
- package/dist/tool-history.js +3 -10
- package/dist/tool-runtime.d.ts +9 -2
- package/dist/tool-runtime.js +17 -31
- package/dist/tool.d.ts +39 -43
- package/dist/tool.js +16 -26
- package/dist/transcription-client.d.ts +7 -7
- package/dist/transcription.d.ts +314 -265
- package/dist/transcription.js +16 -14
- package/dist/utils/provider-codes.d.ts +3 -0
- package/dist/utils/provider-codes.js +41 -0
- package/dist/video-client.d.ts +7 -7
- package/dist/video-client.js +1 -1
- package/dist/video.d.ts +269 -287
- package/dist/video.js +16 -14
- package/package.json +309 -257
- package/dist/experimental/testing.d.ts +0 -3
- package/dist/experimental/testing.js +0 -3
- package/dist/testing/decision.d.ts +0 -16
- package/dist/testing/decision.js +0 -20
- package/dist/testing/image.d.ts +0 -14
- package/dist/testing/image.js +0 -12
- package/dist/testing/media.d.ts +0 -18
- package/dist/testing/media.js +0 -36
- package/dist/testing/script.d.ts +0 -28
- package/dist/testing/script.js +0 -60
- package/dist/testing/speech.d.ts +0 -14
- package/dist/testing/speech.js +0 -12
- package/dist/testing/transcription.d.ts +0 -14
- package/dist/testing/transcription.js +0 -12
- package/dist/testing/video.d.ts +0 -14
- package/dist/testing/video.js +0 -12
package/dist/providers/zai.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type { ProviderPackage } from "../provider-package.js";
|
|
2
2
|
import { ZAIChat } from "../protocols/zai-chat.js";
|
|
3
|
-
import {
|
|
4
|
-
import { Route
|
|
5
|
-
import { type ModelID, type ProviderOptions } from "../schema/index.js";
|
|
3
|
+
import type { ProviderAuthOption } from "../route/auth-options.js";
|
|
4
|
+
import { Route } from "../route/client.js";
|
|
5
|
+
import { type ModelID, type ProviderOptions, type RequestOptions } from "../schema/index.js";
|
|
6
6
|
export declare const id: string & import("effect/Brand").Brand<"AI.ProviderID">;
|
|
7
7
|
export type ChatOptionsInput = ZAIChat.OptionsInput;
|
|
8
|
-
export type Config = Omit<
|
|
8
|
+
export type Config = Omit<RequestOptions, "providerOptions"> & ProviderAuthOption<"optional"> & {
|
|
9
9
|
readonly baseURL?: string;
|
|
10
10
|
readonly providerOptions?: ChatOptionsInput;
|
|
11
11
|
};
|
|
12
12
|
export type Settings = ProviderPackage.Settings & ChatOptionsInput & {
|
|
13
|
-
readonly apiKey?: string;
|
|
13
|
+
readonly apiKey?: string | false;
|
|
14
14
|
readonly baseURL?: string;
|
|
15
15
|
};
|
|
16
16
|
export type { ZAIImageOptions } from "../protocols/zai-images.js";
|
|
@@ -114,6 +114,7 @@ export declare const routes: Route<{
|
|
|
114
114
|
readonly reasoning?: string | undefined;
|
|
115
115
|
readonly reasoning_text?: string | undefined;
|
|
116
116
|
readonly reasoning_details?: unknown;
|
|
117
|
+
readonly reasoning_opaque?: string | undefined;
|
|
117
118
|
})[];
|
|
118
119
|
readonly tools?: readonly ({
|
|
119
120
|
readonly type: "function";
|
package/dist/providers/zai.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { ZAIChat } from "../protocols/zai-chat.js";
|
|
2
2
|
import { ZAIImages } from "../protocols/zai-images.js";
|
|
3
3
|
import { OpenAIChat } from "../protocols/openai-chat.js";
|
|
4
|
-
import { AuthOptions } from "../route/auth-options.js";
|
|
5
4
|
import { Route } from "../route/client.js";
|
|
6
5
|
import { Endpoint } from "../route/endpoint.js";
|
|
7
6
|
import { MediaRoute } from "../route/media.js";
|
|
8
7
|
import { ProviderID } from "../schema/index.js";
|
|
8
|
+
import { bearerRouteDefaults, toConfig } from "./settings.js";
|
|
9
9
|
export const id = ProviderID.make("zai");
|
|
10
|
-
const auth = (options) => AuthOptions.bearer(options, "ZAI_API_KEY");
|
|
11
10
|
const chatRoute = Route.make({
|
|
12
11
|
id: "zai-chat",
|
|
13
12
|
provider: id,
|
|
@@ -18,15 +17,13 @@ const chatRoute = Route.make({
|
|
|
18
17
|
});
|
|
19
18
|
export const routes = [chatRoute];
|
|
20
19
|
export const configure = (input = {}) => {
|
|
21
|
-
const
|
|
22
|
-
const chat = (modelID) => chatRoute
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
.model({ id: modelID, compatibility: ZAIChat.compatibility });
|
|
29
|
-
const media = MediaRoute.deployment(input, auth(input));
|
|
20
|
+
const defaults = bearerRouteDefaults(input, "ZAI_API_KEY");
|
|
21
|
+
const chat = (modelID) => chatRoute.with(defaults).model({
|
|
22
|
+
id: modelID,
|
|
23
|
+
compatibility: ZAIChat.compatibility,
|
|
24
|
+
capabilities: ZAIChat.capabilities,
|
|
25
|
+
});
|
|
26
|
+
const media = MediaRoute.deployment(input, defaults.auth);
|
|
30
27
|
const image = (modelID, providerOptions) => ZAIImages.model({ ...media, id: modelID, providerOptions });
|
|
31
28
|
return {
|
|
32
29
|
id,
|
|
@@ -39,18 +36,9 @@ export const configure = (input = {}) => {
|
|
|
39
36
|
export const provider = configure();
|
|
40
37
|
export const image = provider.image;
|
|
41
38
|
export const chat = provider.chat;
|
|
42
|
-
export const model = (modelID, settings) => configure(
|
|
39
|
+
export const model = (modelID, settings) => configure(toConfig(settings)).model(modelID);
|
|
43
40
|
export const imageModel = (modelID, settings) => {
|
|
44
|
-
const input =
|
|
41
|
+
const input = toConfig(settings);
|
|
45
42
|
return configure(input).image(modelID, input.providerOptions);
|
|
46
43
|
};
|
|
47
|
-
function config({ apiKey, baseURL, body, headers, ...providerOptions } = {}) {
|
|
48
|
-
return {
|
|
49
|
-
apiKey,
|
|
50
|
-
baseURL,
|
|
51
|
-
headers,
|
|
52
|
-
http: body === undefined ? undefined : { body },
|
|
53
|
-
providerOptions,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
44
|
export * as ZAI from "./zai.js";
|
package/dist/public.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
|
|
2
|
+
export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, GENERATION_KEYS, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, SystemMessage, UserMessage, AssistantMessage, ToolMessage, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, FinishReasonDetails, StepFinish, Finish, MediaEvent, RawEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
|
|
3
|
+
export type { BaseRequest, ContentPartInput, GenerationOptionsFields, HttpOptions, RequestOptions, LanguageModelInput, LanguageModelProviderOptions, LLMRequest, MediaPartInput, RequestTimeout, TimeoutConfig, ToolDefinitionInput, ToolNamespaceInput, ToolNamespaceEntryInput, ToolEntryInput, TransportOptions, UsageInput, } from "./schema/index.js";
|
|
4
|
+
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageResponse } from "./image.js";
|
|
5
|
+
export type { ImageModelOptions, ImageOptions, ImageRequest, ImageRequestInput, ImageResolution, ImageRoute, } from "./image.js";
|
|
6
|
+
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
|
|
7
|
+
export type { AnswerFor, AnswersFor, DecisionInput, DecisionMediaPartInput, DecisionModelOptions, DecisionOptions, DecisionPartInput, DecisionQuestions, DecisionRequest, DecisionRequestInput, DecisionResponseFor, DecisionModelRoute, } from "./decision.js";
|
|
8
|
+
export { EmbeddingModel, EmbeddingModelSchema, EmbeddingResponse, EmbeddingUsage, EmbeddingValue } from "./embedding.js";
|
|
9
|
+
export type { EmbeddingInputType, EmbeddingModelOptions, EmbeddingModelRoute, EmbeddingOptions, EmbeddingRequest, EmbeddingRequestInput, EmbeddingValueInput, } from "./embedding.js";
|
|
10
|
+
export { Media } from "./media.js";
|
|
11
|
+
export type { AwaitOptions as GenerationAwaitOptions, Event as GenerationEvent, Poll, Route as GenerationRoute, Snapshot as GenerationSnapshot, Status as GenerationStatus, } from "./generation.js";
|
|
12
|
+
export type { ExecutableTool, Tool, ToolExecute, ToolExecuteContext, ToolInput, ToolInputType, ToolModelOutputInput, ToolOptions, ToolSchema, ToolToModelOutput, } from "./tool.js";
|
|
13
|
+
export type { LLM } from "./llm.js";
|
|
14
|
+
export type { LLMLoopInput, LLMRequestInput } from "./llm.js";
|
package/dist/public.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { isContextOverflow, isContextOverflowFailure, isRetryable } from "./provider-error.js";
|
|
2
|
+
export { ProtocolID, RouteID, ModelID, ProviderID, ResponseID, ContentBlockID, ToolCallID, JsonSchema, ProviderOptions, ProviderMetadata, HttpTimeout, RequestEncoding, GENERATION_KEYS, ReasoningEfforts, ReasoningEffort, LanguageModelCompatibility, LanguageModel, LanguageModelSchema, CacheTTL, CacheHint, CachePolicyObject, CachePolicy, MessageRole, SystemPart, TextPart, MediaPart, ToolResultValue, ToolOutput, ToolCallPart, ToolResultPart, ReasoningPart, CompactionPart, EffortPart, ContentPart, Message, SystemMessage, UserMessage, AssistantMessage, ToolMessage, Freeform, ToolDefinition, ToolNamespace, ToolEntry, ToolChoice, FinishReason, Usage, MediaUsage, CompactionResponse, CompactionCheckpointResponse, StepStart, TextStart, TextDelta, TextEnd, ReasoningStart, ReasoningDelta, ReasoningEnd, ToolInputStart, ToolInputDelta, ToolInputEnd, ToolInputError, ToolCall, ToolResult, FinishReasonDetails, StepFinish, Finish, MediaEvent, RawEvent, LLMEvent, LLMResponse, HttpContext, HttpRateLimitDetails, InvalidRequestError, UnsupportedOperationError, ProviderConfigurationError, AuthenticationError, RateLimitError, QuotaExceededError, ContentPolicyError, ProviderInternalError, TransportType, TransportOperation, TransportError, InvalidProviderOutputError, UnknownProviderError, TimeoutError, AIErrorReason, AIError, } from "./schema/index.js";
|
|
3
|
+
export { ImageAspectRatio, ImageEvent, ImageModel, ImageModelSchema, ImageResponse } from "./image.js";
|
|
4
|
+
export { ChoiceAnswer, ChoiceQuestion, ChoiceValue, DecisionAnswer, DecisionJsonPart, DecisionMediaPart, DecisionModel, DecisionModelCompatibility, DecisionModelSchema, DecisionPart, DecisionQuestion, DecisionResponse, DecisionTextPart, DecisionUsage, ErrorAnswer, ErrorReason, PredicateAnswer, PredicateQuestion, ScoreAnswer, ScoreQuestion, } from "./decision.js";
|
|
5
|
+
export { EmbeddingModel, EmbeddingModelSchema, EmbeddingResponse, EmbeddingUsage, EmbeddingValue } from "./embedding.js";
|
|
6
|
+
export { Media } from "./media.js";
|
|
@@ -5,12 +5,14 @@ export type AuthOverride = {
|
|
|
5
5
|
readonly auth: Auth.Definition;
|
|
6
6
|
readonly apiKey?: never;
|
|
7
7
|
};
|
|
8
|
+
/** `false` marks a provider that needs no key: no env lookup and no auth header from the package. */
|
|
9
|
+
export type ApiKey = string | Redacted.Redacted<string> | Config.Config<string | Redacted.Redacted<string>> | false;
|
|
8
10
|
export type OptionalApiKeyAuth = {
|
|
9
|
-
readonly apiKey?:
|
|
11
|
+
readonly apiKey?: ApiKey;
|
|
10
12
|
readonly auth?: never;
|
|
11
13
|
};
|
|
12
14
|
export type RequiredApiKeyAuth = {
|
|
13
|
-
readonly apiKey:
|
|
15
|
+
readonly apiKey: ApiKey;
|
|
14
16
|
readonly auth?: never;
|
|
15
17
|
};
|
|
16
18
|
export type ProviderAuthOption<Mode extends ApiKeyMode> = AuthOverride | (Mode extends "optional" ? OptionalApiKeyAuth : RequiredApiKeyAuth);
|
|
@@ -23,9 +25,11 @@ export type AtLeastOne<T> = {
|
|
|
23
25
|
[K in keyof T]: Required<Pick<T, K>> & Partial<Omit<T, K>>;
|
|
24
26
|
}[keyof T];
|
|
25
27
|
/**
|
|
26
|
-
* Standard
|
|
27
|
-
* override,
|
|
28
|
-
*
|
|
28
|
+
* Standard API-key resolution for providers: honor an explicit `auth`
|
|
29
|
+
* override, skip auth entirely for `apiKey: false`, otherwise resolve `apiKey`
|
|
30
|
+
* (option > config vars) and render it with `apply`.
|
|
29
31
|
*/
|
|
32
|
+
export declare const apiKey: (options: ProviderAuthOption<"optional">, envVar: string | ReadonlyArray<string>, apply: (credential: Auth.Credential) => Auth.Definition) => Auth.Definition;
|
|
33
|
+
/** `apiKey` resolution applied as `Authorization: Bearer <key>`. */
|
|
30
34
|
export declare const bearer: (options: ProviderAuthOption<"optional">, envVar: string | ReadonlyArray<string>) => Auth.Definition;
|
|
31
35
|
export * as AuthOptions from "./auth-options.js";
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { Auth } from "./auth.js";
|
|
2
2
|
/**
|
|
3
|
-
* Standard
|
|
4
|
-
* override,
|
|
5
|
-
*
|
|
3
|
+
* Standard API-key resolution for providers: honor an explicit `auth`
|
|
4
|
+
* override, skip auth entirely for `apiKey: false`, otherwise resolve `apiKey`
|
|
5
|
+
* (option > config vars) and render it with `apply`.
|
|
6
6
|
*/
|
|
7
|
-
export const
|
|
7
|
+
export const apiKey = (options, envVar, apply) => {
|
|
8
8
|
if ("auth" in options && options.auth)
|
|
9
9
|
return options.auth;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
.
|
|
10
|
+
const key = "apiKey" in options ? options.apiKey : undefined;
|
|
11
|
+
if (key === false)
|
|
12
|
+
return Auth.none;
|
|
13
|
+
return apply((Array.isArray(envVar) ? envVar : [envVar]).reduce((auth, name) => auth.orElse(Auth.config(name)), Auth.optional(key, "apiKey")));
|
|
13
14
|
};
|
|
15
|
+
/** `apiKey` resolution applied as `Authorization: Bearer <key>`. */
|
|
16
|
+
export const bearer = (options, envVar) => apiKey(options, envVar, (credential) => credential.bearer());
|
|
14
17
|
export * as AuthOptions from "./auth-options.js";
|
package/dist/route/client.d.ts
CHANGED
|
@@ -7,7 +7,8 @@ import { HttpTransport } from "./transport/index.js";
|
|
|
7
7
|
import type { HttpMiddleware, Transport, TransportRuntime, WebSocketChannelExecutor } from "./transport/index.js";
|
|
8
8
|
import type { Protocol } from "./protocol.js";
|
|
9
9
|
import type { LanguageModelSanitizerCompatibility, ProtocolID, ProviderOptions } from "../schema/index.js";
|
|
10
|
-
import {
|
|
10
|
+
import type { LLMRequestInput } from "../llm.js";
|
|
11
|
+
import { AIError, CompactionResponse, CompactionCheckpointResponse, LLMResponse, LanguageModel, LLMEvent, ProviderID, type LLMRequest, type RequestOptions } from "../schema/index.js";
|
|
11
12
|
export interface RouteBody<Body> {
|
|
12
13
|
/** Schema for the validated provider-native body sent as the JSON request. */
|
|
13
14
|
readonly schema: Schema.Codec<Body, unknown>;
|
|
@@ -28,7 +29,8 @@ export interface Route<Body, Prepared = unknown, Compact extends CompactionOpera
|
|
|
28
29
|
readonly request: LLMRequest;
|
|
29
30
|
}) => Record<string, string>;
|
|
30
31
|
readonly transport: Transport<Body, Prepared, unknown>;
|
|
31
|
-
|
|
32
|
+
/** Options every model built on this route starts from. */
|
|
33
|
+
readonly options?: RequestOptions;
|
|
32
34
|
readonly body: RouteBody<Body>;
|
|
33
35
|
readonly supportsEffortUpdates?: (request: LLMRequest) => boolean;
|
|
34
36
|
readonly sanitizer?: LanguageModelSanitizerCompatibility;
|
|
@@ -48,19 +50,7 @@ export interface Route<Body, Prepared = unknown, Compact extends CompactionOpera
|
|
|
48
50
|
export type AnyRoute<Compact extends CompactionOperations | undefined = CompactionOperations | undefined> = Route<any, any, Compact>;
|
|
49
51
|
export type RouteLanguageModelInput = Omit<LanguageModel.Input, "provider" | "route">;
|
|
50
52
|
export type RouteRoutedLanguageModelInput = Omit<LanguageModel.Input, "route">;
|
|
51
|
-
export interface
|
|
52
|
-
readonly headers?: Record<string, string>;
|
|
53
|
-
readonly generation?: GenerationOptions;
|
|
54
|
-
readonly providerOptions?: ProviderOptions;
|
|
55
|
-
readonly http?: HttpOptions;
|
|
56
|
-
}
|
|
57
|
-
export interface RouteDefaultsInput {
|
|
58
|
-
readonly headers?: Record<string, string>;
|
|
59
|
-
readonly generation?: GenerationOptions.Input;
|
|
60
|
-
readonly providerOptions?: ProviderOptions;
|
|
61
|
-
readonly http?: HttpOptions.Input;
|
|
62
|
-
}
|
|
63
|
-
export interface RoutePatch<Body, Prepared> extends RouteDefaultsInput {
|
|
53
|
+
export interface RoutePatch<Body, Prepared> extends RequestOptions {
|
|
64
54
|
readonly compact?: CompactionOperations;
|
|
65
55
|
readonly id?: string;
|
|
66
56
|
readonly provider?: string | ProviderID;
|
|
@@ -99,26 +89,49 @@ export interface EndpointCompactOptions extends Pick<StreamOptions, "http"> {
|
|
|
99
89
|
export interface TriggerCompactOptions extends StreamOptions {
|
|
100
90
|
readonly mechanism: "trigger";
|
|
101
91
|
}
|
|
102
|
-
|
|
103
|
-
readonly
|
|
104
|
-
|
|
105
|
-
|
|
92
|
+
type EndpointModel = LanguageModel<ProviderOptions, {
|
|
93
|
+
readonly endpoint: CompactOperation;
|
|
94
|
+
}>;
|
|
95
|
+
type TriggerModel = LanguageModel<ProviderOptions, {
|
|
96
|
+
readonly trigger: TriggerCompactOperation;
|
|
97
|
+
}>;
|
|
98
|
+
/** A text request whose model supports explicit compaction. */
|
|
99
|
+
export type CompactionRequest = LLMRequestInput & {
|
|
100
|
+
readonly model: EndpointModel;
|
|
106
101
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}>;
|
|
102
|
+
/** A text request whose model supports trigger compaction checkpoints. */
|
|
103
|
+
export type CheckpointRequest = LLMRequestInput & {
|
|
104
|
+
readonly model: TriggerModel;
|
|
111
105
|
};
|
|
112
106
|
export interface CompactMethod<R = never> {
|
|
113
|
-
(request:
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}):
|
|
119
|
-
|
|
107
|
+
(request: LLMRequest & {
|
|
108
|
+
readonly model: TriggerModel;
|
|
109
|
+
}, options: TriggerCompactOptions): Effect.Effect<CompactionCheckpointResponse, AIError, R>;
|
|
110
|
+
(request: LLMRequest & {
|
|
111
|
+
readonly model: EndpointModel;
|
|
112
|
+
}, options?: EndpointCompactOptions): Effect.Effect<CompactionResponse, AIError, R>;
|
|
113
|
+
}
|
|
114
|
+
/** Whether a model, or a text request's model, supports compaction through the given mechanism. */
|
|
115
|
+
export declare function canCompact<Model extends LanguageModel>(model: Model, options: {
|
|
116
|
+
readonly mechanism: "trigger";
|
|
117
|
+
}): model is Model & TriggerModel;
|
|
118
|
+
export declare function canCompact<Request extends {
|
|
119
|
+
readonly model: LanguageModel;
|
|
120
|
+
}>(request: Request, options: {
|
|
120
121
|
readonly mechanism: "trigger";
|
|
121
|
-
}): request is
|
|
122
|
+
}): request is Request & {
|
|
123
|
+
readonly model: TriggerModel;
|
|
124
|
+
};
|
|
125
|
+
export declare function canCompact<Model extends LanguageModel>(model: Model, options?: {
|
|
126
|
+
readonly mechanism?: "endpoint";
|
|
127
|
+
}): model is Model & EndpointModel;
|
|
128
|
+
export declare function canCompact<Request extends {
|
|
129
|
+
readonly model: LanguageModel;
|
|
130
|
+
}>(request: Request, options?: {
|
|
131
|
+
readonly mechanism?: "endpoint";
|
|
132
|
+
}): request is Request & {
|
|
133
|
+
readonly model: EndpointModel;
|
|
134
|
+
};
|
|
122
135
|
declare const LLMClientService_base: Context.ServiceClass<LLMClientService, "@opencode/LLMClient", Interface>;
|
|
123
136
|
export declare class LLMClientService extends LLMClientService_base {
|
|
124
137
|
}
|
|
@@ -144,8 +157,8 @@ export interface MakeInput<Body, Frame, Event, State> {
|
|
|
144
157
|
readonly headers?: (input: {
|
|
145
158
|
readonly request: LLMRequest;
|
|
146
159
|
}) => Record<string, string>;
|
|
147
|
-
/**
|
|
148
|
-
readonly
|
|
160
|
+
/** Options every model built on this route starts from. */
|
|
161
|
+
readonly options?: RequestOptions;
|
|
149
162
|
}
|
|
150
163
|
export interface MakeTransportInput<Body, Prepared, Frame, Event, State> {
|
|
151
164
|
readonly compact?: CompactionOperations;
|
|
@@ -167,8 +180,8 @@ export interface MakeTransportInput<Body, Prepared, Frame, Event, State> {
|
|
|
167
180
|
}) => Record<string, string>;
|
|
168
181
|
/** Runnable transport route. */
|
|
169
182
|
readonly transport: Transport<Body, Prepared, Frame>;
|
|
170
|
-
/**
|
|
171
|
-
readonly
|
|
183
|
+
/** Options every model built on this route starts from. */
|
|
184
|
+
readonly options?: RequestOptions;
|
|
172
185
|
}
|
|
173
186
|
export declare function make<Body, Prepared, Frame, Event, State, Compact extends CompactionOperations>(input: MakeTransportInput<Body, Prepared, Frame, Event, State> & {
|
|
174
187
|
readonly compact: Compact;
|
|
@@ -213,8 +226,8 @@ export declare const generateWith: (stream: Interface["stream"]) => (request: LL
|
|
|
213
226
|
}, CompactionOperations | undefined>>, options?: StreamOptions | undefined) => Effect.Effect<LLMResponse, AIError, never>;
|
|
214
227
|
export declare function stream(request: LLMRequest, options?: StreamOptions): Stream.Stream<LLMEvent, AIError, Service>;
|
|
215
228
|
export declare function generate(request: LLMRequest, options?: StreamOptions): Effect.Effect<LLMResponse, AIError, Service>;
|
|
216
|
-
|
|
217
|
-
export declare function
|
|
229
|
+
/** Compact through the client for `options.mechanism`, failing typed when the request's route does not support it. */
|
|
230
|
+
export declare function compactRequest(request: LLMRequest, options?: EndpointCompactOptions | TriggerCompactOptions): Effect.Effect<CompactionResponse | CompactionCheckpointResponse, AIError, Service>;
|
|
218
231
|
export declare const layer: Layer.Layer<Service, never, RequestExecutor.Service>;
|
|
219
232
|
export declare const Route: {
|
|
220
233
|
readonly make: typeof make;
|
package/dist/route/client.js
CHANGED
|
@@ -10,7 +10,7 @@ import { normalizeToolHistory } from "../tool-history.js";
|
|
|
10
10
|
import { sanitizeSurrogates } from "../utils/sanitize.js";
|
|
11
11
|
import * as ProviderShared from "../protocols/shared.js";
|
|
12
12
|
import { ToolSchemaProjection } from "../protocols/utils/tool-schema.js";
|
|
13
|
-
import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason,
|
|
13
|
+
import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, LLMResponse, ResponseFold, LanguageModel, Message, LLMEvent, InvalidProviderOutputError, ProviderConfigurationError, ProviderID, ToolChoice, ToolEntry, mergeGeneration, mergeRequestOptions, mergeProviderOptions, mergeTransport, withPartial, } from "../schema/index.js";
|
|
14
14
|
const makeRouteLanguageModel = (route, mapped) => {
|
|
15
15
|
const provider = route.provider ?? ("provider" in mapped ? mapped.provider : undefined);
|
|
16
16
|
if (!provider)
|
|
@@ -26,38 +26,22 @@ const makeRouteLanguageModel = (route, mapped) => {
|
|
|
26
26
|
route,
|
|
27
27
|
});
|
|
28
28
|
};
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
return {
|
|
32
|
-
...base,
|
|
33
|
-
...patch,
|
|
34
|
-
headers,
|
|
35
|
-
generation: mergeGenerationOptions(base?.generation, patch.generation),
|
|
36
|
-
providerOptions: mergeProviderOptions(base?.providerOptions, patch.providerOptions),
|
|
37
|
-
http: mergeHttpOptions(base?.http, HttpOptions.make(patch.http), headers === undefined ? undefined : new HttpOptions({ headers })),
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
const mergeHeaders = (...items) => {
|
|
41
|
-
const entries = items.flatMap((item) => item === undefined ? [] : Object.entries(item).filter((entry) => entry[1] !== undefined));
|
|
42
|
-
if (entries.length === 0)
|
|
43
|
-
return undefined;
|
|
44
|
-
return Object.fromEntries(entries);
|
|
45
|
-
};
|
|
46
|
-
export function canCompact(request, options) {
|
|
29
|
+
export function canCompact(target, options) {
|
|
30
|
+
const model = target instanceof LanguageModel ? target : target.model;
|
|
47
31
|
if (options?.mechanism === "trigger")
|
|
48
|
-
return
|
|
32
|
+
return model.route.compact?.trigger !== undefined;
|
|
49
33
|
if (options?.mechanism !== undefined && options.mechanism !== "endpoint")
|
|
50
34
|
return false;
|
|
51
|
-
return
|
|
35
|
+
return model.route.compact?.endpoint !== undefined;
|
|
52
36
|
}
|
|
53
|
-
const unsupportedCompaction = (
|
|
37
|
+
const unsupportedCompaction = (model, mechanism) => {
|
|
54
38
|
if (mechanism !== undefined && mechanism !== "endpoint" && mechanism !== "trigger")
|
|
55
39
|
return ProviderShared.invalidRequest(`Unknown compaction mechanism: ${mechanism}`);
|
|
56
40
|
return ProviderShared.unsupportedOperation({
|
|
57
41
|
operation: mechanism === "trigger" ? "compact.trigger" : "compact",
|
|
58
|
-
provider:
|
|
59
|
-
route:
|
|
60
|
-
message: `${
|
|
42
|
+
provider: model.provider,
|
|
43
|
+
route: model.route.id,
|
|
44
|
+
message: `${model.provider}/${model.route.id} does not support ${mechanism === "trigger" ? "trigger" : "explicit"} compaction`,
|
|
61
45
|
});
|
|
62
46
|
};
|
|
63
47
|
export class LLMClientService extends Context.Service()("@opencode/LLMClient") {
|
|
@@ -65,15 +49,14 @@ export class LLMClientService extends Context.Service()("@opencode/LLMClient") {
|
|
|
65
49
|
export const Service = LLMClientService;
|
|
66
50
|
const resolveRequestOptions = (request) => {
|
|
67
51
|
const messages = normalizeToolHistory(request.messages);
|
|
68
|
-
const normalized = messages === request.messages ? request :
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
});
|
|
52
|
+
const normalized = messages === request.messages ? request : { ...request, messages };
|
|
53
|
+
const options = normalized.model.options;
|
|
54
|
+
return {
|
|
55
|
+
...normalized,
|
|
56
|
+
...mergeGeneration(options, normalized),
|
|
57
|
+
providerOptions: mergeProviderOptions(options?.providerOptions, normalized.providerOptions),
|
|
58
|
+
...mergeTransport(options, normalized),
|
|
59
|
+
};
|
|
77
60
|
};
|
|
78
61
|
const streamError = (route, message, cause) => {
|
|
79
62
|
const failed = cause.reasons.find(Cause.isFailReason)?.error;
|
|
@@ -94,7 +77,7 @@ const requireTerminalEvent = (route) => (events) => Stream.suspend(() => {
|
|
|
94
77
|
// Raw events carry provider frames that may trail the terminal event.
|
|
95
78
|
if (terminal && !LLMEvent.is.raw(event))
|
|
96
79
|
return Effect.fail(ProviderShared.eventError(route, `Provider emitted ${event.type} after the terminal event`));
|
|
97
|
-
if (LLMEvent.is.finish(event)
|
|
80
|
+
if (LLMEvent.is.finish(event))
|
|
98
81
|
terminal = true;
|
|
99
82
|
return Effect.succeed(event);
|
|
100
83
|
}), Stream.onEnd(Effect.suspend(() => (terminal ? Effect.void : Effect.fail(incompleteStreamError(route))))));
|
|
@@ -122,12 +105,12 @@ function makeFromTransport(input) {
|
|
|
122
105
|
auth: routeInput.auth ?? Auth.none,
|
|
123
106
|
headers: routeInput.headers,
|
|
124
107
|
transport: routeInput.transport,
|
|
125
|
-
|
|
108
|
+
options: routeInput.options,
|
|
126
109
|
body: protocol.body,
|
|
127
110
|
supportsEffortUpdates: protocol.supportsEffortUpdates,
|
|
128
111
|
sanitizer: protocol.sanitizer,
|
|
129
112
|
with: (patch) => {
|
|
130
|
-
const { compact, id, provider, providerMetadataKey, auth, transport, endpoint, ...
|
|
113
|
+
const { compact, id, provider, providerMetadataKey, auth, transport, endpoint, ...options } = patch;
|
|
131
114
|
return build({
|
|
132
115
|
...routeInput,
|
|
133
116
|
compact: "compact" in patch ? compact : routeInput.compact,
|
|
@@ -138,7 +121,7 @@ function makeFromTransport(input) {
|
|
|
138
121
|
auth: auth ?? routeInput.auth,
|
|
139
122
|
endpoint: endpoint ? Endpoint.merge(routeInput.endpoint, endpoint) : routeInput.endpoint,
|
|
140
123
|
transport: transport ?? routeInput.transport,
|
|
141
|
-
|
|
124
|
+
options: mergeRequestOptions(route.options, options),
|
|
142
125
|
});
|
|
143
126
|
},
|
|
144
127
|
model: (input) => makeRouteLanguageModel(route, input),
|
|
@@ -148,7 +131,7 @@ function makeFromTransport(input) {
|
|
|
148
131
|
endpoint: routeInput.endpoint,
|
|
149
132
|
auth: routeInput.auth ?? Auth.none,
|
|
150
133
|
encodeBody: ProviderShared.encodeJson,
|
|
151
|
-
middleware: options?.http,
|
|
134
|
+
middleware: RequestExecutor.withFetch(request.fetch, options?.http),
|
|
152
135
|
webSocket: options?.webSocket,
|
|
153
136
|
}),
|
|
154
137
|
streamPrepared: (prepared, request, runtime, options) => {
|
|
@@ -194,7 +177,7 @@ function makeFromTransport(input) {
|
|
|
194
177
|
};
|
|
195
178
|
return route;
|
|
196
179
|
};
|
|
197
|
-
return build({ ...input,
|
|
180
|
+
return build({ ...input, options: mergeRequestOptions(input.options) });
|
|
198
181
|
}
|
|
199
182
|
export function make(input) {
|
|
200
183
|
if ("transport" in input)
|
|
@@ -210,20 +193,25 @@ export function make(input) {
|
|
|
210
193
|
auth: input.auth,
|
|
211
194
|
headers: input.headers,
|
|
212
195
|
transport: HttpTransport.httpJson({ framing: input.framing }),
|
|
213
|
-
|
|
196
|
+
options: input.options,
|
|
214
197
|
});
|
|
215
198
|
}
|
|
216
199
|
const prepareRequest = (request) => {
|
|
217
200
|
const original = resolveRequestOptions(request);
|
|
218
|
-
const sanitized =
|
|
201
|
+
const sanitized = restoreClasses({ ...sanitizeSurrogates({ ...original, model: undefined }), model: original.model });
|
|
219
202
|
// Deduplicate per sibling level; a tool and a namespace may share a name.
|
|
220
203
|
const dedupe = (tools) => [...new Map(tools.map((tool) => [`${tool.type}:${tool.name}`, tool])).values()].map((tool) => tool.type === "tool" ? tool : { ...tool, tools: dedupe(tool.tools) });
|
|
221
|
-
const resolved = applyCachePolicy(applyEffortUpdates(
|
|
204
|
+
const resolved = applyCachePolicy(applyEffortUpdates({ ...sanitized, tools: ToolSchemaProjection.tools(dedupe(sanitized.tools), sanitized.model) }));
|
|
222
205
|
const headers = resolved.model.route.headers?.({ request: resolved });
|
|
223
|
-
return headers === undefined
|
|
224
|
-
? resolved
|
|
225
|
-
: LLMRequest.update(resolved, { http: mergeHttpOptions(new HttpOptions({ headers }), resolved.http) });
|
|
206
|
+
return headers === undefined ? resolved : { ...resolved, headers: { ...headers, ...resolved.headers } };
|
|
226
207
|
};
|
|
208
|
+
// Sanitizing copies changed class instances into plain records; rebuild them so protocols see the canonical classes.
|
|
209
|
+
const restoreClasses = (request) => ({
|
|
210
|
+
...request,
|
|
211
|
+
messages: request.messages.map(Message.make),
|
|
212
|
+
tools: request.tools.map(ToolEntry.make),
|
|
213
|
+
toolChoice: request.toolChoice === undefined ? undefined : ToolChoice.make(request.toolChoice),
|
|
214
|
+
});
|
|
227
215
|
const compile = Effect.fn("LLM.compile")(function* (request, options) {
|
|
228
216
|
const references = request.messages.flatMap((message) => message.content.flatMap((part) => {
|
|
229
217
|
if (part.type === "media") {
|
|
@@ -264,12 +252,12 @@ const streamRequestWith = (runtime) => (request, options) => Stream.unwrap(Effec
|
|
|
264
252
|
return compiled.route.streamPrepared(compiled.prepared, compiled.request, runtime, options);
|
|
265
253
|
}));
|
|
266
254
|
export const generateWith = (stream) => Effect.fn("LLM.generate")(function* (request, options) {
|
|
267
|
-
let state =
|
|
268
|
-
yield* stream(request, options).pipe(Stream.runForEach((event) => Effect.sync(() => (state =
|
|
269
|
-
const response =
|
|
255
|
+
let state = ResponseFold.empty();
|
|
256
|
+
yield* stream(request, options).pipe(Stream.runForEach((event) => Effect.sync(() => (state = ResponseFold.reduce(state, event)))), Effect.mapError((error) => withPartial(error, ResponseFold.partial(state))));
|
|
257
|
+
const response = ResponseFold.complete(state);
|
|
270
258
|
if (response)
|
|
271
259
|
return response;
|
|
272
|
-
return yield* withPartial(incompleteStreamError(`${request.model.provider}/${request.model.route.id}`),
|
|
260
|
+
return yield* withPartial(incompleteStreamError(`${request.model.provider}/${request.model.route.id}`), ResponseFold.partial(state));
|
|
273
261
|
});
|
|
274
262
|
export function stream(request, options) {
|
|
275
263
|
return Stream.unwrap(Effect.gen(function* () {
|
|
@@ -281,14 +269,15 @@ export function generate(request, options) {
|
|
|
281
269
|
return yield* (yield* Service).generate(request, options);
|
|
282
270
|
});
|
|
283
271
|
}
|
|
284
|
-
|
|
272
|
+
/** Compact through the client for `options.mechanism`, failing typed when the request's route does not support it. */
|
|
273
|
+
export function compactRequest(request, options) {
|
|
285
274
|
return Effect.gen(function* () {
|
|
286
275
|
const client = yield* Service;
|
|
287
276
|
if (options?.mechanism === "trigger" && canCompact(request, options))
|
|
288
277
|
return yield* client.compact(request, options);
|
|
289
278
|
if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
|
|
290
279
|
return yield* client.compact(request, options);
|
|
291
|
-
return yield* unsupportedCompaction(request, options?.mechanism);
|
|
280
|
+
return yield* unsupportedCompaction(request.model, options?.mechanism);
|
|
292
281
|
});
|
|
293
282
|
}
|
|
294
283
|
export const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
@@ -300,7 +289,7 @@ export const layer = Layer.effect(Service, Effect.gen(function* () {
|
|
|
300
289
|
return request.model.route.compact.trigger(prepareRequest(request), executor, options);
|
|
301
290
|
if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
|
|
302
291
|
return request.model.route.compact.endpoint(prepareRequest(request), executor, options);
|
|
303
|
-
return unsupportedCompaction(request, options?.mechanism);
|
|
292
|
+
return unsupportedCompaction(request.model, options?.mechanism);
|
|
304
293
|
});
|
|
305
294
|
}
|
|
306
295
|
return Service.of({
|
package/dist/route/decision.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import type { HttpClientResponse } from "effect/http";
|
|
3
|
-
import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type
|
|
4
|
-
import {
|
|
3
|
+
import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type DecisionRequest, type DecisionResponse } from "../decision.js";
|
|
4
|
+
import { ProviderID, type HttpOptions, type AIError, type ProviderOptions } from "../schema/index.js";
|
|
5
5
|
import { Auth } from "./auth.js";
|
|
6
6
|
import { Endpoint } from "./endpoint.js";
|
|
7
7
|
import { MediaProtocol } from "./media-protocol.js";
|
|
@@ -12,11 +12,11 @@ export interface Protocol {
|
|
|
12
12
|
/** The host only takes inline data, so URL media is downloaded before `body.from`. */
|
|
13
13
|
readonly inlineMedia?: boolean;
|
|
14
14
|
readonly body: {
|
|
15
|
-
readonly from: (request:
|
|
15
|
+
readonly from: (request: DecisionRequest) => Effect.Effect<Record<string, unknown>, AIError>;
|
|
16
16
|
};
|
|
17
17
|
readonly response: {
|
|
18
18
|
readonly decode: (response: HttpClientResponse.HttpClientResponse, context: {
|
|
19
|
-
readonly request:
|
|
19
|
+
readonly request: DecisionRequest;
|
|
20
20
|
readonly providerMetadataKey: string;
|
|
21
21
|
}) => Effect.Effect<DecisionResponse, AIError>;
|
|
22
22
|
};
|
|
@@ -27,17 +27,17 @@ export interface Patch {
|
|
|
27
27
|
readonly provider?: string | ProviderID;
|
|
28
28
|
/** ProviderMetadata namespace for responses; defaults to the provider. */
|
|
29
29
|
readonly providerMetadataKey?: string;
|
|
30
|
-
readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body,
|
|
30
|
+
readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body, DecisionRequest>;
|
|
31
31
|
readonly auth?: Auth.Definition;
|
|
32
32
|
readonly headers?: Record<string, string>;
|
|
33
|
-
readonly http?: HttpOptions
|
|
33
|
+
readonly http?: HttpOptions;
|
|
34
34
|
}
|
|
35
35
|
export interface Route {
|
|
36
36
|
readonly id: string;
|
|
37
37
|
readonly provider: ProviderID;
|
|
38
38
|
readonly providerMetadataKey: string;
|
|
39
39
|
readonly protocol: Protocol;
|
|
40
|
-
readonly endpoint: Endpoint.Definition<MediaProtocol.Body,
|
|
40
|
+
readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
|
|
41
41
|
readonly auth: Auth.Definition;
|
|
42
42
|
readonly headers?: Record<string, string>;
|
|
43
43
|
readonly http?: HttpOptions;
|
|
@@ -52,7 +52,7 @@ export interface MakeInput extends Omit<Patch, "endpoint"> {
|
|
|
52
52
|
readonly id: string;
|
|
53
53
|
readonly provider: string | ProviderID;
|
|
54
54
|
readonly protocol: Protocol;
|
|
55
|
-
readonly endpoint: Endpoint.Definition<MediaProtocol.Body,
|
|
55
|
+
readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
|
|
56
56
|
}
|
|
57
57
|
export declare const make: (input: MakeInput) => Route;
|
|
58
58
|
export * as DecisionRoute from "./decision.js";
|
package/dist/route/decision.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
2
|
import { DecisionModel, DecisionModelCompatibility, } from "../decision.js";
|
|
3
|
-
import {
|
|
3
|
+
import { ProviderID } from "../schema/index.js";
|
|
4
4
|
import { Auth } from "./auth.js";
|
|
5
5
|
import { Endpoint } from "./endpoint.js";
|
|
6
6
|
import { RequestExecutorService } from "./executor-service.js";
|
|
@@ -17,7 +17,7 @@ export const make = (input) => {
|
|
|
17
17
|
endpoint: input.endpoint,
|
|
18
18
|
auth: input.auth ?? Auth.none,
|
|
19
19
|
headers: input.headers,
|
|
20
|
-
http:
|
|
20
|
+
http: input.http,
|
|
21
21
|
};
|
|
22
22
|
return {
|
|
23
23
|
...route,
|
|
@@ -28,7 +28,7 @@ export const make = (input) => {
|
|
|
28
28
|
providerMetadataKey: patch.providerMetadataKey ?? (patch.provider === undefined ? route.providerMetadataKey : undefined),
|
|
29
29
|
endpoint: patch.endpoint === undefined ? route.endpoint : Endpoint.merge(route.endpoint, patch.endpoint),
|
|
30
30
|
headers: patch.headers === undefined ? route.headers : { ...route.headers, ...patch.headers },
|
|
31
|
-
http: patch.http
|
|
31
|
+
http: patch.http ?? route.http,
|
|
32
32
|
}),
|
|
33
33
|
model: (model) => {
|
|
34
34
|
const send = transport(route);
|