Skip to content

Commit 62c0c3c

Browse files
authored
Merge pull request #10 from copilot-extensions/add-stream-cmd
Add stream cmd
2 parents 547e11a + 1ce7211 commit 62c0c3c

File tree

4 files changed

+145
-7
lines changed

4 files changed

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

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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// Handle special cases
44+
if line == "[DONE]" {
45+
break // stop processing if we encounter [DONE]
46+
}
47+
if line == "" {
48+
continue // skip empty data lines
49+
}
50+
51+
// Parse the JSON line into our `Data` struct
52+
var data Data
53+
err := json.Unmarshal([]byte(line), &data)
54+
if err != nil {
55+
// Skip this line if JSON is incomplete or malformed
56+
continue
57+
}
58+
59+
// Extract delta.content and concatenate it
60+
for _, choice := range data.Choices {
61+
contentBuilder.WriteString(choice.Delta.Content)
62+
}
63+
}
64+
65+
// Check for scanner errors
66+
if err := scanner.Err(); err != nil {
67+
return fmt.Errorf("error reading file: %w", err)
68+
}
69+
70+
// Print the final concatenated result
71+
result := contentBuilder.String()
72+
fmt.Println(result)
73+
74+
return nil
75+
}

0 commit comments

Comments
 (0)