summarize_precedent
Generate a summary of a Korean legal precedent using its serial number. Choose the maximum length for the summary.
Instructions
[판례] 판례 요약 생성.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | 판례일련번호 | |
| maxLength | Yes | 요약 최대 길이 (기본값: 500자) | |
| apiKey | No | 법제처 Open API 인증키(OC). 사용자가 제공한 경우 전달 |
Implementation Reference
- src/tools/precedent-summary.ts:23-55 (handler)The main handler function 'summarizePrecedent' that takes an apiClient and input, fetches precedent text via getPrecedentText, extracts a summary using extractPrecedentSummary, and returns the result.
export async function summarizePrecedent( apiClient: LawApiClient, input: SummarizePrecedentInput ): Promise<{ content: Array<{ type: string, text: string }>, isError?: boolean }> { try { // 1. 판례 전문 조회 const precedentResult = await getPrecedentText(apiClient, { id: input.id, apiKey: input.apiKey }) if (precedentResult.isError || precedentResult.content.length === 0) { return { content: [{ type: "text", text: "판례를 찾을 수 없습니다." }], isError: true } } const fullText = precedentResult.content[0].text // 2. 핵심 정보 추출 const summary = extractPrecedentSummary(fullText, input.maxLength) return { content: [{ type: "text", text: truncateResponse(summary) }] } } catch (error) { return formatToolError(error, "summarize_precedent") } } - src/tools/precedent-summary.ts:11-15 (schema)Zod schema 'SummarizePrecedentSchema' defining input: id (string), maxLength (optional number, default 500), and apiKey (optional string).
export const SummarizePrecedentSchema = z.object({ id: z.string().describe("판례일련번호"), maxLength: z.number().optional().default(500).describe("요약 최대 길이 (기본값: 500자)"), apiKey: z.string().optional().describe("법제처 Open API 인증키(OC). 사용자가 제공한 경우 전달") }) - src/tool-registry.ts:292-297 (registration)Registration of the 'summarize_precedent' tool in the tool registry with its name, description, schema, and handler.
{ name: "summarize_precedent", description: "[판례] 판례 요약 생성.", schema: SummarizePrecedentSchema, handler: summarizePrecedent }, - Helper function 'extractPrecedentSummary' that parses the full precedent text, extracting key sections (caseNumber, court, judgment, summary, mainText) and assembling a summarized output.
function extractPrecedentSummary(fullText: string, maxLength: number): string { const lines = fullText.split('\n') // 판시사항, 판결요지, 주문 등 핵심 섹션 추출 const sections = { title: "", court: "", caseNumber: "", judgment: "", summary: "", mainText: "" } let currentSection = "" for (const line of lines) { const trimmed = line.trim() // 섹션 구분 if (trimmed.includes("사건번호:") || trimmed.startsWith("사건:")) { sections.caseNumber = trimmed } else if (trimmed.includes("법원:") || trimmed.includes("선고:")) { sections.court = trimmed } else if (trimmed === "【판시사항】" || trimmed.startsWith("판시사항")) { currentSection = "judgment" } else if (trimmed === "【판결요지】" || trimmed.startsWith("판결요지")) { currentSection = "summary" } else if (trimmed === "【주문】" || trimmed.startsWith("주문")) { currentSection = "mainText" } else if (currentSection === "judgment" && trimmed.length > 0) { sections.judgment += trimmed + "\n" } else if (currentSection === "summary" && trimmed.length > 0) { sections.summary += trimmed + "\n" } else if (currentSection === "mainText" && trimmed.length > 0) { sections.mainText += trimmed + "\n" } } // 요약 생성 let result = "📋 판례 요약\n\n" if (sections.caseNumber) { result += `${sections.caseNumber}\n` } if (sections.court) { result += `${sections.court}\n\n` } if (sections.judgment) { result += "【판시사항】\n" result += truncateText(sections.judgment, maxLength / 3) + "\n\n" } if (sections.summary) { result += "【판결요지】\n" result += truncateText(sections.summary, maxLength / 3) + "\n\n" } if (sections.mainText) { result += "【주문】\n" result += truncateText(sections.mainText, maxLength / 3) + "\n" } return result } - Helper function 'truncateText' that truncates text to a maximum length with ellipsis.
function truncateText(text: string, maxLength: number): string { const trimmed = text.trim() if (trimmed.length <= maxLength) { return trimmed } return trimmed.substring(0, maxLength) + "..." }