mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
chore: optim
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::{conf::ChatConfJson, utils};
|
||||
use std::{fs, path::PathBuf};
|
||||
use std::{collections::HashMap, fs, path::PathBuf};
|
||||
use tauri::{api, command, AppHandle, Manager};
|
||||
|
||||
#[command]
|
||||
@@ -72,7 +72,7 @@ pub fn get_chat_model_cmd() -> serde_json::Value {
|
||||
serde_json::from_str(&content).unwrap()
|
||||
}
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct PromptRecord {
|
||||
pub cmd: Option<String>,
|
||||
pub act: String,
|
||||
@@ -99,9 +99,8 @@ pub fn window_reload(app: AppHandle, label: &str) {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
|
||||
use walkdir::WalkDir;
|
||||
use utils::chat_root;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
|
||||
pub struct ModelRecord {
|
||||
@@ -115,12 +114,14 @@ pub struct ModelRecord {
|
||||
#[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()) {
|
||||
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);
|
||||
let enable_list = data.into_iter().filter(|v| v.enable);
|
||||
list.extend(enable_list)
|
||||
}
|
||||
}
|
||||
@@ -128,3 +129,65 @@ pub fn cmd_list() -> Vec<ModelRecord> {
|
||||
list.sort_by(|a, b| a.cmd.len().cmp(&b.cmd.len()));
|
||||
list
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn sync_prompts(app: AppHandle, data: String, time: u64) {
|
||||
let data = parse_prompt(data)
|
||||
.iter()
|
||||
.map(move |i| ModelRecord {
|
||||
cmd: if i.cmd.is_some() {
|
||||
i.cmd.clone().unwrap()
|
||||
} else {
|
||||
utils::gen_cmd(i.act.clone())
|
||||
},
|
||||
act: i.act.clone(),
|
||||
prompt: i.prompt.clone(),
|
||||
tags: vec!["chatgpt-prompts".to_string()],
|
||||
enable: true,
|
||||
})
|
||||
.collect::<Vec<ModelRecord>>();
|
||||
|
||||
let model = chat_root().join("chat.model.json");
|
||||
let model_cmd = chat_root().join("chat.model.cmd.json");
|
||||
let chatgpt_prompts = chat_root().join("cache_model").join("chatgpt_prompts.json");
|
||||
|
||||
// chatgpt_prompts.json
|
||||
fs::write(
|
||||
chatgpt_prompts,
|
||||
serde_json::to_string_pretty(&data).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let cmd_data = cmd_list();
|
||||
|
||||
// chat.model.cmd.json
|
||||
fs::write(
|
||||
model_cmd,
|
||||
serde_json::to_string_pretty(&serde_json::json!({
|
||||
"name": "ChatGPT CMD",
|
||||
"last_updated": time,
|
||||
"data": cmd_data,
|
||||
}))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut kv = HashMap::new();
|
||||
kv.insert(
|
||||
"sync_prompts".to_string(),
|
||||
serde_json::json!({ "id": "chatgpt_prompts", "last_updated": time }),
|
||||
);
|
||||
let model_data = utils::merge(
|
||||
&serde_json::from_str(&fs::read_to_string(&model).unwrap()).unwrap(),
|
||||
&kv,
|
||||
);
|
||||
|
||||
// chat.model.json
|
||||
fs::write(model, serde_json::to_string_pretty(&model_data).unwrap()).unwrap();
|
||||
|
||||
// refresh window
|
||||
api::dialog::message(
|
||||
app.get_window("core").as_ref(),
|
||||
"Sync Prompts",
|
||||
"ChatGPT Prompts data has been synchronized!",
|
||||
);
|
||||
window_reload(app, "core");
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ use tauri::{
|
||||
};
|
||||
use tauri_plugin_positioner::{on_tray_event, Position, WindowExt};
|
||||
|
||||
use super::window;
|
||||
|
||||
// --- Menu
|
||||
pub fn init() -> Menu {
|
||||
let chat_conf = ChatConfJson::get_chat_conf();
|
||||
@@ -174,7 +176,7 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
|
||||
|
||||
match menu_id {
|
||||
// Preferences
|
||||
"control_center" => app.get_window("main").unwrap().show().unwrap(),
|
||||
"control_center" => window::control_window(&app),
|
||||
"restart" => tauri::api::process::restart(&app.env()),
|
||||
"inject_script" => open(&app, script_path),
|
||||
"go_conf" => utils::open_file(utils::chat_root()),
|
||||
@@ -182,12 +184,12 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
|
||||
"awesome" => open(&app, conf::AWESOME_URL.to_string()),
|
||||
"sync_prompts" => {
|
||||
tauri::api::dialog::ask(
|
||||
app.get_window("main").as_ref(),
|
||||
app.get_window("core").as_ref(),
|
||||
"Sync Prompts",
|
||||
"Data sync will enable all prompts, are you sure you want to sync?",
|
||||
move |is_restart| {
|
||||
if is_restart {
|
||||
app.get_window("main")
|
||||
app.get_window("core")
|
||||
.unwrap()
|
||||
.eval("window.__sync_prompts && window.__sync_prompts()")
|
||||
.unwrap()
|
||||
@@ -304,7 +306,7 @@ pub fn tray_handler(handle: &AppHandle, event: SystemTrayEvent) {
|
||||
}
|
||||
}
|
||||
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
|
||||
"control_center" => app.get_window("main").unwrap().show().unwrap(),
|
||||
"control_center" => window::control_window(&app),
|
||||
"restart" => tauri::api::process::restart(&handle.env()),
|
||||
"show_dock_icon" => {
|
||||
ChatConfJson::amend(&serde_json::json!({ "hide_dock_icon": false }), Some(app))
|
||||
|
||||
@@ -5,11 +5,11 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
|
||||
let chat_conf = ChatConfJson::get_chat_conf();
|
||||
let url = chat_conf.origin.to_string();
|
||||
let theme = ChatConfJson::theme();
|
||||
let handle = app.app_handle();
|
||||
// let handle = app.app_handle();
|
||||
|
||||
std::thread::spawn(move || {
|
||||
window::tray_window(&handle);
|
||||
});
|
||||
// std::thread::spawn(move || {
|
||||
// window::tray_window(&handle);
|
||||
// });
|
||||
|
||||
if chat_conf.hide_dock_icon {
|
||||
#[cfg(target_os = "macos")]
|
||||
|
||||
@@ -28,3 +28,17 @@ pub fn tray_window(handle: &tauri::AppHandle) {
|
||||
.unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn control_window(handle: &tauri::AppHandle) {
|
||||
let app = handle.clone();
|
||||
std::thread::spawn(move || {
|
||||
WindowBuilder::new(&app, "main", WindowUrl::App("index.html".into()))
|
||||
.title("ChatGPT")
|
||||
.resizable(false)
|
||||
.fullscreen(false)
|
||||
.inner_size(800.0, 600.0)
|
||||
.min_inner_size(800.0, 600.0)
|
||||
.build()
|
||||
.unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
2
src-tauri/src/assets/cmd.js
vendored
2
src-tauri/src/assets/cmd.js
vendored
@@ -106,8 +106,6 @@ async function cmdTip() {
|
||||
|
||||
// input text
|
||||
if (window.__CHAT_MODEL_STATUS__ === 2 && event.keyCode === 9) {
|
||||
console.log('«110» /src/assets/cmd.js ~> ', __CHAT_MODEL_STATUS__);
|
||||
|
||||
searchInput.value = window.__CHAT_MODEL_CMD_PROMPT__;
|
||||
modelDom.innerHTML = '';
|
||||
delete window.__CHAT_MODEL_STATUS__;
|
||||
|
||||
20
src-tauri/src/assets/core.js
vendored
20
src-tauri/src/assets/core.js
vendored
@@ -86,6 +86,26 @@ async function init() {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
window.__sync_prompts = async function() {
|
||||
const res = await fetch('https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv');
|
||||
if (res.ok) {
|
||||
const data = await res.text();
|
||||
console.log('«94» /src/assets/core.js ~> ', data);
|
||||
|
||||
await invoke('sync_prompts', { data, time: Date.now() });
|
||||
} else {
|
||||
invoke('messageDialog', {
|
||||
__tauriModule: 'Dialog',
|
||||
message: {
|
||||
cmd: 'messageDialog',
|
||||
message: 'ChatGPT Prompts data sync failed, please try again!'.toString(),
|
||||
title: 'Sync Prompts'.toString(),
|
||||
type: 'error'
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
|
||||
@@ -61,6 +61,7 @@ fn main() {
|
||||
cmd::open_file,
|
||||
cmd::get_chat_model_cmd,
|
||||
cmd::parse_prompt,
|
||||
cmd::sync_prompts,
|
||||
cmd::window_reload,
|
||||
cmd::cmd_list,
|
||||
fs_extra::metadata,
|
||||
@@ -76,7 +77,7 @@ fn main() {
|
||||
if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() {
|
||||
let win = event.window();
|
||||
if win.label() == "main" {
|
||||
win.hide().unwrap();
|
||||
win.close().unwrap();
|
||||
} else {
|
||||
// TODO: https://github.com/tauri-apps/tauri/issues/3084
|
||||
// event.window().hide().unwrap();
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
use regex::Regex;
|
||||
use serde_json::Value;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs::{self, File},
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
@@ -89,3 +92,21 @@ pub fn clear_conf(app: &tauri::AppHandle) {
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn merge(v: &Value, fields: &HashMap<String, Value>) -> Value {
|
||||
match v {
|
||||
Value::Object(m) => {
|
||||
let mut m = m.clone();
|
||||
for (k, v) in fields {
|
||||
m.insert(k.clone(), v.clone());
|
||||
}
|
||||
Value::Object(m)
|
||||
}
|
||||
v => v.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gen_cmd(name: String) -> String {
|
||||
let re = Regex::new(r"[^a-zA-Z0-9]").unwrap();
|
||||
re.replace_all(&name, "_").to_lowercase()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user