fix(openai): 修复 prepareRequestBody 异步调用问题并添加监控钩子

将 prepareRequestBody 方法改为异步以支持插件钩子调用,修复因缺少 await 导致的潜在问题。同时添加内部请求转换监控钩子,便于跟踪请求处理流程。
This commit is contained in:
hex2077 2026-03-08 22:23:43 +08:00
parent 7726ca1f38
commit 8fb4d59b23
2 changed files with 21 additions and 4 deletions

View file

@ -1 +1 @@
2.10.9.1 2.10.9.2

View file

@ -172,7 +172,7 @@ export class CodexApiService {
} }
const url = `${this.baseUrl}/responses`; const url = `${this.baseUrl}/responses`;
const body = this.prepareRequestBody(selectedModel, requestBody, true); const body = await this.prepareRequestBody(selectedModel, requestBody, true);
const headers = this.buildHeaders(body.prompt_cache_key, true); const headers = this.buildHeaders(body.prompt_cache_key, true);
try { try {
@ -253,7 +253,7 @@ export class CodexApiService {
} }
const url = `${this.baseUrl}/responses`; const url = `${this.baseUrl}/responses`;
const body = this.prepareRequestBody(selectedModel, requestBody, true); const body = await this.prepareRequestBody(selectedModel, requestBody, true);
const headers = this.buildHeaders(body.prompt_cache_key, true); const headers = this.buildHeaders(body.prompt_cache_key, true);
try { try {
@ -334,7 +334,7 @@ export class CodexApiService {
/** /**
* 准备请求体 * 准备请求体
*/ */
prepareRequestBody(model, requestBody, stream) { async prepareRequestBody(model, requestBody, stream) {
// 提取 metadata 并从请求体中移除,避免透传到上游 // 提取 metadata 并从请求体中移除,避免透传到上游
const metadata = requestBody.metadata || {}; const metadata = requestBody.metadata || {};
@ -393,6 +393,23 @@ export class CodexApiService {
delete result.service_tier; delete result.service_tier;
} }
// 监控钩子:内部请求转换
if (this.config?._monitorRequestId) {
try {
const { getPluginManager } = await import('../../core/plugin-manager.js');
const pluginManager = getPluginManager();
if (pluginManager) {
await pluginManager.executeHook('onInternalRequestConverted', {
requestId: this.config._monitorRequestId,
internalRequest: result,
converterName: 'prepareRequestBody'
});
}
} catch (e) {
logger.error('[Codex] Error calling onInternalRequestConverted hook:', e.message);
}
}
return result; return result;
} }