github_copilot_sdk/provider_token.rs
1/*---------------------------------------------------------------------------------------------
2 * Copyright (c) Microsoft Corporation. All rights reserved.
3 *--------------------------------------------------------------------------------------------*/
4
5//! BYOK bearer-token provider callbacks.
6//!
7//! <div class="warning">
8//!
9//! **Experimental.** These types are part of an experimental wire-protocol
10//! surface and may change or be removed in future SDK or CLI releases.
11//!
12//! </div>
13
14use std::future::Future;
15
16use async_trait::async_trait;
17
18/// Arguments passed to a BYOK bearer-token provider callback.
19///
20/// <div class="warning">
21///
22/// **Experimental.** This type is part of an experimental wire-protocol
23/// surface and may change or be removed in future SDK or CLI releases.
24///
25/// </div>
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct ProviderTokenArgs {
28 /// Name of the BYOK provider needing a token.
29 ///
30 /// This is `"default"` for the singular whole-session provider, otherwise
31 /// the named provider's `name`.
32 pub provider_name: String,
33}
34
35/// Error returned by a [`BearerTokenProvider`].
36///
37/// <div class="warning">
38///
39/// **Experimental.** This type is part of an experimental wire-protocol
40/// surface and may change or be removed in future SDK or CLI releases.
41///
42/// </div>
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct BearerTokenError {
45 message: String,
46}
47
48impl BearerTokenError {
49 /// Construct a bearer-token error with a human-readable message.
50 pub fn message(message: impl Into<String>) -> Self {
51 Self {
52 message: message.into(),
53 }
54 }
55
56 /// Return the human-readable error message.
57 pub fn as_str(&self) -> &str {
58 &self.message
59 }
60}
61
62impl std::fmt::Display for BearerTokenError {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 f.write_str(&self.message)
65 }
66}
67
68impl std::error::Error for BearerTokenError {}
69
70impl From<String> for BearerTokenError {
71 fn from(message: String) -> Self {
72 Self::message(message)
73 }
74}
75
76impl From<&str> for BearerTokenError {
77 fn from(message: &str) -> Self {
78 Self::message(message)
79 }
80}
81
82/// Provider-side callback used to acquire bearer tokens for BYOK providers.
83///
84/// <div class="warning">
85///
86/// **Experimental.** This trait is part of an experimental wire-protocol
87/// surface and may change or be removed in future SDK or CLI releases.
88///
89/// </div>
90#[async_trait]
91pub trait BearerTokenProvider: Send + Sync {
92 /// Acquire a bearer token without the `Bearer ` prefix.
93 async fn get_token(&self, args: ProviderTokenArgs) -> Result<String, BearerTokenError>;
94}
95
96#[async_trait]
97impl<F, Fut> BearerTokenProvider for F
98where
99 F: Fn(ProviderTokenArgs) -> Fut + Send + Sync,
100 Fut: Future<Output = Result<String, BearerTokenError>> + Send,
101{
102 async fn get_token(&self, args: ProviderTokenArgs) -> Result<String, BearerTokenError> {
103 (self)(args).await
104 }
105}