Repository navigation
Expand file tree
/
Copy pathchatWithClaude.js
More file actions
136 lines (122 loc) · 5.3 KB
/
Copy pathchatWithClaude.js
File metadata and controls
136 lines (122 loc) · 5.3 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import Anthropic from '@anthropic-ai/sdk';
import dotenv from 'dotenv';
import { fileURLToPath } from 'node:url';
import {
ProviderControlError,
ProviderRequestControls,
readProviderNumber
} from '../../library/javascript/commonHelpers/providerRequestControls.js';
dotenv.config({ path: fileURLToPath(new URL('.env', import.meta.url)), quiet: true });
const SYSTEM_INSTRUCTIONS = 'You are a helpful assistant. Analyze or respond based on the provided meeting transcript.';
export const claudeConfig = Object.freeze({
model: process.env.CLAUDE_MODEL || 'claude-sonnet-5',
timeoutMs: readProviderNumber('CLAUDE_TIMEOUT_MS', 20_000, { integer: true, minimum: 1 }),
maxRetries: readProviderNumber('CLAUDE_MAX_RETRIES', 2, { integer: true }),
maxOutputTokens: readProviderNumber('CLAUDE_MAX_OUTPUT_TOKENS', 512, { integer: true, minimum: 1 }),
maxInputCharacters: readProviderNumber('CLAUDE_MAX_INPUT_CHARACTERS', 40_000, { integer: true, minimum: 1 }),
maxHistoryMessages: readProviderNumber('CLAUDE_MAX_HISTORY_MESSAGES', 20, { integer: true, minimum: 2 }),
maxHistoryCharacters: readProviderNumber('CLAUDE_MAX_HISTORY_CHARACTERS', 40_000, { integer: true, minimum: 1 }),
maxRequestsPerMinute: readProviderNumber('CLAUDE_MAX_REQUESTS_PER_MINUTE', 30, { integer: true }),
maxRequestsPerStream: readProviderNumber('CLAUDE_MAX_REQUESTS_PER_STREAM', 300, { integer: true }),
maxSpendUsdPerStream: readProviderNumber('CLAUDE_MAX_SPEND_USD_PER_STREAM', 1),
inputCostPerMillionTokens: readProviderNumber('CLAUDE_INPUT_COST_PER_MILLION_TOKENS', 2),
outputCostPerMillionTokens: readProviderNumber('CLAUDE_OUTPUT_COST_PER_MILLION_TOKENS', 10)
});
export function createClaudeService({ client, config = claudeConfig } = {}) {
const sessions = new Map();
const controls = new ProviderRequestControls({ provider: 'Claude', ...config });
let anthropic = client;
function getClient() {
if (!anthropic) {
anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
timeout: config.timeoutMs,
maxRetries: config.maxRetries
});
}
return anthropic;
}
function getSession(streamId) {
const normalizedStreamId = String(streamId || '').trim();
if (!sessions.has(normalizedStreamId)) {
sessions.set(normalizedStreamId, { history: [], queue: Promise.resolve() });
}
return sessions.get(normalizedStreamId);
}
function trimHistory(history) {
const characterCount = () => history.reduce((total, message) => total + message.content.length, 0);
while (
history.length > 2 &&
(history.length > config.maxHistoryMessages || characterCount() > config.maxHistoryCharacters)
) {
history.splice(0, Math.min(2, history.length - 1));
}
}
async function send(userMessage, streamId) {
const normalizedStreamId = String(streamId || '').trim();
const normalizedMessage = String(userMessage || '').trim();
if (!normalizedStreamId) {
throw new ProviderControlError(
'missing_stream_id',
'Claude request requires an RTMS stream ID.',
'Pass event.streamId from the RTMS transcript event.'
);
}
if (!normalizedMessage) {
throw new ProviderControlError(
'empty_input',
'Claude request has no transcript text.',
'Wait for a non-empty RTMS transcript event before calling the provider.'
);
}
const session = getSession(normalizedStreamId);
const operation = session.queue.catch(() => {}).then(async () => {
const userEntry = { role: 'user', content: normalizedMessage };
session.history.push(userEntry);
trimHistory(session.history);
let reservation;
try {
const requestContext = [
SYSTEM_INSTRUCTIONS,
...session.history.map((message) => message.content)
].join('\n');
reservation = controls.reserve(normalizedStreamId, requestContext, config.maxOutputTokens);
const response = await getClient().messages.create({
model: config.model,
system: SYSTEM_INSTRUCTIONS,
max_tokens: config.maxOutputTokens,
messages: session.history
});
const assistantMessage = response.content
.filter((block) => block.type === 'text')
.map((block) => block.text)
.join('\n') || '(no text response)';
session.history.push({ role: 'assistant', content: assistantMessage });
trimHistory(session.history);
controls.complete(reservation, {
inputTokens: response.usage?.input_tokens,
outputTokens: response.usage?.output_tokens
});
return assistantMessage;
} catch (error) {
if (session.history.at(-1) === userEntry) session.history.pop();
if (reservation) controls.fail(reservation);
throw error;
}
});
session.queue = operation.catch(() => {});
return operation;
}
function clearStream(streamId) {
sessions.delete(String(streamId || '').trim());
controls.clearStream(streamId);
}
return {
chatWithClaude: send,
clearClaudeStream: clearStream,
getSessionHistory: (streamId) => [...(sessions.get(String(streamId || '').trim())?.history || [])]
};
}
const defaultService = createClaudeService();
export const chatWithClaude = defaultService.chatWithClaude;
export const clearClaudeStream = defaultService.clearClaudeStream;