Poll OpenCode permission requests and forward chat_reply
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user