feat: #2 add prompt list
parent
19b511e3f8
commit
7d5e742ea6
@ -0,0 +1,96 @@
|
||||
import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
import JsSearch from "js-search";
|
||||
|
||||
export interface Prompt {
|
||||
id?: number;
|
||||
shortcut: string;
|
||||
title: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface PromptStore {
|
||||
latestId: number;
|
||||
prompts: Map<number, Prompt>;
|
||||
|
||||
add: (prompt: Prompt) => number;
|
||||
remove: (id: number) => void;
|
||||
search: (text: string) => Prompt[];
|
||||
}
|
||||
|
||||
export const PROMPT_KEY = "prompt-store";
|
||||
|
||||
export const SearchService = {
|
||||
ready: false,
|
||||
progress: 0, // 0 - 1, 1 means ready
|
||||
engine: new JsSearch.Search("prompts"),
|
||||
deleted: new Set<number>(),
|
||||
|
||||
async init(prompts: PromptStore["prompts"]) {
|
||||
this.engine.addIndex("id");
|
||||
this.engine.addIndex("shortcut");
|
||||
this.engine.addIndex("title");
|
||||
|
||||
const n = prompts.size;
|
||||
let count = 0;
|
||||
for await (const prompt of prompts.values()) {
|
||||
this.engine.addDocument(prompt);
|
||||
count += 1;
|
||||
this.progress = count / n;
|
||||
}
|
||||
this.ready = true;
|
||||
},
|
||||
|
||||
remove(id: number) {
|
||||
this.deleted.add(id);
|
||||
},
|
||||
|
||||
add(prompt: Prompt) {
|
||||
this.engine.addDocument(prompt);
|
||||
},
|
||||
|
||||
search(text: string) {
|
||||
const results = this.engine.search(text) as Prompt[];
|
||||
return results.filter((v) => !v.id || !this.deleted.has(v.id));
|
||||
},
|
||||
};
|
||||
|
||||
export const usePromptStore = create<PromptStore>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
latestId: 0,
|
||||
prompts: new Map(),
|
||||
|
||||
add(prompt) {
|
||||
const prompts = get().prompts;
|
||||
prompt.id = get().latestId + 1;
|
||||
prompts.set(prompt.id, prompt);
|
||||
|
||||
set(() => ({
|
||||
latestId: prompt.id!,
|
||||
prompts: prompts,
|
||||
}));
|
||||
|
||||
return prompt.id!;
|
||||
},
|
||||
|
||||
remove(id) {
|
||||
const prompts = get().prompts;
|
||||
prompts.delete(id);
|
||||
SearchService.remove(id);
|
||||
|
||||
set(() => ({
|
||||
prompts,
|
||||
}));
|
||||
},
|
||||
|
||||
search(text) {
|
||||
return SearchService.search(text) as Prompt[];
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: PROMPT_KEY,
|
||||
version: 1,
|
||||
}
|
||||
)
|
||||
);
|
@ -0,0 +1,49 @@
|
||||
import fetch from "node-fetch";
|
||||
import fs from "fs/promises";
|
||||
|
||||
const CN_URL =
|
||||
"https://raw.githubusercontent.com/PlexPt/awesome-chatgpt-prompts-zh/main/prompts-zh.json";
|
||||
const EN_URL =
|
||||
"https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv";
|
||||
const FILE = "./public/prompts.json";
|
||||
|
||||
async function fetchCN() {
|
||||
console.log("[Fetch] fetching cn prompts...");
|
||||
try {
|
||||
const raw = await (await fetch(CN_URL)).json();
|
||||
return raw.map((v) => [v.act, v.prompt]);
|
||||
} catch (error) {
|
||||
console.error("[Fetch] failed to fetch cn prompts", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchEN() {
|
||||
console.log("[Fetch] fetching en prompts...");
|
||||
try {
|
||||
const raw = await (await fetch(EN_URL)).text();
|
||||
return raw
|
||||
.split("\n")
|
||||
.slice(1)
|
||||
.map((v) => v.split('","').map((v) => v.replace('"', "")));
|
||||
} catch (error) {
|
||||
console.error("[Fetch] failed to fetch cn prompts", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
Promise.all([fetchCN(), fetchEN()])
|
||||
.then(([cn, en]) => {
|
||||
fs.writeFile(FILE, JSON.stringify({ cn, en }));
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error("[Fetch] failed to fetch prompts");
|
||||
fs.writeFile(FILE, JSON.stringify({ cn: [], en: [] }));
|
||||
})
|
||||
.finally(() => {
|
||||
console.log("[Fetch] saved to " + FILE);
|
||||
});
|
||||
}
|
||||
|
||||
main();
|
Loading…
Reference in new issue