@2kog/kacha 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +57 -2
  2. package/bin/init-project.js +63 -0
  3. package/bin/kacha.js +51 -0
  4. package/index.html +5 -0
  5. package/package.json +45 -4
  6. package/server/asset-directories.js +65 -0
  7. package/server/asset-watch.js +35 -0
  8. package/server/character-assets.js +399 -0
  9. package/server/image-assets.js +652 -0
  10. package/server/index.js +15 -0
  11. package/server/project-brand.js +56 -0
  12. package/server/project-paths.js +4 -0
  13. package/server/project-settings.js +55 -0
  14. package/src/App.vue +34 -0
  15. package/src/assets/css/workbench.css +272 -0
  16. package/src/assets/img/rabbit-mark.svg +1 -0
  17. package/src/components/AssetDeleteButton.vue +53 -0
  18. package/src/components/AssetHistory.vue +40 -0
  19. package/src/components/AssetStoryEditor.vue +32 -0
  20. package/src/components/AssetTagInput.vue +26 -0
  21. package/src/components/ProjectMarkdown.vue +33 -0
  22. package/src/i18n/en.json +163 -0
  23. package/src/i18n/index.js +40 -0
  24. package/src/layouts/WorkbenchLayout.vue +61 -0
  25. package/src/main.js +7 -0
  26. package/src/router/index.js +38 -0
  27. package/src/service/assets.js +13 -0
  28. package/src/service/project-markdown.js +26 -0
  29. package/src/views/assets/AudioAssets.vue +325 -0
  30. package/src/views/assets/CharacterAssets.vue +412 -0
  31. package/src/views/assets/ImageAssets.vue +456 -0
  32. package/src/views/settings/ProjectSettings.vue +80 -0
  33. package/templates/project/AGENTS.md +14 -0
  34. package/templates/project/README.md +7 -0
  35. package/templates/project/assets/audio/README.md +5 -0
  36. package/templates/project/assets/characters/README.md +5 -0
  37. package/templates/project/assets/other/README.md +3 -0
  38. package/templates/project/assets/props/README.md +3 -0
  39. package/templates/project/assets/scenes/README.md +3 -0
  40. package/vite.config.js +10 -0
@@ -0,0 +1,325 @@
1
+ <script>
2
+ import AssetDeleteButton from '@/components/AssetDeleteButton.vue'
3
+ import AssetTagInput from '@/components/AssetTagInput.vue'
4
+ import { requestAsset } from '@/service/assets.js'
5
+ import { ElInput, ElOption, ElSelect, ElUpload } from 'element-plus'
6
+ import 'element-plus/es/components/input/style/css'
7
+ import 'element-plus/es/components/select/style/css'
8
+ import 'element-plus/es/components/upload/style/css'
9
+ import { AudioLines, Plus, Search, Upload, X } from 'lucide-vue-next'
10
+
11
+ const categories = { music: '音乐', effect: '音效', dialogue: '台词' }
12
+
13
+ export default {
14
+ components: { AssetDeleteButton, AssetTagInput, AudioLines, ElInput, ElOption, ElSelect, ElUpload, Plus, Search, Upload, X },
15
+ data() {
16
+ return { categories, assets: [], query: '', filter: '', tagFilters: [], overview: null, create: null, active: null, busy: false, error: '', durations: {} }
17
+ },
18
+ computed: {
19
+ typedAssets() {
20
+ return this.assets.filter(asset => !this.filter || asset.categories.includes(this.filter))
21
+ },
22
+ availableTags() {
23
+ return [...new Set(this.typedAssets.flatMap(asset => asset.tags || []))].sort((a, b) => a.localeCompare(b, 'zh-CN'))
24
+ },
25
+ visibleAssets() {
26
+ const query = this.query.trim().toLowerCase()
27
+ return this.typedAssets.filter(asset => this.tagFilters.every(tag => asset.tags?.includes(tag))
28
+ && [asset.name, ...asset.groupPath, ...(asset.tags || []), ...asset.categories.flatMap(category => [categories[category], this.$t(categories[category])])]
29
+ .some(value => value?.toLowerCase().includes(query)))
30
+ },
31
+ },
32
+ watch: {
33
+ availableTags(tags) {
34
+ this.tagFilters = this.tagFilters.filter(tag => tags.includes(tag))
35
+ },
36
+ },
37
+ beforeRouteLeave() {
38
+ if (this.busy) return false
39
+ },
40
+ mounted() {
41
+ this.refresh()
42
+ import.meta.hot?.on('workbench:audio:changed', this.refresh)
43
+ },
44
+ beforeUnmount() {
45
+ import.meta.hot?.off('workbench:audio:changed', this.refresh)
46
+ },
47
+ methods: {
48
+ toggleTag(tag) {
49
+ this.tagFilters = this.tagFilters.includes(tag)
50
+ ? this.tagFilters.filter(value => value !== tag)
51
+ : [...this.tagFilters, tag]
52
+ },
53
+ async submitOverview(method, data) {
54
+ if (this.busy || !this.active) return
55
+ this.busy = true
56
+ this.error = ''
57
+ try {
58
+ await requestAsset('audio', this.active.id, method, data)
59
+ this.$refs.dialog.close()
60
+ await this.refresh()
61
+ } catch (cause) {
62
+ this.error = cause.message
63
+ } finally {
64
+ this.busy = false
65
+ }
66
+ },
67
+ async refresh() {
68
+ try {
69
+ this.assets = await requestAsset('audio')
70
+ const active = this.active
71
+ if (!active || this.busy || !this.assets.some(item => item.id === active.id)) return
72
+ const detail = await requestAsset('audio', active.id)
73
+ if (this.active === active && !this.busy) this.active = detail
74
+ } catch (cause) {
75
+ this.error = cause.message
76
+ }
77
+ },
78
+ showCreate() {
79
+ this.error = ''
80
+ this.create = { name: '' }
81
+ this.$refs.create.showModal()
82
+ },
83
+ async createAudio() {
84
+ this.busy = true
85
+ this.error = ''
86
+ try {
87
+ await requestAsset('audio', null, 'POST', { name: this.create.name.trim() })
88
+ this.$refs.create.close()
89
+ this.query = ''
90
+ this.filter = ''
91
+ this.tagFilters = []
92
+ await this.refresh()
93
+ } catch (cause) {
94
+ this.error = cause.message
95
+ } finally {
96
+ this.busy = false
97
+ }
98
+ },
99
+ loadOverview() {
100
+ this.overview = {
101
+ name: this.active.name,
102
+ type: this.active.type,
103
+ tags: [...this.active.tags],
104
+ note: this.active.note,
105
+ }
106
+ },
107
+ async open(item) {
108
+ this.error = ''
109
+ this.busy = true
110
+ try {
111
+ this.active = await requestAsset('audio', item.id)
112
+ this.loadOverview()
113
+ this.$refs.dialog.showModal()
114
+ } catch (cause) {
115
+ this.error = cause.message
116
+ } finally {
117
+ this.busy = false
118
+ }
119
+ },
120
+ close() {
121
+ this.active = null
122
+ this.error = ''
123
+ },
124
+ async uploadTrack({ raw: file }) {
125
+ this.$refs.audioUpload?.clearFiles()
126
+ if (!file || this.busy || !this.active) return
127
+ const track = this.active.cover || this.active.images[0]
128
+ if (file.size > 30 * 1024 * 1024) {
129
+ this.error = this.$t('请选择 30 MB 以内的音频')
130
+ return
131
+ }
132
+ this.busy = true
133
+ this.error = ''
134
+ try {
135
+ const base64 = await new Promise((resolve, reject) => {
136
+ const reader = new FileReader()
137
+ reader.onload = () => resolve(reader.result.split(',')[1])
138
+ reader.onerror = () => reject(new Error(this.$t('读取音频失败')))
139
+ reader.readAsDataURL(file)
140
+ })
141
+ this.active = await requestAsset('audio', this.active.id, 'PATCH', {
142
+ track: {
143
+ id: track?.id,
144
+ label: file.name,
145
+ filename: file.name,
146
+ category: this.active.type || track?.category || 'music',
147
+ status: track?.status || 'reference',
148
+ prompt: '',
149
+ source: track?.source,
150
+ usage: track?.usage,
151
+ loop: track?.loop,
152
+ base64,
153
+ },
154
+ })
155
+ await this.refresh()
156
+ } catch (cause) {
157
+ this.error = cause.message
158
+ } finally {
159
+ this.busy = false
160
+ }
161
+ },
162
+ recordDuration(track, event) {
163
+ const duration = event.target.duration
164
+ if (!Number.isFinite(duration)) return
165
+ const seconds = Math.round(duration)
166
+ this.durations[track.url] = Math.floor(seconds / 60) + ':' + String(seconds % 60).padStart(2, '0')
167
+ },
168
+ },
169
+ }
170
+ </script>
171
+
172
+ <template>
173
+ <section class="content">
174
+ <div class="heading">
175
+ <div><h1>{{ $t('音频资产') }}</h1><p>{{ $t('{count} 个音频资产', { count: assets.length }) }}</p></div>
176
+ <button class="primary" :disabled="busy" @click="showCreate"><Plus :size="18" /> {{ $t('新建音频') }}</button>
177
+ </div>
178
+ <div class="tools">
179
+ <label class="search"><Search :size="17" /><input v-model="query" :placeholder="$t('搜索名称或标签')" :aria-label="$t('搜索名称或标签')"></label>
180
+ <div class="filter-list" role="group" :aria-label="$t('音频类型')">
181
+ <span class="filter-label">{{ $t('类型') }}</span>
182
+ <button class="tag" :class="{ chosen: !filter }" :aria-pressed="!filter" @click="filter = ''">{{ $t('全部') }}</button>
183
+ <button v-for="(label, key) in categories" :key="key" class="tag" :class="{ chosen: filter === key }" :aria-pressed="filter === key" @click="filter = key">{{ $t(label) }}</button>
184
+ </div>
185
+ <div class="filter-list" role="group" :aria-label="$t('音频标签')">
186
+ <span class="filter-label">{{ $t('标签') }}</span>
187
+ <button class="tag" :class="{ chosen: !tagFilters.length }" :aria-pressed="!tagFilters.length" @click="tagFilters = []">{{ $t('全部') }}</button>
188
+ <button v-for="tag in availableTags" :key="tag" class="tag" :class="{ chosen: tagFilters.includes(tag) }"
189
+ :aria-pressed="tagFilters.includes(tag)" @click="toggleTag(tag)">{{ tag }}</button>
190
+ </div>
191
+ </div>
192
+ <p v-if="error && !active && !create" class="error" role="alert">{{ error }}</p>
193
+ <p v-if="!visibleAssets.length" class="empty">{{ assets.length ? $t('没有符合筛选条件的音频。') : $t('暂无音频素材。') }}</p>
194
+ <div class="grid">
195
+ <article v-for="item in visibleAssets" :key="item.id" class="card audio-card" @click="!busy && open(item)">
196
+ <button class="character-name" :disabled="busy"><AudioLines :size="17" />{{ item.name }}</button>
197
+ <p v-if="item.groupPath.length" class="asset-group-path">{{ item.groupPath.join(' / ') }}</p>
198
+ <div class="card-tags"><span v-for="category in item.categories" :key="category" class="tag compact">{{ $t(categories[category]) }}</span><span v-for="tag in item.tags" :key="tag" class="tag compact">{{ tag }}</span></div>
199
+ <audio v-if="item.cover?.url" :src="item.cover.url" controls preload="none" :aria-label="item.cover.label" @click.stop></audio>
200
+ </article>
201
+ </div>
202
+
203
+ <dialog ref="create" class="dialog small-dialog" @close="create = null" @cancel.prevent="!busy && $refs.create.close()">
204
+ <form v-if="create" @submit.prevent="createAudio">
205
+ <header class="dialog-head">
206
+ <h2>{{ $t('新建音频') }}</h2>
207
+ <button type="button" class="icon-button" :aria-label="$t('关闭')" :disabled="busy" @click="$refs.create.close()"><X :size="20" /></button>
208
+ </header>
209
+ <div class="dialog-body">
210
+ <div class="audio-field">
211
+ <label for="create-audio-name">{{ $t('名称') }}</label>
212
+ <ElInput id="create-audio-name" v-model="create.name" required autofocus :disabled="busy" />
213
+ </div>
214
+ <p v-if="error" class="error" role="alert">{{ error }}</p>
215
+ <div class="dialog-actions create-actions">
216
+ <button class="primary" :disabled="busy">{{ $t('创建音频') }}</button>
217
+ </div>
218
+ </div>
219
+ </form>
220
+ </dialog>
221
+
222
+ <dialog ref="dialog" class="dialog" @close="close"
223
+ @cancel.prevent="!busy && !$event.currentTarget.querySelector('.asset-delete-message') && $refs.dialog.close()">
224
+ <header class="dialog-head">
225
+ <h2>{{ active?.name }}</h2>
226
+ <button class="icon-button" :aria-label="$t('关闭')" :disabled="busy" @click="$refs.dialog.close()"><X :size="20" /></button>
227
+ </header>
228
+ <div class="dialog-body">
229
+ <p v-if="error" class="error" role="alert">{{ error }}</p>
230
+ <template v-if="active">
231
+ <form class="edit-form" @submit.prevent="submitOverview('PATCH', overview)">
232
+ <div class="audio-field">
233
+ <label for="audio-name">{{ $t('名称') }}</label>
234
+ <ElInput id="audio-name" v-model="overview.name" required :disabled="busy" />
235
+ </div>
236
+ <div class="audio-field">
237
+ <label for="audio-type">{{ $t('类型') }}</label>
238
+ <ElSelect id="audio-type" v-model="overview.type" :placeholder="$t('未设置')" :teleported="false" :disabled="busy">
239
+ <ElOption :label="$t('未设置')" value="" />
240
+ <ElOption v-for="(label, key) in categories" :key="key" :label="$t(label)" :value="key" />
241
+ </ElSelect>
242
+ </div>
243
+ <div class="audio-field">
244
+ <label for="audio-tags">{{ $t('标签') }}</label>
245
+ <AssetTagInput id="audio-tags" v-model="overview.tags" :disabled="busy" />
246
+ </div>
247
+ <div class="audio-field">
248
+ <label for="audio-note">{{ $t('备注') }}</label>
249
+ <ElInput id="audio-note" v-model="overview.note" type="textarea" :rows="3" :disabled="busy" />
250
+ </div>
251
+ <div class="audio-field">
252
+ <span>{{ $t('音频文件') }}</span>
253
+ <ElUpload ref="audioUpload" accept=".mp3,.wav,.ogg,.m4a,.flac,.aac" :auto-upload="false"
254
+ :show-file-list="false" :disabled="busy" :on-change="uploadTrack">
255
+ <button type="button" class="secondary" :disabled="busy">
256
+ <Upload :size="15" /> {{ active.images.length ? $t('替换音频') : $t('上传音频') }}
257
+ </button>
258
+ </ElUpload>
259
+ <article v-for="track in active.images" :key="track.id" class="audio-track">
260
+ <strong>{{ track.label }}</strong>
261
+ <p class="asset-group-path">{{ $t(categories[track.category]) }}<template v-if="durations[track.url]"> · {{ durations[track.url] }}</template><template v-if="track.loop"> · {{ $t('可循环') }}</template></p>
262
+ <audio v-if="track.url" controls preload="metadata" :src="track.url" @loadedmetadata="recordDuration(track, $event)"></audio>
263
+ <p v-else class="empty">{{ $t('找不到音频文件 {file}', { file: track.file }) }}</p>
264
+ <p v-if="track.usage">{{ $t('备注') }} {{ track.usage }}</p>
265
+ <p v-if="track.source">{{ $t('来源与授权') }} {{ track.source }}</p>
266
+ </article>
267
+ </div>
268
+ <details v-if="active.history.length" class="audio-track">
269
+ <summary>{{ $t('历史版本') }}</summary>
270
+ <section v-for="version in active.history" :key="version.version">
271
+ <p>{{ version.version }} {{ version.note }}</p>
272
+ <audio v-for="track in version.images.filter(item => item.url)" :key="track.url" :src="track.url" :aria-label="track.label" controls preload="none"></audio>
273
+ </section>
274
+ </details>
275
+
276
+ <div class="dialog-actions overview-actions">
277
+ <AssetDeleteButton :asset-name="active.name" :busy="busy" @confirm="submitOverview('DELETE', {})" />
278
+ <button class="primary overview-save-button" :disabled="busy">{{ $t('保存') }}</button>
279
+ </div>
280
+ </form>
281
+ </template>
282
+ </div>
283
+ </dialog>
284
+ </section>
285
+ </template>
286
+
287
+ <style scoped>
288
+ .audio-card {
289
+ cursor: pointer;
290
+ }
291
+
292
+ .audio-field {
293
+ display: grid;
294
+ gap: 6px;
295
+ color: #4b5267;
296
+ font-size: 13px;
297
+ }
298
+
299
+ .small-dialog .audio-field {
300
+ margin-bottom: 12px;
301
+ }
302
+
303
+ .dialog {
304
+ --form-focus-ring: rgb(91 92 245 / 16%);
305
+ --el-color-primary: var(--accent);
306
+ --el-border-radius-base: 7px;
307
+ }
308
+
309
+ .audio-field :deep(:is(.el-input__wrapper, .el-select__wrapper, .el-input-tag, .el-textarea__inner)) {
310
+ min-height: 34px;
311
+ transition: box-shadow 160ms ease;
312
+ }
313
+
314
+ .audio-field :deep(:is(.el-input__wrapper, .el-select__wrapper, .el-input-tag, .el-textarea__inner):focus-within),
315
+ .audio-field :deep(:is(.el-input__wrapper, .el-select__wrapper, .el-input-tag, .el-textarea__inner):focus) {
316
+ box-shadow: inset 0 0 0 1px var(--focus-border), 0 0 0 3px var(--form-focus-ring);
317
+ }
318
+
319
+ .filter-label {
320
+ align-self: center;
321
+ margin-right: 6px;
322
+ color: var(--muted, #64748b);
323
+ font-size: 13px;
324
+ }
325
+ </style>