Poll OpenCode permission requests and forward chat_reply
This commit is contained in:
@@ -22,9 +22,12 @@ const (
|
||||
ChatMessages = "chat_messages"
|
||||
ChatInterrupt = "chat_interrupt"
|
||||
ChatReply = "chat_reply"
|
||||
// ChatEvent is the server-pushed frame; it is the only type the server
|
||||
// sends without a matching request.
|
||||
// ChatEvent and ChatPermission are the server-pushed frames; they are the
|
||||
// only types the server sends without a matching request.
|
||||
ChatEvent = "chat_event"
|
||||
// ChatPermission carries one pending OpenCode approval request the phone
|
||||
// answers with chat_reply.
|
||||
ChatPermission = "chat_permission"
|
||||
)
|
||||
|
||||
// Content part types carried by ChatPart.Type and ChatEvent.Kind.
|
||||
@@ -200,6 +203,35 @@ type ChatUsage struct {
|
||||
Cost float64 `json:"cost,omitempty"`
|
||||
}
|
||||
|
||||
// ChatPermissionFrame is the server-pushed chat_permission payload: one pending
|
||||
// OpenCode permission request the phone must approve or reject. It is a distinct
|
||||
// Message field (like ChatEvent) rather than a ChatEvent.Kind, so it does not
|
||||
// overload the turn-progress kinds. Its field names match the service's
|
||||
// Permission.Request schema, verified against the installed service's
|
||||
// /openapi.json (id, sessionID, action and resources required; save, metadata,
|
||||
// source and message optional), with id -> RequestID and sessionID -> SessionID.
|
||||
// The wire shape is spec-verified; a live pending request has not been observed.
|
||||
// Every optional field is omitempty so older peers ignore it.
|
||||
type ChatPermissionFrame struct {
|
||||
SessionID string `json:"session_id"`
|
||||
RequestID string `json:"request_id"`
|
||||
Action string `json:"action,omitempty"`
|
||||
Resources []string `json:"resources,omitempty"`
|
||||
Save []string `json:"save,omitempty"`
|
||||
Metadata map[string]any `json:"metadata,omitempty"`
|
||||
Source *ChatPermissionSource `json:"source,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
// ChatPermissionSource identifies what raised a permission request, matching the
|
||||
// Permission.Source union (currently only a tool call: type "tool" plus the
|
||||
// message and call id) verified against the service's /openapi.json.
|
||||
type ChatPermissionSource struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
MessageID string `json:"message_id,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
}
|
||||
|
||||
// ChatReplyRequest is the chat_reply request body: the phone's decision on a
|
||||
// pending permission request, mirroring
|
||||
// POST /api/session/{id}/permission/{requestID}/reply.
|
||||
|
||||
@@ -55,6 +55,13 @@ func chatSamples() []struct {
|
||||
}},
|
||||
{"event_step", ChatEventFrame{SessionID: "ses_1", Kind: ChatEventStep, Step: &ChatStep{Status: ChatStepEnded, Index: 2}}},
|
||||
{"event_usage", ChatEventFrame{SessionID: "ses_1", Kind: ChatEventUsage, Usage: &ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001}}},
|
||||
{"permission", ChatPermissionFrame{
|
||||
SessionID: "ses_1", RequestID: "per_1", Action: "bash",
|
||||
Resources: []string{"rm -rf /tmp"}, Save: []string{"bash:rm -rf /tmp"},
|
||||
Metadata: map[string]any{"command": "rm -rf /tmp"},
|
||||
Source: &ChatPermissionSource{Type: "tool", MessageID: "msg_1", ID: "call_1"},
|
||||
Message: "wants to run a command",
|
||||
}},
|
||||
{"reply", ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionOnce, Message: "ok"}},
|
||||
}
|
||||
}
|
||||
@@ -114,6 +121,32 @@ func TestChatEventGoldenJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestChatPermissionGoldenJSON pins the exact wire shape of the pushed
|
||||
// chat_permission frame so a JSON tag change cannot silently alter what the
|
||||
// phone parses.
|
||||
func TestChatPermissionGoldenJSON(t *testing.T) {
|
||||
frame := ChatPermissionFrame{
|
||||
SessionID: "ses_1", RequestID: "per_1", Action: "bash",
|
||||
Resources: []string{"rm -rf /tmp"},
|
||||
Source: &ChatPermissionSource{Type: "tool", MessageID: "msg_1", ID: "call_1"},
|
||||
}
|
||||
encoded, err := json.Marshal(frame)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := `{"session_id":"ses_1","request_id":"per_1","action":"bash","resources":["rm -rf /tmp"],"source":{"type":"tool","message_id":"msg_1","id":"call_1"}}`
|
||||
if string(encoded) != want {
|
||||
t.Fatalf("golden mismatch\n got %s\nwant %s", encoded, want)
|
||||
}
|
||||
var decoded ChatPermissionFrame
|
||||
if err := json.Unmarshal([]byte(want), &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(decoded, frame) {
|
||||
t.Fatalf("golden did not decode back: %+v", decoded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatEnvelopeRoundTrip(t *testing.T) {
|
||||
session := ChatSession{ID: "ses_1", Title: "demo"}
|
||||
message := Message{
|
||||
@@ -125,7 +158,10 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
|
||||
ChatPrompt: &ChatPromptRequest{SessionID: "ses_1", Text: "hi"},
|
||||
ChatMessages: []ChatMessage{{ID: "msg_1", SessionID: "ses_1", Role: ChatRoleUser, Parts: []ChatPart{{Type: ChatPartText, Text: "hi"}}}},
|
||||
ChatEvent: &ChatEventFrame{SessionID: "ses_1", Kind: ChatEventText, Delta: "hi"},
|
||||
ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject},
|
||||
ChatPermission: &ChatPermissionFrame{
|
||||
SessionID: "ses_1", RequestID: "per_1", Action: "bash", Resources: []string{"rm -rf /"},
|
||||
},
|
||||
ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject},
|
||||
}
|
||||
encoded, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
@@ -145,6 +181,7 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
|
||||
!reflect.DeepEqual(decoded.ChatPrompt, message.ChatPrompt) ||
|
||||
!reflect.DeepEqual(decoded.ChatMessages, message.ChatMessages) ||
|
||||
!reflect.DeepEqual(decoded.ChatEvent, message.ChatEvent) ||
|
||||
!reflect.DeepEqual(decoded.ChatPermission, message.ChatPermission) ||
|
||||
!reflect.DeepEqual(decoded.ChatReply, message.ChatReply) {
|
||||
t.Fatalf("chat payloads lost: %+v", decoded)
|
||||
}
|
||||
@@ -155,7 +192,7 @@ func TestChatFieldsOmittedWhenUnused(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_reply"} {
|
||||
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply"} {
|
||||
if strings.Contains(string(minimal), absent) {
|
||||
t.Fatalf("absent %q must be omitted: %s", absent, minimal)
|
||||
}
|
||||
|
||||
@@ -135,12 +135,13 @@ type Message struct {
|
||||
Workspace string `json:"workspace,omitempty"`
|
||||
// Chat is the brokered OpenCode chat traffic (route B). See chat.go for the
|
||||
// per-type payload structs and the wire shapes they mirror.
|
||||
ChatSessions []ChatSession `json:"chat_sessions,omitempty"`
|
||||
ChatSession *ChatSession `json:"chat_session,omitempty"`
|
||||
ChatCreate *ChatCreateRequest `json:"chat_create,omitempty"`
|
||||
ChatQuery *ChatQuery `json:"chat_query,omitempty"`
|
||||
ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"`
|
||||
ChatMessages []ChatMessage `json:"chat_messages,omitempty"`
|
||||
ChatEvent *ChatEventFrame `json:"chat_event,omitempty"`
|
||||
ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"`
|
||||
ChatSessions []ChatSession `json:"chat_sessions,omitempty"`
|
||||
ChatSession *ChatSession `json:"chat_session,omitempty"`
|
||||
ChatCreate *ChatCreateRequest `json:"chat_create,omitempty"`
|
||||
ChatQuery *ChatQuery `json:"chat_query,omitempty"`
|
||||
ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"`
|
||||
ChatMessages []ChatMessage `json:"chat_messages,omitempty"`
|
||||
ChatEvent *ChatEventFrame `json:"chat_event,omitempty"`
|
||||
ChatPermission *ChatPermissionFrame `json:"chat_permission,omitempty"`
|
||||
ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user