Skip to content

Commit

Permalink
- Updated SmartThread and SmartThreads classes to utilize a new `…
Browse files Browse the repository at this point in the history
…render_component` method for rendering threads and chats, improving modularity.

- Enhanced chat input handling in `post_process` function to support asynchronous message sending on Enter key press, streamlining user interaction.
- Deprecated the `threads` component in favor of platform-specific container components, indicating a shift in architecture.
- Introduced a helper method in `CollectionItem` for rendering components within the item scope, enhancing reusability.

These changes aim to improve the overall structure and functionality of the chat interface.
  • Loading branch information
Brian Joseph Petro committed Dec 27, 2024
1 parent 6c71195 commit 6820e6d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
33 changes: 16 additions & 17 deletions smart-chats/components/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ export async function post_process(thread, frag, opts) {
const chat_input = frag.querySelector('.sc-chat-form textarea');
console.log('chat_input', chat_input);
if (chat_input) {
chat_input.addEventListener('keydown', (e) => handle_chat_input_keydown.call(this, e, thread, chat_input, opts));
chat_input.addEventListener('keydown', async (e) => {
const is_mod = this.adapter.is_mod_event(e);
if (e.key === "Enter" && (is_mod || e.ctrlKey || e.metaKey)) {
e.preventDefault();
await send_message(chat_input, thread);
return;
}
});
chat_input.addEventListener('keyup', (e) => handle_chat_input_keyup.call(this, e, chat_input));
}

Expand All @@ -99,9 +106,8 @@ export async function post_process(thread, frag, opts) {

// Send button
const send_button = frag.querySelector('#sc-send-button');
send_button.addEventListener('click', () => {
thread.handle_message_from_user(chat_input.value);
chat_input.value = '';
send_button.addEventListener('click', async () => {
await send_message(chat_input, thread);
});
// Abort button
const abort_button = frag.querySelector('#sc-abort-button');
Expand Down Expand Up @@ -146,22 +152,15 @@ export async function post_process(thread, frag, opts) {
}

return frag;

}

/**
* Handles chat input keydown events
* @private
*/
function handle_chat_input_keydown(e, thread, chat_input, opts) {
const mod = this.adapter.is_mod_event(e);
if (e.key === "Enter" && mod) {
e.preventDefault();
thread.handle_message_from_user(chat_input.value);
chat_input.value = '';
return;
}
opts.handle_chat_input_keydown(e, chat_input);

async function send_message(chat_input, thread) {
const message = chat_input.value;
chat_input.value = '';
await thread.handle_message_from_user(message);
await thread.save();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions smart-chats/components/threads.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @deprecated in favor of platform-specific container components
*/
/**
* @module components/threads
* @description Renders the main chat interface including threads list, active thread, and input area
Expand Down
2 changes: 1 addition & 1 deletion smart-chats/smart_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class SmartThread extends SmartSource {
* @returns {Promise<DocumentFragment>} The rendered thread interface.
*/
async render(container = this.container, opts = {}) {
const frag = await thread_template.call(this.smart_view, this, opts);
const frag = await this.render_component('thread', opts);
if (container) {
container.empty();
if (container.classList.contains('sc-thread')) {
Expand Down
4 changes: 3 additions & 1 deletion smart-chats/smart_threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class SmartThreads extends SmartSources {
.forEach(file => {
const key = file.path.replace(this.source_dir + '/', '').replace('.' + file.extension, '');
this.items[key] = new this.item_type(this.env, { path: file.path, key });
this.items[key].source_adapter.import();
})
;
this.notices?.remove('initial scan');
Expand All @@ -46,7 +47,8 @@ export class SmartThreads extends SmartSources {
async render(container=this.container, opts={}) {
if(Object.keys(opts).length > 0) this.render_opts = opts; // persist render options for future renders (not ideal, but required since declaring render_opts outside of this class)
if(container && (!this.container || this.container !== container)) this.container = container;
const frag = await chat_template.call(this.smart_view, this, this.render_opts);
// const frag = await chat_template.call(this.smart_view, this, this.render_opts);
const frag = await this.render_component('chat', this.render_opts);
container.innerHTML = '';
container.appendChild(frag);
return frag;
Expand Down
10 changes: 10 additions & 0 deletions smart-collections/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ export class CollectionItem {
*/
parse() { /* NO-OP by default */ }

/**
* Helper function to render a component in the item scope
* @param {*} component_key
* @param {*} opts
* @returns
*/
async render_component(component_key, opts = {}) {
return await this.env.render_component(component_key, this, opts);
}

/**
* Derives the collection key from the class name.
* @returns {string}
Expand Down

0 comments on commit 6820e6d

Please sign in to comment.