Repository navigation
Expand file tree
/
Copy path024_BackfillProjectionThreadShellSummary.ts
More file actions
277 lines (271 loc) · 9.38 KB
/
Copy path024_BackfillProjectionThreadShellSummary.ts
File metadata and controls
277 lines (271 loc) · 9.38 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import * as SqlClient from "effect/sql/SqlClient";
import * as Effect from "effect/Effect";
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`
INSERT OR IGNORE INTO projection_pending_approvals (
request_id,
thread_id,
turn_id,
status,
decision,
created_at,
resolved_at
)
SELECT
requested.request_id,
requested.thread_id,
requested.turn_id,
'pending',
NULL,
requested.created_at,
NULL
FROM (
SELECT
json_extract(payload_json, '$.requestId') AS request_id,
thread_id,
turn_id,
created_at,
ROW_NUMBER() OVER (
PARTITION BY json_extract(payload_json, '$.requestId')
ORDER BY created_at ASC, activity_id ASC
) AS row_number
FROM projection_thread_activities
WHERE kind = 'approval.requested'
AND json_extract(payload_json, '$.requestId') IS NOT NULL
) AS requested
WHERE requested.row_number = 1
`;
yield* sql`
WITH latest_resolutions AS (
SELECT
resolved.request_id,
resolved.resolved_at,
resolved.decision
FROM (
SELECT
json_extract(payload_json, '$.requestId') AS request_id,
created_at AS resolved_at,
CASE
WHEN json_extract(payload_json, '$.decision') IN (
'accept',
'acceptForSession',
'decline',
'cancel'
)
THEN json_extract(payload_json, '$.decision')
ELSE NULL
END AS decision,
ROW_NUMBER() OVER (
PARTITION BY json_extract(payload_json, '$.requestId')
ORDER BY created_at DESC, activity_id DESC
) AS row_number
FROM projection_thread_activities
WHERE kind = 'approval.resolved'
AND json_extract(payload_json, '$.requestId') IS NOT NULL
) AS resolved
WHERE resolved.row_number = 1
)
UPDATE projection_pending_approvals
SET
status = 'resolved',
decision = (
SELECT latest_resolutions.decision
FROM latest_resolutions
WHERE latest_resolutions.request_id = projection_pending_approvals.request_id
),
resolved_at = (
SELECT latest_resolutions.resolved_at
FROM latest_resolutions
WHERE latest_resolutions.request_id = projection_pending_approvals.request_id
)
WHERE EXISTS (
SELECT 1
FROM latest_resolutions
WHERE latest_resolutions.request_id = projection_pending_approvals.request_id
)
`;
yield* sql`
WITH latest_response_events AS (
SELECT
response.request_id,
response.resolved_at,
response.decision
FROM (
SELECT
json_extract(payload_json, '$.requestId') AS request_id,
occurred_at AS resolved_at,
CASE
WHEN json_extract(payload_json, '$.decision') IN (
'accept',
'acceptForSession',
'decline',
'cancel'
)
THEN json_extract(payload_json, '$.decision')
ELSE NULL
END AS decision,
ROW_NUMBER() OVER (
PARTITION BY json_extract(payload_json, '$.requestId')
ORDER BY occurred_at DESC, sequence DESC
) AS row_number
FROM orchestration_events
WHERE event_type = 'thread.approval-response-requested'
AND json_extract(payload_json, '$.requestId') IS NOT NULL
) AS response
WHERE response.row_number = 1
)
UPDATE projection_pending_approvals
SET
status = 'resolved',
decision = (
SELECT latest_response_events.decision
FROM latest_response_events
WHERE latest_response_events.request_id = projection_pending_approvals.request_id
),
resolved_at = (
SELECT latest_response_events.resolved_at
FROM latest_response_events
WHERE latest_response_events.request_id = projection_pending_approvals.request_id
)
WHERE EXISTS (
SELECT 1
FROM latest_response_events
WHERE latest_response_events.request_id = projection_pending_approvals.request_id
)
`;
yield* sql`
WITH latest_stale_failures AS (
SELECT
failure.request_id,
failure.resolved_at
FROM (
SELECT
json_extract(payload_json, '$.requestId') AS request_id,
created_at AS resolved_at,
ROW_NUMBER() OVER (
PARTITION BY json_extract(payload_json, '$.requestId')
ORDER BY created_at DESC, activity_id DESC
) AS row_number
FROM projection_thread_activities
WHERE kind = 'provider.approval.respond.failed'
AND json_extract(payload_json, '$.requestId') IS NOT NULL
AND (
lower(COALESCE(json_extract(payload_json, '$.detail'), ''))
LIKE '%stale pending approval request%'
OR lower(COALESCE(json_extract(payload_json, '$.detail'), ''))
LIKE '%unknown pending approval request%'
OR lower(COALESCE(json_extract(payload_json, '$.detail'), ''))
LIKE '%unknown pending permission request%'
)
) AS failure
WHERE failure.row_number = 1
)
UPDATE projection_pending_approvals
SET
status = 'resolved',
decision = NULL,
resolved_at = (
SELECT latest_stale_failures.resolved_at
FROM latest_stale_failures
WHERE latest_stale_failures.request_id = projection_pending_approvals.request_id
)
WHERE status = 'pending'
AND EXISTS (
SELECT 1
FROM latest_stale_failures
WHERE latest_stale_failures.request_id = projection_pending_approvals.request_id
)
`;
yield* sql`
UPDATE projection_threads
SET
latest_user_message_at = (
SELECT MAX(message.created_at)
FROM projection_thread_messages AS message
WHERE message.thread_id = projection_threads.thread_id
AND message.role = 'user'
),
pending_approval_count = COALESCE((
SELECT COUNT(*)
FROM projection_pending_approvals
WHERE projection_pending_approvals.thread_id = projection_threads.thread_id
AND projection_pending_approvals.status = 'pending'
), 0),
pending_user_input_count = COALESCE((
WITH latest_user_input_states AS (
SELECT
latest.request_id,
latest.kind,
latest.detail
FROM (
SELECT
json_extract(activity.payload_json, '$.requestId') AS request_id,
activity.kind,
lower(COALESCE(json_extract(activity.payload_json, '$.detail'), '')) AS detail,
ROW_NUMBER() OVER (
PARTITION BY json_extract(activity.payload_json, '$.requestId')
ORDER BY activity.created_at DESC, activity.activity_id DESC
) AS row_number
FROM projection_thread_activities AS activity
WHERE activity.thread_id = projection_threads.thread_id
AND json_extract(activity.payload_json, '$.requestId') IS NOT NULL
AND activity.kind IN (
'user-input.requested',
'user-input.resolved',
'provider.user-input.respond.failed'
)
) AS latest
WHERE latest.row_number = 1
)
SELECT COUNT(*)
FROM latest_user_input_states
WHERE latest_user_input_states.kind = 'user-input.requested'
OR (
latest_user_input_states.kind = 'provider.user-input.respond.failed'
AND latest_user_input_states.detail NOT LIKE '%stale pending user-input request%'
AND latest_user_input_states.detail NOT LIKE '%unknown pending user-input request%'
)
), 0),
has_actionable_proposed_plan = COALESCE((
SELECT CASE
WHEN projection_threads.latest_turn_id IS NOT NULL
AND EXISTS (
SELECT 1
FROM projection_thread_proposed_plans AS latest_turn_plan_exists
WHERE latest_turn_plan_exists.thread_id = projection_threads.thread_id
AND latest_turn_plan_exists.turn_id = projection_threads.latest_turn_id
)
THEN CASE
WHEN (
SELECT latest_turn_plan.implemented_at
FROM projection_thread_proposed_plans AS latest_turn_plan
WHERE latest_turn_plan.thread_id = projection_threads.thread_id
AND latest_turn_plan.turn_id = projection_threads.latest_turn_id
ORDER BY latest_turn_plan.updated_at DESC, latest_turn_plan.plan_id DESC
LIMIT 1
) IS NULL
THEN 1
ELSE 0
END
WHEN EXISTS (
SELECT 1
FROM projection_thread_proposed_plans AS any_plan
WHERE any_plan.thread_id = projection_threads.thread_id
)
THEN CASE
WHEN (
SELECT latest_plan.implemented_at
FROM projection_thread_proposed_plans AS latest_plan
WHERE latest_plan.thread_id = projection_threads.thread_id
ORDER BY latest_plan.updated_at DESC, latest_plan.plan_id DESC
LIMIT 1
) IS NULL
THEN 1
ELSE 0
END
ELSE 0
END
), 0)
`;
});