Skip to content

Commit 43d25f4

Browse files
committed
Clean up constructors
1 parent 58569db commit 43d25f4

1 file changed

Lines changed: 51 additions & 11 deletions

File tree

src/elicitation.rs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -915,22 +915,33 @@ impl CreateElicitationRequest {
915915
}
916916
}
917917

918+
/// Sets the session scope for this elicitation.
919+
///
920+
/// Clears `request_id` and `tool_call_id` since scopes are mutually exclusive.
921+
/// Use [`for_tool_call`](Self::for_tool_call) if you also need a `tool_call_id`.
918922
#[must_use]
919-
pub fn session_id(mut self, session_id: impl Into<SessionId>) -> Self {
923+
pub fn for_session(mut self, session_id: impl Into<SessionId>) -> Self {
920924
self.session_id = Some(session_id.into());
925+
self.request_id = None;
926+
self.tool_call_id = None;
921927
self
922928
}
923929

930+
/// Sets the request scope for this elicitation.
931+
///
932+
/// Clears `session_id` and `tool_call_id` since scopes are mutually exclusive.
924933
#[must_use]
925-
pub fn request_id(mut self, request_id: impl Into<RequestId>) -> Self {
934+
pub fn for_request(mut self, request_id: impl Into<RequestId>) -> Self {
926935
self.request_id = Some(request_id.into());
936+
self.session_id = None;
937+
self.tool_call_id = None;
927938
self
928939
}
929940

930-
/// Sets both `session_id` and `tool_call_id` for a tool-call-scoped elicitation.
941+
/// Sets the tool-call scope for this elicitation.
931942
///
932-
/// This is the recommended way to create a tool-call-scoped elicitation,
933-
/// since `tool_call_id` should always be accompanied by a `session_id`.
943+
/// Sets both `session_id` and `tool_call_id`, and clears `request_id`,
944+
/// since scopes are mutually exclusive and `tool_call_id` requires a `session_id`.
934945
#[must_use]
935946
pub fn for_tool_call(
936947
mut self,
@@ -939,6 +950,7 @@ impl CreateElicitationRequest {
939950
) -> Self {
940951
self.session_id = Some(session_id.into());
941952
self.tool_call_id = Some(tool_call_id.into());
953+
self.request_id = None;
942954
self
943955
}
944956

@@ -1323,13 +1335,11 @@ mod tests {
13231335
)),
13241336
"Please authenticate",
13251337
)
1326-
.session_id("sess_2")
1327-
.request_id(42i64)
13281338
.for_tool_call("sess_2", "tc_1");
13291339

13301340
let json = serde_json::to_value(&req).unwrap();
13311341
assert_eq!(json["sessionId"], "sess_2");
1332-
assert_eq!(json["requestId"], 42);
1342+
assert!(json.get("requestId").is_none());
13331343
assert_eq!(json["toolCallId"], "tc_1");
13341344
assert_eq!(json["mode"], "url");
13351345
assert_eq!(json["elicitationId"], "elic_1");
@@ -1338,7 +1348,7 @@ mod tests {
13381348

13391349
let roundtripped: CreateElicitationRequest = serde_json::from_value(json).unwrap();
13401350
assert_eq!(roundtripped.session_id, Some(SessionId::new("sess_2")));
1341-
assert_eq!(roundtripped.request_id, Some(RequestId::Number(42)));
1351+
assert_eq!(roundtripped.request_id, None);
13421352
assert_eq!(roundtripped.tool_call_id, Some(ToolCallId::new("tc_1")));
13431353
assert!(matches!(roundtripped.mode, ElicitationMode::Url(_)));
13441354
}
@@ -1396,7 +1406,7 @@ mod tests {
13961406
)),
13971407
"Enter your name",
13981408
)
1399-
.session_id("sess_1");
1409+
.for_session("sess_1");
14001410

14011411
let json = serde_json::to_value(&req).unwrap();
14021412
assert_eq!(json["sessionId"], "sess_1");
@@ -1417,7 +1427,7 @@ mod tests {
14171427
)),
14181428
"Enter workspace name",
14191429
)
1420-
.request_id(99i64);
1430+
.for_request(99i64);
14211431

14221432
let json = serde_json::to_value(&req).unwrap();
14231433
assert!(json.get("sessionId").is_none());
@@ -1450,6 +1460,36 @@ mod tests {
14501460
assert!(json.get("requestId").is_none());
14511461
}
14521462

1463+
#[test]
1464+
fn scope_setters_are_mutually_exclusive() {
1465+
let base = || {
1466+
CreateElicitationRequest::new(
1467+
ElicitationMode::Form(ElicitationFormMode::new(
1468+
ElicitationSchema::new().string("name", true),
1469+
)),
1470+
"msg",
1471+
)
1472+
};
1473+
1474+
// session_id clears request_id and tool_call_id
1475+
let req = base().for_request(1i64).for_session("s1");
1476+
assert_eq!(req.session_id, Some(SessionId::new("s1")));
1477+
assert_eq!(req.request_id, None);
1478+
assert_eq!(req.tool_call_id, None);
1479+
1480+
// request_id clears session_id and tool_call_id
1481+
let req = base().for_tool_call("s1", "tc1").for_request(2i64);
1482+
assert_eq!(req.request_id, Some(RequestId::Number(2)));
1483+
assert_eq!(req.session_id, None);
1484+
assert_eq!(req.tool_call_id, None);
1485+
1486+
// for_tool_call clears request_id
1487+
let req = base().for_request(3i64).for_tool_call("s2", "tc2");
1488+
assert_eq!(req.session_id, Some(SessionId::new("s2")));
1489+
assert_eq!(req.tool_call_id, Some(ToolCallId::new("tc2")));
1490+
assert_eq!(req.request_id, None);
1491+
}
1492+
14531493
#[test]
14541494
fn completion_notification_serialization() {
14551495
let notif = CompleteElicitationNotification::new("elic_1");

0 commit comments

Comments
 (0)