nextclaw 0.45.5 → 0.47.0

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 (159) hide show
  1. package/dist/cli/app/index.js +2 -2
  2. package/dist/cli/launcher/index.js +10 -4
  3. package/dist/cli/launcher/index.js.map +1 -1
  4. package/dist/distribution-BAU3XUQJ.js +2 -0
  5. package/dist/{nextclaw-distribution.utils-C6_UWes0.js → distribution-CND3AWXk.js} +1 -1
  6. package/dist/distribution-CND3AWXk.js.map +1 -0
  7. package/dist/{nextclaw-cli-app-Bwa2L2Jv.js → nextclaw-cli-app-wU9aUEKj.js} +767 -74
  8. package/dist/nextclaw-cli-app-wU9aUEKj.js.map +1 -0
  9. package/package.json +12 -12
  10. package/resources/USAGE.md +118 -113
  11. package/resources/apps/nextclaw-github-issue-watcher/README.md +43 -0
  12. package/resources/apps/nextclaw-github-issue-watcher/guest/Cargo.lock +455 -0
  13. package/resources/apps/nextclaw-github-issue-watcher/guest/Cargo.toml +27 -0
  14. package/resources/apps/nextclaw-github-issue-watcher/guest/src/bindings.rs +11177 -0
  15. package/resources/apps/nextclaw-github-issue-watcher/guest/src/lib.rs +307 -0
  16. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/clocks@0.2.6/monotonic-clock.wit +50 -0
  17. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/clocks@0.2.6/timezone.wit +55 -0
  18. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/clocks@0.2.6/wall-clock.wit +46 -0
  19. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/clocks@0.2.6/world.wit +11 -0
  20. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/config@0.2.0-draft-2024-09-27/package.wit +1 -0
  21. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/config@0.2.0-draft-2024-09-27/store.wit +9 -0
  22. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/http@0.2.6/handler.wit +49 -0
  23. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/http@0.2.6/package.wit +1 -0
  24. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/http@0.2.6/types.wit +688 -0
  25. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/io@0.2.6/error.wit +34 -0
  26. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/io@0.2.6/poll.wit +47 -0
  27. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/io@0.2.6/streams.wit +290 -0
  28. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/io@0.2.6/world.wit +10 -0
  29. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/deps/keyvalue@0.2.0-draft2/store.wit +124 -0
  30. package/resources/apps/nextclaw-github-issue-watcher/guest/wit/portable-service.wit +56 -0
  31. package/resources/apps/nextclaw-github-issue-watcher/manifest.json +34 -0
  32. package/resources/apps/nextclaw-github-issue-watcher/marketplace.json +18 -0
  33. package/resources/apps/nextclaw-github-issue-watcher/panels/nextclaw-github-issue-watcher-panel.panel/assets/app.js +148 -0
  34. package/resources/apps/nextclaw-github-issue-watcher/panels/nextclaw-github-issue-watcher-panel.panel/index.html +190 -0
  35. package/resources/apps/nextclaw-github-issue-watcher/panels/nextclaw-github-issue-watcher-panel.panel/panel-app.json +11 -0
  36. package/resources/apps/nextclaw-github-issue-watcher/service-components/nextclaw-github-issue-watcher-service/service-app.json +36 -0
  37. package/resources/apps/nextclaw-github-issue-watcher/service-components/nextclaw-github-issue-watcher-service/service.wasm +0 -0
  38. package/resources/apps/nextclaw-github-issue-watcher/tests/public-github-runner-smoke.tools.mjs +150 -0
  39. package/resources/apps/nextclaw-github-issue-watcher/tests/service-smoke.json +12 -0
  40. package/resources/apps/nextclaw-portable-runtime-lab/README.md +5 -3
  41. package/resources/apps/nextclaw-portable-runtime-lab/manifest.json +14 -2
  42. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-contacts.panel/assets/app.js +21 -0
  43. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-contacts.panel/index.html +1 -0
  44. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-focus.panel/assets/app.js +21 -0
  45. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-focus.panel/index.html +1 -0
  46. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-notes.panel/assets/app.js +21 -0
  47. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-notes.panel/index.html +1 -0
  48. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-panel.panel/assets/app.js +201 -0
  49. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-panel.panel/assets/style.css +42 -0
  50. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-panel.panel/index.html +40 -19
  51. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-panel.panel/panel-app.json +2 -2
  52. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-todos.panel/assets/app.js +21 -0
  53. package/resources/apps/nextclaw-portable-runtime-lab/panels/nextclaw-portable-runtime-lab-todos.panel/index.html +1 -0
  54. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-capabilities/service-app.json +110 -3
  55. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-capabilities/service.wasm +0 -0
  56. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-composition/service-app.json +12 -3
  57. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-composition/service.wasm +0 -0
  58. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-provider/service-app.json +11 -0
  59. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-provider/service.wasm +0 -0
  60. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-resident/service.wasm +0 -0
  61. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-sqlite/service-app.json +37 -0
  62. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-sqlite/service.wasm +0 -0
  63. package/resources/apps/nextclaw-portable-runtime-lab/service-components/nextclaw-portable-runtime-lab-state/service.wasm +0 -0
  64. package/ui-dist/assets/{api-DU3C4HfX.js → api-n96IfCKb.js} +3 -3
  65. package/ui-dist/assets/api-n96IfCKb.js.gz +0 -0
  66. package/ui-dist/assets/{appearance-settings-page-CoB3e64T.js → appearance-settings-page-C_AqxSF_.js} +1 -1
  67. package/ui-dist/assets/appearance-settings-page-C_AqxSF_.js.gz +0 -0
  68. package/ui-dist/assets/{book-open-DMnwJfVM.js → book-open-DYRapW_h.js} +1 -1
  69. package/ui-dist/assets/{channels-list-page-BhIoYsyo.js → channels-list-page-uR-M4Ls8.js} +1 -1
  70. package/ui-dist/assets/channels-list-page-uR-M4Ls8.js.gz +0 -0
  71. package/ui-dist/assets/{config-split-page-DON62DcK.js → config-split-page-DJDp86Ne.js} +1 -1
  72. package/ui-dist/assets/config-split-page-DJDp86Ne.js.gz +0 -0
  73. package/ui-dist/assets/{confirm-dialog-B539oJpr.js → confirm-dialog-CHVBMpla.js} +1 -1
  74. package/ui-dist/assets/confirm-dialog-CHVBMpla.js.gz +0 -0
  75. package/ui-dist/assets/{createLucideIcon-CLfqNlvk.js → createLucideIcon-CgM0rnsa.js} +1 -1
  76. package/ui-dist/assets/{desktop-capabilities-page-BgGZ1I8q.js → desktop-capabilities-page-phMuNq1B.js} +1 -1
  77. package/ui-dist/assets/desktop-capabilities-page-phMuNq1B.js.gz +0 -0
  78. package/ui-dist/assets/{desktop-update-config-TrA-h5OG.js → desktop-update-config-C77hkbor.js} +1 -1
  79. package/ui-dist/assets/desktop-update-config-C77hkbor.js.gz +0 -0
  80. package/ui-dist/assets/{dist-GWWehc4w.js → dist-BjYvUhPL.js} +1 -1
  81. package/ui-dist/assets/dist-BjYvUhPL.js.gz +0 -0
  82. package/ui-dist/assets/{dist-WPei0-v8.js → dist-N9zc5bkM.js} +1 -1
  83. package/ui-dist/assets/dist-N9zc5bkM.js.gz +0 -0
  84. package/ui-dist/assets/{doc-browser-CKpps-rb.js → doc-browser-DwRLokCI.js} +1 -1
  85. package/ui-dist/assets/doc-browser-DwRLokCI.js.gz +0 -0
  86. package/ui-dist/assets/doc-browser-JlXVLNRe.js +1 -0
  87. package/ui-dist/assets/{doc-browser-context-Vx418ptC.js → doc-browser-context-BDuypObS.js} +1 -1
  88. package/ui-dist/assets/doc-browser-context-BDuypObS.js.gz +0 -0
  89. package/ui-dist/assets/{extensions-DQ3IQpiE.js → extensions-CQKXTqTI.js} +1 -1
  90. package/ui-dist/assets/extensions-CQKXTqTI.js.gz +0 -0
  91. package/ui-dist/assets/{form-actions-DJlfyLEH.js → form-actions-CzGmrPAv.js} +1 -1
  92. package/ui-dist/assets/{index-CsL49uK2.js → index-CLw9DuCJ.js} +5 -5
  93. package/ui-dist/assets/index-CLw9DuCJ.js.gz +0 -0
  94. package/ui-dist/assets/loader-circle-D9etMT9f.js +1 -0
  95. package/ui-dist/assets/mcp-marketplace-page-CbWOamyW.js +1 -0
  96. package/ui-dist/assets/{mcp-marketplace-page-CEr3FgVp.js → mcp-marketplace-page-s9PTnSDT.js} +1 -1
  97. package/ui-dist/assets/mcp-marketplace-page-s9PTnSDT.js.gz +0 -0
  98. package/ui-dist/assets/{model-config-page-BIpU9Bq5.js → model-config-page-CxnCUWRW.js} +1 -1
  99. package/ui-dist/assets/model-config-page-CxnCUWRW.js.gz +0 -0
  100. package/ui-dist/assets/{navigation-link-DI0mOIVl.js → navigation-link-Ci4Z7Phi.js} +1 -1
  101. package/ui-dist/assets/navigation-link-Ci4Z7Phi.js.gz +0 -0
  102. package/ui-dist/assets/plus-DKGD04AO.js +1 -0
  103. package/ui-dist/assets/{privacy-settings-page-D9qnS5ZE.js → privacy-settings-page-D8HZHHjL.js} +1 -1
  104. package/ui-dist/assets/privacy-settings-page-D8HZHHjL.js.gz +0 -0
  105. package/ui-dist/assets/{providers-config-page-j03VhVjT.js → providers-config-page-C0GIoGxm.js} +1 -1
  106. package/ui-dist/assets/providers-config-page-C0GIoGxm.js.gz +0 -0
  107. package/ui-dist/assets/{react-BwGMzqXA.js → react-CVFx225w.js} +1 -1
  108. package/ui-dist/assets/{react-BwGMzqXA.js.gz → react-CVFx225w.js.gz} +0 -0
  109. package/ui-dist/assets/{refresh-cw-DmR6-f9A.js → refresh-cw-C-3_thgr.js} +1 -1
  110. package/ui-dist/assets/remote-DvTCSAxQ.js +1 -0
  111. package/ui-dist/assets/{rotate-cw-BUWqEgob.js → rotate-cw-DSFTq_yu.js} +1 -1
  112. package/ui-dist/assets/{runtime-config-page-DcyDsmw-.js → runtime-config-page-VNJZui5f.js} +1 -1
  113. package/ui-dist/assets/runtime-config-page-VNJZui5f.js.gz +0 -0
  114. package/ui-dist/assets/{save-DdohrEEJ.js → save-CpPr78_S.js} +1 -1
  115. package/ui-dist/assets/{search-config-page-DBUVi0PY.js → search-config-page-D3_qNXbB.js} +1 -1
  116. package/ui-dist/assets/search-config-page-D3_qNXbB.js.gz +0 -0
  117. package/ui-dist/assets/{search-BVeIVVGT.js → search-msqk2-rd.js} +1 -1
  118. package/ui-dist/assets/{secrets-config-page-Bu58SQa1.js → secrets-config-page-Be4P1E8o.js} +1 -1
  119. package/ui-dist/assets/secrets-config-page-Be4P1E8o.js.gz +0 -0
  120. package/ui-dist/assets/security-config-BtcpKIhQ.js +1 -0
  121. package/ui-dist/assets/{select-DvuHOwPH.js → select-DPKCWajZ.js} +1 -1
  122. package/ui-dist/assets/select-DPKCWajZ.js.gz +0 -0
  123. package/ui-dist/assets/skeleton-BsR5rwB7.js +1 -0
  124. package/ui-dist/assets/{tag-chip-BI9NffVf.js → tag-chip-Dc_NcCEe.js} +1 -1
  125. package/ui-dist/assets/x-Dp5GAFmP.js +1 -0
  126. package/ui-dist/index.html +20 -20
  127. package/ui-dist/index.html.gz +0 -0
  128. package/dist/nextclaw-cli-app-Bwa2L2Jv.js.map +0 -1
  129. package/dist/nextclaw-distribution.utils-C6_UWes0.js.map +0 -1
  130. package/ui-dist/assets/api-DU3C4HfX.js.gz +0 -0
  131. package/ui-dist/assets/appearance-settings-page-CoB3e64T.js.gz +0 -0
  132. package/ui-dist/assets/channels-list-page-BhIoYsyo.js.gz +0 -0
  133. package/ui-dist/assets/config-split-page-DON62DcK.js.gz +0 -0
  134. package/ui-dist/assets/confirm-dialog-B539oJpr.js.gz +0 -0
  135. package/ui-dist/assets/desktop-capabilities-page-BgGZ1I8q.js.gz +0 -0
  136. package/ui-dist/assets/desktop-update-config-TrA-h5OG.js.gz +0 -0
  137. package/ui-dist/assets/dist-GWWehc4w.js.gz +0 -0
  138. package/ui-dist/assets/dist-WPei0-v8.js.gz +0 -0
  139. package/ui-dist/assets/doc-browser-CKpps-rb.js.gz +0 -0
  140. package/ui-dist/assets/doc-browser-context-Vx418ptC.js.gz +0 -0
  141. package/ui-dist/assets/doc-browser-y3e2_MXe.js +0 -1
  142. package/ui-dist/assets/extensions-DQ3IQpiE.js.gz +0 -0
  143. package/ui-dist/assets/index-CsL49uK2.js.gz +0 -0
  144. package/ui-dist/assets/loader-circle-DXH2fVRq.js +0 -1
  145. package/ui-dist/assets/mcp-marketplace-page-CEr3FgVp.js.gz +0 -0
  146. package/ui-dist/assets/mcp-marketplace-page-Dj0xiFAV.js +0 -1
  147. package/ui-dist/assets/model-config-page-BIpU9Bq5.js.gz +0 -0
  148. package/ui-dist/assets/navigation-link-DI0mOIVl.js.gz +0 -0
  149. package/ui-dist/assets/plus-C6Kx4b6T.js +0 -1
  150. package/ui-dist/assets/privacy-settings-page-D9qnS5ZE.js.gz +0 -0
  151. package/ui-dist/assets/providers-config-page-j03VhVjT.js.gz +0 -0
  152. package/ui-dist/assets/remote-BPMM79JV.js +0 -1
  153. package/ui-dist/assets/runtime-config-page-DcyDsmw-.js.gz +0 -0
  154. package/ui-dist/assets/search-config-page-DBUVi0PY.js.gz +0 -0
  155. package/ui-dist/assets/secrets-config-page-Bu58SQa1.js.gz +0 -0
  156. package/ui-dist/assets/security-config-CNdun2s-.js +0 -1
  157. package/ui-dist/assets/select-DvuHOwPH.js.gz +0 -0
  158. package/ui-dist/assets/skeleton-u2lc3JJg.js +0 -1
  159. package/ui-dist/assets/x-27QFIvUt.js +0 -1
@@ -0,0 +1,307 @@
1
+ #[allow(warnings)]
2
+ mod bindings;
3
+
4
+ use bindings::exports::nextclaw::portable_service::service::{Action, Guest};
5
+ use bindings::wasi::{
6
+ clocks::wall_clock,
7
+ config::store as config_store,
8
+ http::{
9
+ outgoing_handler,
10
+ types::{Fields, Method, OutgoingBody, OutgoingRequest, RequestOptions, Scheme},
11
+ },
12
+ io::streams::StreamError,
13
+ keyvalue::store as keyvalue_store,
14
+ };
15
+ use serde_json::{Value, json};
16
+
17
+ const STORE_NAME: &str = "default";
18
+ const SNAPSHOT_KEY: &str = "github-issues/snapshot";
19
+ const GITHUB_TOKEN_SLOT: &str = "github-token";
20
+ const MAX_RESPONSE_BYTES: usize = 512 * 1024;
21
+ const REQUEST_TIMEOUT_MS: u64 = 10_000;
22
+
23
+ struct Watcher;
24
+
25
+ impl Guest for Watcher {
26
+ fn list_actions() -> Vec<Action> {
27
+ vec![
28
+ Action {
29
+ name: "issues_sync".into(),
30
+ title: "同步 GitHub Issue".into(),
31
+ description: "从 GitHub 同步一个仓库最近的 Issue,并保存在此应用自己的数据中。"
32
+ .into(),
33
+ },
34
+ Action {
35
+ name: "issues_list".into(),
36
+ title: "查看已同步 Issue".into(),
37
+ description: "读取本应用上次同步的 Issue;可按打开、关闭或全部筛选。".into(),
38
+ },
39
+ ]
40
+ }
41
+
42
+ fn invoke(action: String, input_json: String) -> Result<String, String> {
43
+ let input: Value = serde_json::from_str(&input_json)
44
+ .map_err(|_| "INVALID_INPUT: input must be a JSON object".to_string())?;
45
+ match action.as_str() {
46
+ "issues_sync" => sync_issues(&input),
47
+ "issues_list" => list_issues(&input),
48
+ _ => Err(format!("UNKNOWN_ACTION: {action}")),
49
+ }
50
+ }
51
+
52
+ fn start(_config_json: String) -> Result<String, String> {
53
+ Ok(json!({ "started": true, "mode": "action" }).to_string())
54
+ }
55
+
56
+ fn handle_event(_event_json: String) -> Result<String, String> {
57
+ Err("UNSUPPORTED_LIFECYCLE: this app only responds when you request a sync or list".into())
58
+ }
59
+
60
+ fn stop(_reason_json: String) -> Result<String, String> {
61
+ Ok(json!({ "stopped": true, "mode": "action" }).to_string())
62
+ }
63
+ }
64
+
65
+ fn sync_issues(input: &Value) -> Result<String, String> {
66
+ let repository = normalized_repository(input.get("repository").and_then(Value::as_str))?;
67
+ let token = optional_github_token()?;
68
+ let response = github_issues_request(&repository, token.as_deref())?;
69
+ if !(200..300).contains(&response.status) {
70
+ return Err(format!(
71
+ "GITHUB_HTTP_{}: GitHub did not accept this repository request",
72
+ response.status
73
+ ));
74
+ }
75
+ let remote_issues: Value = serde_json::from_slice(&response.body).map_err(|_| {
76
+ "GITHUB_RESPONSE_INVALID: GitHub returned an unexpected response".to_string()
77
+ })?;
78
+ let issues = remote_issues
79
+ .as_array()
80
+ .ok_or("GITHUB_RESPONSE_INVALID: GitHub Issue response must be a list")?
81
+ .iter()
82
+ // GitHub returns pull requests from the Issue endpoint too. This app is deliberately
83
+ // an Issue watcher, so do not present PRs as Issues.
84
+ .filter(|issue| issue.get("pull_request").is_none())
85
+ .map(public_issue)
86
+ .collect::<Vec<_>>();
87
+ let now = wall_clock::now();
88
+ let snapshot = json!({
89
+ "repository": repository,
90
+ "issues": issues,
91
+ "syncedAtEpochMs": now.seconds.saturating_mul(1_000).saturating_add(u64::from(now.nanoseconds) / 1_000_000),
92
+ "source": "github-api",
93
+ });
94
+ let bucket = keyvalue_store::open(STORE_NAME).map_err(format_keyvalue_error)?;
95
+ bucket
96
+ .set(SNAPSHOT_KEY, snapshot.to_string().as_bytes())
97
+ .map_err(format_keyvalue_error)?;
98
+ Ok(json!({
99
+ "repository": snapshot["repository"],
100
+ "synced": true,
101
+ "issueCount": snapshot["issues"].as_array().map_or(0, Vec::len),
102
+ "syncedAtEpochMs": snapshot["syncedAtEpochMs"],
103
+ "authenticated": token.is_some(),
104
+ "persistedBy": "wasi:keyvalue/store",
105
+ "requestedVia": "wasi:http/outgoing-handler",
106
+ })
107
+ .to_string())
108
+ }
109
+
110
+ fn list_issues(input: &Value) -> Result<String, String> {
111
+ let state = input.get("state").and_then(Value::as_str).unwrap_or("open");
112
+ if !matches!(state, "open" | "closed" | "all") {
113
+ return Err("INVALID_INPUT: state must be open, closed, or all".into());
114
+ }
115
+ let bucket = keyvalue_store::open(STORE_NAME).map_err(format_keyvalue_error)?;
116
+ let saved = bucket.get(SNAPSHOT_KEY).map_err(format_keyvalue_error)?;
117
+ let Some(saved) = saved else {
118
+ return Ok(json!({
119
+ "repository": null,
120
+ "issues": [],
121
+ "syncedAtEpochMs": null,
122
+ "message": "还没有同步过仓库。先输入 owner/repository 后点击同步。",
123
+ "persistedBy": "wasi:keyvalue/store",
124
+ })
125
+ .to_string());
126
+ };
127
+ let mut snapshot: Value = serde_json::from_slice(&saved)
128
+ .map_err(|_| "STORAGE_CORRUPT: saved Issue data is not valid JSON".to_string())?;
129
+ let filtered = snapshot["issues"]
130
+ .as_array()
131
+ .into_iter()
132
+ .flatten()
133
+ .filter(|issue| state == "all" || issue.get("state").and_then(Value::as_str) == Some(state))
134
+ .cloned()
135
+ .collect::<Vec<_>>();
136
+ snapshot["issues"] = Value::Array(filtered);
137
+ snapshot["filter"] = Value::String(state.into());
138
+ snapshot["persistedBy"] = Value::String("wasi:keyvalue/store".into());
139
+ Ok(snapshot.to_string())
140
+ }
141
+
142
+ fn normalized_repository(value: Option<&str>) -> Result<String, String> {
143
+ let repository = value.unwrap_or("").trim();
144
+ let mut segments = repository.split('/');
145
+ let (Some(owner), Some(name), None) = (segments.next(), segments.next(), segments.next())
146
+ else {
147
+ return Err("INVALID_INPUT: repository must be owner/repository".into());
148
+ };
149
+ if owner.is_empty()
150
+ || name.is_empty()
151
+ || owner.len() > 100
152
+ || name.len() > 100
153
+ || !owner.chars().all(is_github_name_character)
154
+ || !name.chars().all(is_github_name_character)
155
+ {
156
+ return Err(
157
+ "INVALID_INPUT: repository must contain only GitHub owner/repository characters".into(),
158
+ );
159
+ }
160
+ Ok(format!("{owner}/{name}"))
161
+ }
162
+
163
+ fn is_github_name_character(character: char) -> bool {
164
+ character.is_ascii_alphanumeric() || matches!(character, '-' | '_' | '.')
165
+ }
166
+
167
+ fn optional_github_token() -> Result<Option<String>, String> {
168
+ let variable = spin_secret_variable_name(GITHUB_TOKEN_SLOT);
169
+ config_store::get(&variable)
170
+ .map_err(|_| "SECRET_UNAVAILABLE: the optional GitHub token could not be read".to_string())
171
+ }
172
+
173
+ fn spin_secret_variable_name(slot: &str) -> String {
174
+ format!(
175
+ "nextclaw_secret_{}",
176
+ slot.as_bytes()
177
+ .iter()
178
+ .map(|byte| format!("{byte:02x}"))
179
+ .collect::<String>()
180
+ )
181
+ }
182
+
183
+ struct HttpResponse {
184
+ status: u16,
185
+ body: Vec<u8>,
186
+ }
187
+
188
+ fn github_issues_request(repository: &str, token: Option<&str>) -> Result<HttpResponse, String> {
189
+ let fields = Fields::new();
190
+ fields
191
+ .append("accept", b"application/vnd.github+json")
192
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: GitHub accept header".to_string())?;
193
+ fields
194
+ .append("user-agent", b"NextClaw-GitHub-Issue-Watcher")
195
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: GitHub user-agent header".to_string())?;
196
+ fields
197
+ .append("x-github-api-version", b"2022-11-28")
198
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: GitHub API version header".to_string())?;
199
+ if let Some(token) = token.filter(|token| !token.trim().is_empty()) {
200
+ let authorization = format!("Bearer {token}");
201
+ fields
202
+ .append("authorization", authorization.as_bytes())
203
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: GitHub authorization header".to_string())?;
204
+ }
205
+ let request = OutgoingRequest::new(fields);
206
+ request
207
+ .set_method(&Method::Get)
208
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: GET method was rejected".to_string())?;
209
+ request
210
+ .set_scheme(Some(&Scheme::Https))
211
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: HTTPS scheme was rejected".to_string())?;
212
+ request
213
+ .set_authority(Some("api.github.com"))
214
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: GitHub authority was rejected".to_string())?;
215
+ request
216
+ .set_path_with_query(Some(&format!(
217
+ "/repos/{repository}/issues?state=all&per_page=20&sort=updated&direction=desc"
218
+ )))
219
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: GitHub path was rejected".to_string())?;
220
+ let body = request
221
+ .body()
222
+ .map_err(|_| "WASI_HTTP_REQUEST_INVALID: request body unavailable".to_string())?;
223
+ OutgoingBody::finish(body, None).map_err(format_wasi_http_error)?;
224
+
225
+ let options = RequestOptions::new();
226
+ let timeout_ns = REQUEST_TIMEOUT_MS * 1_000_000;
227
+ options
228
+ .set_connect_timeout(Some(timeout_ns))
229
+ .map_err(|_| "WASI_HTTP_TIMEOUT_UNSUPPORTED: connect timeout".to_string())?;
230
+ options
231
+ .set_first_byte_timeout(Some(timeout_ns))
232
+ .map_err(|_| "WASI_HTTP_TIMEOUT_UNSUPPORTED: first-byte timeout".to_string())?;
233
+ options
234
+ .set_between_bytes_timeout(Some(timeout_ns))
235
+ .map_err(|_| "WASI_HTTP_TIMEOUT_UNSUPPORTED: between-bytes timeout".to_string())?;
236
+ let pending =
237
+ outgoing_handler::handle(request, Some(options)).map_err(format_wasi_http_error)?;
238
+ let response = loop {
239
+ match pending.get() {
240
+ Some(Ok(Ok(response))) => break response,
241
+ Some(Ok(Err(error))) => return Err(format_wasi_http_error(error)),
242
+ Some(Err(())) => return Err("WASI_HTTP_RESPONSE_ALREADY_CONSUMED".into()),
243
+ None => pending.subscribe().block(),
244
+ }
245
+ };
246
+ let status = response.status();
247
+ let body = response
248
+ .consume()
249
+ .map_err(|_| "WASI_HTTP_RESPONSE_BODY_UNAVAILABLE".to_string())?;
250
+ let stream = body
251
+ .stream()
252
+ .map_err(|_| "WASI_HTTP_RESPONSE_STREAM_UNAVAILABLE".to_string())?;
253
+ Ok(HttpResponse {
254
+ status,
255
+ body: read_response_body(stream)?,
256
+ })
257
+ }
258
+
259
+ fn read_response_body(stream: bindings::wasi::io::streams::InputStream) -> Result<Vec<u8>, String> {
260
+ let mut body = Vec::new();
261
+ loop {
262
+ match stream.read(8 * 1024) {
263
+ Ok(chunk) if chunk.is_empty() => stream.subscribe().block(),
264
+ Ok(chunk) => {
265
+ let next_length = body
266
+ .len()
267
+ .checked_add(chunk.len())
268
+ .ok_or("GITHUB_RESPONSE_TOO_LARGE: response size overflow")?;
269
+ if next_length > MAX_RESPONSE_BYTES {
270
+ return Err("GITHUB_RESPONSE_TOO_LARGE: GitHub response exceeds 512 KiB".into());
271
+ }
272
+ body.extend_from_slice(&chunk);
273
+ }
274
+ Err(StreamError::Closed) => return Ok(body),
275
+ Err(StreamError::LastOperationFailed(_)) => {
276
+ return Err("WASI_HTTP_RESPONSE_STREAM_FAILED".into());
277
+ }
278
+ }
279
+ }
280
+ }
281
+
282
+ fn public_issue(issue: &Value) -> Value {
283
+ json!({
284
+ "number": issue.get("number").and_then(Value::as_u64),
285
+ "title": issue.get("title").and_then(Value::as_str).unwrap_or("未命名 Issue"),
286
+ "state": issue.get("state").and_then(Value::as_str).unwrap_or("open"),
287
+ "url": issue.get("html_url").and_then(Value::as_str),
288
+ "updatedAt": issue.get("updated_at").and_then(Value::as_str),
289
+ "author": issue.pointer("/user/login").and_then(Value::as_str),
290
+ "labels": issue.get("labels").and_then(Value::as_array).map(|labels| labels.iter()
291
+ .filter_map(|label| label.get("name").and_then(Value::as_str))
292
+ .collect::<Vec<_>>()).unwrap_or_default(),
293
+ })
294
+ }
295
+
296
+ fn format_wasi_http_error(error: bindings::wasi::http::types::ErrorCode) -> String {
297
+ // WASI HTTP error variants contain transport policy facts, not request
298
+ // headers. Keeping the variant makes a blocked domain/DNS/timeout
299
+ // actionable without ever exposing an optional GitHub token.
300
+ format!("WASI_HTTP_ERROR: {error:?}")
301
+ }
302
+
303
+ fn format_keyvalue_error(_error: keyvalue_store::Error) -> String {
304
+ "WASI_KEYVALUE_ERROR: application storage is unavailable".into()
305
+ }
306
+
307
+ bindings::export!(Watcher with_types_in bindings);
@@ -0,0 +1,50 @@
1
+ package wasi:clocks@0.2.6;
2
+ /// WASI Monotonic Clock is a clock API intended to let users measure elapsed
3
+ /// time.
4
+ ///
5
+ /// It is intended to be portable at least between Unix-family platforms and
6
+ /// Windows.
7
+ ///
8
+ /// A monotonic clock is a clock which has an unspecified initial value, and
9
+ /// successive reads of the clock will produce non-decreasing values.
10
+ @since(version = 0.2.0)
11
+ interface monotonic-clock {
12
+ @since(version = 0.2.0)
13
+ use wasi:io/poll@0.2.6.{pollable};
14
+
15
+ /// An instant in time, in nanoseconds. An instant is relative to an
16
+ /// unspecified initial value, and can only be compared to instances from
17
+ /// the same monotonic-clock.
18
+ @since(version = 0.2.0)
19
+ type instant = u64;
20
+
21
+ /// A duration of time, in nanoseconds.
22
+ @since(version = 0.2.0)
23
+ type duration = u64;
24
+
25
+ /// Read the current value of the clock.
26
+ ///
27
+ /// The clock is monotonic, therefore calling this function repeatedly will
28
+ /// produce a sequence of non-decreasing values.
29
+ @since(version = 0.2.0)
30
+ now: func() -> instant;
31
+
32
+ /// Query the resolution of the clock. Returns the duration of time
33
+ /// corresponding to a clock tick.
34
+ @since(version = 0.2.0)
35
+ resolution: func() -> duration;
36
+
37
+ /// Create a `pollable` which will resolve once the specified instant
38
+ /// has occurred.
39
+ @since(version = 0.2.0)
40
+ subscribe-instant: func(
41
+ when: instant,
42
+ ) -> pollable;
43
+
44
+ /// Create a `pollable` that will resolve after the specified duration has
45
+ /// elapsed from the time this function is invoked.
46
+ @since(version = 0.2.0)
47
+ subscribe-duration: func(
48
+ when: duration,
49
+ ) -> pollable;
50
+ }
@@ -0,0 +1,55 @@
1
+ package wasi:clocks@0.2.6;
2
+
3
+ @unstable(feature = clocks-timezone)
4
+ interface timezone {
5
+ @unstable(feature = clocks-timezone)
6
+ use wall-clock.{datetime};
7
+
8
+ /// Return information needed to display the given `datetime`. This includes
9
+ /// the UTC offset, the time zone name, and a flag indicating whether
10
+ /// daylight saving time is active.
11
+ ///
12
+ /// If the timezone cannot be determined for the given `datetime`, return a
13
+ /// `timezone-display` for `UTC` with a `utc-offset` of 0 and no daylight
14
+ /// saving time.
15
+ @unstable(feature = clocks-timezone)
16
+ display: func(when: datetime) -> timezone-display;
17
+
18
+ /// The same as `display`, but only return the UTC offset.
19
+ @unstable(feature = clocks-timezone)
20
+ utc-offset: func(when: datetime) -> s32;
21
+
22
+ /// Information useful for displaying the timezone of a specific `datetime`.
23
+ ///
24
+ /// This information may vary within a single `timezone` to reflect daylight
25
+ /// saving time adjustments.
26
+ @unstable(feature = clocks-timezone)
27
+ record timezone-display {
28
+ /// The number of seconds difference between UTC time and the local
29
+ /// time of the timezone.
30
+ ///
31
+ /// The returned value will always be less than 86400 which is the
32
+ /// number of seconds in a day (24*60*60).
33
+ ///
34
+ /// In implementations that do not expose an actual time zone, this
35
+ /// should return 0.
36
+ utc-offset: s32,
37
+
38
+ /// The abbreviated name of the timezone to display to a user. The name
39
+ /// `UTC` indicates Coordinated Universal Time. Otherwise, this should
40
+ /// reference local standards for the name of the time zone.
41
+ ///
42
+ /// In implementations that do not expose an actual time zone, this
43
+ /// should be the string `UTC`.
44
+ ///
45
+ /// In time zones that do not have an applicable name, a formatted
46
+ /// representation of the UTC offset may be returned, such as `-04:00`.
47
+ name: string,
48
+
49
+ /// Whether daylight saving time is active.
50
+ ///
51
+ /// In implementations that do not expose an actual time zone, this
52
+ /// should return false.
53
+ in-daylight-saving-time: bool,
54
+ }
55
+ }
@@ -0,0 +1,46 @@
1
+ package wasi:clocks@0.2.6;
2
+ /// WASI Wall Clock is a clock API intended to let users query the current
3
+ /// time. The name "wall" makes an analogy to a "clock on the wall", which
4
+ /// is not necessarily monotonic as it may be reset.
5
+ ///
6
+ /// It is intended to be portable at least between Unix-family platforms and
7
+ /// Windows.
8
+ ///
9
+ /// A wall clock is a clock which measures the date and time according to
10
+ /// some external reference.
11
+ ///
12
+ /// External references may be reset, so this clock is not necessarily
13
+ /// monotonic, making it unsuitable for measuring elapsed time.
14
+ ///
15
+ /// It is intended for reporting the current date and time for humans.
16
+ @since(version = 0.2.0)
17
+ interface wall-clock {
18
+ /// A time and date in seconds plus nanoseconds.
19
+ @since(version = 0.2.0)
20
+ record datetime {
21
+ seconds: u64,
22
+ nanoseconds: u32,
23
+ }
24
+
25
+ /// Read the current value of the clock.
26
+ ///
27
+ /// This clock is not monotonic, therefore calling this function repeatedly
28
+ /// will not necessarily produce a sequence of non-decreasing values.
29
+ ///
30
+ /// The returned timestamps represent the number of seconds since
31
+ /// 1970-01-01T00:00:00Z, also known as [POSIX's Seconds Since the Epoch],
32
+ /// also known as [Unix Time].
33
+ ///
34
+ /// The nanoseconds field of the output is always less than 1000000000.
35
+ ///
36
+ /// [POSIX's Seconds Since the Epoch]: https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd_chap04.html#tag_21_04_16
37
+ /// [Unix Time]: https://en.wikipedia.org/wiki/Unix_time
38
+ @since(version = 0.2.0)
39
+ now: func() -> datetime;
40
+
41
+ /// Query the resolution of the clock.
42
+ ///
43
+ /// The nanoseconds field of the output is always less than 1000000000.
44
+ @since(version = 0.2.0)
45
+ resolution: func() -> datetime;
46
+ }
@@ -0,0 +1,11 @@
1
+ package wasi:clocks@0.2.6;
2
+
3
+ @since(version = 0.2.0)
4
+ world imports {
5
+ @since(version = 0.2.0)
6
+ import monotonic-clock;
7
+ @since(version = 0.2.0)
8
+ import wall-clock;
9
+ @unstable(feature = clocks-timezone)
10
+ import timezone;
11
+ }
@@ -0,0 +1,9 @@
1
+ interface store {
2
+ variant error {
3
+ upstream(string),
4
+ io(string),
5
+ }
6
+
7
+ get: func(key: string) -> result<option<string>, error>;
8
+ get-all: func() -> result<list<tuple<string, string>>, error>;
9
+ }
@@ -0,0 +1,49 @@
1
+ /// This interface defines a handler of incoming HTTP Requests. It should
2
+ /// be exported by components which can respond to HTTP Requests.
3
+ @since(version = 0.2.0)
4
+ interface incoming-handler {
5
+ @since(version = 0.2.0)
6
+ use types.{incoming-request, response-outparam};
7
+
8
+ /// This function is invoked with an incoming HTTP Request, and a resource
9
+ /// `response-outparam` which provides the capability to reply with an HTTP
10
+ /// Response. The response is sent by calling the `response-outparam.set`
11
+ /// method, which allows execution to continue after the response has been
12
+ /// sent. This enables both streaming to the response body, and performing other
13
+ /// work.
14
+ ///
15
+ /// The implementor of this function must write a response to the
16
+ /// `response-outparam` before returning, or else the caller will respond
17
+ /// with an error on its behalf.
18
+ @since(version = 0.2.0)
19
+ handle: func(
20
+ request: incoming-request,
21
+ response-out: response-outparam
22
+ );
23
+ }
24
+
25
+ /// This interface defines a handler of outgoing HTTP Requests. It should be
26
+ /// imported by components which wish to make HTTP Requests.
27
+ @since(version = 0.2.0)
28
+ interface outgoing-handler {
29
+ @since(version = 0.2.0)
30
+ use types.{
31
+ outgoing-request, request-options, future-incoming-response, error-code
32
+ };
33
+
34
+ /// This function is invoked with an outgoing HTTP Request, and it returns
35
+ /// a resource `future-incoming-response` which represents an HTTP Response
36
+ /// which may arrive in the future.
37
+ ///
38
+ /// The `options` argument accepts optional parameters for the HTTP
39
+ /// protocol's transport layer.
40
+ ///
41
+ /// This function may return an error if the `outgoing-request` is invalid
42
+ /// or not allowed to be made. Otherwise, protocol errors are reported
43
+ /// through the `future-incoming-response`.
44
+ @since(version = 0.2.0)
45
+ handle: func(
46
+ request: outgoing-request,
47
+ options: option<request-options>
48
+ ) -> result<future-incoming-response, error-code>;
49
+ }