|
| 1 | +export const SESSION_INDEX_VERSION = 1; |
| 2 | + |
| 3 | +const TOKEN_FIELDS = ['input', 'output', 'cacheRead', 'cacheWrite', 'total']; |
| 4 | + |
| 5 | +function cleanString(value) { |
| 6 | + return typeof value === 'string' && value.trim() ? value.trim() : null; |
| 7 | +} |
| 8 | + |
| 9 | +function stringList(value) { |
| 10 | + if (!Array.isArray(value)) return []; |
| 11 | + return [...new Set(value.map(cleanString).filter(Boolean))].sort(); |
| 12 | +} |
| 13 | + |
| 14 | +function tokenStats(value = {}) { |
| 15 | + const out = {}; |
| 16 | + for (const field of TOKEN_FIELDS) { |
| 17 | + const n = Number(value[field] ?? 0); |
| 18 | + out[field] = Number.isFinite(n) && n >= 0 ? n : 0; |
| 19 | + } |
| 20 | + if (!out.total) { |
| 21 | + out.total = out.input + out.output + out.cacheRead + out.cacheWrite; |
| 22 | + } |
| 23 | + return out; |
| 24 | +} |
| 25 | + |
| 26 | +function iso(value, fallback = null) { |
| 27 | + const text = cleanString(value); |
| 28 | + if (!text) return fallback; |
| 29 | + const d = new Date(text); |
| 30 | + return Number.isNaN(d.getTime()) ? fallback : d.toISOString(); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Normalize an agent-session summary into the stable ATS handoff schema. |
| 35 | + * UI/indexing tools can own raw transcript search; ATS stores the durable |
| 36 | + * task-linked facts an agent should retrieve across sessions. |
| 37 | + */ |
| 38 | +export function normalizeSessionIndexEntry(entry) { |
| 39 | + if (!entry || typeof entry !== 'object') { |
| 40 | + throw new Error('session index entry must be an object'); |
| 41 | + } |
| 42 | + const id = cleanString(entry.id); |
| 43 | + if (!id) throw new Error('session index entry requires id'); |
| 44 | + const startedAt = iso(entry.startedAt); |
| 45 | + if (!startedAt) throw new Error('session index entry requires valid startedAt'); |
| 46 | + |
| 47 | + const tasks = Array.isArray(entry.tasks) ? entry.tasks : []; |
| 48 | + return { |
| 49 | + version: SESSION_INDEX_VERSION, |
| 50 | + id, |
| 51 | + source: cleanString(entry.source) || 'agent-session', |
| 52 | + title: cleanString(entry.title) || id, |
| 53 | + cwd: cleanString(entry.cwd), |
| 54 | + repo: cleanString(entry.repo), |
| 55 | + branch: cleanString(entry.branch), |
| 56 | + startedAt, |
| 57 | + endedAt: iso(entry.endedAt), |
| 58 | + models: stringList(entry.models), |
| 59 | + tools: stringList(entry.tools), |
| 60 | + files: stringList(entry.files), |
| 61 | + taskRefs: tasks |
| 62 | + .map((task) => ({ |
| 63 | + projectId: cleanString(task?.projectId), |
| 64 | + taskId: cleanString(task?.taskId), |
| 65 | + role: cleanString(task?.role) || 'related', |
| 66 | + })) |
| 67 | + .filter((task) => task.projectId && task.taskId), |
| 68 | + tokenStats: tokenStats(entry.tokenStats), |
| 69 | + outcome: cleanString(entry.outcome), |
| 70 | + summary: cleanString(entry.summary), |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +export function normalizeSessionIndex(entries) { |
| 75 | + if (!Array.isArray(entries)) throw new Error('session index must be an array'); |
| 76 | + return entries.map(normalizeSessionIndexEntry).sort((a, b) => a.startedAt.localeCompare(b.startedAt) || a.id.localeCompare(b.id)); |
| 77 | +} |
| 78 | + |
| 79 | +export function sessionIndexTaskBody(entry) { |
| 80 | + const s = normalizeSessionIndexEntry(entry); |
| 81 | + const lines = [ |
| 82 | + `Agent session: ${s.title}`, |
| 83 | + '', |
| 84 | + `Source: ${s.source}`, |
| 85 | + `Started: ${s.startedAt}`, |
| 86 | + ]; |
| 87 | + if (s.endedAt) lines.push(`Ended: ${s.endedAt}`); |
| 88 | + if (s.repo) lines.push(`Repo: ${s.repo}`); |
| 89 | + if (s.cwd) lines.push(`CWD: ${s.cwd}`); |
| 90 | + if (s.branch) lines.push(`Branch: ${s.branch}`); |
| 91 | + if (s.models.length) lines.push(`Models: ${s.models.join(', ')}`); |
| 92 | + if (s.tools.length) lines.push(`Tools: ${s.tools.join(', ')}`); |
| 93 | + if (s.files.length) lines.push(`Files: ${s.files.join(', ')}`); |
| 94 | + lines.push(`Tokens: ${s.tokenStats.total}`); |
| 95 | + if (s.outcome) lines.push(`Outcome: ${s.outcome}`); |
| 96 | + if (s.summary) lines.push('', s.summary); |
| 97 | + return lines.join('\n'); |
| 98 | +} |
0 commit comments