Spaces:
Running
Running
Download app.ts from Museris/rpkg-character-builder: direct link, hf CLI and curl.
- Browser
- Download file 8.8 kB
-
https://huggingface.co/spaces/Museris/rpkg-character-builder/resolve/main/app.ts
- Command line
-
hf download hf://spaces/Museris/rpkg-character-builder/app.ts
-
curl -L -o app.ts https://huggingface.co/spaces/Museris/rpkg-character-builder/resolve/main/app.ts
8.8 kB
| import {strToU8, zipSync} from 'fflate' | |
| const form = document.querySelector('#builder') | |
| const name = document.querySelector('#name') | |
| const tagline = document.querySelector('#tagline') | |
| const definition = document.querySelector('#definition') | |
| const greeting = document.querySelector('#greeting') | |
| const portrait = document.querySelector('#portrait') | |
| const previewPortrait = document.querySelector('#preview-portrait') | |
| const alternatives = document.querySelector('#alternatives') | |
| const status = document.querySelector('#status') | |
| const download = document.querySelector('#download') | |
| const removePortrait = document.querySelector('#remove-portrait') | |
| if ( | |
| !(form instanceof HTMLFormElement) || !(name instanceof HTMLInputElement) | |
| || !(tagline instanceof HTMLInputElement) || !(definition instanceof HTMLTextAreaElement) | |
| || !(greeting instanceof HTMLTextAreaElement) || !(portrait instanceof HTMLInputElement) | |
| || !(previewPortrait instanceof HTMLImageElement) || !(alternatives instanceof HTMLElement) | |
| || !(status instanceof HTMLElement) || !(download instanceof HTMLButtonElement) | |
| || !(removePortrait instanceof HTMLButtonElement) | |
| ) { | |
| throw new Error('Character builder markup is incomplete') | |
| } | |
| let avatar: string | null = null | |
| let portraitVersion = 0 | |
| let loadingPortrait = false | |
| let openingNumber = 0 | |
| const updatePreview = () => { | |
| const previewName = document.querySelector('#preview-name') | |
| const previewTagline = document.querySelector('#preview-tagline') | |
| const previewGreeting = document.querySelector('#preview-greeting') | |
| if (previewName) previewName.textContent = name.value.trim() || 'Character name' | |
| if (previewTagline) previewTagline.textContent = tagline.value.trim() || 'Character tagline' | |
| if (previewGreeting) { | |
| previewGreeting.textContent = greeting.value.trim() | |
| || 'Your opening message will appear here.' | |
| } | |
| } | |
| const clearPortrait = () => { | |
| portraitVersion++ | |
| avatar = null | |
| portrait.value = '' | |
| previewPortrait.removeAttribute('src') | |
| previewPortrait.hidden = true | |
| const placeholder = document.querySelector('#portrait-placeholder') | |
| if (placeholder instanceof HTMLElement) placeholder.hidden = false | |
| removePortrait.hidden = true | |
| loadingPortrait = false | |
| download.disabled = false | |
| } | |
| form.addEventListener('input', () => { | |
| for (const field of [name, definition, greeting]) field.setCustomValidity('') | |
| status.textContent = '' | |
| updatePreview() | |
| }) | |
| portrait.addEventListener('change', async () => { | |
| const file = portrait.files?.[0] | |
| clearPortrait() | |
| status.textContent = '' | |
| if (!file) return | |
| if (!['image/png', 'image/jpeg', 'image/webp'].includes(file.type) || file.size > 5 * 1024 * 1024) { | |
| status.textContent = 'Choose a PNG, JPEG, or WebP portrait smaller than 5 MB.' | |
| return | |
| } | |
| const version = portraitVersion | |
| loadingPortrait = true | |
| download.disabled = true | |
| try { | |
| // Decode the image as well as checking its declared MIME type. | |
| const bitmap = await createImageBitmap(file) | |
| bitmap.close() | |
| const dataUrl = await new Promise<string>((resolve, reject) => { | |
| const reader = new FileReader() | |
| reader.onload = () => | |
| typeof reader.result === 'string' | |
| ? resolve(reader.result) | |
| : reject(new Error('Unable to read portrait')) | |
| reader.onerror = () => reject(new Error('Unable to read portrait')) | |
| reader.readAsDataURL(file) | |
| }) | |
| if (version !== portraitVersion) return | |
| avatar = dataUrl | |
| previewPortrait.src = dataUrl | |
| previewPortrait.hidden = false | |
| const placeholder = document.querySelector('#portrait-placeholder') | |
| if (placeholder instanceof HTMLElement) placeholder.hidden = true | |
| removePortrait.hidden = false | |
| } catch { | |
| if (version === portraitVersion) { | |
| status.textContent = 'That image could not be opened. Please choose another portrait.' | |
| } | |
| } finally { | |
| if (version === portraitVersion) { | |
| loadingPortrait = false | |
| download.disabled = false | |
| } | |
| } | |
| }) | |
| removePortrait.addEventListener('click', clearPortrait) | |
| document.querySelector('#add-greeting')?.addEventListener('click', () => { | |
| openingNumber++ | |
| const row = document.createElement('div') | |
| row.className = 'alternative' | |
| const label = document.createElement('label') | |
| label.htmlFor = `opening-${openingNumber}` | |
| label.textContent = 'Alternative opening' | |
| const field = document.createElement('textarea') | |
| field.id = label.htmlFor | |
| field.rows = 4 | |
| field.placeholder = 'Another way your story could begin…' | |
| const remove = document.createElement('button') | |
| remove.type = 'button' | |
| remove.className = 'text-button' | |
| remove.textContent = 'Remove opening' | |
| remove.addEventListener('click', () => row.remove()) | |
| row.append(label, field, remove) | |
| alternatives.append(row) | |
| field.focus() | |
| }) | |
| document.querySelector('#example')?.addEventListener('click', () => { | |
| if ( | |
| (name.value || tagline.value || definition.value || greeting.value || alternatives.children.length || avatar) | |
| && !window.confirm('Replace your current character with the example?') | |
| ) return | |
| clearPortrait() | |
| alternatives.replaceChildren() | |
| name.value = 'Nyx Calder' | |
| tagline.value = 'A starship mechanic with a secret worth keeping' | |
| definition.value = | |
| `Nyx Calder keeps the last repair dock on the edge of settled space running with salvaged parts and stubborn optimism. She is observant, dryly funny, and fiercely protective of anyone stranded far from home. She masks her worry with jokes and talks to broken engines as though they are old friends. | |
| Once a celebrated navigation officer, Nyx left the fleet after refusing an order to abandon a civilian ship. She keeps her old insignia hidden beneath her workbench. She wants to restore a derelict courier and see the stars again, but cannot bring herself to leave the people who depend on her. | |
| Speak with warm, understated wit and concrete sensory detail. Reveal her past gradually through conversation. Leave the other participant's thoughts, choices, and dialogue to them. | |
| The story begins when a traveler arrives at her dock carrying a damaged navigation core that broadcasts a signal from a supposedly lost ship.` | |
| greeting.value = | |
| `The repair dock smells of hot metal and overbrewed coffee. Beneath a half-disassembled engine, a wrench clatters to the floor. | |
| “If that's another coolant leak, I'm officially on my lunch break.” Nyx slides out, then notices the flickering core in your hands. Her smile fades. | |
| She sets the wrench down very carefully. “Where did you get that?”` | |
| for (const field of [name, definition, greeting]) field.setCustomValidity('') | |
| status.textContent = 'Example loaded. Edit the fields or download the character.' | |
| updatePreview() | |
| }) | |
| form.addEventListener('submit', (event) => { | |
| event.preventDefault() | |
| if (loadingPortrait) return | |
| for (const field of [name, definition, greeting]) { | |
| field.setCustomValidity(field.value.trim() ? '' : 'Please add some text here.') | |
| } | |
| if (!form.reportValidity()) return | |
| try { | |
| const now = new Date() | |
| const id = crypto.randomUUID() | |
| const character = { | |
| id, | |
| lastUpdate: now.getTime(), | |
| name: name.value.trim(), | |
| definition: definition.value.trim(), | |
| greetings: [ | |
| greeting.value.trim(), | |
| ...Array.from(alternatives.querySelectorAll('textarea'), field => field.value.trim()).filter(Boolean), | |
| ], | |
| tagline: tagline.value.trim(), | |
| avatar, | |
| } | |
| const archive = zipSync({ | |
| mimetype: [strToU8('application/vnd.museris.rpkg'), {level: 0}], | |
| 'manifest.json': strToU8(JSON.stringify({version: '1.0', exportedAt: now.toISOString()}, null, 2)), | |
| [`characters/${id}.json`]: strToU8(JSON.stringify(character, null, 2)), | |
| }) | |
| const url = URL.createObjectURL(new Blob([new Uint8Array(archive)], {type: 'application/vnd.museris.rpkg'})) | |
| const link = document.createElement('a') | |
| link.href = url | |
| link.download = `${character.name.replace(/[^a-z0-9]/gi, '_').toLowerCase() || 'character'}.rpkg` | |
| document.body.append(link) | |
| link.click() | |
| link.remove() | |
| setTimeout(() => URL.revokeObjectURL(url), 60_000) | |
| status.textContent = 'Your character file is ready. Import the downloaded .rpkg file into Museris.' | |
| } catch { | |
| status.textContent = 'Your character file could not be created. Please try again.' | |
| } | |
| }) | |