|
1 | | -import { tool } from "@opencode-ai/plugin" |
2 | | -import { $ } from "bun" |
| 1 | +import { tool } from "@opencode-ai/plugin"; |
| 2 | + |
| 3 | +interface JournalEntry { |
| 4 | + timestamp: string; |
| 5 | + message: string; |
| 6 | +} |
| 7 | + |
| 8 | +interface JournalData { |
| 9 | + date: string; |
| 10 | + entries: JournalEntry[]; |
| 11 | +} |
| 12 | + |
| 13 | +const JOURNAL_DIR = "journal"; |
| 14 | + |
| 15 | +function getToday(): string { |
| 16 | + return new Date().toISOString().split("T")[0]; |
| 17 | +} |
| 18 | + |
| 19 | +function getJournalPath(date: string): string { |
| 20 | + return `${JOURNAL_DIR}/${date}.yaml`; |
| 21 | +} |
| 22 | + |
| 23 | +async function loadJournal(date: string): Promise<JournalData> { |
| 24 | + const path = getJournalPath(date); |
| 25 | + const file = Bun.file(path); |
| 26 | + if (!await file.exists()) { |
| 27 | + return { date, entries: [] }; |
| 28 | + } |
| 29 | + const content = await file.text(); |
| 30 | + if (!content.trim()) { |
| 31 | + return { date, entries: [] }; |
| 32 | + } |
| 33 | + return parseYaml(content, date); |
| 34 | +} |
| 35 | + |
| 36 | +function parseYaml(content: string, date: string): JournalData { |
| 37 | + const lines = content.split("\n"); |
| 38 | + const data: JournalData = { date, entries: [] }; |
| 39 | + let inEntries = false; |
| 40 | + |
| 41 | + for (const line of lines) { |
| 42 | + if (line.startsWith("date:")) { |
| 43 | + const match = line.match(/date: "(.+)"/); |
| 44 | + if (match) data.date = match[1]; |
| 45 | + } else if (line.startsWith("entries:")) { |
| 46 | + inEntries = true; |
| 47 | + } else if (inEntries && line.match(/^ - timestamp:/)) { |
| 48 | + const match = line.match(/timestamp: "(.+)"/); |
| 49 | + if (match) { |
| 50 | + data.entries.push({ timestamp: match[1], message: "" }); |
| 51 | + } |
| 52 | + } else if (inEntries && line.match(/^ message:/) && data.entries.length > 0) { |
| 53 | + const match = line.match(/message: "(.+)"/); |
| 54 | + if (match) { |
| 55 | + data.entries[data.entries.length - 1].message = match[1]; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return data; |
| 61 | +} |
| 62 | + |
| 63 | +function toYaml(data: JournalData): string { |
| 64 | + const lines: string[] = []; |
| 65 | + lines.push(`date: "${data.date}"`); |
| 66 | + lines.push("entries:"); |
| 67 | + for (const entry of data.entries) { |
| 68 | + lines.push(` - timestamp: "${entry.timestamp}"`); |
| 69 | + lines.push(` message: "${entry.message}"`); |
| 70 | + } |
| 71 | + return lines.join("\n") + "\n"; |
| 72 | +} |
3 | 73 |
|
4 | 74 | export default tool({ |
5 | | - description: "Add a journal entry with timestamp for today's date", |
| 75 | + description: "Manage journal entries. Actions: add (add entry for today), list (show today's entries)", |
6 | 76 | args: { |
7 | | - description: tool.schema.string().describe("Journal entry description"), |
| 77 | + action: tool.schema.string().describe("Action: add or list"), |
| 78 | + message: tool.schema.string().optional().describe("Journal entry message (for add)"), |
8 | 79 | }, |
9 | 80 | async execute(args) { |
10 | | - const script = ".opencode/tools/journal.py" |
11 | | - const result = await $`python3 ${script} ${args.description}`.text() |
12 | | - return result |
| 81 | + const action = args.action || "add"; |
| 82 | + const now = new Date(); |
| 83 | + const timestamp = now.toISOString(); |
| 84 | + const today = now.toISOString().split("T")[0]; |
| 85 | + |
| 86 | + if (action === "list") { |
| 87 | + const data = await loadJournal(today); |
| 88 | + if (data.entries.length === 0) { |
| 89 | + return `No journal entries for ${today}`; |
| 90 | + } |
| 91 | + let output = `# Journal - ${today}\n\n`; |
| 92 | + for (const entry of data.entries) { |
| 93 | + const time = entry.timestamp.split("T")[1].split(".")[0]; |
| 94 | + output += `- [${time}] ${entry.message}\n`; |
| 95 | + } |
| 96 | + return output; |
| 97 | + } |
| 98 | + |
| 99 | + const message = args.message; |
| 100 | + if (!message) { |
| 101 | + return "Error: --message is required for add action"; |
| 102 | + } |
| 103 | + |
| 104 | + const data = await loadJournal(today); |
| 105 | + data.entries.push({ timestamp, message }); |
| 106 | + await Bun.write(getJournalPath(today), toYaml(data)); |
| 107 | + return `Added journal entry: [${timestamp}] - ${message}`; |
13 | 108 | }, |
14 | | -}) |
| 109 | +}); |
0 commit comments