first commit
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
@@ -47,7 +48,24 @@ func handleNewSession(piClient *rpc.Client) gin.HandlerFunc {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, jsonRaw(resp.Result))
|
||||
if !resp.Success {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: resp.Error})
|
||||
return
|
||||
}
|
||||
// merge sessionFile from a follow-up get_state
|
||||
out := map[string]any{}
|
||||
if len(resp.Data) > 0 {
|
||||
_ = jsonUnmarshalInto(resp.Data, &out)
|
||||
}
|
||||
if state, serr := piClient.SendCmd(models.RPCCommand{Type: "get_state"}); serr == nil && state.Success {
|
||||
var sd struct {
|
||||
SessionFile string `json:"sessionFile"`
|
||||
}
|
||||
if jsonUnmarshalInto(state.Data, &sd) == nil && sd.SessionFile != "" {
|
||||
out["sessionFile"] = sd.SessionFile
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, out)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,20 +139,31 @@ func handleLoadSession(cfg *config.Config, piClient *rpc.Client) gin.HandlerFunc
|
||||
c.JSON(http.StatusForbidden, models.ErrorResponse{Error: "invalid path"})
|
||||
return
|
||||
}
|
||||
lines, err := services.ReadSessionMessages(req.Path)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, models.ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
resp, err := piClient.SendCmd(models.RPCCommand{
|
||||
Type: "switch_session",
|
||||
Path: req.Path,
|
||||
sw, err := piClient.SendCmd(models.RPCCommand{
|
||||
Type: "switch_session",
|
||||
SessionPath: req.Path,
|
||||
CwdOverride: cfg.RepoRoot,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"messages": lines, "result": jsonRaw(resp.Result)})
|
||||
if !sw.Success {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: sw.Error})
|
||||
return
|
||||
}
|
||||
mr, err := piClient.SendCmd(models.RPCCommand{Type: "get_messages"})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
if !mr.Success {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: mr.Error})
|
||||
return
|
||||
}
|
||||
// mr.Data is { messages: [...] }; forward it plus a session summary
|
||||
summary, _ := services.ReadSessionSummaryByPath(req.Path)
|
||||
c.JSON(http.StatusOK, gin.H{"messages": extractMessages(mr.Data), "session": summary})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,15 +178,25 @@ func handleActivateSession(cfg *config.Config, piClient *rpc.Client) gin.Handler
|
||||
c.JSON(http.StatusForbidden, models.ErrorResponse{Error: "invalid path"})
|
||||
return
|
||||
}
|
||||
resp, err := piClient.SendCmd(models.RPCCommand{
|
||||
Type: "switch_session",
|
||||
Path: req.Path,
|
||||
sw, err := piClient.SendCmd(models.RPCCommand{
|
||||
Type: "switch_session",
|
||||
SessionPath: req.Path,
|
||||
CwdOverride: cfg.RepoRoot,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, jsonRaw(resp.Result))
|
||||
if !sw.Success {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: sw.Error})
|
||||
return
|
||||
}
|
||||
state, err := piClient.SendCmd(models.RPCCommand{Type: "get_state"})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "state": jsonRaw(state.Data)})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,10 +221,36 @@ func handleNameSession(cfg *config.Config) gin.HandlerFunc {
|
||||
|
||||
func handleSessionState(piClient *rpc.Client) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
snap := piClient.GetSnapshot()
|
||||
c.JSON(http.StatusOK, models.SessionStateResponse{
|
||||
IsStreaming: snap.IsStreaming,
|
||||
SessionFile: snap.SessionFile,
|
||||
})
|
||||
resp, err := piClient.SendCmd(models.RPCCommand{Type: "get_state"})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: err.Error()})
|
||||
return
|
||||
}
|
||||
if !resp.Success {
|
||||
c.JSON(http.StatusInternalServerError, models.ErrorResponse{Error: resp.Error})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, jsonRaw(resp.Data))
|
||||
}
|
||||
}
|
||||
|
||||
// extractMessages pulls the "messages" array out of a get_messages data payload.
|
||||
func extractMessages(data json.RawMessage) any {
|
||||
if len(data) == 0 {
|
||||
return []any{}
|
||||
}
|
||||
var wrap struct {
|
||||
Messages json.RawMessage `json:"messages"`
|
||||
}
|
||||
if jsonUnmarshalInto(data, &wrap) == nil && len(wrap.Messages) > 0 {
|
||||
return jsonRaw(wrap.Messages)
|
||||
}
|
||||
return jsonRaw(data)
|
||||
}
|
||||
|
||||
func jsonUnmarshalInto(raw json.RawMessage, v any) error {
|
||||
if len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(raw, v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user