-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
221 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package httpserver | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/synw/agent-smith/server/lm" | ||
"github.com/synw/agent-smith/server/state" | ||
"github.com/synw/agent-smith/server/types" | ||
) | ||
|
||
func ExecuteCmdHandler(c echo.Context) error { | ||
m := echo.Map{} | ||
if err := c.Bind(&m); err != nil { | ||
return err | ||
} | ||
cmd, ok := m["cmd"] | ||
if !ok { | ||
msg := "Provide a 'cmd' string parameter" | ||
return echo.NewHTTPError(http.StatusBadRequest, msg) | ||
} | ||
params, ok := m["params"] | ||
if !ok { | ||
params = []string{} | ||
} | ||
|
||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
c.Response().WriteHeader(http.StatusOK) | ||
ch := make(chan types.StreamedMessage) | ||
errCh := make(chan types.StreamedMessage) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
rawParams := params.([]interface{}) | ||
params := lm.InterfaceToStringArray(rawParams) | ||
lm.RunCmd(cmd.(string), params, c, ch, errCh) | ||
}() | ||
|
||
select { | ||
case res, ok := <-ch: | ||
if ok { | ||
if state.IsDebug { | ||
fmt.Println("-------- result ----------") | ||
for key, value := range res.Data { | ||
fmt.Printf("%s: %v\n", key, value) | ||
} | ||
fmt.Println("--------------------------") | ||
} | ||
} | ||
wg.Wait() | ||
close(ch) | ||
close(errCh) | ||
return nil | ||
case err, ok := <-errCh: | ||
if ok { | ||
enc := json.NewEncoder(c.Response()) | ||
err := lm.StreamMsg(err, c, enc) | ||
if err != nil { | ||
if state.IsDebug { | ||
fmt.Println("Streaming error", err) | ||
errCh <- types.StreamedMessage{ | ||
Content: "Streaming error", | ||
MsgType: types.ErrorMsgType, | ||
} | ||
} | ||
wg.Wait() | ||
close(ch) | ||
close(errCh) | ||
return c.NoContent(http.StatusInternalServerError) | ||
} | ||
} else { | ||
wg.Wait() | ||
close(ch) | ||
close(errCh) | ||
return c.JSON(http.StatusInternalServerError, err) | ||
} | ||
wg.Wait() | ||
close(ch) | ||
close(errCh) | ||
return nil | ||
case <-c.Request().Context().Done(): | ||
fmt.Println("\nRequest canceled") | ||
state.ContinueInferingController = false | ||
wg.Wait() | ||
close(ch) | ||
close(errCh) | ||
return c.NoContent(http.StatusNoContent) | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package lm | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/synw/agent-smith/server/types" | ||
) | ||
|
||
func RunCmd( | ||
cmdName string, | ||
params []string, | ||
c echo.Context, | ||
ch chan<- types.StreamedMessage, | ||
errCh chan<- types.StreamedMessage, | ||
) { | ||
// Create the command with the arguments | ||
params = append([]string{cmdName}, params...) | ||
fmt.Println("Params:") | ||
for _, p := range params { | ||
fmt.Println("-", p) | ||
} | ||
fmt.Println("Cmd", "lm", params) | ||
cmd := exec.Command("lm", params...) | ||
|
||
// Create a pipe to capture the command's output | ||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
msg := fmt.Errorf("Error creating stdout pipe:", err) | ||
errCh <- createErrorMsg(msg.Error()) | ||
return | ||
} | ||
|
||
// Start the command | ||
if err := cmd.Start(); err != nil { | ||
msg := fmt.Errorf("Error starting command:", err) | ||
errCh <- createErrorMsg(msg.Error()) | ||
return | ||
} | ||
|
||
// Create a scanner to read the output word by word | ||
scanner := bufio.NewScanner(stdout) | ||
scanner.Split(bufio.ScanWords) // Set the split function to scan words | ||
|
||
// Read and print the output word by word | ||
i := 0 | ||
for scanner.Scan() { | ||
i++ | ||
token := scanner.Text() | ||
fmt.Println("T", token) | ||
ch <- createMsg(token, i) | ||
} | ||
|
||
// Check for errors during scanning | ||
if err := scanner.Err(); err != nil { | ||
msg := fmt.Errorf("Error reading output:", err) | ||
fmt.Println(msg) | ||
errCh <- createErrorMsg(msg.Error()) | ||
} | ||
|
||
// Wait for the command to finish | ||
if err := cmd.Wait(); err != nil { | ||
msg := fmt.Errorf("Command finished with error:", err) | ||
errCh <- createErrorMsg(msg.Error()) | ||
} | ||
} |
This file contains 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 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 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