mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-03 15:53:56 +00:00
feat: waiter CLI remote mode calls REST API directly for management commands
This commit is contained in:
@ -76,19 +76,19 @@ func (c *localConn) Close() error {
|
||||
|
||||
func dialRemote(baseURL, apiKey string) (Conn, error) {
|
||||
baseURL = strings.TrimRight(baseURL, "/")
|
||||
return &remoteConn{url: baseURL + "/api/v1/chat", apiKey: apiKey}, nil
|
||||
return &remoteConn{baseURL: baseURL, apiKey: apiKey}, nil
|
||||
}
|
||||
|
||||
type remoteConn struct {
|
||||
url string
|
||||
apiKey string
|
||||
mu sync.Mutex
|
||||
buf []string
|
||||
closed bool
|
||||
baseURL string
|
||||
apiKey string
|
||||
mu sync.Mutex
|
||||
buf []string
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (c *remoteConn) Send(line string) error {
|
||||
req, err := http.NewRequest("POST", c.url, strings.NewReader(
|
||||
req, err := http.NewRequest("POST", c.baseURL+"/api/v1/chat", strings.NewReader(
|
||||
fmt.Sprintf(`{"message":%q}`, line),
|
||||
))
|
||||
if err != nil {
|
||||
@ -117,6 +117,31 @@ func (c *remoteConn) Send(line string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *remoteConn) DoAPI(method, path string, body string) (map[string]interface{}, error) {
|
||||
var bodyReader io.Reader
|
||||
if body != "" {
|
||||
bodyReader = strings.NewReader(body)
|
||||
}
|
||||
req, err := http.NewRequest(method, c.baseURL+path, bodyReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if c.apiKey != "" {
|
||||
req.Header.Set("X-API-Key", c.apiKey)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *remoteConn) ReadLine() (string, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user