|
| 1 | +# DANP-Engine WASM Module Development Manual |
| 2 | + |
| 3 | +This document provides a clear guide for developers on creating, compiling, and integrating new WASM modules into the DANP-Engine. |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +WASM (WebAssembly) modules are a core component of the DANP-Engine, offering a secure and portable way to execute decentralized AI tools and various computational tasks. By encapsulating functionality within WASM modules, we ensure that code runs in a sandboxed environment, protecting the host system's security. |
| 8 | + |
| 9 | +## Technology Stack |
| 10 | + |
| 11 | +To streamline the development process and maintain consistency across the project, we have chosen the following technologies for writing WASM modules: |
| 12 | + |
| 13 | +- **Go:** Aligns with the core language of the DANP-Engine. |
| 14 | +- **extism/go-pdk:** A powerful Go PDK (Plugin Development Kit) that greatly simplifies WASM module development, especially input/output handling and interaction with the host environment, avoiding complex manual memory management. |
| 15 | +- **TinyGo:** A Go compiler for embedded systems and WebAssembly, which produces smaller WASM files. |
| 16 | + |
| 17 | +## Steps to Write a New Module |
| 18 | + |
| 19 | +Here are the complete steps to add a new WASM module to the DANP-Engine: |
| 20 | + |
| 21 | +### 1. Create the Directory |
| 22 | + |
| 23 | +In the `wasm-examples/` directory at the project root, create a new subdirectory for your module. For example, for the `say_hello` module: |
| 24 | + |
| 25 | +```bash |
| 26 | +mkdir -p wasm-examples/say_hello |
| 27 | +``` |
| 28 | + |
| 29 | +### 2. Write the Go Code |
| 30 | + |
| 31 | +In your module's directory, create a `main.go` file. Use `extism/go-pdk` to handle inputs and outputs. |
| 32 | + |
| 33 | +**Example: `wasm-examples/say_hello/main.go`** |
| 34 | + |
| 35 | +```go |
| 36 | +package main |
| 37 | + |
| 38 | +import ( |
| 39 | + "fmt" |
| 40 | + "github.com/extism/go-pdk" |
| 41 | +) |
| 42 | + |
| 43 | +//export say_hello |
| 44 | +func say_hello() int32 { |
| 45 | + // Read the input string from the host |
| 46 | + name := pdk.InputString() |
| 47 | + |
| 48 | + // Create the greeting message |
| 49 | + greeting := fmt.Sprintf("Hello, %s!", name) |
| 50 | + |
| 51 | + // Return the greeting to the host |
| 52 | + pdk.OutputString(greeting) |
| 53 | + return 0 // Indicate success |
| 54 | +} |
| 55 | + |
| 56 | +func main() {} |
| 57 | +``` |
| 58 | + |
| 59 | +### 3. Compile the WASM Module |
| 60 | + |
| 61 | +We provide a unified command for compiling, which handles Go module initialization, dependency fetching, and WASM compilation automatically. To produce the smallest possible WASM files, we use several optimization flags. |
| 62 | + |
| 63 | +Run the following command in your module's directory: |
| 64 | + |
| 65 | +```bash |
| 66 | +cd wasm-examples/YOUR_MODULE_NAME && \ |
| 67 | +go mod init YOUR_MODULE_NAME >/dev/null 2>&1 && \ |
| 68 | +go get github.com/extism/go-pdk && \ |
| 69 | +GOOS=wasip1 GOARCH=wasm tinygo build -o YOUR_MODULE_NAME.wasm -target wasi -opt=z -no-debug -scheduler=none main.go |
| 70 | +``` |
| 71 | + |
| 72 | +Replace `YOUR_MODULE_NAME` with the name of your module. |
| 73 | + |
| 74 | +**Optimization Flags:** |
| 75 | +- `-opt=z`: Aggressively optimizes for size, potentially at the cost of some execution speed. |
| 76 | +- `-no-debug`: Strips all debug information from the binary. |
| 77 | +- `-scheduler=none`: Removes the Go scheduler, which is not needed for simple, non-concurrent WASM modules. |
| 78 | + |
| 79 | +### 4. Integrate into `mcp_manifest.yaml` |
| 80 | + |
| 81 | + |
| 82 | +Finally, in the `config/mcp_manifest.yaml` file, add your new module to the `modules` list. |
| 83 | + |
| 84 | +**Example:** |
| 85 | + |
| 86 | +```yaml |
| 87 | +modules: |
| 88 | + - name: "say_hello" |
| 89 | + wasm_path: "file://wasm-examples/say_hello/say_hello.wasm" |
| 90 | + tools: |
| 91 | + - name: "say_hello" |
| 92 | + description: "Greet someone by name" |
| 93 | + inputs: |
| 94 | + - name: "name" |
| 95 | + type: "string" |
| 96 | + required: true |
| 97 | + description: "Name to greet" |
| 98 | + outputs: |
| 99 | + type: "string" |
| 100 | + description: "Greeting message" |
| 101 | +``` |
| 102 | +
|
| 103 | +## Notes |
| 104 | +
|
| 105 | +- **Input/Output:** `extism/go-pdk` greatly simplifies I/O. For simple strings, you can use `pdk.InputString()` and `pdk.OutputString()`. For complex JSON data, read the bytes with `pdk.Input()`, then parse with `json.Unmarshal`. |
| 106 | +- **Error Handling:** It is recommended to add proper error handling in your WASM functions and pass error messages back to the host via return values or output strings. |
| 107 | +- **Performance:** Keep your WASM modules lightweight and efficient. Avoid long-running or memory-intensive tasks within the WASM module itself. |
0 commit comments