File size: 2,466 Bytes
9e2b962 | 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 | package plugin
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
type ContinueConfig struct {
Models []map[string]interface{} `json:"models"`
Rest map[string]interface{} `json:"-"`
}
// ConnectVSCode connects WingLLM to VS Code's Continue extension by updating ~/.continue/config.json.
func ConnectVSCode(modelName string) error {
userProfile := os.Getenv("USERPROFILE")
if userProfile == "" {
home, _ := os.UserHomeDir()
userProfile = home
}
continueDir := filepath.Join(userProfile, ".continue")
if err := os.MkdirAll(continueDir, 0755); err != nil {
return fmt.Errorf("failed to create .continue directory: %w", err)
}
configFile := filepath.Join(continueDir, "config.json")
wingllmModel := map[string]interface{}{
"title": fmt.Sprintf("WingLLM (%s - 2x Turbo)", modelName),
"provider": "openai",
"model": modelName,
"apiBase": "http://localhost:8080/v1",
}
var root map[string]interface{}
if data, err := os.ReadFile(configFile); err == nil && len(data) > 0 {
_ = json.Unmarshal(data, &root)
}
if root == nil {
root = make(map[string]interface{})
}
// Update models array
var models []interface{}
if existing, ok := root["models"].([]interface{}); ok {
models = existing
}
// Check if already present
exists := false
for _, m := range models {
if mObj, ok := m.(map[string]interface{}); ok {
if mObj["apiBase"] == "http://localhost:8080/v1" {
exists = true
mObj["title"] = fmt.Sprintf("WingLLM (%s - 2x Turbo)", modelName)
mObj["model"] = modelName
break
}
}
}
if !exists {
// Prepend to top of models list so it appears first in the dropdown
models = append([]interface{}{wingllmModel}, models...)
}
root["models"] = models
// Also configure Tab Autocomplete if not present
if _, ok := root["tabAutocompleteModel"]; !ok {
root["tabAutocompleteModel"] = map[string]interface{}{
"title": "WingLLM Autocomplete",
"provider": "openai",
"model": modelName,
"apiBase": "http://localhost:8080/v1",
}
}
output, err := json.MarshalIndent(root, "", " ")
if err != nil {
return err
}
if err := os.WriteFile(configFile, output, 0644); err != nil {
return fmt.Errorf("failed to write config.json: %w", err)
}
fmt.Printf("\n Successfully connected to VS Code (Continue Extension)!\n")
fmt.Printf(" Config written to: %s\n", configFile)
fmt.Printf(" Model in dropdown: WingLLM (%s - 2x Turbo)\n\n", modelName)
return nil
}
|