-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.rs
More file actions
255 lines (232 loc) · 8.26 KB
/
Copy pathmodule.rs
File metadata and controls
255 lines (232 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::sync::Arc;
use a3s_boot::{Module, ProviderDefinition, ProviderToken, Result as BootResult};
use super::capabilities::CapabilitiesModule;
use super::code_intelligence::CodeIntelligenceModule;
use super::config::ConfigModule;
use super::context::ContextModule;
use super::evolution::EvolutionModule;
use super::health::HealthModule;
use super::kernel::KernelModule;
use super::knowledge::KnowledgeModule;
use super::loops::LoopsModule;
use super::os::OsModule;
use super::plugins::PluginsModule;
use super::previews::PreviewsModule;
use super::processes::ProcessesModule;
use super::state::CodeWebState;
use super::weixin::WeixinModule;
use super::work::WorkModule;
use super::workspace::WorkspaceModule;
pub(in crate::api) struct CodeWebModule {
state: Arc<CodeWebState>,
}
impl CodeWebModule {
pub(in crate::api) fn new(state: Arc<CodeWebState>) -> Self {
Self { state }
}
}
impl Module for CodeWebModule {
fn name(&self) -> &'static str {
"a3s-code-web"
}
fn imports(&self) -> Vec<Arc<dyn Module>> {
vec![
Arc::new(CodeWebStateModule::new(Arc::clone(&self.state))),
Arc::new(HealthModule),
Arc::new(ConfigModule),
Arc::new(WorkModule),
Arc::new(WorkspaceModule),
Arc::new(CodeIntelligenceModule),
Arc::new(CapabilitiesModule),
Arc::new(KnowledgeModule),
Arc::new(ContextModule),
Arc::new(EvolutionModule),
Arc::new(KernelModule),
Arc::new(ProcessesModule),
Arc::new(PreviewsModule),
Arc::new(LoopsModule),
Arc::new(PluginsModule),
Arc::new(OsModule),
Arc::new(WeixinModule::configured()),
]
}
}
struct CodeWebStateModule {
state: Arc<CodeWebState>,
}
impl CodeWebStateModule {
fn new(state: Arc<CodeWebState>) -> Self {
Self { state }
}
}
impl Module for CodeWebStateModule {
fn name(&self) -> &'static str {
"a3s-code-web-state"
}
fn providers(&self) -> BootResult<Vec<ProviderDefinition>> {
Ok(vec![ProviderDefinition::from_arc(Arc::clone(&self.state))])
}
fn exports(&self) -> BootResult<Vec<ProviderToken>> {
Ok(vec![ProviderToken::of::<CodeWebState>()])
}
fn is_global(&self) -> bool {
true
}
fn on_application_shutdown(
&self,
_module_ref: a3s_boot::ModuleRef,
) -> a3s_boot::BoxFuture<'static, BootResult<()>> {
let state = Arc::clone(&self.state);
Box::pin(async move {
state.close().await;
Ok(())
})
}
}
#[cfg(test)]
mod tests {
use a3s_boot::{BootApplication, BootRequest, HttpMethod};
use super::*;
#[tokio::test]
async fn complete_code_web_module_builds_with_nested_remote_kernel_imports() {
let temporary = tempfile::tempdir().expect("create Code Web module fixture");
let workspace = temporary.path().join("workspace");
std::fs::create_dir_all(&workspace).expect("create fixture workspace");
let code_config = a3s_code_core::CodeConfig::from_acl(
r#"
default_model = "openai/test-model"
providers "openai" {
apiKey = "sk-test"
baseUrl = "https://example.com/v1"
models "test-model" {}
}
"#,
)
.expect("parse fixture config");
let agent = Arc::new(
a3s_code_core::Agent::from_config(code_config.clone())
.await
.expect("create fixture agent"),
);
let repository = Arc::new(
super::super::session_store::CodeWebSessionRepository::open(
temporary.path().join("sessions"),
)
.await
.expect("open fixture session repository"),
);
let state = Arc::new(CodeWebState::new(
agent,
temporary.path().join("config.acl"),
workspace,
code_config,
repository,
));
let app = BootApplication::builder()
.global_prefix("/api")
.import(CodeWebModule::new(Arc::clone(&state)))
.build()
.expect("build complete Code Web application");
let capability = app
.call(BootRequest::new(
HttpMethod::Get,
"/api/v1/weixin/capability",
))
.await
.expect("read built-in Weixin capability")
.body_json::<serde_json::Value>()
.expect("decode capability");
assert_eq!(capability["state"], "unbound");
assert_eq!(capability["protocolMode"], "tencent");
assert_eq!(capability["schemaVersion"], 2);
assert_eq!(capability["releaseBlockers"], serde_json::json!([]));
let targets = app
.call(BootRequest::new(HttpMethod::Get, "/api/v1/weixin/targets"))
.await
.expect("read remote target snapshot")
.body_json::<serde_json::Value>()
.expect("decode target snapshot");
assert_eq!(targets["schemaVersion"], 1);
assert!(
targets["items"].is_array(),
"system-agent discovery is host-dependent but must return an item array"
);
assert_eq!(targets["warnings"], serde_json::json!([]));
app.shutdown().await.expect("shutdown Code Web application");
}
#[tokio::test]
#[cfg(unix)]
async fn complete_code_web_module_honors_explicit_weixin_enable() {
let temporary = tempfile::tempdir().expect("create Code Web module fixture");
let workspace = temporary.path().join("workspace");
std::fs::create_dir_all(&workspace).expect("create fixture workspace");
let source = r#"
default_model = "openai/test-model"
providers "openai" {
apiKey = "sk-test"
baseUrl = "https://example.com/v1"
models "test-model" {}
}
channels {
weixin {
enabled = true
}
}
"#;
let config_path = temporary.path().join("config.acl");
std::fs::write(&config_path, source).expect("write configured fixture");
let code_config =
a3s_code_core::CodeConfig::from_acl(source).expect("parse configured fixture");
let agent = Arc::new(
a3s_code_core::Agent::from_config(code_config.clone())
.await
.expect("create fixture agent"),
);
let repository = Arc::new(
super::super::session_store::CodeWebSessionRepository::open(
temporary.path().join("sessions"),
)
.await
.expect("open fixture session repository"),
);
let state = Arc::new(CodeWebState::new(
agent,
config_path,
workspace,
code_config,
repository,
));
let app = BootApplication::builder()
.global_prefix("/api")
.import(CodeWebModule::new(state))
.build()
.expect("build configured Code Web application");
app.bootstrap()
.await
.expect("bootstrap configured Code Web application");
let capability = app
.call(BootRequest::new(
HttpMethod::Get,
"/api/v1/weixin/capability",
))
.await
.expect("read configured Weixin capability")
.body_json::<serde_json::Value>()
.expect("decode configured capability");
assert_eq!(capability["state"], "unbound");
assert_eq!(capability["protocolMode"], "tencent");
assert_eq!(capability["schemaVersion"], 2);
assert_eq!(capability["releaseBlockers"], serde_json::json!([]));
let account = app
.call(BootRequest::new(HttpMethod::Get, "/api/v1/weixin/account"))
.await
.expect("read configured Weixin account")
.body_json::<serde_json::Value>()
.expect("decode configured account");
assert_eq!(account["bound"], false);
assert_eq!(account["protocolMode"], "tencent");
app.shutdown()
.await
.expect("shutdown configured Code Web application");
}
}