@opencode-ai/ai 0.0.0-bootstrap.0 → 0.0.0-next-15617
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 +176 -1
- package/dist/cache-policy.d.ts +2 -0
- package/dist/cache-policy.js +108 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +9 -0
- package/dist/llm.d.ts +223 -0
- package/dist/llm.js +81 -0
- package/dist/protocols/anthropic-messages.d.ts +503 -0
- package/dist/protocols/anthropic-messages.js +745 -0
- package/dist/protocols/bedrock-converse.d.ts +476 -0
- package/dist/protocols/bedrock-converse.js +526 -0
- package/dist/protocols/bedrock-event-stream.d.ts +9 -0
- package/dist/protocols/bedrock-event-stream.js +64 -0
- package/dist/protocols/gemini.d.ts +220 -0
- package/dist/protocols/gemini.js +420 -0
- package/dist/protocols/google-vertex-anthropic.d.ts +494 -0
- package/dist/protocols/google-vertex-anthropic.js +33 -0
- package/dist/protocols/google-vertex-gemini.d.ts +59 -0
- package/dist/protocols/google-vertex-gemini.js +18 -0
- package/dist/protocols/index.d.ts +9 -0
- package/dist/protocols/index.js +9 -0
- package/dist/protocols/openai-chat.d.ts +391 -0
- package/dist/protocols/openai-chat.js +423 -0
- package/dist/protocols/openai-compatible-chat.d.ts +73 -0
- package/dist/protocols/openai-compatible-chat.js +20 -0
- package/dist/protocols/openai-compatible-responses.d.ts +85 -0
- package/dist/protocols/openai-compatible-responses.js +18 -0
- package/dist/protocols/openai-responses.d.ts +613 -0
- package/dist/protocols/openai-responses.js +828 -0
- package/dist/protocols/shared.d.ts +192 -0
- package/dist/protocols/shared.js +236 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +18 -0
- package/dist/protocols/utils/bedrock-auth.js +41 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +15 -0
- package/dist/protocols/utils/bedrock-cache.js +29 -0
- package/dist/protocols/utils/bedrock-media.d.ts +49 -0
- package/dist/protocols/utils/bedrock-media.js +68 -0
- package/dist/protocols/utils/cache.d.ts +6 -0
- package/dist/protocols/utils/cache.js +8 -0
- package/dist/protocols/utils/gemini-tool-schema.d.ts +2 -0
- package/dist/protocols/utils/gemini-tool-schema.js +83 -0
- package/dist/protocols/utils/lifecycle.d.ts +19 -0
- package/dist/protocols/utils/lifecycle.js +65 -0
- package/dist/protocols/utils/openai-options.d.ts +22 -0
- package/dist/protocols/utils/openai-options.js +64 -0
- package/dist/protocols/utils/tool-schema.d.ts +7 -0
- package/dist/protocols/utils/tool-schema.js +79 -0
- package/dist/protocols/utils/tool-stream.d.ts +155 -0
- package/dist/protocols/utils/tool-stream.js +136 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +13 -0
- package/dist/provider-error.js +122 -0
- package/dist/provider-package.d.ts +13 -0
- package/dist/provider-package.js +1 -0
- package/dist/provider.d.ts +23 -0
- package/dist/provider.js +2 -0
- package/dist/providers/amazon-bedrock.d.ts +150 -0
- package/dist/providers/amazon-bedrock.js +41 -0
- package/dist/providers/anthropic-compatible.d.ts +183 -0
- package/dist/providers/anthropic-compatible.js +44 -0
- package/dist/providers/anthropic.d.ts +181 -0
- package/dist/providers/anthropic.js +39 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +183 -0
- package/dist/providers/azure.js +89 -0
- package/dist/providers/cloudflare.d.ts +232 -0
- package/dist/providers/cloudflare.js +87 -0
- package/dist/providers/github-copilot.d.ts +167 -0
- package/dist/providers/github-copilot.js +47 -0
- package/dist/providers/google-vertex/anthropic.d.ts +2 -0
- package/dist/providers/google-vertex/anthropic.js +1 -0
- package/dist/providers/google-vertex-anthropic.d.ts +181 -0
- package/dist/providers/google-vertex-anthropic.js +46 -0
- package/dist/providers/google-vertex-shared.d.ts +22 -0
- package/dist/providers/google-vertex-shared.js +61 -0
- package/dist/providers/google-vertex.d.ts +93 -0
- package/dist/providers/google-vertex.js +49 -0
- package/dist/providers/google.d.ts +85 -0
- package/dist/providers/google.js +33 -0
- package/dist/providers/index.d.ts +15 -0
- package/dist/providers/index.js +15 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-profile.d.ts +43 -0
- package/dist/providers/openai-compatible-profile.js +12 -0
- package/dist/providers/openai-compatible-responses.d.ts +107 -0
- package/dist/providers/openai-compatible-responses.js +33 -0
- package/dist/providers/openai-compatible.d.ts +130 -0
- package/dist/providers/openai-compatible.js +55 -0
- package/dist/providers/openai-options.d.ts +30 -0
- package/dist/providers/openai-options.js +42 -0
- package/dist/providers/openai.d.ts +264 -0
- package/dist/providers/openai.js +62 -0
- package/dist/providers/openrouter.d.ts +321 -0
- package/dist/providers/openrouter.js +65 -0
- package/dist/providers/xai.d.ts +169 -0
- package/dist/providers/xai.js +43 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/route/auth-options.d.ts +34 -0
- package/dist/route/auth-options.js +14 -0
- package/dist/route/auth.d.ts +49 -0
- package/dist/route/auth.js +89 -0
- package/dist/route/client.d.ts +330 -0
- package/dist/route/client.js +211 -0
- package/dist/route/endpoint.d.ts +28 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor.d.ts +12 -0
- package/dist/route/executor.js +229 -0
- package/dist/route/framing.d.ts +23 -0
- package/dist/route/framing.js +4 -0
- package/dist/route/index.d.ts +16 -0
- package/dist/route/index.js +9 -0
- package/dist/route/protocol.d.ts +76 -0
- package/dist/route/protocol.js +17 -0
- package/dist/route/transport/http.d.ts +33 -0
- package/dist/route/transport/http.js +98 -0
- package/dist/route/transport/index.d.ts +27 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket.d.ts +56 -0
- package/dist/route/transport/websocket.js +181 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +153 -0
- package/dist/schema/errors.js +150 -0
- package/dist/schema/events.d.ts +4292 -0
- package/dist/schema/events.js +482 -0
- package/dist/schema/ids.d.ts +30 -0
- package/dist/schema/ids.js +18 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +385 -0
- package/dist/schema/messages.js +246 -0
- package/dist/schema/options.d.ts +143 -0
- package/dist/schema/options.js +195 -0
- package/dist/tool-runtime.d.ts +15 -0
- package/dist/tool-runtime.js +35 -0
- package/dist/tool.d.ts +134 -0
- package/dist/tool.js +66 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/package.json +36 -9
- package/index.js +0 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LLMError, type HttpContext, type HttpRateLimitDetails, type ProviderMetadata } from "./schema";
|
|
2
|
+
export declare const isContextOverflow: (message: string) => boolean;
|
|
3
|
+
export declare const isContextOverflowFailure: (failure: unknown) => boolean;
|
|
4
|
+
export interface ProviderFailure {
|
|
5
|
+
readonly message: string;
|
|
6
|
+
readonly status?: number | undefined;
|
|
7
|
+
readonly code?: string | undefined;
|
|
8
|
+
readonly retryAfterMs?: number | undefined;
|
|
9
|
+
readonly rateLimit?: HttpRateLimitDetails | undefined;
|
|
10
|
+
readonly http?: HttpContext | undefined;
|
|
11
|
+
readonly providerMetadata?: ProviderMetadata | undefined;
|
|
12
|
+
}
|
|
13
|
+
export declare function classifyProviderFailure(input: ProviderFailure): LLMError["reason"];
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { AuthenticationReason, ContentPolicyReason, InvalidRequestReason, LLMError, ProviderErrorEvent, ProviderInternalReason, QuotaExceededReason, RateLimitReason, UnknownProviderReason, } from "./schema";
|
|
3
|
+
const patterns = [
|
|
4
|
+
/prompt is too long/i,
|
|
5
|
+
/input is too long for requested model/i,
|
|
6
|
+
/exceeds the context window/i,
|
|
7
|
+
/input token count.*exceeds the maximum/i,
|
|
8
|
+
/tokens in request more than max tokens allowed/i,
|
|
9
|
+
/maximum prompt length is \d+/i,
|
|
10
|
+
/reduce the length of the messages/i,
|
|
11
|
+
/maximum context length is \d+ tokens/i,
|
|
12
|
+
/exceeds the limit of \d+/i,
|
|
13
|
+
/exceeds the available context size/i,
|
|
14
|
+
/greater than the context length/i,
|
|
15
|
+
/context window exceeds limit/i,
|
|
16
|
+
/exceeded model token limit/i,
|
|
17
|
+
/context[_ ]length[_ ]exceeded/i,
|
|
18
|
+
/request entity too large/i,
|
|
19
|
+
/context length is only \d+ tokens/i,
|
|
20
|
+
/input length.*exceeds.*context length/i,
|
|
21
|
+
/prompt too long; exceeded (?:max )?context length/i,
|
|
22
|
+
/too large for model with \d+ maximum context length/i,
|
|
23
|
+
/model_context_window_exceeded/i,
|
|
24
|
+
];
|
|
25
|
+
export const isContextOverflow = (message) => patterns.some((pattern) => pattern.test(message)) || /^4(00|13)\s*(status code)?\s*\(no body\)/i.test(message);
|
|
26
|
+
export const isContextOverflowFailure = (failure) => failure instanceof LLMError
|
|
27
|
+
? failure.reason._tag === "InvalidRequest" && failure.reason.classification === "context-overflow"
|
|
28
|
+
: Schema.is(ProviderErrorEvent)(failure) && failure.classification === "context-overflow";
|
|
29
|
+
const decodeJson = Schema.decodeUnknownOption(Schema.UnknownFromJsonString);
|
|
30
|
+
const QUOTA_CODES = new Set(["insufficient_quota", "usage_not_included", "billing_error"]);
|
|
31
|
+
const SERVER_CODES = new Set([
|
|
32
|
+
"api_error",
|
|
33
|
+
"internal_error",
|
|
34
|
+
"internalserverexception",
|
|
35
|
+
"modelstreamerrorexception",
|
|
36
|
+
"overloaded_error",
|
|
37
|
+
"server_error",
|
|
38
|
+
"server_is_overloaded",
|
|
39
|
+
"serviceunavailableexception",
|
|
40
|
+
]);
|
|
41
|
+
const INVALID_REQUEST_CODES = new Set(["invalid_prompt", "invalid_request_error", "validationexception"]);
|
|
42
|
+
const RATE_LIMIT_TEXT = /rate increased too quickly|rate[-_\s]?limit|too[_\s]?many[_\s]?requests/i;
|
|
43
|
+
const QUOTA_TEXT = /insufficient[-_\s]?quota|quota[-_\s]?exceeded/i;
|
|
44
|
+
const CONTENT_POLICY_TEXT = /content[-_\s]?policy|content_filter|safety/i;
|
|
45
|
+
// Keep HTTP failures and provider-reported stream failures on one typed path so
|
|
46
|
+
// session retry policy never needs provider-specific string matching.
|
|
47
|
+
export function classifyProviderFailure(input) {
|
|
48
|
+
const body = input.http?.body ?? "";
|
|
49
|
+
const codes = [input.code, ...providerCodes(body), ...providerCodes(input.message)]
|
|
50
|
+
.filter((code) => code !== undefined)
|
|
51
|
+
.map((code) => code.toLowerCase());
|
|
52
|
+
const text = body || input.message;
|
|
53
|
+
const common = { message: input.message, providerMetadata: input.providerMetadata, http: input.http };
|
|
54
|
+
const clientScoped = input.status === undefined || (input.status >= 400 && input.status < 500);
|
|
55
|
+
if (clientScoped &&
|
|
56
|
+
(codes.includes("context_length_exceeded") ||
|
|
57
|
+
codes.includes("model_context_window_exceeded") ||
|
|
58
|
+
isContextOverflow(text)))
|
|
59
|
+
return new InvalidRequestReason({ ...common, classification: "context-overflow" });
|
|
60
|
+
if (CONTENT_POLICY_TEXT.test(text))
|
|
61
|
+
return new ContentPolicyReason(common);
|
|
62
|
+
if (codes.some((code) => QUOTA_CODES.has(code)) || (input.status === 429 && QUOTA_TEXT.test(text)))
|
|
63
|
+
return new QuotaExceededReason(common);
|
|
64
|
+
if (input.status === 401)
|
|
65
|
+
return new AuthenticationReason({ ...common, kind: "invalid" });
|
|
66
|
+
if (input.status === 403)
|
|
67
|
+
return new AuthenticationReason({ ...common, kind: "insufficient-permissions" });
|
|
68
|
+
if (codes.includes("authentication_error"))
|
|
69
|
+
return new AuthenticationReason({ ...common, kind: "invalid" });
|
|
70
|
+
if (codes.includes("permission_error"))
|
|
71
|
+
return new AuthenticationReason({ ...common, kind: "insufficient-permissions" });
|
|
72
|
+
if (codes.some((code) => code.includes("rate_limit") || code === "too_many_requests" || code === "throttlingexception"))
|
|
73
|
+
return new RateLimitReason({
|
|
74
|
+
...common,
|
|
75
|
+
retryAfterMs: input.retryAfterMs,
|
|
76
|
+
rateLimit: input.rateLimit,
|
|
77
|
+
});
|
|
78
|
+
if (RATE_LIMIT_TEXT.test(text))
|
|
79
|
+
return new RateLimitReason({
|
|
80
|
+
...common,
|
|
81
|
+
retryAfterMs: input.retryAfterMs,
|
|
82
|
+
rateLimit: input.rateLimit,
|
|
83
|
+
});
|
|
84
|
+
if (codes.some((code) => SERVER_CODES.has(code) || code.includes("exhausted") || code.includes("unavailable")))
|
|
85
|
+
return new ProviderInternalReason({
|
|
86
|
+
...common,
|
|
87
|
+
status: input.status,
|
|
88
|
+
retryAfterMs: input.retryAfterMs,
|
|
89
|
+
});
|
|
90
|
+
if (input.status === 429) {
|
|
91
|
+
return new RateLimitReason({
|
|
92
|
+
...common,
|
|
93
|
+
retryAfterMs: input.retryAfterMs,
|
|
94
|
+
rateLimit: input.rateLimit,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (input.status !== undefined && input.status >= 500)
|
|
98
|
+
return new ProviderInternalReason({
|
|
99
|
+
...common,
|
|
100
|
+
status: input.status,
|
|
101
|
+
retryAfterMs: input.retryAfterMs,
|
|
102
|
+
});
|
|
103
|
+
if (codes.some((code) => INVALID_REQUEST_CODES.has(code)))
|
|
104
|
+
return new InvalidRequestReason(common);
|
|
105
|
+
if (input.status === 400 ||
|
|
106
|
+
input.status === 404 ||
|
|
107
|
+
input.status === 409 ||
|
|
108
|
+
input.status === 413 ||
|
|
109
|
+
input.status === 422)
|
|
110
|
+
return new InvalidRequestReason(common);
|
|
111
|
+
return new UnknownProviderReason({ ...common, status: input.status });
|
|
112
|
+
}
|
|
113
|
+
function providerCodes(value) {
|
|
114
|
+
const decoded = Option.getOrUndefined(decodeJson(value));
|
|
115
|
+
if (!isRecord(decoded))
|
|
116
|
+
return [];
|
|
117
|
+
const error = isRecord(decoded.error) ? decoded.error : undefined;
|
|
118
|
+
return [decoded.code, error?.code, error?.type].filter((value) => typeof value === "string");
|
|
119
|
+
}
|
|
120
|
+
function isRecord(value) {
|
|
121
|
+
return typeof value === "object" && value !== null;
|
|
122
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Model } from "./schema";
|
|
2
|
+
export interface Settings extends Readonly<Record<string, unknown>> {
|
|
3
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
4
|
+
readonly body?: Readonly<Record<string, unknown>>;
|
|
5
|
+
readonly limits?: {
|
|
6
|
+
readonly context: number;
|
|
7
|
+
readonly output: number;
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export interface Definition<ProviderSettings extends Settings = Settings> {
|
|
11
|
+
readonly model: (modelID: string, settings: ProviderSettings) => Model;
|
|
12
|
+
}
|
|
13
|
+
export * as ProviderPackage from "./provider-package";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as ProviderPackage from "./provider-package";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Model, ModelID, ProviderID } from "./schema";
|
|
2
|
+
export type ModelOptions = Pick<Model.Input, "defaults" | "compatibility">;
|
|
3
|
+
/**
|
|
4
|
+
* Advanced structural provider definition helper. Built-in providers should
|
|
5
|
+
* prefer explicit `configure(options).model(id)` facades so deployment config is
|
|
6
|
+
* chosen before model selection. The optional `apis` map remains for external
|
|
7
|
+
* structural providers that expose multiple route selectors behind one provider.
|
|
8
|
+
*/
|
|
9
|
+
export type ModelFactory<Options extends ModelOptions = ModelOptions> = (id: string | ModelID, options?: Options) => Model;
|
|
10
|
+
type AnyModelFactory = (...args: never[]) => Model;
|
|
11
|
+
export interface Definition<Factory extends AnyModelFactory = ModelFactory> {
|
|
12
|
+
readonly id: ProviderID;
|
|
13
|
+
readonly model: Factory;
|
|
14
|
+
readonly apis?: Record<string, AnyModelFactory>;
|
|
15
|
+
}
|
|
16
|
+
type DefinitionShape = {
|
|
17
|
+
readonly id: ProviderID;
|
|
18
|
+
readonly model: (...args: never[]) => Model;
|
|
19
|
+
readonly apis?: Record<string, (...args: never[]) => Model>;
|
|
20
|
+
};
|
|
21
|
+
type NoExtraFields<Input, Shape> = Input & Record<Exclude<keyof Input, keyof Shape>, never>;
|
|
22
|
+
export declare const make: <DefinitionType extends DefinitionShape>(definition: NoExtraFields<DefinitionType, DefinitionShape>) => NoExtraFields<DefinitionType, DefinitionShape>;
|
|
23
|
+
export * as Provider from "./provider";
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { RouteDefaultsInput } from "../route/client";
|
|
2
|
+
import type { ProviderPackage } from "../provider-package";
|
|
3
|
+
import { type ModelID } from "../schema";
|
|
4
|
+
import type { BedrockCredentials } from "../protocols/bedrock-converse";
|
|
5
|
+
export declare const id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
6
|
+
export type Config = RouteDefaultsInput & {
|
|
7
|
+
readonly apiKey?: string;
|
|
8
|
+
readonly headers?: Record<string, string>;
|
|
9
|
+
readonly credentials?: BedrockCredentials;
|
|
10
|
+
/** AWS region. Defaults to `us-east-1` when neither this nor `credentials.region` is set. */
|
|
11
|
+
readonly region?: string;
|
|
12
|
+
/** Override the computed `https://bedrock-runtime.<region>.amazonaws.com` URL. */
|
|
13
|
+
readonly baseURL?: string;
|
|
14
|
+
};
|
|
15
|
+
export interface Settings extends ProviderPackage.Settings {
|
|
16
|
+
readonly apiKey?: string;
|
|
17
|
+
readonly auth?: "bearer" | "sigv4";
|
|
18
|
+
readonly baseURL?: string;
|
|
19
|
+
readonly credentials?: BedrockCredentials;
|
|
20
|
+
readonly region?: string;
|
|
21
|
+
readonly topP?: number;
|
|
22
|
+
}
|
|
23
|
+
export declare const routes: import("../route").Route<{
|
|
24
|
+
readonly modelId: string;
|
|
25
|
+
readonly messages: readonly ({
|
|
26
|
+
readonly role: "user";
|
|
27
|
+
readonly content: readonly ({
|
|
28
|
+
readonly cachePoint: {
|
|
29
|
+
readonly type: "default";
|
|
30
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
31
|
+
};
|
|
32
|
+
} | {
|
|
33
|
+
readonly image: {
|
|
34
|
+
readonly format: "png" | "jpeg" | "gif" | "webp";
|
|
35
|
+
readonly source: {
|
|
36
|
+
readonly bytes: string;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
} | {
|
|
40
|
+
readonly document: {
|
|
41
|
+
readonly format: "pdf" | "csv" | "doc" | "docx" | "xls" | "xlsx" | "html" | "txt" | "md";
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly source: {
|
|
44
|
+
readonly bytes: string;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
} | {
|
|
48
|
+
readonly text: string;
|
|
49
|
+
} | {
|
|
50
|
+
readonly toolResult: {
|
|
51
|
+
readonly toolUseId: string;
|
|
52
|
+
readonly content: readonly ({
|
|
53
|
+
readonly image: {
|
|
54
|
+
readonly format: "png" | "jpeg" | "gif" | "webp";
|
|
55
|
+
readonly source: {
|
|
56
|
+
readonly bytes: string;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
} | {
|
|
60
|
+
readonly text: string;
|
|
61
|
+
} | {
|
|
62
|
+
readonly json: unknown;
|
|
63
|
+
})[];
|
|
64
|
+
readonly status?: "error" | "success" | undefined;
|
|
65
|
+
};
|
|
66
|
+
})[];
|
|
67
|
+
} | {
|
|
68
|
+
readonly role: "assistant";
|
|
69
|
+
readonly content: readonly ({
|
|
70
|
+
readonly cachePoint: {
|
|
71
|
+
readonly type: "default";
|
|
72
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
73
|
+
};
|
|
74
|
+
} | {
|
|
75
|
+
readonly text: string;
|
|
76
|
+
} | {
|
|
77
|
+
readonly toolUse: {
|
|
78
|
+
readonly toolUseId: string;
|
|
79
|
+
readonly name: string;
|
|
80
|
+
readonly input: unknown;
|
|
81
|
+
};
|
|
82
|
+
} | {
|
|
83
|
+
readonly reasoningContent: {
|
|
84
|
+
readonly reasoningText?: {
|
|
85
|
+
readonly text: string;
|
|
86
|
+
readonly signature?: string | undefined;
|
|
87
|
+
} | undefined;
|
|
88
|
+
};
|
|
89
|
+
})[];
|
|
90
|
+
})[];
|
|
91
|
+
readonly system?: readonly ({
|
|
92
|
+
readonly cachePoint: {
|
|
93
|
+
readonly type: "default";
|
|
94
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
95
|
+
};
|
|
96
|
+
} | {
|
|
97
|
+
readonly text: string;
|
|
98
|
+
})[] | undefined;
|
|
99
|
+
readonly inferenceConfig?: {
|
|
100
|
+
readonly maxTokens?: number | undefined;
|
|
101
|
+
readonly temperature?: number | undefined;
|
|
102
|
+
readonly topP?: number | undefined;
|
|
103
|
+
readonly stopSequences?: readonly string[] | undefined;
|
|
104
|
+
} | undefined;
|
|
105
|
+
readonly toolConfig?: {
|
|
106
|
+
readonly tools: readonly ({
|
|
107
|
+
readonly cachePoint: {
|
|
108
|
+
readonly type: "default";
|
|
109
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
110
|
+
};
|
|
111
|
+
} | {
|
|
112
|
+
readonly toolSpec: {
|
|
113
|
+
readonly name: string;
|
|
114
|
+
readonly description: string;
|
|
115
|
+
readonly inputSchema: {
|
|
116
|
+
readonly json: {
|
|
117
|
+
readonly [x: string]: unknown;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
})[];
|
|
122
|
+
readonly toolChoice?: {
|
|
123
|
+
readonly auto: {};
|
|
124
|
+
} | {
|
|
125
|
+
readonly any: {};
|
|
126
|
+
} | {
|
|
127
|
+
readonly tool: {
|
|
128
|
+
readonly name: string;
|
|
129
|
+
};
|
|
130
|
+
} | undefined;
|
|
131
|
+
} | undefined;
|
|
132
|
+
readonly additionalModelRequestFields?: {
|
|
133
|
+
readonly [x: string]: unknown;
|
|
134
|
+
} | undefined;
|
|
135
|
+
}, import("../route/transport/http").HttpPrepared<object>>[];
|
|
136
|
+
export declare const configure: (input?: Config) => {
|
|
137
|
+
id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
138
|
+
model: (modelID: string | ModelID) => import("..").Model;
|
|
139
|
+
configure: (input?: Config) => /*elided*/ any;
|
|
140
|
+
};
|
|
141
|
+
export declare const provider: {
|
|
142
|
+
id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
143
|
+
model: (modelID: string | ModelID) => import("..").Model;
|
|
144
|
+
configure: (input?: Config) => {
|
|
145
|
+
id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
146
|
+
model: (modelID: string | ModelID) => import("..").Model;
|
|
147
|
+
configure: /*elided*/ any;
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
export declare const model: ProviderPackage.Definition<Settings>["model"];
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Auth } from "../route/auth";
|
|
2
|
+
import { ProviderID } from "../schema";
|
|
3
|
+
import * as BedrockConverse from "../protocols/bedrock-converse";
|
|
4
|
+
export const id = ProviderID.make("amazon-bedrock");
|
|
5
|
+
export const routes = [BedrockConverse.route];
|
|
6
|
+
const bedrockBaseURL = (region) => `https://bedrock-runtime.${region}.amazonaws.com`;
|
|
7
|
+
const configuredRoute = (input) => {
|
|
8
|
+
const { apiKey, credentials, region, baseURL, ...rest } = input;
|
|
9
|
+
const resolvedRegion = region ?? credentials?.region ?? "us-east-1";
|
|
10
|
+
return BedrockConverse.route.with({
|
|
11
|
+
...rest,
|
|
12
|
+
provider: id,
|
|
13
|
+
endpoint: { baseURL: baseURL ?? bedrockBaseURL(resolvedRegion) },
|
|
14
|
+
auth: apiKey === undefined ? BedrockConverse.sigV4Auth(credentials) : Auth.bearer(apiKey),
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
export const configure = (input = {}) => {
|
|
18
|
+
const route = configuredRoute(input);
|
|
19
|
+
return {
|
|
20
|
+
id,
|
|
21
|
+
model: (modelID) => route.model({ id: modelID }),
|
|
22
|
+
configure,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
export const provider = configure();
|
|
26
|
+
export const model = (modelID, settings) => {
|
|
27
|
+
if (settings.auth === "bearer" && settings.apiKey === undefined)
|
|
28
|
+
throw new Error("Amazon Bedrock bearer auth requires apiKey");
|
|
29
|
+
if (settings.auth === "sigv4" && settings.apiKey !== undefined)
|
|
30
|
+
throw new Error("Amazon Bedrock SigV4 auth does not accept apiKey");
|
|
31
|
+
return configure({
|
|
32
|
+
apiKey: settings.auth === "sigv4" ? undefined : settings.apiKey,
|
|
33
|
+
baseURL: settings.baseURL,
|
|
34
|
+
credentials: settings.credentials,
|
|
35
|
+
generation: settings.topP === undefined ? undefined : { topP: settings.topP },
|
|
36
|
+
headers: settings.headers === undefined ? undefined : { ...settings.headers },
|
|
37
|
+
http: settings.body === undefined ? undefined : { body: { ...settings.body } },
|
|
38
|
+
limits: settings.limits,
|
|
39
|
+
region: settings.region,
|
|
40
|
+
}).model(modelID);
|
|
41
|
+
};
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { ProviderPackage } from "../provider-package";
|
|
2
|
+
import type { ProviderAuthOption } from "../route/auth-options";
|
|
3
|
+
import type { RouteDefaultsInput } from "../route/client";
|
|
4
|
+
import { type ModelID } from "../schema";
|
|
5
|
+
export declare const id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
6
|
+
export type Config = RouteDefaultsInput & ProviderAuthOption<"optional"> & {
|
|
7
|
+
readonly provider?: string;
|
|
8
|
+
readonly baseURL: string;
|
|
9
|
+
};
|
|
10
|
+
export type Settings = ProviderPackage.Settings & ({
|
|
11
|
+
readonly apiKey?: string;
|
|
12
|
+
readonly authToken?: never;
|
|
13
|
+
} | {
|
|
14
|
+
readonly apiKey?: never;
|
|
15
|
+
readonly authToken?: string;
|
|
16
|
+
}) & {
|
|
17
|
+
readonly baseURL: string;
|
|
18
|
+
readonly provider?: string;
|
|
19
|
+
};
|
|
20
|
+
export declare const routes: import("../route").Route<{
|
|
21
|
+
readonly model: string;
|
|
22
|
+
readonly messages: readonly ({
|
|
23
|
+
readonly role: "user";
|
|
24
|
+
readonly content: readonly ({
|
|
25
|
+
readonly type: "text";
|
|
26
|
+
readonly text: string;
|
|
27
|
+
readonly cache_control?: {
|
|
28
|
+
readonly type: "ephemeral";
|
|
29
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
30
|
+
} | undefined;
|
|
31
|
+
} | {
|
|
32
|
+
readonly type: "image";
|
|
33
|
+
readonly source: {
|
|
34
|
+
readonly type: "base64";
|
|
35
|
+
readonly media_type: string;
|
|
36
|
+
readonly data: string;
|
|
37
|
+
};
|
|
38
|
+
readonly cache_control?: {
|
|
39
|
+
readonly type: "ephemeral";
|
|
40
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
41
|
+
} | undefined;
|
|
42
|
+
} | {
|
|
43
|
+
readonly type: "tool_result";
|
|
44
|
+
readonly tool_use_id: string;
|
|
45
|
+
readonly content: string | readonly ({
|
|
46
|
+
readonly type: "text";
|
|
47
|
+
readonly text: string;
|
|
48
|
+
readonly cache_control?: {
|
|
49
|
+
readonly type: "ephemeral";
|
|
50
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
51
|
+
} | undefined;
|
|
52
|
+
} | {
|
|
53
|
+
readonly type: "image";
|
|
54
|
+
readonly source: {
|
|
55
|
+
readonly type: "base64";
|
|
56
|
+
readonly media_type: string;
|
|
57
|
+
readonly data: string;
|
|
58
|
+
};
|
|
59
|
+
readonly cache_control?: {
|
|
60
|
+
readonly type: "ephemeral";
|
|
61
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
62
|
+
} | undefined;
|
|
63
|
+
})[];
|
|
64
|
+
readonly is_error?: boolean | undefined;
|
|
65
|
+
readonly cache_control?: {
|
|
66
|
+
readonly type: "ephemeral";
|
|
67
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
68
|
+
} | undefined;
|
|
69
|
+
})[];
|
|
70
|
+
} | {
|
|
71
|
+
readonly role: "assistant";
|
|
72
|
+
readonly content: readonly ({
|
|
73
|
+
readonly type: "text";
|
|
74
|
+
readonly text: string;
|
|
75
|
+
readonly cache_control?: {
|
|
76
|
+
readonly type: "ephemeral";
|
|
77
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
78
|
+
} | undefined;
|
|
79
|
+
} | {
|
|
80
|
+
readonly type: "tool_use";
|
|
81
|
+
readonly id: string;
|
|
82
|
+
readonly name: string;
|
|
83
|
+
readonly input: unknown;
|
|
84
|
+
readonly cache_control?: {
|
|
85
|
+
readonly type: "ephemeral";
|
|
86
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
87
|
+
} | undefined;
|
|
88
|
+
} | {
|
|
89
|
+
readonly type: "server_tool_use";
|
|
90
|
+
readonly id: string;
|
|
91
|
+
readonly name: string;
|
|
92
|
+
readonly input: unknown;
|
|
93
|
+
readonly cache_control?: {
|
|
94
|
+
readonly type: "ephemeral";
|
|
95
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
96
|
+
} | undefined;
|
|
97
|
+
} | {
|
|
98
|
+
readonly type: "web_search_tool_result" | "code_execution_tool_result" | "web_fetch_tool_result";
|
|
99
|
+
readonly tool_use_id: string;
|
|
100
|
+
readonly content: unknown;
|
|
101
|
+
readonly cache_control?: {
|
|
102
|
+
readonly type: "ephemeral";
|
|
103
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
104
|
+
} | undefined;
|
|
105
|
+
} | {
|
|
106
|
+
readonly type: "thinking";
|
|
107
|
+
readonly thinking: string;
|
|
108
|
+
readonly signature?: string | undefined;
|
|
109
|
+
readonly cache_control?: {
|
|
110
|
+
readonly type: "ephemeral";
|
|
111
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
112
|
+
} | undefined;
|
|
113
|
+
})[];
|
|
114
|
+
} | {
|
|
115
|
+
readonly role: "system";
|
|
116
|
+
readonly content: readonly {
|
|
117
|
+
readonly type: "text";
|
|
118
|
+
readonly text: string;
|
|
119
|
+
readonly cache_control?: {
|
|
120
|
+
readonly type: "ephemeral";
|
|
121
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
122
|
+
} | undefined;
|
|
123
|
+
}[];
|
|
124
|
+
})[];
|
|
125
|
+
readonly stream: true;
|
|
126
|
+
readonly max_tokens: number;
|
|
127
|
+
readonly system?: readonly {
|
|
128
|
+
readonly type: "text";
|
|
129
|
+
readonly text: string;
|
|
130
|
+
readonly cache_control?: {
|
|
131
|
+
readonly type: "ephemeral";
|
|
132
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
133
|
+
} | undefined;
|
|
134
|
+
}[] | undefined;
|
|
135
|
+
readonly tools?: readonly {
|
|
136
|
+
readonly name: string;
|
|
137
|
+
readonly description: string;
|
|
138
|
+
readonly input_schema: {
|
|
139
|
+
readonly [x: string]: unknown;
|
|
140
|
+
};
|
|
141
|
+
readonly cache_control?: {
|
|
142
|
+
readonly type: "ephemeral";
|
|
143
|
+
readonly ttl?: "1h" | "5m" | undefined;
|
|
144
|
+
} | undefined;
|
|
145
|
+
}[] | undefined;
|
|
146
|
+
readonly tool_choice?: {
|
|
147
|
+
readonly type: "auto" | "any";
|
|
148
|
+
} | {
|
|
149
|
+
readonly type: "tool";
|
|
150
|
+
readonly name: string;
|
|
151
|
+
} | undefined;
|
|
152
|
+
readonly temperature?: number | undefined;
|
|
153
|
+
readonly top_p?: number | undefined;
|
|
154
|
+
readonly top_k?: number | undefined;
|
|
155
|
+
readonly stop_sequences?: readonly string[] | undefined;
|
|
156
|
+
readonly thinking?: {
|
|
157
|
+
readonly type: "enabled";
|
|
158
|
+
readonly budget_tokens: number;
|
|
159
|
+
} | {
|
|
160
|
+
readonly type: "adaptive";
|
|
161
|
+
readonly display?: "summarized" | "omitted" | undefined;
|
|
162
|
+
} | {
|
|
163
|
+
readonly type: "disabled";
|
|
164
|
+
} | undefined;
|
|
165
|
+
readonly output_config?: {
|
|
166
|
+
readonly effort?: string | undefined;
|
|
167
|
+
} | undefined;
|
|
168
|
+
}, import("../route/transport/http").HttpPrepared<string>>[];
|
|
169
|
+
export declare const configure: (input: Config) => {
|
|
170
|
+
id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
171
|
+
model: (modelID: string | ModelID) => import("..").Model;
|
|
172
|
+
configure: (input: Config) => /*elided*/ any;
|
|
173
|
+
};
|
|
174
|
+
export declare const provider: {
|
|
175
|
+
id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
176
|
+
configure: (input: Config) => {
|
|
177
|
+
id: string & import("effect/Brand").Brand<"LLM.ProviderID">;
|
|
178
|
+
model: (modelID: string | ModelID) => import("..").Model;
|
|
179
|
+
configure: /*elided*/ any;
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
export declare const model: ProviderPackage.Definition<Settings>["model"];
|
|
183
|
+
export * as AnthropicCompatible from "./anthropic-compatible";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { AnthropicMessages } from "../protocols/anthropic-messages";
|
|
2
|
+
import { Auth } from "../route/auth";
|
|
3
|
+
import { ProviderID } from "../schema";
|
|
4
|
+
export const id = ProviderID.make("anthropic-compatible");
|
|
5
|
+
export const routes = [AnthropicMessages.route];
|
|
6
|
+
const auth = (input) => {
|
|
7
|
+
if ("auth" in input && input.auth)
|
|
8
|
+
return input.auth;
|
|
9
|
+
return Auth.optional("apiKey" in input ? input.apiKey : undefined, "apiKey").pipe(Auth.header("x-api-key"));
|
|
10
|
+
};
|
|
11
|
+
export const configure = (input) => {
|
|
12
|
+
if (!input.baseURL)
|
|
13
|
+
throw new Error("Anthropic-compatible providers require a baseURL");
|
|
14
|
+
const provider = input.provider ?? "anthropic-compatible";
|
|
15
|
+
const { provider: _, baseURL, apiKey: _apiKey, auth: _auth, ...rest } = input;
|
|
16
|
+
const route = AnthropicMessages.route.with({
|
|
17
|
+
...rest,
|
|
18
|
+
provider,
|
|
19
|
+
endpoint: { baseURL },
|
|
20
|
+
auth: auth(input),
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
id: ProviderID.make(provider),
|
|
24
|
+
model: (modelID) => route.model({ id: modelID }),
|
|
25
|
+
configure,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export const provider = {
|
|
29
|
+
id,
|
|
30
|
+
configure,
|
|
31
|
+
};
|
|
32
|
+
export const model = (modelID, settings) => {
|
|
33
|
+
if (settings.apiKey !== undefined && settings.authToken !== undefined)
|
|
34
|
+
throw new Error("Anthropic-compatible apiKey cannot be combined with authToken");
|
|
35
|
+
return configure({
|
|
36
|
+
...(settings.authToken === undefined ? { apiKey: settings.apiKey } : { auth: Auth.bearer(settings.authToken) }),
|
|
37
|
+
baseURL: settings.baseURL,
|
|
38
|
+
headers: settings.headers === undefined ? undefined : { ...settings.headers },
|
|
39
|
+
http: settings.body === undefined ? undefined : { body: { ...settings.body } },
|
|
40
|
+
limits: settings.limits,
|
|
41
|
+
provider: settings.provider,
|
|
42
|
+
}).model(modelID);
|
|
43
|
+
};
|
|
44
|
+
export * as AnthropicCompatible from "./anthropic-compatible";
|