SPB Git

Repository Public

fake-ai.mjs 2.8 KB 39 lines javascript

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Scripts/fake-ai.mjs3// Description: A FAKE OpenAI-compatible chat-completions server for the local test backend (same wire format as the4// backend's own tests use). It is configured as the `maclustr` provider of the testbed so that the AI screens and the5// SPDriveNext client can be exercised end to end. No real model, no real key: the "answer" is scripted — it quotes6// the first words of the first retrieved passage and cites [1].7// Usage: node Scripts/fake-ai.mjs <port>8import http from 'node:http';9const port = Number(process.argv[2] ?? 8669);10http.createServer((req, res) => {11  let s = ''; req.on('data', (c) => (s += c)); req.on('end', () => {12    let body = {}; try { body = JSON.parse(s); } catch {}13    const text = (body.messages ?? []).map((m) => typeof m.content === 'string' ? m.content : JSON.stringify(m.content)).join('\n');14    if (/FAKE-AUTH-ERROR/.test(text)) { res.writeHead(401, { 'content-type': 'application/json' }); return res.end(JSON.stringify({ error: { message: 'bad key' } })); }15    const m = /\[1\][^\n]*\n([^\n]{1,90})/.exec(text);16    let answer = `Réponse de test (aucun vrai modèle) : « ${m ? m[1].trim() : 'passage introuvable'} » [1].`;17    // an organisation request (ai/suggest) carries a JSON payload listing files, folders and existing tags: answer a scripted proposal18    try {19      const last = (body.messages ?? []).filter((x) => x.role === 'user').pop()?.content;20      const payload = JSON.parse(typeof last === 'string' ? last : '');21      if (Array.isArray(payload.files)) {22        const folder = (payload.folders ?? [])[0];23        answer = JSON.stringify({ suggestions: payload.files.map((f) => ({ fileId: f.fileId, name: `Classé – ${f.name}`, tags: ['Factice'], ...(folder ? { folderId: folder.id } : {}), category: 'Test', reason: 'Proposition du faux fournisseur (aucun vrai modèle).' })) });24      }25    } catch { /* a question, not a payload */ }26    if (body.stream === false) { res.writeHead(200, { 'content-type': 'application/json' }); return res.end(JSON.stringify({ choices: [{ message: { role: 'assistant', content: '{"suggestions":[]}' }, finish_reason: 'stop' }], usage: { prompt_tokens: 10, completion_tokens: 3 } })); }27    res.writeHead(200, { 'content-type': 'text/event-stream' });28    const words = answer.match(/\S+\s*/g) ?? [];29    let i = 0;30    const tick = () => {31      if (i < words.length) { res.write(`data: ${JSON.stringify({ choices: [{ delta: { content: words[i++] } }] })}\n\n`); return setTimeout(tick, 40); }32      res.write(`data: ${JSON.stringify({ choices: [{ delta: {}, finish_reason: 'stop' }] })}\n\n`);33      res.write(`data: ${JSON.stringify({ choices: [], usage: { prompt_tokens: 321, completion_tokens: words.length } })}\n\n`);34      res.end('data: [DONE]\n\n');35    };36    tick();37  });38}).listen(port, '127.0.0.1');39
Files main
Menu

Theme