feat: implement R42 chat infrastructure with Anthropic API integration and custom design system
All checks were successful
On Push to Main / test (push) Successful in 30s
On Push to Main / publish (push) Successful in 57s
On Push to Main / deploy-dev (push) Successful in 1m31s

This commit is contained in:
RaymondVerhoef
2026-05-17 16:48:40 +02:00
parent 43d01dff58
commit 98e32d8ac0
21 changed files with 2631 additions and 6 deletions

View File

@@ -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({