transcription

package
v0.45.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 10 Imported by: 11

Documentation

Overview

Package transcription defines the serializable audio-to-text protocol and its single-method Model capability.

NewRequest accepts validated media audio. Options carries the shared language hint and provider extensions written through Options.Extensions.Set so they are JSON-safe. Request has no arbitrary parameter bag. Implementations and defaults live outside Core.

Example
package main

import (
	"fmt"

	"github.com/Tangerg/scope/core/media"
	"github.com/Tangerg/scope/core/transcription"
)

func main() {
	audio, err := media.NewBytes("audio/wav", []byte("audio"))
	if err != nil {
		panic(err)
	}
	request, err := transcription.NewRequest(audio)
	if err != nil {
		panic(err)
	}
	options := transcription.Options{Model: "transcription-model"}
	err = options.Validate()
	if err != nil {
		panic(err)
	}
	options.Language = "en"
	request.Options = options

	fmt.Println(request.Audio.MIME, request.Options.Language)
}
Output:
audio/wav en

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOptions  = errors.New("transcription: invalid options")
	ErrInvalidRequest  = errors.New("transcription: invalid request")
	ErrInvalidResponse = errors.New("transcription: invalid response")
)

Functions

This section is empty.

Types

type Model

type Model interface {
	// Call transcribes one validated media request without retaining or mutating
	// it. The returned provider-neutral response belongs to the caller, and
	// context cancellation remains identifiable through errors.Is.
	Call(ctx context.Context, request *Request) (*Response, error)
}

Model is the complete provider-neutral transcription SPI. Implementations validate requests before I/O, reject explicit options they cannot represent, and return responses that pass Validate; provider defaults and identity belong to provider construction.

type ModelFunc

type ModelFunc func(context.Context, *Request) (*Response, error)

func (ModelFunc) Call

func (m ModelFunc) Call(ctx context.Context, request *Request) (*Response, error)

type Options

type Options struct {
	Model string `json:"model"`

	// Language is an ISO-639-1 language code (e.g. "en", "zh") hinting
	// the spoken language. Empty leaves detection to the provider.
	Language string `json:"language"`

	Extensions metadata.Extensions `json:"extensions,omitzero"`
}

Options holds provider-neutral transcription configuration. Provider-specific controls belong in Extensions. Resolve overlays only explicitly supplied values and snapshots extension data, leaving both inputs unchanged.

func (Options) Clone

func (o Options) Clone() Options

func (Options) MarshalJSON

func (o Options) MarshalJSON() ([]byte, error)

func (Options) Resolve

func (o Options) Resolve(override Options) (Options, error)

func (*Options) UnmarshalJSON

func (o *Options) UnmarshalJSON(data []byte) error

func (Options) Validate

func (o Options) Validate() error

type Output

type Output struct {
	// Text may be empty for partial or silent segments.
	Text string `json:"text"`

	Metadata metadata.Map `json:"metadata,omitzero"`
}

func NewOutput

func NewOutput(text string, outputMetadata metadata.Map) (*Output, error)

NewOutput validates and snapshots one provider result before it enters a Response.

func (Output) MarshalJSON

func (o Output) MarshalJSON() ([]byte, error)

func (*Output) UnmarshalJSON

func (o *Output) UnmarshalJSON(data []byte) error

func (*Output) Validate

func (o *Output) Validate() error

type Request

type Request struct {
	Audio *media.Media `json:"audio,omitzero"`

	Options Options `json:"options,omitzero"`
}

func NewRequest

func NewRequest(audio *media.Media) (*Request, error)

func (Request) MarshalJSON

func (r Request) MarshalJSON() ([]byte, error)

func (*Request) UnmarshalJSON

func (r *Request) UnmarshalJSON(data []byte) error

func (*Request) Validate

func (r *Request) Validate() error

type Response

type Response struct {
	Output *Output `json:"output,omitzero"`

	Metadata *ResponseMetadata `json:"metadata,omitzero"`
}

Response is one transcription call's output plus shared metadata. Providers may store per-segment timing in Output.Metadata while Output.Text holds the merged transcript.

func NewResponse

func NewResponse(output *Output, responseMetadata *ResponseMetadata) (*Response, error)

func (Response) MarshalJSON

func (r Response) MarshalJSON() ([]byte, error)

func (*Response) UnmarshalJSON

func (r *Response) UnmarshalJSON(data []byte) error

func (*Response) Validate

func (r *Response) Validate() error

type ResponseMetadata

type ResponseMetadata struct {
	Model string `json:"model"`

	CreatedAt time.Time `json:"created_at,omitzero"`

	Extra metadata.Map `json:"extra,omitzero"`
}

func (ResponseMetadata) MarshalJSON

func (r ResponseMetadata) MarshalJSON() ([]byte, error)

func (*ResponseMetadata) UnmarshalJSON

func (r *ResponseMetadata) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL