@gowelle/stint-agent 1.0.8 → 1.2.0

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,369 @@
1
+ import {
2
+ apiService
3
+ } from "./chunk-W4JGOGR7.js";
4
+ import {
5
+ gitService,
6
+ projectService
7
+ } from "./chunk-FBQA4K5J.js";
8
+ import {
9
+ authService,
10
+ config,
11
+ logger
12
+ } from "./chunk-RHMTZK2J.js";
13
+
14
+ // src/daemon/queue.ts
15
+ var CommitQueueProcessor = class {
16
+ queue = [];
17
+ isProcessing = false;
18
+ /**
19
+ * Add commit to processing queue
20
+ */
21
+ addToQueue(commit, project) {
22
+ this.queue.push({ commit, project });
23
+ logger.info("queue", `Added commit ${commit.id} to queue (position: ${this.queue.length})`);
24
+ if (!this.isProcessing) {
25
+ this.processQueue();
26
+ }
27
+ }
28
+ /**
29
+ * Process commits sequentially
30
+ */
31
+ async processQueue() {
32
+ if (this.isProcessing) {
33
+ return;
34
+ }
35
+ this.isProcessing = true;
36
+ while (this.queue.length > 0) {
37
+ const item = this.queue.shift();
38
+ if (!item) break;
39
+ try {
40
+ await this.executeCommit(item.commit, item.project);
41
+ } catch (error) {
42
+ logger.error("queue", `Failed to execute commit ${item.commit.id}`, error);
43
+ }
44
+ }
45
+ this.isProcessing = false;
46
+ }
47
+ /**
48
+ * Execute a single commit
49
+ */
50
+ async executeCommit(commit, project, onProgress) {
51
+ logger.info("queue", `Processing commit: ${commit.id} - ${commit.message}`);
52
+ try {
53
+ onProgress?.("Finding project directory...");
54
+ const projectPath = this.findProjectPath(project.id);
55
+ if (!projectPath) {
56
+ throw new Error(`Project ${project.id} is not linked to any local directory`);
57
+ }
58
+ logger.info("queue", `Executing in directory: ${projectPath}`);
59
+ onProgress?.("Validating repository...");
60
+ const isRepo = await gitService.isRepo(projectPath);
61
+ if (!isRepo) {
62
+ throw new Error(`Directory ${projectPath} is not a git repository`);
63
+ }
64
+ onProgress?.("Checking repository status...");
65
+ const status = await gitService.getStatus(projectPath);
66
+ const hasStagedChanges = status.staged.length > 0;
67
+ if (!hasStagedChanges) {
68
+ throw new Error('No staged changes to commit. Please stage files using "git add" before committing.');
69
+ }
70
+ logger.info("queue", `Committing ${status.staged.length} staged files.`);
71
+ onProgress?.("Creating commit...");
72
+ logger.info("queue", `Creating commit with message: "${commit.message}"`);
73
+ const sha = await gitService.commit(projectPath, commit.message);
74
+ logger.success("queue", `Commit created successfully: ${sha}`);
75
+ onProgress?.("Reporting to server...");
76
+ await this.reportSuccess(commit.id, sha);
77
+ return sha;
78
+ } catch (error) {
79
+ const errorMessage = error.message;
80
+ logger.error("queue", `Commit execution failed: ${errorMessage}`);
81
+ await this.reportFailure(commit.id, errorMessage);
82
+ throw error;
83
+ }
84
+ }
85
+ /**
86
+ * Report successful execution to API
87
+ */
88
+ async reportSuccess(commitId, sha) {
89
+ try {
90
+ await apiService.markCommitExecuted(commitId, sha);
91
+ logger.success("queue", `Reported commit execution to API: ${commitId} -> ${sha}`);
92
+ } catch (error) {
93
+ logger.error("queue", "Failed to report commit success to API", error);
94
+ }
95
+ }
96
+ /**
97
+ * Report failed execution to API
98
+ */
99
+ async reportFailure(commitId, error) {
100
+ try {
101
+ await apiService.markCommitFailed(commitId, error);
102
+ logger.info("queue", `Reported commit failure to API: ${commitId}`);
103
+ } catch (apiError) {
104
+ logger.error("queue", "Failed to report commit failure to API", apiError);
105
+ }
106
+ }
107
+ /**
108
+ * Find local path for a project ID
109
+ */
110
+ findProjectPath(projectId) {
111
+ const allProjects = projectService.getAllLinkedProjects();
112
+ for (const [path, linkedProject] of Object.entries(allProjects)) {
113
+ if (linkedProject.projectId === projectId) {
114
+ return path;
115
+ }
116
+ }
117
+ return null;
118
+ }
119
+ /**
120
+ * Check if queue is currently processing
121
+ */
122
+ isCurrentlyProcessing() {
123
+ return this.isProcessing;
124
+ }
125
+ /**
126
+ * Get queue length
127
+ */
128
+ getQueueLength() {
129
+ return this.queue.length;
130
+ }
131
+ };
132
+ var commitQueue = new CommitQueueProcessor();
133
+
134
+ // src/services/websocket.ts
135
+ import WebSocket from "ws";
136
+ var WebSocketServiceImpl = class {
137
+ ws = null;
138
+ userId = null;
139
+ reconnectAttempts = 0;
140
+ maxReconnectAttempts = 10;
141
+ reconnectTimer = null;
142
+ isManualDisconnect = false;
143
+ // Event handlers
144
+ commitApprovedHandlers = [];
145
+ commitPendingHandlers = [];
146
+ suggestionCreatedHandlers = [];
147
+ projectUpdatedHandlers = [];
148
+ disconnectHandlers = [];
149
+ agentDisconnectedHandlers = [];
150
+ syncRequestedHandlers = [];
151
+ /**
152
+ * Connect to the WebSocket server
153
+ * @throws Error if connection fails or no auth token available
154
+ */
155
+ async connect() {
156
+ try {
157
+ const token = await authService.getToken();
158
+ if (!token) {
159
+ throw new Error("No authentication token available");
160
+ }
161
+ const wsUrl = config.getWsUrl();
162
+ const url = `${wsUrl}?token=${encodeURIComponent(token)}`;
163
+ logger.info("websocket", `Connecting to ${wsUrl}...`);
164
+ this.ws = new WebSocket(url);
165
+ return new Promise((resolve, reject) => {
166
+ if (!this.ws) {
167
+ reject(new Error("WebSocket not initialized"));
168
+ return;
169
+ }
170
+ this.ws.on("open", () => {
171
+ logger.success("websocket", "WebSocket connected");
172
+ this.reconnectAttempts = 0;
173
+ this.isManualDisconnect = false;
174
+ resolve();
175
+ });
176
+ this.ws.on("message", (data) => {
177
+ this.handleMessage(data);
178
+ });
179
+ this.ws.on("close", () => {
180
+ logger.warn("websocket", "WebSocket disconnected");
181
+ this.handleDisconnect();
182
+ });
183
+ this.ws.on("error", (error) => {
184
+ logger.error("websocket", "WebSocket error", error);
185
+ reject(error);
186
+ });
187
+ });
188
+ } catch (error) {
189
+ logger.error("websocket", "Failed to connect", error);
190
+ throw error;
191
+ }
192
+ }
193
+ /**
194
+ * Disconnect from the WebSocket server
195
+ * Prevents automatic reconnection
196
+ */
197
+ disconnect() {
198
+ this.isManualDisconnect = true;
199
+ if (this.reconnectTimer) {
200
+ clearTimeout(this.reconnectTimer);
201
+ this.reconnectTimer = null;
202
+ }
203
+ if (this.ws) {
204
+ if (this.userId) {
205
+ this.sendMessage({
206
+ event: "pusher:unsubscribe",
207
+ data: {
208
+ channel: `private-user.${this.userId}`
209
+ }
210
+ });
211
+ }
212
+ this.ws.close();
213
+ this.ws = null;
214
+ logger.info("websocket", "WebSocket disconnected");
215
+ }
216
+ }
217
+ /**
218
+ * Check if WebSocket is currently connected
219
+ * @returns True if connected and ready
220
+ */
221
+ isConnected() {
222
+ return this.ws !== null && this.ws.readyState === WebSocket.OPEN;
223
+ }
224
+ /**
225
+ * Subscribe to user-specific channel for real-time updates
226
+ * @param userId - User ID to subscribe to
227
+ */
228
+ subscribeToUserChannel(userId) {
229
+ this.userId = userId;
230
+ if (!this.isConnected()) {
231
+ logger.warn("websocket", "Cannot subscribe: not connected");
232
+ return;
233
+ }
234
+ const channel = `private-user.${userId}`;
235
+ logger.info("websocket", `Subscribing to channel: ${channel}`);
236
+ this.sendMessage({
237
+ event: "pusher:subscribe",
238
+ data: {
239
+ channel
240
+ }
241
+ });
242
+ }
243
+ /**
244
+ * Register handler for commit approved events
245
+ * @param handler - Callback function
246
+ */
247
+ onCommitApproved(handler) {
248
+ this.commitApprovedHandlers.push(handler);
249
+ }
250
+ onCommitPending(handler) {
251
+ this.commitPendingHandlers.push(handler);
252
+ }
253
+ onSuggestionCreated(handler) {
254
+ this.suggestionCreatedHandlers.push(handler);
255
+ }
256
+ onProjectUpdated(handler) {
257
+ this.projectUpdatedHandlers.push(handler);
258
+ }
259
+ onDisconnect(handler) {
260
+ this.disconnectHandlers.push(handler);
261
+ }
262
+ onAgentDisconnected(handler) {
263
+ this.agentDisconnectedHandlers.push(handler);
264
+ }
265
+ onSyncRequested(handler) {
266
+ this.syncRequestedHandlers.push(handler);
267
+ }
268
+ sendMessage(message) {
269
+ if (!this.isConnected()) {
270
+ logger.warn("websocket", "Cannot send message: not connected");
271
+ return;
272
+ }
273
+ this.ws.send(JSON.stringify(message));
274
+ }
275
+ handleMessage(data) {
276
+ try {
277
+ const message = JSON.parse(data.toString());
278
+ logger.debug("websocket", `Received message: ${message.event}`);
279
+ if (message.event === "pusher:connection_established") {
280
+ logger.success("websocket", "Connection established");
281
+ return;
282
+ }
283
+ if (message.event === "pusher_internal:subscription_succeeded") {
284
+ logger.success("websocket", `Subscribed to channel: ${message.channel}`);
285
+ return;
286
+ }
287
+ if (message.event === "commit.approved") {
288
+ const { commit, project } = message.data;
289
+ logger.info("websocket", `Commit approved: ${commit.id}`);
290
+ this.commitApprovedHandlers.forEach((handler) => handler(commit, project));
291
+ return;
292
+ }
293
+ if (message.event === "commit.pending") {
294
+ const { pendingCommit } = message.data;
295
+ logger.info("websocket", `Commit pending: ${pendingCommit.id}`);
296
+ this.commitPendingHandlers.forEach((handler) => handler(pendingCommit));
297
+ return;
298
+ }
299
+ if (message.event === "suggestion.created") {
300
+ const { suggestion } = message.data;
301
+ logger.info("websocket", `Suggestion created: ${suggestion.id}`);
302
+ this.suggestionCreatedHandlers.forEach((handler) => handler(suggestion));
303
+ return;
304
+ }
305
+ if (message.event === "project.updated") {
306
+ const { project } = message.data;
307
+ logger.info("websocket", `Project updated: ${project.id}`);
308
+ this.projectUpdatedHandlers.forEach((handler) => handler(project));
309
+ return;
310
+ }
311
+ if (message.event === "sync.requested") {
312
+ const { projectId } = message.data;
313
+ logger.info("websocket", `Sync requested for project: ${projectId}`);
314
+ this.syncRequestedHandlers.forEach((handler) => handler(projectId));
315
+ return;
316
+ }
317
+ if (message.event === "agent.disconnected") {
318
+ const reason = message.data?.reason || "Server requested disconnect";
319
+ logger.warn("websocket", `Agent disconnected by server: ${reason}`);
320
+ this.agentDisconnectedHandlers.forEach((handler) => handler(reason));
321
+ return;
322
+ }
323
+ logger.debug("websocket", `Unhandled event: ${message.event}`);
324
+ } catch (error) {
325
+ logger.error("websocket", "Failed to parse message", error);
326
+ }
327
+ }
328
+ handleDisconnect() {
329
+ this.ws = null;
330
+ this.disconnectHandlers.forEach((handler) => handler());
331
+ if (this.isManualDisconnect) {
332
+ return;
333
+ }
334
+ if (this.reconnectAttempts < this.maxReconnectAttempts) {
335
+ const delay = this.getReconnectDelay();
336
+ this.reconnectAttempts++;
337
+ logger.info("websocket", `Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts})`);
338
+ this.reconnectTimer = setTimeout(async () => {
339
+ try {
340
+ await this.connect();
341
+ if (this.userId) {
342
+ this.subscribeToUserChannel(this.userId);
343
+ }
344
+ } catch (error) {
345
+ logger.error("websocket", "Reconnection failed", error);
346
+ }
347
+ }, delay);
348
+ } else {
349
+ logger.error("websocket", "Max reconnection attempts reached");
350
+ }
351
+ }
352
+ /**
353
+ * Get reconnect delay with exponential backoff and jitter
354
+ * Jitter prevents thundering herd problem when many clients reconnect simultaneously
355
+ */
356
+ getReconnectDelay() {
357
+ const delays = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
358
+ const index = Math.min(this.reconnectAttempts, delays.length - 1);
359
+ const baseDelay = delays[index];
360
+ const jitter = baseDelay * (Math.random() * 0.3);
361
+ return Math.floor(baseDelay + jitter);
362
+ }
363
+ };
364
+ var websocketService = new WebSocketServiceImpl();
365
+
366
+ export {
367
+ commitQueue,
368
+ websocketService
369
+ };