@layer-ai/core 0.7.2 โ 0.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/routes/v2/tests/test-byok-completion.d.ts +10 -0
- package/dist/routes/v2/tests/test-byok-completion.d.ts.map +1 -0
- package/dist/routes/v2/tests/test-byok-completion.js +82 -0
- package/dist/routes/v2/tests/test-byok.d.ts +8 -0
- package/dist/routes/v2/tests/test-byok.d.ts.map +1 -0
- package/dist/routes/v2/tests/test-byok.js +73 -0
- package/package.json +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test BYOK completion flow
|
|
3
|
+
*
|
|
4
|
+
* This test verifies that when making a completion request:
|
|
5
|
+
* 1. The system checks for user's BYOK keys
|
|
6
|
+
* 2. Uses BYOK keys when available
|
|
7
|
+
* 3. Falls back to platform keys when BYOK not configured
|
|
8
|
+
*/
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=test-byok-completion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-byok-completion.d.ts","sourceRoot":"","sources":["../../../../src/routes/v2/tests/test-byok-completion.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test BYOK completion flow
|
|
3
|
+
*
|
|
4
|
+
* This test verifies that when making a completion request:
|
|
5
|
+
* 1. The system checks for user's BYOK keys
|
|
6
|
+
* 2. Uses BYOK keys when available
|
|
7
|
+
* 3. Falls back to platform keys when BYOK not configured
|
|
8
|
+
*/
|
|
9
|
+
import { OpenAIAdapter } from '../../../services/providers/openai-adapter.js';
|
|
10
|
+
import { AnthropicAdapter } from '../../../services/providers/anthropic-adapter.js';
|
|
11
|
+
import { GoogleAdapter } from '../../../services/providers/google-adapter.js';
|
|
12
|
+
// Test user ID from the database
|
|
13
|
+
const TEST_USER_ID = 'ebd64998-465d-4211-ad67-87b4e01ad0da';
|
|
14
|
+
const SIMPLE_REQUEST = {
|
|
15
|
+
gate: 'byok-test',
|
|
16
|
+
model: 'gpt-4o-mini',
|
|
17
|
+
type: 'chat',
|
|
18
|
+
data: {
|
|
19
|
+
messages: [
|
|
20
|
+
{
|
|
21
|
+
role: 'user',
|
|
22
|
+
content: 'Say "BYOK test successful" and nothing else.',
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
maxTokens: 20,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
async function testBYOKCompletion() {
|
|
29
|
+
console.log('\n๐งช Testing BYOK Completion Flow...\n');
|
|
30
|
+
try {
|
|
31
|
+
// Test 1: OpenAI with BYOK (should use user's key)
|
|
32
|
+
console.log('Test 1: OpenAI completion with BYOK key');
|
|
33
|
+
console.log('----------------------------------------');
|
|
34
|
+
const openaiAdapter = new OpenAIAdapter();
|
|
35
|
+
const openaiRequest = { ...SIMPLE_REQUEST, model: 'gpt-4o-mini' };
|
|
36
|
+
console.log(`Making request to OpenAI with userId: ${TEST_USER_ID}`);
|
|
37
|
+
const openaiResult = await openaiAdapter.call(openaiRequest, TEST_USER_ID);
|
|
38
|
+
console.log('โ OpenAI request successful');
|
|
39
|
+
console.log(`Response: ${openaiResult.content}`);
|
|
40
|
+
console.log('Note: This request used your BYOK OpenAI key\n');
|
|
41
|
+
// Test 2: Anthropic with BYOK (should use user's key)
|
|
42
|
+
console.log('Test 2: Anthropic completion with BYOK key');
|
|
43
|
+
console.log('------------------------------------------');
|
|
44
|
+
const anthropicAdapter = new AnthropicAdapter();
|
|
45
|
+
const anthropicRequest = { ...SIMPLE_REQUEST, model: 'claude-3-haiku-20240307' };
|
|
46
|
+
console.log(`Making request to Anthropic with userId: ${TEST_USER_ID}`);
|
|
47
|
+
const anthropicResult = await anthropicAdapter.call(anthropicRequest, TEST_USER_ID);
|
|
48
|
+
console.log('โ Anthropic request successful');
|
|
49
|
+
console.log(`Response: ${anthropicResult.content}`);
|
|
50
|
+
console.log('Note: This request used your BYOK Anthropic key\n');
|
|
51
|
+
// Test 3: Google without BYOK (should use platform key)
|
|
52
|
+
console.log('Test 3: Google completion without BYOK key');
|
|
53
|
+
console.log('------------------------------------------');
|
|
54
|
+
const googleAdapter = new GoogleAdapter();
|
|
55
|
+
const googleRequest = { ...SIMPLE_REQUEST, model: 'gemini-2.0-flash' };
|
|
56
|
+
console.log(`Making request to Google with userId: ${TEST_USER_ID}`);
|
|
57
|
+
const googleResult = await googleAdapter.call(googleRequest, TEST_USER_ID);
|
|
58
|
+
console.log('โ Google request successful');
|
|
59
|
+
console.log(`Response: ${googleResult.content}`);
|
|
60
|
+
console.log('Note: This request used the platform Google key (no BYOK configured)\n');
|
|
61
|
+
console.log('โ
All BYOK completion tests passed!\n');
|
|
62
|
+
console.log('Summary:');
|
|
63
|
+
console.log('- OpenAI: Used BYOK key โ');
|
|
64
|
+
console.log('- Anthropic: Used BYOK key โ');
|
|
65
|
+
console.log('- Google: Used platform key (fallback) โ');
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error('\nโ BYOK completion test failed:', error);
|
|
69
|
+
if (error instanceof Error) {
|
|
70
|
+
console.error('Error message:', error.message);
|
|
71
|
+
console.error('Stack:', error.stack);
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Run the test
|
|
77
|
+
testBYOKCompletion()
|
|
78
|
+
.then(() => process.exit(0))
|
|
79
|
+
.catch((error) => {
|
|
80
|
+
console.error('Test failed:', error);
|
|
81
|
+
process.exit(1);
|
|
82
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-byok.d.ts","sourceRoot":"","sources":["../../../../src/routes/v2/tests/test-byok.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test BYOK (Bring Your Own Keys) functionality
|
|
3
|
+
*
|
|
4
|
+
* This test verifies that when a user has configured their own provider keys,
|
|
5
|
+
* those keys are used instead of the platform keys.
|
|
6
|
+
*/
|
|
7
|
+
import { db } from '../../../lib/db/postgres.js';
|
|
8
|
+
import { decrypt } from '../../../lib/encryption.js';
|
|
9
|
+
async function testBYOK() {
|
|
10
|
+
console.log('\n๐ Testing BYOK Key Resolution...\n');
|
|
11
|
+
// Test user ID from the database query
|
|
12
|
+
const userId = 'ebd64998-465d-4211-ad67-87b4e01ad0da';
|
|
13
|
+
try {
|
|
14
|
+
// Test 1: Retrieve OpenAI key
|
|
15
|
+
console.log('Test 1: Retrieving OpenAI BYOK key...');
|
|
16
|
+
const openaiKey = await db.getProviderKey(userId, 'openai');
|
|
17
|
+
if (openaiKey) {
|
|
18
|
+
console.log('โ OpenAI key found in database');
|
|
19
|
+
console.log(` Provider: ${openaiKey.provider}`);
|
|
20
|
+
console.log(` Key Prefix: ${openaiKey.keyPrefix}`);
|
|
21
|
+
console.log(` Created: ${openaiKey.createdAt}`);
|
|
22
|
+
// Decrypt and verify the key
|
|
23
|
+
const encryptionKey = process.env.ENCRYPTION_KEY;
|
|
24
|
+
if (!encryptionKey) {
|
|
25
|
+
throw new Error('ENCRYPTION_KEY not found in environment');
|
|
26
|
+
}
|
|
27
|
+
const decryptedKey = decrypt(openaiKey.encryptedKey, encryptionKey);
|
|
28
|
+
console.log(` Decrypted key starts with: ${decryptedKey.substring(0, 10)}...`);
|
|
29
|
+
console.log(` Decrypted key length: ${decryptedKey.length} characters`);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
console.log('โ No OpenAI key found');
|
|
33
|
+
}
|
|
34
|
+
console.log('\nTest 2: Retrieving Anthropic BYOK key...');
|
|
35
|
+
const anthropicKey = await db.getProviderKey(userId, 'anthropic');
|
|
36
|
+
if (anthropicKey) {
|
|
37
|
+
console.log('โ Anthropic key found in database');
|
|
38
|
+
console.log(` Provider: ${anthropicKey.provider}`);
|
|
39
|
+
console.log(` Key Prefix: ${anthropicKey.keyPrefix}`);
|
|
40
|
+
console.log(` Created: ${anthropicKey.createdAt}`);
|
|
41
|
+
const encryptionKey = process.env.ENCRYPTION_KEY;
|
|
42
|
+
if (!encryptionKey) {
|
|
43
|
+
throw new Error('ENCRYPTION_KEY not found in environment');
|
|
44
|
+
}
|
|
45
|
+
const decryptedKey = decrypt(anthropicKey.encryptedKey, encryptionKey);
|
|
46
|
+
console.log(` Decrypted key starts with: ${decryptedKey.substring(0, 14)}...`);
|
|
47
|
+
console.log(` Decrypted key length: ${decryptedKey.length} characters`);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
console.log('โ No Anthropic key found');
|
|
51
|
+
}
|
|
52
|
+
console.log('\nTest 3: Retrieving Google BYOK key (should not exist)...');
|
|
53
|
+
const googleKey = await db.getProviderKey(userId, 'google');
|
|
54
|
+
if (googleKey) {
|
|
55
|
+
console.log('โ Google key found in database');
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
console.log('โ No Google key found (as expected)');
|
|
59
|
+
}
|
|
60
|
+
console.log('\nโ
BYOK test completed successfully!\n');
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
console.error('\nโ BYOK test failed:', error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Run the test
|
|
68
|
+
testBYOK()
|
|
69
|
+
.then(() => process.exit(0))
|
|
70
|
+
.catch((error) => {
|
|
71
|
+
console.error('Test failed:', error);
|
|
72
|
+
process.exit(1);
|
|
73
|
+
});
|