1use std::collections::BTreeMap;
2
3use reqwest::Method;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use super::{Client, Located, LocationInfo, LocationQuery, ModelRef, ServeDialect};
8use crate::Error;
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
11pub struct Health {
12 pub healthy: bool,
13 #[serde(flatten)]
15 pub extra: BTreeMap<String, Value>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
19#[serde(rename_all = "camelCase")]
20pub struct AgentInfo {
21 pub id: String,
22 #[serde(default)]
23 pub model: Option<ModelRef>,
24 pub request: Value,
25 #[serde(default)]
26 pub system: Option<String>,
27 #[serde(default)]
28 pub description: Option<String>,
29 pub mode: String,
30 pub hidden: bool,
31 #[serde(default)]
32 pub color: Option<Value>,
33 #[serde(default)]
34 pub steps: Option<u64>,
35 pub permissions: Value,
36 #[serde(flatten)]
37 pub extra: BTreeMap<String, Value>,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
41pub struct CommandInfo {
42 pub name: String,
43 #[serde(default)]
45 pub template: Option<String>,
46 #[serde(default)]
47 pub description: Option<String>,
48 #[serde(default)]
49 pub agent: Option<String>,
50 #[serde(default)]
51 pub model: Option<ModelRef>,
52 #[serde(default)]
53 pub subtask: Option<bool>,
54 #[serde(flatten)]
55 pub extra: BTreeMap<String, Value>,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
59pub struct SkillInfo {
60 pub name: String,
61 #[serde(default)]
62 pub description: Option<String>,
63 #[serde(default)]
64 pub slash: Option<bool>,
65 #[serde(default)]
67 pub location: Option<String>,
68 #[serde(default)]
69 pub content: String,
70 #[serde(flatten)]
71 pub extra: BTreeMap<String, Value>,
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
75pub struct ReferenceInfo {
76 pub name: String,
77 pub path: String,
78 #[serde(default)]
79 pub description: Option<String>,
80 #[serde(default)]
81 pub hidden: Option<bool>,
82 pub source: Value,
83 #[serde(flatten)]
84 pub extra: BTreeMap<String, Value>,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
88pub struct IntegrationInfo {
89 pub id: String,
90 pub name: String,
91 #[serde(default)]
92 pub methods: Vec<Value>,
93 #[serde(default)]
94 pub connections: Vec<Value>,
95 #[serde(flatten)]
96 pub extra: BTreeMap<String, Value>,
97}
98
99#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
100#[serde(rename_all = "camelCase")]
101pub struct IntegrationAttempt {
102 #[serde(rename = "attemptID")]
103 pub attempt_id: String,
104 pub url: String,
105 pub instructions: String,
106 pub mode: String,
107 pub time: Value,
108 #[serde(flatten)]
109 pub extra: BTreeMap<String, Value>,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
113pub struct IntegrationAttemptStatus {
114 pub status: String,
115 #[serde(default)]
116 pub message: Option<String>,
117 pub time: Value,
118 #[serde(flatten)]
119 pub extra: BTreeMap<String, Value>,
120}
121
122#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
123pub struct IntegrationKeyRequest {
124 pub key: String,
125 #[serde(default, skip_serializing_if = "Option::is_none")]
126 pub label: Option<String>,
127}
128
129#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
130#[serde(rename_all = "camelCase")]
131pub struct IntegrationOauthRequest {
132 #[serde(rename = "methodID")]
133 pub method_id: String,
134 #[serde(default)]
135 pub inputs: BTreeMap<String, String>,
136 #[serde(default, skip_serializing_if = "Option::is_none")]
137 pub label: Option<String>,
138}
139
140#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
141pub struct IntegrationAttemptCompleteRequest {
142 #[serde(default, skip_serializing_if = "Option::is_none")]
143 pub code: Option<String>,
144}
145
146#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
147pub struct CredentialUpdateRequest {
148 pub label: String,
149}
150
151pub struct HealthApi<'a> {
152 client: &'a Client,
153}
154
155impl HealthApi<'_> {
156 pub async fn get(&self) -> Result<Health, Error> {
158 match self.client.serve_dialect().await? {
159 ServeDialect::Preview1x => {
160 let response = self
161 .client
162 .inner
163 .request_base(Method::GET, "api/health")?
164 .send()
165 .await?;
166 self.client.inner.decode(response).await
167 }
168 ServeDialect::Native2x => {
169 let response = self
170 .client
171 .inner
172 .request_base(Method::GET, "api/info")?
173 .send()
174 .await?;
175 let info: Value = self.client.inner.decode(response).await?;
176 let mut extra = match info {
177 Value::Object(map) => map,
178 other => {
179 let mut map = serde_json::Map::new();
180 map.insert("info".to_owned(), other);
181 map
182 }
183 };
184 extra.remove("healthy");
185 Ok(Health {
186 healthy: true,
187 extra: extra.into_iter().collect(),
188 })
189 }
190 }
191 }
192}
193
194pub struct LocationApi<'a> {
195 client: &'a Client,
196}
197
198impl LocationApi<'_> {
199 pub async fn get(&self, location: Option<&LocationQuery>) -> Result<LocationInfo, Error> {
200 let mut url = self.client.inner.url("api/location")?;
201 super::resources::apply_location(&mut url, self.client, location);
202 let response = self
203 .client
204 .inner
205 .request_url(Method::GET, url)
206 .send()
207 .await?;
208 self.client.inner.decode(response).await
209 }
210}
211
212pub struct AgentApi<'a> {
213 client: &'a Client,
214}
215
216impl AgentApi<'_> {
217 pub async fn list(
218 &self,
219 location: Option<&LocationQuery>,
220 ) -> Result<Located<Vec<AgentInfo>>, Error> {
221 let mut url = self.client.inner.url("api/agent")?;
222 super::resources::apply_location(&mut url, self.client, location);
223 let response = self
224 .client
225 .inner
226 .request_url(Method::GET, url)
227 .send()
228 .await?;
229 self.client.inner.decode(response).await
230 }
231}
232
233pub struct CommandApi<'a> {
234 client: &'a Client,
235}
236
237impl CommandApi<'_> {
238 pub async fn list(
239 &self,
240 location: Option<&LocationQuery>,
241 ) -> Result<Located<Vec<CommandInfo>>, Error> {
242 let mut url = self.client.inner.url("api/command")?;
243 super::resources::apply_location(&mut url, self.client, location);
244 let response = self
245 .client
246 .inner
247 .request_url(Method::GET, url)
248 .send()
249 .await?;
250 self.client.inner.decode(response).await
251 }
252}
253
254pub struct SkillApi<'a> {
255 client: &'a Client,
256}
257
258impl SkillApi<'_> {
259 pub async fn list(
260 &self,
261 location: Option<&LocationQuery>,
262 ) -> Result<Located<Vec<SkillInfo>>, Error> {
263 let mut url = self.client.inner.url("api/skill")?;
264 super::resources::apply_location(&mut url, self.client, location);
265 let response = self
266 .client
267 .inner
268 .request_url(Method::GET, url)
269 .send()
270 .await?;
271 self.client.inner.decode(response).await
272 }
273}
274
275pub struct ReferenceApi<'a> {
276 client: &'a Client,
277}
278
279impl ReferenceApi<'_> {
280 pub async fn list(
281 &self,
282 location: Option<&LocationQuery>,
283 ) -> Result<Located<Vec<ReferenceInfo>>, Error> {
284 let mut url = self.client.inner.url("api/reference")?;
285 super::resources::apply_location(&mut url, self.client, location);
286 let response = self
287 .client
288 .inner
289 .request_url(Method::GET, url)
290 .send()
291 .await?;
292 self.client.inner.decode(response).await
293 }
294}
295
296pub struct IntegrationApi<'a> {
297 client: &'a Client,
298}
299
300impl IntegrationApi<'_> {
301 pub async fn list(
302 &self,
303 location: Option<&LocationQuery>,
304 ) -> Result<Located<Vec<IntegrationInfo>>, Error> {
305 let mut url = self.client.inner.url("api/integration")?;
306 super::resources::apply_location(&mut url, self.client, location);
307 let response = self
308 .client
309 .inner
310 .request_url(Method::GET, url)
311 .send()
312 .await?;
313 self.client.inner.decode(response).await
314 }
315
316 pub async fn get(
317 &self,
318 integration_id: &str,
319 location: Option<&LocationQuery>,
320 ) -> Result<Located<IntegrationInfo>, Error> {
321 let mut url = self
322 .client
323 .inner
324 .url(&format!("api/integration/{integration_id}"))?;
325 super::resources::apply_location(&mut url, self.client, location);
326 let response = self
327 .client
328 .inner
329 .request_url(Method::GET, url)
330 .send()
331 .await?;
332 self.client.inner.decode(response).await
333 }
334
335 pub fn connect(&self) -> IntegrationConnectApi<'_> {
336 IntegrationConnectApi {
337 client: self.client,
338 }
339 }
340
341 pub fn attempt(&self) -> IntegrationAttemptApi<'_> {
342 IntegrationAttemptApi {
343 client: self.client,
344 }
345 }
346}
347
348pub struct IntegrationConnectApi<'a> {
349 client: &'a Client,
350}
351
352impl IntegrationConnectApi<'_> {
353 pub async fn key(
354 &self,
355 integration_id: &str,
356 body: &IntegrationKeyRequest,
357 location: Option<&LocationQuery>,
358 ) -> Result<(), Error> {
359 let mut url = self
360 .client
361 .inner
362 .url(&format!("api/integration/{integration_id}/connect/key"))?;
363 super::resources::apply_location(&mut url, self.client, location);
364 let response = self
365 .client
366 .inner
367 .request_url(Method::POST, url)
368 .json(body)
369 .send()
370 .await?;
371 self.client.inner.ensure_success(response).await
372 }
373
374 pub async fn oauth(
375 &self,
376 integration_id: &str,
377 body: &IntegrationOauthRequest,
378 location: Option<&LocationQuery>,
379 ) -> Result<Located<IntegrationAttempt>, Error> {
380 let mut url = self
381 .client
382 .inner
383 .url(&format!("api/integration/{integration_id}/connect/oauth"))?;
384 super::resources::apply_location(&mut url, self.client, location);
385 let response = self
386 .client
387 .inner
388 .request_url(Method::POST, url)
389 .json(body)
390 .send()
391 .await?;
392 self.client.inner.decode(response).await
393 }
394}
395
396pub struct IntegrationAttemptApi<'a> {
397 client: &'a Client,
398}
399
400impl IntegrationAttemptApi<'_> {
401 pub async fn status(
408 &self,
409 attempt_id: &str,
410 location: Option<&LocationQuery>,
411 ) -> Result<Located<IntegrationAttemptStatus>, Error> {
412 if self.client.serve_dialect().await? == ServeDialect::Native2x {
413 return Err(Error::Unsupported(
414 "api/integration/attempt is 1.x only; use api/integration/{id}/connect/{command|oauth}/{attemptID} on 2.x",
415 ));
416 }
417 let mut url = self
418 .client
419 .inner
420 .url(&format!("api/integration/attempt/{attempt_id}"))?;
421 super::resources::apply_location(&mut url, self.client, location);
422 let response = self
423 .client
424 .inner
425 .request_url(Method::GET, url)
426 .send()
427 .await?;
428 self.client.inner.decode(response).await
429 }
430
431 pub async fn cancel(
432 &self,
433 attempt_id: &str,
434 location: Option<&LocationQuery>,
435 ) -> Result<(), Error> {
436 if self.client.serve_dialect().await? == ServeDialect::Native2x {
437 return Err(Error::Unsupported(
438 "api/integration/attempt is 1.x only; use api/integration/{id}/connect/{command|oauth}/{attemptID} on 2.x",
439 ));
440 }
441 let mut url = self
442 .client
443 .inner
444 .url(&format!("api/integration/attempt/{attempt_id}"))?;
445 super::resources::apply_location(&mut url, self.client, location);
446 let response = self
447 .client
448 .inner
449 .request_url(Method::DELETE, url)
450 .send()
451 .await?;
452 self.client.inner.ensure_success(response).await
453 }
454
455 pub async fn complete(
456 &self,
457 attempt_id: &str,
458 body: &IntegrationAttemptCompleteRequest,
459 location: Option<&LocationQuery>,
460 ) -> Result<(), Error> {
461 if self.client.serve_dialect().await? == ServeDialect::Native2x {
462 return Err(Error::Unsupported(
463 "api/integration/attempt is 1.x only; use api/integration/{id}/connect/{command|oauth}/{attemptID} on 2.x",
464 ));
465 }
466 let mut url = self
467 .client
468 .inner
469 .url(&format!("api/integration/attempt/{attempt_id}/complete"))?;
470 super::resources::apply_location(&mut url, self.client, location);
471 let response = self
472 .client
473 .inner
474 .request_url(Method::POST, url)
475 .json(body)
476 .send()
477 .await?;
478 self.client.inner.ensure_success(response).await
479 }
480}
481
482pub struct CredentialApi<'a> {
483 client: &'a Client,
484}
485
486impl CredentialApi<'_> {
487 pub async fn activate(
489 &self,
490 credential_id: &str,
491 location: Option<&LocationQuery>,
492 ) -> Result<(), Error> {
493 if self.client.serve_dialect().await? != ServeDialect::Native2x {
494 return Err(Error::Unsupported(
495 "api/credential/{id}/activate requires OpenCode 2.x",
496 ));
497 }
498 let mut url = self
499 .client
500 .inner
501 .url(&format!("api/credential/{credential_id}/activate"))?;
502 super::resources::apply_location(&mut url, self.client, location);
503 let response = self
504 .client
505 .inner
506 .request_url(Method::POST, url)
507 .send()
508 .await?;
509 self.client.inner.ensure_success(response).await
510 }
511
512 pub async fn update(
513 &self,
514 credential_id: &str,
515 body: &CredentialUpdateRequest,
516 location: Option<&LocationQuery>,
517 ) -> Result<(), Error> {
518 let mut url = self
519 .client
520 .inner
521 .url(&format!("api/credential/{credential_id}"))?;
522 super::resources::apply_location(&mut url, self.client, location);
523 let response = self
524 .client
525 .inner
526 .request_url(Method::PATCH, url)
527 .json(body)
528 .send()
529 .await?;
530 self.client.inner.ensure_success(response).await
531 }
532
533 pub async fn remove(
534 &self,
535 credential_id: &str,
536 location: Option<&LocationQuery>,
537 ) -> Result<(), Error> {
538 let mut url = self
539 .client
540 .inner
541 .url(&format!("api/credential/{credential_id}"))?;
542 super::resources::apply_location(&mut url, self.client, location);
543 let response = self
544 .client
545 .inner
546 .request_url(Method::DELETE, url)
547 .send()
548 .await?;
549 self.client.inner.ensure_success(response).await
550 }
551}
552
553impl Client {
554 pub fn health(&self) -> HealthApi<'_> {
555 HealthApi { client: self }
556 }
557
558 pub fn location(&self) -> LocationApi<'_> {
559 LocationApi { client: self }
560 }
561
562 pub fn agent(&self) -> AgentApi<'_> {
563 AgentApi { client: self }
564 }
565
566 pub fn command(&self) -> CommandApi<'_> {
567 CommandApi { client: self }
568 }
569
570 pub fn skill(&self) -> SkillApi<'_> {
571 SkillApi { client: self }
572 }
573
574 pub fn reference(&self) -> ReferenceApi<'_> {
575 ReferenceApi { client: self }
576 }
577
578 pub fn integration(&self) -> IntegrationApi<'_> {
579 IntegrationApi { client: self }
580 }
581
582 pub fn credential(&self) -> CredentialApi<'_> {
583 CredentialApi { client: self }
584 }
585}