chore: sync

This commit is contained in:
lencx
2022-12-23 15:27:05 +08:00
parent 2be560e69a
commit 389e00a5e0
17 changed files with 260 additions and 252 deletions

View File

@@ -19,11 +19,12 @@ serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2.2", features = ["api-all", "devtools", "system-tray", "updater"] }
tauri-plugin-positioner = { version = "1.0.4", features = ["system-tray"] }
tokio = { version = "1.23.0", features = ["macros"] }
log = "0.4.17"
csv = "1.1.6"
thiserror = "1.0.38"
reqwest = "0.11.13"
walkdir = "2.3.2"
# tokio = { version = "1.23.0", features = ["macros"] }
# reqwest = "0.11.13"
[dependencies.tauri-plugin-log]
git = "https://github.com/tauri-apps/tauri-plugin-log"

View File

@@ -66,8 +66,8 @@ pub fn open_file(path: PathBuf) {
}
#[command]
pub fn get_chat_model() -> serde_json::Value {
let path = utils::chat_root().join("chat.model.json");
pub fn get_chat_model_cmd() -> serde_json::Value {
let path = utils::chat_root().join("chat.model.cmd.json");
let content = fs::read_to_string(path).unwrap_or_else(|_| r#"{"data":[]}"#.to_string());
serde_json::from_str(&content).unwrap()
}
@@ -98,3 +98,33 @@ pub fn window_reload(app: AppHandle, label: &str) {
.eval("window.location.reload()")
.unwrap();
}
use walkdir::WalkDir;
use utils::chat_root;
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct ModelRecord {
pub cmd: String,
pub act: String,
pub prompt: String,
pub tags: Vec<String>,
pub enable: bool,
}
#[command]
pub fn cmd_list() -> Vec<ModelRecord> {
let mut list = vec![];
for entry in WalkDir::new(chat_root().join("cache_model")).into_iter().filter_map(|e| e.ok()) {
let file = fs::read_to_string(entry.path().display().to_string());
if let Ok(v) = file {
let data: Vec<ModelRecord> = serde_json::from_str(&v).unwrap_or_else(|_| vec![]);
let enable_list = data.into_iter()
.filter(|v| v.enable);
list.extend(enable_list)
}
}
// dbg!(&list);
list.sort_by(|a, b| a.cmd.len().cmp(&b.cmd.len()));
list
}

View File

@@ -61,10 +61,8 @@ function init() {
}
async function cmdTip() {
const chatModelJson = await invoke('get_chat_model') || {};
const user_custom = chatModelJson.user_custom || [];
const sys_sync_prompts = chatModelJson.sys_sync_prompts || [];
const data = [...user_custom, ...sys_sync_prompts];
const chatModelJson = await invoke('get_chat_model_cmd') || {};
const data = chatModelJson.data;
if (data.length <= 0) return;
const modelDom = document.createElement('div');
@@ -82,18 +80,43 @@ async function cmdTip() {
// Enter a command starting with `/` and press a space to automatically fill `chatgpt prompt`.
// If more than one command appears in the search results, the first one will be used by default.
searchInput.addEventListener('keydown', (event) => {
if (!window.__CHAT_MODEL_CMD__) {
if (!window.__CHAT_MODEL_CMD_PROMPT__) {
return;
}
if (event.keyCode === 13 && window.__CHAT_MODEL_CMD__) {
searchInput.value = window.__CHAT_MODEL_CMD__;
// feat: https://github.com/lencx/ChatGPT/issues/54
if (event.keyCode === 9) {
const strGroup = window.__CHAT_MODEL_CMD_PROMPT__.match(/\{([^{}]*)\}/) || [];
if (strGroup[1]) {
searchInput.value = `/${window.__CHAT_MODEL_CMD__}` + `{${strGroup[1]}}` + ' |-> ';
window.__CHAT_MODEL_VAR__ = true;
}
event.preventDefault();
}
if (window.__CHAT_MODEL_VAR__ && event.keyCode === 9) {
const data = searchInput.value.split('|->');
if (data[1]) {
window.__CHAT_MODEL_CMD_PROMPT__ = window.__CHAT_MODEL_CMD_PROMPT__?.replace(/\{([^{}]*)\}/, `{${data[1]?.trim()}}`);
// searchInput.value = window.__CHAT_MODEL_CMD_PROMPT__;
}
// event.preventDefault();
}
// send
if (event.keyCode === 13 && window.__CHAT_MODEL_CMD_PROMPT__) {
searchInput.value = window.__CHAT_MODEL_CMD_PROMPT__;
modelDom.innerHTML = '';
delete window.__CHAT_MODEL_CMD_PROMPT__;
delete window.__CHAT_MODEL_CMD__;
delete window.__CHAT_MODEL_VAR__;
}
});
searchInput.addEventListener('input', (event) => {
if (window.__CHAT_MODEL_VAR__) return;
const query = searchInput.value;
if (!query || !/^\//.test(query)) {
modelDom.innerHTML = '';
@@ -102,18 +125,20 @@ async function cmdTip() {
// all cmd result
if (query === '/') {
const result = data.filter(i => i.enable);
modelDom.innerHTML = `<div>${result.map(itemDom).join('')}</div>`;
window.__CHAT_MODEL_CMD__ = result[0]?.prompt.trim();
modelDom.innerHTML = `<div>${data.map(itemDom).join('')}</div>`;
window.__CHAT_MODEL_CMD_PROMPT__ = data[0]?.prompt.trim();
window.__CHAT_MODEL_CMD__ = data[0]?.cmd.trim();
return;
}
const result = data.filter(i => i.enable && new RegExp(query.substring(1)).test(i.cmd));
const result = data.filter(i => new RegExp(query.substring(1)).test(i.cmd));
if (result.length > 0) {
modelDom.innerHTML = `<div>${result.map(itemDom).join('')}</div>`;
window.__CHAT_MODEL_CMD__ = result[0]?.prompt.trim();
window.__CHAT_MODEL_CMD_PROMPT__ = result[0]?.prompt.trim();
window.__CHAT_MODEL_CMD__ = result[0]?.cmd.trim();
} else {
modelDom.innerHTML = '';
delete window.__CHAT_MODEL_CMD_PROMPT__;
delete window.__CHAT_MODEL_CMD__;
}
}, {
@@ -136,7 +161,7 @@ async function cmdTip() {
const val = decodeURIComponent(item.getAttribute('data-prompt'));
searchInput.value = val;
document.querySelector('form textarea').focus();
window.__CHAT_MODEL_CMD__ = val;
window.__CHAT_MODEL_CMD_PROMPT__ = val;
modelDom.innerHTML = '';
}
}, {

View File

@@ -15,8 +15,7 @@ use tauri_plugin_log::{
LogTarget, LoggerBuilder,
};
#[tokio::main]
async fn main() {
fn main() {
ChatConfJson::init();
let chat_conf = ChatConfJson::get_chat_conf();
let context = tauri::generate_context!();
@@ -58,9 +57,10 @@ async fn main() {
cmd::form_confirm,
cmd::form_msg,
cmd::open_file,
cmd::get_chat_model,
cmd::get_chat_model_cmd,
cmd::parse_prompt,
cmd::window_reload,
cmd::cmd_list,
fs_extra::metadata,
])
.setup(setup::init)