998 lines
30 KiB
Go
998 lines
30 KiB
Go
// Package codex brokers an operator chat through the Codex App Server.
|
|
//
|
|
// Codex exposes threads, turns and items over JSON-RPC. The App Server is
|
|
// normally reached over a Unix WebSocket socket (the desktop app and the CLI use
|
|
// the same control socket). This adapter owns one client connection and mirrors
|
|
// the protocol into the Pipelines chat frames; it never starts a second Codex
|
|
// daemon or changes the operator's Codex configuration.
|
|
package codex
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
// Backend is the wire value stored in the operator-session registry.
|
|
const Backend = "codex"
|
|
|
|
// DefaultSocketPath is the Codex desktop app-server control socket.
|
|
func DefaultSocketPath() string {
|
|
home := os.Getenv("CODEX_HOME")
|
|
if home == "" {
|
|
userHome, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
home = filepath.Join(userHome, ".codex")
|
|
}
|
|
return filepath.Join(home, "app-server-control", "app-server-control.sock")
|
|
}
|
|
|
|
// Chat is one owned App Server connection.
|
|
type Chat struct {
|
|
conn *websocket.Conn
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
writeMu sync.Mutex
|
|
nextID int64
|
|
|
|
mu sync.Mutex
|
|
pending map[string]chan rpcResponse
|
|
subs map[string]map[chan protocol.ChatEventFrame]struct{}
|
|
forms map[string]pendingForm
|
|
approvals map[string]pendingApproval
|
|
commands map[string]map[string]commandSnapshot
|
|
commandOrder uint64
|
|
starting map[string]bool
|
|
turns map[string]string
|
|
emitted map[string]bool
|
|
closed bool
|
|
connectionErr error
|
|
compactions map[string]protocol.ChatEventFrame
|
|
endedTurns map[string]string
|
|
usage map[string]usageSnapshot
|
|
usageOrder uint64
|
|
modes map[string]string
|
|
}
|
|
|
|
type pendingForm struct {
|
|
requestID json.RawMessage
|
|
threadID string
|
|
form protocol.ChatForm
|
|
answering bool
|
|
elicitation bool
|
|
}
|
|
|
|
type rpcResponse struct {
|
|
Result json.RawMessage
|
|
Error *rpcError
|
|
Transport bool
|
|
}
|
|
|
|
type rpcError struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
type rpcCallError struct {
|
|
Code int
|
|
Message string
|
|
}
|
|
|
|
func (e *rpcCallError) Error() string { return e.Message }
|
|
|
|
type rpcMessage struct {
|
|
ID json.RawMessage `json:"id"`
|
|
Method string `json:"method"`
|
|
Params json.RawMessage `json:"params"`
|
|
Result json.RawMessage `json:"result"`
|
|
Error *rpcError `json:"error"`
|
|
}
|
|
|
|
// Open connects to the app-server socket and performs the initialize handshake.
|
|
func Open(ctx context.Context) (*Chat, error) {
|
|
socket := DefaultSocketPath()
|
|
if socket == "" {
|
|
return nil, errors.New("Codex home is unavailable")
|
|
}
|
|
transport := &http.Transport{
|
|
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
return (&net.Dialer{}).DialContext(ctx, "unix", socket)
|
|
},
|
|
}
|
|
client := &http.Client{Transport: transport}
|
|
conn, _, err := websocket.Dial(ctx, "ws://localhost/", &websocket.DialOptions{HTTPClient: client})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("connect Codex app-server: %w", err)
|
|
}
|
|
conn.SetReadLimit(16 << 20)
|
|
child, cancel := context.WithCancel(ctx)
|
|
c := &Chat{
|
|
conn: conn, ctx: child, cancel: cancel,
|
|
pending: map[string]chan rpcResponse{},
|
|
subs: map[string]map[chan protocol.ChatEventFrame]struct{}{},
|
|
forms: map[string]pendingForm{},
|
|
turns: map[string]string{},
|
|
emitted: map[string]bool{},
|
|
}
|
|
go c.readLoop()
|
|
initCtx, initCancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer initCancel()
|
|
if _, err := c.call(initCtx, "initialize", map[string]any{
|
|
"clientInfo": map[string]any{"name": "pipelines", "title": "Pipelines", "version": "0.1"},
|
|
}); err != nil {
|
|
c.Close()
|
|
return nil, fmt.Errorf("initialize Codex app-server: %w", err)
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
// Available reports whether the app-server control socket exists.
|
|
func Available() bool {
|
|
path := DefaultSocketPath()
|
|
if path == "" {
|
|
return false
|
|
}
|
|
info, err := os.Stat(path)
|
|
return err == nil && info.Mode()&os.ModeSocket != 0
|
|
}
|
|
|
|
// Close shuts the owned client connection down.
|
|
func (c *Chat) Close() {
|
|
c.mu.Lock()
|
|
if c.closed {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
c.closed = true
|
|
c.usage = nil
|
|
c.mu.Unlock()
|
|
c.cancel()
|
|
_ = c.conn.Close(websocket.StatusNormalClosure, "")
|
|
}
|
|
|
|
func (c *Chat) readLoop() {
|
|
for {
|
|
_, data, err := c.conn.Read(c.ctx)
|
|
if err != nil {
|
|
c.failPending(err)
|
|
return
|
|
}
|
|
var msg rpcMessage
|
|
if err := json.Unmarshal(data, &msg); err != nil {
|
|
continue
|
|
}
|
|
if len(msg.ID) > 0 && msg.Method != "" {
|
|
c.handleServerRequest(msg)
|
|
continue
|
|
}
|
|
if len(msg.ID) > 0 {
|
|
key := string(msg.ID)
|
|
c.mu.Lock()
|
|
ch := c.pending[key]
|
|
delete(c.pending, key)
|
|
c.mu.Unlock()
|
|
if ch != nil {
|
|
ch <- rpcResponse{Result: msg.Result, Error: msg.Error}
|
|
}
|
|
continue
|
|
}
|
|
if msg.Method != "" {
|
|
c.handleNotification(msg)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Chat) failPending(err error) {
|
|
c.mu.Lock()
|
|
c.compactions = nil
|
|
c.usage = nil
|
|
c.connectionErr = err
|
|
c.approvals = nil
|
|
c.forms = map[string]pendingForm{}
|
|
pending := c.pending
|
|
c.pending = map[string]chan rpcResponse{}
|
|
subs := c.subs
|
|
c.subs = map[string]map[chan protocol.ChatEventFrame]struct{}{}
|
|
c.mu.Unlock()
|
|
for _, ch := range pending {
|
|
ch <- rpcResponse{Error: &rpcError{Message: err.Error()}, Transport: true}
|
|
}
|
|
for _, set := range subs {
|
|
for ch := range set {
|
|
close(ch)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Chat) call(ctx context.Context, method string, params any) (json.RawMessage, error) {
|
|
c.mu.Lock()
|
|
if c.closed {
|
|
c.mu.Unlock()
|
|
return nil, errors.New("Codex connection is closed")
|
|
}
|
|
c.nextID++
|
|
id := c.nextID
|
|
ch := make(chan rpcResponse, 1)
|
|
c.pending[strconv.FormatInt(id, 10)] = ch
|
|
c.mu.Unlock()
|
|
|
|
payload := map[string]any{"id": id, "method": method}
|
|
if params != nil {
|
|
payload["params"] = params
|
|
}
|
|
data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
c.writeMu.Lock()
|
|
err = c.conn.Write(ctx, websocket.MessageText, data)
|
|
c.writeMu.Unlock()
|
|
if err != nil {
|
|
c.mu.Lock()
|
|
delete(c.pending, strconv.FormatInt(id, 10))
|
|
c.mu.Unlock()
|
|
return nil, err
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
c.mu.Lock()
|
|
delete(c.pending, strconv.FormatInt(id, 10))
|
|
c.mu.Unlock()
|
|
return nil, ctx.Err()
|
|
case resp := <-ch:
|
|
if resp.Error != nil {
|
|
if resp.Transport {
|
|
return nil, errors.New(resp.Error.Message)
|
|
}
|
|
return nil, &rpcCallError{Code: resp.Error.Code, Message: resp.Error.Message}
|
|
}
|
|
return resp.Result, nil
|
|
}
|
|
}
|
|
|
|
func (c *Chat) respond(id json.RawMessage, result any) {
|
|
payload := map[string]any{"id": id, "result": result}
|
|
data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return
|
|
}
|
|
c.writeMu.Lock()
|
|
_ = c.conn.Write(c.ctx, websocket.MessageText, data)
|
|
c.writeMu.Unlock()
|
|
}
|
|
|
|
func (c *Chat) emit(frame protocol.ChatEventFrame) {
|
|
c.mu.Lock()
|
|
if frame.Kind == protocol.ChatEventUsage && !c.retainUsageLocked(frame) {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
if frame.Kind == protocol.ChatEventCompaction {
|
|
if frame.Compaction == nil {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
if frame.NativeTurnID != "" && c.endedTurns[frame.SessionID] == frame.NativeTurnID {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
if current := c.turns[frame.SessionID]; current != "" && frame.NativeTurnID != "" && current != frame.NativeTurnID {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
if frame.Compaction.Status == "running" {
|
|
if c.compactions == nil {
|
|
c.compactions = map[string]protocol.ChatEventFrame{}
|
|
}
|
|
c.compactions[frame.SessionID] = frame
|
|
} else {
|
|
previous := c.compactions[frame.SessionID]
|
|
if previous.Compaction != nil && frame.Compaction.ID != "" && previous.Compaction.ID != frame.Compaction.ID {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
delete(c.compactions, frame.SessionID)
|
|
}
|
|
}
|
|
if frame.Kind == protocol.ChatEventStep && frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded {
|
|
delete(c.compactions, frame.SessionID)
|
|
if frame.Step.TurnID != "" {
|
|
if c.endedTurns == nil {
|
|
c.endedTurns = map[string]string{}
|
|
}
|
|
c.endedTurns[frame.SessionID] = frame.Step.TurnID
|
|
}
|
|
}
|
|
set := c.subs[frame.SessionID]
|
|
var targets []chan protocol.ChatEventFrame
|
|
for ch := range set {
|
|
targets = append(targets, ch)
|
|
}
|
|
c.mu.Unlock()
|
|
for _, ch := range targets {
|
|
select {
|
|
case ch <- frame:
|
|
default:
|
|
// A slow client must not block the shared Codex connection. The hub
|
|
// will repair from history on its next subscription.
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Chat) subscribe(id string) chan protocol.ChatEventFrame {
|
|
ch := make(chan protocol.ChatEventFrame, 256)
|
|
c.mu.Lock()
|
|
if c.subs[id] == nil {
|
|
c.subs[id] = map[chan protocol.ChatEventFrame]struct{}{}
|
|
}
|
|
c.subs[id][ch] = struct{}{}
|
|
if snapshot, ok := c.usage[id]; ok {
|
|
ch <- snapshot.frame
|
|
}
|
|
if frame, ok := c.compactions[id]; ok {
|
|
ch <- frame
|
|
}
|
|
c.mu.Unlock()
|
|
return ch
|
|
}
|
|
|
|
func (c *Chat) unsubscribe(id string, ch chan protocol.ChatEventFrame) {
|
|
c.mu.Lock()
|
|
if set := c.subs[id]; set != nil {
|
|
delete(set, ch)
|
|
if len(set) == 0 {
|
|
delete(c.subs, id)
|
|
}
|
|
}
|
|
c.mu.Unlock()
|
|
}
|
|
|
|
// Stream exposes one thread's notifications as chat frames.
|
|
func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error {
|
|
return c.streamWithReadback(ctx, id, ready, emit)
|
|
}
|
|
|
|
// Prompt starts one turn. The user turn is already broadcast by the server, so
|
|
// the adapter only forwards the assistant's items.
|
|
func (c *Chat) Prompt(ctx context.Context, id, text string) error {
|
|
return c.PromptWithMessageID(ctx, id, text, "")
|
|
}
|
|
|
|
// PromptWithMessageID carries the client's native user-message id so the
|
|
// optimistic browser bubble and the authoritative history item can reconcile.
|
|
func (c *Chat) PromptWithMessageID(ctx context.Context, id, text, messageID string) error {
|
|
return c.PromptWithMessageIDModel(ctx, id, text, messageID, "")
|
|
}
|
|
|
|
// PromptWithMessageIDModel applies a persisted per-chat choice at the explicit
|
|
// next-turn boundary. It does not resume a thread or edit global configuration.
|
|
func (c *Chat) PromptWithMessageIDModel(ctx context.Context, id, text, messageID, model string) error {
|
|
return c.PromptWithMessageIDModelFiles(ctx, id, text, messageID, model, nil)
|
|
}
|
|
|
|
// PromptWithMessageIDFiles submits server-prepared attachments without changing
|
|
// provider policy or configuration.
|
|
func (c *Chat) PromptWithMessageIDFiles(ctx context.Context, id, text, messageID string, files []opencode.PromptFile) error {
|
|
return c.PromptWithMessageIDModelFiles(ctx, id, text, messageID, "", files)
|
|
}
|
|
|
|
// PromptWithMessageIDModelFiles keeps attachment and model overrides on one turn.
|
|
func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, messageID, model string, files []opencode.PromptFile) error {
|
|
input, err := promptInput(text, files)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
params := map[string]any{
|
|
"threadId": id,
|
|
"input": input,
|
|
}
|
|
if messageID != "" {
|
|
params["clientUserMessageId"] = messageID
|
|
}
|
|
if model != "" {
|
|
params["model"] = model
|
|
}
|
|
c.applyMode(id, params)
|
|
c.mu.Lock()
|
|
if c.starting == nil {
|
|
c.starting = map[string]bool{}
|
|
}
|
|
c.starting[id] = true
|
|
c.mu.Unlock()
|
|
raw, err := c.call(ctx, "turn/start", params)
|
|
// A read-only browser connection may point at a persisted, unloaded thread.
|
|
// Only an explicit Send can load it. Retry solely after the provider's exact
|
|
// application-level refusal: transport/timeout failures may have admitted a
|
|
// turn and must never be blindly repeated.
|
|
var refused *rpcCallError
|
|
if errors.As(err, &refused) && refused.Message == "thread not found: "+id {
|
|
var resumed json.RawMessage
|
|
resumed, err = c.call(ctx, "thread/resume", map[string]any{"threadId": id, "excludeTurns": true})
|
|
if err == nil {
|
|
var response struct {
|
|
Thread struct {
|
|
ID string `json:"id"`
|
|
} `json:"thread"`
|
|
}
|
|
if json.Unmarshal(resumed, &response) != nil || response.Thread.ID != id {
|
|
err = errors.New("Codex did not confirm the requested thread was loaded")
|
|
} else {
|
|
raw, err = c.call(ctx, "turn/start", params)
|
|
}
|
|
}
|
|
}
|
|
var reply struct {
|
|
Turn struct {
|
|
ID string `json:"id"`
|
|
} `json:"turn"`
|
|
}
|
|
if err == nil {
|
|
_ = json.Unmarshal(raw, &reply)
|
|
}
|
|
c.mu.Lock()
|
|
delete(c.starting, id)
|
|
if reply.Turn.ID != "" {
|
|
c.turns[id] = reply.Turn.ID
|
|
}
|
|
c.mu.Unlock()
|
|
if err == nil && reply.Turn.ID != "" {
|
|
c.emit(stepFrame(id, protocol.ChatStepStarted, reply.Turn.ID))
|
|
}
|
|
return err
|
|
}
|
|
|
|
// ActivateSkill is an OpenCode-only chat concept.
|
|
func (c *Chat) ActivateSkill(context.Context, string, string) error {
|
|
return errors.New("Codex chat has no out-of-band skill activation")
|
|
}
|
|
|
|
// Interrupt cancels the thread's active turn.
|
|
func (c *Chat) Interrupt(ctx context.Context, id string) error {
|
|
c.mu.Lock()
|
|
turn := c.turns[id]
|
|
c.mu.Unlock()
|
|
if turn == "" {
|
|
return nil
|
|
}
|
|
_, err := c.call(ctx, "turn/interrupt", map[string]any{"threadId": id, "turnId": turn})
|
|
return err
|
|
}
|
|
|
|
// Create starts one Codex thread in directory.
|
|
func (c *Chat) Create(ctx context.Context, title, directory string) (protocol.ChatSession, error) {
|
|
if strings.TrimSpace(directory) == "" {
|
|
return protocol.ChatSession{}, errors.New("a directory is required")
|
|
}
|
|
raw, err := c.call(ctx, "thread/start", map[string]any{
|
|
"cwd": directory,
|
|
"sandbox": "workspace-write",
|
|
})
|
|
if err != nil {
|
|
return protocol.ChatSession{}, err
|
|
}
|
|
var result struct {
|
|
Thread thread `json:"thread"`
|
|
Model string `json:"model"`
|
|
ModelProvider string `json:"modelProvider"`
|
|
}
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
return protocol.ChatSession{}, err
|
|
}
|
|
session := result.Thread.chatSession()
|
|
if session.Model == "" {
|
|
session.Model = result.Model
|
|
}
|
|
if session.ModelProvider == "" {
|
|
session.ModelProvider = result.ModelProvider
|
|
}
|
|
session.Backend = Backend
|
|
if strings.TrimSpace(title) != "" {
|
|
session.Title = strings.TrimSpace(title)
|
|
}
|
|
if title != "" {
|
|
_, _ = c.call(ctx, "thread/name/set", map[string]any{"threadId": session.ID, "name": title})
|
|
}
|
|
return session, nil
|
|
}
|
|
|
|
// Session returns one thread's current record.
|
|
func (c *Chat) Session(ctx context.Context, id string) (protocol.ChatSession, bool, error) {
|
|
raw, err := c.call(ctx, "thread/read", map[string]any{"threadId": id})
|
|
if err != nil {
|
|
return protocol.ChatSession{}, false, err
|
|
}
|
|
var result struct {
|
|
Thread thread `json:"thread"`
|
|
}
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
return protocol.ChatSession{}, false, err
|
|
}
|
|
if result.Thread.ID == "" {
|
|
return protocol.ChatSession{}, false, nil
|
|
}
|
|
session := result.Thread.chatSession()
|
|
session.Backend = Backend
|
|
return session, true, nil
|
|
}
|
|
|
|
// Sessions lists Codex threads (bounded) so the server can intersect them with
|
|
// the operator registry.
|
|
func (c *Chat) Sessions(ctx context.Context) ([]protocol.ChatSession, error) {
|
|
raw, err := c.call(ctx, "thread/list", map[string]any{"limit": 200})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result struct {
|
|
Data []thread `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
sessions := make([]protocol.ChatSession, 0, len(result.Data))
|
|
for _, t := range result.Data {
|
|
session := t.chatSession()
|
|
session.Backend = Backend
|
|
sessions = append(sessions, session)
|
|
}
|
|
return sessions, nil
|
|
}
|
|
|
|
// Messages reads a thread's turns and flattens them into chat messages.
|
|
func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string) (protocol.ChatHistoryPage, error) {
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
if limit > 100 {
|
|
limit = 100
|
|
}
|
|
params := map[string]any{"threadId": id, "limit": limit, "sortDirection": "desc", "itemsView": "full"}
|
|
if cursor != "" {
|
|
params["cursor"] = cursor
|
|
}
|
|
raw, err := c.call(ctx, "thread/turns/list", params)
|
|
if err != nil {
|
|
return protocol.ChatHistoryPage{}, err
|
|
}
|
|
var result struct {
|
|
Data []turn `json:"data"`
|
|
NextCursor *string `json:"nextCursor"`
|
|
}
|
|
if err := json.Unmarshal(raw, &result); err != nil {
|
|
return protocol.ChatHistoryPage{}, err
|
|
}
|
|
if result.Data == nil {
|
|
return protocol.ChatHistoryPage{}, errors.New("invalid Codex history page")
|
|
}
|
|
messages := []protocol.ChatMessage{}
|
|
for index := len(result.Data) - 1; index >= 0; index-- {
|
|
turn := result.Data[index]
|
|
for _, item := range turn.Items {
|
|
if msg, ok := item.chatMessage(id); ok {
|
|
messages = append(messages, msg)
|
|
}
|
|
}
|
|
if turn.ID != "" && rawText(turn.Error) != "" {
|
|
part := item{ID: turn.ID + "#error", Type: "Codex turn", Status: "failed", Error: turn.Error, Input: map[string]any{"turn_id": turn.ID}}.toolPart(true)
|
|
messages = append(messages, protocol.ChatMessage{ID: part.ID, SessionID: id, Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{part}})
|
|
}
|
|
}
|
|
page := protocol.ChatHistoryPage{Messages: messages}
|
|
if result.NextCursor != nil {
|
|
page.NextCursor = *result.NextCursor
|
|
page.HasMore = page.NextCursor != ""
|
|
}
|
|
return page, nil
|
|
}
|
|
|
|
// Answer replies to a pending requestUserInput request.
|
|
func (c *Chat) Answer(ctx context.Context, request protocol.ChatAnswerRequest) error {
|
|
if request.Action != "" && request.Action != "accept" && request.Action != "cancel" && request.Action != "decline" {
|
|
return errors.New("invalid form action")
|
|
}
|
|
c.mu.Lock()
|
|
pending, ok := c.forms[request.FormID]
|
|
if request.Action != "" && request.Action != "accept" && (!ok || !pending.elicitation) {
|
|
c.mu.Unlock()
|
|
return errors.New("this Codex question does not support that action")
|
|
}
|
|
if ok && pending.threadID == request.SessionID && !pending.answering {
|
|
pending.answering = true
|
|
c.forms[request.FormID] = pending
|
|
} else {
|
|
ok = false
|
|
}
|
|
c.mu.Unlock()
|
|
if !ok {
|
|
return errors.New("the Codex question is no longer awaiting an answer")
|
|
}
|
|
answers := map[string]any{}
|
|
for key, value := range request.Answers {
|
|
switch v := value.(type) {
|
|
case []any:
|
|
answers[key] = map[string]any{"answers": v}
|
|
case []string:
|
|
answers[key] = map[string]any{"answers": v}
|
|
case string:
|
|
answers[key] = map[string]any{"answers": []string{v}}
|
|
default:
|
|
answers[key] = map[string]any{"answers": []string{fmt.Sprint(v)}}
|
|
}
|
|
}
|
|
var result any = map[string]any{"answers": answers}
|
|
if pending.elicitation {
|
|
action := request.Action
|
|
if action == "" {
|
|
action = "accept"
|
|
}
|
|
var content any = request.Answers
|
|
if action != "accept" || pending.form.Kind == "elicitation-url" {
|
|
content = nil
|
|
}
|
|
result = map[string]any{"action": action, "content": content, "_meta": nil}
|
|
}
|
|
err := c.respondResult(ctx, pending.requestID, result)
|
|
c.mu.Lock()
|
|
if err == nil {
|
|
delete(c.forms, request.FormID)
|
|
} else if _, exists := c.forms[request.FormID]; exists {
|
|
pending.answering = false
|
|
c.forms[request.FormID] = pending
|
|
}
|
|
c.mu.Unlock()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.emit(protocol.ChatEventFrame{
|
|
SessionID: pending.threadID,
|
|
Kind: protocol.ChatEventForm,
|
|
Form: &protocol.ChatForm{ID: pending.form.ID, SessionID: pending.threadID, Resolved: true},
|
|
})
|
|
return nil
|
|
}
|
|
|
|
func (c *Chat) handleServerRequest(msg rpcMessage) {
|
|
switch msg.Method {
|
|
case "item/tool/requestUserInput":
|
|
var params struct {
|
|
ThreadID string `json:"threadId"`
|
|
TurnID string `json:"turnId"`
|
|
ItemID string `json:"itemId"`
|
|
Questions []struct {
|
|
ID string `json:"id"`
|
|
Header string `json:"header"`
|
|
Question string `json:"question"`
|
|
IsOther bool `json:"isOther"`
|
|
IsSecret bool `json:"isSecret"`
|
|
Options []struct {
|
|
Value string `json:"value"`
|
|
Label string `json:"label"`
|
|
Description string `json:"description"`
|
|
} `json:"options"`
|
|
} `json:"questions"`
|
|
}
|
|
if err := json.Unmarshal(msg.Params, ¶ms); err != nil || params.ThreadID == "" || params.ItemID == "" {
|
|
c.respond(msg.ID, map[string]any{"answers": map[string]any{}})
|
|
return
|
|
}
|
|
form := protocol.ChatForm{
|
|
ID: params.ItemID,
|
|
SessionID: params.ThreadID,
|
|
Title: "Question",
|
|
Kind: "question",
|
|
}
|
|
for _, q := range params.Questions {
|
|
field := protocol.ChatFormField{
|
|
Key: q.ID, Title: q.Question, Description: q.Header,
|
|
Type: "string", Custom: q.IsOther, Required: true,
|
|
Secret: q.IsSecret,
|
|
}
|
|
for _, option := range q.Options {
|
|
value := option.Value
|
|
if value == "" {
|
|
value = option.Label
|
|
}
|
|
field.Options = append(field.Options, protocol.ChatFormOption{
|
|
Value: value, Label: option.Label, Description: option.Description,
|
|
})
|
|
}
|
|
form.Fields = append(form.Fields, field)
|
|
}
|
|
c.mu.Lock()
|
|
c.forms[form.ID] = pendingForm{requestID: msg.ID, threadID: params.ThreadID, form: form}
|
|
c.mu.Unlock()
|
|
c.emit(protocol.ChatEventFrame{SessionID: params.ThreadID, Kind: protocol.ChatEventForm, Form: &form})
|
|
case "item/commandExecution/requestApproval", "item/fileChange/requestApproval", "item/permissions/requestApproval":
|
|
c.receiveApproval(msg)
|
|
case "mcpServer/elicitation/request":
|
|
c.receiveElicitation(msg)
|
|
default:
|
|
// Unknown server requests must not stall the shared connection.
|
|
c.respondUnsupported(msg.ID)
|
|
}
|
|
}
|
|
|
|
func (c *Chat) handleNotification(msg rpcMessage) {
|
|
if msg.Method == "thread/tokenUsage/updated" {
|
|
c.receiveUsage(msg.Params)
|
|
return
|
|
}
|
|
if msg.Method == "serverRequest/resolved" {
|
|
var p struct {
|
|
ThreadID string `json:"threadId"`
|
|
RequestID json.RawMessage `json:"requestId"`
|
|
}
|
|
if json.Unmarshal(msg.Params, &p) == nil && p.ThreadID != "" && len(p.RequestID) > 0 {
|
|
c.resolveInteraction(p.ThreadID, p.RequestID)
|
|
}
|
|
return
|
|
}
|
|
var params struct {
|
|
ThreadID string `json:"threadId"`
|
|
TurnID string `json:"turnId"`
|
|
ItemID string `json:"itemId"`
|
|
Delta string `json:"delta"`
|
|
Item json.RawMessage `json:"item"`
|
|
Turn struct {
|
|
ID string `json:"id"`
|
|
Status string `json:"status"`
|
|
} `json:"turn"`
|
|
Status struct {
|
|
Type string `json:"type"`
|
|
} `json:"status"`
|
|
}
|
|
if err := json.Unmarshal(msg.Params, ¶ms); err != nil || params.ThreadID == "" {
|
|
return
|
|
}
|
|
switch msg.Method {
|
|
case "turn/started":
|
|
c.mu.Lock()
|
|
c.turns[params.ThreadID] = params.TurnID
|
|
if c.turns[params.ThreadID] == "" {
|
|
c.turns[params.ThreadID] = params.Turn.ID
|
|
}
|
|
nativeID := c.turns[params.ThreadID]
|
|
c.mu.Unlock()
|
|
c.emit(stepFrame(params.ThreadID, "started", nativeID))
|
|
case "turn/completed":
|
|
completedID := params.TurnID
|
|
if completedID == "" {
|
|
completedID = params.Turn.ID
|
|
}
|
|
if completedID == "" {
|
|
return
|
|
}
|
|
c.mu.Lock()
|
|
if current := c.turns[params.ThreadID]; current != "" && current != completedID {
|
|
c.mu.Unlock()
|
|
return
|
|
}
|
|
delete(c.turns, params.ThreadID)
|
|
c.mu.Unlock()
|
|
c.emit(stepFrame(params.ThreadID, protocol.ChatStepEnded, completedID))
|
|
case "item/started":
|
|
c.handleItem(params.ThreadID, params.ItemID, params.Item, false, params.TurnID)
|
|
case "item/completed":
|
|
c.handleItem(params.ThreadID, params.ItemID, params.Item, true, params.TurnID)
|
|
case "thread/compacted":
|
|
// Deprecated ContextCompactedNotification has only threadId/turnId.
|
|
// Modern item lifecycles are correlated more precisely; never let this
|
|
// unkeyed legacy duplicate clear a newer modern compaction item.
|
|
c.mu.Lock()
|
|
modern := c.compactions[params.ThreadID].Compaction != nil
|
|
c.mu.Unlock()
|
|
if !modern {
|
|
c.emit(protocol.ChatEventFrame{SessionID: params.ThreadID, NativeTurnID: params.TurnID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "completed"}})
|
|
}
|
|
case "item/agentMessage/delta", "item/agentMessage/streaming":
|
|
if params.ItemID != "" && params.Delta != "" {
|
|
c.markEmitted(params.ItemID)
|
|
c.emit(textFrame(params.ThreadID, params.ItemID, protocol.ChatEventText, params.Delta))
|
|
}
|
|
case "item/reasoning/delta":
|
|
if params.ItemID != "" && params.Delta != "" {
|
|
c.markEmitted(params.ItemID)
|
|
c.emit(textFrame(params.ThreadID, params.ItemID, protocol.ChatEventReasoning, params.Delta))
|
|
}
|
|
case "item/commandExecution/outputDelta":
|
|
c.commandDelta(params.ThreadID, params.ItemID, params.Delta)
|
|
}
|
|
}
|
|
|
|
func (c *Chat) handleItem(threadID, itemID string, raw json.RawMessage, completed bool, nativeTurnID ...string) {
|
|
var record item
|
|
if err := json.Unmarshal(raw, &record); err != nil {
|
|
return
|
|
}
|
|
if record.ID == "" {
|
|
record.ID = itemID
|
|
}
|
|
switch record.Type {
|
|
case "contextCompaction":
|
|
if record.ID == "" {
|
|
return
|
|
}
|
|
status := "running"
|
|
if completed {
|
|
status = "completed"
|
|
}
|
|
frame := protocol.ChatEventFrame{SessionID: threadID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: status, ID: record.ID}}
|
|
if len(nativeTurnID) > 0 {
|
|
frame.NativeTurnID = nativeTurnID[0]
|
|
}
|
|
c.emit(frame)
|
|
case "agentMessage":
|
|
if completed && record.Text != "" && !c.wasEmitted(record.ID) {
|
|
c.emit(textFrame(threadID, record.ID, protocol.ChatEventText, record.Text))
|
|
}
|
|
case "reasoning":
|
|
text := record.reasoningText()
|
|
if completed && text != "" && !c.wasEmitted(record.ID) {
|
|
c.emit(textFrame(threadID, record.ID, protocol.ChatEventReasoning, text))
|
|
}
|
|
case "userMessage":
|
|
return
|
|
default:
|
|
if record.Type == "commandExecution" {
|
|
c.recordCommand(threadID, record, true)
|
|
}
|
|
part := record.toolPart(completed)
|
|
c.emit(protocol.ChatEventFrame{
|
|
SessionID: threadID, MessageID: record.ID, Kind: protocol.ChatEventTool, Part: &part,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (c *Chat) markEmitted(id string) {
|
|
c.mu.Lock()
|
|
c.emitted[id] = true
|
|
c.mu.Unlock()
|
|
}
|
|
|
|
func (c *Chat) wasEmitted(id string) bool {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
return c.emitted[id]
|
|
}
|
|
|
|
func textFrame(threadID, itemID, kind, delta string) protocol.ChatEventFrame {
|
|
return protocol.ChatEventFrame{
|
|
SessionID: threadID, MessageID: itemID, Kind: kind, Delta: delta,
|
|
Part: &protocol.ChatPart{Type: kind, ID: protocol.ChatDeltaPartID(itemID, kind, 0)},
|
|
}
|
|
}
|
|
|
|
func stepFrame(threadID, status string, nativeID ...string) protocol.ChatEventFrame {
|
|
step := &protocol.ChatStep{Status: status}
|
|
if len(nativeID) > 0 {
|
|
step.TurnID = nativeID[0]
|
|
}
|
|
return protocol.ChatEventFrame{
|
|
SessionID: threadID, Kind: protocol.ChatEventStep,
|
|
Step: step,
|
|
}
|
|
}
|
|
|
|
type thread struct {
|
|
Model string `json:"model"`
|
|
ModelProvider string `json:"modelProvider"`
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Preview string `json:"preview"`
|
|
Cwd string `json:"cwd"`
|
|
CreatedAt int64 `json:"createdAt"`
|
|
UpdatedAt int64 `json:"updatedAt"`
|
|
Turns []turn `json:"turns"`
|
|
}
|
|
|
|
type turn struct {
|
|
ID string `json:"id"`
|
|
Status string `json:"status"`
|
|
Error json.RawMessage `json:"error"`
|
|
Items []item `json:"items"`
|
|
}
|
|
|
|
type item struct {
|
|
Raw json.RawMessage `json:"-"`
|
|
ID string `json:"id"`
|
|
ClientID string `json:"clientId"`
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
Content json.RawMessage `json:"content"`
|
|
Status string `json:"status"`
|
|
Command string `json:"command"`
|
|
Query string `json:"query"`
|
|
Name string `json:"name"`
|
|
Input any `json:"input"`
|
|
Error json.RawMessage `json:"error"`
|
|
Arguments json.RawMessage `json:"arguments"`
|
|
Result json.RawMessage `json:"result"`
|
|
Output json.RawMessage `json:"output"`
|
|
ContentItems json.RawMessage `json:"contentItems"`
|
|
Changes json.RawMessage `json:"changes"`
|
|
Tool string `json:"tool"`
|
|
Namespace string `json:"namespace"`
|
|
Server string `json:"server"`
|
|
AggregatedOutput string `json:"aggregatedOutput"`
|
|
ExitCode *int `json:"exitCode"`
|
|
DurationMS *int64 `json:"durationMs"`
|
|
Summary []string `json:"summary"`
|
|
}
|
|
|
|
func (t thread) chatSession() protocol.ChatSession {
|
|
title := strings.TrimSpace(t.Name)
|
|
if title == "" {
|
|
title = strings.TrimSpace(t.Preview)
|
|
}
|
|
return protocol.ChatSession{
|
|
ID: t.ID, Title: title, Directory: t.Cwd,
|
|
Model: t.Model, ModelProvider: t.ModelProvider,
|
|
CreatedAt: t.CreatedAt * 1000, UpdatedAt: t.UpdatedAt * 1000,
|
|
}
|
|
}
|
|
|
|
func (i item) chatMessage(sessionID string) (protocol.ChatMessage, bool) {
|
|
msg := protocol.ChatMessage{ID: i.ID, SessionID: sessionID}
|
|
switch i.Type {
|
|
case "contextCompaction":
|
|
// Historical ThreadItem has no lifecycle status. It is neither a
|
|
// synthetic tool nor evidence that compaction is currently running.
|
|
return protocol.ChatMessage{}, false
|
|
case "userMessage":
|
|
msg.Role = protocol.ChatRoleUser
|
|
if i.ClientID != "" {
|
|
msg.ID = i.ClientID
|
|
}
|
|
var content []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
Name string `json:"name"`
|
|
Path string `json:"path"`
|
|
URL string `json:"url"`
|
|
}
|
|
_ = json.Unmarshal(i.Content, &content)
|
|
for _, part := range content {
|
|
if part.Type == "text" && part.Text != "" {
|
|
msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartText, Text: part.Text})
|
|
} else if (part.Type == "mention" || part.Type == "localImage") && filepath.IsAbs(part.Path) {
|
|
name := part.Name
|
|
if name == "" {
|
|
name = filepath.Base(part.Path)
|
|
}
|
|
msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: name, URI: (&url.URL{Scheme: "file", Path: part.Path}).String()})
|
|
} else if part.Type == "image" && part.URL != "" {
|
|
msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartFile, Name: "image", URI: part.URL})
|
|
}
|
|
}
|
|
case "agentMessage":
|
|
msg.Role = protocol.ChatRoleAssistant
|
|
if i.Text != "" {
|
|
msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartText, Text: i.Text})
|
|
}
|
|
case "reasoning":
|
|
msg.Role = protocol.ChatRoleAssistant
|
|
if text := i.reasoningText(); text != "" {
|
|
msg.Parts = append(msg.Parts, protocol.ChatPart{Type: protocol.ChatPartReasoning, Text: text})
|
|
}
|
|
default:
|
|
msg.Role = protocol.ChatRoleAssistant
|
|
msg.Parts = []protocol.ChatPart{i.toolPart(!strings.EqualFold(i.Status, "inProgress"))}
|
|
}
|
|
return msg, len(msg.Parts) > 0
|
|
}
|