Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions .github/scripts/deepseek-common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ export async function callDeepSeekJson({

const parsed = parseJsonObject(content);
if (!parsed || typeof parsed !== 'object') {
throw new Error(
const error = new Error(
`DeepSeek returned non-JSON content: ${truncate(content, 1000, 'model output')}`
);
error.modelOutput = content;
throw error;
}

return {
Expand Down Expand Up @@ -391,32 +393,50 @@ export async function callDeepSeekJsonWithRetries(options) {
...requestOptions
} = options
let lastError = null
let previousModelOutput = null

for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
const attemptMaxTokens = attempt === 1 ? maxTokens : Math.max(maxTokens, 16384)
const retryInstructions = [
'AUTOMATION RETRY NOTE:',
'- Return ONLY valid JSON.',
`- The JSON MUST include a non-empty string field named "${fieldName}".`,
'- Do not wrap the JSON in code fences.',
`- Do not return an empty, null, or missing "${fieldName}" field.`,
]
const attemptPrompt =
attempt === 1
? userPrompt
: [
userPrompt,
'',
'AUTOMATION RETRY NOTE:',
'- Your previous response was invalid for automation.',
'- Return ONLY valid JSON.',
`- The JSON MUST include a non-empty string field named "${fieldName}".`,
'- Do not wrap the JSON in code fences.',
`- Do not return an empty, null, or missing "${fieldName}" field.`,
].join('\n')

: previousModelOutput
? [
'Finalize the prior model analysis below into the requested JSON response.',
'Do not repeat the analysis and do not inspect the pull request again.',
...retryInstructions,
'',
'PRIOR MODEL ANALYSIS:',
truncate(previousModelOutput, 60000, 'prior model analysis'),
].join('\n')
: [userPrompt, '', ...retryInstructions].join('\n')

let result = null
try {
const result = await callDeepSeekJson({
result = await callDeepSeekJson({
...requestOptions,
maxTokens: attemptMaxTokens,
userPrompt: attemptPrompt,
})
assertNonEmptyParsedString(result.parsed, fieldName)
return result
} catch (error) {
const modelOutput =
typeof result?.content === 'string'
? result.content
: typeof error?.modelOutput === 'string'
? error.modelOutput
: null
if (modelOutput?.trim()) {
previousModelOutput = modelOutput
}
lastError = error
if (attempt >= maxAttempts || !isRetryableDeepSeekOutputError(error)) {
throw error
Expand Down
9 changes: 7 additions & 2 deletions tests/deepseek-common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ describe('deepseek-common structured response parsing', () => {
{
message: {
content: '',
reasoning_content: 'Analysis was truncated before the final JSON object.',
reasoning_content: 'Analysis was truncated before the final JSON object.\n{}',
role: 'assistant',
},
},
Expand Down Expand Up @@ -302,13 +302,18 @@ describe('deepseek-common structured response parsing', () => {
effort: 'high',
model: 'deepseek-v4-flash',
systemPrompt: 'Review the pull request.',
userPrompt: 'Return JSON.',
userPrompt: 'ORIGINAL LARGE PR PROMPT',
})
).resolves.toMatchObject({
parsed: { body: 'No findings.' },
});

const requestBodies = fetchMock.mock.calls.map(([, init]) => JSON.parse(String(init?.body)));
expect(requestBodies.map((body) => body.max_tokens)).toEqual([8192, 16384]);
expect(requestBodies[1].messages[1].content).toContain('PRIOR MODEL ANALYSIS:');
expect(requestBodies[1].messages[1].content).toContain(
'Analysis was truncated before the final JSON object.'
);
expect(requestBodies[1].messages[1].content).not.toContain('ORIGINAL LARGE PR PROMPT');
});
});
Loading