Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cmd/opencodereview/config_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,34 @@ func TestSetConfigValueProviderEntryExtraBody(t *testing.T) {
}
}

func TestSetConfigValueProviderCursor(t *testing.T) {
cfg := &Config{}

if err := setConfigValue(cfg, "provider", "cursor"); err != nil {
t.Fatalf("setConfigValue provider: %v", err)
}
if cfg.Provider != "cursor" {
t.Errorf("Provider = %q, want cursor", cfg.Provider)
}
if cfg.Providers["cursor"].APIKey != "" {
t.Error("expected empty cursor provider entry after provider switch")
}

if err := setConfigValue(cfg, "providers.cursor.api_key", "cursor-key"); err != nil {
t.Fatalf("setConfigValue api_key: %v", err)
}
if cfg.Providers["cursor"].APIKey != "cursor-key" {
t.Errorf("api_key = %q, want cursor-key", cfg.Providers["cursor"].APIKey)
}

if err := setConfigValue(cfg, "providers.cursor.model", "composer-2.5"); err != nil {
t.Fatalf("setConfigValue model: %v", err)
}
if cfg.Providers["cursor"].Model != "composer-2.5" {
t.Errorf("model = %q, want composer-2.5", cfg.Providers["cursor"].Model)
}
}

func TestSetConfigValueModelWithCustomProvider(t *testing.T) {
cfg := &Config{
Provider: "my-gateway",
Expand Down
50 changes: 33 additions & 17 deletions cmd/opencodereview/llm_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"context"
"fmt"
"os"
"time"

"github.com/open-code-review/open-code-review/internal/config/testconnection"
"github.com/open-code-review/open-code-review/internal/llm"
"github.com/open-code-review/open-code-review/internal/reviewbackend"
)

func runLLM(args []string) error {
Expand Down Expand Up @@ -37,9 +39,14 @@ func runLLMTest() error {
return fmt.Errorf("load config: %w", err)
}

ep, err := llm.ResolveEndpoint(cfgPath)
resolved, err := reviewbackend.ResolveBackend(cfgPath)
if err != nil {
return fmt.Errorf("resolve LLM endpoint: %w", err)
return fmt.Errorf("resolve review backend: %w", err)
}

repoDir, err := os.Getwd()
if err != nil {
return err
}

task, err := testconnection.LoadDefault()
Expand All @@ -55,33 +62,42 @@ func runLLMTest() error {
timeout = time.Duration(task.Timeout) * time.Second
}

llmClient := llm.NewLLMClient(ep)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

backend, err := reviewbackend.New(ctx, resolved, repoDir)
if err != nil {
return fmt.Errorf("create review backend: %w", err)
}

llmClient := reviewbackend.TextClient(backend)

model := backend.Model()
source := backend.Source()

messages := make([]llm.Message, 0, len(task.Messages))
for _, m := range task.Messages {
messages = append(messages, llm.Message{Role: m.Role, Content: m.Content})
}

resp, err := func() (*llm.ChatResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return llmClient.CompletionsWithCtx(ctx, llm.ChatRequest{
Model: ep.Model,
Messages: messages,
MaxTokens: 256,
})
}()
resp, err := llmClient.CompletionsWithCtx(ctx, llm.ChatRequest{
Model: model,
Messages: messages,
MaxTokens: 256,
})
if err != nil {
return fmt.Errorf("llm request failed: %w", err)
}

model := ep.Model
outModel := model
if resp.Model != "" {
model = resp.Model
outModel = resp.Model
}
fmt.Printf("Source: %s\n", source)
if resolved.Kind == reviewbackend.KindChatCompletions {
fmt.Printf("URL: %s\n", resolved.Endpoint.URL)
}
fmt.Printf("Source: %s\n", ep.Source)
fmt.Printf("URL: %s\n", ep.URL)
fmt.Printf("Model: %s\n", model)
fmt.Printf("Model: %s\n", outModel)
fmt.Printf("%s\n", resp.Content())
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/opencodereview/provider_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ func applyOfficialProviderConfig(configPath string, cfg *Config, result provider
fmt.Printf("\nProvider set to: %s\n", result.provider)
fmt.Printf("Model: %s\n", result.model)

if isPreset && preset.IsCursorAgent() {
fmt.Println("\nCursor backend requires the SDK bridge before the first review.")
fmt.Printf("Run: %s\n", cursorBridgeSetupHint)
}

fmt.Println("\nTesting connection...")
if err := runLLMTest(); err != nil {
fmt.Fprintf(os.Stderr, "Connection test failed: %v\n", err)
Expand Down
22 changes: 22 additions & 0 deletions cmd/opencodereview/provider_tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ func (m providerTUIModel) viewOfficialTab(s *strings.Builder) {
s.WriteString(cursor + tuiItemStyle.Render(name))
}
s.WriteString("\n")
if subtitle := providerOfficialSubtitle(p); subtitle != "" {
s.WriteString(" " + tuiDimStyle.Render(subtitle) + "\n")
}
}
}

Expand Down Expand Up @@ -1109,6 +1112,13 @@ func (m providerTUIModel) viewAPIKey(s *strings.Builder) {

if m.activeTab == tabOfficial {
provider := m.currentProvider()
if provider.IsCursorAgent() {
s.WriteString("\n")
s.WriteString(tuiDimStyle.Render(" Requires Cursor SDK bridge (Node.js >= 18, npm)."))
s.WriteString("\n")
s.WriteString(tuiDimStyle.Render(" One-time setup: " + cursorBridgeSetupHint))
s.WriteString("\n")
}
if envKey := os.Getenv(provider.EnvVar); envKey != "" {
s.WriteString("\n")
s.WriteString(tuiDimStyle.Render(fmt.Sprintf(" $%s is set", provider.EnvVar)))
Expand All @@ -1127,8 +1137,20 @@ func (m providerTUIModel) viewAPIKey(s *strings.Builder) {

// --- Styles ---

const cursorBridgeSetupHint = "go run github.com/remdev/cursor-go-sdk/cmd/setup@latest"

const tuiCursor = "▸"

func providerOfficialSubtitle(p llm.Provider) string {
if p.IsCursorAgent() {
return "local agent via Cursor SDK (no HTTP endpoint)"
}
if p.BaseURL != "" {
return p.BaseURL
}
return ""
}

var (
tuiTitleStyle = lipgloss.NewStyle().
Bold(true).
Expand Down
51 changes: 51 additions & 0 deletions cmd/opencodereview/provider_tui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,57 @@ func TestProviderTUI_TabSwitchOnlyOnStepProvider(t *testing.T) {

// --- Official tab tests (updated from original) ---

func TestProviderTUI_OfficialTabIncludesCursor(t *testing.T) {
m := newProviderTUI(&Config{})

found := false
for _, p := range m.providers {
if p.Name == "cursor" {
found = true
if !p.IsCursorAgent() {
t.Error("cursor preset should use Cursor agent backend")
}
if subtitle := providerOfficialSubtitle(p); subtitle == "" {
t.Error("expected subtitle for cursor preset")
}
break
}
}
if !found {
t.Fatal("cursor preset missing from official provider list")
}
}

func TestProviderTUI_CursorProviderSelectsModels(t *testing.T) {
m := newProviderTUI(&Config{})
idx := -1
for i, p := range m.providers {
if p.Name == "cursor" {
idx = i
break
}
}
if idx < 0 {
t.Fatal("cursor preset not found")
}

m.officialIdx = idx
result, _ := m.Update(enterKey())
m2 := result.(providerTUIModel)
if m2.step != stepModel {
t.Fatalf("step = %d, want stepModel", m2.step)
}

models := m2.models()
want := map[string]bool{"auto": true, "composer-2.5": true, "composer-2": true}
for _, model := range models {
delete(want, model)
}
if len(want) > 0 {
t.Errorf("missing cursor models: %v", want)
}
}

func TestProviderTUI_EscFromModelGoesBackToProvider(t *testing.T) {
m := newProviderTUI(&Config{})

Expand Down
16 changes: 11 additions & 5 deletions cmd/opencodereview/review_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/open-code-review/open-code-review/internal/config/toolsconfig"
"github.com/open-code-review/open-code-review/internal/diff"
"github.com/open-code-review/open-code-review/internal/gitcmd"
"github.com/open-code-review/open-code-review/internal/llm"
"github.com/open-code-review/open-code-review/internal/reviewbackend"
"github.com/open-code-review/open-code-review/internal/stdout"
"github.com/open-code-review/open-code-review/internal/telemetry"
"github.com/open-code-review/open-code-review/internal/tool"
Expand Down Expand Up @@ -88,13 +88,18 @@ func runReview(args []string) error {
tpl.ApplyLanguage(appCfg.Language)
}

ep, err := llm.ResolveEndpoint(cfgPath)
resolved, err := reviewbackend.ResolveBackend(cfgPath)
if err != nil {
return fmt.Errorf("resolve LLM endpoint: %w", err)
return fmt.Errorf("resolve review backend: %w", err)
}

llmClient := llm.NewLLMClient(ep)
model := ep.Model
backend, err := reviewbackend.New(context.Background(), resolved, repoDir)
if err != nil {
return fmt.Errorf("create review backend: %w", err)
}

llmClient := reviewbackend.TextClient(backend)
model := backend.Model()

gitRunner := gitcmd.New(opts.maxGitProcs)

Expand All @@ -118,6 +123,7 @@ func runReview(args []string) error {
SystemRule: resolver,
FileFilter: fileFilter,
LLMClient: llmClient,
Backend: backend,
Tools: tools,
PlanToolDefs: planToolDefs,
MainToolDefs: mainToolDefs,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.10.0
github.com/openai/openai-go/v3 v3.39.0
github.com/pkoukk/tiktoken-go v0.1.8
github.com/remdev/cursor-go-sdk v0.0.3
go.opentelemetry.io/otel v1.43.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.43.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.43.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ github.com/pkoukk/tiktoken-go v0.1.8 h1:85ENo+3FpWgAACBaEUVp+lctuTcYUO7BtmfhlN/Q
github.com/pkoukk/tiktoken-go v0.1.8/go.mod h1:9NiV+i9mJKGj1rYOT+njbv+ZwA/zJxYdewGl6qVatpg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remdev/cursor-go-sdk v0.0.3 h1:iDkAip8KdmXg5OID1F9wNbufmInlIcGEE74+8BantFE=
github.com/remdev/cursor-go-sdk v0.0.3/go.mod h1:8+NZymOCljHsqHoor5ZU9o4kuwy573NuY7U8n6jajAY=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 h1:uOfcYT+3QungH6tIGSVCR/Y3KJmgJiHcojJbMTPDZAI=
Expand Down
Loading