-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (94 loc) · 2.57 KB
/
main.go
File metadata and controls
108 lines (94 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Example: chat — fetch chat history and send a message.
//
// Usage:
//
// GATEWAY_URL=ws://localhost:18789 \
// GATEWAY_TOKEN=mytoken \
// SESSION_KEY=main \
// MESSAGE="What can you do?" \
// go run ./examples/chat/
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/google/uuid"
"github.com/yuchou87/openclaw-go/client"
"github.com/yuchou87/openclaw-go/protocol"
)
func main() {
url := getenv("GATEWAY_URL", "ws://localhost:18789")
token := getenv("GATEWAY_TOKEN", "")
password := getenv("GATEWAY_PASSWORD", "")
sessionKey := getenv("SESSION_KEY", "main")
message := getenv("MESSAGE", "What can you do?")
c := client.New(url, token, password)
ctx := context.Background()
fmt.Printf("Connecting to %s ...\n", url)
if err := c.Connect(ctx); err != nil {
log.Fatalf("Connect: %v", err)
}
defer c.Close()
fmt.Println("✓ Connected")
// 1. Fetch chat history.
limit := 20
historyRaw, err := c.ChatHistory(ctx, sessionKey, &limit)
if err != nil {
log.Printf("ChatHistory: %v (may be empty for new sessions)", err)
} else {
pretty, _ := json.MarshalIndent(json.RawMessage(historyRaw), " ", " ")
fmt.Printf("✓ Chat history (last %d):\n %s\n\n", limit, pretty)
}
// 2. Subscribe to events before sending.
events, err := c.Subscribe(ctx)
if err != nil {
log.Fatalf("Subscribe: %v", err)
}
// 3. Send a chat message.
thinking := "default"
fmt.Printf("Sending message to session %q: %q\n", sessionKey, message)
respRaw, err := c.ChatSend(ctx, protocol.ChatSendParams{
SessionKey: sessionKey,
Message: message,
Thinking: &thinking,
IdempotencyKey: uuid.NewString(),
})
if err != nil {
log.Fatalf("ChatSend: %v", err)
}
fmt.Printf("✓ ChatSend response: %s\n\n", string(respRaw))
// 4. Stream chat events until done.
fmt.Println("Streaming chat events (Ctrl+C to stop):")
for evt := range events {
if evt.Event != "chat" {
continue
}
// Decode the chat event state.
var chatEvt struct {
State string `json:"state"`
StopReason string `json:"stopReason"`
Message *struct {
Text string `json:"text"`
} `json:"message"`
}
if err := json.Unmarshal(evt.Payload, &chatEvt); err != nil {
continue
}
if chatEvt.Message != nil {
fmt.Printf(" [%s] %s\n", chatEvt.State, chatEvt.Message.Text)
} else {
fmt.Printf(" [%s] stopReason=%s\n", chatEvt.State, chatEvt.StopReason)
}
if chatEvt.State == "done" || chatEvt.State == "error" {
break
}
}
}
func getenv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}