feat(platform): Agent 与媒体能力 - 完善角色卡、音频协议与任务恢复链路
This commit is contained in:
1 parent
a8f83fedd5
commit
5650da6d31
91 files changed
+3438
-380
No files matched your search
@@ -13,6 +13,10 @@ type Adapter interface {
|
||||
BuildCancel(context.Context, PollContext) (RequestSpec, error)
|
||||
}
|
||||
|
||||
type RequestAwareCreateParser interface {
|
||||
ParseCreateWithRequest(context.Context, GenerationRequest, []byte) (CreateResult, error)
|
||||
}
|
||||
|
||||
// AgentAdapter is the optional protocol surface for tool-capable text calls.
|
||||
// The host still owns credentials, outbound policy and billing; a plugin only
|
||||
// maps the platform's agent request into the provider payload and parses the
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// IndexTTS2 的音色完全来自 prompt_simple 参考音频,缺失时上游返回“缺少必填参数”,
|
||||
// 因此插件必须把画布连线的参考音频映射过去,并在本地拦截未连接音频的请求。
|
||||
func TestAutoDLIndexTTS2Workflow(t *testing.T) {
|
||||
data, err := os.ReadFile(filepath.Join("..", "..", "..", "plugin-packages", "autodl-comfyui.yingce-plugin"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pkg, err := ParsePluginPackage(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
adapters, err := LoadInstalledProviders(pkg.ManifestRaw, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var adapter Adapter
|
||||
for _, a := range adapters {
|
||||
if a.Metadata().ID == "autodl-comfyui-audio" {
|
||||
adapter = a
|
||||
}
|
||||
}
|
||||
if adapter == nil {
|
||||
t.Fatal("autodl-comfyui-audio provider adapter not found")
|
||||
}
|
||||
if !adapter.Metadata().RequiresPublicMediaURLs {
|
||||
t.Fatal("IndexTTS2 reference audio must be sent as public URLs")
|
||||
}
|
||||
|
||||
if _, err := adapter.BuildCreate(context.Background(), RequestContext{BaseURL: "https://autodl.art", Request: GenerationRequest{
|
||||
Capability: CapabilityAudio, Model: "indextts2-v1", Prompt: "你好,这是一段测试文本",
|
||||
}}); err == nil || !strings.Contains(err.Error(), "音色参考音频") {
|
||||
t.Fatalf("missing prompt_simple must be rejected locally, err = %v", err)
|
||||
}
|
||||
|
||||
create, err := adapter.BuildCreate(context.Background(), RequestContext{BaseURL: "https://autodl.art", Request: GenerationRequest{
|
||||
Capability: CapabilityAudio, Model: "indextts2-v1", Prompt: "你好,这是一段测试文本",
|
||||
Audios: []MediaReference{{URL: "https://cdn.example/voice.mp3", Order: 0}},
|
||||
Extra: map[string]any{"audioVoice": "alloy"},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if create.Method != "POST" || create.Path != "/api/v1/comfyui/comfyui_workflow/indextts2-v1" {
|
||||
t.Fatalf("create spec = %#v", create)
|
||||
}
|
||||
body := create.Body.(map[string]any)
|
||||
if body["prompt_text"] != "你好,这是一段测试文本" || body["prompt_simple"] != "https://cdn.example/voice.mp3" || body["emo_control_method"] != "与音色参考音频相同" || body["emo_random"] != false {
|
||||
t.Fatalf("single reference body = %#v", body)
|
||||
}
|
||||
for _, name := range []string{"emo_happy", "emo_angry", "emo_sad", "emo_afraid", "emo_disgusted", "emo_melancholic", "emo_surprised", "emo_calm"} {
|
||||
if value, ok := body[name]; !ok || manifestFloat(value) != 0 {
|
||||
t.Fatalf("%s = %#v, body = %#v", name, value, body)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"emo_ref_audio", "voice", "prompt"} {
|
||||
if _, exists := body[name]; exists {
|
||||
t.Fatalf("single reference body must not send %s: %#v", name, body)
|
||||
}
|
||||
}
|
||||
|
||||
withEmotion, err := adapter.BuildCreate(context.Background(), RequestContext{BaseURL: "https://autodl.art", Request: GenerationRequest{
|
||||
Capability: CapabilityAudio, Model: "indextts2-v1", Prompt: "测试",
|
||||
Audios: []MediaReference{{URL: "https://cdn.example/emotion.mp3", Order: 2}, {URL: "https://cdn.example/voice.mp3", Order: 1}},
|
||||
ProviderOptions: map[string]map[string]any{"autodl-comfyui-audio": {"emo_happy": 0.5, "emo_calm": 0.3}},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
emotionBody := withEmotion.Body.(map[string]any)
|
||||
if emotionBody["prompt_simple"] != "https://cdn.example/voice.mp3" || emotionBody["emo_ref_audio"] != "https://cdn.example/emotion.mp3" || emotionBody["emo_control_method"] != "使用情感参考音频" || manifestFloat(emotionBody["emo_happy"]) != 0.5 || manifestFloat(emotionBody["emo_calm"]) != 0.3 {
|
||||
t.Fatalf("emotion reference body = %#v", emotionBody)
|
||||
}
|
||||
|
||||
if _, err := adapter.BuildCreate(context.Background(), RequestContext{BaseURL: "https://autodl.art", Request: GenerationRequest{
|
||||
Capability: CapabilityAudio, Model: "indextts2-v1", Prompt: "测试",
|
||||
Audios: []MediaReference{{URL: "https://cdn.example/a.mp3"}, {URL: "https://cdn.example/b.mp3", Order: 1}, {URL: "https://cdn.example/c.mp3", Order: 2}},
|
||||
}}); err == nil {
|
||||
t.Fatal("more than two reference audios must be rejected")
|
||||
}
|
||||
|
||||
completed, err := adapter.ParsePoll(context.Background(), PollContext{TaskID: "671ce5ca"}, []byte(`{"msg":"","code":"Success","data":{"status":"completed","results":[{"url":"https://cos.example/ComfyUI_00010_.wav","type":"audio","node_id":"9","file_type":"wav","output_type":"output"}],"task_id":"671ce5ca"}}`))
|
||||
if err != nil || completed.Status != StatusSucceeded || completed.Result == nil || len(completed.Result.Audios) != 1 || completed.Result.Audios[0].URL != "https://cos.example/ComfyUI_00010_.wav" {
|
||||
t.Fatalf("completed = %#v, err = %v", completed, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDoubaoStreamingTTSPackageBuildsOfficialRequest(t *testing.T) {
|
||||
manifest := []byte(`{
|
||||
"apiVersion":"yingce.plugin/v2",
|
||||
"id":"doubao-streaming-tts","name":"Doubao Streaming TTS","version":"2.0.0","author":"Volcengine",
|
||||
"contributes":{"providers":[{"id":"doubao-streaming-tts","label":"Doubao Streaming TTS","capabilities":["audio"],"scopes":["canvas"],"auth":{"type":"header","field":"apiKey","header":"X-Api-Key"},"create":{"method":"POST","path":"/api/v3/tts/unidirectional","contentType":"application/json","body":{"req_params":{"$json":{"text":{"$ref":"request.prompt"},"speaker":{"$ref":"request.extra.audioVoice"},"model":{"$ref":"request.model"}}}},"headers":{"X-Api-Resource-Id":{"$coalesce":[{"$ref":"request.providerOptions.doubao-streaming-tts.resourceId"},"seed-tts-2.0"]},"X-Api-Request-Id":{"$ref":"request.extra.idempotencyKey"}}},"response":{"streamedJsonAudio":true,"resultKind":"audio"}}]}
|
||||
}`)
|
||||
adapter, err := LoadManifest(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spec, err := adapter.BuildCreate(context.Background(), RequestContext{Request: GenerationRequest{
|
||||
Capability: CapabilityAudio,
|
||||
Model: "seed-tts-2.0",
|
||||
Prompt: "你好",
|
||||
Extra: map[string]any{"audioVoice": "speaker-1", "idempotencyKey": "request-1"},
|
||||
ProviderOptions: map[string]map[string]any{
|
||||
"doubao-streaming-tts": {"resourceId": "seed-icl-2.0"},
|
||||
},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if spec.Headers["X-Api-Resource-Id"] != "seed-icl-2.0" || spec.Headers["X-Api-Request-Id"] != "request-1" {
|
||||
t.Fatalf("headers = %#v", spec.Headers)
|
||||
}
|
||||
body, ok := spec.Body.(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("body type = %T", spec.Body)
|
||||
}
|
||||
params, ok := body["req_params"].(string)
|
||||
if !ok {
|
||||
t.Fatalf("req_params type = %T, want JSON string", body["req_params"])
|
||||
}
|
||||
var decoded map[string]any
|
||||
if err := json.Unmarshal([]byte(params), &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded["text"] != "你好" || decoded["speaker"] != "speaker-1" || decoded["model"] != "seed-tts-2.0" {
|
||||
t.Fatalf("req_params = %#v", decoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamedJSONAudioCreateResultConcatenatesFrames(t *testing.T) {
|
||||
first := base64.StdEncoding.EncodeToString([]byte("first"))
|
||||
second := base64.StdEncoding.EncodeToString([]byte("second"))
|
||||
body := []byte(`{"code":0,"data":"` + first + `"}{"code":0,"data":"` + second + `","usage":{"characters":6}}`)
|
||||
adapter := manifestAdapter{manifest: Manifest{Response: ManifestResponse{StreamedJSONAudio: true, ResultKind: "audio"}}}
|
||||
result, err := adapter.ParseCreate(context.Background(), body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Status != StatusSucceeded || result.Result == nil || len(result.Result.Audios) != 1 {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
if !strings.HasPrefix(result.Result.Audios[0].DataURL, "data:audio/mpeg;base64,") {
|
||||
t.Fatalf("data URL = %q", result.Result.Audios[0].DataURL)
|
||||
}
|
||||
encoded := strings.TrimPrefix(result.Result.Audios[0].DataURL, "data:audio/mpeg;base64,")
|
||||
decoded, err := base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil || string(decoded) != "firstsecond" {
|
||||
t.Fatalf("audio bytes = %q, err = %v", decoded, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamedJSONAudioCreateResultRejectsBusinessError(t *testing.T) {
|
||||
adapter := manifestAdapter{manifest: Manifest{Response: ManifestResponse{StreamedJSONAudio: true, ResultKind: "audio"}}}
|
||||
_, err := adapter.ParseCreate(context.Background(), []byte(`{"code":30000001,"message":"invalid speaker"}`))
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid speaker") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoubaoSeedAudioCreateUsesTextPromptWithoutResourceHeader(t *testing.T) {
|
||||
manifest, err := os.ReadFile("../../../plugin-packages/doubao-streaming-tts/manifest.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
adapter, err := LoadManifest(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spec, err := adapter.BuildCreate(context.Background(), RequestContext{Request: GenerationRequest{Capability: CapabilityAudio, Model: "seed-audio-1.0", Prompt: "你好"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if spec.Path != "/api/v3/tts/create" {
|
||||
t.Fatalf("path = %s", spec.Path)
|
||||
}
|
||||
if _, ok := spec.Headers["X-Api-Resource-Id"]; ok {
|
||||
t.Fatalf("seed-audio create must not send a TTS resource header: %#v", spec.Headers)
|
||||
}
|
||||
body := spec.Body.(map[string]any)
|
||||
if body["model"] != "seed-audio-1.0" || body["text_prompt"] != "你好" || body["references"] != nil {
|
||||
t.Fatalf("text-only body = %#v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoubaoSeedAudioCreateSendsReferenceAudioAndImage(t *testing.T) {
|
||||
manifest, err := os.ReadFile("../../../plugin-packages/doubao-streaming-tts/manifest.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
adapter, err := LoadManifest(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
audioSpec, err := adapter.BuildCreate(context.Background(), RequestContext{Request: GenerationRequest{
|
||||
Capability: CapabilityAudio,
|
||||
Model: "seed-audio-1.0",
|
||||
Prompt: "@Audio1 用这个声音说你好",
|
||||
Audios: []MediaReference{{URL: "https://cdn.example.com/voice.mp3", Kind: "audio"}},
|
||||
Extra: map[string]any{"audioVoice": "zh_female_qingxinnvsheng_uranus_bigtts", "audioFormat": "mp3"},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
audioBody := audioSpec.Body.(map[string]any)
|
||||
references, _ := audioBody["references"].([]any)
|
||||
if len(references) != 2 {
|
||||
t.Fatalf("audio references = %#v", audioBody["references"])
|
||||
}
|
||||
reference, _ := references[0].(map[string]any)
|
||||
speaker, _ := references[1].(map[string]any)
|
||||
if reference["audio_url"] != "https://cdn.example.com/voice.mp3" || reference["audio_data"] != nil || speaker["speaker"] != "zh_female_qingxinnvsheng_uranus_bigtts" {
|
||||
t.Fatalf("audio references = %#v", references)
|
||||
}
|
||||
|
||||
imageSpec, err := adapter.BuildCreate(context.Background(), RequestContext{Request: GenerationRequest{
|
||||
Capability: CapabilityAudio,
|
||||
Model: "seed-audio-1.0",
|
||||
Prompt: "根据图片内容生成音频",
|
||||
Images: []MediaReference{{DataURL: "data:image/png;base64,aGVsbG8=", Kind: "image"}},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
imageBody := imageSpec.Body.(map[string]any)
|
||||
imageReferences, _ := imageBody["references"].([]any)
|
||||
if len(imageReferences) != 1 {
|
||||
t.Fatalf("image references = %#v", imageBody["references"])
|
||||
}
|
||||
imageReference, _ := imageReferences[0].(map[string]any)
|
||||
if imageReference["image_data"] != "aGVsbG8=" || imageReference["image_url"] != nil {
|
||||
t.Fatalf("image reference = %#v", imageReference)
|
||||
}
|
||||
}
|
||||
@@ -44,15 +44,18 @@ type ManifestResponse struct {
|
||||
// BinaryPayload 表示 create 同步返回二进制媒体(如 /audio/speech 的音频流)。
|
||||
// 声明式解析会把整个响应体包装为对应能力的单个媒体结果,不做 JSON 路径提取。
|
||||
BinaryPayload bool `json:"binaryPayload,omitempty"`
|
||||
TaskID any `json:"taskId,omitempty"`
|
||||
Status any `json:"status,omitempty"`
|
||||
Message any `json:"message,omitempty"`
|
||||
Text any `json:"text,omitempty"`
|
||||
Reasoning any `json:"reasoning,omitempty"`
|
||||
Images any `json:"images,omitempty"`
|
||||
Videos any `json:"videos,omitempty"`
|
||||
Audios any `json:"audios,omitempty"`
|
||||
Usage any `json:"usage,omitempty"`
|
||||
// StreamedJSONAudio 表示 create 返回由连续 JSON 对象组成的 HTTP chunked 音频流;
|
||||
// 每个对象可携带 data(Base64 音频片段)、code/message 和 usage。
|
||||
StreamedJSONAudio bool `json:"streamedJsonAudio,omitempty"`
|
||||
TaskID any `json:"taskId,omitempty"`
|
||||
Status any `json:"status,omitempty"`
|
||||
Message any `json:"message,omitempty"`
|
||||
Text any `json:"text,omitempty"`
|
||||
Reasoning any `json:"reasoning,omitempty"`
|
||||
Images any `json:"images,omitempty"`
|
||||
Videos any `json:"videos,omitempty"`
|
||||
Audios any `json:"audios,omitempty"`
|
||||
Usage any `json:"usage,omitempty"`
|
||||
}
|
||||
|
||||
// ManifestAgentResponse describes the provider response shape for a
|
||||
|
||||
@@ -6,10 +6,13 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -82,7 +85,18 @@ func syntheticAgentToolCallID(body []byte, index int) string {
|
||||
return fmt.Sprintf("call_%x", sum[:8])
|
||||
}
|
||||
|
||||
func (a manifestAdapter) ParseCreate(_ context.Context, body []byte) (CreateResult, error) {
|
||||
func (a manifestAdapter) ParseCreate(ctx context.Context, body []byte) (CreateResult, error) {
|
||||
return a.ParseCreateWithRequest(ctx, GenerationRequest{}, body)
|
||||
}
|
||||
|
||||
func (a manifestAdapter) ParseCreateWithRequest(_ context.Context, request GenerationRequest, body []byte) (CreateResult, error) {
|
||||
if a.manifest.Response.StreamedJSONAudio {
|
||||
format := ""
|
||||
if request.Extra != nil {
|
||||
format = manifestString(request.Extra["audioFormat"])
|
||||
}
|
||||
return streamedJSONAudioCreateResult(body, a.manifest.Response.ResultKind, format)
|
||||
}
|
||||
if a.manifest.Response.BinaryPayload {
|
||||
return binaryPayloadCreateResult(a.manifest.Response.ResultKind, body)
|
||||
}
|
||||
@@ -93,8 +107,127 @@ func (a manifestAdapter) ParseCreate(_ context.Context, body []byte) (CreateResu
|
||||
return a.parse(payload, PollContext{}), nil
|
||||
}
|
||||
|
||||
// binaryPayloadCreateResult 把同步二进制响应包装为单个媒体结果。MIME 以响应内容探测为准,
|
||||
// 空响应必须失败,不能把空内容伪装成生成成功。
|
||||
// streamedJSONAudioCreateResult decodes the chunked JSON response used by
|
||||
// Doubao's unidirectional TTS endpoint. Go exposes HTTP chunked transfer as a
|
||||
// single byte stream, so the decoder accepts both concatenated JSON objects and
|
||||
// newline/SSE-style data frames.
|
||||
const maxStreamedJSONAudioBytes = 64 << 20
|
||||
|
||||
func streamedJSONAudioCreateResult(body []byte, resultKind string, format string) (CreateResult, error) {
|
||||
if len(body) == 0 {
|
||||
return CreateResult{}, fmt.Errorf("streamed JSON audio response is empty")
|
||||
}
|
||||
if len(body) > maxStreamedJSONAudioBytes {
|
||||
return CreateResult{}, fmt.Errorf("streamed JSON audio response exceeds %d bytes", maxStreamedJSONAudioBytes)
|
||||
}
|
||||
frames, err := decodeJSONFrames(body)
|
||||
if err != nil {
|
||||
return CreateResult{}, fmt.Errorf("decode streamed JSON audio response: %w", err)
|
||||
}
|
||||
var audio bytes.Buffer
|
||||
var usage map[string]any
|
||||
for _, frame := range frames {
|
||||
code := strings.ToLower(strings.TrimSpace(manifestString(firstNonNilPathValue(frame, "code", "status_code", "statusCode"))))
|
||||
if code != "" && code != "0" && code != "ok" && code != "success" && code != "succeeded" {
|
||||
message := strings.TrimSpace(manifestString(firstNonNilPathValue(frame, "message", "msg", "error.message")))
|
||||
if message == "" {
|
||||
message = "上游返回失败状态"
|
||||
}
|
||||
return CreateResult{}, fmt.Errorf("流式音频合成失败(code %s):%s", code, message)
|
||||
}
|
||||
if value := firstPathValue(frame, "data", "audio", "audio.data"); value != "" {
|
||||
decoded, decodeErr := decodeStreamedAudioBase64(value)
|
||||
if decodeErr != nil {
|
||||
return CreateResult{}, fmt.Errorf("decode streamed audio data: %w", decodeErr)
|
||||
}
|
||||
_, _ = audio.Write(decoded)
|
||||
}
|
||||
if rawUsage := pathValue(frame, "usage"); rawUsage != nil {
|
||||
usage = manifestObject(rawUsage)
|
||||
}
|
||||
}
|
||||
if audio.Len() == 0 {
|
||||
return CreateResult{}, fmt.Errorf("streamed JSON audio response contains no audio data")
|
||||
}
|
||||
mimeType := streamedAudioMIMEType(format)
|
||||
reference := MediaReference{DataURL: "data:" + mimeType + ";base64," + base64.StdEncoding.EncodeToString(audio.Bytes()), MIMEType: mimeType}
|
||||
return CreateResult{Status: StatusSucceeded, Result: &Result{Audios: []MediaReference{reference}, Usage: usage}}, nil
|
||||
}
|
||||
|
||||
func decodeJSONFrames(body []byte) ([]map[string]any, error) {
|
||||
decoder := json.NewDecoder(bytes.NewReader(body))
|
||||
frames := make([]map[string]any, 0, 4)
|
||||
for {
|
||||
var frame map[string]any
|
||||
err := decoder.Decode(&frame)
|
||||
if err == io.EOF {
|
||||
return frames, nil
|
||||
}
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
if frame != nil {
|
||||
frames = append(frames, frame)
|
||||
}
|
||||
}
|
||||
// Some gateways wrap each JSON object in an SSE-like "data:" line.
|
||||
for _, line := range strings.Split(string(body), "\n") {
|
||||
line = strings.TrimSpace(strings.TrimSuffix(line, "\r"))
|
||||
line = strings.TrimSpace(strings.TrimPrefix(line, "data:"))
|
||||
if line == "" || line == "[DONE]" {
|
||||
continue
|
||||
}
|
||||
var frame map[string]any
|
||||
if err := json.Unmarshal([]byte(line), &frame); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
frames = append(frames, frame)
|
||||
}
|
||||
if len(frames) == 0 {
|
||||
return nil, fmt.Errorf("no JSON frames found")
|
||||
}
|
||||
return frames, nil
|
||||
}
|
||||
|
||||
func streamedAudioMIMEType(format string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(format)) {
|
||||
case "wav":
|
||||
return "audio/wav"
|
||||
case "ogg_opus", "ogg":
|
||||
return "audio/ogg"
|
||||
case "pcm":
|
||||
return "audio/pcm"
|
||||
default:
|
||||
return "audio/mpeg"
|
||||
}
|
||||
}
|
||||
|
||||
func firstNonNilPathValue(payload map[string]any, paths ...string) any {
|
||||
for _, path := range paths {
|
||||
if value := pathValue(payload, path); value != nil {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeStreamedAudioBase64(value string) ([]byte, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return nil, nil
|
||||
}
|
||||
decoded, err := base64.StdEncoding.DecodeString(value)
|
||||
if err == nil {
|
||||
return decoded, nil
|
||||
}
|
||||
decoded, rawErr := base64.RawStdEncoding.DecodeString(value)
|
||||
if rawErr == nil {
|
||||
return decoded, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// binaryPayloadCreateResult wraps a synchronous binary response as one media result.
|
||||
func binaryPayloadCreateResult(resultKind string, body []byte) (CreateResult, error) {
|
||||
if len(body) == 0 {
|
||||
return CreateResult{}, fmt.Errorf("binary payload response is empty")
|
||||
|
||||
Reference in new issue
Block a user