/** * TaskLog Stream Definition * * Manages log entries for each task execution. * Uses sessionId as groupId for per-task log isolation. * Retains recent N entries for state recovery. * * @see Requirements 4.2, 4.6 */ import { defineStream } from '@motia/core' import { z } from 'zod' import type { StepId } from './taskState' // Log level enum export const LogLevelSchema = z.enum(['INFO', 'WARN', 'ERROR']) export type LogLevel = z.infer // Task log entry schema export const TaskLogEntrySchema = z.object({ sessionId: z.string(), stepId: z.string(), // StepId type ts: z.string(), // ISO timestamp level: LogLevelSchema, message: z.string(), seq: z.number(), // Log sequence number for ordering }) export type TaskLogEntry = z.infer // Maximum number of log entries to retain per task export const MAX_LOG_ENTRIES = 1000 /** * Create a new log entry */ export function createLogEntry( sessionId: string, stepId: StepId, level: LogLevel, message: string, seq: number ): TaskLogEntry { return { sessionId, stepId, ts: new Date().toISOString(), level, message, seq, } } // Define the taskLog stream export const taskLogStream = defineStream({ name: 'taskLog', schema: TaskLogEntrySchema, persistence: { enabled: true, // Retain recent entries for state recovery maxEntries: MAX_LOG_ENTRIES, }, })