@steerable/agent-shell-tauri 0.6.42
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.
- package/Cargo.toml +26 -0
- package/LICENSE +201 -0
- package/README.md +14 -0
- package/package.json +28 -0
- package/src/commands.rs +167 -0
- package/src/host.rs +283 -0
- package/src/lib.rs +162 -0
- package/src/update.rs +108 -0
package/Cargo.toml
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "steerable-agent-shell-tauri"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Reusable Tauri desktop host for @steerable/agent-shell"
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
edition = "2021"
|
|
7
|
+
rust-version = "1.77.2"
|
|
8
|
+
publish = false
|
|
9
|
+
|
|
10
|
+
[lib]
|
|
11
|
+
name = "steerable_agent_shell_tauri"
|
|
12
|
+
|
|
13
|
+
[dependencies]
|
|
14
|
+
arboard = "3.6.1"
|
|
15
|
+
base64 = "0.22"
|
|
16
|
+
command-group = "5.0.1"
|
|
17
|
+
image = { version = "0.25", default-features = false, features = ["png"] }
|
|
18
|
+
serde = { version = "1", features = ["derive"] }
|
|
19
|
+
serde_json = "1"
|
|
20
|
+
tauri = { version = "2.11.6", features = [] }
|
|
21
|
+
tauri-plugin-dialog = "2.7.3"
|
|
22
|
+
tauri-plugin-process = "2.3.1"
|
|
23
|
+
tauri-plugin-single-instance = "2.4.5"
|
|
24
|
+
tauri-plugin-updater = "2.12.0"
|
|
25
|
+
tokio = { version = "1", features = ["time"] }
|
|
26
|
+
url = "2"
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Steerable Tauri host
|
|
2
|
+
|
|
3
|
+
Reusable native desktop host shipped inside the `@steerable/agent-shell` npm
|
|
4
|
+
package. Consuming products keep a thin `src-tauri` crate that supplies Tauri
|
|
5
|
+
configuration and calls `steerable_agent_shell_tauri::run`.
|
|
6
|
+
|
|
7
|
+
The host supervises the product's compiled Node BS entry on an ephemeral
|
|
8
|
+
loopback port. Node continues to own storage, PTY, tools, approvals, packs,
|
|
9
|
+
and CoreLoop sidecars. Rust owns the native window, menus, dialogs,
|
|
10
|
+
single-instance behavior, screenshot clipboard transfer, process containment,
|
|
11
|
+
resource paths, and updates.
|
|
12
|
+
|
|
13
|
+
The crate is source-distributed with the npm package and is not published to
|
|
14
|
+
crates.io.
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@steerable/agent-shell-tauri",
|
|
3
|
+
"version": "0.6.42",
|
|
4
|
+
"description": "Reusable Tauri Rust host source for @steerable/agent-shell",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"homepage": "https://steerableframework.com/",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/pathlyapp/steerable.git",
|
|
10
|
+
"directory": "packages/agent-shell/tauri"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"Cargo.toml",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public",
|
|
19
|
+
"provenance": true
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"gen": "echo agent-shell-tauri",
|
|
23
|
+
"build": "echo agent-shell-tauri source package",
|
|
24
|
+
"test": "echo agent-shell-tauri tests run in the rust CI job",
|
|
25
|
+
"lint": "echo agent-shell-tauri lint runs in the rust CI job",
|
|
26
|
+
"check:drift": "echo ok"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/commands.rs
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
use arboard::{Clipboard, ImageData};
|
|
2
|
+
use base64::{engine::general_purpose::STANDARD, Engine};
|
|
3
|
+
use serde::{Deserialize, Serialize};
|
|
4
|
+
use std::borrow::Cow;
|
|
5
|
+
use std::fs;
|
|
6
|
+
use tauri::AppHandle;
|
|
7
|
+
use tauri_plugin_dialog::DialogExt;
|
|
8
|
+
|
|
9
|
+
const MAX_SCREENSHOT_BASE64_BYTES: usize = 32 * 1024 * 1024;
|
|
10
|
+
|
|
11
|
+
#[derive(Default, Deserialize)]
|
|
12
|
+
#[serde(rename_all = "camelCase")]
|
|
13
|
+
pub struct SelectDirectoryOptions {
|
|
14
|
+
pub title: Option<String>,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#[derive(Serialize)]
|
|
18
|
+
#[serde(rename_all = "camelCase")]
|
|
19
|
+
pub struct SelectDirectoryResult {
|
|
20
|
+
canceled: bool,
|
|
21
|
+
file_paths: Vec<String>,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#[derive(Deserialize)]
|
|
25
|
+
#[serde(rename_all = "camelCase")]
|
|
26
|
+
pub struct SaveTextFileOptions {
|
|
27
|
+
pub title: Option<String>,
|
|
28
|
+
pub default_path: Option<String>,
|
|
29
|
+
pub content: String,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
#[derive(Serialize)]
|
|
33
|
+
#[serde(rename_all = "camelCase")]
|
|
34
|
+
pub struct SaveTextFileResult {
|
|
35
|
+
canceled: bool,
|
|
36
|
+
file_path: Option<String>,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
#[derive(Deserialize)]
|
|
40
|
+
#[serde(rename_all = "camelCase")]
|
|
41
|
+
pub struct ScreenshotImage {
|
|
42
|
+
pub png_base64: String,
|
|
43
|
+
pub width: u32,
|
|
44
|
+
pub height: u32,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#[derive(Serialize)]
|
|
48
|
+
#[serde(untagged)]
|
|
49
|
+
pub enum CaptureScreenshotResult {
|
|
50
|
+
Success {
|
|
51
|
+
success: bool,
|
|
52
|
+
width: u32,
|
|
53
|
+
height: u32,
|
|
54
|
+
},
|
|
55
|
+
Failure {
|
|
56
|
+
success: bool,
|
|
57
|
+
error: String,
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#[tauri::command]
|
|
62
|
+
pub async fn host_select_directory(
|
|
63
|
+
app: AppHandle,
|
|
64
|
+
options: SelectDirectoryOptions,
|
|
65
|
+
) -> Result<SelectDirectoryResult, String> {
|
|
66
|
+
let mut dialog = app.dialog().file();
|
|
67
|
+
if let Some(title) = options.title {
|
|
68
|
+
dialog = dialog.set_title(title);
|
|
69
|
+
}
|
|
70
|
+
let selected = dialog.blocking_pick_folder();
|
|
71
|
+
let Some(selected) = selected else {
|
|
72
|
+
return Ok(SelectDirectoryResult {
|
|
73
|
+
canceled: true,
|
|
74
|
+
file_paths: Vec::new(),
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
let path = selected.into_path().map_err(|error| error.to_string())?;
|
|
78
|
+
Ok(SelectDirectoryResult {
|
|
79
|
+
canceled: false,
|
|
80
|
+
file_paths: vec![path.to_string_lossy().into_owned()],
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#[tauri::command]
|
|
85
|
+
pub async fn host_save_text_file(
|
|
86
|
+
app: AppHandle,
|
|
87
|
+
options: SaveTextFileOptions,
|
|
88
|
+
) -> Result<SaveTextFileResult, String> {
|
|
89
|
+
let mut dialog = app.dialog().file();
|
|
90
|
+
if let Some(title) = options.title {
|
|
91
|
+
dialog = dialog.set_title(title);
|
|
92
|
+
}
|
|
93
|
+
if let Some(default_path) = &options.default_path {
|
|
94
|
+
dialog = dialog.set_file_name(default_path);
|
|
95
|
+
}
|
|
96
|
+
let selected = dialog.blocking_save_file();
|
|
97
|
+
let Some(selected) = selected else {
|
|
98
|
+
return Ok(SaveTextFileResult {
|
|
99
|
+
canceled: true,
|
|
100
|
+
file_path: None,
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
let path = selected.into_path().map_err(|error| error.to_string())?;
|
|
104
|
+
fs::write(&path, options.content).map_err(|error| error.to_string())?;
|
|
105
|
+
Ok(SaveTextFileResult {
|
|
106
|
+
canceled: false,
|
|
107
|
+
file_path: Some(path.to_string_lossy().into_owned()),
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#[tauri::command]
|
|
112
|
+
pub async fn host_capture_screenshot(
|
|
113
|
+
image: ScreenshotImage,
|
|
114
|
+
) -> Result<CaptureScreenshotResult, String> {
|
|
115
|
+
if image.png_base64.len() > MAX_SCREENSHOT_BASE64_BYTES {
|
|
116
|
+
return Ok(CaptureScreenshotResult::Failure {
|
|
117
|
+
success: false,
|
|
118
|
+
error: "Screenshot exceeds the 32 MB transfer limit".to_string(),
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
let png = STANDARD
|
|
122
|
+
.decode(image.png_base64)
|
|
123
|
+
.map_err(|error| error.to_string())?;
|
|
124
|
+
let rgba = image::load_from_memory_with_format(&png, image::ImageFormat::Png)
|
|
125
|
+
.map_err(|error| error.to_string())?
|
|
126
|
+
.into_rgba8();
|
|
127
|
+
let (width, height) = rgba.dimensions();
|
|
128
|
+
if width != image.width || height != image.height {
|
|
129
|
+
return Ok(CaptureScreenshotResult::Failure {
|
|
130
|
+
success: false,
|
|
131
|
+
error: "Screenshot dimensions changed during transfer".to_string(),
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
let mut clipboard = Clipboard::new().map_err(|error| error.to_string())?;
|
|
135
|
+
clipboard
|
|
136
|
+
.set_image(ImageData {
|
|
137
|
+
width: width as usize,
|
|
138
|
+
height: height as usize,
|
|
139
|
+
bytes: Cow::Owned(rgba.into_raw()),
|
|
140
|
+
})
|
|
141
|
+
.map_err(|error| error.to_string())?;
|
|
142
|
+
Ok(CaptureScreenshotResult::Success {
|
|
143
|
+
success: true,
|
|
144
|
+
width,
|
|
145
|
+
height,
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#[cfg(test)]
|
|
150
|
+
mod tests {
|
|
151
|
+
use super::*;
|
|
152
|
+
|
|
153
|
+
#[test]
|
|
154
|
+
fn rejects_oversized_screenshot_before_decoding() {
|
|
155
|
+
let result = tauri::async_runtime::block_on(host_capture_screenshot(ScreenshotImage {
|
|
156
|
+
png_base64: "a".repeat(MAX_SCREENSHOT_BASE64_BYTES + 1),
|
|
157
|
+
width: 1,
|
|
158
|
+
height: 1,
|
|
159
|
+
}))
|
|
160
|
+
.expect("command should return a structured failure");
|
|
161
|
+
assert!(matches!(
|
|
162
|
+
result,
|
|
163
|
+
CaptureScreenshotResult::Failure { error, .. }
|
|
164
|
+
if error.contains("32 MB")
|
|
165
|
+
));
|
|
166
|
+
}
|
|
167
|
+
}
|
package/src/host.rs
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
use crate::DesktopConfig;
|
|
2
|
+
use command_group::{CommandGroup, GroupChild};
|
|
3
|
+
#[cfg(unix)]
|
|
4
|
+
use command_group::{Signal, UnixChildExt};
|
|
5
|
+
use serde::Deserialize;
|
|
6
|
+
use std::env;
|
|
7
|
+
use std::io::{BufRead, BufReader};
|
|
8
|
+
use std::path::PathBuf;
|
|
9
|
+
use std::process::{Command, Stdio};
|
|
10
|
+
use std::sync::mpsc;
|
|
11
|
+
use std::sync::Mutex;
|
|
12
|
+
use std::thread;
|
|
13
|
+
use std::time::{Duration, Instant};
|
|
14
|
+
use tauri::{AppHandle, Manager};
|
|
15
|
+
use url::Url;
|
|
16
|
+
|
|
17
|
+
const READY_PREFIX: &str = "STEERABLE_HOST_READY ";
|
|
18
|
+
const START_TIMEOUT: Duration = Duration::from_secs(30);
|
|
19
|
+
|
|
20
|
+
#[derive(Deserialize)]
|
|
21
|
+
struct ReadyRecord {
|
|
22
|
+
host: String,
|
|
23
|
+
port: u16,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
pub struct HostProcess {
|
|
27
|
+
child: Mutex<Option<GroupChild>>,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
impl HostProcess {
|
|
31
|
+
pub fn spawn(app: &AppHandle, config: &DesktopConfig) -> Result<(Self, Url), String> {
|
|
32
|
+
let paths = HostPaths::resolve(app, config)?;
|
|
33
|
+
let user_data = match env::var_os("DEEPPATH_USER_DATA_DIR") {
|
|
34
|
+
Some(path) => PathBuf::from(path),
|
|
35
|
+
None => app
|
|
36
|
+
.path()
|
|
37
|
+
.home_dir()
|
|
38
|
+
.map_err(|error| error.to_string())?
|
|
39
|
+
.join(&config.data_dir_name),
|
|
40
|
+
};
|
|
41
|
+
std::fs::create_dir_all(&user_data).map_err(|error| error.to_string())?;
|
|
42
|
+
|
|
43
|
+
let mut command = Command::new(&paths.node);
|
|
44
|
+
command
|
|
45
|
+
.arg(&paths.server_entry)
|
|
46
|
+
.current_dir(&paths.app_root)
|
|
47
|
+
.env("APP_FLAVOR", &config.product_id)
|
|
48
|
+
.env("VITE_APP_FLAVOR", &config.product_id)
|
|
49
|
+
.env("DEEPPATH_BS_HOST", "127.0.0.1")
|
|
50
|
+
.env("DEEPPATH_BS_PORT", "0")
|
|
51
|
+
.env("DEEPPATH_WEB_DIST", &paths.web_dist)
|
|
52
|
+
.env("DEEPPATH_USER_DATA_DIR", user_data)
|
|
53
|
+
.env("STEERABLE_HOST_PARENT_PID", std::process::id().to_string())
|
|
54
|
+
.stdin(Stdio::null())
|
|
55
|
+
.stdout(Stdio::piped())
|
|
56
|
+
.stderr(Stdio::piped());
|
|
57
|
+
paths.apply_runtime_env(&mut command);
|
|
58
|
+
|
|
59
|
+
let mut child = spawn_group(&mut command).map_err(|error| {
|
|
60
|
+
format!(
|
|
61
|
+
"failed to start Node host with {}: {error}",
|
|
62
|
+
paths.node.display()
|
|
63
|
+
)
|
|
64
|
+
})?;
|
|
65
|
+
let stdout = child
|
|
66
|
+
.inner()
|
|
67
|
+
.stdout
|
|
68
|
+
.take()
|
|
69
|
+
.ok_or_else(|| "Node host stdout was not piped".to_string())?;
|
|
70
|
+
let stderr = child
|
|
71
|
+
.inner()
|
|
72
|
+
.stderr
|
|
73
|
+
.take()
|
|
74
|
+
.ok_or_else(|| "Node host stderr was not piped".to_string())?;
|
|
75
|
+
|
|
76
|
+
let (ready_tx, ready_rx) = mpsc::channel();
|
|
77
|
+
thread::spawn(move || {
|
|
78
|
+
for line in BufReader::new(stdout).lines() {
|
|
79
|
+
match line {
|
|
80
|
+
Ok(line) => {
|
|
81
|
+
println!("[node-host] {line}");
|
|
82
|
+
if let Some(record) = line.strip_prefix(READY_PREFIX) {
|
|
83
|
+
let parsed = serde_json::from_str::<ReadyRecord>(record)
|
|
84
|
+
.map_err(|error| error.to_string());
|
|
85
|
+
let _ = ready_tx.send(parsed);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
Err(error) => {
|
|
89
|
+
let _ = ready_tx.send(Err(error.to_string()));
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
thread::spawn(move || {
|
|
96
|
+
for line in BufReader::new(stderr).lines().map_while(Result::ok) {
|
|
97
|
+
eprintln!("[node-host] {line}");
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
let started = Instant::now();
|
|
102
|
+
let ready = loop {
|
|
103
|
+
match ready_rx.recv_timeout(Duration::from_millis(100)) {
|
|
104
|
+
Ok(result) => break result?,
|
|
105
|
+
Err(mpsc::RecvTimeoutError::Disconnected) => {
|
|
106
|
+
let _ = child.kill();
|
|
107
|
+
return Err("Node host exited before reporting readiness".to_string());
|
|
108
|
+
}
|
|
109
|
+
Err(mpsc::RecvTimeoutError::Timeout) => {}
|
|
110
|
+
}
|
|
111
|
+
if let Some(status) = child.try_wait().map_err(|error| error.to_string())? {
|
|
112
|
+
return Err(format!(
|
|
113
|
+
"Node host exited before reporting readiness: {status}"
|
|
114
|
+
));
|
|
115
|
+
}
|
|
116
|
+
if started.elapsed() >= START_TIMEOUT {
|
|
117
|
+
let _ = child.kill();
|
|
118
|
+
return Err("Node host did not become ready within 30 seconds".to_string());
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
let url = Url::parse(&format!("http://{}:{}/", ready.host, ready.port))
|
|
122
|
+
.map_err(|error| error.to_string())?;
|
|
123
|
+
Ok((
|
|
124
|
+
Self {
|
|
125
|
+
child: Mutex::new(Some(child)),
|
|
126
|
+
},
|
|
127
|
+
url,
|
|
128
|
+
))
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
pub fn stop(&self) {
|
|
132
|
+
let Ok(mut child) = self.child.lock() else {
|
|
133
|
+
return;
|
|
134
|
+
};
|
|
135
|
+
if let Some(mut child) = child.take() {
|
|
136
|
+
#[cfg(unix)]
|
|
137
|
+
{
|
|
138
|
+
let _ = child.signal(Signal::SIGTERM);
|
|
139
|
+
let deadline = Instant::now() + Duration::from_secs(5);
|
|
140
|
+
while Instant::now() < deadline {
|
|
141
|
+
if matches!(child.try_wait(), Ok(Some(_))) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
thread::sleep(Duration::from_millis(50));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
let _ = child.kill();
|
|
148
|
+
let _ = child.wait();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
impl Drop for HostProcess {
|
|
154
|
+
fn drop(&mut self) {
|
|
155
|
+
self.stop();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
struct HostPaths {
|
|
160
|
+
node: PathBuf,
|
|
161
|
+
server_entry: PathBuf,
|
|
162
|
+
app_root: PathBuf,
|
|
163
|
+
web_dist: PathBuf,
|
|
164
|
+
engine_dir: Option<PathBuf>,
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
impl HostPaths {
|
|
168
|
+
fn apply_runtime_env(&self, command: &mut Command) {
|
|
169
|
+
let Some(engine_dir) = &self.engine_dir else {
|
|
170
|
+
return;
|
|
171
|
+
};
|
|
172
|
+
let rust_sidecar = engine_dir.join(platform_binary("steerable-sidecar"));
|
|
173
|
+
if rust_sidecar.exists() {
|
|
174
|
+
command
|
|
175
|
+
.env("STEERABLE_RUST_SIDECAR", "1")
|
|
176
|
+
.env("STEERABLE_RUST_SIDECAR_BIN", rust_sidecar);
|
|
177
|
+
}
|
|
178
|
+
let egress_proxy = engine_dir.join(platform_binary("steerable-egress-proxy"));
|
|
179
|
+
if egress_proxy.exists() {
|
|
180
|
+
command.env("STEERABLE_EGRESS_PROXY_BIN", egress_proxy);
|
|
181
|
+
}
|
|
182
|
+
let python =
|
|
183
|
+
engine_dir
|
|
184
|
+
.join("python-runtime")
|
|
185
|
+
.join(platform_tag())
|
|
186
|
+
.join(if cfg!(windows) {
|
|
187
|
+
"python/python.exe"
|
|
188
|
+
} else {
|
|
189
|
+
"python/bin/python3"
|
|
190
|
+
});
|
|
191
|
+
if python.exists() {
|
|
192
|
+
command.env("STEERABLE_SIDECAR_PYTHON", python);
|
|
193
|
+
}
|
|
194
|
+
let win_spawn_helper = engine_dir.join("win-spawn-helper/win-spawn-helper.exe");
|
|
195
|
+
if win_spawn_helper.exists() {
|
|
196
|
+
command.env("DEEPPATH_WIN_SPAWN_HELPER", win_spawn_helper);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
fn resolve(app: &AppHandle, config: &DesktopConfig) -> Result<Self, String> {
|
|
201
|
+
if cfg!(debug_assertions) {
|
|
202
|
+
let app_root = config.development_root.clone();
|
|
203
|
+
return Ok(Self {
|
|
204
|
+
node: env::var_os("DEEPPATH_TAURI_NODE")
|
|
205
|
+
.map(PathBuf::from)
|
|
206
|
+
.unwrap_or_else(|| PathBuf::from("node")),
|
|
207
|
+
server_entry: env::var_os("DEEPPATH_TAURI_SERVER_ENTRY")
|
|
208
|
+
.map(PathBuf::from)
|
|
209
|
+
.unwrap_or_else(|| app_root.join("dist/devtools/dev-server.js")),
|
|
210
|
+
web_dist: env::var_os("DEEPPATH_WEB_DIST")
|
|
211
|
+
.map(PathBuf::from)
|
|
212
|
+
.unwrap_or_else(|| {
|
|
213
|
+
app_root
|
|
214
|
+
.join("products")
|
|
215
|
+
.join(&config.product_id)
|
|
216
|
+
.join("web/dist")
|
|
217
|
+
}),
|
|
218
|
+
app_root,
|
|
219
|
+
engine_dir: None,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let resource_dir = app
|
|
224
|
+
.path()
|
|
225
|
+
.resource_dir()
|
|
226
|
+
.map_err(|error| error.to_string())?;
|
|
227
|
+
let host_root = resource_dir.join("node-host");
|
|
228
|
+
let app_root = host_root.join("app-dist");
|
|
229
|
+
Ok(Self {
|
|
230
|
+
node: env::var_os("DEEPPATH_TAURI_NODE")
|
|
231
|
+
.map(PathBuf::from)
|
|
232
|
+
.unwrap_or_else(|| resource_dir.join(node_resource_name())),
|
|
233
|
+
server_entry: app_root
|
|
234
|
+
.join("products")
|
|
235
|
+
.join(&config.product_id)
|
|
236
|
+
.join("server.js"),
|
|
237
|
+
web_dist: host_root.join("web-dist"),
|
|
238
|
+
app_root,
|
|
239
|
+
engine_dir: Some(resource_dir.join("engine")),
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
fn platform_binary(name: &str) -> String {
|
|
245
|
+
if cfg!(windows) {
|
|
246
|
+
format!("{name}.exe")
|
|
247
|
+
} else {
|
|
248
|
+
name.to_string()
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
fn platform_tag() -> &'static str {
|
|
253
|
+
if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
|
|
254
|
+
"darwin-arm64"
|
|
255
|
+
} else if cfg!(all(target_os = "macos", target_arch = "x86_64")) {
|
|
256
|
+
"darwin-x64"
|
|
257
|
+
} else if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
|
|
258
|
+
"win32-x64"
|
|
259
|
+
} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
|
|
260
|
+
"linux-x64"
|
|
261
|
+
} else {
|
|
262
|
+
"unsupported"
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
fn node_resource_name() -> &'static str {
|
|
267
|
+
if cfg!(windows) {
|
|
268
|
+
"node/node.exe"
|
|
269
|
+
} else {
|
|
270
|
+
"node/node"
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
fn spawn_group(command: &mut Command) -> std::io::Result<GroupChild> {
|
|
275
|
+
#[cfg(windows)]
|
|
276
|
+
{
|
|
277
|
+
return command.group().kill_on_drop(true).spawn();
|
|
278
|
+
}
|
|
279
|
+
#[cfg(not(windows))]
|
|
280
|
+
{
|
|
281
|
+
command.group_spawn()
|
|
282
|
+
}
|
|
283
|
+
}
|
package/src/lib.rs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
mod commands;
|
|
2
|
+
mod host;
|
|
3
|
+
mod update;
|
|
4
|
+
|
|
5
|
+
use commands::{host_capture_screenshot, host_save_text_file, host_select_directory};
|
|
6
|
+
use host::HostProcess;
|
|
7
|
+
use std::path::PathBuf;
|
|
8
|
+
use tauri::{
|
|
9
|
+
menu::{MenuBuilder, MenuItemBuilder, SubmenuBuilder},
|
|
10
|
+
Emitter, Manager, RunEvent, WebviewUrl, WebviewWindowBuilder, WindowEvent,
|
|
11
|
+
};
|
|
12
|
+
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
|
|
13
|
+
|
|
14
|
+
/// Product values required by the reusable desktop host.
|
|
15
|
+
pub struct DesktopConfig {
|
|
16
|
+
pub product_id: String,
|
|
17
|
+
pub product_name: String,
|
|
18
|
+
pub data_dir_name: String,
|
|
19
|
+
pub development_root: PathBuf,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl DesktopConfig {
|
|
23
|
+
pub fn new(
|
|
24
|
+
product_id: impl Into<String>,
|
|
25
|
+
product_name: impl Into<String>,
|
|
26
|
+
data_dir_name: impl Into<String>,
|
|
27
|
+
development_root: impl Into<PathBuf>,
|
|
28
|
+
) -> Self {
|
|
29
|
+
Self {
|
|
30
|
+
product_id: product_id.into(),
|
|
31
|
+
product_name: product_name.into(),
|
|
32
|
+
data_dir_name: data_dir_name.into(),
|
|
33
|
+
development_root: development_root.into(),
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn install_menu(app: &tauri::App) -> tauri::Result<()> {
|
|
39
|
+
let new_chat = MenuItemBuilder::with_id("new-chat", "新建对话")
|
|
40
|
+
.accelerator("CmdOrCtrl+N")
|
|
41
|
+
.build(app)?;
|
|
42
|
+
let open_terminal = MenuItemBuilder::with_id("open-terminal", "打开终端")
|
|
43
|
+
.accelerator("CmdOrCtrl+T")
|
|
44
|
+
.build(app)?;
|
|
45
|
+
let file = SubmenuBuilder::new(app, "文件")
|
|
46
|
+
.item(&new_chat)
|
|
47
|
+
.separator()
|
|
48
|
+
.close_window()
|
|
49
|
+
.build()?;
|
|
50
|
+
let edit = SubmenuBuilder::new(app, "编辑")
|
|
51
|
+
.undo()
|
|
52
|
+
.redo()
|
|
53
|
+
.separator()
|
|
54
|
+
.cut()
|
|
55
|
+
.copy()
|
|
56
|
+
.paste()
|
|
57
|
+
.select_all()
|
|
58
|
+
.build()?;
|
|
59
|
+
let view = SubmenuBuilder::new(app, "视图")
|
|
60
|
+
.item(&open_terminal)
|
|
61
|
+
.separator()
|
|
62
|
+
.fullscreen()
|
|
63
|
+
.build()?;
|
|
64
|
+
let menu = MenuBuilder::new(app)
|
|
65
|
+
.items(&[&file, &edit, &view])
|
|
66
|
+
.build()?;
|
|
67
|
+
app.set_menu(menu)?;
|
|
68
|
+
app.on_menu_event(|app, event| match event.id().0.as_str() {
|
|
69
|
+
"new-chat" => {
|
|
70
|
+
let _ = app.emit("menu:new-chat", ());
|
|
71
|
+
}
|
|
72
|
+
"open-terminal" => {
|
|
73
|
+
let _ = app.emit("menu:open-terminal", ());
|
|
74
|
+
}
|
|
75
|
+
_ => {}
|
|
76
|
+
});
|
|
77
|
+
Ok(())
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/// Runs a Tauri desktop shell around the shared Node HostRuntime.
|
|
81
|
+
pub fn run(context: tauri::Context<tauri::Wry>, config: DesktopConfig) {
|
|
82
|
+
let product_name = config.product_name.clone();
|
|
83
|
+
let builder = tauri::Builder::default()
|
|
84
|
+
.manage(update::UpdateState::default())
|
|
85
|
+
.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
|
|
86
|
+
if let Some(window) = app.get_webview_window("main") {
|
|
87
|
+
let _ = window.show();
|
|
88
|
+
let _ = window.unminimize();
|
|
89
|
+
let _ = window.set_focus();
|
|
90
|
+
}
|
|
91
|
+
}))
|
|
92
|
+
.plugin(tauri_plugin_dialog::init())
|
|
93
|
+
.plugin(tauri_plugin_process::init())
|
|
94
|
+
.plugin(tauri_plugin_updater::Builder::new().build())
|
|
95
|
+
.invoke_handler(tauri::generate_handler![
|
|
96
|
+
host_select_directory,
|
|
97
|
+
host_save_text_file,
|
|
98
|
+
host_capture_screenshot
|
|
99
|
+
])
|
|
100
|
+
.setup(move |app| {
|
|
101
|
+
install_menu(app)?;
|
|
102
|
+
let (host, url) = match HostProcess::spawn(app.handle(), &config) {
|
|
103
|
+
Ok(started) => started,
|
|
104
|
+
Err(error) => {
|
|
105
|
+
let handle = app.handle().clone();
|
|
106
|
+
app.dialog()
|
|
107
|
+
.message(error)
|
|
108
|
+
.title(format!("{} 无法启动", config.product_name))
|
|
109
|
+
.kind(MessageDialogKind::Error)
|
|
110
|
+
.show(move |_| handle.exit(1));
|
|
111
|
+
return Ok(());
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
app.manage(host);
|
|
115
|
+
let allowed_origin = url.origin().ascii_serialization();
|
|
116
|
+
WebviewWindowBuilder::new(app, "main", WebviewUrl::External(url))
|
|
117
|
+
.title(&config.product_name)
|
|
118
|
+
.inner_size(1200.0, 800.0)
|
|
119
|
+
.min_inner_size(900.0, 650.0)
|
|
120
|
+
.on_navigation(move |url| url.origin().ascii_serialization() == allowed_origin)
|
|
121
|
+
.build()?;
|
|
122
|
+
update::start(app.handle());
|
|
123
|
+
Ok(())
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
let app = builder
|
|
127
|
+
.build(context)
|
|
128
|
+
.unwrap_or_else(|error| panic!("failed to build {product_name} desktop host: {error}"));
|
|
129
|
+
app.run(|app, event| match event {
|
|
130
|
+
RunEvent::ExitRequested { .. } => {
|
|
131
|
+
if let Some(host) = app.try_state::<HostProcess>() {
|
|
132
|
+
host.stop();
|
|
133
|
+
}
|
|
134
|
+
app.state::<update::UpdateState>().install_pending();
|
|
135
|
+
}
|
|
136
|
+
RunEvent::WindowEvent {
|
|
137
|
+
label,
|
|
138
|
+
event: WindowEvent::CloseRequested { api, .. },
|
|
139
|
+
..
|
|
140
|
+
} if label == "main" && cfg!(target_os = "macos") => {
|
|
141
|
+
api.prevent_close();
|
|
142
|
+
if let Some(window) = app.get_webview_window("main") {
|
|
143
|
+
let _ = window.hide();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
RunEvent::WindowEvent {
|
|
147
|
+
label,
|
|
148
|
+
event: WindowEvent::Destroyed,
|
|
149
|
+
..
|
|
150
|
+
} if label == "main" && !cfg!(target_os = "macos") => {
|
|
151
|
+
app.exit(0);
|
|
152
|
+
}
|
|
153
|
+
#[cfg(target_os = "macos")]
|
|
154
|
+
RunEvent::Reopen { .. } => {
|
|
155
|
+
if let Some(window) = app.get_webview_window("main") {
|
|
156
|
+
let _ = window.show();
|
|
157
|
+
let _ = window.set_focus();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
_ => {}
|
|
161
|
+
});
|
|
162
|
+
}
|
package/src/update.rs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
use std::sync::Mutex;
|
|
2
|
+
use std::time::Duration;
|
|
3
|
+
use tauri::{AppHandle, Emitter, Manager};
|
|
4
|
+
use tauri_plugin_updater::{Update, UpdaterExt};
|
|
5
|
+
|
|
6
|
+
const CHECK_INTERVAL: Duration = Duration::from_secs(4 * 60 * 60);
|
|
7
|
+
|
|
8
|
+
struct PendingUpdate {
|
|
9
|
+
update: Update,
|
|
10
|
+
bytes: Vec<u8>,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
#[derive(Default)]
|
|
14
|
+
pub struct UpdateState {
|
|
15
|
+
pending: Mutex<Option<PendingUpdate>>,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
impl UpdateState {
|
|
19
|
+
pub fn install_pending(&self) {
|
|
20
|
+
let Ok(mut pending) = self.pending.lock() else {
|
|
21
|
+
return;
|
|
22
|
+
};
|
|
23
|
+
let Some(pending) = pending.take() else {
|
|
24
|
+
return;
|
|
25
|
+
};
|
|
26
|
+
if let Err(error) = pending.update.install(pending.bytes) {
|
|
27
|
+
eprintln!("[updater] install failed: {error}");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
pub fn start(app: &AppHandle) {
|
|
33
|
+
if cfg!(debug_assertions) || std::env::var("DEEPPATH_APP_UPDATE").as_deref() == Ok("0") {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
let app = app.clone();
|
|
37
|
+
tauri::async_runtime::spawn(async move {
|
|
38
|
+
loop {
|
|
39
|
+
check_once(&app).await;
|
|
40
|
+
tokio::time::sleep(CHECK_INTERVAL).await;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async fn check_once(app: &AppHandle) {
|
|
46
|
+
if app
|
|
47
|
+
.state::<UpdateState>()
|
|
48
|
+
.pending
|
|
49
|
+
.lock()
|
|
50
|
+
.map(|pending| pending.is_some())
|
|
51
|
+
.unwrap_or(true)
|
|
52
|
+
{
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
let _ = app.emit(
|
|
56
|
+
"app-update-state",
|
|
57
|
+
serde_json::json!({ "phase": "checking" }),
|
|
58
|
+
);
|
|
59
|
+
let updater = match app.updater() {
|
|
60
|
+
Ok(updater) => updater,
|
|
61
|
+
Err(error) => {
|
|
62
|
+
eprintln!("[updater] initialization failed: {error}");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
let update = match updater.check().await {
|
|
67
|
+
Ok(Some(update)) => update,
|
|
68
|
+
Ok(None) => {
|
|
69
|
+
let _ = app.emit("app-update-state", serde_json::json!({ "phase": "idle" }));
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
Err(error) => {
|
|
73
|
+
eprintln!("[updater] check failed: {error}");
|
|
74
|
+
let _ = app.emit(
|
|
75
|
+
"app-update-state",
|
|
76
|
+
serde_json::json!({ "phase": "error", "message": error.to_string() }),
|
|
77
|
+
);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
let version = update.version.clone();
|
|
82
|
+
let _ = app.emit(
|
|
83
|
+
"app-update-state",
|
|
84
|
+
serde_json::json!({ "phase": "downloading", "version": version }),
|
|
85
|
+
);
|
|
86
|
+
let bytes = match update.download(|_chunk, _total| {}, || {}).await {
|
|
87
|
+
Ok(bytes) => bytes,
|
|
88
|
+
Err(error) => {
|
|
89
|
+
eprintln!("[updater] download failed: {error}");
|
|
90
|
+
let _ = app.emit(
|
|
91
|
+
"app-update-state",
|
|
92
|
+
serde_json::json!({
|
|
93
|
+
"phase": "error",
|
|
94
|
+
"version": version,
|
|
95
|
+
"message": error.to_string()
|
|
96
|
+
}),
|
|
97
|
+
);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
if let Ok(mut pending) = app.state::<UpdateState>().pending.lock() {
|
|
102
|
+
*pending = Some(PendingUpdate { update, bytes });
|
|
103
|
+
}
|
|
104
|
+
let _ = app.emit(
|
|
105
|
+
"app-update-state",
|
|
106
|
+
serde_json::json!({ "phase": "ready", "version": version }),
|
|
107
|
+
);
|
|
108
|
+
}
|