Skip to content

Commit b0e82e1

Browse files
authored
fix: Handle missing trace index, better multimedia samples for AI (#3682)
1 parent 5c8f080 commit b0e82e1

File tree

3 files changed

+13
-86
lines changed

3 files changed

+13
-86
lines changed

genkit-tools/cli/context/GENKIT.go.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Genkit Go API Rules (v1.0.0)
1+
# Genkit Go API Rules (v1.20.0)
22

33
This document provides rules and examples for building with the Genkit API in Go.
44

genkit-tools/cli/context/GENKIT.js.md

Lines changed: 7 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Genkit Node.js API Rules (v1.17.1)
1+
# Genkit Node.js API Rules (v1.20.0)
22

33
This document provides rules and examples for building with the Genkit API in Node.js.
44

@@ -102,41 +102,6 @@ export const basicInferenceFlow = ai.defineFlow(
102102
103103
### Text-to-Speech (TTS) Generation
104104
105-
This helper function converts PCM audio data from the TTS model into a WAV-formatted data URI.
106-
107-
```ts
108-
import { Buffer } from 'buffer';
109-
import { PassThrough } from 'stream';
110-
import { Writer as WavWriter } from 'wav';
111-
112-
...
113-
114-
async function pcmToWavDataUri(
115-
pcmData: Buffer,
116-
channels = 1,
117-
sampleRate = 24000,
118-
bitDepth = 16
119-
): Promise<string> {
120-
return new Promise((resolve, reject) => {
121-
const chunks: Buffer[] = [];
122-
const passThrough = new PassThrough();
123-
124-
passThrough.on('data', (chunk) => chunks.push(chunk as Buffer));
125-
passThrough.on('end', () => {
126-
const wavBuffer = Buffer.concat(chunks);
127-
const dataUri = `data:audio/wav;base64,${wavBuffer.toString('base64')}`;
128-
resolve(dataUri);
129-
});
130-
passThrough.on('error', reject);
131-
132-
const writer = new WavWriter({ channels, sampleRate, bitDepth });
133-
writer.pipe(passThrough);
134-
writer.write(pcmData);
135-
writer.end();
136-
});
137-
}
138-
```
139-
140105
#### Single-Speaker TTS
141106
142107
```ts
@@ -147,17 +112,12 @@ const TextToSpeechInputSchema = z.object({
147112
.optional()
148113
.describe('The voice name to use. Defaults to Algenib if not specified.'),
149114
});
150-
const TextToSpeechOutputSchema = z.object({
151-
audioDataUri: z
152-
.string()
153-
.describe('The generated speech in WAV format as a base64 data URI.'),
154-
});
155115
156116
export const textToSpeechFlow = ai.defineFlow(
157117
{
158118
name: 'textToSpeechFlow',
159119
inputSchema: TextToSpeechInputSchema,
160-
outputSchema: TextToSpeechOutputSchema,
120+
outputSchema: z.string().optional().describe('The generated audio URI'),
161121
},
162122
async (input) => {
163123
const response = await ai.generate({
@@ -175,16 +135,7 @@ export const textToSpeechFlow = ai.defineFlow(
175135
},
176136
});
177137
178-
const audioUrl = response.media?.url;
179-
if (!audioUrl)
180-
throw new Error('Audio generation failed: No media URL in response.');
181-
182-
const base64 = audioUrl.split(';base64,')[1];
183-
if (!base64) throw new Error('Invalid audio data URI format from Genkit.');
184-
185-
const pcmBuffer = Buffer.from(base64, 'base64');
186-
const audioDataUri = await pcmToWavDataUri(pcmBuffer);
187-
return { audioDataUri };
138+
return response.media?.url;
188139
}
189140
);
190141
```
@@ -204,7 +155,7 @@ export const multiSpeakerTextToSpeechFlow = ai.defineFlow(
204155
{
205156
name: 'multiSpeakerTextToSpeechFlow',
206157
inputSchema: MultiSpeakerInputSchema,
207-
outputSchema: TextToSpeechOutputSchema,
158+
outputSchema: z.string().optional().describe('The generated audio URI'),
208159
},
209160
async (input) => {
210161
const response = await ai.generate({
@@ -233,16 +184,7 @@ export const multiSpeakerTextToSpeechFlow = ai.defineFlow(
233184
},
234185
});
235186
236-
const audioUrl = response.media?.url;
237-
if (!audioUrl)
238-
throw new Error('Audio generation failed: No media URL in response.');
239-
240-
const base64 = audioUrl.split(';base64,')[1];
241-
if (!base64) throw new Error('Invalid audio data URI format from Genkit.');
242-
243-
const pcmBuffer = Buffer.from(base64, 'base64');
244-
const audioDataUri = await pcmToWavDataUri(pcmBuffer);
245-
return { audioDataUri };
187+
return response.media?.url;
246188
}
247189
);
248190
```
@@ -254,18 +196,13 @@ export const multiSpeakerTextToSpeechFlow = ai.defineFlow(
254196
### Image Generation
255197
256198
```ts
257-
import * as fs from 'fs/promises';
258-
import parseDataURL from 'data-urls';
259-
260-
...
261-
262199
export const imageGenerationFlow = ai.defineFlow(
263200
{
264201
name: 'imageGenerationFlow',
265202
inputSchema: z
266203
.string()
267204
.describe('A detailed description of the image to generate'),
268-
outputSchema: z.string().describe('Path to the generated .png image file'),
205+
outputSchema: z.string().optional().describe('The generated image as URI'),
269206
},
270207
async (prompt) => {
271208
const response = await ai.generate({
@@ -274,18 +211,7 @@ export const imageGenerationFlow = ai.defineFlow(
274211
output: { format: 'media' },
275212
});
276213
277-
if (!response.media?.url) {
278-
throw new Error('Image generation failed to produce media.');
279-
}
280-
281-
const parsed = parseDataURL(response.media.url);
282-
if (!parsed) {
283-
throw new Error('Could not parse image data URL.');
284-
}
285-
286-
const outputPath = './output.png';
287-
await fs.writeFile(outputPath, parsed.body);
288-
return outputPath;
214+
return response.media?.url;
289215
}
290216
);
291217
```

genkit-tools/telemetry-server/src/file-trace-store.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,13 @@ export class Index {
317317
}
318318

319319
getMetadata(): { version: string } | undefined {
320-
if (!fs.existsSync(this.metadataFileName())) {
320+
try {
321+
return JSON.parse(
322+
fs.readFileSync(this.metadataFileName(), { encoding: 'utf8' })
323+
);
324+
} catch (e) {
321325
return undefined;
322326
}
323-
return JSON.parse(
324-
fs.readFileSync(this.metadataFileName(), { encoding: 'utf8' })
325-
);
326327
}
327328

328329
private newIndexFileName() {

0 commit comments

Comments
 (0)