🛟 Updated.Unverified
1c857ff parent: 88bde05 modified
04-memory/reset.sh +0 -0 | old mode 100755 | ||
| new mode 100644 |
| old mode 100755 | |||
| new mode 100644 |
added
07-wasm-tools/agent.yaml +63 -0 | new file mode 100644 | ||
| @@ -0,0 +1,63 @@ | ||
| 1 | +# Agent settings — "notes desk": an assistant for a folder of notes that acts | |
| 2 | +# ONLY through four WASM tools (ticket 119). Each tool is a WebAssembly module | |
| 3 | +# built with TinyGo (tools-src/ → tools/, ./build.sh) and run by mm itself, in | |
| 4 | +# a sandbox: no file, no environment variable, no network — except the | |
| 5 | +# folders its entry opens. mini-me on Qwen3.5 4B, served by llmman. | |
| 6 | +# | |
| 7 | +# cd demos/wasm-tools | |
| 8 | +# mm -p "Which notes mention TinyGo?" | |
| 9 | +provider: llmman | |
| 10 | +model: "hf.co/unsloth/Qwen3.5-4B-GGUF:Q4_K_M" | |
| 11 | + | |
| 12 | +# The four tools below are the agent's only way to act. | |
| 13 | +bashTool: false | |
| 14 | +editTools: false | |
| 15 | +planTool: false | |
| 16 | +fetchContentTool: false | |
| 17 | +skillsDir: no-skills | |
| 18 | +# true: the tools run without asking. Set it to false to confirm each call | |
| 19 | +# (in -p nobody can answer, so every call is then refused). | |
| 20 | +yolo: true | |
| 21 | +maxTurns: 6 | |
| 22 | + | |
| 23 | +system: | | |
| 24 | + You are the notes desk: you help with the notes kept in the notes/ folder. | |
| 25 | + You act only through your tools; never invent what a note says. | |
| 26 | + To find which notes talk about something, call search_notes; to read a | |
| 27 | + whole note, call read_note with its file name (e.g. wazero.md); to count | |
| 28 | + the words of a text, call word_stats; to keep a new note, call save_note. | |
| 29 | + Then answer briefly, in the user's language, from what the tools returned. | |
| 30 | + | |
| 31 | +tools: | |
| 32 | + # Pure computation: no folder at all. | |
| 33 | + - name: word_stats | |
| 34 | + description: Count the words, lines and characters of a text, and list its most frequent words. | |
| 35 | + parameters: | |
| 36 | + text: { type: string, description: "The text to measure" } | |
| 37 | + top: { type: integer, required: false, default: 5, description: "How many frequent words to list" } | |
| 38 | + wasm: ./tools/word_stats.wasm | |
| 39 | + | |
| 40 | + # Sees notes/, read-only — nothing else of the project. | |
| 41 | + - name: search_notes | |
| 42 | + description: Find the lines of the notes that mention a word; each line comes with its note's file name and line number. | |
| 43 | + parameters: | |
| 44 | + word: { type: string, description: "One word, e.g. wazero" } | |
| 45 | + wasm: ./tools/search_notes.wasm | |
| 46 | + read: [notes] | |
| 47 | + | |
| 48 | + - name: read_note | |
| 49 | + description: Show one whole note. | |
| 50 | + parameters: | |
| 51 | + name: { type: string, description: "The note's file name, e.g. wazero.md" } | |
| 52 | + wasm: ./tools/read_note.wasm | |
| 53 | + read: [notes] | |
| 54 | + | |
| 55 | + # Writes into out/ only: it can neither read nor change notes/. | |
| 56 | + - name: save_note | |
| 57 | + description: Save a new note in out/, under a file name made from its title. | |
| 58 | + parameters: | |
| 59 | + title: { type: string, description: "The note's title, e.g. Ideas for v1" } | |
| 60 | + text: { type: string, description: "The note's text, in Markdown" } | |
| 61 | + wasm: ./tools/save_note.wasm | |
| 62 | + write: [out] | |
| 63 | + timeout: 10s | |
| new file mode 100644 | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | +# Agent settings — "notes desk": an assistant for a folder of notes that acts | ||
| 2 | +# ONLY through four WASM tools (ticket 119). Each tool is a WebAssembly module | ||
| 3 | +# built with TinyGo (tools-src/ → tools/, ./build.sh) and run by mm itself, in | ||
| 4 | +# a sandbox: no file, no environment variable, no network — except the | ||
| 5 | +# folders its entry opens. mini-me on Qwen3.5 4B, served by llmman. | ||
| 6 | +# | ||
| 7 | +# cd demos/wasm-tools | ||
| 8 | +# mm -p "Which notes mention TinyGo?" | ||
| 9 | +provider: llmman | ||
| 10 | +model: "hf.co/unsloth/Qwen3.5-4B-GGUF:Q4_K_M" | ||
| 11 | + | ||
| 12 | +# The four tools below are the agent's only way to act. | ||
| 13 | +bashTool: false | ||
| 14 | +editTools: false | ||
| 15 | +planTool: false | ||
| 16 | +fetchContentTool: false | ||
| 17 | +skillsDir: no-skills | ||
| 18 | +# true: the tools run without asking. Set it to false to confirm each call | ||
| 19 | +# (in -p nobody can answer, so every call is then refused). | ||
| 20 | +yolo: true | ||
| 21 | +maxTurns: 6 | ||
| 22 | + | ||
| 23 | +system: | | ||
| 24 | + You are the notes desk: you help with the notes kept in the notes/ folder. | ||
| 25 | + You act only through your tools; never invent what a note says. | ||
| 26 | + To find which notes talk about something, call search_notes; to read a | ||
| 27 | + whole note, call read_note with its file name (e.g. wazero.md); to count | ||
| 28 | + the words of a text, call word_stats; to keep a new note, call save_note. | ||
| 29 | + Then answer briefly, in the user's language, from what the tools returned. | ||
| 30 | + | ||
| 31 | +tools: | ||
| 32 | + # Pure computation: no folder at all. | ||
| 33 | + - name: word_stats | ||
| 34 | + description: Count the words, lines and characters of a text, and list its most frequent words. | ||
| 35 | + parameters: | ||
| 36 | + text: { type: string, description: "The text to measure" } | ||
| 37 | + top: { type: integer, required: false, default: 5, description: "How many frequent words to list" } | ||
| 38 | + wasm: ./tools/word_stats.wasm | ||
| 39 | + | ||
| 40 | + # Sees notes/, read-only — nothing else of the project. | ||
| 41 | + - name: search_notes | ||
| 42 | + description: Find the lines of the notes that mention a word; each line comes with its note's file name and line number. | ||
| 43 | + parameters: | ||
| 44 | + word: { type: string, description: "One word, e.g. wazero" } | ||
| 45 | + wasm: ./tools/search_notes.wasm | ||
| 46 | + read: [notes] | ||
| 47 | + | ||
| 48 | + - name: read_note | ||
| 49 | + description: Show one whole note. | ||
| 50 | + parameters: | ||
| 51 | + name: { type: string, description: "The note's file name, e.g. wazero.md" } | ||
| 52 | + wasm: ./tools/read_note.wasm | ||
| 53 | + read: [notes] | ||
| 54 | + | ||
| 55 | + # Writes into out/ only: it can neither read nor change notes/. | ||
| 56 | + - name: save_note | ||
| 57 | + description: Save a new note in out/, under a file name made from its title. | ||
| 58 | + parameters: | ||
| 59 | + title: { type: string, description: "The note's title, e.g. Ideas for v1" } | ||
| 60 | + text: { type: string, description: "The note's text, in Markdown" } | ||
| 61 | + wasm: ./tools/save_note.wasm | ||
| 62 | + write: [out] | ||
| 63 | + timeout: 10s | ||
added
07-wasm-tools/build.sh +10 -0 | new file mode 100755 | ||
| @@ -0,0 +1,10 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# Builds the demo's WASM tools with TinyGo: tools-src/<name>/ → tools/<name>.wasm. | |
| 3 | +# cd demos/wasm-tools && ./build.sh | |
| 4 | +set -eu | |
| 5 | +cd "$(dirname "$0")/tools-src" | |
| 6 | +for dir in */; do | |
| 7 | + name=${dir%/} | |
| 8 | + tinygo build -o "../tools/$name.wasm" -target=wasip1 -no-debug "./$name" | |
| 9 | + echo "built tools/$name.wasm ($(wc -c < "../tools/$name.wasm") bytes)" | |
| 10 | +done | |
| new file mode 100755 | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | +#!/bin/sh | ||
| 2 | +# Builds the demo's WASM tools with TinyGo: tools-src/<name>/ → tools/<name>.wasm. | ||
| 3 | +# cd demos/wasm-tools && ./build.sh | ||
| 4 | +set -eu | ||
| 5 | +cd "$(dirname "$0")/tools-src" | ||
| 6 | +for dir in */; do | ||
| 7 | + name=${dir%/} | ||
| 8 | + tinygo build -o "../tools/$name.wasm" -target=wasip1 -no-debug "./$name" | ||
| 9 | + echo "built tools/$name.wasm ($(wc -c < "../tools/$name.wasm") bytes)" | ||
| 10 | +done | ||
added
07-wasm-tools/notes/release-plan.md +5 -0 | new file mode 100644 | ||
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Release plan | |
| 2 | + | |
| 3 | +v0.9.0: mm init, the openai provider, custom tools as WASM modules. | |
| 4 | +The WASM tools need a demo with a folder they can read and one they can write. | |
| 5 | +Before the release: run the quality gate and write the release notes. | |
| new file mode 100644 | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | +# Release plan | ||
| 2 | + | ||
| 3 | +v0.9.0: mm init, the openai provider, custom tools as WASM modules. | ||
| 4 | +The WASM tools need a demo with a folder they can read and one they can write. | ||
| 5 | +Before the release: run the quality gate and write the release notes. | ||
added
07-wasm-tools/notes/tinygo.md +6 -0 | new file mode 100644 | ||
| @@ -0,0 +1,6 @@ | ||
| 1 | +# TinyGo | |
| 2 | + | |
| 3 | +TinyGo compiles Go for small places: microcontrollers, and WebAssembly. | |
| 4 | +`tinygo build -target=wasip1` makes a WASI command: a main, stdin, stdout. | |
| 5 | +A module that uses encoding/json weighs about half a megabyte. | |
| 6 | +Paths must be clean inside a WASI module: "notes/a.md", not "./notes/a.md". | |
| new file mode 100644 | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | +# TinyGo | ||
| 2 | + | ||
| 3 | +TinyGo compiles Go for small places: microcontrollers, and WebAssembly. | ||
| 4 | +`tinygo build -target=wasip1` makes a WASI command: a main, stdin, stdout. | ||
| 5 | +A module that uses encoding/json weighs about half a megabyte. | ||
| 6 | +Paths must be clean inside a WASI module: "notes/a.md", not "./notes/a.md". | ||
added
07-wasm-tools/notes/wazero.md +6 -0 | new file mode 100644 | ||
| @@ -0,0 +1,6 @@ | ||
| 1 | +# wazero | |
| 2 | + | |
| 3 | +wazero is a WebAssembly runtime written in pure Go: no CGO, no shared library. | |
| 4 | +It runs WASI modules, such as the ones TinyGo builds with -target=wasip1. | |
| 5 | +A module only sees the folders the host mounts for it. | |
| 6 | +Compiling a module takes a fraction of a second; each run is a fresh instance. | |
| new file mode 100644 | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | +# wazero | ||
| 2 | + | ||
| 3 | +wazero is a WebAssembly runtime written in pure Go: no CGO, no shared library. | ||
| 4 | +It runs WASI modules, such as the ones TinyGo builds with -target=wasip1. | ||
| 5 | +A module only sees the folders the host mounts for it. | ||
| 6 | +Compiling a module takes a fraction of a second; each run is a fresh instance. | ||
added
07-wasm-tools/out/.gitignore +3 -0 | new file mode 100644 | ||
| @@ -0,0 +1,3 @@ | ||
| 1 | +# Written by save_note: everything here is generated. | |
| 2 | +* | |
| 3 | +!.gitignore | |
| new file mode 100644 | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | +# Written by save_note: everything here is generated. | ||
| 2 | +* | ||
| 3 | +!.gitignore | ||
added
07-wasm-tools/tools-src/go.mod +6 -0 | new file mode 100644 | ||
| @@ -0,0 +1,6 @@ | ||
| 1 | +// The sources of the demo's WASM tools. Their own module, so that `go build | |
| 2 | +// ./...` in mini-me does not build them: they are built with TinyGo | |
| 3 | +// (../build.sh). | |
| 4 | +module wasmtools | |
| 5 | + | |
| 6 | +go 1.24 | |
| new file mode 100644 | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | +// The sources of the demo's WASM tools. Their own module, so that `go build | ||
| 2 | +// ./...` in mini-me does not build them: they are built with TinyGo | ||
| 3 | +// (../build.sh). | ||
| 4 | +module wasmtools | ||
| 5 | + | ||
| 6 | +go 1.24 | ||
added
07-wasm-tools/tools-src/read_note/main.go +35 -0 | new file mode 100644 | ||
| @@ -0,0 +1,35 @@ | ||
| 1 | +// read_note prints one note. The name comes from the model, so it may be | |
| 2 | +// anything — "../agent.yaml" included: the module looks for it under notes/, | |
| 3 | +// and the sandbox has nothing else to show it. | |
| 4 | +// | |
| 5 | +// Input (stdin): {"name": "wazero.md"} | |
| 6 | +package main | |
| 7 | + | |
| 8 | +import ( | |
| 9 | + "encoding/json" | |
| 10 | + "fmt" | |
| 11 | + "os" | |
| 12 | + "path" | |
| 13 | +) | |
| 14 | + | |
| 15 | +type input struct { | |
| 16 | + Name string `json:"name"` | |
| 17 | +} | |
| 18 | + | |
| 19 | +func main() { | |
| 20 | + var in input | |
| 21 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | |
| 22 | + fmt.Fprintln(os.Stderr, "read_note: bad input:", err) | |
| 23 | + os.Exit(2) | |
| 24 | + } | |
| 25 | + // TinyGo's WASI layer refuses "./" and ".." in a path: clean it first. | |
| 26 | + // path.Join("notes", "../agent.yaml") is "agent.yaml", which is not | |
| 27 | + // mounted — the sandbox, not this line, is what keeps it out. | |
| 28 | + name := path.Join("notes", in.Name) | |
| 29 | + data, err := os.ReadFile(name) | |
| 30 | + if err != nil { | |
| 31 | + fmt.Printf("cannot read %s: %v\n", name, err) | |
| 32 | + os.Exit(1) | |
| 33 | + } | |
| 34 | + fmt.Print(string(data)) | |
| 35 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | +// read_note prints one note. The name comes from the model, so it may be | ||
| 2 | +// anything — "../agent.yaml" included: the module looks for it under notes/, | ||
| 3 | +// and the sandbox has nothing else to show it. | ||
| 4 | +// | ||
| 5 | +// Input (stdin): {"name": "wazero.md"} | ||
| 6 | +package main | ||
| 7 | + | ||
| 8 | +import ( | ||
| 9 | + "encoding/json" | ||
| 10 | + "fmt" | ||
| 11 | + "os" | ||
| 12 | + "path" | ||
| 13 | +) | ||
| 14 | + | ||
| 15 | +type input struct { | ||
| 16 | + Name string `json:"name"` | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +func main() { | ||
| 20 | + var in input | ||
| 21 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | ||
| 22 | + fmt.Fprintln(os.Stderr, "read_note: bad input:", err) | ||
| 23 | + os.Exit(2) | ||
| 24 | + } | ||
| 25 | + // TinyGo's WASI layer refuses "./" and ".." in a path: clean it first. | ||
| 26 | + // path.Join("notes", "../agent.yaml") is "agent.yaml", which is not | ||
| 27 | + // mounted — the sandbox, not this line, is what keeps it out. | ||
| 28 | + name := path.Join("notes", in.Name) | ||
| 29 | + data, err := os.ReadFile(name) | ||
| 30 | + if err != nil { | ||
| 31 | + fmt.Printf("cannot read %s: %v\n", name, err) | ||
| 32 | + os.Exit(1) | ||
| 33 | + } | ||
| 34 | + fmt.Print(string(data)) | ||
| 35 | +} | ||
added
07-wasm-tools/tools-src/save_note/main.go +54 -0 | new file mode 100644 | ||
| @@ -0,0 +1,54 @@ | ||
| 1 | +// save_note writes a note into out/, the one folder its entry opens for | |
| 2 | +// writing. It cannot touch notes/ or anything else. | |
| 3 | +// | |
| 4 | +// Input (stdin): {"title": "…", "text": "…"} | |
| 5 | +package main | |
| 6 | + | |
| 7 | +import ( | |
| 8 | + "encoding/json" | |
| 9 | + "fmt" | |
| 10 | + "os" | |
| 11 | + "strings" | |
| 12 | + "unicode" | |
| 13 | +) | |
| 14 | + | |
| 15 | +type input struct { | |
| 16 | + Title string `json:"title"` | |
| 17 | + Text string `json:"text"` | |
| 18 | +} | |
| 19 | + | |
| 20 | +// slug turns a title into a file name: "Ideas for v1" → "ideas-for-v1". | |
| 21 | +func slug(title string) string { | |
| 22 | + var b strings.Builder | |
| 23 | + dash := false | |
| 24 | + for _, r := range strings.ToLower(title) { | |
| 25 | + if unicode.IsLetter(r) || unicode.IsDigit(r) { | |
| 26 | + b.WriteRune(r) | |
| 27 | + dash = false | |
| 28 | + } else if !dash && b.Len() > 0 { | |
| 29 | + b.WriteByte('-') | |
| 30 | + dash = true | |
| 31 | + } | |
| 32 | + } | |
| 33 | + return strings.Trim(b.String(), "-") | |
| 34 | +} | |
| 35 | + | |
| 36 | +func main() { | |
| 37 | + var in input | |
| 38 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | |
| 39 | + fmt.Fprintln(os.Stderr, "save_note: bad input:", err) | |
| 40 | + os.Exit(2) | |
| 41 | + } | |
| 42 | + name := slug(in.Title) | |
| 43 | + if name == "" { | |
| 44 | + fmt.Println("give the note a title with at least one letter or digit") | |
| 45 | + os.Exit(1) | |
| 46 | + } | |
| 47 | + file := "out/" + name + ".md" | |
| 48 | + body := "# " + strings.TrimSpace(in.Title) + "\n\n" + strings.TrimSpace(in.Text) + "\n" | |
| 49 | + if err := os.WriteFile(file, []byte(body), 0o644); err != nil { | |
| 50 | + fmt.Printf("cannot write %s: %v\n", file, err) | |
| 51 | + os.Exit(1) | |
| 52 | + } | |
| 53 | + fmt.Printf("saved %s (%d bytes)\n", file, len(body)) | |
| 54 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | +// save_note writes a note into out/, the one folder its entry opens for | ||
| 2 | +// writing. It cannot touch notes/ or anything else. | ||
| 3 | +// | ||
| 4 | +// Input (stdin): {"title": "…", "text": "…"} | ||
| 5 | +package main | ||
| 6 | + | ||
| 7 | +import ( | ||
| 8 | + "encoding/json" | ||
| 9 | + "fmt" | ||
| 10 | + "os" | ||
| 11 | + "strings" | ||
| 12 | + "unicode" | ||
| 13 | +) | ||
| 14 | + | ||
| 15 | +type input struct { | ||
| 16 | + Title string `json:"title"` | ||
| 17 | + Text string `json:"text"` | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +// slug turns a title into a file name: "Ideas for v1" → "ideas-for-v1". | ||
| 21 | +func slug(title string) string { | ||
| 22 | + var b strings.Builder | ||
| 23 | + dash := false | ||
| 24 | + for _, r := range strings.ToLower(title) { | ||
| 25 | + if unicode.IsLetter(r) || unicode.IsDigit(r) { | ||
| 26 | + b.WriteRune(r) | ||
| 27 | + dash = false | ||
| 28 | + } else if !dash && b.Len() > 0 { | ||
| 29 | + b.WriteByte('-') | ||
| 30 | + dash = true | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + return strings.Trim(b.String(), "-") | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +func main() { | ||
| 37 | + var in input | ||
| 38 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | ||
| 39 | + fmt.Fprintln(os.Stderr, "save_note: bad input:", err) | ||
| 40 | + os.Exit(2) | ||
| 41 | + } | ||
| 42 | + name := slug(in.Title) | ||
| 43 | + if name == "" { | ||
| 44 | + fmt.Println("give the note a title with at least one letter or digit") | ||
| 45 | + os.Exit(1) | ||
| 46 | + } | ||
| 47 | + file := "out/" + name + ".md" | ||
| 48 | + body := "# " + strings.TrimSpace(in.Title) + "\n\n" + strings.TrimSpace(in.Text) + "\n" | ||
| 49 | + if err := os.WriteFile(file, []byte(body), 0o644); err != nil { | ||
| 50 | + fmt.Printf("cannot write %s: %v\n", file, err) | ||
| 51 | + os.Exit(1) | ||
| 52 | + } | ||
| 53 | + fmt.Printf("saved %s (%d bytes)\n", file, len(body)) | ||
| 54 | +} | ||
added
07-wasm-tools/tools-src/search_notes/main.go +59 -0 | new file mode 100644 | ||
| @@ -0,0 +1,59 @@ | ||
| 1 | +// search_notes finds the lines of the notes that mention a word. Its entry | |
| 2 | +// opens notes/ read-only: that folder is all this module can see. | |
| 3 | +// | |
| 4 | +// Input (stdin): {"word": "…"} | |
| 5 | +package main | |
| 6 | + | |
| 7 | +import ( | |
| 8 | + "encoding/json" | |
| 9 | + "fmt" | |
| 10 | + "os" | |
| 11 | + "path" | |
| 12 | + "sort" | |
| 13 | + "strings" | |
| 14 | +) | |
| 15 | + | |
| 16 | +type input struct { | |
| 17 | + Word string `json:"word"` | |
| 18 | +} | |
| 19 | + | |
| 20 | +func main() { | |
| 21 | + var in input | |
| 22 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | |
| 23 | + fmt.Fprintln(os.Stderr, "search_notes: bad input:", err) | |
| 24 | + os.Exit(2) | |
| 25 | + } | |
| 26 | + word := strings.ToLower(strings.TrimSpace(in.Word)) | |
| 27 | + if word == "" { | |
| 28 | + fmt.Println("give a word to look for") | |
| 29 | + os.Exit(1) | |
| 30 | + } | |
| 31 | + entries, err := os.ReadDir("notes") | |
| 32 | + if err != nil { | |
| 33 | + fmt.Println("cannot list notes/:", err) | |
| 34 | + os.Exit(1) | |
| 35 | + } | |
| 36 | + var names []string | |
| 37 | + for _, e := range entries { | |
| 38 | + if !e.IsDir() && strings.HasSuffix(e.Name(), ".md") { | |
| 39 | + names = append(names, e.Name()) | |
| 40 | + } | |
| 41 | + } | |
| 42 | + sort.Strings(names) | |
| 43 | + found := 0 | |
| 44 | + for _, name := range names { | |
| 45 | + data, err := os.ReadFile(path.Join("notes", name)) | |
| 46 | + if err != nil { | |
| 47 | + continue | |
| 48 | + } | |
| 49 | + for i, line := range strings.Split(string(data), "\n") { | |
| 50 | + if strings.Contains(strings.ToLower(line), word) { | |
| 51 | + fmt.Printf("%s:%d: %s\n", name, i+1, strings.TrimSpace(line)) | |
| 52 | + found++ | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + if found == 0 { | |
| 57 | + fmt.Printf("no note mentions %q (searched %d notes)\n", in.Word, len(names)) | |
| 58 | + } | |
| 59 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | +// search_notes finds the lines of the notes that mention a word. Its entry | ||
| 2 | +// opens notes/ read-only: that folder is all this module can see. | ||
| 3 | +// | ||
| 4 | +// Input (stdin): {"word": "…"} | ||
| 5 | +package main | ||
| 6 | + | ||
| 7 | +import ( | ||
| 8 | + "encoding/json" | ||
| 9 | + "fmt" | ||
| 10 | + "os" | ||
| 11 | + "path" | ||
| 12 | + "sort" | ||
| 13 | + "strings" | ||
| 14 | +) | ||
| 15 | + | ||
| 16 | +type input struct { | ||
| 17 | + Word string `json:"word"` | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +func main() { | ||
| 21 | + var in input | ||
| 22 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | ||
| 23 | + fmt.Fprintln(os.Stderr, "search_notes: bad input:", err) | ||
| 24 | + os.Exit(2) | ||
| 25 | + } | ||
| 26 | + word := strings.ToLower(strings.TrimSpace(in.Word)) | ||
| 27 | + if word == "" { | ||
| 28 | + fmt.Println("give a word to look for") | ||
| 29 | + os.Exit(1) | ||
| 30 | + } | ||
| 31 | + entries, err := os.ReadDir("notes") | ||
| 32 | + if err != nil { | ||
| 33 | + fmt.Println("cannot list notes/:", err) | ||
| 34 | + os.Exit(1) | ||
| 35 | + } | ||
| 36 | + var names []string | ||
| 37 | + for _, e := range entries { | ||
| 38 | + if !e.IsDir() && strings.HasSuffix(e.Name(), ".md") { | ||
| 39 | + names = append(names, e.Name()) | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + sort.Strings(names) | ||
| 43 | + found := 0 | ||
| 44 | + for _, name := range names { | ||
| 45 | + data, err := os.ReadFile(path.Join("notes", name)) | ||
| 46 | + if err != nil { | ||
| 47 | + continue | ||
| 48 | + } | ||
| 49 | + for i, line := range strings.Split(string(data), "\n") { | ||
| 50 | + if strings.Contains(strings.ToLower(line), word) { | ||
| 51 | + fmt.Printf("%s:%d: %s\n", name, i+1, strings.TrimSpace(line)) | ||
| 52 | + found++ | ||
| 53 | + } | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + if found == 0 { | ||
| 57 | + fmt.Printf("no note mentions %q (searched %d notes)\n", in.Word, len(names)) | ||
| 58 | + } | ||
| 59 | +} | ||
added
07-wasm-tools/tools-src/word_stats/main.go +58 -0 | new file mode 100644 | ||
| @@ -0,0 +1,58 @@ | ||
| 1 | +// word_stats counts the words, lines and characters of a text, and lists | |
| 2 | +// its most frequent words. Pure computation: the tool's entry opens no | |
| 3 | +// folder, so this module sees no file at all. | |
| 4 | +// | |
| 5 | +// Input (stdin): {"text": "…", "top": 5} | |
| 6 | +package main | |
| 7 | + | |
| 8 | +import ( | |
| 9 | + "encoding/json" | |
| 10 | + "fmt" | |
| 11 | + "os" | |
| 12 | + "sort" | |
| 13 | + "strings" | |
| 14 | + "unicode" | |
| 15 | +) | |
| 16 | + | |
| 17 | +type input struct { | |
| 18 | + Text string `json:"text"` | |
| 19 | + Top int `json:"top"` | |
| 20 | +} | |
| 21 | + | |
| 22 | +func main() { | |
| 23 | + var in input | |
| 24 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | |
| 25 | + fmt.Fprintln(os.Stderr, "word_stats: bad input:", err) | |
| 26 | + os.Exit(2) | |
| 27 | + } | |
| 28 | + words := strings.FieldsFunc(strings.ToLower(in.Text), func(r rune) bool { | |
| 29 | + return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '\'' && r != '-' | |
| 30 | + }) | |
| 31 | + lines := strings.Count(in.Text, "\n") | |
| 32 | + if in.Text != "" && !strings.HasSuffix(in.Text, "\n") { | |
| 33 | + lines++ | |
| 34 | + } | |
| 35 | + fmt.Printf("words: %d\nlines: %d\ncharacters: %d\n", len(words), lines, len([]rune(in.Text))) | |
| 36 | + | |
| 37 | + count := map[string]int{} | |
| 38 | + for _, w := range words { | |
| 39 | + count[w]++ | |
| 40 | + } | |
| 41 | + ranked := make([]string, 0, len(count)) | |
| 42 | + for w := range count { | |
| 43 | + ranked = append(ranked, w) | |
| 44 | + } | |
| 45 | + sort.Slice(ranked, func(i, j int) bool { | |
| 46 | + if count[ranked[i]] != count[ranked[j]] { | |
| 47 | + return count[ranked[i]] > count[ranked[j]] | |
| 48 | + } | |
| 49 | + return ranked[i] < ranked[j] | |
| 50 | + }) | |
| 51 | + if in.Top > 0 && len(ranked) > in.Top { | |
| 52 | + ranked = ranked[:in.Top] | |
| 53 | + } | |
| 54 | + fmt.Println("most frequent:") | |
| 55 | + for _, w := range ranked { | |
| 56 | + fmt.Printf(" %s: %d\n", w, count[w]) | |
| 57 | + } | |
| 58 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | +// word_stats counts the words, lines and characters of a text, and lists | ||
| 2 | +// its most frequent words. Pure computation: the tool's entry opens no | ||
| 3 | +// folder, so this module sees no file at all. | ||
| 4 | +// | ||
| 5 | +// Input (stdin): {"text": "…", "top": 5} | ||
| 6 | +package main | ||
| 7 | + | ||
| 8 | +import ( | ||
| 9 | + "encoding/json" | ||
| 10 | + "fmt" | ||
| 11 | + "os" | ||
| 12 | + "sort" | ||
| 13 | + "strings" | ||
| 14 | + "unicode" | ||
| 15 | +) | ||
| 16 | + | ||
| 17 | +type input struct { | ||
| 18 | + Text string `json:"text"` | ||
| 19 | + Top int `json:"top"` | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +func main() { | ||
| 23 | + var in input | ||
| 24 | + if err := json.NewDecoder(os.Stdin).Decode(&in); err != nil { | ||
| 25 | + fmt.Fprintln(os.Stderr, "word_stats: bad input:", err) | ||
| 26 | + os.Exit(2) | ||
| 27 | + } | ||
| 28 | + words := strings.FieldsFunc(strings.ToLower(in.Text), func(r rune) bool { | ||
| 29 | + return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '\'' && r != '-' | ||
| 30 | + }) | ||
| 31 | + lines := strings.Count(in.Text, "\n") | ||
| 32 | + if in.Text != "" && !strings.HasSuffix(in.Text, "\n") { | ||
| 33 | + lines++ | ||
| 34 | + } | ||
| 35 | + fmt.Printf("words: %d\nlines: %d\ncharacters: %d\n", len(words), lines, len([]rune(in.Text))) | ||
| 36 | + | ||
| 37 | + count := map[string]int{} | ||
| 38 | + for _, w := range words { | ||
| 39 | + count[w]++ | ||
| 40 | + } | ||
| 41 | + ranked := make([]string, 0, len(count)) | ||
| 42 | + for w := range count { | ||
| 43 | + ranked = append(ranked, w) | ||
| 44 | + } | ||
| 45 | + sort.Slice(ranked, func(i, j int) bool { | ||
| 46 | + if count[ranked[i]] != count[ranked[j]] { | ||
| 47 | + return count[ranked[i]] > count[ranked[j]] | ||
| 48 | + } | ||
| 49 | + return ranked[i] < ranked[j] | ||
| 50 | + }) | ||
| 51 | + if in.Top > 0 && len(ranked) > in.Top { | ||
| 52 | + ranked = ranked[:in.Top] | ||
| 53 | + } | ||
| 54 | + fmt.Println("most frequent:") | ||
| 55 | + for _, w := range ranked { | ||
| 56 | + fmt.Printf(" %s: %d\n", w, count[w]) | ||
| 57 | + } | ||
| 58 | +} | ||
added
07-wasm-tools/tools/read_note.wasm +0 -0 | new file mode 100644 | ||
| Binary files /dev/null and b/07-wasm-tools/tools/read_note.wasm differ | ||
| new file mode 100644 | |||
| Binary files /dev/null and b/07-wasm-tools/tools/read_note.wasm differ | Binary files /dev/null and b/07-wasm-tools/tools/read_note.wasm differ | ||
added
07-wasm-tools/tools/save_note.wasm +0 -0 | new file mode 100644 | ||
| Binary files /dev/null and b/07-wasm-tools/tools/save_note.wasm differ | ||
| new file mode 100644 | |||
| Binary files /dev/null and b/07-wasm-tools/tools/save_note.wasm differ | Binary files /dev/null and b/07-wasm-tools/tools/save_note.wasm differ | ||
added
07-wasm-tools/tools/search_notes.wasm +0 -0 | new file mode 100644 | ||
| Binary files /dev/null and b/07-wasm-tools/tools/search_notes.wasm differ | ||
| new file mode 100644 | |||
| Binary files /dev/null and b/07-wasm-tools/tools/search_notes.wasm differ | Binary files /dev/null and b/07-wasm-tools/tools/search_notes.wasm differ | ||
added
07-wasm-tools/tools/word_stats.wasm +0 -0 | new file mode 100644 | ||
| Binary files /dev/null and b/07-wasm-tools/tools/word_stats.wasm differ | ||
| new file mode 100644 | |||
| Binary files /dev/null and b/07-wasm-tools/tools/word_stats.wasm differ | Binary files /dev/null and b/07-wasm-tools/tools/word_stats.wasm differ | ||