@marlin-notes/scheduler-cli 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +20 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +22 -6
- package/src/summarizer.ts +21 -2
package/dist/index.js
CHANGED
|
@@ -70,7 +70,8 @@ async function summarize({
|
|
|
70
70
|
frequency,
|
|
71
71
|
tags,
|
|
72
72
|
apiUrl,
|
|
73
|
-
userId
|
|
73
|
+
userId,
|
|
74
|
+
baseDir
|
|
74
75
|
}) {
|
|
75
76
|
const now = /* @__PURE__ */ new Date();
|
|
76
77
|
let startDate;
|
|
@@ -84,8 +85,14 @@ async function summarize({
|
|
|
84
85
|
console.log(`[Scheduler] Running '${taskName}' (${frequency})`);
|
|
85
86
|
console.log(`[Scheduler] Filtering from: ${startDate.toISOString()}`);
|
|
86
87
|
console.log(`[Scheduler] Tags: ${tags.join(", ") || "(none)"}`);
|
|
87
|
-
const notesDir = process.cwd();
|
|
88
|
-
|
|
88
|
+
const notesDir = baseDir ? import_path.default.resolve(process.cwd(), baseDir) : process.cwd();
|
|
89
|
+
console.log(`[Scheduler] Searching in: ${notesDir}`);
|
|
90
|
+
if (!import_fs.default.existsSync(notesDir)) {
|
|
91
|
+
throw new Error(`Directory not found: ${notesDir}`);
|
|
92
|
+
}
|
|
93
|
+
const files = import_fs.default.readdirSync(notesDir, { recursive: true }).map((f) => String(f)).filter(
|
|
94
|
+
(f) => f.endsWith(".md") && !f.includes("node_modules") && !f.includes(".git")
|
|
95
|
+
);
|
|
89
96
|
const matchedNotes = [];
|
|
90
97
|
for (const file of files) {
|
|
91
98
|
const filePath = import_path.default.join(notesDir, file);
|
|
@@ -181,15 +188,21 @@ ${result.summary}
|
|
|
181
188
|
// src/index.ts
|
|
182
189
|
var program = new import_commander.Command();
|
|
183
190
|
program.name("marlin-scheduler").description("CLI for Marlin scheduled tasks").version("0.0.1");
|
|
184
|
-
program.command("summarize").description("Run summarization task").option("--task-name <name>", "Name of the task").option("--frequency <freq>", "Frequency (weekly/monthly)").option("--tags <json>", "JSON string of tags").option("--api-url <url>", "Marlin API URL").option("--user-id <id>", "GitHub User ID").
|
|
191
|
+
program.command("summarize").description("Run summarization task").option("--task-name <name>", "Name of the task").option("--frequency <freq>", "Frequency (weekly/monthly)").option("--tags <json>", "JSON string of tags").option("--api-url <url>", "Marlin API URL").option("--user-id <id>", "GitHub User ID").option(
|
|
192
|
+
"--dir <path>",
|
|
193
|
+
"Directory to search for notes (default: current dir)"
|
|
194
|
+
).action(async (options) => {
|
|
185
195
|
try {
|
|
186
196
|
const taskName = options.taskName || process.env.INPUT_TASK_NAME || "Manual Task";
|
|
187
197
|
const frequency = options.frequency || process.env.INPUT_FREQUENCY || "weekly";
|
|
188
198
|
const tagsJson = options.tags || process.env.INPUT_TAGS || "[]";
|
|
189
199
|
const apiUrl = options.apiUrl || process.env.INPUT_API_URL || "https://marlinnotes.com/api/ai/summarize";
|
|
190
200
|
const userId = options.userId || process.env.INPUT_USER_ID || process.env.GITHUB_ACTOR_ID;
|
|
201
|
+
const baseDir = options.dir || process.env.INPUT_DIR;
|
|
191
202
|
if (!userId) {
|
|
192
|
-
console.error(
|
|
203
|
+
console.error(
|
|
204
|
+
"Error: User ID is required. Pass via --user-id, set INPUT_USER_ID, or run in GitHub Actions (GITHUB_ACTOR_ID)."
|
|
205
|
+
);
|
|
193
206
|
process.exit(1);
|
|
194
207
|
}
|
|
195
208
|
let tags = [];
|
|
@@ -203,7 +216,8 @@ program.command("summarize").description("Run summarization task").option("--tas
|
|
|
203
216
|
frequency,
|
|
204
217
|
tags,
|
|
205
218
|
apiUrl,
|
|
206
|
-
userId
|
|
219
|
+
userId,
|
|
220
|
+
baseDir
|
|
207
221
|
});
|
|
208
222
|
} catch (error) {
|
|
209
223
|
console.error("Failed:", error);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/summarizer.ts","../src/oidc.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport { summarize } from \"./summarizer\";\n\nconst program = new Command();\n\nprogram\n .name(\"marlin-scheduler\")\n .description(\"CLI for Marlin scheduled tasks\")\n .version(\"0.0.1\");\n\nprogram\n .command(\"summarize\")\n .description(\"Run summarization task\")\n .option(\"--task-name <name>\", \"Name of the task\")\n .option(\"--frequency <freq>\", \"Frequency (weekly/monthly)\")\n .option(\"--tags <json>\", \"JSON string of tags\")\n .option(\"--api-url <url>\", \"Marlin API URL\")\n .option(\"--user-id <id>\", \"GitHub User ID\")\n .action(async (options) => {\n try {\n // Prioritize flags, then Env Vars (standard Action inputs often use INPUT_ prefix)\n const taskName = options.taskName || process.env.INPUT_TASK_NAME || \"Manual Task\";\n const frequency = options.frequency || process.env.INPUT_FREQUENCY || \"weekly\";\n const tagsJson = options.tags || process.env.INPUT_TAGS || \"[]\";\n // Default to production API if not specified\n const apiUrl = options.apiUrl || process.env.INPUT_API_URL || \"https://marlinnotes.com/api/ai/summarize\";\n \n // Resolve User ID: Flag -> Input -> GitHub Actor ID\n const userId = options.userId || process.env.INPUT_USER_ID || process.env.GITHUB_ACTOR_ID;\n\n if (!userId) {\n console.error(\"Error: User ID is required. Pass via --user-id, set INPUT_USER_ID, or run in GitHub Actions (GITHUB_ACTOR_ID).\");\n process.exit(1);\n }\n\n let tags: string[] = [];\n try {\n tags = JSON.parse(tagsJson);\n } catch (e) {\n console.warn(\"Failed to parse tags JSON, assuming empty array:\", e);\n }\n\n await summarize({\n taskName,\n frequency: frequency as \"weekly\" | \"monthly\",\n tags,\n apiUrl,\n userId,\n });\n } catch (error) {\n console.error(\"Failed:\", error);\n process.exit(1);\n }\n });\n\nprogram.parse();\n","import fs from \"fs\";\nimport path from \"path\";\nimport matter from \"gray-matter\";\nimport { subDays, subMonths, isBefore, isAfter } from \"date-fns\";\nimport { getGithubOidcToken } from \"./oidc\";\n\ninterface SummarizeOptions {\n taskName: string;\n frequency: \"weekly\" | \"monthly\";\n tags: string[];\n apiUrl: string;\n userId: string;\n}\n\nexport async function summarize({\n taskName,\n frequency,\n tags,\n apiUrl,\n userId,\n}: SummarizeOptions) {\n // ... (existing date logic unchanged) ...\n const now = new Date();\n let startDate: Date;\n\n if (frequency === \"weekly\") {\n startDate = subDays(now, 7);\n } else if (frequency === \"monthly\") {\n startDate = subMonths(now, 1);\n } else {\n throw new Error(`Unknown frequency: ${frequency}`);\n }\n\n console.log(`[Scheduler] Running '${taskName}' (${frequency})`);\n console.log(`[Scheduler] Filtering from: ${startDate.toISOString()}`);\n console.log(`[Scheduler] Tags: ${tags.join(\", \") || \"(none)\"}`);\n\n const notesDir = process.cwd(); // Run in current dir\n const files = fs.readdirSync(notesDir).filter((f) => f.endsWith(\".md\"));\n const matchedNotes = [];\n\n for (const file of files) {\n const filePath = path.join(notesDir, file);\n const content = fs.readFileSync(filePath, \"utf-8\");\n const { data, content: body } = matter(content);\n\n // 1. Date Check\n let noteDate: Date | null = null;\n\n // Check fields in priority: date -> updatedAt -> createdAt\n const dateFields = [data.date, data.updatedAt, data.createdAt];\n\n for (const field of dateFields) {\n if (!field) continue;\n\n if (field instanceof Date) {\n noteDate = field;\n } else {\n const d = new Date(field);\n if (!isNaN(d.getTime())) {\n noteDate = d;\n }\n }\n\n if (noteDate) break;\n }\n\n // Fallback to filename timestamp if no valid date found in frontmatter\n if (!noteDate) {\n const filenameTs = parseInt(file.replace(\".md\", \"\"));\n if (!isNaN(filenameTs) && filenameTs > 1000000000000) {\n noteDate = new Date(filenameTs);\n }\n }\n\n if (!noteDate || isNaN(noteDate.getTime())) continue;\n\n if (isBefore(noteDate, startDate) || isAfter(noteDate, now)) continue;\n\n // 2. Tag Check\n const noteTags: string[] = Array.isArray(data.tags) ? data.tags : [];\n const normalizedNoteTags = noteTags.map((t) =>\n String(t).replace(/^#/, \"\").toLowerCase()\n );\n const normalizedSearchTags = tags.map((t) =>\n t.replace(/^#/, \"\").toLowerCase()\n );\n\n const hasAllTags = normalizedSearchTags.every((t) =>\n normalizedNoteTags.includes(t)\n );\n\n if (hasAllTags || tags.length === 0) {\n matchedNotes.push({\n title: data.title || file.replace(\".md\", \"\"),\n content: body,\n date: noteDate.toISOString(),\n tags: normalizedNoteTags,\n });\n }\n }\n\n console.log(`[Scheduler] Found ${matchedNotes.length} matching notes.`);\n\n if (matchedNotes.length === 0) {\n console.log(\"No notes to summarize.\");\n return;\n }\n\n // 3. API Call\n console.log(`[Scheduler] Sending to API: ${apiUrl}`);\n\n // Try to get OIDC token\n const oidcToken = await getGithubOidcToken(\"marlin-api\");\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n \"X-Marlin-User-Id\": userId,\n };\n\n if (oidcToken) {\n console.log(\"[Scheduler] Authenticating with GitHub OIDC\");\n headers[\"Authorization\"] = `Bearer ${oidcToken}`;\n } else {\n // Fallback or Error\n // We allow running without OIDC if strictly testing, but warn loudly.\n // In production, the API will likely reject it.\n console.warn(\n \"[Scheduler] WARNING: No OIDC token found. Ensure 'id-token: write' permission is set in the workflow.\"\n );\n }\n\n const response = await fetch(apiUrl, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n notes: matchedNotes,\n taskName,\n period: frequency,\n }),\n });\n\n if (!response.ok) {\n const errText = await response.text();\n throw new Error(`API Error ${response.status}: ${errText}`);\n }\n\n const result = (await response.json()) as { summary: string };\n\n // 4. Save Summary\n const summaryFilename = `summary-${frequency}-${now.toISOString().split(\"T\")[0]}.md`;\n\n const summaryContent = `---\ndate: ${now.getTime()}\ntags: [\"#summary/${frequency}\", \"#auto\"]\ntitle: ${frequency} Summary - ${taskName}\n---\n\n${result.summary}\n`;\n\n fs.writeFileSync(path.join(notesDir, summaryFilename), summaryContent);\n console.log(`[Scheduler] Summary saved to ${summaryFilename}`);\n}\n","export async function getGithubOidcToken(audience?: string): Promise<string | null> {\n const requestUrl = process.env.ACTIONS_ID_TOKEN_REQUEST_URL;\n const requestToken = process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;\n\n if (!requestUrl || !requestToken) {\n // Not running in GitHub Actions or permissions not set\n return null;\n }\n\n try {\n const url = new URL(requestUrl);\n if (audience) {\n url.searchParams.append(\"audience\", audience);\n }\n\n const response = await fetch(url.toString(), {\n headers: {\n Authorization: `Bearer ${requestToken}`,\n Accept: \"application/json; api-version=2.0\",\n \"Content-Type\": \"application/json\",\n },\n });\n\n if (!response.ok) {\n const text = await response.text();\n console.warn(`[OIDC] Failed to fetch token: ${response.status} ${text}`);\n return null;\n }\n\n const data = (await response.json()) as { value: string };\n return data.value;\n } catch (error) {\n console.warn(\"[OIDC] Error fetching token:\", error);\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uBAAwB;;;ACAxB,gBAAe;AACf,kBAAiB;AACjB,yBAAmB;AACnB,sBAAsD;;;ACHtD,eAAsB,mBAAmB,UAA2C;AAClF,QAAM,aAAa,QAAQ,IAAI;AAC/B,QAAM,eAAe,QAAQ,IAAI;AAEjC,MAAI,CAAC,cAAc,CAAC,cAAc;AAEhC,WAAO;AAAA,EACT;AAEA,MAAI;AACF,UAAM,MAAM,IAAI,IAAI,UAAU;AAC9B,QAAI,UAAU;AACZ,UAAI,aAAa,OAAO,YAAY,QAAQ;AAAA,IAC9C;AAEA,UAAM,WAAW,MAAM,MAAM,IAAI,SAAS,GAAG;AAAA,MAC3C,SAAS;AAAA,QACP,eAAe,UAAU,YAAY;AAAA,QACrC,QAAQ;AAAA,QACR,gBAAgB;AAAA,MAClB;AAAA,IACF,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,OAAO,MAAM,SAAS,KAAK;AACjC,cAAQ,KAAK,iCAAiC,SAAS,MAAM,IAAI,IAAI,EAAE;AACvE,aAAO;AAAA,IACT;AAEA,UAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,WAAO,KAAK;AAAA,EACd,SAAS,OAAO;AACd,YAAQ,KAAK,gC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AQ,QAAQ,IAAI,cAAc;AAE3D,UAAM,SAAS,QAAQ,UAAU,QAAQ,IAAI,iBAAiB;AAG9D,UAAM,SAAS,QAAQ,UAAU,QAAQ,IAAI,iBAAiB,QAAQ,IAAI;AAE1E,QAAI,CAAC,QAAQ;AACX,cAAQ,MAAM,gHAAgH;AAC9H,cAAQ,KAAK,CAAC;AAAA,IAChB;AAEA,QAAI,OAAiB,CAAC;AACtB,QAAI;AACF,aAAO,KAAK,MAAM,QAAQ;AAAA,IAC5B,SAAS,GAAG;AACV,cAAQ,KAAK,oDAAoD,CAAC;AAAA,IACpE;AAEA,UAAM,UAAU;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH,SAAS,OAAO;AACd,YAAQ,MAAM,WAAW,KAAK;AAC9B,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF,CAAC;AAEH,QAAQ,MAAM;","names":["fs","path","matter"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/summarizer.ts","../src/oidc.ts"],"sourcesContent":["import { Command } from \"commander\";\nimport { summarize } from \"./summarizer\";\n\nconst program = new Command();\n\nprogram\n .name(\"marlin-scheduler\")\n .description(\"CLI for Marlin scheduled tasks\")\n .version(\"0.0.1\");\n\nprogram\n .command(\"summarize\")\n .description(\"Run summarization task\")\n .option(\"--task-name <name>\", \"Name of the task\")\n .option(\"--frequency <freq>\", \"Frequency (weekly/monthly)\")\n .option(\"--tags <json>\", \"JSON string of tags\")\n .option(\"--api-url <url>\", \"Marlin API URL\")\n .option(\"--user-id <id>\", \"GitHub User ID\")\n .option(\n \"--dir <path>\",\n \"Directory to search for notes (default: current dir)\"\n )\n .action(async (options) => {\n try {\n // Prioritize flags, then Env Vars (standard Action inputs often use INPUT_ prefix)\n const taskName =\n options.taskName || process.env.INPUT_TASK_NAME || \"Manual Task\";\n const frequency =\n options.frequency || process.env.INPUT_FREQUENCY || \"weekly\";\n const tagsJson = options.tags || process.env.INPUT_TAGS || \"[]\";\n // Default to production API if not specified\n const apiUrl =\n options.apiUrl ||\n process.env.INPUT_API_URL ||\n \"https://marlinnotes.com/api/ai/summarize\";\n\n // Resolve User ID: Flag -> Input -> GitHub Actor ID\n const userId =\n options.userId ||\n process.env.INPUT_USER_ID ||\n process.env.GITHUB_ACTOR_ID;\n const baseDir = options.dir || process.env.INPUT_DIR;\n\n if (!userId) {\n console.error(\n \"Error: User ID is required. Pass via --user-id, set INPUT_USER_ID, or run in GitHub Actions (GITHUB_ACTOR_ID).\"\n );\n process.exit(1);\n }\n\n let tags: string[] = [];\n try {\n tags = JSON.parse(tagsJson);\n } catch (e) {\n console.warn(\"Failed to parse tags JSON, assuming empty array:\", e);\n }\n\n await summarize({\n taskName,\n frequency: frequency as \"weekly\" | \"monthly\",\n tags,\n apiUrl,\n userId,\n baseDir,\n });\n } catch (error) {\n console.error(\"Failed:\", error);\n process.exit(1);\n }\n });\n\nprogram.parse();\n","import fs from \"fs\";\nimport path from \"path\";\nimport matter from \"gray-matter\";\nimport { subDays, subMonths, isBefore, isAfter } from \"date-fns\";\nimport { getGithubOidcToken } from \"./oidc\";\n\ninterface SummarizeOptions {\n taskName: string;\n frequency: \"weekly\" | \"monthly\";\n tags: string[];\n apiUrl: string;\n userId: string;\n baseDir?: string;\n}\n\nexport async function summarize({\n taskName,\n frequency,\n tags,\n apiUrl,\n userId,\n baseDir,\n}: SummarizeOptions) {\n // ... (existing date logic unchanged) ...\n const now = new Date();\n let startDate: Date;\n\n if (frequency === \"weekly\") {\n startDate = subDays(now, 7);\n } else if (frequency === \"monthly\") {\n startDate = subMonths(now, 1);\n } else {\n throw new Error(`Unknown frequency: ${frequency}`);\n }\n\n console.log(`[Scheduler] Running '${taskName}' (${frequency})`);\n console.log(`[Scheduler] Filtering from: ${startDate.toISOString()}`);\n console.log(`[Scheduler] Tags: ${tags.join(\", \") || \"(none)\"}`);\n\n const notesDir = baseDir\n ? path.resolve(process.cwd(), baseDir)\n : process.cwd();\n console.log(`[Scheduler] Searching in: ${notesDir}`);\n\n if (!fs.existsSync(notesDir)) {\n throw new Error(`Directory not found: ${notesDir}`);\n }\n\n // Use recursive search if available (Node 20+), otherwise shallow\n // We filter out node_modules and .git to avoid clutter\n const files = fs\n .readdirSync(notesDir, { recursive: true } as any)\n .map((f) => String(f)) // Ensure string\n .filter(\n (f) =>\n f.endsWith(\".md\") && !f.includes(\"node_modules\") && !f.includes(\".git\")\n );\n\n const matchedNotes = [];\n\n for (const file of files) {\n const filePath = path.join(notesDir, file);\n const content = fs.readFileSync(filePath, \"utf-8\");\n const { data, content: body } = matter(content);\n\n // 1. Date Check\n let noteDate: Date | null = null;\n\n // Check fields in priority: date -> updatedAt -> createdAt\n const dateFields = [data.date, data.updatedAt, data.createdAt];\n\n for (const field of dateFields) {\n if (!field) continue;\n\n if (field instanceof Date) {\n noteDate = field;\n } else {\n const d = new Date(field);\n if (!isNaN(d.getTime())) {\n noteDate = d;\n }\n }\n\n if (noteDate) break;\n }\n\n // Fallback to filename timestamp if no valid date found in frontmatter\n if (!noteDate) {\n const filenameTs = parseInt(file.replace(\".md\", \"\"));\n if (!isNaN(filenameTs) && filenameTs > 1000000000000) {\n noteDate = new Date(filenameTs);\n }\n }\n\n if (!noteDate || isNaN(noteDate.getTime())) continue;\n\n if (isBefore(noteDate, startDate) || isAfter(noteDate, now)) continue;\n\n // 2. Tag Check\n const noteTags: string[] = Array.isArray(data.tags) ? data.tags : [];\n const normalizedNoteTags = noteTags.map((t) =>\n String(t).replace(/^#/, \"\").toLowerCase()\n );\n const normalizedSearchTags = tags.map((t) =>\n t.replace(/^#/, \"\").toLowerCase()\n );\n\n const hasAllTags = normalizedSearchTags.every((t) =>\n normalizedNoteTags.includes(t)\n );\n\n if (hasAllTags || tags.length === 0) {\n matchedNotes.push({\n title: data.title || file.replace(\".md\", \"\"),\n content: body,\n date: noteDate.toISOString(),\n tags: normalizedNoteTags,\n });\n }\n }\n\n console.log(`[Scheduler] Found ${matchedNotes.length} matching notes.`);\n\n if (matchedNotes.length === 0) {\n console.log(\"No notes to summarize.\");\n return;\n }\n\n // 3. API Call\n console.log(`[Scheduler] Sending to API: ${apiUrl}`);\n\n // Try to get OIDC token\n const oidcToken = await getGithubOidcToken(\"marlin-api\");\n const headers: Record<string, string> = {\n \"Content-Type\": \"application/json\",\n \"X-Marlin-User-Id\": userId,\n };\n\n if (oidcToken) {\n console.log(\"[Scheduler] Authenticating with GitHub OIDC\");\n headers[\"Authorization\"] = `Bearer ${oidcToken}`;\n } else {\n // Fallback or Error\n // We allow running without OIDC if strictly testing, but warn loudly.\n // In production, the API will likely reject it.\n console.warn(\n \"[Scheduler] WARNING: No OIDC token found. Ensure 'id-token: write' permission is set in the workflow.\"\n );\n }\n\n const response = await fetch(apiUrl, {\n method: \"POST\",\n headers,\n body: JSON.stringify({\n notes: matchedNotes,\n taskName,\n period: frequency,\n }),\n });\n\n if (!response.ok) {\n const errText = await response.text();\n throw new Error(`API Error ${response.status}: ${errText}`);\n }\n\n const result = (await response.json()) as { summary: string };\n\n // 4. Save Summary\n const summaryFilename = `summary-${frequency}-${now.toISOString().split(\"T\")[0]}.md`;\n\n const summaryContent = `---\ndate: ${now.getTime()}\ntags: [\"#summary/${frequency}\", \"#auto\"]\ntitle: ${frequency} Summary - ${taskName}\n---\n\n${result.summary}\n`;\n\n fs.writeFileSync(path.join(notesDir, summaryFilename), summaryContent);\n console.log(`[Scheduler] Summary saved to ${summaryFilename}`);\n}\n","export async function getGithubOidcToken(audience?: string): Promise<string | null> {\n const requestUrl = process.env.ACTIONS_ID_TOKEN_REQUEST_URL;\n const requestToken = process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;\n\n if (!requestUrl || !requestToken) {\n // Not running in GitHub Actions or permissions not set\n return null;\n }\n\n try {\n const url = new URL(requestUrl);\n if (audience) {\n url.searchParams.append(\"audience\", audience);\n }\n\n const response = await fetch(url.toString(), {\n headers: {\n Authorization: `Bearer ${requestToken}`,\n Accept: \"application/json; api-version=2.0\",\n \"Content-Type\": \"application/json\",\n },\n });\n\n if (!response.ok) {\n const text = await response.text();\n console.warn(`[OIDC] Failed to fetch token: ${response.status} ${text}`);\n return null;\n }\n\n const data = (await response.json()) as { value: string };\n return data.value;\n } catch (error) {\n console.warn(\"[OIDC] Error fetching token:\", error);\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uBAAwB;;;ACAxB,gBAAe;AACf,kBAAiB;AACjB,yBAAmB;AACnB,sBAAsD;;;ACHtD,eAAsB,mBAAmB,UAA2C;AAClF,QAAM,aAAa,QAAQ,IAAI;AAC/B,QAAM,eAAe,QAAQ,IAAI;AAEjC,MAAI,CAAC,cAAc,CAAC,cAAc;AAEhC,WAAO;AAAA,EACT;AAEA,MAAI;AACF,UAAM,MAAM,IAAI,IAAI,UAAU;AAC9B,QAAI,UAAU;AACZ,UAAI,aAAa,OAAO,YAAY,QAAQ;AAAA,IAC9C;AAEA,UAAM,WAAW,MAAM,MAAM,IAAI,SAAS,GAAG;AAAA,MAC3C,SAAS;AAAA,QACP,eAAe,UAAU,YAAY;AAAA,QACrC,QAAQ;AAAA,QACR,gBAAgB;AAAA,MAClB;AAAA,IACF,CAAC;AAED,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,OAAO,MAAM,SAAS,KAAK;AACjC,cAAQ,KAAK,iCAAiC,SAAS,MAAM,IAAI,IAAI,EAAE;AACvE,aAAO;AAAA,IACT;AAEA,UAAM,OAAQ,MAAM,SAAS,KAAK;AAClC,WAAO,KAAK;AAAA,EACd,SAAS,OAAO;AACd,YAAQ,KAAK,gCAAgC,KAAK;AAClD,WAAO;AAAA,EACT;AACF;;;ADpBA,eAAsB,UAAU;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAqB;AAEnB,QAAM,MAAM,oBAAI,KAAK;AACrB,MAAI;AAEJ,MAAI,cAAc,UAAU;AAC1B,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C,OAAO,OAAO,YAAY;AACzB,MAAI;AAEF,UAAM,WACJ,QAAQ,YAAY,QAAQ,IAAI,mBAAmB;AACrD,UAAM,YACJ,QAAQ,aAAa,QAAQ,IAAI,mBAAmB;AACtD,UAAM,WAAW,QAAQ,QAAQ,QAAQ,IAAI,cAAc;AAE3D,UAAM,SACJ,QAAQ,UACR,QAAQ,IAAI,iBACZ;AAGF,UAAM,SACJ,QAAQ,UACR,QAAQ,IAAI,iBACZ,QAAQ,IAAI;AACd,UAAM,UAAU,QAAQ,OAAO,QAAQ,IAAI;AAE3C,QAAI,CAAC,QAAQ;AACX,cAAQ;AAAA,QACN;AAAA,MACF;AACA,cAAQ,KAAK,CAAC;AAAA,IAChB;AAEA,QAAI,OAAiB,CAAC;AACtB,QAAI;AACF,aAAO,KAAK,MAAM,QAAQ;AAAA,IAC5B,SAAS,GAAG;AACV,cAAQ,KAAK,oDAAoD,CAAC;AAAA,IACpE;AAEA,UAAM,UAAU;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAAA,EACH,SAAS,OAAO;AACd,YAAQ,MAAM,WAAW,KAAK;AAC9B,YAAQ,KAAK,CAAC;AAAA,EAChB;AACF,CAAC;AAEH,QAAQ,MAAM;","names":["path","fs","matter"]}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -16,20 +16,35 @@ program
|
|
|
16
16
|
.option("--tags <json>", "JSON string of tags")
|
|
17
17
|
.option("--api-url <url>", "Marlin API URL")
|
|
18
18
|
.option("--user-id <id>", "GitHub User ID")
|
|
19
|
+
.option(
|
|
20
|
+
"--dir <path>",
|
|
21
|
+
"Directory to search for notes (default: current dir)"
|
|
22
|
+
)
|
|
19
23
|
.action(async (options) => {
|
|
20
24
|
try {
|
|
21
25
|
// Prioritize flags, then Env Vars (standard Action inputs often use INPUT_ prefix)
|
|
22
|
-
const taskName =
|
|
23
|
-
|
|
26
|
+
const taskName =
|
|
27
|
+
options.taskName || process.env.INPUT_TASK_NAME || "Manual Task";
|
|
28
|
+
const frequency =
|
|
29
|
+
options.frequency || process.env.INPUT_FREQUENCY || "weekly";
|
|
24
30
|
const tagsJson = options.tags || process.env.INPUT_TAGS || "[]";
|
|
25
31
|
// Default to production API if not specified
|
|
26
|
-
const apiUrl =
|
|
27
|
-
|
|
32
|
+
const apiUrl =
|
|
33
|
+
options.apiUrl ||
|
|
34
|
+
process.env.INPUT_API_URL ||
|
|
35
|
+
"https://marlinnotes.com/api/ai/summarize";
|
|
36
|
+
|
|
28
37
|
// Resolve User ID: Flag -> Input -> GitHub Actor ID
|
|
29
|
-
const userId =
|
|
38
|
+
const userId =
|
|
39
|
+
options.userId ||
|
|
40
|
+
process.env.INPUT_USER_ID ||
|
|
41
|
+
process.env.GITHUB_ACTOR_ID;
|
|
42
|
+
const baseDir = options.dir || process.env.INPUT_DIR;
|
|
30
43
|
|
|
31
44
|
if (!userId) {
|
|
32
|
-
console.error(
|
|
45
|
+
console.error(
|
|
46
|
+
"Error: User ID is required. Pass via --user-id, set INPUT_USER_ID, or run in GitHub Actions (GITHUB_ACTOR_ID)."
|
|
47
|
+
);
|
|
33
48
|
process.exit(1);
|
|
34
49
|
}
|
|
35
50
|
|
|
@@ -46,6 +61,7 @@ program
|
|
|
46
61
|
tags,
|
|
47
62
|
apiUrl,
|
|
48
63
|
userId,
|
|
64
|
+
baseDir,
|
|
49
65
|
});
|
|
50
66
|
} catch (error) {
|
|
51
67
|
console.error("Failed:", error);
|
package/src/summarizer.ts
CHANGED
|
@@ -10,6 +10,7 @@ interface SummarizeOptions {
|
|
|
10
10
|
tags: string[];
|
|
11
11
|
apiUrl: string;
|
|
12
12
|
userId: string;
|
|
13
|
+
baseDir?: string;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export async function summarize({
|
|
@@ -18,6 +19,7 @@ export async function summarize({
|
|
|
18
19
|
tags,
|
|
19
20
|
apiUrl,
|
|
20
21
|
userId,
|
|
22
|
+
baseDir,
|
|
21
23
|
}: SummarizeOptions) {
|
|
22
24
|
// ... (existing date logic unchanged) ...
|
|
23
25
|
const now = new Date();
|
|
@@ -35,8 +37,25 @@ export async function summarize({
|
|
|
35
37
|
console.log(`[Scheduler] Filtering from: ${startDate.toISOString()}`);
|
|
36
38
|
console.log(`[Scheduler] Tags: ${tags.join(", ") || "(none)"}`);
|
|
37
39
|
|
|
38
|
-
const notesDir =
|
|
39
|
-
|
|
40
|
+
const notesDir = baseDir
|
|
41
|
+
? path.resolve(process.cwd(), baseDir)
|
|
42
|
+
: process.cwd();
|
|
43
|
+
console.log(`[Scheduler] Searching in: ${notesDir}`);
|
|
44
|
+
|
|
45
|
+
if (!fs.existsSync(notesDir)) {
|
|
46
|
+
throw new Error(`Directory not found: ${notesDir}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Use recursive search if available (Node 20+), otherwise shallow
|
|
50
|
+
// We filter out node_modules and .git to avoid clutter
|
|
51
|
+
const files = fs
|
|
52
|
+
.readdirSync(notesDir, { recursive: true } as any)
|
|
53
|
+
.map((f) => String(f)) // Ensure string
|
|
54
|
+
.filter(
|
|
55
|
+
(f) =>
|
|
56
|
+
f.endsWith(".md") && !f.includes("node_modules") && !f.includes(".git")
|
|
57
|
+
);
|
|
58
|
+
|
|
40
59
|
const matchedNotes = [];
|
|
41
60
|
|
|
42
61
|
for (const file of files) {
|