/** * Stop handler: learn the people and companies the user mentioned since the last * stop, so the prompt-time cards know them next time, and research the new ones on * the web for review. Runs detached; one extraction call, only when the new text * names someone, and one web research call per new entry. */ import { ingestEntities } from "../../tools/knowledge/ingest"; import { lookupCompanyRegister } from "../lib/company-register-router"; import { excludedNames, loadKnownEntities, loadNameIndex } from "../lib/entity-cards"; import { extractionSchema, extractionSystem, extractionUser, markSeen, parseExtraction, planExtraction, queueForReview, seenCursor, unseenUserText, worthExtracting, } from "../lib/entity-extraction"; import { newResearchTargets, researchNewEntities } from "../lib/entity-research"; import { canInfer, inference } from "../lib/inference"; import { logDebug, logError } from "../lib/log"; import { identity, isEnabled, isOptedIn } from "../lib/settings"; import { parseMessages } from "../lib/transcript"; function sourceId(sessionId: string, lastId: string | undefined): string { const turn = lastId ? ` #${lastId.slice(0, 8)}` : ""; return `chat ${new Date().toISOString().slice(0, 10)} ${sessionId.slice(0, 8)}${turn}`; } /** @lintignore exercised directly by test/entity-extraction.test.ts */ export async function extractSessionEntities( transcript: string, sessionId?: string ): Promise { if (!sessionId || !isEnabled("entityExtraction")) return; const { text, count, lastId } = unseenUserText( parseMessages(transcript), seenCursor(sessionId) ); if (lastId) markSeen(sessionId, lastId); if (count === 0) return; const index = loadNameIndex(loadKnownEntities()); const excluded = excludedNames(); if (!worthExtracting(text, index, excluded) || !canInfer()) return; const id = identity(); const result = await inference({ system: extractionSystem(id.principal.name, id.ai.name), user: extractionUser(text, index), tier: "medium", maxTokens: 900, timeout: 90000, jsonSchema: extractionSchema(), caller: "entity-extraction", sessionId, }); if (!result.success || !result.output) return; const source = sourceId(sessionId, lastId); const plan = planExtraction(parseExtraction(result.output), index, excluded, source); const written = ingestEntities(plan.ingest, source); queueForReview(plan.review); logDebug( "entity-extraction", `${written.people.length} people, ${written.companies.length} companies, ${plan.review.length} for review` ); if (isOptedIn("entityResearch")) await researchNewEntities(newResearchTargets(plan.ingest, written), { principal: id.principal.name, infer: inference, registry: lookupCompanyRegister, }); } if (process.argv[2] === "--run") { const sid = process.argv[3]; const transcriptPath = process.argv[4]; if (transcriptPath) { const { readFile, unlink } = await import("node:fs/promises"); try { const transcript = await readFile(transcriptPath, "utf-8"); await extractSessionEntities(transcript, sid === "" ? undefined : sid); } catch (err) { logError("entity-extraction:run", err); } finally { await unlink(transcriptPath).catch(() => {}); } } process.exit(0); }