#!/usr/bin/env bun /** * grocery-basket-optimizer * Compares grocery options for savings and nutrition using OpenAI. */ import { parseArgs } from "util"; import { existsSync, mkdirSync, appendFileSync, readFileSync } from "fs"; import { join, dirname, resolve } from "path"; type OutputFormat = "markdown" | "json"; type GroceryTemplate = { items?: Array>; prices?: Array>; nutrition?: Record; notes?: string; }; interface SkillOptions { basket: string; budget?: string; audience?: string; tone?: string; format: OutputFormat; model: string; output?: string; template?: GroceryTemplate; } interface OpenAIChatResponse { choices?: Array<{ message?: { content?: string | null; }; }>; error?: { message?: string; }; } const SKILL_SLUG = "grocery-basket-optimizer"; function ensureDir(path: string) { if (!existsSync(path)) { mkdirSync(path, { recursive: true }); } } function getPaths() { const sessionStamp = new Date().toISOString().replace(/[:.]/g, "_").replace(/-/g, "_"); const exportsRoot = process.env.SKILLS_EXPORTS_DIR || join(process.cwd(), ".skills", "exports"); const logsRoot = process.env.SKILLS_LOGS_DIR || join(process.cwd(), ".skills", "logs"); const skillExportsDir = join(exportsRoot, SKILL_SLUG); const skillLogsDir = join(logsRoot, SKILL_SLUG); ensureDir(skillExportsDir); ensureDir(skillLogsDir); return { sessionStamp, skillExportsDir, skillLogsDir, }; } function createLogger(logDir: string, sessionStamp: string) { const logFile = join(logDir, `log_${sessionStamp}.txt`); function write(level: "info" | "success" | "error", message: string) { const timestamp = new Date().toISOString(); const entry = `[${timestamp}] [${level.toUpperCase()}] ${message}\n`; appendFileSync(logFile, entry); const prefix = level === "success" ? "✅" : level === "error" ? "❌" : "â„šī¸"; console.log(`${prefix} ${message}`); } return { info: (message: string) => write("info", message), success: (message: string) => write("success", message), error: (message: string) => write("error", message), logFile, }; } function slugify(value: string): string { return value .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 40) || "grocery-optimizer"; } function parseJsonTemplate(content: string): GroceryTemplate | undefined { try { const data = JSON.parse(content); if (typeof data === "object" && data !== null) { return data as GroceryTemplate; } } catch (_error) { // treat as plain text when parsing fails } return undefined; } function showHelp(): void { console.log(` grocery-basket-optimizer - Compare grocery options for savings and nutrition using AI Usage: skills run grocery-basket-optimizer -- [options] skills run grocery-basket-optimizer -- --text "" [options] Options: -h, --help Show this help message --text Inline grocery list/basket --budget Budget constraint --audience Target audience (default: Individual) --tone