|
| 1 | +import { tool } from '@openai/agents'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { z } from 'zod'; |
| 5 | + |
| 6 | +import type { Config } from '../classes/config'; |
| 7 | +import type { Logger } from '../classes/logger'; |
| 8 | +import { ToolUtils } from '../utils/tool-utils'; |
| 9 | + |
| 10 | +const name = 'append_changelog_entry'; |
| 11 | +const description = 'Append a changelog entry to a file with a date and body text'; |
| 12 | +const parametersSchema = z.object({ |
| 13 | + path: z.string().describe('The path to the changelog file'), |
| 14 | + date: z.string().describe('The date for the changelog entry (e.g., YYYY-MM-DD)'), |
| 15 | + body: z.string().describe('The body text of the changelog entry'), |
| 16 | +}); |
| 17 | + |
| 18 | +export const appendChangelogEntryTool = (config: Config, logger: Logger) => |
| 19 | + tool({ |
| 20 | + name, |
| 21 | + description, |
| 22 | + parameters: parametersSchema, |
| 23 | + execute: ToolUtils.wrappedExecute(name, execute, config, logger), |
| 24 | + }); |
| 25 | + |
| 26 | +export async function execute( |
| 27 | + parameters: z.infer<typeof parametersSchema>, |
| 28 | + config: Config |
| 29 | +) { |
| 30 | + try { |
| 31 | + // Resolve the target path relative to the configured directory |
| 32 | + const targetPath = path.resolve(parameters.path); |
| 33 | + const configDir = path.resolve(config.directory); |
| 34 | + |
| 35 | + // Check if the target path is outside the configured directory |
| 36 | + if (!targetPath.startsWith(configDir)) { |
| 37 | + return `Error appending changelog entry: Cannot write outside configured directory ${configDir}`; |
| 38 | + } |
| 39 | + |
| 40 | + // Check if the file exists |
| 41 | + if (!fs.existsSync(targetPath)) { |
| 42 | + return `Error appending changelog entry: File does not exist: ${parameters.path}`; |
| 43 | + } |
| 44 | + |
| 45 | + // Read the current file content |
| 46 | + const currentContent = fs.readFileSync(targetPath, 'utf-8'); |
| 47 | + |
| 48 | + // Format the changelog entry |
| 49 | + const entry = `## ${parameters.date}\n\n${parameters.body}\n\n`; |
| 50 | + |
| 51 | + // Find the first occurrence of ## (latest entry) |
| 52 | + const firstH2Index = currentContent.indexOf('## '); |
| 53 | + |
| 54 | + let newContent: string; |
| 55 | + if (firstH2Index !== -1) { |
| 56 | + // Insert the new entry before the latest entry |
| 57 | + newContent = |
| 58 | + currentContent.slice(0, firstH2Index) + |
| 59 | + entry + |
| 60 | + currentContent.slice(firstH2Index); |
| 61 | + } else { |
| 62 | + // No existing entries, append to the end |
| 63 | + newContent = currentContent + '\n' + entry; |
| 64 | + } |
| 65 | + |
| 66 | + // Write the updated content |
| 67 | + fs.writeFileSync(targetPath, newContent, 'utf-8'); |
| 68 | + |
| 69 | + return `Changelog entry inserted successfully to ${parameters.path}`; |
| 70 | + } catch (error: unknown) { |
| 71 | + if (error instanceof Error) { |
| 72 | + return `Error appending changelog entry: ${error.message}`; |
| 73 | + } |
| 74 | + return `Error appending changelog entry: ${error}`; |
| 75 | + } |
| 76 | +} |
| 77 | + |
0 commit comments