Files
Socrates/apps/web/lib/llm/socrates.ts
dtoro 78faca9968 MVP M6: live Socrates dock — LLM gateway + persisted thread + reply loop
The dock now talks to a real LLM. On first load it asks Socrates for an
opening turn that's grounded in the project's actual SysML model + active
validation issues. User replies and option-picks send turns through the
same channel. Thread + every message persist in SQLite so refresh keeps
the conversation.

apps/web/lib/llm
- gateway.ts: LLMGateway interface (chat + ChatOptions). Two adapters:
    - lmstudio: OpenAI SDK against LMSTUDIO_BASE_URL (default
      http://localhost:1234/v1)
    - anthropic: @anthropic-ai/sdk against claude-sonnet-4-6 (set
      ANTHROPIC_API_KEY when LLM_PROVIDER=anthropic)
  Provider chosen via LLM_PROVIDER env (default: lmstudio).
- chatJSON(): JSON-mode helper with parse-error repair-retry — the same
  defensive pattern proven against gemma-4-e4b in Phase 0.
- prompts.ts: server-only loader that caches .md prompts.
- prompts/socrates/character.md + review.md: ported verbatim from
  phase-0/src/prompts/ (Phase 0 corpus validated these 10/10).
- socrates.ts: sendUserTurn() — builds the system prompt (character +
  review + project context with trimmed model + active issues), runs
  chatJSON against the gateway, persists user + assistant turns,
  returns the structured turn. SocratesTurn schema is { text, options? }
  with up to 3 numbered options matching the prototype.

apps/web/prisma
- SocratesThread + SocratesMessage tables. Auto-create one open thread
  per project on first load.

apps/web/app/api/projects/[projectId]/socrates
- GET: returns active thread + parsed messages.
- POST: body { text }. Empty text triggers an opening turn. Persists user
  + assistant turns, returns assistant turn + provider metadata.

apps/web/components/socrates/SocratesDock.tsx
- Replaces the static thread prop with a projectId. Loads from API on
  mount, auto-triggers an opening turn if the thread is empty, sends
  user replies via POST. Numbered options click-to-pick or 1–3 keyboard
  shortcut (skipped when focus is in an input). Status line shows the
  active provider + model. Optimistic-local: user message appears
  instantly, "thinking…" placeholder shows while the LLM works, errors
  surface inline.

apps/web/.env.example + .env.local
- LLM_PROVIDER, LMSTUDIO_BASE_URL/MODEL/API_KEY, ANTHROPIC_API_KEY/MODEL.
- .env.local committed only with the local default (no real secrets);
  user supplies their own per-machine.

What's not yet here (next iterations):
- Streaming responses (currently waits for full response, ~5-15s)
- Multi-thread switcher (one auto-thread per project)
- Socrates-proposes-ops flow (M7)
2026-04-29 07:50:34 +02:00

232 lines
6.6 KiB
TypeScript

// Socrates conversation runner — server-side.
//
// Builds the LLM prompt (character + review + project context), wraps the
// thread history, asks for a structured JSON turn (`text` + optional
// `options`), persists user + assistant messages, and returns the new turn.
import "server-only";
import { defaultGateway, chatJSON, type Message } from "./gateway";
import { loadPrompt } from "./prompts";
import { prisma } from "../db/client";
import type { SysMLModel } from "../sysml/model";
import type { ValidationIssue } from "../sysml/validate";
// ─── Output schema for one Socrates turn ────────────────────────────────
export interface SocratesOption {
n: number;
label: string;
sub?: string;
}
export interface SocratesTurn {
text: string;
options?: SocratesOption[];
}
const turnJsonSchema = {
type: "object",
additionalProperties: false,
required: ["text"],
properties: {
text: { type: "string", minLength: 1 },
options: {
type: "array",
maxItems: 3,
items: {
type: "object",
additionalProperties: false,
required: ["n", "label"],
properties: {
n: { type: "integer", minimum: 1, maximum: 3 },
label: { type: "string", minLength: 1, maxLength: 60 },
sub: { type: "string", maxLength: 80 },
},
},
},
},
} as const;
// ─── Public: handle one user turn → Socrates reply, persist both ────────
export interface SendUserTurnArgs {
threadId: string;
/** Caller passes the latest model so we don't re-load it inside this fn. */
model: SysMLModel;
issues?: ValidationIssue[];
/** Body of the user's message. Empty string means "open the conversation". */
userText: string;
}
export interface SendUserTurnResult {
user: { id: string; createdAt: Date };
assistant: {
id: string;
createdAt: Date;
turn: SocratesTurn;
provider: string;
model: string;
inputTokens: number;
outputTokens: number;
};
}
export async function sendUserTurn(args: SendUserTurnArgs): Promise<SendUserTurnResult> {
const thread = await prisma.socratesThread.findUnique({
where: { id: args.threadId },
include: {
messages: { orderBy: { createdAt: "asc" } },
},
});
if (!thread) throw new Error(`thread ${args.threadId} not found`);
// 1. Persist the user turn first so concurrent reads see it.
let userRecord: { id: string; createdAt: Date } | undefined;
if (args.userText.trim().length > 0) {
userRecord = await prisma.socratesMessage.create({
data: {
threadId: thread.id,
role: "user",
content: JSON.stringify({ text: args.userText }),
},
select: { id: true, createdAt: true },
});
}
// 2. Build the LLM context.
const character = loadPrompt("socrates/character.md");
const review = loadPrompt("socrates/review.md");
const projectContext = JSON.stringify(
{
model: trimModel(args.model),
issues: (args.issues ?? []).slice(0, 30).map(i => ({
code: i.code,
severity: i.severity,
message: i.message,
anchor: i.anchor,
})),
},
null,
2
);
const systemPrompt = [
character,
"---",
review,
"---",
"Current project context (model + active validation issues):",
"```json",
projectContext,
"```",
].join("\n\n");
const historyMessages: Message[] = thread.messages.map(m => {
const parsed = JSON.parse(m.content) as { text: string };
return {
role: m.role === "user" ? "user" : "assistant",
content: parsed.text,
};
});
if (args.userText.trim().length > 0) {
historyMessages.push({ role: "user", content: args.userText });
} else if (historyMessages.length === 0) {
// Opening turn — give Socrates a kick.
historyMessages.push({
role: "user",
content: "Open the conversation. Surface the most important tension you see in this model.",
});
}
// 3. Ask the model.
const gateway = defaultGateway();
const { value, result } = await chatJSON<SocratesTurn>(
gateway,
[{ role: "system", content: systemPrompt }, ...historyMessages],
{
temperature: 0.4,
maxTokens: 768,
jsonSchema: { name: "socrates_turn", schema: turnJsonSchema as Record<string, unknown> },
jsonObjectMode: true, // safety net for adapters lacking strict json_schema
maxRepairs: 2,
}
);
// Defensive normalization
const turn: SocratesTurn = {
text: typeof value?.text === "string" ? value.text : "[empty]",
...(Array.isArray(value?.options) && value.options.length > 0
? { options: value.options.slice(0, 3) }
: {}),
};
// 4. Persist the assistant turn.
const assistantRecord = await prisma.socratesMessage.create({
data: {
threadId: thread.id,
role: "assistant",
content: JSON.stringify(turn),
provider: gateway.provider,
model: gateway.model,
inputTokens: result.inputTokens,
outputTokens: result.outputTokens,
},
select: { id: true, createdAt: true },
});
await prisma.socratesThread.update({
where: { id: thread.id },
data: { updatedAt: new Date() },
});
return {
user: userRecord ?? { id: "(skipped)", createdAt: new Date(0) },
assistant: {
id: assistantRecord.id,
createdAt: assistantRecord.createdAt,
turn,
provider: gateway.provider,
model: gateway.model,
inputTokens: result.inputTokens,
outputTokens: result.outputTokens,
},
};
}
// ─── Helpers ─────────────────────────────────────────────────────────────
function trimModel(model: SysMLModel): unknown {
// Compact view for Socrates — drop ids of properties (just names) and
// multiplicity / type detail to save tokens.
return {
blocks: model.blocks.map(b => ({
id: b.id,
label: b.label,
kind: b.kind,
properties: b.properties.map(p => p.name),
})),
associations: model.associations.map(a => ({
id: a.id,
from: a.fromBlockId,
to: a.toBlockId,
label: a.label,
kind: a.kind,
})),
constraints: model.constraints.map(c => ({
id: c.id,
label: c.label,
appliesTo: c.appliesTo,
})),
requirements: model.requirements.map(r => ({
id: r.id,
tag: r.tag,
text: r.text,
satisfiedBy: r.relations
.filter(rel => rel.kind === "satisfy")
.map(rel => (rel as { kind: "satisfy"; blockId: string }).blockId),
})),
};
}