@opencode/ai 0.0.0-dev-21756 → 0.0.0-dev-21761
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.
|
@@ -21,6 +21,10 @@ export type RealtimeSession = Readonly<Record<string, unknown>>;
|
|
|
21
21
|
export interface RealtimeConnectInput {
|
|
22
22
|
readonly model: string;
|
|
23
23
|
readonly apiKey: Redacted.Redacted;
|
|
24
|
+
/** Where a GPT-Live session starts, OpenAI by default: another host of its API, such as OpenCode's. */
|
|
25
|
+
readonly baseURL?: string;
|
|
26
|
+
/** Headers that host needs besides the API key, such as OpenCode's workspace. */
|
|
27
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
24
28
|
readonly session?: RealtimeSession;
|
|
25
29
|
readonly transport: RealtimeTransportInput;
|
|
26
30
|
}
|
|
@@ -35,7 +35,7 @@ export const connect = Effect.fn("Realtime.connect")(function* (input) {
|
|
|
35
35
|
const session = { ...input.session, model: input.model };
|
|
36
36
|
switch (resolved.id) {
|
|
37
37
|
case "openai-live": {
|
|
38
|
-
const data = yield* post(resolved.id, `${OPENAI_BASE_URL}/live/sessions`, input.apiKey, { session, transport: input.transport }, LiveSessionResponse);
|
|
38
|
+
const data = yield* post(resolved.id, `${input.baseURL ?? OPENAI_BASE_URL}/live/sessions`, input.apiKey, { session, transport: input.transport }, LiveSessionResponse, { headers: input.headers });
|
|
39
39
|
return { type: "webrtc", sdp: data.transport.sdp };
|
|
40
40
|
}
|
|
41
41
|
case "openai-realtime": {
|
|
@@ -63,7 +63,7 @@ export const connect = Effect.fn("Realtime.connect")(function* (input) {
|
|
|
63
63
|
expireTime: new Date(now + 30 * 60_000).toISOString(),
|
|
64
64
|
newSessionExpireTime: new Date(expiresAt).toISOString(),
|
|
65
65
|
bidiGenerateContentSetup: { ...input.session, model: `models/${input.model}` },
|
|
66
|
-
}, AuthTokenResponse, "x-goog-api-key");
|
|
66
|
+
}, AuthTokenResponse, { authHeader: "x-goog-api-key" });
|
|
67
67
|
return {
|
|
68
68
|
type: "websocket",
|
|
69
69
|
url: "wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContentConstrained",
|
|
@@ -92,10 +92,10 @@ const AuthTokenResponse = Schema.fromJsonString(Schema.Struct({
|
|
|
92
92
|
name: Schema.String,
|
|
93
93
|
}));
|
|
94
94
|
const invalidRequest = (message) => new AIError({ reason: new InvalidRequestError({ message }) });
|
|
95
|
-
const post = (route, url, apiKey, body, response,
|
|
95
|
+
const post = (route, url, apiKey, body, response, options = {}) => Effect.gen(function* () {
|
|
96
96
|
const executor = yield* RequestExecutor.Service;
|
|
97
|
-
const res = yield* executor.execute(HttpClientRequest.post(url).pipe(authHeader
|
|
98
|
-
? HttpClientRequest.setHeader(authHeader, Redacted.value(apiKey))
|
|
97
|
+
const res = yield* executor.execute(HttpClientRequest.post(url).pipe(HttpClientRequest.setHeaders(options.headers ?? {}), options.authHeader
|
|
98
|
+
? HttpClientRequest.setHeader(options.authHeader, Redacted.value(apiKey))
|
|
99
99
|
: HttpClientRequest.bearerToken(apiKey), body instanceof FormData
|
|
100
100
|
? HttpClientRequest.bodyFormData(body)
|
|
101
101
|
: HttpClientRequest.bodyText(encodeJson(body), "application/json")));
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Effect, Schema, Stream } from "effect";
|
|
1
|
+
import { Effect, Schema, SchemaGetter, Stream } from "effect";
|
|
2
2
|
import { ImageFinishEvent, ImageModel, ImageOutputEvent, ImagePartialEvent, } from "../image.js";
|
|
3
3
|
import { Media } from "../media.js";
|
|
4
4
|
import { classifyProviderFailure } from "../provider-error.js";
|
|
@@ -38,6 +38,20 @@ const OpenAIImageResponse = Schema.Struct({
|
|
|
38
38
|
// ---------------------------------------------------------------------------
|
|
39
39
|
// 3. Streaming event schema
|
|
40
40
|
// ---------------------------------------------------------------------------
|
|
41
|
+
const EVENT_TYPES = new Set([
|
|
42
|
+
"image_generation.partial_image",
|
|
43
|
+
"image_edit.partial_image",
|
|
44
|
+
"image_generation.completed",
|
|
45
|
+
"image_edit.completed",
|
|
46
|
+
"error",
|
|
47
|
+
]);
|
|
48
|
+
/** OpenAI may add stream event types; frames outside `EVENT_TYPES` are ignored, while a malformed known type still fails decoding. */
|
|
49
|
+
const UnknownEvent = Schema.Struct({
|
|
50
|
+
type: Schema.String.check(Schema.makeFilter((type) => !EVENT_TYPES.has(type))),
|
|
51
|
+
}).pipe(Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown"), rawType: Schema.String }), {
|
|
52
|
+
decode: SchemaGetter.transform((event) => ({ type: "unknown", rawType: event.type })),
|
|
53
|
+
encode: SchemaGetter.forbidden(() => "Unknown OpenAI Images stream events are decode-only"),
|
|
54
|
+
}));
|
|
41
55
|
const StreamEvent = Schema.Union([
|
|
42
56
|
Schema.Struct({
|
|
43
57
|
type: Schema.Literals(["image_generation.partial_image", "image_edit.partial_image"]),
|
|
@@ -57,6 +71,7 @@ const StreamEvent = Schema.Union([
|
|
|
57
71
|
type: Schema.Literal("error"),
|
|
58
72
|
error: Schema.optional(Schema.Struct({ message: Schema.optional(Schema.String) })),
|
|
59
73
|
}),
|
|
74
|
+
UnknownEvent,
|
|
60
75
|
]);
|
|
61
76
|
const decodeEvent = route.decodeFrame(StreamEvent);
|
|
62
77
|
const decodeDocument = Schema.decodeUnknownEffect(Schema.fromJsonString(OpenAIImageResponse));
|
|
@@ -165,6 +180,8 @@ const eventImage = (frame, label, data, format, size) => MediaInput.decodedAsset
|
|
|
165
180
|
});
|
|
166
181
|
const onEvent = Effect.fnUntraced(function* (state, frame) {
|
|
167
182
|
const event = yield* decodeEvent(frame);
|
|
183
|
+
if (event.type === "unknown")
|
|
184
|
+
return [state, []];
|
|
168
185
|
if (event.type === "error")
|
|
169
186
|
return yield* new AIError({
|
|
170
187
|
reason: classifyProviderFailure({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-21761",
|
|
4
4
|
"name": "@opencode/ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@clack/prompts": "1.0.0-alpha.1",
|
|
37
37
|
"@effect/platform-node": "4.0.1",
|
|
38
|
-
"@opencode/http-recorder": "0.0.0-dev-
|
|
38
|
+
"@opencode/http-recorder": "0.0.0-dev-21761",
|
|
39
39
|
"@tsconfig/bun": "1.0.9",
|
|
40
40
|
"@types/bun": "1.4.3",
|
|
41
41
|
"@typescript/native": "npm:typescript@7.1.0-beta",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@aws-sdk/credential-providers": "3.1057.0",
|
|
46
46
|
"@smithy/eventstream-codec": "4.2.14",
|
|
47
47
|
"@smithy/util-utf8": "4.2.2",
|
|
48
|
-
"@opencode/schema": "0.0.0-dev-
|
|
48
|
+
"@opencode/schema": "0.0.0-dev-21761",
|
|
49
49
|
"aws4fetch": "1.0.20",
|
|
50
50
|
"effect": "4.0.1",
|
|
51
51
|
"google-auth-library": "10.5.0"
|