-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Rust SDK: add typed per-session capability controls #1455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Morabbin
wants to merge
2
commits into
github:main
Choose a base branch
from
Morabbin:morabbin/rust-typed-session-capabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+716
−2
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -402,6 +402,162 @@ impl OtelExporterType { | |
| } | ||
| } | ||
|
|
||
| /// A named session capability sent in the `session.create` and | ||
| /// `session.resume` wire payloads. | ||
| /// | ||
| /// Capabilities gate optional CLI features (extra tools, system-prompt | ||
| /// sections, host-rendered surfaces). The runtime starts from a | ||
| /// hard-coded `SDK_CAPABILITIES` set; use | ||
| /// [`SessionConfig::with_enable_capability`] / | ||
| /// [`SessionConfig::with_disable_capability`] (and their plural | ||
| /// counterparts) to opt individual sessions in or out. | ||
| /// | ||
| /// > **Not** the same as [`SessionCapabilities`] — that struct is the | ||
| /// > *runtime-negotiated* capability descriptor reported by the CLI on | ||
| /// > `session.create`. [`SessionCapability`] is the *opt-in / opt-out | ||
| /// > toggle name* sent with each `session.create` / `session.resume`. | ||
| /// > | ||
| /// > This public type is also separate from the generated protocol enum: | ||
| /// > unknown generated enum values collapse to `Unknown`, while callers | ||
| /// > need [`Other`](Self::Other) to preserve and send capability names | ||
| /// > introduced by newer runtimes. | ||
| /// | ||
| /// The runtime's overlap semantics are **disable-wins**: if a capability | ||
| /// appears in both the enabled and disabled lists, the disable wins. | ||
| /// The SDK preserves the order callers add capabilities in so the | ||
| /// resulting wire payload is deterministic. | ||
| /// | ||
| /// The enum is `#[non_exhaustive]` and carries an [`Other`](Self::Other) | ||
| /// variant so forward-compat capabilities the runtime grows ahead of an | ||
| /// SDK release can still be opted into without waiting for a new | ||
| /// enum variant. | ||
| /// | ||
| /// Requires github/copilot-agent-runtime#8918 or later. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid referring to closed source. There are a few comments like this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix incoming; removing all mentions from all parts of the PR |
||
| #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
| #[non_exhaustive] | ||
| pub enum SessionCapability { | ||
| /// TUI-only prompt hints (keyboard shortcuts). | ||
| TuiHints, | ||
| /// `[[PLAN]]` handling and plan-mode instructions. | ||
| PlanMode, | ||
| /// `store_memory` tool and the `<memories>` system-prompt section. | ||
| Memory, | ||
| /// `fetch_copilot_cli_documentation` tool plus the | ||
| /// `<self_documentation>` system-prompt section. | ||
| CliDocumentation, | ||
| /// `ask_user` tool for interactive clarification. | ||
| AskUser, | ||
| /// Interactive-CLI identity (vs non-interactive / headless). | ||
| InteractiveMode, | ||
| /// Automatic system notifications to the agent (batched, hidden | ||
| /// from the user timeline). | ||
| SystemNotifications, | ||
| /// Elicitation support (confirm / select / input prompts). | ||
| Elicitation, | ||
| /// Cross-session history tools and session-store prompt/tool metadata. | ||
| SessionStore, | ||
| /// MCP-Apps (SEP-1865) `ui://` resource passthrough. | ||
| McpApps, | ||
| /// Extension-provided canvases rendered by the host. | ||
| CanvasRenderer, | ||
| /// A capability name the SDK doesn't have a typed variant for yet. | ||
| /// | ||
| /// Pass any kebab-case capability string here to forward it | ||
| /// verbatim to the runtime. | ||
| Other(String), | ||
| } | ||
|
|
||
| impl SessionCapability { | ||
| /// The kebab-case wire string sent in `enabledCapabilities` / | ||
| /// `disabledCapabilities` on `session.create` and `session.resume`. | ||
| pub fn as_str(&self) -> &str { | ||
| match self { | ||
| Self::TuiHints => "tui-hints", | ||
| Self::PlanMode => "plan-mode", | ||
| Self::Memory => "memory", | ||
| Self::CliDocumentation => "cli-documentation", | ||
| Self::AskUser => "ask-user", | ||
| Self::InteractiveMode => "interactive-mode", | ||
| Self::SystemNotifications => "system-notifications", | ||
| Self::Elicitation => "elicitation", | ||
| Self::SessionStore => "session-store", | ||
| Self::McpApps => "mcp-apps", | ||
| Self::CanvasRenderer => "canvas-renderer", | ||
| Self::Other(name) => name.as_str(), | ||
| } | ||
| } | ||
|
|
||
| fn from_known_name(name: &str) -> Option<Self> { | ||
| Some(match name { | ||
| "tui-hints" => Self::TuiHints, | ||
| "plan-mode" => Self::PlanMode, | ||
| "memory" => Self::Memory, | ||
| "cli-documentation" => Self::CliDocumentation, | ||
| "ask-user" => Self::AskUser, | ||
| "interactive-mode" => Self::InteractiveMode, | ||
| "system-notifications" => Self::SystemNotifications, | ||
| "elicitation" => Self::Elicitation, | ||
| "session-store" => Self::SessionStore, | ||
| "mcp-apps" => Self::McpApps, | ||
| "canvas-renderer" => Self::CanvasRenderer, | ||
| _ => return None, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl std::fmt::Display for SessionCapability { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| f.write_str(self.as_str()) | ||
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for SessionCapability { | ||
| type Err = std::convert::Infallible; | ||
|
|
||
| /// Parse a kebab-case capability name. Unknown names round-trip | ||
| /// through [`SessionCapability::Other`] so old SDK builds stay | ||
| /// useful against CLIs that add new capabilities. Always returns | ||
| /// `Ok` — the error type is [`Infallible`](std::convert::Infallible). | ||
| fn from_str(s: &str) -> std::result::Result<Self, std::convert::Infallible> { | ||
| Ok(Self::from(s)) | ||
| } | ||
| } | ||
|
|
||
| impl From<&str> for SessionCapability { | ||
| fn from(s: &str) -> Self { | ||
| Self::from_known_name(s).unwrap_or_else(|| Self::Other(s.to_owned())) | ||
| } | ||
| } | ||
|
|
||
| impl From<String> for SessionCapability { | ||
| fn from(s: String) -> Self { | ||
| if let Some(capability) = Self::from_known_name(&s) { | ||
| capability | ||
| } else { | ||
| Self::Other(s) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Serialize for SessionCapability { | ||
| fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> | ||
| where | ||
| S: serde::Serializer, | ||
| { | ||
| serializer.serialize_str(self.as_str()) | ||
| } | ||
| } | ||
|
|
||
| impl<'de> Deserialize<'de> for SessionCapability { | ||
| fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| let capability = String::deserialize(deserializer)?; | ||
| Ok(Self::from(capability)) | ||
| } | ||
| } | ||
|
|
||
| /// OpenTelemetry configuration forwarded to the spawned GitHub Copilot CLI | ||
| /// process. | ||
| /// | ||
|
|
@@ -2379,6 +2535,106 @@ mod tests { | |
| assert_eq!(Client::remote_args(&opts), vec!["--remote".to_string()]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn session_capability_round_trips_via_str() { | ||
| for cap in [ | ||
| SessionCapability::TuiHints, | ||
| SessionCapability::PlanMode, | ||
| SessionCapability::Memory, | ||
| SessionCapability::CliDocumentation, | ||
| SessionCapability::AskUser, | ||
| SessionCapability::InteractiveMode, | ||
| SessionCapability::SystemNotifications, | ||
| SessionCapability::Elicitation, | ||
| SessionCapability::SessionStore, | ||
| SessionCapability::McpApps, | ||
| SessionCapability::CanvasRenderer, | ||
| ] { | ||
| let s = cap.to_string(); | ||
| let parsed: SessionCapability = s.parse().unwrap(); | ||
| assert_eq!(parsed, cap, "round-trip failed for {s}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn session_capability_from_str_falls_back_to_other_for_unknown_names() { | ||
| let parsed: SessionCapability = "brand-new-cap".parse().unwrap(); | ||
| assert_eq!( | ||
| parsed, | ||
| SessionCapability::Other("brand-new-cap".to_string()) | ||
| ); | ||
| assert_eq!(parsed.as_str(), "brand-new-cap"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn session_capability_into_from_str_and_string() { | ||
| let from_str: SessionCapability = "memory".into(); | ||
| let from_string: SessionCapability = "memory".to_string().into(); | ||
| assert_eq!(from_str, SessionCapability::Memory); | ||
| assert_eq!(from_string, SessionCapability::Memory); | ||
| // Unknown names go to Other | ||
| let other: SessionCapability = "future-cap".into(); | ||
| assert_eq!(other, SessionCapability::Other("future-cap".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn session_capability_serializes_as_wire_string() { | ||
| assert_eq!( | ||
| serde_json::to_value(SessionCapability::Memory).unwrap(), | ||
| serde_json::json!("memory") | ||
| ); | ||
| assert_eq!( | ||
| serde_json::to_value(SessionCapability::Other("future-cap".to_string())).unwrap(), | ||
| serde_json::json!("future-cap") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn session_capability_deserializes_unknown_as_other() { | ||
| let parsed: SessionCapability = | ||
| serde_json::from_value(serde_json::json!("future-cap")).unwrap(); | ||
| assert_eq!(parsed, SessionCapability::Other("future-cap".to_string())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn generated_session_capabilities_have_public_variants() { | ||
| use crate::generated::api_types::SessionCapability as GeneratedSessionCapability; | ||
|
|
||
| fn expected_wire_name(capability: GeneratedSessionCapability) -> Option<&'static str> { | ||
| match capability { | ||
| GeneratedSessionCapability::TuiHints => Some("tui-hints"), | ||
| GeneratedSessionCapability::PlanMode => Some("plan-mode"), | ||
| GeneratedSessionCapability::Memory => Some("memory"), | ||
| GeneratedSessionCapability::CliDocumentation => Some("cli-documentation"), | ||
| GeneratedSessionCapability::AskUser => Some("ask-user"), | ||
| GeneratedSessionCapability::InteractiveMode => Some("interactive-mode"), | ||
| GeneratedSessionCapability::SystemNotifications => Some("system-notifications"), | ||
| GeneratedSessionCapability::Elicitation => Some("elicitation"), | ||
| GeneratedSessionCapability::SessionStore => Some("session-store"), | ||
| GeneratedSessionCapability::McpApps => Some("mcp-apps"), | ||
| GeneratedSessionCapability::CanvasRenderer => Some("canvas-renderer"), | ||
| GeneratedSessionCapability::Unknown => None, | ||
| } | ||
| } | ||
|
|
||
| for generated in [ | ||
| GeneratedSessionCapability::TuiHints, | ||
| GeneratedSessionCapability::PlanMode, | ||
| GeneratedSessionCapability::Memory, | ||
| GeneratedSessionCapability::CliDocumentation, | ||
| GeneratedSessionCapability::AskUser, | ||
| GeneratedSessionCapability::InteractiveMode, | ||
| GeneratedSessionCapability::SystemNotifications, | ||
| GeneratedSessionCapability::Elicitation, | ||
| GeneratedSessionCapability::SessionStore, | ||
| GeneratedSessionCapability::McpApps, | ||
| GeneratedSessionCapability::CanvasRenderer, | ||
| ] { | ||
| let wire_name = expected_wire_name(generated).unwrap(); | ||
| assert_eq!(SessionCapability::from(wire_name).as_str(), wire_name); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn log_level_args_omitted_when_unset() { | ||
| let opts = ClientOptions::default(); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid referring to closed-source repos anywhere in the open-source code.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang it! Will excise, apologies.