Unverified 提交 cb60a056 authored 作者: Will Chen's avatar Will Chen 提交者: GitHub

Send request id for LLM engine calls (#659)

上级 29065335
import { v4 as uuidv4 } from "uuid";
import { ipcMain } from "electron"; import { ipcMain } from "electron";
import { import {
CoreMessage, CoreMessage,
...@@ -534,12 +535,16 @@ This conversation includes one or more image attachments. When the user uploads ...@@ -534,12 +535,16 @@ This conversation includes one or more image attachments. When the user uploads
chatMessages: CoreMessage[]; chatMessages: CoreMessage[];
modelClient: ModelClient; modelClient: ModelClient;
}) => { }) => {
const dyadRequestId = uuidv4();
return streamText({ return streamText({
maxTokens: await getMaxTokens(settings.selectedModel), maxTokens: await getMaxTokens(settings.selectedModel),
temperature: 0, temperature: 0,
maxRetries: 2, maxRetries: 2,
model: modelClient.model, model: modelClient.model,
providerOptions: { providerOptions: {
"dyad-engine": {
dyadRequestId,
},
"dyad-gateway": getExtraProviderOptions( "dyad-gateway": getExtraProviderOptions(
modelClient.builtinProviderId, modelClient.builtinProviderId,
settings, settings,
...@@ -560,9 +565,12 @@ This conversation includes one or more image attachments. When the user uploads ...@@ -560,9 +565,12 @@ This conversation includes one or more image attachments. When the user uploads
errorMessage += "\n\nDetails: " + responseBody; errorMessage += "\n\nDetails: " + responseBody;
} }
const message = errorMessage || JSON.stringify(error); const message = errorMessage || JSON.stringify(error);
const requestIdPrefix = isEngineEnabled
? `[Request ID: ${dyadRequestId}] `
: "";
event.sender.send( event.sender.send(
"chat:response:error", "chat:response:error",
`Sorry, there was an error from the AI: ${message}`, `Sorry, there was an error from the AI: ${requestIdPrefix}${message}`,
); );
// Clean up the abort controller // Clean up the abort controller
activeStreams.delete(req.chatId); activeStreams.delete(req.chatId);
......
...@@ -75,6 +75,10 @@ export function createDyadEngine( ...@@ -75,6 +75,10 @@ export function createDyadEngine(
): DyadEngineProvider { ): DyadEngineProvider {
const baseURL = withoutTrailingSlash(options.baseURL); const baseURL = withoutTrailingSlash(options.baseURL);
logger.info("creating dyad engine with baseURL", baseURL); logger.info("creating dyad engine with baseURL", baseURL);
// Track request ID attempts
const requestIdAttempts = new Map<string, number>();
const getHeaders = () => ({ const getHeaders = () => ({
Authorization: `Bearer ${loadApiKey({ Authorization: `Bearer ${loadApiKey({
apiKey: options.apiKey, apiKey: options.apiKey,
...@@ -91,8 +95,8 @@ export function createDyadEngine( ...@@ -91,8 +95,8 @@ export function createDyadEngine(
fetch?: FetchFunction; fetch?: FetchFunction;
} }
const getCommonModelConfig = (modelType: string): CommonModelConfig => ({ const getCommonModelConfig = (): CommonModelConfig => ({
provider: `example.${modelType}`, provider: `dyad-engine`,
url: ({ path }) => { url: ({ path }) => {
const url = new URL(`${baseURL}${path}`); const url = new URL(`${baseURL}${path}`);
if (options.queryParams) { if (options.queryParams) {
...@@ -113,7 +117,7 @@ export function createDyadEngine( ...@@ -113,7 +117,7 @@ export function createDyadEngine(
// Create configuration with file handling // Create configuration with file handling
const config = { const config = {
...getCommonModelConfig("chat"), ...getCommonModelConfig(),
defaultObjectGenerationMode: defaultObjectGenerationMode:
"tool" as LanguageModelV1ObjectGenerationMode, "tool" as LanguageModelV1ObjectGenerationMode,
// Custom fetch implementation that adds files to the request // Custom fetch implementation that adds files to the request
...@@ -132,6 +136,18 @@ export function createDyadEngine( ...@@ -132,6 +136,18 @@ export function createDyadEngine(
options.settings, options.settings,
), ),
}; };
const requestId = parsedBody.dyadRequestId;
if ("dyadRequestId" in parsedBody) {
delete parsedBody.dyadRequestId;
}
// Track and modify requestId with attempt number
let modifiedRequestId = requestId;
if (requestId) {
const currentAttempt = (requestIdAttempts.get(requestId) || 0) + 1;
requestIdAttempts.set(requestId, currentAttempt);
modifiedRequestId = `${requestId}:attempt-${currentAttempt}`;
}
// Add files to the request if they exist // Add files to the request if they exist
if (files?.length) { if (files?.length) {
...@@ -143,9 +159,15 @@ export function createDyadEngine( ...@@ -143,9 +159,15 @@ export function createDyadEngine(
}; };
} }
// Return modified request with files included // Return modified request with files included and requestId in headers
const modifiedInit = { const modifiedInit = {
...init, ...init,
headers: {
...init.headers,
...(modifiedRequestId && {
"X-Dyad-Request-Id": modifiedRequestId,
}),
},
body: JSON.stringify(parsedBody), body: JSON.stringify(parsedBody),
}; };
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论