zigsm 1.4.4 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/docs.js CHANGED
@@ -34,6 +34,10 @@ export async function ensureDocs(zigVersion, updatePolicy = "manual", isMcpMode
34
34
  console.log(`Updating documentation for Zig version: ${zigVersion}`);
35
35
  const builtinFunctions = await extractBuiltinFunctions(zigVersion, isMcpMode, true);
36
36
  await downloadSourcesTar(zigVersion, isMcpMode, true, docSource);
37
+ // Pre-download version-matched WASM for remote docs
38
+ if (docSource === "remote") {
39
+ await downloadVersionWasm(zigVersion, isMcpMode, true);
40
+ }
37
41
  const dir = path.dirname(metadataPath);
38
42
  if (!fs.existsSync(dir)) {
39
43
  fs.mkdirSync(dir, { recursive: true });
@@ -100,6 +104,41 @@ export async function downloadSourcesTar(zigVersion, isMcpMode = false, forceUpd
100
104
  console.log(`Downloaded sources.tar to ${sourcesPath}`);
101
105
  return uint8Array;
102
106
  }
107
+ export async function downloadVersionWasm(zigVersion, isMcpMode = false, forceUpdate = false) {
108
+ const paths = envPaths("zigsm", { suffix: "" });
109
+ const versionCacheDir = path.join(paths.cache, zigVersion);
110
+ const wasmCachePath = path.join(versionCacheDir, "main.wasm");
111
+ if (fs.existsSync(wasmCachePath) && !forceUpdate) {
112
+ if (!isMcpMode)
113
+ console.log(`Using cached main.wasm from ${wasmCachePath}`);
114
+ return new Uint8Array(fs.readFileSync(wasmCachePath));
115
+ }
116
+ const url = `https://ziglang.org/documentation/${zigVersion}/std/main.wasm`;
117
+ if (!isMcpMode)
118
+ console.log(`Downloading main.wasm from: ${url}`);
119
+ try {
120
+ const response = await fetch(url);
121
+ if (!response.ok) {
122
+ if (!isMcpMode)
123
+ console.log(`Failed to download main.wasm: HTTP ${response.status}`);
124
+ return null;
125
+ }
126
+ const buffer = await response.arrayBuffer();
127
+ const uint8Array = new Uint8Array(buffer);
128
+ if (!fs.existsSync(versionCacheDir)) {
129
+ fs.mkdirSync(versionCacheDir, { recursive: true });
130
+ }
131
+ fs.writeFileSync(wasmCachePath, uint8Array);
132
+ if (!isMcpMode)
133
+ console.log(`Downloaded main.wasm to ${wasmCachePath}`);
134
+ return uint8Array;
135
+ }
136
+ catch (error) {
137
+ if (!isMcpMode)
138
+ console.log(`Failed to download main.wasm: ${error}`);
139
+ return null;
140
+ }
141
+ }
103
142
  async function downloadSourcesTarPath(zigVersion) {
104
143
  const paths = envPaths("zigsm", { suffix: "" });
105
144
  const versionCacheDir = path.join(paths.cache, zigVersion);
@@ -1,6 +1,5 @@
1
1
  import * as child_process from "node:child_process";
2
2
  import * as fs from "node:fs";
3
- import * as http from "node:http";
4
3
  import * as path from "node:path";
5
4
  let activeServer = null;
6
5
  function findZigExecutable() {
@@ -105,50 +104,23 @@ export function stopLocalStdServer() {
105
104
  activeServer = null;
106
105
  }
107
106
  }
108
- export async function fetchFromLocalServer(path) {
107
+ export async function fetchFromLocalServer(urlPath) {
109
108
  const server = await startLocalStdServer();
110
- const url = `${server.baseUrl}${path}`;
111
- return new Promise((resolve, reject) => {
112
- http.get(url, (res) => {
113
- let data = "";
114
- res.on("data", (chunk) => {
115
- data += chunk;
116
- });
117
- res.on("end", () => {
118
- if (res.statusCode === 200) {
119
- resolve(data);
120
- }
121
- else {
122
- reject(new Error(`HTTP ${res.statusCode}: ${data}`));
123
- }
124
- });
125
- }).on("error", (error) => {
126
- reject(error);
127
- });
128
- });
109
+ const url = `${server.baseUrl}${urlPath}`;
110
+ const response = await fetch(url);
111
+ if (!response.ok) {
112
+ throw new Error(`HTTP ${response.status}: ${await response.text()}`);
113
+ }
114
+ return await response.text();
129
115
  }
130
116
  export async function getLocalStdSources() {
131
117
  const server = await startLocalStdServer();
132
118
  const url = `${server.baseUrl}/sources.tar`;
133
- return new Promise((resolve, reject) => {
134
- http.get(url, (res) => {
135
- const chunks = [];
136
- res.on("data", (chunk) => {
137
- chunks.push(chunk);
138
- });
139
- res.on("end", () => {
140
- if (res.statusCode === 200) {
141
- const buffer = Buffer.concat(chunks);
142
- resolve(new Uint8Array(buffer));
143
- }
144
- else {
145
- reject(new Error(`Failed to fetch sources.tar: HTTP ${res.statusCode}`));
146
- }
147
- });
148
- }).on("error", (error) => {
149
- reject(error);
150
- });
151
- });
119
+ const response = await fetch(url);
120
+ if (!response.ok) {
121
+ throw new Error(`Failed to fetch sources.tar: HTTP ${response.status}`);
122
+ }
123
+ return new Uint8Array(await response.arrayBuffer());
152
124
  }
153
125
  process.on("exit", stopLocalStdServer);
154
126
  process.on("SIGINT", stopLocalStdServer);
package/dist/mcp.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
3
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
- import { downloadSourcesTar, ensureDocs, startViewServer, } from "./docs.js";
4
+ import { downloadSourcesTar, downloadVersionWasm, ensureDocs, startViewServer, } from "./docs.js";
5
5
  import { registerAllTools } from "./tools.js";
6
6
  function parseArgs(args) {
7
7
  const options = {
@@ -102,6 +102,11 @@ async function main() {
102
102
  }
103
103
  const builtinFunctions = await ensureDocs(options.version, options.updatePolicy, true, options.docSource);
104
104
  const stdSources = await downloadSourcesTar(options.version, true, false, options.docSource);
105
+ // Download version-matched WASM for remote docs (different Zig versions have incompatible AST formats)
106
+ let versionWasm = null;
107
+ if (options.docSource === "remote") {
108
+ versionWasm = await downloadVersionWasm(options.version, true);
109
+ }
105
110
  const mcpServer = new McpServer({
106
111
  name: "ZSM",
107
112
  description: "Retrieves up-to-date documentation for the Zig programming language standard library and builtin functions.",
@@ -110,6 +115,7 @@ async function main() {
110
115
  await registerAllTools(mcpServer, builtinFunctions, stdSources, {
111
116
  zigVersion: options.version,
112
117
  docSource: options.docSource,
118
+ versionWasm,
113
119
  });
114
120
  const transport = new StdioServerTransport();
115
121
  await mcpServer.connect(transport);
package/dist/tools.js CHANGED
@@ -224,7 +224,8 @@ function getStdLibItemTool(wasmPath, stdSources) {
224
224
  }
225
225
  export async function registerAllTools(mcpServer, builtinFunctions, stdSources, options) {
226
226
  const currentDir = path.dirname(fileURLToPath(import.meta.url));
227
- const wasmPath = path.join(currentDir, "main.wasm");
227
+ const bundledWasmPath = path.join(currentDir, "main.wasm");
228
+ const wasmPath = options.versionWasm ?? bundledWasmPath;
228
229
  const listBuiltinFunctionsTool = createListBuiltinFunctionsTool(builtinFunctions);
229
230
  mcpServer.registerTool(listBuiltinFunctionsTool.name, listBuiltinFunctionsTool.config, listBuiltinFunctionsTool.handler);
230
231
  const getBuiltinFunction = getBuiltinFunctionTool(builtinFunctions);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zigsm",
3
- "version": "1.4.4",
3
+ "version": "1.5.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "zsm": "dist/mcp.js"