Asked what the first message of the conversation was, the assistant answered that it keeps no memory between interactions while the replayed context sat right in front of it. Two causes, both measured on the real model. The system prompt never said the history is its memory, nor who it is, so it fell back on its training habit of denying recall. And Ollama's default context window silently truncates from the beginning: on a 9194-token conversation only 4053 tokens were evaluated, so the oldest messages never reached the model at all. The window is now requested explicitly (16384 by default) and the warm-up asks for the same one, otherwise Ollama reloads the weights and throws the preheat away. The prompt states the identity and forbids the false denial, while still letting the assistant admit a message older than the replayed window.
142 lines
4.1 KiB
JavaScript
142 lines
4.1 KiB
JavaScript
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
|
|
import { buildApp } from '../src/app.js';
|
|
import { loadConfig } from '../src/config.js';
|
|
import { createDatabase } from '../src/db/index.js';
|
|
import { createOllama } from '../src/services/ollama.js';
|
|
import { startMockOllama } from './mock-ollama.js';
|
|
|
|
/** Starts an isolated instance (in-memory database + mock Ollama) on a free port. */
|
|
export async function startServer(options = {}) {
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'jarvise-test-'));
|
|
const mock = await startMockOllama(options);
|
|
|
|
const config = loadConfig({
|
|
env: 'test',
|
|
jwtSecret: 'test-secret',
|
|
dbPath: ':memory:',
|
|
ollamaUrl: mock.url,
|
|
ollamaModel: 'mock:1',
|
|
maxMessagesPerProject: options.maxMessagesPerProject,
|
|
systemPrompt: options.systemPrompt,
|
|
});
|
|
|
|
const db = createDatabase(config.dbPath, {
|
|
maxMessagesPerProject: config.messages.maxPerProject,
|
|
});
|
|
const ollama = createOllama(config);
|
|
const app = await buildApp({ config, db, ollama });
|
|
|
|
await app.listen({ port: 0, host: '127.0.0.1' });
|
|
const { port } = app.server.address();
|
|
|
|
return {
|
|
app,
|
|
db,
|
|
mock,
|
|
base: `http://127.0.0.1:${port}`,
|
|
async close() {
|
|
await app.close();
|
|
await mock.close();
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
},
|
|
};
|
|
}
|
|
|
|
/** Simple JSON request; only sets Content-Type when there is a body. */
|
|
export async function api(base, method, apiPath, { token, body, headers = {} } = {}) {
|
|
const res = await fetch(base + apiPath, {
|
|
method,
|
|
headers: {
|
|
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
...headers,
|
|
},
|
|
body: body ? JSON.stringify(body) : undefined,
|
|
});
|
|
|
|
const text = await res.text();
|
|
let json = null;
|
|
try {
|
|
json = text ? JSON.parse(text) : null;
|
|
} catch {
|
|
json = text;
|
|
}
|
|
|
|
return { status: res.status, json, headers: res.headers };
|
|
}
|
|
|
|
/** Creates a user and returns its token. */
|
|
export async function registerUser(base, email = `u${Date.now()}${Math.random()}@x.io`) {
|
|
const res = await api(base, 'POST', '/api/auth/register', {
|
|
body: { email, password: 'motdepasse123', name: 'Test' },
|
|
});
|
|
if (res.status !== 201) throw new Error(`register failed: ${JSON.stringify(res.json)}`);
|
|
return { token: res.json.token, user: res.json.user, email };
|
|
}
|
|
|
|
/**
|
|
* Consumes an SSE stream and returns each `data:` line verbatim (no JSON parse),
|
|
* which is what the `[DONE]` sentinel requires.
|
|
*/
|
|
export async function readSsePayloads(response) {
|
|
const reader = response.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
const payloads = [];
|
|
let buffer = '';
|
|
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
if (done) break;
|
|
buffer += decoder.decode(value, { stream: true });
|
|
|
|
let sep;
|
|
while ((sep = buffer.indexOf('\n\n')) !== -1) {
|
|
const block = buffer.slice(0, sep);
|
|
buffer = buffer.slice(sep + 2);
|
|
if (!block || block.startsWith(':')) continue;
|
|
|
|
for (const line of block.split('\n')) {
|
|
if (line.startsWith('data: ')) payloads.push(line.slice(6));
|
|
}
|
|
}
|
|
}
|
|
|
|
return payloads;
|
|
}
|
|
|
|
/**
|
|
* Consumes an SSE stream and returns the events as `{ event, data }` objects.
|
|
*/
|
|
export async function readSse(response) {
|
|
const reader = response.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
const events = [];
|
|
let buffer = '';
|
|
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
if (done) break;
|
|
buffer += decoder.decode(value, { stream: true });
|
|
|
|
let sep;
|
|
while ((sep = buffer.indexOf('\n\n')) !== -1) {
|
|
const block = buffer.slice(0, sep);
|
|
buffer = buffer.slice(sep + 2);
|
|
if (!block || block.startsWith(':')) continue;
|
|
|
|
let event = 'message';
|
|
let data = '';
|
|
for (const line of block.split('\n')) {
|
|
if (line.startsWith('event: ')) event = line.slice(7).trim();
|
|
else if (line.startsWith('data: ')) data += line.slice(6);
|
|
}
|
|
events.push({ event, data: data ? JSON.parse(data) : null });
|
|
}
|
|
}
|
|
|
|
return events;
|
|
}
|