gunk-dev/the-valley

oneshot.go

sourcecaaa5bee5e11main's published tip history
2.1 KiB
1package backend
2
3import "fmt"
4
5type OneShotOptions struct {
6 SystemPrompt, Model, Effort, ResumeID, SessionID, LastMessage, AgyAgent, DiagnosticLog string
7 Threaded bool
8}
9
10// OneShot emits text. The prompt is never in argv: every backend reads
11// k.Stdin(o.SystemPrompt, prompt) on stdin, so a long one meets no argv limit.
12func OneShot(k Kind, o OneShotOptions) ([]string, error) {
13 var a []string
14 switch k {
15 case Claude:
16 a = []string{"claude", "-p", "--safe-mode", "--output-format", "text", "--system-prompt", o.SystemPrompt,
17 "--tools", "Read,Grep,Glob", "--permission-mode", "dontAsk", "--strict-mcp-config", "--disable-slash-commands"}
18 if !o.Threaded {
19 a = append(a, "--no-session-persistence")
20 } else if o.ResumeID != "" {
21 a = append(a, "--resume", o.ResumeID)
22 } else if o.SessionID != "" {
23 a = append(a, "--session-id", o.SessionID)
24 }
25 case Codex:
26 // User-configured MCP, hooks, and plugins bypass filesystem-only isolation.
27 a = []string{"codex", "exec", "--ignore-user-config", "--sandbox", "read-only", "-c", `approval_policy="never"`,
28 "-c", "features.apps=false", "-c", "features.plugins=false", "-c", "features.hooks=false", "-c", "features.multi_agent=false",
29 "-c", "developer_instructions=" + tomlString(o.SystemPrompt)}
30 if o.ResumeID != "" {
31 a = append(a, "resume", o.ResumeID)
32 }
33 a = append(a, "--skip-git-repo-check")
34 if !o.Threaded {
35 a = append(a, "--ephemeral")
36 }
37 if o.LastMessage != "" {
38 a = append(a, "--output-last-message", o.LastMessage)
39 }
40 case Agy:
41 if o.AgyAgent == "" {
42 return nil, fmt.Errorf("agy OneShot requires a prepared read-only agent")
43 }
44 a = []string{"agy", "--add-dir", ".", "--output-format", "text", "--mode", "plan", "--sandbox", "--agent", o.AgyAgent}
45 if o.ResumeID != "" {
46 a = append(a, "--conversation", o.ResumeID)
47 }
48 if o.DiagnosticLog != "" {
49 a = append(a, "--log-file", o.DiagnosticLog)
50 }
51 default:
52 return nil, fmt.Errorf("unsupported one-shot backend %q", k)
53 }
54 a = k.modelArgs(a, Options{Model: o.Model, Effort: o.Effort})
55 if k == Codex {
56 return append(a, "-"), nil
57 }
58 return a, nil
59}