initial work on message history and data transfer
This commit is contained in:
@@ -10,29 +10,42 @@ pub fn Popup() -> impl IntoView {
|
||||
let prompt_input_ref = NodeRef::<Input>::new();
|
||||
let (prompt_text, set_prompt_text) = signal(String::new());
|
||||
let (messages, set_messages) = signal(Vec::<Message>::new());
|
||||
// Action that calls the promp daemon
|
||||
|
||||
let init_history = Action::new_local(|(): &()| async move {
|
||||
let response = invoke(
|
||||
"chat_history",
|
||||
serde_wasm_bindgen::to_value(&serde_json::json!({"chat_id": 1})).unwrap(),
|
||||
)
|
||||
.await;
|
||||
let history: MessageHistory = serde_wasm_bindgen::from_value(response).unwrap();
|
||||
history
|
||||
});
|
||||
Effect::new(move |_| {
|
||||
init_history.dispatch(());
|
||||
});
|
||||
Effect::new(move |_| {
|
||||
if let Some(mut dat) = init_history.value().get() {
|
||||
set_messages.update(|m| m.append(&mut dat.history));
|
||||
}
|
||||
});
|
||||
|
||||
// Action that calls the chat action on the daemon
|
||||
let prompt_action = Action::new_local(|prompt: &String| {
|
||||
let prompt = prompt.clone();
|
||||
async move {
|
||||
let response = invoke(
|
||||
"prompt_llm",
|
||||
"chat",
|
||||
serde_wasm_bindgen::to_value(&serde_json::json!({"prompt": prompt})).unwrap(),
|
||||
)
|
||||
.await;
|
||||
let result: String = serde_wasm_bindgen::from_value(response).unwrap();
|
||||
let result: Vec<Message> = serde_wasm_bindgen::from_value(response).unwrap();
|
||||
result
|
||||
}
|
||||
});
|
||||
// Update the model response div with the prompt result
|
||||
Effect::new(move |_| {
|
||||
if let Some(result) = prompt_action.value().get() {
|
||||
set_messages.update(|previous| {
|
||||
previous.push(Message {
|
||||
id: previous.len() as i64,
|
||||
text: result,
|
||||
is_user: false,
|
||||
});
|
||||
});
|
||||
if let Some(mut result) = prompt_action.value().get() {
|
||||
set_messages.update(|m| m.append(&mut result));
|
||||
}
|
||||
});
|
||||
// Clear the propt text-input when the window loses focus (and is hidden)
|
||||
@@ -60,30 +73,17 @@ pub fn Popup() -> impl IntoView {
|
||||
}
|
||||
});
|
||||
|
||||
spawn_local(async move {
|
||||
let response = invoke("chat_history", JsValue::bigint_from_str("1")).await;
|
||||
let history: MessageHistory = serde_wasm_bindgen::from_value(response).unwrap();
|
||||
set_messages.set(history.history.clone());
|
||||
});
|
||||
|
||||
view! {
|
||||
<main class="window-shell rounded-container">
|
||||
<h3>"AI quick action"</h3>
|
||||
<input
|
||||
class="dark-input"
|
||||
type="text"
|
||||
node_ref=prompt_input_ref
|
||||
placeholder="Ask Gordon AI"
|
||||
placeholder="Prompt..."
|
||||
autofocus
|
||||
on:input=move |ev| set_prompt_text.set(event_target_value(&ev))
|
||||
on:keydown=move |ev| {
|
||||
if ev.key() == "Enter" {
|
||||
set_messages.update(|previous| {
|
||||
previous.push(Message {
|
||||
id: previous.len() as i64,
|
||||
text: prompt_text.get(),
|
||||
is_user: true,
|
||||
});
|
||||
});
|
||||
prompt_action.dispatch(prompt_text.get());
|
||||
set_prompt_text.update(|s| *s = "".to_string());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user