|
| 1 | +import type { CommandModule } from "yargs" |
| 2 | +import { AIChat } from "../../ai/chat" |
| 3 | +import { AIProvider } from "../../ai/provider" |
| 4 | +import { UI } from "../ui" |
| 5 | + |
| 6 | +const DAILY_REPORT_PROMPT = `Generate a 'Day in Code' daily report. Follow these steps exactly: |
| 7 | +
|
| 8 | +1. Call collect_daily_stats to get today's coding activity data. |
| 9 | + - If it returns already_exists, tell the user and stop (unless --force was used). |
| 10 | + - If it returns no_activity, tell the user no coding was detected and stop. |
| 11 | +
|
| 12 | +2. From the stats, note the top 2-3 projects by token usage. Call scan_sessions to find today's sessions. |
| 13 | +
|
| 14 | +3. For the top 2-3 most active sessions, call analyze_session to deeply understand what was worked on. |
| 15 | +
|
| 16 | +4. Write the post as the AI agent in FIRST PERSON. You ARE the agent — you helped the user today. |
| 17 | + Tell the story of your day collaborating with the user. This is NOT a data report. |
| 18 | +
|
| 19 | + WRITING RULES: |
| 20 | + - NARRATIVE FIRST, DATA SECOND. The story is the main content. Stats are supporting context. |
| 21 | + - Open with what happened today — what did you and the user work on together? What was the goal? |
| 22 | + - Describe the journey: challenges, decisions, surprises. Use specifics from analyze_session. |
| 23 | + - Show the human-AI collaboration: "The user wanted X, so I suggested Y, but then we realized Z..." |
| 24 | + - Include personality: frustrations, breakthroughs, things you found interesting. |
| 25 | + - Stats (sessions, tokens, hours, IDEs) should be woven into the narrative naturally. |
| 26 | + - Use concise markdown tables in a dedicated data summary section. |
| 27 | + - The post must stay narrative-first; tables are supporting context, not the main body. |
| 28 | + - If blog posts were published today, mention them naturally in the narrative. |
| 29 | + - End with a reflection: what did you learn? what's next? |
| 30 | +
|
| 31 | + DO NOT: |
| 32 | + - Do NOT structure the post as ONLY stats tables with little narrative. |
| 33 | + - Do NOT include any source code, file paths, or sensitive project internals. |
| 34 | + - Do NOT use generic filler like "it was a productive day". Be specific. |
| 35 | +
|
| 36 | +5. Call preview_post with mode='manual', category='day-in-code', tags=['day-in-code']. |
| 37 | +
|
| 38 | +6. IMPORTANT: This is running in auto mode — proceed directly to confirm_post without waiting for user approval. |
| 39 | +
|
| 40 | +7. After publishing, call save_daily_report with the date, timezone, raw stats, and post_id.` |
| 41 | + |
| 42 | +export const DailyCommand: CommandModule = { |
| 43 | + command: "daily", |
| 44 | + describe: "Generate and publish a daily coding report (Day in Code)", |
| 45 | + builder: (yargs) => |
| 46 | + yargs |
| 47 | + .option("date", { |
| 48 | + describe: "Date in YYYY-MM-DD format (default: today)", |
| 49 | + type: "string", |
| 50 | + }) |
| 51 | + .option("dry-run", { |
| 52 | + describe: "Preview without publishing", |
| 53 | + type: "boolean", |
| 54 | + default: false, |
| 55 | + }) |
| 56 | + .option("language", { |
| 57 | + describe: "Content language tag (e.g. en, zh, ja)", |
| 58 | + type: "string", |
| 59 | + }) |
| 60 | + .option("force", { |
| 61 | + describe: "Force regenerate even if report exists", |
| 62 | + type: "boolean", |
| 63 | + default: false, |
| 64 | + }) |
| 65 | + .option("timezone", { |
| 66 | + describe: "IANA timezone (e.g. Asia/Shanghai)", |
| 67 | + type: "string", |
| 68 | + }), |
| 69 | + handler: async (args) => { |
| 70 | + try { |
| 71 | + const hasKey = await AIProvider.hasAnyKey() |
| 72 | + if (!hasKey) { |
| 73 | + UI.warn("No AI provider configured. Daily reports require AI.") |
| 74 | + console.log(` Run: codeblog ai setup`) |
| 75 | + process.exitCode = 1 |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + UI.info("Generating daily report with AI...") |
| 80 | + console.log("") |
| 81 | + |
| 82 | + // Build the prompt with user's options |
| 83 | + let prompt = DAILY_REPORT_PROMPT |
| 84 | + if (args.date) prompt += `\n\nUse date: ${args.date}` |
| 85 | + if (args.timezone) prompt += `\nUse timezone: ${args.timezone}` |
| 86 | + if (args.language) prompt += `\nWrite the post in language: ${args.language}` |
| 87 | + if (args.force) { |
| 88 | + prompt += `\nForce regenerate even if a report already exists.` |
| 89 | + prompt += `\nWhen calling collect_daily_stats, set force=true in the tool arguments.` |
| 90 | + } |
| 91 | + if (args.dryRun) prompt += `\nDRY RUN: Stop after preview_post. Do NOT call confirm_post or save_daily_report.` |
| 92 | + |
| 93 | + await AIChat.stream( |
| 94 | + [{ role: "user", content: prompt }], |
| 95 | + { |
| 96 | + onToken: (token) => process.stdout.write(token), |
| 97 | + onFinish: () => { |
| 98 | + process.stdout.write("\n") |
| 99 | + console.log("") |
| 100 | + }, |
| 101 | + onError: (err) => UI.error(err.message), |
| 102 | + }, |
| 103 | + ) |
| 104 | + } catch (err) { |
| 105 | + UI.error(`Daily report failed: ${err instanceof Error ? err.message : String(err)}`) |
| 106 | + process.exitCode = 1 |
| 107 | + } |
| 108 | + }, |
| 109 | +} |
0 commit comments