Skip to content

Commit 8c1fc80

Browse files
committed
add stream cmd
1 parent 547e11a commit 8c1fc80

File tree

4 files changed

+139
-7
lines changed

4 files changed

+139
-7
lines changed

cmd/chat.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
)
2121

2222
var chatCmd = &cobra.Command{
23+
Use: "chat",
2324
Short: "Interact with your agent.",
2425
Long: `This cli tool allows you to debug your agent by chatting with it locally.`,
2526
Run: agentChat,
@@ -74,10 +75,3 @@ func agentChat(cmd *cobra.Command, args []string) {
7475
fmt.Println(err)
7576
}
7677
}
77-
78-
func Execute() {
79-
err := chatCmd.Execute()
80-
if err != nil {
81-
os.Exit(1)
82-
}
83-
}

cmd/rootCmd.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// rootCmd.go
2+
package cmd
3+
4+
import (
5+
"fmt"
6+
"os"
7+
8+
"github.com/spf13/cobra"
9+
// "github.com/github-technology-partners/gh-debug-cli/cmd/stream"
10+
)
11+
12+
var rootCmd = &cobra.Command{
13+
Use: "gh-debug-cli",
14+
Short: "A CLI tool for debugging",
15+
Long: `This CLI tool allows you to debug your agent by chatting with it locally.`,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
fmt.Println("Use 'gh-debug-cli --help' to see available commands")
18+
},
19+
}
20+
21+
func Execute() {
22+
if err := rootCmd.Execute(); err != nil {
23+
fmt.Println(err)
24+
os.Exit(1)
25+
}
26+
}
27+
28+
func init() {
29+
// Add subcommands to rootCmd
30+
rootCmd.AddCommand(chatCmd)
31+
rootCmd.AddCommand(streamCmd)
32+
}

cmd/stream.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// stream.go
2+
package cmd
3+
4+
import (
5+
"fmt"
6+
"os"
7+
8+
"github.com/github-technology-partners/gh-debug-cli/pkg/stream"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
const (
13+
streamCmdFileFlag = "file"
14+
)
15+
16+
// streamCmd represents the new command for streaming functionality
17+
var streamCmd = &cobra.Command{
18+
Use: "stream [file]",
19+
Short: "Stream data to your agent",
20+
Long: `The stream command allows you to initiate a data stream to your agent.`,
21+
Run: agentStream,
22+
}
23+
24+
func init() {
25+
streamCmd.PersistentFlags().String(streamCmdFileFlag, "", "Parse agent responses from a file")
26+
}
27+
28+
func agentStream(cmd *cobra.Command, args []string) {
29+
fmt.Println("stream command executed successfully")
30+
31+
file := args[0]
32+
33+
err := stream.ParseFile(file)
34+
if err != nil {
35+
fmt.Fprintf(os.Stderr, "Error parsing file: %v\n", err)
36+
os.Exit(1)
37+
}
38+
}

pkg/stream/parse.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package stream
2+
3+
import (
4+
"bufio"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"strings"
9+
)
10+
11+
type Choice struct {
12+
Delta struct {
13+
Content string `json:"content"`
14+
} `json:"delta"`
15+
}
16+
17+
type Data struct {
18+
Choices []Choice `json:"choices"`
19+
}
20+
21+
func ParseFile(filename string) error {
22+
// Open the file
23+
file, err := os.Open(filename)
24+
if err != nil {
25+
return fmt.Errorf("could not open file: %w", err)
26+
}
27+
defer file.Close()
28+
29+
scanner := bufio.NewScanner(file)
30+
var contentBuilder strings.Builder
31+
32+
for scanner.Scan() {
33+
line := scanner.Text()
34+
35+
// Check if the line has "data: " prefix
36+
if strings.HasPrefix(line, "data: ") {
37+
// Remove the "data: " prefix
38+
line = strings.TrimPrefix(line, "data: ")
39+
} else {
40+
continue // skip lines without "data: "
41+
}
42+
43+
// Parse the JSON line into our `Data` struct
44+
var data Data
45+
err := json.Unmarshal([]byte(line), &data)
46+
if err != nil {
47+
// Skip this line if JSON is incomplete or malformed
48+
// fmt.Fprintf(os.Stderr, "Warning: could not parse JSON: %v\n", err)
49+
continue
50+
}
51+
52+
// Extract delta.content and concatenate it
53+
for _, choice := range data.Choices {
54+
contentBuilder.WriteString(choice.Delta.Content)
55+
}
56+
}
57+
58+
// Check for scanner errors
59+
if err := scanner.Err(); err != nil {
60+
return fmt.Errorf("error reading file: %w", err)
61+
}
62+
63+
// Print the final concatenated result
64+
result := contentBuilder.String()
65+
fmt.Println(result)
66+
67+
return nil
68+
}

0 commit comments

Comments
 (0)