Files
STCourses/packages/feature-ai/scripts/dev.ts
T
Lucas WintherandClaude Opus 5.5 d34a5c1f0c feat(ai): citations column on ai_messages (contract request 5)
New packages/feature-ai/migrations/0001_ai_message_citations.sql adds
ai_messages.citations_json and folds v1 'sources' rows into the assistant
row before them. The feature writes and reads the column directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 20:28:23 +01:00

50 lines
2.7 KiB
TypeScript

// Local dev server for the AI package until Phase 2 wires feature factories into apps/server.
// STC_PORT=3464 STC_DATA_DIR=/tmp/stc-ai-data STC_COURSE_DIRS=./fixtures/courses:./overlays bun packages/feature-ai/scripts/dev.ts
// STC_AI_DEV_USER=owner|learner|none picks the signed-in user (dev only). STC_AI_DEV_FAKE=1 uses a canned provider.
import type { AiProvider, AuthService, ServerContext } from "@stc/contracts";
import { createApp, createJobQueue, loadConfig } from "@stc/core";
import { createCourseRepository } from "@stc/course-format";
import { connect, migrate } from "@stc/db";
import indexHtml from "../../../apps/web/index.html";
import { aiFeature } from "../src/feature";
import { migrationsDirs } from "../src/migrations";
const config = loadConfig();
const who = process.env.STC_AI_DEV_USER ?? "owner";
const auth: AuthService = {
userFromRequest: async () =>
who === "none" ? null : { id: `dev-${who}`, email: `${who}@localhost`, name: who, roles: who === "owner" ? ["owner"] : ["learner"], groups: [] },
};
const fake: AiProvider = {
id: "fake",
async *chat(messages) {
const sys = messages[0]?.content ?? "";
if (sys.includes("=== FILE:")) {
yield { text: "=== FILE: questions/adding-many-numbers.md ===\n# Check your understanding: adding many numbers\n\n1. Predict: what does `sum([])` return?\n2. What would break if `sum` used `a - b`?\n=== END FILE ===\n" };
return;
}
const mentor = sys.includes("## Course mentor rules");
const answer = mentor
? "Before I explain: what does the failing test give as input, and what did you expect `add` to return?\n\nThe lesson's nudge asks **which operator combines two numbers**. Try it, run the tests, and tell me what happens. [lessons/04-exercise.md · Adding numbers]"
: "Here is the whole answer.\n\n```js\nexport const add = (a, b) => a + b\n```\n\nIt returns the sum of its arguments. [lessons/04-exercise.md · Adding numbers]";
for (const part of answer.match(/.{1,12}/gs) ?? []) { await Bun.sleep(5); yield { text: part }; }
},
};
const db = await connect(config.databaseUrl);
await migrate(db, { "feature-ai": migrationsDirs });
const courses = await createCourseRepository(config.courseDirs);
const ctx = { config, db, courses, auth } as ServerContext;
const feature = aiFeature(ctx, process.env.STC_AI_DEV_FAKE ? { providerFactory: () => fake } : {});
const jobs = createJobQueue(db, feature.jobs ?? {});
ctx.jobs = jobs;
const app = createApp(ctx, [feature]);
jobs.start();
const server = Bun.serve({
port: config.port,
routes: { ...app.routes, "/api/*": (req) => app.fetch(req), "/*": indexHtml },
fetch: (req) => app.fetch(req),
});
console.log(`STCourses (AI dev) on ${server.url} as ${who}`);