@ngockhoale/ckit 0.2.7 → 0.2.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 (60) hide show
  1. package/dist/src/adapters/launch/executor.d.ts +2 -0
  2. package/dist/src/adapters/launch/executor.js +12 -1
  3. package/dist/src/gui/server.js +1 -1
  4. package/dist/src/team/engine.d.ts +10 -0
  5. package/dist/src/team/engine.js +118 -10
  6. package/dist/src/team/journal.d.ts +2 -0
  7. package/dist/src/team/journal.js +28 -3
  8. package/dist/src/team/runners.d.ts +4 -0
  9. package/dist/src/team/runners.js +16 -2
  10. package/dist/src/team/types.d.ts +4 -0
  11. package/dist/src/tui/team/model.js +2 -0
  12. package/gui/app.css +1 -19
  13. package/gui/app.js +43 -53
  14. package/gui/comic/assets/backgrounds/command-center.svg +1 -0
  15. package/gui/comic/assets/backgrounds/developer-workshop.svg +1 -0
  16. package/gui/comic/assets/backgrounds/handoff-corridor.svg +1 -0
  17. package/gui/comic/assets/backgrounds/incident-room.svg +1 -0
  18. package/gui/comic/assets/backgrounds/mission-overview.svg +1 -0
  19. package/gui/comic/assets/backgrounds/quality-lab.svg +1 -0
  20. package/gui/comic/assets/backgrounds/reporting-hall.svg +1 -0
  21. package/gui/comic/assets/backgrounds/research-library.svg +1 -0
  22. package/gui/comic/assets/backgrounds/review-chamber.svg +1 -0
  23. package/gui/comic/assets/backgrounds/strategy-room.svg +1 -0
  24. package/gui/comic/assets/effects/check-burst.svg +1 -0
  25. package/gui/comic/assets/effects/focus-lines.svg +1 -0
  26. package/gui/comic/assets/effects/glow.svg +1 -0
  27. package/gui/comic/assets/effects/particles.svg +1 -0
  28. package/gui/comic/assets/effects/speed-lines.svg +1 -0
  29. package/gui/comic/assets/effects/warning-flash.svg +1 -0
  30. package/gui/comic/assets/frame/episode-banner.svg +1 -0
  31. package/gui/comic/assets/frame/panel-corner.svg +1 -0
  32. package/gui/comic/assets/props/approval-seal.svg +1 -0
  33. package/gui/comic/assets/props/bug-warning.svg +1 -0
  34. package/gui/comic/assets/props/capsule.svg +1 -0
  35. package/gui/comic/assets/props/checklist.svg +1 -0
  36. package/gui/comic/assets/props/code-display.svg +1 -0
  37. package/gui/comic/assets/props/document.svg +1 -0
  38. package/gui/comic/assets/props/files.svg +1 -0
  39. package/gui/comic/assets/props/folder.svg +1 -0
  40. package/gui/comic/assets/props/magnifier.svg +1 -0
  41. package/gui/comic/assets/props/package.svg +1 -0
  42. package/gui/comic/assets/props/rejection-mark.svg +1 -0
  43. package/gui/comic/assets/props/report.svg +1 -0
  44. package/gui/comic/assets/props/scroll.svg +1 -0
  45. package/gui/comic/assets/props/shield.svg +1 -0
  46. package/gui/comic/assets/props/tablet.svg +1 -0
  47. package/gui/comic/assets/props/workstation.svg +1 -0
  48. package/gui/comic/comic.css +130 -0
  49. package/gui/comic/contact-sheet.html +15 -0
  50. package/gui/comic/contact-sheet.js +41 -0
  51. package/gui/comic/engine.js +366 -0
  52. package/gui/comic/manifests/backgrounds.json +216 -0
  53. package/gui/comic/manifests/characters.json +96 -0
  54. package/gui/comic/manifests/effects.json +96 -0
  55. package/gui/comic/manifests/props.json +246 -0
  56. package/gui/comic/manifests/scenes.json +205 -0
  57. package/gui/comic/render.js +594 -0
  58. package/gui/index.html +2 -2
  59. package/gui/network.js +195 -284
  60. package/package.json +1 -1
@@ -0,0 +1,366 @@
1
+ /* CKit Living Comic Engine - pure story logic (no DOM). Turns TeamEvents into comic beats. */
2
+ (function () {
3
+ 'use strict';
4
+ var POSES = ['idle', 'listening', 'speaking', 'thinking', 'researching', 'working', 'presenting', 'sending', 'reviewing', 'concerned', 'celebrating', 'waiting'];
5
+ var SCENES = ['director-intake', 'briefing', 'ba-research', 'discussion', 'implement', 'qc-check', 'qc-fail', 'review', 'handoff', 'blocked', 'failure', 'waiting', 'owner-message', 'consult', 'episode', 'delivered', 'extra-team'];
6
+ var MAX_SEQS = 50;
7
+
8
+ // ---------- text helpers ----------
9
+ var ANSI_RE = /\u001b\[[0-9;?]*[ -\/]*[@-~]|\u001b\][^\u0007]*\u0007/g;
10
+ var SECRET_RES = [
11
+ /-----BEGIN [A-Z ]*KEY-----[\s\S]*?(?:-----END [A-Z ]*KEY-----|$)/g,
12
+ /\bBearer\s+[A-Za-z0-9._~+\/=-]{8,}/g,
13
+ /\bsk-[A-Za-z0-9_-]{8,}/g,
14
+ /\bgh[pousr]_[A-Za-z0-9]{10,}/g,
15
+ /\bAKIA[0-9A-Z]{16}\b/g
16
+ ];
17
+ function mask(text) {
18
+ var s = String(text == null ? '' : text).replace(ANSI_RE, '');
19
+ for (var i = 0; i < SECRET_RES.length; i++) s = s.replace(SECRET_RES[i], '•••');
20
+ return s;
21
+ }
22
+ function stripMd(s) {
23
+ return s.replace(/```[a-z]*\n?/gi, '').replace(/^[ \t]*#{1,6}[ \t]*/gm, '').replace(/^[ \t]*(?:[-*+]|\d+[.)])[ \t]+/gm, '')
24
+ .replace(/^[ \t]*>[ \t]?/gm, '').replace(/!?\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/(\*\*|__|\*|`)/g, '');
25
+ }
26
+ function oneLine(s) { return String(s).replace(/\s+/g, ' ').trim(); }
27
+ function clip(s, n) { s = oneLine(s); return s.length > n ? s.slice(0, Math.max(0, n - 1)).replace(/\s+\S*$/, function (m) { return m.length > 12 ? '' : m; }).trimEnd() + '…' : s; }
28
+ // normalize: masked, ANSI-free, markdown-free, whitespace collapsed (single line)
29
+ function normalize(text, max) { var s = oneLine(stripMd(mask(text))); return max ? clip(s, max) : s; }
30
+ // first 1-2 sentences, <= max chars
31
+ function excerpt(text, max) {
32
+ max = max || 180;
33
+ var parts = stripMd(mask(text)).split(/(?<=[.!?。])\s+|\n+/).map(oneLine).filter(Boolean);
34
+ if (!parts.length) return '';
35
+ var out = parts[0];
36
+ if (out.length > max) return clip(out, max);
37
+ if (parts[1] && (out + ' ' + parts[1]).length <= max) out += ' ' + parts[1];
38
+ return out;
39
+ }
40
+ function fullText(text) { return mask(text).trim(); }
41
+ function cleanNote(note) {
42
+ var s = String(note == null ? '' : note).trim();
43
+ if (!s) return null;
44
+ if (s[0] === '{' || s[0] === '[') {
45
+ try { var j = JSON.parse(s); var n = j && typeof j.note === 'string' ? j.note.trim() : ''; return n ? normalize(n) : null; } catch (e) {
46
+ var m = /"note"\s*:\s*"((?:[^"\\]|\\.)*)"/.exec(s);
47
+ return m && m[1].trim() ? normalize(m[1].replace(/\\"/g, '"').replace(/\\n/g, ' ')) : null;
48
+ }
49
+ }
50
+ return normalize(s) || null;
51
+ }
52
+ function stepStatus(label, step, total) {
53
+ var l = String(label || '').toLowerCase();
54
+ if (/read|request|prompt|brief|đọc|yêu cầu|understand|context/.test(l)) return 'Đang đọc yêu cầu';
55
+ if (/tool|bash|command|run |edit|search|grep|fetch|test|build|file|công cụ/.test(l)) return 'Đang dùng công cụ';
56
+ if (/answer|writ|compos|respon|reply|draft|summar|report|viết|trả lời/.test(l)) return 'Đang viết câu trả lời';
57
+ var t = oneLine(String(label || '')); if (t) return clip(mask(t), 80); return step ? 'Đang thực hiện bước ' + step + (total ? '/' + total : '') : 'Đang làm việc';
58
+ }
59
+
60
+ // ---------- context ----------
61
+ var DEFAULT_TEAMS = { director: { name: 'Director' }, ba: { name: 'BA' }, implement: { name: 'Implement' }, qc: { name: 'QC' } };
62
+ function ctxFromConfig(cfg) {
63
+ var agents = {}, teams = {}, k;
64
+ for (k in DEFAULT_TEAMS) teams[k] = DEFAULT_TEAMS[k];
65
+ function add(a) { if (a && a.id) agents[a.id] = { name: a.name || a.id, team: a.team, short: a.short }; }
66
+ add(cfg.director);
67
+ var t = cfg.teams || {};
68
+ ['ba', 'implement', 'qc'].forEach(function (id) { (t[id] || []).forEach(add); });
69
+ (cfg.extraTeams || []).forEach(function (x) { if (x && x.id) { teams[x.id] = { name: x.name || x.id }; (x.agents || []).forEach(add); } });
70
+ return { agents: agents, teams: teams, directorId: cfg.director && cfg.director.id, directorReview: cfg.directorReview, hasTeams: !!(t.ba && t.ba.length || t.implement && t.implement.length || t.qc && t.qc.length || (cfg.extraTeams || []).length) };
71
+ }
72
+
73
+ // ---------- story ----------
74
+ function createStory(ctx0) {
75
+ var S;
76
+ function fresh() {
77
+ S = { ctx: { agents: {}, teams: {}, directorId: null }, beats: [], episodes: [], seen: {}, open: {}, stage: null, waiting: false, completed: false, rv: {}, reviewOff: false, sim: false, ep: null, last: null };
78
+ story.beats = S.beats; story.episodes = S.episodes;
79
+ }
80
+ var story = { beats: [], episodes: [] };
81
+ function setContext(c) {
82
+ c = c || {};
83
+ var teams = {}, k;
84
+ for (k in DEFAULT_TEAMS) teams[k] = DEFAULT_TEAMS[k];
85
+ for (k in (c.teams || {})) teams[k] = c.teams[k];
86
+ S.ctx = { agents: c.agents || {}, teams: teams, directorId: c.directorId || null, directorReview: c.directorReview, hasTeams: c.hasTeams };
87
+ }
88
+ story.setContext = setContext;
89
+ story.reset = function () { fresh(); setContext({}); };
90
+ fresh(); setContext(ctx0);
91
+
92
+ function agentName(id) { if (id === 'owner') return 'Owner'; var a = S.ctx.agents[id]; return a && a.name || (id === S.ctx.directorId ? 'Director' : String(id)); }
93
+ function teamOf(id) { if (id === S.ctx.directorId) return 'director'; var a = S.ctx.agents[id]; return a && a.team || 'director'; }
94
+ function teamName(id) { var t = S.ctx.teams[id]; var n = t && t.name || id; return id === 'director' ? 'Director' : (/^team\b/i.test(n) ? n : 'Team ' + n); }
95
+ function isDirector(id) { return id === S.ctx.directorId; }
96
+ function who(id) { return isDirector(id) ? 'Director' : agentName(id); }
97
+ function endpoint(id) { // agent id | team id | 'director' | 'all' -> {agentId, team, label}
98
+ if (id === 'all') return { agentId: 'owner', team: 'director', label: 'mọi người' };
99
+ if (id === 'director') return { agentId: S.ctx.directorId, team: 'director', label: 'Director' };
100
+ if (S.ctx.agents[id]) return { agentId: id, team: teamOf(id), label: who(id) };
101
+ if (S.ctx.teams[id]) return { agentId: null, teamId: id, team: id, label: teamName(id) };
102
+ return { agentId: null, team: 'director', label: String(id) };
103
+ }
104
+
105
+ var added, updated;
106
+ function touch(b) { if (added.indexOf(b) < 0 && updated.indexOf(b) < 0) updated.push(b); }
107
+ function addSeq(b, seq) {
108
+ b.sourceCount = (b.sourceCount || 0) + 1;
109
+ if (b.sourceSeqs.indexOf(seq) >= 0) return;
110
+ if (b.sourceSeqs.length >= MAX_SEQS) b.sourceSeqs.splice(MAX_SEQS / 2, 1);
111
+ b.sourceSeqs.push(seq);
112
+ }
113
+ function newEpisode(title) {
114
+ var n = S.episodes.length + 1;
115
+ S.ep = { id: 'e' + n, index: n, title: title, beatIds: [] };
116
+ S.episodes.push(S.ep);
117
+ S.completed = false;
118
+ return S.ep;
119
+ }
120
+ function cast(id, side, pose, speaking) { return { agentId: id, side: side, pose: pose, speaking: !!speaking }; }
121
+ function castEp(ep, side, pose, speaking) { // an agent, or the whole team when the event names no single agent
122
+ if (ep.agentId) return cast(ep.agentId, side, pose, speaking);
123
+ if (ep.teamId) { var c = cast(null, side, pose, speaking); c.teamId = ep.teamId; return c; }
124
+ return null;
125
+ }
126
+ function pushCast(arr, c) { if (c) arr.push(c); return arr; }
127
+ function invalidate() { var r = S.ep && S.rv[S.ep.id]; if (r) r.valid = false; }
128
+ function line(speakerId, kind, text, full) { var l = { speakerId: speakerId == null ? null : speakerId, kind: kind, text: text }; if (full && full !== text) l.full = full; return l; }
129
+ function mk(e, f) {
130
+ if (!S.ep) newEpisode('Tập 1: Yêu cầu mới');
131
+ var n = S.beats.length + 1;
132
+ var b = { id: 'b' + n, seq: n, episodeId: S.ep.id, type: f.type, title: f.title, team: f.team || 'director', scene: f.scene, weight: f.weight || 'normal',
133
+ cast: f.cast || [], lines: f.lines || [], truth: f.truth || 'unknown', status: f.status || '', open: !!f.open, transfer: f.transfer || null,
134
+ props: f.props || [], effect: f.effect || null, sourceSeqs: [], sourceCount: 0, at: e.at, endAt: e.at };
135
+ if (f.parallelWith) b.parallelWith = f.parallelWith;
136
+ if (S.sim) b.simulated = true;
137
+ addSeq(b, e.seq);
138
+ S.beats.push(b); S.ep.beatIds.push(b.id); added.push(b); S.last = b;
139
+ return b;
140
+ }
141
+ function closeBeat(b, at) { if (!b.open) return; b.open = false; b.endAt = at; if (b.truth === 'working') b.status = ''; touch(b); }
142
+ function closeAgent(agentId, at) { var b = S.open[agentId]; if (b) { closeBeat(b, at); delete S.open[agentId]; } }
143
+ function closeAll(at) { for (var id in S.open) closeBeat(S.open[id], at); S.open = {}; }
144
+
145
+ // working beat
146
+ function working(e, agentId, status, countOp) {
147
+ var b = S.open[agentId];
148
+ if (!b) {
149
+ var team = teamOf(agentId), others = [], names = [];
150
+ for (var id in S.open) { others.push(S.open[id].id); names.push(who(id)); }
151
+ var scene = team === 'ba' ? 'ba-research' : team === 'implement' ? 'implement' : team === 'qc' ? 'qc-check' : team === 'director' ? (S.stage === 'review' ? 'review' : 'director-intake') : 'extra-team';
152
+ var pose = team === 'director' ? 'thinking' : team === 'ba' ? 'researching' : team === 'implement' ? 'working' : team === 'qc' ? 'reviewing' : 'working';
153
+ b = mk(e, { type: 'working', title: who(agentId) + ' đang làm việc', team: team, scene: scene, weight: 'small', cast: [cast(agentId, 'left', pose, false)], truth: 'working', status: status, open: true, parallelWith: others.length ? others : undefined });
154
+ b._ops = 0; b._status = status; b._par = names.length ? 'Trong lúc đó, ' + names.join(', ') + ' cũng đang làm việc.' : '';
155
+ S.open[agentId] = b;
156
+ } else addSeq(b, e.seq);
157
+ if (status) b._status = status;
158
+ if (countOp) b._ops++;
159
+ b.status = b._status; b.endAt = e.at;
160
+ var ls = [];
161
+ if (b._par) ls.push(line(null, 'narration', b._par));
162
+ if (b._status) ls.push(line(agentId, 'status', b._status));
163
+ if (b._ops) ls.push(line(null, 'status', 'Đã thực hiện ' + b._ops + ' thao tác công cụ'));
164
+ b.lines = ls;
165
+ touch(b);
166
+ }
167
+
168
+ function push(e) {
169
+ added = []; updated = [];
170
+ if (!e || typeof e.seq !== 'number') return { added: added, updated: updated };
171
+ var key = (e.runId || '') + ':' + e.seq;
172
+ if (S.seen[key]) return { added: [], updated: [] };
173
+ S.seen[key] = 1;
174
+ try { handle(e); } catch (err) { /* never break the renderer */ }
175
+ return { added: added, updated: updated };
176
+ }
177
+
178
+ function handle(e) {
179
+ var t = e.type, d, b, ep, who_, tm;
180
+ switch (t) {
181
+ case 'run.started': {
182
+ closeAll(e.at);
183
+ if (e.config) { var c = ctxFromConfig(e.config); setContext(c); S.reviewOff = e.config.directorReview === false || !c.hasTeams; }
184
+ else S.reviewOff = S.ctx.directorReview === false || S.ctx.hasTeams === false;
185
+ S.stage = null; S.waiting = false; S.sim = e.simulated === true;
186
+ var goal = normalize(e.goal || '');
187
+ ep = newEpisode('Tập ' + (S.episodes.length + 1) + ': ' + (clip(goal, 60) || 'Yêu cầu mới') + (S.sim ? ' (Mô phỏng)' : ''));
188
+ mk(e, { type: 'episode', title: ep.title, scene: 'episode', weight: 'hero', cast: [cast(S.ctx.directorId, 'center', 'idle', false)], lines: [line(null, 'narration', 'Một yêu cầu mới được gửi tới Director.' + (S.sim ? ' (Mô phỏng)' : ''))], truth: 'planned', status: 'Bắt đầu', effect: 'focus-lines' });
189
+ mk(e, { type: 'owner', title: 'Owner giao yêu cầu', team: 'director', scene: 'owner-message', cast: [cast('owner', 'left', 'speaking', true), cast(S.ctx.directorId, 'right', 'listening', false)], lines: [line('owner', 'quote', excerpt(e.goal, 180), fullText(e.goal || ''))], truth: 'reported', status: 'Yêu cầu của owner', transfer: { from: 'owner', to: S.ctx.directorId }, props: ['document'] });
190
+ return;
191
+ }
192
+ case 'stage.changed':
193
+ closeAll(e.at); S.stage = e.stage; return;
194
+ case 'agent.started':
195
+ closeAgent(e.agentId, e.at); working(e, e.agentId, 'Đã bắt đầu lượt', false); if (!isDirector(e.agentId)) invalidate(); return;
196
+ case 'agent.step':
197
+ working(e, e.agentId, stepStatus(e.label, e.step, e.total), false); return;
198
+ case 'agent.activity':
199
+ working(e, e.agentId, '', /^\s*(tool:|\$ |edited )/.test(String(e.text || ''))); return;
200
+ case 'subagent.started':
201
+ working(e, e.agentId, 'Gọi thêm trợ thủ: ' + (normalize(e.task || e.kind || '', 80) || 'trợ thủ'), false); return;
202
+ case 'subagent.activity':
203
+ working(e, e.agentId, '', false); return;
204
+ case 'subagent.finished':
205
+ var sm = normalize(e.summary || '', 100); working(e, e.agentId, (e.ok ? 'Trợ thủ đã xong' : 'Trợ thủ gặp trục trặc') + (sm ? ': ' + sm : ''), false); return;
206
+ case 'agent.message': {
207
+ closeAgent(e.agentId, e.at);
208
+ var sp = e.agentId, spDir = isDirector(sp), to = endpoint(e.to), title, listener = to;
209
+ if (!spDir) invalidate();
210
+ if (!spDir && e.to === 'all') { listener = null; title = who(sp) + ' gửi tới cả nhóm'; }
211
+ else if (!spDir && e.to === 'director') { to = endpoint('director'); listener = to; title = who(sp) + ' báo cáo cho Director'; }
212
+ else if (spDir && S.ctx.teams[e.to] && e.to !== 'director') title = 'Director giao việc cho ' + teamName(e.to);
213
+ else if (spDir && e.to === 'all') { listener = null; title = 'Director thông báo'; }
214
+ else if (e.to === sp) title = who(sp) + ' ghi chú';
215
+ else if (S.ctx.agents[e.to]) title = who(sp) + ' trao đổi với ' + to.label;
216
+ else title = who(sp) + ' gửi ' + to.label;
217
+ if (e.round) title += ' (thảo luận vòng ' + e.round + ')';
218
+ var q = excerpt(e.text, 180), full = fullText(e.text);
219
+ var pose = /^(report|báo)/.test(title) || !spDir ? 'presenting' : 'speaking';
220
+ var cs = [cast(sp, 'left', pose, true)];
221
+ if (listener && listener.agentId !== sp) pushCast(cs, castEp(listener, 'right', 'listening', false));
222
+ mk(e, { type: 'message', title: title, team: teamOf(sp), scene: e.round ? 'discussion' : spDir ? 'briefing' : 'handoff', cast: cs, lines: [line(sp, 'quote', q, full)], truth: 'reported', status: 'Đã báo cáo', transfer: { from: sp, to: listener ? (listener.agentId || listener.teamId || e.to) : e.to } });
223
+ return;
224
+ }
225
+ case 'agent.failed': {
226
+ closeAgent(e.agentId, e.at);
227
+ var raw = fullText(e.reason || ''), short = raw.replace(/\s*\(full log:[^)]*\)\s*$/i, '');
228
+ mk(e, { type: 'failure', title: who(e.agentId) + ' gặp lỗi', team: teamOf(e.agentId), scene: 'failure', weight: 'hero', cast: [cast(e.agentId, 'center', 'concerned', false)], lines: [line(e.agentId, 'warning', clip(normalize(short), 160), raw !== short || normalize(short).length > 160 ? raw : undefined)], truth: 'failed', status: 'Gặp lỗi', props: ['bug-warning'], effect: 'warning-flash' });
229
+ return;
230
+ }
231
+ case 'handoff.created': {
232
+ invalidate(); var from = e.from, toT = e.to, ft = endpoint(from), tt = endpoint(toT), team = from === 'director' ? toT : from, title2, truth;
233
+ if (toT === 'director') { title2 = teamName(from) + ' nộp kết quả cho Director'; truth = 'reported'; }
234
+ else if (from === 'director') { title2 = 'Director chuyển việc cho ' + teamName(toT); truth = /send-back limit reached/i.test(String(e.text || '')) ? 'blocked' : 'reported'; }
235
+ else { title2 = teamName(from) + ' chuyển việc cho ' + teamName(toT); truth = 'reported'; }
236
+ var cs2 = []; pushCast(cs2, castEp(ft, 'left', 'sending', false)); pushCast(cs2, castEp(tt, 'right', 'listening', false));
237
+ mk(e, { type: 'handoff', title: title2, team: team, scene: 'handoff', cast: cs2, lines: [line(null, 'narration', 'Tài liệu bàn giao: ' + (normalize(e.artifact || e.title || '', 80) || 'báo cáo'), e.text ? fullText(e.text) : undefined)], truth: truth, status: truth === 'blocked' ? 'Chuyển tiếp dù còn vấn đề mở (hết lượt trả về)' : from === 'director' ? 'Đã chuyển giao' : 'Đã nộp', transfer: { from: ft.agentId || from, to: tt.agentId || toT }, props: ['capsule', 'document'], effect: 'speed-lines' });
238
+ return;
239
+ }
240
+ case 'flow.back': {
241
+ invalidate(); var isRet = e.kind === 'return', f2 = endpoint(e.from), t2 = endpoint(e.to);
242
+ var ttl = isRet ? (e.from === 'director' ? 'Director' : teamName(e.from)) + ' trả việc về ' + teamName(e.to) : teamName(e.from) + ' hỏi lại ' + (e.to === 'director' ? 'Director' : teamName(e.to));
243
+ var cs3 = []; pushCast(cs3, castEp(f2, 'left', isRet ? 'concerned' : 'speaking', true)); pushCast(cs3, castEp(t2, 'right', 'listening', false));
244
+ mk(e, { type: isRet ? 'return' : 'question', title: ttl, team: e.to === 'director' ? e.from : e.to, scene: isRet ? 'review' : 'discussion', cast: cs3, lines: [line(null, 'narration', (isRet ? 'Lý do: ' : 'Câu hỏi: ') + clip(normalize(e.reason || ''), 200), fullText(e.reason || ''))], truth: isRet ? 'failed' : 'reported', status: isRet ? 'Cần làm lại' : 'Có câu hỏi', transfer: { from: f2.agentId || e.from, to: t2.agentId || e.to }, effect: isRet ? 'warning-flash' : null });
245
+ return;
246
+ }
247
+ case 'director.consult': {
248
+ var di = S.ctx.directorId;
249
+ mk(e, { type: 'consult', title: 'Director hỏi ý kiến ' + who(e.agentId), team: 'director', scene: 'consult', cast: [cast(di, 'left', 'speaking', true), cast(e.agentId, 'right', 'listening', false)], lines: [line(di, 'question', excerpt(e.question, 180), fullText(e.question || ''))], truth: 'reported', status: 'Đang hỏi ý kiến', transfer: { from: di, to: e.agentId }, props: ['scroll'] });
250
+ return;
251
+ }
252
+ case 'qc.verdict': {
253
+ var pass = e.verdict === 'PASS';
254
+ mk(e, { type: 'verdict', title: pass ? 'QC báo đạt' : 'QC phát hiện vấn đề', team: 'qc', scene: pass ? 'qc-check' : 'qc-fail', cast: pushCast([], castEp(endpoint('qc'), 'center', pass ? 'presenting' : 'concerned', true)), lines: [line(null, 'paraphrase', excerpt(e.summary || '', 180), fullText(e.summary || ''))], truth: pass ? 'reported' : 'failed', status: pass ? 'QC báo đạt (chờ Director xem xét)' : 'QC báo lỗi', props: pass ? ['checklist'] : ['bug-warning'], effect: pass ? 'check-burst' : 'warning-flash' });
255
+ return;
256
+ }
257
+ case 'review.verdict': {
258
+ var ok = e.verdict === 'PASS', dd = S.ctx.directorId;
259
+ if (S.ep) S.rv[S.ep.id] = { verdict: ok ? 'PASS' : 'FAIL', valid: true };
260
+ mk(e, { type: 'verdict', title: ok ? 'Director xác nhận kết quả' : 'Director chưa duyệt kết quả', team: 'director', scene: 'review', weight: 'hero', cast: [cast(dd, 'center', ok ? 'celebrating' : 'concerned', true)], lines: [line(dd, ok ? 'paraphrase' : 'warning', excerpt(e.summary || '', 180), fullText([e.summary, e.checklist].filter(Boolean).join('\n\n')))], truth: ok ? 'verified' : 'failed', status: ok ? 'Đã xác nhận' : 'Chưa đạt', props: [ok ? 'approval-seal' : 'rejection-mark'], effect: ok ? 'glow' : 'warning-flash' });
261
+ return;
262
+ }
263
+ case 'run.waiting': {
264
+ closeAll(e.at);
265
+ var qs = stripMd(mask(e.questions || '')).split(/\n+/).map(oneLine).filter(Boolean).slice(0, 3).map(function (x) { return line(S.ctx.directorId, 'question', clip(x, 140)); });
266
+ if (!qs.length) qs.push(line(S.ctx.directorId, 'question', 'Director cần thêm thông tin từ owner.'));
267
+ if (e.assumptions) qs.push(line(null, 'paraphrase', 'Nếu không có trả lời, Director sẽ giả định: ' + clip(normalize(e.assumptions), 140), fullText(e.assumptions)));
268
+ qs[0].full = fullText(e.questions || '');
269
+ if (qs[0].full === qs[0].text) delete qs[0].full;
270
+ var f3 = { type: 'waiting', title: 'Director cần owner trả lời', team: 'director', scene: 'waiting', weight: 'hero', cast: [cast(S.ctx.directorId, 'left', 'waiting', true), cast('owner', 'right', 'listening', false)], lines: qs, truth: 'waiting', status: 'Đang chờ owner', transfer: { from: S.ctx.directorId, to: 'owner' }, props: ['scroll'], effect: 'focus-lines' };
271
+ mk(e, f3);
272
+ S.waiting = true; return;
273
+ }
274
+ case 'user.message': {
275
+ closeAll(e.at);
276
+ var fresh2 = !S.ep || S.waiting || S.completed || (S.last && S.last.type === 'waiting');
277
+ if (fresh2) newEpisode('Tập ' + (S.episodes.length + 1) + ': ' + (clip(normalize(e.text || ''), 60) || 'Owner trả lời'));
278
+ S.waiting = false;
279
+ mk(e, { type: 'owner', title: 'Owner nhắn cho Director', team: 'director', scene: 'owner-message', cast: [cast('owner', 'left', 'speaking', true), cast(S.ctx.directorId, 'right', 'listening', false)], lines: [line('owner', 'quote', excerpt(e.text, 180), fullText(e.text || ''))], truth: 'reported', status: 'Tin nhắn của owner', transfer: { from: 'owner', to: S.ctx.directorId } });
280
+ return;
281
+ }
282
+ case 'director.decision': {
283
+ var note = cleanNote(e.note), di2 = S.ctx.directorId;
284
+ if (e.decision === 'redirect') {
285
+ closeAll(e.at);
286
+ var ttl2 = note ? 'Đổi hướng: ' + clip(note, 60) : 'Director điều chỉnh hướng đi';
287
+ if (S.ep && S.ep.beatIds.length === 1 && S.last && S.last.type === 'owner') S.ep.title = ttl2;
288
+ else newEpisode(ttl2);
289
+ mk(e, { type: 'decision', title: ttl2, team: 'director', scene: 'episode', weight: 'hero', cast: [cast(di2, 'center', 'presenting', true)], lines: [line(null, 'narration', note ? 'Director đổi hướng: ' + clip(note, 160) : 'Director điều chỉnh hướng đi.', note && note.length > 160 ? note : undefined)], truth: 'confirmed', status: 'Đổi hướng', effect: 'focus-lines' });
290
+ } else if (e.decision === 'stop') {
291
+ closeAll(e.at);
292
+ mk(e, { type: 'decision', title: 'Director dừng công việc', team: 'director', scene: 'blocked', cast: [cast(di2, 'center', 'concerned', true)], lines: [line(null, 'narration', note ? clip(note, 160) : 'Director quyết định dừng.')], truth: 'stopped', status: 'Đã dừng' });
293
+ } else {
294
+ var ls = [line(null, 'narration', 'Director tiếp tục kế hoạch')]; if (note) ls.push(line(di2, 'paraphrase', clip(note, 160), note.length > 160 ? note : undefined));
295
+ mk(e, { type: 'decision', title: 'Director tiếp tục kế hoạch', team: 'director', scene: 'briefing', cast: [cast(di2, 'center', 'speaking', true)], lines: ls, truth: 'reported', status: 'Tiếp tục' });
296
+ }
297
+ return;
298
+ }
299
+ case 'lane.chosen':
300
+ mk(e, { type: 'lane', title: (e.by === 'owner' ? 'Chủ dự án chọn quy trình ' : e.by === 'fallback' ? 'Hệ thống chọn quy trình mặc định ' : 'Director chọn quy trình ') + (normalize(e.name || e.laneId || '', 60)), team: 'director', scene: 'briefing', weight: 'small', cast: e.by === 'owner' ? [cast('owner', 'center', 'presenting', true)] : e.by === 'fallback' ? [] : [cast(S.ctx.directorId, 'center', 'presenting', true)], lines: e.reason ? [line(null, 'narration', clip(normalize(e.reason), 160))] : [], truth: 'confirmed', status: e.by === 'fallback' ? 'Quy trình mặc định' : 'Đã chọn quy trình' });
301
+ return;
302
+ case 'run.completed': {
303
+ closeAll(e.at); S.completed = true; S.waiting = false;
304
+ var di3 = S.ctx.directorId, rep2 = e.report || '', rv = S.ep && S.rv[S.ep.id];
305
+ if (e.outcome === 'delivered') {
306
+ var ver = !S.reviewOff && !!rv && rv.verdict === 'PASS' && rv.valid, bad = !S.reviewOff && !!rv && rv.verdict === 'FAIL' && rv.valid;
307
+ var dStatus = ver ? 'Đã bàn giao' : S.reviewOff ? 'Đã bàn giao (không bật Director Review)' : bad ? 'Bàn giao khi Director chưa duyệt' : rv ? 'Đã bàn giao (có thay đổi sau lần Director duyệt cuối)' : 'Đã bàn giao (chưa có bước Director Review)';
308
+ mk(e, { type: 'delivery', title: 'Director bàn giao kết quả', team: 'director', scene: 'delivered', weight: 'hero', cast: [cast(di3, 'center', ver ? 'celebrating' : 'presenting', true), cast('owner', 'right', 'listening', false)], lines: [line(di3, 'paraphrase', excerpt(rep2, 180), fullText(rep2))], truth: ver ? 'verified' : bad ? 'failed' : 'reported', status: dStatus, transfer: { from: di3, to: 'owner' }, props: ['package', 'report'], effect: ver ? 'particles' : null });
309
+ } else if (e.outcome === 'failed') {
310
+ mk(e, { type: 'failure', title: 'Run thất bại', team: 'director', scene: 'failure', weight: 'hero', cast: [cast(di3, 'center', 'concerned', false)], lines: [line(di3, 'warning', excerpt(rep2, 180) || 'Run kết thúc với lỗi.', fullText(rep2))], truth: 'failed', status: 'Thất bại', props: ['bug-warning'], effect: 'warning-flash' });
311
+ } else if (e.outcome === 'stopped') {
312
+ mk(e, { type: 'stopped', title: 'Run đã dừng', team: 'director', scene: 'blocked', weight: 'normal', cast: [cast(di3, 'center', 'idle', false)], lines: [line(null, 'narration', 'Run đã được dừng.')], truth: 'stopped', status: 'Đã dừng' });
313
+ } else {
314
+ var f4 = { type: 'declined', title: 'Director chưa nhận việc', team: 'director', scene: 'blocked', weight: 'hero', cast: [cast(di3, 'left', 'concerned', true), cast('owner', 'right', 'listening', false)], lines: [line(di3, 'warning', excerpt(rep2, 180) || 'Director chưa thể nhận yêu cầu này.', fullText(rep2))], truth: 'blocked', status: 'Chưa nhận việc', props: ['rejection-mark'] };
315
+ mk(e, f4);
316
+ }
317
+ return;
318
+ }
319
+ }
320
+ }
321
+
322
+ story.push = push;
323
+ return story;
324
+ }
325
+
326
+ // ---------- layout ----------
327
+ function textLen(b) { var n = 0; for (var i = 0; i < b.lines.length; i++) n += (b.lines[i].text || '').length; return n; }
328
+ function closeRow(items, idx, beatsById) {
329
+ var n = items.length;
330
+ if (n === 1) return { cols: 12, items: [{ beatId: items[0].id, span: 12 }] };
331
+ if (n === 3) return { cols: 12, items: items.map(function (b) { return { beatId: b.id, span: 4 }; }) };
332
+ var a = textLen(items[0]), c = textLen(items[1]), mx = Math.max(a, c, 1), diff = Math.abs(a - c) / mx, spans;
333
+ if (diff <= 0.1) spans = [6, 6];
334
+ else if (diff >= 0.4) spans = a > c ? [7, 5] : [5, 7];
335
+ else spans = idx % 2 === 0 ? [7, 5] : [5, 7];
336
+ return { cols: 12, items: [{ beatId: items[0].id, span: spans[0] }, { beatId: items[1].id, span: spans[1] }] };
337
+ }
338
+ function layoutRows(beats, opts) {
339
+ var width = opts && typeof opts.width === 'number' ? opts.width : 1200, rows = [], cur = [];
340
+ if (width < 900) return beats.map(function (b) { return { cols: 12, items: [{ beatId: b.id, span: 12 }] }; });
341
+ function flush() { if (cur.length) { rows.push(closeRow(cur, rows.length)); cur = []; } }
342
+ for (var i = 0; i < beats.length; i++) {
343
+ var b = beats[i];
344
+ if (b.weight === 'hero') { flush(); rows.push({ cols: 12, items: [{ beatId: b.id, span: 12 }] }); continue; }
345
+ cur.push(b);
346
+ if (cur.length === 3) flush();
347
+ }
348
+ flush();
349
+ return rows;
350
+ }
351
+
352
+ // pure check for same-origin comic/avatar asset paths (pathname only, already URL-parsed)
353
+ function safeAssetPath(pathname, base) {
354
+ if (typeof pathname !== 'string' || !pathname || pathname.length > 300) return false;
355
+ var d; try { d = decodeURIComponent(pathname); } catch (e) { return false; }
356
+ if (d.indexOf('..') >= 0 || d.indexOf('\\') >= 0 || d.indexOf('%') >= 0 || /[\u0000-\u001f]/.test(d) || pathname.indexOf('//') >= 0) return false;
357
+ base = base || '/';
358
+ if (d.indexOf(base) !== 0) return false;
359
+ var rest = d.slice(base.length);
360
+ if (!(rest.indexOf('comic/assets/') === 0 || rest.indexOf('assets/ckit-oriental-avatar-pack/') === 0)) return false;
361
+ return /\.(svg|png|webp)$/i.test(rest);
362
+ }
363
+
364
+ var api = { version: 1, safeAssetPath: safeAssetPath, POSES: POSES, SCENES: SCENES, createStory: createStory, layoutRows: layoutRows, narrate: excerpt, normalize: normalize, mask: mask, cleanNote: cleanNote };
365
+ (typeof globalThis !== 'undefined' ? globalThis : window).CKComicEngine = api;
366
+ })();
@@ -0,0 +1,216 @@
1
+ {
2
+ "schemaVersion": 1,
3
+ "artStyleVersion": "cyber-wuxia-v1",
4
+ "items": [
5
+ {
6
+ "id": "command-center",
7
+ "type": "background",
8
+ "src": "assets/backgrounds/command-center.svg",
9
+ "width": 1536,
10
+ "height": 1024,
11
+ "transparent": false,
12
+ "tintable": false,
13
+ "anchor": {
14
+ "x": 0.5,
15
+ "y": 0.5
16
+ },
17
+ "safeTextRegion": {
18
+ "x": 0.28,
19
+ "y": 0.05,
20
+ "w": 0.44,
21
+ "h": 0.25
22
+ },
23
+ "provenance": "hand-authored-svg",
24
+ "version": 1
25
+ },
26
+ {
27
+ "id": "strategy-room",
28
+ "type": "background",
29
+ "src": "assets/backgrounds/strategy-room.svg",
30
+ "width": 1536,
31
+ "height": 1024,
32
+ "transparent": false,
33
+ "tintable": false,
34
+ "anchor": {
35
+ "x": 0.5,
36
+ "y": 0.5
37
+ },
38
+ "safeTextRegion": {
39
+ "x": 0.28,
40
+ "y": 0.05,
41
+ "w": 0.44,
42
+ "h": 0.25
43
+ },
44
+ "provenance": "hand-authored-svg",
45
+ "version": 1
46
+ },
47
+ {
48
+ "id": "research-library",
49
+ "type": "background",
50
+ "src": "assets/backgrounds/research-library.svg",
51
+ "width": 1536,
52
+ "height": 1024,
53
+ "transparent": false,
54
+ "tintable": false,
55
+ "anchor": {
56
+ "x": 0.5,
57
+ "y": 0.5
58
+ },
59
+ "safeTextRegion": {
60
+ "x": 0.28,
61
+ "y": 0.05,
62
+ "w": 0.44,
63
+ "h": 0.25
64
+ },
65
+ "provenance": "hand-authored-svg",
66
+ "version": 1
67
+ },
68
+ {
69
+ "id": "developer-workshop",
70
+ "type": "background",
71
+ "src": "assets/backgrounds/developer-workshop.svg",
72
+ "width": 1536,
73
+ "height": 1024,
74
+ "transparent": false,
75
+ "tintable": false,
76
+ "anchor": {
77
+ "x": 0.5,
78
+ "y": 0.5
79
+ },
80
+ "safeTextRegion": {
81
+ "x": 0.28,
82
+ "y": 0.05,
83
+ "w": 0.44,
84
+ "h": 0.25
85
+ },
86
+ "provenance": "hand-authored-svg",
87
+ "version": 1
88
+ },
89
+ {
90
+ "id": "quality-lab",
91
+ "type": "background",
92
+ "src": "assets/backgrounds/quality-lab.svg",
93
+ "width": 1536,
94
+ "height": 1024,
95
+ "transparent": false,
96
+ "tintable": false,
97
+ "anchor": {
98
+ "x": 0.5,
99
+ "y": 0.5
100
+ },
101
+ "safeTextRegion": {
102
+ "x": 0.28,
103
+ "y": 0.05,
104
+ "w": 0.44,
105
+ "h": 0.25
106
+ },
107
+ "provenance": "hand-authored-svg",
108
+ "version": 1
109
+ },
110
+ {
111
+ "id": "review-chamber",
112
+ "type": "background",
113
+ "src": "assets/backgrounds/review-chamber.svg",
114
+ "width": 1536,
115
+ "height": 1024,
116
+ "transparent": false,
117
+ "tintable": false,
118
+ "anchor": {
119
+ "x": 0.5,
120
+ "y": 0.5
121
+ },
122
+ "safeTextRegion": {
123
+ "x": 0.28,
124
+ "y": 0.05,
125
+ "w": 0.44,
126
+ "h": 0.25
127
+ },
128
+ "provenance": "hand-authored-svg",
129
+ "version": 1
130
+ },
131
+ {
132
+ "id": "incident-room",
133
+ "type": "background",
134
+ "src": "assets/backgrounds/incident-room.svg",
135
+ "width": 1536,
136
+ "height": 1024,
137
+ "transparent": false,
138
+ "tintable": false,
139
+ "anchor": {
140
+ "x": 0.5,
141
+ "y": 0.5
142
+ },
143
+ "safeTextRegion": {
144
+ "x": 0.28,
145
+ "y": 0.05,
146
+ "w": 0.44,
147
+ "h": 0.25
148
+ },
149
+ "provenance": "hand-authored-svg",
150
+ "version": 1
151
+ },
152
+ {
153
+ "id": "handoff-corridor",
154
+ "type": "background",
155
+ "src": "assets/backgrounds/handoff-corridor.svg",
156
+ "width": 1536,
157
+ "height": 1024,
158
+ "transparent": false,
159
+ "tintable": false,
160
+ "anchor": {
161
+ "x": 0.5,
162
+ "y": 0.5
163
+ },
164
+ "safeTextRegion": {
165
+ "x": 0.28,
166
+ "y": 0.05,
167
+ "w": 0.44,
168
+ "h": 0.25
169
+ },
170
+ "provenance": "hand-authored-svg",
171
+ "version": 1
172
+ },
173
+ {
174
+ "id": "reporting-hall",
175
+ "type": "background",
176
+ "src": "assets/backgrounds/reporting-hall.svg",
177
+ "width": 1536,
178
+ "height": 1024,
179
+ "transparent": false,
180
+ "tintable": false,
181
+ "anchor": {
182
+ "x": 0.5,
183
+ "y": 0.5
184
+ },
185
+ "safeTextRegion": {
186
+ "x": 0.28,
187
+ "y": 0.05,
188
+ "w": 0.44,
189
+ "h": 0.25
190
+ },
191
+ "provenance": "hand-authored-svg",
192
+ "version": 1
193
+ },
194
+ {
195
+ "id": "mission-overview",
196
+ "type": "background",
197
+ "src": "assets/backgrounds/mission-overview.svg",
198
+ "width": 1536,
199
+ "height": 1024,
200
+ "transparent": false,
201
+ "tintable": false,
202
+ "anchor": {
203
+ "x": 0.5,
204
+ "y": 0.5
205
+ },
206
+ "safeTextRegion": {
207
+ "x": 0.28,
208
+ "y": 0.05,
209
+ "w": 0.44,
210
+ "h": 0.25
211
+ },
212
+ "provenance": "hand-authored-svg",
213
+ "version": 1
214
+ }
215
+ ]
216
+ }