-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgraphql.go
More file actions
164 lines (149 loc) Β· 5.66 KB
/
Copy pathgraphql.go
File metadata and controls
164 lines (149 loc) Β· 5.66 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package metaai
// graphql.go implements the persisted-query GraphQL HTTP transport used by
// meta.ai for SUPPORTING operations (conversation config, mark-seen, latency,
// history, media fetch, generation). Chat sendMessage itself goes over the
// clippy WebSocket.
//
// Request shape (captured): POST https://meta.ai/api/graphql with JSON
// {"doc_id": "...", "variables": {...}}, headers Authorization (ecto1: token),
// Origin, Referer, browser UA, plus auth cookies.
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// graphqlResponse is the raw JSON envelope returned by /api/graphql.
type graphqlResponse struct {
Data json.RawMessage `json:"data,omitempty"`
Errors []graphQLError `json:"errors,omitempty"`
}
// graphQLError mirrors a Meta GraphQL error entry.
type graphQLError struct {
Message string `json:"message"`
Extensions map[string]any `json:"extensions,omitempty"`
}
// code returns the GraphQL error extension code (e.g. GRAPHQL_VALIDATION_FAILED).
func (e graphQLError) code() string {
if e.Extensions == nil {
return ""
}
if c, ok := e.Extensions["code"].(string); ok {
return c
}
return ""
}
// graphqlRequest sends a persisted-query GraphQL POST and decodes the JSON.
// The body is {"doc_id": docID, "variables": variables}. The Authorization
// header is set to the raw access token (matches the capture; the history/search
// paths use "OAuth <token>" via graphqlRequestOAuth).
//
// Raises ErrGraphQLValidation when Meta returns a GRAPHQL_VALIDATION_FAILED
// error (doc_id drift).
func (c *Client) graphqlRequest(ctx context.Context, docID string, variables any) (json.RawMessage, error) {
return c.graphqlRequestAuth(ctx, docID, variables, false)
}
// graphqlRequestOAuth is like graphqlRequest but sends "Authorization: OAuth <token>"
// (used by viewer/history/search queries).
func (c *Client) graphqlRequestOAuth(ctx context.Context, docID string, variables any) (json.RawMessage, error) {
return c.graphqlRequestAuth(ctx, docID, variables, true)
}
func (c *Client) graphqlRequestAuth(ctx context.Context, docID string, variables any, oauth bool) (json.RawMessage, error) {
body, err := json.Marshal(map[string]any{"doc_id": docID, "variables": variables})
if err != nil {
return nil, fmt.Errorf("metaai: marshal graphql body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, GraphqlURL, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Accept", "*/*")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Origin", "https://www.meta.ai")
req.Header.Set("Referer", "https://www.meta.ai/")
req.Header.Set("User-Agent", c.cfg.UserAgent)
if c.accessToken != "" {
if oauth {
req.Header.Set("Authorization", "OAuth "+c.accessToken)
} else {
// Supporting ops send the bare ecto1: token.
req.Header.Set("Authorization", c.accessToken)
}
}
attachCookies(req, c.cookies)
resp, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("metaai: graphql POST: %w", err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("metaai: read graphql response: %w", err)
}
var gr graphqlResponse
if jerr := json.Unmarshal(raw, &gr); jerr != nil {
return nil, fmt.Errorf("metaai: decode graphql response: %w (body: %s)", jerr, truncate(string(raw), 200))
}
if len(gr.Errors) > 0 {
if gr.Errors[0].code() == "GRAPHQL_VALIDATION_FAILED" {
return nil, fmt.Errorf("%w: %s", ErrGraphQLValidation, gr.Errors[0].Message)
}
return nil, fmt.Errorf("metaai: graphql error: %s", gr.Errors[0].Message)
}
return gr.Data, nil
}
// RegisterConversation POSTs the conversation-registration mutation
// (doc_id e7f80258β¦) captured in the new-conversation lifecycle. Optional β
// the default chat flow does not require it, but callers wanting to mirror the
// full browser lifecycle may call it before StreamChat.
func (c *Client) RegisterConversation(ctx context.Context, conversationID string) error {
_, err := c.graphqlRequest(ctx, DocIDs.ConversationRegistration, map[string]any{
"conversationId": conversationID,
})
return err
}
// SetConversationMode POSTs the mode-setter mutation (doc_id c32bbe99β¦).
// mode is "think_hard" when thinking is enabled, else "fast". Optional.
func (c *Client) SetConversationMode(ctx context.Context, conversationID string, thinking bool) error {
mode := "fast"
if thinking {
mode = "think_hard"
}
_, err := c.graphqlRequest(ctx, DocIDs.ModeSetter, map[string]any{
"input": map[string]any{
"conversationId": conversationID,
"mode": mode,
},
})
return err
}
// markConversationSeen POSTs the mark-seen mutation (doc_id 0b2cb3a4...,
// = META_AI_LAST_SEEN_DOC_ID) captured in the live flow. Errors are logged
// because this is a best-effort notification; the chat stream continues
// regardless of whether the server acknowledges the seen marker.
func (c *Client) markConversationSeen(ctx context.Context, conversationID, lastSeenMessageID string) {
if conversationID == "" || lastSeenMessageID == "" {
return
}
vars := map[string]any{
"input": map[string]any{
"conversationId": conversationID,
"lastSeenMessageId": lastSeenMessageID,
},
}
if _, err := c.graphqlRequest(ctx, DocIDs.MarkSeen, vars); err != nil {
// Non-fatal: mark-seen is a best-effort server notification and the
// chat stream continues regardless. Surface the failure at debug level
// so operators can detect persistent breakage (e.g. doc_id drift).
c.logger.Debugf("metaai: mark-seen failed for conversation %s (lastSeen=%s): %v",
conversationID, lastSeenMessageID, err)
}
}
func truncate(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n] + "β¦"
}