| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package event
- import (
- "strconv"
- "time"
- )
- type SSEEvent struct {
- Type string `json:"type"`
- Properties map[string]interface{} `json:"properties"`
- }
- type ParsedEvent struct {
- Timestamp string
- Port int
- Message string
- }
- func ParseStatus(status map[string]interface{}) string {
- if status == nil {
- return "未知"
- }
- t, _ := status["type"].(string)
- switch t {
- case "idle":
- return "空闲"
- case "busy":
- return "忙碌"
- case "retry":
- return "重试中"
- default:
- return t
- }
- }
- func ParseToolState(state map[string]interface{}) string {
- if state == nil {
- return ""
- }
- s, _ := state["status"].(string)
- title, _ := state["title"].(string)
- switch s {
- case "running":
- if title != "" {
- return "运行中: " + title
- }
- return "运行中"
- case "completed":
- if title != "" {
- return "完成: " + title
- }
- return "完成"
- case "error":
- return "错误"
- default:
- return s
- }
- }
- func FormatEvent(port int, evt *SSEEvent) string {
- ts := time.Now().Format("15:04:05")
- prefix := "[" + ts + "] [:" + strconv.Itoa(port) + "]"
- switch evt.Type {
- case "session.status":
- if status, ok := evt.Properties["status"].(map[string]interface{}); ok {
- return prefix + " 状态: " + ParseStatus(status)
- }
- case "session.idle":
- return prefix + " 状态: 空闲"
- case "message.part.updated":
- if part, ok := evt.Properties["part"].(map[string]interface{}); ok {
- pt, _ := part["type"].(string)
- switch pt {
- case "tool":
- tool, _ := part["tool"].(string)
- state := ParseToolState(part["state"].(map[string]interface{}))
- return prefix + " 工具: " + tool + " - " + state
- case "reasoning":
- return prefix + " 思考中..."
- }
- }
- case "permission.updated":
- if title, ok := evt.Properties["title"].(string); ok {
- return prefix + " 权限请求: " + title
- }
- case "session.error":
- return prefix + " 错误"
- }
- return ""
- }
|