-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathprotocol.py
More file actions
173 lines (114 loc) · 4.04 KB
/
protocol.py
File metadata and controls
173 lines (114 loc) · 4.04 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""IPC message types and serialization helpers for daemon communication."""
from __future__ import annotations
import msgspec as _msgspec
# ---------------------------------------------------------------------------
# Requests (tagged union via struct tag)
# ---------------------------------------------------------------------------
class HandshakeRequest(_msgspec.Struct, tag="handshake"):
version: str
class IndexRequest(_msgspec.Struct, tag="index"):
project_root: str
class SearchRequest(_msgspec.Struct, tag="search"):
project_root: str
query: str
languages: list[str] | None = None
paths: list[str] | None = None
limit: int = 5
offset: int = 0
refresh: bool = False
class ProjectStatusRequest(_msgspec.Struct, tag="project_status"):
project_root: str
class DaemonStatusRequest(_msgspec.Struct, tag="daemon_status"):
pass
class StopRequest(_msgspec.Struct, tag="stop"):
pass
Request = (
HandshakeRequest
| IndexRequest
| SearchRequest
| ProjectStatusRequest
| DaemonStatusRequest
| StopRequest
)
# ---------------------------------------------------------------------------
# Responses
# ---------------------------------------------------------------------------
class HandshakeResponse(_msgspec.Struct, tag="handshake"):
ok: bool
daemon_version: str
class IndexResponse(_msgspec.Struct, tag="index"):
success: bool
message: str | None = None
class IndexingProgress(_msgspec.Struct):
"""Indexing stats snapshot, shared between progress updates and status responses."""
num_execution_starts: int
num_unchanged: int
num_adds: int
num_deletes: int
num_reprocesses: int
num_errors: int
class IndexProgressUpdate(_msgspec.Struct, tag="index_progress"):
"""Streamed during indexing — one per stats change, before the final IndexResponse."""
progress: IndexingProgress
class IndexWaitingNotice(_msgspec.Struct, tag="index_waiting"):
"""Sent when another indexing is already in progress and the client must wait."""
pass
class SearchResult(_msgspec.Struct):
file_path: str
language: str
content: str
start_line: int
end_line: int
score: float
class SearchResponse(_msgspec.Struct, tag="search"):
success: bool
results: list[SearchResult] = []
total_returned: int = 0
offset: int = 0
message: str | None = None
class ProjectStatusResponse(_msgspec.Struct, tag="project_status"):
indexing: bool
total_chunks: int
total_files: int
languages: dict[str, int]
progress: IndexingProgress | None = None
class DaemonProjectInfo(_msgspec.Struct):
project_root: str
indexing: bool
class DaemonStatusResponse(_msgspec.Struct, tag="daemon_status"):
version: str
uptime_seconds: float
projects: list[DaemonProjectInfo]
class StopResponse(_msgspec.Struct, tag="stop"):
ok: bool
class ErrorResponse(_msgspec.Struct, tag="error"):
message: str
Response = (
HandshakeResponse
| IndexResponse
| IndexProgressUpdate
| IndexWaitingNotice
| SearchResponse
| ProjectStatusResponse
| DaemonStatusResponse
| StopResponse
| ErrorResponse
)
IndexStreamResponse = IndexProgressUpdate | IndexWaitingNotice | IndexResponse | ErrorResponse
# ---------------------------------------------------------------------------
# Encode / decode helpers (msgpack binary)
# ---------------------------------------------------------------------------
_request_encoder = _msgspec.msgpack.Encoder()
_request_decoder = _msgspec.msgpack.Decoder(Request)
_response_encoder = _msgspec.msgpack.Encoder()
_response_decoder = _msgspec.msgpack.Decoder(Response)
def encode_request(req: Request) -> bytes:
return _request_encoder.encode(req)
def decode_request(data: bytes) -> Request:
result: Request = _request_decoder.decode(data)
return result
def encode_response(resp: Response) -> bytes:
return _response_encoder.encode(resp)
def decode_response(data: bytes) -> Response:
result: Response = _response_decoder.decode(data)
return result