Repository navigation
feat(chat): translate inline audio to Chat Completions #7271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
harryzhou2000
wants to merge
7
commits into
lidge-jun:dev
from
harryzhou2000:codex/chat-completions-audio-input
+525
−145
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcdfd96
feat: translate inline audio to chat completions
harryzhou2000 56c0643
test: cover translated chat audio forwarding
harryzhou2000 a97fc88
docs: describe inline audio on the chat wire
harryzhou2000 2b70e49
fix: tighten translated audio media guards
harryzhou2000 17dbdab
docs: define translated audio media contract
harryzhou2000 445a0be
test: register chat audio layout coverage
harryzhou2000 7452d40
docs: clarify orphan audio and raw base64
harryzhou2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import type { OcxAudioContent } from "../types"; | ||
|
|
||
| export type ChatAudioFormat = OcxAudioContent["format"]; | ||
|
|
||
| const MIME_FORMATS: Readonly<Record<string, ChatAudioFormat>> = { | ||
| "audio/mp3": "mp3", | ||
| "audio/mpeg": "mp3", | ||
| "audio/wav": "wav", | ||
| "audio/wave": "wav", | ||
| "audio/x-wav": "wav", | ||
| }; | ||
|
|
||
| function normalizedFormat(value: unknown): ChatAudioFormat | undefined { | ||
| if (typeof value !== "string") return undefined; | ||
| const format = value.trim().toLowerCase(); | ||
| return format === "mp3" || format === "wav" ? format : undefined; | ||
| } | ||
|
|
||
| function validBase64(value: string): boolean { | ||
| return value.length > 0 | ||
| && value.length % 4 === 0 | ||
| && /^[A-Za-z0-9+/]+={0,2}$/.test(value); | ||
| } | ||
|
|
||
| /** | ||
| * Parse the inline audio shape Codex puts in a Responses content block. Chat Completions | ||
| * needs bare base64 plus `wav`/`mp3`, while Codex supplies an RFC 2397 data URL. A raw | ||
| * base64 value with an explicit supported format is accepted as well; remote URLs are not. | ||
| */ | ||
| export function audioContentFromRaw(audioUrl: unknown, declaredFormat?: unknown): OcxAudioContent | undefined { | ||
| if (typeof audioUrl !== "string" || audioUrl.length === 0) return undefined; | ||
| const dataUrl = /^data:([^;,]+);base64,([\s\S]*)$/.exec(audioUrl); | ||
| let data: string; | ||
| let format: ChatAudioFormat | undefined; | ||
| if (dataUrl) { | ||
| const declared = normalizedFormat(declaredFormat); | ||
| const mime = dataUrl[1].trim().toLowerCase(); | ||
| format = MIME_FORMATS[mime] ?? (mime === "application/octet-stream" ? declared : undefined); | ||
| data = dataUrl[2].replace(/\s+/g, ""); | ||
| } else { | ||
| format = normalizedFormat(declaredFormat); | ||
| data = audioUrl.replace(/\s+/g, ""); | ||
| } | ||
| if (!format || !validBase64(data)) return undefined; | ||
| return { type: "audio", data, format, text: `[audio: ${format}]` }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.