kimi-code/packages/protocol/src/time.ts
Haozhe 4603d8ad6e
feat(protocol): extract shared protocol package from agent-core (#612)
* feat(protocol): extract shared protocol package from agent-core

- add `@moonshot-ai/protocol` package with REST/WS schemas, envelopes, error codes, event types, and display schemas\n- migrate agent-core `events.ts` and `display/schemas.ts` to re-export from protocol
- add centralized `onUnexpectedError` handler for safe emitter listener callbacks
- reject forkSession when source session has an active running turn
- add protocol schema tests and unexpectedError handler tests
2026-06-10 14:03:38 +08:00

29 lines
724 B
TypeScript

import { z } from 'zod';
const ISO_8601_REGEX =
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}(?::?\d{2})?)$/;
export const isoDateTimeSchema = z
.string()
.refine((value) => ISO_8601_REGEX.test(value), {
message: 'must be an ISO 8601 datetime string',
})
.transform((value, ctx) => {
const ms = Date.parse(value);
if (Number.isNaN(ms)) {
ctx.addIssue({
code: 'custom',
message: 'invalid ISO 8601 datetime',
});
return z.NEVER;
}
return new Date(ms).toISOString();
});
export const IsoDateTime = isoDateTimeSchema;
export type IsoDateTime = string;
export function nowIsoDateTime(): IsoDateTime {
return new Date().toISOString();
}