Skip to content

Commit 92490c2

Browse files
authored
Merge pull request #36 from MaxLinCode/codex/flatten-planner-response-schema
Flatten planner response schema for Responses API
2 parents 93a3d06 + 691fbf6 commit 92490c2

2 files changed

Lines changed: 82 additions & 14 deletions

File tree

packages/core/src/index.ts

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,12 @@ export const createScheduleBlockPlanningActionSchema = z.object({
497497

498498
export const createScheduleBlockPlanningActionResponseFormatSchema = z.object({
499499
type: z.literal("create_schedule_block"),
500-
taskRef: taskReferenceSchema,
501-
scheduleConstraint: scheduleConstraintResponseFormatSchema.nullable(),
502-
reason: z.string().min(1)
500+
taskRef: z.object({
501+
kind: z.enum(["created_task", "existing_task"]),
502+
alias: z.string().min(1)
503+
}).nullable().optional(),
504+
scheduleConstraint: scheduleConstraintResponseFormatSchema.nullable().optional(),
505+
reason: z.string().min(1).nullable().optional()
503506
});
504507

505508
export const moveScheduleBlockPlanningActionSchema = z.object({
@@ -511,9 +514,11 @@ export const moveScheduleBlockPlanningActionSchema = z.object({
511514

512515
export const moveScheduleBlockPlanningActionResponseFormatSchema = z.object({
513516
type: z.literal("move_schedule_block"),
514-
blockRef: scheduleBlockReferenceSchema,
515-
scheduleConstraint: scheduleConstraintResponseFormatSchema.nullable(),
516-
reason: z.string().min(1)
517+
blockRef: z.object({
518+
alias: z.string().min(1)
519+
}).nullable().optional(),
520+
scheduleConstraint: scheduleConstraintResponseFormatSchema.nullable().optional(),
521+
reason: z.string().min(1).nullable().optional()
517522
});
518523

519524
export const completeTaskPlanningActionSchema = z.object({
@@ -535,13 +540,28 @@ export const planningActionSchema = z.discriminatedUnion("type", [
535540
clarifyPlanningActionSchema
536541
]);
537542

538-
export const planningActionResponseFormatSchema = z.discriminatedUnion("type", [
539-
createTaskPlanningActionSchema,
540-
createScheduleBlockPlanningActionResponseFormatSchema,
541-
moveScheduleBlockPlanningActionResponseFormatSchema,
542-
completeTaskPlanningActionSchema,
543-
clarifyPlanningActionSchema
544-
]);
543+
export const planningActionResponseFormatSchema = z.object({
544+
type: z.enum([
545+
"create_task",
546+
"create_schedule_block",
547+
"move_schedule_block",
548+
"complete_task",
549+
"clarify"
550+
]),
551+
alias: z.string().min(1).nullable().optional(),
552+
title: z.string().min(1).nullable().optional(),
553+
priority: z.enum(["low", "medium", "high"]).nullable().optional(),
554+
urgency: z.enum(["low", "medium", "high"]).nullable().optional(),
555+
taskRef: z.object({
556+
kind: z.enum(["created_task", "existing_task"]),
557+
alias: z.string().min(1)
558+
}).nullable().optional(),
559+
blockRef: z.object({
560+
alias: z.string().min(1)
561+
}).nullable().optional(),
562+
scheduleConstraint: scheduleConstraintResponseFormatSchema.nullable().optional(),
563+
reason: z.string().min(1).nullable().optional()
564+
});
545565

546566
export const inboxPlanningOutputSchema = z.object({
547567
confidence: z.number().min(0).max(1),

packages/integrations/src/openai.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ export async function planInboxItemWithResponses(
100100
}
101101
});
102102

103-
return inboxPlanningOutputSchema.parse(inboxPlanningResponseFormatSchema.parse(response.output_parsed));
103+
return inboxPlanningOutputSchema.parse(
104+
normalizePlanningOutput(inboxPlanningResponseFormatSchema.parse(response.output_parsed))
105+
);
104106
}
105107

106108
export async function routeTurnWithResponses(
@@ -275,3 +277,49 @@ function normalizeConfirmedMutationRecoveryOutput(
275277

276278
return output;
277279
}
280+
281+
function normalizePlanningOutput(
282+
output: z.infer<typeof inboxPlanningResponseFormatSchema>
283+
) {
284+
return {
285+
confidence: output.confidence,
286+
summary: output.summary,
287+
actions: output.actions.map((action) => {
288+
switch (action.type) {
289+
case "create_task":
290+
return {
291+
type: action.type,
292+
alias: action.alias,
293+
title: action.title,
294+
priority: action.priority,
295+
urgency: action.urgency
296+
};
297+
case "create_schedule_block":
298+
return {
299+
type: action.type,
300+
taskRef: action.taskRef,
301+
scheduleConstraint: action.scheduleConstraint ?? null,
302+
reason: action.reason
303+
};
304+
case "move_schedule_block":
305+
return {
306+
type: action.type,
307+
blockRef: action.blockRef,
308+
scheduleConstraint: action.scheduleConstraint ?? null,
309+
reason: action.reason
310+
};
311+
case "complete_task":
312+
return {
313+
type: action.type,
314+
taskRef: action.taskRef,
315+
reason: action.reason
316+
};
317+
case "clarify":
318+
return {
319+
type: action.type,
320+
reason: action.reason
321+
};
322+
}
323+
})
324+
};
325+
}

0 commit comments

Comments
 (0)