|
@@ -14,21 +14,15 @@ export interface Prompt {
|
|
|
|
|
|
|
|
export const SearchService = {
|
|
export const SearchService = {
|
|
|
ready: false,
|
|
ready: false,
|
|
|
- builtinEngine: new Fuse<Prompt>([], {keys: [ "title" ]}),
|
|
|
|
|
userEngine: new Fuse<Prompt>([], {keys: [ "title" ]}),
|
|
userEngine: new Fuse<Prompt>([], {keys: [ "title" ]}),
|
|
|
- count: {
|
|
|
|
|
- builtin: 0,
|
|
|
|
|
- },
|
|
|
|
|
allPrompts: [] as Prompt[],
|
|
allPrompts: [] as Prompt[],
|
|
|
- builtinPrompts: [] as Prompt[],
|
|
|
|
|
|
|
|
|
|
init(builtinPrompts: Prompt[], userPrompts: Prompt[]) {
|
|
init(builtinPrompts: Prompt[], userPrompts: Prompt[]) {
|
|
|
if ( this.ready ) {
|
|
if ( this.ready ) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- this.allPrompts = userPrompts.concat(builtinPrompts);
|
|
|
|
|
- this.builtinPrompts = builtinPrompts.slice();
|
|
|
|
|
- this.builtinEngine.setCollection(builtinPrompts);
|
|
|
|
|
|
|
+ // 移除内置提示词支持,仅使用用户提示词
|
|
|
|
|
+ this.allPrompts = userPrompts;
|
|
|
this.userEngine.setCollection(userPrompts);
|
|
this.userEngine.setCollection(userPrompts);
|
|
|
this.ready = true;
|
|
this.ready = true;
|
|
|
},
|
|
},
|
|
@@ -43,8 +37,7 @@ export const SearchService = {
|
|
|
|
|
|
|
|
search(text: string) {
|
|
search(text: string) {
|
|
|
const userResults = this.userEngine.search(text);
|
|
const userResults = this.userEngine.search(text);
|
|
|
- const builtinResults = this.builtinEngine.search(text);
|
|
|
|
|
- return userResults.concat(builtinResults).map((v) => v.item);
|
|
|
|
|
|
|
+ return userResults.map((v) => v.item);
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -71,11 +64,6 @@ export const usePromptStore = createPersistStore(
|
|
|
|
|
|
|
|
get(id: string) {
|
|
get(id: string) {
|
|
|
const targetPrompt = get().prompts[id];
|
|
const targetPrompt = get().prompts[id];
|
|
|
-
|
|
|
|
|
- if ( !targetPrompt ) {
|
|
|
|
|
- return SearchService.builtinPrompts.find((v) => v.id === id);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
return targetPrompt;
|
|
return targetPrompt;
|
|
|
},
|
|
},
|
|
|
|
|
|
|
@@ -124,8 +112,8 @@ export const usePromptStore = createPersistStore(
|
|
|
|
|
|
|
|
search(text: string) {
|
|
search(text: string) {
|
|
|
if ( text.length === 0 ) {
|
|
if ( text.length === 0 ) {
|
|
|
- // return all rompts
|
|
|
|
|
- return this.getUserPrompts().concat(SearchService.builtinPrompts);
|
|
|
|
|
|
|
+ // 仅返回用户提示词
|
|
|
|
|
+ return this.getUserPrompts();
|
|
|
}
|
|
}
|
|
|
return SearchService.search(text) as Prompt[];
|
|
return SearchService.search(text) as Prompt[];
|
|
|
},
|
|
},
|
|
@@ -147,42 +135,20 @@ export const usePromptStore = createPersistStore(
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
onRehydrateStorage(state) {
|
|
onRehydrateStorage(state) {
|
|
|
- const PROMPT_URL = "./prompts.json";
|
|
|
|
|
-
|
|
|
|
|
- type PromptList = Array<[ string, string ]>; // [标题, 内容]的元组数组
|
|
|
|
|
- // tw: Array<[string, string]>; // 已停用繁体中文提示词
|
|
|
|
|
- // en: Array<[string, string]>; // 已停用英文提示词
|
|
|
|
|
-
|
|
|
|
|
- fetch(PROMPT_URL)
|
|
|
|
|
- .then((res) => res.json())
|
|
|
|
|
- .then((res) => {
|
|
|
|
|
- let fetchPrompts = [
|
|
|
|
|
- // res.en, res.tw, // 注释掉英文和繁体中文提示词
|
|
|
|
|
- res.cn ];
|
|
|
|
|
- // if ( getLang() === "cn" ) {
|
|
|
|
|
- // fetchPrompts = fetchPrompts.reverse();
|
|
|
|
|
- // }
|
|
|
|
|
- const builtinPrompts = fetchPrompts.filter(item => item).map((promptList: PromptList) => {
|
|
|
|
|
- return promptList.map(
|
|
|
|
|
- ([ title, content ]) =>
|
|
|
|
|
- ({
|
|
|
|
|
- id: nanoid(),
|
|
|
|
|
- title,
|
|
|
|
|
- content,
|
|
|
|
|
- createdAt: Date.now(),
|
|
|
|
|
- }) as Prompt,
|
|
|
|
|
- );
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const userPrompts = usePromptStore.getState().getUserPrompts() ?? [];
|
|
|
|
|
-
|
|
|
|
|
- const allPromptsForSearch = builtinPrompts
|
|
|
|
|
- .reduce((pre, cur) => pre.concat(cur), [])
|
|
|
|
|
- .filter((v) => !!v.title && !!v.content);
|
|
|
|
|
- SearchService.count.builtin =
|
|
|
|
|
- res.cn.length; // 仅计算中文提示词数量
|
|
|
|
|
- SearchService.init(allPromptsForSearch, userPrompts);
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ // ==============
|
|
|
|
|
+ // 移除 prompts.json 自动加载
|
|
|
|
|
+ // 说明:不再从前端控制提示词配置,移除内置提示词系统
|
|
|
|
|
+ // ==============
|
|
|
|
|
+
|
|
|
|
|
+ return (state) => {
|
|
|
|
|
+ // 使用回调中的 state 参数获取用户提示词
|
|
|
|
|
+ const userPrompts = state ? Object.values(state.prompts ?? {}).sort((a, b) =>
|
|
|
|
|
+ b.id && a.id ? b.createdAt - a.createdAt : 0
|
|
|
|
|
+ ) : [];
|
|
|
|
|
+
|
|
|
|
|
+ // 仅使用用户自定义提示词,不加载内置提示词
|
|
|
|
|
+ SearchService.init([], userPrompts);
|
|
|
|
|
+ };
|
|
|
},
|
|
},
|
|
|
},
|
|
},
|
|
|
);
|
|
);
|