1 import path from "path"
2 import {
3 AnalysisResult,
4 ChangedPackage,
5 DocProjectMapping,
6 } from "../types/index.js"
7 import getMonorepoRoot from "../utils/get-monorepo-root.js"
8
9 export class ContextBuilder {
10 private monorepoRoot: string
11
12 constructor() {
13 this.monorepoRoot = getMonorepoRoot()
14 }
15
16 build(
17 changedPackages: ChangedPackage[],
18 affectedProjects: DocProjectMapping[],
19 commitSha: string
20 ): AnalysisResult {
21 const claudePrompt = this.buildPrompt(
22 changedPackages,
23 affectedProjects,
24 commitSha
25 )
26
27 return {
28 changedPackages,
29 affectedProjects,
30 claudePrompt,
31 triggerCommitSha: commitSha,
32 }
33 }
34
35 private buildPrompt(
36 changedPackages: ChangedPackage[],
37 affectedProjects: DocProjectMapping[],
38 commitSha: string
39 ): string {
40 const sections: string[] = []
41
42 const knownPackages = changedPackages.filter((p) => p.kind !== "other")
43 const otherPackages = changedPackages.filter((p) => p.kind === "other")
44
45 sections.push(this.buildSkillSection())
46 sections.push(this.buildTaskSection(commitSha))
47 sections.push(this.buildChangedPackagesSection(knownPackages))
48 if (otherPackages.length > 0) {
49 sections.push(this.buildOtherPackagesSection(otherPackages))
50 }
51 sections.push(this.buildAffectedProjectsSection(affectedProjects))
52 sections.push(this.buildConstraintsSection())
53
54 return sections.join("\n\n---\n\n")
55 }
56
57 private buildSkillSection(): string {
58 return `## Instructions
59
60 Use the /writing-docs skill before making any documentation changes. This skill contains the rules and patterns for writing Medusa documentation correctly.`
61 }
62
63 private buildTaskSection(commitSha: string): string {
64 return `## Task
65
66 Code changes were merged to the \`develop\` branch (commit: \`${commitSha}\`). Review the diffs below and make the necessary documentation updates across the affected documentation projects.
67
68 Your job is to:
69 1. Load the /writing-docs skill
70 2. Review each changed package diff
71 3. Determine which documentation actually needs updating (not all code changes need docs — the skill's \`when-to-document.md\` reference file explains the rules)
72 4. **When reviewing, also check whether the affected feature is already documented.** If a user-facing feature (CLI option, workflow, service method, config key, admin screen) appears in the diff but is missing from the docs, add it — even if the current diff is only a rename or modification, not a new addition.
73 5. Make targeted edits to the relevant MDX files in the listed writable directories
74 6. If a new page is needed, create it and add it to the appropriate sidebar file`
75 }
76
77 private buildChangedPackagesSection(
78 changedPackages: ChangedPackage[]
79 ): string {
80 const lines: string[] = ["## Changed Packages"]
81
82 for (const pkg of changedPackages) {
83 lines.push(`### ${pkg.name} (${pkg.path})`)
84 lines.push(`**Kind:** ${pkg.kind}`)
85 lines.push(`**Changed files:**`)
86 for (const file of pkg.files) {
87 lines.push(`- \`${file}\``)
88 }
89 lines.push("")
90 lines.push("**Diff:**")
91 lines.push(pkg.diff)
92 }
93
94 return lines.join("\n")
95 }
96
97 private buildOtherPackagesSection(otherPackages: ChangedPackage[]): string {
98 const lines: string[] = [
99 "## Other Changed Packages",
100 "",
101 "The following packages changed but are not automatically mapped to a documentation project. Consider whether these changes require documentation updates — if so, make them in the writable directories listed in the Affected Documentation Projects section.",
102 ]
103
104 for (const pkg of otherPackages) {
105 lines.push(`### ${pkg.name} (${pkg.path})`)
106 lines.push(`**Changed files:**`)
107 for (const file of pkg.files) {
108 lines.push(`- \`${file}\``)
109 }
110 lines.push("")
111 lines.push("**Diff:**")
112 lines.push(pkg.diff)
113 }
114
115 return lines.join("\n")
116 }
117
118 private buildAffectedProjectsSection(
119 affectedProjects: DocProjectMapping[]
120 ): string {
121 const lines: string[] = [
122 "## Affected Documentation Projects",
123 "",
124 "These are the documentation projects that *may* need updates. Use the /writing-docs skill to determine which ones actually need changes based on the diffs above.",
125 ]
126
127 for (const project of affectedProjects) {
128 const relProjectPath = path.relative(
129 this.monorepoRoot,
130 project.projectPath
131 )
132 lines.push(`### ${project.project}`)
133 lines.push(`**Project path:** \`${relProjectPath}\``)
134 lines.push(`**Reason selected:** ${project.reason}`)
135 lines.push(`**Writable directories (only modify files within these):**`)
136 for (const dir of project.writableContentDirs) {
137 const relDir = path.relative(this.monorepoRoot, dir)
138 lines.push(`- \`${relDir}\``)
139 }
140 lines.push("")
141 }
142
143 return lines.join("\n")
144 }
145
146 private buildConstraintsSection(): string {
147 return `## Hard Constraints
148
149 These are absolute rules. Violating them will cause the workflow to fail:
150
151 1. **Never modify \`www/apps/resources/references/\`** — these files are auto-generated by a separate process
152 2. **Never modify \`www/apps/ui/specs/components/\`** — these files are auto-generated by a separate process
153 3. **Never modify \`www/apps/api-reference/\`** — this is managed by a separate process entirely
154 4. **Only write files within the listed writable directories** for each project
155 5. **Do not run \`yarn prep\` or \`yarn lint:content\`** — these will run automatically after your session ends
156 6. **Do not invent Cloudinary screenshot URLs** in user-guide files. Images will be added manually if needed.
157 7. **Only update \`www/apps/user-guide\` if there are actual changes in \`packages/admin/dashboard/src\`** — backend or API changes alone do not imply dashboard UI changes. Do not assume a backend change includes a corresponding admin dashboard change.`
158 }
159 }