feat(proxy): add OpenAI Responses API compatibility for Open CoDesign
- Normalize Responses API tool format ({name, parameters} at top level)
to Chat Completions format ({function: {name, parameters}})
- Handle 'developer' role as system prompt (OpenAI's newer equivalent)
- Add null/non-array guards to convertOpenAIPartsToAnthropicContent and
extractOpenAITextContent to prevent 'content is not iterable' crashes
- Support input_text, output_text, and reasoning item types in Responses
API input parsing
- Forward reasoning.effort to Claude adaptive thinking config
- Extend JSON schema sanitization blacklist with draft-2020 keywords
(patternProperties, , unevaluatedProperties, etc.)
- Add fallback for untyped items with role/content in Responses API input
- Add diagnostic logging for /v1/responses input conversion
This commit is contained in:
1 parent
9bf7c46ff6
commit
68474b56dc
3 files changed
+253
-33
No files matched your search
@@ -162,6 +162,11 @@ function cleanJsonSchemaRecursive(value: any) {
|
|||||||
// 4. Physically remove "hard" blacklist items that interfere with generation
|
// 4. Physically remove "hard" blacklist items that interfere with generation
|
||||||
const hardRemoveFields = [
|
const hardRemoveFields = [
|
||||||
'$schema',
|
'$schema',
|
||||||
|
'$id',
|
||||||
|
'$comment',
|
||||||
|
'$anchor',
|
||||||
|
'$dynamicRef',
|
||||||
|
'$dynamicAnchor',
|
||||||
'additionalProperties',
|
'additionalProperties',
|
||||||
'enumCaseInsensitive',
|
'enumCaseInsensitive',
|
||||||
'enumNormalizeWhitespace',
|
'enumNormalizeWhitespace',
|
||||||
@@ -170,7 +175,15 @@ function cleanJsonSchemaRecursive(value: any) {
|
|||||||
'const',
|
'const',
|
||||||
'examples',
|
'examples',
|
||||||
// Advanced logic fields common in MCP tools but unsupported by Gemini
|
// Advanced logic fields common in MCP tools but unsupported by Gemini
|
||||||
|
'patternProperties',
|
||||||
'propertyNames',
|
'propertyNames',
|
||||||
|
'additionalItems',
|
||||||
|
'prefixItems',
|
||||||
|
'contains',
|
||||||
|
'minContains',
|
||||||
|
'maxContains',
|
||||||
|
'unevaluatedProperties',
|
||||||
|
'unevaluatedItems',
|
||||||
'anyOf',
|
'anyOf',
|
||||||
'oneOf',
|
'oneOf',
|
||||||
'allOf',
|
'allOf',
|
||||||
|
|||||||
@@ -46,7 +46,9 @@ export class ProxyController {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(ProxyService) private readonly proxyService: ProxyService,
|
@Inject(ProxyService) private readonly proxyService: ProxyService,
|
||||||
@Optional() @Inject(ProxyReplayService) private readonly proxyReplayService?: ProxyReplayService,
|
@Optional()
|
||||||
|
@Inject(ProxyReplayService)
|
||||||
|
private readonly proxyReplayService?: ProxyReplayService,
|
||||||
@Optional() @Inject(TokenManagerService) private readonly tokenManager?: TokenManagerService,
|
@Optional() @Inject(TokenManagerService) private readonly tokenManager?: TokenManagerService,
|
||||||
@Optional() @Inject(ProxyMetricsService) private readonly proxyMetrics?: ProxyMetricsService,
|
@Optional() @Inject(ProxyMetricsService) private readonly proxyMetrics?: ProxyMetricsService,
|
||||||
) {}
|
) {}
|
||||||
@@ -113,7 +115,9 @@ export class ProxyController {
|
|||||||
const metrics = this.proxyMetrics?.getMetrics();
|
const metrics = this.proxyMetrics?.getMetrics();
|
||||||
const circuitBreakerStatus = this.tokenManager?.getCircuitBreakerStatus() ?? {};
|
const circuitBreakerStatus = this.tokenManager?.getCircuitBreakerStatus() ?? {};
|
||||||
const accountCount = Object.keys(circuitBreakerStatus).length;
|
const accountCount = Object.keys(circuitBreakerStatus).length;
|
||||||
const healthyAccounts = Object.values(circuitBreakerStatus).filter(s => s.state === 'healthy').length;
|
const healthyAccounts = Object.values(circuitBreakerStatus).filter(
|
||||||
|
(s) => s.state === 'healthy',
|
||||||
|
).length;
|
||||||
|
|
||||||
res.status(HttpStatus.OK).send({
|
res.status(HttpStatus.OK).send({
|
||||||
status: 'healthy',
|
status: 'healthy',
|
||||||
@@ -162,7 +166,8 @@ export class ProxyController {
|
|||||||
}
|
}
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
try {
|
try {
|
||||||
const requests = this.proxyReplayService?.getRecentRequests(limit ? parseInt(limit, 10) : undefined) ?? [];
|
const requests =
|
||||||
|
this.proxyReplayService?.getRecentRequests(limit ? parseInt(limit, 10) : undefined) ?? [];
|
||||||
this.setProxyHeaders(res, { requestId: uuidv4(), accountId: '', retryCount: 0, model: '' });
|
this.setProxyHeaders(res, { requestId: uuidv4(), accountId: '', retryCount: 0, model: '' });
|
||||||
res.status(HttpStatus.OK).send({
|
res.status(HttpStatus.OK).send({
|
||||||
object: 'list',
|
object: 'list',
|
||||||
@@ -226,7 +231,8 @@ export class ProxyController {
|
|||||||
const capabilitiesMap = this.buildCapabilitiesMap();
|
const capabilitiesMap = this.buildCapabilitiesMap();
|
||||||
|
|
||||||
const data = modelIds.map((id) => {
|
const data = modelIds.map((id) => {
|
||||||
const baseId = Object.keys(capabilitiesMap).find((key) => id.includes(key)) ?? 'gemini-3-flash';
|
const baseId =
|
||||||
|
Object.keys(capabilitiesMap).find((key) => id.includes(key)) ?? 'gemini-3-flash';
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
object: 'model_capability',
|
object: 'model_capability',
|
||||||
@@ -247,7 +253,9 @@ export class ProxyController {
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = error instanceof Error ? error.message : 'Failed to list model capabilities';
|
const message = error instanceof Error ? error.message : 'Failed to list model capabilities';
|
||||||
this.logger.error(message, error instanceof Error ? error.stack : undefined);
|
this.logger.error(message, error instanceof Error ? error.stack : undefined);
|
||||||
res.status(HttpStatus.INTERNAL_SERVER_ERROR).send({ error: { message, type: 'internal_server_error' } });
|
res
|
||||||
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.send({ error: { message, type: 'internal_server_error' } });
|
||||||
this.proxyMetrics?.recordRequest({
|
this.proxyMetrics?.recordRequest({
|
||||||
timestamp: startTime,
|
timestamp: startTime,
|
||||||
duration: Date.now() - startTime,
|
duration: Date.now() - startTime,
|
||||||
@@ -257,9 +265,27 @@ export class ProxyController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildCapabilitiesMap(): Record<string, { vision: boolean; streaming: boolean; jsonMode: boolean; audio: boolean; imageGeneration: boolean }> {
|
private buildCapabilitiesMap(): Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
vision: boolean;
|
||||||
|
streaming: boolean;
|
||||||
|
jsonMode: boolean;
|
||||||
|
audio: boolean;
|
||||||
|
imageGeneration: boolean;
|
||||||
|
}
|
||||||
|
> {
|
||||||
const allTokens = this.tokenManager?.getAllTokens() ?? [];
|
const allTokens = this.tokenManager?.getAllTokens() ?? [];
|
||||||
const map: Record<string, { vision: boolean; streaming: boolean; jsonMode: boolean; audio: boolean; imageGeneration: boolean }> = {};
|
const map: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
vision: boolean;
|
||||||
|
streaming: boolean;
|
||||||
|
jsonMode: boolean;
|
||||||
|
audio: boolean;
|
||||||
|
imageGeneration: boolean;
|
||||||
|
}
|
||||||
|
> = {};
|
||||||
|
|
||||||
for (const [, tokenData] of allTokens) {
|
for (const [, tokenData] of allTokens) {
|
||||||
for (const [modelId, modelInfo] of Object.entries(tokenData.quota?.models ?? {})) {
|
for (const [modelId, modelInfo] of Object.entries(tokenData.quota?.models ?? {})) {
|
||||||
@@ -278,7 +304,13 @@ export class ProxyController {
|
|||||||
|
|
||||||
if (Object.keys(map).length === 0) {
|
if (Object.keys(map).length === 0) {
|
||||||
return {
|
return {
|
||||||
'gemini-3-flash': { vision: true, streaming: true, jsonMode: true, audio: false, imageGeneration: false },
|
'gemini-3-flash': {
|
||||||
|
vision: true,
|
||||||
|
streaming: true,
|
||||||
|
jsonMode: true,
|
||||||
|
audio: false,
|
||||||
|
imageGeneration: false,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,14 +334,14 @@ export class ProxyController {
|
|||||||
const request = requests[i];
|
const request = requests[i];
|
||||||
const proxyContext: ProxyRequestContext = {};
|
const proxyContext: ProxyRequestContext = {};
|
||||||
try {
|
try {
|
||||||
const result = await this.proxyService.handleChatCompletions(
|
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||||
request,
|
|
||||||
proxyContext,
|
|
||||||
);
|
|
||||||
if (this.isObservableLike(result)) {
|
if (this.isObservableLike(result)) {
|
||||||
responses.push({
|
responses.push({
|
||||||
index: i,
|
index: i,
|
||||||
error: { message: 'Streaming not supported in batch mode', type: 'invalid_request_error' },
|
error: {
|
||||||
|
message: 'Streaming not supported in batch mode',
|
||||||
|
type: 'invalid_request_error',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
responses.push({
|
responses.push({
|
||||||
@@ -462,7 +494,14 @@ export class ProxyController {
|
|||||||
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, undefined, '/v1/completions', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
undefined,
|
||||||
|
'/v1/completions',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -508,6 +547,7 @@ export class ProxyController {
|
|||||||
temperature?: number;
|
temperature?: number;
|
||||||
top_p?: number;
|
top_p?: number;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
|
reasoning?: { effort?: string };
|
||||||
},
|
},
|
||||||
@Req() req: FastifyRequest,
|
@Req() req: FastifyRequest,
|
||||||
@Res() res: FastifyReply,
|
@Res() res: FastifyReply,
|
||||||
@@ -527,11 +567,24 @@ export class ProxyController {
|
|||||||
const proxyContext: ProxyRequestContext = {};
|
const proxyContext: ProxyRequestContext = {};
|
||||||
let isStream = false;
|
let isStream = false;
|
||||||
|
|
||||||
|
this.logger.log(
|
||||||
|
`[/v1/responses] Converted input: ${Array.isArray(body.input) ? body.input.length : 0} items → ${request.messages.length} messages, ` +
|
||||||
|
`model=${request.model}, stream=${body.stream}, tools=${request.tools?.length ?? 0}, ` +
|
||||||
|
`reasoning=${body.reasoning?.effort ?? 'default'}`,
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, undefined, '/v1/responses', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
undefined,
|
||||||
|
'/v1/responses',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -840,14 +893,18 @@ export class ProxyController {
|
|||||||
const proxyContext: ProxyRequestContext = {};
|
const proxyContext: ProxyRequestContext = {};
|
||||||
let isStream = false;
|
let isStream = false;
|
||||||
try {
|
try {
|
||||||
const result = await this.proxyService.handleChatCompletions(
|
const result = await this.proxyService.handleChatCompletions(body, proxyContext);
|
||||||
body,
|
|
||||||
proxyContext,
|
|
||||||
);
|
|
||||||
|
|
||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, requestId, '/v1/chat/completions', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
requestId,
|
||||||
|
'/v1/chat/completions',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
@@ -918,7 +975,14 @@ export class ProxyController {
|
|||||||
isStream = !!(body.stream && this.isObservableLike(result));
|
isStream = !!(body.stream && this.isObservableLike(result));
|
||||||
|
|
||||||
if (isStream) {
|
if (isStream) {
|
||||||
this.writeSseResponse(res, result as Observable<unknown>, undefined, '/v1/messages', startTime, proxyContext);
|
this.writeSseResponse(
|
||||||
|
res,
|
||||||
|
result as Observable<unknown>,
|
||||||
|
undefined,
|
||||||
|
'/v1/messages',
|
||||||
|
startTime,
|
||||||
|
proxyContext,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
@@ -1017,6 +1081,7 @@ export class ProxyController {
|
|||||||
temperature?: number;
|
temperature?: number;
|
||||||
top_p?: number;
|
top_p?: number;
|
||||||
stream?: boolean;
|
stream?: boolean;
|
||||||
|
reasoning?: { effort?: string };
|
||||||
}): OpenAIChatRequest {
|
}): OpenAIChatRequest {
|
||||||
const messages: OpenAIChatRequest['messages'] = [];
|
const messages: OpenAIChatRequest['messages'] = [];
|
||||||
if (isString(body.instructions) && !isEmpty(body.instructions.trim())) {
|
if (isString(body.instructions) && !isEmpty(body.instructions.trim())) {
|
||||||
@@ -1054,21 +1119,59 @@ export class ProxyController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const item of inputItems) {
|
for (let idx = 0; idx < inputItems.length; idx++) {
|
||||||
|
const item = inputItems[idx];
|
||||||
const itemObj = this.toRecord(item);
|
const itemObj = this.toRecord(item);
|
||||||
if (!itemObj) {
|
if (!itemObj) {
|
||||||
|
this.logger.debug(`[/v1/responses] input[${idx}]: skipped (not an object)`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const type = this.asString(itemObj.type);
|
const type = this.asString(itemObj.type);
|
||||||
|
const role = this.asString(itemObj.role);
|
||||||
|
|
||||||
|
// Items without a type but with role/content — treat as regular chat messages
|
||||||
|
// (common in clients like Open CoDesign that mix chat and responses formats)
|
||||||
if (!type) {
|
if (!type) {
|
||||||
|
if (role && itemObj.content !== undefined) {
|
||||||
|
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
||||||
|
messages.push({ role, content });
|
||||||
|
this.logger.debug(`[/v1/responses] input[${idx}]: no-type fallback → role=${role}`);
|
||||||
|
} else {
|
||||||
|
this.logger.warn(
|
||||||
|
`[/v1/responses] input[${idx}]: dropped (no type, no role/content). Keys: ${Object.keys(itemObj).join(',')}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'message') {
|
if (type === 'message') {
|
||||||
const role = this.asString(itemObj.role) ?? 'user';
|
const messageRole = role ?? 'user';
|
||||||
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
||||||
messages.push({ role, content });
|
messages.push({ role: messageRole, content });
|
||||||
|
this.logger.debug(`[/v1/responses] input[${idx}]: type=message, role=${messageRole}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Responses API text item types
|
||||||
|
if (type === 'input_text') {
|
||||||
|
const text = this.asString(itemObj.text) ?? '';
|
||||||
|
if (text) {
|
||||||
|
messages.push({ role: 'user', content: text });
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'output_text') {
|
||||||
|
const text = this.asString(itemObj.text) ?? '';
|
||||||
|
if (text) {
|
||||||
|
messages.push({ role: 'assistant', content: text });
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reasoning items can be silently skipped — they are model-internal
|
||||||
|
if (type === 'reasoning') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1105,6 +1208,13 @@ export class ProxyController {
|
|||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fallback: if the item has role + content but an unrecognized type, treat as message
|
||||||
|
const fallbackRole = this.asString(itemObj.role);
|
||||||
|
if (fallbackRole && itemObj.content !== undefined) {
|
||||||
|
const content = this.normalizeResponsesMessageContent(itemObj.content);
|
||||||
|
messages.push({ role: fallbackRole, content });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (isString(body.input)) {
|
} else if (isString(body.input)) {
|
||||||
messages.push({
|
messages.push({
|
||||||
@@ -1125,17 +1235,64 @@ export class ProxyController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Forward reasoning effort to the extra field so convertOpenAIToClaude can relay it
|
||||||
|
const extra: Record<string, unknown> = {};
|
||||||
|
if (body.reasoning?.effort) {
|
||||||
|
extra.reasoning_effort = body.reasoning.effort;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize Responses API tool format to Chat Completions format.
|
||||||
|
// Responses API: { type: "function", name: "...", parameters: {...} }
|
||||||
|
// Chat Completions: { type: "function", function: { name: "...", parameters: {...} } }
|
||||||
|
const normalizedTools = this.normalizeResponsesTools(body.tools);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
model: body.model ?? 'gemini-3-flash',
|
model: body.model ?? 'gemini-3-flash',
|
||||||
messages,
|
messages,
|
||||||
tools: body.tools,
|
tools: normalizedTools,
|
||||||
max_tokens: body.max_output_tokens,
|
max_tokens: body.max_output_tokens,
|
||||||
temperature: body.temperature,
|
temperature: body.temperature,
|
||||||
top_p: body.top_p,
|
top_p: body.top_p,
|
||||||
stream: body.stream,
|
stream: body.stream,
|
||||||
|
...(Object.keys(extra).length > 0 ? { extra } : {}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert tools from OpenAI Responses API format to Chat Completions format.
|
||||||
|
* Responses API puts name/description/parameters at the top level of the tool object,
|
||||||
|
* while Chat Completions nests them under a `.function` property.
|
||||||
|
*/
|
||||||
|
private normalizeResponsesTools(tools?: OpenAIChatRequest['tools']): OpenAIChatRequest['tools'] {
|
||||||
|
if (!tools || tools.length === 0) {
|
||||||
|
return tools;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tools.map((tool) => {
|
||||||
|
// Already in Chat Completions format (has .function.name)
|
||||||
|
if (tool.function?.name) {
|
||||||
|
return tool;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Responses API format: name/description/parameters at top level
|
||||||
|
const topLevelName = this.asString((tool as Record<string, unknown>).name);
|
||||||
|
if (topLevelName) {
|
||||||
|
const raw = tool as Record<string, unknown>;
|
||||||
|
return {
|
||||||
|
type: 'function',
|
||||||
|
function: {
|
||||||
|
name: topLevelName,
|
||||||
|
description: this.asString(raw.description) ?? undefined,
|
||||||
|
parameters: (raw.parameters ?? raw.input_schema) as Record<string, unknown> | undefined,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unknown format — pass through as-is
|
||||||
|
return tool;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private normalizeResponsesMessageContent(content: unknown): string | OpenAIContentPart[] {
|
private normalizeResponsesMessageContent(content: unknown): string | OpenAIContentPart[] {
|
||||||
if (isString(content)) {
|
if (isString(content)) {
|
||||||
return content;
|
return content;
|
||||||
|
|||||||
@@ -55,7 +55,10 @@ export interface ProxyConfigOverride {
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProxyService {
|
export class ProxyService {
|
||||||
private readonly logger = new Logger(ProxyService.name);
|
private readonly logger = new Logger(ProxyService.name);
|
||||||
private readonly responseCache = new Map<string, { response: OpenAIChatResponse; expiry: number }>();
|
private readonly responseCache = new Map<
|
||||||
|
string,
|
||||||
|
{ response: OpenAIChatResponse; expiry: number }
|
||||||
|
>();
|
||||||
private readonly CACHE_TTL_MS = 60000; // 1 minute
|
private readonly CACHE_TTL_MS = 60000; // 1 minute
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@@ -79,7 +82,7 @@ export class ProxyService {
|
|||||||
}
|
}
|
||||||
const prompt = usage.promptTokenCount ?? 0;
|
const prompt = usage.promptTokenCount ?? 0;
|
||||||
const completion = usage.candidatesTokenCount ?? 0;
|
const completion = usage.candidatesTokenCount ?? 0;
|
||||||
const total = usage.totalTokenCount ?? (prompt + completion);
|
const total = usage.totalTokenCount ?? prompt + completion;
|
||||||
this.tokenUsageService.recordUsage({
|
this.tokenUsageService.recordUsage({
|
||||||
accountId,
|
accountId,
|
||||||
model,
|
model,
|
||||||
@@ -995,7 +998,10 @@ export class ProxyService {
|
|||||||
`Transformed Claude response snippet: ${JSON.stringify(claudeResponse).substring(0, 500)}`,
|
`Transformed Claude response snippet: ${JSON.stringify(claudeResponse).substring(0, 500)}`,
|
||||||
);
|
);
|
||||||
const openaiResponse = this.convertClaudeToOpenAIResponse(claudeResponse, request.model);
|
const openaiResponse = this.convertClaudeToOpenAIResponse(claudeResponse, request.model);
|
||||||
const transformedResponse = this.applyResponseFormatTransformation(openaiResponse, request);
|
const transformedResponse = this.applyResponseFormatTransformation(
|
||||||
|
openaiResponse,
|
||||||
|
request,
|
||||||
|
);
|
||||||
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
||||||
if (context) {
|
if (context) {
|
||||||
context.cacheStatus = 'MISS';
|
context.cacheStatus = 'MISS';
|
||||||
@@ -1058,8 +1064,14 @@ export class ProxyService {
|
|||||||
);
|
);
|
||||||
this.recordUsage(token.id, effectiveTargetModel, response.usageMetadata, 'openai');
|
this.recordUsage(token.id, effectiveTargetModel, response.usageMetadata, 'openai');
|
||||||
const claudeResponse = transformResponse(response);
|
const claudeResponse = transformResponse(response);
|
||||||
const openaiResponse = this.convertClaudeToOpenAIResponse(claudeResponse, request.model);
|
const openaiResponse = this.convertClaudeToOpenAIResponse(
|
||||||
const transformedResponse = this.applyResponseFormatTransformation(openaiResponse, request);
|
claudeResponse,
|
||||||
|
request.model,
|
||||||
|
);
|
||||||
|
const transformedResponse = this.applyResponseFormatTransformation(
|
||||||
|
openaiResponse,
|
||||||
|
request,
|
||||||
|
);
|
||||||
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
||||||
if (context) {
|
if (context) {
|
||||||
context.cacheStatus = 'MISS';
|
context.cacheStatus = 'MISS';
|
||||||
@@ -1636,7 +1648,8 @@ export class ProxyService {
|
|||||||
const anthropicMessages: ClaudeRequest['messages'] = [];
|
const anthropicMessages: ClaudeRequest['messages'] = [];
|
||||||
|
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
if (msg.role === 'system') {
|
// 'developer' is OpenAI's newer equivalent of 'system'
|
||||||
|
if (msg.role === 'system' || msg.role === 'developer') {
|
||||||
const systemText = this.extractOpenAITextContent(msg.content);
|
const systemText = this.extractOpenAITextContent(msg.content);
|
||||||
if (systemText) {
|
if (systemText) {
|
||||||
systemPromptParts.push(systemText);
|
systemPromptParts.push(systemText);
|
||||||
@@ -1679,12 +1692,31 @@ export class ProxyService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.response_format?.type === 'json_object' || request.response_format?.type === 'json_schema') {
|
if (
|
||||||
systemPromptParts.push('You must format your entire response as a single valid JSON object. Do not include any markdown formatting, explanations, or text outside the JSON structure.');
|
request.response_format?.type === 'json_object' ||
|
||||||
|
request.response_format?.type === 'json_schema'
|
||||||
|
) {
|
||||||
|
systemPromptParts.push(
|
||||||
|
'You must format your entire response as a single valid JSON object. Do not include any markdown formatting, explanations, or text outside the JSON structure.',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const systemPrompt = systemPromptParts.length > 0 ? systemPromptParts.join('\n') : undefined;
|
const systemPrompt = systemPromptParts.length > 0 ? systemPromptParts.join('\n') : undefined;
|
||||||
|
|
||||||
|
// Map OpenAI reasoning_effort to Claude thinking config
|
||||||
|
const reasoningEffort = String(request.extra?.reasoning_effort ?? '').toLowerCase();
|
||||||
|
let thinking: ClaudeRequest['thinking'] | undefined;
|
||||||
|
if (reasoningEffort === 'none') {
|
||||||
|
// Explicitly disable thinking when client requests no reasoning
|
||||||
|
thinking = { type: 'disabled' };
|
||||||
|
} else if (
|
||||||
|
reasoningEffort === 'low' ||
|
||||||
|
reasoningEffort === 'medium' ||
|
||||||
|
reasoningEffort === 'high'
|
||||||
|
) {
|
||||||
|
thinking = { type: 'adaptive', effort: reasoningEffort };
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
model: request.model,
|
model: request.model,
|
||||||
messages: anthropicMessages,
|
messages: anthropicMessages,
|
||||||
@@ -1694,6 +1726,7 @@ export class ProxyService {
|
|||||||
temperature: request.temperature,
|
temperature: request.temperature,
|
||||||
top_p: request.top_p,
|
top_p: request.top_p,
|
||||||
stream: request.stream,
|
stream: request.stream,
|
||||||
|
...(thinking ? { thinking } : {}),
|
||||||
metadata: {
|
metadata: {
|
||||||
...(request.extra ?? {}),
|
...(request.extra ?? {}),
|
||||||
source: 'openai',
|
source: 'openai',
|
||||||
@@ -1704,10 +1737,19 @@ export class ProxyService {
|
|||||||
private convertOpenAIPartsToAnthropicContent(
|
private convertOpenAIPartsToAnthropicContent(
|
||||||
content: OpenAIChatRequest['messages'][number]['content'],
|
content: OpenAIChatRequest['messages'][number]['content'],
|
||||||
): AnthropicContent[] {
|
): AnthropicContent[] {
|
||||||
|
if (content == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
if (isString(content)) {
|
if (isString(content)) {
|
||||||
return content.trim() ? [{ type: 'text', text: content }] : [];
|
return content.trim() ? [{ type: 'text', text: content }] : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Guard against non-array content (e.g. an object or number)
|
||||||
|
if (!Array.isArray(content)) {
|
||||||
|
return [{ type: 'text', text: String(content) }];
|
||||||
|
}
|
||||||
|
|
||||||
const blocks: AnthropicContent[] = [];
|
const blocks: AnthropicContent[] = [];
|
||||||
for (const part of content) {
|
for (const part of content) {
|
||||||
if (part.type === 'text' && part.text) {
|
if (part.type === 'text' && part.text) {
|
||||||
@@ -1738,10 +1780,18 @@ export class ProxyService {
|
|||||||
private extractOpenAITextContent(
|
private extractOpenAITextContent(
|
||||||
content: OpenAIChatRequest['messages'][number]['content'],
|
content: OpenAIChatRequest['messages'][number]['content'],
|
||||||
): string {
|
): string {
|
||||||
|
if (content == null) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
if (isString(content)) {
|
if (isString(content)) {
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(content)) {
|
||||||
|
return String(content);
|
||||||
|
}
|
||||||
|
|
||||||
return content
|
return content
|
||||||
.filter((part) => part.type === 'text')
|
.filter((part) => part.type === 'text')
|
||||||
.map((part) => part.text || '')
|
.map((part) => part.text || '')
|
||||||
|
|||||||
Reference in new issue
Block a user