|
| 1 | +package ipc |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type Request struct { |
| 12 | + Type string `json:"type"` |
| 13 | + Method string `json:"method"` |
| 14 | + ID string `json:"id"` |
| 15 | + Payload interface{} `json:"payload"` |
| 16 | +} |
| 17 | + |
| 18 | +type Response struct { |
| 19 | + Type string `json:"type"` |
| 20 | + ID string `json:"id"` |
| 21 | + Payload json.RawMessage `json:"payload"` |
| 22 | +} |
| 23 | + |
| 24 | +type QueryResult struct { |
| 25 | + Content string `json:"content"` |
| 26 | + Reasoning string `json:"reasoning"` |
| 27 | +} |
| 28 | + |
| 29 | +// Query sends a mutation/query payload over the vibeauracle UDS. |
| 30 | +func Query(content string) (string, error) { |
| 31 | + conn, err := net.DialTimeout("unix", SocketPath(), 5*time.Second) |
| 32 | + if err != nil { |
| 33 | + return "", fmt.Errorf("vibeauracle uds: %w", err) |
| 34 | + } |
| 35 | + defer conn.Close() |
| 36 | + |
| 37 | + id := fmt.Sprintf("polygeist-%d", time.Now().UnixNano()) |
| 38 | + req := Request{ |
| 39 | + Type: "request", |
| 40 | + Method: "query", |
| 41 | + ID: id, |
| 42 | + Payload: map[string]string{ |
| 43 | + "content": content, |
| 44 | + "intent": "vibe", |
| 45 | + }, |
| 46 | + } |
| 47 | + data, err := json.Marshal(req) |
| 48 | + if err != nil { |
| 49 | + return "", err |
| 50 | + } |
| 51 | + if _, err := conn.Write(append(data, '\n')); err != nil { |
| 52 | + return "", err |
| 53 | + } |
| 54 | + |
| 55 | + scanner := bufio.NewScanner(conn) |
| 56 | + scanner.Buffer(make([]byte, 0, 64*1024), 10*1024*1024) |
| 57 | + if !scanner.Scan() { |
| 58 | + return "", fmt.Errorf("no response from vibeauracle") |
| 59 | + } |
| 60 | + |
| 61 | + var resp Response |
| 62 | + if err := json.Unmarshal(scanner.Bytes(), &resp); err != nil { |
| 63 | + return "", err |
| 64 | + } |
| 65 | + if resp.Type == "error" { |
| 66 | + return "", fmt.Errorf("%s", string(resp.Payload)) |
| 67 | + } |
| 68 | + |
| 69 | + var result QueryResult |
| 70 | + if err := json.Unmarshal(resp.Payload, &result); err != nil { |
| 71 | + return string(resp.Payload), nil |
| 72 | + } |
| 73 | + if result.Content != "" { |
| 74 | + return result.Content, nil |
| 75 | + } |
| 76 | + return result.Reasoning, nil |
| 77 | +} |
| 78 | + |
| 79 | +// Ping checks whether the vibeauracle daemon is reachable. |
| 80 | +func Ping() error { |
| 81 | + conn, err := net.DialTimeout("unix", SocketPath(), 2*time.Second) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + defer conn.Close() |
| 86 | + |
| 87 | + id := fmt.Sprintf("ping-%d", time.Now().UnixNano()) |
| 88 | + req := Request{Type: "request", Method: "ping", ID: id} |
| 89 | + data, _ := json.Marshal(req) |
| 90 | + if _, err := conn.Write(append(data, '\n')); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + scanner := bufio.NewScanner(conn) |
| 94 | + if !scanner.Scan() { |
| 95 | + return fmt.Errorf("no pong") |
| 96 | + } |
| 97 | + return nil |
| 98 | +} |
0 commit comments