UNPKG

hataraku

Version:

An autonomous coding agent for building AI-powered development tools. The name "Hataraku" (働く) means "to work" in Japanese.

94 lines 3.65 kB
import * as fs from 'fs/promises'; import * as path from 'path'; import { getConfigPaths } from '../config/config-paths'; export class TaskHistory { historyDir; constructor() { const paths = getConfigPaths(); this.historyDir = paths.logsDir; } async ensureHistoryDir() { await fs.mkdir(this.historyDir, { recursive: true }); } async saveTask(entry) { await this.ensureHistoryDir(); const taskDir = path.join(this.historyDir, entry.taskId); await fs.mkdir(taskDir, { recursive: true }); // Save task metadata const metadataPath = path.join(taskDir, 'metadata.json'); const metadata = { taskId: entry.taskId, timestamp: entry.timestamp, task: entry.task, tokensIn: entry.tokensIn, tokensOut: entry.tokensOut, cacheWrites: entry.cacheWrites, cacheReads: entry.cacheReads, totalCost: entry.totalCost, model: entry.model, mcpServers: entry.debug?.mcpServers }; await fs.writeFile(metadataPath, JSON.stringify(metadata, null, 2)); // Save conversation history const historyPath = path.join(taskDir, 'conversation.json'); await fs.writeFile(historyPath, JSON.stringify(entry.messages, null, 2)); // Save debug information if present if (entry.debug) { const debugPath = path.join(taskDir, 'debug.json'); await fs.writeFile(debugPath, JSON.stringify(entry.debug, null, 2)); } } async getTask(taskId) { try { const taskDir = path.join(this.historyDir, taskId); const metadataPath = path.join(taskDir, 'metadata.json'); const historyPath = path.join(taskDir, 'conversation.json'); const [metadataContent, historyContent, debugContent] = await Promise.all([ fs.readFile(metadataPath, 'utf-8'), fs.readFile(historyPath, 'utf-8'), fs.readFile(path.join(taskDir, 'debug.json'), 'utf-8').catch(() => null) ]); const metadata = JSON.parse(metadataContent); const messages = JSON.parse(historyContent); const debug = debugContent ? JSON.parse(debugContent) : undefined; return { ...metadata, messages, debug }; } catch (error) { return null; } } async listTasks() { await this.ensureHistoryDir(); const tasks = []; try { const taskDirs = await fs.readdir(this.historyDir); for (const taskId of taskDirs) { try { const metadataPath = path.join(this.historyDir, taskId, 'metadata.json'); const metadataContent = await fs.readFile(metadataPath, 'utf-8'); const metadata = JSON.parse(metadataContent); tasks.push({ taskId: metadata.taskId, timestamp: metadata.timestamp, task: metadata.task, }); } catch (error) { // Skip invalid task directories continue; } } } catch (error) { // Return empty array if history directory doesn't exist return []; } // Sort by timestamp, most recent first return tasks.sort((a, b) => b.timestamp - a.timestamp); } } //# sourceMappingURL=task-history.js.map