common.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233
  1. import memoize from 'lodash-es/memoize.js'
  2. // This ensures you get the LOCAL date in ISO format
  3. export function getLocalISODate(): string {
  4. // Check for ant-only date override
  5. if (process.env.CLAUDE_CODE_OVERRIDE_DATE) {
  6. return process.env.CLAUDE_CODE_OVERRIDE_DATE
  7. }
  8. const now = new Date()
  9. const year = now.getFullYear()
  10. const month = String(now.getMonth() + 1).padStart(2, '0')
  11. const day = String(now.getDate()).padStart(2, '0')
  12. return `${year}-${month}-${day}`
  13. }
  14. // Memoized for prompt-cache stability — captures the date once at session start.
  15. // The main interactive path gets this behavior via memoize(getUserContext) in
  16. // context.ts; simple mode (--bare) calls getSystemPrompt per-request and needs
  17. // an explicit memoized date to avoid busting the cached prefix at midnight.
  18. // When midnight rolls over, getDateChangeAttachments appends the new date at
  19. // the tail (though simple mode disables attachments, so the trade-off there is:
  20. // stale date after midnight vs. ~entire-conversation cache bust — stale wins).
  21. export const getSessionStartDate = memoize(getLocalISODate)
  22. // Returns "Month YYYY" (e.g. "February 2026") in the user's local timezone.
  23. // Changes monthly, not daily — used in tool prompts to minimize cache busting.
  24. export function getLocalMonthYear(): string {
  25. const date = process.env.CLAUDE_CODE_OVERRIDE_DATE
  26. ? new Date(process.env.CLAUDE_CODE_OVERRIDE_DATE)
  27. : new Date()
  28. return date.toLocaleString('en-US', { month: 'long', year: 'numeric' })
  29. }