ituoke/internal/agent/service.go

85 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package agent
import (
"fmt"
"sync"
"github.com/ituoke/ituoke/internal/protocol"
)
// ============================================================
// [Agent 桥 · 关键] AgentService — 前端对话入口
// 第一期:本地 stub / 可直连云端 API
// 第二期:经 JSON-RPC 调 agent/ 下的 Python 独立进程
// 改对话协议字段:internal/protocol/messages.go
// 改 Python 进程启停:见 Start / Stop / client.go
// ============================================================
type Service struct {
mu sync.RWMutex
running bool
endpoint string
client *Client
}
func NewService() *Service {
return &Service{
endpoint: protocol.DefaultAgentAddr,
client: NewClient(protocol.DefaultAgentAddr),
}
}
// Status 返回 Agent 进程/连接状态(给设置页、状态栏用)
func (s *Service) Status() map[string]any {
s.mu.RLock()
defer s.mu.RUnlock()
return map[string]any{
"protocolVersion": protocol.Version,
"endpoint": s.endpoint,
"running": s.running,
"mode": "stub", // stub | python | cloud — 后续切换
}
}
// Start 启动 Python Agent sidecar(骨架:仅标记状态)
func (s *Service) Start() error {
s.mu.Lock()
defer s.mu.Unlock()
// TODO[Agent]: 用 exec.Command 拉起 agent/venv 里的 main.py
// TODO[Agent]: 健康检查 MethodAgentHealth
s.running = true
return nil
}
// Stop 停止 Python Agent
func (s *Service) Stop() error {
s.mu.Lock()
defer s.mu.Unlock()
// TODO[Agent]: 发信号结束子进程
s.running = false
return nil
}
// Chat 发送一条用户消息,返回助手回复(第一期 stub)
func (s *Service) Chat(sessionID, message string) (map[string]string, error) {
if message == "" {
return nil, fmt.Errorf("empty message")
}
// TODO[Agent]:
// 1) 可选:device.CaptureFrame → 带图
// 2) client.Call(MethodAgentChat / MethodAgentPlanNext, params)
// 3) 若 Result 含 actions → 交给 device.Service 执行
_ = sessionID
return map[string]string{
"role": "assistant",
"content": "(骨架)已收到:「" + message + "」。接入 Python Agent / 云端 API 后,这里返回真实回复与下一步操作。",
}, nil
}
// ProtocolVersion 给前端校验契约
func (s *Service) ProtocolVersion() string {
return protocol.Version
}