Skip to content

Commit

Permalink
Fix 'none' model selected for guests (#1024)
Browse files Browse the repository at this point in the history
- Pick default preset if init not found
* reorder sub models and add desc
- add model count
-Add model list to sub page tier card
- tweak model link position
- Fix preset model update
- fix chat memory book removal
- component emitter
- fix ai char gen guide
- fix json schema enum input
- use json schema v2
- use edge url
- fix schema imports
- fix temp character json schemas
- configurable agnai user lock
  • Loading branch information
sceuick authored Sep 10, 2024
1 parent 2a561f1 commit f5f37ab
Show file tree
Hide file tree
Showing 36 changed files with 719 additions and 370 deletions.
2 changes: 2 additions & 0 deletions common/types/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export interface Configuration {
/** Not yet implemented */
policiesEnabled: boolean

lockSeconds: number

/** Not yet implemented */
tosUpdated: string
/** Not yet implemented */
Expand Down
8 changes: 3 additions & 5 deletions common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,19 +564,17 @@ export function getSubscriptionModelLimits(
) {
if (!model) return

if (!Array.isArray(model.levels)) {
model.levels = []
}
const levels = Array.isArray(model.levels) ? model.levels.slice() : []

model.levels.push({
levels.push({
level: model.subLevel,
maxContextLength: model.maxContextLength!,
maxTokens: model.maxTokens,
})

let match: AppSchema.SubscriptionModelLevel | undefined

for (const candidate of model.levels) {
for (const candidate of levels) {
if (candidate.level > level) continue

if (!match || match.level < candidate.level) {
Expand Down
16 changes: 11 additions & 5 deletions srv/adapter/agnaistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { parseStops } from '/common/util'
import { getTextgenCompletion } from './dispatch'
import { handleVenus } from './venus'
import { sanitise, sanitiseAndTrim, trimResponseV2 } from '/common/requests/util'
import { obtainLock, releaseLock } from '../api/chat/lock'
import { getServerConfiguration } from '../db/admin'

export async function getSubscriptionPreset(
user: AppSchema.User,
Expand Down Expand Up @@ -120,13 +122,15 @@ export const handleAgnaistic: ModelAdapter = async function* (opts) {
return
}

const srv = await getServerConfiguration()

/**
* Lock per user per model
*/
// if (!opts.guidance) {
// const lockId = `${opts.user._id}-${preset.subModel}`
// await obtainLock(lockId, 15)
// }
const lockId = `${opts.user._id}-${opts.subscription.preset.name}`
if (!opts.guidance && +srv.lockSeconds > 0) {
await obtainLock(lockId, srv.lockSeconds)
}

const useRecommended = !!opts.gen.registered?.agnaistic?.useRecommended
if (useRecommended) {
Expand Down Expand Up @@ -297,7 +301,9 @@ export const handleAgnaistic: ModelAdapter = async function* (opts) {
}
}

// await releaseLock(lockId)
if (+srv.lockSeconds > 0) {
await releaseLock(lockId)
}

const parsed = sanitise((result || accumulated).replace(prompt, ''))
const trimmed = trimResponseV2(parsed, opts.replyAs, members, opts.characters, ['END_OF_DIALOG'])
Expand Down
1 change: 1 addition & 0 deletions srv/adapter/payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function getBasePayload(opts: AdapterProps, stops: string[] = []) {
placeholders: opts.placeholders,
lists: opts.lists,
previous: opts.previous,
json_schema_v2: json_schema,
json_schema,
imageData: opts.imageData,
}
Expand Down
2 changes: 2 additions & 0 deletions srv/api/chat/characters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const upsertTempCharacter = handle(async ({ body, params, userId }) => {
visualType: 'string?',
sprite: 'any?',
culture: 'string?',
json: 'any?',
},
body
)
Expand Down Expand Up @@ -123,6 +124,7 @@ export const upsertTempCharacter = handle(async ({ body, params, userId }) => {
visualType: body.visualType,
sprite: body.sprite,
culture: body.culture,
json: body.json,
}

tempCharacters[upserted._id] = upserted
Expand Down
4 changes: 2 additions & 2 deletions srv/api/chat/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const generateMessageV2 = handle(async (req, res) => {
}

// Coalesce for backwards compatibly while new UI rolls out
const replyAs = body.replyAs._id.startsWith('temp-')
const replyAs: AppSchema.Character = body.replyAs._id.startsWith('temp-')
? body.replyAs
: await store.characters.getCharacter(chat.userId, body.replyAs._id || body.char._id)

Expand Down Expand Up @@ -249,7 +249,7 @@ export const generateMessageV2 = handle(async (req, res) => {
res.json({ requestId, success: true, generating: true, message: 'Generating message', messageId })

const entities = await getResponseEntities(chat, body.sender.userId, body.settings)
const schema = entities.gen.jsonSource === 'character' ? entities.char.json : entities.gen.json
const schema = entities.gen.jsonSource === 'character' ? replyAs.json : entities.gen.json
const hydrator = entities.gen.jsonEnabled && schema ? jsonHydrator(schema) : undefined

let hydration: HydratedJson | undefined
Expand Down
1 change: 1 addition & 0 deletions srv/db/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export async function getServerConfiguration() {
modPrompt: '',
modSchema: [],
actionCalls: [],
lockSeconds: 0,
}

await db('configuration').insertOne(next)
Expand Down
93 changes: 65 additions & 28 deletions web/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
Sliders,
Speaker,
Sun,
VenetianMask,
Volume2,
VolumeX,
Wand2,
Expand Down Expand Up @@ -62,6 +61,7 @@ import { Badge } from './shared/Card'
import { navStore } from './subnav'
import { getRgbaFromVar } from './shared/colors'
import { CallToAction } from './shared/CallToAction'
import Button from './shared/Button'

const Navigation: Component = () => {
let parent: any
Expand Down Expand Up @@ -119,6 +119,17 @@ const Navigation: Component = () => {
return true
})

const sha = createMemo(() => {
const apiSha = state.config.version.startsWith('development')
? 'dev'
: state.config.version.slice(0, 4)
const webSha = window.agnai_version.startsWith('{{')
? ''
: `/ ${window.agnai_version.slice(0, 4)}`

return `${apiSha} ${webSha}`
})

return (
<>
<Show when={!state.showMenu && dismissable()}>
Expand All @@ -144,7 +155,7 @@ const Navigation: Component = () => {
>
<div
ref={content}
class="drawer__content sm:text-md text-md flex flex-col gap-0 px-2 sm:gap-1"
class="drawer__content sm:text-md text-md flex flex-col gap-1 px-2 sm:gap-1"
>
<div class="flex w-full items-center justify-between">
<div
Expand Down Expand Up @@ -176,16 +187,18 @@ const Navigation: Component = () => {
</Show>

<div class="flex w-2/12 justify-end">
<Show when={nav.body && subnav()}>
<div class="icon-button" onClick={() => setSubnav(false)}>
<ChevronLeft />
</div>
</Show>
<Show when={nav.body && !subnav()}>
<div class="icon-button" onClick={() => setSubnav(true)}>
<ChevronRight />
</div>
</Show>
<Switch>
<Match when={nav.body && subnav()}>
<div class="icon-button" onClick={() => setSubnav(false)}>
<ChevronLeft />
</div>
</Match>
<Match when={nav.body && !subnav()}>
<div class="icon-button" onClick={() => setSubnav(true)}>
<ChevronRight />
</div>
</Match>
</Switch>
</div>
</div>

Expand Down Expand Up @@ -215,7 +228,7 @@ const Navigation: Component = () => {
}
}
>
<SubCTA patreon={state.config.patreon} />
<SubCTA />
<Show when={state.config.policies}>
<div class="text-500 flex w-full justify-center gap-4 text-xs">
<div>
Expand All @@ -227,7 +240,7 @@ const Navigation: Component = () => {
</div>
</Show>
<div class="text-500 mb-1 text-[0.6rem] italic" role="contentinfo" aria-label="Version">
{state.config.version}
{sha()}
</div>
</div>
</div>
Expand Down Expand Up @@ -507,10 +520,13 @@ const Item: Component<{
<Tooltip position="top" tip={props.tooltip}>
<Show when={!props.href}>
<div
class={`flex min-h-[2.5rem] cursor-pointer items-center justify-start gap-4 rounded-lg px-2 hover:bg-[var(--bg-700)] sm:min-h-[2.5rem] ${
class={`flex cursor-pointer items-center justify-start gap-4 rounded-lg px-2 hover:bg-[var(--bg-700)] ${
props.class || ''
}`}
classList={{ 'gap-4': !props.class?.includes('gap-') }}
classList={{
'gap-4': !props.class?.includes('gap-'),
'min-h-[2.25rem]': !props.class?.includes('h-'),
}}
onClick={onItemClick(props.onClick)}
tabindex={0}
role="button"
Expand All @@ -522,9 +538,12 @@ const Item: Component<{
<Show when={props.href}>
<A
href={props.href!}
class={`flex min-h-[2.5rem] items-center justify-start gap-4 rounded-lg px-2 hover:bg-[var(--bg-700)] sm:min-h-[2.5rem] ${
class={`flex items-center justify-start gap-4 rounded-lg px-2 hover:bg-[var(--bg-700)] ${
props.class || ''
}`}
classList={{
'min-h-[2.25rem]': !props.class?.includes('h-'),
}}
onClick={onItemClick(props.onClick)}
role="button"
aria-label={props.ariaLabel}
Expand Down Expand Up @@ -671,7 +690,7 @@ export const UserProfile = () => {
<div
class="grid w-full items-center justify-between gap-2"
style={{
'grid-template-columns': '1fr 30px',
'grid-template-columns': '1fr max-content',
}}
>
<Item
Expand Down Expand Up @@ -700,18 +719,19 @@ export const UserProfile = () => {
<span aria-hidden="true">{chars.impersonating?.name || user.profile?.handle}</span>
</Item>
<div class="flex items-center">
<a
href="#"
role="button"
<Button
class="text-600 text-xs"
schema="secondary"
size="sm"
aria-label="Open impersonation menu"
class="icon-button"
onClick={() => {
settingStore.toggleImpersonate(true)
if (menu.showMenu) settingStore.closeMenu()
}}
>
<VenetianMask aria-hidden="true" />
</a>
Persona
{/* <VenetianMask aria-hidden="true" /> */}
</Button>
</div>
</div>
</>
Expand All @@ -726,6 +746,14 @@ const MultiItem: Component<{ children: any }> = (props) => {
)
}

const DoubleItem: Component<{ children: any }> = (props) => {
return (
<div class="grid w-full gap-2" style={{ 'grid-template-columns': '1fr 1fr' }}>
{props.children}
</div>
)
}

const EndItem: Component<{ children: any }> = (props) => {
return <div class="flex items-center">{props.children}</div>
}
Expand Down Expand Up @@ -761,21 +789,30 @@ export const Nav = {
Item,
MultiItem,
SubItem,
DoubleItem,
}

export const SubCTA: Component<{ patreon: boolean | undefined }> = (props) => {
export const SubCTA: Component<{
width?: 'fit' | 'full'
children?: any
onClick?: () => void
}> = (props) => {
const settings = settingStore()
const [, setSearch] = useSearchParams()

const openSubPage = () => {
setSearch({ profile_tab: 'subscription' })
userStore.modal(true)
props.onClick?.()
}

return (
<Show when={props.patreon}>
<CallToAction theme="hl" targets={['guests', 'users']} width="fit">
<Show when={settings.config.patreon}>
<CallToAction theme="hl" targets={['guests', 'users']} width={props.width || 'fit'}>
<div class="flex cursor-pointer justify-center text-center text-sm" onClick={openSubPage}>
Subscribe for higher quality chats and no ads
<Show when={props.children} fallback={<>Subscribe for higher quality chats and no ads</>}>
{props.children}
</Show>
</div>
</CallToAction>
</Show>
Expand Down
8 changes: 8 additions & 0 deletions web/pages/Admin/Config/General.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export const General: Component<{ slots: Accessor<string>; setSlots: Setter<stri
helperText="Markdown is supported"
value={state.config?.maintenanceMessage}
/>

<TextInput
fieldName="lockSeconds"
type="number"
label="Lock Duration (seconds)"
helperText="Maximum TTL of user-level lock - Set to zero (0) to disable"
value={state.config?.lockSeconds ?? 0}
/>
</Card>

<Card bg="bg-500">
Expand Down
1 change: 1 addition & 0 deletions web/pages/Admin/Configuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const ServerConfiguration: Component = () => {
modPrompt: 'string',
modFieldPrompt: 'string',
charlibGuidelines: 'string',
lockSeconds: 'number',
charlibPublish: ['off', 'users', 'subscribers', 'moderators', 'admins'],
})

Expand Down
24 changes: 17 additions & 7 deletions web/pages/Admin/SubscriptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ import { Page } from '/web/Layout'
const SubscriptionList: Component = () => {
setComponentPageTitle('Subscriptions')
const nav = useNavigate()
const state = presetStore((s) => ({
subs: s.subs
.map((pre) => ({ ...pre, label: `[${getServiceName(pre.service)}] ${pre.name}` }))
.sort(sortByLabel),
}))
const state = presetStore((s) => {
return {
enabled: s.subs
.filter((s) => !s.subDisabled)
.map((pre) => ({ ...pre, label: `[${getServiceName(pre.service)}] ${pre.name}` }))
.sort(sortByLabel),
disabled: s.subs
.filter((s) => s.subDisabled)
.map((pre) => ({ ...pre, label: `[${getServiceName(pre.service)}] ${pre.name}` }))
.sort(sortByLabel),
}
})

const cfg = userStore()

Expand Down Expand Up @@ -113,8 +120,8 @@ const SubscriptionList: Component = () => {
</div>
</Show>
<Divider />
<div class="flex justify-center font-bold">Subscription Presets</div>
<For each={state.subs}>
<div class="flex justify-center font-bold">Models</div>
<For each={state.enabled.concat(state.disabled)}>
{(sub) => (
<div class="flex w-full items-center gap-2">
<A
Expand All @@ -133,6 +140,9 @@ const SubscriptionList: Component = () => {
[Level: {sub.subLevel}] {getServiceName(sub.service)}
</span>
{sub.name}
<Show when={sub.description}>
<span class="text-500 ml-1 text-xs">{sub.description}</span>
</Show>
<span class="mr-1 text-xs italic text-[var(--text-600)]">
{sub.isDefaultSub ? ' default' : ''}
{sub.subDisabled ? ' (disabled)' : ''}
Expand Down
Loading

0 comments on commit f5f37ab

Please sign in to comment.