summaryrefslogtreecommitdiff
path: root/internal/lsp/server.go
blob: c226ab41a4e63e472c579585b3e92959bc990412 (plain)
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
// Summary: Minimal LSP server over stdio; manages documents, dispatches requests, and tracks stats.
package lsp

import (
	"bufio"
	"encoding/json"
	"io"
	"log"
	"os"
	"strings"
	"sync"
	"time"

	"codeberg.org/snonux/hexai/internal/appconfig"
	"codeberg.org/snonux/hexai/internal/llm"
	"codeberg.org/snonux/hexai/internal/logging"
	"codeberg.org/snonux/hexai/internal/runtimeconfig"
)

// Server implements a minimal LSP over stdio.
type Server struct {
	in          *bufio.Reader
	out         io.Writer
	outMu       sync.Mutex
	logger      *log.Logger
	exited      bool
	mu          sync.RWMutex
	docs        map[string]*document
	logContext  bool
	configStore *runtimeconfig.Store
	cfg         appconfig.App
	llmClient   llm.Client
	llmProvider string
	altClients  map[string]llm.Client
	lastInput   time.Time
	// LLM request stats
	llmReqTotal       int64
	llmSentBytesTotal int64
	llmRespTotal      int64
	llmRespBytesTotal int64
	startTime         time.Time
	// Small LRU cache for recent code completion outputs (keyed by context)
	compCache          map[string]string
	compCacheOrder     []string // most-recent at end; cap ~10
	pendingCompletions map[string][]CompletionItem
	configLoadOpts     appconfig.LoadOptions
	// Outgoing JSON-RPC id counter for server-initiated requests
	nextID      int64
	lastLLMCall time.Time

	completionsDisabled bool

	// Dispatch table for JSON-RPC methods → handler functions
	handlers map[string]func(Request)
}

// ServerOptions collects configuration for NewServer to avoid long parameter lists.
type ServerOptions struct {
	LogContext        bool
	ConfigStore       *runtimeconfig.Store
	Config            *appconfig.App
	MaxTokens         int
	ContextMode       string
	WindowLines       int
	MaxContextTokens  int
	ConfigLoadOptions appconfig.LoadOptions

	Client                llm.Client
	TriggerCharacters     []string
	CodingTemperature     *float64
	ManualInvokeMinPrefix int
	CompletionDebounceMs  int
	CompletionThrottleMs  int
	CompletionWaitAll     *bool

	// Inline/chat triggers
	InlineOpen   string
	InlineClose  string
	ChatSuffix   string
	ChatPrefixes []string

	// Prompt templates
	PromptCompSysGeneral    string
	PromptCompSysParams     string
	PromptCompSysInline     string
	PromptCompUserGeneral   string
	PromptCompUserParams    string
	PromptCompExtraHeader   string
	PromptNativeCompletion  string
	PromptChatSystem        string
	PromptRewriteSystem     string
	PromptDiagnosticsSystem string
	PromptDocumentSystem    string
	PromptRewriteUser       string
	PromptDiagnosticsUser   string
	PromptDocumentUser      string
	PromptGoTestSystem      string
	PromptGoTestUser        string
	PromptSimplifySystem    string
	PromptSimplifyUser      string

	// Custom actions
	CustomActions []CustomAction
}

// CustomAction mirrors user-defined code actions passed from config.
type CustomAction struct {
	ID          string
	Title       string
	Kind        string
	Scope       string // "selection" | "diagnostics"
	Instruction string // if set, use rewrite templates
	System      string // optional when User is set
	User        string // if set, use this user template
}

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)
	s.pendingCompletions = make(map[string][]CompletionItem)
	s.applyOptions(opts)
	// Initialize dispatch table
	s.handlers = map[string]func(Request){
		"initialize":               s.handleInitialize,
		"initialized":              func(_ Request) { s.handleInitialized() },
		"shutdown":                 s.handleShutdown,
		"exit":                     func(_ Request) { s.handleExit() },
		"textDocument/didOpen":     s.handleDidOpen,
		"textDocument/didChange":   s.handleDidChange,
		"textDocument/didClose":    s.handleDidClose,
		"textDocument/completion":  s.handleCompletion,
		"textDocument/codeAction":  s.handleCodeAction,
		"codeAction/resolve":       s.handleCodeActionResolve,
		"workspace/executeCommand": s.handleExecuteCommand,
	}
	return s
}

func (s *Server) applyOptions(opts ServerOptions) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.logContext = opts.LogContext
	s.configLoadOpts = opts.ConfigLoadOptions
	if opts.ConfigStore != nil {
		s.configStore = opts.ConfigStore
	}
	if opts.Config != nil {
		s.cfg = *opts.Config
	} else if opts.ConfigStore != nil {
		s.cfg = opts.ConfigStore.Snapshot()
	} else {
		s.cfg = appconfig.App{}
		// populate from legacy ServerOptions fields
		s.cfg.MaxTokens = opts.MaxTokens
		s.cfg.ContextMode = opts.ContextMode
		s.cfg.ContextWindowLines = opts.WindowLines
		s.cfg.MaxContextTokens = opts.MaxContextTokens
		s.cfg.TriggerCharacters = append([]string{}, opts.TriggerCharacters...)
		s.cfg.CodingTemperature = opts.CodingTemperature
		s.cfg.ManualInvokeMinPrefix = opts.ManualInvokeMinPrefix
		s.cfg.CompletionDebounceMs = opts.CompletionDebounceMs
		s.cfg.CompletionThrottleMs = opts.CompletionThrottleMs
		s.cfg.CompletionWaitAll = opts.CompletionWaitAll
		s.cfg.InlineOpen = opts.InlineOpen
		s.cfg.InlineClose = opts.InlineClose
		s.cfg.ChatSuffix = opts.ChatSuffix
		s.cfg.ChatPrefixes = append([]string{}, opts.ChatPrefixes...)
		s.cfg.PromptCompletionSystemGeneral = opts.PromptCompSysGeneral
		s.cfg.PromptCompletionSystemParams = opts.PromptCompSysParams
		s.cfg.PromptCompletionSystemInline = opts.PromptCompSysInline
		s.cfg.PromptCompletionUserGeneral = opts.PromptCompUserGeneral
		s.cfg.PromptCompletionUserParams = opts.PromptCompUserParams
		s.cfg.PromptCompletionExtraHeader = opts.PromptCompExtraHeader
		s.cfg.PromptNativeCompletion = opts.PromptNativeCompletion
		s.cfg.PromptChatSystem = opts.PromptChatSystem
		s.cfg.PromptCodeActionRewriteSystem = opts.PromptRewriteSystem
		s.cfg.PromptCodeActionDiagnosticsSystem = opts.PromptDiagnosticsSystem
		s.cfg.PromptCodeActionDocumentSystem = opts.PromptDocumentSystem
		s.cfg.PromptCodeActionRewriteUser = opts.PromptRewriteUser
		s.cfg.PromptCodeActionDiagnosticsUser = opts.PromptDiagnosticsUser
		s.cfg.PromptCodeActionDocumentUser = opts.PromptDocumentUser
		s.cfg.PromptCodeActionGoTestSystem = opts.PromptGoTestSystem
		s.cfg.PromptCodeActionGoTestUser = opts.PromptGoTestUser
		s.cfg.PromptCodeActionSimplifySystem = opts.PromptSimplifySystem
		s.cfg.PromptCodeActionSimplifyUser = opts.PromptSimplifyUser
		s.cfg.CustomActions = make([]appconfig.CustomAction, len(opts.CustomActions))
		for i, ca := range opts.CustomActions {
			s.cfg.CustomActions[i] = appconfig.CustomAction{
				ID:          ca.ID,
				Title:       ca.Title,
				Kind:        ca.Kind,
				Scope:       ca.Scope,
				Instruction: ca.Instruction,
				System:      ca.System,
				User:        ca.User,
			}
		}
	}
	s.llmClient = opts.Client
	if opts.Client != nil {
		s.llmProvider = canonicalProvider(opts.Client.Name())
	} else {
		s.llmProvider = canonicalProvider(s.cfg.Provider)
	}
	s.altClients = make(map[string]llm.Client)
}

// ApplyOptions updates the server's configuration at runtime.
func (s *Server) ApplyOptions(opts ServerOptions) {
	s.applyOptions(opts)
}

func (s *Server) currentLLMClient() llm.Client {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.llmClient
}

func newClientForProvider(cfg appconfig.App, provider string) (llm.Client, error) {
	llmCfg := llm.Config{
		Provider:              provider,
		RequestTimeout:        cfg.RequestTimeout,
		OpenAIBaseURL:         cfg.OpenAIBaseURL,
		OpenAIModel:           cfg.OpenAIModel,
		OpenAITemperature:     cfg.OpenAITemperature,
		OpenRouterBaseURL:     cfg.OpenRouterBaseURL,
		OpenRouterModel:       cfg.OpenRouterModel,
		OpenRouterTemperature: cfg.OpenRouterTemperature,
		OllamaBaseURL:         cfg.OllamaBaseURL,
		OllamaModel:           cfg.OllamaModel,
		OllamaTemperature:     cfg.OllamaTemperature,
		AnthropicBaseURL:      cfg.AnthropicBaseURL,
		AnthropicModel:        cfg.AnthropicModel,
		AnthropicTemperature:  cfg.AnthropicTemperature,
	}
	oaKey := strings.TrimSpace(os.Getenv("HEXAI_OPENAI_API_KEY"))
	if oaKey == "" {
		oaKey = strings.TrimSpace(os.Getenv("OPENAI_API_KEY"))
	}
	orKey := strings.TrimSpace(os.Getenv("HEXAI_OPENROUTER_API_KEY"))
	if orKey == "" {
		orKey = strings.TrimSpace(os.Getenv("OPENROUTER_API_KEY"))
	}
	anKey := strings.TrimSpace(os.Getenv("HEXAI_ANTHROPIC_API_KEY"))
	if anKey == "" {
		anKey = strings.TrimSpace(os.Getenv("ANTHROPIC_API_KEY"))
	}
	return llm.NewFromConfig(llmCfg, oaKey, orKey, anKey)
}

func (s *Server) clientFor(spec requestSpec) llm.Client {
	provider := canonicalProvider(spec.provider)
	s.mu.RLock()
	baseProvider := s.llmProvider
	baseClient := s.llmClient
	if baseClient != nil && strings.TrimSpace(baseProvider) == "" {
		baseProvider = canonicalProvider(baseClient.Name())
	}
	if provider == "" {
		provider = baseProvider
	}
	if provider == baseProvider && baseClient != nil {
		s.mu.RUnlock()
		return baseClient
	}
	if c, ok := s.altClients[provider]; ok {
		s.mu.RUnlock()
		return c
	}
	cfg := s.cfg
	store := s.configStore
	s.mu.RUnlock()
	if store != nil {
		cfg = store.Snapshot()
	}
	cfg.Provider = provider
	modelOverride := strings.TrimSpace(spec.entry.Model)
	switch provider {
	case "openai":
		if modelOverride != "" {
			cfg.OpenAIModel = modelOverride
		} else if spec.fallbackModel != "" {
			cfg.OpenAIModel = spec.fallbackModel
		}
	case "openrouter":
		if modelOverride != "" {
			cfg.OpenRouterModel = modelOverride
		} else if spec.fallbackModel != "" {
			cfg.OpenRouterModel = spec.fallbackModel
		}
	case "ollama":
		if modelOverride != "" {
			cfg.OllamaModel = modelOverride
		} else if spec.fallbackModel != "" {
			cfg.OllamaModel = spec.fallbackModel
		}
	case "anthropic":
		if modelOverride != "" {
			cfg.AnthropicModel = modelOverride
		} else if spec.fallbackModel != "" {
			cfg.AnthropicModel = spec.fallbackModel
		}
	}
	client, err := newClientForProvider(cfg, provider)
	if err != nil {
		logging.Logf("lsp ", "failed to build client for provider=%s: %v", provider, err)
		if baseClient != nil {
			return baseClient
		}
		return nil
	}
	s.mu.Lock()
	defer s.mu.Unlock()
	if provider == s.llmProvider {
		if s.llmClient == nil {
			s.llmClient = client
			s.llmProvider = provider
		}
		return s.llmClient
	}
	if existing, ok := s.altClients[provider]; ok {
		return existing
	}
	if s.altClients == nil {
		s.altClients = make(map[string]llm.Client)
	}
	s.altClients[provider] = client
	return client
}

func (s *Server) currentConfig() appconfig.App {
	if s.configStore != nil {
		return s.configStore.Snapshot()
	}
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.cfg
}

func (s *Server) storePendingCompletion(key string, items []CompletionItem) {
	if len(items) == 0 {
		return
	}
	cpy := make([]CompletionItem, len(items))
	copy(cpy, items)
	s.mu.Lock()
	if s.pendingCompletions == nil {
		s.pendingCompletions = make(map[string][]CompletionItem)
	}
	s.pendingCompletions[key] = cpy
	s.mu.Unlock()
}

func (s *Server) setCompletionsDisabled(disabled bool) bool {
	s.mu.Lock()
	prev := s.completionsDisabled
	s.completionsDisabled = disabled
	s.mu.Unlock()
	return prev
}

func (s *Server) completionDisabled() bool {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.completionsDisabled
}

func (s *Server) takePendingCompletion(key string) []CompletionItem {
	s.mu.Lock()
	defer s.mu.Unlock()
	if len(s.pendingCompletions) == 0 {
		return nil
	}
	items, ok := s.pendingCompletions[key]
	if !ok {
		return nil
	}
	delete(s.pendingCompletions, key)
	cpy := make([]CompletionItem, len(items))
	copy(cpy, items)
	return cpy
}

func (s *Server) maxTokens() int {
	cfg := s.currentConfig()
	if cfg.MaxTokens <= 0 {
		return 500
	}
	return cfg.MaxTokens
}

func (s *Server) contextMode() string {
	mode := strings.TrimSpace(s.currentConfig().ContextMode)
	if mode == "" {
		return "file-on-new-func"
	}
	return mode
}

func (s *Server) windowLines() int {
	cfg := s.currentConfig()
	if cfg.ContextWindowLines <= 0 {
		return 120
	}
	return cfg.ContextWindowLines
}

func (s *Server) maxContextTokens() int {
	cfg := s.currentConfig()
	if cfg.MaxContextTokens <= 0 {
		return 2000
	}
	return cfg.MaxContextTokens
}

func (s *Server) triggerCharacters() []string {
	cfg := s.currentConfig()
	if len(cfg.TriggerCharacters) == 0 {
		return []string{".", ":", "/", "_", ")", "{"}
	}
	return append([]string{}, cfg.TriggerCharacters...)
}

func (s *Server) codingTemperature() *float64 {
	cfg := s.currentConfig()
	return cfg.CodingTemperature
}

func (s *Server) manualInvokeMinPrefix() int {
	return s.currentConfig().ManualInvokeMinPrefix
}

func (s *Server) completionDebounce() time.Duration {
	cfg := s.currentConfig()
	if cfg.CompletionDebounceMs <= 0 {
		return 0
	}
	return time.Duration(cfg.CompletionDebounceMs) * time.Millisecond
}

func (s *Server) completionThrottle() time.Duration {
	cfg := s.currentConfig()
	if cfg.CompletionThrottleMs <= 0 {
		return 0
	}
	return time.Duration(cfg.CompletionThrottleMs) * time.Millisecond
}

func (s *Server) completionWaitAll() bool {
	cfg := s.currentConfig()
	if cfg.CompletionWaitAll == nil {
		return true // default: wait for all backends
	}
	return *cfg.CompletionWaitAll
}

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)
	if close == "" {
		close = ">"
	}
	openChar = '>'
	if len(open) > 0 {
		openChar = open[0]
	}
	closeChar = '>'
	if len(close) > 0 {
		closeChar = close[0]
	}
	return open, close, openChar, closeChar
}

func (s *Server) chatConfig() (suffix string, prefixes []string, suffixChar byte) {
	cfg := s.currentConfig()
	suffix = cfg.ChatSuffix
	if suffix != "" {
		suffix = strings.TrimSpace(suffix)
		if suffix == "" {
			suffix = ">"
		}
	} else {
		suffix = ""
	}
	if len(cfg.ChatPrefixes) == 0 {
		prefixes = []string{"?", "!", ":", ";"}
	} else {
		prefixes = append([]string{}, cfg.ChatPrefixes...)
	}
	suffixChar = '>'
	if len(suffix) > 0 {
		suffixChar = suffix[0]
	}
	return suffix, prefixes, suffixChar
}

func (s *Server) promptSet() appconfig.App {
	return s.currentConfig()
}

func (s *Server) customActions() []CustomAction {
	cfg := s.currentConfig()
	if len(cfg.CustomActions) == 0 {
		return nil
	}
	customs := make([]CustomAction, 0, len(cfg.CustomActions))
	for _, ca := range cfg.CustomActions {
		customs = append(customs, CustomAction{
			ID:          ca.ID,
			Title:       ca.Title,
			Kind:        ca.Kind,
			Scope:       ca.Scope,
			Instruction: ca.Instruction,
			System:      ca.System,
			User:        ca.User,
		})
	}
	return customs
}

func (s *Server) Run() error {
	for {
		body, err := s.readMessage()
		if err == io.EOF {
			return nil
		}
		if err != nil {
			return err
		}
		var req Request
		if err := json.Unmarshal(body, &req); err != nil {
			logging.Logf("lsp ", "invalid JSON: %v", err)
			continue
		}
		if req.Method == "" {
			// A response from client; ignore
			continue
		}
		go s.handle(req)
		if s.exited {
			return nil
		}
	}
}