-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathroute.ts
More file actions
178 lines (156 loc) · 7.08 KB
/
Copy pathroute.ts
File metadata and controls
178 lines (156 loc) · 7.08 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
import { sew } from "@/middleware/sew";
import { createMessageStream, ResolvedChatUserPreferences } from "@/features/chat/agent";
import { additionalChatRequestParamsSchema } from "@/features/chat/types";
import { getLanguageModelKey } from "@/features/chat/utils";
import { getAISDKLanguageModelAndOptions, getConfiguredLanguageModels, isOwnerOfChat, updateChatMessages } from "@/features/chat/utils.server";
import { chatPreferencesSchema } from "@/features/chat/userPreferences";
import { apiHandler } from "@/lib/apiHandler";
import { ErrorCode } from "@/lib/errorCodes";
import { captureEvent } from "@/lib/posthog";
import { notFound, requestBodySchemaValidationError, ServiceError, serviceErrorResponse } from "@/lib/serviceError";
import { isServiceError } from "@/lib/utils";
import { withOptionalAuth } from "@/middleware/withAuth";
import * as Sentry from "@sentry/nextjs";
import { createLogger, env } from "@sourcebot/shared";
import {
createUIMessageStreamResponse
} from "ai";
import { StatusCodes } from "http-status-codes";
import { NextRequest } from "next/server";
import { z } from "zod";
const logger = createLogger('chat-api');
const chatRequestSchema = z.object({
messages: z.array(z.any()),
id: z.string(),
...additionalChatRequestParamsSchema.shape,
})
export const POST = apiHandler(async (req: NextRequest) => {
const requestBody = await req.json();
const parsed = await chatRequestSchema.safeParseAsync(requestBody);
if (!parsed.success) {
return serviceErrorResponse(requestBodySchemaValidationError(parsed.error));
}
const { messages, id, selectedSearchScopes, languageModel: _languageModel } = parsed.data;
// @note: a bit of type massaging is required here since the
// zod schema does not enum on `model` or `provider`.
// @see: chat/types.ts
const languageModel = _languageModel;
const response = await sew(() =>
withOptionalAuth(async ({ org, user, prisma }) => {
// Validate that the chat exists.
const chat = await prisma.chat.findUnique({
where: {
orgId: org.id,
id,
},
});
if (!chat) {
return notFound();
}
// Check ownership - only the owner can send messages
const isOwner = await isOwnerOfChat(chat, user);
if (!isOwner) {
return {
statusCode: StatusCodes.FORBIDDEN,
errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS,
message: 'Only the owner of a chat can send messages.',
} satisfies ServiceError;
}
// From the language model ID, attempt to find the
// corresponding config in `config.json`.
const languageModelConfig =
(await getConfiguredLanguageModels())
.find((model) => getLanguageModelKey(model) === getLanguageModelKey(languageModel));
if (!languageModelConfig) {
return {
statusCode: StatusCodes.BAD_REQUEST,
errorCode: ErrorCode.INVALID_REQUEST_BODY,
message: `Language model ${languageModel.model} is not configured.`,
} satisfies ServiceError;
}
const { model, providerOptions, temperature } = await getAISDKLanguageModelAndOptions(languageModelConfig);
const expandedRepos = (await Promise.all(selectedSearchScopes.map(async (scope) => {
if (scope.type === 'repo') return [scope.value];
if (scope.type === 'reposet') {
const reposet = await prisma.searchContext.findFirst({
where: { orgId: org.id, name: scope.value },
include: { repos: true }
});
return reposet ? reposet.repos.map(r => r.name) : [];
}
return [];
}))).flat();
const source = req.headers.get('X-Sourcebot-Client-Source') ?? undefined;
// Load the user's chat-style preferences. Anonymous users skip this
// entirely so the agent uses default behavior with no
// `<user_preferences>` block in the system prompt.
let userPreferences: ResolvedChatUserPreferences | undefined;
if (user) {
const row = await prisma.user.findUnique({
where: { id: user.id },
select: {
chatPreferences: true,
chatCustomInstructions: true,
},
});
if (row) {
const parsed = chatPreferencesSchema.safeParse(row.chatPreferences);
// Cast back to the narrower literal-union map: the schema
// is built dynamically so its inferred type widens to
// `string`, but the runtime validation still constrains
// each value to its per-dimension level list.
userPreferences = {
preferences: parsed.success
? (parsed.data as ResolvedChatUserPreferences["preferences"])
: {},
customInstructions: row.chatCustomInstructions,
};
}
}
await captureEvent('ask_message_sent', {
chatId: id,
messageCount: messages.length,
selectedReposCount: expandedRepos.length,
source,
...(env.EXPERIMENT_ASK_GH_ENABLED === 'true' ? { selectedRepos: expandedRepos } : {}),
});
const stream = await createMessageStream({
chatId: id,
messages,
metadata: {
selectedSearchScopes,
},
selectedRepos: expandedRepos,
model,
modelName: languageModelConfig.displayName ?? languageModelConfig.model,
modelProviderOptions: providerOptions,
modelTemperature: temperature,
userPreferences,
onFinish: async ({ messages }) => {
await updateChatMessages({ chatId: id, messages, prisma });
},
onError: (error: unknown) => {
logger.error(error);
Sentry.captureException(error);
if (error == null) {
return 'unknown error';
}
if (typeof error === 'string') {
return error;
}
if (error instanceof Error) {
return error.message;
}
return JSON.stringify(error);
}
});
return createUIMessageStreamResponse({
stream,
});
})
)
if (isServiceError(response)) {
return serviceErrorResponse(response);
}
return response;
});