44 "context"
55 "encoding/json"
66 "fmt"
7+ "os"
78 "path"
89 "strings"
910
@@ -34,12 +35,73 @@ type Gemini struct {
3435 model Model
3536}
3637
38+ func (in * Gemini ) BabysitRun (ctx context.Context , bCtx * v1.BabysitContext ) bool {
39+ if bCtx == nil {
40+ return false
41+ }
42+
43+ env := []string {fmt .Sprintf ("GEMINI_API_KEY=%s" , in .apiKey ), fmt .Sprintf ("GEMINI_CLI_TRUST_WORKSPACE=%s" , "true" )}
44+ if in .Config .Run .Runtime .Config .Gemini .Endpoint != nil {
45+ env = append (env , fmt .Sprintf ("GEMINI_API_BASE_URL=%s" , * in .Config .Run .Runtime .Config .Gemini .Endpoint ))
46+ }
47+
48+ in .executable = exec .NewExecutable (
49+ "gemini" ,
50+ exec .WithArgs (in .args (bCtx .Prompt )),
51+ exec .WithDir (in .Config .WorkDir ),
52+ exec .WithEnv (env ),
53+ exec .WithTimeout (in .Config .Run .Runtime .Config .Gemini .Timeout ),
54+ )
55+
56+ klog .V (log .LogLevelInfo ).InfoS ("Gemini executable configured" , "timeout" , in .Config .Run .Runtime .Config .Gemini .Timeout )
57+
58+ // Send the initial prompt as a message too
59+ if in .onMessage != nil {
60+ in .onMessage (& console.AgentMessageAttributes {Message : bCtx .Prompt , Role : console .AiRoleUser })
61+ }
62+
63+ err := in .executable .RunStream (ctx , func (line []byte ) {
64+ klog .V (log .LogLevelTrace ).InfoS ("Gemini stream event" , "line" , string (line ))
65+
66+ // This is here to prevent unavoidable log lines being reported as errors.
67+ // TODO: Remove once https://github.com/google-gemini/gemini-cli/issues/15053 is fixed.
68+ trimmed := strings .TrimSpace (string (line ))
69+ if ! strings .HasPrefix (trimmed , "{" ) {
70+ klog .V (log .LogLevelDebug ).InfoS ("ignoring non-json Gemini stream line" , "trimmed" , trimmed )
71+ return
72+ }
73+
74+ event := & events.EventBase {}
75+ if err := json .Unmarshal (line , event ); err != nil {
76+ klog .ErrorS (err , "failed to unmarshal Gemini stream event" , "line" , line )
77+ in .Config .ErrorChan <- err
78+ return
79+ }
80+
81+ if err := event .OnMessage (line , in .onMessage ); err != nil {
82+ klog .ErrorS (err , "failed to process Gemini stream event" , "line" , string (line ))
83+ in .Config .ErrorChan <- err
84+ }
85+ })
86+ if err != nil {
87+ klog .ErrorS (err , "Gemini execution failed" )
88+ in .Config .ErrorChan <- err
89+ return false
90+ }
91+
92+ return false
93+ }
94+
95+ func (in * Gemini ) ConfigureBabysitRun () error {
96+ return in .ConfigureSystemPromptForBabysitRun (console .AgentRuntimeTypeGemini )
97+ }
98+
3799func (in * Gemini ) Run (ctx context.Context , options ... exec.Option ) {
38100 go in .start (ctx , options ... )
39101}
40102
41103func (in * Gemini ) start (ctx context.Context , options ... exec.Option ) {
42- env := []string {fmt .Sprintf ("GEMINI_API_KEY=%s" , in .apiKey )}
104+ env := []string {fmt .Sprintf ("GEMINI_API_KEY=%s" , in .apiKey ), fmt . Sprintf ( "GEMINI_CLI_TRUST_WORKSPACE=%s" , "true" ) }
43105 if in .Config .Run .Runtime .Config .Gemini .Endpoint != nil {
44106 env = append (env , fmt .Sprintf ("GEMINI_API_BASE_URL=%s" , * in .Config .Run .Runtime .Config .Gemini .Endpoint ))
45107 }
@@ -48,7 +110,7 @@ func (in *Gemini) start(ctx context.Context, options ...exec.Option) {
48110 "gemini" ,
49111 append (
50112 options ,
51- exec .WithArgs (in .args ()),
113+ exec .WithArgs (in .args ("" )),
52114 exec .WithDir (in .Config .WorkDir ),
53115 exec .WithEnv (env ),
54116 exec .WithTimeout (in .Config .Run .Runtime .Config .Gemini .Timeout ),
@@ -94,17 +156,20 @@ func (in *Gemini) start(ctx context.Context, options ...exec.Option) {
94156 // FinishedChan is closed by the controller after the babysit loop exits.
95157}
96158
97- func (in * Gemini ) args () []string {
159+ func (in * Gemini ) args (prompt string ) []string {
160+ if len (prompt ) > 0 {
161+ in .Config .Run .Prompt = prompt
162+ }
98163 if in .Config .Run .Mode == console .AgentRunModeWrite {
99164 return []string {
100165 "--approval-mode" , "yolo" ,
101- "--output-format" , "stream-json" ,
166+ "--output-format" , "stream-json" , "--prompt" ,
102167 in .Config .Run .Prompt ,
103168 }
104169 }
105170
106171 return []string {
107- "--output-format" , "stream-json" ,
172+ "--output-format" , "stream-json" , "--prompt" ,
108173 in .Config .Run .Prompt ,
109174 }
110175}
@@ -124,6 +189,7 @@ func (in *Gemini) Configure(consoleURL, consoleToken, deployToken string) error
124189 InactivityTimeout : int64 (in .Config .Run .Runtime .Config .Gemini .InactivityTimeout .Seconds ()),
125190 Model : in .model ,
126191 ExaMcpConfigs : in .Config .Run .Runtime .ExaMcpConfigs ,
192+ GitAccessToken : os .Getenv ("GIT_ACCESS_TOKEN" ),
127193 }
128194
129195 _ , content , err := settings (input )
0 commit comments