File size: 8,928 Bytes
801f06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { UnavailableFeature } from '../../shared/components/unavailable-feature';
import { useCapabilities, unavailable } from '../../shared/api/use-capabilities';
import { useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { api, assetUrl } from '../../shared/api/client';
import type { Character, CharacterInput, Profile } from '../../shared/api/types';
import { Modal, ErrorNotice } from '../../shared/components/ui';
import { AssetUpload } from '../../shared/components/asset-upload';
export const defaultProfile: Profile = {
  personality: '',
  region: 'Creator defined',
  vocabulary: '',
  aave: 'Creator defined; no automatic dialect',
  slang: 0,
  pronunciation: '',
  cadence: 'Natural',
  code_switching: 'Context dependent',
  expressiveness: 0.5,
  pace: 1,
  gestures: 'Subtle conversational gestures',
  voice_notes: '',
};
export function CharacterEditor({
  character,
  onClose,
}: {
  character?: Character;
  onClose: () => void;
}) {
  const capabilities = useCapabilities();
  const [value, setValue] = useState<CharacterInput>(
    character
      ? { ...character, profile: { ...defaultProfile, ...character.profile } }
      : {
          name: '',
          description: '',
          color: '#b9ace8',
          profile: defaultProfile,
          image_asset_id: null,
          voice_asset_id: null,
          archived: false,
        },
  );
  const [tab, setTab] = useState('Identity'),
    [busy, setBusy] = useState(false),
    [error, setError] = useState<unknown>();
  const query = useQueryClient();
  const profile = (key: keyof Profile, v: string | number) =>
    setValue({ ...value, profile: { ...value.profile, [key]: v } });
  async function save(e: React.FormEvent) {
    e.preventDefault();
    setBusy(true);
    setError(null);
    try {
      await api.saveCharacter(value, character?.id);
      await query.invalidateQueries({ queryKey: ['characters'] });
      onClose();
    } catch (e) {
      setError(e);
    } finally {
      setBusy(false);
    }
  }
  return (
    <Modal title={character ? 'Edit character' : 'Create a character'} onClose={onClose}>
      <form onSubmit={save}>
        <div className="segmented">
          {['Identity', 'Speech style', 'Performance', 'Assets'].map((t) => (
            <button
              type="button"
              className={tab === t ? 'active' : ''}
              key={t}
              onClick={() => setTab(t)}
            >
              {t}
            </button>
          ))}
        </div>
        <div className="modal-body">
          {tab === 'Identity' && (
            <>
              <div className="field-row">
                <label>
                  Name
                  <input
                    required
                    maxLength={120}
                    value={value.name}
                    onChange={(e) => setValue({ ...value, name: e.target.value })}
                  />
                </label>
                <label className="color-field">
                  Accent
                  <input
                    type="color"
                    value={value.color}
                    onChange={(e) => setValue({ ...value, color: e.target.value })}
                  />
                </label>
              </div>
              <label>
                Short description
                <input
                  value={value.description}
                  onChange={(e) => setValue({ ...value, description: e.target.value })}
                />
              </label>
              <label>
                Personality
                <textarea
                  rows={5}
                  value={value.profile.personality}
                  onChange={(e) => profile('personality', e.target.value)}
                  placeholder="Their point of view, humor, interests and how they relate to others…"
                />
              </label>
              <label className="check">
                <input
                  type="checkbox"
                  checked={value.archived}
                  onChange={(e) => setValue({ ...value, archived: e.target.checked })}
                />
                Archive character
              </label>
            </>
          )}
          {tab === 'Speech style' && (
            <UnavailableFeature
              reason={!capabilities.dialogue ? unavailable.speechStyle : undefined}
            >
              <p className="hint">
                Speech preferences belong to the character, not their ethnicity. Dialect controls
                require listening review.
              </p>
              {(
                [
                  ['region', 'Regional influence'],
                  ['vocabulary', 'Preferred vocabulary'],
                  ['aave', 'AAVE preferences'],
                  ['pronunciation', 'Pronunciation notes'],
                  ['cadence', 'Cadence'],
                  ['code_switching', 'Code-switching'],
                ] as const
              ).map(([k, label]) => (
                <label key={k}>
                  {label}
                  <input value={value.profile[k]} onChange={(e) => profile(k, e.target.value)} />
                </label>
              ))}
              <label>
                Slang preference <span className="range-value">{value.profile.slang}%</span>
                <input
                  type="range"
                  min="0"
                  max="100"
                  value={value.profile.slang}
                  onChange={(e) => profile('slang', +e.target.value)}
                />
              </label>
            </UnavailableFeature>
          )}
          {tab === 'Performance' && (
            <UnavailableFeature
              reason={!capabilities.animation ? unavailable.performance : undefined}
            >
              <label>
                Expressiveness{' '}
                <span className="range-value">
                  {Math.round(value.profile.expressiveness * 100)}%
                </span>
                <input
                  type="range"
                  min="0"
                  max="1"
                  step="0.05"
                  value={value.profile.expressiveness}
                  onChange={(e) => profile('expressiveness', +e.target.value)}
                />
              </label>
              <label>
                Speaking pace <span className="range-value">{value.profile.pace.toFixed(2)}×</span>
                <input
                  type="range"
                  min="0.8"
                  max="1.2"
                  step="0.05"
                  value={value.profile.pace}
                  onChange={(e) => profile('pace', +e.target.value)}
                />
              </label>
              <label>
                Gestures and listener reactions
                <textarea
                  value={value.profile.gestures}
                  onChange={(e) => profile('gestures', e.target.value)}
                />
              </label>
              <UnavailableFeature
                reason={capabilities.animation ? unavailable.voiceDirection : undefined}
              >
                <label>
                  Voice direction
                  <textarea
                    value={value.profile.voice_notes}
                    onChange={(e) => profile('voice_notes', e.target.value)}
                  />
                </label>
              </UnavailableFeature>
              <p className="hint">
                Pace and expressiveness apply to speech. Gesture prompts guide video generation;
                exact motion and pronunciation need review.
              </p>
            </UnavailableFeature>
          )}
          {tab === 'Assets' && (
            <>
              <h3>Character image</h3>
              {value.image_asset_id && (
                <img
                  className="asset-preview"
                  src={assetUrl(value.image_asset_id)}
                  alt="Saved character"
                />
              )}
              <AssetUpload
                kind="image"
                onUploaded={(a) => setValue({ ...value, image_asset_id: a.id })}
              />
              <h3>Authorized voice</h3>
              {value.voice_asset_id && <audio controls src={assetUrl(value.voice_asset_id)} />}
              <AssetUpload
                kind="voice"
                onUploaded={(a) => setValue({ ...value, voice_asset_id: a.id })}
              />
            </>
          )}
          <ErrorNotice error={error} />
        </div>
        <footer className="modal-footer">
          <button type="button" className="button secondary" onClick={onClose}>
            Cancel
          </button>
          <button className="button primary" disabled={busy || !value.name.trim()}>
            {busy ? 'Saving…' : 'Save character'}
          </button>
        </footer>
      </form>
    </Modal>
  );
}