// 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"; 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 ?? ""; 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); 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}`);