@opencode/ai 2.3.2 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/decision.d.ts +4 -3
- package/dist/decision.js +3 -2
- package/dist/embedding.d.ts +4 -3
- package/dist/embedding.js +3 -2
- package/dist/experimental/cloudflare-run-extraction.js +1 -2
- package/dist/experimental/realtime.d.ts +12 -43
- package/dist/experimental/realtime.js +14 -30
- package/dist/experimental.d.ts +1 -1
- package/dist/image.js +1 -1
- package/dist/media-model.d.ts +5 -4
- package/dist/media-model.js +5 -4
- package/dist/media.js +4 -17
- package/dist/protocols/anthropic-messages.js +7 -11
- package/dist/protocols/bedrock-converse.js +6 -6
- package/dist/protocols/cohere-chat.js +3 -3
- package/dist/protocols/gemini.js +6 -10
- package/dist/protocols/google-interactions.d.ts +299 -6
- package/dist/protocols/google-interactions.js +251 -33
- package/dist/protocols/meta-messages.js +3 -3
- package/dist/protocols/minimax-video.js +7 -1
- package/dist/protocols/mistral-chat.d.ts +1 -0
- package/dist/protocols/mistral-chat.js +3 -3
- package/dist/protocols/open-responses-continuation.js +2 -0
- package/dist/protocols/open-responses.d.ts +2 -0
- package/dist/protocols/open-responses.js +5 -3
- package/dist/protocols/openai-chat.d.ts +1 -0
- package/dist/protocols/openai-chat.js +6 -5
- package/dist/protocols/openai-responses.js +1 -0
- package/dist/protocols/shared.d.ts +12 -1
- package/dist/protocols/shared.js +56 -9
- package/dist/protocols/utils/gateway-protocol.js +1 -0
- package/dist/protocols/utils/gemini-generate-content.d.ts +4 -0
- package/dist/protocols/utils/gemini-generate-content.js +5 -1
- package/dist/protocols/utils/responses-compaction.js +3 -3
- package/dist/protocols/xai-responses.d.ts +2 -1
- package/dist/protocols/xai-responses.js +4 -1
- package/dist/provider-error.js +3 -1
- package/dist/providers/alibaba.js +2 -3
- package/dist/providers/assemblyai.d.ts +2 -4
- package/dist/providers/azure.js +5 -11
- package/dist/providers/baseten.js +2 -3
- package/dist/providers/byteplus.d.ts +2 -4
- package/dist/providers/cartesia.d.ts +2 -4
- package/dist/providers/cloudflare-ai-gateway.js +3 -3
- package/dist/providers/cloudflare-workers-ai.js +2 -3
- package/dist/providers/cohere.js +2 -3
- package/dist/providers/deepgram.d.ts +2 -4
- package/dist/providers/deepinfra.js +2 -3
- package/dist/providers/digitalocean.js +3 -5
- package/dist/providers/elevenlabs.d.ts +2 -4
- package/dist/providers/fireworks.js +2 -3
- package/dist/providers/google-vertex-interactions.d.ts +12 -3
- package/dist/providers/google-vertex-interactions.js +1 -2
- package/dist/providers/google-vertex.js +6 -2
- package/dist/providers/google.d.ts +20 -3
- package/dist/providers/google.js +7 -4
- package/dist/providers/openai-compatible.js +3 -5
- package/dist/providers/openai.js +4 -10
- package/dist/providers/opencode.d.ts +2 -4
- package/dist/providers/opencode.js +2 -3
- package/dist/providers/openrouter.js +3 -5
- package/dist/providers/typesafe-ai.d.ts +2 -4
- package/dist/providers/typesafe-ai.js +2 -3
- package/dist/providers/vercel-ai-gateway.js +3 -5
- package/dist/providers/volcengine.d.ts +2 -4
- package/dist/route/decision.d.ts +4 -7
- package/dist/route/decision.js +8 -10
- package/dist/route/embedding.d.ts +4 -7
- package/dist/route/embedding.js +4 -7
- package/dist/route/executor-service.d.ts +20 -3
- package/dist/route/executor-service.js +72 -2
- package/dist/route/executor.d.ts +5 -5
- package/dist/route/executor.js +68 -73
- package/dist/route/media-protocol.d.ts +2 -1
- package/dist/route/media-protocol.js +2 -10
- package/dist/route/media.d.ts +6 -8
- package/dist/route/media.js +8 -8
- package/dist/route/protocol.d.ts +2 -0
- package/dist/route/protocol.js +4 -1
- package/dist/route/transport/http.d.ts +1 -2
- package/dist/route/transport/http.js +5 -28
- package/dist/schema/errors.js +2 -1
- package/dist/schema/options.d.ts +1 -1
- package/dist/speech.js +1 -1
- package/dist/transcription.js +1 -1
- package/dist/video.js +1 -1
- package/package.json +3 -3
package/dist/decision.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import { Service, type Execute } from "./decision-client.js";
|
|
3
3
|
import { Media } from "./media.js";
|
|
4
|
-
import { AIError, type BaseRequest, type
|
|
4
|
+
import { AIError, type BaseRequest, type TransportOptions, ModelID, ProviderID, ProviderOptions, type OpenString } from "./schema/index.js";
|
|
5
5
|
export declare const DecisionTextPart: Schema.Struct<{
|
|
6
6
|
readonly type: Schema.Literal<"text">;
|
|
7
7
|
readonly text: Schema.String;
|
|
@@ -285,7 +285,8 @@ export declare class DecisionModel<Options extends DecisionOptions = DecisionOpt
|
|
|
285
285
|
readonly id: ModelID;
|
|
286
286
|
readonly provider: ProviderID;
|
|
287
287
|
readonly route: DecisionModelRoute<Options>;
|
|
288
|
-
|
|
288
|
+
/** Deployment headers, timeouts and `http` overlays, below each request's own. */
|
|
289
|
+
readonly transport?: TransportOptions;
|
|
289
290
|
readonly compatibility?: DecisionModelCompatibility;
|
|
290
291
|
/** Defaults under each request's `providerOptions`, such as options a provider package reads from its settings. */
|
|
291
292
|
readonly providerOptions?: ProviderOptions;
|
|
@@ -297,7 +298,7 @@ export declare namespace DecisionModel {
|
|
|
297
298
|
readonly id: ModelID;
|
|
298
299
|
readonly provider: ProviderID;
|
|
299
300
|
readonly route: DecisionModelRoute<Options>;
|
|
300
|
-
readonly
|
|
301
|
+
readonly transport?: TransportOptions;
|
|
301
302
|
readonly compatibility?: DecisionModelCompatibility;
|
|
302
303
|
readonly providerOptions?: ProviderOptions;
|
|
303
304
|
}
|
package/dist/decision.js
CHANGED
|
@@ -93,7 +93,8 @@ export class DecisionModel {
|
|
|
93
93
|
id;
|
|
94
94
|
provider;
|
|
95
95
|
route;
|
|
96
|
-
http
|
|
96
|
+
/** Deployment headers, timeouts and `http` overlays, below each request's own. */
|
|
97
|
+
transport;
|
|
97
98
|
compatibility;
|
|
98
99
|
/** Defaults under each request's `providerOptions`, such as options a provider package reads from its settings. */
|
|
99
100
|
providerOptions;
|
|
@@ -101,7 +102,7 @@ export class DecisionModel {
|
|
|
101
102
|
this.id = input.id;
|
|
102
103
|
this.provider = input.provider;
|
|
103
104
|
this.route = input.route;
|
|
104
|
-
this.
|
|
105
|
+
this.transport = input.transport;
|
|
105
106
|
this.compatibility = input.compatibility;
|
|
106
107
|
this.providerOptions = input.providerOptions;
|
|
107
108
|
}
|
package/dist/embedding.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
2
|
import { Service, type Execute } from "./embedding-client.js";
|
|
3
3
|
import { Media } from "./media.js";
|
|
4
|
-
import { type AIError, type BaseRequest, type
|
|
4
|
+
import { type AIError, type BaseRequest, type TransportOptions, ModelID, ProviderID, ProviderOptions } from "./schema/index.js";
|
|
5
5
|
/** One value to embed: text, or a media asset for hosts that embed images, audio or documents. */
|
|
6
6
|
export declare const EmbeddingValue: Schema.Union<readonly [Schema.String, Schema.decodeTo<Schema.declare<Media.Asset, Media.Asset>, Schema.Struct<{
|
|
7
7
|
readonly source: Schema.Union<readonly [Schema.Struct<{
|
|
@@ -53,7 +53,8 @@ export declare class EmbeddingModel<Options extends EmbeddingOptions = Embedding
|
|
|
53
53
|
readonly id: ModelID;
|
|
54
54
|
readonly provider: ProviderID;
|
|
55
55
|
readonly route: EmbeddingModelRoute<Options>;
|
|
56
|
-
|
|
56
|
+
/** Deployment headers, timeouts and `http` overlays, below each request's own. */
|
|
57
|
+
readonly transport?: TransportOptions;
|
|
57
58
|
/** Defaults under each request's `providerOptions`, such as options a provider package reads from its settings. */
|
|
58
59
|
readonly providerOptions?: ProviderOptions;
|
|
59
60
|
constructor(input: EmbeddingModel.Input<Options>);
|
|
@@ -64,7 +65,7 @@ export declare namespace EmbeddingModel {
|
|
|
64
65
|
readonly id: ModelID;
|
|
65
66
|
readonly provider: ProviderID;
|
|
66
67
|
readonly route: EmbeddingModelRoute<Options>;
|
|
67
|
-
readonly
|
|
68
|
+
readonly transport?: TransportOptions;
|
|
68
69
|
readonly providerOptions?: ProviderOptions;
|
|
69
70
|
}
|
|
70
71
|
interface MakeInput<Options extends EmbeddingOptions = EmbeddingOptions> extends Omit<Input<Options>, "id" | "provider"> {
|
package/dist/embedding.js
CHANGED
|
@@ -12,14 +12,15 @@ export class EmbeddingModel {
|
|
|
12
12
|
id;
|
|
13
13
|
provider;
|
|
14
14
|
route;
|
|
15
|
-
http
|
|
15
|
+
/** Deployment headers, timeouts and `http` overlays, below each request's own. */
|
|
16
|
+
transport;
|
|
16
17
|
/** Defaults under each request's `providerOptions`, such as options a provider package reads from its settings. */
|
|
17
18
|
providerOptions;
|
|
18
19
|
constructor(input) {
|
|
19
20
|
this.id = input.id;
|
|
20
21
|
this.provider = input.provider;
|
|
21
22
|
this.route = input.route;
|
|
22
|
-
this.
|
|
23
|
+
this.transport = input.transport;
|
|
23
24
|
this.providerOptions = input.providerOptions;
|
|
24
25
|
}
|
|
25
26
|
static make(input) {
|
|
@@ -73,14 +73,13 @@ export const protocol = MediaProtocol.inline(route, {
|
|
|
73
73
|
export const model = (input) => new ExtractionModel({
|
|
74
74
|
id: input.id,
|
|
75
75
|
provider: route.provider,
|
|
76
|
-
|
|
76
|
+
transport: input.transport,
|
|
77
77
|
route: MediaRoute.inline({
|
|
78
78
|
protocol,
|
|
79
79
|
endpoint: Endpoint.path(({ request }) => `/${request.model.id}`, {
|
|
80
80
|
baseURL: input.baseURL,
|
|
81
81
|
}),
|
|
82
82
|
auth: input.auth,
|
|
83
|
-
headers: input.headers,
|
|
84
83
|
}),
|
|
85
84
|
});
|
|
86
85
|
export const CloudflareRunExtraction = { protocol, model };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Effect, Redacted } from "effect";
|
|
2
2
|
import { RequestExecutor } from "../route/executor.js";
|
|
3
|
-
import { AIError } from "../schema/index.js";
|
|
3
|
+
import { AIError, type RequestTimeout } from "../schema/index.js";
|
|
4
4
|
export type RealtimeTransportInput = {
|
|
5
5
|
readonly type: "webrtc";
|
|
6
6
|
readonly sdp: string;
|
|
@@ -20,92 +20,61 @@ export type RealtimeTransport = {
|
|
|
20
20
|
};
|
|
21
21
|
export type RealtimeSession = Readonly<Record<string, unknown>>;
|
|
22
22
|
export interface RealtimeConnectInput {
|
|
23
|
+
readonly engine: RealtimeEngine;
|
|
23
24
|
readonly model: string;
|
|
24
25
|
readonly apiKey: Redacted.Redacted;
|
|
25
26
|
/** Where a GPT-Live session starts, OpenAI by default: another host of its API, such as OpenCode's. */
|
|
26
27
|
readonly baseURL?: string;
|
|
27
28
|
/** Headers that host needs besides the API key, such as OpenCode's workspace. */
|
|
28
29
|
readonly headers?: Readonly<Record<string, string>>;
|
|
30
|
+
readonly timeout?: RequestTimeout;
|
|
29
31
|
readonly session?: RealtimeSession;
|
|
30
32
|
readonly transport: RealtimeTransportInput;
|
|
31
33
|
}
|
|
32
34
|
export declare const engines: readonly [{
|
|
33
|
-
readonly family: "gpt-live";
|
|
34
|
-
readonly model: "gpt-live-1";
|
|
35
35
|
readonly id: "openai-live";
|
|
36
36
|
readonly provider: "openai";
|
|
37
|
+
readonly api: "live";
|
|
37
38
|
readonly transports: readonly ["webrtc"];
|
|
38
39
|
}, {
|
|
39
|
-
readonly family: "gpt-realtime";
|
|
40
|
-
readonly model: "gpt-realtime-2.1";
|
|
41
40
|
readonly id: "openai-realtime";
|
|
42
41
|
readonly provider: "openai";
|
|
42
|
+
readonly api: "realtime";
|
|
43
43
|
readonly transports: readonly ["webrtc", "websocket"];
|
|
44
44
|
}, {
|
|
45
|
-
readonly family: "grok-voice";
|
|
46
|
-
readonly model: "grok-voice-3.0";
|
|
47
45
|
readonly id: "xai-realtime";
|
|
48
46
|
readonly provider: "xai";
|
|
47
|
+
readonly api: "realtime";
|
|
49
48
|
readonly transports: readonly ["websocket"];
|
|
50
49
|
}, {
|
|
51
|
-
readonly family: "gemini-3.8-live";
|
|
52
|
-
readonly model: "gemini-3.8-live";
|
|
53
50
|
readonly id: "gemini-live";
|
|
54
51
|
readonly provider: "google";
|
|
52
|
+
readonly api: "bidi-generate-content";
|
|
55
53
|
readonly transports: readonly ["websocket"];
|
|
56
54
|
}];
|
|
57
|
-
export
|
|
58
|
-
readonly family: "gpt-live";
|
|
59
|
-
readonly model: "gpt-live-1";
|
|
60
|
-
readonly id: "openai-live";
|
|
61
|
-
readonly provider: "openai";
|
|
62
|
-
readonly transports: readonly ["webrtc"];
|
|
63
|
-
} | {
|
|
64
|
-
readonly family: "gpt-realtime";
|
|
65
|
-
readonly model: "gpt-realtime-2.1";
|
|
66
|
-
readonly id: "openai-realtime";
|
|
67
|
-
readonly provider: "openai";
|
|
68
|
-
readonly transports: readonly ["webrtc", "websocket"];
|
|
69
|
-
} | {
|
|
70
|
-
readonly family: "grok-voice";
|
|
71
|
-
readonly model: "grok-voice-3.0";
|
|
72
|
-
readonly id: "xai-realtime";
|
|
73
|
-
readonly provider: "xai";
|
|
74
|
-
readonly transports: readonly ["websocket"];
|
|
75
|
-
} | {
|
|
76
|
-
readonly family: "gemini-3.8-live";
|
|
77
|
-
readonly model: "gemini-3.8-live";
|
|
78
|
-
readonly id: "gemini-live";
|
|
79
|
-
readonly provider: "google";
|
|
80
|
-
readonly transports: readonly ["websocket"];
|
|
81
|
-
} | undefined;
|
|
55
|
+
export type RealtimeEngine = (typeof engines)[number];
|
|
82
56
|
export declare const connect: (input: RealtimeConnectInput) => Effect.Effect<RealtimeTransport, AIError, RequestExecutor.RequestExecutorService>;
|
|
83
57
|
export declare const Realtime: {
|
|
84
58
|
readonly engines: readonly [{
|
|
85
|
-
readonly family: "gpt-live";
|
|
86
|
-
readonly model: "gpt-live-1";
|
|
87
59
|
readonly id: "openai-live";
|
|
88
60
|
readonly provider: "openai";
|
|
61
|
+
readonly api: "live";
|
|
89
62
|
readonly transports: readonly ["webrtc"];
|
|
90
63
|
}, {
|
|
91
|
-
readonly family: "gpt-realtime";
|
|
92
|
-
readonly model: "gpt-realtime-2.1";
|
|
93
64
|
readonly id: "openai-realtime";
|
|
94
65
|
readonly provider: "openai";
|
|
66
|
+
readonly api: "realtime";
|
|
95
67
|
readonly transports: readonly ["webrtc", "websocket"];
|
|
96
68
|
}, {
|
|
97
|
-
readonly family: "grok-voice";
|
|
98
|
-
readonly model: "grok-voice-3.0";
|
|
99
69
|
readonly id: "xai-realtime";
|
|
100
70
|
readonly provider: "xai";
|
|
71
|
+
readonly api: "realtime";
|
|
101
72
|
readonly transports: readonly ["websocket"];
|
|
102
73
|
}, {
|
|
103
|
-
readonly family: "gemini-3.8-live";
|
|
104
|
-
readonly model: "gemini-3.8-live";
|
|
105
74
|
readonly id: "gemini-live";
|
|
106
75
|
readonly provider: "google";
|
|
76
|
+
readonly api: "bidi-generate-content";
|
|
107
77
|
readonly transports: readonly ["websocket"];
|
|
108
78
|
}];
|
|
109
|
-
readonly engine: typeof engine;
|
|
110
79
|
readonly connect: (input: RealtimeConnectInput) => Effect.Effect<RealtimeTransport, AIError, RequestExecutor.RequestExecutorService>;
|
|
111
80
|
};
|
|
@@ -6,36 +6,21 @@ import { encodeJson } from "../utils/json.js";
|
|
|
6
6
|
const OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
7
7
|
const XAI_BASE_URL = "https://api.x.ai/v1";
|
|
8
8
|
const GOOGLE_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
9
|
+
// The voice APIs this package speaks, by provider and models.dev API. Which models they start comes from models.dev.
|
|
9
10
|
export const engines = [
|
|
10
|
-
{
|
|
11
|
-
{
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
id: "openai-realtime",
|
|
15
|
-
provider: "openai",
|
|
16
|
-
transports: ["webrtc", "websocket"],
|
|
17
|
-
},
|
|
18
|
-
{ family: "grok-voice", model: "grok-voice-3.0", id: "xai-realtime", provider: "xai", transports: ["websocket"] },
|
|
19
|
-
{
|
|
20
|
-
family: "gemini-3.8-live",
|
|
21
|
-
model: "gemini-3.8-live",
|
|
22
|
-
id: "gemini-live",
|
|
23
|
-
provider: "google",
|
|
24
|
-
transports: ["websocket"],
|
|
25
|
-
},
|
|
11
|
+
{ id: "openai-live", provider: "openai", api: "live", transports: ["webrtc"] },
|
|
12
|
+
{ id: "openai-realtime", provider: "openai", api: "realtime", transports: ["webrtc", "websocket"] },
|
|
13
|
+
{ id: "xai-realtime", provider: "xai", api: "realtime", transports: ["websocket"] },
|
|
14
|
+
{ id: "gemini-live", provider: "google", api: "bidi-generate-content", transports: ["websocket"] },
|
|
26
15
|
];
|
|
27
|
-
// Matches the bare family name (`gpt-realtime`) and its versions (`gpt-realtime-2.1`, `gpt-realtime-mini`).
|
|
28
|
-
export const engine = (model) => engines.find((item) => model === item.family || model.startsWith(`${item.family}-`));
|
|
29
16
|
export const connect = Effect.fn("Realtime.connect")(function* (input) {
|
|
30
|
-
const resolved =
|
|
31
|
-
if (!resolved)
|
|
32
|
-
return yield* invalidRequest(`Unsupported voice model: ${input.model}`);
|
|
17
|
+
const resolved = input.engine;
|
|
33
18
|
if (!resolved.transports.some((type) => type === input.transport.type))
|
|
34
19
|
return yield* invalidRequest(`${input.model} only supports ${resolved.transports.join(", ")} transport`);
|
|
35
20
|
const session = { ...input.session, model: input.model };
|
|
36
21
|
switch (resolved.id) {
|
|
37
22
|
case "openai-live": {
|
|
38
|
-
const data = yield* post(resolved.id, `${input.baseURL ?? OPENAI_BASE_URL}/live/sessions`, input.apiKey, { session, transport: input.transport }, LiveSessionResponse, { headers: input.headers });
|
|
23
|
+
const data = yield* post(resolved.id, `${input.baseURL ?? OPENAI_BASE_URL}/live/sessions`, input.apiKey, { session, transport: input.transport }, LiveSessionResponse, { headers: input.headers, timeout: input.timeout });
|
|
39
24
|
return { type: "webrtc", sdp: data.transport.sdp };
|
|
40
25
|
}
|
|
41
26
|
case "openai-realtime": {
|
|
@@ -43,18 +28,18 @@ export const connect = Effect.fn("Realtime.connect")(function* (input) {
|
|
|
43
28
|
return yield* clientSecret(resolved.id, OPENAI_BASE_URL, input.model, input.apiKey, {
|
|
44
29
|
expires_after: { anchor: "created_at", seconds: 60 },
|
|
45
30
|
session,
|
|
46
|
-
});
|
|
31
|
+
}, input.timeout);
|
|
47
32
|
const form = new FormData();
|
|
48
33
|
form.append("sdp", input.transport.sdp);
|
|
49
34
|
form.append("session", encodeJson(session));
|
|
50
35
|
const query = input.transport.dcid === undefined ? "" : `?dcid=${input.transport.dcid}`;
|
|
51
|
-
const sdp = yield* post(resolved.id, `${OPENAI_BASE_URL}/realtime/calls${query}`, input.apiKey, form, Schema.String);
|
|
36
|
+
const sdp = yield* post(resolved.id, `${OPENAI_BASE_URL}/realtime/calls${query}`, input.apiKey, form, Schema.String, { timeout: input.timeout });
|
|
52
37
|
return { type: "webrtc", sdp };
|
|
53
38
|
}
|
|
54
39
|
case "xai-realtime":
|
|
55
40
|
return yield* clientSecret(resolved.id, XAI_BASE_URL, input.model, input.apiKey, {
|
|
56
41
|
expires_after: { seconds: 60 },
|
|
57
|
-
});
|
|
42
|
+
}, input.timeout);
|
|
58
43
|
case "gemini-live": {
|
|
59
44
|
const now = yield* Clock.currentTimeMillis;
|
|
60
45
|
const expiresAt = now + 60_000;
|
|
@@ -63,7 +48,7 @@ export const connect = Effect.fn("Realtime.connect")(function* (input) {
|
|
|
63
48
|
expireTime: new Date(now + 30 * 60_000).toISOString(),
|
|
64
49
|
newSessionExpireTime: new Date(expiresAt).toISOString(),
|
|
65
50
|
bidiGenerateContentSetup: { ...input.session, model: `models/${input.model}` },
|
|
66
|
-
}, AuthTokenResponse, { authHeader: "x-goog-api-key" });
|
|
51
|
+
}, AuthTokenResponse, { authHeader: "x-goog-api-key", timeout: input.timeout });
|
|
67
52
|
return {
|
|
68
53
|
type: "websocket",
|
|
69
54
|
url: "wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContentConstrained",
|
|
@@ -75,7 +60,6 @@ export const connect = Effect.fn("Realtime.connect")(function* (input) {
|
|
|
75
60
|
});
|
|
76
61
|
export const Realtime = {
|
|
77
62
|
engines,
|
|
78
|
-
engine,
|
|
79
63
|
connect,
|
|
80
64
|
};
|
|
81
65
|
const LiveSessionResponse = Schema.fromJsonString(Schema.Struct({
|
|
@@ -94,17 +78,17 @@ const AuthTokenResponse = Schema.fromJsonString(Schema.Struct({
|
|
|
94
78
|
const invalidRequest = (message) => new AIError({ reason: new InvalidRequestError({ message }) });
|
|
95
79
|
const post = (route, url, apiKey, body, response, options = {}) => Effect.gen(function* () {
|
|
96
80
|
const executor = yield* RequestExecutor.Service;
|
|
97
|
-
const res = yield* executor.execute(HttpClientRequest.post(url).pipe(HttpClientRequest.setHeaders(options.headers ?? {}), options.authHeader
|
|
81
|
+
const res = yield* RequestExecutor.withTimeout(executor.execute, options.timeout)(HttpClientRequest.post(url).pipe(HttpClientRequest.setHeaders(options.headers ?? {}), options.authHeader
|
|
98
82
|
? HttpClientRequest.setHeader(options.authHeader, Redacted.value(apiKey))
|
|
99
83
|
: HttpClientRequest.bearerToken(apiKey), body instanceof FormData
|
|
100
84
|
? HttpClientRequest.bodyFormData(body)
|
|
101
85
|
: HttpClientRequest.bodyText(encodeJson(body), "application/json")));
|
|
102
86
|
const http = RequestExecutor.responseHttp(res);
|
|
103
87
|
const fail = (message, cause, raw) => new AIError({ reason: new InvalidProviderOutputError({ route, message, body: raw, http, cause }) });
|
|
104
|
-
const text = yield*
|
|
88
|
+
const text = yield* RequestExecutor.responseText(res);
|
|
105
89
|
return yield* Schema.decodeEffect(response)(text).pipe(Effect.mapError((cause) => fail(`${route} returned an invalid response`, cause, text)));
|
|
106
90
|
});
|
|
107
|
-
const clientSecret = (route, baseURL, model, apiKey, body) => post(route, `${baseURL}/realtime/client_secrets`, apiKey, body, ClientSecretResponse).pipe(Effect.map((data) => ({
|
|
91
|
+
const clientSecret = (route, baseURL, model, apiKey, body, timeout) => post(route, `${baseURL}/realtime/client_secrets`, apiKey, body, ClientSecretResponse, { timeout }).pipe(Effect.map((data) => ({
|
|
108
92
|
type: "websocket",
|
|
109
93
|
url: `${baseURL.replace(/^http/, "ws")}/realtime?model=${encodeURIComponent(model)}`,
|
|
110
94
|
token: data.value,
|
package/dist/experimental.d.ts
CHANGED
|
@@ -9,4 +9,4 @@ export { Transcription } from "./transcription.js";
|
|
|
9
9
|
export { Extraction, ExtractionModel, ExtractionResponse } from "./experimental/extraction.js";
|
|
10
10
|
export type { ExtractionAnswerFor, ExtractionAnswersFor, ExtractionField, ExtractionFields, ExtractionModelOptions, ExtractionOptions, ExtractionRequest, ExtractionRequestInput, ExtractionResponseFor, ExtractionRoute, } from "./experimental/extraction.js";
|
|
11
11
|
export { Realtime } from "./experimental/realtime.js";
|
|
12
|
-
export type { RealtimeConnectInput, RealtimeSession, RealtimeTransport, RealtimeTransportInput, RealtimeTransportType, } from "./experimental/realtime.js";
|
|
12
|
+
export type { RealtimeConnectInput, RealtimeEngine, RealtimeSession, RealtimeTransport, RealtimeTransportInput, RealtimeTransportType, } from "./experimental/realtime.js";
|
package/dist/image.js
CHANGED
|
@@ -10,7 +10,7 @@ export class ImageModel extends MediaModel {
|
|
|
10
10
|
return new ImageModel({
|
|
11
11
|
id: input.id,
|
|
12
12
|
provider: route.protocol.provider,
|
|
13
|
-
|
|
13
|
+
transport: input.transport,
|
|
14
14
|
providerOptions: input.providerOptions,
|
|
15
15
|
route: composeRoute(route, input, collectResponse),
|
|
16
16
|
});
|
package/dist/media-model.d.ts
CHANGED
|
@@ -2,9 +2,9 @@ import { Effect } from "effect";
|
|
|
2
2
|
import { Endpoint } from "./route/endpoint.js";
|
|
3
3
|
import { MediaRoute } from "./route/media.js";
|
|
4
4
|
import type { MediaProtocol } from "./route/media-protocol.js";
|
|
5
|
-
import { AIError, ModelID, ProviderID, type
|
|
5
|
+
import { AIError, ModelID, ProviderID, type ProviderOptions, type TransportOptions } from "./schema/index.js";
|
|
6
6
|
/**
|
|
7
|
-
* What every media model carries: ids, the configured route, and deployment
|
|
7
|
+
* What every media model carries: ids, the configured route, and deployment transport overlays. Modality classes
|
|
8
8
|
* (`ImageModel`, `VideoModel`, `SpeechModel`, `TranscriptionModel`) extend it with their route type and a nominal
|
|
9
9
|
* marker so one cannot stand in for the other in requests.
|
|
10
10
|
*/
|
|
@@ -13,7 +13,8 @@ export declare class MediaModel<Route, Options> {
|
|
|
13
13
|
readonly id: ModelID;
|
|
14
14
|
readonly provider: ProviderID;
|
|
15
15
|
readonly route: Route;
|
|
16
|
-
|
|
16
|
+
/** Deployment headers, timeouts and `http` overlays, below each request's own. */
|
|
17
|
+
readonly transport?: TransportOptions;
|
|
17
18
|
/** Defaults under each request's `providerOptions`, such as options a provider package reads from its settings. */
|
|
18
19
|
readonly providerOptions?: ProviderOptions;
|
|
19
20
|
constructor(input: MediaModel.Input<Route>);
|
|
@@ -24,7 +25,7 @@ export declare namespace MediaModel {
|
|
|
24
25
|
readonly id: string | ModelID;
|
|
25
26
|
readonly provider: string | ProviderID;
|
|
26
27
|
readonly route: Route;
|
|
27
|
-
readonly
|
|
28
|
+
readonly transport?: TransportOptions;
|
|
28
29
|
readonly providerOptions?: ProviderOptions;
|
|
29
30
|
}
|
|
30
31
|
/** A protocol plus its canonical start path; `ModelInput.baseURL` overrides `baseURL` per deployment. */
|
package/dist/media-model.js
CHANGED
|
@@ -3,7 +3,7 @@ import { Endpoint } from "./route/endpoint.js";
|
|
|
3
3
|
import { MediaRoute } from "./route/media.js";
|
|
4
4
|
import { AIError, InvalidRequestError, ModelID, ProviderID, } from "./schema/index.js";
|
|
5
5
|
/**
|
|
6
|
-
* What every media model carries: ids, the configured route, and deployment
|
|
6
|
+
* What every media model carries: ids, the configured route, and deployment transport overlays. Modality classes
|
|
7
7
|
* (`ImageModel`, `VideoModel`, `SpeechModel`, `TranscriptionModel`) extend it with their route type and a nominal
|
|
8
8
|
* marker so one cannot stand in for the other in requests.
|
|
9
9
|
*/
|
|
@@ -11,14 +11,15 @@ export class MediaModel {
|
|
|
11
11
|
id;
|
|
12
12
|
provider;
|
|
13
13
|
route;
|
|
14
|
-
http
|
|
14
|
+
/** Deployment headers, timeouts and `http` overlays, below each request's own. */
|
|
15
|
+
transport;
|
|
15
16
|
/** Defaults under each request's `providerOptions`, such as options a provider package reads from its settings. */
|
|
16
17
|
providerOptions;
|
|
17
18
|
constructor(input) {
|
|
18
19
|
this.id = ModelID.make(input.id);
|
|
19
20
|
this.provider = ProviderID.make(input.provider);
|
|
20
21
|
this.route = input.route;
|
|
21
|
-
this.
|
|
22
|
+
this.transport = input.transport;
|
|
22
23
|
this.providerOptions = input.providerOptions;
|
|
23
24
|
}
|
|
24
25
|
}
|
|
@@ -36,7 +37,7 @@ const composition = (route, input) => ({
|
|
|
36
37
|
protocol: route.protocol,
|
|
37
38
|
endpoint: Endpoint.path(route.path, { baseURL: input.baseURL ?? route.baseURL }),
|
|
38
39
|
auth: input.auth,
|
|
39
|
-
headers: route.headers
|
|
40
|
+
headers: route.headers,
|
|
40
41
|
});
|
|
41
42
|
const isStreamInput = (route) => route.protocol.kind === "stream";
|
|
42
43
|
const isQueuedInput = (route) => route.protocol.kind === "queued";
|
package/dist/media.js
CHANGED
|
@@ -3,9 +3,9 @@ import { Effect, FileSystem, Result, Schema, SchemaGetter, Stream } from "effect
|
|
|
3
3
|
import { Base64 } from "effect/encoding";
|
|
4
4
|
import { HttpClientRequest } from "effect/http";
|
|
5
5
|
import { ProviderID } from "./schema/ids.js";
|
|
6
|
-
import { AIError,
|
|
6
|
+
import { AIError, InvalidRequestError } from "./schema/errors.js";
|
|
7
7
|
import { ProviderMetadata } from "./schema/options.js";
|
|
8
|
-
import { CurrentMiddleware,
|
|
8
|
+
import { CurrentMiddleware, responseBytes, responseStream, Service, } from "./route/executor-service.js";
|
|
9
9
|
import { detectMediaType, fileMediaType } from "./utils/media-type.js";
|
|
10
10
|
export { detectMediaType } from "./utils/media-type.js";
|
|
11
11
|
const OCTET_STREAM = "application/octet-stream";
|
|
@@ -193,27 +193,14 @@ const download = Effect.fn("Media.download")(function* (source, headers, middlew
|
|
|
193
193
|
const response = yield* executor
|
|
194
194
|
.execute(HttpClientRequest.get(source.url).pipe(HttpClientRequest.setHeaders(headers ?? {})))
|
|
195
195
|
.pipe(Effect.provideService(CurrentMiddleware, middleware));
|
|
196
|
-
|
|
197
|
-
reason: new InvalidProviderOutputError({
|
|
198
|
-
message: `Failed to read media from ${source.url}`,
|
|
199
|
-
http: responseHttp(response),
|
|
200
|
-
cause,
|
|
201
|
-
}),
|
|
202
|
-
})));
|
|
203
|
-
return new Uint8Array(buffer);
|
|
196
|
+
return yield* responseBytes(response);
|
|
204
197
|
});
|
|
205
198
|
const downloadStream = Effect.fn("Media.downloadStream")(function* (source, headers, middleware) {
|
|
206
199
|
const executor = yield* Service;
|
|
207
200
|
const response = yield* executor
|
|
208
201
|
.execute(HttpClientRequest.get(source.url).pipe(HttpClientRequest.setHeaders(headers ?? {})))
|
|
209
202
|
.pipe(Effect.provideService(CurrentMiddleware, middleware));
|
|
210
|
-
return response
|
|
211
|
-
reason: new InvalidProviderOutputError({
|
|
212
|
-
message: `Failed to read media from ${source.url}`,
|
|
213
|
-
http: responseHttp(response),
|
|
214
|
-
cause,
|
|
215
|
-
}),
|
|
216
|
-
})));
|
|
203
|
+
return responseStream(response);
|
|
217
204
|
});
|
|
218
205
|
export const from = (source, options) => new Asset({ ...options, source });
|
|
219
206
|
export const bytes = (data, mediaType, options) => from({ type: "bytes", data, mediaType: mediaType ?? detectMediaType(data) ?? OCTET_STREAM }, options);
|
|
@@ -12,7 +12,7 @@ import { AIError, CacheTTL, LLMEvent, mergeJsonRecords, Usage, } from "../schema
|
|
|
12
12
|
import { JsonObject, knownString, optionalArray, optionalNull, ProviderShared, withUntypedFrames } from "./shared.js";
|
|
13
13
|
import { classifyProviderFailure } from "../provider-error.js";
|
|
14
14
|
import { effortUpdate, resolveEffortUpdates } from "../effort-updates.js";
|
|
15
|
-
import { addsTools,
|
|
15
|
+
import { addsTools, toolsUpdate } from "../tool-updates.js";
|
|
16
16
|
import * as Cache from "./utils/cache.js";
|
|
17
17
|
import { claudeVersion, supportsThinkingBlockBinding, THINKING_BINDING_BETA } from "./utils/claude-model.js";
|
|
18
18
|
import { Lifecycle } from "./utils/lifecycle.js";
|
|
@@ -643,7 +643,7 @@ const lowerWrappedSystemUpdate = Effect.fnUntraced(function* (message, breakpoin
|
|
|
643
643
|
const part = yield* ProviderShared.wrappedSystemUpdate("Anthropic Messages", message);
|
|
644
644
|
return { type: "text", text: part.text, cache_control: cacheControl(breakpoints, part.cache) };
|
|
645
645
|
});
|
|
646
|
-
const lowerToolsUpdate = Effect.fnUntraced(function* (message, update,
|
|
646
|
+
const lowerToolsUpdate = Effect.fnUntraced(function* (message, update, breakpoints) {
|
|
647
647
|
const text = message.content.length === 0 ? [] : (yield* lowerNativeSystemUpdate(message, breakpoints)).content;
|
|
648
648
|
return {
|
|
649
649
|
role: "system",
|
|
@@ -652,7 +652,7 @@ const lowerToolsUpdate = Effect.fnUntraced(function* (message, update, withdrawn
|
|
|
652
652
|
type: "tool_addition",
|
|
653
653
|
tool: { type: "tool_definition", definition: lowerTool(breakpoints, tool) },
|
|
654
654
|
})),
|
|
655
|
-
...
|
|
655
|
+
...update.remove.map((name) => ({
|
|
656
656
|
type: "tool_removal",
|
|
657
657
|
tool: { type: "tool_reference", name },
|
|
658
658
|
})),
|
|
@@ -674,8 +674,6 @@ const lowerMessages = Effect.fnUntraced(function* (request, breakpoints) {
|
|
|
674
674
|
// user turn, the nearest spot where Anthropic accepts a native system message.
|
|
675
675
|
const holdUpdates = supportsNativeSystemUpdates(request);
|
|
676
676
|
const held = [];
|
|
677
|
-
// The tools declared so far, so each tool update can name the flattened tools it takes away.
|
|
678
|
-
let declared = request.tools;
|
|
679
677
|
const releaseHeld = Effect.fnUntraced(function* () {
|
|
680
678
|
const native = acceptsNativeSystemAfter(messages.findLast((message) => message.role !== "system"));
|
|
681
679
|
for (const update of held.splice(0)) {
|
|
@@ -683,9 +681,7 @@ const lowerMessages = Effect.fnUntraced(function* (request, breakpoints) {
|
|
|
683
681
|
if (tools !== undefined) {
|
|
684
682
|
if (!native)
|
|
685
683
|
return yield* invalid("Anthropic Messages tool updates must follow a user message or tool result");
|
|
686
|
-
const
|
|
687
|
-
const lowered = yield* lowerToolsUpdate(update, tools, withdrawnTools(declared, next), breakpoints);
|
|
688
|
-
declared = next;
|
|
684
|
+
const lowered = yield* lowerToolsUpdate(update, tools, breakpoints);
|
|
689
685
|
if (lowered.content.length > 0)
|
|
690
686
|
messages.push(lowered);
|
|
691
687
|
continue;
|
|
@@ -866,8 +862,8 @@ const fromRequest = Effect.fn("AnthropicMessages.fromRequest")(function* (reques
|
|
|
866
862
|
// messages. Tools live highest in the cache hierarchy, so when callers
|
|
867
863
|
// over-mark we keep their tool hints and shed the message-tail ones first.
|
|
868
864
|
const breakpoints = Cache.newBreakpoints(ANTHROPIC_BREAKPOINT_CAP);
|
|
869
|
-
const
|
|
870
|
-
const tools =
|
|
865
|
+
const definitions = ProviderShared.flattenTools(updates.request.tools);
|
|
866
|
+
const tools = definitions.length === 0 ? undefined : definitions.map((tool) => lowerTool(breakpoints, tool));
|
|
871
867
|
// Anthropic rejects tool_choice when no tool is declared; "none" is only meaningful with tools present.
|
|
872
868
|
const declared = tools !== undefined || addsTools(request);
|
|
873
869
|
const toolChoice = !declared || !request.toolChoice ? undefined : yield* lowerToolChoice(request.toolChoice);
|
|
@@ -879,7 +875,7 @@ const fromRequest = Effect.fn("AnthropicMessages.fromRequest")(function* (reques
|
|
|
879
875
|
text: part.text,
|
|
880
876
|
cache_control: cacheControl(breakpoints, part.cache),
|
|
881
877
|
}));
|
|
882
|
-
const messages = yield* lowerMessages(
|
|
878
|
+
const messages = yield* lowerMessages(updates.request, breakpoints);
|
|
883
879
|
yield* Cache.warnDropped(breakpoints, "Anthropic Messages", ANTHROPIC_BREAKPOINT_CAP);
|
|
884
880
|
const output_config = updates.effort === undefined && format === undefined ? undefined : { effort: updates.effort, format };
|
|
885
881
|
const maxTokens = request.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
@@ -233,8 +233,8 @@ const removeEmptyToolInputKeys = (input) => {
|
|
|
233
233
|
const lowerToolCall = (part, normalizeID) => ({
|
|
234
234
|
toolUse: {
|
|
235
235
|
toolUseId: normalizeID(part.id),
|
|
236
|
-
//
|
|
237
|
-
name: part.name.
|
|
236
|
+
// Converse rejects replayed names over 64 characters; flattenRequest already fixed invalid characters.
|
|
237
|
+
name: part.name.slice(0, 64),
|
|
238
238
|
input: removeEmptyToolInputKeys(part.input),
|
|
239
239
|
},
|
|
240
240
|
});
|
|
@@ -459,7 +459,7 @@ const applyThinkingBindingDefault = (request, thinking) => {
|
|
|
459
459
|
};
|
|
460
460
|
const fromRequest = Effect.fn("BedrockConverse.fromRequest")(function* (request) {
|
|
461
461
|
const toolChoice = request.toolChoice ? yield* lowerToolChoice(request.toolChoice) : undefined;
|
|
462
|
-
const
|
|
462
|
+
const definitions = ProviderShared.flattenTools(request.tools);
|
|
463
463
|
const options = yield* decodeOptions(request.providerOptions ?? {});
|
|
464
464
|
const maxTokens = isNova2(request.model) && (options.effort === "high" || isHighReasoningEffort(request.http?.body))
|
|
465
465
|
? undefined
|
|
@@ -486,17 +486,17 @@ const fromRequest = Effect.fn("BedrockConverse.fromRequest")(function* (request)
|
|
|
486
486
|
// tools → system → messages order to favour the highest-impact prefixes.
|
|
487
487
|
const breakpoints = BedrockCache.breakpoints(request.model);
|
|
488
488
|
const toolConfig = (() => {
|
|
489
|
-
if (
|
|
489
|
+
if (definitions.length === 0)
|
|
490
490
|
return undefined;
|
|
491
491
|
return {
|
|
492
|
-
tools: lowerTools(breakpoints,
|
|
492
|
+
tools: lowerTools(breakpoints, definitions),
|
|
493
493
|
// Converse has no native "none". Keep definitions stable for prompt
|
|
494
494
|
// caching and omit only the unsupported choice.
|
|
495
495
|
toolChoice,
|
|
496
496
|
};
|
|
497
497
|
})();
|
|
498
498
|
const system = lowerSystem(breakpoints, request.system);
|
|
499
|
-
const messages = yield* lowerMessages(
|
|
499
|
+
const messages = yield* lowerMessages(request, breakpoints);
|
|
500
500
|
yield* warnDropped(breakpoints, "Bedrock Converse", BedrockCache.BEDROCK_BREAKPOINT_CAP);
|
|
501
501
|
const inferenceConfig = (() => {
|
|
502
502
|
if (maxTokens === undefined &&
|
|
@@ -125,7 +125,7 @@ const Event = Schema.Union([
|
|
|
125
125
|
const TOOL_CHOICE = { auto: undefined, none: "NONE", required: "REQUIRED", tool: "REQUIRED" };
|
|
126
126
|
const fromRequest = Effect.fn("CohereChat.fromRequest")(function* (request) {
|
|
127
127
|
const options = yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(Options))(request.providerOptions ?? {});
|
|
128
|
-
const
|
|
128
|
+
const definitions = ProviderShared.flattenTools(request.tools);
|
|
129
129
|
const messages = request.system.length
|
|
130
130
|
? [
|
|
131
131
|
{
|
|
@@ -136,7 +136,7 @@ const fromRequest = Effect.fn("CohereChat.fromRequest")(function* (request) {
|
|
|
136
136
|
},
|
|
137
137
|
]
|
|
138
138
|
: [];
|
|
139
|
-
for (const message of
|
|
139
|
+
for (const message of request.messages) {
|
|
140
140
|
if (message.role === "system") {
|
|
141
141
|
messages.push({ role: "user", content: (yield* ProviderShared.wrappedSystemUpdate("Cohere Chat", message)).text });
|
|
142
142
|
continue;
|
|
@@ -187,7 +187,7 @@ const fromRequest = Effect.fn("CohereChat.fromRequest")(function* (request) {
|
|
|
187
187
|
});
|
|
188
188
|
}
|
|
189
189
|
const selected = request.toolChoice?.type === "tool" ? request.toolChoice.name : undefined;
|
|
190
|
-
const tools = selected === undefined ?
|
|
190
|
+
const tools = selected === undefined ? definitions : definitions.filter((tool) => tool.name === selected);
|
|
191
191
|
if (selected !== undefined && tools.length === 0)
|
|
192
192
|
return yield* ProviderShared.invalidRequest("Cohere Chat tool choice must name an available tool");
|
|
193
193
|
if (tools.some((tool) => tool.native !== undefined))
|