Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { readFileSync } from 'node:fs'; |
| 3 | import { isAbsolute, resolve } from 'node:path'; |
| 4 | import { z } from 'zod'; |
| 5 | import type { ContentFreshnessManifest } from './content-freshness'; |
| 6 | |
| 7 | export interface FreshnessPage { |
| 8 | pathname: string; |
| 9 | /** Absolute or docs-root-relative backing source files, never generated outputs. */ |
| 10 | sources: string[]; |
| 11 | lastUpdated?: string; |
| 12 | } |
| 13 | |
| 14 | /** Date-only declarations mean midnight UTC; timestamps must include a timezone. */ |
| 15 | export function normalizeLastUpdated(value: string): string | null { |
| 16 | if (z.iso.date().safeParse(value).success) return `${value}T00:00:00.000Z`; |
| 17 | if (!z.iso.datetime({ offset: true }).safeParse(value).success) return null; |
| 18 | return new Date(value).toISOString(); |
| 19 | } |
| 20 | |
| 21 | /** Only this explicit field is freshness metadata, not release/written/review dates. */ |
| 22 | export function getExplicitLastUpdated(raw: string): string | undefined { |
| 23 | const frontmatter = raw.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/)?.[1]; |
| 24 | const value = frontmatter?.match(/^last_updated:[ \t]*(.*?)[ \t]*$/m)?.[1]; |
| 25 | if (value === undefined) return undefined; |
| 26 | const unquoted = value.replace(/^(?:"(.*)"|'(.*)')$/, '$1$2'); |
| 27 | if (!normalizeLastUpdated(unquoted)) throw new Error(`Invalid last_updated: ${value}`); |
| 28 | return unquoted; |
| 29 | } |
| 30 | |
| 31 | function getSourceHistory(root: string): Map<string, string | null> { |
| 32 | const history = new Map<string, string | null>(); |
| 33 | const git = (...args: string[]) => execFileSync('git', ['-c', 'core.quotepath=false', ...args], { |
| 34 | cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, |
| 35 | stdio: ['ignore', 'pipe', 'pipe'], |
| 36 | }).trim(); |
| 37 | try { |
| 38 | const repository = git('rev-parse', '--show-toplevel'); |
| 39 | const shallowPath = git('rev-parse', '--git-path', 'shallow'); |
| 40 | let boundaries = new Set<string>(); |
| 41 | try { |
| 42 | boundaries = new Set(readFileSync(resolve(root, shallowPath), 'utf8').trim().split('\n')); |
| 43 | } catch { |
| 44 | // A complete repository has no shallow file. |
| 45 | } |
| 46 | const log = git('log', '--format=%x1e%H%x00%cI', '--name-only', '--no-renames', '--', '.'); |
| 47 | for (const record of log.split('\x1e')) { |
| 48 | const [header, ...files] = record.trim().split('\n'); |
| 49 | if (!header) continue; |
| 50 | const [hash, timestamp] = header.split('\0'); |
| 51 | const date = timestamp ? normalizeLastUpdated(timestamp) : null; |
| 52 | for (const file of files) { |
| 53 | if (!file) continue; |
| 54 | const absolutePath = resolve(repository, file); |
| 55 | if (!history.has(absolutePath)) { |
| 56 | // Git presents every file as added at a shallow boundary; that is not evidence. |
| 57 | history.set(absolutePath, hash && boundaries.has(hash) ? null : date); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } catch { |
| 62 | // Source archives and deployments without Git must not invent modification dates. |
| 63 | } |
| 64 | return history; |
| 65 | } |
| 66 | |
| 67 | export function buildContentFreshness( |
| 68 | root: string, |
| 69 | pages: FreshnessPage[], |
| 70 | sourceLastUpdated: Record<string, string> = {}, |
| 71 | ): ContentFreshnessManifest { |
| 72 | const history = getSourceHistory(root); |
| 73 | const manifest: ContentFreshnessManifest = { version: 1, pages: {} }; |
| 74 | for (const page of pages) { |
| 75 | if (page.lastUpdated !== undefined) { |
| 76 | const explicit = normalizeLastUpdated(page.lastUpdated); |
| 77 | if (!explicit) throw new Error(`Invalid last_updated for ${page.pathname}: ${page.lastUpdated}`); |
| 78 | manifest.pages[page.pathname] = explicit; |
| 79 | continue; |
| 80 | } |
| 81 | const dates = [...new Set(page.sources)].map(source => { |
| 82 | const absolutePath = isAbsolute(source) ? source : resolve(root, source); |
| 83 | const explicit = sourceLastUpdated[absolutePath]; |
| 84 | if (explicit !== undefined) { |
| 85 | const timestamp = normalizeLastUpdated(explicit); |
| 86 | if (!timestamp) throw new Error(`Invalid last_updated for ${source}: ${explicit}`); |
| 87 | return timestamp; |
| 88 | } |
| 89 | return history.get(absolutePath) ?? null; |
| 90 | }); |
| 91 | // A partially-known aggregate is not a reliable last-modification timestamp. |
| 92 | if (dates.length === 0 || dates.some(date => date === null)) continue; |
| 93 | const known = dates.filter((date): date is string => date !== null); |
| 94 | manifest.pages[page.pathname] = known.reduce((latest, date) => date > latest ? date : latest); |
| 95 | } |
| 96 | return manifest; |
| 97 | } |