v0.3.47 56b82c0

openai.go RawHistoryFolder

1package provider
2
3import (
4	"bytes"
5	"context"
6	"encoding/base64"
7	"encoding/json"
8	"fmt"
9	"io"
10	"net/http"
11	"regexp"
12	"strings"
13	"time"
14)
15
16const openaiDefaultBaseURL = "https://api.openai.com"
17
18// versionSegmentSuffix matches a trailing API version segment such as
19// "/v1" or Z.AI's "/v4".
20var versionSegmentSuffix = regexp.MustCompile(`/v\d+$`)
21
22// chatCompletionsURL builds the chat-completions endpoint for an
23// OpenAI-compatible base URL. A base that already carries an API
24// version segment gets "/chat/completions" appended directly; a bare
25// host (e.g. api.openai.com) gets the conventional "/v1/chat/completions".
26//
27// Matching any "/vN" segment (not just "/v1") keeps Z.AI's coding-plan
28// base, which ends in "/paas/v4", from getting a spurious "/v1" that
29// yields ".../paas/v4/v1/chat/completions" and a 404.
30func chatCompletionsURL(baseURL string) string {
31	if versionSegmentSuffix.MatchString(baseURL) {
32		return baseURL + "/chat/completions"
33	}
34	return baseURL + "/v1/chat/completions"
35}
36
37type openaiClient struct {
38	apiKey              string
39	baseURL             string
40	chatCompletionsPath string
41	name                string
42	oauth               bool // when true, apiKey actually holds an OAuth access token
43	headers             map[string]string
44	http                *http.Client
45}
46
47// NewOpenAI creates an OpenAI client using an API key. baseURL may be empty.
48func NewOpenAI(apiKey, baseURL string) Client {
49	if baseURL == "" {
50		baseURL = openaiDefaultBaseURL
51	}
52	return &openaiClient{
53		apiKey:  apiKey,
54		baseURL: strings.TrimRight(baseURL, "/"),
55		name:    "openai",
56		http:    &http.Client{Timeout: 0},
57	}
58}
59
60// NewOpenAIOAuth creates an OpenAI client using a subscription OAuth access token.
61// The token is sent as an HTTP Bearer credential on the standard chat/completions endpoint.
62func NewOpenAIOAuth(accessToken, baseURL string) Client {
63	if baseURL == "" {
64		baseURL = openaiDefaultBaseURL
65	}
66	return &openaiClient{
67		apiKey:  accessToken,
68		baseURL: strings.TrimRight(baseURL, "/"),
69		name:    "openai",
70		oauth:   true,
71		http:    &http.Client{Timeout: 0},
72	}
73}
74
75// NewKimi creates a Kimi/Moonshot client. Kimi's chat API is OpenAI-compatible.
76func NewKimi(apiKey, baseURL string) Client {
77	return NewKimiWithHeaders(apiKey, baseURL, nil)
78}
79
80// NewDeepSeek creates a DeepSeek client. DeepSeek's chat API is
81// OpenAI-compatible at https://api.deepseek.com/v1.
82func NewDeepSeek(apiKey, baseURL string) Client {
83	if baseURL == "" {
84		baseURL = "https://api.deepseek.com/v1"
85	}
86	return &openaiClient{
87		apiKey:  apiKey,
88		baseURL: strings.TrimRight(baseURL, "/"),
89		name:    "deepseek",
90		http:    &http.Client{Timeout: 0},
91	}
92}
93
94// NewKimiWithHeaders creates a Kimi/Moonshot client with extra headers.
95// Subscription tokens from Kimi Code need the official CLI's X-Msh-* headers.
96func NewKimiWithHeaders(apiKey, baseURL string, headers map[string]string) Client {
97	if baseURL == "" {
98		baseURL = "https://api.kimi.com/coding/v1"
99	}
100	return &openaiClient{
101		apiKey:  apiKey,
102		baseURL: strings.TrimRight(baseURL, "/"),
103		name:    "kimi",
104		headers: headers,
105		http:    &http.Client{Timeout: 0},
106	}
107}
108
109func (c *openaiClient) Name() string {
110	if c.name != "" {
111		return c.name
112	}
113	return "openai"
114}
115
116// ---- wire types ----
117
118type oaiContentText struct {
119	Type string `json:"type"` // "text"
120	Text string `json:"text"`
121}
122
123type oaiContentImage struct {
124	Type     string `json:"type"` // "image_url"
125	ImageURL struct {
126		URL string `json:"url"`
127	} `json:"image_url"`
128}
129
130type oaiToolCallFn struct {
131	Name      string `json:"name"`
132	Arguments string `json:"arguments"` // JSON string
133}
134
135type oaiToolCall struct {
136	ID       string        `json:"id"`
137	Type     string        `json:"type"` // "function"
138	Function oaiToolCallFn `json:"function"`
139}
140
141type oaiMessage struct {
142	Role       string        `json:"role"`
143	Content    interface{}   `json:"content,omitempty"` // string or []block
144	Name       string        `json:"name,omitempty"`
145	ToolCalls  []oaiToolCall `json:"tool_calls,omitempty"`
146	ToolCallID string        `json:"tool_call_id,omitempty"`
147	Tools      []oaiTool     `json:"tools,omitempty"`
148	// ReasoningContent carries the model's chain-of-thought summary
149	// alongside an assistant tool-call message. Required by Kimi's
150	// chat completions endpoint when thinking is enabled and the
151	// assistant message contains a tool call; OpenAI ignores it.
152	ReasoningContent string `json:"reasoning_content,omitempty"`
153}
154
155type oaiTool struct {
156	Type     string `json:"type"` // "function"
157	Function struct {
158		Name        string          `json:"name"`
159		Description string          `json:"description,omitempty"`
160		Parameters  json.RawMessage `json:"parameters"`
161	} `json:"function"`
162}
163
164type oaiStreamOptions struct {
165	IncludeUsage bool `json:"include_usage"`
166}
167
168type oaiRequest struct {
169	Model            string            `json:"model"`
170	Messages         []oaiMessage      `json:"messages"`
171	Tools            []oaiTool         `json:"tools,omitempty"`
172	ToolChoice       string            `json:"tool_choice,omitempty"`
173	Stream           bool              `json:"stream"`
174	StreamOptions    *oaiStreamOptions `json:"stream_options,omitempty"`
175	Temperature      *float32          `json:"temperature,omitempty"`
176	MaxTokens        *int              `json:"max_tokens,omitempty"`
177	MaxCompletionTok *int              `json:"max_completion_tokens,omitempty"`
178	ReasoningEffort  string            `json:"reasoning_effort,omitempty"`
179}
180
181// ---- request building ----
182
183func (c *openaiClient) buildRequest(req Request) (*oaiRequest, error) {
184	// The OpenAI wire client is shared by many providers, so prefer its own
185	// provider namespace before falling back to a provider-agnostic lookup.
186	m, err := FindModel(c.Name(), req.Model)
187	if err != nil {
188		m, err = FindModel("", req.Model)
189	}
190	if err != nil {
191		// Unknown model: use sensible defaults so local/custom
192		// models still work without a catalog entry.
193		m = Model{
194			ID:            req.Model,
195			ContextWindow: 32768,
196			MaxOutput:     8192,
197		}
198	}
199	reasoning := ClampReasoningForModel(m, req.Reasoning)
200	out := &oaiRequest{
201		Model:         req.Model,
202		Stream:        true,
203		StreamOptions: &oaiStreamOptions{IncludeUsage: true},
204		Temperature:   req.Temperature,
205	}
206
207	maxTok := req.MaxTokens
208	if maxTok <= 0 {
209		maxTok = m.MaxOutput
210	}
211	// Clamp max_tokens so output plus a minimum input reservation fits
212	// within the context window. Some providers (OpenRouter) enforce
213	// input + max_output <= context_length and reject requests where the
214	// total exceeds it. Reserving headroom guarantees the system prompt,
215	// first message, and tool definitions have room.
216	//
217	// The cap is derived from the context window, never from MaxOutput:
218	// MaxOutput is already the output ceiling, so subtracting from it
219	// would shrink every model's budget even when its output comfortably
220	// fits the window. We only lower maxTok when ContextWindow - reserve
221	// is actually tighter than the requested budget, which is exactly the
222	// pathological case (e.g. a model whose MaxOutput equals its window).
223	//
224	// The reserve is proportional (window/8, capped at 4096) rather than a
225	// flat 4096 so small-window models aren't over-penalized: a flat 4096
226	// would halve gpt-4's 8192 budget, while window/8 reserves a sensible
227	// 1024 there and still tops out at 4096 for large contexts.
228	//
229	// Some providers (OpenRouter) report inflated model-level context
230	// windows (e.g. 1000000) while the serving provider enforces a much
231	// tighter limit (e.g. 262144). Discovery already prefers the serving
232	// provider's smaller context_length, so m.ContextWindow is the real
233	// limit by the time we get here.
234	if m.ContextWindow > 0 {
235		reserve := m.ContextWindow / 8
236		const maxReserve = 4096
237		if reserve > maxReserve {
238			reserve = maxReserve
239		}
240		clamped := m.ContextWindow - reserve
241		if clamped < 1 {
242			clamped = 1
243		}
244		if maxTok > clamped {
245			maxTok = clamped
246		}
247	}
248	if m.Reasoning {
249		if maxTok > 0 {
250			out.MaxCompletionTok = &maxTok
251		}
252		effort := OpenAIReasoningEffort(reasoning)
253		if usesAdaptiveThinking(m) {
254			// Some gateways expose adaptive-thinking Anthropic models through
255			// the OpenAI-compatible chat-completions wire. They accept the
256			// same reasoning_effort knob, including the top "xhigh" tier;
257			// don't clamp zot's "maximum" to "high" for those models.
258			effort = OpenAICompatAnthropicEffort(reasoning)
259		}
260		if hasReasoningLevelOverride(m, req.Reasoning) {
261			// Explicit model mappings describe the endpoint's actual effort
262			// values and take precedence over conservative protocol defaults.
263			effort = reasoning
264		}
265		if effort != "" {
266			out.ReasoningEffort = effort
267		}
268	} else if maxTok > 0 {
269		// Omit max_tokens when unknown (some discovered models don't
270		// advertise an output cap) so the server applies its own default
271		// instead of receiving an invalid max_tokens: 0.
272		out.MaxTokens = &maxTok
273	}
274
275	if req.System != "" {
276		out.Messages = append(out.Messages, oaiMessage{Role: "system", Content: req.System})
277	}
278
279	deferredMode := supportsDeferredTools(c.Name()) && isKimiDeferredModel(req.Model)
280	toolByName := make(map[string]Tool, len(req.Tools))
281	for _, t := range req.Tools {
282		toolByName[t.Name] = t
283	}
284	activatedTools := activatedToolNames(req.Messages)
285
286	// DeepSeek's chat-completions API rejects the multimodal content
287	// schema (parts arrays containing image_url). Force every user/tool
288	// message to a plain string and silently drop image blocks for
289	// this provider so historical sessions with screenshots still replay.
290	textOnly := c.name == "deepseek"
291
292	req.Messages = RepairOrphanedToolResults(req.Messages)
293	for _, msg := range req.Messages {
294		switch msg.Role {
295		case RoleUser:
296			content := buildOAIUserContent(msg.Content, textOnly)
297			out.Messages = append(out.Messages, oaiMessage{Role: "user", Content: content})
298		case RoleAssistant:
299			am := oaiMessage{Role: "assistant"}
300			var text strings.Builder
301			var reasoning strings.Builder
302			for _, b := range msg.Content {
303				switch v := b.(type) {
304				case TextBlock:
305					if strings.TrimSpace(v.Text) == "" {
306						continue
307					}
308					if text.Len() > 0 {
309						text.WriteString("\n")
310					}
311					text.WriteString(v.Text)
312				case ToolCallBlock:
313					args := v.Arguments
314					if len(args) == 0 || !json.Valid(args) {
315						args = json.RawMessage("{}")
316					}
317					am.ToolCalls = append(am.ToolCalls, oaiToolCall{
318						ID:   v.ID,
319						Type: "function",
320						Function: oaiToolCallFn{
321							Name:      v.Name,
322							Arguments: string(args),
323						},
324					})
325				case ReasoningBlock:
326					if v.Summary != "" {
327						if reasoning.Len() > 0 {
328							reasoning.WriteString("\n")
329						}
330						reasoning.WriteString(v.Summary)
331					}
332				}
333			}
334			if text.Len() > 0 {
335				am.Content = text.String()
336			}
337			if reasoning.Len() > 0 && len(am.ToolCalls) > 0 {
338				am.ReasoningContent = reasoning.String()
339			}
340			// Kimi rejects assistant messages with neither visible text nor
341			// tool calls ("assistant must not be empty"). This can happen when
342			// a previous stream produced only reasoning_content, which zot keeps
343			// internally for provider replay but cannot send back as standalone
344			// assistant content on OpenAI-compatible chat-completions APIs.
345			if am.Content == nil && len(am.ToolCalls) == 0 {
346				continue
347			}
348			out.Messages = append(out.Messages, am)
349		case RoleTool:
350			// Each ToolResultBlock becomes its own tool message. Preserve
351			// image blocks for vision-capable OpenAI models instead of
352			// flattening the tool output to plain text.
353			for _, b := range msg.Content {
354				if tr, ok := b.(ToolResultBlock); ok {
355					content := buildOAIToolContent(tr.Content, tr.IsError, textOnly)
356					out.Messages = append(out.Messages, oaiMessage{
357						Role:       "tool",
358						ToolCallID: tr.CallID,
359						Content:    content,
360					})
361				}
362			}
363		}
364		if deferredMode {
365			var loaded []oaiTool
366			for _, name := range msg.AddedToolNames {
367				if t, ok := toolByName[name]; ok && t.Deferred {
368					loaded = append(loaded, makeOAITool(t))
369				}
370			}
371			if len(loaded) > 0 {
372				out.Messages = append(out.Messages, oaiMessage{Role: "system", Tools: loaded})
373			}
374		}
375	}
376
377	for _, t := range req.Tools {
378		if t.Deferred && (deferredMode || !activatedTools[t.Name]) {
379			continue
380		}
381		out.Tools = append(out.Tools, makeOAITool(t))
382	}
383	if len(out.Tools) > 0 {
384		out.ToolChoice = "auto"
385	}
386
387	return out, nil
388}
389
390func isKimiDeferredModel(model string) bool {
391	model = strings.ToLower(model)
392	return model == "kimi-k3" || strings.HasSuffix(model, "/kimi-k3")
393}
394
395// supportsDeferredTools reports whether a provider speaks Moonshot's
396// deferred-tools handshake, where a tool that becomes available mid-conversation
397// is delivered on a system message carrying a `tools` array.
398//
399// That message is a Moonshot API extension, not OpenAI wire format: it has no
400// content, which a strict chat-completions schema rejects outright. Gondola,
401// which serves kimi-k3 on top of Venice, answers "messages.N: Invalid input"
402// and the conversation cannot recover, because the injection is rebuilt from
403// message history on every later turn. Providers outside this list take the
404// standard path instead, where an activated deferred tool joins the top-level
405// tools array.
406func supportsDeferredTools(provider string) bool {
407	return provider == "moonshotai" || provider == "moonshotai-cn"
408}
409
410func makeOAITool(t Tool) oaiTool {
411	var tool oaiTool
412	tool.Type = "function"
413	tool.Function.Name = t.Name
414	tool.Function.Description = t.Description
415	tool.Function.Parameters = t.Schema
416	return tool
417}
418
419func buildOAIUserContent(blocks []Content, textOnly bool) interface{} {
420	hasImage := false
421	for _, b := range blocks {
422		if _, ok := b.(ImageBlock); ok {
423			hasImage = true
424			break
425		}
426	}
427	if textOnly || !hasImage {
428		var sb strings.Builder
429		for _, b := range blocks {
430			if tb, ok := b.(TextBlock); ok {
431				if sb.Len() > 0 {
432					sb.WriteString("\n")
433				}
434				sb.WriteString(tb.Text)
435			}
436		}
437		return sb.String()
438	}
439	return buildOAIContentBlocks(blocks, false)
440}
441
442func buildOAIToolContent(blocks []Content, isError, textOnly bool) interface{} {
443	hasImage := false
444	for _, b := range blocks {
445		if _, ok := b.(ImageBlock); ok {
446			hasImage = true
447			break
448		}
449	}
450	if textOnly || !hasImage {
451		var sb strings.Builder
452		for _, b := range blocks {
453			if tb, ok := b.(TextBlock); ok {
454				if sb.Len() > 0 {
455					sb.WriteString("\n")
456				}
457				sb.WriteString(tb.Text)
458			}
459		}
460		if isError && sb.Len() > 0 {
461			sb.WriteString(" [error]")
462		}
463		return sb.String()
464	}
465	return buildOAIContentBlocks(blocks, isError)
466}
467
468func buildOAIContentBlocks(blocks []Content, isError bool) []interface{} {
469	var arr []interface{}
470	for _, b := range blocks {
471		switch v := b.(type) {
472		case TextBlock:
473			arr = append(arr, oaiContentText{Type: "text", Text: v.Text})
474		case ImageBlock:
475			var img oaiContentImage
476			img.Type = "image_url"
477			img.ImageURL.URL = "data:" + v.MimeType + ";base64," + base64.StdEncoding.EncodeToString(v.Data)
478			arr = append(arr, img)
479		}
480	}
481	if isError {
482		arr = append(arr, oaiContentText{Type: "text", Text: "[error]"})
483	}
484	return arr
485}
486
487// ---- streaming ----
488
489func (c *openaiClient) chatCompletionsURL() string {
490	if c.chatCompletionsPath != "" {
491		return strings.TrimRight(c.baseURL, "/") + "/" + strings.TrimLeft(c.chatCompletionsPath, "/")
492	}
493	return chatCompletionsURL(c.baseURL)
494}
495
496func (c *openaiClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
497	endpoint := c.chatCompletionsURL()
498	wire, err := c.buildRequest(req)
499	if err != nil {
500		return nil, err
501	}
502	body, err := json.Marshal(wire)
503	if err != nil {
504		return nil, err
505	}
506	newReq := func() (*http.Request, error) {
507		httpReq, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewReader(body))
508		if err != nil {
509			return nil, err
510		}
511		httpReq.Header.Set("content-type", "application/json")
512		httpReq.Header.Set("accept", "text/event-stream")
513		httpReq.Header.Set("authorization", "Bearer "+c.apiKey)
514		for k, v := range c.headers {
515			httpReq.Header.Set(k, v)
516		}
517		return httpReq, nil
518	}
519
520	resp, err := doStreamWithRetry(ctx, c.http, newReq)
521	if err != nil {
522		return nil, fmt.Errorf("%s: %w", c.Name(), err)
523	}
524	if resp.StatusCode != http.StatusOK {
525		b, _ := io.ReadAll(resp.Body)
526		resp.Body.Close()
527		return nil, fmt.Errorf("%s: http %d: %s", c.Name(), resp.StatusCode, strings.TrimSpace(string(b)))
528	}
529
530	out := make(chan Event, 16)
531	go c.runStream(ctx, resp, req, out)
532	return out, nil
533}
534
535func (c *openaiClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
536	defer close(out)
537	defer resp.Body.Close()
538
539	model, _ := FindModel("", req.Model)
540	out <- EventStart{Model: req.Model, Provider: c.Name()}
541
542	raw := make(chan sseEvent, 16)
543	go readSSE(resp.Body, raw)
544
545	// Interleaved block tracking: text and tool_calls preserve their
546	// emission order so the assistant message renders in the same order
547	// the model produced it. The builder fires one kind of block at a
548	// time — incoming text deltas after a tool_call split into a fresh
549	// text block; subsequent tool_calls each get their own slot.
550	type blockEntry struct {
551		kind      string // "text" | "tool_use"
552		textBuf   strings.Builder
553		toolID    string
554		toolName  string
555		toolArgs  strings.Builder
556		announced bool
557	}
558	var (
559		blocks       []*blockEntry
560		currentText  *blockEntry             // most-recent text block, nil if none
561		toolByIdx    = map[int]*blockEntry{} // openai tool_call index -> block
562		reasoningBuf strings.Builder
563		usage        Usage
564		stop         StopReason = StopEnd
565		finalErr     error
566	)
567
568	appendText := func(delta string) {
569		if currentText == nil {
570			currentText = &blockEntry{kind: "text"}
571			blocks = append(blocks, currentText)
572		}
573		currentText.textBuf.WriteString(delta)
574	}
575
576	getOrCreateTool := func(idx int) *blockEntry {
577		if t, ok := toolByIdx[idx]; ok {
578			return t
579		}
580		t := &blockEntry{kind: "tool_use"}
581		toolByIdx[idx] = t
582		blocks = append(blocks, t)
583		// A new tool block breaks the current text block. Subsequent text
584		// deltas will start a fresh text block after this tool.
585		currentText = nil
586		return t
587	}
588
589	assembleMsg := func() Message {
590		content := []Content{}
591		for _, b := range blocks {
592			switch b.kind {
593			case "text":
594				if b.textBuf.Len() > 0 {
595					content = append(content, TextBlock{Text: b.textBuf.String()})
596				}
597			case "tool_use":
598				args := b.toolArgs.String()
599				if args == "" || !json.Valid([]byte(args)) {
600					args = "{}"
601				}
602				content = append(content, ToolCallBlock{
603					ID: b.toolID, Name: b.toolName, Arguments: json.RawMessage(args),
604				})
605			}
606		}
607		if reasoningBuf.Len() > 0 {
608			content = append(content, ReasoningBlock{Summary: reasoningBuf.String()})
609		}
610		return Message{Role: RoleAssistant, Content: content, Time: time.Now()}
611	}
612
613	sendDone := func() {
614		usage.CostUSD = ComputeCost(model, usage)
615		out <- EventUsage{Usage: usage}
616		out <- EventDone{Stop: stop, Err: finalErr, Message: assembleMsg()}
617	}
618
619	for {
620		select {
621		case <-ctx.Done():
622			stop = StopAborted
623			finalErr = ctx.Err()
624			sendDone()
625			return
626		case ev, ok := <-raw:
627			if !ok {
628				sendDone()
629				return
630			}
631			if ev.Data == "[DONE]" {
632				sendDone()
633				return
634			}
635			var chunk struct {
636				Choices []struct {
637					Index int `json:"index"`
638					Delta struct {
639						Content          string `json:"content"`
640						ReasoningContent string `json:"reasoning_content"`
641						ToolCalls        []struct {
642							Index    int    `json:"index"`
643							ID       string `json:"id"`
644							Type     string `json:"type"`
645							Function struct {
646								Name      string `json:"name"`
647								Arguments string `json:"arguments"`
648							} `json:"function"`
649						} `json:"tool_calls"`
650					} `json:"delta"`
651					FinishReason string `json:"finish_reason"`
652				} `json:"choices"`
653				Usage *struct {
654					PromptTokens        int `json:"prompt_tokens"`
655					CompletionTokens    int `json:"completion_tokens"`
656					PromptTokensDetails struct {
657						CachedTokens int `json:"cached_tokens"`
658					} `json:"prompt_tokens_details"`
659					CompletionTokensDetails *struct {
660						ReasoningTokens int `json:"reasoning_tokens"`
661					} `json:"completion_tokens_details"`
662				} `json:"usage"`
663				Error *struct {
664					Message string `json:"message"`
665					Type    string `json:"type"`
666				} `json:"error"`
667			}
668			if err := json.Unmarshal([]byte(ev.Data), &chunk); err != nil {
669				continue
670			}
671			if chunk.Error != nil {
672				stop = StopError
673				finalErr = fmt.Errorf("openai: %s", chunk.Error.Message)
674				sendDone()
675				return
676			}
677			if chunk.Usage != nil {
678				usage.InputTokens = chunk.Usage.PromptTokens - chunk.Usage.PromptTokensDetails.CachedTokens
679				if usage.InputTokens < 0 {
680					usage.InputTokens = chunk.Usage.PromptTokens
681				}
682				usage.OutputTokens = chunk.Usage.CompletionTokens
683				usage.CacheReadTokens = chunk.Usage.PromptTokensDetails.CachedTokens
684				if details := chunk.Usage.CompletionTokensDetails; details != nil {
685					usage.ReasoningTokens = details.ReasoningTokens
686					usage.ReasoningTokensKnown = true
687				}
688			}
689			for _, ch := range chunk.Choices {
690				if ch.Delta.ReasoningContent != "" {
691					reasoningBuf.WriteString(ch.Delta.ReasoningContent)
692				}
693				if ch.Delta.Content != "" {
694					appendText(ch.Delta.Content)
695					out <- EventTextDelta{Delta: ch.Delta.Content}
696				}
697				for _, tc := range ch.Delta.ToolCalls {
698					t := getOrCreateTool(tc.Index)
699					if tc.ID != "" {
700						t.toolID = tc.ID
701					}
702					if tc.Function.Name != "" {
703						t.toolName = tc.Function.Name
704					}
705					if !t.announced && t.toolID != "" && t.toolName != "" {
706						t.announced = true
707						out <- EventToolStart{ID: t.toolID, Name: t.toolName}
708					}
709					if tc.Function.Arguments != "" {
710						t.toolArgs.WriteString(tc.Function.Arguments)
711						if t.announced {
712							out <- EventToolArgs{ID: t.toolID, Delta: tc.Function.Arguments}
713						}
714					}
715				}
716				switch ch.FinishReason {
717				case "stop":
718					stop = StopEnd
719				case "length":
720					stop = StopLength
721				case "tool_calls", "function_call":
722					stop = StopToolUse
723					for _, b := range blocks {
724						if b.kind == "tool_use" && b.announced {
725							out <- EventToolEnd{ID: b.toolID}
726						}
727					}
728				}
729			}
730		}
731	}
732}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close