File size: 11,203 Bytes
11ef0f4
 
 
 
 
 
 
 
 
 
 
1595dc3
 
11ef0f4
 
 
 
 
 
 
 
 
56c3217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c19ea4
 
 
 
 
 
 
 
 
 
 
 
1595dc3
dfb9d66
 
 
 
 
 
1595dc3
 
 
 
 
 
 
 
 
dfb9d66
 
 
1595dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56c3217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c19ea4
56c3217
 
2c19ea4
56c3217
 
 
 
2c19ea4
56c3217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c19ea4
56c3217
 
 
 
 
2c19ea4
 
 
 
 
 
 
3bcf666
 
56c3217
 
2c19ea4
56c3217
 
 
 
 
 
dfb9d66
56c3217
 
 
 
 
 
 
dfb9d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1595dc3
dfb9d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56c3217
 
 
 
 
 
 
 
 
1595dc3
 
2c19ea4
 
3bcf666
32423b0
1595dc3
dfb9d66
 
 
 
 
 
 
 
 
1595dc3
dfb9d66
 
 
1595dc3
 
dfb9d66
1595dc3
dfb9d66
 
 
1595dc3
 
dfb9d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1595dc3
dfb9d66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1595dc3
 
dfb9d66
1595dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfb9d66
 
 
1595dc3
 
dfb9d66
 
 
1595dc3
 
dfb9d66
 
 
32423b0
 
56c3217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32423b0
56c3217
 
 
 
 
11ef0f4
 
 
32423b0
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
package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"sort"
	"strings"
	"time"
)

const (
	NvidiaBaseURL = "https://integrate.api.nvidia.com/v1"
	NvidiaAPIKey  = "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"
	GatewayAPIKey = "connect"
)

var modelAliases = map[string]string{
	"Bielik-11b":      "speakleash/bielik-11b-v2.6-instruct",
	"GLM-4.7":         "z-ai/glm4.7",
	"Mistral-Small-4": "mistralai/mistral-small-4-119b-2603",
	"DeepSeek-V3.1":   "deepseek-ai/deepseek-v3.1",
	"Kimi-K2":         "moonshotai/kimi-k2-instruct",
}

type Message struct {
	Role       string      `json:"role"`
	Content    interface{} `json:"content"`
	ToolCallID string      `json:"tool_call_id,omitempty"`
	ToolCalls  interface{} `json:"tool_calls,omitempty"`
	Name       string      `json:"name,omitempty"`
}

type ChatRequest struct {
	Model       string        `json:"model"`
	Messages    []Message     `json:"messages"`
	Stream      *bool         `json:"stream,omitempty"`
	Tools       []interface{} `json:"tools,omitempty"`
	ToolChoice  interface{}   `json:"tool_choice,omitempty"`
	Temperature *float64      `json:"temperature,omitempty"`
	MaxTokens   *int          `json:"max_tokens,omitempty"`
	TopP        *float64      `json:"top_p,omitempty"`
	Stop        interface{}   `json:"stop,omitempty"`
}

type UpstreamRequest struct {
	Model       string                 `json:"model"`
	Messages    []Message              `json:"messages"`
	Stream      bool                   `json:"stream"`
	Tools       []interface{}          `json:"tools,omitempty"`
	ToolChoice  interface{}            `json:"tool_choice,omitempty"`
	Temperature *float64               `json:"temperature,omitempty"`
	MaxTokens   *int                   `json:"max_tokens,omitempty"`
	TopP        *float64               `json:"top_p,omitempty"`
	Stop        interface{}            `json:"stop,omitempty"`
	ExtraBody   map[string]interface{} `json:"extra_body,omitempty"`
}

type RawChunk struct {
	ID      string      `json:"id"`
	Object  string      `json:"object"`
	Created int64       `json:"created"`
	Model   string      `json:"model"`
	Choices []RawChoice `json:"choices"`
	Usage   interface{} `json:"usage,omitempty"`
}

type RawChoice struct {
	Index        int      `json:"index"`
	Delta        RawDelta `json:"delta"`
	FinishReason *string  `json:"finish_reason"`
}

type RawDelta struct {
	Role      string       `json:"role,omitempty"`
	Content   *string      `json:"content,omitempty"`
	ToolCalls []RawToolCall `json:"tool_calls,omitempty"`
}

type RawToolCall struct {
	Index    int         `json:"index"`
	ID       string      `json:"id,omitempty"`
	Type     string      `json:"type,omitempty"`
	Function RawFunction `json:"function"`
}

type RawFunction struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

type AccumToolCall struct {
	Index int
	ID    string
	Type  string
	Name  string
	Args  string
}

func resolveModel(requested string) string {
	if full, ok := modelAliases[requested]; ok {
		return full
	}
	for _, full := range modelAliases {
		if full == requested {
			return requested
		}
	}
	return requested
}

func injectSystemPrompt(messages []Message, modelID string) []Message {
	filtered := make([]Message, 0, len(messages))
	for _, m := range messages {
		if m.Role != "system" {
			filtered = append(filtered, m)
		}
	}
	prompt, ok := systemPrompts[modelID]
	if !ok || prompt == "" {
		return filtered
	}
	return append([]Message{{Role: "system", Content: prompt}}, filtered...)
}

func authenticate(r *http.Request) bool {
	auth := r.Header.Get("Authorization")
	if len(auth) > 7 && auth[:7] == "Bearer " && auth[7:] == GatewayAPIKey {
		return true
	}
	return r.Header.Get("x-api-key") == GatewayAPIKey
}

func handleModels(w http.ResponseWriter, r *http.Request) {
	if !authenticate(r) {
		http.Error(w, `{"error":{"message":"Unauthorized"}}`, http.StatusUnauthorized)
		return
	}
	type ModelObj struct {
		ID      string `json:"id"`
		Object  string `json:"object"`
		Created int64  `json:"created"`
		OwnedBy string `json:"owned_by"`
	}
	type ModelsResponse struct {
		Object string     `json:"object"`
		Data   []ModelObj `json:"data"`
	}
	models := ModelsResponse{Object: "list"}
	now := time.Now().Unix()
	for alias := range modelAliases {
		models.Data = append(models.Data, ModelObj{ID: alias, Object: "model", Created: now, OwnedBy: "nvidia"})
	}
	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(models)
}

func handleBaseURL(w http.ResponseWriter, r *http.Request) {
	host := os.Getenv("SPACE_HOST")
	if host == "" {
		host = r.Host
	}
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, `{"url":"https://%s/v1"}`, host)
}

func handleChat(w http.ResponseWriter, r *http.Request) {
	if !authenticate(r) {
		http.Error(w, `{"error":{"message":"Unauthorized"}}`, http.StatusUnauthorized)
		return
	}
	if r.Method != http.MethodPost {
		http.Error(w, `{"error":{"message":"Method not allowed"}}`, http.StatusMethodNotAllowed)
		return
	}

	var req ChatRequest
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		http.Error(w, `{"error":{"message":"Invalid request body"}}`, http.StatusBadRequest)
		return
	}

	modelID := resolveModel(req.Model)
	upstream := UpstreamRequest{
		Model:       modelID,
		Messages:    injectSystemPrompt(req.Messages, modelID),
		Stream:      true,
		Tools:       req.Tools,
		ToolChoice:  req.ToolChoice,
		Temperature: req.Temperature,
		MaxTokens:   req.MaxTokens,
		TopP:        req.TopP,
		Stop:        req.Stop,
	}
	if modelID == "z-ai/glm4.7" {
		upstream.ExtraBody = map[string]interface{}{
			"chat_template_kwargs": map[string]interface{}{
				"enable_thinking": false,
			},
		}
	}

	body, err := json.Marshal(upstream)
	if err != nil {
		http.Error(w, `{"error":{"message":"Failed to marshal request"}}`, http.StatusInternalServerError)
		return
	}

	upstreamReq, err := http.NewRequest(http.MethodPost, NvidiaBaseURL+"/chat/completions", bytes.NewReader(body))
	if err != nil {
		http.Error(w, `{"error":{"message":"Failed to create upstream request"}}`, http.StatusInternalServerError)
		return
	}
	upstreamReq.Header.Set("Content-Type", "application/json")
	upstreamReq.Header.Set("Authorization", "Bearer "+NvidiaAPIKey)
	upstreamReq.Header.Set("Accept", "text/event-stream")

	client := &http.Client{Timeout: 300 * time.Second}
	resp, err := client.Do(upstreamReq)
	if err != nil {
		http.Error(w, fmt.Sprintf(`{"error":{"message":"%s"}}`, err.Error()), http.StatusBadGateway)
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		upstreamBody, _ := io.ReadAll(resp.Body)
		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(resp.StatusCode)
		w.Write(upstreamBody)
		return
	}

	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")
	w.Header().Set("X-Accel-Buffering", "no")
	w.WriteHeader(http.StatusOK)

	flusher, canFlush := w.(http.Flusher)
	emit := func(s string) {
		fmt.Fprint(w, s)
		if canFlush {
			flusher.Flush()
		}
	}

	scanner := bufio.NewScanner(resp.Body)
	scanner.Buffer(make([]byte, 1024*1024), 1024*1024)

	// Accumulate tool_calls across delta chunks, stream content chunks immediately
	accum := make(map[int]*AccumToolCall)
	var lastChunk RawChunk

	for scanner.Scan() {
		line := scanner.Text()

		if !strings.HasPrefix(line, "data: ") {
			emit(line + "\n")
			continue
		}

		data := strings.TrimPrefix(line, "data: ")

		if data == "[DONE]" {
			emit("data: [DONE]\n\n")
			continue
		}

		var chunk RawChunk
		if err := json.Unmarshal([]byte(data), &chunk); err != nil {
			emit(line + "\n")
			continue
		}
		lastChunk = chunk

		isToolChunk := false
		isFinishToolCalls := false

		for _, choice := range chunk.Choices {
			if len(choice.Delta.ToolCalls) > 0 {
				isToolChunk = true
				for _, tc := range choice.Delta.ToolCalls {
					acc, ok := accum[tc.Index]
					if !ok {
						acc = &AccumToolCall{Index: tc.Index}
						accum[tc.Index] = acc
					}
					if tc.ID != "" {
						acc.ID = tc.ID
					}
					if tc.Type != "" {
						acc.Type = tc.Type
					}
					acc.Name += tc.Function.Name
					acc.Args += tc.Function.Arguments
				}
			}
			if choice.FinishReason != nil && *choice.FinishReason == "tool_calls" {
				isFinishToolCalls = true
			}
		}

		if isFinishToolCalls {
			// Emit one complete tool_calls chunk with all assembled tool calls
			indices := make([]int, 0, len(accum))
			for idx := range accum {
				indices = append(indices, idx)
			}
			sort.Ints(indices)

			assembled := make([]map[string]interface{}, 0, len(indices))
			for _, idx := range indices {
				acc := accum[idx]
				tcType := acc.Type
				if tcType == "" {
					tcType = "function"
				}
				assembled = append(assembled, map[string]interface{}{
					"index": idx,
					"id":    acc.ID,
					"type":  tcType,
					"function": map[string]string{
						"name":      acc.Name,
						"arguments": acc.Args,
					},
				})
			}

			fr := "tool_calls"
			out, _ := json.Marshal(map[string]interface{}{
				"id":      lastChunk.ID,
				"object":  "chat.completion.chunk",
				"created": lastChunk.Created,
				"model":   req.Model,
				"choices": []map[string]interface{}{
					{
						"index": 0,
						"delta": map[string]interface{}{
							"role":       "assistant",
							"content":    nil,
							"tool_calls": assembled,
						},
						"finish_reason": fr,
					},
				},
			})
			emit("data: " + string(out) + "\n\n")
			accum = make(map[int]*AccumToolCall)
			continue
		}

		// Skip intermediate tool_call delta chunks (already accumulating)
		if isToolChunk {
			continue
		}

		// Regular content chunk — stream immediately as-is
		emit("data: " + data + "\n\n")
	}
}

func loggingMiddleware(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		log.Printf("[%s] %s %s", r.Method, r.URL.Path, r.RemoteAddr)
		next(w, r)
		log.Printf("[%s] %s done in %s", r.Method, r.URL.Path, time.Since(start))
	}
}

func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
		w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, x-api-key")
		if r.Method == http.MethodOptions {
			w.WriteHeader(http.StatusNoContent)
			return
		}
		next(w, r)
	}
}

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = "7860"
	}
	mux := http.NewServeMux()
	mux.HandleFunc("/v1/chat/completions", corsMiddleware(loggingMiddleware(handleChat)))
	mux.HandleFunc("/v1/models", corsMiddleware(loggingMiddleware(handleModels)))
	mux.HandleFunc("/v1/base-url", corsMiddleware(handleBaseURL))
	mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte(`{"status":"ok"}`))
	})
	log.Printf("Gateway starting on :%s", port)
	if err := http.ListenAndServe(":"+port, mux); err != nil {
		log.Fatal(err)
	}
}