Skip to content

Commit f166642

Browse files
committed
Add headless mode
1 parent 0d631d6 commit f166642

3 files changed

Lines changed: 322 additions & 3 deletions

File tree

cmd/root.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
package cmd
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"os"
2021

@@ -32,12 +33,24 @@ var (
3233
date = "unknown"
3334
)
3435

36+
var (
37+
instructions string
38+
outputPath string
39+
)
40+
3541
// rootCmd represents the base command when called without any subcommands
3642
var rootCmd = &cobra.Command{
3743
Use: "arlocode",
3844
Short: "ArloCode a Coding Agent focused on long running tasks and local models",
39-
Long: `AlroCode is an AI coding agent designed to assist developers with complex coding tasks.`,
45+
Long: `ArloCode is an AI coding agent designed to assist developers with complex coding tasks.`,
4046
Run: func(cmd *cobra.Command, args []string) {
47+
if instructions != "" {
48+
if err := runHeadless(instructions, outputPath); err != nil {
49+
os.Exit(1)
50+
}
51+
return
52+
}
53+
4154
codingAgent := coding_agent.Agent
4255
agentBridge := bridge.NewDirectBridge(codingAgent)
4356

@@ -87,7 +100,45 @@ func init() {
87100
// Cobra supports persistent flags, which, if defined here,
88101
// will be global for your application.
89102

103+
rootCmd.Flags().StringVarP(&instructions, "instructions", "i", "", "Run in headless mode with the provided instruction text")
104+
rootCmd.Flags().StringVarP(&outputPath, "output", "o", "", "Path to write the ATIF trajectory JSON")
105+
90106
// Set version template for --version flag
91107
rootCmd.Version = version
92108
rootCmd.SetVersionTemplate(fmt.Sprintf("arlocode %s (commit: %s, built: %s)\n", version, commit, date))
93109
}
110+
111+
func runHeadless(prompt string, path string) error {
112+
codingAgent := coding_agent.Agent.WithMaxIterations(500)
113+
agentBridge := bridge.NewDirectBridge(codingAgent)
114+
defer agentBridge.Close()
115+
116+
if err := agentBridge.Run(context.Background(), prompt); err != nil {
117+
return err
118+
}
119+
120+
var runErr error
121+
for {
122+
event, ok := <-agentBridge.Events()
123+
if !ok {
124+
break
125+
}
126+
switch e := event.(type) {
127+
case bridge.ErrorEvent:
128+
if e.Err != nil {
129+
runErr = e.Err
130+
}
131+
case bridge.TurnCompleteEvent:
132+
_, err := agentBridge.ExportATIF(path)
133+
if err != nil {
134+
return err
135+
}
136+
if runErr != nil {
137+
return runErr
138+
}
139+
return nil
140+
}
141+
}
142+
143+
return runErr
144+
}

internal/coding_agent/agent.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ You are in a terminal environment and have access to local resources and files.
2424

2525
var ctx = context.Background()
2626

27-
// var provider = ollama.New(ctx)
28-
2927
var provider = openai_provider.New(ctx,
3028
openai_provider.WithApiKey(os.Getenv("ZAI_API_KEY")),
3129
openai_provider.WithBaseURL("https://api.z.ai/api/coding/paas/v4"),

0 commit comments

Comments
 (0)