Repository navigation
Expand file tree
/
Copy pathconversation.go
More file actions
378 lines (372 loc) · 11.3 KB
/
Copy pathconversation.go
File metadata and controls
378 lines (372 loc) · 11.3 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"os"
"runtime"
"strings"
)
const dynamicCall = "CallDynamicTool"
const missingToolResult = "Tool use was interrupted and did not produce a result."
type chatRequest struct {
Model string `json:"model"`
Messages []map[string]any `json:"messages"`
Tools []map[string]any `json:"tools"`
ToolChoice any `json:"tool_choice"`
ReasoningEffort string `json:"reasoning_effort"`
ServiceTier string `json:"service_tier"`
PromptCacheKey string `json:"prompt_cache_key"`
toolSchemas []string
}
func (c *chatRequest) UnmarshalJSON(raw []byte) error {
type alias chatRequest
if err := json.Unmarshal(raw, (*alias)(c)); err != nil {
return err
}
var source struct {
Tools []struct {
Function struct {
Parameters json.RawMessage `json:"parameters"`
} `json:"function"`
} `json:"tools"`
}
if err := json.Unmarshal(raw, &source); err != nil {
return err
}
c.toolSchemas = make([]string, len(source.Tools))
for i, t := range source.Tools {
if len(t.Function.Parameters) > 0 {
var b bytes.Buffer
if json.Compact(&b, t.Function.Parameters) == nil {
c.toolSchemas[i] = b.String()
}
}
}
return nil
}
type chatTool struct {
Name, Description string
Schema map[string]any
SchemaText string
}
type chatConversation struct {
Messages [][]byte
Last string
Estimate int
}
func textOf(value any) string {
if s, ok := value.(string); ok {
return s
}
var out strings.Builder
for _, raw := range array(value) {
part := object(raw)
if str(part["type"]) == "text" {
out.WriteString(str(part["text"]))
}
}
return out.String()
}
func reasoningOf(m map[string]any) string {
for _, key := range []string{"reasoning_content", "reasoning"} {
if s := str(m[key]); strings.TrimSpace(s) != "" {
return s
}
}
return ""
}
func toolsOf(chat chatRequest) []chatTool {
tools := []chatTool{}
if chat.ToolChoice == "none" {
return tools
}
only := str(object(object(chat.ToolChoice)["function"])["name"])
for i, raw := range chat.Tools {
f := object(raw["function"])
name := str(f["name"])
if str(raw["type"]) != "function" || name == "" || only != "" && only != name {
continue
}
schema := object(f["parameters"])
if schema == nil {
schema = map[string]any{"type": "object", "properties": map[string]any{}}
}
schemaText := string(encodeJSON(schema))
if i < len(chat.toolSchemas) && chat.toolSchemas[i] != "" {
schemaText = chat.toolSchemas[i]
}
tools = append(tools, chatTool{Name: name, Description: str(f["description"]), Schema: schema, SchemaText: schemaText})
}
return tools
}
func toolCatalog(tools []chatTool) string {
if len(tools) == 0 {
return ""
}
var out strings.Builder
out.WriteString("\n\n<dynamic_tool_catalog>\nThe MCP namespace \"magpie\" has the tools below. They are reached only through `CallDynamicTool`, never as tools of their own. Each entry is the exact call to make; its arguments must match the schema given. The schemas are complete, so there is no need to call `GetDynamicTools` first.\n")
for _, t := range tools {
out.WriteString("<call>CallDynamicTool({\"namespace\":\"magpie\",\"toolName\":")
out.Write(encodeJSON(t.Name))
out.WriteString(",\"arguments\":{...}})\n")
out.WriteString(t.Description)
out.WriteString("\narguments schema: ")
out.WriteString(t.SchemaText)
out.WriteString("\n</call>\n")
}
out.WriteString("</dynamic_tool_catalog>")
return out.String()
}
func joinAssistantCalls(messages []map[string]any) []map[string]any {
out := []map[string]any{}
for _, m := range messages {
if len(out) > 0 && str(m["role"]) == "assistant" {
prev := out[len(out)-1]
if str(prev["role"]) == "assistant" && len(array(prev["tool_calls"])) > 0 {
copy := map[string]any{}
for k, v := range prev {
copy[k] = v
}
copy["content"] = joinText(textOf(prev["content"]), textOf(m["content"]))
copy["reasoning_content"] = joinText(reasoningOf(prev), reasoningOf(m))
copy["tool_calls"] = append(append([]any{}, array(prev["tool_calls"])...), array(m["tool_calls"])...)
out[len(out)-1] = copy
continue
}
}
out = append(out, m)
}
return out
}
func joinText(a, b string) string {
if a == "" {
return b
}
if b == "" {
return a
}
return a + "\n\n" + b
}
func cursorCallID(id string) string {
if strings.HasPrefix(id, "call_") {
return strings.Replace(id, "__fc_", "\nfc_", 1)
}
return id
}
func toolResult(id, text string, isError bool) map[string]any {
var result any = text
var decoded any
if json.Unmarshal([]byte(text), &decoded) == nil {
result = decoded
}
out := map[string]any{"type": "tool-result", "toolCallId": cursorCallID(id), "toolName": dynamicCall, "result": result, "experimental_content": []any{map[string]any{"type": "text", "text": text}}}
if isError {
out["isError"] = true
}
return out
}
func conversation(chat chatRequest, tools []chatTool) (chatConversation, error) {
conv := chatConversation{Messages: [][]byte{}, Last: "."}
size := 0
systems := []string{}
add := func(role string, content any) {
conv.Messages = append(conv.Messages, encodeJSON(map[string]any{"role": role, "content": content}))
}
for _, m := range chat.Messages {
if role := str(m["role"]); role == "system" || role == "developer" {
if text := textOf(m["content"]); text != "" {
systems = append(systems, text)
}
}
}
system := strings.Join(systems, "\n\n") + toolCatalog(tools)
size += len(system)
if system != "" {
add("system", system)
}
pending := []string{}
results := []any{}
answer := func() {
for _, id := range pending {
results = append(results, toolResult(id, missingToolResult, true))
}
pending = nil
if len(results) > 0 {
add("tool", results)
}
results = nil
}
for _, m := range joinAssistantCalls(chat.Messages) {
role := str(m["role"])
if role == "system" || role == "developer" {
continue
}
if role == "tool" {
id := str(m["tool_call_id"])
if at := indexOf(pending, id); at >= 0 {
pending = append(pending[:at], pending[at+1:]...)
text := textOf(m["content"])
size += len(text)
results = append(results, toolResult(id, text, m["is_error"] == true || m["isError"] == true))
}
continue
}
if role == "assistant" {
answer()
content := []any{}
if reasoning := reasoningOf(m); reasoning != "" {
content = append(content, map[string]any{"type": "reasoning", "text": reasoning})
size += len(reasoning)
}
if text := textOf(m["content"]); text != "" {
content = append(content, map[string]any{"type": "text", "text": text})
size += len(text)
}
for _, raw := range array(m["tool_calls"]) {
call := object(raw)
if kind := str(call["type"]); kind != "" && kind != "function" {
continue
}
f := object(call["function"])
args := object(f["arguments"])
if args == nil {
args, _ = jsonObject([]byte(str(f["arguments"])))
}
if args == nil {
args = map[string]any{}
}
id := str(call["id"])
content = append(content, map[string]any{"type": "tool-call", "toolCallId": cursorCallID(id), "toolName": dynamicCall, "args": map[string]any{"namespace": "magpie", "toolName": str(f["name"]), "arguments": args}})
pending = append(pending, id)
size += len(encodeJSON(args)) + len(str(f["name"]))
}
if len(content) > 0 {
add("assistant", content)
}
continue
}
content := []any{}
parts := array(m["content"])
if text, ok := m["content"].(string); ok {
parts = []any{map[string]any{"type": "text", "text": text}}
}
first := ""
for _, raw := range parts {
part := object(raw)
switch str(part["type"]) {
case "text":
text := str(part["text"])
if text != "" {
content = append(content, map[string]any{"type": "text", "text": text})
size += len(text)
if first == "" {
first = text
}
}
case "image_url":
u := str(part["image_url"])
if u == "" {
u = str(object(part["image_url"])["url"])
}
if strings.HasPrefix(u, "data:") {
at := strings.Index(u, ";base64,")
if at < 5 {
return conv, failure(400, "Invalid image data URL")
}
data, err := base64.StdEncoding.DecodeString(u[at+8:])
if err != nil {
return conv, failure(400, "Invalid base64 image")
}
content = append(content, map[string]any{"type": "image", "mimeType": u[5:at], "image": map[string]any{"__type": "Uint8Array", "hex": hex.EncodeToString(data)}})
} else {
content = append(content, map[string]any{"type": "text", "text": "[attachment image: " + u + "]"})
}
case "file":
content = append(content, map[string]any{"type": "text", "text": "[attachment " + str(object(part["file"])["filename"]) + "]"})
}
}
answer()
if first != "" {
conv.Last = first
}
if len(content) > 0 {
add("user", content)
}
}
answer()
for _, t := range tools {
size += len(t.Name) + len(t.Description) + len(encodeJSON(t.Schema))
}
conv.Estimate = (size + 3) / 4
return conv, nil
}
func toolDefinition(t chatTool) protobuf {
return protobuf(nil).text(1, t.Name).text(2, t.Description).bytes(3, pbValue(t.Schema)).text(4, "magpie").text(5, t.Name).text(6, t.SchemaText)
}
func cursorEnvironment() protobuf {
return protobuf(nil).text(1, runtime.GOOS).text(2, os.TempDir()).text(10, "UTC")
}
func buildRun(conv chatConversation, tools []chatTool, model modelResolution, id string) ([]byte, map[string][]byte) {
blobs := map[string][]byte{}
put := func(data []byte) []byte {
sum := sha256.Sum256(data)
blobs[hex.EncodeToString(sum[:])] = data
return sum[:]
}
var state protobuf
for _, m := range conv.Messages {
state = state.bytes(1, put(m))
}
mid := randomID()
user := protobuf(nil).text(1, conv.Last).text(2, mid).uint(4, 1)
turn := protobuf(nil).bytes(1, protobuf(nil).bytes(1, put(user)).text(10, mid))
state = state.bytes(8, put(turn)).uint(10, 1).text(22, "cli")
rc := protobuf(nil).bytes(4, cursorEnvironment())
var mcp protobuf
for _, t := range tools {
d := toolDefinition(t)
rc = rc.bytes(7, d)
mcp = mcp.bytes(1, d)
}
action := protobuf(nil).bytes(2, protobuf(nil).bytes(2, rc))
details := protobuf(nil).text(1, model.ID).text(3, model.ID).text(4, model.ID)
requested := protobuf(nil).text(1, model.ID)
if model.Max {
details = details.uint(7, 1)
requested = requested.uint(2, 1)
}
for _, p := range model.Params {
requested = requested.bytes(3, protobuf(nil).text(1, p.ID).text(2, p.Value))
}
run := protobuf(nil).bytes(1, state).bytes(2, action).bytes(3, details).bytes(4, mcp).text(5, id).bytes(9, requested).uint(19, 1)
return protobuf(nil).bytes(1, run), blobs
}
func conversationID(chat chatRequest, req rpcRequest) string {
session := chat.PromptCacheKey
if session == "" {
for _, key := range []string{"canonical_session_id", "execution_session_id", "derived_session_id"} {
if session = str(req.Metadata[key]); session != "" {
break
}
}
}
if session == "" {
return randomID()
}
var first any
for _, m := range chat.Messages {
if str(m["role"]) == "user" {
first = m
break
}
}
sum := sha256.Sum256([]byte("cursor conversation\x00" + req.AuthID + "\x00" + session + "\x00" + string(encodeJSON(first))))
b := sum[:16]
b[6] = (b[6] & 15) | 64
b[8] = (b[8] & 63) | 128
h := hex.EncodeToString(b)
return h[:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:]
}