Allow deletion of last summarization
This commit is contained in:
		
							parent
							
								
									92208f6809
								
							
						
					
					
						commit
						afbe932002
					
				| 
						 | 
					@ -1,7 +1,7 @@
 | 
				
			||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
  import Code from './Code.svelte'
 | 
					  import Code from './Code.svelte'
 | 
				
			||||||
  import { createEventDispatcher, onMount } from 'svelte'
 | 
					  import { createEventDispatcher, onMount } from 'svelte'
 | 
				
			||||||
  import { deleteMessage, chatsStorage } from './Storage.svelte'
 | 
					  import { deleteMessage, chatsStorage, deleteSummaryMessage } from './Storage.svelte'
 | 
				
			||||||
  import { getPrice } from './Stats.svelte'
 | 
					  import { getPrice } from './Stats.svelte'
 | 
				
			||||||
  import SvelteMarkdown from 'svelte-markdown'
 | 
					  import SvelteMarkdown from 'svelte-markdown'
 | 
				
			||||||
  import type { Message, Model, Chat } from './Types.svelte'
 | 
					  import type { Message, Model, Chat } from './Types.svelte'
 | 
				
			||||||
| 
						 | 
					@ -43,6 +43,31 @@
 | 
				
			||||||
    }, 0)
 | 
					    }, 0)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const checkDelete = () => {
 | 
				
			||||||
 | 
					    if (message.summarized) {
 | 
				
			||||||
 | 
					      // is in a summary, so we're summarized
 | 
				
			||||||
 | 
					      window.alert(`Sorry, you can't delete a summarized message`)
 | 
				
			||||||
 | 
					      return
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (message.summary) {
 | 
				
			||||||
 | 
					      // We're linked to messages we're a summary of
 | 
				
			||||||
 | 
					      if (window.confirm("Are you sure you want to delete this summary?\nYour session may be too long to submit again after you do.")) {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					          deleteSummaryMessage(chatId, message.uuid)
 | 
				
			||||||
 | 
					        } catch(e) {
 | 
				
			||||||
 | 
					          alert('Unable to delete summary:\n'+e.message)
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      deleteMessage(chatId, message.uuid)
 | 
				
			||||||
 | 
					    } catch(e) {
 | 
				
			||||||
 | 
					      alert('Unable to delete:\n'+e.message)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let dbnc
 | 
					  let dbnc
 | 
				
			||||||
  const update = () => {
 | 
					  const update = () => {
 | 
				
			||||||
    clearTimeout(dbnc)
 | 
					    clearTimeout(dbnc)
 | 
				
			||||||
| 
						 | 
					@ -67,6 +92,7 @@
 | 
				
			||||||
      editing = false
 | 
					      editing = false
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const scrollToMessage = (uuid:string | string[] | undefined) => {
 | 
					  const scrollToMessage = (uuid:string | string[] | undefined) => {
 | 
				
			||||||
    if (Array.isArray(uuid)) {
 | 
					    if (Array.isArray(uuid)) {
 | 
				
			||||||
      uuid = uuid[0]
 | 
					      uuid = uuid[0]
 | 
				
			||||||
| 
						 | 
					@ -110,21 +136,10 @@
 | 
				
			||||||
>
 | 
					>
 | 
				
			||||||
  <div class="message-body content">
 | 
					  <div class="message-body content">
 | 
				
			||||||
    <div class="greyscale is-pulled-right ml-2 button-pack">
 | 
					    <div class="greyscale is-pulled-right ml-2 button-pack">
 | 
				
			||||||
    {#if !message.summarized && !message.summary}
 | 
					    {#if message.summarized}
 | 
				
			||||||
    <a
 | 
					    <a
 | 
				
			||||||
      href={'#'}
 | 
					      href={'#'}
 | 
				
			||||||
      class=" delButton"
 | 
					      class="msg-summary-button"
 | 
				
			||||||
      on:click|preventDefault={() => {
 | 
					 | 
				
			||||||
        // messages.splice(i, 1)
 | 
					 | 
				
			||||||
        deleteMessage(chatId, message.uuid)
 | 
					 | 
				
			||||||
      }}
 | 
					 | 
				
			||||||
    >
 | 
					 | 
				
			||||||
    <Fa icon={faTrash} />
 | 
					 | 
				
			||||||
    </a>
 | 
					 | 
				
			||||||
    {:else if message.summarized}
 | 
					 | 
				
			||||||
    <a
 | 
					 | 
				
			||||||
      href={'#'}
 | 
					 | 
				
			||||||
      class="delButton"
 | 
					 | 
				
			||||||
      on:click|preventDefault={() => {
 | 
					      on:click|preventDefault={() => {
 | 
				
			||||||
        scrollToMessage(message.summarized)
 | 
					        scrollToMessage(message.summarized)
 | 
				
			||||||
      }}
 | 
					      }}
 | 
				
			||||||
| 
						 | 
					@ -135,7 +150,7 @@
 | 
				
			||||||
    {#if message.summary}
 | 
					    {#if message.summary}
 | 
				
			||||||
    <a
 | 
					    <a
 | 
				
			||||||
      href={'#'}
 | 
					      href={'#'}
 | 
				
			||||||
      class="delButton"
 | 
					      class="msg-summarized-button"
 | 
				
			||||||
      on:click|preventDefault={() => {
 | 
					      on:click|preventDefault={() => {
 | 
				
			||||||
        scrollToMessage(message.summary)
 | 
					        scrollToMessage(message.summary)
 | 
				
			||||||
      }}
 | 
					      }}
 | 
				
			||||||
| 
						 | 
					@ -143,6 +158,18 @@
 | 
				
			||||||
    <Fa icon={faDiagramPredecessor} />
 | 
					    <Fa icon={faDiagramPredecessor} />
 | 
				
			||||||
    </a>
 | 
					    </a>
 | 
				
			||||||
    {/if}
 | 
					    {/if}
 | 
				
			||||||
 | 
					    {#if !message.summarized}
 | 
				
			||||||
 | 
					    <a
 | 
				
			||||||
 | 
					      href={'#'}
 | 
				
			||||||
 | 
					      class=" msg-delete-button"
 | 
				
			||||||
 | 
					      on:click|preventDefault={() => {
 | 
				
			||||||
 | 
					        // messages.splice(i, 1)
 | 
				
			||||||
 | 
					        checkDelete()
 | 
				
			||||||
 | 
					      }}
 | 
				
			||||||
 | 
					    >
 | 
				
			||||||
 | 
					    <Fa icon={faTrash} />
 | 
				
			||||||
 | 
					    </a>
 | 
				
			||||||
 | 
					    {/if}
 | 
				
			||||||
    </div>
 | 
					    </div>
 | 
				
			||||||
    {#if editing && !noEdit}
 | 
					    {#if editing && !noEdit}
 | 
				
			||||||
      <form class="message-edit" on:submit|preventDefault={update} on:keydown={keydown}>
 | 
					      <form class="message-edit" on:submit|preventDefault={update} on:keydown={keydown}>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -166,6 +166,10 @@
 | 
				
			||||||
    return chat.messages
 | 
					    return chat.messages
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const getMessage = (chat: Chat, uuid:string):Message|undefined => {
 | 
				
			||||||
 | 
					    return chat.messages.find((m) => m.uuid === uuid)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  export const insertMessages = (chatId: number, insertAfter: Message, newMessages: Message[]) => {
 | 
					  export const insertMessages = (chatId: number, insertAfter: Message, newMessages: Message[]) => {
 | 
				
			||||||
    const chats = get(chatsStorage)
 | 
					    const chats = get(chatsStorage)
 | 
				
			||||||
    const chat = chats.find((chat) => chat.id === chatId) as Chat
 | 
					    const chat = chats.find((chat) => chat.id === chatId) as Chat
 | 
				
			||||||
| 
						 | 
					@ -178,10 +182,31 @@
 | 
				
			||||||
    chatsStorage.set(chats)
 | 
					    chatsStorage.set(chats)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  export const deleteSummaryMessage = (chatId: number, uuid: string) => {
 | 
				
			||||||
 | 
					    const chats = get(chatsStorage)
 | 
				
			||||||
 | 
					    const chat = chats.find((chat) => chat.id === chatId) as Chat
 | 
				
			||||||
 | 
					    const message = getMessage(chat, uuid)
 | 
				
			||||||
 | 
					    if (message && message.summarized) throw new Error('Unable to delete summarized message')
 | 
				
			||||||
 | 
					    if (message && message.summary) { // messages we summarized
 | 
				
			||||||
 | 
					      message.summary.forEach(sid=>{
 | 
				
			||||||
 | 
					        const m = getMessage(chat, sid)
 | 
				
			||||||
 | 
					        if (m) {
 | 
				
			||||||
 | 
					          delete m.summarized // unbind to this summary
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					      delete message.summary
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    chatsStorage.set(chats)
 | 
				
			||||||
 | 
					    deleteMessage(chatId, uuid)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  export const deleteMessage = (chatId: number, uuid: string) => {
 | 
					  export const deleteMessage = (chatId: number, uuid: string) => {
 | 
				
			||||||
    const chats = get(chatsStorage)
 | 
					    const chats = get(chatsStorage)
 | 
				
			||||||
    const chat = chats.find((chat) => chat.id === chatId) as Chat
 | 
					    const chat = chats.find((chat) => chat.id === chatId) as Chat
 | 
				
			||||||
    const index = chat.messages.findIndex((m) => m.uuid === uuid)
 | 
					    const index = chat.messages.findIndex((m) => m.uuid === uuid)
 | 
				
			||||||
 | 
					    const message = getMessage(chat, uuid)
 | 
				
			||||||
 | 
					    if (message && message.summarized) throw new Error('Unable to delete summarized message')
 | 
				
			||||||
 | 
					    if (message && message.summary) throw new Error('Unable to directly delete message summary')
 | 
				
			||||||
    // const found = chat.messages.filter((m) => m.uuid === uuid)
 | 
					    // const found = chat.messages.filter((m) => m.uuid === uuid)
 | 
				
			||||||
    if (index < 0) {
 | 
					    if (index < 0) {
 | 
				
			||||||
      console.error(`Unable to find and delete message with ID: ${uuid}`)
 | 
					      console.error(`Unable to find and delete message with ID: ${uuid}`)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue