main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //go:build js && wasm
  2. // Command agiwasm is the playground engine compiled to WebAssembly. It runs
  3. // in a Web Worker (static/playground/worker.js) and exposes
  4. // globalThis.agiPlayground:
  5. //
  6. // run(requestJSON, files, onEvent) -> Promise<{json, files}>
  7. // wsPush(text), wsClose() // frames typed in the page during a run
  8. // defaults() -> JSON // the mock answers the tool windows start from
  9. package main
  10. import (
  11. "encoding/json"
  12. "syscall/js"
  13. "imuslab.com/arozos/agidocgen/playground"
  14. )
  15. var live chan playground.WSIn
  16. func main() {
  17. api := js.Global().Get("Object").New()
  18. api.Set("run", js.FuncOf(run))
  19. api.Set("wsPush", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
  20. send(playground.WSIn{Text: args[0].String()})
  21. return nil
  22. }))
  23. api.Set("wsClose", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
  24. send(playground.WSIn{Close: true})
  25. return nil
  26. }))
  27. api.Set("defaults", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
  28. b, _ := json.Marshal(map[string]interface{}{"cnn": playground.CNNDefaults, "nativeLibs": playground.NativeLibs})
  29. return string(b)
  30. }))
  31. js.Global().Set("agiPlayground", api)
  32. select {}
  33. }
  34. func send(in playground.WSIn) {
  35. if live == nil {
  36. return
  37. }
  38. select {
  39. case live <- in:
  40. default:
  41. }
  42. }
  43. // run decodes the request, runs it on its own goroutine (so delay() and
  44. // websocket reads can wait without blocking the worker) and resolves with
  45. // the result JSON plus the written files as Uint8Arrays.
  46. func run(this js.Value, args []js.Value) interface{} {
  47. reqJSON, files, onEvent := args[0].String(), args[1], args[2]
  48. var handler js.Func
  49. handler = js.FuncOf(func(this js.Value, p []js.Value) interface{} {
  50. resolve := p[0]
  51. go func() {
  52. defer handler.Release()
  53. var req playground.Request
  54. if err := json.Unmarshal([]byte(reqJSON), &req); err != nil {
  55. b, _ := json.Marshal(map[string]interface{}{"error": map[string]string{"kind": "error", "message": "bad request: " + err.Error()}})
  56. resolve.Invoke(map[string]interface{}{"json": string(b), "files": js.Global().Get("Array").New()})
  57. return
  58. }
  59. for i := 0; i < files.Length(); i++ {
  60. f := files.Index(i)
  61. data := f.Get("data")
  62. var b []byte
  63. if !data.IsUndefined() && !data.IsNull() {
  64. b = make([]byte, data.Get("length").Int())
  65. js.CopyBytesToGo(b, data)
  66. }
  67. mt := 0.0
  68. if v := f.Get("mtime"); v.Type() == js.TypeNumber {
  69. mt = v.Float()
  70. }
  71. req.Files = append(req.Files, playground.File{Path: f.Get("path").String(), Dir: f.Get("dir").Truthy(), Data: b, Mtime: int64(mt)})
  72. }
  73. live = make(chan playground.WSIn, 64)
  74. req.Live = live
  75. emit := func(kind string, v interface{}) {
  76. if onEvent.Type() != js.TypeFunction {
  77. return
  78. }
  79. b, _ := json.Marshal(v)
  80. onEvent.Invoke(kind, string(b))
  81. }
  82. res := playground.Run(req, playground.Hooks{
  83. Log: func(l playground.LogLine) { emit("log", l) },
  84. WS: func(f playground.WSFrame) { emit("ws", f) },
  85. })
  86. live = nil
  87. out := js.Global().Get("Array").New()
  88. for _, f := range res.Written {
  89. o := js.Global().Get("Object").New()
  90. o.Set("path", f.Path)
  91. o.Set("dir", f.Dir)
  92. o.Set("mtime", f.Mtime)
  93. if !f.Dir {
  94. u := js.Global().Get("Uint8Array").New(len(f.Data))
  95. js.CopyBytesToJS(u, f.Data)
  96. o.Set("data", u)
  97. }
  98. out.Call("push", o)
  99. }
  100. for i := range res.Written {
  101. res.Written[i].Data = nil
  102. }
  103. b, _ := json.Marshal(res)
  104. resolve.Invoke(map[string]interface{}{"json": string(b), "files": out})
  105. }()
  106. return nil
  107. })
  108. return js.Global().Get("Promise").New(handler)
  109. }