45 lines
2.1 KiB
Go
45 lines
2.1 KiB
Go
package realtime
|
|
|
|
import (
|
|
"ai-operator/internal/ai"
|
|
"ai-operator/internal/audio"
|
|
"ai-operator/internal/config"
|
|
"encoding/json"
|
|
)
|
|
|
|
func BuildSessionUpdate(cfg config.OpenAIConfig, instructions string) ([]byte, error) {
|
|
session := map[string]any{"type": "realtime", "model": cfg.RealtimeModel, "instructions": instructions, "audio": map[string]any{"input": map[string]any{"format": map[string]any{"type": "audio/pcm", "rate": cfg.RealtimeInputSampleRate}, "turn_detection": map[string]any{"type": cfg.RealtimeTurnDetection}}, "output": map[string]any{"format": map[string]any{"type": "audio/pcm", "rate": cfg.RealtimeOutputSampleRate}}}, "reasoning": map[string]any{"effort": cfg.RealtimeReasoningEffort}}
|
|
if cfg.RealtimeVoice != "" {
|
|
session["audio"].(map[string]any)["output"].(map[string]any)["voice"] = cfg.RealtimeVoice
|
|
}
|
|
return json.Marshal(map[string]any{"type": "session.update", "session": session})
|
|
}
|
|
func BuildAudioAppend(pcm []byte) ([]byte, error) {
|
|
return json.Marshal(map[string]any{"type": "input_audio_buffer.append", "audio": audio.Base64Encode(pcm)})
|
|
}
|
|
func BuildCommit() ([]byte, error) {
|
|
return json.Marshal(map[string]any{"type": "input_audio_buffer.commit"})
|
|
}
|
|
func BuildResponseCreate() ([]byte, error) {
|
|
return json.Marshal(map[string]any{"type": "response.create"})
|
|
}
|
|
func BuildResponseCreateWithInstructions(instructions string) ([]byte, error) {
|
|
if instructions == "" {
|
|
return BuildResponseCreate()
|
|
}
|
|
return json.Marshal(map[string]any{"type": "response.create", "response": map[string]any{"instructions": instructions}})
|
|
}
|
|
func BuildResponseCancel() ([]byte, error) {
|
|
return json.Marshal(map[string]any{"type": "response.cancel"})
|
|
}
|
|
func BuildInputAudioClear() ([]byte, error) {
|
|
return json.Marshal(map[string]any{"type": "input_audio_buffer.clear"})
|
|
}
|
|
func BuildToolResult(result ai.ToolResult) ([]byte, error) {
|
|
b, _ := json.Marshal(result.Result)
|
|
if result.Error != "" {
|
|
b = []byte(result.Error)
|
|
}
|
|
return json.Marshal(map[string]any{"type": "conversation.item.create", "item": map[string]any{"type": "function_call_output", "call_id": result.ToolCallID, "output": string(b)}})
|
|
}
|