( content: string, filename: string )
| 103 | * 解析 Lua 文件元信息 |
| 104 | */ |
| 105 | export function parseLuaMetadata( |
| 106 | content: string, |
| 107 | filename: string |
| 108 | ): LuaToolMeta { |
| 109 | const lines = content.split("\n"); |
| 110 | const meta: Partial<LuaToolMeta> = { |
| 111 | params: {}, |
| 112 | }; |
| 113 | const scriptLines: string[] = []; |
| 114 | |
| 115 | for (const line of lines) { |
| 116 | if (line.startsWith("-- name:")) { |
| 117 | meta.name = line.replace("-- name:", "").trim(); |
| 118 | } else if (line.startsWith("-- description:")) { |
| 119 | meta.description = line.replace("-- description:", "").trim(); |
| 120 | } else if (line.startsWith("-- params:")) { |
| 121 | const paramsStr = line.replace("-- params:", "").trim(); |
| 122 | try { |
| 123 | meta.params = JSON.parse(paramsStr); |
| 124 | } catch { |
| 125 | meta.params = {}; |
| 126 | } |
| 127 | } else if (!line.trim().startsWith("--")) { |
| 128 | scriptLines.push(line); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // 如果没有显式 name,从文件名推导 |
| 133 | if (!meta.name) { |
| 134 | meta.name = filename.replace(".lua", "").replace(/-/g, "."); |
| 135 | } |
| 136 | |
| 137 | // 如果没有描述,使用空字符串 |
| 138 | if (!meta.description) { |
| 139 | meta.description = ""; |
| 140 | } |
| 141 | |
| 142 | meta.script = scriptLines.join("\n").trim(); |
| 143 | return meta as LuaToolMeta; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * 从 LuaToolMeta 创建 ToolDefinition |
no test coverage detected