| #!/usr/bin/env node
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| 'use strict';
|
| const fs = require('fs');
|
| const path = require('path');
|
| const emit = require('./lib/emit');
|
| const cfg = require('./lib/config');
|
| const sessionLock = require('./lib/session-lock');
|
| const messages = require('./lib/messages');
|
|
|
| const TEAMLOG_HEAD_LINES = 55;
|
| const KANBAN_STATUS_MAX_CHARS = 150;
|
| const KANBAN_ITEMS_MAX = 10;
|
| const KANBAN_RAW_FALLBACK_LINES = 20;
|
|
|
| const MEMORY_DIR_CANDIDATES = ['记忆', 'memory'];
|
| const INBOX_FILENAME_CANDIDATES = ['收件箱.md', 'inbox.md'];
|
|
|
|
|
|
|
|
|
|
|
| const CHARTER_FILENAME_CANDIDATES = ['ARCHITECTURE.md', '团队结构宪章.md'];
|
|
|
|
|
| const IN_PROGRESS_RE = /(进行中|in.?progress)/i;
|
| const STATUS_LINE_RE = /^-\s*\*\*(状态|status)\*\*[::]\s*(.+)$/i;
|
| const UNREAD_SECTION_RE = /(未读|unread)/i;
|
|
|
|
|
|
|
| function safeSplitLines(content) {
|
| return content.split(/\r\n|\r|\n/);
|
| }
|
|
|
| function parseTeamlogHead(content, maxLines) {
|
| return safeSplitLines(content).slice(0, maxLines).join('\n');
|
| }
|
|
|
| |
| |
| |
| |
| |
|
|
| function parseKanbanHot(content, maxChars) {
|
| const lines = safeSplitLines(content);
|
| let inHot = false;
|
| let title = '';
|
| const items = [];
|
| for (const ln of lines) {
|
| if (/^##\s/.test(ln)) {
|
| inHot = IN_PROGRESS_RE.test(ln);
|
| continue;
|
| }
|
| if (!inHot) continue;
|
| const titleMatch = ln.match(/^###\s+(.+)$/);
|
| if (titleMatch) {
|
| title = titleMatch[1].trim();
|
| continue;
|
| }
|
| if (title) {
|
| const stMatch = ln.match(STATUS_LINE_RE);
|
| if (stMatch) {
|
| let st = stMatch[2].trim();
|
| if (st.length > maxChars) st = st.slice(0, maxChars) + '...';
|
| items.push('- ' + title + ' -- ' + st);
|
| title = '';
|
| }
|
| }
|
| }
|
| return { items, total: items.length };
|
| }
|
|
|
| function parseInboxUnreadCount(content) {
|
| const lines = safeSplitLines(content);
|
| let inUnread = false;
|
| let unread = 0;
|
| for (const ln of lines) {
|
| if (/^##\s/.test(ln)) {
|
| inUnread = UNREAD_SECTION_RE.test(ln);
|
| continue;
|
| }
|
| if (inUnread && /^-\s/.test(ln)) unread++;
|
| }
|
| return unread;
|
| }
|
|
|
| function extractCwd(raw) {
|
| try {
|
| if (!raw || !raw.trim()) return '';
|
| const j = JSON.parse(raw);
|
| return typeof j.cwd === 'string' ? j.cwd : '';
|
| } catch (e) {
|
| return '';
|
| }
|
| }
|
|
|
|
|
|
|
| function buildTeamlogSection(teamRoot, config, msg) {
|
| try {
|
| const p = cfg.resolveSharedPath(teamRoot, config, 'team_log');
|
| if (!p || !fs.existsSync(p)) return '';
|
| const content = cfg.readTextFileStripBOM(p);
|
| const head = parseTeamlogHead(content, TEAMLOG_HEAD_LINES);
|
| return msg.teamlogHeader(TEAMLOG_HEAD_LINES) + head + msg.teamlogFooter;
|
| } catch (e) {
|
| return '';
|
| }
|
| }
|
|
|
| function buildKanbanSection(teamRoot, config, msg) {
|
| try {
|
| const p = cfg.resolveSharedPath(teamRoot, config, 'kanban');
|
| if (!p || !fs.existsSync(p)) return '';
|
| const content = cfg.readTextFileStripBOM(p);
|
| const { items, total } = parseKanbanHot(content, KANBAN_STATUS_MAX_CHARS);
|
| if (total > 0) {
|
| const shown = items.slice(0, KANBAN_ITEMS_MAX);
|
| let section = msg.kanbanHeaderStructured(total) + shown.join('\n');
|
| if (total > KANBAN_ITEMS_MAX) section += msg.kanbanTruncatedNote(KANBAN_ITEMS_MAX);
|
| section += msg.kanbanFooter;
|
| return section;
|
| }
|
|
|
|
|
| const rawHead = parseTeamlogHead(content, KANBAN_RAW_FALLBACK_LINES).trim();
|
| if (!rawHead) return '';
|
| return msg.kanbanHeaderRaw(KANBAN_RAW_FALLBACK_LINES) + rawHead + '\n';
|
| } catch (e) {
|
| return '';
|
| }
|
| }
|
|
|
| function buildCharterSection(teamRoot, msg) {
|
| try {
|
| if (!teamRoot) return '';
|
| const charterPath = cfg.firstExisting(teamRoot, CHARTER_FILENAME_CANDIDATES);
|
| if (!charterPath) return '';
|
| return msg.charterPointer(path.basename(charterPath));
|
| } catch (e) {
|
| return '';
|
| }
|
| }
|
|
|
| function buildInboxSection(cwd, msg) {
|
| try {
|
| if (!cwd) return '';
|
| const agentHome = cfg.resolveAgentHome(cwd);
|
| if (!agentHome) return '';
|
| const memDir = cfg.firstExisting(agentHome, MEMORY_DIR_CANDIDATES);
|
| if (!memDir) return '';
|
| const inboxPath = cfg.firstExisting(memDir, INBOX_FILENAME_CANDIDATES);
|
| if (!inboxPath) return '';
|
| const content = cfg.readTextFileStripBOM(inboxPath);
|
| const unread = parseInboxUnreadCount(content);
|
| if (unread > 0) return msg.inboxUnread(unread);
|
| return '';
|
| } catch (e) {
|
| return '';
|
| }
|
| }
|
|
|
| function buildSessionLockSection(cwd, platform, msg) {
|
| try {
|
| if (!cwd) return '';
|
| const agentHome = cfg.resolveAgentHome(cwd);
|
| if (!agentHome) return '';
|
| const result = sessionLock.acquire(agentHome, platform || 'unknown');
|
| if (result && result.warned && result.priorHolder) {
|
| return msg.sessionLockWarning(result.priorHolder.harness, result.priorHolder.pid, result.priorHolder.acquiredAt);
|
| }
|
| return '';
|
| } catch (e) {
|
| return '';
|
| }
|
| }
|
|
|
| async function main() {
|
| const platform = emit.getPlatform();
|
| try {
|
| const raw = await emit.readStdin();
|
| const cwd = emit.resolveCwd(raw, platform);
|
|
|
| const sid = emit.extractSessionId(raw);
|
| if (sid && emit.isDuplicateInvocation('SessionStart', sid)) {
|
| process.exit(0);
|
| return;
|
| }
|
|
|
| const teamRoot = cfg.resolveTeamRoot(cwd || process.cwd());
|
| const config = cfg.loadTeamConfig(teamRoot);
|
| const lang = cfg.resolveLanguage(config);
|
| const msg = messages.get(lang);
|
|
|
| let ctx = msg.openingCheck + '\n';
|
| ctx += buildCharterSection(teamRoot, msg);
|
| ctx += buildTeamlogSection(teamRoot, config, msg);
|
| ctx += buildKanbanSection(teamRoot, config, msg);
|
| ctx += buildInboxSection(cwd, msg);
|
| ctx += buildSessionLockSection(cwd, platform, msg);
|
|
|
| emit.writeJson(emit.buildSessionStartPayload(ctx, platform));
|
| } catch (e) {
|
|
|
|
|
|
|
|
|
|
|
|
|
| process.stderr.write('[session-start] non-fatal error, no context injected (session not blocked): ' + ((e && e.stack) || e) + '\n');
|
| }
|
| process.exit(0);
|
| }
|
|
|
| if (require.main === module) {
|
| main();
|
| }
|
|
|
| module.exports = {
|
| buildTeamlogSection,
|
| buildKanbanSection,
|
| buildInboxSection,
|
| buildSessionLockSection,
|
| buildCharterSection,
|
| extractCwd,
|
| parseTeamlogHead,
|
| parseKanbanHot,
|
| parseInboxUnreadCount,
|
| MEMORY_DIR_CANDIDATES,
|
| INBOX_FILENAME_CANDIDATES,
|
| CHARTER_FILENAME_CANDIDATES,
|
| };
|
|
|