history-message.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import type { CronSendMessageRequest } from '@mavis/cron';
  2. export interface CronHistoryMessage {
  3. content: string;
  4. source?: string;
  5. origin?: unknown;
  6. }
  7. /** Convert a cron trigger into the display-message metadata stored with its session turn. */
  8. export function buildCronHistoryMessage(
  9. msg: Pick<CronSendMessageRequest, 'content' | 'source' | 'inboundContext'>,
  10. ): CronHistoryMessage {
  11. const rawMeta = readCronRawMeta(msg.inboundContext);
  12. return {
  13. content: msg.content,
  14. ...(msg.source ? { source: msg.source } : {}),
  15. ...(rawMeta ? { origin: { rawMeta } } : {}),
  16. };
  17. }
  18. function readCronRawMeta(inboundContext: unknown): Record<string, unknown> | undefined {
  19. if (!inboundContext || typeof inboundContext !== 'object') return undefined;
  20. const ctx = inboundContext as { cronName?: unknown; cronSchedule?: unknown };
  21. const rawMeta: Record<string, unknown> = {};
  22. if (typeof ctx.cronName === 'string' && ctx.cronName.length > 0) {
  23. rawMeta.cronName = ctx.cronName;
  24. }
  25. if (typeof ctx.cronSchedule === 'string' && ctx.cronSchedule.length > 0) {
  26. rawMeta.cronSchedule = ctx.cronSchedule;
  27. }
  28. return Object.keys(rawMeta).length > 0 ? rawMeta : undefined;
  29. }