@@ -9,32 +9,60 @@ import {
9
9
getNotesFromTags ,
10
10
processVariableNameForNotePath ,
11
11
} from "@/utils" ;
12
- import { normalizePath , Notice , TFile , Vault } from "obsidian" ;
12
+ import { normalizePath , Notice , TFile , Vault , App } from "obsidian" ;
13
13
14
14
export interface CustomPrompt {
15
15
title : string ;
16
16
content : string ;
17
+ model ?: string ;
18
+ isTemporaryModel ?: boolean ;
17
19
}
18
20
19
21
export class CustomPromptProcessor {
20
- private static instance : CustomPromptProcessor ;
22
+ private static instance : CustomPromptProcessor | null = null ;
23
+ private vault : Vault ;
21
24
private usageStrategy : TimestampUsageStrategy ;
25
+ private app : App ;
22
26
23
- private constructor ( private vault : Vault ) {
27
+ private constructor ( app : App ) {
28
+ this . app = app ;
29
+ this . vault = app . vault ;
24
30
this . usageStrategy = new TimestampUsageStrategy ( ) ;
25
31
}
26
32
27
33
get customPromptsFolder ( ) : string {
28
34
return getSettings ( ) . customPromptsFolder ;
29
35
}
30
36
31
- static getInstance ( vault : Vault ) : CustomPromptProcessor {
37
+ static getInstance ( app : App ) : CustomPromptProcessor {
32
38
if ( ! CustomPromptProcessor . instance ) {
33
- CustomPromptProcessor . instance = new CustomPromptProcessor ( vault ) ;
39
+ CustomPromptProcessor . instance = new CustomPromptProcessor ( app ) ;
34
40
}
35
41
return CustomPromptProcessor . instance ;
36
42
}
37
43
44
+ private parseFrontMatter (
45
+ file : TFile ,
46
+ content : string
47
+ ) : { frontmatter : { model ?: string ; isTemporaryModel ?: boolean } ; content : string } {
48
+ // Get the cached frontmatter from Obsidian's metadata cache
49
+ const cache = this . app . metadataCache . getFileCache ( file ) ;
50
+ const frontmatter = ( cache ?. frontmatter as Record < string , unknown > ) || { } ;
51
+
52
+ // Handle both model and temp-model fields
53
+ const tempModel = frontmatter [ "temp-model" ] as string | undefined ;
54
+ const model = tempModel || ( frontmatter . model as string | undefined ) ;
55
+ const isTemporaryModel = "temp-model" in frontmatter ;
56
+
57
+ // Get the content without frontmatter
58
+ const contentWithoutFrontmatter = content . replace ( / ^ - - - \n [ \s \S ] * ?\n - - - \n / , "" ) . trim ( ) ;
59
+
60
+ return {
61
+ frontmatter : { model, isTemporaryModel } ,
62
+ content : contentWithoutFrontmatter ,
63
+ } ;
64
+ }
65
+
38
66
recordPromptUsage ( title : string ) {
39
67
this . usageStrategy . recordUsage ( title ) ;
40
68
}
@@ -47,10 +75,13 @@ export class CustomPromptProcessor {
47
75
48
76
const prompts : CustomPrompt [ ] = [ ] ;
49
77
for ( const file of files ) {
50
- const content = await this . vault . read ( file ) ;
78
+ const rawContent = await this . vault . read ( file ) ;
79
+ const { frontmatter, content } = this . parseFrontMatter ( file , rawContent ) ;
51
80
prompts . push ( {
52
81
title : file . basename ,
53
82
content : content ,
83
+ model : frontmatter . model ,
84
+ isTemporaryModel : frontmatter . isTemporaryModel ,
54
85
} ) ;
55
86
}
56
87
@@ -64,8 +95,14 @@ export class CustomPromptProcessor {
64
95
const filePath = `${ this . customPromptsFolder } /${ title } .md` ;
65
96
const file = this . vault . getAbstractFileByPath ( filePath ) ;
66
97
if ( file instanceof TFile ) {
67
- const content = await this . vault . read ( file ) ;
68
- return { title, content } ;
98
+ const rawContent = await this . vault . read ( file ) ;
99
+ const { frontmatter, content } = this . parseFrontMatter ( file , rawContent ) ;
100
+ return {
101
+ title,
102
+ content,
103
+ model : frontmatter . model ,
104
+ isTemporaryModel : frontmatter . isTemporaryModel ,
105
+ } ;
69
106
}
70
107
return null ;
71
108
}
0 commit comments