fix(chat): recover web approvals and questions across clients and reconnects
This commit is contained in:
@@ -442,8 +442,9 @@ func (c *Chat) Reply(ctx context.Context, request protocol.ChatReplyRequest) err
|
||||
}
|
||||
|
||||
// Permissions lists the session's pending OpenCode approval requests, mirroring
|
||||
// GET /api/permission/request. The endpoint is global, so the decoded list is
|
||||
// filtered to sessionID; a malformed response is an error, never an empty
|
||||
// GET /api/session/{id}/permission. The location-wide endpoint can silently
|
||||
// miss chats in another directory. Results are still filtered to sessionID;
|
||||
// a malformed response is an error, never an empty
|
||||
// success. It reuses Connection.Call so the request keeps the shared bounds,
|
||||
// auth and translation, and reuses ValidSessionID for the same rejection the
|
||||
// other chat calls apply.
|
||||
@@ -451,7 +452,7 @@ func (c *Chat) Permissions(ctx context.Context, id string) ([]protocol.ChatPermi
|
||||
if !sessionID.MatchString(id) {
|
||||
return nil, fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
data, err := c.call(ctx, "get", "/api/permission/request", nil)
|
||||
data, err := c.call(ctx, "get", "/api/session/"+id+"/permission", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -874,6 +875,36 @@ type chatFormPayload struct {
|
||||
Fields []chatFormField `json:"fields"`
|
||||
}
|
||||
|
||||
// Forms recovers pending questions even when their creation event preceded the
|
||||
// browser connection or the Pipelines process. Session ownership is explicit.
|
||||
func (c *Chat) Forms(ctx context.Context, id string) ([]protocol.ChatForm, error) {
|
||||
if !ValidSessionID(id) {
|
||||
return nil, fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
data, err := c.call(ctx, "get", "/api/session/"+id+"/form", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
forms, ok := chatEnvelope[chatFormPayload](data)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid OpenCode form response")
|
||||
}
|
||||
result := make([]protocol.ChatForm, 0, len(forms))
|
||||
for _, form := range forms {
|
||||
if form.ID == "" || form.SessionID != id {
|
||||
continue
|
||||
}
|
||||
frame, valid, err := normalizeFormEvent("form.created", chatEvent{Form: &form}, id, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if valid {
|
||||
result = append(result, *frame.Form)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type chatFormField struct {
|
||||
Key string `json:"key"`
|
||||
Title string `json:"title"`
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChatFormsReadbackUsesSessionOwnership(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/session/ses_otherdir/form" {
|
||||
t.Errorf("wrong session path: %s", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"data":[{"id":"f_1","sessionID":"ses_otherdir","title":"Pending before connect","metadata":{"kind":"question"},"fields":[{"key":"answer","type":"text","required":true}]},{"id":"foreign","sessionID":"ses_foreign"}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL, Directory: "/unrelated"})
|
||||
forms, err := chat.Forms(context.Background(), "ses_otherdir")
|
||||
if err != nil || len(forms) != 1 || forms[0].ID != "f_1" || len(forms[0].Fields) != 1 || !forms[0].Fields[0].Required {
|
||||
t.Fatalf("forms=%+v err=%v", forms, err)
|
||||
}
|
||||
}
|
||||
@@ -182,13 +182,13 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestChatPermissionsListsAndFilters loads a pending-request fixture in the
|
||||
// verified V2 envelope (GET /api/permission/request) and asserts the adapter
|
||||
// verified V2 session-owned envelope and asserts the adapter
|
||||
// returns the requested session's request with its fields intact, filters out a
|
||||
// foreign session's request, and reads the endpoint once.
|
||||
func TestChatPermissionsListsAndFilters(t *testing.T) {
|
||||
var reads atomic.Int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/permission/request" {
|
||||
if r.Method != http.MethodGet || r.URL.Path != "/api/session/ses_1/permission" {
|
||||
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -158,6 +158,9 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
return nil, fmt.Errorf("unsupported native OpenCode operation")
|
||||
}
|
||||
id := parts[0]
|
||||
if len(parts) == 2 && parts[1] == "permission" {
|
||||
return nil, fmt.Errorf("permission listing: %w", ErrPermissionUnsupported)
|
||||
}
|
||||
// The approval reply is V2-only in this translation (see the permission-list
|
||||
// gate above); the V2 path has four trailing segments.
|
||||
if len(parts) == 4 && parts[1] == "permission" && parts[3] == "reply" {
|
||||
|
||||
Reference in New Issue
Block a user