Repository navigation
Expand file tree
/
Copy pathexecutor.go
More file actions
551 lines (524 loc) · 15.8 KB
/
Copy pathexecutor.go
File metadata and controls
551 lines (524 loc) · 15.8 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// executor.go — ProviderExecutor handlers for the Zen wire.
//
// Execute validates and aggregates one chat completion: the upstream rejects
// non-streaming requests outright, so the non-stream path runs the same SSE
// and folds it into a chat.completion object.
//
// ExecuteStream forwards the upstream OpenAI-shaped SSE. Each data event is
// cleaned (empty shells removed, tool calls translated back into the client's
// spelling) and emitted either synchronously as a chunk list or, when the host
// supplied a stream id, asynchronously via host.stream.emit/close.
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sort"
"strings"
"time"
"github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi"
)
// payloadOf returns the request body the executor should send upstream.
func payloadOf(req pluginapi.ExecutorRequest) []byte {
if len(req.Payload) > 0 {
return req.Payload
}
return req.OriginalRequest
}
// handleExecExecute serves a non-streaming client request by running the
// streaming upstream call and folding the SSE back into one completion.
func handleExecExecute(raw []byte) ([]byte, error) {
var req pluginapi.ExecutorRequest
if err := json.Unmarshal(raw, &req); err != nil {
return nil, fmt.Errorf("parse execute request: %w", err)
}
upstreamModel := resolveModel(req)
body, mapper, err := zenChatBody(payloadOf(req), upstreamModel)
if err != nil {
return nil, err
}
resp, err := zenChatStream(context.Background(), body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
rawErr, _ := io.ReadAll(io.LimitReader(resp.Body, errorBodyLimit))
return nil, fmt.Errorf("upstream http %d: %s", resp.StatusCode, truncateBody(rawErr))
}
completion, err := aggregateCompletion(resp.Body, upstreamModel, mapper)
if err != nil {
return nil, err
}
return okEnvelope(pluginapi.ExecutorResponse{Payload: completion}), nil
}
// handleExecStream serves a streaming client request.
func handleExecStream(raw []byte) ([]byte, error) {
var req executorStreamRequest
if err := json.Unmarshal(raw, &req); err != nil {
return nil, fmt.Errorf("parse stream request: %w", err)
}
upstreamModel := resolveModel(req.ExecutorRequest)
body, mapper, err := zenChatBody(payloadOf(req.ExecutorRequest), upstreamModel)
if err != nil {
return nil, err
}
upstream, err := zenChatStream(context.Background(), body)
if err != nil {
return nil, err
}
if upstream.StatusCode != http.StatusOK {
rawErr, _ := io.ReadAll(io.LimitReader(upstream.Body, errorBodyLimit))
upstream.Body.Close()
return nil, fmt.Errorf("upstream http %d: %s", upstream.StatusCode, truncateBody(rawErr))
}
headers := streamHeaders()
sseFramed := clientNeedsSSEFrame(req.Metadata)
filter := newDownstreamToolFilter(mapper)
// Async: the host created the stream and reads host.stream.emit pushes.
// NOTE: the upstream body is closed INSIDE the pump goroutine, never by a
// defer here — returning the envelope must not race the pump's reads (a
// premature close surfaces to the client as "http2: response body closed"
// or a truncated stream).
if req.StreamID != "" {
sid := req.StreamID
go func() {
errPump := pumpCleanedSSE(upstream.Body, sseFramed, filter, func(p []byte) error {
return streamEmit(sid, p)
})
upstream.Body.Close()
if errPump != nil {
streamEmitError(sid, errPump.Error())
}
_ = streamClose(sid)
}()
return okEnvelope(streamResponse{Headers: headers}), nil
}
// Sync fallback: collect one cleaned chunk per data event.
var chunks []pluginapi.ExecutorStreamChunk
if _, errWalk := walkCleanedSSE(upstream.Body, sseFramed, filter, func(p string) error {
chunks = append(chunks, pluginapi.ExecutorStreamChunk{Payload: []byte(p)})
return nil
}); errWalk != nil {
upstream.Body.Close()
return nil, errWalk
}
upstream.Body.Close()
return okEnvelope(streamResponse{Headers: headers, Chunks: chunks}), nil
}
// resolveModel picks the upstream model id: the host-resolved name (req.Model)
// wins, otherwise the id embedded in the body, in both cases stripped of any
// client-facing provider prefix.
func resolveModel(req pluginapi.ExecutorRequest) string {
if target := normalizeUpstreamModel(req.Model); target != "" {
return target
}
obj, ok := parseObjectBody(payloadOf(req))
if !ok {
return ""
}
name, _ := obj["model"].(string)
return normalizeUpstreamModel(name)
}
// streamHeaders matches the other plugins' response headers: the host streams
// the client response as SSE, so Content-Type must be text/event-stream (never
// the upstream's own headers, which may carry length/framing mismatches).
func streamHeaders() http.Header {
h := http.Header{}
h.Set("Content-Type", "text/event-stream")
h.Set("Cache-Control", "no-cache")
h.Set("X-Accel-Buffering", "no")
return h
}
// clientNeedsSSEFrame reports whether the host wants payloads pre-framed as
// "data: " lines. OpenAI chat/completions requests do not: the host frames
// bare JSON payloads itself and appends its own terminator.
func clientNeedsSSEFrame(metadata map[string]any) bool {
path, _ := metadata["request_path"].(string)
switch strings.ToLower(strings.TrimSpace(path)) {
case "/v1/chat/completions", "/v1/completions":
return false
default:
return true
}
}
// walkCleanedSSE reads an upstream SSE stream and emits one payload per data
// event: bare JSON (or a "data: " line when sseFramed), with keep-alive
// comments, empty shells and the trailing [DONE] dropped. It returns an error
// for a mid-stream read failure or a stream that carried no data event at all.
func walkCleanedSSE(r io.Reader, sseFramed bool, filter *downstreamToolFilter, emit func(string) error) (bool, error) {
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 64*1024), 4*1024*1024)
seenPayload := false
for sc.Scan() {
content, meaningful, _ := upstreamSSEFrame(sc.Text())
seenPayload = seenPayload || meaningful
if content == "" || content == "[DONE]" {
continue
}
cleaned := cleanChunkJSON(content, filter)
if cleaned == "" {
continue
}
if sseFramed {
cleaned = "data: " + cleaned
}
if err := emit(cleaned); err != nil {
return seenPayload, err
}
}
if err := sc.Err(); err != nil {
return seenPayload, err
}
if !seenPayload {
return seenPayload, errors.New("upstream stream ended without a completion payload")
}
return seenPayload, nil
}
// pumpCleanedSSE is walkCleanedSSE for the asynchronous emit signature.
func pumpCleanedSSE(r io.Reader, sseFramed bool, filter *downstreamToolFilter, emit func([]byte) error) error {
_, err := walkCleanedSSE(r, sseFramed, filter, func(p string) error { return emit([]byte(p)) })
return err
}
// upstreamSSEFrame extracts the payload of one upstream SSE line: content is
// the JSON after "data:", meaningful reports whether the line was a data event
// (the empty-stream guard counts only those), and comment/blank/event lines
// carry no payload.
func upstreamSSEFrame(line string) (string, bool, error) {
trimmed := strings.TrimSpace(line)
if trimmed == "" || strings.HasPrefix(trimmed, ":") {
return "", false, nil
}
if strings.HasPrefix(trimmed, "data:") {
return strings.TrimSpace(strings.TrimPrefix(trimmed, "data:")), true, nil
}
return "", false, nil
}
// cleanChunkJSON normalises one upstream chunk:
//
// - tool calls are translated back into the client's spelling, and calls to
// an injected read/bash placeholder are dropped;
// - empty shells are removed so strict clients do not mistake a terminal
// chunk for a truncated tool call. The upstream emits explicit nulls
// (reasoning_content:null, tool_calls:null) which many clients reject;
// - a chunk whose delta is empty and which carries no finish_reason is
// dropped entirely (returns "").
func cleanChunkJSON(s string, filter *downstreamToolFilter) string {
if strings.HasPrefix(s, ":") {
return ""
}
var obj map[string]any
if json.Unmarshal([]byte(s), &obj) != nil {
return s
}
if filter != nil {
filter.filterChunk(obj)
}
changed := false
if choices, ok := obj["choices"].([]any); ok {
for _, c := range choices {
choice, ok := c.(map[string]any)
if !ok {
continue
}
delta, ok := choice["delta"].(map[string]any)
if !ok {
continue
}
for _, noise := range []string{"function_call", "extra_fields", "refusal", "reasoning_content", "tool_calls", "audio"} {
v, present := delta[noise]
if !present {
continue
}
// A null or empty shell carries no information for the client.
if v == nil || isEmptyValue(v) {
delete(delta, noise)
changed = true
}
}
if !deltaHasMeaning(delta) {
if fr, _ := choice["finish_reason"].(string); fr == "" {
return ""
}
}
}
}
if !changed {
return s
}
out, err := json.Marshal(obj)
if err != nil {
return s
}
return string(out)
}
// deltaHasMeaning reports whether a delta still carries something a client can
// act on after the noise fields were removed. A delta that only ever held
// nulls/empty shells (the upstream's opening "role" frame keeps its role, but a
// content-only empty frame does not) is not worth forwarding.
func deltaHasMeaning(delta map[string]any) bool {
for _, key := range []string{"role", "content", "reasoning_content", "tool_calls", "function_call"} {
v, present := delta[key]
if !present {
continue
}
if key == "role" {
if s, ok := v.(string); ok && s != "" {
return true
}
continue
}
if !isEmptyValue(v) {
return true
}
}
return false
}
func isEmptyValue(v any) bool {
switch t := v.(type) {
case nil:
return true
case string:
return t == ""
case []any:
return len(t) == 0
case map[string]any:
return len(t) == 0
case bool:
return !t
case float64:
return t == 0
}
return false
}
// -----------------------------------------------------------------------------
// Non-stream aggregation
// -----------------------------------------------------------------------------
// toolCallAccum collects one streamed tool call: the upstream sends the name
// once and then streams the JSON arguments in fragments.
type toolCallAccum struct {
ID string
Type string
Name string
Arguments strings.Builder
}
// aggregateCompletion folds an OpenAI-shaped SSE into a single chat.completion
// object for the non-stream executor path, applying the same tool-call
// translation as the streaming path.
func aggregateCompletion(r io.Reader, model string, mapper *toolMapper) ([]byte, error) {
filter := newDownstreamToolFilter(mapper)
var (
id, respModel string
created int64
content, reason, role strings.Builder
finish string
calls = map[int]*toolCallAccum{}
order []int
usage map[string]any
)
sc := bufio.NewScanner(r)
sc.Buffer(make([]byte, 0, 256*1024), 8*1024*1024)
for sc.Scan() {
line := sc.Text()
if !strings.HasPrefix(strings.TrimSpace(line), "data:") {
continue
}
payload := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "data:"))
if payload == "" || payload == "[DONE]" {
continue
}
var obj map[string]any
if json.Unmarshal([]byte(payload), &obj) != nil {
continue
}
if v, _ := obj["id"].(string); v != "" {
id = v
}
if v, _ := obj["model"].(string); v != "" {
respModel = v
}
if v, ok := obj["created"].(float64); ok && v > 0 {
created = int64(v)
}
if u, ok := obj["usage"].(map[string]any); ok && len(u) > 0 {
usage = u
}
// Apply the tool-call filter so dropped placeholder calls never reach
// the aggregated message, and surviving names use the client's spelling.
filter.filterChunk(obj)
choices, _ := obj["choices"].([]any)
for _, c := range choices {
choice, ok := c.(map[string]any)
if !ok {
continue
}
if fr, _ := choice["finish_reason"].(string); fr != "" {
finish = fr
}
delta, ok := choice["delta"].(map[string]any)
if !ok {
continue
}
// The upstream repeats "role":"assistant" on every frame, so this
// must keep the first non-empty value — appending turns nine
// frames into "assistantassistantassistant…".
if v, _ := delta["role"].(string); v != "" && role.Len() == 0 {
role.WriteString(v)
}
if v, _ := delta["content"].(string); v != "" {
content.WriteString(v)
}
if v, _ := delta["reasoning_content"].(string); v != "" {
reason.WriteString(v)
}
collectToolCalls(delta["tool_calls"], calls, &order)
}
}
if err := sc.Err(); err != nil {
return nil, fmt.Errorf("read stream: %w", err)
}
if id == "" {
id = "chatcmpl-" + randomSuffix()
}
if respModel == "" {
respModel = model
}
if created == 0 {
created = time.Now().Unix()
}
if role.Len() == 0 {
role.WriteString("assistant")
}
if finish == "" {
finish = "stop"
}
message := map[string]any{"role": role.String(), "content": content.String()}
if reason.Len() > 0 {
message["reasoning_content"] = reason.String()
}
if len(order) > 0 {
sort.Ints(order)
out := make([]any, 0, len(order))
for _, idx := range order {
acc := calls[idx]
if acc == nil || acc.Name == "" {
continue
}
out = append(out, map[string]any{
"id": acc.ID,
"type": "function",
"function": map[string]any{
"name": acc.Name,
"arguments": acc.Arguments.String(),
},
})
}
if len(out) > 0 {
message["tool_calls"] = out
} else if finish == "tool_calls" {
// Every call targeted an injected placeholder: the client never
// asked for tools, so report a plain stop.
finish = "stop"
}
}
body := map[string]any{
"id": id,
"object": "chat.completion",
"created": created,
"model": respModel,
"choices": []any{map[string]any{
"index": 0,
"message": message,
"finish_reason": finish,
}},
}
if usage != nil {
body["usage"] = normalizeUsage(usage)
}
return json.Marshal(body)
}
// collectToolCalls merges one chunk's tool_calls array into the accumulator,
// preserving first-seen order.
func collectToolCalls(raw any, calls map[int]*toolCallAccum, order *[]int) {
arr, ok := raw.([]any)
if !ok {
return
}
for _, item := range arr {
call, ok := item.(map[string]any)
if !ok {
continue
}
idx := toolCallIndex(call)
acc := calls[idx]
if acc == nil {
acc = &toolCallAccum{Type: "function"}
calls[idx] = acc
*order = append(*order, idx)
}
if v, _ := call["id"].(string); v != "" {
acc.ID = v
}
if v, _ := call["type"].(string); v != "" {
acc.Type = v
}
fn, _ := call["function"].(map[string]any)
if fn == nil {
continue
}
if v, _ := fn["name"].(string); v != "" {
acc.Name = v
}
if v, _ := fn["arguments"].(string); v != "" {
acc.Arguments.WriteString(v)
}
}
}
// normalizeUsage keeps the usage object OpenAI-shaped, deriving
// completion_tokens when the upstream only sent a total.
func normalizeUsage(usage map[string]any) map[string]any {
prompt := intOf(usage["prompt_tokens"])
total := intOf(usage["total_tokens"])
completion := intOf(usage["completion_tokens"])
if completion == 0 && total > 0 && total >= prompt {
completion = total - prompt
}
if total == 0 {
total = prompt + completion
}
out := map[string]any{
"prompt_tokens": prompt,
"completion_tokens": completion,
"total_tokens": total,
}
// Preserve the upstream's detail blocks (cached_tokens / reasoning_tokens)
// so clients that display them keep working.
for _, key := range []string{"prompt_tokens_details", "completion_tokens_details"} {
if v, ok := usage[key]; ok {
out[key] = v
}
}
return out
}
// intOf reads a JSON number as an int.
func intOf(v any) int {
if f, ok := v.(float64); ok {
return int(f)
}
return 0
}
// randomSuffix makes a non-empty opaque completion id without crypto deps.
func randomSuffix() string {
var b bytes.Buffer
now := time.Now().UnixNano()
for i := 0; i < 8; i++ {
b.WriteByte("abcdef0123456789"[now&0xf])
now >>= 4
}
return b.String()
}