Skip to content

Commit 47df53e

Browse files
Catchpowleclaude
andauthored
Add reason_code to Finish Conversation (#134)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 164e65a commit 47df53e

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎src/gradient_labs/_conversation_finish.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class FinishParams:
1717
# reason optionally allows you to describe why this conversation is finishing.
1818
reason: Optional[str] = None
1919

20+
# reason_code optionally categorises why this conversation is finishing.
21+
# Valid values are "customer-ended-chat" and "customer-unresponsive".
22+
reason_code: Optional[str] = None
23+
2024

2125
def finish_conversation(
2226
*, client: HttpClient, conversation_id: str, params: FinishParams
@@ -26,6 +30,8 @@ def finish_conversation(
2630
body["timestamp"] = client.localize(params.timestamp)
2731
if params.reason:
2832
body["reason"] = params.reason
33+
if params.reason_code:
34+
body["reason_code"] = params.reason_code
2935

3036
_ = client.put(
3137
f"conversations/{conversation_id}/finish",

‎tests/test_conversation_finish.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from unittest.mock import MagicMock
2+
3+
from gradient_labs import Client, FinishParams
4+
5+
CONVERSATION_ID = "conv_01ham6bzcdeja9xzqhjf6daq30"
6+
7+
8+
def _client():
9+
client = Client(api_key="test-key")
10+
put = MagicMock(return_value=None)
11+
client.http_client.put = put
12+
return client, put
13+
14+
15+
def test_finish_conversation():
16+
client, put = _client()
17+
18+
client.finish_conversation(
19+
conversation_id=CONVERSATION_ID,
20+
params=FinishParams(),
21+
)
22+
23+
args, kwargs = put.call_args
24+
assert args[0] == f"conversations/{CONVERSATION_ID}/finish"
25+
assert kwargs["body"] == {}
26+
27+
28+
def test_finish_conversation_includes_reason_code():
29+
client, put = _client()
30+
31+
client.finish_conversation(
32+
conversation_id=CONVERSATION_ID,
33+
params=FinishParams(
34+
reason="Customer stopped replying",
35+
reason_code="customer-unresponsive",
36+
),
37+
)
38+
39+
_, kwargs = put.call_args
40+
body = kwargs["body"]
41+
assert body["reason"] == "Customer stopped replying"
42+
assert body["reason_code"] == "customer-unresponsive"
43+
44+
45+
def test_finish_conversation_omits_reason_code_when_unset():
46+
client, put = _client()
47+
48+
client.finish_conversation(
49+
conversation_id=CONVERSATION_ID,
50+
params=FinishParams(reason="Resolved"),
51+
)
52+
53+
_, kwargs = put.call_args
54+
body = kwargs["body"]
55+
assert body["reason"] == "Resolved"
56+
assert "reason_code" not in body

0 commit comments

Comments
 (0)