Skip to content

Commit 324e292

Browse files
committed
fix(translation): resolve workflow stale-scripts, schema, and undefined-content bugs
Three bugs found in the translate-docs workflow and translation scripts: 1. Workflow always ran scripts from the `content` branch, which had an older version of translateFrontMatter.ts with `type: "string"` in the OpenAI response schema instead of `type: "object"`, causing 400 errors. Fix: overlay scripts/ and package.json from main before bun i, then immediately unstage them (git restore --staged) so they are not accidentally committed to the content branch. 2. translateCodeJson.ts used CodeJsonSchema with additionalProperties as a schema object combined with strict: true, which OpenAI Structured Outputs forbids (additionalProperties must be false in strict mode). Fix: switch to response_format { type: "json_object" } — appropriate since code.json has dynamic/unknown keys. 3. removeFrontMatter and createNotionPageFromMarkdown had no guard against receiving undefined content (possible when an older caller caught a translation error but continued with undefined translatedContent). Fix: type guard in removeFrontMatter returns "" for non-strings; createNotionPageFromMarkdown throws with a clear diagnostic message.
1 parent a2313b4 commit 324e292

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

.github/workflows/translate-docs.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ jobs:
2929
with:
3030
ref: ${{ env.TARGET_BRANCH }}
3131

32+
- name: Apply latest scripts from main
33+
run: |
34+
git fetch origin main --depth=1
35+
git checkout origin/main -- scripts/ package.json
36+
git restore --staged scripts/ package.json
37+
3238
- name: Setup Bun
3339
uses: oven-sh/setup-bun@735343b667d3e6f658f44d0eca948eb6282f2b76 # v2.0.2
3440

scripts/notion-translate/markdownToNotion.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ type NotionCodeLanguage =
488488
* @returns The markdown content without front-matter
489489
*/
490490
export function removeFrontMatter(content: string): string {
491+
if (typeof content !== "string") return "";
491492
// Check if content starts with front-matter (---)
492493
const frontMatterRegex = /^---\n[\s\S]*?\n---\n/m;
493494
return content.replace(frontMatterRegex, "");
@@ -562,6 +563,12 @@ export async function createNotionPageFromMarkdown(
562563
? markdownPath
563564
: await fs.readFile(markdownPath, "utf8");
564565

566+
if (typeof markdownContent !== "string") {
567+
throw new Error(
568+
`Invalid content for page "${title}": expected string, got ${typeof markdownContent}`
569+
);
570+
}
571+
565572
// Remove front-matter if present
566573
markdownContent = removeFrontMatter(markdownContent);
567574

scripts/notion-translate/translateCodeJson.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,6 @@ export async function translateJson(
6666
targetLanguage
6767
);
6868

69-
// JSON schema for code.json translation
70-
// Requires both "message" and "description" fields (standard Docusaurus i18n format)
71-
// Structure:
72-
// {
73-
// "Some Key": {
74-
// "message": "Some message",
75-
// "description": "Some description"
76-
// },
77-
// ...
78-
// }
79-
const CodeJsonSchema = {
80-
type: "object",
81-
properties: {},
82-
additionalProperties: {
83-
type: "object",
84-
properties: {
85-
message: { type: "string" },
86-
description: { type: "string" },
87-
},
88-
required: ["message", "description"],
89-
additionalProperties: false,
90-
},
91-
};
92-
9369
// Get model-specific parameters (handles GPT-5 temperature constraints)
9470
// For GPT-5.2, use reasoning_effort="none" to allow custom temperature
9571
const modelParams = getModelParams(model, { useReasoningNone: true });
@@ -101,14 +77,9 @@ export async function translateJson(
10177
{ role: "system", content: prompt },
10278
{ role: "user", content: jsonContent },
10379
],
104-
response_format: {
105-
type: "json_schema",
106-
json_schema: {
107-
name: "translation",
108-
schema: CodeJsonSchema,
109-
strict: true,
110-
},
111-
},
80+
// Use json_object format: code.json has dynamic keys so a strict schema
81+
// cannot be defined (OpenAI strict mode forbids additionalProperties schemas).
82+
response_format: { type: "json_object" },
11283
...modelParams,
11384
});
11485

0 commit comments

Comments
 (0)