feat: implement R42 chat infrastructure with Anthropic API integration and custom design system
This commit is contained in:
@@ -64,6 +64,61 @@ export const anthropicApi = {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Multi-turn chat with optional tool use.
|
||||
* Returns the raw Anthropic response so callers can read both `text` and
|
||||
* `tool_use` content blocks.
|
||||
*
|
||||
* @param {string} systemPrompt
|
||||
* @param {Array<{role: 'user'|'assistant', content: string}>} messages
|
||||
* @param {{tools?: Array}} opts
|
||||
* @returns {Promise<{content: Array, stop_reason: string}>}
|
||||
*/
|
||||
anthropicApi.chat = async function chat(systemPrompt, messages, opts = {}) {
|
||||
const useSimulation = storage.get('admin:use_simulation') === true;
|
||||
if (useSimulation) {
|
||||
await new Promise(r => setTimeout(r, 600));
|
||||
return {
|
||||
content: [{
|
||||
type: 'text',
|
||||
text: 'Simulatiemodus staat aan — vraag een beheerder om Simulation Mode uit te zetten in Admin → Settings om met R42 te chatten.',
|
||||
}],
|
||||
stop_reason: 'end_turn',
|
||||
};
|
||||
}
|
||||
|
||||
const model = storage.get('admin:model') || DEFAULT_MODEL;
|
||||
|
||||
const body = {
|
||||
model,
|
||||
max_tokens: 1024,
|
||||
system: systemPrompt,
|
||||
messages,
|
||||
};
|
||||
if (opts.tools && opts.tools.length) body.tools = opts.tools;
|
||||
|
||||
const response = await fetch('/api/anthropic/v1/messages', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'anthropic-version': '2023-06-01',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errData = await response.json().catch(() => ({}));
|
||||
throw new Error(`API Error: ${response.status} ${response.statusText} - ${JSON.stringify(errData)}`);
|
||||
}
|
||||
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
if (!contentType.includes('application/json')) {
|
||||
throw new Error('Your session has expired. Please refresh the page and log in again.');
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
async function simulateResponse() {
|
||||
await new Promise(r => setTimeout(r, 2000));
|
||||
return JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user