dictation

package
v0.68.39 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package dictation provides an embeddable strict Dictation runtime.

Dictation is the most boundary-strict of the three SpeechKit modes: STT turns speech into text and the pipeline stops there. No LLM rewriting, no codewords, no tool invocation. Host apps integrate this when they want a transcript surface they can route into their own editor, command dispatcher, or downstream processor.

Use NewService (or its original name NewRuntime) to construct an instance; pass a recorder, a speechkit.Transcriber — typically stt.AsTranscriber wrapped around any stt.STTProvider — and the speechkit.RuntimePolicy from the host config.

Example

Example shows the whole Dictation round trip: start capturing, stop, get the final text delivered and returned as a DictationRun. Dictation never rewrites the transcript — no LLM, no codewords — which is what makes it safe to route into an editor unchanged.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/kombifyio/SpeechKit/pkg/speechkit"
	"github.com/kombifyio/SpeechKit/pkg/speechkit/dictation"
)

// silentRecorder stands in for a microphone. A real host plugs in its own
// speechkit.AudioRecorder (for example a WASAPI or PortAudio capture).
type silentRecorder struct{}

func (silentRecorder) Start() error { return nil }

func (silentRecorder) Stop() ([]byte, error) {

	return make([]byte, 16000*2), nil
}

func (silentRecorder) SetPCMHandler(func([]byte)) {}

// echoTranscriber stands in for an STT backend. Use stt.AsTranscriber to adapt
// any stt.STTProvider instead of writing this by hand.
type echoTranscriber struct{}

func (echoTranscriber) Transcribe(_ context.Context, _ []byte, _ float64, language string) (speechkit.Transcript, error) {
	return speechkit.Transcript{Text: "hello world", Language: language, Provider: "example"}, nil
}

// printOutput is the host's delivery sink; the reference app injects into the
// focused text field, a server host would post to a channel or a queue.
type printOutput struct{}

func (printOutput) Deliver(_ context.Context, transcript speechkit.Transcript, target any) error {
	fmt.Printf("deliver %q to %v\n", transcript.Text, target)
	return nil
}

func main() {
	rt, err := dictation.NewRuntime(dictation.Options{
		Recorder:    silentRecorder{},
		Transcriber: echoTranscriber{},
		Output:      printOutput{},
		Language:    "en",
		Target:      "editor",
	})
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.Background()
	if err := rt.Start(ctx); err != nil {
		log.Fatal(err)
	}
	run, err := rt.Stop(ctx)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(run.Transcript.Text, run.Transcript.Language, run.AudioDurationMs)
}
Output:
deliver "hello world" to editor
hello world en 1000
Example (Policy)

Example_policy shows how a host pins Dictation to one provider profile through speechkit.RuntimePolicy. NewRuntime validates the policy against the built-in catalog up front, so a misconfigured host fails at construction rather than on the first recording, and every DictationRun reports the profile that was in force.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/kombifyio/SpeechKit/pkg/speechkit"
	"github.com/kombifyio/SpeechKit/pkg/speechkit/dictation"
)

// silentRecorder stands in for a microphone. A real host plugs in its own
// speechkit.AudioRecorder (for example a WASAPI or PortAudio capture).
type silentRecorder struct{}

func (silentRecorder) Start() error { return nil }

func (silentRecorder) Stop() ([]byte, error) {

	return make([]byte, 16000*2), nil
}

func (silentRecorder) SetPCMHandler(func([]byte)) {}

// echoTranscriber stands in for an STT backend. Use stt.AsTranscriber to adapt
// any stt.STTProvider instead of writing this by hand.
type echoTranscriber struct{}

func (echoTranscriber) Transcribe(_ context.Context, _ []byte, _ float64, language string) (speechkit.Transcript, error) {
	return speechkit.Transcript{Text: "hello world", Language: language, Provider: "example"}, nil
}

func main() {
	rt, err := dictation.NewRuntime(dictation.Options{
		Recorder:    silentRecorder{},
		Transcriber: echoTranscriber{},
		Policy: speechkit.RuntimePolicy{
			EnabledModes:  []speechkit.Mode{speechkit.ModeDictation},
			FixedProfiles: map[speechkit.Mode]string{speechkit.ModeDictation: "stt.local.whispercpp"},
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	ctx := context.Background()
	_ = rt.Start(ctx)
	run, _ := rt.Stop(ctx)
	fmt.Println(run.ProviderProfile)

	// Enabling a non-dictation mode is rejected: this runtime is Dictation only.
	_, err = dictation.NewRuntime(dictation.Options{
		Recorder:    silentRecorder{},
		Transcriber: echoTranscriber{},
		Policy:      speechkit.RuntimePolicy{EnabledModes: []speechkit.Mode{speechkit.ModeAssist}},
	})
	fmt.Println(err != nil)
}
Output:
stt.local.whispercpp
true

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingRecorder    = errors.New("speechkit dictation: recorder is required")
	ErrMissingTranscriber = errors.New("speechkit dictation: transcriber is required")
	ErrAlreadyRecording   = errors.New("speechkit dictation: already recording")
	ErrNotRecording       = errors.New("speechkit dictation: not recording")
	ErrAudioTooShort      = errors.New("speechkit dictation: audio too short")
)

Functions

This section is empty.

Types

type Options

type Options struct {
	// Recorder supplies the PCM audio for one dictation. audio/capture.Session
	// satisfies it; tests use any in-memory AudioRecorder.
	Recorder speechkit.AudioRecorder
	// Transcriber turns the recorded WAV into a Transcript. Wrap an
	// stt.STTProvider or stt.Router with stt.AsTranscriber.
	Transcriber speechkit.Transcriber
	// Output receives the final transcript. Nil means the transcript is only
	// returned from Stop / stored; the runtime does not inject text anywhere.
	Output speechkit.TranscriptOutput
	// Store optionally persists every completed dictation.
	Store speechkit.Persistence
	// Observer can retain recognized text before output or slow history I/O.
	Observer speechkit.TranscriptionFinalizationObserver
	// Policy is the host's runtime policy (local-only, allowed providers, ...).
	// It is validated against Profiles at construction time.
	Policy speechkit.RuntimePolicy
	// Profiles is the provider catalog the Policy is checked against. Empty
	// means catalog.DefaultProviderProfiles().
	Profiles []speechkit.ProviderProfile
	// Language is the BCP-47 hint passed to the transcriber; "" means "auto".
	Language string
	// Target is an opaque, host-defined value handed unchanged to
	// Output.Deliver as its target argument. The runtime never inspects it; it
	// exists so a host can route one Output to several destinations (an editor
	// handle, a window id, a "clipboard" marker) without a per-target Output.
	// Nil is valid and means "the Output's default destination". Pass a value
	// implementing [speechkit.OutputTarget] (for example
	// speechkit.TargetRef{Kind: speechkit.TargetKindEditor, ID: "notes"});
	// untyped values are accepted until the field becomes OutputTarget in
	// v0.69.0.
	Target any
	// MinPCMBytes is the shortest recording that is transcribed; shorter
	// recordings fail Stop with ErrAudioTooShort. <= 0 means
	// speechkit.DefaultMinPCMBytes.
	MinPCMBytes int
}

Options configures a Dictation Runtime. Recorder and Transcriber are required; NewRuntime returns ErrMissingRecorder / ErrMissingTranscriber otherwise. Everything else has a documented default.

type Runtime

type Runtime struct {
	// contains filtered or unexported fields
}

Runtime is an embeddable Dictation-only service. It keeps the mode boundary strict: audio in, final text out, no tool calls or LLM rewriting.

func NewRuntime

func NewRuntime(opts Options) (*Runtime, error)

func (*Runtime) Start

func (r *Runtime) Start(ctx context.Context) error

func (*Runtime) Stop

type Service added in v0.67.14

type Service = Runtime

Service is the mode-service name for the Dictation runtime, matching assist.Service, tts.Service and voiceagent.Service so hosts wire the three modes with one vocabulary. Runtime and Service are the same type.

func NewService added in v0.67.14

func NewService(opts Options) (*Service, error)

NewService constructs a Dictation service; it is NewRuntime under the name the other mode packages use.

Jump to

Keyboard shortcuts

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