Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export async function main(): Promise<void> {

const app = createApp({
mathEngine,
rag,
workflow: {
rag,
mathEngine,
Expand Down
4 changes: 4 additions & 0 deletions packages/agent/src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { Hono } from "hono";
import { cors } from "hono/cors";

import type { MathEngineClient } from "../tools/math-engine-client.js";
import type { RagClient } from "../tools/rag-client.js";
import type { RunOptions, VerificationWorkflowDeps } from "../workflows/verification-workflow.js";
import { createGenerateRoute } from "./routes/generate.js";
import { createHealthRoute } from "./routes/health.js";
import { createSourceProblemsRoute } from "./routes/source-problems.js";

export interface AppDeps {
mathEngine: MathEngineClient;
rag: RagClient;
workflow: VerificationWorkflowDeps;
workflowOptions?: RunOptions;
}
Expand All @@ -27,6 +30,7 @@ export function createApp(deps: AppDeps): Hono {
);

app.route("/", createHealthRoute(deps.mathEngine));
app.route("/", createSourceProblemsRoute(deps.rag));
app.route("/", createGenerateRoute(deps.workflow, deps.workflowOptions));

return app;
Expand Down
81 changes: 81 additions & 0 deletions packages/agent/src/server/routes/source-problems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* GET /api/source-problems — read-only corpus lookup.
*
* Reuses the in-memory RagClient (D-7) without LLM / generation. The web S3
* problem picker calls this to browse SourceProblem rows by school/grade/topic.
*
* Figure-dependent rows are dropped: the corpus image path is a non-servable
* AI-Hub scan, so a statement that points at a figure/graph/table is unusable
* as text-only. See isFigureDependent for why there is no clean corpus flag.
*/

import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";

import {
DifficultySchema,
SchoolLevelSchema,
} from "../../schemas/source-problem.schema.js";
import type { RagClient } from "../../tools/rag-client.js";

// Big k = scan whole topic (≥ largest ~1.8k rows). search() already sorts the full
// match set so this only widens the final slice; lets figure filtering fill `limit`
// on figure-heavy topics instead of returning an all-figure first-50.
const FIGURE_FILTER_POOL = 2000;

// No reliable corpus flag for "needs a figure": media.image is on 100% of rows, and
// the VISUAL taxonomy tag also marks self-contained solids ("직육면체의 겉넓이"). So match
// only explicit references — "그림", a shown graph/도형/표 via deictic phrases, or embedded
// <table>. Bare "그래프"/"도형"/"표"/"좌표" stay (e.g. "y=x²의 그래프의 꼭짓점" is self-contained).
const FIGURE_REFERENCE =
/그림|그래프와\s*같|(?:다음|아래|위)\s*그래프|(?:다음|아래)\s*도형|(?:다음|아래|위)의?\s*표(?:는|를|에|\s)|<table/i;

export function isFigureDependent(questionText: string): boolean {
return FIGURE_REFERENCE.test(questionText);
}

const GradeQueryParamSchema = z.preprocess(
(value) => {
if (value === "common" || value === "null" || value === "") return null;
if (value === "1" || value === 1) return 1;
if (value === "2" || value === 2) return 2;
if (value === "3" || value === 3) return 3;
return value;
},
z.union([z.literal(1), z.literal(2), z.literal(3)]).nullable(),
);

const SourceProblemsQuerySchema = z.object({
school_level: SchoolLevelSchema,
grade: GradeQueryParamSchema,
topic_code: z.string().min(1).optional(),
difficulty: DifficultySchema.optional(),
limit: z.coerce.number().int().min(1).max(50).default(30),
});

export function createSourceProblemsRoute(rag: RagClient): Hono {
const app = new Hono();

app.get(
"/api/source-problems",
zValidator("query", SourceProblemsQuerySchema),
async (c) => {
const q = c.req.valid("query");
const results = await rag.search({
school_level: q.school_level,
grade: q.grade ?? null,
topic_code: q.topic_code,
difficulty: q.difficulty,
k: FIGURE_FILTER_POOL,
});
const problems = results
.map((r) => r.problem)
.filter((p) => !isFigureDependent(p.question_text))
.slice(0, q.limit);
return c.json(problems);
},
);

return app;
}
185 changes: 185 additions & 0 deletions packages/agent/tests/source-problems-route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { Hono } from "hono";
import { describe, expect, it } from "vitest";

import type {
RagQuery,
RagResult,
SourceProblem,
} from "../src/schemas/index.js";
import {
createSourceProblemsRoute,
isFigureDependent,
} from "../src/server/routes/source-problems.js";
import type { RagClient } from "../src/tools/rag-client.js";

function makeProblem(overrides: {
item_id: string;
difficulty_norm: SourceProblem["difficulty_norm"];
topic_code?: SourceProblem["topic_code"];
school_level?: SourceProblem["school_level"];
grade?: SourceProblem["grade"];
question_text?: string;
}): SourceProblem {
return {
item_id: overrides.item_id,
source_dataset: "111",
split: "train",
source_label_type: "problem_label",
school_level: overrides.school_level ?? "middle",
grade: overrides.grade ?? 3,
semester: null,
topic_code: overrides.topic_code ?? "9수02-09",
topic_name: "이차방정식",
achievement_standard: "이차방정식을 풀 수 있다.",
question_text: overrides.question_text ?? `질문 ${overrides.item_id}`,
answer_text: `정답 ${overrides.item_id}`,
explanation_text: null,
choice_blocks: null,
problem_type_norm: "short_answer",
difficulty_norm: overrides.difficulty_norm,
question_image_relpath: null,
answer_image_relpath: null,
question_json_relpath: null,
answer_json_relpath: null,
};
}

function createMockRag(problems: SourceProblem[]): RagClient {
return {
async search(query: RagQuery): Promise<RagResult[]> {
const filtered = problems.filter((problem) => {
if (problem.school_level !== query.school_level) return false;
if (query.grade !== null && problem.grade !== query.grade) return false;
if (query.topic_code && problem.topic_code !== query.topic_code) {
return false;
}
if (query.difficulty && problem.difficulty_norm !== query.difficulty) {
return false;
}
return true;
});

return filtered.slice(0, query.k ?? 8).map((problem) => ({
item_id: problem.item_id,
similarity: 0.5,
problem,
match_reason: "structural",
}));
},
};
}

describe("GET /api/source-problems", () => {
const fixtures: SourceProblem[] = [
makeProblem({ item_id: "mock-1", difficulty_norm: "easy" }),
makeProblem({ item_id: "mock-2", difficulty_norm: "medium" }),
makeProblem({ item_id: "mock-3", difficulty_norm: "hard" }),
makeProblem({ item_id: "mock-4", difficulty_norm: "hard" }),
makeProblem({
item_id: "mock-other-topic",
difficulty_norm: "medium",
topic_code: "9수02-08",
}),
makeProblem({
item_id: "mock-other-grade",
difficulty_norm: "easy",
grade: 2,
}),
makeProblem({
item_id: "mock-figure",
difficulty_norm: "medium",
question_text: "다음 그림과 같이 정육면체를 잘라 낸 입체도형에서 면의 개수를 구하여라.",
}),
];

function buildApp(): Hono {
const app = new Hono();
app.route("/", createSourceProblemsRoute(createMockRag(fixtures)));
return app;
}

it("returns source problems filtered by school_level + grade + topic_code", async () => {
const app = buildApp();
const topic = encodeURIComponent("9수02-09");
const res = await app.request(
`/api/source-problems?school_level=middle&grade=3&topic_code=${topic}`,
);

expect(res.status).toBe(200);
const body = (await res.json()) as SourceProblem[];
expect(Array.isArray(body)).toBe(true);
expect(body).toHaveLength(4);
for (const item of body) {
expect(item).toHaveProperty("item_id");
expect(item).toHaveProperty("question_text");
expect(item).toHaveProperty("answer_text");
expect(item).toHaveProperty("difficulty_norm");
expect(item.topic_code).toBe("9수02-09");
expect(item.school_level).toBe("middle");
expect(item.grade).toBe(3);
}
});

it("returns 400 when school_level is missing", async () => {
const app = buildApp();
const res = await app.request("/api/source-problems?grade=3");
expect(res.status).toBe(400);
});

it("narrows results when difficulty=hard", async () => {
const app = buildApp();
const topic = encodeURIComponent("9수02-09");
const res = await app.request(
`/api/source-problems?school_level=middle&grade=3&topic_code=${topic}&difficulty=hard`,
);

expect(res.status).toBe(200);
const body = (await res.json()) as SourceProblem[];
expect(body).toHaveLength(2);
expect(body.every((problem) => problem.difficulty_norm === "hard")).toBe(true);
});

it("returns [] for an unknown topic_code", async () => {
const app = buildApp();
const topic = encodeURIComponent("9수99-99");
const res = await app.request(
`/api/source-problems?school_level=middle&grade=3&topic_code=${topic}`,
);

expect(res.status).toBe(200);
const body = (await res.json()) as SourceProblem[];
expect(body).toEqual([]);
});

it("excludes figure-dependent problems (statement references 그림)", async () => {
const app = buildApp();
const topic = encodeURIComponent("9수02-09");
const res = await app.request(
`/api/source-problems?school_level=middle&grade=3&topic_code=${topic}`,
);

expect(res.status).toBe(200);
const body = (await res.json()) as SourceProblem[];
expect(body.some((p) => p.item_id === "mock-figure")).toBe(false);
expect(body.every((p) => !p.question_text.includes("그림"))).toBe(true);
});
});

describe("isFigureDependent", () => {
it.each([
["다음 그림과 같이 정육면체를 잘라 낸 입체도형에서", true],
["다음 그래프는 x초 후 높이 y를 나타낸 것이다.", true],
["아래 그래프는 시간과 거리의 관계를 나타낸 것이다.", true],
["걸린 시간과 이동 거리를 그래프와 같이 나타내면", true],
["다음 표는 어느 반의 도수분포표이다.", true],
["다음 도형의 둘레의 길이를 구하여라.", true],
["성적을 정리하면 <table border> 와 같다.", true],
["모든 모서리의 합이 48이고 대각선이 3√6인 직육면체의 겉넓이를 구하시오.", false],
["이차함수 y=2x^2+3x-2의 그래프와 x축의 교점의 x좌표를 구하시오.", false],
["수직선 위에서 -7/5보다 큰 가장 작은 정수를 구하시오.", false],
["a^3+b^3을 보기와 같이 변형하여 값을 구하시오.", false],
["이차방정식 (2x+1)(x-4)=0을 풀어라.", false],
])("%s -> %s", (text, expected) => {
expect(isFigureDependent(text)).toBe(expected);
});
});
Loading
Loading