//go:build js && wasm // Command agiwasm is the playground engine compiled to WebAssembly. It runs // in a Web Worker (static/playground/worker.js) and exposes // globalThis.agiPlayground: // // run(requestJSON, files, onEvent) -> Promise<{json, files}> // wsPush(text), wsClose() // frames typed in the page during a run // defaults() -> JSON // the mock answers the tool windows start from package main import ( "encoding/json" "syscall/js" "imuslab.com/arozos/agidocgen/playground" ) var live chan playground.WSIn func main() { api := js.Global().Get("Object").New() api.Set("run", js.FuncOf(run)) api.Set("wsPush", js.FuncOf(func(this js.Value, args []js.Value) interface{} { send(playground.WSIn{Text: args[0].String()}) return nil })) api.Set("wsClose", js.FuncOf(func(this js.Value, args []js.Value) interface{} { send(playground.WSIn{Close: true}) return nil })) api.Set("defaults", js.FuncOf(func(this js.Value, args []js.Value) interface{} { b, _ := json.Marshal(map[string]interface{}{"cnn": playground.CNNDefaults, "nativeLibs": playground.NativeLibs}) return string(b) })) js.Global().Set("agiPlayground", api) select {} } func send(in playground.WSIn) { if live == nil { return } select { case live <- in: default: } } // run decodes the request, runs it on its own goroutine (so delay() and // websocket reads can wait without blocking the worker) and resolves with // the result JSON plus the written files as Uint8Arrays. func run(this js.Value, args []js.Value) interface{} { reqJSON, files, onEvent := args[0].String(), args[1], args[2] var handler js.Func handler = js.FuncOf(func(this js.Value, p []js.Value) interface{} { resolve := p[0] go func() { defer handler.Release() var req playground.Request if err := json.Unmarshal([]byte(reqJSON), &req); err != nil { b, _ := json.Marshal(map[string]interface{}{"error": map[string]string{"kind": "error", "message": "bad request: " + err.Error()}}) resolve.Invoke(map[string]interface{}{"json": string(b), "files": js.Global().Get("Array").New()}) return } for i := 0; i < files.Length(); i++ { f := files.Index(i) data := f.Get("data") var b []byte if !data.IsUndefined() && !data.IsNull() { b = make([]byte, data.Get("length").Int()) js.CopyBytesToGo(b, data) } mt := 0.0 if v := f.Get("mtime"); v.Type() == js.TypeNumber { mt = v.Float() } req.Files = append(req.Files, playground.File{Path: f.Get("path").String(), Dir: f.Get("dir").Truthy(), Data: b, Mtime: int64(mt)}) } live = make(chan playground.WSIn, 64) req.Live = live emit := func(kind string, v interface{}) { if onEvent.Type() != js.TypeFunction { return } b, _ := json.Marshal(v) onEvent.Invoke(kind, string(b)) } res := playground.Run(req, playground.Hooks{ Log: func(l playground.LogLine) { emit("log", l) }, WS: func(f playground.WSFrame) { emit("ws", f) }, }) live = nil out := js.Global().Get("Array").New() for _, f := range res.Written { o := js.Global().Get("Object").New() o.Set("path", f.Path) o.Set("dir", f.Dir) o.Set("mtime", f.Mtime) if !f.Dir { u := js.Global().Get("Uint8Array").New(len(f.Data)) js.CopyBytesToJS(u, f.Data) o.Set("data", u) } out.Call("push", o) } for i := range res.Written { res.Written[i].Data = nil } b, _ := json.Marshal(res) resolve.Invoke(map[string]interface{}{"json": string(b), "files": out}) }() return nil }) return js.Global().Get("Promise").New(handler) }