feat(chat): per-chat permission modes for Claude, Codex and OpenCode
This commit is contained in:
@@ -45,6 +45,8 @@ type Chat struct {
|
||||
mu sync.Mutex
|
||||
usageMu sync.Mutex
|
||||
usageModels map[string]usageModelCache
|
||||
modeMu sync.Mutex
|
||||
agents map[string]string
|
||||
}
|
||||
|
||||
// NewChat brokers chat for one owned OpenCode endpoint. The connection must be
|
||||
@@ -131,6 +133,9 @@ func (c *Chat) PromptWithMessageIDFiles(ctx context.Context, id, text, messageID
|
||||
if messageID != "" {
|
||||
body["id"] = messageID
|
||||
}
|
||||
if agent := c.agentFor(id); agent != "" {
|
||||
body["agent"] = agent
|
||||
}
|
||||
if len(files) > 0 {
|
||||
attachments := make([]map[string]string, 0, len(files))
|
||||
for _, file := range files {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatModes are OpenCode's two primary agents. What each may do without asking
|
||||
// is the agent's own permission configuration; Pipelines only picks the agent.
|
||||
var chatModes = []protocol.ChatMode{
|
||||
{ID: "build", Name: "Build", Description: "OpenCode's default agent: edits and runs commands under its permission settings."},
|
||||
{ID: "plan", Name: "Plan", Description: "Analyse and plan; edits and commands are restricted."},
|
||||
}
|
||||
|
||||
// Modes lists the agents an OpenCode chat can switch to.
|
||||
func (c *Chat) Modes(context.Context, string) ([]protocol.ChatMode, error) {
|
||||
return append([]protocol.ChatMode(nil), chatModes...), nil
|
||||
}
|
||||
|
||||
// SetMode records the session's agent; every later prompt names it.
|
||||
func (c *Chat) SetMode(_ context.Context, id, mode string) error {
|
||||
known := false
|
||||
for _, candidate := range chatModes {
|
||||
known = known || candidate.ID == mode
|
||||
}
|
||||
if !known {
|
||||
return fmt.Errorf("unknown OpenCode agent %q", mode)
|
||||
}
|
||||
c.modeMu.Lock()
|
||||
defer c.modeMu.Unlock()
|
||||
if c.agents == nil {
|
||||
c.agents = map[string]string{}
|
||||
}
|
||||
c.agents[id] = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Chat) agentFor(id string) string {
|
||||
c.modeMu.Lock()
|
||||
defer c.modeMu.Unlock()
|
||||
return c.agents[id]
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package opencode
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChosenAgentRidesOnEveryPrompt(t *testing.T) {
|
||||
for _, backend := range []string{"v2", "v1"} {
|
||||
t.Run(backend, func(t *testing.T) {
|
||||
var bodies []map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, _ := io.ReadAll(r.Body)
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(raw, &body)
|
||||
bodies = append(bodies, body)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
conn := &Connection{Backend: backend, URL: srv.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}
|
||||
chat := NewChat(conn)
|
||||
ctx := context.Background()
|
||||
if err := chat.SetMode(ctx, "ses_1", "auto"); err == nil {
|
||||
t.Fatal("an unknown agent was accepted")
|
||||
}
|
||||
if err := chat.Prompt(ctx, "ses_1", "before"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.SetMode(ctx, "ses_1", "plan"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.Prompt(ctx, "ses_1", "after"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(bodies) != 2 || bodies[0]["agent"] != nil || bodies[1]["agent"] != "plan" {
|
||||
t.Fatalf("prompt bodies = %v", bodies)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -199,6 +199,7 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
Prompt string `json:"prompt"`
|
||||
Text string `json:"text"`
|
||||
ID string `json:"id"`
|
||||
Agent string `json:"agent"`
|
||||
Format json.RawMessage `json:"format"`
|
||||
Parts []struct {
|
||||
Text string `json:"text"`
|
||||
@@ -226,6 +227,9 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
|
||||
if request.ID != "" {
|
||||
payload["id"] = request.ID
|
||||
}
|
||||
if request.Agent != "" {
|
||||
payload["agent"] = request.Agent
|
||||
}
|
||||
if len(request.Format) > 0 {
|
||||
supported, err := c.supportsFormat(ctx, request.Format)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user