Skip to content

Commit

Permalink
Tweak slot detection (#1036)
Browse files Browse the repository at this point in the history
* fix final template render
* fix tree updates
- correctly rejoin nodes in tree in both directions
* handle graph update in message deletes
  • Loading branch information
sceuick authored Sep 26, 2024
1 parent 52496fe commit 52e13ab
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 22 deletions.
23 changes: 19 additions & 4 deletions common/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion common/presets/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
2 changes: 1 addition & 1 deletion common/template-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
17 changes: 6 additions & 11 deletions web/shared/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] }
Expand Down Expand Up @@ -99,12 +100,13 @@ const Slot: Component<{
const [slotId, setSlotId] = createSignal<string>()
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
Expand Down Expand Up @@ -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')
}
Expand Down Expand Up @@ -383,15 +385,7 @@ const Slot: Component<{
return (
<>
<Switch>
<Match
when={
!cfg.ready ||
!user.user ||
!specs() ||
tier()?.tier.disableSlots ||
user.sub?.tier?.disableSlots
}
>
<Match when={!cfg.ready || !user.user || !specs() || user.sub?.tier?.disableSlots}>
{null}
</Match>
<Match when={specs()!.video && cfg.slots.gtmVideoTag}>
Expand Down Expand Up @@ -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 })
})
Expand Down
8 changes: 8 additions & 0 deletions web/store/data/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down
12 changes: 7 additions & 5 deletions web/store/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ function updateMsgParents(chatId: string, parents: Record<string, string>) {

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
Expand All @@ -1227,6 +1227,12 @@ function updateMsgParents(chatId: string, parents: Record<string, string>) {
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({
Expand All @@ -1252,10 +1258,6 @@ subscribe(
updateMsgSub
)

subscribe('message-parents', { chatId: 'string', parents: 'any' }, (body) => {
updateMsgParents(body.chatId, body.parents)
})

subscribe(
'message-swapped',
{
Expand Down

0 comments on commit 52e13ab

Please sign in to comment.