11import OpenAI from "openai" ;
22import { sourcebot_file_diff_review , sourcebot_file_diff_review_schema } from "@/features/agents/review-agent/types" ;
33import { env } from "@sourcebot/shared" ;
4- import fs from "fs" ;
5- import path from "path" ;
4+ import { appendReviewAgentLog } from "@/features/agents/review-agent/lib"
65import { createLogger } from "@sourcebot/shared" ;
76
87const logger = createLogger ( 'invoke-diff-review-llm' ) ;
98
10- export const getReviewAgentLogDir = ( ) : string => {
11- return path . join ( env . DATA_CACHE_DIR , 'review-agent' ) ;
12- } ;
13-
14- const validateLogPath = ( logPath : string ) : void => {
15- const resolved = path . resolve ( logPath ) ;
16- const logDir = getReviewAgentLogDir ( ) ;
17- if ( ! resolved . startsWith ( logDir + path . sep ) ) {
18- throw new Error ( 'reviewAgentLogPath escapes log directory' ) ;
19- }
20- } ;
21-
22- export const invokeDiffReviewLlm = async ( reviewAgentLogPath : string | undefined , prompt : string ) : Promise < sourcebot_file_diff_review > => {
9+ export const invokeDiffReviewLlm = async ( reviewAgentLogFileName : string | undefined , prompt : string ) : Promise < sourcebot_file_diff_review > => {
2310 logger . debug ( "Executing invoke_diff_review_llm" ) ;
2411
2512 if ( ! env . OPENAI_API_KEY ) {
@@ -31,9 +18,8 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
3118 apiKey : env . OPENAI_API_KEY ,
3219 } ) ;
3320
34- if ( reviewAgentLogPath ) {
35- validateLogPath ( reviewAgentLogPath ) ;
36- fs . appendFileSync ( reviewAgentLogPath , `\n\nPrompt:\n${ prompt } ` ) ;
21+ if ( reviewAgentLogFileName ) {
22+ appendReviewAgentLog ( reviewAgentLogFileName , `\n\nPrompt:\n${ prompt } ` ) ;
3723 }
3824
3925 try {
@@ -45,9 +31,8 @@ export const invokeDiffReviewLlm = async (reviewAgentLogPath: string | undefined
4531 } ) ;
4632
4733 const openaiResponse = completion . choices [ 0 ] . message . content ;
48- if ( reviewAgentLogPath ) {
49- validateLogPath ( reviewAgentLogPath ) ;
50- fs . appendFileSync ( reviewAgentLogPath , `\n\nResponse:\n${ openaiResponse } ` ) ;
34+ if ( reviewAgentLogFileName ) {
35+ appendReviewAgentLog ( reviewAgentLogFileName , `\n\nResponse:\n${ openaiResponse } ` ) ;
5136 }
5237
5338 const diffReviewJson = JSON . parse ( openaiResponse || '{}' ) ;
0 commit comments