-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathchat.go
More file actions
133 lines (117 loc) · 3.87 KB
/
chat.go
File metadata and controls
133 lines (117 loc) · 3.87 KB
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
package coderdtest
import (
"context"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/x/chatd"
"github.com/coder/coder/v2/coderd/x/chatd/chatprovider"
"github.com/coder/coder/v2/coderd/x/chatd/chattest"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
const (
// TestChatProviderOpenAICompat is the default provider for chat runtime tests.
TestChatProviderOpenAICompat = "openai-compat"
// TestChatProviderAPIKey is a non-secret API key for local chat providers.
TestChatProviderAPIKey = "test-api-key"
// TestChatModelOpenAICompat is the default model for chat runtime tests.
TestChatModelOpenAICompat = "gpt-4o-mini"
)
// OpenAICompatProviderAPIKeys returns provider keys that route OpenAI-compatible
// chat calls to baseURL.
func OpenAICompatProviderAPIKeys(baseURL string) chatprovider.ProviderAPIKeys {
return chatprovider.ProviderAPIKeys{
ByProvider: map[string]string{
TestChatProviderOpenAICompat: TestChatProviderAPIKey,
},
BaseURLByProvider: map[string]string{
TestChatProviderOpenAICompat: baseURL,
},
}
}
// FakeOpenAICompatProviderAPIKeys starts a fake OpenAI-compatible provider and
// returns provider keys for coderdtest.Options.
func FakeOpenAICompatProviderAPIKeys(t testing.TB) chatprovider.ProviderAPIKeys {
t.Helper()
return OpenAICompatProviderAPIKeys(chattest.OpenAI(t))
}
// CreateOpenAICompatChatModelConfig creates the default provider and model
// config used by chat runtime tests. Tests can pass a baseURL to route chat work
// to a specific local provider. If baseURL is empty, this helper starts a fake
// OpenAI-compatible provider.
func CreateOpenAICompatChatModelConfig(
t testing.TB,
client *codersdk.ExperimentalClient,
baseURL string,
) codersdk.ChatModelConfig {
t.Helper()
if baseURL == "" {
baseURL = chattest.OpenAI(t)
}
ctx := testutil.Context(t, testutil.WaitLong)
provider, err := client.CreateAIProvider(ctx, codersdk.CreateAIProviderRequest{
Type: codersdk.AIProviderType(TestChatProviderOpenAICompat),
Name: "test-" + uuid.NewString(),
BaseURL: baseURL,
Enabled: true,
APIKeys: []string{TestChatProviderAPIKey},
})
require.NoError(t, err)
contextLimit := int64(4096)
isDefault := true
modelConfig, err := client.CreateChatModelConfig(ctx, codersdk.CreateChatModelConfigRequest{
Provider: TestChatProviderOpenAICompat,
AIProviderID: &provider.ID,
Model: TestChatModelOpenAICompat,
ContextLimit: &contextLimit,
IsDefault: &isDefault,
})
require.NoError(t, err)
return modelConfig
}
// WaitForChatSettled waits for a chat to leave active processing and drains
// tracked chat daemon work before returning the final row.
func WaitForChatSettled(
ctx context.Context,
t testing.TB,
api *coderd.API,
chatID uuid.UUID,
) database.Chat {
t.Helper()
require.NotNil(t, api)
waitForChatTerminalState(ctx, t, api.Database, chatID)
server := api.ChatDaemonForTest()
require.NotNil(t, server)
chatd.WaitUntilIdleForTest(server)
chat, err := getChatByIDAsSystem(ctx, api.Database, chatID)
require.NoError(t, err)
return chat
}
func waitForChatTerminalState(
ctx context.Context,
t testing.TB,
db database.Store,
chatID uuid.UUID,
) {
t.Helper()
require.Eventually(t, func() bool {
chat, err := getChatByIDAsSystem(ctx, db, chatID)
if err != nil {
return false
}
return chat.Status != database.ChatStatusPending && chat.Status != database.ChatStatusRunning
}, testutil.WaitLong, testutil.IntervalFast)
}
func getChatByIDAsSystem(
ctx context.Context,
db database.Store,
chatID uuid.UUID,
) (database.Chat, error) {
// Test helper needs system scope to observe chatd-owned status changes.
//nolint:gocritic
return db.GetChatByID(dbauthz.AsSystemRestricted(ctx), chatID)
}