Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a631887cd1 | ||
|
|
4ce8d266ab | ||
|
|
68474b56dc | ||
|
|
48faa0d4b1 | ||
|
|
9bf7c46ff6 | ||
|
|
195af14707 | ||
|
|
a9a492274c | ||
|
|
3cf82a7999 |
No files matched your search
@@ -1,11 +1,9 @@
|
||||
name: Publish Release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'v*'
|
||||
release:
|
||||
types: [published]
|
||||
workflow_run:
|
||||
workflows: ["Release"]
|
||||
types: [completed]
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
dry_run:
|
||||
@@ -20,7 +18,46 @@ on:
|
||||
default: false
|
||||
|
||||
jobs:
|
||||
check-release:
|
||||
name: Check for new release
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
|
||||
permissions:
|
||||
contents: read
|
||||
outputs:
|
||||
tag: ${{ steps.get_tag.outputs.tag }}
|
||||
has_release: ${{ steps.get_tag.outputs.has_release }}
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Get latest release tag
|
||||
id: get_tag
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
||||
version="$(node -e "process.stdout.write(require('./package.json').version)")"
|
||||
tag="v$version"
|
||||
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
||||
echo "has_release=true" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
tag=$(gh release list --limit 1 --json tagName --jq '.[0].tagName // empty')
|
||||
if [ -n "$tag" ]; then
|
||||
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
||||
echo "has_release=true" >> "$GITHUB_OUTPUT"
|
||||
echo "Found release: $tag"
|
||||
else
|
||||
echo "has_release=false" >> "$GITHUB_OUTPUT"
|
||||
echo "No release found, skipping build."
|
||||
fi
|
||||
fi
|
||||
|
||||
build:
|
||||
needs: check-release
|
||||
if: needs.check-release.outputs.has_release == 'true'
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
@@ -136,16 +173,12 @@ jobs:
|
||||
echo "Dry run enabled: build artifacts generated and uploaded; release publish step will be skipped."
|
||||
|
||||
publish:
|
||||
name: Publish Release (Manual Approval)
|
||||
needs: build
|
||||
name: Publish Release
|
||||
needs: [check-release, build]
|
||||
runs-on: ubuntu-latest
|
||||
if: needs.build.result == 'success' && (github.event_name != 'workflow_dispatch' || github.event.inputs.dry_run != 'true')
|
||||
permissions:
|
||||
contents: write
|
||||
# Configure the `release` environment with required reviewers in GitHub settings
|
||||
# to enforce a manual approval gate before assets are uploaded.
|
||||
environment:
|
||||
name: release
|
||||
|
||||
steps:
|
||||
- name: Checkout git repo
|
||||
@@ -158,54 +191,17 @@ jobs:
|
||||
merge-multiple: true
|
||||
path: release-assets
|
||||
|
||||
- name: Install pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 10
|
||||
|
||||
- name: Install Node and NPM
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 20
|
||||
cache: 'pnpm'
|
||||
|
||||
- name: Resolve release tag
|
||||
id: release_tag
|
||||
env:
|
||||
RELEASE_EVENT_TAG: ${{ github.event_name == 'release' && github.event.release.tag_name || '' }}
|
||||
run: |
|
||||
if [ -n "$RELEASE_EVENT_TAG" ]; then
|
||||
tag="$RELEASE_EVENT_TAG"
|
||||
else
|
||||
version="$(node -e "const v=require('./package.json').version; if (!v) { throw new Error('package.json version is empty'); } process.stdout.write(v);")"
|
||||
tag="${{ needs.check-release.outputs.tag }}"
|
||||
if [ -z "$tag" ]; then
|
||||
version="$(node -e "process.stdout.write(require('./package.json').version)")"
|
||||
tag="v$version"
|
||||
fi
|
||||
|
||||
if [ -z "$tag" ]; then
|
||||
echo "::error::Resolved release tag is empty"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
||||
echo "Using release tag: $tag"
|
||||
|
||||
- name: Ensure release is safe to update
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
RELEASE_TAG: ${{ steps.release_tag.outputs.tag }}
|
||||
ALLOW_UPDATE_PUBLISHED_RELEASE: ${{ github.event_name == 'release' && 'true' || (github.event_name == 'workflow_dispatch' && github.event.inputs.allow_update_published_release) || 'false' }}
|
||||
run: |
|
||||
if gh release view "$RELEASE_TAG" >/dev/null 2>&1; then
|
||||
is_draft="$(gh release view "$RELEASE_TAG" --json isDraft --jq '.isDraft')"
|
||||
if [ "$is_draft" != "true" ] && [ "$ALLOW_UPDATE_PUBLISHED_RELEASE" != "true" ]; then
|
||||
echo "::error::Release $RELEASE_TAG is already published. Refusing to upload assets."
|
||||
echo "::error::Re-run with workflow_dispatch input allow_update_published_release=true if you explicitly want to update it."
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
gh release create "$RELEASE_TAG" --title "$RELEASE_TAG" --draft --notes "Automated release draft."
|
||||
fi
|
||||
|
||||
- name: Upload assets to release
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@
|
||||
"npmPublish": false
|
||||
}],
|
||||
["@semantic-release/git", {
|
||||
"assets": ["package.json", "package-lock.json", "CHANGELOG.md"],
|
||||
"assets": ["package.json", "pnpm-lock.yaml", "CHANGELOG.md"],
|
||||
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
||||
}],
|
||||
["@semantic-release/github", {
|
||||
|
||||
+13
-1
@@ -1,3 +1,15 @@
|
||||
## [0.14.0](https://github.com/evandrodevbr/GeminiNexus/compare/v0.13.1...v0.14.0) (2026-04-29)
|
||||
|
||||
### 🚀 Features
|
||||
|
||||
* **proxy:** add OpenAI Responses API compatibility for Open CoDesign ([68474b5](https://github.com/evandrodevbr/GeminiNexus/commit/68474b56dca9c9bcd7b06eaae04c521f475e8e2c))
|
||||
|
||||
## [0.13.1](https://github.com/evandrodevbr/GeminiNexus/compare/v0.13.0...v0.13.1) (2026-04-29)
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
* lazy-initialize api-logger to prevent EACCES in CI environments ([9bf7c46](https://github.com/evandrodevbr/GeminiNexus/commit/9bf7c46ff6320f49cba80923c9e11eac35a7d4f0))
|
||||
|
||||
<a name="readme-top"></a>
|
||||
|
||||
# Changelog
|
||||
@@ -308,4 +320,4 @@
|
||||
- Secure credential storage (AES-256-GCM)
|
||||
- IDE synchronization
|
||||
- Dark mode support
|
||||
- System tray integration
|
||||
- System tray integration
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "gemini-nexus",
|
||||
"productName": "Gemini Nexus",
|
||||
"version": "0.13.0",
|
||||
"version": "0.14.0",
|
||||
"description": "Gemini Nexus - Electron App",
|
||||
"main": ".vite/build/main.js",
|
||||
"private": true,
|
||||
|
||||
@@ -162,6 +162,11 @@ function cleanJsonSchemaRecursive(value: any) {
|
||||
// 4. Physically remove "hard" blacklist items that interfere with generation
|
||||
const hardRemoveFields = [
|
||||
'$schema',
|
||||
'$id',
|
||||
'$comment',
|
||||
'$anchor',
|
||||
'$dynamicRef',
|
||||
'$dynamicAnchor',
|
||||
'additionalProperties',
|
||||
'enumCaseInsensitive',
|
||||
'enumNormalizeWhitespace',
|
||||
@@ -170,7 +175,15 @@ function cleanJsonSchemaRecursive(value: any) {
|
||||
'const',
|
||||
'examples',
|
||||
// Advanced logic fields common in MCP tools but unsupported by Gemini
|
||||
'patternProperties',
|
||||
'propertyNames',
|
||||
'additionalItems',
|
||||
'prefixItems',
|
||||
'contains',
|
||||
'minContains',
|
||||
'maxContains',
|
||||
'unevaluatedProperties',
|
||||
'unevaluatedItems',
|
||||
'anyOf',
|
||||
'oneOf',
|
||||
'allOf',
|
||||
|
||||
@@ -8,70 +8,97 @@ import { sanitizeObject } from '../../utils/sensitiveDataMasking';
|
||||
const LOG_RETENTION = '30d';
|
||||
const LOG_MAX_SIZE = '50m';
|
||||
|
||||
const agentDir = getAgentDir();
|
||||
let _logger: winston.Logger | null = null;
|
||||
|
||||
if (!fs.existsSync(agentDir)) {
|
||||
try {
|
||||
fs.mkdirSync(agentDir, { recursive: true });
|
||||
} catch (e) {
|
||||
console.error('Failed to create agent directory for API logs', e);
|
||||
/**
|
||||
* Lazily initializes the API logger on first access.
|
||||
* Avoids creating directories and file transports at module load time,
|
||||
* preventing EACCES errors in CI/test environments.
|
||||
*/
|
||||
function getLogger(): winston.Logger {
|
||||
if (_logger) {
|
||||
return _logger;
|
||||
}
|
||||
|
||||
const agentDir = getAgentDir();
|
||||
|
||||
if (!fs.existsSync(agentDir)) {
|
||||
try {
|
||||
fs.mkdirSync(agentDir, { recursive: true });
|
||||
} catch (e) {
|
||||
console.error('Failed to create agent directory for API logs', e);
|
||||
}
|
||||
}
|
||||
|
||||
const fileFormat = winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
||||
const logEntry = {
|
||||
timestamp,
|
||||
level,
|
||||
message,
|
||||
...meta,
|
||||
};
|
||||
return JSON.stringify(logEntry);
|
||||
}),
|
||||
);
|
||||
|
||||
const consoleFormat = winston.format.combine(
|
||||
winston.format.colorize({ all: true }),
|
||||
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
||||
const logEntry = {
|
||||
timestamp,
|
||||
level,
|
||||
message,
|
||||
...meta,
|
||||
};
|
||||
return JSON.stringify(logEntry);
|
||||
}),
|
||||
);
|
||||
|
||||
const rotateTransport = new DailyRotateFile({
|
||||
filename: path.join(agentDir, 'api-%DATE%.log'),
|
||||
datePattern: 'YYYY-MM-DD-HH',
|
||||
maxSize: LOG_MAX_SIZE,
|
||||
maxFiles: LOG_RETENTION,
|
||||
zippedArchive: false,
|
||||
auditFile: path.join(agentDir, '.api-log-audit.json'),
|
||||
level: 'debug',
|
||||
format: fileFormat,
|
||||
});
|
||||
|
||||
rotateTransport.on('error', (error) => {
|
||||
console.error('DailyRotateFile API transport error', error);
|
||||
});
|
||||
|
||||
const consoleTransport = new winston.transports.Console({
|
||||
level: 'debug',
|
||||
format: consoleFormat,
|
||||
});
|
||||
|
||||
consoleTransport.on('error', (error) => {
|
||||
console.error('Console API transport error', error);
|
||||
});
|
||||
|
||||
_logger = winston.createLogger({
|
||||
level: 'debug',
|
||||
transports: [consoleTransport, rotateTransport],
|
||||
exitOnError: false,
|
||||
});
|
||||
|
||||
return _logger;
|
||||
}
|
||||
|
||||
const fileFormat = winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
||||
const logEntry = {
|
||||
timestamp,
|
||||
level,
|
||||
message,
|
||||
...meta,
|
||||
};
|
||||
return JSON.stringify(logEntry);
|
||||
}),
|
||||
);
|
||||
|
||||
const consoleFormat = winston.format.combine(
|
||||
winston.format.colorize({ all: true }),
|
||||
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
||||
const logEntry = {
|
||||
timestamp,
|
||||
level,
|
||||
message,
|
||||
...meta,
|
||||
};
|
||||
return JSON.stringify(logEntry);
|
||||
}),
|
||||
);
|
||||
|
||||
const rotateTransport = new DailyRotateFile({
|
||||
filename: path.join(agentDir, 'api-%DATE%.log'),
|
||||
datePattern: 'YYYY-MM-DD-HH',
|
||||
maxSize: LOG_MAX_SIZE,
|
||||
maxFiles: LOG_RETENTION,
|
||||
zippedArchive: false,
|
||||
auditFile: path.join(agentDir, '.api-log-audit.json'),
|
||||
level: 'debug',
|
||||
format: fileFormat,
|
||||
});
|
||||
|
||||
rotateTransport.on('error', (error) => {
|
||||
console.error('DailyRotateFile API transport error', error);
|
||||
});
|
||||
|
||||
const consoleTransport = new winston.transports.Console({
|
||||
level: 'debug',
|
||||
format: consoleFormat,
|
||||
});
|
||||
|
||||
consoleTransport.on('error', (error) => {
|
||||
console.error('Console API transport error', error);
|
||||
});
|
||||
|
||||
export const apiLogger = winston.createLogger({
|
||||
level: 'debug',
|
||||
transports: [consoleTransport, rotateTransport],
|
||||
exitOnError: false,
|
||||
/** Lazily-initialized API logger instance. */
|
||||
export const apiLogger = new Proxy({} as winston.Logger, {
|
||||
get(_target, prop: string) {
|
||||
const logger = getLogger();
|
||||
const value = logger[prop as keyof winston.Logger];
|
||||
if (typeof value === 'function') {
|
||||
return value.bind(logger);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,7 +46,9 @@ export class ProxyController {
|
||||
|
||||
constructor(
|
||||
@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(ProxyMetricsService) private readonly proxyMetrics?: ProxyMetricsService,
|
||||
) {}
|
||||
@@ -113,7 +115,9 @@ export class ProxyController {
|
||||
const metrics = this.proxyMetrics?.getMetrics();
|
||||
const circuitBreakerStatus = this.tokenManager?.getCircuitBreakerStatus() ?? {};
|
||||
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({
|
||||
status: 'healthy',
|
||||
@@ -162,7 +166,8 @@ export class ProxyController {
|
||||
}
|
||||
const startTime = Date.now();
|
||||
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: '' });
|
||||
res.status(HttpStatus.OK).send({
|
||||
object: 'list',
|
||||
@@ -226,7 +231,8 @@ export class ProxyController {
|
||||
const capabilitiesMap = this.buildCapabilitiesMap();
|
||||
|
||||
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 {
|
||||
id,
|
||||
object: 'model_capability',
|
||||
@@ -247,7 +253,9 @@ export class ProxyController {
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to list model capabilities';
|
||||
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({
|
||||
timestamp: 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 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 [modelId, modelInfo] of Object.entries(tokenData.quota?.models ?? {})) {
|
||||
@@ -278,7 +304,13 @@ export class ProxyController {
|
||||
|
||||
if (Object.keys(map).length === 0) {
|
||||
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 proxyContext: ProxyRequestContext = {};
|
||||
try {
|
||||
const result = await this.proxyService.handleChatCompletions(
|
||||
request,
|
||||
proxyContext,
|
||||
);
|
||||
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||
if (this.isObservableLike(result)) {
|
||||
responses.push({
|
||||
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 {
|
||||
responses.push({
|
||||
@@ -462,7 +494,14 @@ export class ProxyController {
|
||||
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||
isStream = !!(body.stream && this.isObservableLike(result));
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -508,6 +547,7 @@ export class ProxyController {
|
||||
temperature?: number;
|
||||
top_p?: number;
|
||||
stream?: boolean;
|
||||
reasoning?: { effort?: string };
|
||||
},
|
||||
@Req() req: FastifyRequest,
|
||||
@Res() res: FastifyReply,
|
||||
@@ -527,11 +567,24 @@ export class ProxyController {
|
||||
const proxyContext: ProxyRequestContext = {};
|
||||
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 {
|
||||
const result = await this.proxyService.handleChatCompletions(request, proxyContext);
|
||||
isStream = !!(body.stream && this.isObservableLike(result));
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -840,14 +893,18 @@ export class ProxyController {
|
||||
const proxyContext: ProxyRequestContext = {};
|
||||
let isStream = false;
|
||||
try {
|
||||
const result = await this.proxyService.handleChatCompletions(
|
||||
body,
|
||||
proxyContext,
|
||||
);
|
||||
const result = await this.proxyService.handleChatCompletions(body, proxyContext);
|
||||
|
||||
isStream = !!(body.stream && this.isObservableLike(result));
|
||||
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;
|
||||
} else {
|
||||
const duration = Date.now() - startTime;
|
||||
@@ -918,7 +975,14 @@ export class ProxyController {
|
||||
isStream = !!(body.stream && this.isObservableLike(result));
|
||||
|
||||
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;
|
||||
} else {
|
||||
const duration = Date.now() - startTime;
|
||||
@@ -1017,6 +1081,7 @@ export class ProxyController {
|
||||
temperature?: number;
|
||||
top_p?: number;
|
||||
stream?: boolean;
|
||||
reasoning?: { effort?: string };
|
||||
}): OpenAIChatRequest {
|
||||
const messages: OpenAIChatRequest['messages'] = [];
|
||||
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);
|
||||
if (!itemObj) {
|
||||
this.logger.debug(`[/v1/responses] input[${idx}]: skipped (not an object)`);
|
||||
continue;
|
||||
}
|
||||
|
||||
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 (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;
|
||||
}
|
||||
|
||||
if (type === 'message') {
|
||||
const role = this.asString(itemObj.role) ?? 'user';
|
||||
const messageRole = role ?? 'user';
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1105,6 +1208,13 @@ export class ProxyController {
|
||||
});
|
||||
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)) {
|
||||
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 {
|
||||
model: body.model ?? 'gemini-3-flash',
|
||||
messages,
|
||||
tools: body.tools,
|
||||
tools: normalizedTools,
|
||||
max_tokens: body.max_output_tokens,
|
||||
temperature: body.temperature,
|
||||
top_p: body.top_p,
|
||||
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[] {
|
||||
if (isString(content)) {
|
||||
return content;
|
||||
|
||||
@@ -55,7 +55,10 @@ export interface ProxyConfigOverride {
|
||||
@Injectable()
|
||||
export class ProxyService {
|
||||
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
|
||||
|
||||
constructor(
|
||||
@@ -79,7 +82,7 @@ export class ProxyService {
|
||||
}
|
||||
const prompt = usage.promptTokenCount ?? 0;
|
||||
const completion = usage.candidatesTokenCount ?? 0;
|
||||
const total = usage.totalTokenCount ?? (prompt + completion);
|
||||
const total = usage.totalTokenCount ?? prompt + completion;
|
||||
this.tokenUsageService.recordUsage({
|
||||
accountId,
|
||||
model,
|
||||
@@ -995,7 +998,10 @@ export class ProxyService {
|
||||
`Transformed Claude response snippet: ${JSON.stringify(claudeResponse).substring(0, 500)}`,
|
||||
);
|
||||
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);
|
||||
if (context) {
|
||||
context.cacheStatus = 'MISS';
|
||||
@@ -1058,8 +1064,14 @@ export class ProxyService {
|
||||
);
|
||||
this.recordUsage(token.id, effectiveTargetModel, response.usageMetadata, 'openai');
|
||||
const claudeResponse = transformResponse(response);
|
||||
const openaiResponse = this.convertClaudeToOpenAIResponse(claudeResponse, request.model);
|
||||
const transformedResponse = this.applyResponseFormatTransformation(openaiResponse, request);
|
||||
const openaiResponse = this.convertClaudeToOpenAIResponse(
|
||||
claudeResponse,
|
||||
request.model,
|
||||
);
|
||||
const transformedResponse = this.applyResponseFormatTransformation(
|
||||
openaiResponse,
|
||||
request,
|
||||
);
|
||||
this.setCachedResponse(this.buildCacheKey(request), transformedResponse);
|
||||
if (context) {
|
||||
context.cacheStatus = 'MISS';
|
||||
@@ -1636,7 +1648,8 @@ export class ProxyService {
|
||||
const anthropicMessages: ClaudeRequest['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);
|
||||
if (systemText) {
|
||||
systemPromptParts.push(systemText);
|
||||
@@ -1679,12 +1692,31 @@ export class ProxyService {
|
||||
});
|
||||
}
|
||||
|
||||
if (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.');
|
||||
if (
|
||||
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;
|
||||
|
||||
// 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 {
|
||||
model: request.model,
|
||||
messages: anthropicMessages,
|
||||
@@ -1694,6 +1726,7 @@ export class ProxyService {
|
||||
temperature: request.temperature,
|
||||
top_p: request.top_p,
|
||||
stream: request.stream,
|
||||
...(thinking ? { thinking } : {}),
|
||||
metadata: {
|
||||
...(request.extra ?? {}),
|
||||
source: 'openai',
|
||||
@@ -1704,10 +1737,19 @@ export class ProxyService {
|
||||
private convertOpenAIPartsToAnthropicContent(
|
||||
content: OpenAIChatRequest['messages'][number]['content'],
|
||||
): AnthropicContent[] {
|
||||
if (content == null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (isString(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[] = [];
|
||||
for (const part of content) {
|
||||
if (part.type === 'text' && part.text) {
|
||||
@@ -1738,10 +1780,18 @@ export class ProxyService {
|
||||
private extractOpenAITextContent(
|
||||
content: OpenAIChatRequest['messages'][number]['content'],
|
||||
): string {
|
||||
if (content == null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (isString(content)) {
|
||||
return content;
|
||||
}
|
||||
|
||||
if (!Array.isArray(content)) {
|
||||
return String(content);
|
||||
}
|
||||
|
||||
return content
|
||||
.filter((part) => part.type === 'text')
|
||||
.map((part) => part.text || '')
|
||||
|
||||
Reference in new issue
Block a user