Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/gradient_labs/_conversation_finish.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class FinishParams:
# reason optionally allows you to describe why this conversation is finishing.
reason: Optional[str] = None

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


def finish_conversation(
*, client: HttpClient, conversation_id: str, params: FinishParams
Expand All @@ -26,6 +30,8 @@ def finish_conversation(
body["timestamp"] = client.localize(params.timestamp)
if params.reason:
body["reason"] = params.reason
if params.reason_code:
body["reason_code"] = params.reason_code

_ = client.put(
f"conversations/{conversation_id}/finish",
Expand Down
56 changes: 56 additions & 0 deletions tests/test_conversation_finish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from unittest.mock import MagicMock

from gradient_labs import Client, FinishParams

CONVERSATION_ID = "conv_01ham6bzcdeja9xzqhjf6daq30"


def _client():
client = Client(api_key="test-key")
put = MagicMock(return_value=None)
client.http_client.put = put
return client, put


def test_finish_conversation():
client, put = _client()

client.finish_conversation(
conversation_id=CONVERSATION_ID,
params=FinishParams(),
)

args, kwargs = put.call_args
assert args[0] == f"conversations/{CONVERSATION_ID}/finish"
assert kwargs["body"] == {}


def test_finish_conversation_includes_reason_code():
client, put = _client()

client.finish_conversation(
conversation_id=CONVERSATION_ID,
params=FinishParams(
reason="Customer stopped replying",
reason_code="customer-unresponsive",
),
)

_, kwargs = put.call_args
body = kwargs["body"]
assert body["reason"] == "Customer stopped replying"
assert body["reason_code"] == "customer-unresponsive"


def test_finish_conversation_omits_reason_code_when_unset():
client, put = _client()

client.finish_conversation(
conversation_id=CONVERSATION_ID,
params=FinishParams(reason="Resolved"),
)

_, kwargs = put.call_args
body = kwargs["body"]
assert body["reason"] == "Resolved"
assert "reason_code" not in body
Loading