1use std::sync::Arc;
2
3use crate::{
4 types::{PerspectiveExpression, SentPerspectiveMessage},
5 util::query,
6 ClientInfo,
7};
8use anyhow::{Context, Result};
9use graphql_client::GraphQLQuery;
10
11#[derive(GraphQLQuery)]
12#[graphql(
13 schema_path = "schema.gql",
14 query_path = "src/runtime.gql",
15 response_derives = "Debug"
16)]
17pub struct Info;
18
19pub async fn info(executor_url: String, cap_token: String) -> Result<info::InfoRuntimeInfo> {
20 let response_data: info::ResponseData = query(
21 executor_url,
22 cap_token,
23 Info::build_query(info::Variables {}),
24 )
25 .await
26 .with_context(|| "Failed to run runtime->info query")?;
27 Ok(response_data.runtime_info)
28}
29
30#[derive(GraphQLQuery)]
31#[graphql(
32 schema_path = "schema.gql",
33 query_path = "src/runtime.gql",
34 response_derives = "Debug"
35)]
36pub struct Quit;
37
38pub async fn quit(executor_url: String, cap_token: String) -> Result<quit::ResponseData> {
39 query(
40 executor_url,
41 cap_token,
42 Quit::build_query(quit::Variables {}),
43 )
44 .await
45 .with_context(|| "Failed to run runtime->quit query")
46}
47
48#[derive(GraphQLQuery)]
49#[graphql(
50 schema_path = "schema.gql",
51 query_path = "src/runtime.gql",
52 response_derives = "Debug"
53)]
54pub struct AddTrustedAgents;
55
56pub async fn add_trusted_agents(
57 executor_url: String,
58 cap_token: String,
59 agents: Vec<String>,
60) -> Result<add_trusted_agents::ResponseData> {
61 query(
62 executor_url,
63 cap_token,
64 AddTrustedAgents::build_query(add_trusted_agents::Variables { agents }),
65 )
66 .await
67 .with_context(|| "Failed to run runtime->add-trusted-agents query")
68}
69
70#[derive(GraphQLQuery)]
71#[graphql(
72 schema_path = "schema.gql",
73 query_path = "src/runtime.gql",
74 response_derives = "Debug"
75)]
76pub struct DeleteTrustedAgents;
77
78pub async fn delete_trusted_agents(
79 executor_url: String,
80 cap_token: String,
81 agents: Vec<String>,
82) -> Result<delete_trusted_agents::ResponseData> {
83 query(
84 executor_url,
85 cap_token,
86 DeleteTrustedAgents::build_query(delete_trusted_agents::Variables { agents }),
87 )
88 .await
89 .with_context(|| "Failed to run runtime -> delete-trusted-agents query")
90}
91
92#[derive(GraphQLQuery)]
93#[graphql(
94 schema_path = "schema.gql",
95 query_path = "src/runtime.gql",
96 response_derives = "Debug"
97)]
98pub struct TrustedAgents;
99
100pub async fn trusted_agents(executor_url: String, cap_token: String) -> Result<Vec<String>> {
101 let response_data: trusted_agents::ResponseData = query(
102 executor_url,
103 cap_token,
104 TrustedAgents::build_query(trusted_agents::Variables {}),
105 )
106 .await
107 .with_context(|| "Failed to run runtime->trusted-agents query")?;
108 Ok(response_data.get_trusted_agents)
109}
110
111#[derive(GraphQLQuery)]
112#[graphql(
113 schema_path = "schema.gql",
114 query_path = "src/runtime.gql",
115 response_derives = "Debug"
116)]
117pub struct LinkLanguageTemplates;
118
119pub async fn link_language_templates(
120 executor_url: String,
121 cap_token: String,
122) -> Result<Vec<String>> {
123 let response_data: link_language_templates::ResponseData = query(
124 executor_url,
125 cap_token,
126 LinkLanguageTemplates::build_query(link_language_templates::Variables {}),
127 )
128 .await
129 .with_context(|| "Failed to run runtime->link-language-templates query")?;
130
131 Ok(response_data.runtime_known_link_language_templates)
132}
133
134#[derive(GraphQLQuery)]
135#[graphql(
136 schema_path = "schema.gql",
137 query_path = "src/runtime.gql",
138 response_derives = "Debug"
139)]
140pub struct AddLinkLanguageTemplates;
141
142pub async fn add_link_language_templates(
143 executor_url: String,
144 cap_token: String,
145 addresses: Vec<String>,
146) -> Result<add_link_language_templates::ResponseData> {
147 query(
148 executor_url,
149 cap_token,
150 AddLinkLanguageTemplates::build_query(add_link_language_templates::Variables { addresses }),
151 )
152 .await
153 .with_context(|| "Failed to run runtime->add-link-language-templates query")
154}
155
156#[derive(GraphQLQuery)]
157#[graphql(
158 schema_path = "schema.gql",
159 query_path = "src/runtime.gql",
160 response_derives = "Debug"
161)]
162pub struct RemoveLinkLanguageTemplates;
163
164pub async fn remove_link_language_templates(
165 executor_url: String,
166 cap_token: String,
167 addresses: Vec<String>,
168) -> Result<remove_link_language_templates::ResponseData> {
169 query(
170 executor_url,
171 cap_token,
172 RemoveLinkLanguageTemplates::build_query(remove_link_language_templates::Variables {
173 addresses,
174 }),
175 )
176 .await
177 .with_context(|| "Failed to run runtime->remove-link-language-templates query")
178}
179
180#[derive(GraphQLQuery)]
181#[graphql(
182 schema_path = "schema.gql",
183 query_path = "src/runtime.gql",
184 response_derives = "Debug"
185)]
186pub struct Friends;
187
188pub async fn friends(executor_url: String, cap_token: String) -> Result<Vec<String>> {
189 let response_data: friends::ResponseData = query(
190 executor_url,
191 cap_token,
192 Friends::build_query(friends::Variables {}),
193 )
194 .await
195 .with_context(|| "Failed to run runtime->friends query")?;
196 Ok(response_data.runtime_friends)
197}
198
199#[derive(GraphQLQuery)]
200#[graphql(
201 schema_path = "schema.gql",
202 query_path = "src/runtime.gql",
203 response_derives = "Debug"
204)]
205pub struct AddFriends;
206
207pub async fn add_friends(
208 executor_url: String,
209 cap_token: String,
210 dids: Vec<String>,
211) -> Result<add_friends::ResponseData> {
212 query(
213 executor_url,
214 cap_token,
215 AddFriends::build_query(add_friends::Variables { dids }),
216 )
217 .await
218 .with_context(|| "Failed to run runtime->add-friends query")
219}
220
221#[derive(GraphQLQuery)]
222#[graphql(
223 schema_path = "schema.gql",
224 query_path = "src/runtime.gql",
225 response_derives = "Debug"
226)]
227pub struct RemoveFriends;
228
229pub async fn remove_friends(
230 executor_url: String,
231 cap_token: String,
232 dids: Vec<String>,
233) -> Result<remove_friends::ResponseData> {
234 query(
235 executor_url,
236 cap_token,
237 RemoveFriends::build_query(remove_friends::Variables { dids }),
238 )
239 .await
240 .with_context(|| "Failed to run runtime->remove-friends query")
241}
242
243#[derive(GraphQLQuery)]
244#[graphql(
245 schema_path = "schema.gql",
246 query_path = "src/runtime.gql",
247 response_derives = "Debug"
248)]
249pub struct HcAgentInfos;
250
251pub async fn hc_agent_infos(executor_url: String, cap_token: String) -> Result<String> {
252 let response_data: hc_agent_infos::ResponseData = query(
253 executor_url,
254 cap_token,
255 HcAgentInfos::build_query(hc_agent_infos::Variables {}),
256 )
257 .await
258 .with_context(|| "Failed to run runtime->hc-agent-infos query")?;
259 Ok(response_data.runtime_hc_agent_infos)
260}
261
262#[derive(GraphQLQuery)]
263#[graphql(
264 schema_path = "schema.gql",
265 query_path = "src/runtime.gql",
266 response_derives = "Debug"
267)]
268pub struct HcAddAgentInfos;
269
270pub async fn hc_add_agent_infos(
271 executor_url: String,
272 cap_token: String,
273 agent_infos: String,
274) -> Result<hc_add_agent_infos::ResponseData> {
275 query(
276 executor_url,
277 cap_token,
278 HcAddAgentInfos::build_query(hc_add_agent_infos::Variables { agent_infos }),
279 )
280 .await
281 .with_context(|| "Failed to run runtime->hc-add-agent-infos query")
282}
283
284#[derive(GraphQLQuery)]
285#[graphql(
286 schema_path = "schema.gql",
287 query_path = "src/runtime.gql",
288 response_derives = "Debug"
289)]
290pub struct VerifyStringSignedByDid;
291
292pub async fn verify_string_signed_by_did(
293 executor_url: String,
294 cap_token: String,
295 did: String,
296 did_signing_key_id: String,
297 data: String,
298 signed_data: String,
299) -> Result<verify_string_signed_by_did::ResponseData> {
300 query(
301 executor_url,
302 cap_token,
303 VerifyStringSignedByDid::build_query(verify_string_signed_by_did::Variables {
304 did,
305 did_signing_key_id,
306 data,
307 signed_data,
308 }),
309 )
310 .await
311 .with_context(|| "Failed to run runtime->verify-string-signed-by-did query")
312}
313
314#[derive(GraphQLQuery)]
315#[graphql(
316 schema_path = "schema.gql",
317 query_path = "src/runtime.gql",
318 response_derives = "Debug"
319)]
320pub struct SetStatus;
321
322pub async fn set_status(
323 executor_url: String,
324 cap_token: String,
325 status: set_status::PerspectiveInput,
326) -> Result<set_status::ResponseData> {
327 query(
328 executor_url,
329 cap_token,
330 SetStatus::build_query(set_status::Variables { status }),
331 )
332 .await
333 .with_context(|| "Failed to run runtime->set-status query")
334}
335
336#[derive(GraphQLQuery)]
337#[graphql(
338 schema_path = "schema.gql",
339 query_path = "src/runtime.gql",
340 response_derives = "Debug"
341)]
342pub struct FriendStatus;
343
344pub async fn friend_status(
345 executor_url: String,
346 cap_token: String,
347 did: String,
348) -> Result<friend_status::ResponseData> {
349 query(
350 executor_url,
351 cap_token,
352 FriendStatus::build_query(friend_status::Variables { did }),
353 )
354 .await
355 .with_context(|| "Failed to run runtime->friend-status query")
356}
357
358#[derive(GraphQLQuery)]
359#[graphql(
360 schema_path = "schema.gql",
361 query_path = "src/runtime.gql",
362 response_derives = "Debug"
363)]
364pub struct FriendSendMessage;
365
366pub async fn friend_send_message(
367 executor_url: String,
368 cap_token: String,
369 did: String,
370 message: friend_send_message::PerspectiveInput,
371) -> Result<friend_send_message::ResponseData> {
372 query(
373 executor_url,
374 cap_token,
375 FriendSendMessage::build_query(friend_send_message::Variables { did, message }),
376 )
377 .await
378 .with_context(|| "Failed to run runtime->friend-send-message query")
379}
380
381#[derive(GraphQLQuery)]
382#[graphql(
383 schema_path = "schema.gql",
384 query_path = "src/runtime.gql",
385 response_derives = "Debug"
386)]
387pub struct MessageInbox;
388
389pub async fn message_inbox(
390 executor_url: String,
391 cap_token: String,
392 filter: Option<String>,
393) -> Result<Vec<PerspectiveExpression>> {
394 let response: message_inbox::ResponseData = query(
395 executor_url,
396 cap_token,
397 MessageInbox::build_query(message_inbox::Variables { filter }),
398 )
399 .await
400 .with_context(|| "Failed to run runtime->message-inbox query")?;
401
402 Ok(response
403 .runtime_message_inbox
404 .into_iter()
405 .map(|d| d.into())
406 .collect())
407}
408
409#[derive(GraphQLQuery)]
410#[graphql(
411 schema_path = "schema.gql",
412 query_path = "src/runtime.gql",
413 response_derives = "Debug"
414)]
415pub struct MessageOutbox;
416
417pub async fn message_outbox(
418 executor_url: String,
419 cap_token: String,
420 filter: Option<String>,
421) -> Result<Vec<SentPerspectiveMessage>> {
422 let response: message_outbox::ResponseData = query(
423 executor_url,
424 cap_token,
425 MessageOutbox::build_query(message_outbox::Variables { filter }),
426 )
427 .await
428 .with_context(|| "Failed to run runtime->message-outbox query")?;
429
430 Ok(response
431 .runtime_message_outbox
432 .into_iter()
433 .map(|d| d.into())
434 .collect())
435}
436
437pub struct RuntimeClient {
438 info: Arc<ClientInfo>,
439}
440
441impl RuntimeClient {
442 pub fn new(info: Arc<ClientInfo>) -> Self {
443 Self { info }
444 }
445
446 pub async fn info(&self) -> Result<info::InfoRuntimeInfo> {
447 info(self.info.executor_url.clone(), self.info.cap_token.clone()).await
448 }
449
450 pub async fn quit(&self) -> Result<quit::ResponseData> {
451 quit(self.info.executor_url.clone(), self.info.cap_token.clone()).await
452 }
453
454 pub async fn add_trusted_agents(
455 &self,
456 agents: Vec<String>,
457 ) -> Result<add_trusted_agents::ResponseData> {
458 add_trusted_agents(
459 self.info.executor_url.clone(),
460 self.info.cap_token.clone(),
461 agents,
462 )
463 .await
464 }
465
466 pub async fn delete_trusted_agents(
467 &self,
468 agents: Vec<String>,
469 ) -> Result<delete_trusted_agents::ResponseData> {
470 delete_trusted_agents(
471 self.info.executor_url.clone(),
472 self.info.cap_token.clone(),
473 agents,
474 )
475 .await
476 }
477
478 pub async fn trusted_agents(&self) -> Result<Vec<String>> {
479 trusted_agents(self.info.executor_url.clone(), self.info.cap_token.clone()).await
480 }
481
482 pub async fn link_language_templates(&self) -> Result<Vec<String>> {
483 link_language_templates(self.info.executor_url.clone(), self.info.cap_token.clone()).await
484 }
485
486 pub async fn add_link_language_templates(
487 &self,
488 addresses: Vec<String>,
489 ) -> Result<add_link_language_templates::ResponseData> {
490 add_link_language_templates(
491 self.info.executor_url.clone(),
492 self.info.cap_token.clone(),
493 addresses,
494 )
495 .await
496 }
497
498 pub async fn remove_link_language_templates(
499 &self,
500 addresses: Vec<String>,
501 ) -> Result<remove_link_language_templates::ResponseData> {
502 remove_link_language_templates(
503 self.info.executor_url.clone(),
504 self.info.cap_token.clone(),
505 addresses,
506 )
507 .await
508 }
509
510 pub async fn friends(&self) -> Result<Vec<String>> {
511 friends(self.info.executor_url.clone(), self.info.cap_token.clone()).await
512 }
513
514 pub async fn add_friends(&self, friends: Vec<String>) -> Result<add_friends::ResponseData> {
515 add_friends(
516 self.info.executor_url.clone(),
517 self.info.cap_token.clone(),
518 friends,
519 )
520 .await
521 }
522
523 pub async fn remove_friends(
524 &self,
525 friends: Vec<String>,
526 ) -> Result<remove_friends::ResponseData> {
527 remove_friends(
528 self.info.executor_url.clone(),
529 self.info.cap_token.clone(),
530 friends,
531 )
532 .await
533 }
534
535 pub async fn hc_agent_infos(&self) -> Result<String> {
536 hc_agent_infos(self.info.executor_url.clone(), self.info.cap_token.clone()).await
537 }
538
539 pub async fn hc_add_agent_infos(
540 &self,
541 agent_infos: String,
542 ) -> Result<hc_add_agent_infos::ResponseData> {
543 hc_add_agent_infos(
544 self.info.executor_url.clone(),
545 self.info.cap_token.clone(),
546 agent_infos,
547 )
548 .await
549 }
550
551 pub async fn verify_string_signed_by_did(
552 &self,
553 did: String,
554 did_signing_key_id: String,
555 data: String,
556 signed_data: String,
557 ) -> Result<bool> {
558 let response = verify_string_signed_by_did(
559 self.info.executor_url.clone(),
560 self.info.cap_token.clone(),
561 did,
562 did_signing_key_id,
563 data,
564 signed_data,
565 )
566 .await?;
567
568 Ok(response.runtime_verify_string_signed_by_did)
569 }
570
571 pub async fn set_status(&self, status: set_status::PerspectiveInput) -> Result<()> {
572 set_status(
573 self.info.executor_url.clone(),
574 self.info.cap_token.clone(),
575 status,
576 )
577 .await?;
578
579 Ok(())
580 }
581
582 pub async fn friend_status(&self, did: String) -> Result<friend_status::ResponseData> {
583 friend_status(
584 self.info.executor_url.clone(),
585 self.info.cap_token.clone(),
586 did,
587 )
588 .await
589 }
590
591 pub async fn friend_send_message(
592 &self,
593 did: String,
594 message: friend_send_message::PerspectiveInput,
595 ) -> Result<friend_send_message::ResponseData> {
596 friend_send_message(
597 self.info.executor_url.clone(),
598 self.info.cap_token.clone(),
599 did,
600 message,
601 )
602 .await
603 }
604
605 pub async fn message_inbox(
606 &self,
607 filter: Option<String>,
608 ) -> Result<Vec<PerspectiveExpression>> {
609 message_inbox(
610 self.info.executor_url.clone(),
611 self.info.cap_token.clone(),
612 filter,
613 )
614 .await
615 }
616
617 pub async fn message_outbox(
618 &self,
619 filter: Option<String>,
620 ) -> Result<Vec<SentPerspectiveMessage>> {
621 message_outbox(
622 self.info.executor_url.clone(),
623 self.info.cap_token.clone(),
624 filter,
625 )
626 .await
627 }
628}