@opencode/ai 0.0.0-dev-20039 → 0.0.0-dev-20041

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.
Files changed (46) hide show
  1. package/README.md +86 -1
  2. package/dist/index.d.ts +4 -0
  3. package/dist/index.js +3 -0
  4. package/dist/media-model.d.ts +3 -3
  5. package/dist/media-model.js +3 -3
  6. package/dist/promise.d.ts +111 -3
  7. package/dist/promise.js +9 -1
  8. package/dist/protocols/bedrock-converse.js +2 -8
  9. package/dist/protocols/cartesia-speech.d.ts +127 -0
  10. package/dist/protocols/cartesia-speech.js +126 -0
  11. package/dist/protocols/deepgram-speech.d.ts +119 -0
  12. package/dist/protocols/deepgram-speech.js +90 -0
  13. package/dist/protocols/elevenlabs-speech.d.ts +122 -0
  14. package/dist/protocols/elevenlabs-speech.js +115 -0
  15. package/dist/protocols/google-speech.d.ts +160 -0
  16. package/dist/protocols/google-speech.js +122 -0
  17. package/dist/protocols/openai-speech.d.ts +116 -0
  18. package/dist/protocols/openai-speech.js +98 -0
  19. package/dist/protocols/utils/speech-stream.d.ts +49 -0
  20. package/dist/protocols/utils/speech-stream.js +67 -0
  21. package/dist/providers/cartesia.d.ts +24 -0
  22. package/dist/providers/cartesia.js +22 -0
  23. package/dist/providers/deepgram.d.ts +24 -0
  24. package/dist/providers/deepgram.js +28 -0
  25. package/dist/providers/elevenlabs.d.ts +24 -0
  26. package/dist/providers/elevenlabs.js +28 -0
  27. package/dist/providers/google.d.ts +5 -0
  28. package/dist/providers/google.js +3 -0
  29. package/dist/providers/index.d.ts +3 -0
  30. package/dist/providers/index.js +3 -0
  31. package/dist/providers/openai.d.ts +5 -0
  32. package/dist/providers/openai.js +6 -1
  33. package/dist/route/framing.d.ts +5 -1
  34. package/dist/route/framing.js +9 -0
  35. package/dist/route/media-protocol.d.ts +35 -2
  36. package/dist/route/media-protocol.js +22 -1
  37. package/dist/route/media.d.ts +15 -1
  38. package/dist/route/media.js +41 -7
  39. package/dist/speech-client.d.ts +21 -0
  40. package/dist/speech-client.js +25 -0
  41. package/dist/speech.d.ts +1150 -0
  42. package/dist/speech.js +119 -0
  43. package/dist/utils/bytes.d.ts +1 -0
  44. package/dist/utils/bytes.js +10 -0
  45. package/dist/utils/media-type.js +8 -1
  46. package/package.json +3 -3
package/dist/speech.js ADDED
@@ -0,0 +1,119 @@
1
+ import { Effect, Schema, Stream } from "effect";
2
+ import { Media } from "./media.js";
3
+ import { MediaModel, composeRoute, tryRequest } from "./media-model.js";
4
+ import { MediaRoute } from "./route/media.js";
5
+ import { AIError, HttpOptions, MediaUsage, ProviderMetadata } from "./schema/index.js";
6
+ import { SpeechClient, Service } from "./speech-client.js";
7
+ export class SpeechModel extends MediaModel {
8
+ static make(input) {
9
+ return new SpeechModel(input);
10
+ }
11
+ /** Compose a streaming speech protocol with its canonical path into a model for one deployment. */
12
+ static fromRoute(route, input) {
13
+ return new SpeechModel({
14
+ id: input.id,
15
+ provider: route.provider,
16
+ http: input.http,
17
+ route: composeRoute((composition) => MediaRoute.stream({ ...composition, collect: collectResponse }), route, input),
18
+ });
19
+ }
20
+ }
21
+ export const SpeechModelSchema = Schema.declare((value) => value instanceof SpeechModel, {
22
+ expected: "Speech.Model",
23
+ });
24
+ // ---------------------------------------------------------------------------
25
+ // Request
26
+ // ---------------------------------------------------------------------------
27
+ /** Provider-native: a name on OpenAI and Gemini, a voice id on ElevenLabs and Cartesia; `{ id }` is an OpenAI custom voice. */
28
+ export const SpeechVoice = Schema.Union([Schema.String, Schema.Struct({ id: Schema.String })]).annotate({
29
+ identifier: "Speech.Voice",
30
+ });
31
+ /** Granularity is provider-native: characters on ElevenLabs, words on Cartesia. */
32
+ export const SpeechTimestamp = Schema.Struct({
33
+ text: Schema.String,
34
+ startSeconds: Schema.Number,
35
+ endSeconds: Schema.Number,
36
+ }).annotate({ identifier: "Speech.Timestamp" });
37
+ export class SpeechRequest extends Schema.Class("Speech.Request")({
38
+ model: SpeechModelSchema,
39
+ text: Schema.String,
40
+ voice: Schema.optional(SpeechVoice),
41
+ format: Schema.optional(Schema.String),
42
+ speed: Schema.optional(Schema.Number),
43
+ language: Schema.optional(Schema.String),
44
+ instructions: Schema.optional(Schema.String),
45
+ timestamps: Schema.optional(Schema.Boolean),
46
+ providerOptions: Schema.optional(Schema.Record(Schema.String, Schema.Unknown)),
47
+ http: Schema.optional(HttpOptions),
48
+ }) {
49
+ }
50
+ // ---------------------------------------------------------------------------
51
+ // Response and events
52
+ // ---------------------------------------------------------------------------
53
+ export class SpeechResponse extends Schema.Class("Speech.Response")({
54
+ /** The complete audio. Headerless PCM carries `info.encoding`, `info.sampleRate`, and `info.channels`. */
55
+ audio: Media.AssetSchema,
56
+ timestamps: Schema.optional(Schema.Array(SpeechTimestamp)),
57
+ usage: Schema.optional(MediaUsage),
58
+ notices: Schema.optional(Schema.Array(Media.Notice)),
59
+ providerMetadata: Schema.optional(ProviderMetadata),
60
+ }) {
61
+ }
62
+ export const SpeechAudioDeltaEvent = Schema.Struct({
63
+ type: Schema.tag("audio-delta"),
64
+ chunk: Schema.Uint8Array,
65
+ }).annotate({ identifier: "Speech.Event.AudioDelta" });
66
+ export const SpeechTimestampsEvent = Schema.Struct({
67
+ type: Schema.tag("timestamps"),
68
+ items: Schema.Array(SpeechTimestamp),
69
+ }).annotate({ identifier: "Speech.Event.Timestamps" });
70
+ /** `audio` is every `audio-delta` chunk concatenated, so the route holds the whole clip in memory until `finish`. */
71
+ export const SpeechFinishEvent = Schema.Struct({
72
+ type: Schema.tag("finish"),
73
+ audio: Media.AssetSchema,
74
+ usage: Schema.optional(MediaUsage),
75
+ notices: Schema.optional(Schema.Array(Media.Notice)),
76
+ providerMetadata: Schema.optional(ProviderMetadata),
77
+ }).annotate({ identifier: "Speech.Event.Finish" });
78
+ const speechEventTagged = Schema.Union([SpeechAudioDeltaEvent, SpeechTimestampsEvent, SpeechFinishEvent]).pipe(Schema.toTaggedUnion("type"));
79
+ export const SpeechEvent = Object.assign(speechEventTagged, {
80
+ is: {
81
+ audioDelta: speechEventTagged.guards["audio-delta"],
82
+ timestamps: speechEventTagged.guards.timestamps,
83
+ finish: speechEventTagged.guards.finish,
84
+ },
85
+ });
86
+ const collectResponse = (events) => {
87
+ const finish = events.find(SpeechEvent.is.finish);
88
+ // Every speech protocol's `finish` emits the terminal event or fails, so a completed stream always has one.
89
+ if (finish === undefined)
90
+ return Effect.die(new Error("The speech stream completed without a finish event"));
91
+ const timestamps = events.filter(SpeechEvent.is.timestamps).flatMap((event) => event.items);
92
+ return Effect.succeed(new SpeechResponse({
93
+ audio: finish.audio,
94
+ timestamps: timestamps.length === 0 ? undefined : timestamps,
95
+ usage: finish.usage,
96
+ notices: finish.notices,
97
+ providerMetadata: finish.providerMetadata,
98
+ }));
99
+ };
100
+ export function request(input) {
101
+ if (input instanceof SpeechRequest)
102
+ return input;
103
+ return new SpeechRequest({
104
+ ...input,
105
+ http: input.http === undefined ? undefined : HttpOptions.make(input.http),
106
+ });
107
+ }
108
+ const requestEffect = (input) => tryRequest(() => request(input));
109
+ export function generate(input) {
110
+ return requestEffect(input).pipe(Effect.flatMap((request) => SpeechClient.generate(request)));
111
+ }
112
+ export function stream(input) {
113
+ return Stream.unwrap(requestEffect(input).pipe(Effect.map((request) => SpeechClient.stream(request))));
114
+ }
115
+ export const Speech = {
116
+ request,
117
+ generate,
118
+ stream,
119
+ };
@@ -0,0 +1 @@
1
+ export declare const concatBytes: (chunks: ReadonlyArray<Uint8Array>) => Uint8Array<ArrayBufferLike>;
@@ -0,0 +1,10 @@
1
+ export const concatBytes = (chunks) => {
2
+ if (chunks.length === 1)
3
+ return chunks[0];
4
+ const bytes = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
5
+ chunks.reduce((offset, chunk) => {
6
+ bytes.set(chunk, offset);
7
+ return offset + chunk.length;
8
+ }, 0);
9
+ return bytes;
10
+ };
@@ -24,10 +24,15 @@ export const detectMediaType = (bytes) => {
24
24
  return "video/mp4";
25
25
  if (startsWith(bytes, [0x1a, 0x45, 0xdf, 0xa3]))
26
26
  return "video/webm";
27
- if (startsWith(bytes, [0x49, 0x44, 0x33]) || startsWith(bytes, [0xff, 0xfb]) || startsWith(bytes, [0xff, 0xf3]))
27
+ if (startsWith(bytes, [0x49, 0x44, 0x33]))
28
28
  return "audio/mpeg";
29
+ // An 11-bit frame sync; layer bits `00` mark AAC ADTS, any other layer is MPEG audio.
30
+ if (bytes.length >= 2 && bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0)
31
+ return (bytes[1] & 0x06) === 0 ? "audio/aac" : "audio/mpeg";
29
32
  if (startsWith(bytes, [0x4f, 0x67, 0x67, 0x53]))
30
33
  return "audio/ogg";
34
+ if (startsWith(bytes, [0x66, 0x4c, 0x61, 0x43]))
35
+ return "audio/flac";
31
36
  return undefined;
32
37
  };
33
38
  const EXTENSIONS = {
@@ -42,6 +47,8 @@ const EXTENSIONS = {
42
47
  mp3: "audio/mpeg",
43
48
  wav: "audio/wav",
44
49
  ogg: "audio/ogg",
50
+ flac: "audio/flac",
51
+ aac: "audio/aac",
45
52
  txt: "text/plain",
46
53
  md: "text/markdown",
47
54
  csv: "text/csv",
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-20039",
3
+ "version": "0.0.0-dev-20041",
4
4
  "name": "@opencode/ai",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "devDependencies": {
35
35
  "@clack/prompts": "1.0.0-alpha.1",
36
36
  "@effect/platform-node": "4.0.0-rc.112",
37
- "@opencode/http-recorder": "0.0.0-dev-20039",
37
+ "@opencode/http-recorder": "0.0.0-dev-20041",
38
38
  "@tsconfig/bun": "1.0.9",
39
39
  "@types/bun": "1.4.0",
40
40
  "@typescript/native-preview": "7.0.0-dev.20251207.1",
@@ -44,7 +44,7 @@
44
44
  "@aws-sdk/credential-providers": "3.1057.0",
45
45
  "@smithy/eventstream-codec": "4.2.14",
46
46
  "@smithy/util-utf8": "4.2.2",
47
- "@opencode/schema": "0.0.0-dev-20039",
47
+ "@opencode/schema": "0.0.0-dev-20041",
48
48
  "aws4fetch": "1.0.20",
49
49
  "effect": "4.0.0-rc.112",
50
50
  "google-auth-library": "10.5.0"