@allbluecn/web-app 0.28.11 → 0.28.13
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.
- package/package.json +5 -5
- package/public/dev-gallery.html +19 -0
- package/src/__tests__/bench/baselines/format-timestamp.json +44 -0
- package/src/__typeprobe__.d.ts +4 -0
- package/src/components/harness/extension-panel/panels/terminal/__tests__/terminal-instance.test.tsx +93 -4
- package/src/components/harness/extension-panel/panels/terminal/sse-transport.ts +28 -5
- package/src/components/harness/extension-panel/panels/terminal/terminal-instance.tsx +53 -10
- package/src/components/harness/extension-panel/panels/terminal/transport.ts +10 -3
- package/src/components/harness/extension-panel/panels/terminal/ws-transport.ts +13 -2
- package/src/components/settings/harness/connectors-card.tsx +5 -0
- package/src/components/story/detail/story-detail-toolbar/associated-attachments-panel.tsx +2 -1
- package/src/dev/gallery-entry.tsx +38 -0
- package/src/dev/gallery.tsx +610 -0
- package/src/lib/changelog/sdk-versions.ts +4 -4
- package/src/lib/session.ts +10 -0
- package/src/paraglide/README.md +1 -1
- package/src/paraglide/messages/_index.d.ts +9 -0
- package/src/paraglide/messages/_index.js +9 -0
- package/src/paraglide/messages/capabilities_webhook.d.ts +16 -0
- package/src/paraglide/messages/capabilities_webhook.js +29 -0
- package/src/paraglide/messages/capabilities_workflow.d.ts +16 -0
- package/src/paraglide/messages/capabilities_workflow.js +29 -0
- package/src/paraglide/messages/harness_panel_terminal_exitedrecycled1.d.ts +17 -0
- package/src/paraglide/messages/harness_panel_terminal_exitedrecycled1.js +30 -0
- package/src/paraglide/messages/harness_panel_terminal_restarted.d.ts +16 -0
- package/src/paraglide/messages/harness_panel_terminal_restarted.js +29 -0
- package/src/paraglide/messages/settings_harness_connectors_needsauth1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_connectors_needsauth1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphenabled1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphenabled1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphenabled_description1.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphenabled_description1.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds2.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds2.js +30 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds_description2.d.ts +17 -0
- package/src/paraglide/messages/settings_harness_ralphmaxrounds_description2.js +30 -0
- package/src/routeTree.gen.ts +21 -0
- package/src/routes/api/auth/desktop-callback.ts +9 -0
- package/src/routes/api/chat.ts +8 -1
- package/src/routes/api/harness/browser/proxy.ts +41 -12
- package/src/routes/api/harness/terminal/$.ts +32 -13
- package/src/routes/api/harness/terminal/__tests__/route.test.ts +41 -0
- package/src/routes/api/harness/webhook/$token.ts +23 -0
- package/src/routes/api/sync/__tests__/bootstrap.test.ts +32 -1
- package/src/routes/api/sync/bootstrap.ts +11 -0
- package/src/server/ai-gateway/adapter.ts +7 -3
- package/src/server/ai-gateway/connectivity-check.ts +3 -2
- package/src/server/ai-gateway/durability/__tests__/sse-resume-frames.test.ts +123 -0
- package/src/server/ai-gateway/embedding.ts +3 -2
- package/src/server/ai-gateway/model-fetch.ts +3 -2
- package/src/server/ai-gateway/sandbox/__tests__/provider.test.ts +12 -1
- package/src/server/ai-gateway/sandbox/policy.ts +27 -1
- package/src/server/ai-gateway/sandbox/provider.ts +10 -0
- package/src/server/ai-gateway/tools/definitions.ts +1 -1
- package/src/server/ai-gateway/tools/server.ts +4 -2
- package/src/server/harness/__tests__/agent-terminal-sandbox.test.ts +23 -4
- package/src/server/harness/__tests__/agent-terminal.server.test.ts +118 -2
- package/src/server/harness/__tests__/credentials.server.test.ts +25 -2
- package/src/server/harness/__tests__/host-schedule-worker.test.ts +43 -0
- package/src/server/harness/__tests__/runtime-without-providers.test.ts +11 -0
- package/src/server/harness/__tests__/sticky-route.test.ts +86 -0
- package/src/server/harness/__tests__/terminal-foreground.server.test.ts +80 -0
- package/src/server/harness/__tests__/terminal-manager.server.test.ts +75 -1
- package/src/server/harness/__tests__/tool-entitlements.test.ts +6 -2
- package/src/server/harness/agent-terminal.server.ts +25 -4
- package/src/server/harness/agents/__tests__/agent-control.test.ts +1 -0
- package/src/server/harness/agents/run-view.server.ts +55 -2
- package/src/server/harness/capabilities/__tests__/__snapshots__/matrix.test.ts.snap +92 -0
- package/src/server/harness/capabilities/__tests__/execution-plane-denials.test.ts +3 -1
- package/src/server/harness/capabilities/__tests__/p3-matrix.test.ts +3 -3
- package/src/server/harness/capabilities/__tests__/p5-matrix.test.ts +293 -0
- package/src/server/harness/capabilities/__tests__/validate.test.ts +8 -2
- package/src/server/harness/capabilities/execution-plane-denials.ts +9 -3
- package/src/server/harness/capabilities/manifests/agent-terminal.ts +3 -2
- package/src/server/harness/capabilities/manifests/webhook.ts +45 -0
- package/src/server/harness/capabilities/manifests/workflow.ts +40 -0
- package/src/server/harness/capabilities/registry.ts +4 -0
- package/src/server/harness/connectors/__tests__/connector-accounts.test.ts +22 -1
- package/src/server/harness/connectors/connector-accounts.server.ts +7 -0
- package/src/server/harness/credentials.server.ts +3 -1
- package/src/server/harness/define.server.ts +11 -0
- package/src/server/harness/mcp/mcp-config.server.ts +3 -1
- package/src/server/harness/outbox/outbox-dispatcher.server.ts +9 -0
- package/src/server/harness/outbox/outbox-store.server.ts +7 -0
- package/src/server/harness/persistence/__tests__/log-store.server.test.ts +4 -1
- package/src/server/harness/plugins/__tests__/tool-risk-audit.test.ts +5 -1
- package/src/server/harness/plugins/builtin/agent-terminal.plugin.ts +12 -2
- package/src/server/harness/providers/contracts.ts +16 -0
- package/src/server/harness/schedule/schedule-dispatcher.server.ts +3 -0
- package/src/server/harness/search/__tests__/indexer.test.ts +4 -1
- package/src/server/harness/search/__tests__/lifecycle.test.ts +4 -1
- package/src/server/harness/search/__tests__/search.test.ts +4 -1
- package/src/server/harness/sticky-route.server.ts +72 -0
- package/src/server/harness/terminal-foreground.server.ts +90 -0
- package/src/server/harness/terminal-manager.server.ts +78 -3
- package/src/server/harness/tool-entitlements.server.ts +5 -0
- package/src/server/harness/webhook/__tests__/body.test.ts +73 -0
- package/src/server/harness/webhook/__tests__/payload.test.ts +215 -0
- package/src/server/harness/webhook/__tests__/signature.test.ts +170 -0
- package/src/server/harness/webhook/__tests__/webhook-dispatcher.test.ts +212 -0
- package/src/server/harness/webhook/__tests__/webhook.plugin.test.ts +195 -0
- package/src/server/harness/webhook/__tests__/webhook.service.test.ts +354 -0
- package/src/server/harness/webhook/body.server.ts +56 -0
- package/src/server/harness/webhook/payload.server.ts +169 -0
- package/src/server/harness/webhook/signature.server.ts +118 -0
- package/src/server/harness/webhook/types.ts +57 -0
- package/src/server/harness/webhook/webhook-dispatcher.server.ts +495 -0
- package/src/server/harness/webhook/webhook-store.server.ts +115 -0
- package/src/server/harness/webhook/webhook.plugin.ts +273 -0
- package/src/server/harness/webhook/webhook.service.server.ts +577 -0
- package/src/server/harness/worker/__tests__/worker-http.test.ts +8 -1
- package/src/server/harness/worker/worker-health.server.ts +4 -1
- package/src/server/harness/worker/worker-runtime.server.ts +43 -1
- package/src/server/harness/workflow/__tests__/engine.test.ts +195 -0
- package/src/server/harness/workflow/__tests__/settings.test.ts +125 -0
- package/src/server/harness/workflow/__tests__/validate.test.ts +151 -0
- package/src/server/harness/workflow/engine.server.ts +155 -0
- package/src/server/harness/workflow/settings.ts +84 -0
- package/src/server/harness/workflow/store.server.ts +147 -0
- package/src/server/harness/workflow/types.ts +80 -0
- package/src/server/harness/workflow/validate.ts +130 -0
- package/src/server/harness/workflow/workflow.plugin.ts +261 -0
- package/src/server/knowledge/search-scope.ts +8 -1
- package/src/server/middlewares/security-headers.ts +1 -0
- package/src/server/serverFns/__tests__/link-preview.test.ts +2 -2
- package/src/server/serverFns/collab.ts +4 -0
- package/src/server/serverFns/design/versions.ts +6 -0
- package/src/server/serverFns/harness/__tests__/fork-thread.test.ts +14 -1
- package/src/server/serverFns/harness/fork-thread.ts +17 -0
- package/src/server/serverFns/story/link-preview.ts +3 -2
- package/src/server-entry.ts +13 -1
- package/src/start.ts +15 -1
- package/vite.shared.ts +0 -13
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
//
|
|
5
|
+
// webhook 投递 dispatcher(P5 任务 3,母规格 §5.2/§6.2)。
|
|
6
|
+
//
|
|
7
|
+
// 只领 kind='webhook.delivery' 的 outbox 行(与 schedule dispatcher 按 kind 隔离),
|
|
8
|
+
// 每行:复查 delivery 状态与规则 active → 渲染 prompt → host.submit 在该线程发起
|
|
9
|
+
// root Session → 标记 delivered。租约/心跳/fencing/DLQ 语义全部复用 P1 契约。
|
|
10
|
+
// Desktop 离线时 outbox 行持久化在库,恢复后按 inputId 幂等键补投(母规格 §8)。
|
|
11
|
+
|
|
12
|
+
import { randomUUID } from 'node:crypto'
|
|
13
|
+
|
|
14
|
+
import type { Prisma } from '@allbluecn/database'
|
|
15
|
+
import { prisma } from '@allbluecn/database'
|
|
16
|
+
|
|
17
|
+
import { auditLog } from '@/server/audit'
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
claim,
|
|
21
|
+
complete,
|
|
22
|
+
fail,
|
|
23
|
+
heartbeat,
|
|
24
|
+
OUTBOX_MAX_ATTEMPTS,
|
|
25
|
+
OutboxLeaseLostError,
|
|
26
|
+
releaseClaimed,
|
|
27
|
+
type ClaimedOutbox,
|
|
28
|
+
} from '../outbox/outbox-dispatcher.server'
|
|
29
|
+
import {
|
|
30
|
+
OUTBOX_KIND_WEBHOOK,
|
|
31
|
+
type ScheduleExecutionPlane,
|
|
32
|
+
} from '../outbox/outbox-store.server'
|
|
33
|
+
import { renderWebhookPrompt } from './payload.server'
|
|
34
|
+
import { resolveCurrentPlane } from '../schedule/schedule.service.server'
|
|
35
|
+
import type { ScheduleSubmit } from '../schedule/schedule-dispatcher.server'
|
|
36
|
+
import type { WebhookDeliveryPayload } from './webhook-store.server'
|
|
37
|
+
|
|
38
|
+
export interface WebhookDispatcherOptions {
|
|
39
|
+
userId?: string
|
|
40
|
+
plane?: ScheduleExecutionPlane
|
|
41
|
+
workerId?: string
|
|
42
|
+
limit?: number
|
|
43
|
+
now?: () => number
|
|
44
|
+
signal?: AbortSignal
|
|
45
|
+
submit: ScheduleSubmit
|
|
46
|
+
audit?: typeof auditLog
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface WebhookTickResult {
|
|
50
|
+
claimed: number
|
|
51
|
+
delivered: number
|
|
52
|
+
skipped: number
|
|
53
|
+
failed: number
|
|
54
|
+
retried: number
|
|
55
|
+
leaseLost: number
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type DeliveryResult = 'delivered' | 'skipped' | 'failed' | 'retried' | 'leaseLost'
|
|
59
|
+
|
|
60
|
+
type DeliveryTarget =
|
|
61
|
+
| {
|
|
62
|
+
kind: 'ready'
|
|
63
|
+
deliveryId: string
|
|
64
|
+
rule: { id: string; threadId: string; prompt: string; name: string; provider: string; workspaceId: string | null }
|
|
65
|
+
payload: WebhookDeliveryPayload
|
|
66
|
+
}
|
|
67
|
+
| { kind: 'skip'; reason: string; deliveryId?: string; ruleId?: string }
|
|
68
|
+
|
|
69
|
+
function emptyResult(): WebhookTickResult {
|
|
70
|
+
return { claimed: 0, delivered: 0, skipped: 0, failed: 0, retried: 0, leaseLost: 0 }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function asDeliveryPayload(value: Prisma.JsonValue): WebhookDeliveryPayload | null {
|
|
74
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) return null
|
|
75
|
+
const record = value as Record<string, unknown>
|
|
76
|
+
const summary = record.summary
|
|
77
|
+
if (typeof summary !== 'object' || summary === null || Array.isArray(summary)) return null
|
|
78
|
+
const summaryRecord = summary as Record<string, unknown>
|
|
79
|
+
if (typeof summaryRecord.event !== 'string') return null
|
|
80
|
+
if (typeof record.ruleId !== 'string') return null
|
|
81
|
+
if (typeof record.deliveryId !== 'string') return null
|
|
82
|
+
if (typeof record.threadId !== 'string') return null
|
|
83
|
+
if (typeof record.name !== 'string') return null
|
|
84
|
+
if (typeof record.provider !== 'string') return null
|
|
85
|
+
return {
|
|
86
|
+
ruleId: record.ruleId,
|
|
87
|
+
deliveryId: record.deliveryId,
|
|
88
|
+
threadId: record.threadId,
|
|
89
|
+
workspaceId: typeof record.workspaceId === 'string' ? record.workspaceId : null,
|
|
90
|
+
name: record.name,
|
|
91
|
+
provider: record.provider,
|
|
92
|
+
summary: {
|
|
93
|
+
event: summaryRecord.event,
|
|
94
|
+
action: typeof summaryRecord.action === 'string' ? summaryRecord.action : null,
|
|
95
|
+
repo: typeof summaryRecord.repo === 'string' ? summaryRecord.repo : null,
|
|
96
|
+
owner: typeof summaryRecord.owner === 'string' ? summaryRecord.owner : null,
|
|
97
|
+
ref: typeof summaryRecord.ref === 'string' ? summaryRecord.ref : null,
|
|
98
|
+
title: typeof summaryRecord.title === 'string' ? summaryRecord.title : null,
|
|
99
|
+
url: typeof summaryRecord.url === 'string' ? summaryRecord.url : null,
|
|
100
|
+
actor: typeof summaryRecord.actor === 'string' ? summaryRecord.actor : null,
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function resolveTarget(row: ClaimedOutbox, plane: ScheduleExecutionPlane): Promise<DeliveryTarget> {
|
|
106
|
+
if (row.kind !== OUTBOX_KIND_WEBHOOK) {
|
|
107
|
+
return { kind: 'skip', reason: 'invalid_outbox_kind' }
|
|
108
|
+
}
|
|
109
|
+
const payload = asDeliveryPayload(row.payload)
|
|
110
|
+
if (!payload) return { kind: 'skip', reason: 'invalid_outbox_payload' }
|
|
111
|
+
|
|
112
|
+
const delivery = await prisma.harnessWebhookDelivery.findFirst({
|
|
113
|
+
where: { id: payload.deliveryId, userId: row.userId },
|
|
114
|
+
select: { id: true, ruleId: true, executionPlane: true, state: true },
|
|
115
|
+
})
|
|
116
|
+
if (!delivery) {
|
|
117
|
+
return { kind: 'skip', reason: 'delivery_not_found', deliveryId: payload.deliveryId }
|
|
118
|
+
}
|
|
119
|
+
if (delivery.state !== 'pending' && delivery.state !== 'claimed') {
|
|
120
|
+
return {
|
|
121
|
+
kind: 'skip',
|
|
122
|
+
reason: `delivery_${delivery.state}`,
|
|
123
|
+
deliveryId: delivery.id,
|
|
124
|
+
ruleId: delivery.ruleId,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (delivery.executionPlane !== plane) {
|
|
128
|
+
return {
|
|
129
|
+
kind: 'skip',
|
|
130
|
+
reason: 'delivery_plane_mismatch',
|
|
131
|
+
deliveryId: delivery.id,
|
|
132
|
+
ruleId: delivery.ruleId,
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const rule = await prisma.harnessWebhookRule.findFirst({
|
|
137
|
+
where: { id: delivery.ruleId, userId: row.userId },
|
|
138
|
+
select: {
|
|
139
|
+
id: true,
|
|
140
|
+
threadId: true,
|
|
141
|
+
prompt: true,
|
|
142
|
+
name: true,
|
|
143
|
+
provider: true,
|
|
144
|
+
workspaceId: true,
|
|
145
|
+
status: true,
|
|
146
|
+
executionPlane: true,
|
|
147
|
+
},
|
|
148
|
+
})
|
|
149
|
+
if (!rule) return { kind: 'skip', reason: 'rule_not_found', deliveryId: delivery.id }
|
|
150
|
+
if (rule.status !== 'active') {
|
|
151
|
+
return {
|
|
152
|
+
kind: 'skip',
|
|
153
|
+
reason: `rule_${rule.status}`,
|
|
154
|
+
deliveryId: delivery.id,
|
|
155
|
+
ruleId: rule.id,
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (rule.executionPlane !== plane) {
|
|
159
|
+
return { kind: 'skip', reason: 'rule_plane_mismatch', deliveryId: delivery.id, ruleId: rule.id }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
kind: 'ready',
|
|
164
|
+
deliveryId: delivery.id,
|
|
165
|
+
rule: {
|
|
166
|
+
id: rule.id,
|
|
167
|
+
threadId: rule.threadId,
|
|
168
|
+
prompt: rule.prompt,
|
|
169
|
+
name: rule.name,
|
|
170
|
+
provider: rule.provider,
|
|
171
|
+
workspaceId: rule.workspaceId,
|
|
172
|
+
},
|
|
173
|
+
payload,
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function markSkipped(input: {
|
|
178
|
+
row: ClaimedOutbox
|
|
179
|
+
target: Extract<DeliveryTarget, { kind: 'skip' }>
|
|
180
|
+
now: Date
|
|
181
|
+
}): Promise<void> {
|
|
182
|
+
await prisma.$transaction(async (tx) => {
|
|
183
|
+
const completed = await complete({
|
|
184
|
+
id: input.row.id,
|
|
185
|
+
workerId: input.row.leaseOwner!,
|
|
186
|
+
fencingToken: input.row.fencingToken,
|
|
187
|
+
outcome: { kind: 'skipped', reason: input.target.reason },
|
|
188
|
+
now: input.now,
|
|
189
|
+
db: tx,
|
|
190
|
+
})
|
|
191
|
+
if (!completed) throw new OutboxLeaseLostError(input.row.id)
|
|
192
|
+
if (input.target.deliveryId) {
|
|
193
|
+
await tx.harnessWebhookDelivery.updateMany({
|
|
194
|
+
where: { id: input.target.deliveryId, userId: input.row.userId },
|
|
195
|
+
data: {
|
|
196
|
+
state: 'skipped',
|
|
197
|
+
leaseOwner: null,
|
|
198
|
+
leaseUntil: null,
|
|
199
|
+
lastError: input.target.reason,
|
|
200
|
+
attempt: input.row.attempt,
|
|
201
|
+
fencingToken: input.row.fencingToken,
|
|
202
|
+
},
|
|
203
|
+
})
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function markDelivered(input: {
|
|
209
|
+
row: ClaimedOutbox
|
|
210
|
+
target: Extract<DeliveryTarget, { kind: 'ready' }>
|
|
211
|
+
receipt: 'accepted' | 'queued' | 'duplicate'
|
|
212
|
+
now: Date
|
|
213
|
+
}): Promise<void> {
|
|
214
|
+
await prisma.$transaction(async (tx) => {
|
|
215
|
+
const completed = await complete({
|
|
216
|
+
id: input.row.id,
|
|
217
|
+
workerId: input.row.leaseOwner!,
|
|
218
|
+
fencingToken: input.row.fencingToken,
|
|
219
|
+
outcome: { kind: 'delivered', receipt: input.receipt },
|
|
220
|
+
now: input.now,
|
|
221
|
+
db: tx,
|
|
222
|
+
})
|
|
223
|
+
if (!completed) throw new OutboxLeaseLostError(input.row.id)
|
|
224
|
+
|
|
225
|
+
await tx.harnessWebhookDelivery.updateMany({
|
|
226
|
+
where: {
|
|
227
|
+
id: input.target.deliveryId,
|
|
228
|
+
userId: input.row.userId,
|
|
229
|
+
state: { in: ['pending', 'claimed', 'delivered'] },
|
|
230
|
+
},
|
|
231
|
+
data: {
|
|
232
|
+
state: 'delivered',
|
|
233
|
+
leaseOwner: null,
|
|
234
|
+
leaseUntil: null,
|
|
235
|
+
deliveredAt: input.now,
|
|
236
|
+
lastError: null,
|
|
237
|
+
attempt: input.row.attempt,
|
|
238
|
+
fencingToken: input.row.fencingToken,
|
|
239
|
+
},
|
|
240
|
+
})
|
|
241
|
+
await tx.harnessWebhookRule.updateMany({
|
|
242
|
+
where: { id: input.target.rule.id, userId: input.row.userId },
|
|
243
|
+
data: { lastDeliveryAt: input.now, lastError: null },
|
|
244
|
+
})
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function markRejected(input: {
|
|
249
|
+
row: ClaimedOutbox
|
|
250
|
+
target: Extract<DeliveryTarget, { kind: 'ready' }>
|
|
251
|
+
reason: string
|
|
252
|
+
now: Date
|
|
253
|
+
}): Promise<void> {
|
|
254
|
+
await prisma.$transaction(async (tx) => {
|
|
255
|
+
const completed = await complete({
|
|
256
|
+
id: input.row.id,
|
|
257
|
+
workerId: input.row.leaseOwner!,
|
|
258
|
+
fencingToken: input.row.fencingToken,
|
|
259
|
+
outcome: { kind: 'rejected', reason: input.reason },
|
|
260
|
+
now: input.now,
|
|
261
|
+
db: tx,
|
|
262
|
+
})
|
|
263
|
+
if (!completed) throw new OutboxLeaseLostError(input.row.id)
|
|
264
|
+
await tx.harnessWebhookDelivery.updateMany({
|
|
265
|
+
where: { id: input.target.deliveryId, userId: input.row.userId },
|
|
266
|
+
data: {
|
|
267
|
+
state: 'failed',
|
|
268
|
+
leaseOwner: null,
|
|
269
|
+
leaseUntil: null,
|
|
270
|
+
lastError: input.reason,
|
|
271
|
+
attempt: input.row.attempt,
|
|
272
|
+
fencingToken: input.row.fencingToken,
|
|
273
|
+
},
|
|
274
|
+
})
|
|
275
|
+
await tx.harnessWebhookRule.updateMany({
|
|
276
|
+
where: { id: input.target.rule.id, userId: input.row.userId },
|
|
277
|
+
data: { lastError: input.reason },
|
|
278
|
+
})
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function markRetryableFailure(input: {
|
|
283
|
+
row: ClaimedOutbox
|
|
284
|
+
target: Extract<DeliveryTarget, { kind: 'ready' }>
|
|
285
|
+
error: unknown
|
|
286
|
+
now: Date
|
|
287
|
+
}): Promise<DeliveryResult> {
|
|
288
|
+
const terminal = input.row.attempt >= OUTBOX_MAX_ATTEMPTS
|
|
289
|
+
const lastError = input.error instanceof Error ? input.error.message : String(input.error)
|
|
290
|
+
|
|
291
|
+
await prisma.$transaction(async (tx) => {
|
|
292
|
+
const fenced = await heartbeat({
|
|
293
|
+
id: input.row.id,
|
|
294
|
+
workerId: input.row.leaseOwner!,
|
|
295
|
+
fencingToken: input.row.fencingToken,
|
|
296
|
+
now: input.now,
|
|
297
|
+
db: tx,
|
|
298
|
+
})
|
|
299
|
+
if (!fenced) throw new OutboxLeaseLostError(input.row.id)
|
|
300
|
+
|
|
301
|
+
await tx.harnessWebhookDelivery.updateMany({
|
|
302
|
+
where: {
|
|
303
|
+
id: input.target.deliveryId,
|
|
304
|
+
userId: input.row.userId,
|
|
305
|
+
state: { in: ['pending', 'claimed'] },
|
|
306
|
+
fencingToken: { lte: input.row.fencingToken },
|
|
307
|
+
},
|
|
308
|
+
data: {
|
|
309
|
+
state: terminal ? 'failed' : 'pending',
|
|
310
|
+
leaseOwner: null,
|
|
311
|
+
leaseUntil: null,
|
|
312
|
+
lastError,
|
|
313
|
+
attempt: input.row.attempt,
|
|
314
|
+
fencingToken: input.row.fencingToken,
|
|
315
|
+
},
|
|
316
|
+
})
|
|
317
|
+
await tx.harnessWebhookRule.updateMany({
|
|
318
|
+
where: { id: input.target.rule.id, userId: input.row.userId },
|
|
319
|
+
data: { lastError },
|
|
320
|
+
})
|
|
321
|
+
await fail({
|
|
322
|
+
id: input.row.id,
|
|
323
|
+
workerId: input.row.leaseOwner!,
|
|
324
|
+
fencingToken: input.row.fencingToken,
|
|
325
|
+
error: input.error,
|
|
326
|
+
now: input.now,
|
|
327
|
+
db: tx,
|
|
328
|
+
})
|
|
329
|
+
})
|
|
330
|
+
return terminal ? 'failed' : 'retried'
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async function deliver(
|
|
334
|
+
row: ClaimedOutbox,
|
|
335
|
+
options: WebhookDispatcherOptions,
|
|
336
|
+
plane: ScheduleExecutionPlane,
|
|
337
|
+
now: Date,
|
|
338
|
+
signal: AbortSignal,
|
|
339
|
+
): Promise<DeliveryResult | null> {
|
|
340
|
+
if (signal.aborted) return null
|
|
341
|
+
const target = await resolveTarget(row, plane)
|
|
342
|
+
if (signal.aborted) return null
|
|
343
|
+
if (target.kind === 'skip') {
|
|
344
|
+
try {
|
|
345
|
+
await markSkipped({ row, target, now })
|
|
346
|
+
return 'skipped'
|
|
347
|
+
} catch (error) {
|
|
348
|
+
if (error instanceof OutboxLeaseLostError) return 'leaseLost'
|
|
349
|
+
throw error
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
try {
|
|
354
|
+
if (signal.aborted) return null
|
|
355
|
+
const message = renderWebhookPrompt({
|
|
356
|
+
name: target.rule.name,
|
|
357
|
+
provider: target.rule.provider,
|
|
358
|
+
prompt: target.rule.prompt,
|
|
359
|
+
summary: target.payload.summary,
|
|
360
|
+
})
|
|
361
|
+
const receipt = await options.submit({
|
|
362
|
+
userId: row.userId,
|
|
363
|
+
threadId: target.rule.threadId,
|
|
364
|
+
inputId: row.inputId,
|
|
365
|
+
message,
|
|
366
|
+
})
|
|
367
|
+
if (signal.aborted) return null
|
|
368
|
+
if (receipt.inputId !== row.inputId) {
|
|
369
|
+
await markRejected({ row, target, reason: 'input_id_mismatch', now })
|
|
370
|
+
return 'failed'
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
switch (receipt.status) {
|
|
374
|
+
case 'accepted':
|
|
375
|
+
case 'duplicate':
|
|
376
|
+
await markDelivered({ row, target, receipt: receipt.status, now })
|
|
377
|
+
break
|
|
378
|
+
case 'queued':
|
|
379
|
+
await markDelivered({ row, target, receipt: 'queued', now })
|
|
380
|
+
break
|
|
381
|
+
case 'rejected':
|
|
382
|
+
await markRejected({ row, target, reason: receipt.reason, now })
|
|
383
|
+
return 'failed'
|
|
384
|
+
default: {
|
|
385
|
+
const exhaustive: never = receipt
|
|
386
|
+
void exhaustive
|
|
387
|
+
return await markRetryableFailure({
|
|
388
|
+
row,
|
|
389
|
+
target,
|
|
390
|
+
error: new Error('unsupported_receipt'),
|
|
391
|
+
now,
|
|
392
|
+
})
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
await writeDeliverAudit(options, target, row)
|
|
397
|
+
return 'delivered'
|
|
398
|
+
} catch (error) {
|
|
399
|
+
if (signal.aborted) return null
|
|
400
|
+
if (error instanceof OutboxLeaseLostError) return 'leaseLost'
|
|
401
|
+
try {
|
|
402
|
+
return await markRetryableFailure({ row, target, error, now })
|
|
403
|
+
} catch (retryError) {
|
|
404
|
+
if (retryError instanceof OutboxLeaseLostError) return 'leaseLost'
|
|
405
|
+
throw retryError
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function writeDeliverAudit(
|
|
411
|
+
options: WebhookDispatcherOptions,
|
|
412
|
+
target: Extract<DeliveryTarget, { kind: 'ready' }>,
|
|
413
|
+
row: ClaimedOutbox,
|
|
414
|
+
): Promise<void> {
|
|
415
|
+
const audit = options.audit ?? auditLog
|
|
416
|
+
try {
|
|
417
|
+
await audit({
|
|
418
|
+
workspaceId: target.rule.workspaceId ?? '',
|
|
419
|
+
userId: row.userId,
|
|
420
|
+
resourceType: 'harness_webhook',
|
|
421
|
+
resourceId: target.rule.id,
|
|
422
|
+
action: 'harness.webhook.deliver',
|
|
423
|
+
metadata: {
|
|
424
|
+
deliveryId: target.deliveryId,
|
|
425
|
+
event: target.payload.summary.event,
|
|
426
|
+
repo: target.payload.summary.repo,
|
|
427
|
+
},
|
|
428
|
+
})
|
|
429
|
+
} catch (error) {
|
|
430
|
+
console.warn('[webhook] 投递审计写入失败:', error instanceof Error ? error.message : 'unknown')
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export function createWebhookDispatcher(options: WebhookDispatcherOptions) {
|
|
435
|
+
const workerId = options.workerId ?? `webhook-${randomUUID()}`
|
|
436
|
+
const controller = new AbortController()
|
|
437
|
+
const abortFromParent = () => controller.abort()
|
|
438
|
+
if (options.signal?.aborted) controller.abort()
|
|
439
|
+
else options.signal?.addEventListener('abort', abortFromParent, { once: true })
|
|
440
|
+
let running = false
|
|
441
|
+
let queue: Promise<WebhookTickResult> = Promise.resolve(emptyResult())
|
|
442
|
+
|
|
443
|
+
const runTick = async (): Promise<WebhookTickResult> => {
|
|
444
|
+
if (controller.signal.aborted) return emptyResult()
|
|
445
|
+
const plane = options.plane ?? resolveCurrentPlane()
|
|
446
|
+
const nowMs = options.now?.() ?? Date.now()
|
|
447
|
+
const limit = options.limit ?? 100
|
|
448
|
+
const result = emptyResult()
|
|
449
|
+
|
|
450
|
+
const rows = await claim({
|
|
451
|
+
workerId,
|
|
452
|
+
userId: options.userId,
|
|
453
|
+
plane,
|
|
454
|
+
kinds: [OUTBOX_KIND_WEBHOOK],
|
|
455
|
+
limit,
|
|
456
|
+
now: new Date(nowMs),
|
|
457
|
+
signal: controller.signal,
|
|
458
|
+
})
|
|
459
|
+
if (controller.signal.aborted) {
|
|
460
|
+
if (rows.length > 0) await releaseClaimed({ workerId, rows, now: new Date(nowMs) })
|
|
461
|
+
return result
|
|
462
|
+
}
|
|
463
|
+
result.claimed = rows.length
|
|
464
|
+
for (let index = 0; index < rows.length; index += 1) {
|
|
465
|
+
if (controller.signal.aborted) {
|
|
466
|
+
await releaseClaimed({ workerId, rows: rows.slice(index), now: new Date(nowMs) })
|
|
467
|
+
break
|
|
468
|
+
}
|
|
469
|
+
const outcome = await deliver(rows[index], options, plane, new Date(nowMs), controller.signal)
|
|
470
|
+
if (outcome === null) {
|
|
471
|
+
await releaseClaimed({ workerId, rows: rows.slice(index), now: new Date(nowMs) })
|
|
472
|
+
break
|
|
473
|
+
}
|
|
474
|
+
result[outcome] += 1
|
|
475
|
+
}
|
|
476
|
+
return result
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const tick = (): Promise<WebhookTickResult> => {
|
|
480
|
+
if (controller.signal.aborted) return Promise.resolve(emptyResult())
|
|
481
|
+
if (running) return queue
|
|
482
|
+
running = true
|
|
483
|
+
queue = runTick().finally(() => {
|
|
484
|
+
running = false
|
|
485
|
+
})
|
|
486
|
+
return queue
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const cancel = () => {
|
|
490
|
+
options.signal?.removeEventListener('abort', abortFromParent)
|
|
491
|
+
controller.abort()
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return { tick, cancel }
|
|
495
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// AllBlue - 理想之海
|
|
3
|
+
// Copyright (C) 2026 AllBlue Contributors
|
|
4
|
+
|
|
5
|
+
// webhook 投递的 outbox 落库(P5 任务 3)。
|
|
6
|
+
// 复用 P1 已交付的 harness_outbox 契约(租约 / heartbeat / fencing / DLQ),
|
|
7
|
+
// 只新增 kind='webhook.delivery' 一种负载,不新建第二套队列(母规格 §5.2)。
|
|
8
|
+
|
|
9
|
+
import { prisma, type Prisma } from '@allbluecn/database'
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
OUTBOX_KIND_SCHEDULE,
|
|
13
|
+
OUTBOX_KIND_WEBHOOK,
|
|
14
|
+
SCHEDULE_OUTBOX_SELECT,
|
|
15
|
+
type ScheduleExecutionPlane,
|
|
16
|
+
} from '../outbox/outbox-store.server'
|
|
17
|
+
import type { WebhookEventSummary } from './types'
|
|
18
|
+
|
|
19
|
+
/** outbox 选择集与 schedule 同源(同一张表、同一租约契约)。 */
|
|
20
|
+
export const WEBHOOK_OUTBOX_SELECT = SCHEDULE_OUTBOX_SELECT
|
|
21
|
+
|
|
22
|
+
export const WEBHOOK_OUTBOX_KIND = OUTBOX_KIND_WEBHOOK
|
|
23
|
+
export const SCHEDULE_OUTBOX_KIND = OUTBOX_KIND_SCHEDULE
|
|
24
|
+
|
|
25
|
+
/** 投递给 Worker 的载荷快照:足够渲染 prompt,且不含签名/密钥/payload 全文。 */
|
|
26
|
+
export interface WebhookDeliveryPayload {
|
|
27
|
+
ruleId: string
|
|
28
|
+
deliveryId: string
|
|
29
|
+
threadId: string
|
|
30
|
+
workspaceId: string | null
|
|
31
|
+
name: string
|
|
32
|
+
provider: string
|
|
33
|
+
summary: WebhookEventSummary
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type OutboxDb = Pick<Prisma.TransactionClient, 'harnessOutbox'>
|
|
37
|
+
|
|
38
|
+
export function webhookInputId(deliveryId: string): string {
|
|
39
|
+
return `webhook:${deliveryId}`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 建/复投 webhook outbox 行。按 inputId 幂等:同一 delivery 只会有一行,
|
|
44
|
+
* 崩溃后重入不产生重复投递(与 schedule 的 inputId 语义一致)。
|
|
45
|
+
*/
|
|
46
|
+
export async function createWebhookOutbox(
|
|
47
|
+
tx: OutboxDb,
|
|
48
|
+
input: {
|
|
49
|
+
userId: string
|
|
50
|
+
executionPlane: ScheduleExecutionPlane
|
|
51
|
+
deliveryId: string
|
|
52
|
+
payload: WebhookDeliveryPayload
|
|
53
|
+
nextAttemptAt: Date
|
|
54
|
+
},
|
|
55
|
+
) {
|
|
56
|
+
const inputId = webhookInputId(input.deliveryId)
|
|
57
|
+
return tx.harnessOutbox.upsert({
|
|
58
|
+
where: { inputId },
|
|
59
|
+
create: {
|
|
60
|
+
userId: input.userId,
|
|
61
|
+
executionPlane: input.executionPlane,
|
|
62
|
+
kind: WEBHOOK_OUTBOX_KIND,
|
|
63
|
+
occurrenceId: null,
|
|
64
|
+
inputId,
|
|
65
|
+
payload: input.payload as unknown as Prisma.InputJsonValue,
|
|
66
|
+
idempotencyKey: inputId,
|
|
67
|
+
nextAttemptAt: input.nextAttemptAt,
|
|
68
|
+
},
|
|
69
|
+
update: {
|
|
70
|
+
executionPlane: input.executionPlane,
|
|
71
|
+
payload: input.payload as unknown as Prisma.InputJsonValue,
|
|
72
|
+
state: 'pending',
|
|
73
|
+
leaseOwner: null,
|
|
74
|
+
leaseUntil: null,
|
|
75
|
+
lastHeartbeatAt: null,
|
|
76
|
+
lastError: null,
|
|
77
|
+
nextAttemptAt: input.nextAttemptAt,
|
|
78
|
+
},
|
|
79
|
+
select: WEBHOOK_OUTBOX_SELECT,
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** 取消规则下尚未投递的 outbox(规则删除/暂停时调用,与 schedule 删除同语义)。 */
|
|
84
|
+
export async function cancelPendingWebhookOutbox(
|
|
85
|
+
tx: Pick<Prisma.TransactionClient, 'harnessWebhookDelivery' | 'harnessOutbox'>,
|
|
86
|
+
input: { userId: string; deliveryIds: string[] },
|
|
87
|
+
): Promise<number> {
|
|
88
|
+
if (input.deliveryIds.length === 0) return 0
|
|
89
|
+
const inputIds = input.deliveryIds.map(webhookInputId)
|
|
90
|
+
const result = await tx.harnessOutbox.updateMany({
|
|
91
|
+
where: { userId: input.userId, inputId: { in: inputIds }, state: 'pending' },
|
|
92
|
+
data: { state: 'dead', lastError: 'webhook_rule_removed' },
|
|
93
|
+
})
|
|
94
|
+
return result.count
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 默认库上的读取(dispatcher 热路径之外的小查询)。 */
|
|
98
|
+
export function findWebhookDeliveryForDispatch(input: {
|
|
99
|
+
deliveryId: string
|
|
100
|
+
userId: string
|
|
101
|
+
}) {
|
|
102
|
+
return prisma.harnessWebhookDelivery.findFirst({
|
|
103
|
+
where: { id: input.deliveryId, userId: input.userId },
|
|
104
|
+
select: {
|
|
105
|
+
id: true,
|
|
106
|
+
ruleId: true,
|
|
107
|
+
userId: true,
|
|
108
|
+
executionPlane: true,
|
|
109
|
+
eventType: true,
|
|
110
|
+
state: true,
|
|
111
|
+
attempt: true,
|
|
112
|
+
fencingToken: true,
|
|
113
|
+
},
|
|
114
|
+
})
|
|
115
|
+
}
|