@@ -17,7 +17,7 @@ import {
17
17
} from '../../shared/utils/file_operations' ;
18
18
import logger from '../../shared/utils/logger' ;
19
19
import { appConfig } from '../config/app.config' ;
20
- import { processMetadataGeneration } from '../utils/prompt_operations ' ;
20
+ import { processMetadataGeneration } from '../utils/analyzer_operations ' ;
21
21
import { dumpYamlContent , sanitizeYamlContent } from '../utils/yaml_operations' ;
22
22
23
23
export async function generateMetadata ( promptContent : string ) : Promise < Metadata > {
@@ -50,18 +50,18 @@ export async function shouldUpdateMetadata(promptFile: string, metadataFile: str
50
50
const metadataContent = await readFileContent ( metadataFile ) ;
51
51
const storedHashLine = metadataContent . split ( '\n' ) . find ( ( line ) => line . trim ( ) . startsWith ( 'content_hash:' ) ) ;
52
52
53
- if ( storedHashLine ) {
54
- const storedHash = storedHashLine . split ( ':' ) [ 1 ] . trim ( ) ;
55
-
56
- if ( promptHash !== storedHash ) {
57
- logger . info ( `Content hash mismatch for ${ promptFile } . Update needed.` ) ;
58
- return [ true , promptHash ] ;
59
- }
60
- } else {
53
+ if ( ! storedHashLine ) {
61
54
logger . info ( `No content hash found in ${ metadataFile } . Update needed.` ) ;
62
55
return [ true , promptHash ] ;
63
56
}
64
57
58
+ const storedHash = storedHashLine . split ( ':' ) [ 1 ] . trim ( ) ;
59
+
60
+ if ( promptHash !== storedHash ) {
61
+ logger . info ( `Content hash mismatch for ${ promptFile } . Update needed.` ) ;
62
+ return [ true , promptHash ] ;
63
+ }
64
+
65
65
logger . info ( `Content hash match for ${ promptFile } . No update needed.` ) ;
66
66
return [ false , promptHash ] ;
67
67
} catch ( error ) {
@@ -74,17 +74,11 @@ export async function updateMetadataHash(metadataFile: string, newHash: string):
74
74
try {
75
75
const content = await readFileContent ( metadataFile ) ;
76
76
const lines = content . split ( '\n' ) ;
77
- let hashUpdated = false ;
78
-
79
- for ( let i = 0 ; i < lines . length ; i ++ ) {
80
- if ( lines [ i ] . trim ( ) . startsWith ( 'content_hash:' ) ) {
81
- lines [ i ] = `content_hash: ${ newHash } ` ;
82
- hashUpdated = true ;
83
- break ;
84
- }
85
- }
77
+ const hashIndex = lines . findIndex ( ( line ) => line . trim ( ) . startsWith ( 'content_hash:' ) ) ;
86
78
87
- if ( ! hashUpdated ) {
79
+ if ( hashIndex !== - 1 ) {
80
+ lines [ hashIndex ] = `content_hash: ${ newHash } ` ;
81
+ } else {
88
82
lines . push ( `content_hash: ${ newHash } ` ) ;
89
83
}
90
84
@@ -98,27 +92,39 @@ export async function updateMetadataHash(metadataFile: string, newHash: string):
98
92
99
93
export async function updatePromptMetadata ( ) : Promise < void > {
100
94
logger . info ( 'Starting update_prompt_metadata process' ) ;
101
- await processMainPrompt ( appConfig . PROMPTS_DIR ) ;
102
- await processPromptDirectories ( appConfig . PROMPTS_DIR ) ;
103
- logger . info ( 'update_prompt_metadata process completed' ) ;
95
+
96
+ try {
97
+ await processMainPrompt ( appConfig . PROMPTS_DIR ) ;
98
+ await processPromptDirectories ( appConfig . PROMPTS_DIR ) ;
99
+ logger . info ( 'update_prompt_metadata process completed' ) ;
100
+ } catch ( error ) {
101
+ logger . error ( 'Error in updatePromptMetadata:' , error ) ;
102
+ throw error ;
103
+ }
104
104
}
105
105
106
106
async function processMainPrompt ( promptsDir : string ) : Promise < void > {
107
107
const mainPromptFile = path . join ( promptsDir , commonConfig . PROMPT_FILE_NAME ) ;
108
108
109
109
if ( await fileExists ( mainPromptFile ) ) {
110
110
logger . info ( 'Processing main prompt.md file' ) ;
111
- const promptContent = await readFileContent ( mainPromptFile ) ;
112
- const metadata = await generateMetadata ( promptContent ) ;
113
- const newDirName = metadata . directory ;
114
- const newDirPath = path . join ( promptsDir , newDirName ) ;
115
- await createDirectory ( newDirPath ) ;
116
- const newPromptFile = path . join ( newDirPath , commonConfig . PROMPT_FILE_NAME ) ;
117
- await renameFile ( mainPromptFile , newPromptFile ) ;
118
- const metadataPath = path . join ( newDirPath , commonConfig . METADATA_FILE_NAME ) ;
119
- await writeFileContent ( metadataPath , sanitizeYamlContent ( dumpYamlContent ( metadata ) ) ) ;
120
- const newHash = crypto . createHash ( 'md5' ) . update ( promptContent ) . digest ( 'hex' ) ;
121
- await updateMetadataHash ( metadataPath , newHash ) ;
111
+
112
+ try {
113
+ const promptContent = await readFileContent ( mainPromptFile ) ;
114
+ const metadata = await generateMetadata ( promptContent ) ;
115
+ const newDirName = metadata . directory ;
116
+ const newDirPath = path . join ( promptsDir , newDirName ) ;
117
+ await createDirectory ( newDirPath ) ;
118
+ const newPromptFile = path . join ( newDirPath , commonConfig . PROMPT_FILE_NAME ) ;
119
+ await renameFile ( mainPromptFile , newPromptFile ) ;
120
+ const metadataPath = path . join ( newDirPath , commonConfig . METADATA_FILE_NAME ) ;
121
+ await writeFileContent ( metadataPath , sanitizeYamlContent ( dumpYamlContent ( metadata ) ) ) ;
122
+ const newHash = crypto . createHash ( 'md5' ) . update ( promptContent ) . digest ( 'hex' ) ;
123
+ await updateMetadataHash ( metadataPath , newHash ) ;
124
+ } catch ( error ) {
125
+ logger . error ( 'Error processing main prompt:' , error ) ;
126
+ throw error ;
127
+ }
122
128
}
123
129
}
124
130
@@ -150,13 +156,12 @@ async function processPromptFile(
150
156
promptsDir : string ,
151
157
item : string
152
158
) : Promise < void > {
153
- const [ shouldUpdate , newHash ] = await shouldUpdateMetadata ( promptFile , metadataFile ) ;
154
-
155
- if ( shouldUpdate ) {
156
- logger . info ( `Updating metadata for ${ item } ` ) ;
157
- const promptContent = await readFileContent ( promptFile ) ;
159
+ try {
160
+ const [ shouldUpdate , newHash ] = await shouldUpdateMetadata ( promptFile , metadataFile ) ;
158
161
159
- try {
162
+ if ( shouldUpdate ) {
163
+ logger . info ( `Updating metadata for ${ item } ` ) ;
164
+ const promptContent = await readFileContent ( promptFile ) ;
160
165
const metadata = await generateMetadata ( promptContent ) ;
161
166
162
167
if ( ! metadata || Object . keys ( metadata ) . length === 0 ) {
@@ -174,11 +179,12 @@ async function processPromptFile(
174
179
const metadataPath = path . join ( currentItemPath , commonConfig . METADATA_FILE_NAME ) ;
175
180
await writeFileContent ( metadataPath , sanitizeYamlContent ( dumpYamlContent ( metadata ) ) ) ;
176
181
await updateMetadataHash ( metadataPath , newHash ) ;
177
- } catch ( error ) {
178
- logger . error ( `Error processing ${ item } :` , error ) ;
182
+ } else {
183
+ logger . info ( `Metadata for ${ item } is up to date` ) ;
179
184
}
180
- } else {
181
- logger . info ( `Metadata for ${ item } is up to date` ) ;
185
+ } catch ( error ) {
186
+ logger . error ( `Error processing ${ item } :` , error ) ;
187
+ throw error ;
182
188
}
183
189
}
184
190
@@ -191,22 +197,27 @@ async function updatePromptDirectory(
191
197
const newDirPath = path . join ( promptsDir , newDirName ) ;
192
198
logger . info ( `Renaming directory from ${ oldDirName } to ${ newDirName } ` ) ;
193
199
194
- if ( await fileExists ( newDirPath ) ) {
195
- logger . warn ( `Directory ${ newDirName } already exists. Updating contents.` ) ;
196
- const files = await readDirectory ( currentItemPath ) ;
197
- await Promise . all (
198
- files . map ( async ( file ) => {
199
- const src = path . join ( currentItemPath , file ) ;
200
- const dst = path . join ( newDirPath , file ) ;
201
-
202
- if ( await isFile ( src ) ) {
203
- await copyFile ( src , dst ) ;
204
- }
205
- } )
206
- ) ;
207
- await removeDirectory ( currentItemPath ) ;
208
- } else {
209
- await renameFile ( currentItemPath , newDirPath ) ;
200
+ try {
201
+ if ( await fileExists ( newDirPath ) ) {
202
+ logger . warn ( `Directory ${ newDirName } already exists. Updating contents.` ) ;
203
+ const files = await readDirectory ( currentItemPath ) ;
204
+ await Promise . all (
205
+ files . map ( async ( file ) => {
206
+ const src = path . join ( currentItemPath , file ) ;
207
+ const dst = path . join ( newDirPath , file ) ;
208
+
209
+ if ( await isFile ( src ) ) {
210
+ await copyFile ( src , dst ) ;
211
+ }
212
+ } )
213
+ ) ;
214
+ await removeDirectory ( currentItemPath ) ;
215
+ } else {
216
+ await renameFile ( currentItemPath , newDirPath ) ;
217
+ }
218
+ } catch ( error ) {
219
+ logger . error ( `Error updating prompt directory from ${ oldDirName } to ${ newDirName } :` , error ) ;
220
+ throw error ;
210
221
}
211
222
}
212
223
0 commit comments