-
Notifications
You must be signed in to change notification settings - Fork 0
Improve AI response parsing and sanitization for command suggestions #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -72,24 +72,26 @@ func (s *sseAIService) QueryCommandStream( | |||||||||||||||
| for scanner.Scan() { | ||||||||||||||||
| line := scanner.Text() | ||||||||||||||||
|
|
||||||||||||||||
| if line == "event: error" { | ||||||||||||||||
| isError = true | ||||||||||||||||
| if line == "" { | ||||||||||||||||
| isError = false | ||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if strings.HasPrefix(line, "data:") { | ||||||||||||||||
| data := line[len("data:"):] | ||||||||||||||||
| if v, ok := stripSSEField(line, "event:"); ok { | ||||||||||||||||
| if v == "error" { | ||||||||||||||||
| isError = true | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+81
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||
| continue | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| if v, ok := stripSSEField(line, "data:"); ok { | ||||||||||||||||
| if isError { | ||||||||||||||||
| return fmt.Errorf("server error: %s", data) | ||||||||||||||||
| return fmt.Errorf("server error: %s", v) | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
88
to
90
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the server sends a multi-line error message (multiple |
||||||||||||||||
|
|
||||||||||||||||
| if data == "[DONE]" { | ||||||||||||||||
| if v == "[DONE]" { | ||||||||||||||||
| return nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| onToken(data) | ||||||||||||||||
| isError = false | ||||||||||||||||
| onToken(v) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -99,3 +101,17 @@ func (s *sseAIService) QueryCommandStream( | |||||||||||||||
|
|
||||||||||||||||
| return nil | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| // stripSSEField returns the value after prefix, stripping one optional leading | ||||||||||||||||
| // space per the SSE specification (§9.2 "If value starts with a U+0020 SPACE | ||||||||||||||||
| // character, remove it from value"). | ||||||||||||||||
| func stripSSEField(line, prefix string) (string, bool) { | ||||||||||||||||
| if !strings.HasPrefix(line, prefix) { | ||||||||||||||||
| return "", false | ||||||||||||||||
| } | ||||||||||||||||
| v := line[len(prefix):] | ||||||||||||||||
| if strings.HasPrefix(v, " ") { | ||||||||||||||||
| v = v[1:] | ||||||||||||||||
| } | ||||||||||||||||
| return v, true | ||||||||||||||||
| } | ||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the AI returns a fenced command followed by explanatory prose (for example
bash\nls -la\n```\nThis lists files),sanitizeSuggestedCommandonly removes a closing fence if it is the final suffix, so it leaves\n```\nThis lists filesin the command. Because classification uses the first token, this can now be auto-run as aview/editcommand while still containing non-command prose, causing unintended shell execution and failures whenever auto-run is enabled. The sanitizer should extract content up to the first closing fence and ignore trailing text before classification/execution.Useful? React with 👍 / 👎.