|
1
|
import { |
|
2
|
WebhookEvent, |
|
3
|
WebhookProperty, |
|
4
|
WebhookPropertyChange, |
|
5
|
WebhooksAnalysisResult, |
|
6
|
WebhooksDispatchPayload, |
|
7
|
} from "../types/index.js" |
|
8
|
|
|
9
|
const REFERENCE_PAGE = "www/apps/cloud/app/webhooks/page.mdx" |
|
10
|
const CHANGELOG_PAGE = "www/apps/cloud/app/webhooks/changelog/page.mdx" |
|
11
|
|
|
12
|
/** |
|
13
|
* Builds the Claude prompt that updates the Cloud webhooks reference and |
|
14
|
* changelog pages from a webhook deployment dispatch payload. |
|
15
|
* |
|
16
|
* Webhook docs are dated rather than versioned, so every entry is keyed by the |
|
17
|
* date the change went live. |
|
18
|
* |
|
19
|
* Everything that must be accurate (payload schemas, example payloads, and |
|
20
|
* changelog entries) is rendered here as ready-to-paste MDX so that Claude |
|
21
|
* only decides placement and prose, never the schema itself. |
|
22
|
*/ |
|
23
|
export class WebhooksContextBuilder { |
|
24
|
build( |
|
25
|
payload: WebhooksDispatchPayload, |
|
26
|
fallbackDate: Date |
|
27
|
): WebhooksAnalysisResult { |
|
28
|
const events = payload.webhooks ?? [] |
|
29
|
const changelogDate = this.formatDate(payload.releasedAt, fallbackDate) |
|
30
|
|
|
31
|
return { |
|
32
|
affectedProjects: [ |
|
33
|
{ |
|
34
|
project: "cloud", |
|
35
|
reason: "Webhook event delivery changed in Cloud Production", |
|
36
|
}, |
|
37
|
], |
|
38
|
claudePrompt: this.buildPrompt(payload, events, changelogDate), |
|
39
|
featureFlaggedFeatures: events |
|
40
|
.filter((event) => !!event.featureFlag) |
|
41
|
.map((event) => `${event.event} (${event.featureFlag})`), |
|
42
|
changelogDate, |
|
43
|
changeSummary: events.map( |
|
44
|
(event) => `${this.pastTense(event.changeType)} \`${event.event}\`` |
|
45
|
), |
|
46
|
} |
|
47
|
} |
|
48
|
|
|
49
|
/** |
|
50
|
* Formats `YYYY-MM-DD` as "August 4, 2026". Falls back to the date the |
|
51
|
* automation runs when the payload doesn't carry a release date. |
|
52
|
*/ |
|
53
|
private formatDate(releasedAt: string | undefined, fallback: Date): string { |
|
54
|
const date = releasedAt ? new Date(`${releasedAt}T00:00:00Z`) : fallback |
|
55
|
|
|
56
|
if (Number.isNaN(date.getTime())) { |
|
57
|
throw new Error( |
|
58
|
`Invalid releasedAt value: ${releasedAt}. Expected YYYY-MM-DD.` |
|
59
|
) |
|
60
|
} |
|
61
|
|
|
62
|
return date.toLocaleDateString("en-US", { |
|
63
|
year: "numeric", |
|
64
|
month: "long", |
|
65
|
day: "numeric", |
|
66
|
timeZone: "UTC", |
|
67
|
}) |
|
68
|
} |
|
69
|
|
|
70
|
private pastTense(changeType: WebhookEvent["changeType"]): string { |
|
71
|
switch (changeType) { |
|
72
|
case "added": |
|
73
|
return "Added" |
|
74
|
case "removed": |
|
75
|
return "Removed" |
|
76
|
default: |
|
77
|
return "Updated" |
|
78
|
} |
|
79
|
} |
|
80
|
|
|
81
|
/** Wraps a value in a template literal, escaping what would break it. */ |
|
82
|
private toTemplate(value: string): string { |
|
83
|
const escaped = value |
|
84
|
.replace(/\\/g, "\\\\") |
|
85
|
.replace(/`/g, "\\`") |
|
86
|
.replace(/\$\{/g, "\\${") |
|
87
|
|
|
88
|
return `\`${escaped}\`` |
|
89
|
} |
|
90
|
|
|
91
|
/** Types are shown as inline code in the docs, e.g. `` "`string`" ``. */ |
|
92
|
private toTypeValue(type: string): string { |
|
93
|
return JSON.stringify(`\`${type.replace(/`/g, "")}\``) |
|
94
|
} |
|
95
|
|
|
96
|
private renderProperties( |
|
97
|
properties: WebhookProperty[], |
|
98
|
indentLevel: number |
|
99
|
): string { |
|
100
|
const indent = " ".repeat(indentLevel * 2) |
|
101
|
const inner = " ".repeat((indentLevel + 1) * 2) |
|
102
|
|
|
103
|
return properties |
|
104
|
.map((property) => { |
|
105
|
const lines = [ |
|
106
|
`${indent}{`, |
|
107
|
`${inner}name: ${JSON.stringify(property.name)},`, |
|
108
|
`${inner}type: ${this.toTypeValue(property.type)},`, |
|
109
|
] |
|
110
|
|
|
111
|
if (property.description) { |
|
112
|
lines.push( |
|
113
|
`${inner}description: ${this.toTemplate(property.description)},` |
|
114
|
) |
|
115
|
} |
|
116
|
|
|
117
|
if (property.optional) { |
|
118
|
lines.push(`${inner}optional: true,`) |
|
119
|
} |
|
120
|
|
|
121
|
if (property.defaultValue) { |
|
122
|
lines.push( |
|
123
|
`${inner}defaultValue: ${this.toTemplate(property.defaultValue)},` |
|
124
|
) |
|
125
|
} |
|
126
|
|
|
127
|
if (property.children?.length) { |
|
128
|
lines.push(`${inner}children: [`) |
|
129
|
lines.push(this.renderProperties(property.children, indentLevel + 2)) |
|
130
|
lines.push(`${inner}],`) |
|
131
|
} |
|
132
|
|
|
133
|
lines.push(`${indent}},`) |
|
134
|
|
|
135
|
return lines.join("\n") |
|
136
|
}) |
|
137
|
.join("\n") |
|
138
|
} |
|
139
|
|
|
140
|
/** Renders the `TypeList` block documenting an event's payload. */ |
|
141
|
private renderTypeList(event: WebhookEvent): string { |
|
142
|
if (!event.properties?.length) { |
|
143
|
return "" |
|
144
|
} |
|
145
|
|
|
146
|
return [ |
|
147
|
`<TypeList`, |
|
148
|
` types={[`, |
|
149
|
this.renderProperties(event.properties, 2), |
|
150
|
` ]}`, |
|
151
|
` sectionTitle="${event.event}"`, |
|
152
|
`/>`, |
|
153
|
].join("\n") |
|
154
|
} |
|
155
|
|
|
156
|
/** Renders the example payload code block for an event. */ |
|
157
|
private renderExample(event: WebhookEvent): string { |
|
158
|
if (event.example === undefined || event.example === null) { |
|
159
|
return "" |
|
160
|
} |
|
161
|
|
|
162
|
const json = |
|
163
|
typeof event.example === "string" |
|
164
|
? event.example.trim() |
|
165
|
: JSON.stringify(event.example, null, 2) |
|
166
|
|
|
167
|
return ["```json", json, "```"].join("\n") |
|
168
|
} |
|
169
|
|
|
170
|
/** Renders the inline "Changes" note added under a changed event. */ |
|
171
|
private renderChangesNote( |
|
172
|
changes: WebhookPropertyChange[], |
|
173
|
changelogDate: string |
|
174
|
): string { |
|
175
|
if (!changes.length) { |
|
176
|
return "" |
|
177
|
} |
|
178
|
|
|
179
|
const lines = [`<Note title="Changes">`, ""] |
|
180
|
|
|
181
|
for (const change of changes) { |
|
182
|
lines.push( |
|
183
|
`- ${this.describePropertyChange(change, changelogDate)}`.trimEnd() |
|
184
|
) |
|
185
|
} |
|
186
|
|
|
187
|
lines.push("", "</Note>") |
|
188
|
|
|
189
|
return lines.join("\n") |
|
190
|
} |
|
191
|
|
|
192
|
private describePropertyChange( |
|
193
|
change: WebhookPropertyChange, |
|
194
|
changelogDate: string |
|
195
|
): string { |
|
196
|
const verb = |
|
197
|
change.changeType === "added" |
|
198
|
? "added" |
|
199
|
: change.changeType === "removed" |
|
200
|
? "removed" |
|
201
|
: "changed" |
|
202
|
|
|
203
|
const sentence = `Medusa ${verb} the \`${change.property}\` property on ${changelogDate}.` |
|
204
|
|
|
205
|
return change.description ? `${sentence} ${change.description}` : sentence |
|
206
|
} |
|
207
|
|
|
208
|
/** Renders the full changelog entry for this release. */ |
|
209
|
private renderChangelogEntry( |
|
210
|
events: WebhookEvent[], |
|
211
|
descriptions: string | undefined, |
|
212
|
changelogDate: string |
|
213
|
): string { |
|
214
|
const lines = [`## ${changelogDate}`, ""] |
|
215
|
const sorted = [...events].sort((a, b) => a.event.localeCompare(b.event)) |
|
216
|
|
|
217
|
for (const event of sorted) { |
|
218
|
lines.push(`### ${event.event}`, "") |
|
219
|
|
|
220
|
if (event.changeType === "added") { |
|
221
|
const suffix = event.description |
|
222
|
? ` ${this.toSentence(event.description)}` |
|
223
|
: "" |
|
224
|
lines.push(`- Added the \`${event.event}\` event.${suffix}`) |
|
225
|
} else if (event.changeType === "removed") { |
|
226
|
lines.push( |
|
227
|
`- Removed the \`${event.event}\` event. Medusa no longer delivers it.` |
|
228
|
) |
|
229
|
} |
|
230
|
|
|
231
|
for (const change of event.propertyChanges ?? []) { |
|
232
|
const suffix = change.description ? ` ${change.description}` : "" |
|
233
|
lines.push( |
|
234
|
`- ${this.pastTense(change.changeType)} the \`${change.property}\` property.${suffix}` |
|
235
|
) |
|
236
|
} |
|
237
|
|
|
238
|
if ( |
|
239
|
event.changeType === "updated" && |
|
240
|
!(event.propertyChanges ?? []).length |
|
241
|
) { |
|
242
|
const suffix = event.description |
|
243
|
? ` ${this.toSentence(event.description)}` |
|
244
|
: "" |
|
245
|
lines.push(`- Updated the \`${event.event}\` event.${suffix}`) |
|
246
|
} |
|
247
|
|
|
248
|
lines.push("") |
|
249
|
} |
|
250
|
|
|
251
|
if (descriptions?.trim()) { |
|
252
|
lines.push("### Delivery", "", descriptions.trim(), "") |
|
253
|
} |
|
254
|
|
|
255
|
return lines.join("\n").trimEnd() |
|
256
|
} |
|
257
|
|
|
258
|
/** Makes sure a description reads as its own sentence in a bullet point. */ |
|
259
|
private toSentence(description: string): string { |
|
260
|
const trimmed = description.trim() |
|
261
|
|
|
262
|
return /[.!?]$/.test(trimmed) ? trimmed : `${trimmed}.` |
|
263
|
} |
|
264
|
|
|
265
|
private buildPrompt( |
|
266
|
payload: WebhooksDispatchPayload, |
|
267
|
events: WebhookEvent[], |
|
268
|
changelogDate: string |
|
269
|
): string { |
|
270
|
const sections: string[] = [ |
|
271
|
this.buildSkillSection(), |
|
272
|
this.buildTaskSection(changelogDate), |
|
273
|
this.buildEventsSection(events, changelogDate), |
|
274
|
] |
|
275
|
|
|
276
|
if (payload.descriptions?.trim()) { |
|
277
|
sections.push(this.buildDescriptionsSection(payload.descriptions.trim())) |
|
278
|
} |
|
279
|
|
|
280
|
sections.push( |
|
281
|
this.buildChangelogSection( |
|
282
|
this.renderChangelogEntry(events, payload.descriptions, changelogDate), |
|
283
|
changelogDate |
|
284
|
) |
|
285
|
) |
|
286
|
sections.push(this.buildAffectedProjectSection()) |
|
287
|
sections.push(this.buildConstraintsSection()) |
|
288
|
|
|
289
|
return sections.join("\n\n---\n\n") |
|
290
|
} |
|
291
|
|
|
292
|
private buildSkillSection(): string { |
|
293
|
return `## Instructions |
|
294
|
|
|
295
|
Use the /writing-docs skill before making any documentation changes. Load the \`reference/cloud-style.md\` reference file from that skill. It contains the rules and patterns for writing Cloud documentation correctly, including the Webhooks section that describes the structure of the pages you're about to update.` |
|
296
|
} |
|
297
|
|
|
298
|
private buildTaskSection(changelogDate: string): string { |
|
299
|
return `## Task |
|
300
|
|
|
301
|
Cloud's webhook event delivery changed in production on **${changelogDate}**. The sections below list every webhook event that was added, updated, or removed, along with ready-to-paste MDX for each one. |
|
302
|
|
|
303
|
Your job is to: |
|
304
|
1. Load the /writing-docs skill and read \`reference/cloud-style.md\` |
|
305
|
2. Update the webhooks reference page at \`${REFERENCE_PAGE}\` so it documents every event exactly as described below, keeping events grouped by resource |
|
306
|
3. Add the changelog entry to \`${CHANGELOG_PAGE}\` |
|
307
|
4. Keep the \`## Events\` summary table on the reference page in sync: add a row for each new event, remove the row of each removed event, and update descriptions when they changed. Keep its rows in alphabetical order by event name |
|
308
|
|
|
309
|
Webhook docs are dated, not versioned. Never introduce version numbers, and always use the date **${changelogDate}** for this release.` |
|
310
|
} |
|
311
|
|
|
312
|
/** |
|
313
|
* The reference page groups events by the resource they belong to, which is |
|
314
|
* the part of the event name before the dot. For example, `build.created` |
|
315
|
* belongs under the `## Build Events` heading. |
|
316
|
*/ |
|
317
|
private groupTitle(eventName: string): string { |
|
318
|
const resource = eventName.split(".")[0] |
|
319
|
const words = resource |
|
320
|
.split(/[-_]/) |
|
321
|
.filter(Boolean) |
|
322
|
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
|
323
|
|
|
324
|
return `${words.join(" ")} Events` |
|
325
|
} |
|
326
|
|
|
327
|
private buildEventsSection( |
|
328
|
events: WebhookEvent[], |
|
329
|
changelogDate: string |
|
330
|
): string { |
|
331
|
if (!events.length) { |
|
332
|
return `## Webhook Events |
|
333
|
|
|
334
|
No structured event changes were reported in this release. Only apply the prose changes described below, if any.` |
|
335
|
} |
|
336
|
|
|
337
|
// Keep events of the same resource together, alphabetically within each |
|
338
|
// group, so the prompt lists them in the order the page uses |
|
339
|
const groups = new Map<string, WebhookEvent[]>() |
|
340
|
for (const event of events) { |
|
341
|
const title = this.groupTitle(event.event) |
|
342
|
groups.set(title, [...(groups.get(title) ?? []), event]) |
|
343
|
} |
|
344
|
|
|
345
|
const sortedGroups = [...groups.entries()] |
|
346
|
.sort(([a], [b]) => a.localeCompare(b)) |
|
347
|
.map( |
|
348
|
([title, groupEvents]) => |
|
349
|
[ |
|
350
|
title, |
|
351
|
[...groupEvents].sort((a, b) => a.event.localeCompare(b.event)), |
|
352
|
] as const |
|
353
|
) |
|
354
|
|
|
355
|
const blocks: string[] = [] |
|
356
|
for (const [title, groupEvents] of sortedGroups) { |
|
357
|
blocks.push( |
|
358
|
[ |
|
359
|
`### ${title}`, |
|
360
|
``, |
|
361
|
`These events belong under the \`## ${title}\` heading on the reference page.`, |
|
362
|
].join("\n") |
|
363
|
) |
|
364
|
|
|
365
|
for (const event of groupEvents) { |
|
366
|
blocks.push(this.buildEventBlock(event, changelogDate)) |
|
367
|
} |
|
368
|
} |
|
369
|
|
|
370
|
return [ |
|
371
|
`## Webhook Events`, |
|
372
|
``, |
|
373
|
`The reference page groups events by the resource they belong to. Each group is a \`##\` heading, such as \`## ${this.groupTitle(events[0].event)}\`, with a one-sentence intro and one \`###\` subsection per event. Separate groups with \`---\` dividers, but not the events within a group.`, |
|
374
|
``, |
|
375
|
`If a group heading doesn't exist yet, create it and write its intro sentence. Order groups alphabetically by their heading, and order events alphabetically within a group.`, |
|
376
|
``, |
|
377
|
blocks.join("\n\n"), |
|
378
|
].join("\n") |
|
379
|
} |
|
380
|
|
|
381
|
private buildEventBlock(event: WebhookEvent, changelogDate: string): string { |
|
382
|
const lines = [`#### \`${event.event}\` (${event.changeType})`, ``] |
|
383
|
|
|
384
|
if (event.changeType === "removed") { |
|
385
|
lines.push( |
|
386
|
`Remove the \`### ${event.event}\` subsection from \`${REFERENCE_PAGE}\`, along with its row in the \`## Events\` table. If it was the last event of the \`## ${this.groupTitle(event.event)}\` group, remove that heading and its intro too. Do not keep a placeholder section for it. The removal is recorded in the changelog entry instead.` |
|
387
|
) |
|
388
|
|
|
389
|
return lines.join("\n") |
|
390
|
} |
|
391
|
|
|
392
|
lines.push( |
|
393
|
`Use \`### ${event.event}\` as the subsection heading, under the \`## ${this.groupTitle(event.event)}\` heading.`, |
|
394
|
`` |
|
395
|
) |
|
396
|
|
|
397
|
if (event.description) { |
|
398
|
lines.push( |
|
399
|
`**Description to use in the section intro:** ${event.description}`, |
|
400
|
`` |
|
401
|
) |
|
402
|
} |
|
403
|
|
|
404
|
if (event.featureFlag) { |
|
405
|
lines.push( |
|
406
|
`> **Note:** This event is gated by the \`${event.featureFlag}\` feature flag and may not reach all users yet. Document it as normal.`, |
|
407
|
`` |
|
408
|
) |
|
409
|
} |
|
410
|
|
|
411
|
const typeList = this.renderTypeList(event) |
|
412
|
if (typeList) { |
|
413
|
lines.push( |
|
414
|
`**Payload.** Place this under a \`#### Payload\` heading, replacing the existing \`TypeList\` block for this event if there is one. Paste it exactly as-is:`, |
|
415
|
``, |
|
416
|
"```mdx", |
|
417
|
typeList, |
|
418
|
"```", |
|
419
|
`` |
|
420
|
) |
|
421
|
} |
|
422
|
|
|
423
|
const example = this.renderExample(event) |
|
424
|
if (example) { |
|
425
|
lines.push( |
|
426
|
`**Example payload.** Place this under an \`#### Example Payload\` heading, replacing the existing example if there is one. Paste it exactly as-is:`, |
|
427
|
``, |
|
428
|
"````mdx", |
|
429
|
example, |
|
430
|
"````", |
|
431
|
`` |
|
432
|
) |
|
433
|
} |
|
434
|
|
|
435
|
const note = this.renderChangesNote( |
|
436
|
event.propertyChanges ?? [], |
|
437
|
changelogDate |
|
438
|
) |
|
439
|
if (note) { |
|
440
|
lines.push( |
|
441
|
`**Changes note.** Place this directly after the section intro, above the \`#### Payload\` heading. If the section already has a \`Changes\` note, add these bullet points at the top of the existing note and keep the previous ones. Paste it exactly as-is:`, |
|
442
|
``, |
|
443
|
"```mdx", |
|
444
|
note, |
|
445
|
"```", |
|
446
|
`` |
|
447
|
) |
|
448
|
} |
|
449
|
|
|
450
|
return lines.join("\n").trimEnd() |
|
451
|
} |
|
452
|
|
|
453
|
private buildDescriptionsSection(descriptions: string): string { |
|
454
|
return `## Other Webhook Changes |
|
455
|
|
|
456
|
The following describes webhook changes that aren't tied to a single event, such as changes to delivery, headers, signatures, or retries. These descriptions contain no source code, only behavioral descriptions. Update the \`## Delivery Details\` section of \`${REFERENCE_PAGE}\` accordingly. |
|
457
|
|
|
458
|
${descriptions}` |
|
459
|
} |
|
460
|
|
|
461
|
private buildChangelogSection(entry: string, changelogDate: string): string { |
|
462
|
return `## Webhooks Changelog |
|
463
|
|
|
464
|
Record this release in the webhooks changelog page. |
|
465
|
|
|
466
|
**Changelog page:** \`${CHANGELOG_PAGE}\` |
|
467
|
|
|
468
|
Steps: |
|
469
|
1. If \`${CHANGELOG_PAGE}\` does not exist, create it first: a \`metadata\` export with the title \`Webhooks Changelog\`, an \`# {metadata.title}\` heading, and a one-sentence intro explaining that it lists notable changes to Cloud's webhook events, newest first. Then add a sidebar entry for it in \`www/apps/cloud/sidebar.mjs\` under the Webhooks category. |
|
470
|
2. Add the entry below **newest-first**, directly under the intro and above any existing date sections. |
|
471
|
3. If a section for **${changelogDate}** already exists (this release may be re-run), merge the entry into it instead of adding a duplicate section. |
|
472
|
4. Paste the entry exactly as-is. Do not rewrite, summarize, or invent events, properties, or dates. |
|
473
|
|
|
474
|
Changelog entry: |
|
475
|
|
|
476
|
${entry}` |
|
477
|
} |
|
478
|
|
|
479
|
private buildAffectedProjectSection(): string { |
|
480
|
return `## Affected Documentation Project |
|
481
|
|
|
482
|
### cloud |
|
483
|
|
|
484
|
**Project path:** \`www/apps/cloud\` |
|
485
|
|
|
486
|
**Writable files (only modify these):** |
|
487
|
- \`${REFERENCE_PAGE}\` — the webhook events reference |
|
488
|
- \`${CHANGELOG_PAGE}\` — the dated webhooks changelog |
|
489
|
- \`www/apps/cloud/sidebar.mjs\` — only if a page is missing from the sidebar |
|
490
|
|
|
491
|
**Reference page structure:** |
|
492
|
- \`## Webhooks Overview\` — what webhooks are and how Medusa delivers them |
|
493
|
- \`## Delivery Details\` — headers, signature verification, and retries |
|
494
|
- \`## Events\` — a summary table of every event, across all groups, linking to its section |
|
495
|
- One \`## <Resource> Events\` section per resource, such as \`## Build Events\`, each starting with a one-sentence intro |
|
496
|
- Within a group, one \`### <event name>\` subsection per event, each with a \`#### Payload\` (\`TypeList\`) and a \`#### Example Payload\` (JSON code block)` |
|
497
|
} |
|
498
|
|
|
499
|
private buildConstraintsSection(): string { |
|
500
|
return `## Hard Constraints |
|
501
|
|
|
502
|
These are absolute rules. Violating them will cause the workflow to fail: |
|
503
|
|
|
504
|
1. **Only write the files listed above.** Never create new pages under \`www/apps/cloud/app/webhooks/\`, since all events live on the single reference page |
|
505
|
2. **Never modify \`www/apps/cloud/generated/\`** — auto-generated by \`yarn prep\`, which runs automatically after your session |
|
506
|
3. **Do not run \`yarn prep\` or \`yarn lint:content\`** — these run automatically after your session ends |
|
507
|
4. **Paste the provided MDX blocks verbatim.** Never edit a payload schema, example payload, or changelog entry, and never infer properties that aren't listed |
|
508
|
5. **Never use version numbers for webhooks.** Webhook changes are dated |
|
509
|
6. **Write "Medusa" as the subject, never "Cloud"**, for example "Medusa sends this event". Reserve "Cloud" for the platform as a place, such as "changes in Cloud" |
|
510
|
7. **Do not document internal platform details.** Only document what a customer receiving webhooks can observe |
|
511
|
8. **If a described change is too vague to document accurately, skip it** rather than guess` |
|
512
|
} |
|
513
|
} |