Skip to main content

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    /// Id of the session that triggered this token request.
35    ///
36    /// A client-level shared callback registered for many sessions can use this
37    /// to resolve the owning session and scope token acquisition or caching per
38    /// session.
39    pub session_id: String,
40}
41
42/// Error returned by a [`BearerTokenProvider`].
43///
44/// <div class="warning">
45///
46/// **Experimental.** This type is part of an experimental wire-protocol
47/// surface and may change or be removed in future SDK or CLI releases.
48///
49/// </div>
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct BearerTokenError {
52    message: String,
53}
54
55impl BearerTokenError {
56    /// Construct a bearer-token error with a human-readable message.
57    pub fn message(message: impl Into<String>) -> Self {
58        Self {
59            message: message.into(),
60        }
61    }
62
63    /// Return the human-readable error message.
64    pub fn as_str(&self) -> &str {
65        &self.message
66    }
67}
68
69impl std::fmt::Display for BearerTokenError {
70    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71        f.write_str(&self.message)
72    }
73}
74
75impl std::error::Error for BearerTokenError {}
76
77impl From<String> for BearerTokenError {
78    fn from(message: String) -> Self {
79        Self::message(message)
80    }
81}
82
83impl From<&str> for BearerTokenError {
84    fn from(message: &str) -> Self {
85        Self::message(message)
86    }
87}
88
89/// Provider-side callback used to acquire bearer tokens for BYOK providers.
90///
91/// <div class="warning">
92///
93/// **Experimental.** This trait is part of an experimental wire-protocol
94/// surface and may change or be removed in future SDK or CLI releases.
95///
96/// </div>
97#[async_trait]
98pub trait BearerTokenProvider: Send + Sync {
99    /// Acquire a bearer token without the `Bearer ` prefix.
100    async fn get_token(&self, args: ProviderTokenArgs) -> Result<String, BearerTokenError>;
101}
102
103#[async_trait]
104impl<F, Fut> BearerTokenProvider for F
105where
106    F: Fn(ProviderTokenArgs) -> Fut + Send + Sync,
107    Fut: Future<Output = Result<String, BearerTokenError>> + Send,
108{
109    async fn get_token(&self, args: ProviderTokenArgs) -> Result<String, BearerTokenError> {
110        (self)(args).await
111    }
112}