create_hwpx_document
Creates a new .hwpx document from a JSON list of text items and tables (rendered as flat rows).
Instructions
Create a new .hwpx file from a JSON content list of {type:'text',text} items. Tables (type:'table',headers,rows) are rendered as flat text rows in v0.2. Args: output_path (must end with .hwpx), content (JSON string of items).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| output_path | Yes | ||
| content | Yes |
Implementation Reference
- src/tools/write.ts:25-28 (schema)Input schema (CreateHwpxArgs interface) for create_hwpx_document tool: requires output_path (string) and content (string - JSON array of content items).
export interface CreateHwpxArgs { output_path: string; content: string; } - src/tools/write.ts:30-32 (schema)ContentItem type definition: items can be of type 'text' (with text string) or 'table' (with headers string[] and rows string[][]).
type ContentItem = | { type: "text"; text: string } | { type: "table"; headers: string[]; rows: string[][] }; - src/tools/write.ts:140-191 (handler)Main handler function createHwpxDocument. Validates output_path ends with .hwpx, parses content JSON into ContentItem[], creates an empty HWPX document via @rhwp/core's HwpDocument, inserts text and table items (tables rendered as pipe-separated text for v0.2), exports to HWPX bytes, and writes to output_path.
export async function createHwpxDocument(args: CreateHwpxArgs): Promise<string> { if (!args.output_path.toLowerCase().endsWith(".hwpx")) { return `출력 경로는 .hwpx 확장자여야 합니다 (output must end with .hwpx): ${args.output_path}`; } let items: ContentItem[]; try { items = JSON.parse(args.content); if (!Array.isArray(items)) throw new Error("content must be a JSON array"); } catch (e) { return `content JSON 파싱 오류 (JSON parse error): ${(e as Error).message}`; } // Strategy: // 1) Build a doc via @rhwp/core in memory (text-only round-trips fine in // .hwpx; tables/images do not, so we render tables as plain text rows // with separator pipes — sufficient for v0.2 scope, fully working in // v0.3 once we generate OWPML directly). // 2) exportHwpx and write. await initRhwp(); const doc = HwpDocument.createEmpty(); doc.createBlankDocument(); try { let first = true; for (const item of items) { const sec = doc.getSectionCount() - 1; const para = doc.getParagraphCount(sec) - 1; const tail = doc.getParagraphLength(sec, para); if (item.type === "text") { const prefix = first && tail === 0 ? "" : "\n"; doc.insertText(sec, para, tail, prefix + item.text); first = false; } else if (item.type === "table") { // Render tables as flat lines until v0.3 adds OWPML table generation. const lines: string[] = []; lines.push(item.headers.join(" | ")); lines.push(item.headers.map(() => "---").join(" | ")); for (const row of item.rows) lines.push(row.join(" | ")); const block = (first && tail === 0 ? "" : "\n") + lines.join("\n"); doc.insertText(sec, para, tail, block); first = false; } } const bytes = doc.exportHwpx(); writeFileSync(args.output_path, bytes); } catch (e) { return `문서 생성 오류 (create error): ${(e as Error).message}`; } finally { doc.free(); } return `HWPX 문서 생성 완료 (created): ${args.output_path}`; } - src/server.ts:494-506 (registration)Tool registration in TOOLS array: defines name 'create_hwpx_document', description, and inputSchema (type: object with required output_path and content string properties).
{ name: "create_hwpx_document", description: "Create a new .hwpx file from a JSON content list of {type:'text',text} items. Tables (type:'table',headers,rows) are rendered as flat text rows in v0.2. Args: output_path (must end with .hwpx), content (JSON string of items).", inputSchema: { type: "object", properties: { output_path: { type: "string" }, content: { type: "string" }, }, required: ["output_path", "content"], }, }, - src/server.ts:517-517 (registration)Handler mapping: 'create_hwpx_document' is mapped to the createHwpxDocument function imported from ./tools/write.js.
create_hwpx_document: createHwpxDocument,