dsh-lifecard 0.2.2 → 0.3.1

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.
package/lib/core.js CHANGED
@@ -1035,6 +1035,9 @@ export function safeId(raw) {
1035
1035
  const s = String(raw ?? '').trim()
1036
1036
  if (s === '') return 'unnamed'
1037
1037
  return s
1038
+ // 控制字符(含 NUL)一律抹掉:它们会原样跟着路径走,Node 那一侧只会
1039
+ // ENOENT/EINVAL,排查起来像见鬼。
1040
+ .replace(/[\u0000-\u001f\u007f]/g, '')
1038
1041
  .replace(/[\\/:*?"<>|#%\s]+/g, '-')
1039
1042
  .replace(/^-+|-+$/g, '')
1040
1043
  .replace(/-{2,}/g, '-')
package/lib/index.js CHANGED
@@ -2917,7 +2917,12 @@ function buildApi(ctx, workspace, config, options) {
2917
2917
  const ws = await root(body)
2918
2918
  const id = String(body?.id ?? '').trim()
2919
2919
  if (id === '') return { ok: false, error: '没有说是哪本书' }
2920
- return await novel.deleteChapter(ws, id, body?.index)
2920
+ const gone = await novel.deleteChapter(ws, id, body?.index)
2921
+ if (gone.ok !== true) return gone
2922
+ // 这一章没了,它上面那份剧本留着就是一份谁也点不到的孤儿——
2923
+ // 界面上只能按章翻,第 N 章都不存在了,那份剧本永远打不开也删不掉。
2924
+ const script = await novel.deleteScript(ws, id, body?.index)
2925
+ return script.ok === true ? gone : { ...gone, scriptKept: true }
2921
2926
  },
2922
2927
 
2923
2928
  /** 整本书不要了。 */
@@ -3507,6 +3512,167 @@ function buildApi(ctx, workspace, config, options) {
3507
3512
  usedCast: cast.length,
3508
3513
  }
3509
3514
  },
3515
+
3516
+ // ── 改成剧本 ────────────────────────────────────────────────
3517
+ // 一章小说 → 一份能配音、能拍的剧本。落在 `books/<书>/scripts/<章号>.md`,
3518
+ // 和章节文件同号,回头一眼能对上「第三章改出来的那份」。
3519
+
3520
+ /** 读这一章改出来的剧本(没改过也正常,返回空串)。 */
3521
+ 'novel.script.get': async (body) => {
3522
+ if ((await ensureModules()).novel !== true) return missingFeature('novel')
3523
+ const ws = await root(body)
3524
+ const id = String(body?.id ?? '').trim()
3525
+ if (id === '') return { ok: false, error: '没有说是哪本书' }
3526
+ const target = await scriptTarget(ws, id, body?.index)
3527
+ if (target.error !== undefined) return { ok: false, error: target.error }
3528
+ const got = await novel.readScript(ws, id, target.index)
3529
+ if (got.ok !== true) return got
3530
+ const scan = novel.scanScript(got.text)
3531
+ return { ...got, scan, report: novel.scriptReportText(scan) }
3532
+ },
3533
+
3534
+ /** 存这一章的剧本(手改过的也走这里)。 */
3535
+ 'novel.script.save': async (body) => {
3536
+ if ((await ensureModules()).novel !== true) return missingFeature('novel')
3537
+ const ws = await root(body)
3538
+ const id = String(body?.id ?? '').trim()
3539
+ if (id === '') return { ok: false, error: '没有说是哪本书' }
3540
+ const target = await scriptTarget(ws, id, body?.index)
3541
+ if (target.error !== undefined) return { ok: false, error: target.error }
3542
+ const raw = String(body?.text ?? '')
3543
+ // 只剩空格(含全角空格)和换行:这人不小心清空了。
3544
+ // 以前这里直接删文件,回话还说「存好了」——磁盘上那份就再也找不回来。
3545
+ // 现在只回一句「空的,没往里存」,原来的那份一动不动。
3546
+ if (raw.replace(/[\s\u3000]/g, '') === '') {
3547
+ const scan = novel.scanScript('')
3548
+ return { ok: false, blank: true, index: target.index, error: '这一份是空的,没往里存。原来那一份还在。' }
3549
+ }
3550
+ const saved = await novel.writeScript(ws, id, target.index, raw)
3551
+ if (saved.ok !== true) return saved
3552
+ // 扫的是真正落盘的那一份(writeScript 会先剥掉 ``` 围栏)。
3553
+ // 扫原文的话,回执里的场数、字数会和磁盘上那份对不上。
3554
+ const stored = await novel.readScript(ws, id, target.index)
3555
+ const storedText = stored.ok === true ? stored.text : raw
3556
+ const scan = novel.scanScript(storedText)
3557
+ return { ...saved, scan, report: novel.scriptReportText(scan) }
3558
+ },
3559
+
3560
+ /** 这本书改过哪几章。 */
3561
+ 'novel.script.list': async (body) => {
3562
+ if ((await ensureModules()).novel !== true) return missingFeature('novel')
3563
+ const ws = await root(body)
3564
+ const id = String(body?.id ?? '').trim()
3565
+ if (id === '') return { ok: false, error: '没有说是哪本书' }
3566
+ const book = await novel.readBook(ws, id)
3567
+ if (book.ok !== true) return { ok: false, error: '这本书不在了。回到书架看一眼,可能被移走了。' }
3568
+ return await novel.listScripts(ws, id)
3569
+ },
3570
+
3571
+ /** 删掉这一章改出来的剧本。章节本身不动。 */
3572
+ 'novel.script.delete': async (body) => {
3573
+ if ((await ensureModules()).novel !== true) return missingFeature('novel')
3574
+ const ws = await root(body)
3575
+ const id = String(body?.id ?? '').trim()
3576
+ if (id === '') return { ok: false, error: '没有说是哪本书' }
3577
+ const target = await scriptTarget(ws, id, body?.index)
3578
+ if (target.error !== undefined) return { ok: false, error: target.error }
3579
+ return await novel.deleteScript(ws, id, target.index)
3580
+ },
3581
+
3582
+ /**
3583
+ * 让 AI 把这一章改成剧本。
3584
+ *
3585
+ * 只给这一章的正文 + 人 + 设定 + 上一章剧本的最后一场。
3586
+ * 不给大纲:改剧本不是重写故事,章后面的事它不需要知道——知道了会忍不住
3587
+ * 提前把底抖出来,那是改编里最容易炸的一处。
3588
+ */
3589
+ 'novel.script.draft': async (body) => {
3590
+ if ((await ensureModules()).novel !== true) return missingFeature('novel')
3591
+ const ws = await root(body)
3592
+ const id = String(body?.id ?? '').trim()
3593
+ if (id === '') return { ok: false, error: '没有说是哪本书' }
3594
+ const book = await novel.readBook(ws, id)
3595
+ if (book.ok !== true) return book
3596
+ // 没给章号就用最后一章;给了但不合法要说出来,不能悄悄改成第 1 章去改。
3597
+ const rawIndex = body?.index
3598
+ const gaveIndex = rawIndex !== undefined && rawIndex !== null && String(rawIndex).trim() !== ''
3599
+ if (gaveIndex === true && novel.isValidChapterIndex(rawIndex) !== true) {
3600
+ return { ok: false, error: '章号不对,没法知道你说的是哪一章。' }
3601
+ }
3602
+ const index = gaveIndex === true
3603
+ ? Number(String(rawIndex).trim())
3604
+ : (book.chapters.length === 0 ? 1 : book.chapters[book.chapters.length - 1].index)
3605
+ const chapter = await novel.readChapter(ws, id, index)
3606
+ if (chapter.ok !== true) return chapter
3607
+ const chapterBody = String(chapter.body ?? '').trim()
3608
+ if (chapterBody === '') return { ok: false, error: `第 ${index} 章还是空的,没有东西可以改。`, index }
3609
+ if (chapterBody.length > 20000) {
3610
+ return { ok: false, error: '这一章太长(超过两万字),一次改不完。先分成两章再改。', index }
3611
+ }
3612
+
3613
+ // 上一章改出来的剧本,最后一场整段带上:谁站在哪、手上有什么、门开没开。
3614
+ let previousTail = ''
3615
+ if (index > 1) {
3616
+ const prev = await novel.readScript(ws, id, index - 1)
3617
+ if (prev.ok === true && String(prev.text ?? '').trim() !== '') {
3618
+ const parsed = novel.parseScriptScenes(prev.text)
3619
+ const last = parsed.scenes[parsed.scenes.length - 1]
3620
+ previousTail = last === undefined ? novel.tailOf(prev.text, 600) : last.raw.join('\n').trim()
3621
+ }
3622
+ }
3623
+
3624
+ // 世界设定(改剧本时全量给:场的布景、道具、天气都得对得上)
3625
+ const worldNotes = []
3626
+ const worldRel = typeof body?.world === 'string' && body.world.trim() !== '' ? body.world.trim() : ''
3627
+ if (worldRel !== '') {
3628
+ const world = await store.readCard(ws, worldRel)
3629
+ if (world.ok === true) {
3630
+ for (const entry of core.parseWorldEntries(world.sections)) {
3631
+ const content = String(entry?.content ?? '').trim()
3632
+ if (content !== '') worldNotes.push(content)
3633
+ }
3634
+ }
3635
+ }
3636
+
3637
+ const cast = await gatherCast(ws, body?.cast)
3638
+ const firstLine = chapterBody.split(/\r?\n/).find((line) => line.trim() !== '') ?? ''
3639
+ const user = novel.buildScriptPrompt({
3640
+ title: String(book.data?.title ?? id),
3641
+ genre: String(book.data?.genre ?? ''),
3642
+ idea: String(book.idea ?? ''),
3643
+ chapterNo: index,
3644
+ chapterTitle: firstLine.replace(/^#+\s*/, '').trim().slice(0, 60),
3645
+ chapterBody,
3646
+ cast,
3647
+ worldEntries: worldNotes,
3648
+ previousTail,
3649
+ ask: body?.ask,
3650
+ sceneHint: body?.scenes,
3651
+ })
3652
+ const called = await callModel(ctx, {
3653
+ system: novel.buildScriptSystemNote(),
3654
+ user,
3655
+ model: body?.model,
3656
+ })
3657
+ if (called.ok !== true) {
3658
+ return { ok: false, error: `这次没改成:${called.error ?? '过一会儿再试一次'}`, index }
3659
+ }
3660
+ const text = novel.stripFence(called.text)
3661
+ if (text === '') return { ok: false, error: 'AI 这次什么都没给,再点一次试试。', index }
3662
+
3663
+ const scan = novel.scanScript(text)
3664
+ return {
3665
+ ok: true,
3666
+ index,
3667
+ text,
3668
+ scan,
3669
+ report: novel.scriptReportText(scan),
3670
+ // 不落盘:改出来的东西人要先看、能删掉半场、能整段重写。存盘由 script.save 做。
3671
+ usedCast: cast.length,
3672
+ usedWorld: worldNotes.length,
3673
+ continued: previousTail !== '',
3674
+ }
3675
+ },
3510
3676
  }
3511
3677
  }
3512
3678
 
@@ -3517,6 +3683,31 @@ function buildApi(ctx, workspace, config, options) {
3517
3683
  * 它才不会把「说话很冲的那个人」写成温和的——那是长篇最容易崩的地方。
3518
3684
  * 每张卡只取前 500 字:卡可能写得很长,全贴会把大纲和上一章挤掉。
3519
3685
  */
3686
+ /**
3687
+ * 章号闸。
3688
+ *
3689
+ * `chapterFileName` 里有 `Math.max(1, Math.min(999, Math.floor(Number(index) || 1)))`
3690
+ * —— 它把 0、-1、'abc'、null 一律**悄悄**夹成 1。对「读」来说没问题,
3691
+ * 对「写」就是灾难:`novel.script.save({index:0})` 会盖掉第 1 章原来那份剧本,
3692
+ * 而且回话说的是「存好了」。
3693
+ *
3694
+ * 所以写之前先在这里拦一道:章号得是正整数,而且这一章得真的存在。
3695
+ * 返回 `{ index }` 或者 `{ error }`。
3696
+ */
3697
+ async function scriptTarget(ws, id, rawIndex) {
3698
+ if (novel.isValidChapterIndex(rawIndex) !== true) {
3699
+ return { error: '章号不对,没法知道你说的是哪一章。' }
3700
+ }
3701
+ const index = Number(String(rawIndex).trim())
3702
+ const book = await novel.readBook(ws, id)
3703
+ if (book.ok !== true) return { error: '这本书不在了。回到书架看一眼,可能被移走了。' }
3704
+ const exists = Array.isArray(book.chapters) && book.chapters.some((c) => Number(c.index) === index)
3705
+ if (!exists) {
3706
+ return { error: `这本书没有第 ${index} 章。先翻到真正的那一章再存。` }
3707
+ }
3708
+ return { index }
3709
+ }
3710
+
3520
3711
  async function gatherCast(ws, wants) {
3521
3712
  const out = []
3522
3713
  const list = Array.isArray(wants) ? wants.slice(0, 8) : []
@@ -4138,6 +4329,14 @@ function registerInterviewSkill(ctx) {
4138
4329
  fallback: '中文小说与长文的写法:先分清用不用人物卡,按传统文学的标准写正文,要连载节奏就切。',
4139
4330
  label: '写作',
4140
4331
  })
4332
+ // 小说改剧本。「改剧本」按钮背后的提示词在 lib/novel.js 里,这份 skill 是给
4333
+ // 用户在对话里直接说「把这一章改成剧本」用的——两条路走的是同一套规矩。
4334
+ registerBundledSkill(ctx, {
4335
+ dir: 'novel-to-script',
4336
+ name: 'novel-to-script',
4337
+ fallback: '小说改剧本:分场、把叙述换成 △ 动作、把心理换成演得出来的东西、台词按标点分工。',
4338
+ label: '改剧本',
4339
+ })
4141
4340
  // 这三份是写作用得上的通用手艺,随包一起发:装插件即等于有,
4142
4341
  // 换台机器不必再单独装一遍。名字与内容都保持原样,方便用户照旧用
4143
4342
  // `/human-writing`、`/blind-review-loop`、`/visual-novel` 唤起。