cube-chat-tool 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: cube-chat-tool
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Chat tool implementation for cube-standard benchmarks
|
|
5
|
+
Requires-Dist: cube-standard
|
|
6
|
+
Requires-Dist: cube-chat
|
|
7
|
+
Requires-Dist: pytest>=8.0.0 ; extra == 'dev'
|
|
8
|
+
Requires-Python: >=3.12
|
|
9
|
+
Provides-Extra: dev
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "cube-chat-tool"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Chat tool implementation for cube-standard benchmarks"
|
|
5
|
+
requires-python = ">=3.12"
|
|
6
|
+
dependencies = [
|
|
7
|
+
"cube-standard",
|
|
8
|
+
"cube-chat",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
[project.optional-dependencies]
|
|
12
|
+
dev = [
|
|
13
|
+
"pytest>=8.0.0",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["uv_build>=0.6.0,<0.7.0"]
|
|
18
|
+
build-backend = "uv_build"
|
|
19
|
+
|
|
20
|
+
[tool.uv.build-backend]
|
|
21
|
+
module-name = "cube_chat_tool"
|
|
22
|
+
|
|
23
|
+
[tool.ruff]
|
|
24
|
+
fix = true
|
|
25
|
+
line-length = 120
|
|
26
|
+
indent-width = 4
|
|
27
|
+
|
|
28
|
+
[tool.ruff.format]
|
|
29
|
+
quote-style = "double"
|
|
30
|
+
indent-style = "space"
|
|
31
|
+
skip-magic-trailing-comma = false
|
|
32
|
+
line-ending = "auto"
|
|
33
|
+
|
|
34
|
+
[tool.ruff.lint]
|
|
35
|
+
extend-select = ["I"]
|
|
36
|
+
|
|
37
|
+
[tool.pytest.ini_options]
|
|
38
|
+
testpaths = ["tests"]
|
|
39
|
+
addopts = "-rs"
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""Chat tool for cube-standard benchmarks.
|
|
2
|
+
|
|
3
|
+
Provides ``ChatToolConfig`` and ``ChatTool``, a concrete implementation of the
|
|
4
|
+
``Tool`` base class that wraps a ``ChatSession`` and exposes ``send_message``
|
|
5
|
+
as the single agent-facing action.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import datetime
|
|
9
|
+
|
|
10
|
+
from cube.resources.chat_session import ChatConfig, ChatRole, ChatSession
|
|
11
|
+
from cube.tool import Tool, ToolConfig, tool_action
|
|
12
|
+
from cube_chat import BasicChatConfig
|
|
13
|
+
from pydantic import Field
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ChatToolConfig(ToolConfig):
|
|
17
|
+
"""Configuration for ``ChatTool``.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
chat : ChatConfig
|
|
22
|
+
Chat session configuration. Defaults to an in-memory BasicChatConfig.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
chat: ChatConfig = Field(default_factory=BasicChatConfig)
|
|
26
|
+
|
|
27
|
+
def make(self, container=None) -> "ChatTool":
|
|
28
|
+
"""Create a ChatTool from this configuration."""
|
|
29
|
+
session = self.chat.make()
|
|
30
|
+
return ChatTool(config=self, session=session)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ChatTool(Tool):
|
|
34
|
+
"""Chat tool that exposes a side-channel message interface to the agent.
|
|
35
|
+
|
|
36
|
+
The agent uses ``send_message`` to communicate with the task. The task uses
|
|
37
|
+
``add_message`` and ``wait_for_user_message`` to drive the conversation.
|
|
38
|
+
After every agent action, ``chat_obs()`` is appended to the observation so
|
|
39
|
+
the agent always sees the up-to-date message history.
|
|
40
|
+
|
|
41
|
+
Call ``reset()`` between episodes to get a fresh session. Call ``close()``
|
|
42
|
+
to release session resources.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(self, config: ChatToolConfig, session: ChatSession) -> None:
|
|
46
|
+
self.config = config
|
|
47
|
+
self._session = session
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def session(self) -> ChatSession:
|
|
51
|
+
"""Expose the raw ChatSession for advanced use (escape hatch, SR-003)."""
|
|
52
|
+
return self._session
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def messages(self) -> list[dict]:
|
|
56
|
+
"""Delegate to session.messages."""
|
|
57
|
+
return self._session.messages
|
|
58
|
+
|
|
59
|
+
# ------------------------------------------------------------------
|
|
60
|
+
# Tool lifecycle
|
|
61
|
+
# ------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
def reset(self) -> None:
|
|
64
|
+
"""Stop the current session and create a fresh one."""
|
|
65
|
+
self._session.stop()
|
|
66
|
+
self._session = self.config.chat.make()
|
|
67
|
+
|
|
68
|
+
def close(self) -> None:
|
|
69
|
+
"""Release session resources."""
|
|
70
|
+
self._session.stop()
|
|
71
|
+
|
|
72
|
+
# ------------------------------------------------------------------
|
|
73
|
+
# Task-internal methods (not agent-facing actions)
|
|
74
|
+
# ------------------------------------------------------------------
|
|
75
|
+
|
|
76
|
+
def add_message(self, role: ChatRole, msg: str) -> None:
|
|
77
|
+
"""Post a message to the chat from the task side.
|
|
78
|
+
|
|
79
|
+
Parameters
|
|
80
|
+
----------
|
|
81
|
+
role : ChatRole
|
|
82
|
+
Role of the message sender.
|
|
83
|
+
msg : str
|
|
84
|
+
Message content.
|
|
85
|
+
"""
|
|
86
|
+
self._session.add_message(role, msg)
|
|
87
|
+
|
|
88
|
+
def wait_for_user_message(self) -> str:
|
|
89
|
+
"""Block until the agent sends a message and return it.
|
|
90
|
+
|
|
91
|
+
Returns
|
|
92
|
+
-------
|
|
93
|
+
str
|
|
94
|
+
The agent's message text.
|
|
95
|
+
"""
|
|
96
|
+
return self._session.wait_for_user_message()
|
|
97
|
+
|
|
98
|
+
def chat_obs(self) -> str:
|
|
99
|
+
"""Format the full message history as a string.
|
|
100
|
+
|
|
101
|
+
Returns
|
|
102
|
+
-------
|
|
103
|
+
str
|
|
104
|
+
Formatted chat history with one line per message.
|
|
105
|
+
"""
|
|
106
|
+
lines = []
|
|
107
|
+
for entry in self._session.messages:
|
|
108
|
+
ts = datetime.datetime.fromtimestamp(entry["timestamp"]).strftime("%H:%M:%S")
|
|
109
|
+
lines.append(f"[{ts}] {entry['role']}: {entry['message']}")
|
|
110
|
+
return "\n".join(lines)
|
|
111
|
+
|
|
112
|
+
# ------------------------------------------------------------------
|
|
113
|
+
# Agent-facing actions
|
|
114
|
+
# ------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
@tool_action
|
|
117
|
+
def send_message(self, text: str) -> str:
|
|
118
|
+
"""Send a message to the task.
|
|
119
|
+
|
|
120
|
+
Parameters
|
|
121
|
+
----------
|
|
122
|
+
text : str
|
|
123
|
+
Message content to send.
|
|
124
|
+
"""
|
|
125
|
+
self._session.send_message(text)
|
|
126
|
+
return self.chat_obs()
|