forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.go
More file actions
366 lines (337 loc) · 10.2 KB
/
chat.go
File metadata and controls
366 lines (337 loc) · 10.2 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
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
package coderd
import (
"encoding/json"
"io"
"net/http"
"time"
"github.com/kylecarbs/aisdk-go"
"github.com/coder/coder/v2/coderd/ai"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/database/dbtime"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/coderd/util/strings"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/toolsdk"
)
// postChats creates a new chat.
//
// @Summary Create a chat
// @ID create-a-chat
// @Security CoderSessionToken
// @Produce json
// @Tags Chat
// @Success 201 {object} codersdk.Chat
// @Router /chats [post]
func (api *API) postChats(w http.ResponseWriter, r *http.Request) {
apiKey := httpmw.APIKey(r)
ctx := r.Context()
chat, err := api.Database.InsertChat(ctx, database.InsertChatParams{
OwnerID: apiKey.UserID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Title: "New Chat",
})
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to create chat",
Detail: err.Error(),
})
return
}
httpapi.Write(ctx, w, http.StatusCreated, db2sdk.Chat(chat))
}
// listChats lists all chats for a user.
//
// @Summary List chats
// @ID list-chats
// @Security CoderSessionToken
// @Produce json
// @Tags Chat
// @Success 200 {array} codersdk.Chat
// @Router /chats [get]
func (api *API) listChats(w http.ResponseWriter, r *http.Request) {
apiKey := httpmw.APIKey(r)
ctx := r.Context()
chats, err := api.Database.GetChatsByOwnerID(ctx, apiKey.UserID)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to list chats",
Detail: err.Error(),
})
return
}
httpapi.Write(ctx, w, http.StatusOK, db2sdk.Chats(chats))
}
// chat returns a chat by ID.
//
// @Summary Get a chat
// @ID get-a-chat
// @Security CoderSessionToken
// @Produce json
// @Tags Chat
// @Param chat path string true "Chat ID"
// @Success 200 {object} codersdk.Chat
// @Router /chats/{chat} [get]
func (*API) chat(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
chat := httpmw.ChatParam(r)
httpapi.Write(ctx, w, http.StatusOK, db2sdk.Chat(chat))
}
// chatMessages returns the messages of a chat.
//
// @Summary Get chat messages
// @ID get-chat-messages
// @Security CoderSessionToken
// @Produce json
// @Tags Chat
// @Param chat path string true "Chat ID"
// @Success 200 {array} aisdk.Message
// @Router /chats/{chat}/messages [get]
func (api *API) chatMessages(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
chat := httpmw.ChatParam(r)
rawMessages, err := api.Database.GetChatMessagesByChatID(ctx, chat.ID)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to get chat messages",
Detail: err.Error(),
})
return
}
messages := make([]aisdk.Message, len(rawMessages))
for i, message := range rawMessages {
var msg aisdk.Message
err = json.Unmarshal(message.Content, &msg)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to unmarshal chat message",
Detail: err.Error(),
})
return
}
messages[i] = msg
}
httpapi.Write(ctx, w, http.StatusOK, messages)
}
// postChatMessages creates a new chat message and streams the response.
//
// @Summary Create a chat message
// @ID create-a-chat-message
// @Security CoderSessionToken
// @Accept json
// @Produce json
// @Tags Chat
// @Param chat path string true "Chat ID"
// @Param request body codersdk.CreateChatMessageRequest true "Request body"
// @Success 200 {array} aisdk.DataStreamPart
// @Router /chats/{chat}/messages [post]
func (api *API) postChatMessages(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
chat := httpmw.ChatParam(r)
var req codersdk.CreateChatMessageRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{
Message: "Failed to decode chat message",
Detail: err.Error(),
})
return
}
dbMessages, err := api.Database.GetChatMessagesByChatID(ctx, chat.ID)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to get chat messages",
Detail: err.Error(),
})
return
}
messages := make([]codersdk.ChatMessage, 0)
for _, dbMsg := range dbMessages {
var msg codersdk.ChatMessage
err = json.Unmarshal(dbMsg.Content, &msg)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to unmarshal chat message",
Detail: err.Error(),
})
return
}
messages = append(messages, msg)
}
messages = append(messages, req.Message)
client := codersdk.New(api.AccessURL)
client.SetSessionToken(httpmw.APITokenFromRequest(r))
tools := make([]aisdk.Tool, 0)
handlers := map[string]toolsdk.GenericHandlerFunc{}
for _, tool := range toolsdk.All {
if tool.Name == "coder_report_task" {
continue // This tool requires an agent to run.
}
tools = append(tools, tool.Tool)
handlers[tool.Tool.Name] = tool.Handler
}
provider, ok := api.LanguageModels[req.Model]
if !ok {
httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{
Message: "Model not found",
})
return
}
// If it's the user's first message, generate a title for the chat.
if len(messages) == 1 {
var acc aisdk.DataStreamAccumulator
stream, err := provider.StreamFunc(ctx, ai.StreamOptions{
Model: req.Model,
SystemPrompt: `- You will generate a short title based on the user's message.
- It should be maximum of 40 characters.
- Do not use quotes, colons, special characters, or emojis.`,
Messages: messages,
Tools: []aisdk.Tool{}, // This initial stream doesn't use tools.
})
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to create stream",
Detail: err.Error(),
})
return
}
stream = stream.WithAccumulator(&acc)
err = stream.Pipe(io.Discard)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to pipe stream",
Detail: err.Error(),
})
return
}
var newTitle string
accMessages := acc.Messages()
// If for some reason the stream didn't return any messages, use the
// original message as the title.
if len(accMessages) == 0 {
newTitle = strings.Truncate(messages[0].Content, 40)
} else {
newTitle = strings.Truncate(accMessages[0].Content, 40)
}
err = api.Database.UpdateChatByID(ctx, database.UpdateChatByIDParams{
ID: chat.ID,
Title: newTitle,
UpdatedAt: dbtime.Now(),
})
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to update chat title",
Detail: err.Error(),
})
return
}
}
// Write headers for the data stream!
aisdk.WriteDataStreamHeaders(w)
// Insert the user-requested message into the database!
raw, err := json.Marshal([]aisdk.Message{req.Message})
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to marshal chat message",
Detail: err.Error(),
})
return
}
_, err = api.Database.InsertChatMessages(ctx, database.InsertChatMessagesParams{
ChatID: chat.ID,
CreatedAt: dbtime.Now(),
Model: req.Model,
Provider: provider.Provider,
Content: raw,
})
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to insert chat messages",
Detail: err.Error(),
})
return
}
deps, err := toolsdk.NewDeps(client)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to create tool dependencies",
Detail: err.Error(),
})
return
}
for {
var acc aisdk.DataStreamAccumulator
stream, err := provider.StreamFunc(ctx, ai.StreamOptions{
Model: req.Model,
Messages: messages,
Tools: tools,
SystemPrompt: `You are a chat assistant for Coder - an open-source platform for creating and managing cloud development environments on any infrastructure. You are expected to be precise, concise, and helpful.
You are running as an agent - please keep going until the user's query is completely resolved, before ending your turn and yielding back to the user. Only terminate your turn when you are sure that the problem is solved. Do NOT guess or make up an answer.`,
})
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to create stream",
Detail: err.Error(),
})
return
}
stream = stream.WithToolCalling(func(toolCall aisdk.ToolCall) aisdk.ToolCallResult {
tool, ok := handlers[toolCall.Name]
if !ok {
return nil
}
toolArgs, err := json.Marshal(toolCall.Args)
if err != nil {
return nil
}
result, err := tool(ctx, deps, toolArgs)
if err != nil {
return map[string]any{
"error": err.Error(),
}
}
return result
}).WithAccumulator(&acc)
err = stream.Pipe(w)
if err != nil {
// The client disppeared!
api.Logger.Error(ctx, "stream pipe error", "error", err)
return
}
// acc.Messages() may sometimes return nil. Serializing this
// will cause a pq error: "cannot extract elements from a scalar".
newMessages := append([]aisdk.Message{}, acc.Messages()...)
if len(newMessages) > 0 {
raw, err := json.Marshal(newMessages)
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to marshal chat message",
Detail: err.Error(),
})
return
}
messages = append(messages, newMessages...)
// Insert these messages into the database!
_, err = api.Database.InsertChatMessages(ctx, database.InsertChatMessagesParams{
ChatID: chat.ID,
CreatedAt: dbtime.Now(),
Model: req.Model,
Provider: provider.Provider,
Content: raw,
})
if err != nil {
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{
Message: "Failed to insert chat messages",
Detail: err.Error(),
})
return
}
}
if acc.FinishReason() == aisdk.FinishReasonToolCalls {
continue
}
break
}
}