Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function request(config: Config, opts: RequestOpts): Promise<Respon
? (opts.body as FormData)
: JSON.stringify(opts.body)
: undefined,
signal: AbortSignal.timeout(timeoutMs),
signal: opts.stream ? undefined : AbortSignal.timeout(timeoutMs),
});

if (config.verbose) {
Expand Down
10 changes: 2 additions & 8 deletions src/commands/music/cover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { musicEndpoint } from '../../client/endpoints';
import { formatOutput, detectOutputFormat } from '../../output/formatter';
import { saveAudioOutput } from '../../output/audio';
import { MUSIC_FORMATS, formatList, validateAudioFormat } from '../../utils/audio-formats';
import { pipeAudioStream } from '../../utils/audio-stream';
import type { Config } from '../../config/schema';
import type { GlobalFlags } from '../../types/flags';
import type { MusicRequest, MusicResponse } from '../../types/api';
Expand Down Expand Up @@ -109,14 +110,7 @@ export default defineCommand({

if (flags.stream) {
const res = await request(config, { url, method: 'POST', body, stream: true });
const reader = res.body?.getReader();
if (!reader) throw new CLIError('No response body', ExitCode.GENERAL);
while (true) {
const { done, value } = await reader.read();
if (done) break;
process.stdout.write(value);
}
reader.releaseLock();
await pipeAudioStream(res);
return;
}

Expand Down
12 changes: 3 additions & 9 deletions src/commands/music/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { CLIError } from '../../errors/base';
import { ExitCode } from '../../errors/codes';
import { request, requestJson } from '../../client/http';
import { musicEndpoint } from '../../client/endpoints';
import { formatOutput, detectOutputFormat, dryRun } from '../../output/formatter';
import { detectOutputFormat, dryRun } from '../../output/formatter';
import { saveAudioOutput } from '../../output/audio';
import { readTextFromPathOrStdin } from '../../utils/fs';
import { MUSIC_FORMATS, formatList, validateAudioFormat } from '../../utils/audio-formats';
import { pipeAudioStream } from '../../utils/audio-stream';
import type { Config } from '../../config/schema';
import type { GlobalFlags } from '../../types/flags';
import type { MusicRequest, MusicResponse } from '../../types/api';
Expand Down Expand Up @@ -173,14 +174,7 @@ export default defineCommand({

if (flags.stream) {
const res = await request(config, { url, method: 'POST', body, stream: true });
const reader = res.body?.getReader();
if (!reader) throw new CLIError('No response body', ExitCode.GENERAL);
while (true) {
const { done, value } = await reader.read();
if (done) break;
process.stdout.write(value);
}
reader.releaseLock();
await pipeAudioStream(res);
return;
}

Expand Down
11 changes: 2 additions & 9 deletions src/commands/speech/synthesize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { CLIError } from '../../errors/base';
import { ExitCode } from '../../errors/codes';
import { request, requestJson } from '../../client/http';
import { speechEndpoint } from '../../client/endpoints';
import { parseSSE } from '../../client/stream';
import { detectOutputFormat, formatOutput, dryRun } from '../../output/formatter';
import { saveAudioOutput } from '../../output/audio';
import { writeFileSync } from 'fs';
import { readTextFromPathOrStdin } from '../../utils/fs';
import { T2A_FORMATS, formatList, validateAudioFormat, validateT2AStreaming, t2aDefaultSampleRate } from '../../utils/audio-formats';
import { pipeAudioStream } from '../../utils/audio-stream';
import type { Config } from '../../config/schema';
import type { GlobalFlags } from '../../types/flags';
import type { SpeechRequest, SpeechResponse } from '../../types/api';
Expand Down Expand Up @@ -105,14 +105,7 @@ export default defineCommand({

if (flags.stream) {
const res = await request(config, { url, method: 'POST', body, stream: true });
for await (const event of parseSSE(res)) {
if (!event.data || event.data === '[DONE]') break;
const parsed = JSON.parse(event.data);
const audioHex = parsed?.data?.audio;
if (audioHex) {
process.stdout.write(Buffer.from(audioHex, 'hex'));
}
}
await pipeAudioStream(res);
return;
}

Expand Down
29 changes: 29 additions & 0 deletions src/utils/audio-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { parseSSE } from '../client/stream';

interface SseAudioPayload {
data?: { audio?: string; status?: number };
}

export async function pipeAudioStream(response: Response): Promise<void> {
process.stdout.on('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EPIPE') process.exit(0);
throw err;
});

for await (const event of parseSSE(response)) {
if (!event.data || event.data === '[DONE]') break;

let parsed: SseAudioPayload;
try { parsed = JSON.parse(event.data); } catch { continue; }

if (parsed.data?.status === 2) continue;

const hex = parsed.data?.audio;
if (!hex) continue;

const chunk = Buffer.from(hex, 'hex');
if (!process.stdout.write(chunk)) {
await new Promise<void>(r => process.stdout.once('drain', r));
}
}
}
Loading