feat(chat): broker Codex approvals and complete typed tool interactions
This commit is contained in:
+71
-32
@@ -49,19 +49,23 @@ type Chat struct {
|
||||
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
|
||||
turns map[string]string
|
||||
emitted map[string]bool
|
||||
closed bool
|
||||
mu sync.Mutex
|
||||
pending map[string]chan rpcResponse
|
||||
subs map[string]map[chan protocol.ChatEventFrame]struct{}
|
||||
forms map[string]pendingForm
|
||||
approvals map[string]pendingApproval
|
||||
turns map[string]string
|
||||
emitted map[string]bool
|
||||
closed bool
|
||||
connectionErr error
|
||||
}
|
||||
|
||||
type pendingForm struct {
|
||||
requestID json.RawMessage
|
||||
threadID string
|
||||
form protocol.ChatForm
|
||||
requestID json.RawMessage
|
||||
threadID string
|
||||
form protocol.ChatForm
|
||||
answering bool
|
||||
elicitation bool
|
||||
}
|
||||
|
||||
type rpcResponse struct {
|
||||
@@ -177,6 +181,9 @@ func (c *Chat) readLoop() {
|
||||
|
||||
func (c *Chat) failPending(err error) {
|
||||
c.mu.Lock()
|
||||
c.connectionErr = err
|
||||
c.approvals = nil
|
||||
c.forms = map[string]pendingForm{}
|
||||
pending := c.pending
|
||||
c.pending = map[string]chan rpcResponse{}
|
||||
subs := c.subs
|
||||
@@ -340,23 +347,12 @@ func (c *Chat) PromptWithMessageIDModel(ctx context.Context, id, text, messageID
|
||||
return err
|
||||
}
|
||||
|
||||
// Permissions is empty: Codex approvals are disabled on threads this adapter
|
||||
// creates (approvalPolicy never), and questions arrive as requestUserInput.
|
||||
func (c *Chat) Permissions(context.Context, string) ([]protocol.ChatPermissionFrame, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Shells is empty: Codex background terminals are not exposed through the chat
|
||||
// surface yet.
|
||||
func (c *Chat) Shells(context.Context, string) ([]protocol.ChatShell, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Reply is unused because Codex approvals are not created for these threads.
|
||||
func (c *Chat) Reply(context.Context, protocol.ChatReplyRequest) error {
|
||||
return errors.New("Codex approvals are not enabled for this chat backend")
|
||||
}
|
||||
|
||||
// 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")
|
||||
@@ -380,9 +376,8 @@ func (c *Chat) Create(ctx context.Context, title, directory string) (protocol.Ch
|
||||
return protocol.ChatSession{}, errors.New("a directory is required")
|
||||
}
|
||||
raw, err := c.call(ctx, "thread/start", map[string]any{
|
||||
"cwd": directory,
|
||||
"approvalPolicy": "never",
|
||||
"sandbox": "workspace-write",
|
||||
"cwd": directory,
|
||||
"sandbox": "workspace-write",
|
||||
})
|
||||
if err != nil {
|
||||
return protocol.ChatSession{}, err
|
||||
@@ -481,10 +476,20 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string
|
||||
|
||||
// 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 ok {
|
||||
delete(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 {
|
||||
@@ -503,7 +508,30 @@ func (c *Chat) Answer(ctx context.Context, request protocol.ChatAnswerRequest) e
|
||||
answers[key] = map[string]any{"answers": []string{fmt.Sprint(v)}}
|
||||
}
|
||||
}
|
||||
c.respond(pending.requestID, map[string]any{"answers": answers})
|
||||
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" {
|
||||
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,
|
||||
@@ -546,6 +574,7 @@ func (c *Chat) handleServerRequest(msg rpcMessage) {
|
||||
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
|
||||
@@ -562,17 +591,27 @@ func (c *Chat) handleServerRequest(msg rpcMessage) {
|
||||
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":
|
||||
// The thread is created with approvalPolicy never; if a provider still asks,
|
||||
// deny rather than leave the turn hanging.
|
||||
c.respond(msg.ID, map[string]any{"decision": "deny"})
|
||||
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.respond(msg.ID, map[string]any{})
|
||||
c.respondUnsupported(msg.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Chat) handleNotification(msg rpcMessage) {
|
||||
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"`
|
||||
|
||||
Reference in New Issue
Block a user