-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathroute.ts
More file actions
149 lines (128 loc) · 5.63 KB
/
route.ts
File metadata and controls
149 lines (128 loc) · 5.63 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
import { sew } from "@/middleware/sew";
import { createMessageStream } 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 { 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;
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,
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;
});