import { z } from 'zod'; import { messageSchema } from './message'; import { sessionStatusResponseSchema, sessionWarningSchema, sessionWarningsResponseSchema, updateSessionProfileRequestSchema, type UpdateSessionProfileRequest, } from '@moonshot-ai/agent-core-v2/app/sessionLegacy/sessionProtocol'; import { goalSnapshotSchema } from './goal'; import { cursorQuerySchema, pageResponseSchema } from './pagination'; import { sessionChildCreateSchema, sessionCreateSchema, sessionForkSchema, sessionSchema, } from './session'; export { sessionStatusResponseSchema, sessionWarningSchema, sessionWarningsResponseSchema, updateSessionProfileRequestSchema, }; export type { SessionStatusResponse, SessionWarning, SessionWarningsResponse, UpdateSessionProfileRequest, } from '@moonshot-ai/agent-core-v2/app/sessionLegacy/sessionProtocol'; export const createSessionRequestSchema = sessionCreateSchema; export type CreateSessionRequest = z.infer; export const createSessionResponseSchema = sessionSchema; export type CreateSessionResponse = z.infer; const booleanQueryParam = z.preprocess( (value) => { if (value === 'true' || value === '1' || value === 1 || value === true) return true; if (value === 'false' || value === '0' || value === 0 || value === false) return false; return value; }, z.boolean().optional(), ); export const listSessionsQuerySchema = cursorQuerySchema.and( z.object({ busy: booleanQueryParam, include_archive: booleanQueryParam, archived_only: booleanQueryParam, exclude_empty: booleanQueryParam, }), ); export type ListSessionsQuery = z.infer; export const getSessionResponseSchema = sessionSchema; export type GetSessionResponse = z.infer; export const getSessionProfileResponseSchema = sessionSchema; export type GetSessionProfileResponse = z.infer; export const MAX_SESSION_EXPORT_WEB_LOG_BYTES = 256 * 1024; export const exportSessionParamsSchema = z.object({ session_id: z.string().min(1), }); export type ExportSessionParams = z.infer; export const exportSessionRequestSchema = z .object({ web_log: z .string() .refine((value) => fitsUtf8ByteLimit(value, MAX_SESSION_EXPORT_WEB_LOG_BYTES), { message: `web_log must not exceed ${MAX_SESSION_EXPORT_WEB_LOG_BYTES} UTF-8 bytes`, }) .optional(), desktop: z.boolean().optional(), }) .strict(); export type ExportSessionRequest = z.infer; export const updateSessionProfileResponseSchema = sessionSchema; export type UpdateSessionProfileResponse = z.infer; export const updateSessionMetaRequestSchema = updateSessionProfileRequestSchema; export type UpdateSessionMetaRequest = UpdateSessionProfileRequest; export const updateSessionMetaResponseSchema = updateSessionProfileResponseSchema; export type UpdateSessionMetaResponse = UpdateSessionProfileResponse; export const updateSessionRequestSchema = updateSessionProfileRequestSchema; export type UpdateSessionRequest = z.infer; export const updateSessionResponseSchema = sessionSchema; export type UpdateSessionResponse = z.infer; export const forkSessionRequestSchema = sessionForkSchema; export type ForkSessionRequest = z.infer; export const forkSessionResponseSchema = sessionSchema; export type ForkSessionResponse = z.infer; export const startBtwSessionResponseSchema = z.object({ agent_id: z.string().min(1), }); export type StartBtwSessionResponse = z.infer; export const listSessionChildrenQuerySchema = cursorQuerySchema.and( z.object({ busy: booleanQueryParam, include_archive: booleanQueryParam, }), ); export type ListSessionChildrenQuery = z.infer; export const listSessionChildrenResponseSchema = pageResponseSchema(sessionSchema); export type ListSessionChildrenResponse = z.infer; export const createSessionChildRequestSchema = sessionChildCreateSchema; export type CreateSessionChildRequest = z.infer; export const createSessionChildResponseSchema = sessionSchema; export type CreateSessionChildResponse = z.infer; export const getSessionGoalResponseSchema = goalSnapshotSchema.nullable(); export type GetSessionGoalResponse = z.infer; export const compactSessionRequestSchema = z.preprocess( (value) => value === undefined ? {} : value, z.object({ instruction: z.string().optional(), }), ); export type CompactSessionRequest = z.infer; export const compactSessionResponseSchema = z.object({}); export type CompactSessionResponse = z.infer; export const undoSessionRequestSchema = z.preprocess( (value) => value === undefined ? {} : value, z.object({ count: z.number().int().positive().default(1), page_size: z.number().int().min(1).max(100).optional(), }), ); export type UndoSessionRequest = z.infer; export const undoSessionResponseSchema = z.object({ messages: pageResponseSchema(messageSchema), status: sessionStatusResponseSchema, }); export type UndoSessionResponse = z.infer; export const archiveSessionResponseSchema = z.object({ archived: z.literal(true), }); export type ArchiveSessionResponse = z.infer; export const restoreSessionResponseSchema = sessionSchema; export type RestoreSessionResponse = z.infer; export const deleteSessionResponseSchema = z.object({ deleted: z.literal(true), }); export type DeleteSessionResponse = z.infer; export const sessionAbortResponseSchema = z.object({ aborted: z.boolean(), }); export type SessionAbortResponse = z.infer; function fitsUtf8ByteLimit(value: string, limit: number): boolean { let bytes = 0; for (let index = 0; index < value.length; index += 1) { const codePoint = value.codePointAt(index)!; if (codePoint < 0x80) { bytes += 1; } else if (codePoint < 0x800) { bytes += 2; } else if (codePoint > 0xffff) { bytes += 4; index += 1; } else { bytes += 3; } if (bytes > limit) return false; } return true; }