@unson/brainbase-mcp 0.50.12 → 0.50.14

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.
@@ -494,3 +494,273 @@ export function workGrowthBlock(doc, { stage, changes }) {
494
494
  }
495
495
  return workspaceRailBlock(doc, { title: '区画の育ち', content });
496
496
  }
497
+
498
+ /*
499
+ * The pulse panel (story-world-three-scales-v1): one small panel over the map whose contents follow the
500
+ * scale the viewer is at.
501
+ * - the world: which businesses have work in progress (a way into each);
502
+ * - a business: how far its work has come, its agents, and each gathering of work (a street);
503
+ * - a gathering of work: which agent is on which work and how, then the work without an agent.
504
+ * It is built only from the pulse (`worldPulse`, `districtPulse`) of the records already read; it keeps
505
+ * nothing and writes nothing. Its breadcrumb is the way back up.
506
+ */
507
+ const FLOW_SEGMENTS = Object.freeze([
508
+ { key: 'done', label: '終えた' },
509
+ { key: 'in_progress', label: '進行中' },
510
+ { key: 'waiting', label: '待ち' },
511
+ { key: 'pending', label: 'これから' },
512
+ ]);
513
+
514
+ function flowBar(doc, counts, { legend = true } = {}) {
515
+ const wrap = el(doc, 'div', { className: 'bb-world-flow' });
516
+ const bar = el(doc, 'div', { className: 'bb-world-flow-bar', attrs: { role: 'img', 'aria-label': FLOW_SEGMENTS.map((segment) => `${segment.label}${counts[segment.key]}件`).join('、') } });
517
+ for (const segment of FLOW_SEGMENTS) {
518
+ if (!counts[segment.key]) continue;
519
+ const part = el(doc, 'span', { className: `bb-world-flow-part is-${segment.key}` });
520
+ part.style.flexGrow = String(counts[segment.key]);
521
+ bar.append(part);
522
+ }
523
+ wrap.append(bar);
524
+ if (legend) {
525
+ const list = el(doc, 'ul', { className: 'bb-world-flow-legend' });
526
+ for (const segment of FLOW_SEGMENTS) {
527
+ const item = el(doc, 'li', { className: `is-${segment.key}${counts[segment.key] ? '' : ' is-none'}` });
528
+ item.append(el(doc, 'span', { className: 'bb-world-flow-dot', attrs: { 'aria-hidden': 'true' } }), el(doc, 'span', { text: segment.label }), el(doc, 'strong', { text: String(counts[segment.key]) }));
529
+ list.append(item);
530
+ }
531
+ wrap.append(list);
532
+ }
533
+ return wrap;
534
+ }
535
+
536
+ /** The mark of an agent: its soft colour with the first letter of its name, and a light for how it is there. */
537
+ export function actorMark(doc, actor, { small = false } = {}) {
538
+ const mark = el(doc, 'span', { className: `bb-world-actor-mark is-${actor.presence}${small ? ' is-small' : ''}`, text: [...String(actor.name)][0] ?? '?', attrs: { title: `${actor.name}:${actor.label}` } });
539
+ mark.style.setProperty('--bb-world-actor-tone', actor.tone);
540
+ return mark;
541
+ }
542
+
543
+ function presenceDetail(work) {
544
+ if (work.presence === 'running') return work.minutes === 0 ? 'たったいま稼働の知らせ' : `${work.minutes}分前に稼働の知らせ`;
545
+ if (work.presence === 'quiet') return `最後の稼働の知らせは${work.minutes >= 60 ? `${Math.floor(work.minutes / 60)}時間前` : `${work.minutes}分前`}`;
546
+ if (work.presence === 'waiting') return work.waiting_on ? `待ち:${work.waiting_on}` : '待ちの理由は未記録';
547
+ if (work.presence === 'unconfirmed') return '進行中の記録はあるが、稼働の知らせは確認できていない';
548
+ if (work.presence === 'queued') return 'まだ始めていない';
549
+ return '記録上は完了';
550
+ }
551
+
552
+ function pulseWords(counts) {
553
+ const parts = [];
554
+ if (counts.in_progress) parts.push(['in_progress', `進行中 ${counts.in_progress}`]);
555
+ if (counts.waiting) parts.push(['waiting', `待ち ${counts.waiting}`]);
556
+ if (!parts.length && counts.pending) parts.push(['pending', `これから ${counts.pending}`]);
557
+ return parts;
558
+ }
559
+
560
+ function pulseChips(doc, counts) {
561
+ const chips = el(doc, 'span', { className: 'bb-world-pulse-chips' });
562
+ for (const [key, text] of pulseWords(counts)) chips.append(el(doc, 'span', { className: `bb-world-pulse-chip is-${key}`, text }));
563
+ return chips;
564
+ }
565
+
566
+ /** The pulse of a business in a few words, for the city's nameplate; empty when nothing is open. */
567
+ export function cityPulseChips(doc, pulse) {
568
+ return pulse ? pulseChips(doc, pulse) : null;
569
+ }
570
+
571
+ export function createPulsePanel(doc, { onWorld, onCity, onStreet, onSite, onEnterCity, onFold } = {}) {
572
+ const node = el(doc, 'section', { className: 'bb-world-pulse', attrs: { 'aria-label': 'いまの動き' } });
573
+ const top = el(doc, 'div', { className: 'bb-world-pulse-top' });
574
+ const crumbs = el(doc, 'nav', { className: 'bb-world-pulse-crumbs', attrs: { 'aria-label': '見ている階層' } });
575
+ const fold = el(doc, 'button', { className: 'bb-world-pulse-fold', attrs: { type: 'button', 'aria-expanded': 'true' } });
576
+ const body = el(doc, 'div', { className: 'bb-world-pulse-body' });
577
+ top.append(crumbs, fold);
578
+ node.append(top, body);
579
+ // Pointer activity in the panel must never start the map's drag or picking gesture underneath.
580
+ for (const name of ['pointerdown', 'pointerup', 'click', 'dblclick', 'wheel']) node.addEventListener?.(name, (event) => event.stopPropagation?.());
581
+ let folded = false;
582
+ try {
583
+ folded = globalThis.localStorage?.getItem('brainbase.world.pulse.folded') === '1';
584
+ } catch {
585
+ folded = false;
586
+ }
587
+ function applyFold() {
588
+ node.classList.toggle('is-folded', folded);
589
+ body.hidden = folded;
590
+ fold.setAttribute('aria-expanded', String(!folded));
591
+ fold.setAttribute('aria-label', folded ? 'いまの動きを開く' : 'いまの動きをたたむ');
592
+ fold.title = folded ? 'いまの動きを開く' : 'いまの動きをたたむ';
593
+ fold.textContent = folded ? '+' : '-';
594
+ }
595
+ fold.addEventListener('click', () => {
596
+ folded = !folded;
597
+ try {
598
+ globalThis.localStorage?.setItem('brainbase.world.pulse.folded', folded ? '1' : '0');
599
+ } catch {
600
+ // Without storage the panel simply opens unfolded next time.
601
+ }
602
+ applyFold();
603
+ onFold?.(folded);
604
+ });
605
+ applyFold();
606
+
607
+ function setCrumbs(entries) {
608
+ const nodes = [];
609
+ entries.forEach((entry, index) => {
610
+ if (index) nodes.push(el(doc, 'span', { className: 'bb-world-pulse-sep', text: '›', attrs: { 'aria-hidden': 'true' } }));
611
+ const last = index === entries.length - 1;
612
+ if (last || !entry.onClick) {
613
+ nodes.push(el(doc, 'span', { className: 'bb-world-pulse-crumb is-current', text: entry.text, attrs: last ? { 'aria-current': 'location' } : {} }));
614
+ } else {
615
+ const button = el(doc, 'button', { className: 'bb-world-pulse-crumb', text: entry.text, attrs: { type: 'button', title: `${entry.text}に戻る` } });
616
+ button.addEventListener('click', () => entry.onClick());
617
+ nodes.push(button);
618
+ }
619
+ });
620
+ crumbs.replaceChildren(...nodes);
621
+ }
622
+ const heading = (title, sub) => {
623
+ const head = el(doc, 'div', { className: 'bb-world-pulse-head' });
624
+ head.append(el(doc, 'h2', { text: title }));
625
+ if (sub) head.append(el(doc, 'p', { text: sub }));
626
+ return head;
627
+ };
628
+ const section = (title, ...children) => {
629
+ const wrap = el(doc, 'div', { className: 'bb-world-pulse-section' });
630
+ if (title) wrap.append(el(doc, 'h3', { text: title }));
631
+ wrap.append(...children.filter(Boolean));
632
+ return wrap;
633
+ };
634
+ const note = (text) => el(doc, 'p', { className: 'bb-world-pulse-note', text });
635
+ const rowButton = (className, onClick, title) => {
636
+ const button = el(doc, 'button', { className, attrs: { type: 'button', title } });
637
+ button.addEventListener('click', onClick);
638
+ return button;
639
+ };
640
+
641
+ function actorsLine(pulse) {
642
+ if (!pulse.actors.length) {
643
+ return note(pulse.actors_unconfirmed ? `AIの担当を確認できていない仕事が${pulse.actors_unconfirmed}件あります(いないとは確認できません)。` : 'AIの担当が記録された仕事はありません。');
644
+ }
645
+ const line = el(doc, 'div', { className: 'bb-world-pulse-actors' });
646
+ const marks = el(doc, 'span', { className: 'bb-world-actor-marks' });
647
+ for (const actor of pulse.actors) marks.append(actorMark(doc, actor));
648
+ line.append(marks, el(doc, 'span', { text: `AI ${pulse.actors.length}体のうち${pulse.running}体が動いている` }));
649
+ const wrap = el(doc, 'div');
650
+ wrap.append(line);
651
+ if (pulse.actors_unconfirmed) wrap.append(note(`ほかに、AIの担当を確認できていない仕事が${pulse.actors_unconfirmed}件あります。`));
652
+ return wrap;
653
+ }
654
+
655
+ return {
656
+ node,
657
+ hide() { node.hidden = true; },
658
+ /** The world: `world` from `worldPulse`; `connected` false when the host has no task store. */
659
+ showWorld({ world, connected }) {
660
+ node.hidden = false;
661
+ node.dataset.level = 'world';
662
+ setCrumbs([{ text: '世界' }]);
663
+ const nodes = [];
664
+ if (connected === false) {
665
+ nodes.push(heading('世界のいま'), note('この画面は仕事の記録につながっていないため、事業ごとの動きは出していません(動きが無いとは確認できません)。'));
666
+ } else if (!world.rows.length) {
667
+ nodes.push(heading('世界のいま'), note('事業を読めていません。'));
668
+ } else {
669
+ nodes.push(heading('世界のいま', world.read ? `仕事が進行中の事業 ${world.active} / ${world.read}${world.read < world.rows.length ? `(読めない事業 ${world.rows.length - world.read})` : ''}` : '仕事の動きを読んでいます'));
670
+ const list = el(doc, 'ul', { className: 'bb-world-pulse-list' });
671
+ for (const row of world.rows) {
672
+ const item = el(doc, 'li');
673
+ const button = rowButton('bb-world-pulse-row', () => onEnterCity?.(row.code), `${row.name}の中を見る`);
674
+ button.append(el(doc, 'span', { className: 'bb-world-pulse-name', text: row.name }));
675
+ if (!row.pulse) button.append(el(doc, 'span', { className: 'bb-world-pulse-quiet', text: world.read ? '読めない' : '' }));
676
+ else if (pulseWords(row.pulse).length) button.append(pulseChips(doc, row.pulse));
677
+ else button.append(el(doc, 'span', { className: 'bb-world-pulse-quiet', text: row.pulse.done ? '開いた仕事なし' : '仕事の記録なし' }));
678
+ item.append(button);
679
+ list.append(item);
680
+ }
681
+ nodes.push(list, note('事業を選ぶと、その中の動きを見られます。'));
682
+ }
683
+ body.replaceChildren(...nodes);
684
+ },
685
+ /** A business: `pulse` from `districtPulse`, or `state` saying why its work is not shown. */
686
+ showCity({ business, pulse = null, state = null, partial = false }) {
687
+ node.hidden = false;
688
+ node.dataset.level = 'city';
689
+ setCrumbs([{ text: '世界', onClick: onWorld }, { text: business.name }]);
690
+ const nodes = [heading(`${business.name}のいま`, business.purpose ?? null)];
691
+ if (!pulse) {
692
+ nodes.push(note(state ?? '仕事を読んでいます'));
693
+ } else if (!pulse.counts.total) {
694
+ nodes.push(note('この事業の仕事の記録はまだありません。'));
695
+ } else {
696
+ nodes.push(section(`仕事 ${pulse.counts.total}件${partial ? '(読めた範囲)' : ''}`, flowBar(doc, pulse.counts), actorsLine(pulse), partial ? note('仕事の記録は一部だけ読めています。件数は読めた範囲のものです。') : null));
697
+ const list = el(doc, 'ul', { className: 'bb-world-pulse-list' });
698
+ for (const street of pulse.streets) {
699
+ const item = el(doc, 'li');
700
+ const name = street.label ?? '何のためか未記録の仕事';
701
+ const button = rowButton('bb-world-pulse-row is-street', () => onStreet?.(street.key), `「${name}」の仕事とAIを見る`);
702
+ const line = el(doc, 'span', { className: 'bb-world-pulse-row-line' });
703
+ line.append(el(doc, 'span', { className: `bb-world-pulse-name${street.label ? '' : ' is-unlabelled'}`, text: name }), el(doc, 'span', { className: 'bb-world-pulse-count', text: `${street.counts.done} / ${street.counts.total}` }));
704
+ const second = el(doc, 'span', { className: 'bb-world-pulse-row-line' });
705
+ second.append(flowBar(doc, street.counts, { legend: false }));
706
+ const marks = el(doc, 'span', { className: 'bb-world-actor-marks' });
707
+ for (const actor of street.actors) marks.append(actorMark(doc, actor, { small: true }));
708
+ second.append(marks);
709
+ button.append(line, second, pulseChips(doc, street.counts));
710
+ item.append(button);
711
+ list.append(item);
712
+ }
713
+ nodes.push(section('仕事の集まり(通り)', list));
714
+ }
715
+ body.replaceChildren(...nodes);
716
+ },
717
+ /** A gathering of work: one `street` of the pulse and the `sites` standing on it. */
718
+ showStreet({ business, street, sites, partial = false }) {
719
+ node.hidden = false;
720
+ node.dataset.level = 'street';
721
+ const name = street.label ?? '何のためか未記録の仕事';
722
+ setCrumbs([{ text: '世界', onClick: onWorld }, { text: business.name, onClick: onCity }, { text: name }]);
723
+ const nodes = [heading(name, `${business.name}・${street.counts.total}件のうち${street.counts.done}件を終えた${partial ? '(読めた範囲)' : ''}`), flowBar(doc, street.counts)];
724
+ const byId = new Map(sites.map((site) => [site.task_id, site]));
725
+ if (street.actors.length) {
726
+ const cards = el(doc, 'ul', { className: 'bb-world-actor-cards' });
727
+ for (const actor of street.actors) {
728
+ const card = el(doc, 'li', { className: `bb-world-actor-card is-${actor.presence}` });
729
+ const head = el(doc, 'div', { className: 'bb-world-actor-head' });
730
+ head.append(actorMark(doc, actor), el(doc, 'strong', { text: actor.name }), el(doc, 'span', { className: `bb-world-presence is-${actor.presence}`, text: actor.label }));
731
+ card.append(head);
732
+ const works = el(doc, 'ul', { className: 'bb-world-actor-works' });
733
+ for (const work of actor.works) {
734
+ const item = el(doc, 'li');
735
+ const button = rowButton('bb-world-actor-work', () => onSite?.(work.task_id), 'この仕事の根拠を開く');
736
+ button.append(el(doc, 'span', { className: 'bb-world-actor-work-title', text: work.title }), el(doc, 'span', { className: `bb-world-actor-work-detail is-${work.presence}`, text: presenceDetail(work) }));
737
+ item.append(button);
738
+ works.append(item);
739
+ }
740
+ card.append(works);
741
+ cards.append(card);
742
+ }
743
+ nodes.push(section('ここにいるAI', cards));
744
+ } else {
745
+ nodes.push(section('ここにいるAI', note(street.actors_unconfirmed ? `AIの担当を確認できていない仕事が${street.actors_unconfirmed}件あります(いないとは確認できません)。` : 'この集まりに、AIの担当が記録された仕事はありません。')));
746
+ }
747
+ const withActor = new Set(street.actors.flatMap((actor) => actor.works.map((work) => work.task_id)));
748
+ const rest = street.task_ids.map((id) => byId.get(id)).filter((site) => site && !withActor.has(site.task_id));
749
+ if (rest.length) {
750
+ const list = el(doc, 'ul', { className: 'bb-world-pulse-list' });
751
+ const order = { in_progress: 0, waiting: 1, pending: 2, completed: 3 };
752
+ for (const site of [...rest].sort((a, b) => (order[a.work.status] ?? 2) - (order[b.work.status] ?? 2))) {
753
+ const item = el(doc, 'li');
754
+ const button = rowButton('bb-world-pulse-row is-work', () => onSite?.(site.task_id), 'この仕事の根拠を開く');
755
+ const people = site.people?.filter((person) => person.link === 'recorded').map((person) => person.name) ?? [];
756
+ const who = site.actors_state === 'unconfirmed' ? 'AIの担当は未確認' : people.length ? people.join('・') : '担当の記録なし';
757
+ button.append(el(doc, 'span', { className: 'bb-world-pulse-name', text: site.title }), el(doc, 'span', { className: 'bb-world-pulse-who', text: `${site.work.label}・${who}` }));
758
+ item.append(button);
759
+ list.append(item);
760
+ }
761
+ nodes.push(section('AIのいない仕事', list));
762
+ }
763
+ body.replaceChildren(...nodes);
764
+ },
765
+ };
766
+ }