-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathservice.py
More file actions
145 lines (135 loc) · 4.47 KB
/
Copy pathservice.py
File metadata and controls
145 lines (135 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from typing import List, Optional
from uuid import UUID
from oss.src.core.sessions.interactions.dtos import (
SessionInteraction,
SessionInteractionCreate,
SessionInteractionQuery,
SessionInteractionTransition,
)
from oss.src.core.sessions.interactions.interfaces import (
SessionInteractionsDAOInterface,
)
from oss.src.core.sessions.interactions.types import InteractionNotFound
from oss.src.core.shared.dtos import Windowing
from oss.src.dbs.redis.sessions.contract import (
WATCH_INTERACTION_PENDING,
WATCH_INTERACTION_RESOLVED,
)
from oss.src.core.sessions.watch.interfaces import SessionsWatchPublisherInterface
class SessionInteractionsService:
def __init__(
self,
*,
interactions_dao: SessionInteractionsDAOInterface,
watch_publisher: Optional[SessionsWatchPublisherInterface] = None,
) -> None:
self.interactions_dao = interactions_dao
self._watch = watch_publisher
async def _publish_interaction(
self, *, project_id: UUID, session_id: str, status: str
) -> None:
# Fire-and-forget relay notification; the publisher never raises.
if self._watch is not None:
await self._watch.interaction(
project_id=str(project_id),
session_id=session_id,
status=status,
)
async def create_interaction(
self,
*,
project_id: UUID,
user_id: Optional[UUID] = None,
#
interaction: SessionInteractionCreate,
) -> SessionInteraction:
created = await self.interactions_dao.create_interaction(
project_id=project_id,
user_id=user_id,
interaction=interaction,
)
await self._publish_interaction(
project_id=project_id,
session_id=interaction.session_id,
status=WATCH_INTERACTION_PENDING,
)
return created
async def fetch_interaction(
self,
*,
project_id: UUID,
#
interaction_id: UUID,
) -> SessionInteraction:
result = await self.interactions_dao.fetch_interaction(
project_id=project_id,
interaction_id=interaction_id,
)
if result is None:
raise InteractionNotFound(f"Interaction {interaction_id} not found")
return result
async def transition_interaction(
self,
*,
transition: SessionInteractionTransition,
) -> Optional[SessionInteraction]:
result = await self.interactions_dao.transition_interaction(
transition=transition,
)
if result is None:
raise InteractionNotFound(
f"Interaction with token {transition.token!r} not found or already terminal"
)
await self._publish_interaction(
project_id=transition.project_id,
session_id=transition.session_id,
status=WATCH_INTERACTION_RESOLVED,
)
return result
async def cancel_session_pending(
self,
*,
project_id: UUID,
session_id: str,
except_turn_id: Optional[str] = None,
except_tokens: Optional[List[str]] = None,
only_turn_id: Optional[str] = None,
) -> int:
cancelled = await self.interactions_dao.cancel_session_pending(
project_id=project_id,
session_id=session_id,
except_turn_id=except_turn_id,
except_tokens=except_tokens,
only_turn_id=only_turn_id,
)
if cancelled:
await self._publish_interaction(
project_id=project_id,
session_id=session_id,
status=WATCH_INTERACTION_RESOLVED,
)
return cancelled
async def query_interactions(
self,
*,
project_id: UUID,
#
query: Optional[SessionInteractionQuery] = None,
windowing: Optional[Windowing] = None,
) -> List[SessionInteraction]:
return await self.interactions_dao.query_interactions(
project_id=project_id,
query=query,
windowing=windowing,
)
async def delete_by_session_id(
self,
*,
project_id: UUID,
session_id: str,
) -> int:
"""Hard delete every interaction for a session (S7 delete fan-out, WP5)."""
return await self.interactions_dao.delete_by_session_id(
project_id=project_id,
session_id=session_id,
)