speechv1
import speechv1 "pocketone.dev/ext/contracts/llm/speech/v1"
Package speechv1 is the llm.speech/v1 contract: a model reads text aloud.
A wire contract at /api/_c/llm.speech/v1. In pb_hooks it's $llm.speech.
Source: ext/contracts/llm/speech/v1.
Constants
const (
ModeBuiltin = "builtin"
ModeDesign = "design"
ModeClone = "clone"
)
Voice modes, in Voice.Mode.
Variables
var Contract = extkit.NewContract[Speech]("llm.speech/v1", extkit.One).JS(extkit.JSHooks, nil).JSTypes(jsTypes)
Contract is llm.speech/v1.
var W = extkit.NewWire(Contract, "llm", func(inner Speech, g *extkit.Guard[Speech]) Speech {
return &guardSpeech{inner, g}
},
extkit.Op[Speech]{
Name: "speak",
Desc: "Reads the text aloud and returns the audio.",
Method: "POST",
Path: "speak",
In: reflect.TypeOf(SpeakInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Speech, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in SpeakInput
if err := extkit.DecodeInput(req.JSON, &in); err != nil {
return nil, err
}
return impl.Speak(ctx, a, in)
},
},
extkit.Op[Speech]{
Name: "voices",
Desc: "Lists a model's voices, when the provider says.",
Method: "POST",
Path: "voices",
Scope: "llm:read",
In: reflect.TypeOf(VoicesInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Speech, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in VoicesInput
if err := extkit.DecodeInput(req.JSON, &in); err != nil {
return nil, err
}
return impl.Voices(ctx, a, in)
},
},
extkit.Op[Speech]{
Name: "models",
Desc: "Lists the speech models of every provider in Settings → LLM.",
Method: "GET",
Path: "models",
Scope: "llm:read",
In: reflect.TypeOf(llm.ModelsInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Speech, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in llm.ModelsInput
if err := extkit.DecodeQuery(req.Query, &in); err != nil {
return nil, err
}
return impl.Models(ctx, a, in)
},
},
)
W is llm.speech/v1's wire side: routes at /api/_c/llm.speech/v1/<op>, action hooks, MCP tools and the client.
Functions
OnModels
func OnModels(owner any, fn func(e *extkit.CallEvent, in *llm.ModelsInput) error)
OnModels binds fn to models calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnSpeak
func OnSpeak(owner any, fn func(e *extkit.CallEvent, in *SpeakInput) error)
OnSpeak binds fn to speak calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnVoices
func OnVoices(owner any, fn func(e *extkit.CallEvent, in *VoicesInput) error)
OnVoices binds fn to voices calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
Types
Client
type Client struct {
// contains filtered or unexported fields
}
Client is llm.speech/v1 over HTTP, for the CLI and other apps. It implements Speech; the token it was made with is the credential, so the extkit.Auth argument is ignored.
NewClient
func NewClient(baseURL, token string, hc *http.Client) *Client
NewClient calls llm.speech/v1 on the app at baseURL with token (a PocketBase auth token or an API token).
Client.Models
func (c *Client) Models(ctx context.Context, _ extkit.Auth, in llm.ModelsInput) (llm.Models, error)
Client.Speak
func (c *Client) Speak(ctx context.Context, _ extkit.Auth, in SpeakInput) (Result, error)
Client.Voices
func (c *Client) Voices(ctx context.Context, _ extkit.Auth, in VoicesInput) (VoiceList, error)
Result
type Result struct {
File llm.File `json:"file"`
Model string `json:"model"`
Usage llm.Usage `json:"usage"`
}
Result is the audio.
SpeakInput
type SpeakInput struct {
Model string `json:"model"` // "provider/model", required
Text string `json:"text"`
Voice string `json:"voice,omitempty"`
// Format is the audio format: mp3 (default), wav, opus, flac, pcm, as the provider allows.
Format string `json:"format,omitempty"`
Speed float64 `json:"speed,omitempty"`
// Instructions describe the delivery ("slow and calm, a bedtime voice"), for models that take them. Not with a
// Reference: a cloned voice takes its delivery from the clip.
Instructions string `json:"instructions,omitempty"`
// Reference clones a voice: the model reads Text in the voice of this clip. Leave Voice empty (or the model's
// clone voice, Voices' Mode "clone"). Models that clone list a voice with that mode; a provider that doesn't clone
// refuses the call with llm.ErrNotSupported. Nothing is saved at the provider: send the clip with every call.
Reference *VoiceReference `json:"reference,omitempty"`
Options llm.Options `json:"options,omitempty"`
}
SpeakInput is what to say and how.
Speech
type Speech interface {
// Speak reads the text aloud and returns the audio.
//
//op: POST speak
Speak(ctx context.Context, a extkit.Auth, in SpeakInput) (Result, error)
// Voices lists a model's voices, when the provider says.
//
//op: POST voices scope=llm:read
Voices(ctx context.Context, a extkit.Auth, in VoicesInput) (VoiceList, error)
// Models lists the speech models of every provider in Settings → LLM.
//
//op: GET models scope=llm:read
Models(ctx context.Context, a extkit.Auth, in llm.ModelsInput) (llm.Models, error)
}
Speech turns text into audio.
Voice
type Voice struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Language string `json:"language,omitempty"`
Gender string `json:"gender,omitempty"`
// Mode is how the voice is made, when the provider says: "builtin" (a speaker the model has), "design" (a new
// voice described in Instructions) or "clone" (the voice of a Reference clip). A model with a "clone" voice
// clones.
Mode string `json:"mode,omitempty"`
Description string `json:"description,omitempty"`
}
Voice is one voice.
VoiceList
type VoiceList struct {
Voices []Voice `json:"voices"`
}
VoiceList is a model's voices.
VoiceList.Clones
func (l VoiceList) Clones() bool
Clones reports whether a model's voices include a clone voice, so a Reference works with it.
VoiceReference
type VoiceReference struct {
// Audio is the clip, with its data: a short, clean recording (10 to 30 seconds) read the way the model should
// speak. Its Name's extension (me.wav) or Type tells the provider the format.
Audio llm.File `json:"audio"`
// Text is what the clip says, word for word. Optional, but most models match the voice more closely with it.
Text string `json:"text,omitempty"`
}
VoiceReference is a clip of the voice to imitate.
VoicesInput
type VoicesInput struct {
Model string `json:"model"` // "provider/model", required
}
VoicesInput names the model whose voices to list.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.