refactor: make generateText model return ExtractedFact

This commit is contained in:
2026-05-19 22:06:54 +09:00
parent 185edfdae8
commit 0480ea182f
3 changed files with 5 additions and 311 deletions

View File

@@ -53,7 +53,7 @@ describe('IdentityDB ingestion', () => {
expect(topic?.facts).toHaveLength(1);
});
it('ships an LLM extractor adapter that turns structured JSON responses into facts', async () => {
it('ships an LLM extractor adapter that returns structured facts from the model', async () => {
let prompt = '';
const extractor = new LlmFactExtractor({
@@ -61,7 +61,7 @@ describe('IdentityDB ingestion', () => {
async generateText(input) {
prompt = input;
return JSON.stringify({
return {
statement: 'I have worked with Bun and TypeScript since 2025.',
summary: 'The speaker has Bun and TypeScript experience.',
source: 'chat',
@@ -73,7 +73,7 @@ describe('IdentityDB ingestion', () => {
{ name: 'TypeScript', category: 'entity', granularity: 'concrete', role: 'object' },
{ name: '2025', category: 'temporal', granularity: 'concrete', role: 'time' },
],
});
};
},
},
instructions: 'Prefer technology and time topics.',
@@ -91,47 +91,4 @@ describe('IdentityDB ingestion', () => {
expect(fact.metadata).toEqual({ channel: 'telegram' });
expect(fact.topics.map((topic) => topic.name)).toEqual(['I', 'Bun', 'TypeScript', '2025']);
});
it('parses JSON responses wrapped in markdown code fences', async () => {
const extractor = new LlmFactExtractor({
model: {
async generateText() {
return [
'Here is the extracted fact:',
'```json',
JSON.stringify({
statement: 'Bun powers TypeScript tooling.',
topics: [
{ name: 'Bun', category: 'entity', granularity: 'concrete' },
{ name: 'TypeScript', category: 'entity', granularity: 'concrete' },
],
}),
'```',
].join('\n');
},
},
});
const fact = await db.ingestStatement('Bun powers TypeScript tooling.', {
extractor,
});
expect(fact.topics.map((topic) => topic.name)).toEqual(['Bun', 'TypeScript']);
});
it('rejects invalid LLM responses before writing facts', async () => {
const extractor = new LlmFactExtractor({
model: {
async generateText() {
return 'not json at all';
},
},
});
await expect(
db.ingestStatement('Bun powers TypeScript tooling.', {
extractor,
}),
).rejects.toThrow('LLM extractor returned invalid JSON.');
});
});