File size: 825 Bytes
9705b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const { AnthropicClient } = require('../../../../app');
const { getUserKey, checkUserKeyExpiry } = require('../../../services/UserService');

const initializeClient = async ({ req, res }) => {
  const { ANTHROPIC_API_KEY } = process.env;
  const { key: expiresAt } = req.body;

  const isUserProvided = ANTHROPIC_API_KEY === 'user_provided';

  let key = null;
  if (expiresAt && isUserProvided) {
    checkUserKeyExpiry(
      expiresAt,
      'Your ANTHROPIC_API_KEY has expired. Please provide your API key again.',
    );
    key = await getUserKey({ userId: req.user.id, name: 'anthropic' });
  }
  let anthropicApiKey = isUserProvided ? key : ANTHROPIC_API_KEY;
  const client = new AnthropicClient(anthropicApiKey, { req, res });
  return {
    client,
    anthropicApiKey,
  };
};

module.exports = initializeClient;