Files
pipelines/internal/server/chat_shell_run_handlers.go
T
alan e6bc57c901 Run operator shell commands in a chat's directory on the server
chat_shell_run starts $SHELL -lc in the chat session's directory in its own
process group, streams merged output to every watcher of the session, keeps a
1 MiB tail and persists each run; chat_shell_stop interrupts the group and
kills it after a grace period. chat_shell_share queues an ordinary prompt
with the command and a bounded output tail.
2026-11-09 15:55:14 +00:00

142 lines
4.7 KiB
Go

package server
import (
"context"
"errors"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// handleChatShellRunRequest answers the operator shell-command requests. Every
// one names an operator chat session the registry records: the command runs
// only in that chat's own directory on this server's machine, and a run is
// only readable or stoppable through the session that started it.
func handleChatShellRunRequest(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
fail := func(err error) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) }
runner := hub.shellRunner()
if runner == nil {
fail(errors.New("shell commands are not configured on this server"))
return
}
var sessionID string
body := request.ChatShellRunRequest
switch {
case request.Type == protocol.ChatShellRuns && request.ChatQuery != nil:
sessionID = request.ChatQuery.SessionID
case body != nil:
sessionID = body.SessionID
}
if !validChatSessionID(sessionID) {
fail(errors.New("a valid chat session id is required"))
return
}
if registry == nil {
fail(errors.New("operator session registry is not configured on this server"))
return
}
session, found, err := registry.OperatorSession(ctx, sessionID)
if err != nil {
fail(err)
return
}
if !found {
fail(errors.New("operator session is not available"))
return
}
if request.Type != protocol.ChatShellRuns && body == nil {
fail(errors.New("a chat shell request body is required"))
return
}
switch request.Type {
case protocol.ChatShellRun:
record, err := runner.Start(ctx, sessionID, session.Directory, body.Command)
if err != nil {
fail(err)
return
}
_ = write(protocol.Message{Type: protocol.ChatShellRun, ID: request.ID, ChatShellRun: &record})
case protocol.ChatShellStop:
record, err := runner.Stop(sessionID, body.RunID)
if err != nil {
fail(err)
return
}
_ = write(protocol.Message{Type: protocol.ChatShellStop, ID: request.ID, ChatShellRun: &record})
case protocol.ChatShellRuns:
runs, err := runner.List(ctx, sessionID)
if err != nil {
fail(err)
return
}
_ = write(protocol.Message{Type: protocol.ChatShellRuns, ID: request.ID, ChatShellRuns: runs})
case protocol.ChatShellRunOutput:
record, err := runner.Read(ctx, sessionID, body.RunID)
if err != nil {
fail(err)
return
}
_ = write(protocol.Message{Type: protocol.ChatShellRunOutput, ID: request.ID, ChatShellRun: &record})
case protocol.ChatShellShare:
record, err := runner.Read(ctx, sessionID, body.RunID)
if err != nil {
fail(err)
return
}
if record.Status == protocol.ChatShellRunRunning {
fail(errors.New("wait for the command to finish before sending it to the agent"))
return
}
prompt := protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatPrompt: &protocol.ChatPromptRequest{
SessionID: sessionID, Text: chatShellSharePrompt(record), OperationID: body.OperationID,
}}
admitChatPrompt(ctx, prompt, broker, hub, registry, clientID, write, true)
}
}
// List is the session's recorded runs, oldest first. A running run carries
// its live output rather than the last periodic save; every run carries at
// most protocol.ChatShellRunListOutput bytes of its tail.
func (r *chatShellRunner) List(ctx context.Context, sessionID string) ([]protocol.ChatShellRunRecord, error) {
runs, err := r.store.ChatShellRuns(ctx, sessionID)
if err != nil {
return nil, err
}
for i, run := range runs {
if p := r.activeProcess(sessionID, run.ID); p != nil {
runs[i] = p.snapshot(protocol.ChatShellRunListOutput)
continue
}
runs[i] = chatShellRecordTail(run, protocol.ChatShellRunListOutput)
}
return runs, nil
}
// Read is one run of sessionID with its whole retained output.
func (r *chatShellRunner) Read(ctx context.Context, sessionID, runID string) (protocol.ChatShellRunRecord, error) {
if p := r.activeProcess(sessionID, runID); p != nil {
return p.snapshot(protocol.ChatShellRunRetained), nil
}
record, found, err := r.store.ChatShellRun(ctx, sessionID, runID)
if err != nil {
return protocol.ChatShellRunRecord{}, err
}
if !found {
return protocol.ChatShellRunRecord{}, errors.New("this chat has no such command")
}
return record, nil
}
// chatShellRecordTail trims a stored record's output to its last max bytes.
func chatShellRecordTail(record protocol.ChatShellRunRecord, max int) protocol.ChatShellRunRecord {
if len(record.Output) <= max {
return record
}
out := newChatShellOutput(len(record.Output))
out.write([]byte(record.Output))
text, from := out.tail(max)
record.Output = text
record.OutputStart += from
record.Truncated = true
return record
}