-
Notifications
You must be signed in to change notification settings - Fork 260
feat(mcp): ask subagent #814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4ed56af
wip
brendan-kellam ee6a423
wip
brendan-kellam 865d88e
add back selection
brendan-kellam f1e264f
switch to using pagination api for listing repos. Remove search repos…
brendan-kellam 6b71879
fix client
brendan-kellam e689590
improve search tool
brendan-kellam f04dd33
add apiHandler to chat/blocking
brendan-kellam 2654454
feedback
brendan-kellam 1561c72
Add wa_chat_message_sent event to ask
brendan-kellam e8f9d29
add wa_chat_tool_used event
brendan-kellam 5b89896
feedback
brendan-kellam 2f1b008
docs
brendan-kellam c3cf085
add mechanism for specifying language model explicitly
brendan-kellam ac4aac7
some changelog
brendan-kellam 0833896
feedback
brendan-kellam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
packages/web/src/app/api/(server)/chat/blocking/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { sew } from "@/actions"; | ||
| import { _getConfiguredLanguageModelsFull, _getAISDKLanguageModelAndOptions, updateChatMessages, generateAndUpdateChatNameFromMessage } from "@/features/chat/actions"; | ||
| import { SBChatMessage, SearchScope } from "@/features/chat/types"; | ||
| import { convertLLMOutputToPortableMarkdown, getAnswerPartFromAssistantMessage } from "@/features/chat/utils"; | ||
| import { ErrorCode } from "@/lib/errorCodes"; | ||
| import { requestBodySchemaValidationError, ServiceError, ServiceErrorException, serviceErrorResponse } from "@/lib/serviceError"; | ||
| import { isServiceError } from "@/lib/utils"; | ||
| import { getBaseUrl } from "@/lib/utils.server"; | ||
| import { withOptionalAuthV2 } from "@/withAuthV2"; | ||
| import { ChatVisibility, Prisma } from "@sourcebot/db"; | ||
| import { createLogger } from "@sourcebot/shared"; | ||
| import { randomUUID } from "crypto"; | ||
| import { StatusCodes } from "http-status-codes"; | ||
| import { headers } from "next/headers"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { z } from "zod"; | ||
| import { createMessageStream } from "../route"; | ||
| import { InferUIMessageChunk, UITools, UIDataTypes, UIMessage } from "ai"; | ||
| import { apiHandler } from "@/lib/apiHandler"; | ||
|
|
||
| const logger = createLogger('chat-blocking-api'); | ||
|
|
||
| /** | ||
| * Request schema for the blocking chat API. | ||
| * This is a simpler interface designed for MCP and other programmatic integrations. | ||
| */ | ||
| const blockingChatRequestSchema = z.object({ | ||
| query: z | ||
| .string() | ||
| .describe("The query to ask about the codebase."), | ||
| repos: z | ||
| .array(z.string()) | ||
| .optional() | ||
| .describe("The repositories that are accessible to the agent during the chat. If not provided, all repositories are accessible."), | ||
| }); | ||
|
|
||
| /** | ||
| * Response schema for the blocking chat API. | ||
| */ | ||
| interface BlockingChatResponse { | ||
| answer: string; | ||
| chatId: string; | ||
| chatUrl: string; | ||
| } | ||
|
|
||
| /** | ||
| * POST /api/chat/blocking | ||
| * | ||
| * A blocking (non-streaming) chat endpoint designed for MCP and other integrations. | ||
| * Creates a chat session, runs the agent to completion, and returns the final answer. | ||
| * | ||
| * The chat session is persisted to the database, allowing users to view the full | ||
| * conversation (including tool calls and reasoning) in the web UI. | ||
| */ | ||
| export const POST = apiHandler(async (request: NextRequest) => { | ||
| const requestBody = await request.json(); | ||
| const parsed = await blockingChatRequestSchema.safeParseAsync(requestBody); | ||
|
|
||
| if (!parsed.success) { | ||
| return serviceErrorResponse(requestBodySchemaValidationError(parsed.error)); | ||
| } | ||
|
|
||
| const { query, repos = [] } = parsed.data; | ||
|
|
||
| const response: BlockingChatResponse | ServiceError = await sew(() => | ||
| withOptionalAuthV2(async ({ org, user, prisma }) => { | ||
| // Get all configured language models | ||
| const configuredModels = await _getConfiguredLanguageModelsFull(); | ||
| if (configuredModels.length === 0) { | ||
| return { | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: "No language models are configured. Please configure at least one language model.", | ||
| } satisfies ServiceError; | ||
| } | ||
|
|
||
| // @todo: we should probably have a option of passing the language model | ||
| // into the request body. For now, just use the first configured model. | ||
| const languageModelConfig = configuredModels[0]; | ||
|
|
||
| const { model, providerOptions } = await _getAISDKLanguageModelAndOptions(languageModelConfig); | ||
| const modelName = languageModelConfig.displayName ?? languageModelConfig.model; | ||
|
|
||
| // Create a new chat session | ||
| const chat = await prisma.chat.create({ | ||
| data: { | ||
| orgId: org.id, | ||
| createdById: user?.id, | ||
| visibility: ChatVisibility.PRIVATE, | ||
| messages: [] as unknown as Prisma.InputJsonValue, | ||
| }, | ||
| }); | ||
|
|
||
| // Run the agent to completion | ||
| logger.debug(`Starting blocking agent for chat ${chat.id}`, { | ||
| chatId: chat.id, | ||
| query: query.substring(0, 100), | ||
| model: modelName, | ||
| }); | ||
|
|
||
| // Create the initial user message | ||
| const userMessage: SBChatMessage = { | ||
| id: randomUUID(), | ||
| role: 'user', | ||
| parts: [{ type: 'text', text: query }], | ||
| }; | ||
|
|
||
| const selectedSearchScopes = await Promise.all(repos.map(async (repo) => { | ||
| const repoDB = await prisma.repo.findFirst({ | ||
| where: { | ||
| name: repo, | ||
| }, | ||
| }); | ||
|
|
||
| if (!repoDB) { | ||
| throw new ServiceErrorException({ | ||
| statusCode: StatusCodes.BAD_REQUEST, | ||
| errorCode: ErrorCode.INVALID_REQUEST_BODY, | ||
| message: `Repository '${repo}' not found.`, | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| type: 'repo', | ||
| value: repoDB.name, | ||
| name: repoDB.displayName ?? repoDB.name.split('/').pop() ?? repoDB.name, | ||
| codeHostType: repoDB.external_codeHostType, | ||
| } satisfies SearchScope; | ||
| })); | ||
|
|
||
| // We'll capture the final messages and usage from the stream | ||
| let finalMessages: SBChatMessage[] = []; | ||
|
|
||
| const stream = await createMessageStream({ | ||
| messages: [userMessage], | ||
| selectedSearchScopes, | ||
| model, | ||
| modelName, | ||
| modelProviderOptions: providerOptions, | ||
| orgId: org.id, | ||
| prisma, | ||
| onFinish: async ({ messages }) => { | ||
| finalMessages = messages; | ||
| }, | ||
| }) | ||
|
|
||
| await Promise.all([ | ||
| // Consume the stream fully to trigger onFinish | ||
| blockStreamUntilFinish(stream), | ||
| // Generate and update the chat name | ||
| generateAndUpdateChatNameFromMessage({ | ||
| chatId: chat.id, | ||
| languageModelId: languageModelConfig.model, | ||
| message: query, | ||
| }) | ||
| ]); | ||
|
|
||
| // Persist the messages to the chat | ||
| await updateChatMessages({ | ||
| chatId: chat.id, | ||
| messages: finalMessages, | ||
| }); | ||
|
|
||
| // Extract the answer text from the assistant message | ||
| const assistantMessage = finalMessages.find(m => m.role === 'assistant'); | ||
| const answerPart = assistantMessage | ||
| ? getAnswerPartFromAssistantMessage(assistantMessage, false) | ||
| : undefined; | ||
| const answerText = answerPart?.text ?? ''; | ||
|
|
||
| // Convert to portable markdown (replaces @file: references with markdown links) | ||
| const portableAnswer = convertLLMOutputToPortableMarkdown(answerText); | ||
|
|
||
| // Build the chat URL | ||
| const headersList = await headers(); | ||
| const baseUrl = getBaseUrl(headersList); | ||
| const chatUrl = `${baseUrl}/${org.domain}/chat/${chat.id}`; | ||
|
|
||
| logger.debug(`Completed blocking agent for chat ${chat.id}`, { | ||
| chatId: chat.id, | ||
| }); | ||
|
|
||
| return { | ||
| answer: portableAnswer, | ||
| chatId: chat.id, | ||
| chatUrl, | ||
| } satisfies BlockingChatResponse; | ||
| }) | ||
| ); | ||
|
|
||
| if (isServiceError(response)) { | ||
| return serviceErrorResponse(response); | ||
| } | ||
|
|
||
| return NextResponse.json(response); | ||
| }); | ||
|
|
||
| const blockStreamUntilFinish = async <T extends UIMessage<unknown, UIDataTypes, UITools>>(stream: ReadableStream<InferUIMessageChunk<T>>) => { | ||
| const reader = stream.getReader(); | ||
| while (true as const) { | ||
| const { done } = await reader.read(); | ||
| if (done) break; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.