@binblink/wasmnest-plugin-sdk-desktop 0.0.1 → 0.0.2

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.
Files changed (219) hide show
  1. package/README.md +24 -4
  2. package/README_en.md +1 -1
  3. package/bin/cli.js +5 -5
  4. package/bin/manifest-gen.js +4 -0
  5. package/bin/manifest-validate.js +68 -6
  6. package/bin/permission-vocabulary.json +2 -2
  7. package/dev-shell/README.md +6 -5
  8. package/dev-shell/src/loader.ts +0 -5
  9. package/dev-shell/src/mock/commands.ts +1 -1
  10. package/dev-shell/src/mock/domain-context.ts +1 -1
  11. package/dev-shell/src/shell/types.ts +21 -32
  12. package/dev-shell/src/styles/shell.css +208 -2
  13. package/dev-shell/src/styles/style.css +72 -26
  14. package/dist/index.d.ts +44 -3
  15. package/dist/index.js +69 -1
  16. package/dist/types.d.ts +52 -5
  17. package/go/.gitignore +5 -0
  18. package/go/README.md +80 -0
  19. package/go/gen/wasmnest/api-call/host-api-call/empty.s +3 -0
  20. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wasm.go +13 -0
  21. package/go/gen/wasmnest/api-call/host-api-call/host-api-call.wit.go +34 -0
  22. package/go/gen/wasmnest/app/host-app/empty.s +3 -0
  23. package/go/gen/wasmnest/app/host-app/host-app.wasm.go +29 -0
  24. package/go/gen/wasmnest/app/host-app/host-app.wit.go +108 -0
  25. package/go/gen/wasmnest/auth-center/host-auth-center/abi.go +14 -0
  26. package/go/gen/wasmnest/auth-center/host-auth-center/empty.s +3 -0
  27. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wasm.go +49 -0
  28. package/go/gen/wasmnest/auth-center/host-auth-center/host-auth-center.wit.go +225 -0
  29. package/go/gen/wasmnest/clipboard/host-clipboard/abi.go +14 -0
  30. package/go/gen/wasmnest/clipboard/host-clipboard/empty.s +3 -0
  31. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wasm.go +17 -0
  32. package/go/gen/wasmnest/clipboard/host-clipboard/host-clipboard.wit.go +68 -0
  33. package/go/gen/wasmnest/connection-registry/host-connection-registry/empty.s +3 -0
  34. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wasm.go +13 -0
  35. package/go/gen/wasmnest/connection-registry/host-connection-registry/host-connection-registry.wit.go +50 -0
  36. package/go/gen/wasmnest/crypto/host-crypto/abi.go +18 -0
  37. package/go/gen/wasmnest/crypto/host-crypto/empty.s +3 -0
  38. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wasm.go +37 -0
  39. package/go/gen/wasmnest/crypto/host-crypto/host-crypto.wit.go +139 -0
  40. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/abi.go +14 -0
  41. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/empty.s +3 -0
  42. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wasm.go +21 -0
  43. package/go/gen/wasmnest/fs-desktop/host-fs-desktop/host-fs-desktop.wit.go +62 -0
  44. package/go/gen/wasmnest/http/host-http/abi.go +14 -0
  45. package/go/gen/wasmnest/http/host-http/empty.s +3 -0
  46. package/go/gen/wasmnest/http/host-http/host-http.wasm.go +13 -0
  47. package/go/gen/wasmnest/http/host-http/host-http.wit.go +40 -0
  48. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/empty.s +3 -0
  49. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wasm.go +17 -0
  50. package/go/gen/wasmnest/http-endpoint/host-http-endpoint/host-http-endpoint.wit.go +69 -0
  51. package/go/gen/wasmnest/mdns/host-mdns/empty.s +3 -0
  52. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wasm.go +29 -0
  53. package/go/gen/wasmnest/mdns/host-mdns/host-mdns.wit.go +101 -0
  54. package/go/gen/wasmnest/notify/host-notify/empty.s +3 -0
  55. package/go/gen/wasmnest/notify/host-notify/host-notify.wasm.go +13 -0
  56. package/go/gen/wasmnest/notify/host-notify/host-notify.wit.go +64 -0
  57. package/go/gen/wasmnest/peer/host-peer/abi.go +14 -0
  58. package/go/gen/wasmnest/peer/host-peer/empty.s +3 -0
  59. package/go/gen/wasmnest/peer/host-peer/host-peer.wasm.go +85 -0
  60. package/go/gen/wasmnest/peer/host-peer/host-peer.wit.go +357 -0
  61. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/empty.s +3 -0
  62. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wasm.go +21 -0
  63. package/go/gen/wasmnest/platform-desktop/host-platform-desktop/host-platform-desktop.wit.go +70 -0
  64. package/go/gen/wasmnest/plugin/abi-form/abi-form.exports.go +11 -0
  65. package/go/gen/wasmnest/plugin/abi-form/abi-form.wasm.go +13 -0
  66. package/go/gen/wasmnest/plugin/abi-form/abi-form.wit.go +7 -0
  67. package/go/gen/wasmnest/plugin/abi-form/empty.s +3 -0
  68. package/go/gen/wasmnest/plugin/command/command.exports.go +11 -0
  69. package/go/gen/wasmnest/plugin/command/command.wasm.go +19 -0
  70. package/go/gen/wasmnest/plugin/command/command.wit.go +6 -0
  71. package/go/gen/wasmnest/plugin/command/empty.s +3 -0
  72. package/go/gen/wasmnest/plugin/events/empty.s +3 -0
  73. package/go/gen/wasmnest/plugin/events/events.exports.go +52 -0
  74. package/go/gen/wasmnest/plugin/events/events.wasm.go +74 -0
  75. package/go/gen/wasmnest/plugin/events/events.wit.go +195 -0
  76. package/go/gen/wasmnest/plugin/host-bus/empty.s +3 -0
  77. package/go/gen/wasmnest/plugin/host-bus/host-bus.wasm.go +29 -0
  78. package/go/gen/wasmnest/plugin/host-bus/host-bus.wit.go +94 -0
  79. package/go/gen/wasmnest/plugin/host-config/abi.go +14 -0
  80. package/go/gen/wasmnest/plugin/host-config/empty.s +3 -0
  81. package/go/gen/wasmnest/plugin/host-config/host-config.wasm.go +13 -0
  82. package/go/gen/wasmnest/plugin/host-config/host-config.wit.go +21 -0
  83. package/go/gen/wasmnest/plugin/host-events/empty.s +3 -0
  84. package/go/gen/wasmnest/plugin/host-events/host-events.wasm.go +9 -0
  85. package/go/gen/wasmnest/plugin/host-events/host-events.wit.go +27 -0
  86. package/go/gen/wasmnest/plugin/host-fs/abi.go +14 -0
  87. package/go/gen/wasmnest/plugin/host-fs/empty.s +3 -0
  88. package/go/gen/wasmnest/plugin/host-fs/host-fs.wasm.go +33 -0
  89. package/go/gen/wasmnest/plugin/host-fs/host-fs.wit.go +86 -0
  90. package/go/gen/wasmnest/plugin/host-log/empty.s +3 -0
  91. package/go/gen/wasmnest/plugin/host-log/host-log.wasm.go +25 -0
  92. package/go/gen/wasmnest/plugin/host-log/host-log.wit.go +65 -0
  93. package/go/gen/wasmnest/plugin/host-metrics/empty.s +3 -0
  94. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wasm.go +13 -0
  95. package/go/gen/wasmnest/plugin/host-metrics/host-metrics.wit.go +34 -0
  96. package/go/gen/wasmnest/plugin/host-platform/empty.s +3 -0
  97. package/go/gen/wasmnest/plugin/host-platform/host-platform.wasm.go +21 -0
  98. package/go/gen/wasmnest/plugin/host-platform/host-platform.wit.go +85 -0
  99. package/go/gen/wasmnest/plugin/host-plugin-database/abi.go +14 -0
  100. package/go/gen/wasmnest/plugin/host-plugin-database/empty.s +3 -0
  101. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wasm.go +29 -0
  102. package/go/gen/wasmnest/plugin/host-plugin-database/host-plugin-database.wit.go +70 -0
  103. package/go/gen/wasmnest/plugin/host-storage/abi.go +14 -0
  104. package/go/gen/wasmnest/plugin/host-storage/empty.s +3 -0
  105. package/go/gen/wasmnest/plugin/host-storage/host-storage.wasm.go +21 -0
  106. package/go/gen/wasmnest/plugin/host-storage/host-storage.wit.go +44 -0
  107. package/go/gen/wasmnest/plugin/host-worker/empty.s +3 -0
  108. package/go/gen/wasmnest/plugin/host-worker/host-worker.wasm.go +17 -0
  109. package/go/gen/wasmnest/plugin/host-worker/host-worker.wit.go +50 -0
  110. package/go/gen/wasmnest/plugin/lifecycle/empty.s +3 -0
  111. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.exports.go +32 -0
  112. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wasm.go +41 -0
  113. package/go/gen/wasmnest/plugin/lifecycle/lifecycle.wit.go +9 -0
  114. package/go/gen/wasmnest/plugin/manifest/empty.s +3 -0
  115. package/go/gen/wasmnest/plugin/manifest/manifest.exports.go +11 -0
  116. package/go/gen/wasmnest/plugin/manifest/manifest.wasm.go +13 -0
  117. package/go/gen/wasmnest/plugin/manifest/manifest.wit.go +6 -0
  118. package/go/gen/wasmnest/plugin/plugin/plugin.wit.go +4 -0
  119. package/go/gen/wasmnest/process/host-process/empty.s +3 -0
  120. package/go/gen/wasmnest/process/host-process/host-process.wasm.go +21 -0
  121. package/go/gen/wasmnest/process/host-process/host-process.wit.go +56 -0
  122. package/go/gen/wasmnest/pty/host-pty/abi.go +14 -0
  123. package/go/gen/wasmnest/pty/host-pty/empty.s +3 -0
  124. package/go/gen/wasmnest/pty/host-pty/host-pty.wasm.go +33 -0
  125. package/go/gen/wasmnest/pty/host-pty/host-pty.wit.go +164 -0
  126. package/go/gen/wasmnest/task/host-task/abi.go +14 -0
  127. package/go/gen/wasmnest/task/host-task/empty.s +3 -0
  128. package/go/gen/wasmnest/task/host-task/host-task.wasm.go +29 -0
  129. package/go/gen/wasmnest/task/host-task/host-task.wit.go +110 -0
  130. package/go/gen/wasmnest/timer/host-timer/empty.s +3 -0
  131. package/go/gen/wasmnest/timer/host-timer/host-timer.wasm.go +13 -0
  132. package/go/gen/wasmnest/timer/host-timer/host-timer.wit.go +25 -0
  133. package/go/gen/wasmnest/ws/host-websocket/empty.s +3 -0
  134. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wasm.go +29 -0
  135. package/go/gen/wasmnest/ws/host-websocket/host-websocket.wit.go +109 -0
  136. package/go/gen/wasmnest/ws-server/host-websocket-server/empty.s +3 -0
  137. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wasm.go +49 -0
  138. package/go/gen/wasmnest/ws-server/host-websocket-server/host-websocket-server.wit.go +220 -0
  139. package/go/go.mod +14 -0
  140. package/go/go.sum +2 -0
  141. package/go/sdk/dispatch.go +49 -0
  142. package/go/sdk/dispatch_test.go +81 -0
  143. package/go/sdk/entry_native.go +13 -0
  144. package/go/sdk/entry_wasip2.go +188 -0
  145. package/go/sdk/host_http_wasip2.go +42 -0
  146. package/go/sdk/host_wasip2.go +251 -0
  147. package/go/sdk/plugin.go +103 -0
  148. package/go/testplugin/index.js +1 -0
  149. package/go/testplugin/main.go +67 -0
  150. package/go/testplugin/plugin.json +24 -0
  151. package/go/tools/assemble-wit.mjs +152 -0
  152. package/go/tools/gen-bindings.mjs +51 -0
  153. package/go/wit/core.wit +273 -0
  154. package/go/wit/deps/api-call/cap-api-call.wit +20 -0
  155. package/go/wit/deps/app/cap-app.wit +54 -0
  156. package/go/wit/deps/auth-center/cap-auth-center.wit +124 -0
  157. package/go/wit/deps/clipboard/clipboard.wit +48 -0
  158. package/go/wit/deps/clocks/monotonic-clock.wit +45 -0
  159. package/go/wit/deps/clocks/wall-clock.wit +42 -0
  160. package/go/wit/deps/clocks/world.wit +6 -0
  161. package/go/wit/deps/connection-registry/cap-connection-registry.wit +40 -0
  162. package/go/wit/deps/crypto/cap-crypto.wit +68 -0
  163. package/go/wit/deps/filesystem/preopens.wit +8 -0
  164. package/go/wit/deps/filesystem/types.wit +634 -0
  165. package/go/wit/deps/filesystem/world.wit +6 -0
  166. package/go/wit/deps/fs-desktop/cap-fs-desktop.wit +29 -0
  167. package/go/wit/deps/http/http.wit +28 -0
  168. package/go/wit/deps/http-endpoint/cap-http.wit +44 -0
  169. package/go/wit/deps/io/error.wit +34 -0
  170. package/go/wit/deps/io/poll.wit +41 -0
  171. package/go/wit/deps/io/streams.wit +262 -0
  172. package/go/wit/deps/io/world.wit +6 -0
  173. package/go/wit/deps/mdns/mdns.wit +45 -0
  174. package/go/wit/deps/notify/notify.wit +51 -0
  175. package/go/wit/deps/peer/peer.wit +126 -0
  176. package/go/wit/deps/platform-desktop/cap-platform-desktop.wit +38 -0
  177. package/go/wit/deps/process/cap-process.wit +25 -0
  178. package/go/wit/deps/pty/pty.wit +80 -0
  179. package/go/wit/deps/random/insecure-seed.wit +25 -0
  180. package/go/wit/deps/random/insecure.wit +22 -0
  181. package/go/wit/deps/random/random.wit +26 -0
  182. package/go/wit/deps/random/world.wit +7 -0
  183. package/go/wit/deps/sockets/instance-network.wit +9 -0
  184. package/go/wit/deps/sockets/ip-name-lookup.wit +51 -0
  185. package/go/wit/deps/sockets/network.wit +145 -0
  186. package/go/wit/deps/sockets/tcp-create-socket.wit +27 -0
  187. package/go/wit/deps/sockets/tcp.wit +353 -0
  188. package/go/wit/deps/sockets/udp-create-socket.wit +27 -0
  189. package/go/wit/deps/sockets/udp.wit +266 -0
  190. package/go/wit/deps/sockets/world.wit +11 -0
  191. package/go/wit/deps/task/cap-task.wit +56 -0
  192. package/go/wit/deps/timer/cap-timer.wit +14 -0
  193. package/go/wit/deps/wasi-cli/command.wit +7 -0
  194. package/go/wit/deps/wasi-cli/environment.wit +18 -0
  195. package/go/wit/deps/wasi-cli/exit.wit +4 -0
  196. package/go/wit/deps/wasi-cli/imports.wit +20 -0
  197. package/go/wit/deps/wasi-cli/run.wit +4 -0
  198. package/go/wit/deps/wasi-cli/stdio.wit +17 -0
  199. package/go/wit/deps/wasi-cli/terminal.wit +49 -0
  200. package/go/wit/deps/ws/ws.wit +50 -0
  201. package/go/wit/deps/ws-server/cap-ws.wit +99 -0
  202. package/go/wit/go-plugin.wit +28 -0
  203. package/go/wit/plugin-face.wit +355 -0
  204. package/go/wit/wasmnest.wit +25 -0
  205. package/package.json +12 -1
  206. package/src/index.ts +8 -0
  207. package/src/types.ts +681 -630
  208. package/src/ui/AppUpdateSection.d.ts +44 -0
  209. package/src/ui/AppUpdateSection.vue +253 -0
  210. package/src/ui/NumberField.d.ts +49 -0
  211. package/src/ui/NumberField.vue +390 -0
  212. package/src/ui/Select.vue +4 -4
  213. package/src/update.ts +123 -0
  214. package/template/README.md +39 -39
  215. package/tools/componentize/Cargo.lock +30 -1
  216. package/tools/componentize/Cargo.toml +3 -0
  217. package/tools/componentize/src/main.rs +197 -94
  218. package/dev-shell/src/mock/agent-hub-commands.ts +0 -1065
  219. package/dev-shell/src/mock/agent-hub.ts +0 -42
@@ -0,0 +1,81 @@
1
+ // 可移植面单测(原生 go test;组件面 entry_wasm/host_wasm 由 TinyGo 构建
2
+ // testplugin 验证——Testing Decisions:绑定生成相关改动以产出组件为准)。
3
+ package sdk
4
+
5
+ import (
6
+ "encoding/json"
7
+ "strings"
8
+ "testing"
9
+ )
10
+
11
+ func TestCommandTableUnknownCommandFailsVisibly(t *testing.T) {
12
+ table := CommandTable{"ping": func(json.RawMessage) (any, error) { return "pong", nil }}
13
+ _, err := table.Invoke("nope", json.RawMessage(`{}`))
14
+ if err == nil {
15
+ t.Fatal("未知命令必须显性报错(不静默返回空)")
16
+ }
17
+ if !strings.Contains(err.Error(), "unknown command: nope") {
18
+ t.Fatalf("错误须点名未知命令,实际: %v", err)
19
+ }
20
+ }
21
+
22
+ func TestCommandTableDispatch(t *testing.T) {
23
+ table := CommandTable{
24
+ "echo": func(args json.RawMessage) (any, error) { return string(args), nil },
25
+ }
26
+ got, err := table.Invoke("echo", json.RawMessage(`{"a":1}`))
27
+ if err != nil {
28
+ t.Fatalf("echo 不应报错: %v", err)
29
+ }
30
+ if got != `{"a":1}` {
31
+ t.Fatalf("载荷应原样透传,实际: %v", got)
32
+ }
33
+ }
34
+
35
+ func TestCommandResultJSONErrorShape(t *testing.T) {
36
+ got := commandResultJSON(nil, errString(`boom "quoted" \slash`))
37
+ var decoded map[string]string
38
+ if err := json.Unmarshal([]byte(got), &decoded); err != nil {
39
+ t.Fatalf("错误应答必须是合法 JSON(引号/反斜杠转义): %v → %s", err, got)
40
+ }
41
+ if decoded["error"] != `boom "quoted" \slash` {
42
+ t.Fatalf("错误串应经转义往返不损,实际: %s", got)
43
+ }
44
+ }
45
+
46
+ func TestCommandResultJSONValueShapes(t *testing.T) {
47
+ cases := []struct {
48
+ name string
49
+ v any
50
+ want string
51
+ }{
52
+ {"nil → null(Rust Value::Null 同形)", nil, "null"},
53
+ {"bool", true, "true"},
54
+ {"对象", map[string]any{"pong": true}, `{"pong":true}`},
55
+ }
56
+ for _, c := range cases {
57
+ t.Run(c.name, func(t *testing.T) {
58
+ if got := commandResultJSON(c.v, nil); got != c.want {
59
+ t.Fatalf("got %s want %s", got, c.want)
60
+ }
61
+ })
62
+ }
63
+ }
64
+
65
+ func TestMustJSONNonSerializableStaysValidJSON(t *testing.T) {
66
+ got := mustJSON(map[string]any{"ch": make(chan int)})
67
+ var decoded map[string]string
68
+ if err := json.Unmarshal([]byte(got), &decoded); err != nil {
69
+ t.Fatalf("不可编码值的兜底应答仍须是合法 JSON: %v → %s", err, got)
70
+ }
71
+ if !strings.Contains(decoded["error"], "not JSON-serializable") {
72
+ t.Fatalf("兜底应点名序列化失败,实际: %s", got)
73
+ }
74
+ }
75
+
76
+ // errString 是测试用的最小 error 实现(避免引入 errors 依赖本体已经足够薄)。
77
+ func errString(s string) error { return &staticErr{s} }
78
+
79
+ type staticErr struct{ msg string }
80
+
81
+ func (e *staticErr) Error() string { return e.msg }
@@ -0,0 +1,13 @@
1
+ //go:build !wasip2
2
+
3
+ // 原生目标的 Entry 桩:SDK 壳只对 TinyGo wasip2 组件目标有意义(wasmimport
4
+ // 绑定在原生目标不可链接)。原生构建(go test / go vet)编译到本文件只为
5
+ // 让 sdk 包的可移植面(dispatch 等)可测;若原生代码真调用了 Entry,
6
+ // 显性 panic 而非静默空转——fail-visible,不提供「看似接线实则没接」的假象。
7
+ // (正常应用只在被 TinyGo 编译的 init() 里调用 Entry,原生路径不可达。)
8
+ package sdk
9
+
10
+ // Entry 在原生目标恒 panic(见文件头注)。签名与 wasm 侧逐字一致。
11
+ func Entry(p Plugin) {
12
+ panic("sdk.Entry 仅在 TinyGo wasip2 组件目标可用;原生构建只支持 sdk 包的可移植单测面")
13
+ }
@@ -0,0 +1,188 @@
1
+ //go:build wasip2
2
+
3
+ // Entry:组件全部必选导出的一次性接线——Rust 侧 `wasm_entry!(T)` 宏的
4
+ // 对等物(issue #21 Phase 1;接线形态经 Phase 0 spike 实证:wit-bindgen-go
5
+ // 的 Exports 结构体函数字段赋值即完成导出实现)。
6
+ //
7
+ // 与 Rust 宏的语义对齐点:
8
+ // - 五必选导出全接线:manifest.get / abi-form.form / lifecycle 四函数 /
9
+ // command.invoke / events 七函数并集(桌面宿主只调 on-message /
10
+ // on-process-done,其余 5 个移动词汇对桌面原生插件是默认空实现);
11
+ // - guest 自报失败一律 log_warn 不 log_error(AGENTS §8 日志红线 ③:
12
+ // 宿主故障与插件自报 Err 分级,告警不因「插件返回了个 Err」失真);
13
+ // - 生命周期结果如实上抛(失败 → 宿主置失败态,不静默降级为已激活)。
14
+ package sdk
15
+
16
+ import (
17
+ "encoding/json"
18
+
19
+ "go.bytecodealliance.org/cm"
20
+
21
+ abiform "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/abi-form"
22
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/command"
23
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/events"
24
+ hostlog "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-log"
25
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/lifecycle"
26
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/manifest"
27
+ )
28
+
29
+ // 桌面组件形态值(cap-desktop.wit:form()==1;0 是已退役的自研 core ABI)
30
+ const abiFormComponent uint32 = 1
31
+
32
+ // result<_, string> 的 Go 形状(lifecycle / events 全部回调共用)
33
+ type unitResult = cm.Result[string, struct{}, string]
34
+
35
+ // current 是接线后的插件实例(Entry 调用即覆盖;正常应用整个生命周期只有一个)。
36
+ var current Plugin
37
+
38
+ // Entry 接线全部必选导出。应用在 init() 中调用:
39
+ //
40
+ // func init() { sdk.Entry(myPlugin{}) }
41
+ //
42
+ // 重复调用以后者覆盖(替换接线仅供测试复用同一组件产物)。
43
+ func Entry(p Plugin) {
44
+ current = p
45
+
46
+ // ==================== manifest / abi-form ====================
47
+
48
+ manifest.Exports.Get = func() string { return p.Manifest() }
49
+ abiform.Exports.Form = func() uint32 { return abiFormComponent }
50
+
51
+ // ==================== command(原 __wasmnest_invoke_command) ====================
52
+
53
+ command.Exports.Invoke = func(name string, argsJSON string) string {
54
+ v, err := p.InvokeCommand(name, json.RawMessage(argsJSON))
55
+ return commandResultJSON(v, err)
56
+ }
57
+
58
+ // ==================== lifecycle(原 activate/deactivate/on_startup/on_shutdown) ====================
59
+
60
+ lifecycle.Exports.Activate = func() unitResult {
61
+ if err := p.Activate(); err != nil {
62
+ hostlog.Warn("activate failed: " + err.Error())
63
+ return cm.Err[unitResult](err.Error())
64
+ }
65
+ hostlog.Info("Plugin activated (wasm)")
66
+ return cm.OK[unitResult](struct{}{})
67
+ }
68
+ lifecycle.Exports.Deactivate = func() unitResult {
69
+ if err := p.Deactivate(); err != nil {
70
+ hostlog.Warn("deactivate failed: " + err.Error())
71
+ return cm.Err[unitResult](err.Error())
72
+ }
73
+ return cm.OK[unitResult](struct{}{})
74
+ }
75
+ lifecycle.Exports.OnStartup = func() unitResult {
76
+ hook, ok := p.(StartupHook)
77
+ if !ok {
78
+ return cm.OK[unitResult](struct{}{})
79
+ }
80
+ if err := hook.OnStartup(); err != nil {
81
+ hostlog.Warn("on_startup failed: " + err.Error())
82
+ return cm.Err[unitResult](err.Error())
83
+ }
84
+ hostlog.Info("Plugin startup init completed")
85
+ return cm.OK[unitResult](struct{}{})
86
+ }
87
+ lifecycle.Exports.OnShutdown = func() unitResult {
88
+ hook, ok := p.(ShutdownHook)
89
+ if !ok {
90
+ return cm.OK[unitResult](struct{}{})
91
+ }
92
+ if err := hook.OnShutdown(); err != nil {
93
+ hostlog.Warn("on_shutdown failed: " + err.Error())
94
+ return cm.Err[unitResult](err.Error())
95
+ }
96
+ return cm.OK[unitResult](struct{}{})
97
+ }
98
+
99
+ // ==================== events(七函数并集;可选回调未实现即空实现) ====================
100
+
101
+ events.Exports.OnMessage = func(topic, sender, payloadJSON string) unitResult {
102
+ observer, ok := p.(MessageObserver)
103
+ if !ok {
104
+ return cm.OK[unitResult](struct{}{})
105
+ }
106
+ if err := observer.OnMessage(BusMessage{
107
+ Topic: topic,
108
+ Sender: sender,
109
+ Payload: json.RawMessage(payloadJSON),
110
+ Timestamp: 0,
111
+ }); err != nil {
112
+ hostlog.Warn("on_message failed: " + err.Error())
113
+ return cm.Err[unitResult](err.Error())
114
+ }
115
+ return cm.OK[unitResult](struct{}{})
116
+ }
117
+ events.Exports.OnProcessDone = func(payloadJSON string) unitResult {
118
+ observer, ok := p.(ProcessDoneObserver)
119
+ if !ok {
120
+ return cm.OK[unitResult](struct{}{})
121
+ }
122
+ if err := observer.OnProcessDone(payloadJSON); err != nil {
123
+ hostlog.Warn("on_process_done failed: " + err.Error())
124
+ return cm.Err[unitResult](err.Error())
125
+ }
126
+ return cm.OK[unitResult](struct{}{})
127
+ }
128
+ events.Exports.OnBusMessage = func(topic, payloadJSON string) unitResult {
129
+ observer, ok := p.(BusMessageObserver)
130
+ if !ok {
131
+ return cm.OK[unitResult](struct{}{})
132
+ }
133
+ if err := observer.OnBusMessage(BusMessage{
134
+ Topic: topic,
135
+ Sender: "", // 移动词汇无 sender 段(与 Rust 宏同形)
136
+ Payload: json.RawMessage(payloadJSON),
137
+ Timestamp: 0,
138
+ }); err != nil {
139
+ hostlog.Warn("on_bus_message failed: " + err.Error())
140
+ return cm.Err[unitResult](err.Error())
141
+ }
142
+ return cm.OK[unitResult](struct{}{})
143
+ }
144
+ events.Exports.OnAuthSuccess = func() unitResult {
145
+ observer, ok := p.(AuthSuccessObserver)
146
+ if !ok {
147
+ return cm.OK[unitResult](struct{}{})
148
+ }
149
+ if err := observer.OnAuthSuccess(); err != nil {
150
+ hostlog.Warn("on_auth_success failed: " + err.Error())
151
+ return cm.Err[unitResult](err.Error())
152
+ }
153
+ return cm.OK[unitResult](struct{}{})
154
+ }
155
+ events.Exports.OnDisconnect = func(reason string) unitResult {
156
+ observer, ok := p.(DisconnectObserver)
157
+ if !ok {
158
+ return cm.OK[unitResult](struct{}{})
159
+ }
160
+ if err := observer.OnDisconnect(reason); err != nil {
161
+ hostlog.Warn("on_disconnect failed: " + err.Error())
162
+ return cm.Err[unitResult](err.Error())
163
+ }
164
+ return cm.OK[unitResult](struct{}{})
165
+ }
166
+ events.Exports.OnSessionCreated = func(sessionID string) unitResult {
167
+ observer, ok := p.(SessionCreatedObserver)
168
+ if !ok {
169
+ return cm.OK[unitResult](struct{}{})
170
+ }
171
+ if err := observer.OnSessionCreated(sessionID); err != nil {
172
+ hostlog.Warn("on_session_created failed: " + err.Error())
173
+ return cm.Err[unitResult](err.Error())
174
+ }
175
+ return cm.OK[unitResult](struct{}{})
176
+ }
177
+ events.Exports.OnSessionStopped = func(sessionID string) unitResult {
178
+ observer, ok := p.(SessionStoppedObserver)
179
+ if !ok {
180
+ return cm.OK[unitResult](struct{}{})
181
+ }
182
+ if err := observer.OnSessionStopped(sessionID); err != nil {
183
+ hostlog.Warn("on_session_stopped failed: " + err.Error())
184
+ return cm.Err[unitResult](err.Error())
185
+ }
186
+ return cm.OK[unitResult](struct{}{})
187
+ }
188
+ }
@@ -0,0 +1,42 @@
1
+ //go:build wasip2
2
+
3
+ // 宿主能力域惯用包装:host-http(HTTP 出站代理)——随首个消费面(ai-chatbox
4
+ // Go 试点,issue #21 Solution 5)落 SDK,Rust 侧 `HostHttp::http_fetch` 的对等物。
5
+ // 其余能力域(ws / peer / pty / mdns / notify / …)仍经 gen 包直用,消费面出现再补。
6
+ package sdk
7
+
8
+ import (
9
+ "encoding/json"
10
+ "errors"
11
+ "fmt"
12
+
13
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/http/host-http"
14
+ )
15
+
16
+ // HostHTTPFetch 经宿主代理发起 HTTP 请求(需要 network:http 权限)。
17
+ //
18
+ // request 为任意 JSON 可序列化载荷(method / url / headers / body / stream /
19
+ // streamEvent / sseFormat,形状契约见 core.wit host-http 注释)。返回解包约定:
20
+ // - err != nil:请求不可序列化 / 宿主侧失败(权限、网络错误);
21
+ // - ok=false 且 err=nil:宿主返回空结果(合法空态,调用方显性处置);
22
+ // - ok=true:响应 JSON 原文(已校验合法性;流式请求为 `{streamId, streamEvent}`,
23
+ // 非流式为 `{status, body, headers}`)。
24
+ func HostHTTPFetch(request any) (json.RawMessage, bool, error) {
25
+ reqJSON, err := json.Marshal(request)
26
+ if err != nil {
27
+ return nil, false, fmt.Errorf("http_fetch: serialize request failed: %w", err)
28
+ }
29
+ res := hosthttp.Fetch(string(reqJSON))
30
+ if res.IsErr() {
31
+ return nil, false, errOf(res)
32
+ }
33
+ opt := res.OK()
34
+ if opt == nil || opt.None() {
35
+ return nil, false, nil
36
+ }
37
+ some := opt.Some()
38
+ if some == nil || !json.Valid([]byte(*some)) {
39
+ return nil, false, errors.New("http_fetch: response is not valid JSON")
40
+ }
41
+ return json.RawMessage(*some), true, nil
42
+ }
@@ -0,0 +1,251 @@
1
+ //go:build wasip2
2
+
3
+ // 宿主能力 Go 惯用包装——core.wit 九个内核机制接口(Rust 侧 `WasmHost` /
4
+ // `HostApi` 的对等物,issue #21 User Story 3)。能力域接口(ws / peer / pty /
5
+ // mdns / notify / …)首期经 gen 包直用(`gen/wasmnest/<pkg>/host-*`,ADR 0061 每域一包),惯用
6
+ // 包装随消费面出现再补(http 已落,见 host_http_wasip2.go)——不为没有调用者
7
+ // 的面预制 API。
8
+ //
9
+ // 返回形状约定(WIT result<T, string> 的 Go 落地):
10
+ // - result<option<string>, string> → (value string, ok bool, err error)
11
+ // (ok=false 且 err=nil = 键不存在 / 文件不存在等合法空态)
12
+ // - result<_, string> → error
13
+ // - result<string, string> / result<u32, string> / result<bool, string> → (value, error)
14
+ //
15
+ // 注意:每个 gen 包的 Shape 类型(如 OptionStringShape)是独立命名类型,
16
+ // 解包经对 Shape 泛型的内部辅助函数统一。
17
+ package sdk
18
+
19
+ import (
20
+ "errors"
21
+
22
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-bus"
23
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-config"
24
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-events"
25
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-fs"
26
+ hostlog "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-log"
27
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-metrics"
28
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-platform"
29
+ hostplugindatabase "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-plugin-database"
30
+ "github.com/7zai/wasmnest-plugin-sdk-go/gen/wasmnest/plugin/host-storage"
31
+ "go.bytecodealliance.org/cm"
32
+ )
33
+
34
+ // ==================== 内部解包辅助 ====================
35
+
36
+ // optString 解包 result<option<string>, string>:err 非 nil = 宿主侧 Err;
37
+ // ok=false 且 err=nil = 合法空态。
38
+ func optString[Shape any](res cm.Result[Shape, cm.Option[string], string]) (value string, ok bool, err error) {
39
+ if res.IsErr() {
40
+ return "", false, errOf(res)
41
+ }
42
+ opt := res.OK()
43
+ if opt == nil || opt.None() {
44
+ return "", false, nil
45
+ }
46
+ if some := opt.Some(); some != nil {
47
+ return *some, true, nil
48
+ }
49
+ return "", false, nil
50
+ }
51
+
52
+ // errOf 取 result 错误串(本 SDK 面内 Err 恒为 string)。
53
+ func errOf[Shape, OK any](res cm.Result[Shape, OK, string]) error {
54
+ if e := res.Err(); e != nil {
55
+ return errors.New(*e)
56
+ }
57
+ return nil
58
+ }
59
+
60
+ // unitErr 解包 result<_, string> 为 error。
61
+ func unitErr(res cm.Result[string, struct{}, string]) error {
62
+ if res.IsErr() {
63
+ return errOf(res)
64
+ }
65
+ return nil
66
+ }
67
+
68
+ // ==================== host-log(日志与插件状态上报) ====================
69
+
70
+ // HostLogInfo / HostLogDebug / HostLogWarn / HostLogError 写宿主日志。
71
+ // 纪律(AGENTS §8 日志红线):guest 自报的可处理错误用 Warn 不用 Error。
72
+ func HostLogInfo(message string) { hostlog.Info(message) }
73
+ func HostLogDebug(message string) { hostlog.Debug(message) }
74
+ func HostLogWarn(message string) { hostlog.Warn(message) }
75
+ func HostLogError(message string) { hostlog.Error(message) }
76
+
77
+ // HostMarkPluginError 上报插件错误态(宿主计入插件健康面)。
78
+ func HostMarkPluginError(err string) { hostlog.MarkPluginError(err) }
79
+
80
+ // ==================== host-config(宿主配置读取) ====================
81
+
82
+ // HostConfigGet 读宿主配置键(key 为 ConfigKey::as_str 串,如 home_dir)。
83
+ func HostConfigGet(key string) (string, bool, error) {
84
+ return optString(hostconfig.Get(key))
85
+ }
86
+
87
+ // ==================== host-storage(插件 KV 存储) ====================
88
+
89
+ // HostStorageGet 读插件 KV;ok=false = 键不存在。
90
+ func HostStorageGet(key string) (string, bool, error) {
91
+ return optString(hoststorage.Get(key))
92
+ }
93
+
94
+ // HostStorageSet 写插件 KV(覆盖写)。
95
+ func HostStorageSet(key, value string) error { return unitErr(hoststorage.Set(key, value)) }
96
+
97
+ // HostStorageDelete 删插件 KV(键不存在也视为成功,幂等)。
98
+ func HostStorageDelete(key string) error { return unitErr(hoststorage.Delete(key)) }
99
+
100
+ // ==================== host-events(事件发射) ====================
101
+
102
+ // HostEventEmit 发射插件事件(无返回值;载荷非法等失败由宿主侧记录)。
103
+ func HostEventEmit(eventName, payloadJSON string) { hostevents.Emit(eventName, payloadJSON) }
104
+
105
+ // ==================== host-bus(消息总线) ====================
106
+
107
+ // HostBusPublish 发布 JSON 载荷总线消息(topic 命名空间纪律见 core.wit
108
+ // host-bus 注释:私有 `<plugin-id>::<name>` / 公开 `task:*` 等)。
109
+ func HostBusPublish(topic, payloadJSON string) error {
110
+ return unitErr(hostbus.Publish(topic, payloadJSON))
111
+ }
112
+
113
+ // HostBusPublishBinary 发布二进制载荷(零 JSON 编解码,可传非 UTF-8 与大载荷;
114
+ // 订阅方须以 HostBusSubscribeBinary 声明偏好)。
115
+ func HostBusPublishBinary(topic string, payload []byte) error {
116
+ return unitErr(hostbus.PublishBinary(topic, cm.ToList(payload)))
117
+ }
118
+
119
+ // HostBusSubscribe 订阅 topic(JSON 格式)。
120
+ func HostBusSubscribe(topic string) error { return unitErr(hostbus.Subscribe(topic)) }
121
+
122
+ // HostBusSubscribeBinary 以二进制格式偏好订阅(只收 publish-binary 投递)。
123
+ func HostBusSubscribeBinary(topic string) error { return unitErr(hostbus.SubscribeBinary(topic)) }
124
+
125
+ // HostBusUnsubscribe 退订。
126
+ func HostBusUnsubscribe(topic string) error { return unitErr(hostbus.Unsubscribe(topic)) }
127
+
128
+ // ==================== host-plugin-database(插件独立数据库) ====================
129
+
130
+ // HostDBExecute 执行 SQL,返回受影响行数。
131
+ func HostDBExecute(sql string) (uint32, error) {
132
+ res := hostplugindatabase.Execute(sql)
133
+ if res.IsErr() {
134
+ return 0, errOf(res)
135
+ }
136
+ if v := res.OK(); v != nil {
137
+ return *v, nil
138
+ }
139
+ return 0, nil
140
+ }
141
+
142
+ // HostDBQuery 查询 SQL,返回结果 JSON(ok=false = 无结果行)。
143
+ func HostDBQuery(sql string) (string, bool, error) {
144
+ return optString(hostplugindatabase.Query(sql))
145
+ }
146
+
147
+ // HostDBExecuteParams 参数化执行(paramsJSON 形如 `[?, ?, …]` 位置参数数组)。
148
+ func HostDBExecuteParams(sql, paramsJSON string) (uint32, error) {
149
+ res := hostplugindatabase.ExecuteParams(sql, paramsJSON)
150
+ if res.IsErr() {
151
+ return 0, errOf(res)
152
+ }
153
+ if v := res.OK(); v != nil {
154
+ return *v, nil
155
+ }
156
+ return 0, nil
157
+ }
158
+
159
+ // HostDBQueryParams 参数化查询。
160
+ func HostDBQueryParams(sql, paramsJSON string) (string, bool, error) {
161
+ return optString(hostplugindatabase.QueryParams(sql, paramsJSON))
162
+ }
163
+
164
+ // HostDBExecuteBatch 事务内顺序执行多语句(sqlsJSON 为 SQL 数组),返回合计行数。
165
+ func HostDBExecuteBatch(sqlsJSON string) (uint32, error) {
166
+ res := hostplugindatabase.ExecuteBatch(sqlsJSON)
167
+ if res.IsErr() {
168
+ return 0, errOf(res)
169
+ }
170
+ if v := res.OK(); v != nil {
171
+ return *v, nil
172
+ }
173
+ return 0, nil
174
+ }
175
+
176
+ // ==================== host-metrics(运行监控快照) ====================
177
+
178
+ // HostMetricsSnapshot 只读运行指标快照 JSON(权限 metrics:read;快照源未装配
179
+ // 时显性报错,不返回空对象冒充)。
180
+ func HostMetricsSnapshot() (string, error) {
181
+ res := hostmetrics.Snapshot()
182
+ if res.IsErr() {
183
+ return "", errOf(res)
184
+ }
185
+ if v := res.OK(); v != nil {
186
+ return *v, nil
187
+ }
188
+ return "", nil
189
+ }
190
+
191
+ // ==================== host-fs(文件系统交集 6 原语) ====================
192
+
193
+ // HostFSRead 读文本文件(ok=false = 文件不存在;路径未授权等显性报错)。
194
+ func HostFSRead(path string) (string, bool, error) { return optString(hostfs.Read(path)) }
195
+
196
+ // HostFSWrite 写文本文件。
197
+ func HostFSWrite(path, data string) error { return unitErr(hostfs.Write(path, data)) }
198
+
199
+ // HostFSCopy 复制文件。
200
+ func HostFSCopy(src, dst string) error { return unitErr(hostfs.Copy(src, dst)) }
201
+
202
+ // HostFSDelete 删除文件。
203
+ func HostFSDelete(path string) error { return unitErr(hostfs.Delete(path)) }
204
+
205
+ // HostFSExists 探测存在性。
206
+ func HostFSExists(path string) (bool, error) {
207
+ res := hostfs.Exists(path)
208
+ if res.IsErr() {
209
+ return false, errOf(res)
210
+ }
211
+ if v := res.OK(); v != nil {
212
+ return *v, nil
213
+ }
214
+ return false, nil
215
+ }
216
+
217
+ // HostFSRequestAuth 批量请求目录授权(pathsJSON 为绝对路径数组;返回是否
218
+ // 全部同意——用户拒绝时显性 false,不静默降级为空授权)。
219
+ func HostFSRequestAuth(pathsJSON string) (bool, error) {
220
+ res := hostfs.RequestAuth(pathsJSON)
221
+ if res.IsErr() {
222
+ return false, errOf(res)
223
+ }
224
+ if v := res.OK(); v != nil {
225
+ return *v, nil
226
+ }
227
+ return false, nil
228
+ }
229
+
230
+ // ==================== host-platform(平台能力交集) ====================
231
+
232
+ // HostPickFiles 系统多文件选择器 → string[] JSON(用户取消为空数组;
233
+ // 权限 fs:pick + 选择后授权校验,见 core.wit)。
234
+ func HostPickFiles() (string, error) { return stringResult(hostplatform.PickFiles()) }
235
+
236
+ // HostPickFolders 系统文件夹选择器(多选)→ string[] JSON。
237
+ func HostPickFolders() (string, error) { return stringResult(hostplatform.PickFolders()) }
238
+
239
+ // HostPlatformName 宿主平台标识(desktop / mobile;无权限门)。
240
+ func HostPlatformName() string { return hostplatform.Platform() }
241
+
242
+ // stringResult 解包 result<string, string>。
243
+ func stringResult(res cm.Result[string, string, string]) (string, error) {
244
+ if res.IsErr() {
245
+ return "", errOf(res)
246
+ }
247
+ if v := res.OK(); v != nil {
248
+ return *v, nil
249
+ }
250
+ return "", nil
251
+ }
@@ -0,0 +1,103 @@
1
+ // Package sdk 是 WasmNest 插件的 Go 壳——Rust 侧 `wasmnest-plugin-api`
2
+ // (`WasmPlugin` trait + `wasm_entry!` 宏)的对等物(issue #21 Phase 1)。
3
+ //
4
+ // 应用后端实现 [Plugin] 接口并在 init() 调用 [Entry],即完成组件全部
5
+ // 必选导出(command / lifecycle / manifest / events / abi-form)的接线;
6
+ // 可选回调经接口升级(本文件末尾的 Observer 接口)按需实现,未实现的
7
+ // 回调导出空实现(宿主按名探测,与 Rust 侧 trait 默认实现同语义)。
8
+ //
9
+ // 文件分两类(原生 `go test ./sdk` 只编译可移植文件,wasmimport 重定位
10
+ // 在原生目标不可链接——Phase 0 spike 实证):
11
+ // - 可移植(无 build tag):本文件、dispatch.go——纯 Go 逻辑,原生可测;
12
+ // - 组件目标(//go:build wasip2):entry_wasip2.go、host_wasip2.go——引用
13
+ // gen/ 绑定,仅 TinyGo `-target=wasip2` 构建参与编译。
14
+ // 文件名后缀刻意用 `wasip2` 而非 `wasm`:`_wasm` 是 GOARCH 隐式约束
15
+ // (与 //go:build AND 组合),在本目标(GOARCH=arm/amd64)下会把文件
16
+ // 无条件排除——`wasip2` 不是 GOOS/GOARCH 值,无隐式约束。
17
+ //
18
+ // 可选探测导出(events-binary / events-ws / events-task / auth-policy)
19
+ // 首期不提供:宿主按名探测、缺失容忍(spec #21 Implementation Decisions)。
20
+ package sdk
21
+
22
+ import "encoding/json"
23
+
24
+ // Plugin 是 Go 后端应用必须实现的核心接口(Rust `WasmPlugin` 必选方法的对等物)。
25
+ type Plugin interface {
26
+ // Manifest 返回插件清单 JSON(plugin.json 内容,原样字符串)。
27
+ Manifest() string
28
+
29
+ // Activate 激活插件;返回错误时宿主将插件置为失败态(不静默降级)。
30
+ Activate() error
31
+
32
+ // Deactivate 停用插件;返回错误仅记录日志,不影响宿主停用流程。
33
+ Deactivate() error
34
+
35
+ // InvokeCommand 调用自定义命令。args 为原始 JSON 载荷(不预解码——
36
+ // 无效 JSON 由插件自行 Unmarshal 并显性失败,不静默替换为 null);
37
+ // 返回值经 JSON 序列化回传前端,错误序列化为 `{"error":"…"}` 形状
38
+ // (与 Rust 侧 wasm_entry! 的命令应答口径一致)。
39
+ // 未知命令必须显性报错(spec #21 User Story 5)——用 [CommandTable]
40
+ // 即自动获得该纪律。
41
+ InvokeCommand(name string, args json.RawMessage) (any, error)
42
+ }
43
+
44
+ // BusMessage 是总线消息的可移植形状(Rust SDK `BusMessage` 的对等物;
45
+ // ABI 三段字符串在 entry_wasm.go 装配,timestamp 当前 ABI 未传递、恒 0)。
46
+ type BusMessage struct {
47
+ Topic string `json:"topic"`
48
+ Sender string `json:"sender"`
49
+ Payload json.RawMessage `json:"payload"`
50
+ Timestamp uint64 `json:"timestamp"`
51
+ }
52
+
53
+ // ==================== 可选回调(接口升级,等价 Rust trait 默认实现) ====================
54
+
55
+ // StartupHook 应用启动完成回调(建表 / 注册扩展点等;错误 → 宿主置 Degraded)。
56
+ type StartupHook interface {
57
+ OnStartup() error
58
+ }
59
+
60
+ // ShutdownHook 应用即将关闭回调(错误仅记录日志)。
61
+ type ShutdownHook interface {
62
+ OnShutdown() error
63
+ }
64
+
65
+ // MessageObserver 接收总线消息(桌面词汇;订阅须先经 HostBusSubscribe)。
66
+ type MessageObserver interface {
67
+ OnMessage(msg BusMessage) error
68
+ }
69
+
70
+ // ProcessDoneObserver 接收进程执行完成事件(host-process run 的异步回执,
71
+ // payloadJSON 形如 `{"run_id":…,"exit_code":…,"timed_out":…}`)。
72
+ type ProcessDoneObserver interface {
73
+ OnProcessDone(payloadJSON string) error
74
+ }
75
+
76
+ // 以下 5 个为移动词汇回调(events 七函数并集,桌面宿主不调用):
77
+ // 双端共享产物按桌面 world 构建时同时导出,桌面原生插件可不实现。
78
+
79
+ // BusMessageObserver 移动词汇总线消息回调(无 sender 段;与 OnMessage 汇流由
80
+ // 共享产物自行决定)。
81
+ type BusMessageObserver interface {
82
+ OnBusMessage(msg BusMessage) error
83
+ }
84
+
85
+ // AuthSuccessObserver 移动词汇:WS 认证成功回调。
86
+ type AuthSuccessObserver interface {
87
+ OnAuthSuccess() error
88
+ }
89
+
90
+ // DisconnectObserver 移动词汇:WS 断开回调(reason 为人类可读原因)。
91
+ type DisconnectObserver interface {
92
+ OnDisconnect(reason string) error
93
+ }
94
+
95
+ // SessionCreatedObserver 移动词汇:会话创建回调。
96
+ type SessionCreatedObserver interface {
97
+ OnSessionCreated(sessionID string) error
98
+ }
99
+
100
+ // SessionStoppedObserver 移动词汇:会话停止回调。
101
+ type SessionStoppedObserver interface {
102
+ OnSessionStopped(sessionID string) error
103
+ }
@@ -0,0 +1 @@
1
+ // Go SDK 验证组件前端桩(纯后端形态,产物目录布局要求存在)