Pivot to text-first column-stack workspace + merge-with-review across AI artifacts
Workspace - Pivot from "set of open panes" to a Finder-style miller column stack: TopBar / LeftSidebar / [section → entity → entity ...] / pinned text editor. openPanesStore is now an ordered Column[] with pushFrom / closeFrom / setStack; only one top-level section is rooted at a time. - New entity column panes: Term, Block, Association, Constraint, Requirement, Finding. Click-through navigation truncates deeper columns automatically. - LeftSidebar surfaces a pending-count chip per section (single-glance navigation cue) and spins its analyze ↻ via SVG Spinner whenever the LLM is working — including server-initiated runs caught by the runs poll, not just user-triggered ones. Analyze pipeline + persistence - Unified `concepts` pass (taxonomy + glossary in one LLM call) replaces the two-pass setup. Server still accepts ?section=taxonomy|glossary and normalizes them for back-compat. - model / requirements / detection (assumptions, risks, inconsistencies) + cross-layer validation rules (X1–X4: stale term link, unlinked formalism, undefined linked term, prose-only term). - Persistence: NarrativeDocument, ModelSnapshot, ChangelogEntry, TaxonomyTerm, RequirementEntry, Finding, AnalysisRun. Re-runs MERGE instead of replace: gentle update on existing items, suggested on new, deprecated on missing — same idiom for every artifact kind. User pins preserve "kept" decisions across re-analyses. - Migrations: pivot_text_first, add_requirement_linked_term, term_review_state, review_state_for_reqs_and_findings, add_term_definition_pinned. Concept ↔ ontology integration - linkedTermId on Block / Association / Constraint / Requirement. PromoteToolbar lets the user formalize a concept inline: + Block / + Association / + Constraint / + Requirement, all routed through applyOps so undo/redo and SSE work for free. - decideElement op for in-canvas keep/discard on review-pending model elements. User-authored definitions - TermColumn definition is click-to-edit. Save (Cmd-Enter / blur), Cancel (Esc), Reset to AI suggestion when pinned. - definitionPinned flag on TaxonomyTerm: future Analyze runs leave the user's text alone. setTermDefinition repo function + POST /api/projects/[id]/terms/[termId]/definition endpoint. - mergeTaxonomySuggestion + applyGlossaryDefinitions both pin-aware. UX/UI - StatusChip: single component for all state idioms (suggested, deprecated, accepted, dismissed, resolved, severity, validation code, confidence, warn). Replaces 5+ ad-hoc badge classes. - PaneControls (PaneViewTabs + PaneFilterChip): separates view-mode toggles from filter chips so toggling Pending no longer flips you off the current view. - PaneEmpty: unified empty-state with title + hint + action. - PaneDrawer: collapsible groups for Pending / Discarded review; cards group as Kept (top) → Pending (bottom drawer) → Discarded (Findings only, hidden when empty). Restore action recovers dismissed/resolved findings. - ConceptCard unifies Tree and A–Z views in Concepts; only Tree parents carry the chevron (no empty placeholder offset). - Type + spacing tokens (--text-xs..xl, --space-1..6, --lh-tight/ui/ prose, --radius-*) replace every ad-hoc value. - Buttons standardized to body sans 500 (was a mishmash of mono / display). - Card shells unified across Concepts / Requirements / Findings. Cleanup - Removed: LeftRail, FindingsPanel, IssuesPanel, SocratesDock, ProposalCard, SlashMenu, SlashExtension, slashSuggestion, CanvasHeader, TaxonomyPane, GlossaryPane, TermDetail (popover; now TermColumn). - Section ids in openPanesStore: dropped taxonomy/glossary, added concepts. localStorage migration runs on hydrate. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
95
apps/web/lib/llm/analyze/concepts.ts
Normal file
95
apps/web/lib/llm/analyze/concepts.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
// Single-pass "concepts" analyzer — replaces the legacy taxonomy + glossary
|
||||
// pair (T3 of the integration plan). One LLM call emits both the term list
|
||||
// (with hierarchy + synonyms) and definitions, so the two layers can never
|
||||
// drift out of sync.
|
||||
//
|
||||
// The legacy `taxonomy` / `glossary` analyzers (analyze/taxonomy.ts and
|
||||
// analyze/glossary.ts) are still in the tree as deprecated fallbacks for one
|
||||
// release; nothing in the runtime path imports them anymore.
|
||||
|
||||
import "server-only";
|
||||
import { defaultGateway, chatJSON, type Message } from "../gateway";
|
||||
import { loadPrompt } from "../prompts";
|
||||
import type { DetectedTerm } from "../../db/repo";
|
||||
|
||||
interface RawTerm {
|
||||
label?: string;
|
||||
parentLabel?: string | null;
|
||||
synonyms?: string[];
|
||||
definition?: string;
|
||||
}
|
||||
|
||||
const conceptsJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["terms"],
|
||||
properties: {
|
||||
terms: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["label"],
|
||||
properties: {
|
||||
label: { type: "string", minLength: 1 },
|
||||
parentLabel: { type: ["string", "null"] },
|
||||
synonyms: { type: "array", items: { type: "string" } },
|
||||
definition: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
export interface ConceptsResult {
|
||||
terms: DetectedTerm[];
|
||||
definitions: Array<{ label: string; definition: string }>;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
provider: string;
|
||||
model: string;
|
||||
}
|
||||
|
||||
export async function analyzeConcepts(documentText: string): Promise<ConceptsResult> {
|
||||
const prompt = loadPrompt("socrates/analyze-concepts.md");
|
||||
const gateway = defaultGateway();
|
||||
const messages: Message[] = [
|
||||
{ role: "system", content: prompt },
|
||||
{ role: "user", content: `Document:\n\n${documentText}` },
|
||||
];
|
||||
|
||||
const { value, result } = await chatJSON<{ terms?: RawTerm[] }>(gateway, messages, {
|
||||
temperature: 0.2,
|
||||
maxTokens: 2400,
|
||||
jsonSchema: { name: "concepts", schema: conceptsJsonSchema as Record<string, unknown> },
|
||||
jsonObjectMode: true,
|
||||
maxRepairs: 2,
|
||||
});
|
||||
|
||||
const seen = new Set<string>();
|
||||
const terms: DetectedTerm[] = [];
|
||||
const definitions: Array<{ label: string; definition: string }> = [];
|
||||
for (const t of value?.terms ?? []) {
|
||||
const label = (t.label ?? "").trim();
|
||||
if (!label) continue;
|
||||
const key = label.toLowerCase();
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
terms.push({
|
||||
label,
|
||||
parentLabel: t.parentLabel?.trim() || null,
|
||||
synonyms: Array.isArray(t.synonyms) ? t.synonyms.filter(s => typeof s === "string") : [],
|
||||
});
|
||||
const def = (t.definition ?? "").trim();
|
||||
if (def) definitions.push({ label, definition: def });
|
||||
}
|
||||
|
||||
return {
|
||||
terms,
|
||||
definitions,
|
||||
inputTokens: result.inputTokens,
|
||||
outputTokens: result.outputTokens,
|
||||
provider: gateway.provider,
|
||||
model: gateway.model,
|
||||
};
|
||||
}
|
||||
81
apps/web/lib/llm/analyze/glossary.ts
Normal file
81
apps/web/lib/llm/analyze/glossary.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
// Analyze pass: write definitions for taxonomy terms grounded in the prose.
|
||||
|
||||
import "server-only";
|
||||
import { defaultGateway, chatJSON, type Message } from "../gateway";
|
||||
import { loadPrompt } from "../prompts";
|
||||
|
||||
const glossaryJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["definitions"],
|
||||
properties: {
|
||||
definitions: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["label", "definition"],
|
||||
properties: {
|
||||
label: { type: "string", minLength: 1 },
|
||||
definition: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
export interface GlossaryResult {
|
||||
defs: Array<{ label: string; definition: string }>;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
provider: string;
|
||||
model: string;
|
||||
}
|
||||
|
||||
export async function analyzeGlossary(
|
||||
documentText: string,
|
||||
terms: Array<{ label: string; parentLabel?: string | null; synonyms?: string[] }>
|
||||
): Promise<GlossaryResult> {
|
||||
if (terms.length === 0) {
|
||||
return { defs: [], inputTokens: 0, outputTokens: 0, provider: "n/a", model: "n/a" };
|
||||
}
|
||||
|
||||
const prompt = loadPrompt("socrates/analyze-glossary.md");
|
||||
const gateway = defaultGateway();
|
||||
const userPayload =
|
||||
`Document:\n\n${documentText}\n\n---\n\n` +
|
||||
`Terms:\n\`\`\`json\n${JSON.stringify(terms, null, 2)}\n\`\`\``;
|
||||
|
||||
const messages: Message[] = [
|
||||
{ role: "system", content: prompt },
|
||||
{ role: "user", content: userPayload },
|
||||
];
|
||||
|
||||
const { value, result } = await chatJSON<{ definitions?: Array<{ label?: string; definition?: string }> }>(
|
||||
gateway,
|
||||
messages,
|
||||
{
|
||||
temperature: 0.2,
|
||||
maxTokens: 1800,
|
||||
jsonSchema: { name: "glossary", schema: glossaryJsonSchema as Record<string, unknown> },
|
||||
jsonObjectMode: true,
|
||||
maxRepairs: 2,
|
||||
}
|
||||
);
|
||||
|
||||
const defs: Array<{ label: string; definition: string }> = [];
|
||||
for (const d of value?.definitions ?? []) {
|
||||
const label = (d.label ?? "").trim();
|
||||
const def = (d.definition ?? "").trim();
|
||||
if (!label || !def) continue;
|
||||
defs.push({ label, definition: def });
|
||||
}
|
||||
|
||||
return {
|
||||
defs,
|
||||
inputTokens: result.inputTokens,
|
||||
outputTokens: result.outputTokens,
|
||||
provider: gateway.provider,
|
||||
model: gateway.model,
|
||||
};
|
||||
}
|
||||
496
apps/web/lib/llm/analyze/model.ts
Normal file
496
apps/web/lib/llm/analyze/model.ts
Normal file
@@ -0,0 +1,496 @@
|
||||
// Analyze pass: derive (or refresh) the SysML model from prose + taxonomy.
|
||||
//
|
||||
// Differs from generateModel.ts (the seed-time generator) in two ways:
|
||||
// 1. Inputs are prose + the current taxonomy term list, not a SeedDraft.
|
||||
// 2. Re-runs *merge* with the existing model: blocks already linked to a
|
||||
// taxonomy term are preserved (id, position, properties) so the user's
|
||||
// manual canvas work isn't blown away on every Analyze.
|
||||
|
||||
import "server-only";
|
||||
import { defaultGateway, chatJSON, type Message } from "../gateway";
|
||||
import { loadPrompt } from "../prompts";
|
||||
import type {
|
||||
SysMLModel,
|
||||
Block,
|
||||
Association,
|
||||
Constraint,
|
||||
Requirement,
|
||||
PropertyType,
|
||||
ReviewStatus,
|
||||
} from "../../sysml/model";
|
||||
|
||||
interface LeanProperty {
|
||||
name: string;
|
||||
type: { kind: "string" | "number" | "boolean" | "enum"; values?: string[] };
|
||||
}
|
||||
interface LeanBlock {
|
||||
id: string;
|
||||
label: string;
|
||||
kind: "system" | "actor" | "block";
|
||||
linkedTermLabel?: string;
|
||||
properties?: LeanProperty[];
|
||||
confidence: number;
|
||||
}
|
||||
interface LeanAssoc {
|
||||
id: string;
|
||||
fromBlockId: string;
|
||||
toBlockId: string;
|
||||
label?: string;
|
||||
kind: "association" | "composition" | "aggregation" | "generalization" | "constraintApplies";
|
||||
/** Optional concept-name for the relationship itself (T2 integration). */
|
||||
linkedTermLabel?: string;
|
||||
confidence: number;
|
||||
}
|
||||
interface LeanConstraint {
|
||||
id: string;
|
||||
label: string;
|
||||
expression?: string;
|
||||
appliesTo?: string[];
|
||||
/** Optional concept-name the constraint enforces. */
|
||||
linkedTermLabel?: string;
|
||||
confidence: number;
|
||||
}
|
||||
interface LeanRequirement {
|
||||
id: string;
|
||||
tag: string;
|
||||
text: string;
|
||||
satisfiedBy?: string[];
|
||||
/** Optional concept this requirement is "about." */
|
||||
linkedTermLabel?: string;
|
||||
confidence: number;
|
||||
}
|
||||
interface LeanModel {
|
||||
systemOfInterestId?: string;
|
||||
blocks: LeanBlock[];
|
||||
associations?: LeanAssoc[];
|
||||
constraints?: LeanConstraint[];
|
||||
requirements?: LeanRequirement[];
|
||||
overallConfidence?: number;
|
||||
}
|
||||
|
||||
const modelJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["blocks"],
|
||||
properties: {
|
||||
systemOfInterestId: { type: "string" },
|
||||
blocks: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["id", "label", "kind", "confidence"],
|
||||
properties: {
|
||||
id: { type: "string", minLength: 1 },
|
||||
label: { type: "string", minLength: 1 },
|
||||
kind: { type: "string", enum: ["system", "actor", "block"] },
|
||||
linkedTermLabel: { type: "string" },
|
||||
confidence: { type: "number", minimum: 0, maximum: 1 },
|
||||
properties: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["name", "type"],
|
||||
properties: {
|
||||
name: { type: "string", minLength: 1 },
|
||||
type: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["kind"],
|
||||
properties: {
|
||||
kind: { type: "string", enum: ["string", "number", "boolean", "enum"] },
|
||||
values: { type: "array", items: { type: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
associations: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["id", "fromBlockId", "toBlockId", "kind", "confidence"],
|
||||
properties: {
|
||||
id: { type: "string", minLength: 1 },
|
||||
fromBlockId: { type: "string", minLength: 1 },
|
||||
toBlockId: { type: "string", minLength: 1 },
|
||||
label: { type: "string" },
|
||||
kind: {
|
||||
type: "string",
|
||||
enum: ["association", "composition", "aggregation", "generalization", "constraintApplies"],
|
||||
},
|
||||
linkedTermLabel: { type: "string" },
|
||||
confidence: { type: "number", minimum: 0, maximum: 1 },
|
||||
},
|
||||
},
|
||||
},
|
||||
constraints: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["id", "label", "confidence"],
|
||||
properties: {
|
||||
id: { type: "string", minLength: 1 },
|
||||
label: { type: "string", minLength: 1 },
|
||||
expression: { type: "string" },
|
||||
appliesTo: { type: "array", items: { type: "string" } },
|
||||
linkedTermLabel: { type: "string" },
|
||||
confidence: { type: "number", minimum: 0, maximum: 1 },
|
||||
},
|
||||
},
|
||||
},
|
||||
requirements: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["id", "tag", "text", "confidence"],
|
||||
properties: {
|
||||
id: { type: "string", minLength: 1 },
|
||||
tag: { type: "string", minLength: 1 },
|
||||
text: { type: "string", minLength: 1 },
|
||||
satisfiedBy: { type: "array", items: { type: "string" } },
|
||||
linkedTermLabel: { type: "string" },
|
||||
confidence: { type: "number", minimum: 0, maximum: 1 },
|
||||
},
|
||||
},
|
||||
},
|
||||
overallConfidence: { type: "number", minimum: 0, maximum: 1 },
|
||||
},
|
||||
} as const;
|
||||
|
||||
export interface AnalyzeModelResult {
|
||||
model: SysMLModel;
|
||||
termIdToBlockId: Map<string, string>;
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
provider: string;
|
||||
model_name: string;
|
||||
}
|
||||
|
||||
export async function analyzeModelFromProse(
|
||||
documentText: string,
|
||||
terms: Array<{ id: string; label: string; linkedBlockId: string | null }>,
|
||||
current: SysMLModel
|
||||
): Promise<AnalyzeModelResult> {
|
||||
const character = loadPrompt("socrates/character.md");
|
||||
const generate = loadPrompt("socrates/analyze-model.md");
|
||||
|
||||
const userPayload =
|
||||
`Document:\n\n${documentText}\n\n---\n\n` +
|
||||
`Taxonomy:\n\`\`\`json\n${JSON.stringify(terms.map(t => t.label), null, 2)}\n\`\`\`\n` +
|
||||
`Return only the JSON object conforming to the schema.`;
|
||||
|
||||
const messages: Message[] = [
|
||||
{ role: "system", content: `${character}\n\n---\n\n${generate}` },
|
||||
{ role: "user", content: userPayload },
|
||||
];
|
||||
|
||||
const gateway = defaultGateway();
|
||||
const { value, result } = await chatJSON<LeanModel>(gateway, messages, {
|
||||
temperature: 0.3,
|
||||
maxTokens: 2400,
|
||||
jsonSchema: { name: "sysml_model", schema: modelJsonSchema as Record<string, unknown> },
|
||||
jsonObjectMode: true,
|
||||
maxRepairs: 2,
|
||||
});
|
||||
|
||||
const merged = mergeIntoCurrent(value, current, terms);
|
||||
|
||||
return {
|
||||
...merged,
|
||||
inputTokens: result.inputTokens,
|
||||
outputTokens: result.outputTokens,
|
||||
provider: gateway.provider,
|
||||
model_name: gateway.model,
|
||||
};
|
||||
}
|
||||
|
||||
// ─── Lean → canonical, merged with current model (review-aware) ─────────
|
||||
//
|
||||
// Re-running the analyzer is non-destructive. For each element kind we
|
||||
// match against current by a stable key:
|
||||
// block — id (preserved via linkedTermId chain) OR linkedTermId
|
||||
// association — `${kind}|${fromBlockId}|${toBlockId}`
|
||||
// constraint — `${linkedTermId ?? "label:"+normalize(label)}`
|
||||
// requirement — `${linkedTermId ?? "text:"+normalize(text)}`
|
||||
//
|
||||
// New incoming → reviewStatus = "suggested".
|
||||
// Matched + currently deprecated → flips back to "suggested".
|
||||
// Existing not in incoming + not pinned + not currently suggested → "deprecated".
|
||||
// Existing not in incoming + status was "suggested" → quietly dropped.
|
||||
// Pinned elements left alone regardless.
|
||||
|
||||
function mergeIntoCurrent(
|
||||
lean: LeanModel | null | undefined,
|
||||
current: SysMLModel,
|
||||
terms: Array<{ id: string; label: string; linkedBlockId: string | null }>
|
||||
): { model: SysMLModel; termIdToBlockId: Map<string, string> } {
|
||||
if (!lean || !Array.isArray(lean.blocks)) {
|
||||
return { model: current, termIdToBlockId: new Map() };
|
||||
}
|
||||
|
||||
const termByLabel = new Map<string, { id: string; linkedBlockId: string | null }>();
|
||||
for (const t of terms) termByLabel.set(t.label.toLowerCase(), { id: t.id, linkedBlockId: t.linkedBlockId });
|
||||
const resolveTermId = (label: string | undefined): string | undefined => {
|
||||
if (!label) return undefined;
|
||||
return termByLabel.get(label.toLowerCase())?.id;
|
||||
};
|
||||
|
||||
// ─── Blocks ────────────────────────────────────────────────────────────
|
||||
|
||||
const existingByLinkedTerm = new Map<string, Block>();
|
||||
const existingById = new Map<string, Block>();
|
||||
for (const b of current.blocks) {
|
||||
existingById.set(b.id, b);
|
||||
if (b.linkedTermId) existingByLinkedTerm.set(b.linkedTermId, b);
|
||||
}
|
||||
|
||||
const blocks: Block[] = [];
|
||||
const leanIdToCanonical = new Map<string, string>();
|
||||
const termIdToBlockId = new Map<string, string>();
|
||||
const matchedBlockIds = new Set<string>();
|
||||
|
||||
for (const b of lean.blocks) {
|
||||
const linkedTerm = b.linkedTermLabel ? termByLabel.get(b.linkedTermLabel.toLowerCase()) : undefined;
|
||||
|
||||
let existing: Block | undefined;
|
||||
if (linkedTerm) existing = existingByLinkedTerm.get(linkedTerm.id);
|
||||
if (!existing && existingById.has(b.id)) existing = existingById.get(b.id);
|
||||
|
||||
let canonicalId = b.id;
|
||||
let stereotypes: string[] = [b.kind];
|
||||
let propertiesSource = b.properties ?? [];
|
||||
let nextStatus: ReviewStatus = "suggested";
|
||||
let pinned = false;
|
||||
|
||||
if (existing) {
|
||||
canonicalId = existing.id;
|
||||
stereotypes = existing.stereotypes;
|
||||
propertiesSource = mergeProperties(existing.properties, b.properties ?? []);
|
||||
pinned = !!existing.reviewPinned;
|
||||
// Existing accepted stays accepted; existing deprecated flips back to
|
||||
// suggested (analyzer changed its mind); existing suggested stays
|
||||
// suggested.
|
||||
const cur: ReviewStatus = (existing.reviewStatus ?? "accepted") as ReviewStatus;
|
||||
nextStatus = cur === "deprecated" ? "suggested" : cur;
|
||||
matchedBlockIds.add(existing.id);
|
||||
}
|
||||
|
||||
if (linkedTerm) termIdToBlockId.set(linkedTerm.id, canonicalId);
|
||||
leanIdToCanonical.set(b.id, canonicalId);
|
||||
|
||||
blocks.push({
|
||||
id: canonicalId,
|
||||
label: b.label,
|
||||
kind: b.kind,
|
||||
stereotypes,
|
||||
properties: propertiesSource.map((p, i) => ({
|
||||
id: `${canonicalId}_p${i + 1}`,
|
||||
name: p.name,
|
||||
type: expandPropertyType(p.type),
|
||||
multiplicity: "0..1" as const,
|
||||
})),
|
||||
linkedTermId: linkedTerm?.id,
|
||||
reviewStatus: nextStatus,
|
||||
reviewPinned: pinned,
|
||||
});
|
||||
}
|
||||
|
||||
// Existing blocks the analyzer didn't mention.
|
||||
for (const b of current.blocks) {
|
||||
if (matchedBlockIds.has(b.id)) continue;
|
||||
const cur: ReviewStatus = (b.reviewStatus ?? "accepted") as ReviewStatus;
|
||||
if (cur === "suggested") continue; // never reviewed + dropped → drop.
|
||||
if (b.reviewPinned) {
|
||||
blocks.push(b);
|
||||
continue;
|
||||
}
|
||||
blocks.push({ ...b, reviewStatus: "deprecated" });
|
||||
}
|
||||
|
||||
// ─── Associations ─────────────────────────────────────────────────────
|
||||
|
||||
const associations = mergeListByKey<Association, LeanAssoc>({
|
||||
existing: current.associations,
|
||||
incoming: lean.associations ?? [],
|
||||
keyExisting: a => `${a.kind}|${a.fromBlockId}|${a.toBlockId}`,
|
||||
keyIncoming: a =>
|
||||
`${a.kind}|${leanIdToCanonical.get(a.fromBlockId) ?? a.fromBlockId}|${
|
||||
leanIdToCanonical.get(a.toBlockId) ?? a.toBlockId
|
||||
}`,
|
||||
fromIncoming: a => ({
|
||||
id: a.id,
|
||||
fromBlockId: leanIdToCanonical.get(a.fromBlockId) ?? a.fromBlockId,
|
||||
toBlockId: leanIdToCanonical.get(a.toBlockId) ?? a.toBlockId,
|
||||
label: a.label ?? "",
|
||||
kind: a.kind,
|
||||
linkedTermId: resolveTermId(a.linkedTermLabel),
|
||||
}),
|
||||
mergeFields: (existing, incoming) => ({
|
||||
...existing,
|
||||
label: incoming.label ?? existing.label,
|
||||
// linkedTermId: prefer fresh suggestion, fall back to existing.
|
||||
linkedTermId:
|
||||
resolveTermId(incoming.linkedTermLabel) ?? existing.linkedTermId,
|
||||
}),
|
||||
});
|
||||
|
||||
// ─── Constraints ──────────────────────────────────────────────────────
|
||||
|
||||
const constraints = mergeListByKey<Constraint, LeanConstraint>({
|
||||
existing: current.constraints,
|
||||
incoming: lean.constraints ?? [],
|
||||
keyExisting: c => c.linkedTermId ? `term:${c.linkedTermId}` : `label:${normalizeText(c.label)}`,
|
||||
keyIncoming: c => {
|
||||
const tid = resolveTermId(c.linkedTermLabel);
|
||||
return tid ? `term:${tid}` : `label:${normalizeText(c.label)}`;
|
||||
},
|
||||
fromIncoming: c => ({
|
||||
id: c.id,
|
||||
label: c.label,
|
||||
expression: c.expression ?? "",
|
||||
appliesTo: (c.appliesTo ?? []).map(x => leanIdToCanonical.get(x) ?? x),
|
||||
linkedTermId: resolveTermId(c.linkedTermLabel),
|
||||
}),
|
||||
mergeFields: (existing, incoming) => ({
|
||||
...existing,
|
||||
label: incoming.label,
|
||||
expression: incoming.expression ?? existing.expression,
|
||||
appliesTo: (incoming.appliesTo ?? []).map(x => leanIdToCanonical.get(x) ?? x),
|
||||
linkedTermId: resolveTermId(incoming.linkedTermLabel) ?? existing.linkedTermId,
|
||||
}),
|
||||
});
|
||||
|
||||
// ─── Requirements ─────────────────────────────────────────────────────
|
||||
|
||||
const requirements = mergeListByKey<Requirement, LeanRequirement>({
|
||||
existing: current.requirements,
|
||||
incoming: lean.requirements ?? [],
|
||||
keyExisting: r => r.linkedTermId ? `term:${r.linkedTermId}` : `text:${normalizeText(r.text)}`,
|
||||
keyIncoming: r => {
|
||||
const tid = resolveTermId(r.linkedTermLabel);
|
||||
return tid ? `term:${tid}` : `text:${normalizeText(r.text)}`;
|
||||
},
|
||||
fromIncoming: r => ({
|
||||
id: r.id,
|
||||
tag: r.tag,
|
||||
text: r.text,
|
||||
relations: (r.satisfiedBy ?? []).map(blockId => ({
|
||||
kind: "satisfy" as const,
|
||||
blockId: leanIdToCanonical.get(blockId) ?? blockId,
|
||||
})),
|
||||
linkedTermId: resolveTermId(r.linkedTermLabel),
|
||||
}),
|
||||
mergeFields: (existing, incoming) => ({
|
||||
...existing,
|
||||
tag: incoming.tag || existing.tag,
|
||||
text: incoming.text || existing.text,
|
||||
relations: (incoming.satisfiedBy ?? []).map(blockId => ({
|
||||
kind: "satisfy" as const,
|
||||
blockId: leanIdToCanonical.get(blockId) ?? blockId,
|
||||
})),
|
||||
linkedTermId: resolveTermId(incoming.linkedTermLabel) ?? existing.linkedTermId,
|
||||
}),
|
||||
});
|
||||
|
||||
let soiId = lean.systemOfInterestId ? leanIdToCanonical.get(lean.systemOfInterestId) ?? lean.systemOfInterestId : undefined;
|
||||
if (!soiId) {
|
||||
const systems = blocks.filter(b => b.kind === "system");
|
||||
if (systems.length === 1) soiId = systems[0]!.id;
|
||||
}
|
||||
|
||||
return { model: { systemOfInterestId: soiId, blocks, associations, constraints, requirements }, termIdToBlockId };
|
||||
}
|
||||
|
||||
// ─── Generic merge-with-review helper ──────────────────────────────────
|
||||
|
||||
interface ReviewableElement {
|
||||
reviewStatus?: ReviewStatus;
|
||||
reviewPinned?: boolean;
|
||||
}
|
||||
|
||||
interface MergeArgs<E extends ReviewableElement, I> {
|
||||
existing: E[];
|
||||
incoming: I[];
|
||||
keyExisting: (e: E) => string;
|
||||
keyIncoming: (i: I) => string;
|
||||
fromIncoming: (i: I) => E;
|
||||
mergeFields: (existing: E, incoming: I) => E;
|
||||
}
|
||||
|
||||
function mergeListByKey<E extends ReviewableElement, I>(args: MergeArgs<E, I>): E[] {
|
||||
const existingByKey = new Map<string, E>();
|
||||
for (const e of args.existing) existingByKey.set(args.keyExisting(e), e);
|
||||
|
||||
const incomingByKey = new Map<string, I>();
|
||||
for (const i of args.incoming) {
|
||||
const k = args.keyIncoming(i);
|
||||
if (!incomingByKey.has(k)) incomingByKey.set(k, i);
|
||||
}
|
||||
|
||||
const out: E[] = [];
|
||||
const matched = new Set<string>();
|
||||
|
||||
// Phase 1 — incoming.
|
||||
for (const [key, inc] of incomingByKey.entries()) {
|
||||
const prior = existingByKey.get(key);
|
||||
if (!prior) {
|
||||
const created = args.fromIncoming(inc);
|
||||
out.push({ ...created, reviewStatus: "suggested", reviewPinned: false });
|
||||
} else {
|
||||
const merged = args.mergeFields(prior, inc);
|
||||
const cur: ReviewStatus = (prior.reviewStatus ?? "accepted") as ReviewStatus;
|
||||
const nextStatus: ReviewStatus = cur === "deprecated" ? "suggested" : cur;
|
||||
out.push({ ...merged, reviewStatus: nextStatus, reviewPinned: !!prior.reviewPinned });
|
||||
matched.add(key);
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 2 — existing not in incoming.
|
||||
for (const e of args.existing) {
|
||||
const k = args.keyExisting(e);
|
||||
if (matched.has(k)) continue;
|
||||
const cur: ReviewStatus = (e.reviewStatus ?? "accepted") as ReviewStatus;
|
||||
if (cur === "suggested") continue; // unreviewed + dropped → drop.
|
||||
if (e.reviewPinned) {
|
||||
out.push(e);
|
||||
continue;
|
||||
}
|
||||
out.push({ ...e, reviewStatus: "deprecated" });
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
function normalizeText(s: string): string {
|
||||
return (s ?? "").trim().toLowerCase().replace(/\s+/g, " ");
|
||||
}
|
||||
|
||||
function mergeProperties(
|
||||
current: { name: string }[],
|
||||
incoming: LeanProperty[]
|
||||
): LeanProperty[] {
|
||||
const have = new Set(current.map(p => p.name.toLowerCase()));
|
||||
const merged: LeanProperty[] = current.map(p => ({
|
||||
name: p.name,
|
||||
type: { kind: "string" }, // type re-expanded in caller
|
||||
}));
|
||||
for (const p of incoming) {
|
||||
if (!have.has(p.name.toLowerCase())) merged.push(p);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
function expandPropertyType(type: { kind: string; values?: string[] }): PropertyType {
|
||||
if (type.kind === "enum") return { kind: "enum", values: type.values ?? [] };
|
||||
if (type.kind === "number") return { kind: "number" };
|
||||
if (type.kind === "boolean") return { kind: "boolean" };
|
||||
return { kind: "string" };
|
||||
}
|
||||
52
apps/web/lib/llm/analyze/proseText.ts
Normal file
52
apps/web/lib/llm/analyze/proseText.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
// Convert a ProseMirror JSON document into a plain-text string for LLM input.
|
||||
// Headings get markdown-style "##"; chip nodes render as their label so the
|
||||
// analyzer sees the same surface forms a human would.
|
||||
|
||||
import "server-only";
|
||||
|
||||
interface PMNode {
|
||||
type: string;
|
||||
text?: string;
|
||||
attrs?: Record<string, unknown>;
|
||||
content?: PMNode[];
|
||||
}
|
||||
|
||||
export function proseDocToPlainText(doc: unknown): string {
|
||||
if (!doc || typeof doc !== "object") return "";
|
||||
const root = doc as PMNode;
|
||||
return walk(root, 0).trim();
|
||||
}
|
||||
|
||||
function walk(node: PMNode, depth: number): string {
|
||||
if (!node) return "";
|
||||
if (node.type === "text") return node.text ?? "";
|
||||
if (node.type === "chip") {
|
||||
const label = (node.attrs?.label as string | undefined) ?? "";
|
||||
return label;
|
||||
}
|
||||
|
||||
const children = (node.content ?? []).map(c => walk(c, depth + 1)).join("");
|
||||
|
||||
switch (node.type) {
|
||||
case "heading": {
|
||||
const level = typeof node.attrs?.level === "number" ? (node.attrs.level as number) : 1;
|
||||
const hash = "#".repeat(Math.max(1, Math.min(level, 6)));
|
||||
return `\n\n${hash} ${children}\n\n`;
|
||||
}
|
||||
case "paragraph":
|
||||
return `${children}\n\n`;
|
||||
case "bulletList":
|
||||
case "bullet_list":
|
||||
case "orderedList":
|
||||
case "ordered_list":
|
||||
return `${children}\n`;
|
||||
case "listItem":
|
||||
case "list_item":
|
||||
return `- ${children.trim()}\n`;
|
||||
case "hardBreak":
|
||||
case "hard_break":
|
||||
return "\n";
|
||||
default:
|
||||
return children;
|
||||
}
|
||||
}
|
||||
104
apps/web/lib/llm/analyze/requirements.ts
Normal file
104
apps/web/lib/llm/analyze/requirements.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
// Analyze pass: extract requirements from prose with traceability hints.
|
||||
|
||||
import "server-only";
|
||||
import { defaultGateway, chatJSON, type Message } from "../gateway";
|
||||
import { loadPrompt } from "../prompts";
|
||||
import type { DetectedRequirement } from "../../db/repo";
|
||||
|
||||
const reqsJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["requirements"],
|
||||
properties: {
|
||||
requirements: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["tag", "text"],
|
||||
properties: {
|
||||
tag: { type: "string", minLength: 1 },
|
||||
text: { type: "string", minLength: 1 },
|
||||
tracedToLabels: { type: "array", items: { type: "string" } },
|
||||
/// Optional concept the requirement is conceptually "about." Lets the
|
||||
/// requirement render a back-link to the term in TermDetail even when
|
||||
/// the term has no formalized block yet.
|
||||
linkedTermLabel: { type: "string" },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
export interface RequirementsResult {
|
||||
reqs: DetectedRequirement[];
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
provider: string;
|
||||
model: string;
|
||||
}
|
||||
|
||||
export async function analyzeRequirements(
|
||||
documentText: string,
|
||||
terms: Array<{ id: string; label: string; linkedBlockId: string | null }>
|
||||
): Promise<RequirementsResult> {
|
||||
const prompt = loadPrompt("socrates/analyze-requirements.md");
|
||||
const gateway = defaultGateway();
|
||||
const userPayload =
|
||||
`Document:\n\n${documentText}\n\n---\n\n` +
|
||||
`Available term labels:\n${JSON.stringify(terms.map(t => t.label))}`;
|
||||
|
||||
const messages: Message[] = [
|
||||
{ role: "system", content: prompt },
|
||||
{ role: "user", content: userPayload },
|
||||
];
|
||||
|
||||
const { value, result } = await chatJSON<{
|
||||
requirements?: Array<{
|
||||
tag?: string;
|
||||
text?: string;
|
||||
tracedToLabels?: string[];
|
||||
linkedTermLabel?: string;
|
||||
}>;
|
||||
}>(gateway, messages, {
|
||||
temperature: 0.2,
|
||||
maxTokens: 1500,
|
||||
jsonSchema: { name: "requirements", schema: reqsJsonSchema as Record<string, unknown> },
|
||||
jsonObjectMode: true,
|
||||
maxRepairs: 2,
|
||||
});
|
||||
|
||||
// Map term labels → block ids via the term-to-block links + label → termId.
|
||||
const labelToBlock = new Map<string, string>();
|
||||
const labelToTermId = new Map<string, string>();
|
||||
for (const t of terms) {
|
||||
if (t.linkedBlockId) labelToBlock.set(t.label.toLowerCase(), t.linkedBlockId);
|
||||
labelToTermId.set(t.label.toLowerCase(), t.id);
|
||||
}
|
||||
|
||||
const reqs: DetectedRequirement[] = [];
|
||||
for (const r of value?.requirements ?? []) {
|
||||
const tag = (r.tag ?? "").trim();
|
||||
const text = (r.text ?? "").trim();
|
||||
if (!tag || !text) continue;
|
||||
const blockIds = (r.tracedToLabels ?? [])
|
||||
.map(l => labelToBlock.get(l.toLowerCase()))
|
||||
.filter((id): id is string => Boolean(id));
|
||||
const linkedTermId = r.linkedTermLabel ? labelToTermId.get(r.linkedTermLabel.toLowerCase()) : undefined;
|
||||
reqs.push({
|
||||
tag,
|
||||
text,
|
||||
tracedToIds: blockIds,
|
||||
unsupported: blockIds.length === 0,
|
||||
linkedTermId: linkedTermId ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
reqs,
|
||||
inputTokens: result.inputTokens,
|
||||
outputTokens: result.outputTokens,
|
||||
provider: gateway.provider,
|
||||
model: gateway.model,
|
||||
};
|
||||
}
|
||||
206
apps/web/lib/llm/analyze/runAll.ts
Normal file
206
apps/web/lib/llm/analyze/runAll.ts
Normal file
@@ -0,0 +1,206 @@
|
||||
// Analyze orchestrator. Runs the requested sections sequentially. Sections
|
||||
// are independent: a partial failure in one doesn't abort the rest. Each
|
||||
// section writes an AnalysisRun row for telemetry / "last-run" timestamps.
|
||||
//
|
||||
// Order matters when "all" is selected:
|
||||
// 1. concepts (taxonomy + glossary in one pass — others depend on its output)
|
||||
// 2. model (uses the term list, can populate Block/Assoc/Constraint/Req
|
||||
// linkedTermIds)
|
||||
// 3. requirements (uses term-to-block links from model)
|
||||
// 4. assumptions, risks, inconsistencies (run on the refreshed model)
|
||||
//
|
||||
// Per-section runs skip the dependency steps and use whatever's already in
|
||||
// the DB. That keeps "I just want to refresh concepts" cheap.
|
||||
|
||||
import "server-only";
|
||||
import {
|
||||
loadDocument,
|
||||
loadProject,
|
||||
replaceModel,
|
||||
startAnalysisRun,
|
||||
finishAnalysisRun,
|
||||
mergeTaxonomySuggestion,
|
||||
applyGlossaryDefinitions,
|
||||
listTerms,
|
||||
mergeRequirementsSuggestion,
|
||||
mergeFindingsSuggestion,
|
||||
linkTermToBlock,
|
||||
type AnalysisSection,
|
||||
} from "../../db/repo";
|
||||
import { proseDocToPlainText } from "./proseText";
|
||||
import { analyzeConcepts } from "./concepts";
|
||||
import { analyzeRequirements } from "./requirements";
|
||||
import { analyzeModelFromProse } from "./model";
|
||||
import { detectFindings, type Finding } from "../detect";
|
||||
import { crossValidate } from "../../sysml/crossValidate";
|
||||
|
||||
export type RunSection = AnalysisSection;
|
||||
|
||||
export interface SectionOutcome {
|
||||
section: RunSection;
|
||||
status: "succeeded" | "failed" | "skipped";
|
||||
message?: string;
|
||||
inputTokens?: number;
|
||||
outputTokens?: number;
|
||||
}
|
||||
|
||||
export interface AnalyzeRunResult {
|
||||
outcomes: SectionOutcome[];
|
||||
modelVersion: number;
|
||||
}
|
||||
|
||||
const ALL: RunSection[] = [
|
||||
"concepts",
|
||||
"model",
|
||||
"requirements",
|
||||
"assumptions",
|
||||
"risks",
|
||||
"inconsistencies",
|
||||
];
|
||||
|
||||
export async function runAnalyze(projectId: string, requested: RunSection): Promise<AnalyzeRunResult> {
|
||||
const sections: RunSection[] = requested === "all" ? ALL : [requested];
|
||||
|
||||
const docRow = await loadDocument(projectId);
|
||||
const documentText = docRow ? proseDocToPlainText(docRow.doc) : "";
|
||||
if (!documentText.trim()) {
|
||||
return {
|
||||
outcomes: sections.map(s => ({ section: s, status: "skipped", message: "Empty document" })),
|
||||
modelVersion: (await loadProject(projectId)).version,
|
||||
};
|
||||
}
|
||||
|
||||
const outcomes: SectionOutcome[] = [];
|
||||
|
||||
for (const section of sections) {
|
||||
const { version: modelVersion } = await loadProject(projectId);
|
||||
const runId = await startAnalysisRun(projectId, section, modelVersion);
|
||||
try {
|
||||
const out = await runSection(projectId, section, documentText);
|
||||
await finishAnalysisRun(runId, {
|
||||
status: "succeeded",
|
||||
inputTokens: out.inputTokens,
|
||||
outputTokens: out.outputTokens,
|
||||
});
|
||||
outcomes.push({ section, status: "succeeded", inputTokens: out.inputTokens, outputTokens: out.outputTokens });
|
||||
} catch (err) {
|
||||
const message = (err as Error).message;
|
||||
console.error(`[analyze] ${section} failed:`, message);
|
||||
await finishAnalysisRun(runId, { status: "failed", errorMessage: message });
|
||||
outcomes.push({ section, status: "failed", message });
|
||||
}
|
||||
}
|
||||
|
||||
const finalVersion = (await loadProject(projectId)).version;
|
||||
return { outcomes, modelVersion: finalVersion };
|
||||
}
|
||||
|
||||
// ─── Per-section runners ─────────────────────────────────────────────────
|
||||
|
||||
interface RunOut {
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
}
|
||||
|
||||
async function runSection(projectId: string, section: RunSection, documentText: string): Promise<RunOut> {
|
||||
switch (section) {
|
||||
case "concepts":
|
||||
return runConcepts(projectId, documentText);
|
||||
case "model":
|
||||
return runModel(projectId, documentText);
|
||||
case "requirements":
|
||||
return runRequirements(projectId, documentText);
|
||||
case "assumptions":
|
||||
case "risks":
|
||||
case "inconsistencies":
|
||||
return runFindings(projectId);
|
||||
case "all":
|
||||
throw new Error("'all' must be expanded by the caller");
|
||||
}
|
||||
}
|
||||
|
||||
async function runConcepts(projectId: string, documentText: string): Promise<RunOut> {
|
||||
const { version } = await loadProject(projectId);
|
||||
const { terms, definitions, inputTokens, outputTokens } = await analyzeConcepts(documentText);
|
||||
// Merge instead of replace — analyzer suggestions surface as a *review*.
|
||||
// The user keeps or discards each pending change; accepted state survives.
|
||||
await mergeTaxonomySuggestion(projectId, terms, version);
|
||||
if (definitions.length > 0) {
|
||||
// Definitions still apply only to "accepted" terms whose definition is
|
||||
// empty; mergeTaxonomySuggestion's gentle-update path keeps user-edited
|
||||
// definitions, but the dedicated patch is harmless for new terms.
|
||||
await applyGlossaryDefinitions(projectId, definitions);
|
||||
}
|
||||
return { inputTokens, outputTokens };
|
||||
}
|
||||
|
||||
async function runModel(projectId: string, documentText: string): Promise<RunOut> {
|
||||
const { model: current } = await loadProject(projectId);
|
||||
const terms = await listTerms(projectId);
|
||||
const { model, termIdToBlockId, inputTokens, outputTokens } = await analyzeModelFromProse(
|
||||
documentText,
|
||||
terms.map(t => ({ id: t.id, label: t.label, linkedBlockId: t.linkedBlockId })),
|
||||
current
|
||||
);
|
||||
|
||||
await replaceModel(projectId, model, "Analyze: model refresh");
|
||||
|
||||
// Update term → block linkage based on what the analyzer linked.
|
||||
for (const [termId, blockId] of termIdToBlockId.entries()) {
|
||||
await linkTermToBlock(termId, blockId);
|
||||
}
|
||||
|
||||
return { inputTokens, outputTokens };
|
||||
}
|
||||
|
||||
async function runRequirements(projectId: string, documentText: string): Promise<RunOut> {
|
||||
const { version } = await loadProject(projectId);
|
||||
const terms = await listTerms(projectId);
|
||||
const { reqs, inputTokens, outputTokens } = await analyzeRequirements(
|
||||
documentText,
|
||||
terms.map(t => ({ id: t.id, label: t.label, linkedBlockId: t.linkedBlockId }))
|
||||
);
|
||||
await mergeRequirementsSuggestion(projectId, reqs, version);
|
||||
return { inputTokens, outputTokens };
|
||||
}
|
||||
|
||||
async function runFindings(projectId: string): Promise<RunOut> {
|
||||
const { model, version } = await loadProject(projectId);
|
||||
const result = await detectFindings(model);
|
||||
|
||||
// Cross-layer validation (T2): glue rules between terms and ontology.
|
||||
// Surfaced as inconsistency-kind findings keyed by validationCode = X*.
|
||||
const terms = await listTerms(projectId);
|
||||
const cross = crossValidate({
|
||||
model,
|
||||
terms: terms.map(t => ({ id: t.id, label: t.label, linkedBlockId: t.linkedBlockId })),
|
||||
// X4 needs prose-occurrence counts; left undefined here so it stays
|
||||
// off until we wire prose extraction. X1–X3 still fire.
|
||||
});
|
||||
const crossFindings: Finding[] = cross.map(issue => ({
|
||||
kind: "inconsistency",
|
||||
text: issue.message,
|
||||
linkedElementIds: anchorIds(issue.anchor),
|
||||
confidence: 1.0,
|
||||
severity: issue.severity === "warning" ? "medium" : "low",
|
||||
validationCode: issue.code,
|
||||
}));
|
||||
|
||||
await mergeFindingsSuggestion(
|
||||
projectId,
|
||||
[...result.findings, ...crossFindings],
|
||||
version,
|
||||
result.provider,
|
||||
result.model
|
||||
);
|
||||
return { inputTokens: result.inputTokens, outputTokens: result.outputTokens };
|
||||
}
|
||||
|
||||
function anchorIds(a: import("../../sysml/validate").IssueAnchor): string[] {
|
||||
if (a.kind === "model") return [];
|
||||
if (a.kind === "property") return [a.blockId];
|
||||
// T3: prefix term anchors with `term:` so consumers (FindingsPane,
|
||||
// TermDetail) can route them to the concept popover instead of the model.
|
||||
if (a.kind === "term") return [`term:${a.id}`];
|
||||
return [a.id];
|
||||
}
|
||||
81
apps/web/lib/llm/analyze/taxonomy.ts
Normal file
81
apps/web/lib/llm/analyze/taxonomy.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
// Analyze pass: extract taxonomy terms from prose.
|
||||
|
||||
import "server-only";
|
||||
import { defaultGateway, chatJSON, type Message } from "../gateway";
|
||||
import { loadPrompt } from "../prompts";
|
||||
import type { DetectedTerm } from "../../db/repo";
|
||||
|
||||
interface RawTerm {
|
||||
label?: string;
|
||||
parentLabel?: string | null;
|
||||
synonyms?: string[];
|
||||
}
|
||||
|
||||
const taxonomyJsonSchema = {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["terms"],
|
||||
properties: {
|
||||
terms: {
|
||||
type: "array",
|
||||
items: {
|
||||
type: "object",
|
||||
additionalProperties: false,
|
||||
required: ["label"],
|
||||
properties: {
|
||||
label: { type: "string", minLength: 1 },
|
||||
parentLabel: { type: ["string", "null"] },
|
||||
synonyms: { type: "array", items: { type: "string" } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
export interface TaxonomyResult {
|
||||
terms: DetectedTerm[];
|
||||
inputTokens: number;
|
||||
outputTokens: number;
|
||||
provider: string;
|
||||
model: string;
|
||||
}
|
||||
|
||||
export async function analyzeTaxonomy(documentText: string): Promise<TaxonomyResult> {
|
||||
const prompt = loadPrompt("socrates/analyze-taxonomy.md");
|
||||
const gateway = defaultGateway();
|
||||
const messages: Message[] = [
|
||||
{ role: "system", content: prompt },
|
||||
{ role: "user", content: `Document:\n\n${documentText}` },
|
||||
];
|
||||
|
||||
const { value, result } = await chatJSON<{ terms?: RawTerm[] }>(gateway, messages, {
|
||||
temperature: 0.2,
|
||||
maxTokens: 1500,
|
||||
jsonSchema: { name: "taxonomy", schema: taxonomyJsonSchema as Record<string, unknown> },
|
||||
jsonObjectMode: true,
|
||||
maxRepairs: 2,
|
||||
});
|
||||
|
||||
const seen = new Set<string>();
|
||||
const terms: DetectedTerm[] = [];
|
||||
for (const t of value?.terms ?? []) {
|
||||
const label = (t.label ?? "").trim();
|
||||
if (!label) continue;
|
||||
const key = label.toLowerCase();
|
||||
if (seen.has(key)) continue;
|
||||
seen.add(key);
|
||||
terms.push({
|
||||
label,
|
||||
parentLabel: t.parentLabel?.trim() || null,
|
||||
synonyms: Array.isArray(t.synonyms) ? t.synonyms.filter(s => typeof s === "string") : [],
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
terms,
|
||||
inputTokens: result.inputTokens,
|
||||
outputTokens: result.outputTokens,
|
||||
provider: gateway.provider,
|
||||
model: gateway.model,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user