@biunow/agent-biutalk 1.1.6 → 1.1.8

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 (112) hide show
  1. package/_shared/.tmp-panel-smoke/art/smoke.mp4 +0 -0
  2. package/_shared/.tmp-panel-smoke/art-pipe/out/biuart-01_cave-mwq5kfpm.mp4 +0 -0
  3. package/_shared/.tmp-panel-smoke/art-pipe/panel-delivery.json +7 -0
  4. package/_shared/.tmp-panel-smoke/reel-pipe/film/index.html +7 -0
  5. package/_shared/.tmp-panel-smoke/reel-pipe/film/main.js +68 -0
  6. package/_shared/.tmp-panel-smoke/reel-pipe/film/panel-beats.json +20 -0
  7. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/CREDITS +20 -0
  8. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/build.sh +17 -0
  9. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/ease.js +43 -0
  10. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/events.json +1 -0
  11. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/film.js +533 -0
  12. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/OFL-Archivo.txt +93 -0
  13. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/OFL-DMSerifDisplay.txt +93 -0
  14. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/OFL-UnifrakturMaguntia.txt +94 -0
  15. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/fonts/fonts.css +3 -0
  16. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/index.html +6 -0
  17. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/lines.json +10 -0
  18. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/main.js +15 -0
  19. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/mix.py +116 -0
  20. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/music/score.py +168 -0
  21. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/music/score_meta.json +59 -0
  22. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/score.json +235 -0
  23. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/stills/styleframe.jpg +0 -0
  24. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/tools/fonts.sh +17 -0
  25. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/tools/subs.py +11 -0
  26. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/tools/words.py +20 -0
  27. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/voices/dur.json +10 -0
  28. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/voices/words.json +213 -0
  29. package/_shared/.tmp-panel-smoke/reel-pipe/style-demo/voices/words_rel.json +56 -0
  30. package/_shared/.tmp-panel-smoke/reel-pipe/style-ref.json +5 -0
  31. package/_shared/.tmp-panel-smoke/reel-pipe2/film/index.html +7 -0
  32. package/_shared/.tmp-panel-smoke/reel-pipe2/film/main.js +68 -0
  33. package/_shared/.tmp-panel-smoke/reel-pipe2/film/panel-beats.json +20 -0
  34. package/_shared/.tmp-panel-smoke/reel-pipe2/out/video.mp4 +0 -0
  35. package/_shared/.tmp-panel-smoke/reel-pipe2/panel-delivery.json +8 -0
  36. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/CREDITS +20 -0
  37. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/build.sh +17 -0
  38. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/ease.js +43 -0
  39. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/events.json +1 -0
  40. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/film.js +533 -0
  41. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/OFL-Archivo.txt +93 -0
  42. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/OFL-DMSerifDisplay.txt +93 -0
  43. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/OFL-UnifrakturMaguntia.txt +94 -0
  44. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/fonts/fonts.css +3 -0
  45. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/index.html +6 -0
  46. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/lines.json +10 -0
  47. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/main.js +15 -0
  48. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/mix.py +116 -0
  49. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/music/score.py +168 -0
  50. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/music/score_meta.json +59 -0
  51. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/score.json +235 -0
  52. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/stills/styleframe.jpg +0 -0
  53. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/tools/fonts.sh +17 -0
  54. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/tools/subs.py +11 -0
  55. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/tools/words.py +20 -0
  56. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/voices/dur.json +10 -0
  57. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/voices/words.json +213 -0
  58. package/_shared/.tmp-panel-smoke/reel-pipe2/style-demo/voices/words_rel.json +56 -0
  59. package/_shared/.tmp-panel-smoke/reel-pipe2/style-ref.json +5 -0
  60. package/_shared/.tmp-panel-smoke/remo-pipe/brief.md +2 -0
  61. package/_shared/.tmp-panel-smoke/remo-pipe/remo-job.json +4 -0
  62. package/_shared/.tmp-panel-smoke/remo-pipe2/brief.md +1 -0
  63. package/_shared/.tmp-panel-smoke/remo-pipe2/out/final.mp4 +0 -0
  64. package/_shared/.tmp-panel-smoke/remo-pipe2/panel-delivery.json +8 -0
  65. package/_shared/.tmp-panel-smoke/remo-pipe2/remo-job.json +4 -0
  66. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/ep00-demo.mp4 +0 -0
  67. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/out/ep00-demo.mp4 +0 -0
  68. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/panel-delivery.json +7 -0
  69. package/_shared/.tmp-panel-smoke/vibe-pipe-ok/publish.json +10 -0
  70. package/_shared/.tmp-panel-smoke/vibe-still.jpg +0 -0
  71. package/_shared/.tmp-panel-smoke/vibe-still.log +6 -0
  72. package/_shared/.tmp-panel-smoke/vibe-still.log.err +0 -0
  73. package/_shared/.tmp-panel-smoke/vibe-still2.log +26 -0
  74. package/_shared/.tmp-panel-smoke/vibe-still2.log.err +0 -0
  75. package/_shared/lib/film-locate.mjs +203 -0
  76. package/_shared/lib/voice-catalog.mjs +127 -0
  77. package/_shared/scripts/apply-upload-limits.mjs +62 -0
  78. package/_shared/scripts/patch-film-hosts.mjs +233 -0
  79. package/_shared/scripts/patch-voice-options.mjs +41 -0
  80. package/_shared/scripts/sync-local-agents.mjs +67 -0
  81. package/_shared/scripts/wire-film-bridge.mjs +24 -0
  82. package/_shared/scripts/wire-voice-plus.mjs +68 -0
  83. package/_shared/studio/assert-panel-permit.mjs +67 -0
  84. package/_shared/studio/composer.css +559 -0
  85. package/_shared/studio/composer.js +728 -0
  86. package/_shared/studio/optimize-prompt.mjs +84 -0
  87. package/_shared/studio/optimize-prompt.test.mjs +47 -0
  88. package/_shared/studio/recipes.mjs +87 -0
  89. package/_shared/studio/select-menu.js +450 -0
  90. package/_shared/studio/to-english-prompt.mjs +165 -0
  91. package/_shared/studio/to-english-prompt.test.mjs +72 -0
  92. package/bin/server.js +52 -36
  93. package/data/looks.json +8 -0
  94. package/lib/film-bridge.mjs +1 -0
  95. package/lib/host.mjs +149 -264
  96. package/lib/voice-bridge.mjs +2 -0
  97. package/manifest.json +4 -3
  98. package/package.json +1 -1
  99. package/skills/biutalk/EXEC.md +69 -0
  100. package/skills/biutalk/SKILL.md +12 -10
  101. package/skills/biutalk/scripts/doctor.mjs +124 -0
  102. package/skills/biutalk/scripts/qc.mjs +129 -0
  103. package/skills/biutalk/scripts/scaffold.mjs +205 -0
  104. package/studio/app.js +17 -1
  105. package/studio/composer.css +115 -1
  106. package/studio/composer.js +389 -10
  107. package/studio/index.html +5 -1
  108. package/studio/optimize-prompt.mjs +84 -0
  109. package/studio/optimize-prompt.test.mjs +47 -0
  110. package/studio/recipes.mjs +1 -0
  111. package/studio/to-english-prompt.mjs +165 -0
  112. package/studio/to-english-prompt.test.mjs +72 -0
@@ -0,0 +1,84 @@
1
+ /** 把用户原话收成当前模板能直接出片的一句。会员接口才调用。 */
2
+
3
+ function same(a, b) {
4
+ return String(a || '').replace(/\s+/g, '') === String(b || '').replace(/\s+/g, '');
5
+ }
6
+
7
+ function quotes(text) {
8
+ return [...String(text || '').matchAll(/「([^」]+)」/g)]
9
+ .map((match) => match[1].trim())
10
+ .filter(Boolean);
11
+ }
12
+
13
+ function clip(text, max = 80) {
14
+ const chars = [...String(text || '').replace(/\s+/g, ' ').trim()];
15
+ if (chars.length <= max) return chars.join('');
16
+ return chars.slice(0, max).join('').replace(/[,,、::;;\s]+$/u, '');
17
+ }
18
+
19
+ function topicFromFreeText(text) {
20
+ let value = String(text || '').replace(/\s+/g, ' ').trim();
21
+ value = value.replace(/^用[\u4e00-\u9fffA-Za-z0-9]{1,16}讲/, '');
22
+ value = value.replace(/^[「『"']+|[」』"']+$/g, '').trim();
23
+ const sentence = (value.split(/[。!?!?]/)[0] || value).trim();
24
+ return sentence.replace(/^[「『"']+|[」』"']+$/g, '').trim();
25
+ }
26
+
27
+ function fail(message) {
28
+ const error = new Error(message);
29
+ error.code = 'PROMPT';
30
+ throw error;
31
+ }
32
+
33
+ export function optimizePrompt(input) {
34
+ const text = String(input?.text || '').trim();
35
+ const skeleton = String(input?.templatePrompt || '').trim();
36
+ const narration = String(input?.narration || '');
37
+ const attachment = String(input?.attachment || '');
38
+ if (!text) fail('先写下要讲的事');
39
+ if (!(skeleton && /「[^」]*」/.test(skeleton))) return { prompt: cap(text), kept: true };
40
+
41
+ const samples = quotes(skeleton);
42
+ let topic = quotes(text).find((quote) => !samples.some((sample) => same(sample, quote))) || '';
43
+ if (!topic) {
44
+ const free = topicFromFreeText(text);
45
+ const stillSample = !free || samples.some((sample) => same(sample, free)) || (skeleton && same(text, skeleton));
46
+ if (stillSample) fail(skeleton ? '先把引号里换成你要讲的事' : '先写下要讲的事');
47
+ topic = free;
48
+ }
49
+ topic = clip(topic);
50
+ if (!topic) fail('先写下要讲的事');
51
+
52
+ let prompt = skeleton.replace(/「[^」]*」/, `「${topic}」`);
53
+ const detail = extraDetail(text, skeleton, topic);
54
+ if (detail) prompt = `${prompt.replace(/。?$/, '。')}${detail}。`;
55
+ if (narration === 'off' && !/没有口播|不要口播|没有旁白|不要旁白/.test(prompt)) prompt += '没有口播。';
56
+ if (narration === 'required' && !/口播|旁白/.test(prompt)) prompt += '口播用你上传的录音。';
57
+ if (attachment === 'required' && !/附件|上传/.test(prompt)) prompt += '必须的附件跟这一条一起交。';
58
+ return { prompt: cap(prompt.replace(/[ \t]+/g, ' ').trim()) };
59
+ }
60
+
61
+ function extraDetail(text, skeleton, topic) {
62
+ const known = skeleton.replace(/\s+/g, '');
63
+ const sentences = String(text || '')
64
+ .replace(/\s+/g, ' ')
65
+ .split(/[。!?!?]/)
66
+ .map((sentence) => sentence.trim())
67
+ .filter(Boolean);
68
+ for (const sentence of sentences) {
69
+ const bare = sentence
70
+ .replace(/^用[\u4e00-\u9fffA-Za-z0-9]{1,16}讲/, '')
71
+ .replace(/[「」『』]/g, '')
72
+ .trim();
73
+ if (!bare || same(bare, topic)) continue;
74
+ if (known.includes(bare.replace(/\s+/g, ''))) continue;
75
+ if (/另外|再讲|同时再|以及/.test(bare)) continue;
76
+ return clip(bare, 48);
77
+ }
78
+ return '';
79
+ }
80
+
81
+ function cap(prompt) {
82
+ if ([...prompt].length <= 4000) return prompt;
83
+ return [...prompt].slice(0, 4000).join('');
84
+ }
@@ -0,0 +1,47 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { optimizePrompt } from './optimize-prompt.mjs';
4
+
5
+ const skeleton = '用灯火史话讲「古代驿站为什么能把一封信送到这么远」。火光里先给结论,再给一个能记住的细节,没有口播。';
6
+
7
+ test('空内容要求先写要讲的事', () => {
8
+ assert.throws(() => optimizePrompt({ text: ' ', templatePrompt: skeleton }), /先写下要讲的事/);
9
+ });
10
+
11
+ test('没改模板例句时要求换成自己的事', () => {
12
+ assert.throws(() => optimizePrompt({ text: skeleton, templatePrompt: skeleton }), /先把引号里换成你要讲的事/);
13
+ });
14
+
15
+ test('自由输入收进当前模板,并保留模板后半句', () => {
16
+ const got = optimizePrompt({ text: '古代人都想当皇帝', templatePrompt: skeleton });
17
+ assert.equal(
18
+ got.prompt,
19
+ '用灯火史话讲「古代人都想当皇帝」。火光里先给结论,再给一个能记住的细节,没有口播。',
20
+ );
21
+ });
22
+
23
+ test('改过引号后仍锁回模板句式', () => {
24
+ const got = optimizePrompt({
25
+ text: '用灯火史话讲「古代人都想当皇帝」。另外再讲科举和驿站。',
26
+ templatePrompt: skeleton,
27
+ });
28
+ assert.equal(got.prompt.includes('古代人都想当皇帝'), true);
29
+ assert.equal(got.prompt.includes('没有口播'), true);
30
+ assert.equal(got.prompt.includes('科举'), false);
31
+ });
32
+
33
+ test('没选模板时保留原文,不压成套话', () => {
34
+ const text = '飞机上为什么要开飞行模式。先讲信号会干扰导航,再说规定,最后说实际影响。';
35
+ const got = optimizePrompt({ text, narration: 'required' });
36
+ assert.equal(got.prompt, text);
37
+ assert.equal(got.kept, true);
38
+ });
39
+
40
+ test('模板句后面留下用户写的那一个细节', () => {
41
+ const got = optimizePrompt({
42
+ text: '用灯火史话讲「古代人都想当皇帝」。嘴里说一句想反,唐律也要流二千里。',
43
+ templatePrompt: skeleton,
44
+ });
45
+ assert.equal(got.prompt.includes('没有口播'), true);
46
+ assert.equal(got.prompt.includes('嘴里说一句想反,唐律也要流二千里'), true);
47
+ });
@@ -81,6 +81,7 @@ export function recipeView(recipe, extras = {}) {
81
81
  blurb: recipe?.blurb || '',
82
82
  narration: extras.narration || 'optional',
83
83
  attachment: extras.attachment || 'optional',
84
+ audio: extras.audio || '',
84
85
  hint: extras.hint || recipe?.blurb || '',
85
86
  };
86
87
  }
@@ -0,0 +1,165 @@
1
+ /** 把用户看到的中文提示词收成交给大模型的英文提示词。画风名保持原样,方便对上模板。 */
2
+
3
+ const REPLACEMENTS = [
4
+ [/there is no word of mouth/gi, 'no voiceover'],
5
+ [/no word of mouth/gi, 'no voiceover'],
6
+ [/word of mouth/gi, 'voiceover'],
7
+ [/no spoken words?/gi, 'no voiceover'],
8
+ [/without spoken words?/gi, 'no voiceover'],
9
+ [/no oral (?:broadcast|narration)/gi, 'no voiceover'],
10
+ [/without oral (?:broadcast|narration)/gi, 'with no voiceover'],
11
+ [/oral broadcast/gi, 'voiceover'],
12
+ [/there was no voiceover/gi, 'no voiceover'],
13
+ [/horizontal screen/gi, 'landscape frame'],
14
+ [/vertical screen/gi, 'portrait frame'],
15
+ ];
16
+
17
+ const SOURCE_FLAGS = [
18
+ [/没有口播|不要口播|无口播|没有旁白|不要旁白|无旁白|不依赖旁白|不要另起旁白|全程靠字幕/, 'No voiceover.'],
19
+ [/口播用你上传的录音|只用上传的录音/, 'Use the uploaded recording as the voiceover.'],
20
+ [/横屏/, 'Landscape frame.'],
21
+ [/竖屏/, 'Portrait frame.'],
22
+ [/中英字幕/, 'On-screen subtitles in Chinese and English.'],
23
+ [/必须的附件/, 'The required attachment is part of this film.'],
24
+ ];
25
+
26
+ export function filmLanguage(text) {
27
+ return /[\u4e00-\u9fff]/.test(String(text || '')) ? 'Chinese' : 'English';
28
+ }
29
+
30
+ export function audienceLine(text) {
31
+ const language = filmLanguage(text);
32
+ return `On-screen text and narration language: ${language}. Write the on-screen title and every spoken or written sentence in ${language}.`;
33
+ }
34
+
35
+ function enforceFlags(source, english) {
36
+ let out = polishEnglish(english);
37
+ for (const [pattern, sentence] of SOURCE_FLAGS) {
38
+ if (!pattern.test(source)) continue;
39
+ const key = sentence.replace(/\.$/, '').toLowerCase();
40
+ if (!out.toLowerCase().includes(key)) out = polishEnglish(`${out} ${sentence}`);
41
+ }
42
+ return out;
43
+ }
44
+
45
+ export async function modelTitle(title, source, englishBrief, fetchImpl = globalThis.fetch) {
46
+ const raw = String(title || '').trim();
47
+ const brief = String(englishBrief || '').trim();
48
+ if (raw && !/[\u4e00-\u9fff]/.test(raw)) return raw.slice(0, 120);
49
+ if ((!raw || String(source || '').trim().startsWith(raw)) && brief) {
50
+ const subject = brief.match(/Subject:\s*"([^"]+)"/);
51
+ if (subject && subject[1] && !/[\u4e00-\u9fff]/.test(subject[1])) return subject[1].replace(/[.。\s]+$/g, '').slice(0, 120);
52
+ const line = brief.replace(/^Use the [\s\S]+? style\.\s*/i, '').split('\n')[0].trim();
53
+ if (line && !/[\u4e00-\u9fff]/.test(line)) return line.slice(0, 120);
54
+ }
55
+ if (!raw) return 'Untitled';
56
+ const translated = await toEnglishPrompt(raw, fetchImpl);
57
+ return (translated || 'Untitled').slice(0, 120);
58
+ }
59
+
60
+ export function polishEnglish(text) {
61
+ let out = String(text || '');
62
+ for (const [pattern, to] of REPLACEMENTS) out = out.replace(pattern, to);
63
+ return out.replace(/[ \t]+/g, ' ').replace(/\s*\n\s*/g, '\n').trim();
64
+ }
65
+
66
+ export function parseMyMemory(payload) {
67
+ const data = typeof payload === 'string' ? JSON.parse(payload) : payload;
68
+ if (!data || Number(data.responseStatus) >= 400) return '';
69
+ const text = data.responseData && data.responseData.translatedText;
70
+ if (!text || /MYMEMORY WARNING|QUERY LENGTH LIMIT/i.test(text)) return '';
71
+ return String(text)
72
+ .replace(/&quot;/g, '"')
73
+ .replace(/&#39;|&apos;/g, "'")
74
+ .replace(/&amp;/g, '&')
75
+ .replace(/&lt;/g, '<')
76
+ .replace(/&gt;/g, '>')
77
+ .trim();
78
+ }
79
+
80
+ function chunks(text) {
81
+ const parts = String(text || '').split(/(?<=[。!?!?\n])/);
82
+ const out = [];
83
+ let buf = '';
84
+ const push = (value) => {
85
+ const piece = value.trim();
86
+ if (piece) out.push(piece);
87
+ };
88
+ for (const part of parts) {
89
+ const next = buf + part;
90
+ if (Buffer.byteLength(next, 'utf8') > 420 && buf) {
91
+ push(buf);
92
+ buf = part;
93
+ } else {
94
+ buf = next;
95
+ }
96
+ }
97
+ push(buf);
98
+ return out.flatMap((piece) => {
99
+ if (Buffer.byteLength(piece, 'utf8') <= 420) return [piece];
100
+ const chars = [...piece];
101
+ const slices = [];
102
+ let current = '';
103
+ for (const char of chars) {
104
+ if (Buffer.byteLength(current + char, 'utf8') > 420) {
105
+ slices.push(current);
106
+ current = char;
107
+ } else current += char;
108
+ }
109
+ if (current) slices.push(current);
110
+ return slices;
111
+ });
112
+ }
113
+
114
+ async function translatePiece(text, fetchImpl) {
115
+ const pieces = [];
116
+ for (const piece of chunks(text)) {
117
+ if (!/[\u4e00-\u9fff]/.test(piece)) {
118
+ pieces.push(piece);
119
+ continue;
120
+ }
121
+ const url = `https://api.mymemory.translated.net/get?q=${encodeURIComponent(piece)}&langpair=zh-CN|en`;
122
+ const res = await fetchImpl(url, { signal: AbortSignal.timeout(12000) });
123
+ if (!res.ok) throw new Error('英文提示词没有生成');
124
+ const english = polishEnglish(parseMyMemory(await res.json()));
125
+ if (!english || /[\u4e00-\u9fff]/.test(english)) throw new Error('英文提示词没有生成');
126
+ pieces.push(english);
127
+ }
128
+ const joined = polishEnglish(pieces.join(' '));
129
+ if (!joined) throw new Error('英文提示词没有生成');
130
+ return joined;
131
+ }
132
+
133
+ export async function toEnglishPrompt(text, fetchImpl = globalThis.fetch) {
134
+ const source = String(text || '').trim();
135
+ if (!source) return '';
136
+ if (!/[\u4e00-\u9fff]/.test(source)) return source;
137
+
138
+ // Prefer BN panel LLM settings when injected (PANEL_LLM_*).
139
+ // This file may live in products/_shared/studio or products/<agent>/studio.
140
+ try {
141
+ let panelTranslateToEnglish;
142
+ try {
143
+ ({ panelTranslateToEnglish } = await import('../lib/panel-llm.mjs'));
144
+ } catch {
145
+ ({ panelTranslateToEnglish } = await import('../_shared/lib/panel-llm.mjs'));
146
+ }
147
+ const viaPanel = await panelTranslateToEnglish(source, fetchImpl);
148
+ if (viaPanel) return enforceFlags(source, polishEnglish(viaPanel));
149
+ } catch {
150
+ /* fall through to MyMemory */
151
+ }
152
+
153
+ const framed = source.match(/^用([\s\S]{1,40}?)讲「([^」]+)」\s*[。.]?\s*([\s\S]*)$/);
154
+ if (framed) {
155
+ const topicEn = (await translatePiece(framed[2], fetchImpl)).replace(/[.。\s]+$/g, '');
156
+ const tailEn = framed[3].trim() ? await translatePiece(framed[3], fetchImpl) : '';
157
+ return enforceFlags(source, `Use the ${framed[1].trim()} style. Subject: "${topicEn}".${tailEn ? ` ${tailEn}` : ''}`);
158
+ }
159
+ const ref = source.match(/^选\s*(ref\d+)\s*[。.]?\s*([\s\S]*)$/i);
160
+ if (ref) {
161
+ const body = ref[2].trim() ? await translatePiece(ref[2], fetchImpl) : '';
162
+ return enforceFlags(source, `Lock ${ref[1]}. ${body}`);
163
+ }
164
+ return enforceFlags(source, await translatePiece(source, fetchImpl));
165
+ }
@@ -0,0 +1,72 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { audienceLine, filmLanguage, modelTitle, parseMyMemory, polishEnglish, toEnglishPrompt } from './to-english-prompt.mjs';
4
+
5
+ test('已经是英文时不请求翻译', async () => {
6
+ let called = false;
7
+ const got = await toEnglishPrompt('Keep the sentences short.', async () => {
8
+ called = true;
9
+ return { ok: true, json: async () => ({}) };
10
+ });
11
+ assert.equal(got, 'Keep the sentences short.');
12
+ assert.equal(called, false);
13
+ });
14
+
15
+ test('模板句保留画风名,主题和后半句译成英文', async () => {
16
+ const fetchImpl = async (url) => {
17
+ const q = decodeURIComponent(url.split('q=')[1].split('&')[0]);
18
+ const translated = q.includes('古代人')
19
+ ? 'why everyone in antiquity wanted to be emperor'
20
+ : 'State the conclusion first in the firelight, then one memorable detail. No oral broadcast.';
21
+ return { ok: true, json: async () => ({ responseStatus: 200, responseData: { translatedText: translated } }) };
22
+ };
23
+ const got = await toEnglishPrompt(
24
+ '用灯火史话讲「古代人都想当皇帝」。火光里先给结论,再给一个能记住的细节,没有口播。',
25
+ fetchImpl,
26
+ );
27
+ assert.equal(got.includes('灯火史话'), true);
28
+ assert.equal(got.includes('why everyone in antiquity wanted to be emperor'), true);
29
+ assert.equal(got.includes('no voiceover'), true);
30
+ assert.equal(/[\u4e00-\u9fff]/.test(got.replace('灯火史话', '')), false);
31
+ });
32
+
33
+ test('口播被收成 voiceover', () => {
34
+ assert.equal(polishEnglish('There was no oral broadcast.'), 'no voiceover.');
35
+ assert.equal(polishEnglish('There is no word of mouth.'), 'no voiceover.');
36
+ });
37
+
38
+ test('原文里的没有口播,译文漏掉时也会补上', async () => {
39
+ const fetchImpl = async () => ({
40
+ ok: true,
41
+ json: async () => ({ responseStatus: 200, responseData: { translatedText: 'A short film about bees.' } }),
42
+ });
43
+ const got = await toEnglishPrompt('讲蜜蜂。没有口播。', fetchImpl);
44
+ assert.equal(got.includes('No voiceover.'), true);
45
+ });
46
+
47
+ test('片名从英文主题来,不再把中文原句交给模型', async () => {
48
+ let called = false;
49
+ const source = '用灯火史话讲「古代人都想当皇帝」。没有口播。';
50
+ const title = await modelTitle(
51
+ source.slice(0, 40),
52
+ source,
53
+ 'Use the 灯火史话 style. Subject: "why everyone wanted to be emperor". No voiceover.',
54
+ async () => {
55
+ called = true;
56
+ return { ok: true, json: async () => ({}) };
57
+ },
58
+ );
59
+ assert.equal(title, 'why everyone wanted to be emperor');
60
+ assert.equal(called, false);
61
+ });
62
+
63
+ test('解析翻译结果', () => {
64
+ assert.equal(parseMyMemory({ responseStatus: 200, responseData: { translatedText: 'Hello &amp; welcome' } }), 'Hello & welcome');
65
+ assert.equal(parseMyMemory({ responseStatus: 200, responseData: { translatedText: 'MYMEMORY WARNING: too long' } }), '');
66
+ });
67
+
68
+ test('成片语言跟着用户原文', () => {
69
+ assert.equal(filmLanguage('古代人都想当皇帝'), 'Chinese');
70
+ assert.equal(filmLanguage('airplane mode'), 'English');
71
+ assert.equal(audienceLine('古代人').includes('in Chinese'), true);
72
+ });