70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package codex
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// codexMode is one approval/sandbox preset, applied as turn/start overrides
|
|
// that Codex keeps for the thread's later turns too.
|
|
type codexMode struct {
|
|
protocol.ChatMode
|
|
approval string
|
|
sandbox string
|
|
}
|
|
|
|
// The presets mirror Codex's own: ask in the workspace sandbox (the thread
|
|
// default chats start with), act without asking inside it, or full access.
|
|
var codexModes = []codexMode{
|
|
{protocol.ChatMode{ID: "ask", Name: "Ask for approval", Description: "Work in the project sandbox and ask when an action needs more."}, "on-request", "workspaceWrite"},
|
|
{protocol.ChatMode{ID: "auto", Name: "Auto", Description: "Work in the project sandbox without asking."}, "never", "workspaceWrite"},
|
|
{protocol.ChatMode{ID: "full", Name: "Full access", Description: "No sandbox and no approvals. Everything runs."}, "never", "dangerFullAccess"},
|
|
}
|
|
|
|
// Modes lists the approval/sandbox presets a Codex chat can switch to.
|
|
func (c *Chat) Modes(context.Context, string) ([]protocol.ChatMode, error) {
|
|
modes := make([]protocol.ChatMode, len(codexModes))
|
|
for index, mode := range codexModes {
|
|
modes[index] = mode.ChatMode
|
|
}
|
|
return modes, nil
|
|
}
|
|
|
|
// SetMode records the thread's preset; it is sent with the next turn/start.
|
|
func (c *Chat) SetMode(_ context.Context, id, mode string) error {
|
|
if _, ok := codexModeFor(mode); !ok {
|
|
return fmt.Errorf("unknown Codex mode %q", mode)
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.modes == nil {
|
|
c.modes = map[string]string{}
|
|
}
|
|
c.modes[id] = mode
|
|
return nil
|
|
}
|
|
|
|
func codexModeFor(id string) (codexMode, bool) {
|
|
for _, mode := range codexModes {
|
|
if mode.ID == id {
|
|
return mode, true
|
|
}
|
|
}
|
|
return codexMode{}, false
|
|
}
|
|
|
|
// applyMode adds the thread's preset to turn/start params, if one was chosen.
|
|
func (c *Chat) applyMode(id string, params map[string]any) {
|
|
c.mu.Lock()
|
|
chosen := c.modes[id]
|
|
c.mu.Unlock()
|
|
mode, ok := codexModeFor(chosen)
|
|
if !ok {
|
|
return
|
|
}
|
|
params["approvalPolicy"] = mode.approval
|
|
params["sandboxPolicy"] = map[string]any{"type": mode.sandbox}
|
|
}
|