Skip to content

Commit

Permalink
Task/remove schema print (#1008)
Browse files Browse the repository at this point in the history
* remove schema print
* fix message edit with json
* memoize context preset
* only gen json when json enabled
  • Loading branch information
sceuick authored Aug 18, 2024
1 parent 9190891 commit 364ca40
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 15 deletions.
6 changes: 5 additions & 1 deletion srv/adapter/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ export async function createChatStream(
* - There is both a history and response template
*/
let jsonSchema: JsonField[] | undefined
if (subscription?.preset?.jsonSchemaCapable && opts.chatSchema) {
if (
subscription?.preset?.jsonSchemaCapable &&
opts.entities?.gen.jsonEnabled &&
opts.chatSchema
) {
jsonSchema = opts.chatSchema.schema
}

Expand Down
10 changes: 9 additions & 1 deletion srv/api/chat/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,15 @@ export const generateMessageV2 = handle(async (req, res) => {
let jsonPartial: any

const { stream, adapter, ...metadata } = await createChatStream(
{ ...body, chat, replyAs, impersonate, requestId, entities, chatSchema: schema },
{
...body,
chat,
replyAs,
impersonate,
requestId,
entities,
chatSchema: schema,
},
log
)

Expand Down
1 change: 1 addition & 0 deletions srv/db/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export async function getAllChats(userId: string) {
createdAt: 1,
updatedAt: 1,
messageCount: 1,
genPreset: 1,
'character.name': 1,
},
},
Expand Down
13 changes: 10 additions & 3 deletions web/pages/Chat/components/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ const Message: Component<MessageProps> = (props) => {
const saveEdit = () => {
if (props.msg.json) {
const json = jsonValues()
const update = getJsonUpdate(ctx.char?.json!, json)
const update = getJsonUpdate(
ctx.preset?.jsonSource === 'character' ? ctx.char?.json : ctx.preset?.json,
json
)

if (update) {
msgStore.editMessageProp(props.msg._id, update)
}

msgStore.editMessageProp(props.msg._id, update)
setEdit(false)
return
}
Expand Down Expand Up @@ -941,7 +947,8 @@ function getMessageContent(ctx: ContextState, props: MessageProps, state: ChatSt
}
}

function getJsonUpdate(def: NonNullable<AppSchema.Character['json']>, json: any) {
function getJsonUpdate(def: AppSchema.Character['json'], json: any) {
if (!def) return
const hydration = hydrateTemplate(def, json)

return {
Expand Down
1 change: 0 additions & 1 deletion web/shared/JsonSchema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export const JsonSchema: Component<{
if (!match) return

const field = prop.join('.')
console.log(fieldName, value)
const next = state.fields.map<JsonField>((f, i) => {
if (i !== +index) return f

Expand Down
9 changes: 0 additions & 9 deletions web/shared/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ import './modal.css'
import Tabs, { TabHook } from './Tabs'
import { markdown } from './markdown'
import { Portal } from 'solid-js/web'
import { setRootVariable } from './colors'

window.addEventListener('resize', updatePageVars)
updatePageVars()

function updatePageVars() {
setRootVariable('window-width', `${window.innerWidth}px`)
setRootVariable('window-height', `${window.innerHeight}px`)
}

interface Props {
title?: string | JSX.Element
Expand Down
15 changes: 15 additions & 0 deletions web/shared/colors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import { JSX } from 'solid-js'

let sizeTimeout: NodeJS.Timeout
window.addEventListener('resize', updatePageVars)
updatePageVars()

function updatePageVars() {
if (sizeTimeout) {
clearTimeout(sizeTimeout)
}

sizeTimeout = setTimeout(() => {
setRootVariable('window-width', `${window.innerWidth}px`)
setRootVariable('window-height', `${window.innerHeight}px`)
}, 10)
}

export function getRgbaFromVar(cssVar: string, opacity: number, id?: string): JSX.CSSProperties {
const hex = getSettingColor(cssVar)
const rgb = hexToRgb(hex)
Expand Down
10 changes: 10 additions & 0 deletions web/store/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PresetInfo, getClientPreset } from '../shared/adapter'
import { getRgbaFromVar } from '../shared/colors'
import { MsgState, msgStore } from './message'
import { ChatTree } from '/common/chat'
import { presetStore } from './presets'

export type ContextState = {
tooltip?: string | JSX.Element
Expand Down Expand Up @@ -53,6 +54,7 @@ export type ContextState = {
info?: PresetInfo
waiting?: MsgState['waiting']
status?: MsgState['hordeStatus']
preset?: AppSchema.UserGenPreset
}

const initial: ContextState = {
Expand Down Expand Up @@ -85,6 +87,7 @@ export function ContextProvider(props: { children: any }) {
const users = userStore()
const cfg = settingStore()
const msgs = msgStore()
const presets = presetStore()

const visuals = createMemo(() => {
const botBackground = getRgbaFromVar(
Expand Down Expand Up @@ -131,6 +134,12 @@ export function ContextProvider(props: { children: any }) {
return impersonate || handle || 'You'
})

const preset = createMemo(() => {
const id = chats.active?.chat.genPreset || users.user?.defaultPreset
const match = presets.presets.find((p) => p._id === id)
return match
})

createEffect(() => {
const info = getClientPreset(chats.active?.chat)
const next: Partial<ContextState> = {
Expand All @@ -157,6 +166,7 @@ export function ContextProvider(props: { children: any }) {
chatTree: msgs.graph.tree,
waiting: msgs.waiting,
status: msgs.hordeStatus,
preset: preset(),
}

setState(next)
Expand Down

0 comments on commit 364ca40

Please sign in to comment.