Skip to content

Studio: characters, personas and group chats, with character card import - #16729

Open
oobabooga wants to merge 153 commits into
unslothai:mainfrom
oobabooga:studio-character-chat
Open

oobabooga wants to merge 153 commits into
unslothai:mainfrom
oobabooga:studio-character-chat

Conversation

@oobabooga

@oobabooga oobabooga commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Adds character chat to Studio: a library of characters you can talk to, alone or in a group, with import and export of the standard character card formats (V1, V2 and V3 as PNG or JSON, and CHARX).

Rin and Leo in a group chat, visual novel view

What it adds

  • Characters page (/characters, in the sidebar's More menu and the command palette): a portrait grid with search, tags, favorites and sorting, import by file picker or drag and drop, and Select mode to start a group chat.
  • Two original starter characters: Leo, an AI tutor for any language, and Rin Takeda, a cyberpunk netrunner. Leo's card replaces the roleplay instructions with its own: it asks for the language and level, replies in that language with translations for beginners, lists at most three corrections and reuses new words later. Both portraits were made with Z-Image-Turbo (Apache-2.0).
  • Personas: who you are in character chats (name, picture, description), with a default for new chats.
  • Profile and editor pages. The editor leads with name, picture, tags, description and greetings; personality, example dialogue, prompt overrides, lorebook and talkativeness are under "More details", next to the count of tokens always sent.
  • Character chats are ordinary Studio chats, with:
    • every message under its speaker's portrait, name and time, and alternate greetings as branches;
    • a bar to let a character go on, pick who replies next in a group, or draft your own next line (Write my reply);
    • a Characters section in the run settings: cast, reply order, persona, scene, author's note, and View prompt;
    • three views per chat: plain, Stage (the speaker's portrait beside the chat) and Visual novel (the cast in the scene, the conversation in a panel below).
  • Group reply orders: Natural (default: whoever is named answers, otherwise one speaker picked by talkativeness), Everyone, and One at a time.
  • Ordinary chats are unchanged. Character chats start with tools, web search, code execution, research and RAG off; each can be turned on per chat.

Screenshots

The replies are live, from Qwen3.8-27B (UD-Q4_K_M). Every character, the persona (Alex) and the visual novel background were made for this PR.

Stage view

Stage view with Rin

A group chat: characters answer each other as well as you. Here Rin brings in Leo to get Alex's Spanish ready for a job.

Group chat with Rin and Leo

A lesson with Leo

Spanish lesson with Leo

The Characters page

Characters page

A character's profile

Rin's profile

The editor

Editing Rin

Existing menus that changed

Before is the merge base (e27c6806ad), after is this PR.

Sidebar More menu

Composer + menu, More

Command palette

How it works

Storage (storage/characters_db.py): four new tables in each account's studio.db: characters, personas, chat_cast (a chat's members, order and mute) and chat_scene (its persona and settings). chat_threads and chat_messages are not changed. Deleting a chat removes its cast and scene; deleting a character keeps its chats. A fork copies cast and scene in the same transaction. Each character keeps its original card, so an export carries the fields Studio does not edit. Portraits are files next to the database, served by authenticated routes. Starters are recorded in character_starters when added, so each is added once per account and a deleted one stays deleted.

Cards (core/characters/cards.py): reads ccv3 before chara from PNG text chunks, JSON and CHARX, and exports a PNG with V2 and V3 chunks, or V3 JSON. Files that are not cards are refused with a reason. Cards are untrusted input: decompression stays within one card-size budget and tags are clamped to the editor's limits.

Routes (routes/characters.py, /api/characters): characters, import and export, portraits, personas, and POST /chats, GET and PUT /chats/{thread_id} for a chat's cast and scene. PUT applies one edit on top of what is stored (settings merge; members are added, removed or muted by id), so edits from two tabs compose.

Chat structure: a character chat is created on the server with a hidden opening user turn and each greeting as a reply under it, so alternate greetings are ordinary branches. In a group, a hidden turn message precedes each member's reply. Every reply therefore has a user parent, which durable chat runs require, and strict templates (Mistral, Gemma 2) keep alternating. Each reply stores its speaker in metadata.roleplay, so the speaker survives a Stop, a reload, or a run that finishes with no tab open.

Prompt (features/characters/prompt/, pure modules with tests): one system block in a fixed order (instructions, lore, characters, persona, scenario, example dialogue) that does not depend on who speaks next, so llama.cpp's prompt cache holds when the speaker changes. Depth notes and final instructions ride late user turns, because the backend merges system messages into the first. Lorebooks support keywords, /regex/ keys, secondary keys, constant entries, scan depth and a token budget; regex keys come from imported cards, so they run on a linear-time matcher. In groups, history lines carry the speaker's name and stop strings cover the other speakers.

Compaction: on local models (GGUF, MLX, NPU and managed engines), character chats use the rolling context policy: the oldest turns are dropped while the cards stay, and the closest archived turns are quoted back silently. The checkpoint default would turn earlier user messages into standing instructions and show its recall as a tool call inside the character's reply. On a cloud connection a character chat compacts like any other chat there, which since #17743 means a summary of the older turns.

Outside the feature folder: hooks in the chat adapter (prompt, stop strings, speaker), the thread (hidden turns, message frames), run settings, chat header, sidebar, composer + menu, router and command palette. The image viewer now closes when a picture shown at fit size is clicked. Strings are in all 24 locales. One fix also reaches ordinary chats: Mistral templates require nine-character alphanumeric tool-call ids, so web search failed with Mistral models; replayed history now maps ids when the template checks them (core/inference/chat_template_helpers.py).

Limitations

  • No shared lorebooks, chat log import, expression sprites or per-character voices.
  • A group's system block carries every member's card.
  • Lorebook regex keys with backreferences or lookaround have no linear-time form and are skipped.
  • External providers are not sent stop strings; Studio cuts their streamed reply at them instead, so a non-streamed reply from one is not cut.
  • Once a group chat's context fills, its saved compaction boundary is not found, because the history it sends carries speaker names the stored messages lack: each later turn re-processes the history instead of reusing llama.cpp's prompt cache. One-character chats keep their boundary.
  • Chat history export and restore keeps a character chat's messages but not its cast and scene, and "Use chats as training data" leaves character chats out.

Testing

On the current head (67c432a1f6, which merges main at f49006976f):

  • Backend: tests/test_characters.py (61), tests/test_mistral_tool_call_ids.py (5), tests/test_chat_generation_runs.py (75) and tests/multi_account (1279, of which 96 are character and chat-scene cases) pass.
  • Frontend: tests/characters-prompt.test.ts (23) and tests/auto-compaction-settings.test.ts (18) pass; typecheck, build, bundle:check and i18n:check:strict pass. The full frontend suite's 12 failures also fail on main.

On the opening commit (7e2df2c): tests/multi_account with the schema, chat history, chat storage, fork and account lifecycle suites, 2606 passed; i18n:check:strict, build and bundle:check passed; the full frontend suite's 15 failures also fail on the merge base.

Live, with Mistral-Nemo-Instruct-2407, driven in Chromium: importing public cards in every supported format (one with 350 lore entries); one-on-one and group chats with regenerate, alternate greetings, Write my reply, both reply orders, and Stop mid-round followed by a reload; fork, export and re-import; compaction at a 3,072-token context over nine long turns; web search with Mistral before and after the id fix; the three views in both themes and at phone width, and the stage view at window widths from 360 to 3840 px.

The starter characters were checked live with Qwen3.8-27B: a one-on-one Spanish lesson with Leo (corrections of ser and estar and of gustar), a group chat with Rin and Leo, and Rin one-on-one.

… 3:4 reply portraits, and a friendlier Characters icon
…d characters answer, titles and surnames count
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T06:22:03.726070Z 44ca12b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 95ab598244

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 6d1ce5664d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

LeoBorcherding and others added 6 commits November 20, 2026 21:20
…r memory

- Cloud providers are never sent stop sequences, so a character reply from one ran on into the
  next speaker's line. The provider stream now cuts at them; connection-model routes keep their
  own filter, and /v1/messages no longer passes the stop twice.
- Avatars use the shared, reference-counted object URL cache (moved from the library to lib/),
  so a picture on screen is never revoked and the rest stay within its budget; library cards
  fetch a picture only once near the screen (useSeen, moved to hooks/).
- A send re-checks the cast at most once per chat every 30 s, so a group round asks once.
- A resumed group reply is replayed with its speaker's name, like every other group reply.
- Click-to-close in the image viewer is opt-in (avatars), not a change to every image.
- The avatar upload routes look up the row off the event loop.
- A character's chat list says when older chats were left out.
- The python-picture export test gets the character transcript helper its siblings already had.
# Conflicts:
#	studio/frontend/tests/characters-wiring.test.ts
The harness stubbed prepareCharacterRun but not prepareCharacterCountRun, which the count path calls; its dependency resolver pulled the real one in without the countRuns cache it uses, so two cases failed with ReferenceError on every OS.
The python floor gate counts studio files that evaluate PEP 604 unions on 3.9; this test file added one (dict | None).
@danielhanchen

Copy link
Copy Markdown
Member

The latest run on 95c07fe1 (merged onto main at 1d83925d62) has three reds that come from this PR's own files. They pass on main:

  1. Core (HF=default + TRL=default) and Core (HF=latest + TRL=latest): tests/test_python39_compatibility.py::test_studio_evaluated_unions_do_not_grow reports studio/backend/tests/test_character_cards.py as a new file that evaluates PEP 604 unions on Python 3.9. Adding from __future__ import annotations at the top of that file clears it (the test asks not to raise the ratchet).
  2. Repo tests (CPU, studio): tests/studio/test_tauri_branding_contract.py::test_desktop_surfaces_do_not_restore_studio_branding flags 49 new characters.* strings in the locale overlays (first one ar.ts::characters.editor.systemPromptHint). The desktop surfaces must not say "Studio"; the test's full diff lists each key.
  3. Repo tests (CPU, studio): tests/studio/test_deep_research_frontend_contract.py::test_research_presentation_is_integrated fails with IndexError at thread.split("const UserActionBar: FC = () =>", 1)[1], so the thread component no longer contains const UserActionBar: FC = () => in the form the contract test reads. Either keep that declaration shape or update the contract test alongside the change.

pip scan-packages :: hf-stack was a repo-wide failure from the unsloth-zoo 2026.11.8 release and is fixed on main by #18724; it will clear on the next push or rebase.

The desktop branding contract on main forbids "Unsloth Studio" in display strings; the system prompt hint and the imported-card note used it in every locale.
chat-export-python-images.test.ts evaluates the export loader, which now calls characterChatTranscript, so its context provides it as chat-export-html-images.test.ts does. The research contract sliced thread.tsx at "const UserActionBar: FC = () =>", which this PR changed to take showTime.
The restructure made the bottom spacer's aui-thread-bottom-spacer class unconditional again, so every chat's markup grew by 25 characters and UI parity failed. Only character chats get it now, as d5ba349 had it, with a test.
# Conflicts:
#	studio/frontend/tests/characters-wiring.test.ts
@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: f1be33b255

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 44ca12bdd1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Conflicts:
#	studio/backend/routes/chat_generation_runs.py
#	studio/frontend/tests/chat-export-python-images.test.ts

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants