-
Notifications
You must be signed in to change notification settings - Fork 53
Add agentic JSON output mode for AI agent integration #536
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
Open
danielsuguimoto
wants to merge
2
commits into
kool-dev:main
Choose a base branch
from
danielsuguimoto:feat/agentic-output
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| package commands | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "encoding/json" | ||
| "kool-dev/kool/core/builder" | ||
| "os" | ||
| "os/exec" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
|
|
@@ -14,6 +18,13 @@ type KoolLogsFlags struct { | |
| Follow bool | ||
| } | ||
|
|
||
| type logEntryJSON struct { | ||
| Service string `json:"service"` | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| var execLogsCmd = exec.Command | ||
|
|
||
| // KoolLogs holds handlers and functions to implement the logs command logic | ||
| type KoolLogs struct { | ||
| DefaultKoolService | ||
|
|
@@ -65,6 +76,10 @@ func (l *KoolLogs) Execute(args []string) (err error) { | |
| l.logs.AppendArgs("--follow") | ||
| } | ||
|
|
||
| if l.Shell().IsJSONOutput() { | ||
| return l.printLogsJSON(args...) | ||
| } | ||
|
|
||
| err = l.Shell().Interactive(l.logs, args...) | ||
| return | ||
| } | ||
|
|
@@ -86,3 +101,74 @@ the command to follow the log output (i.e. 'kool logs -f [SERVICE...]').`, | |
| logsCmd.Flags().BoolVarP(&logs.Flags.Follow, "follow", "f", false, "Follow log output.") | ||
| return | ||
| } | ||
|
|
||
| func (l *KoolLogs) printLogsJSON(args ...string) (err error) { | ||
| if l.Flags.Follow { | ||
| return l.streamLogsJSON(args...) | ||
| } | ||
|
|
||
| var output string | ||
| if output, err = l.Shell().Exec(l.logs, args...); err != nil { | ||
| return | ||
| } | ||
|
|
||
| for _, line := range strings.Split(output, "\n") { | ||
| if line = strings.TrimSpace(line); line == "" { | ||
| continue | ||
| } | ||
| entry := parseLogLine(line) | ||
| var payload []byte | ||
| if payload, err = json.Marshal(entry); err != nil { | ||
| return | ||
| } | ||
| l.Shell().Println(string(payload)) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func (l *KoolLogs) streamLogsJSON(args ...string) (err error) { | ||
| cmdArgs := l.logs.Args() | ||
| if len(args) > 0 { | ||
| cmdArgs = append(cmdArgs, args...) | ||
| } | ||
| cmd := execLogsCmd(l.logs.Cmd(), cmdArgs...) | ||
| cmd.Env = os.Environ() | ||
| cmd.Stderr = l.Shell().ErrStream() | ||
|
|
||
| stdout, e := cmd.StdoutPipe() | ||
| if e != nil { | ||
| err = e | ||
| return | ||
| } | ||
|
|
||
| if err = cmd.Start(); err != nil { | ||
| return | ||
| } | ||
|
|
||
| scanner := bufio.NewScanner(stdout) | ||
|
Member
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. Two problems with this scanner in the follow path:
Suggest raising the buffer and checking the error: scanner := bufio.NewScanner(stdout)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for scanner.Scan() {
...
}
if e := scanner.Err(); e != nil {
_ = cmd.Wait()
return e
}Or drop the scanner entirely for a |
||
| for scanner.Scan() { | ||
| line := strings.TrimSpace(scanner.Text()) | ||
| if line == "" { | ||
| continue | ||
| } | ||
| entry := parseLogLine(line) | ||
| if payload, e := json.Marshal(entry); e == nil { | ||
| l.Shell().Println(string(payload)) | ||
| } | ||
| } | ||
|
|
||
| err = cmd.Wait() | ||
| return | ||
| } | ||
|
|
||
| // parseLogLine parses a docker-compose log line into a logEntryJSON. | ||
| // Docker compose log format: "service_name | message" (with optional padding). | ||
| // If the line doesn't match, service is empty and message is the full line. | ||
| func parseLogLine(line string) logEntryJSON { | ||
| if idx := strings.Index(line, "|"); idx >= 0 { | ||
| service := strings.TrimSpace(line[:idx]) | ||
| message := strings.TrimSpace(line[idx+1:]) | ||
| return logEntryJSON{Service: service, Message: message} | ||
| } | ||
| return logEntryJSON{Service: "", Message: line} | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The non-follow path goes through
Shell().Exec, which usesCombinedOutputβ so docker compose's own stderr chatter (WARN[0000] ..., orphan-container notices, deprecation warnings) is interleaved intooutputand then fed line-by-line throughparseLogLine, producing bogus log entries in the JSON stream.The
--followpath gets this right by taking onlyStdoutPipe(). Worth making the two consistent, so the same command with and without-fdoesn't yield structurally different data.