import { defineAction } from "@agent-native/core/action"; import { z } from "zod"; import { fieldValueInputSchema, parseJsonArg, } from "../server/custom-fields/schema.js"; import { listTaskFieldValues } from "../server/custom-fields/task-fields.js"; import type { FieldValueInput } from "../server/custom-fields/values/store.js"; import { UserInputError } from "../server/errors.js"; import { updateTask, requireUserEmail } from "../server/tasks/store.js"; type FieldValuePatch = { fieldId: string; value: FieldValueInput; }; const fieldValuePatchSchema: z.ZodType = z.object({ fieldId: z.string().describe("Custom field id"), value: fieldValueInputSchema.describe("Custom field value; null clears it"), }); const fieldValuesSchema: z.ZodType = z.preprocess( parseJsonArg, z.array(fieldValuePatchSchema), ); export const updateTaskSchema = z.object({ taskId: z.string().describe("Task id"), title: z.string().min(1).optional().describe("New task title"), done: z.boolean().optional().describe("Completion state"), fieldValues: fieldValuesSchema .optional() .describe("Custom field values to set or clear for this task"), }); export default defineAction({ description: "Update one task's title, completion state, and/or custom fields. Use bulk-update-tasks to apply the same title or completion state to multiple tasks.", schema: updateTaskSchema, run: async (args, ctx) => { const ownerEmail = requireUserEmail(ctx?.userEmail); if ( args.title === undefined && args.done === undefined && args.fieldValues === undefined ) { throw new UserInputError( "Provide at least one of title, done, or fieldValues.", ); } const task = await updateTask({ ownerEmail, id: args.taskId, title: args.title, done: args.done, fieldValues: args.fieldValues, }); if (args.fieldValues !== undefined) { const fields = await listTaskFieldValues({ ownerEmail, taskId: args.taskId, }); return { ...task, fields }; } return task; }, });