@opencode/ai 0.0.0-dev-1297 → 0.0.0-dev-1299
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 +27 -3
- package/dist/generation.d.ts +10 -10
- package/dist/generation.js +8 -14
- package/dist/image-client.d.ts +5 -1
- package/dist/image-client.js +1 -1
- package/dist/image.d.ts +10 -89
- package/dist/image.js +2 -9
- package/dist/index.d.ts +13 -18
- package/dist/index.js +17 -15
- package/dist/media-client.d.ts +5 -1
- package/dist/media.d.ts +9 -1
- package/dist/media.js +14 -7
- package/dist/protocols/assemblyai-transcription.js +7 -6
- package/dist/protocols/cartesia-speech.d.ts +0 -8
- package/dist/protocols/deepgram-speech.d.ts +0 -8
- package/dist/protocols/elevenlabs-speech.d.ts +0 -8
- package/dist/protocols/google-interactions-video.js +3 -3
- package/dist/protocols/google-speech.d.ts +0 -8
- package/dist/protocols/google-transcription.d.ts +0 -8
- package/dist/protocols/minimax-video.js +11 -11
- package/dist/protocols/openai-images.d.ts +0 -8
- package/dist/protocols/openai-speech.d.ts +0 -8
- package/dist/protocols/openai-transcription.d.ts +0 -8
- package/dist/protocols/openrouter-video.js +7 -7
- package/dist/protocols/seedance-video.js +5 -5
- package/dist/protocols/xai-video.js +6 -3
- package/dist/public.d.ts +2 -1
- package/dist/route/executor-service.d.ts +14 -1
- package/dist/route/executor-service.js +28 -1
- package/dist/route/executor.d.ts +5 -2
- package/dist/route/executor.js +20 -5
- package/dist/route/media-protocol.d.ts +12 -7
- package/dist/route/media-protocol.js +11 -4
- package/dist/route/media.js +12 -3
- package/dist/speech-client.d.ts +5 -5
- package/dist/speech.d.ts +7 -88
- package/dist/speech.js +1 -3
- package/dist/transcription-client.d.ts +5 -5
- package/dist/transcription.d.ts +7 -88
- package/dist/transcription.js +1 -3
- package/dist/video-client.js +1 -1
- package/dist/video.d.ts +3 -1
- package/package.json +3 -3
package/dist/media.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Effect, FileSystem, Schema, Stream } from "effect";
|
|
|
3
3
|
import { ProviderID } from "./schema/ids.js";
|
|
4
4
|
import { AIError } from "./schema/errors.js";
|
|
5
5
|
import { ProviderMetadata } from "./schema/options.js";
|
|
6
|
-
import { Service } from "./route/executor-service.js";
|
|
6
|
+
import { type HttpMiddleware, Service } from "./route/executor-service.js";
|
|
7
7
|
export { detectMediaType } from "./utils/media-type.js";
|
|
8
8
|
export declare const Source: Schema.Union<readonly [Schema.Struct<{
|
|
9
9
|
readonly type: Schema.Literal<"bytes">;
|
|
@@ -60,6 +60,8 @@ export declare class Asset {
|
|
|
60
60
|
readonly providerMetadata?: ProviderMetadata;
|
|
61
61
|
/** Transient download credentials for `url` sources; see `Asset.Input.headers`. */
|
|
62
62
|
readonly headers?: Record<string, string>;
|
|
63
|
+
/** Middleware for downloading a `url` source; see `Asset.Input.middleware`. */
|
|
64
|
+
readonly middleware?: HttpMiddleware;
|
|
63
65
|
constructor(input: Asset.Input);
|
|
64
66
|
/** Inline payload without effects, for protocols that embed base64 or data URLs directly. */
|
|
65
67
|
inline(): Inline | undefined;
|
|
@@ -122,6 +124,12 @@ export declare namespace Asset {
|
|
|
122
124
|
* credentials and cannot be downloaded again after a round-trip. Call `materialize()` before persisting.
|
|
123
125
|
*/
|
|
124
126
|
readonly headers?: Record<string, string>;
|
|
127
|
+
/**
|
|
128
|
+
* Middleware for downloading a `url` source, runtime-only like `headers`. A download never uses the caller's
|
|
129
|
+
* scoped middleware (`RequestExecutor.withMiddleware`), so hooks that add provider credentials can't reach another
|
|
130
|
+
* host. Routes set this to that middleware only for result URLs on the provider's origin.
|
|
131
|
+
*/
|
|
132
|
+
readonly middleware?: HttpMiddleware;
|
|
125
133
|
}
|
|
126
134
|
}
|
|
127
135
|
/** JSON form of an asset: the serializable `Source` plus caller-supplied metadata. `bytes` sources encode as base64. */
|
package/dist/media.js
CHANGED
|
@@ -5,7 +5,7 @@ import { HttpClientRequest } from "effect/http";
|
|
|
5
5
|
import { ProviderID } from "./schema/ids.js";
|
|
6
6
|
import { AIError, InvalidProviderOutputError, InvalidRequestError } from "./schema/errors.js";
|
|
7
7
|
import { ProviderMetadata } from "./schema/options.js";
|
|
8
|
-
import { responseHttp, Service } from "./route/executor-service.js";
|
|
8
|
+
import { CurrentMiddleware, responseHttp, 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";
|
|
@@ -78,6 +78,8 @@ export class Asset {
|
|
|
78
78
|
providerMetadata;
|
|
79
79
|
/** Transient download credentials for `url` sources; see `Asset.Input.headers`. */
|
|
80
80
|
headers;
|
|
81
|
+
/** Middleware for downloading a `url` source; see `Asset.Input.middleware`. */
|
|
82
|
+
middleware;
|
|
81
83
|
// Derived payload forms are cached on the instance because every protocol lowering re-reads the same payload. The
|
|
82
84
|
// cache is check-then-set (concurrent first reads of a `url` source may both download) and is never observable
|
|
83
85
|
// through `source`, so round-tripping through `Media.from(asset.source)` stays lossless.
|
|
@@ -94,6 +96,7 @@ export class Asset {
|
|
|
94
96
|
this.expiresAt = input.source.type === "url" ? input.source.expiresAt : undefined;
|
|
95
97
|
this.providerMetadata = input.providerMetadata;
|
|
96
98
|
this.headers = input.source.type === "url" ? input.headers : undefined;
|
|
99
|
+
this.middleware = input.source.type === "url" ? input.middleware : undefined;
|
|
97
100
|
}
|
|
98
101
|
/** Inline payload without effects, for protocols that embed base64 or data URLs directly. */
|
|
99
102
|
inline() {
|
|
@@ -116,7 +119,7 @@ export class Asset {
|
|
|
116
119
|
return Effect.fail(invalid(`Cannot materialize provider ref ${source.provider}:${source.id}`));
|
|
117
120
|
const decoded = source.type === "base64"
|
|
118
121
|
? Effect.fromResult(Base64.decode(source.data)).pipe(Effect.mapError((cause) => invalid(`Media asset contains invalid base64 data`, cause)))
|
|
119
|
-
: download(source, this.headers);
|
|
122
|
+
: download(source, this.headers, this.middleware);
|
|
120
123
|
return decoded.pipe(Effect.tap((data) => Effect.sync(() => (this.#bytes = data))));
|
|
121
124
|
});
|
|
122
125
|
}
|
|
@@ -125,7 +128,7 @@ export class Asset {
|
|
|
125
128
|
const source = this.source;
|
|
126
129
|
if (source.type !== "url" || this.#bytes !== undefined)
|
|
127
130
|
return Stream.fromEffect(this.bytes());
|
|
128
|
-
return Stream.unwrap(downloadStream(source, this.headers));
|
|
131
|
+
return Stream.unwrap(downloadStream(source, this.headers, this.middleware));
|
|
129
132
|
}
|
|
130
133
|
base64() {
|
|
131
134
|
return Effect.suspend(() => {
|
|
@@ -185,9 +188,11 @@ export const AssetSchema = AssetEncoded.pipe(Schema.decodeTo(AssetInstance, {
|
|
|
185
188
|
export const ImageAssetSchema = AssetInstance.check(Schema.makeFilter((asset) => asset.kind === "image" ||
|
|
186
189
|
((asset.source.type === "url" || asset.source.type === "ref") && asset.source.mediaType === undefined) ||
|
|
187
190
|
`expected an image, got ${asset.mediaType}`)).pipe(Schema.brand("Media.ImageAsset"));
|
|
188
|
-
const download = Effect.fn("Media.download")(function* (source, headers) {
|
|
191
|
+
const download = Effect.fn("Media.download")(function* (source, headers, middleware) {
|
|
189
192
|
const executor = yield* Service;
|
|
190
|
-
const response = yield* executor
|
|
193
|
+
const response = yield* executor
|
|
194
|
+
.execute(HttpClientRequest.get(source.url).pipe(HttpClientRequest.setHeaders(headers ?? {})))
|
|
195
|
+
.pipe(Effect.provideService(CurrentMiddleware, middleware));
|
|
191
196
|
const buffer = yield* response.arrayBuffer.pipe(Effect.mapError((cause) => new AIError({
|
|
192
197
|
reason: new InvalidProviderOutputError({
|
|
193
198
|
message: `Failed to read media from ${source.url}`,
|
|
@@ -197,9 +202,11 @@ const download = Effect.fn("Media.download")(function* (source, headers) {
|
|
|
197
202
|
})));
|
|
198
203
|
return new Uint8Array(buffer);
|
|
199
204
|
});
|
|
200
|
-
const downloadStream = Effect.fn("Media.downloadStream")(function* (source, headers) {
|
|
205
|
+
const downloadStream = Effect.fn("Media.downloadStream")(function* (source, headers, middleware) {
|
|
201
206
|
const executor = yield* Service;
|
|
202
|
-
const response = yield* executor
|
|
207
|
+
const response = yield* executor
|
|
208
|
+
.execute(HttpClientRequest.get(source.url).pipe(HttpClientRequest.setHeaders(headers ?? {})))
|
|
209
|
+
.pipe(Effect.provideService(CurrentMiddleware, middleware));
|
|
203
210
|
return response.stream.pipe(Stream.mapError((cause) => new AIError({
|
|
204
211
|
reason: new InvalidProviderOutputError({
|
|
205
212
|
message: `Failed to read media from ${source.url}`,
|
|
@@ -39,7 +39,7 @@ const Transcript = Schema.Struct({
|
|
|
39
39
|
error: optionalNull(Schema.String),
|
|
40
40
|
});
|
|
41
41
|
const STATUS = {
|
|
42
|
-
queued: "
|
|
42
|
+
queued: "running",
|
|
43
43
|
processing: "running",
|
|
44
44
|
completed: "completed",
|
|
45
45
|
error: "failed",
|
|
@@ -77,19 +77,20 @@ const fromRequest = Effect.fn("AssemblyAITranscription.fromRequest")(function* (
|
|
|
77
77
|
const decodeTranscript = route.decodeJson(Transcript);
|
|
78
78
|
const decodeStart = Effect.fn("AssemblyAITranscription.decodeStart")(function* (response) {
|
|
79
79
|
const output = yield* decodeTranscript(response);
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
return {
|
|
81
|
+
token: { transcriptID: output.value.id },
|
|
82
|
+
snapshot: { id: output.value.id, ...MediaProtocol.status(STATUS, output.value.status) },
|
|
83
|
+
};
|
|
82
84
|
});
|
|
83
85
|
const decodeStatus = Effect.fn("AssemblyAITranscription.decodeStatus")(function* (response, context) {
|
|
84
86
|
const output = yield* decodeTranscript(response);
|
|
85
|
-
|
|
86
|
-
return { id: context.token.transcriptID, status };
|
|
87
|
+
return { id: context.token.transcriptID, ...MediaProtocol.status(STATUS, output.value.status) };
|
|
87
88
|
});
|
|
88
89
|
const seconds = (milliseconds) => milliseconds / 1000;
|
|
89
90
|
const decodeResult = Effect.fn("AssemblyAITranscription.decodeResult")(function* (response, context) {
|
|
90
91
|
const output = yield* decodeTranscript(response);
|
|
91
92
|
const transcript = output.value;
|
|
92
|
-
const status = MediaProtocol.
|
|
93
|
+
const status = MediaProtocol.state(STATUS, transcript.status);
|
|
93
94
|
const error = transcript.error ?? undefined;
|
|
94
95
|
if (status === "failed")
|
|
95
96
|
return yield* output.ended("failed", `${route.name} transcription failed${error === undefined ? "" : `: ${error}`}`);
|
|
@@ -32,10 +32,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
32
32
|
readonly startSeconds: number;
|
|
33
33
|
readonly endSeconds: number;
|
|
34
34
|
}[];
|
|
35
|
-
} | {
|
|
36
|
-
readonly type: "generation-queued";
|
|
37
|
-
readonly id: string;
|
|
38
|
-
readonly position?: number | undefined;
|
|
39
35
|
} | {
|
|
40
36
|
readonly type: "generation-progress";
|
|
41
37
|
readonly id: string;
|
|
@@ -82,10 +78,6 @@ export declare const CartesiaSpeech: {
|
|
|
82
78
|
readonly startSeconds: number;
|
|
83
79
|
readonly endSeconds: number;
|
|
84
80
|
}[];
|
|
85
|
-
} | {
|
|
86
|
-
readonly type: "generation-queued";
|
|
87
|
-
readonly id: string;
|
|
88
|
-
readonly position?: number | undefined;
|
|
89
81
|
} | {
|
|
90
82
|
readonly type: "generation-progress";
|
|
91
83
|
readonly id: string;
|
|
@@ -25,10 +25,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
25
25
|
readonly startSeconds: number;
|
|
26
26
|
readonly endSeconds: number;
|
|
27
27
|
}[];
|
|
28
|
-
} | {
|
|
29
|
-
readonly type: "generation-queued";
|
|
30
|
-
readonly id: string;
|
|
31
|
-
readonly position?: number | undefined;
|
|
32
28
|
} | {
|
|
33
29
|
readonly type: "generation-progress";
|
|
34
30
|
readonly id: string;
|
|
@@ -75,10 +71,6 @@ export declare const DeepgramSpeech: {
|
|
|
75
71
|
readonly startSeconds: number;
|
|
76
72
|
readonly endSeconds: number;
|
|
77
73
|
}[];
|
|
78
|
-
} | {
|
|
79
|
-
readonly type: "generation-queued";
|
|
80
|
-
readonly id: string;
|
|
81
|
-
readonly position?: number | undefined;
|
|
82
74
|
} | {
|
|
83
75
|
readonly type: "generation-progress";
|
|
84
76
|
readonly id: string;
|
|
@@ -28,10 +28,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
28
28
|
readonly startSeconds: number;
|
|
29
29
|
readonly endSeconds: number;
|
|
30
30
|
}[];
|
|
31
|
-
} | {
|
|
32
|
-
readonly type: "generation-queued";
|
|
33
|
-
readonly id: string;
|
|
34
|
-
readonly position?: number | undefined;
|
|
35
31
|
} | {
|
|
36
32
|
readonly type: "generation-progress";
|
|
37
33
|
readonly id: string;
|
|
@@ -78,10 +74,6 @@ export declare const ElevenLabsSpeech: {
|
|
|
78
74
|
readonly startSeconds: number;
|
|
79
75
|
readonly endSeconds: number;
|
|
80
76
|
}[];
|
|
81
|
-
} | {
|
|
82
|
-
readonly type: "generation-queued";
|
|
83
|
-
readonly id: string;
|
|
84
|
-
readonly position?: number | undefined;
|
|
85
77
|
} | {
|
|
86
78
|
readonly type: "generation-progress";
|
|
87
79
|
readonly id: string;
|
|
@@ -104,17 +104,17 @@ const fromRequest = Effect.fn("GoogleInteractionsVideo.fromRequest")(function* (
|
|
|
104
104
|
// ---------------------------------------------------------------------------
|
|
105
105
|
const decodeStart = route.decodeStarted(Interaction, (value) => ({
|
|
106
106
|
token: value.id,
|
|
107
|
-
snapshot: { id: value.id,
|
|
107
|
+
snapshot: { id: value.id, ...MediaProtocol.status(STATUS, value.status) },
|
|
108
108
|
}));
|
|
109
109
|
const decodeInteraction = route.decodeJson(Interaction);
|
|
110
110
|
const decodeStatus = Effect.fn("GoogleInteractionsVideo.decodeStatus")(function* (response, context) {
|
|
111
111
|
const output = yield* decodeInteraction(response);
|
|
112
|
-
return { id: context.token,
|
|
112
|
+
return { id: context.token, ...MediaProtocol.status(STATUS, output.value.status) };
|
|
113
113
|
});
|
|
114
114
|
const decodeResult = Effect.fn("GoogleInteractionsVideo.decodeResult")(function* (response, context) {
|
|
115
115
|
const output = yield* decodeInteraction(response);
|
|
116
116
|
const decoded = output.value;
|
|
117
|
-
const status = MediaProtocol.
|
|
117
|
+
const status = MediaProtocol.state(STATUS, decoded.status);
|
|
118
118
|
if (status === "running")
|
|
119
119
|
return yield* output.pending(context.token);
|
|
120
120
|
const errors = decoded.errors ?? [];
|
|
@@ -35,10 +35,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
35
35
|
readonly startSeconds: number;
|
|
36
36
|
readonly endSeconds: number;
|
|
37
37
|
}[];
|
|
38
|
-
} | {
|
|
39
|
-
readonly type: "generation-queued";
|
|
40
|
-
readonly id: string;
|
|
41
|
-
readonly position?: number | undefined;
|
|
42
38
|
} | {
|
|
43
39
|
readonly type: "generation-progress";
|
|
44
40
|
readonly id: string;
|
|
@@ -85,10 +81,6 @@ export declare const GoogleSpeech: {
|
|
|
85
81
|
readonly startSeconds: number;
|
|
86
82
|
readonly endSeconds: number;
|
|
87
83
|
}[];
|
|
88
|
-
} | {
|
|
89
|
-
readonly type: "generation-queued";
|
|
90
|
-
readonly id: string;
|
|
91
|
-
readonly position?: number | undefined;
|
|
92
84
|
} | {
|
|
93
85
|
readonly type: "generation-progress";
|
|
94
86
|
readonly id: string;
|
|
@@ -32,10 +32,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
32
32
|
readonly endSeconds: number;
|
|
33
33
|
readonly speaker?: string | undefined;
|
|
34
34
|
};
|
|
35
|
-
} | {
|
|
36
|
-
readonly type: "generation-queued";
|
|
37
|
-
readonly id: string;
|
|
38
|
-
readonly position?: number | undefined;
|
|
39
35
|
} | {
|
|
40
36
|
readonly type: "generation-progress";
|
|
41
37
|
readonly id: string;
|
|
@@ -98,10 +94,6 @@ export declare const GoogleTranscription: {
|
|
|
98
94
|
readonly endSeconds: number;
|
|
99
95
|
readonly speaker?: string | undefined;
|
|
100
96
|
};
|
|
101
|
-
} | {
|
|
102
|
-
readonly type: "generation-queued";
|
|
103
|
-
readonly id: string;
|
|
104
|
-
readonly position?: number | undefined;
|
|
105
97
|
} | {
|
|
106
98
|
readonly type: "generation-progress";
|
|
107
99
|
readonly id: string;
|
|
@@ -61,15 +61,15 @@ const HailuoFile = Schema.Struct({
|
|
|
61
61
|
base_resp: BaseResp,
|
|
62
62
|
});
|
|
63
63
|
const STATUS = {
|
|
64
|
-
queued: "
|
|
64
|
+
queued: "running",
|
|
65
65
|
running: "running",
|
|
66
66
|
succeeded: "completed",
|
|
67
67
|
failed: "failed",
|
|
68
68
|
cancelled: "cancelled",
|
|
69
69
|
};
|
|
70
70
|
const HAILUO_STATUS = {
|
|
71
|
-
Preparing: "
|
|
72
|
-
Queueing: "
|
|
71
|
+
Preparing: "running",
|
|
72
|
+
Queueing: "running",
|
|
73
73
|
Processing: "running",
|
|
74
74
|
Success: "completed",
|
|
75
75
|
Fail: "failed",
|
|
@@ -155,19 +155,19 @@ const hailuoFromRequest = Effect.fn("MiniMaxHailuoVideo.fromRequest")(function*
|
|
|
155
155
|
// ---------------------------------------------------------------------------
|
|
156
156
|
const decodeStart = route.decodeStarted(StartResponse, (value) => ({
|
|
157
157
|
token: value.task_id,
|
|
158
|
-
snapshot: { id: value.task_id, status: "
|
|
158
|
+
snapshot: { id: value.task_id, status: "running" },
|
|
159
159
|
}));
|
|
160
160
|
const decodeTask = route.decodeJson(TaskResponse);
|
|
161
161
|
const decodeStatus = Effect.fn("MiniMaxVideo.decodeStatus")(function* (response, context) {
|
|
162
162
|
const output = yield* decodeTask(response);
|
|
163
163
|
const task = output.value.task ?? output.value;
|
|
164
|
-
return { id: context.token,
|
|
164
|
+
return { id: context.token, ...MediaProtocol.status(STATUS, task.status ?? "") };
|
|
165
165
|
});
|
|
166
166
|
const decodeResult = Effect.fn("MiniMaxVideo.decodeResult")(function* (response, context) {
|
|
167
167
|
const output = yield* decodeTask(response);
|
|
168
168
|
const task = output.value.task ?? output.value;
|
|
169
|
-
const status = MediaProtocol.
|
|
170
|
-
if (status === "
|
|
169
|
+
const status = MediaProtocol.state(STATUS, task.status ?? "");
|
|
170
|
+
if (status === "running")
|
|
171
171
|
return yield* output.pending(context.token);
|
|
172
172
|
if (status === "failed")
|
|
173
173
|
return yield* failed(output, route.name, task.error?.code ?? undefined, task.error?.message ?? undefined);
|
|
@@ -206,23 +206,23 @@ const decodeHailuoStart = Effect.fn("MiniMaxHailuoVideo.decodeStart")(function*
|
|
|
206
206
|
const taskID = output.value.task_id ?? "";
|
|
207
207
|
if (taskID === "")
|
|
208
208
|
return yield* output.invalid(`${hailuoRoute.name} returned no task_id`);
|
|
209
|
-
return { token: taskID, snapshot: { id: taskID, status: "
|
|
209
|
+
return { token: taskID, snapshot: { id: taskID, status: "running" } };
|
|
210
210
|
});
|
|
211
211
|
const decodeHailuoTask = hailuoRoute.decodeJson(HailuoTask);
|
|
212
212
|
const decodeHailuoStatus = Effect.fn("MiniMaxHailuoVideo.decodeStatus")(function* (response, context) {
|
|
213
213
|
const output = yield* decodeHailuoTask(response);
|
|
214
214
|
const status = MediaProtocol.status(HAILUO_STATUS, output.value.status ?? "");
|
|
215
215
|
// A failed task also carries its reason in `base_resp`; any other non-zero code is a failed read.
|
|
216
|
-
const error = status === "failed" ? undefined : baseError(output, output.value.base_resp);
|
|
216
|
+
const error = status.reason === "failed" ? undefined : baseError(output, output.value.base_resp);
|
|
217
217
|
if (error)
|
|
218
218
|
return yield* error;
|
|
219
|
-
return { id: context.token, status };
|
|
219
|
+
return { id: context.token, ...status };
|
|
220
220
|
});
|
|
221
221
|
const decodeHailuoFile = hailuoRoute.decodeJson(HailuoFile);
|
|
222
222
|
const decodeHailuoResult = Effect.fn("MiniMaxHailuoVideo.decodeResult")(function* (response, context) {
|
|
223
223
|
const output = yield* decodeHailuoTask(response);
|
|
224
224
|
const task = output.value;
|
|
225
|
-
const status = MediaProtocol.
|
|
225
|
+
const status = MediaProtocol.state(HAILUO_STATUS, task.status ?? "");
|
|
226
226
|
const error = baseError(output, task.base_resp);
|
|
227
227
|
if (status === "failed")
|
|
228
228
|
return yield* error ?? failed(output, hailuoRoute.name, undefined, undefined);
|
|
@@ -58,10 +58,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
58
58
|
readonly type: "image";
|
|
59
59
|
readonly index: number;
|
|
60
60
|
readonly image: Media.Asset;
|
|
61
|
-
} | {
|
|
62
|
-
readonly type: "generation-queued";
|
|
63
|
-
readonly id: string;
|
|
64
|
-
readonly position?: number | undefined;
|
|
65
61
|
} | {
|
|
66
62
|
readonly type: "generation-progress";
|
|
67
63
|
readonly id: string;
|
|
@@ -113,10 +109,6 @@ export declare const OpenAIImages: {
|
|
|
113
109
|
readonly type: "image";
|
|
114
110
|
readonly index: number;
|
|
115
111
|
readonly image: Media.Asset;
|
|
116
|
-
} | {
|
|
117
|
-
readonly type: "generation-queued";
|
|
118
|
-
readonly id: string;
|
|
119
|
-
readonly position?: number | undefined;
|
|
120
112
|
} | {
|
|
121
113
|
readonly type: "generation-progress";
|
|
122
114
|
readonly id: string;
|
|
@@ -25,10 +25,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
25
25
|
readonly startSeconds: number;
|
|
26
26
|
readonly endSeconds: number;
|
|
27
27
|
}[];
|
|
28
|
-
} | {
|
|
29
|
-
readonly type: "generation-queued";
|
|
30
|
-
readonly id: string;
|
|
31
|
-
readonly position?: number | undefined;
|
|
32
28
|
} | {
|
|
33
29
|
readonly type: "generation-progress";
|
|
34
30
|
readonly id: string;
|
|
@@ -75,10 +71,6 @@ export declare const OpenAISpeech: {
|
|
|
75
71
|
readonly startSeconds: number;
|
|
76
72
|
readonly endSeconds: number;
|
|
77
73
|
}[];
|
|
78
|
-
} | {
|
|
79
|
-
readonly type: "generation-queued";
|
|
80
|
-
readonly id: string;
|
|
81
|
-
readonly position?: number | undefined;
|
|
82
74
|
} | {
|
|
83
75
|
readonly type: "generation-progress";
|
|
84
76
|
readonly id: string;
|
|
@@ -69,10 +69,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
69
69
|
readonly endSeconds: number;
|
|
70
70
|
readonly speaker?: string | undefined;
|
|
71
71
|
};
|
|
72
|
-
} | {
|
|
73
|
-
readonly type: "generation-queued";
|
|
74
|
-
readonly id: string;
|
|
75
|
-
readonly position?: number | undefined;
|
|
76
72
|
} | {
|
|
77
73
|
readonly type: "generation-progress";
|
|
78
74
|
readonly id: string;
|
|
@@ -135,10 +131,6 @@ export declare const OpenAITranscription: {
|
|
|
135
131
|
readonly endSeconds: number;
|
|
136
132
|
readonly speaker?: string | undefined;
|
|
137
133
|
};
|
|
138
|
-
} | {
|
|
139
|
-
readonly type: "generation-queued";
|
|
140
|
-
readonly id: string;
|
|
141
|
-
readonly position?: number | undefined;
|
|
142
134
|
} | {
|
|
143
135
|
readonly type: "generation-progress";
|
|
144
136
|
readonly id: string;
|
|
@@ -30,7 +30,7 @@ const Job = Schema.Struct({
|
|
|
30
30
|
// 3. Status
|
|
31
31
|
// ---------------------------------------------------------------------------
|
|
32
32
|
const STATUS = {
|
|
33
|
-
pending: "
|
|
33
|
+
pending: "running",
|
|
34
34
|
in_progress: "running",
|
|
35
35
|
completed: "completed",
|
|
36
36
|
failed: "failed",
|
|
@@ -79,18 +79,18 @@ const resolution = (value) => (/^\dk$/.test(value) ? value.toUpperCase() : value
|
|
|
79
79
|
// ---------------------------------------------------------------------------
|
|
80
80
|
const decodeStart = route.decodeStarted(StartResponse, (value) => ({
|
|
81
81
|
token: value.id,
|
|
82
|
-
snapshot: { id: value.id,
|
|
82
|
+
snapshot: { id: value.id, ...MediaProtocol.status(STATUS, value.status ?? "pending") },
|
|
83
83
|
}));
|
|
84
84
|
const decodeJob = route.decodeJson(Job);
|
|
85
85
|
const decodeStatus = Effect.fn("OpenRouterVideo.decodeStatus")(function* (response, context) {
|
|
86
86
|
const output = yield* decodeJob(response);
|
|
87
|
-
return { id: context.token,
|
|
87
|
+
return { id: context.token, ...MediaProtocol.status(STATUS, output.value.status) };
|
|
88
88
|
});
|
|
89
89
|
const decodeResult = Effect.fn("OpenRouterVideo.decodeResult")(function* (response, context) {
|
|
90
90
|
const output = yield* decodeJob(response);
|
|
91
91
|
const job = output.value;
|
|
92
|
-
const status = MediaProtocol.
|
|
93
|
-
if (status === "
|
|
92
|
+
const status = MediaProtocol.state(STATUS, job.status);
|
|
93
|
+
if (status === "running")
|
|
94
94
|
return yield* output.pending(context.token);
|
|
95
95
|
if (status === "failed") {
|
|
96
96
|
const message = `${route.name} generation failed${job.error ? `: ${job.error}` : ""}`;
|
|
@@ -104,10 +104,10 @@ const decodeResult = Effect.fn("OpenRouterVideo.decodeResult")(function* (respon
|
|
|
104
104
|
if (urls.length === 0)
|
|
105
105
|
return yield* output.invalid(`${route.name} completed without a video URL`);
|
|
106
106
|
// The content URLs are OpenRouter's own and only download with the API key.
|
|
107
|
-
const videos = yield* Effect.forEach(urls, (url) => context.
|
|
107
|
+
const videos = yield* Effect.forEach(urls, (url) => context.credentials(url).pipe(Effect.map((credentials) => Media.url(url, {
|
|
108
108
|
mediaType: "video/mp4",
|
|
109
109
|
expiresAt: job.expires_at === undefined || job.expires_at === null ? undefined : job.expires_at * 1000,
|
|
110
|
-
|
|
110
|
+
...credentials,
|
|
111
111
|
}))));
|
|
112
112
|
return new VideoResponse({
|
|
113
113
|
videos,
|
|
@@ -46,7 +46,7 @@ const Task = Schema.Struct({
|
|
|
46
46
|
service_tier: optionalNull(Schema.String),
|
|
47
47
|
});
|
|
48
48
|
const STATUS = {
|
|
49
|
-
queued: "
|
|
49
|
+
queued: "running",
|
|
50
50
|
running: "running",
|
|
51
51
|
succeeded: "completed",
|
|
52
52
|
failed: "failed",
|
|
@@ -127,18 +127,18 @@ const make = (provider) => {
|
|
|
127
127
|
});
|
|
128
128
|
const decodeStart = route.decodeStarted(StartResponse, (value) => ({
|
|
129
129
|
token: value.id,
|
|
130
|
-
snapshot: { id: value.id, status: "
|
|
130
|
+
snapshot: { id: value.id, status: "running" },
|
|
131
131
|
}));
|
|
132
132
|
const decodeTask = route.decodeJson(Task);
|
|
133
133
|
const decodeStatus = Effect.fn("SeedanceVideo.decodeStatus")(function* (response, context) {
|
|
134
134
|
const output = yield* decodeTask(response);
|
|
135
|
-
return { id: context.token,
|
|
135
|
+
return { id: context.token, ...MediaProtocol.status(STATUS, output.value.status) };
|
|
136
136
|
});
|
|
137
137
|
const decodeResult = Effect.fn("SeedanceVideo.decodeResult")(function* (response, context) {
|
|
138
138
|
const output = yield* decodeTask(response);
|
|
139
139
|
const task = output.value;
|
|
140
|
-
const status = MediaProtocol.
|
|
141
|
-
if (status === "
|
|
140
|
+
const status = MediaProtocol.state(STATUS, task.status);
|
|
141
|
+
if (status === "running")
|
|
142
142
|
return yield* output.pending(context.token);
|
|
143
143
|
if (status === "failed") {
|
|
144
144
|
const code = task.error?.code ?? undefined;
|
|
@@ -87,13 +87,16 @@ const fraction = (progress) => progress !== undefined && progress !== null && pr
|
|
|
87
87
|
const decodeVideoStatus = route.decodeJson(VideoStatus);
|
|
88
88
|
const decodeStatus = Effect.fn("XAIVideo.decodeStatus")(function* (response, context) {
|
|
89
89
|
const output = yield* decodeVideoStatus(response);
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
return {
|
|
91
|
+
id: context.token,
|
|
92
|
+
...MediaProtocol.status(STATUS, output.value.status),
|
|
93
|
+
progress: fraction(output.value.progress),
|
|
94
|
+
};
|
|
92
95
|
});
|
|
93
96
|
const decodeResult = Effect.fn("XAIVideo.decodeResult")(function* (response, context) {
|
|
94
97
|
const output = yield* decodeVideoStatus(response);
|
|
95
98
|
const decoded = output.value;
|
|
96
|
-
const status = MediaProtocol.
|
|
99
|
+
const status = MediaProtocol.state(STATUS, decoded.status);
|
|
97
100
|
if (status === "running")
|
|
98
101
|
return yield* output.pending(context.token);
|
|
99
102
|
if (status === "failed") {
|
package/dist/public.d.ts
CHANGED
|
@@ -10,7 +10,8 @@ export type { AnswerFor, AnswersFor, DecisionInput, DecisionMediaPartInput, Deci
|
|
|
10
10
|
export { EmbeddingModel, EmbeddingModelSchema, EmbeddingResponse, EmbeddingUsage, EmbeddingValue } from "./embedding.js";
|
|
11
11
|
export type { EmbeddingInputType, EmbeddingModelOptions, EmbeddingModelRoute, EmbeddingOptions, EmbeddingRequest, EmbeddingRequestInput, EmbeddingValueInput, } from "./embedding.js";
|
|
12
12
|
export { Media } from "./media.js";
|
|
13
|
-
export type { AwaitOptions as GenerationAwaitOptions, Event as GenerationEvent, Poll, Route as GenerationRoute, Snapshot as GenerationSnapshot, Status as GenerationStatus, } from "./generation.js";
|
|
13
|
+
export type { AwaitOptions as GenerationAwaitOptions, Event as GenerationEvent, Poll, Reason as GenerationReason, Route as GenerationRoute, Snapshot as GenerationSnapshot, Status as GenerationStatus, } from "./generation.js";
|
|
14
14
|
export type { ExecutableTool, Tool, ToolExecute, ToolExecuteContext, ToolInput, ToolInputType, ToolModelOutputInput, ToolOptions, ToolSchema, ToolToModelOutput, } from "./tool.js";
|
|
15
15
|
export type { LLM } from "./llm.js";
|
|
16
16
|
export type { LLMLoopInput, LLMRequestInput } from "./llm.js";
|
|
17
|
+
export type { HttpHandler, HttpMiddleware } from "./route/executor-service.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context,
|
|
1
|
+
import { Context, Effect, Stream } from "effect";
|
|
2
2
|
import type { Headers, HttpClientRequest, HttpClientResponse } from "effect/http";
|
|
3
3
|
import { HttpContext, type AIError } from "../schema/errors.js";
|
|
4
4
|
import type { ClientTls } from "../schema/options.js";
|
|
@@ -17,6 +17,19 @@ export declare const Service: typeof RequestExecutorService;
|
|
|
17
17
|
*/
|
|
18
18
|
export declare const CurrentTls: Context.Reference<ClientTls | undefined>;
|
|
19
19
|
export type Service = RequestExecutorService;
|
|
20
|
+
/**
|
|
21
|
+
* Middleware for every provider request made while it is set: model calls, media submits, polls, results and cancels,
|
|
22
|
+
* whatever the modality. Set it with {@link withMiddleware}. Downloads of a `url` asset don't use it; they use the
|
|
23
|
+
* asset's own `middleware`, which a route sets only for URLs on the provider's origin.
|
|
24
|
+
*/
|
|
25
|
+
export declare const CurrentMiddleware: Context.Reference<HttpMiddleware | undefined>;
|
|
26
|
+
/** `outer` wraps `inner`: it sees the request first and the response last. */
|
|
27
|
+
export declare const composeMiddleware: (outer: HttpMiddleware | undefined, inner: HttpMiddleware | undefined) => HttpMiddleware | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Runs `fn` on every provider request `self` makes, an Effect or a Stream. Inside an outer `withMiddleware`, the outer
|
|
30
|
+
* one wraps this one. Executor-wide middleware (`RequestExecutor.middleware`) wraps both.
|
|
31
|
+
*/
|
|
32
|
+
export declare const withMiddleware: (fn: HttpMiddleware) => <Self extends Effect.Effect<unknown, unknown, unknown> | Stream.Stream<unknown, unknown, unknown>>(self: Self) => Self;
|
|
20
33
|
export declare const headerDetails: (headers: Headers.Headers) => {
|
|
21
34
|
[k: string]: string;
|
|
22
35
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Context } from "effect";
|
|
1
|
+
import { Context, Effect, Stream } from "effect";
|
|
2
2
|
import { HttpContext } from "../schema/errors.js";
|
|
3
3
|
export class RequestExecutorService extends Context.Service()("@opencode/AI/RequestExecutor") {
|
|
4
4
|
}
|
|
@@ -10,6 +10,33 @@ export const Service = RequestExecutorService;
|
|
|
10
10
|
export const CurrentTls = Context.Reference("@opencode/AI/RequestExecutor/CurrentTls", {
|
|
11
11
|
defaultValue: () => undefined,
|
|
12
12
|
});
|
|
13
|
+
/**
|
|
14
|
+
* Middleware for every provider request made while it is set: model calls, media submits, polls, results and cancels,
|
|
15
|
+
* whatever the modality. Set it with {@link withMiddleware}. Downloads of a `url` asset don't use it; they use the
|
|
16
|
+
* asset's own `middleware`, which a route sets only for URLs on the provider's origin.
|
|
17
|
+
*/
|
|
18
|
+
export const CurrentMiddleware = Context.Reference("@opencode/AI/RequestExecutor/CurrentMiddleware", { defaultValue: () => undefined });
|
|
19
|
+
/** `outer` wraps `inner`: it sees the request first and the response last. */
|
|
20
|
+
export const composeMiddleware = (outer, inner) => {
|
|
21
|
+
if (outer === undefined)
|
|
22
|
+
return inner;
|
|
23
|
+
if (inner === undefined)
|
|
24
|
+
return outer;
|
|
25
|
+
return (request, handler) => outer(request, (forwarded) => inner(forwarded, handler));
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Runs `fn` on every provider request `self` makes, an Effect or a Stream. Inside an outer `withMiddleware`, the outer
|
|
29
|
+
* one wraps this one. Executor-wide middleware (`RequestExecutor.middleware`) wraps both.
|
|
30
|
+
*/
|
|
31
|
+
export const withMiddleware = (fn) => (self) => {
|
|
32
|
+
const merged = Effect.gen(function* () {
|
|
33
|
+
return composeMiddleware(yield* CurrentMiddleware, fn);
|
|
34
|
+
});
|
|
35
|
+
// One signature for both, so it infers inside `pipe`; each branch returns the same kind it was given.
|
|
36
|
+
return (Stream.isStream(self)
|
|
37
|
+
? Stream.provideServiceEffect(self, CurrentMiddleware, merged)
|
|
38
|
+
: Effect.provideServiceEffect(self, CurrentMiddleware, merged));
|
|
39
|
+
};
|
|
13
40
|
export const headerDetails = (headers) => Object.fromEntries(Object.entries(headers).map(([name, value]) => [name, value]));
|
|
14
41
|
export const responseHttp = (response) => new HttpContext({
|
|
15
42
|
url: response.request.url,
|
package/dist/route/executor.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
|
|
|
3
3
|
import { AIError } from "../schema/index.js";
|
|
4
4
|
import { Service, type HttpMiddleware, type Interface } from "./executor-service.js";
|
|
5
5
|
import type { ClientTls } from "../schema/options.js";
|
|
6
|
-
export { CurrentTls, responseHttp, Service } from "./executor-service.js";
|
|
6
|
+
export { composeMiddleware, CurrentMiddleware, CurrentTls, responseHttp, Service, withMiddleware, } from "./executor-service.js";
|
|
7
7
|
export type { HttpHandler, HttpMiddleware, Interface, RequestExecutorService } from "./executor-service.js";
|
|
8
8
|
/** Preserve HTTP diagnostics for executor and externally captured failures alike. */
|
|
9
9
|
export declare const httpFailure: (input: {
|
|
@@ -29,6 +29,9 @@ export declare const withTls: (execute: Interface["execute"], tls: ClientTls | u
|
|
|
29
29
|
* `middleware` still wrap the call, and status classification still applies.
|
|
30
30
|
*/
|
|
31
31
|
export declare const withFetch: (fetch: typeof globalThis.fetch | undefined, middleware?: HttpMiddleware) => HttpMiddleware | undefined;
|
|
32
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* Run `fn` on every request: it sees the raw response before status classification, inside middleware already on
|
|
34
|
+
* `executor`, and outside per-call and scoped ({@link withMiddleware}) middleware.
|
|
35
|
+
*/
|
|
33
36
|
export declare const middleware: (fn: HttpMiddleware, executor?: Layer.Layer<Service>) => Layer.Layer<Service>;
|
|
34
37
|
export * as RequestExecutor from "./executor.js";
|