assist

package
v0.78.4 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package assist provides an embeddable Assist Mode service.

Assist is the one-shot pipeline: speech (or text) in, a single useful result out (codeword, deterministic utility, or LLM generation), with optional TTS playback. It is the middle of the three SpeechKit modes (Dictation < Assist < Voice Agent) and the right surface when the user wants an answer back, not a transcript and not a dialogue.

Construct an instance with NewService, passing a generator (LLM) and/or a tool executor plus the strict-mode policy fields from the host config. The ready-made Voice-Companion skills live in the skills subpackage; the codeword catalog they match against lives in the shortcuts subpackage.

Stability: Stable — no breaking change without a minor version bump and a changelog callout.

Index

Constants

View Source
const (
	// KindAnswer is a direct reply (LLM answer, time, weather, ...).
	KindAnswer = "answer"
	// KindWorkProduct is generated text meant to be inserted or kept
	// (summary, draft).
	KindWorkProduct = "work_product"
	// KindUtilityAction acknowledges a side effect the host performed
	// (copied, inserted, timer set, smart-home command sent).
	KindUtilityAction = "utility_action"
)

Result kinds carried in ToolResult.Kind and speechkit.AssistResult.Kind. The value classifies what the host received so it can pick a presentation: an answer to read or speak, a work product to insert, or the acknowledgement of a utility action.

Variables

View Source
var (
	// ErrMissingHandler is returned by NewService when neither a Generator
	// nor a ToolExecutor is configured, and by Process when a request needs
	// the Generator (no utility matched) but none was configured.
	ErrMissingHandler = errors.New("speechkit assist: generator or tool executor is required")
	// ErrCleanModeNeedsUtility is returned in ModeBehaviorClean when no
	// deterministic utility matched: clean mode never reaches an LLM.
	ErrCleanModeNeedsUtility = errors.New("speechkit assist: clean mode requires a matched deterministic utility")
	// ErrMissingExecutor is returned when a session has an active skill
	// context or the matcher recognised a utility but the service was built
	// without a ToolExecutor.
	ErrMissingExecutor = errors.New("speechkit assist: no tool executor configured")
	// ErrEmptyText is returned by Process when the request carries no text
	// (or only whitespace); there is nothing to route or generate from.
	ErrEmptyText = errors.New("speechkit assist: text is empty")
)

Functions

func ComposeContext added in v0.73.0

func ComposeContext(parts ContextParts) string

ComposeContext folds the structured active-window fields, the free-form context, and the vocabulary hint into a single labelled block for the ToolCall and the Generator. Order: active-window block, free-form context, then the vocabulary guidance last. Whitespace-only fields are dropped and the result is "" when nothing is set.

Types

type ContextParts added in v0.73.0

type ContextParts struct {
	// ActiveApp names the foreground application the user is in when they
	// trigger Assist, e.g. "Visual Studio Code" or "chrome".
	ActiveApp string
	// WindowTitle is the foreground window's title bar text.
	WindowTitle string
	// Context is free-form context (speaker labels, host hints, ...).
	Context string
	// VocabularyHint carries the user's resolved Words/Replacements
	// customization as a short guidance sentence ("Prefer these names and
	// product terms ...: Kombify, SpeechKit."). It is the Assist-LLM-prompt leg
	// of the customization story; empty means no customization terms.
	VocabularyHint string
}

ContextParts are the host-supplied fragments ComposeContext folds into one AssistRequest.Context block. Adapters populate them from the OS (Device-Target) or the request body (Server-Target); the Service and the skills never need to know the prompt format.

type FollowupState added in v0.40.1

type FollowupState map[string]string

FollowupState carries skill-private multi-turn state without making ToolResult non-comparable for existing SDK consumers.

func NewFollowupState added in v0.40.1

func NewFollowupState(values map[string]string) *FollowupState

NewFollowupState copies values into a FollowupState; nil for an empty map.

func (*FollowupState) Map added in v0.40.1

func (s *FollowupState) Map() map[string]string

Map returns a copy of the state as a plain map; nil when empty.

type GenerateFunc

GenerateFunc adapts a plain function to the Generator contract.

func (GenerateFunc) GenerateAssist

GenerateAssist implements Generator.

type Generator

type Generator interface {
	GenerateAssist(context.Context, speechkit.AssistRequest) (speechkit.AssistResult, error)
}

Generator produces the free-form Assist answer when no deterministic utility handled the request; normally an LLM.

type InMemorySkillContextStore added in v0.73.0

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

InMemorySkillContextStore is the default SkillContextStore: a mutex-guarded map that prunes expired entries lazily on every Get, so no background goroutine has to be lifecycled by the host. Every method is safe for concurrent use and on a nil receiver.

func NewInMemorySkillContextStore added in v0.73.0

func NewInMemorySkillContextStore(ttl time.Duration, now func() time.Time) *InMemorySkillContextStore

NewInMemorySkillContextStore returns a ready store. A zero or negative ttl falls back to 60 seconds. The clock override is for tests; nil uses time.Now.

func (*InMemorySkillContextStore) Clear added in v0.73.0

func (s *InMemorySkillContextStore) Clear(key string)

Clear removes any context stored under key. Safe to call when no entry exists.

func (*InMemorySkillContextStore) Get added in v0.73.0

Get returns the active context for key, after lazily pruning an expired entry.

func (*InMemorySkillContextStore) Len added in v0.73.0

func (s *InMemorySkillContextStore) Len() int

Len reports how many entries are currently tracked, expired ones included until their next Get. Exported for tests and diagnostics.

func (*InMemorySkillContextStore) Set added in v0.73.0

func (s *InMemorySkillContextStore) Set(key, intent string, state map[string]string)

Set stores a copy of state for key; ExpiresAt is now + ttl.

type Options

type Options struct {
	Behavior      speechkit.ModeBehavior
	Generator     Generator
	Matcher       ToolMatcher
	Executor      ToolExecutor
	SkillContexts SkillContextStore
	TTSRouter     TTSRouter
	TTSEnabled    bool
	// TTS tunes synthesis (speech defaults and per-provider overrides); nil
	// leaves the router's own defaults in charge.
	TTS *TTSOptions
	// TTSBestEffort keeps the text result when synthesis fails: the failure
	// is recorded as [telemetry.OutcomeAssistTTSFailed] and logged, and the
	// result is returned without audio. Hosts that would rather show text
	// than nothing set it; the default fails the request so a broken voice
	// path stays visible.
	TTSBestEffort bool
	// Logger receives the service's diagnostics. Nil falls back to
	// slog.Default() at log time.
	Logger *slog.Logger
}

Options configures a Service. Generator or Executor is required; every other field is optional.

type Service

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

Service is the embeddable Assist Mode implementation of speechkit.AssistService.

func NewService

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

NewService validates opts and returns a ready Service. It fails with ErrMissingHandler when neither Generator nor Executor is set.

func (*Service) HasGenerator added in v0.73.0

func (s *Service) HasGenerator() bool

HasGenerator reports whether the service can answer requests that no deterministic utility handles. Hosts use it to explain "no Assist model" before recording instead of after.

func (*Service) Process

Process runs one Assist request: an active multi-turn skill context wins, then the ToolMatcher, then the Generator (unless the behavior is clean). Tool errors are wrapped with the intent; the executor's error stays reachable with errors.Is/As.

type SkillContext added in v0.40.1

type SkillContext struct {
	Intent    string
	State     map[string]string
	ExpiresAt time.Time
}

SkillContext is the multi-turn state container. When a skill returns a ToolResult with FollowupNeeded=true the Service stores its Intent + state under the request's SessionKey. On the user's next transcript the Service reuses the stored Intent rather than running the matcher again — that lets a Timer skill ask "for how long?" and treat the next utterance as the duration answer.

type SkillContextStore added in v0.40.1

type SkillContextStore interface {
	// Get returns the active context for a key. The ok return is false when
	// no context exists or it has expired.
	Get(key string) (SkillContext, bool)
	// Set stores or replaces the context. ExpiresAt is set by the store
	// using its configured TTL — callers do not set it.
	Set(key, intent string, state map[string]string)
	// Clear removes any active context for the key. Idempotent.
	Clear(key string)
}

SkillContextStore is the storage contract for multi-turn follow-ups. InMemorySkillContextStore is the shipped implementation; satellite topologies can swap in a shared backing store.

Scope: keyed by the host's SessionKey (a user id, or a per-device or per-wake-word session id). Multi-tenant installations must include the tenant in the key.

type TTSOptions added in v0.73.0

type TTSOptions struct {
	// Defaults are provider-neutral speech defaults (voice, speed, language)
	// applied to every synthesis.
	Defaults provideropts.Values
	// ProviderOptions holds per-provider overrides keyed by provider id; the
	// router applies the entry of whichever provider wins the request.
	ProviderOptions map[string]provideropts.Values
}

TTSOptions tunes every synthesis the Service performs. Hosts derive it from their speech configuration; the Service clones both maps on construction.

type TTSRouter added in v0.40.1

type TTSRouter interface {
	Synthesize(context.Context, string, tts.SynthesizeOpts) (*tts.Result, error)
}

TTSRouter is the synthesis surface the Service needs; *tts.Router satisfies it.

type ToolCall

type ToolCall struct {
	// Intent is the utility id the matcher recognised (a shortcuts.Intent
	// value for the built-in catalog).
	Intent string
	// Payload is the text that followed the matched codeword phrase.
	Payload string
	// Transcript is the complete utterance the call was derived from. Skills
	// that need the whole command rather than the payload after the trigger
	// phrase (Home Assistant) read it; on a multi-turn follow-up it carries
	// the new turn's text.
	Transcript string
	// Locale is the BCP-47 locale of the request.
	Locale string
	// Selection is the text the user had selected when the request started.
	Selection string
	// Context is the host-composed context block (see ComposeContext); on a
	// follow-up turn the stored skill state is appended to it.
	Context string
	// Target is the host destination for insertion or execution, carried
	// unchanged from the recording that triggered the call. Pass a value
	// implementing [speechkit.OutputTarget]; untyped values are accepted until
	// the field becomes OutputTarget in v0.69.0.
	Target any
}

ToolCall is one deterministic utility invocation handed to a ToolExecutor.

type ToolExecutor

type ToolExecutor interface {
	ExecuteTool(context.Context, ToolCall) (ToolResult, error)
}

ToolExecutor runs a matched ToolCall and produces its ToolResult.

type ToolExecutorFunc

type ToolExecutorFunc func(context.Context, ToolCall) (ToolResult, error)

ToolExecutorFunc adapts a plain function to the ToolExecutor contract.

func (ToolExecutorFunc) ExecuteTool

func (f ToolExecutorFunc) ExecuteTool(ctx context.Context, call ToolCall) (ToolResult, error)

ExecuteTool implements ToolExecutor.

type ToolMatcher

type ToolMatcher interface {
	MatchTool(context.Context, speechkit.AssistRequest) (ToolCall, bool, error)
}

ToolMatcher decides whether a request is a deterministic utility and, if so, which ToolCall to execute.

type ToolMatcherFunc

type ToolMatcherFunc func(context.Context, speechkit.AssistRequest) (ToolCall, bool, error)

ToolMatcherFunc adapts a plain function to the ToolMatcher contract.

func (ToolMatcherFunc) MatchTool

MatchTool implements ToolMatcher.

type ToolResult

type ToolResult struct {
	Text           string
	SpeakText      string
	Action         string
	Kind           string
	Surface        speechkit.AssistSurfaceDecision
	Locale         string
	MessageID      localization.MessageID
	ReasonCode     string
	FollowupNeeded bool
	FollowupState  *FollowupState
}

ToolResult is what a ToolExecutor returns for one ToolCall. Empty SpeakText, Action and Surface are filled in by the Service (Text, "execute" and action_ack respectively).

Jump to

Keyboard shortcuts

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