src/index.js
// Study Assistant — AgentRove native agent (MIT). Offline flashcards, SM-2 scheduling and seeded quizzes.
import { defineAgent, AgentError, runCli, runMcpStdio, handleMcpMessage, validateManifest } from "../sdk/index.js";
import { readFileSync } from "node:fs";
import { extractCards, sm2, makeQuiz } from "./cards.js";
const manifest = JSON.parse(readFileSync(new URL("../agent.json", import.meta.url), "utf8"));
function nowMs(input, ctx) {
const ms = input.now ? Date.parse(input.now) : (() => { const n = ctx.now(); return n instanceof Date ? n.getTime() : Date.parse(String(n)); })();
if (Number.isNaN(ms)) throw new AgentError("invalid_input", "now is not a valid date-time");
return ms;
}
const handlers = {
async make_flashcards(input, ctx) {
const nowIso = new Date(nowMs(input, ctx)).toISOString();
return extractCards(input.text, { tags: input.tags ?? [], deck: input.deck, includeHeadings: input.include_headings !== false, nowIso });
},
async review(input, ctx) {
const now = nowMs(input, ctx);
const r = sm2(input.card, input.quality, now);
return {
card: { ...input.card, repetitions: r.repetitions, interval_days: r.interval_days, ease: r.ease, lapses: r.lapses, due: r.due, last_reviewed: r.last_reviewed },
previous: r.previous,
quality: input.quality,
lapse: r.lapse,
// SM-2 step 7: items graded below 4 are repeated again in the same session.
repeat_today: input.quality < 4,
algorithm: "SM-2",
};
},
async quiz(input, ctx) {
const seed = input.seed ?? 1;
const ids = new Set();
for (const c of input.cards) {
if (ids.has(c.id)) throw new AgentError("invalid_input", `Duplicate card id: ${c.id}`);
ids.add(c.id);
}
const out = makeQuiz(input.cards, {
count: input.count ?? 10,
choices: input.choices ?? 4,
seed,
dueOnly: input.due_only === true,
nowMs: input.due_only ? nowMs(input, ctx) : 0,
});
return { seed, ...out };
},
};
const agent = defineAgent(manifest, handlers);
export default agent;
export { manifest, runCli, runMcpStdio, handleMcpMessage, validateManifest };