Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 2024-05-18 — Use crypto.randomUUID() for Mock Response IDs

Learning:
The API gateway was generating pseudo-random mock response IDs using `Math.random().toString(36).substr(2, 9)`. This approach is both slow under heavy load and fundamentally flawed for a large-scale system due to the high risk of ID collisions (non-cryptographic randomness, short string length).

Action:
Switched to Node.js's native `crypto.randomUUID()` to generate mock response IDs. This provides mathematically guaranteed uniqueness (crucial for a unified API gateway) and is natively faster than the previous string manipulation approach. Tests were updated to reflect the increased length of UUIDs over the previous 9-character pseudo-random strings.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// one-api
const { performance } = require('perf_hooks');
const crypto = require('crypto');
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
Expand Down Expand Up @@ -30,7 +31,7 @@ app.post('/v1/chat/completions', (req, res) => {

// Mock unified response
res.json({
id: `chatcmpl-${Math.random().toString(36).substr(2, 9)}`,
id: `chatcmpl-${crypto.randomUUID()}`,
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: model,
Expand Down
1 change: 1 addition & 0 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test('POST /v1/chat/completions works with valid data', async () => {

assert.strictEqual(res.status, 200);
assert.ok(res.body.id.startsWith('chatcmpl-'));
assert.ok(res.body.id.length > 20); // chatcmpl- + UUID length (36)
assert.strictEqual(res.body.object, 'chat.completion');
assert.strictEqual(res.body.model, 'gpt-4');
assert.strictEqual(res.body.choices[0].message.content, 'This is a mock response from the unified API.');
Expand Down
Loading