-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathdesktop.go
More file actions
1447 lines (1253 loc) · 44.2 KB
/
desktop.go
File metadata and controls
1447 lines (1253 loc) · 44.2 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package desktop
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
"github.com/docker/model-runner/pkg/distribution/distribution"
"github.com/docker/model-runner/pkg/inference"
dmrm "github.com/docker/model-runner/pkg/inference/models"
"github.com/docker/model-runner/pkg/inference/scheduling"
"github.com/fatih/color"
"github.com/pkg/errors"
"go.opentelemetry.io/otel"
)
const (
// maxToolCallIterations caps the number of agentic tool-call rounds to
// prevent infinite loops when a model repeatedly requests tool calls.
maxToolCallIterations = 10
)
var (
ErrNotFound = errors.New("model not found")
ErrServiceUnavailable = errors.New("service unavailable")
)
// ClientTool is a tool that can be registered with the chat client.
type ClientTool interface {
Name() string
Schema() Tool
Execute(ctx context.Context, args map[string]any) (string, error)
}
type otelErrorSilencer struct{}
func (oes *otelErrorSilencer) Handle(error) {}
func init() {
otel.SetErrorHandler(&otelErrorSilencer{})
}
type Client struct {
modelRunner *ModelRunnerContext
}
//go:generate mockgen -source=desktop.go -destination=../mocks/mock_desktop.go -package=mocks DockerHttpClient
type DockerHttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
func New(modelRunner *ModelRunnerContext) *Client {
return &Client{modelRunner}
}
type Status struct {
Running bool `json:"running"`
Status []byte `json:"status"`
Error error `json:"error"`
}
func (c *Client) Status() Status {
// TODO: Query "/".
resp, err := c.doRequest(http.MethodGet, inference.ModelsPrefix, nil)
if err != nil {
err = c.handleQueryError(err, inference.ModelsPrefix)
if errors.Is(err, ErrServiceUnavailable) {
return Status{
Running: false,
}
}
return Status{
Running: false,
Error: err,
}
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
var status []byte
statusResp, err := c.doRequest(http.MethodGet, inference.InferencePrefix+"/status", nil)
if err != nil {
status = []byte(fmt.Sprintf("error querying status: %v", err))
} else {
defer statusResp.Body.Close()
statusBody, err := io.ReadAll(statusResp.Body)
if err != nil {
status = []byte(fmt.Sprintf("error reading status body: %v", err))
} else {
status = statusBody
}
}
return Status{
Running: true,
Status: status,
}
}
return Status{
Running: false,
Error: fmt.Errorf("unexpected status code: %d", resp.StatusCode),
}
}
func (c *Client) Pull(model string, printer standalone.StatusPrinter) (string, bool, error) {
// Check if this is a Hugging Face model and if HF_TOKEN is set
var hfToken string
if distribution.IsHuggingFaceReference(strings.ToLower(model)) {
hfToken = os.Getenv("HF_TOKEN")
}
return c.withRetries("download", 3, printer, func(attempt int) (string, bool, error, bool) {
jsonData, err := json.Marshal(dmrm.ModelCreateRequest{
From: model,
BearerToken: hfToken,
})
if err != nil {
// Marshaling errors are not retryable
return "", false, fmt.Errorf("error marshaling request: %w", err), false
}
createPath := inference.ModelsPrefix + "/create"
resp, err := c.doRequest(
http.MethodPost,
createPath,
bytes.NewReader(jsonData),
)
if err != nil {
// Only retry on network errors, not on client errors
if isRetryableError(err) {
return "", false, c.handleQueryError(err, createPath), true
}
return "", false, c.handleQueryError(err, createPath), false
}
// Close response body explicitly at the end of this attempt, not deferred
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, readErr := io.ReadAll(resp.Body)
var bodyStr string
if readErr != nil {
bodyStr = fmt.Sprintf("failed to read response body: %v", readErr)
} else {
bodyStr = strings.TrimSpace(string(body))
}
var err error
if resp.StatusCode == http.StatusUnprocessableEntity {
// 422 means the model uses a config type this client does not
// support. Reattach the sentinel so callers can use errors.Is.
err = fmt.Errorf("pulling %s failed with status %s: %w: %s",
model, resp.Status, distribution.ErrUnsupportedMediaType, bodyStr)
} else {
err = fmt.Errorf("pulling %s failed with status %s: %s",
model, resp.Status, bodyStr)
}
// Only retry on gateway/proxy errors (502, 503, 504).
// Do not retry 500 (usually deterministic server errors) or
// 4xx (client errors including 422 for unsupported media type).
shouldRetry := resp.StatusCode == http.StatusBadGateway ||
resp.StatusCode == http.StatusServiceUnavailable ||
resp.StatusCode == http.StatusGatewayTimeout
return "", false, err, shouldRetry
}
// Use Docker-style progress display
message, shown, err := DisplayProgress(resp.Body, printer)
if err != nil {
// Retry on progress display errors (likely network interruption)
shouldRetry := isRetryableError(err)
return "", shown, err, shouldRetry
}
return message, shown, nil, false
})
}
// isRetryableError determines if an error is retryable (network-related)
func isRetryableError(err error) bool {
if err == nil {
return false
}
// First check for specific error types using errors.Is
if errors.Is(err, context.DeadlineExceeded) ||
errors.Is(err, io.ErrUnexpectedEOF) ||
errors.Is(err, io.EOF) ||
errors.Is(err, ErrServiceUnavailable) {
return true
}
// Fall back to string matching for network errors that don't have specific types
// This is necessary because many network errors are only available as strings
errStr := err.Error()
retryablePatterns := []string{
"connection refused",
"connection reset",
"broken pipe",
"timeout",
"temporary failure",
"no such host",
"no route to host",
"network is unreachable",
"i/o timeout",
"stream error",
"internal_error",
"protocol_error",
}
for _, pattern := range retryablePatterns {
if strings.Contains(strings.ToLower(errStr), pattern) {
return true
}
}
return false
}
// withRetries executes an operation with automatic retry logic for transient failures
func (c *Client) withRetries(
operationName string,
maxRetries int,
printer standalone.StatusPrinter,
operation func(attempt int) (message string, progressShown bool, err error, shouldRetry bool),
) (string, bool, error) {
var lastErr error
var progressShown bool
for attempt := 0; attempt <= maxRetries; attempt++ {
if attempt > 0 {
// Calculate exponential backoff: 2^(attempt-1) seconds (1s, 2s, 4s)
backoffDuration := time.Duration(1<<uint(attempt-1)) * time.Second
printer.PrintErrf("Retrying %s (attempt %d/%d) in %v...\n", operationName, attempt, maxRetries, backoffDuration)
time.Sleep(backoffDuration)
}
message, shown, err, shouldRetry := operation(attempt)
progressShown = progressShown || shown
if err == nil {
return message, progressShown, nil
}
lastErr = err
if !shouldRetry {
return "", progressShown, err
}
}
return "", progressShown, fmt.Errorf("%s failed after %d retries: %w", operationName, maxRetries, lastErr)
}
func (c *Client) Push(model string, printer standalone.StatusPrinter) (string, bool, error) {
var hfToken string
if distribution.IsHuggingFaceReference(strings.ToLower(model)) {
hfToken = os.Getenv("HF_TOKEN")
}
return c.withRetries("push", 3, printer, func(attempt int) (string, bool, error, bool) {
pushPath := inference.ModelsPrefix + "/" + model + "/push"
var body io.Reader
if hfToken != "" {
jsonData, err := json.Marshal(dmrm.ModelPushRequest{
BearerToken: hfToken,
})
if err != nil {
return "", false, fmt.Errorf("error marshaling request: %w", err), false
}
body = bytes.NewReader(jsonData)
}
resp, err := c.doRequest(
http.MethodPost,
pushPath,
body,
)
if err != nil {
// Only retry on network errors, not on client errors
if isRetryableError(err) {
return "", false, c.handleQueryError(err, pushPath), true
}
return "", false, c.handleQueryError(err, pushPath), false
}
// Close response body explicitly at the end of this attempt, not deferred
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, readErr := io.ReadAll(resp.Body)
var bodyStr string
if readErr != nil {
bodyStr = fmt.Sprintf("(failed to read response body: %v)", readErr)
} else {
bodyStr = strings.TrimSpace(string(body))
}
err := fmt.Errorf("pushing %s failed with status %s: %s", model, resp.Status, bodyStr)
// Only retry on gateway/proxy errors. Do not retry plain 500
// (usually deterministic server errors) or 4xx (client errors).
shouldRetry := resp.StatusCode == http.StatusBadGateway ||
resp.StatusCode == http.StatusServiceUnavailable ||
resp.StatusCode == http.StatusGatewayTimeout
return "", false, err, shouldRetry
}
// Use Docker-style progress display
message, shown, err := DisplayProgress(resp.Body, printer)
if err != nil {
// Retry on progress display errors (likely network interruption)
shouldRetry := isRetryableError(err)
return "", shown, err, shouldRetry
}
return message, shown, nil, false
})
}
func (c *Client) List() ([]dmrm.Model, error) {
modelsRoute := inference.ModelsPrefix
body, err := c.listRaw(modelsRoute, "")
if err != nil {
return []dmrm.Model{}, err
}
var modelsJson []dmrm.Model
if err := json.Unmarshal(body, &modelsJson); err != nil {
return modelsJson, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return modelsJson, nil
}
func (c *Client) ListOpenAI() (dmrm.OpenAIModelList, error) {
modelsRoute := c.modelRunner.OpenAIPathPrefix() + "/models"
body, err := c.listRaw(modelsRoute, "")
if err != nil {
return dmrm.OpenAIModelList{}, err
}
var modelsJson dmrm.OpenAIModelList
if err := json.Unmarshal(body, &modelsJson); err != nil {
return modelsJson, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return modelsJson, nil
}
func (c *Client) Inspect(model string, remote bool) (dmrm.Model, error) {
rawResponse, err := c.listRawWithQuery(fmt.Sprintf("%s/%s", inference.ModelsPrefix, model), model, remote)
if err != nil {
return dmrm.Model{}, err
}
var modelInspect dmrm.Model
if err := json.Unmarshal(rawResponse, &modelInspect); err != nil {
return modelInspect, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return modelInspect, nil
}
func (c *Client) ResolveModelBackend(model string) (scheduling.ModelBackendSelection, error) {
resolvePath := fmt.Sprintf("%s/backend?model=%s", inference.ModelsPrefix, url.QueryEscape(model))
resp, err := c.doRequest(http.MethodGet, resolvePath, nil)
if err != nil {
return scheduling.ModelBackendSelection{}, c.handleQueryError(err, resolvePath)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return scheduling.ModelBackendSelection{}, errors.Wrap(ErrNotFound, model)
}
body, _ := io.ReadAll(resp.Body)
return scheduling.ModelBackendSelection{}, fmt.Errorf("failed to resolve model backend: %s: %s", resp.Status, string(body))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return scheduling.ModelBackendSelection{}, fmt.Errorf("failed to read response body: %w", err)
}
var selection scheduling.ModelBackendSelection
if err := json.Unmarshal(body, &selection); err != nil {
return scheduling.ModelBackendSelection{}, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return selection, nil
}
func (c *Client) InspectOpenAI(model string) (dmrm.OpenAIModel, error) {
modelsRoute := c.modelRunner.OpenAIPathPrefix() + "/models"
rawResponse, err := c.listRaw(fmt.Sprintf("%s/%s", modelsRoute, model), model)
if err != nil {
return dmrm.OpenAIModel{}, err
}
var modelInspect dmrm.OpenAIModel
if err := json.Unmarshal(rawResponse, &modelInspect); err != nil {
return modelInspect, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return modelInspect, nil
}
func (c *Client) listRaw(route string, model string) ([]byte, error) {
return c.listRawWithQuery(route, model, false)
}
func (c *Client) listRawWithQuery(route string, model string, remote bool) ([]byte, error) {
if remote {
route += "?remote=true"
}
resp, err := c.doRequest(http.MethodGet, route, nil)
if err != nil {
return nil, c.handleQueryError(err, route)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
if model != "" && resp.StatusCode == http.StatusNotFound {
return nil, errors.Wrap(ErrNotFound, model)
}
return nil, fmt.Errorf("failed to list models: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
return body, nil
}
// Chat performs a chat request and streams the response content with selective markdown rendering.
func (c *Client) Chat(model, prompt string, imageURLs []string, outputFunc func(string), shouldUseMarkdown bool) error {
return c.ChatWithContext(context.Background(), model, prompt, imageURLs, outputFunc, shouldUseMarkdown)
}
// accumulatedToolCall collects streamed tool call fragments into a complete call.
type accumulatedToolCall struct {
id string
name string
arguments strings.Builder
}
// Preload loads a model into memory without running inference.
// The model stays loaded for the idle timeout period.
func (c *Client) Preload(ctx context.Context, model string) error {
reqBody := OpenAIChatRequest{
Model: model,
Messages: []OpenAIChatMessage{},
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return fmt.Errorf("error marshaling request: %w", err)
}
completionsPath := c.modelRunner.OpenAIPathPrefix() + "/chat/completions"
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.modelRunner.URL(completionsPath), bytes.NewReader(jsonData))
if err != nil {
return fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "docker-model-cli/"+Version)
req.Header.Set("X-Preload-Only", "true")
resp, err := c.modelRunner.Client().Do(req)
if err != nil {
return c.handleQueryError(err, completionsPath)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("preload failed with status %d and could not read response body: %w", resp.StatusCode, err)
}
return fmt.Errorf("preload failed: status=%d body=%s", resp.StatusCode, body)
}
return nil
}
// ChatWithMessagesContext performs a chat request with conversation history and returns the assistant's response.
// This allows maintaining conversation context across multiple exchanges.
// When tools are provided, the function implements an agentic loop: if the model requests a tool call,
// the tool is executed and the result is sent back until the model produces a final response.
func (c *Client) ChatWithMessagesContext(ctx context.Context, model string, conversationHistory []OpenAIChatMessage, prompt string, imageURLs []string, outputFunc func(string), shouldUseMarkdown bool, tools ...ClientTool) (string, error) {
// Build the current user message content - either simple string or multimodal array
var messageContent interface{}
if len(imageURLs) > 0 {
// Multimodal message with images
contentParts := make([]ContentPart, 0, len(imageURLs)+1)
// Add all images first
for _, imageURL := range imageURLs {
contentParts = append(contentParts, ContentPart{
Type: "image_url",
ImageURL: &ImageURL{
URL: imageURL,
},
})
}
// Add text prompt if present
if prompt != "" {
contentParts = append(contentParts, ContentPart{
Type: "text",
Text: prompt,
})
}
messageContent = contentParts
} else {
// Simple text-only message
messageContent = prompt
}
// Build messages array with conversation history plus current message
messages := make([]OpenAIChatMessage, 0, len(conversationHistory)+1)
messages = append(messages, conversationHistory...)
messages = append(messages, OpenAIChatMessage{
Role: "user",
Content: messageContent,
})
// initialMessages captures the messages before any tool calls so we can
// fall back to them if the model's chat template doesn't support tool roles.
initialMessages := messages
// Build tool schemas and lookup map
var toolSchemas []Tool
toolMap := make(map[string]ClientTool, len(tools))
for _, t := range tools {
toolSchemas = append(toolSchemas, t.Schema())
toolMap[t.Name()] = t
}
// toolsSupported is cleared if the model returns a Jinja template error,
// indicating its chat template doesn't support tool calling.
toolsSupported := len(toolSchemas) > 0
type chatPrinterState int
const (
chatPrinterNone chatPrinterState = iota
chatPrinterReasoning
chatPrinterContent
)
reasoningFmt := color.New().Add(color.Italic)
if !shouldUseMarkdown {
reasoningFmt.DisableColor()
}
var finalUsage *struct {
CompletionTokens int `json:"completion_tokens"`
PromptTokens int `json:"prompt_tokens"`
TotalTokens int `json:"total_tokens"`
}
completionsPath := c.modelRunner.OpenAIPathPrefix() + "/chat/completions"
var assistantResponse strings.Builder
// Agentic loop: iterate until the model produces a stop response (no more tool calls).
// toolCallIterations counts rounds where the model requested tool calls; it is capped
// at maxToolCallIterations to prevent infinite loops with poorly-behaved models.
toolCallIterations := 0
for {
reqBody := OpenAIChatRequest{
Model: model,
Messages: messages,
Stream: true,
Tools: toolSchemas,
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
return assistantResponse.String(), fmt.Errorf("error marshaling request: %w", err)
}
resp, err := c.doRequestWithAuthContext(
ctx,
http.MethodPost,
completionsPath,
bytes.NewReader(jsonData),
)
if err != nil {
return assistantResponse.String(), c.handleQueryError(err, completionsPath)
}
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
// If the model doesn't support tool calling (e.g., its chat template throws a
// Jinja exception when tools are present), retry the request without tools.
// Only do this before any tool calls have been executed to avoid corrupting
// the message history.
// If the model's chat template doesn't support tool calling (Jinja exception),
// fall back to retrying with the original messages and no tools.
// This handles both cases:
// - Error before any tool calls: the tools parameter in the request itself
// breaks the template (e.g. injects an incompatible system message).
// - Error after tool calls: the tool/assistant(tool_calls) messages in the
// history use roles the template doesn't understand.
// In both cases we reset to the initial user messages and disable tools so the
// model can answer from its training data.
//
// Note: This detection relies on string matching because the model runner does
// not provide a structured error code for template incompatibility. The check
// looks for "Jinja" (the templating engine used by many models) or
// "template" in the error body. If this proves too brittle in practice,
// consider adding a specific error code or flag to the model runner API.
if toolsSupported && isTemplateIncompatibleError(body) {
toolSchemas = nil
toolMap = nil
toolsSupported = false
messages = initialMessages
assistantResponse.Reset()
continue
}
return assistantResponse.String(), fmt.Errorf("error response: status=%d body=%s", resp.StatusCode, body)
}
printerState := chatPrinterNone
// Accumulated tool calls for this iteration, keyed by index.
pendingToolCalls := make(map[int]*accumulatedToolCall)
var finishReason string
// Use a buffered reader so we can consume server-sent progress
// lines (e.g. "Installing vllm-metal backend...") that arrive
// before the actual SSE or JSON inference response.
br := bufio.NewReader(resp.Body)
// Consume any plain-text progress lines that precede the real
// response. We peek ahead: if the next non-empty content starts
// with '{' (JSON) or "data:" / ":" (SSE), the progress section
// is over and we fall through to normal processing.
for {
peek, err := br.Peek(1)
if err != nil {
break
}
// JSON object or SSE stream — stop consuming progress lines.
if peek[0] == '{' || peek[0] == ':' {
break
}
line, err := br.ReadString('\n')
if err != nil && line == "" {
break
}
line = strings.TrimRight(line, "\r\n")
if line == "" {
continue
}
// SSE data line — stop, let the normal SSE parser handle it.
if strings.HasPrefix(line, "data:") {
// Put the line back by chaining a reader with the rest.
br = bufio.NewReader(io.MultiReader(
strings.NewReader(line+"\n"),
br,
))
break
}
// Progress message — print to stderr.
fmt.Fprintln(os.Stderr, line)
}
// Detect streaming vs non-streaming response. Because server-sent
// progress lines may have been flushed before the Content-Type was
// set, we also peek at the body content to detect SSE.
isStreaming := strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream")
if !isStreaming {
if peek, err := br.Peek(5); err == nil {
isStreaming = strings.HasPrefix(string(peek), "data:")
}
}
if !isStreaming {
// Non-streaming JSON response
body, err := io.ReadAll(br)
resp.Body.Close()
if err != nil {
return assistantResponse.String(), fmt.Errorf("error reading response body: %w", err)
}
var nonStreamResp OpenAIChatResponse
if err := json.Unmarshal(body, &nonStreamResp); err != nil {
return assistantResponse.String(), fmt.Errorf("error parsing response: %w", err)
}
// Extract content from non-streaming response
if len(nonStreamResp.Choices) > 0 {
if nonStreamResp.Choices[0].Message.Content != "" {
content := nonStreamResp.Choices[0].Message.Content
outputFunc(content)
assistantResponse.WriteString(content)
}
finishReason = nonStreamResp.Choices[0].FinishReason
for _, tc := range nonStreamResp.Choices[0].Message.ToolCalls {
atc := &accumulatedToolCall{id: tc.ID, name: tc.Function.Name}
atc.arguments.WriteString(tc.Function.Arguments)
pendingToolCalls[tc.Index] = atc
}
}
if nonStreamResp.Usage != nil {
finalUsage = nonStreamResp.Usage
}
} else {
// SSE streaming response - process line by line
scanner := bufio.NewScanner(br)
for scanner.Scan() {
// Check if context was cancelled
select {
case <-ctx.Done():
resp.Body.Close()
return assistantResponse.String(), ctx.Err()
default:
}
line := scanner.Text()
if line == "" {
continue
}
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimPrefix(line, "data: ")
if data == "[DONE]" {
break
}
var streamResp OpenAIChatResponse
if err := json.Unmarshal([]byte(data), &streamResp); err != nil {
resp.Body.Close()
return assistantResponse.String(), fmt.Errorf("error parsing stream response: %w", err)
}
if streamResp.Usage != nil {
finalUsage = streamResp.Usage
}
if len(streamResp.Choices) > 0 {
choice := streamResp.Choices[0]
if choice.FinishReason != "" {
finishReason = choice.FinishReason
}
// Accumulate tool call fragments.
for _, tc := range choice.Delta.ToolCalls {
atc, ok := pendingToolCalls[tc.Index]
if !ok {
atc = &accumulatedToolCall{}
pendingToolCalls[tc.Index] = atc
}
if tc.ID != "" {
atc.id = tc.ID
}
if tc.Function.Name != "" {
atc.name = tc.Function.Name
}
atc.arguments.WriteString(tc.Function.Arguments)
}
if choice.Delta.ReasoningContent != "" {
chunk := choice.Delta.ReasoningContent
if printerState == chatPrinterContent {
outputFunc("\n\n")
}
if printerState != chatPrinterReasoning {
const thinkingHeader = "Thinking:\n"
if reasoningFmt != nil {
reasoningFmt.Print(thinkingHeader)
} else {
outputFunc(thinkingHeader)
}
}
printerState = chatPrinterReasoning
if reasoningFmt != nil {
reasoningFmt.Print(chunk)
} else {
outputFunc(chunk)
}
}
if choice.Delta.Content != "" {
chunk := choice.Delta.Content
if printerState == chatPrinterReasoning {
outputFunc("\n\n--\n\n")
}
printerState = chatPrinterContent
outputFunc(chunk)
assistantResponse.WriteString(chunk)
}
}
}
resp.Body.Close()
if err := scanner.Err(); err != nil {
return assistantResponse.String(), fmt.Errorf("error reading response stream: %w", err)
}
}
// If the model requested tool calls, execute them and loop.
if finishReason == "tool_calls" && len(pendingToolCalls) > 0 {
toolCallIterations++
if toolCallIterations >= maxToolCallIterations {
return assistantResponse.String(), fmt.Errorf("tool call loop exceeded %d iterations", maxToolCallIterations)
}
// Build assistant message with the tool calls.
toolCallSlice := make([]ToolCall, 0, len(pendingToolCalls))
for idx := 0; idx < len(pendingToolCalls); idx++ {
atc, ok := pendingToolCalls[idx]
if !ok {
continue
}
toolCallSlice = append(toolCallSlice, ToolCall{
ID: atc.id,
Type: "function",
Function: ToolCallFunction{
Name: atc.name,
Arguments: atc.arguments.String(),
},
})
}
messages = append(messages, OpenAIChatMessage{
Role: "assistant",
ToolCalls: toolCallSlice,
})
// Execute each tool and append results.
for _, tc := range toolCallSlice {
var result string
if tool, ok := toolMap[tc.Function.Name]; ok {
var args map[string]any
if err := json.Unmarshal([]byte(tc.Function.Arguments), &args); err != nil {
result = fmt.Sprintf("error parsing tool arguments: %v", err)
} else {
fmt.Fprintf(os.Stderr, "[tool] calling %s with args: %s\n", tc.Function.Name, tc.Function.Arguments)
var execErr error
result, execErr = tool.Execute(ctx, args)
if execErr != nil {
result = fmt.Sprintf("tool execution error: %v", execErr)
}
}
} else {
result = fmt.Sprintf("unknown tool: %s", tc.Function.Name)
}
messages = append(messages, OpenAIChatMessage{
Role: "tool",
ToolCallID: tc.ID,
Content: result,
})
}
// Reset for next iteration
assistantResponse.Reset()
continue
}
// Normal stop — we're done.
break
}
if finalUsage != nil {
usageInfo := fmt.Sprintf("\n\nToken usage: %d prompt + %d completion = %d total",
finalUsage.PromptTokens,
finalUsage.CompletionTokens,
finalUsage.TotalTokens)
usageFmt := color.New(color.FgHiBlack)
if !shouldUseMarkdown {
usageFmt.DisableColor()
}
outputFunc(usageFmt.Sprint(usageInfo))
}
return assistantResponse.String(), nil
}
// ChatWithContext performs a chat request with context support for cancellation and streams the response content with selective markdown rendering.
func (c *Client) ChatWithContext(ctx context.Context, model, prompt string, imageURLs []string, outputFunc func(string), shouldUseMarkdown bool) error {
_, err := c.ChatWithMessagesContext(ctx, model, nil, prompt, imageURLs, outputFunc, shouldUseMarkdown)
return err
}
func (c *Client) Remove(modelArgs []string, force bool) (string, error) {
modelRemoved := ""
for _, model := range modelArgs {
// Construct the URL with query parameters
removePath := fmt.Sprintf("%s/%s?force=%s",
inference.ModelsPrefix,
model,
strconv.FormatBool(force),
)
resp, err := c.doRequest(http.MethodDelete, removePath, nil)
if err != nil {
return modelRemoved, c.handleQueryError(err, removePath)
}
defer resp.Body.Close()
var bodyStr string
body, err := io.ReadAll(resp.Body)
if err != nil {
bodyStr = fmt.Sprintf("(failed to read response body: %v)", err)
} else {
bodyStr = string(body)
}
if resp.StatusCode == http.StatusOK {
var deleteResponse distribution.DeleteModelResponse
if err := json.Unmarshal(body, &deleteResponse); err != nil {
modelRemoved += fmt.Sprintf("Model %s removed successfully, but failed to parse response: %v\n", model, err)
} else {
for _, msg := range deleteResponse {
if msg.Untagged != nil {
modelRemoved += fmt.Sprintf("Untagged: %s\n", *msg.Untagged)
}
if msg.Deleted != nil {
modelRemoved += fmt.Sprintf("Deleted: %s\n", *msg.Deleted)
}
}
}
} else {
if resp.StatusCode == http.StatusNotFound {
return modelRemoved, fmt.Errorf("no such model: %s", model)
}
return modelRemoved, fmt.Errorf("removing %s failed with status %s: %s", model, resp.Status, bodyStr)
}
}
return modelRemoved, nil
}
type ServerVersionResponse struct {
Version string `json:"version"`
}
func (c *Client) ServerVersion() (ServerVersionResponse, error) {
resp, err := c.doRequest(http.MethodGet, "/version", nil)
if err != nil {
return ServerVersionResponse{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return ServerVersionResponse{}, fmt.Errorf("failed to get server version: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return ServerVersionResponse{}, fmt.Errorf("failed to read response body: %w", err)
}
var version ServerVersionResponse
if err := json.Unmarshal(body, &version); err != nil {
return ServerVersionResponse{}, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return version, nil
}
// BackendStatus to be imported from docker/model-runner when https://github.com/docker/model-runner/pull/42 is merged.
type BackendStatus struct {
BackendName string `json:"backend_name"`
ModelName string `json:"model_name"`
Mode string `json:"mode"`
LastUsed time.Time `json:"last_used,omitempty"`
InUse bool `json:"in_use,omitempty"`
Loading bool `json:"loading,omitempty"`
KeepAlive *inference.KeepAlive `json:"keep_alive,omitempty"`
}
func (c *Client) PS() ([]BackendStatus, error) {
psPath := inference.InferencePrefix + "/ps"
resp, err := c.doRequest(http.MethodGet, psPath, nil)
if err != nil {
return []BackendStatus{}, c.handleQueryError(err, psPath)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return []BackendStatus{}, fmt.Errorf("failed to list running models: %s", resp.Status)
}
body, _ := io.ReadAll(resp.Body)
var ps []BackendStatus
if err := json.Unmarshal(body, &ps); err != nil {
return []BackendStatus{}, fmt.Errorf("failed to unmarshal response body: %w", err)
}