From 9bcccbd80d36ae678d58cd8f83c4d0c790c16b48 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 26 Sep 2025 08:19:26 +0300 Subject: Auto apply inline prompt completions --- docs/coverage.html | 715 ++++++++++++++++++++++++++++------------------------- 1 file changed, 380 insertions(+), 335 deletions(-) (limited to 'docs/coverage.html') diff --git a/docs/coverage.html b/docs/coverage.html index a1db0c8..7cda0d1 100644 --- a/docs/coverage.html +++ b/docs/coverage.html @@ -111,13 +111,13 @@ - + - + @@ -3593,7 +3593,7 @@ type RequestOption func(*Options) func WithModel(model string) RequestOption { return func(o *Options) { o.Model = model } } func WithTemperature(t float64) RequestOption { return func(o *Options) { o.Temperature = t } } -func WithMaxTokens(n int) RequestOption { return func(o *Options) { o.MaxTokens = n } } +func WithMaxTokens(n int) RequestOption { return func(o *Options) { o.MaxTokens = n } } func WithStop(stop ...string) RequestOption { return func(o *Options) { o.Stop = append([]string{}, stop...) } } @@ -3773,11 +3773,11 @@ var std *log.Logger func Bind(l *log.Logger) { std = l } // Logf prints a formatted message with a module prefix and base ANSI style. -func Logf(prefix, format string, args ...any) { +func Logf(prefix, format string, args ...any) { if std == nil { return } - msg := fmt.Sprintf(format, args...) + msg := fmt.Sprintf(format, args...) std.Print(AnsiBase + prefix + msg + AnsiReset) } @@ -3868,18 +3868,18 @@ import ( // - window: include a window of lines around the cursor // - file-on-new-func: include full file only when defining a new function // - always-full: always include the full file -func (s *Server) buildAdditionalContext(newFunc bool, uri string, pos Position) (string, bool) { +func (s *Server) buildAdditionalContext(newFunc bool, uri string, pos Position) (string, bool) { mode := s.contextMode() switch mode { case "minimal": return "", false case "window": return s.windowContext(uri, pos), true - case "file-on-new-func": + case "file-on-new-func": if newFunc { return s.fullFileContext(uri), true } - return "", false + return "", false case "always-full": return s.fullFileContext(uri), true default: @@ -3953,7 +3953,7 @@ type document struct { lines []string } -func (s *Server) setDocument(uri, text string) { +func (s *Server) setDocument(uri, text string) { s.mu.Lock() defer s.mu.Unlock() s.docs[uri] = &document{uri: uri, text: text, lines: splitLines(text)} @@ -3971,76 +3971,76 @@ func (s *Server) markActivity() { s.mu.Unlock() } -func (s *Server) getDocument(uri string) *document { +func (s *Server) getDocument(uri string) *document { s.mu.RLock() defer s.mu.RUnlock() return s.docs[uri] } // splitLines splits the input string into lines, normalizing line endings to '\n'. -func splitLines(sx string) []string { +func splitLines(sx string) []string { sx = strings.ReplaceAll(sx, "\r\n", "\n") return strings.Split(sx, "\n") } -func (s *Server) lineContext(uri string, pos Position) (above, current, below, funcCtx string) { +func (s *Server) lineContext(uri string, pos Position) (above, current, below, funcCtx string) { d := s.getDocument(uri) if d == nil || len(d.lines) == 0 { return "", "", "", "" } - idx := pos.Line + idx := pos.Line if idx < 0 { idx = 0 } - if idx >= len(d.lines) { + if idx >= len(d.lines) { idx = len(d.lines) - 1 } - current = d.lines[idx] + current = d.lines[idx] if idx-1 >= 0 { above = d.lines[idx-1] } - if idx+1 < len(d.lines) { + if idx+1 < len(d.lines) { below = d.lines[idx+1] } - for i := idx; i >= 0; i-- { + for i := idx; i >= 0; i-- { line := strings.TrimSpace(d.lines[i]) if hasAny(line, []string{"func ", "def ", "class ", "fn ", "procedure ", "sub "}) { funcCtx = line break } } - return above, current, below, funcCtx + return above, current, below, funcCtx } // isDefiningNewFunction returns true when the cursor appears to be within // a function declaration/signature and before the opening '{' of the body. // Heuristic: find nearest preceding line containing "func "; ensure no '{' // appears before the cursor across those lines. -func (s *Server) isDefiningNewFunction(uri string, pos Position) bool { +func (s *Server) isDefiningNewFunction(uri string, pos Position) bool { d := s.getDocument(uri) if d == nil || len(d.lines) == 0 { return false } - idx := pos.Line + idx := pos.Line if idx < 0 { idx = 0 } - if idx >= len(d.lines) { + if idx >= len(d.lines) { idx = len(d.lines) - 1 } // Find signature start - sigStart := -1 - for i := idx; i >= 0; i-- { + sigStart := -1 + for i := idx; i >= 0; i-- { if strings.Contains(d.lines[i], "func ") { sigStart = i break } // stop if we hit a closing brace which likely ends a previous block - if strings.Contains(d.lines[i], "}") { + if strings.Contains(d.lines[i], "}") { break } } - if sigStart == -1 { + if sigStart == -1 { return false } // Scan for '{' from sigStart up to cursor position; if found before or at cursor, we're in body @@ -4060,29 +4060,29 @@ func (s *Server) isDefiningNewFunction(uri string, pos Position) bool return true } -func hasAny(s string, needles []string) bool { - for _, n := range needles { +func hasAny(s string, needles []string) bool { + for _, n := range needles { if strings.Contains(s, n) { return true } } - return false + return false } -func trimLen(s string) string { +func trimLen(s string) string { s = strings.TrimSpace(s) if len(s) > 200 { return s[:200] + "…" } - return s + return s } -func firstLine(s string) string { +func firstLine(s string) string { s = strings.ReplaceAll(s, "\r\n", "\n") if idx := strings.IndexByte(s, '\n'); idx >= 0 { return s[:idx] } - return s + return s } @@ -4203,7 +4203,7 @@ func (s *Server) findFirstInstructionInLine(line string) (instr string, cleaned // handleCompletion moved to handlers_completion.go -func (s *Server) reply(id json.RawMessage, result any, err *RespError) { +func (s *Server) reply(id json.RawMessage, result any, err *RespError) { resp := Response{JSONRPC: "2.0", ID: id, Result: result, Error: err} s.writeMessage(resp) } @@ -4277,33 +4277,33 @@ func (s *Server) reply(id json.RawMessage, result any, err *RespError) { +func (s *Server) completionCacheKey(p CompletionParams, above, current, below, funcCtx string, inParams bool, hasExtra bool, extraText string) string { // Normalize left-of-cursor by trimming trailing spaces/tabs idx := p.Position.Character if idx > len(current) { idx = len(current) } - left := strings.TrimRight(current[:idx], " \t") + left := strings.TrimRight(current[:idx], " \t") right := "" if idx < len(current) { right = current[idx:] } - prov := "" + prov := "" model := "" - if client := s.currentLLMClient(); client != nil { + if client := s.currentLLMClient(); client != nil { prov = client.Name() model = client.DefaultModel() } - temp := "" + temp := "" if tempPtr := s.codingTemperature(); tempPtr != nil { temp = fmt.Sprintf("%.3f", *tempPtr) } - extra := "" + extra := "" if hasExtra { extra = strings.TrimSpace(extraText) } // Compose a key from essential context parts - return strings.Join([]string{ + return strings.Join([]string{ "v1", // version for future-proofing prov, model, @@ -4320,11 +4320,11 @@ func (s *Server) completionCacheKey(p CompletionParams, above, current, below, f }, "\x1f") // use unit separator to avoid collisions } -func (s *Server) completionCacheGet(key string) (string, bool) { +func (s *Server) completionCacheGet(key string) (string, bool) { s.mu.Lock() defer s.mu.Unlock() v, ok := s.compCache[key] - if !ok { + if !ok { return "", false } // move to most-recent @@ -4332,13 +4332,13 @@ func (s *Server) completionCacheGet(key string) (string, bool) { +func (s *Server) completionCachePut(key, value string) { s.mu.Lock() defer s.mu.Unlock() if s.compCache == nil { s.compCache = make(map[string]string) } - if _, exists := s.compCache[key]; !exists { + if _, exists := s.compCache[key]; !exists { s.compCacheOrder = append(s.compCacheOrder, key) s.compCache[key] = value if len(s.compCacheOrder) > 10 { @@ -4347,7 +4347,7 @@ func (s *Server) completionCachePut(key, value string) - return + return } // update existing and mark most-recent s.compCache[key] = value @@ -4431,15 +4431,15 @@ func (s *Server) isTriggerEvent(p CompletionParams, current string) bool return false } -func (s *Server) makeCompletionItems(cleaned string, inParams bool, current string, p CompletionParams, docStr string) []CompletionItem { +func (s *Server) makeCompletionItems(cleaned string, inParams bool, current string, p CompletionParams, docStr string) []CompletionItem { te, filter := computeTextEditAndFilter(cleaned, inParams, current, p) rm := s.collectPromptRemovalEdits(p.TextDocument.URI) label := labelForCompletion(cleaned, filter) detail := "Hexai LLM completion" - if client := s.currentLLMClient(); client != nil { + if client := s.currentLLMClient(); client != nil { detail = "Hexai " + client.Name() + ":" + client.DefaultModel() } - return []CompletionItem{{ + return []CompletionItem{{ Label: label, Kind: 1, Detail: detail, @@ -5183,10 +5183,10 @@ type completionPlan struct { cacheKey string } -func (s *Server) handleCompletion(req Request) { +func (s *Server) handleCompletion(req Request) { var p CompletionParams var docStr string - if err := json.Unmarshal(req.Params, &p); err == nil { + if err := json.Unmarshal(req.Params, &p); err == nil { // Log trigger information for every completion request from client tk, tch := extractTriggerInfo(p) logging.Logf("lsp ", "completion trigger kind=%d char=%q uri=%s line=%d char=%d", @@ -5196,11 +5196,11 @@ func (s *Server) handleCompletion(req Request) { if s.logContext { s.logCompletionContext(p, above, current, below, funcCtx) } - if s.llmClient != nil { + if s.llmClient != nil { newFunc := s.isDefiningNewFunction(p.TextDocument.URI, p.Position) extra, has := s.buildAdditionalContext(newFunc, p.TextDocument.URI, p.Position) items, ok := s.tryLLMCompletion(p, above, current, below, funcCtx, docStr, has, extra) - if ok { + if ok { s.reply(req.ID, CompletionList{IsIncomplete: false, Items: items}, nil) return } @@ -5212,26 +5212,26 @@ func (s *Server) handleCompletion(req Request) { // extractTriggerInfo returns the LSP completion TriggerKind and TriggerCharacter // if provided by the client; when absent it returns zeros. -func extractTriggerInfo(p CompletionParams) (kind int, ch string) { +func extractTriggerInfo(p CompletionParams) (kind int, ch string) { if p.Context == nil { return 0, "" } - var ctx struct { + var ctx struct { TriggerKind int `json:"triggerKind"` TriggerCharacter string `json:"triggerCharacter,omitempty"` } if raw, ok := p.Context.(json.RawMessage); ok { _ = json.Unmarshal(raw, &ctx) - } else { + } else { b, _ := json.Marshal(p.Context) _ = json.Unmarshal(b, &ctx) } - return ctx.TriggerKind, ctx.TriggerCharacter + return ctx.TriggerKind, ctx.TriggerCharacter } // --- completion helpers --- -func (s *Server) buildDocString(p CompletionParams, above, current, below, funcCtx string) string { +func (s *Server) buildDocString(p CompletionParams, above, current, below, funcCtx string) string { return fmt.Sprintf("file: %s\nline: %d\nabove: %s\ncurrent: %s\nbelow: %s\nfunction: %s", p.TextDocument.URI, p.Position.Line, trimLen(above), trimLen(current), trimLen(below), trimLen(funcCtx)) } @@ -5241,7 +5241,7 @@ func (s *Server) logCompletionContext(p CompletionParams, above, current, below, p.TextDocument.URI, p.Position.Line, p.Position.Character, trimLen(above), trimLen(current), trimLen(below), trimLen(funcCtx)) } -func (s *Server) tryLLMCompletion(p CompletionParams, above, current, below, funcCtx, docStr string, hasExtra bool, extraText string) ([]CompletionItem, bool) { +func (s *Server) tryLLMCompletion(p CompletionParams, above, current, below, funcCtx, docStr string, hasExtra bool, extraText string) ([]CompletionItem, bool) { ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second) defer cancel() @@ -5250,14 +5250,14 @@ func (s *Server) tryLLMCompletion(p CompletionParams, above, current, below, fun return items, true } - if items, ok := s.tryProviderNativeCompletion(current, p, above, below, funcCtx, docStr, hasExtra, extraText, plan.inParams); ok { + if items, ok := s.tryProviderNativeCompletion(current, p, above, below, funcCtx, docStr, hasExtra, extraText, plan.inParams); ok { return items, true } - return s.executeChatCompletion(ctx, plan) + return s.executeChatCompletion(ctx, plan) } -func (s *Server) prepareCompletionPlan(p CompletionParams, above, current, below, funcCtx, docStr string, hasExtra bool, extraText string) (completionPlan, []CompletionItem, bool) { +func (s *Server) prepareCompletionPlan(p CompletionParams, above, current, below, funcCtx, docStr string, hasExtra bool, extraText string) (completionPlan, []CompletionItem, bool) { plan := completionPlan{ params: p, above: above, @@ -5274,10 +5274,10 @@ func (s *Server) prepareCompletionPlan(p CompletionParams, above, current, below logging.Logf("lsp ", "%scompletion skip=no-trigger line=%d char=%d current=%q%s", logging.AnsiYellow, p.Position.Line, p.Position.Character, trimLen(current), logging.AnsiBase) return plan, []CompletionItem{}, true } - if s.shouldSuppressForChatTriggerEOL(current, p) { + if s.shouldSuppressForChatTriggerEOL(current, p) { return plan, []CompletionItem{}, true } - plan.inParams = inParamList(current, p.Position.Character) + plan.inParams = inParamList(current, p.Position.Character) plan.manualInvoke = parseManualInvoke(p.Context) plan.cacheKey = s.completionCacheKey(p, above, current, below, funcCtx, plan.inParams, hasExtra, extraText) if cleaned, ok := s.completionCacheGet(plan.cacheKey); ok && strings.TrimSpace(cleaned) != "" { @@ -5286,107 +5286,107 @@ func (s *Server) prepareCompletionPlan(p CompletionParams, above, current, below logging.AnsiGreen, logging.PreviewForLog(cleaned), logging.AnsiBase) return plan, s.makeCompletionItems(cleaned, plan.inParams, current, p, docStr), true } - if isBareDoubleOpen(current, openChar, closeChar) || isBareDoubleOpen(below, openChar, closeChar) { + if isBareDoubleOpen(current, openChar, closeChar) || isBareDoubleOpen(below, openChar, closeChar) { logging.Logf("lsp ", "%scompletion skip=empty-double-semicolon line=%d char=%d current=%q%s", logging.AnsiYellow, p.Position.Line, p.Position.Character, trimLen(current), logging.AnsiBase) return plan, []CompletionItem{}, true } - if !plan.inParams && !s.prefixHeuristicAllows(plan.inlinePrompt, current, p, plan.manualInvoke) { + if !plan.inParams && !s.prefixHeuristicAllows(plan.inlinePrompt, current, p, plan.manualInvoke) { logging.Logf("lsp ", "%scompletion skip=short-prefix line=%d char=%d current=%q%s", logging.AnsiYellow, p.Position.Line, p.Position.Character, trimLen(current), logging.AnsiBase) return plan, []CompletionItem{}, true } - return plan, nil, false + return plan, nil, false } -func (s *Server) executeChatCompletion(ctx context.Context, plan completionPlan) ([]CompletionItem, bool) { +func (s *Server) executeChatCompletion(ctx context.Context, plan completionPlan) ([]CompletionItem, bool) { messages := s.buildCompletionMessages(plan.inlinePrompt, plan.hasExtra, plan.extraText, plan.inParams, plan.params, plan.above, plan.current, plan.below, plan.funcCtx) sentSize := 0 - for _, m := range messages { + for _, m := range messages { sentSize += len(m.Content) } - s.incSentCounters(sentSize) + s.incSentCounters(sentSize) opts := s.llmRequestOpts() s.waitForDebounce(ctx) if !s.waitForThrottle(ctx) { return nil, false } - client := s.currentLLMClient() + client := s.currentLLMClient() if client == nil { return nil, false } - logging.Logf("lsp ", "completion llm=requesting model=%s", client.DefaultModel()) + logging.Logf("lsp ", "completion llm=requesting model=%s", client.DefaultModel()) text, err := client.Chat(ctx, messages, opts...) if err != nil { logging.Logf("lsp ", "llm completion error: %v", err) s.logLLMStats() return nil, false } - s.incRecvCounters(len(text)) + s.incRecvCounters(len(text)) s.logLLMStats() trimmed := strings.TrimSpace(text) cleaned := s.postProcessCompletion(trimmed, plan.current[:plan.params.Position.Character], plan.current) if cleaned == "" { return nil, false } - s.completionCachePut(plan.cacheKey, cleaned) + s.completionCachePut(plan.cacheKey, cleaned) items := s.makeCompletionItems(cleaned, plan.inParams, plan.current, plan.params, plan.docStr) return items, true } // parseManualInvoke inspects the LSP completion context and reports whether the user manually invoked completion. -func parseManualInvoke(ctx any) bool { +func parseManualInvoke(ctx any) bool { if ctx == nil { return false } - var c struct { + var c struct { TriggerKind int `json:"triggerKind"` } if raw, ok := ctx.(json.RawMessage); ok { _ = json.Unmarshal(raw, &c) - } else { + } else { b, _ := json.Marshal(ctx) _ = json.Unmarshal(b, &c) } - return c.TriggerKind == 1 + return c.TriggerKind == 1 } // shouldSuppressForChatTriggerEOL returns true when a chat trigger like ">" follows ?, !, :, or ; at EOL. -func (s *Server) shouldSuppressForChatTriggerEOL(current string, p CompletionParams) bool { +func (s *Server) shouldSuppressForChatTriggerEOL(current string, p CompletionParams) bool { t := strings.TrimRight(current, " \t") suffix, prefixes, _ := s.chatConfig() if suffix == "" { return false } - if strings.HasSuffix(t, suffix) { + if strings.HasSuffix(t, suffix) { if len(t) < len(suffix)+1 { return false } - prev := string(t[len(t)-len(suffix)-1]) - for _, pf := range prefixes { + prev := string(t[len(t)-len(suffix)-1]) + for _, pf := range prefixes { if prev == pf { logging.Logf("lsp ", "completion skip=chat-trigger-eol uri=%s line=%d", p.TextDocument.URI, p.Position.Line) return true } } } - return false + return false } // prefixHeuristicAllows applies minimal prefix rules unless inlinePrompt or structural triggers apply. -func (s *Server) prefixHeuristicAllows(inlinePrompt bool, current string, p CompletionParams, manualInvoke bool) bool { +func (s *Server) prefixHeuristicAllows(inlinePrompt bool, current string, p CompletionParams, manualInvoke bool) bool { // Determine the effective cursor index within current line, clamped, and // skip over trailing spaces/tabs to support cases like "type Matrix| ". idx := p.Position.Character if idx > len(current) { idx = len(current) } - allowNoPrefix := inlinePrompt - if idx > 0 { + allowNoPrefix := inlinePrompt + if idx > 0 { ch := current[idx-1] if ch == '.' || ch == ':' || ch == '/' || ch == '_' || ch == ')' { allowNoPrefix = true } } - if allowNoPrefix { + if allowNoPrefix { return true } // Walk left over whitespace @@ -5410,10 +5410,10 @@ func (s *Server) prefixHeuristicAllows(inlinePrompt bool, current string, p Comp } // tryProviderNativeCompletion attempts provider-native completion and returns items when successful. -func (s *Server) tryProviderNativeCompletion(current string, p CompletionParams, above, below, funcCtx, docStr string, hasExtra bool, extraText string, inParams bool) ([]CompletionItem, bool) { +func (s *Server) tryProviderNativeCompletion(current string, p CompletionParams, above, below, funcCtx, docStr string, hasExtra bool, extraText string, inParams bool) ([]CompletionItem, bool) { client := s.currentLLMClient() cc, ok := client.(llm.CodeCompleter) - if !ok { + if !ok { return nil, false } before, after := s.docBeforeAfter(p.TextDocument.URI, p.Position) @@ -5484,9 +5484,9 @@ func (s *Server) tryProviderNativeCompletion(current string, p CompletionParams, // waitForDebounce sleeps until there has been no input activity for at least // completionDebounce. If debounce is zero or ctx is done, it returns promptly. -func (s *Server) waitForDebounce(ctx context.Context) { +func (s *Server) waitForDebounce(ctx context.Context) { d := s.completionDebounce() - if d <= 0 { + if d <= 0 { return } for { @@ -5514,9 +5514,9 @@ func (s *Server) waitForDebounce(ctx context.Context) { +func (s *Server) waitForThrottle(ctx context.Context) bool { interval := s.completionThrottle() - if interval <= 0 { + if interval <= 0 { return true } var wait time.Duration @@ -5545,7 +5545,7 @@ func (s *Server) waitForThrottle(ctx context.Context) bool { +func (s *Server) buildCompletionMessages(inlinePrompt, hasExtra bool, extraText string, inParams bool, p CompletionParams, above, current, below, funcCtx string) []llm.Message { vars := map[string]string{ "file": p.TextDocument.URI, "function": funcCtx, @@ -5561,10 +5561,10 @@ func (s *Server) buildCompletionMessages(inlinePrompt, hasExtra bool, extraText sys = cfg.PromptCompletionSystemParams userTpl = cfg.PromptCompletionUserParams } - if inlinePrompt && strings.TrimSpace(cfg.PromptCompletionSystemInline) != "" { + if inlinePrompt && strings.TrimSpace(cfg.PromptCompletionSystemInline) != "" { sys = cfg.PromptCompletionSystemInline } - user := renderTemplate(userTpl, vars) + user := renderTemplate(userTpl, vars) messages := []llm.Message{{Role: "system", Content: sys}, {Role: "user", Content: user}} if hasExtra && strings.TrimSpace(extraText) != "" { extra := renderTemplate(cfg.PromptCompletionExtraHeader, map[string]string{"context": extraText}) @@ -5573,30 +5573,30 @@ func (s *Server) buildCompletionMessages(inlinePrompt, hasExtra bool, extraText } messages = append(messages, llm.Message{Role: "user", Content: extra}) } - return messages + return messages } // postProcessCompletion normalizes and deduplicates completion text and applies indentation rules. -func (s *Server) postProcessCompletion(text string, leftOfCursor string, currentLine string) string { +func (s *Server) postProcessCompletion(text string, leftOfCursor string, currentLine string) string { cleaned := stripCodeFences(text) if cleaned != "" && strings.ContainsRune(cleaned, '`') { if inline := stripInlineCodeSpan(cleaned); strings.TrimSpace(inline) != "" { cleaned = inline } } - if cleaned != "" { + if cleaned != "" { cleaned = stripDuplicateAssignmentPrefix(leftOfCursor, cleaned) } - if cleaned != "" { + if cleaned != "" { cleaned = stripDuplicateGeneralPrefix(leftOfCursor, cleaned) } - _, _, openChar, closeChar := s.inlineMarkers() - if cleaned != "" && hasDoubleOpenTrigger(currentLine, openChar, closeChar) { + _, _, openChar, closeChar := s.inlineMarkers() + if cleaned != "" && hasDoubleOpenTrigger(currentLine, openChar, closeChar) { if indent := leadingIndent(currentLine); indent != "" { cleaned = applyIndent(indent, cleaned) } } - return cleaned + return cleaned } @@ -5696,7 +5696,11 @@ func (s *Server) detectAndHandleChat(uri string) { _, _, openChar, closeChar := s.inlineMarkers() for i, raw := range d.lines { if lineHasInlinePrompt(raw, openChar, closeChar) { - continue + if s.currentLLMClient() != nil { + pos := Position{Line: i, Character: len(raw)} + go s.runInlinePrompt(uri, pos) + } + continue } // Find last non-space character index j := len(raw) - 1 @@ -5812,6 +5816,47 @@ func (s *Server) applyChatEdits(uri string, lineIdx int, lastNonSpace int, remov s.clientApplyEdit("Hexai: insert chat response", we) } +func (s *Server) runInlinePrompt(uri string, pos Position) { + if s.currentLLMClient() == nil { + return + } + d := s.getDocument(uri) + if d == nil || pos.Line < 0 || pos.Line >= len(d.lines) { + return + } + line := d.lines[pos.Line] + _, _, openChar, closeChar := s.inlineMarkers() + if !lineHasInlinePrompt(line, openChar, closeChar) { + return + } + p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: uri}, Position: Position{Line: pos.Line, Character: len(line)}} + p.Context = map[string]int{"triggerKind": 1} + above, current, below, funcCtx := s.lineContext(uri, p.Position) + docStr := s.buildDocString(p, above, current, below, funcCtx) + newFunc := s.isDefiningNewFunction(uri, p.Position) + extra, hasExtra := s.buildAdditionalContext(newFunc, uri, p.Position) + items, ok := s.tryLLMCompletion(p, above, current, below, funcCtx, docStr, hasExtra, extra) + if !ok || len(items) == 0 { + return + } + s.applyInlineCompletion(uri, items[0]) +} + +func (s *Server) applyInlineCompletion(uri string, item CompletionItem) { + var edits []TextEdit + if len(item.AdditionalTextEdits) > 0 { + edits = append(edits, item.AdditionalTextEdits...) + } + if item.TextEdit != nil { + edits = append(edits, *item.TextEdit) + } + if len(edits) == 0 { + return + } + we := WorkspaceEdit{Changes: map[string][]TextEdit{uri: edits}} + s.clientApplyEdit("Hexai: inline prompt", we) +} + // buildChatHistory walks upwards from the current line to collect the most recent // Q/A pairs in the in-editor transcript. Returns messages ending with current prompt. func (s *Server) buildChatHistory(uri string, lineIdx int, currentPrompt string) []llm.Message { @@ -6068,7 +6113,7 @@ import ( ) // llmRequestOpts builds request options from server settings. -func (s *Server) llmRequestOpts() []llm.RequestOption { +func (s *Server) llmRequestOpts() []llm.RequestOption { maxTokens := s.maxTokens() client := s.currentLLMClient() tempPtr := s.codingTemperature() @@ -6084,63 +6129,63 @@ func (s *Server) llmRequestOpts() []llm.RequestOption opts = append(opts, llm.WithTemperature(temp)) } - return opts + return opts } // small helpers for LLM traffic stats -func (s *Server) incSentCounters(n int) { +func (s *Server) incSentCounters(n int) { s.mu.Lock() s.llmReqTotal++ s.llmSentBytesTotal += int64(n) s.mu.Unlock() } -func (s *Server) incRecvCounters(n int) { +func (s *Server) incRecvCounters(n int) { s.mu.Lock() s.llmRespTotal++ s.llmRespBytesTotal += int64(n) s.mu.Unlock() } -func (s *Server) logLLMStats() { +func (s *Server) logLLMStats() { s.mu.RLock() avgSent := int64(0) - if s.llmReqTotal > 0 { + if s.llmReqTotal > 0 { avgSent = s.llmSentBytesTotal / s.llmReqTotal } - avgRecv := int64(0) - if s.llmRespTotal > 0 { + avgRecv := int64(0) + if s.llmRespTotal > 0 { avgRecv = s.llmRespBytesTotal / s.llmRespTotal } - reqs, sentTot, recvTot := s.llmReqTotal, s.llmSentBytesTotal, s.llmRespBytesTotal + reqs, sentTot, recvTot := s.llmReqTotal, s.llmSentBytesTotal, s.llmRespBytesTotal s.mu.RUnlock() mins := time.Since(s.startTime).Minutes() if mins <= 0 { mins = 0.001 } - rpmLocal := float64(reqs) / mins + rpmLocal := float64(reqs) / mins sentPerMin := float64(sentTot) / mins recvPerMin := float64(recvTot) / mins // Log local process counters logging.Logf("lsp ", "llm stats (local) reqs=%d avg_sent=%d avg_recv=%d sent_total=%d recv_total=%d rpm=%.2f sent_per_min=%.0f recv_per_min=%.0f", reqs, avgSent, avgRecv, sentTot, recvTot, rpmLocal, sentPerMin, recvPerMin) // Global snapshot for tmux status snap, err := stats.TakeSnapshot() - if err == nil { - if client := s.currentLLMClient(); client != nil { + if err == nil { + if client := s.currentLLMClient(); client != nil { provider := client.Name() model := client.DefaultModel() // Per-scope rpm estimated from window scopeReqs := int64(0) - if pe, ok := snap.Providers[provider]; ok { - if mc, ok2 := pe.Models[model]; ok2 { + if pe, ok := snap.Providers[provider]; ok { + if mc, ok2 := pe.Models[model]; ok2 { scopeReqs = mc.Reqs } } - minsWin := snap.Window.Minutes() + minsWin := snap.Window.Minutes() if minsWin <= 0 { minsWin = 0.001 } - scopeRPM := float64(scopeReqs) / minsWin + scopeRPM := float64(scopeReqs) / minsWin status := tmx.FormatGlobalStatusColored(snap.Global.Reqs, snap.RPM, snap.Global.Sent, snap.Global.Recv, provider, model, scopeRPM, scopeReqs, snap.Window) _ = tmx.SetStatus(status) } @@ -6148,8 +6193,8 @@ func (s *Server) logLLMStats() { } // Completion prompt builders and filters -func inParamList(current string, cursor int) bool { - if !strings.Contains(current, "func ") { +func inParamList(current string, cursor int) bool { + if !strings.Contains(current, "func ") { return false } open := strings.Index(current, "(") @@ -6158,78 +6203,78 @@ func inParamList(current string, cursor int) bool } // renderTemplate performs simple {{var}} replacement in a template string. -func renderTemplate(t string, vars map[string]string) string { return textutil.RenderTemplate(t, vars) } +func renderTemplate(t string, vars map[string]string) string { return textutil.RenderTemplate(t, vars) } -func computeTextEditAndFilter(cleaned string, inParams bool, current string, p CompletionParams) (*TextEdit, string) { - if inParams { +func computeTextEditAndFilter(cleaned string, inParams bool, current string, p CompletionParams) (*TextEdit, string) { + if inParams { open := strings.Index(current, "(") close := strings.Index(current, ")") - if open >= 0 { + if open >= 0 { left := open + 1 right := len(current) - if close >= 0 && close >= left { + if close >= 0 && close >= left { right = close } - if p.Position.Character < right { + if p.Position.Character < right { right = p.Position.Character } - te := &TextEdit{Range: Range{Start: Position{Line: p.Position.Line, Character: left}, End: Position{Line: p.Position.Line, Character: right}}, NewText: cleaned} + te := &TextEdit{Range: Range{Start: Position{Line: p.Position.Line, Character: left}, End: Position{Line: p.Position.Line, Character: right}}, NewText: cleaned} var filter string - if left >= 0 && right >= left && right <= len(current) { + if left >= 0 && right >= left && right <= len(current) { filter = strings.TrimLeft(current[left:right], " \t") } - return te, filter + return te, filter } } - startChar := computeWordStart(current, p.Position.Character) + startChar := computeWordStart(current, p.Position.Character) te := &TextEdit{Range: Range{Start: Position{Line: p.Position.Line, Character: startChar}, End: Position{Line: p.Position.Line, Character: p.Position.Character}}, NewText: cleaned} filter := strings.TrimLeft(current[startChar:p.Position.Character], " \t") return te, filter } -func computeWordStart(current string, at int) int { +func computeWordStart(current string, at int) int { if at > len(current) { at = len(current) } - for at > 0 { + for at > 0 { ch := current[at-1] if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' { at-- continue } - break + break } - return at + return at } -func isIdentChar(ch byte) bool { +func isIdentChar(ch byte) bool { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_' } // chatWithStats wraps llmClient.Chat to increment counters and emit a tmux heartbeat. -func (s *Server) chatWithStats(ctx context.Context, msgs []llm.Message, opts ...llm.RequestOption) (string, error) { +func (s *Server) chatWithStats(ctx context.Context, msgs []llm.Message, opts ...llm.RequestOption) (string, error) { // Count bytes sent sent := 0 for _, m := range msgs { sent += len(m.Content) } - s.incSentCounters(sent) + s.incSentCounters(sent) // Debounce/throttle if configured (reuse completion gates) s.waitForDebounce(ctx) if !s.waitForThrottle(ctx) { return "", context.Canceled } // Perform request - client := s.currentLLMClient() + client := s.currentLLMClient() if client == nil { return "", fmt.Errorf("llm client unavailable") } - txt, err := client.Chat(ctx, msgs, opts...) + txt, err := client.Chat(ctx, msgs, opts...) if err != nil { s.logLLMStats() return "", err } - s.incRecvCounters(len(txt)) + s.incRecvCounters(len(txt)) // Update global stats cache _ = stats.Update(ctx, client.Name(), client.DefaultModel(), sent, len(txt)) s.logLLMStats() @@ -6238,23 +6283,23 @@ func (s *Server) chatWithStats(ctx context.Context, msgs []llm.Message, opts ... // Inline prompt utilities -func lineHasInlinePrompt(line string, open, close byte) bool { - if _, _, _, ok := findStrictInlineTag(line, open, close); ok { +func lineHasInlinePrompt(line string, open, close byte) bool { + if _, _, _, ok := findStrictInlineTag(line, open, close); ok { return true } - return hasDoubleOpenTrigger(line, open, close) + return hasDoubleOpenTrigger(line, open, close) } -func leadingIndent(line string) string { +func leadingIndent(line string) string { i := 0 - for i < len(line) { + for i < len(line) { if line[i] == ' ' || line[i] == '\t' { i++ continue } - break + break } - if i == 0 { + if i == 0 { return "" } return line[:i] @@ -6269,10 +6314,10 @@ func applyIndent(indent, suggestion string) string if strings.TrimSpace(ln) == "" { continue } - if strings.HasPrefix(ln, indent) { + if strings.HasPrefix(ln, indent) { continue } - lines[i] = indent + ln + lines[i] = indent + ln } return strings.Join(lines, "\n") } @@ -6282,36 +6327,36 @@ func applyIndent(indent, suggestion string) string // findStrictInlineTag finds >text> (configurable), with no space after the first // opening marker and no space immediately before the closing marker. Returns the // text between markers, the start index, the end index just after closing, and ok. -func findStrictInlineTag(line string, open, close byte) (string, int, int, bool) { +func findStrictInlineTag(line string, open, close byte) (string, int, int, bool) { pos := 0 - for pos < len(line) { + for pos < len(line) { // find opening marker j := strings.IndexByte(line[pos:], open) - if j < 0 { + if j < 0 { return "", 0, 0, false } - j += pos + j += pos // ensure single open (not double) and non-space after - if j+1 >= len(line) || line[j+1] == open || line[j+1] == ' ' { + if j+1 >= len(line) || line[j+1] == open || line[j+1] == ' ' { pos = j + 1 continue } // find closing marker - k := strings.IndexByte(line[j+1:], close) + k := strings.IndexByte(line[j+1:], close) if k < 0 { return "", 0, 0, false } - closeIdx := j + 1 + k + closeIdx := j + 1 + k if closeIdx-1 < 0 || line[closeIdx-1] == ' ' { pos = closeIdx + 1 continue } - inner := strings.TrimSpace(line[j+1 : closeIdx]) + inner := strings.TrimSpace(line[j+1 : closeIdx]) if inner == "" { pos = closeIdx + 1 continue } - end := closeIdx + 1 + end := closeIdx + 1 return inner, j, end, true } return "", 0, 0, false @@ -6320,14 +6365,14 @@ func findStrictInlineTag(line string, open, close byte) (string, int, int, bool) // isBareDoubleSemicolon reports whether the line contains a standalone // double-semicolon marker with no inline content (";;" possibly with only // whitespace after it). It explicitly excludes the valid form ";;text;". -func isBareDoubleOpen(line string, open, close byte) bool { +func isBareDoubleOpen(line string, open, close byte) bool { t := strings.TrimSpace(line) // check for double-open pattern dbl := string([]byte{open, open}) - if !strings.Contains(t, dbl) { + if !strings.Contains(t, dbl) { return false } - if hasDoubleOpenTrigger(t, open, close) { + if hasDoubleOpenTrigger(t, open, close) { return false } if strings.HasPrefix(t, dbl) { @@ -6340,7 +6385,7 @@ func isBareDoubleOpen(line string, open, close byte) bool { +func stripDuplicateAssignmentPrefix(prefixBeforeCursor, suggestion string) string { s2 := strings.TrimLeft(suggestion, " \t") // Prefer := if present at end of prefix if idx := strings.LastIndex(prefixBeforeCursor, ":="); idx >= 0 && idx+2 <= len(prefixBeforeCursor) { @@ -6358,7 +6403,7 @@ func stripDuplicateAssignmentPrefix(prefixBeforeCursor, suggestion string) strin } } // Fallback to plain '=' if present - if idx := strings.LastIndex(prefixBeforeCursor, "="); idx >= 0 { + if idx := strings.LastIndex(prefixBeforeCursor, "="); idx >= 0 { if !(idx > 0 && prefixBeforeCursor[idx-1] == ':') { // not := tail := prefixBeforeCursor[idx+1:] if strings.TrimSpace(tail) == "" { @@ -6374,40 +6419,40 @@ func stripDuplicateAssignmentPrefix(prefixBeforeCursor, suggestion string) strin } } } - return suggestion + return suggestion } // stripDuplicateGeneralPrefix removes any already-typed prefix that the model repeated. -func stripDuplicateGeneralPrefix(prefixBeforeCursor, suggestion string) string { +func stripDuplicateGeneralPrefix(prefixBeforeCursor, suggestion string) string { if suggestion == "" { return suggestion } - s := strings.TrimLeft(suggestion, " \t") + s := strings.TrimLeft(suggestion, " \t") p := strings.TrimRight(prefixBeforeCursor, " \t") - if p != "" && strings.HasPrefix(s, p) { + if p != "" && strings.HasPrefix(s, p) { return strings.TrimLeft(s[len(p):], " \t") } - for k := len(p) - 1; k > 0; k-- { - if !isIdentBoundary(p[k-1]) { + for k := len(p) - 1; k > 0; k-- { + if !isIdentBoundary(p[k-1]) { continue } - suf := strings.TrimLeft(p[k:], " \t") + suf := strings.TrimLeft(p[k:], " \t") if suf == "" { continue } - if strings.HasPrefix(s, suf) { + if strings.HasPrefix(s, suf) { return strings.TrimLeft(s[len(suf):], " \t") } } - return suggestion + return suggestion } -func isIdentBoundary(ch byte) bool { +func isIdentBoundary(ch byte) bool { return !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_') } // stripCodeFences removes surrounding Markdown code fences from a model response. -func stripCodeFences(s string) string { return textutil.StripCodeFences(s) } +func stripCodeFences(s string) string { return textutil.StripCodeFences(s) } // stripInlineCodeSpan returns the contents of the first inline backtick code span if present. func stripInlineCodeSpan(s string) string { @@ -6419,7 +6464,7 @@ func stripInlineCodeSpan(s string) string { if i < 0 { return t } - jrel := strings.IndexByte(t[i+1:], '`') + jrel := strings.IndexByte(t[i+1:], '`') if jrel < 0 { return t } @@ -6428,25 +6473,25 @@ func stripInlineCodeSpan(s string) string { } // labelForCompletion picks a short, readable label for the completion list. -func labelForCompletion(cleaned, filter string) string { +func labelForCompletion(cleaned, filter string) string { label := trimLen(firstLine(cleaned)) - if filter != "" && !strings.HasPrefix(strings.ToLower(label), strings.ToLower(filter)) { + if filter != "" && !strings.HasPrefix(strings.ToLower(label), strings.ToLower(filter)) { return filter } - return label + return label } // extractRangeText returns the exact text within the given document range. func extractRangeText(d *document, r Range) string { - if r.Start.Line == r.End.Line { + if r.Start.Line == r.End.Line { line := d.lines[r.Start.Line] if r.Start.Character < 0 { r.Start.Character = 0 } - if r.End.Character > len(line) { + if r.End.Character > len(line) { r.End.Character = len(line) } - if r.Start.Character > r.End.Character { + if r.Start.Character > r.End.Character { return "" } return line[r.Start.Character:r.End.Character] @@ -6482,61 +6527,61 @@ func extractRangeText(d *document, r Range) string } // collectPromptRemovalEdits returns edits to remove all inline prompt markers. -func (s *Server) collectPromptRemovalEdits(uri string) []TextEdit { +func (s *Server) collectPromptRemovalEdits(uri string) []TextEdit { d := s.getDocument(uri) if d == nil || len(d.lines) == 0 { return nil } - var edits []TextEdit + var edits []TextEdit _, _, openChar, closeChar := s.inlineMarkers() - for i, line := range d.lines { + for i, line := range d.lines { edits = append(edits, promptRemovalEditsForLine(line, i, openChar, closeChar)...) } - return edits + return edits } -func promptRemovalEditsForLine(line string, lineNum int, open, close byte) []TextEdit { - if hasDoubleOpenTrigger(line, open, close) { +func promptRemovalEditsForLine(line string, lineNum int, open, close byte) []TextEdit { + if hasDoubleOpenTrigger(line, open, close) { return []TextEdit{{Range: Range{Start: Position{Line: lineNum, Character: 0}, End: Position{Line: lineNum, Character: len(line)}}, NewText: ""}} } return collectSemicolonMarkers(line, lineNum, open, close) } -func hasDoubleOpenTrigger(line string, open, close byte) bool { +func hasDoubleOpenTrigger(line string, open, close byte) bool { pos := 0 - for pos < len(line) { + for pos < len(line) { // look for double-open sequence dbl := string([]byte{open, open}) j := strings.Index(line[pos:], dbl) - if j < 0 { + if j < 0 { return false } - j += pos + j += pos contentStart := j + len(dbl) - if contentStart >= len(line) { + if contentStart >= len(line) { return false } - first := line[contentStart] - if first == ' ' || first == open { + first := line[contentStart] + if first == ' ' || first == open { pos = contentStart + 1 continue } // find closing - k := strings.IndexByte(line[contentStart+1:], close) + k := strings.IndexByte(line[contentStart+1:], close) if k < 0 { return false } - closeIdx := contentStart + 1 + k + closeIdx := contentStart + 1 + k if closeIdx-1 >= 0 && line[closeIdx-1] == ' ' { pos = closeIdx + 1 continue } - return true + return true } return false } -func collectSemicolonMarkers(line string, lineNum int, open, close byte) []TextEdit { +func collectSemicolonMarkers(line string, lineNum int, open, close byte) []TextEdit { var edits []TextEdit startSemi := 0 for startSemi < len(line) { @@ -6573,7 +6618,7 @@ func collectSemicolonMarkers(line string, lineNum int, open, close byte) []TextE edits = append(edits, TextEdit{Range: Range{Start: Position{Line: lineNum, Character: j}, End: Position{Line: lineNum, Character: endChar}}, NewText: ""}) startSemi = endChar } - return edits + return edits } @@ -6684,7 +6729,7 @@ type CustomAction struct { User string // if set, use this user template } -func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) *Server { +func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) *Server { s := &Server{in: bufio.NewReader(r), out: w, logger: logger, docs: make(map[string]*document), logContext: opts.LogContext, configStore: opts.ConfigStore} s.startTime = time.Now() s.compCache = make(map[string]string) @@ -6703,21 +6748,21 @@ func NewServer(r io.Reader, w io.Writer, logger *log.Logger, opts ServerOptions) "codeAction/resolve": s.handleCodeActionResolve, "workspace/executeCommand": s.handleExecuteCommand, } - return s + return s } -func (s *Server) applyOptions(opts ServerOptions) { +func (s *Server) applyOptions(opts ServerOptions) { s.mu.Lock() defer s.mu.Unlock() s.logContext = opts.LogContext if opts.ConfigStore != nil { s.configStore = opts.ConfigStore } - if opts.Config != nil { + if opts.Config != nil { s.cfg = *opts.Config - } else if opts.ConfigStore != nil { + } else if opts.ConfigStore != nil { s.cfg = opts.ConfigStore.Snapshot() - } else { + } else { s.cfg = appconfig.App{} // populate from legacy ServerOptions fields s.cfg.MaxTokens = opts.MaxTokens @@ -6764,7 +6809,7 @@ func (s *Server) applyOptions(opts ServerOptions) { } } } - s.llmClient = opts.Client + s.llmClient = opts.Client } // ApplyOptions updates the server's configuration at runtime. @@ -6772,32 +6817,32 @@ func (s *Server) ApplyOptions(opts ServerOptions) { s.applyOptions(opts) } -func (s *Server) currentLLMClient() llm.Client { +func (s *Server) currentLLMClient() llm.Client { s.mu.RLock() defer s.mu.RUnlock() return s.llmClient } -func (s *Server) currentConfig() appconfig.App { +func (s *Server) currentConfig() appconfig.App { if s.configStore != nil { return s.configStore.Snapshot() } - s.mu.RLock() + s.mu.RLock() defer s.mu.RUnlock() return s.cfg } -func (s *Server) maxTokens() int { +func (s *Server) maxTokens() int { cfg := s.currentConfig() - if cfg.MaxTokens <= 0 { + if cfg.MaxTokens <= 0 { return 500 } return cfg.MaxTokens } -func (s *Server) contextMode() string { +func (s *Server) contextMode() string { mode := strings.TrimSpace(s.currentConfig().ContextMode) - if mode == "" { + if mode == "" { return "file-on-new-func" } return mode @@ -6827,7 +6872,7 @@ func (s *Server) triggerCharacters() []string { return append([]string{}, cfg.TriggerCharacters...) } -func (s *Server) codingTemperature() *float64 { +func (s *Server) codingTemperature() *float64 { cfg := s.currentConfig() return cfg.CodingTemperature } @@ -6836,47 +6881,47 @@ func (s *Server) manualInvokeMinPrefix() int { return s.currentConfig().ManualInvokeMinPrefix } -func (s *Server) completionDebounce() time.Duration { +func (s *Server) completionDebounce() time.Duration { cfg := s.currentConfig() - if cfg.CompletionDebounceMs <= 0 { + if cfg.CompletionDebounceMs <= 0 { return 0 } return time.Duration(cfg.CompletionDebounceMs) * time.Millisecond } -func (s *Server) completionThrottle() time.Duration { +func (s *Server) completionThrottle() time.Duration { cfg := s.currentConfig() - if cfg.CompletionThrottleMs <= 0 { + if cfg.CompletionThrottleMs <= 0 { return 0 } return time.Duration(cfg.CompletionThrottleMs) * time.Millisecond } -func (s *Server) inlineMarkers() (open string, close string, openChar byte, closeChar byte) { +func (s *Server) inlineMarkers() (open string, close string, openChar byte, closeChar byte) { cfg := s.currentConfig() open = strings.TrimSpace(cfg.InlineOpen) if open == "" { open = ">" } - close = strings.TrimSpace(cfg.InlineClose) + close = strings.TrimSpace(cfg.InlineClose) if close == "" { close = ">" } - openChar = '>' - if len(open) > 0 { + openChar = '>' + if len(open) > 0 { openChar = open[0] } - closeChar = '>' - if len(close) > 0 { + closeChar = '>' + if len(close) > 0 { closeChar = close[0] } - return open, close, openChar, closeChar + return open, close, openChar, closeChar } -func (s *Server) chatConfig() (suffix string, prefixes []string, suffixChar byte) { +func (s *Server) chatConfig() (suffix string, prefixes []string, suffixChar byte) { cfg := s.currentConfig() suffix = cfg.ChatSuffix - if suffix != "" { + if suffix != "" { suffix = strings.TrimSpace(suffix) if suffix == "" { suffix = ">" @@ -6884,16 +6929,16 @@ func (s *Server) chatConfig() (suffix string, prefixes []string, suffixChar byte } else { suffix = "" } - if len(cfg.ChatPrefixes) == 0 { + if len(cfg.ChatPrefixes) == 0 { prefixes = []string{"?", "!", ":", ";"} - } else { + } else { prefixes = append([]string{}, cfg.ChatPrefixes...) } - suffixChar = '>' - if len(suffix) > 0 { + suffixChar = '>' + if len(suffix) > 0 { suffixChar = suffix[0] } - return suffix, prefixes, suffixChar + return suffix, prefixes, suffixChar } func (s *Server) promptSet() appconfig.App { @@ -6996,7 +7041,7 @@ func (s *Server) readMessage() ([]byte, error) { return buf, nil } -func (s *Server) writeMessage(v any) { +func (s *Server) writeMessage(v any) { s.outMu.Lock() defer s.outMu.Unlock() @@ -7005,12 +7050,12 @@ func (s *Server) writeMessage(v any) { logging.Logf("lsp ", "marshal error: %v", err) return } - header := fmt.Sprintf("Content-Length: %d\r\n\r\n", len(data)) + header := fmt.Sprintf("Content-Length: %d\r\n\r\n", len(data)) if _, err := io.WriteString(s.out, header); err != nil { logging.Logf("lsp ", "write header error: %v", err) return } - if _, err := s.out.Write(data); err != nil { + if _, err := s.out.Write(data); err != nil { logging.Logf("lsp ", "write body error: %v", err) return } @@ -7224,9 +7269,9 @@ import ( "golang.org/x/sys/unix" ) -func tryLockFile(fd uintptr) error { - if err := unix.Flock(int(fd), unix.LOCK_EX|unix.LOCK_NB); err != nil { - if errors.Is(err, unix.EWOULDBLOCK) { +func tryLockFile(fd uintptr) error { + if err := unix.Flock(int(fd), unix.LOCK_EX|unix.LOCK_NB); err != nil { + if errors.Is(err, unix.EWOULDBLOCK) { return errLockWouldBlock } return err @@ -7270,18 +7315,18 @@ var windowSeconds int64 = int64(defaultWindow.Seconds()) var errLockWouldBlock = errors.New("stats: lock would block") // SetWindow sets the sliding window used for pruning and aggregation. -func SetWindow(d time.Duration) { +func SetWindow(d time.Duration) { if d < time.Second { d = time.Second } - if d > 24*time.Hour { + if d > 24*time.Hour { d = 24 * time.Hour } - atomic.StoreInt64(&windowSeconds, int64(d.Seconds())) + atomic.StoreInt64(&windowSeconds, int64(d.Seconds())) } // Window returns the current sliding window. -func Window() time.Duration { return time.Duration(atomic.LoadInt64(&windowSeconds)) * time.Second } +func Window() time.Duration { return time.Duration(atomic.LoadInt64(&windowSeconds)) * time.Second } // Event represents a single request/response with sizes. type Event struct { @@ -7316,108 +7361,108 @@ type Snapshot struct { } // Update appends one event and prunes old entries under lock. -func Update(ctx context.Context, provider, model string, sentBytes, recvBytes int) error { +func Update(ctx context.Context, provider, model string, sentBytes, recvBytes int) error { dir, err := CacheDir() if err != nil { return err } - if err := os.MkdirAll(dir, 0o755); err != nil { + if err := os.MkdirAll(dir, 0o755); err != nil { return err } - lockPath := filepath.Join(dir, lockFileName) + lockPath := filepath.Join(dir, lockFileName) f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0o600) if err != nil { return err } - defer f.Close() + defer f.Close() unlock, err := acquireFileLock(ctx, f) if err != nil { return err } - defer func() { _ = unlock() }() + defer func() { _ = unlock() }() // Read existing file (if any) - path := filepath.Join(dir, fileName) + path := filepath.Join(dir, fileName) var sf File - if b, rerr := os.ReadFile(path); rerr == nil { + if b, rerr := os.ReadFile(path); rerr == nil { _ = json.Unmarshal(b, &sf) } - if sf.Version != fileVersion { + if sf.Version != fileVersion { sf = File{Version: fileVersion} } - now := time.Now() + now := time.Now() win := Window() sf.WindowSeconds = int(win.Seconds()) // Append event sf.Events = append(sf.Events, Event{TS: now, Provider: provider, Model: model, Sent: int64(sentBytes), Recv: int64(recvBytes)}) // Prune old cutoff := now.Add(-win) - if len(sf.Events) > 0 { + if len(sf.Events) > 0 { // Find first >= cutoff i := 0 - for ; i < len(sf.Events); i++ { - if !sf.Events[i].TS.Before(cutoff) { + for ; i < len(sf.Events); i++ { + if !sf.Events[i].TS.Before(cutoff) { break } } - if i > 0 { + if i > 0 { sf.Events = append([]Event(nil), sf.Events[i:]...) } } - sf.UpdatedAt = now + sf.UpdatedAt = now // Write atomically tmp, err := os.CreateTemp(dir, fileName+".tmp.") if err != nil { return err } - enc := json.NewEncoder(tmp) + enc := json.NewEncoder(tmp) enc.SetEscapeHTML(false) if err := enc.Encode(&sf); err != nil { tmp.Close() os.Remove(tmp.Name()) return err } - if err := tmp.Sync(); err != nil { + if err := tmp.Sync(); err != nil { tmp.Close() os.Remove(tmp.Name()) return err } - if err := tmp.Close(); err != nil { + if err := tmp.Close(); err != nil { os.Remove(tmp.Name()) return err } - if err := os.Rename(tmp.Name(), path); err != nil { + if err := os.Rename(tmp.Name(), path); err != nil { os.Remove(tmp.Name()) return err } - return nil + return nil } -func acquireFileLock(ctx context.Context, f *os.File) (func() error, error) { +func acquireFileLock(ctx context.Context, f *os.File) (func() error, error) { fd := f.Fd() - for { + for { err := tryLockFile(fd) - if err == nil { - return func() error { return unlockFile(fd) }, nil + if err == nil { + return func() error { return unlockFile(fd) }, nil } - if errors.Is(err, errLockWouldBlock) { + if errors.Is(err, errLockWouldBlock) { select { case <-ctx.Done(): return nil, ctx.Err() - case <-time.After(5 * time.Millisecond): + case <-time.After(5 * time.Millisecond): } - continue + continue } return nil, err } } // Snapshot reads and aggregates events within the configured window. -func TakeSnapshot() (Snapshot, error) { +func TakeSnapshot() (Snapshot, error) { dir, err := CacheDir() if err != nil { return Snapshot{}, err } - path := filepath.Join(dir, fileName) + path := filepath.Join(dir, fileName) b, err := os.ReadFile(path) if err != nil { if errors.Is(err, os.ErrNotExist) { @@ -7425,30 +7470,30 @@ func TakeSnapshot() (Snapshot, error) { } return Snapshot{}, err } - var sf File + var sf File if err := json.Unmarshal(b, &sf); err != nil { return Snapshot{}, err } - win := time.Duration(sf.WindowSeconds) * time.Second + win := time.Duration(sf.WindowSeconds) * time.Second if win <= 0 { win = Window() - } else { + } else { SetWindow(win) // align process with file window if changed elsewhere } - cutoff := time.Now().Add(-win) + cutoff := time.Now().Add(-win) snap := Snapshot{Providers: make(map[string]ProviderEntry), Window: win} - for _, ev := range sf.Events { + for _, ev := range sf.Events { if ev.TS.Before(cutoff) { continue } - snap.Global.Reqs++ + snap.Global.Reqs++ snap.Global.Sent += ev.Sent snap.Global.Recv += ev.Recv pe := snap.Providers[ev.Provider] - if pe.Models == nil { + if pe.Models == nil { pe.Models = make(map[string]Counters) } - pe.Totals.Reqs++ + pe.Totals.Reqs++ pe.Totals.Sent += ev.Sent pe.Totals.Recv += ev.Recv mc := pe.Models[ev.Model] @@ -7458,37 +7503,37 @@ func TakeSnapshot() (Snapshot, error) { pe.Models[ev.Model] = mc snap.Providers[ev.Provider] = pe } - mins := win.Minutes() + mins := win.Minutes() if mins <= 0 { mins = 0.001 } - snap.RPM = float64(snap.Global.Reqs) / mins + snap.RPM = float64(snap.Global.Reqs) / mins return snap, nil } // CacheDir resolves the cache directory for stats. -func CacheDir() (string, error) { +func CacheDir() (string, error) { if x := os.Getenv("XDG_CACHE_HOME"); stringsTrim(x) != "" { return filepath.Join(x, "hexai"), nil } - home, err := os.UserHomeDir() + home, err := os.UserHomeDir() if err != nil { return "", fmt.Errorf("cannot resolve home: %w", err) } - return filepath.Join(home, ".cache", "hexai"), nil + return filepath.Join(home, ".cache", "hexai"), nil } // stringsTrim is a tiny helper to avoid importing strings everywhere here. -func stringsTrim(s string) string { +func stringsTrim(s string) string { i := 0 j := len(s) for i < j && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r') { i++ } - for j > i && (s[j-1] == ' ' || s[j-1] == '\t' || s[j-1] == '\n' || s[j-1] == '\r') { + for j > i && (s[j-1] == ' ' || s[j-1] == '\t' || s[j-1] == '\n' || s[j-1] == '\r') { j-- } - if i == 0 && j == len(s) { + if i == 0 && j == len(s) { return s } return s[i:j] @@ -7534,24 +7579,24 @@ import "fmt" // HumanBytes renders n in a short human-friendly form using base-1000 units. // Examples: 999 -> 999B, 1200 -> 1.2k, 1540000 -> 1.5M -func HumanBytes(n int64) string { +func HumanBytes(n int64) string { if n < 1000 { return fmt.Sprintf("%dB", n) } - const unit = 1000.0 + const unit = 1000.0 v := float64(n) suffix := []string{"k", "M", "G", "T"} i := 0 - for v >= unit && i < len(suffix)-1 { + for v >= unit && i < len(suffix)-1 { v /= unit i++ } - s := fmt.Sprintf("%.1f%s", v, suffix[i]) + s := fmt.Sprintf("%.1f%s", v, suffix[i]) // Strip trailing ".0" if len(s) >= 3 && s[len(s)-2:] == ".0" { s = fmt.Sprintf("%d%s", int(v), suffix[i]) } - return s + return s } @@ -7560,8 +7605,8 @@ func HumanBytes(n int64) string { import "strings" // RenderTemplate performs simple {{var}} replacement in a template string. -func RenderTemplate(t string, vars map[string]string) string { - if t == "" || len(vars) == 0 { +func RenderTemplate(t string, vars map[string]string) string { + if t == "" || len(vars) == 0 { return t } out := t @@ -7572,30 +7617,30 @@ func RenderTemplate(t string, vars map[string]string) string { +func StripCodeFences(s string) string { t := strings.TrimSpace(s) if t == "" { return t } - lines := strings.Split(t, "\n") + lines := strings.Split(t, "\n") start := 0 for start < len(lines) && strings.TrimSpace(lines[start]) == "" { start++ } - end := len(lines) - 1 + end := len(lines) - 1 for end >= 0 && strings.TrimSpace(lines[end]) == "" { end-- } - if start >= len(lines) || end < 0 || start > end { + if start >= len(lines) || end < 0 || start > end { return t } - first := strings.TrimSpace(lines[start]) + first := strings.TrimSpace(lines[start]) last := strings.TrimSpace(lines[end]) if strings.HasPrefix(first, "```") && last == "```" && end > start { inner := strings.Join(lines[start+1:end], "\n") return inner } - return t + return t } // InstructionFromSelection extracts the first inline instruction and returns @@ -7709,9 +7754,9 @@ const ( ) // Enabled reports whether tmux status updates are enabled via env (default: on). -func Enabled() bool { +func Enabled() bool { v := strings.TrimSpace(os.Getenv("HEXAI_TMUX_STATUS")) - if v == "" { + if v == "" { return true } v = strings.ToLower(v) @@ -7719,20 +7764,20 @@ func Enabled() bool { } // SetUserOption sets a global tmux user option like @hexai_status to value. -func SetUserOption(key, value string) error { +func SetUserOption(key, value string) error { if !Enabled() || !HasBinary() || !InSession() { return nil } - k := strings.TrimPrefix(strings.TrimSpace(key), "@") + k := strings.TrimPrefix(strings.TrimSpace(key), "@") if k == "" { return nil } // Use set-option -g so it appears for all windows - return exec.Command("tmux", "set-option", "-g", "@"+k, value).Run() + return exec.Command("tmux", "set-option", "-g", "@"+k, value).Run() } // SetStatus is a convenience for setting @hexai_status. -func SetStatus(value string) error { return SetUserOption("hexai_status", applyTheme(value)) } +func SetStatus(value string) error { return SetUserOption("hexai_status", applyTheme(value)) } // FormatLLMStatsStatus builds a compact tmux status string for LLM heartbeats. // Example: "LLM:gpt-4.1 5r 0.8rpm in12k out34k" @@ -7758,7 +7803,7 @@ func FormatLLMStatsStatusColored(provider, model string, reqs int64, rpm float64 // scoped provider:model tail. The window indicator (e.g., Σ@1h) should be composed // by the caller if needed; this function focuses on numbers and labels. // Example: "Σ ↑120k ↓340k 4.2rpm | openai:gpt-4.1 3.1rpm 80r" -func FormatGlobalStatusColored(globalReqs int64, globalRPM float64, globalIn, globalOut int64, scopeProvider, scopeModel string, scopeRPM float64, scopeReqs int64, window time.Duration) string { +func FormatGlobalStatusColored(globalReqs int64, globalRPM float64, globalIn, globalOut int64, scopeProvider, scopeModel string, scopeRPM float64, scopeReqs int64, window time.Duration) string { gin := textutil.HumanBytes(globalIn) gout := textutil.HumanBytes(globalOut) head := fmt.Sprintf("%sΣ@%s %s↑%s%s %s↓%s%s %.1frpm", baseFGToken, humanWindow(window), arrowUpToken, baseFGToken, gin, arrowDownToken, baseFGToken, gout, globalRPM) @@ -7766,7 +7811,7 @@ func FormatGlobalStatusColored(globalReqs int64, globalRPM float64, globalIn, gl if narrowEnabled() || stringsTrim(scopeProvider) == "" || stringsTrim(scopeModel) == "" { return head } - tail := fmt.Sprintf(" | %s:%s %.1frpm %dr", scopeProvider, scopeModel, scopeRPM, scopeReqs) + tail := fmt.Sprintf(" | %s:%s %.1frpm %dr", scopeProvider, scopeModel, scopeRPM, scopeReqs) // Respect max length when configured: drop tail if it would overflow if ml := maxStatusLen(); ml > 0 { if len(head) <= ml && len(head)+len(tail) > ml { @@ -7776,15 +7821,15 @@ func FormatGlobalStatusColored(globalReqs int64, globalRPM float64, globalIn, gl return truncateStatus(head, ml) } } - return head + tail + return head + tail } -func humanWindow(d time.Duration) string { +func humanWindow(d time.Duration) string { if d <= 0 { return "?" } - mins := int(d.Minutes()) - if mins%60 == 0 { + mins := int(d.Minutes()) + if mins%60 == 0 { return fmt.Sprintf("%dh", mins/60) } if mins >= 60 { @@ -7794,9 +7839,9 @@ func humanWindow(d time.Duration) string { } // narrowEnabled returns true when HEXAI_TMUX_STATUS_NARROW is truthy (1/true/yes/on). -func narrowEnabled() bool { +func narrowEnabled() bool { v := strings.ToLower(stringsTrim(os.Getenv("HEXAI_TMUX_STATUS_NARROW"))) - if v == "" { + if v == "" { return false } switch v { @@ -7808,9 +7853,9 @@ func narrowEnabled() bool { } // maxStatusLen returns HEXAI_TMUX_STATUS_MAXLEN parsed as int; 0 disables. -func maxStatusLen() int { +func maxStatusLen() int { v := stringsTrim(os.Getenv("HEXAI_TMUX_STATUS_MAXLEN")) - if v == "" { + if v == "" { return 0 } n, err := strconv.Atoi(v) @@ -7833,16 +7878,16 @@ func truncateStatus(s string, n int) string { return s[:n-1] + "…" } -func stringsTrim(s string) string { +func stringsTrim(s string) string { i := 0 j := len(s) for i < j && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r') { i++ } - for j > i && (s[j-1] == ' ' || s[j-1] == '\t' || s[j-1] == '\n' || s[j-1] == '\r') { + for j > i && (s[j-1] == ' ' || s[j-1] == '\t' || s[j-1] == '\n' || s[j-1] == '\r') { j-- } - if i == 0 && j == len(s) { + if i == 0 && j == len(s) { return s } return s[i:j] @@ -7850,13 +7895,13 @@ func stringsTrim(s string) string { // FormatLLMStartStatus renders a short colored heartbeat at start/initialize time. // Example: "LLM:openai:gpt-4.1 ⏳" -func FormatLLMStartStatus(provider, model string) string { +func FormatLLMStartStatus(provider, model string) string { return fmt.Sprintf("%sLLM:%s:%s #[fg=colour11]⏳%s", baseFGToken, provider, model, baseFGToken) } // applyTheme wraps the status string with a user-selected tmux style if requested. // Set HEXAI_TMUX_STATUS_THEME=white-on-purple to get white-on-purple background. -func applyTheme(s string) string { +func applyTheme(s string) string { theme := strings.ToLower(strings.TrimSpace(os.Getenv("HEXAI_TMUX_STATUS_THEME"))) // Allow explicit fg/bg override fg := strings.TrimSpace(os.Getenv("HEXAI_TMUX_STATUS_FG")) @@ -7872,23 +7917,23 @@ func applyTheme(s string) string { baseFG = fg } // bg used as provided (may be empty) - } else { + } else { switch theme { - case "white-on-purple", "purple", "magenta", "white-on-magenta": + case "white-on-purple", "purple", "magenta", "white-on-magenta": baseFG, bg, wrap = "white", "magenta", true case "black-on-yellow", "yellow", "black-on-gold": baseFG, bg, wrap = "black", "yellow", true case "white-on-blue", "blue", "white-on-navy": baseFG, bg, wrap = "white", "blue", true } - if baseFG == "" { // no theme selected + if baseFG == "" { // no theme selected baseFG = "default" } } // Theme-aware arrow styles - upStyle, downStyle := "#[fg=colour3]", "#[fg=colour2]" // defaults: yellow up, green down - if fg != "" || bg != "" { // explicit override path: match arrows to base fg, bold for visibility + upStyle, downStyle := "#[fg=colour3]", "#[fg=colour2]" // defaults: yellow up, green down + if fg != "" || bg != "" { // explicit override path: match arrows to base fg, bold for visibility upStyle = "#[bold,fg=" + baseFG + "]" downStyle = upStyle } else { @@ -7903,25 +7948,25 @@ func applyTheme(s string) string { } // Replace base-foreground and arrow placeholders with selected styles - if strings.Contains(s, baseFGToken) { + if strings.Contains(s, baseFGToken) { s = strings.ReplaceAll(s, baseFGToken, "#[fg="+baseFG+"]") } - if strings.Contains(s, arrowUpToken) { + if strings.Contains(s, arrowUpToken) { s = strings.ReplaceAll(s, arrowUpToken, upStyle) } - if strings.Contains(s, arrowDownToken) { + if strings.Contains(s, arrowDownToken) { s = strings.ReplaceAll(s, arrowDownToken, downStyle) } - if !wrap { + if !wrap { return s } // Wrap with base fg and optional bg, then reset at the end - prefix := "#[fg=" + baseFG - if bg != "" { + prefix := "#[fg=" + baseFG + if bg != "" { prefix += ",bg=" + bg } - prefix += "]" + prefix += "]" return prefix + s + "#[fg=default,bg=default]" } @@ -7944,10 +7989,10 @@ var ( command = exec.Command ) -func HasBinary() bool { _, err := lookPath("tmux"); return err == nil } +func HasBinary() bool { _, err := lookPath("tmux"); return err == nil } // InSession reports whether we seem to be running inside a tmux session. -func InSession() bool { return strings.TrimSpace(os.Getenv("TMUX")) != "" } +func InSession() bool { return strings.TrimSpace(os.Getenv("TMUX")) != "" } // SplitOpts controls how a new pane is created for running a command. type SplitOpts struct { -- cgit v1.2.3