Coverage for src/copytrading_engine/assistant/service.py: 97%

119 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-16 16:55 +0000

1"""Answers run as background tasks the app polls; nothing outlives a reset.""" 

2 

3import asyncio 

4import datetime as dt 

5import uuid 

6from collections.abc import Awaitable, Callable, Iterable 

7from typing import Literal 

8 

9from pydantic import BaseModel, ConfigDict, Field, SecretStr 

10from pydantic_ai.models import Model 

11 

12from copytrading_engine.assistant import insights 

13from copytrading_engine.assistant.agent import BudgetExceeded, ModelRejected, run_turn 

14from copytrading_engine.assistant.conversation import ( 

15 AssistantEvent, 

16 Conversation, 

17 ConversationBook, 

18 Turn, 

19) 

20from copytrading_engine.assistant.tools import AssistantTools 

21from copytrading_engine.control.service import ControlService 

22from copytrading_engine.parsing.providers.registry import open_chat_model 

23from copytrading_engine.trading.domain.config import ProviderConfiguration 

24 

25MESSAGES = { 

26 "model_timeout": ( 

27 "Your model didn't answer in time. Try again, or pick another model in Connections." 

28 ), 

29 "model_unavailable": ( 

30 "Your model couldn't be reached. Check your connection, " 

31 "or pick another model in Connections." 

32 ), 

33 "model_rejected": ( 

34 "Your model refused the request. Check its key and model name in Connections." 

35 ), 

36 "budget_exceeded": "That needed more steps than one answer allows. Try a narrower question.", 

37} 

38 

39 

40MAX_GURUS = 50 

41 

42 

43class AskGuru(BaseModel): 

44 """A guru as the owner knows them: the id the engine records, and the name the app shows.""" 

45 

46 model_config = ConfigDict(frozen=True, extra="forbid") 

47 

48 id: str = Field(pattern=r"^[a-zA-Z0-9_-]{1,64}$") 

49 name: str = Field(min_length=1, max_length=100) 

50 

51 

52MAX_SETUP_ACCOUNTS = 20 

53 

54type SetupState = Literal["connected", "filled", "missing", "failed"] 

55 

56 

57class AskSetupAccount(BaseModel): 

58 """An account in Connections: its name, paper or live, and how far it is set up. No keys.""" 

59 

60 model_config = ConfigDict(frozen=True, extra="forbid") 

61 

62 name: str = Field(min_length=1, max_length=64) 

63 environment: Literal["paper", "live"] 

64 state: SetupState 

65 

66 

67class AskSetup(BaseModel): 

68 """What Connections holds, which may not be saved yet, so the assistant can tell an account 

69 that is set up but not started from no account at all. It never carries a key or a token.""" 

70 

71 model_config = ConfigDict(frozen=True, extra="forbid") 

72 

73 saved: bool 

74 copying: bool 

75 unsaved_changes: bool 

76 discord: SetupState 

77 interpreter: SetupState 

78 accounts: tuple[AskSetupAccount, ...] = Field(default=(), max_length=MAX_SETUP_ACCOUNTS) 

79 

80 

81class AskContext(BaseModel): 

82 model_config = ConfigDict(frozen=True, extra="forbid") 

83 

84 screen: Literal[ 

85 "today", 

86 "activity", 

87 "people", 

88 "accounts", 

89 "connections", 

90 "gettingStarted", 

91 "diagnostics", 

92 "settings", 

93 ] 

94 selected_source_id: str | None = None 

95 selected_account_id: str | None = None 

96 selected_guru_id: str | None = None 

97 language: Literal["en", "zh-Hans"] = "en" 

98 gurus: tuple[AskGuru, ...] = Field(default=(), max_length=MAX_GURUS) 

99 setup: AskSetup | None = None 

100 

101 def guru_names(self) -> dict[str, str]: 

102 return {guru.id: guru.name for guru in self.gurus} 

103 

104 

105class AssistantService: 

106 def __init__( 

107 self, 

108 control: ControlService, 

109 operator: insights.Operator, 

110 *, 

111 engine_state: Callable[[], str], 

112 register_secrets: Callable[[Iterable[str]], object], 

113 open_model: Callable[..., Awaitable[tuple[Model, Callable[[], Awaitable[None]]]]] = ( 

114 open_chat_model 

115 ), 

116 now: Callable[[], dt.datetime] = lambda: dt.datetime.now(dt.UTC), 

117 time_budget: float = 60, 

118 ) -> None: 

119 self._control = control 

120 self._operator = operator 

121 self._engine_state = engine_state 

122 self._register_secrets = register_secrets 

123 self._open_model = open_model 

124 self._now = now 

125 self._time_budget = time_budget 

126 self._book = ConversationBook() 

127 self._tasks: dict[asyncio.Task[None], Turn] = {} 

128 

129 async def ask( 

130 self, 

131 conversation_id: str, 

132 text: str, 

133 context: AskContext, 

134 provider: ProviderConfiguration, 

135 provider_api_key: SecretStr, 

136 ) -> str: 

137 conversation = self._book.get_or_open(conversation_id) 

138 self._stop_cancelled() 

139 if conversation.running is not None and not conversation.running.done: 

140 return conversation.running.turn_id 

141 turn = Turn(f"t-{uuid.uuid4().hex[:12]}") 

142 conversation.running = turn 

143 self._book.register(turn, conversation) 

144 self._register_secrets((provider_api_key.get_secret_value(),)) 

145 task = asyncio.create_task( 

146 self._answer(conversation, turn, text, context, provider, provider_api_key), 

147 name=turn.turn_id, 

148 ) 

149 self._tasks[task] = turn 

150 task.add_done_callback(lambda finished: self._tasks.pop(finished, None)) 

151 return turn.turn_id 

152 

153 async def _answer( 

154 self, 

155 conversation: Conversation, 

156 turn: Turn, 

157 text: str, 

158 context: AskContext, 

159 provider: ProviderConfiguration, 

160 key: SecretStr, 

161 ) -> None: 

162 close: Callable[[], Awaitable[None]] | None = None 

163 try: 

164 try: 

165 config = provider.reader(key, timeout=self._time_budget) 

166 model, close = await self._open_model(provider.name, config) 

167 except ValueError: 

168 turn.fail("model_rejected", MESSAGES["model_rejected"]) 

169 return 

170 tools = AssistantTools( 

171 self._control, 

172 self._operator, 

173 turn, 

174 engine_state=self._engine_state, 

175 now=self._now, 

176 guru_names=context.guru_names(), 

177 ) 

178 messages = await asyncio.wait_for( 

179 run_turn(model, tools, turn, text, context, conversation.history), 

180 timeout=self._time_budget, 

181 ) 

182 conversation.remember(messages) 

183 turn.finish() 

184 except TimeoutError: 

185 turn.fail("model_timeout", MESSAGES["model_timeout"]) 

186 except asyncio.CancelledError: 

187 turn.finish() 

188 raise 

189 except BudgetExceeded: 

190 turn.fail("budget_exceeded", MESSAGES["budget_exceeded"]) 

191 except ModelRejected: 

192 turn.fail("model_rejected", MESSAGES["model_rejected"]) 

193 except Exception: # noqa: BLE001 - any provider failure ends the turn in plain words 

194 turn.fail("model_unavailable", MESSAGES["model_unavailable"]) 

195 finally: 

196 if close is not None: 

197 await self._close(close) 

198 

199 @staticmethod 

200 async def _close(close: Callable[[], Awaitable[None]]) -> None: 

201 """Close the client even when a second cancellation arrives while closing.""" 

202 closing = asyncio.ensure_future(close()) 

203 try: 

204 await asyncio.shield(closing) 

205 except asyncio.CancelledError: 

206 raise # the shielded close keeps running to completion on its own 

207 except Exception: # noqa: BLE001 - a failed close must not change how the turn ended 

208 pass 

209 

210 def _stop_cancelled(self) -> None: 

211 """Stop the tasks of turns cancelled elsewhere, such as an evicted conversation's.""" 

212 for task, turn in self._tasks.items(): 

213 if turn.cancelled: 

214 task.cancel() 

215 

216 def turn(self, turn_id: str, after: int) -> tuple[tuple[AssistantEvent, ...], bool]: 

217 turn = self._book.turn(turn_id) 

218 return turn.events(after), turn.done 

219 

220 def cancel(self, turn_id: str) -> bool: 

221 try: 

222 turn = self._book.turn(turn_id) 

223 except KeyError: 

224 return False 

225 turn.cancel() 

226 for task in self._tasks: 

227 if task.get_name() == turn_id: 227 ↛ 226line 227 didn't jump to line 226 because the condition on line 227 was always true

228 task.cancel() 

229 return True 

230 

231 async def reset(self) -> None: 

232 self._book.forget_all() 

233 for task in list(self._tasks): 

234 task.cancel() 

235 await asyncio.gather(*list(self._tasks), return_exceptions=True)