From 52e13ab75ed4700c46d37026152c8c058628611a Mon Sep 17 00:00:00 2001 From: Sceik <60520115+sceuick@users.noreply.github.com> Date: Thu, 26 Sep 2024 23:33:55 +0800 Subject: [PATCH] Tweak slot detection (#1036) * fix final template render * fix tree updates - correctly rejoin nodes in tree in both directions * handle graph update in message deletes --- common/chat.ts | 23 +++++++++++++++++++---- common/presets/templates.ts | 2 +- common/template-parser.ts | 2 +- web/shared/Slot.tsx | 17 ++++++----------- web/store/data/messages.ts | 8 ++++++++ web/store/message.ts | 12 +++++++----- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/common/chat.ts b/common/chat.ts index ad24dd79d..7bcc98c06 100644 --- a/common/chat.ts +++ b/common/chat.ts @@ -43,7 +43,7 @@ export function toChatGraph(messages: AppSchema.ChatMessage[]): { tree: ChatTree } export function updateChatTreeNode(tree: ChatTree, msg: AppSchema.ChatMessage) { - const next: ChatTree = Object.assign({}, tree) + const next: ChatTree = { ...tree } next[msg._id] = { msg, @@ -62,15 +62,30 @@ export function updateChatTreeNode(tree: ChatTree, msg: AppSchema.ChatMessage) { } export function removeChatTreeNodes(tree: ChatTree, ids: string[]) { + const next = { ...tree } + for (const id of ids) { - const parent = tree[id] + const node = next[id] + if (!node) continue + + const parent = node.msg.parent ? next[node.msg.parent] : null if (parent) { parent.children.delete(id) + + for (const childId of node.children) { + parent.children.add(childId) + + const child = next[childId] + if (!child) continue + + child.msg.parent = node.msg.parent + } } - delete tree[id] + + delete next[id] } - return Object.assign({}, tree) + return next } export function getChatDepths(tree: ChatTree) { diff --git a/common/presets/templates.ts b/common/presets/templates.ts index f616aeba9..03e054a73 100644 --- a/common/presets/templates.ts +++ b/common/presets/templates.ts @@ -230,7 +230,7 @@ Summary: {{scenario}} <|model|>{{post}}`, ChatML: neat` <|im_start|>system -{{#if system_prompt}}{{system_prompt}}{{else}}{{else}}Write "{{char}}'s" next reply in a fictional roleplay chat between "{{user}}" and "{{char}}".{{/else}}{{/if}}<|im_end|> +{{#if system_prompt}}{{system_prompt}}{{else}}Write "{{char}}'s" next reply in a fictional roleplay chat between "{{user}}" and "{{char}}".{{/else}}{{/if}}<|im_end|> "{{char}}'s" Persona: {{personality}} diff --git a/common/template-parser.ts b/common/template-parser.ts index b7061a1a9..ad85c1af1 100644 --- a/common/template-parser.ts +++ b/common/template-parser.ts @@ -471,7 +471,7 @@ function renderCondition( const output: string[] = [] for (const child of children) { if (typeof child !== 'string' && child.kind === 'else') continue - const result = renderNode(child, { ...opts, isPart: true }, value) + const result = renderNode(child, { ...opts, isPart: false }, value) if (result) output.push(result) } diff --git a/web/shared/Slot.tsx b/web/shared/Slot.tsx index 65c11e439..02efd7a3b 100644 --- a/web/shared/Slot.tsx +++ b/web/shared/Slot.tsx @@ -15,6 +15,7 @@ import { getUserSubscriptionTier, wait } from '/common/util' import { createDebounce } from './util' const win: any = window +win.enableSticky = JSON.parse(localStorage.getItem('agnai-sticky') || 'true') window.googletag = window.googletag || { cmd: [] } window.ezstandalone = window.ezstandalone || { cmd: [] } @@ -99,12 +100,13 @@ const Slot: Component<{ const [slotId, setSlotId] = createSignal() const [actualId, setActualId] = createSignal('...') - const tier = createMemo(() => { + createEffect(() => { if (!user.user) return const subtier = getUserSubscriptionTier(user.user, user.tiers) if (subtier?.tier.disableSlots) { win.enableSticky = undefined + localStorage.setItem('agnai-sticky', 'false') } return subtier @@ -286,7 +288,7 @@ const Slot: Component<{ return log('No publisher id') } - if (user.sub?.tier?.disableSlots) { + if (user.sub?.tier.disableSlots) { props.parent.style.display = 'hidden' return log('Slots are tier disabled') } @@ -383,15 +385,7 @@ const Slot: Component<{ return ( <> - + {null} @@ -722,6 +716,7 @@ const [invokeFuse] = createDebounce(() => { console.log(`[fuse] init ${ids}`) const win: any = window win.enableSticky = true + localStorage.setItem('agnai-sticky', 'true') window.fusetag.que.push(() => { window.fusetag.pageInit({ blockingFuseIds: ids }) }) diff --git a/web/store/data/messages.ts b/web/store/data/messages.ts index e21502d1e..3dba7a0e9 100644 --- a/web/store/data/messages.ts +++ b/web/store/data/messages.ts @@ -95,6 +95,14 @@ async function getActiveTemplateParts() { return opts } +/** + * + * @param chatId + * @param msgIds + * @param leafId + * @param parents Which nodes need their parent updated. Key=node, Value=parentId + * @returns + */ export async function deleteMessages( chatId: string, msgIds: string[], diff --git a/web/store/message.ts b/web/store/message.ts index 00e8d1c87..c201eb8e5 100644 --- a/web/store/message.ts +++ b/web/store/message.ts @@ -1216,7 +1216,7 @@ function updateMsgParents(chatId: string, parents: Record) { let nextHist = messageHistory.slice() let nextMsgs = msgs.slice() - let tree = graph.tree + let tree = { ...graph.tree } for (const [nodeId, parentId] of Object.entries(parents)) { if (typeof parentId !== 'string') continue @@ -1227,6 +1227,12 @@ function updateMsgParents(chatId: string, parents: Record) { nextHist = replace(nodeId, nextHist, { parent: parentId }) nextMsgs = replace(nodeId, nextMsgs, { parent: parentId }) tree = updateChatTreeNode(tree, next) + tree[next._id].children = new Set(prev.children) + + const parent = tree[parentId] + if (parent) { + parent.children.add(next._id) + } } msgStore.setState({ @@ -1252,10 +1258,6 @@ subscribe( updateMsgSub ) -subscribe('message-parents', { chatId: 'string', parents: 'any' }, (body) => { - updateMsgParents(body.chatId, body.parents) -}) - subscribe( 'message-swapped', {