Extended tentacle capabilities a bit.

This commit is contained in:
Storm Dragon committed 2026-10-22 21:13:08 -04:00
1 parent d7b1efeba0
commit 1d75168807
44 files changed
+2874 -88

No files matched your search

+44 -4
View File
@@ -10,6 +10,7 @@ import (
"context"
"encoding/json"
"errors"
"git.stormux.org/storm/cthulhu/internal/config"
"io"
"strconv"
"sync"
@@ -28,6 +29,16 @@ type Binding struct {
// Declare Command.Presentation and obtain the local host grant before using it.
// Uninterruptible protects accepted speech through ordinary input/focus changes;
// session revocation and core lifecycle/output policy still take precedence.
type Extensions = tentaclewire.Extensions
type PreferencePage = tentaclewire.PreferencePage
type QuickSetting = tentaclewire.QuickSetting
type ReaderRequest = tentaclewire.ReaderRequest
type ReaderState = tentaclewire.ReaderState
type ExtensionEvent = tentaclewire.ExtensionEvent
type SpeechConfiguration = tentaclewire.SpeechConfiguration
type Voice = config.Voice
type VoiceFamily = config.VoiceFamily
type Presentation = tentaclewire.Presentation
// Interaction requires the explicit interaction grant as well as presentation.
@@ -60,6 +71,7 @@ type Command struct {
Interaction bool `json:"interaction,omitempty"`
// Settings exposes this interactive command through global Preferences.
// Register at most one; its handler opens a form without requiring arguments.
Reader bool `json:"reader,omitempty"`
Settings bool `json:"settings,omitempty"`
Handle Handler `json:"-"`
}
@@ -78,21 +90,35 @@ type request struct {
// handlers and waits for them; the host kills a child that ignores cancellation.
// Never print to stdout. Command declarations must remain unchanged during Serve.
func Serve(input io.ReadCloser, output io.WriteCloser, commands []Command) error {
return serve(input, output, commands, nil)
}
// ServeWithExtensions declares session-owned preferences pages and quick settings.
func ServeWithExtensions(input io.ReadCloser, output io.WriteCloser, commands []Command, extensions Extensions) error {
if err := tentaclewire.ValidateExtensions(extensions); err != nil {
return err
}
return serve(input, output, commands, &extensions)
}
func serve(input io.ReadCloser, output io.WriteCloser, commands []Command, extensions *Extensions) error {
ctx, cancel := context.WithCancel(context.Background())
var workers sync.WaitGroup
var writeMutex sync.Mutex
defer func() { cancel(); input.Close(); output.Close(); workers.Wait() }()
handlers := make(map[string]Handler)
readerCommands := make(map[string]bool)
if len(commands) == 0 || len(commands) > 32 {
return ErrProtocol
}
settings := false
for _, command := range commands {
if command.Settings && (!command.Interaction || !command.Presentation || settings) || command.ID == "" || command.Handle == nil || handlers[command.ID] != nil {
if command.Reader && !command.Interaction || command.Settings && (!command.Interaction || !command.Presentation || settings) || command.ID == "" || command.Handle == nil || handlers[command.ID] != nil {
return ErrProtocol
}
settings = settings || command.Settings
handlers[command.ID] = command.Handle
readerCommands[command.ID] = command.Reader
}
writeReply := func(id string, value any, failed bool, release func()) error {
reply := map[string]any{"jsonrpc": "2.0", "id": id, "result": value}
@@ -169,12 +195,19 @@ func Serve(input io.ReadCloser, output io.WriteCloser, commands []Command) error
return ErrProtocol
}
for _, command := range commands {
if command.Presentation && len(params.APIs) < 2 || command.Interaction && (!command.Presentation || len(params.APIs) != 3) {
if command.Presentation && len(params.APIs) < 2 || command.Interaction && (!command.Presentation || len(params.APIs) < 3) || command.Reader && len(params.APIs) != 4 {
return ErrProtocol
}
}
if extensions != nil && len(params.APIs) != 4 {
return ErrProtocol
}
session = params.Session
if err := writeReply(message.ID, map[string]any{"protocol": params.Protocol, "session": session, "commands": commands}, false, nil); err != nil {
registration := map[string]any{"protocol": params.Protocol, "session": session, "commands": commands}
if extensions != nil {
registration["extensions"] = extensions
}
if err := writeReply(message.ID, registration, false, nil); err != nil {
return err
}
case "initialized":
@@ -190,7 +223,7 @@ func Serve(input io.ReadCloser, output io.WriteCloser, commands []Command) error
Command string `json:"command"`
Arguments json.RawMessage `json:"arguments"`
}
if !active || message.ID == "" || tentaclewire.DecodeStrict(message.Params, &params) != nil || len(params.Arguments) == 0 || len(params.Arguments) > 16*1024 || handlers[params.Command] == nil {
if !active || message.ID == "" || tentaclewire.DecodeStrict(message.Params, &params) != nil || len(params.Arguments) == 0 || len(params.Arguments) > command_argument_limit(readerCommands[params.Command]) || handlers[params.Command] == nil {
return ErrProtocol
}
select {
@@ -235,3 +268,10 @@ func call_handler(ctx context.Context, handler Handler, arguments json.RawMessag
}()
return handler(ctx, arguments)
}
func command_argument_limit(reader bool) int {
if reader {
return 48 * 1024
}
return 16 * 1024
}