@celljs/ta-app-use 4.17.0 → 4.18.0

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/web/index.js CHANGED
@@ -297,6 +297,20 @@ var TA_WEB_SETTINGS = [
297
297
  title: words("Model of tasks", "任务的模型", "任務的模型", "タスクのモデル")
298
298
  },
299
299
  { key: "reduceMotion", type: "boolean", default: false, what: "Fewer animations.", title: words("Reduce motion", "减少动画", "減少動畫", "動きを減らす") },
300
+ {
301
+ key: "diagramStyle",
302
+ type: "enum",
303
+ values: ["clean", "sketch", "colour", "motion"],
304
+ default: "clean",
305
+ what: "How diagrams in replies draw: in the colours of the page, as by hand, a colour to a node, or coming in with flowing lines.",
306
+ title: words("Diagram style", "图表风格", "圖表風格", "図のスタイル"),
307
+ shown: {
308
+ clean: words("clean", "简洁", "簡潔", "シンプル"),
309
+ sketch: words("sketch", "草图", "草圖", "スケッチ"),
310
+ colour: words("colour", "彩色", "彩色", "カラー"),
311
+ motion: words("motion", "动效", "動效", "モーション")
312
+ }
313
+ },
300
314
  {
301
315
  key: "keepAliveMinutes",
302
316
  type: "number",
@@ -367,6 +381,7 @@ var DONE_KEY = "done";
367
381
  var DONE_MAX = 20;
368
382
 
369
383
  // web-src/app-util.ts
384
+ var SCROLL_STEPS = ["down", "up", "top", "bottom"];
370
385
  var DATA_CHARS = 8000;
371
386
  var AppUseUtil;
372
387
  ((AppUseUtil) => {
@@ -407,6 +422,25 @@ var AppUseUtil;
407
422
  `);
408
423
  }
409
424
  AppUseUtil.resultText = resultText;
425
+ function scroll(value) {
426
+ if (value === undefined || value === null || value === "") {
427
+ return;
428
+ }
429
+ if (typeof value === "string" && SCROLL_STEPS.includes(value.trim().toLowerCase())) {
430
+ return value.trim().toLowerCase();
431
+ }
432
+ if (value && typeof value === "object" && !Array.isArray(value)) {
433
+ const { item, find: find2 } = value;
434
+ if (typeof item === "string" && item.trim() && find2 === undefined) {
435
+ return { item: item.trim() };
436
+ }
437
+ if (typeof find2 === "string" && find2.trim() && item === undefined) {
438
+ return { find: find2.trim() };
439
+ }
440
+ }
441
+ throw new Error(`scroll takes ${SCROLL_STEPS.join(", ")}, { "item": "p1.7" } or { "find": "words of a row" }, not ${JSON.stringify(value)}.`);
442
+ }
443
+ AppUseUtil.scroll = scroll;
410
444
  function add(done, entry) {
411
445
  return [...entries(done), entry].slice(-DONE_MAX);
412
446
  }
@@ -439,6 +473,15 @@ var EN = {
439
473
  look: "Look at the screen",
440
474
  go: "Go to",
441
475
  do: "Do on the screen",
476
+ scroll: "Scroll",
477
+ "scroll.down": "down",
478
+ "scroll.up": "up",
479
+ "scroll.top": "to the top",
480
+ "scroll.bottom": "to the bottom",
481
+ "game.point": "Tap in the game",
482
+ "game.move": "Move in the game",
483
+ "game.key": "Press",
484
+ "game.hold": "Hold",
442
485
  screen: "Ta's screen",
443
486
  operating: "Ta is at work",
444
487
  watch: "Watch",
@@ -468,6 +511,15 @@ var ZH = {
468
511
  look: "看看屏幕",
469
512
  go: "打开",
470
513
  do: "在屏幕上操作",
514
+ scroll: "滚动",
515
+ "scroll.down": "向下",
516
+ "scroll.up": "向上",
517
+ "scroll.top": "到顶部",
518
+ "scroll.bottom": "到底部",
519
+ "game.point": "在游戏里点一下",
520
+ "game.move": "在游戏里移动",
521
+ "game.key": "按下",
522
+ "game.hold": "按住",
471
523
  screen: "Ta 的屏幕",
472
524
  operating: "Ta 正在操作",
473
525
  watch: "看看",
@@ -497,6 +549,15 @@ var ZH_TW = {
497
549
  look: "看看螢幕",
498
550
  go: "打開",
499
551
  do: "在螢幕上操作",
552
+ scroll: "捲動",
553
+ "scroll.down": "向下",
554
+ "scroll.up": "向上",
555
+ "scroll.top": "到頂部",
556
+ "scroll.bottom": "到底部",
557
+ "game.point": "在遊戲裡點一下",
558
+ "game.move": "在遊戲裡移動",
559
+ "game.key": "按下",
560
+ "game.hold": "按住",
500
561
  screen: "Ta 的螢幕",
501
562
  operating: "Ta 正在操作",
502
563
  watch: "看看",
@@ -526,6 +587,15 @@ var JA = {
526
587
  look: "画面を見る",
527
588
  go: "開く",
528
589
  do: "画面で操作",
590
+ scroll: "スクロール",
591
+ "scroll.down": "下へ",
592
+ "scroll.up": "上へ",
593
+ "scroll.top": "先頭へ",
594
+ "scroll.bottom": "末尾へ",
595
+ "game.point": "ゲームをタップ",
596
+ "game.move": "ゲームで移動",
597
+ "game.key": "押す",
598
+ "game.hold": "長押し",
529
599
  screen: "Ta の画面",
530
600
  operating: "Ta が操作中",
531
601
  watch: "見る",
@@ -550,58 +620,125 @@ var AppUseWords;
550
620
  // web-src/screen-cursor.ts
551
621
  var STYLE = `
552
622
  .ta-agent-layer { position: fixed; inset: 0; pointer-events: none; z-index: 2147483646; }
553
- .ta-agent-cursor { position: absolute; left: 0; top: 0; transform: translate3d(-40px, -40px, 0); transition: transform 300ms cubic-bezier(.2,.8,.2,1), opacity 200ms; opacity: 0; will-change: transform; }
623
+ .ta-agent-cursor { position: absolute; left: 0; top: 0; transform: translate3d(-40px, -40px, 0); opacity: 0; transition: opacity 180ms; will-change: transform; }
554
624
  .ta-agent-cursor[data-shown] { opacity: 1; }
555
- .ta-agent-cursor svg { display: block; filter: drop-shadow(0 2px 4px rgba(0,0,0,.25)); transition: transform 120ms; transform-origin: 3px 3px; }
556
- .ta-agent-cursor[data-press] svg { transform: scale(.86); }
557
- .ta-agent-caption { position: absolute; left: 22px; top: 20px; max-width: 260px; padding: 4px 9px; border-radius: 9px; background: var(--ta-accent, #07c160); color: #fff; font: 500 12px/1.4 var(--ta-sans, system-ui, sans-serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; box-shadow: 0 4px 12px rgba(0,0,0,.18); }
625
+ .ta-agent-cursor svg { display: block; filter: drop-shadow(0 2px 4px rgba(0,0,0,.25)); transition: transform 110ms ease-out; transform-origin: 3px 3px; }
626
+ .ta-agent-cursor[data-press] svg { transform: scale(.84); }
627
+ .ta-agent-touch { display: none; position: absolute; left: -15px; top: -15px; width: 30px; height: 30px; border-radius: 50%; background: color-mix(in srgb, var(--ta-accent, #07c160) 30%, transparent); border: 2px solid #fff; box-shadow: 0 0 0 2px var(--ta-accent, #07c160), 0 2px 8px rgba(0,0,0,.25); transition: transform 120ms ease-out, opacity 200ms; }
628
+ .ta-agent-layer[data-touch] .ta-agent-cursor svg { display: none; }
629
+ .ta-agent-layer[data-touch] .ta-agent-touch { display: block; }
630
+ .ta-agent-cursor[data-press] .ta-agent-touch { transform: scale(.8); }
631
+ .ta-agent-cursor[data-lifted] .ta-agent-touch { transform: scale(.6); opacity: 0; }
632
+ .ta-agent-caption { position: absolute; left: 22px; top: 20px; max-width: min(260px, calc(100vw - 16px)); padding: 4px 9px; border-radius: 9px; background: var(--ta-accent, #07c160); color: #fff; font: 500 12px/1.4 var(--ta-sans, system-ui, sans-serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; box-shadow: 0 4px 12px rgba(0,0,0,.18); transition: opacity 200ms; }
558
633
  .ta-agent-caption:empty { display: none; }
559
- .ta-agent-ring { position: absolute; border-radius: 10px; box-shadow: 0 0 0 2px var(--ta-accent, #07c160), 0 0 0 6px color-mix(in srgb, var(--ta-accent, #07c160) 22%, transparent); opacity: 0; transition: opacity 160ms, left 300ms cubic-bezier(.2,.8,.2,1), top 300ms cubic-bezier(.2,.8,.2,1), width 300ms, height 300ms; }
560
- .ta-agent-ring[data-shown] { opacity: 1; }
634
+ .ta-agent-cursor[data-quiet] .ta-agent-caption { opacity: 0; }
635
+ .ta-agent-ring { position: absolute; left: 0; top: 0; border-radius: 10px; box-shadow: 0 0 0 2px var(--ta-accent, #07c160), 0 0 0 6px color-mix(in srgb, var(--ta-accent, #07c160) 22%, transparent); opacity: 0; transition: opacity 200ms; will-change: transform; }
636
+ .ta-agent-ring[data-shown] { opacity: 1; animation: ta-agent-ring-in 240ms cubic-bezier(.2,.8,.2,1); }
637
+ @keyframes ta-agent-ring-in { from { scale: 1.08; } }
561
638
  .ta-agent-ripple { position: absolute; width: 36px; height: 36px; margin: -18px 0 0 -18px; border-radius: 50%; background: color-mix(in srgb, var(--ta-accent, #07c160) 35%, transparent); animation: ta-agent-ripple 420ms ease-out forwards; }
562
639
  @keyframes ta-agent-ripple { from { transform: scale(.2); opacity: 1; } to { transform: scale(1.6); opacity: 0; } }
563
- @media (prefers-reduced-motion: reduce) { .ta-agent-cursor, .ta-agent-ring { transition: none; } .ta-agent-ripple { animation-duration: 1ms; } }
640
+ @media (prefers-reduced-motion: reduce) { .ta-agent-ring[data-shown] { animation: none; } .ta-agent-ripple { animation-duration: 1ms; } }
564
641
  `;
565
642
  var ARROW = '<svg width="22" height="22" viewBox="0 0 22 22"><path d="M3 2.5 L18 11 L11 12.6 L7.6 19.5 Z" fill="var(--ta-accent, #07c160)" stroke="#fff" stroke-width="1.6" stroke-linejoin="round"/></svg>';
566
643
  var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
644
+ var EASE = "cubic-bezier(.35,.05,.2,1)";
645
+ var CursorPath;
646
+ ((CursorPath) => {
647
+ function duration(distance) {
648
+ return Math.round(Math.min(640, Math.max(200, 160 + Math.sqrt(distance) * 16)));
649
+ }
650
+ CursorPath.duration = duration;
651
+ function arc(from, to, count = 12) {
652
+ const dx = to.x - from.x;
653
+ const dy = to.y - from.y;
654
+ const distance = Math.hypot(dx, dy);
655
+ const bow = Math.min(60, distance * 0.12) * (dx >= 0 ? 1 : -1);
656
+ const control = { x: (from.x + to.x) / 2 + (distance ? -dy / distance * bow : 0), y: (from.y + to.y) / 2 + (distance ? dx / distance * bow : 0) };
657
+ const points = [];
658
+ for (let index = 0;index <= count; index++) {
659
+ const t = index / count;
660
+ const a = (1 - t) * (1 - t);
661
+ const b = 2 * (1 - t) * t;
662
+ const c = t * t;
663
+ points.push({ x: a * from.x + b * control.x + c * to.x, y: a * from.y + b * control.y + c * to.y });
664
+ }
665
+ return points;
666
+ }
667
+ CursorPath.arc = arc;
668
+ function caption(point, size, view, touch) {
669
+ let left = touch ? -Math.round(size.width / 2) : 22;
670
+ let top = touch ? 24 : 20;
671
+ if (touch) {
672
+ left = Math.min(Math.max(point.x + left, 8), Math.max(8, view.width - size.width - 8)) - point.x;
673
+ } else if (point.x + left + size.width > view.width - 8) {
674
+ left = Math.max(-point.x + 8, -size.width - 8);
675
+ }
676
+ if (point.y + top + size.height > view.height - 8) {
677
+ top = touch ? -size.height - 24 : -size.height - 6;
678
+ }
679
+ return { left, top };
680
+ }
681
+ CursorPath.caption = caption;
682
+ function gone(from, to, view) {
683
+ const x = to.left + to.width / 2;
684
+ const y = to.top + to.height / 2;
685
+ return Math.abs(to.left - from.left) > 4 || Math.abs(to.width - from.width) > 4 || x < 0 || x > view.width || y < 0 || y > view.height;
686
+ }
687
+ CursorPath.gone = gone;
688
+ })(CursorPath ||= {});
567
689
 
568
690
  class ScreenCursor {
569
691
  document;
692
+ touch;
693
+ linger;
570
694
  layer;
571
695
  pointer;
572
696
  caption;
573
697
  ring;
574
698
  hideTimer;
699
+ ringTimer;
700
+ quietTimer;
575
701
  at = { x: -40, y: -40 };
576
- constructor(document2) {
702
+ ringed;
703
+ following = false;
704
+ placed = "";
705
+ anchor;
706
+ constructor(document2, touch = () => false, linger = 1600) {
577
707
  this.document = document2;
708
+ this.touch = touch;
709
+ this.linger = linger;
578
710
  }
579
- async point(element, says) {
711
+ async point(element, says, spot) {
580
712
  this.mount();
581
- clearTimeout(this.hideTimer);
582
- const view = this.document.defaultView;
583
- let rect = element.getBoundingClientRect();
584
- if (view && (rect.top < 0 || rect.bottom > view.innerHeight || rect.left < 0 || rect.right > view.innerWidth)) {
585
- element.scrollIntoView({ block: "center", inline: "nearest", behavior: "smooth" });
586
- await wait(320);
587
- rect = element.getBoundingClientRect();
588
- }
589
- const x = rect.left + Math.min(rect.width / 2, 40);
590
- const y = rect.top + rect.height / 2;
713
+ for (const timer of [this.hideTimer, this.ringTimer, this.quietTimer]) {
714
+ clearTimeout(timer);
715
+ }
716
+ const touch = this.touch();
717
+ this.layer?.toggleAttribute("data-touch", touch);
718
+ const rect = await this.inSight(element);
719
+ const x = rect.left + (spot ? spot.x * rect.width : Math.min(rect.width / 2, touch ? 64 : 40));
720
+ const y = rect.top + (spot ? spot.y : 0.5) * rect.height;
591
721
  const { pointer, caption, ring } = this.parts();
722
+ delete pointer.dataset.quiet;
723
+ delete pointer.dataset.lifted;
592
724
  caption.textContent = says;
593
- if (this.at.x < 0) {
594
- pointer.style.transition = "none";
595
- pointer.style.transform = `translate3d(${x + 60}px, ${y + 50}px, 0)`;
596
- pointer.getBoundingClientRect();
597
- pointer.style.transition = "";
725
+ this.placeCaption({ x, y }, touch);
726
+ if (touch) {
727
+ await this.land(x, y);
728
+ } else {
729
+ await this.glide(x, y);
598
730
  }
599
- pointer.dataset.shown = "";
600
- pointer.style.transform = `translate3d(${x}px, ${y}px, 0)`;
601
- Object.assign(ring.style, { left: `${rect.left - 3}px`, top: `${rect.top - 3}px`, width: `${rect.width + 6}px`, height: `${rect.height + 6}px` });
602
- ring.dataset.shown = "";
603
731
  this.at = { x, y };
604
- await wait(320);
732
+ if (spot) {
733
+ this.unring();
734
+ } else {
735
+ this.ringed = element;
736
+ this.anchor = element.getBoundingClientRect();
737
+ this.placeRing(this.anchor, true);
738
+ ring.dataset.shown = "";
739
+ this.follow();
740
+ }
741
+ await wait(110);
605
742
  }
606
743
  async press() {
607
744
  const { pointer } = this.parts();
@@ -610,37 +747,167 @@ class ScreenCursor {
610
747
  ripple.className = "ta-agent-ripple";
611
748
  Object.assign(ripple.style, { left: `${this.at.x}px`, top: `${this.at.y}px` });
612
749
  this.layer?.appendChild(ripple);
613
- await wait(140);
750
+ await wait(130);
614
751
  delete pointer.dataset.press;
752
+ if (this.layer?.hasAttribute("data-touch")) {
753
+ pointer.dataset.lifted = "";
754
+ }
615
755
  setTimeout(() => ripple.remove(), 500);
616
756
  }
617
757
  async type(text, set) {
618
- const slow = Math.min(text.length, 32);
758
+ const letters = [...text];
759
+ const slow = Math.min(letters.length, 32);
619
760
  for (let index = 1;index <= slow; index++) {
620
- set(text.slice(0, index));
621
- await wait(18);
761
+ set(letters.slice(0, index).join(""));
762
+ await wait(16 + index * 7 % 13);
622
763
  }
623
- if (text.length > slow) {
764
+ if (letters.length > slow) {
624
765
  set(text);
625
766
  }
626
767
  }
627
768
  rest() {
628
- clearTimeout(this.hideTimer);
629
- this.hideTimer = setTimeout(() => {
769
+ for (const timer of [this.hideTimer, this.ringTimer, this.quietTimer]) {
770
+ clearTimeout(timer);
771
+ }
772
+ this.ringTimer = setTimeout(() => this.unring(), 900);
773
+ this.quietTimer = setTimeout(() => {
630
774
  if (this.pointer) {
631
- delete this.pointer.dataset.shown;
632
- }
633
- if (this.ring) {
634
- delete this.ring.dataset.shown;
775
+ this.pointer.dataset.quiet = "";
635
776
  }
636
- this.at = { x: -40, y: -40 };
637
777
  }, 1600);
778
+ this.hideTimer = setTimeout(() => this.hide(), Math.max(this.linger, 1600));
779
+ }
780
+ hide() {
781
+ for (const timer of [this.hideTimer, this.ringTimer, this.quietTimer]) {
782
+ clearTimeout(timer);
783
+ }
784
+ if (this.pointer) {
785
+ delete this.pointer.dataset.shown;
786
+ }
787
+ this.unring();
788
+ this.at = { x: -40, y: -40 };
638
789
  }
639
790
  dispose() {
640
- clearTimeout(this.hideTimer);
791
+ this.hide();
641
792
  this.layer?.remove();
642
793
  this.layer = undefined;
643
794
  }
795
+ reduced() {
796
+ return this.document.defaultView?.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
797
+ }
798
+ async inSight(element) {
799
+ const view = this.document.defaultView;
800
+ let rect = element.getBoundingClientRect();
801
+ if (!view || rect.top >= 0 && rect.bottom <= view.innerHeight && rect.left >= 0 && rect.right <= view.innerWidth) {
802
+ return rect;
803
+ }
804
+ element.scrollIntoView({ block: "center", inline: "nearest", behavior: this.reduced() ? "auto" : "smooth" });
805
+ const until = Date.now() + 900;
806
+ let still = 0;
807
+ while (Date.now() < until && still < 3) {
808
+ await wait(32);
809
+ const next = element.getBoundingClientRect();
810
+ still = Math.abs(next.top - rect.top) < 0.5 && Math.abs(next.left - rect.left) < 0.5 ? still + 1 : 0;
811
+ rect = next;
812
+ }
813
+ return rect;
814
+ }
815
+ async glide(x, y) {
816
+ const pointer = this.pointer;
817
+ const to = `translate3d(${x}px, ${y}px, 0)`;
818
+ const from = this.at.x < 0 ? { x: x + 70, y: y + 56 } : this.at;
819
+ const distance = Math.hypot(x - from.x, y - from.y);
820
+ pointer.dataset.shown = "";
821
+ if (this.reduced() || distance < 2 || typeof pointer.animate !== "function") {
822
+ pointer.style.transform = to;
823
+ return;
824
+ }
825
+ for (const running of pointer.getAnimations?.() ?? []) {
826
+ running.cancel();
827
+ }
828
+ const duration = CursorPath.duration(distance);
829
+ const frames = CursorPath.arc(from, { x, y }).map((point, index, all) => ({
830
+ transform: `translate3d(${point.x}px, ${point.y}px, 0)`,
831
+ offset: index / (all.length - 1)
832
+ }));
833
+ pointer.style.transform = to;
834
+ const motion = pointer.animate(frames, { duration, easing: EASE });
835
+ await Promise.race([motion.finished.catch(() => {
836
+ return;
837
+ }), wait(duration + 80)]);
838
+ }
839
+ async land(x, y) {
840
+ const pointer = this.pointer;
841
+ for (const running of pointer.getAnimations?.({ subtree: true }) ?? []) {
842
+ running.cancel();
843
+ }
844
+ pointer.style.transform = `translate3d(${x}px, ${y}px, 0)`;
845
+ pointer.dataset.shown = "";
846
+ const dot = pointer.querySelector(".ta-agent-touch");
847
+ if (dot && !this.reduced() && typeof dot.animate === "function") {
848
+ dot.animate([
849
+ { transform: "scale(1.6)", opacity: 0 },
850
+ { transform: "scale(1)", opacity: 1 }
851
+ ], { duration: 170, easing: "cubic-bezier(.2,.8,.2,1)" });
852
+ await wait(170);
853
+ }
854
+ }
855
+ placeCaption(point, touch) {
856
+ const caption = this.caption;
857
+ const view = this.document.defaultView;
858
+ Object.assign(caption.style, { left: "", top: "" });
859
+ if (!view || !caption.textContent) {
860
+ return;
861
+ }
862
+ const { left, top } = CursorPath.caption(point, { width: caption.offsetWidth, height: caption.offsetHeight }, { width: view.innerWidth, height: view.innerHeight }, touch);
863
+ Object.assign(caption.style, { left: `${left}px`, top: `${top}px` });
864
+ }
865
+ placeRing(rect, now = false) {
866
+ const ring = this.ring;
867
+ const key = `${Math.round(rect.left)},${Math.round(rect.top)},${Math.round(rect.width)},${Math.round(rect.height)}`;
868
+ if (!now && key === this.placed) {
869
+ return;
870
+ }
871
+ this.placed = key;
872
+ Object.assign(ring.style, {
873
+ transform: `translate3d(${rect.left - 3}px, ${rect.top - 3}px, 0)`,
874
+ width: `${rect.width + 6}px`,
875
+ height: `${rect.height + 6}px`
876
+ });
877
+ }
878
+ follow() {
879
+ const view = this.document.defaultView;
880
+ if (this.following || !view?.requestAnimationFrame) {
881
+ return;
882
+ }
883
+ this.following = true;
884
+ const step = () => {
885
+ const element = this.ringed;
886
+ if (!element || !this.ring?.hasAttribute("data-shown")) {
887
+ this.following = false;
888
+ return;
889
+ }
890
+ const rect = element.getBoundingClientRect();
891
+ if (!element.isConnected || this.anchor && CursorPath.gone(this.anchor, rect, { width: view.innerWidth, height: view.innerHeight })) {
892
+ this.following = false;
893
+ this.unring();
894
+ if (this.pointer) {
895
+ this.pointer.dataset.quiet = "";
896
+ }
897
+ return;
898
+ }
899
+ this.placeRing(rect);
900
+ view.requestAnimationFrame(step);
901
+ };
902
+ view.requestAnimationFrame(step);
903
+ }
904
+ unring() {
905
+ this.ringed = undefined;
906
+ this.anchor = undefined;
907
+ if (this.ring) {
908
+ delete this.ring.dataset.shown;
909
+ }
910
+ }
644
911
  parts() {
645
912
  this.mount();
646
913
  return { pointer: this.pointer, caption: this.caption, ring: this.ring };
@@ -664,9 +931,11 @@ class ScreenCursor {
664
931
  const pointer = document2.createElement("div");
665
932
  pointer.className = "ta-agent-cursor";
666
933
  pointer.innerHTML = ARROW;
934
+ const touch = document2.createElement("div");
935
+ touch.className = "ta-agent-touch";
667
936
  const caption = document2.createElement("div");
668
937
  caption.className = "ta-agent-caption";
669
- pointer.appendChild(caption);
938
+ pointer.append(touch, caption);
670
939
  layer.append(ring, pointer);
671
940
  document2.body.appendChild(layer);
672
941
  Object.assign(this, { layer, pointer, caption, ring });
@@ -711,7 +980,7 @@ var ScreenDom;
711
980
  const controls2 = [];
712
981
  const elements = [];
713
982
  for (const element of root.querySelectorAll(CONTROLS)) {
714
- if (element.closest("[data-ta-item], [data-ta-action], .ta-agent-layer, .ta-rail-item") || element.closest('[aria-hidden="true"], [inert]') || !visible(element)) {
983
+ if (element.closest("[data-ta-item], [data-ta-action], [data-ta-skip], .ta-agent-layer, .ta-screen-box, .ta-screen-pill, .ta-rail-item") || element.closest('[aria-hidden="true"], [inert]') || !visible(element)) {
715
984
  continue;
716
985
  }
717
986
  const control = read(element);
@@ -812,23 +1081,125 @@ var ScreenDom;
812
1081
 
813
1082
  // web-src/screen-protocol.ts
814
1083
  var LOOK_MAX = 3000;
1084
+ var OLD_MAX = 200;
815
1085
  var CONTROLS_MAX = 50;
816
1086
 
817
1087
  // web-src/screen-text.ts
818
1088
  var ScreenText;
819
1089
  ((ScreenText) => {
820
1090
  const cut = (text, length) => text.length > length ? `${text.slice(0, length - 1)}…` : text;
1091
+ const HEAD = /^(The user's screen|Ta's own screen) \(/m;
1092
+ function placeNames(places) {
1093
+ return places.map((place) => place.title && places.filter((one) => one.title === place.title).length === 1 ? place.title : `${place.id}${place.title ? ` “${place.title}”` : ""}`);
1094
+ }
1095
+ function scrolled(scrollable) {
1096
+ const { at, offset, total, shown } = scrollable.position;
1097
+ const span = total !== undefined ? `rows ${offset}–${offset + Math.max(1, shown ?? 1) - 1} of ${total}` : `${offset}% down`;
1098
+ const where = at === "top" ? "at the top" : at === "bottom" ? "at the bottom" : "in the middle";
1099
+ const side = scrollable.more === "top" ? "above" : "below";
1100
+ const more = scrollable.more === "none" ? "all loaded" : scrollable.loading ? `more loading ${side}: look again` : scrollable.more ? `more ${side} as it scrolls ${scrollable.more === "top" ? "up" : "down"}` : "";
1101
+ return [span, where, more].filter(Boolean).join(", ");
1102
+ }
1103
+ ScreenText.scrolled = scrolled;
1104
+ function rowId(partIndex, part, itemIndex) {
1105
+ const item = part.items[itemIndex];
1106
+ if (item?.at !== undefined) {
1107
+ return `p${partIndex + 1}.${item.at}`;
1108
+ }
1109
+ if (part.items.some((one) => one.at !== undefined)) {
1110
+ return `p${partIndex + 1}.f${part.items.slice(0, itemIndex + 1).filter((one) => one.at === undefined).length}`;
1111
+ }
1112
+ return `p${partIndex + 1}.${itemIndex + 1}`;
1113
+ }
1114
+ ScreenText.rowId = rowId;
1115
+ function whole(text) {
1116
+ return HEAD.test(text);
1117
+ }
1118
+ ScreenText.whole = whole;
1119
+ const heads = (line) => /^\[p\d+\]/.test(line) || line === "Also on the screen:" || /^ {2}Dialog “/.test(line);
1120
+ function changes(before, after) {
1121
+ if (!before || !whole(before) || !whole(after) || before.split(`
1122
+ `)[0] !== after.split(`
1123
+ `)[0]) {
1124
+ return;
1125
+ }
1126
+ const old = new Set(before.split(`
1127
+ `));
1128
+ const lines = after.split(`
1129
+ `);
1130
+ const now = new Set(lines);
1131
+ const body = lines.filter((line) => line.trim());
1132
+ const changed = body.filter((line) => !old.has(line));
1133
+ if (changed.length > Math.max(3, body.length * 0.4)) {
1134
+ return;
1135
+ }
1136
+ const gone = before.split(`
1137
+ `).filter((line) => line.trim() && !now.has(line)).length - changed.length;
1138
+ const out = [];
1139
+ let head;
1140
+ let said;
1141
+ for (const line of lines) {
1142
+ if (heads(line)) {
1143
+ head = line;
1144
+ }
1145
+ if (!line.trim() || old.has(line)) {
1146
+ continue;
1147
+ }
1148
+ if (head && head !== line && said !== head) {
1149
+ out.push(head);
1150
+ }
1151
+ out.push(line);
1152
+ said = head;
1153
+ }
1154
+ if (!out.length) {
1155
+ return gone > 0 ? `Nothing new on the screen; ${gone} line(s) went away.` : "Nothing else on the screen changed.";
1156
+ }
1157
+ return `What changed (the rest is as before, with the same ids):
1158
+ ${out.join(`
1159
+ `)}${gone > 0 ? `
1160
+ (${gone} more line(s) went away)` : ""}`;
1161
+ }
1162
+ ScreenText.changes = changes;
1163
+ function forget(messages, tools) {
1164
+ const textOf = (message) => Array.isArray(message.content) ? message.content.map((part) => part.type === "text" ? part.text ?? "" : "").join("") : typeof message.content === "string" ? message.content : "";
1165
+ const sight = (message) => message.role === "toolResult" && tools.includes(message.toolName ?? "");
1166
+ let last = -1;
1167
+ messages.forEach((message, index) => {
1168
+ if (sight(message) && whole(textOf(message))) {
1169
+ last = index;
1170
+ }
1171
+ });
1172
+ let cut2 = false;
1173
+ const out = messages.map((message, index) => {
1174
+ const text = index < last && sight(message) ? textOf(message) : "";
1175
+ if (text.length <= OLD_MAX) {
1176
+ return message;
1177
+ }
1178
+ cut2 = true;
1179
+ const first = text.split(`
1180
+ `).find((line) => line.trim()) ?? "";
1181
+ return { ...message, content: [{ type: "text", text: `${cut1(first)} … (an older sight of the screen, left out: a later one is below)` }] };
1182
+ });
1183
+ return cut2 ? out : undefined;
1184
+ }
1185
+ ScreenText.forget = forget;
1186
+ const cut1 = (line) => cut(line, 160);
821
1187
  function look(sight, only) {
822
1188
  const targets = new Map;
823
1189
  const lines = [];
824
1190
  if (!only) {
825
- lines.push(`${sight.whose === "user" ? "The user's screen" : "Ta's screen"} (${sight.device}): ${sight.where || "starting"}`);
1191
+ lines.push(`${sight.whose === "ta" ? "Ta's own screen" : "The user's screen"} (${sight.device}): ${sight.where || "starting"}`);
826
1192
  if (sight.places.length) {
827
- lines.push(`Places (app_go to): ${sight.places.map((place) => `${place.id} “${place.title}”`).join(" · ")} · chat:<name> a conversation`);
1193
+ lines.push(`Places (app_go to): ${placeNames(sight.places).join(" · ")} · chat:<name> a conversation`);
828
1194
  }
829
1195
  if (sight.headings.length) {
830
1196
  lines.push(`Headings: ${sight.headings.map((heading) => cut(heading, 60)).join(" · ")}`);
831
1197
  }
1198
+ const page = sight.scroll;
1199
+ const index = page?.part ? sight.parts.findIndex((part) => part.id === page.part) : -1;
1200
+ if (page) {
1201
+ lines.push(`Scrolls (app_do scroll, no id): ${index >= 0 ? `[p${index + 1}]` : `the page, ${scrolled(page.scrollable)}`}`);
1202
+ }
832
1203
  }
833
1204
  sight.parts.forEach((part, index) => {
834
1205
  const id = `p${index + 1}`;
@@ -836,9 +1207,12 @@ var ScreenText;
836
1207
  if (only && only !== part.id && only !== id) {
837
1208
  return;
838
1209
  }
839
- lines.push("", `[${id}] ${part.title}${part.state ? ` — ${part.state}` : ""}`);
1210
+ lines.push("", `[${id}] ${part.title}${part.state ? ` — ${part.state}` : ""}${part.floats ? " (over every page)" : ""}`);
1211
+ if (part.scrollable) {
1212
+ lines.push(` scrolls: ${scrolled(part.scrollable)}`);
1213
+ }
840
1214
  part.items.forEach((item, itemIndex) => {
841
- const itemId = `${id}.${itemIndex + 1}`;
1215
+ const itemId = rowId(index, part, itemIndex);
842
1216
  targets.set(itemId, { part: part.id, item: item.id });
843
1217
  lines.push(` [${itemId}] ${cut(item.text, 200)}${item.marks?.length ? ` · ${item.marks.map((mark) => cut(mark, 80)).join(" · ")}` : ""}`);
844
1218
  });
@@ -878,17 +1252,21 @@ var ScreenText;
878
1252
 
879
1253
  // web-src/screen-driver.ts
880
1254
  var wait2 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1255
+ var ARROWS = { down: "↓", up: "↑", top: "⤒", bottom: "⤓" };
1256
+ var score0 = (id) => id.startsWith("e") || id.includes(".");
881
1257
 
882
1258
  class ScreenDriver {
883
1259
  window;
884
1260
  host;
885
1261
  targets = new Map;
1262
+ names = new Map;
886
1263
  elements = [];
1264
+ shown = new Map;
887
1265
  cursor;
888
1266
  constructor(window2, host) {
889
1267
  this.window = window2;
890
1268
  this.host = host;
891
- this.cursor = new ScreenCursor(window2.document);
1269
+ this.cursor = new ScreenCursor(window2.document, () => this.host.touch?.() ?? false, host.linger);
892
1270
  }
893
1271
  async ready(timeoutMs = 20000) {
894
1272
  const until = Date.now() + timeoutMs;
@@ -897,10 +1275,20 @@ class ScreenDriver {
897
1275
  }
898
1276
  await wait2(200);
899
1277
  }
900
- look(part) {
1278
+ look(part, who) {
1279
+ const text = this.read(part);
1280
+ if (who !== undefined && !part) {
1281
+ this.shown.set(who, text);
1282
+ }
1283
+ return text;
1284
+ }
1285
+ hideCursor() {
1286
+ this.cursor.hide();
1287
+ }
1288
+ read(part) {
901
1289
  const { controls, elements } = ScreenDom.controls(this.window.document);
902
1290
  const sight = {
903
- whose: this.host.user ? "user" : "ta",
1291
+ whose: this.host.apart ? "ta" : "user",
904
1292
  device: this.host.device(),
905
1293
  where: this.host.screen.where(),
906
1294
  places: this.host.screen.places(),
@@ -908,12 +1296,29 @@ class ScreenDriver {
908
1296
  controls,
909
1297
  headings: ScreenDom.headings(this.window.document)
910
1298
  };
1299
+ const scroll = this.host.screen.pageScroll?.();
1300
+ if (scroll) {
1301
+ sight.scroll = scroll;
1302
+ }
911
1303
  const { text, targets } = ScreenText.look(sight, part);
912
1304
  this.targets = targets;
913
1305
  this.elements = elements;
1306
+ this.names = new Map;
1307
+ for (const [index, one] of sight.parts.entries()) {
1308
+ this.names.set(`p${index + 1}`, one.title);
1309
+ for (const [itemIndex, item] of one.items.entries()) {
1310
+ this.names.set(ScreenText.rowId(index, one, itemIndex), item.text);
1311
+ }
1312
+ }
1313
+ for (const [index, control] of controls.entries()) {
1314
+ if (targets.has(`e${index + 1}`)) {
1315
+ this.names.set(`e${index + 1}`, control.label);
1316
+ }
1317
+ }
914
1318
  return text;
915
1319
  }
916
- async go(to) {
1320
+ async go(to, who) {
1321
+ await this.host.quiet?.();
917
1322
  const word = to.trim().toLowerCase();
918
1323
  const place = this.host.screen.places().find((one) => one.id.toLowerCase() === word || one.title.toLowerCase() === word);
919
1324
  const button = place && this.window.document.querySelector(`[data-ta-place="${CSS.escape(place.id)}"]`);
@@ -921,25 +1326,91 @@ class ScreenDriver {
921
1326
  await this.cursor.point(button, place.title);
922
1327
  await this.cursor.press();
923
1328
  }
924
- if (!await this.host.screen.go(to)) {
1329
+ const went = await this.host.screen.go(to);
1330
+ if (!went) {
925
1331
  throw new Error(`There is no place ${JSON.stringify(to)}: see the places of app_look.`);
926
1332
  }
927
- await wait2(450);
1333
+ await this.settle(150, 800);
928
1334
  this.cursor.rest();
929
- return this.look();
1335
+ return this.look(undefined, who);
930
1336
  }
931
1337
  async do(id, action, input = {}, options = {}) {
932
- if (!this.targets.size) {
933
- this.look();
1338
+ const words2 = options.find?.trim();
1339
+ if (!this.targets.size || words2 && !id) {
1340
+ this.read();
1341
+ }
1342
+ let key = id.replace(/^\[|\]$/g, "");
1343
+ if (!key && words2) {
1344
+ const found = this.find(words2);
1345
+ if (typeof found !== "string") {
1346
+ return { text: `${found.why}
1347
+
1348
+ ${this.look(undefined, options.who)}` };
1349
+ }
1350
+ key = found;
934
1351
  }
935
- const target = this.targets.get(id.replace(/^\[|\]$/g, ""));
1352
+ const target = this.targets.get(key);
936
1353
  if (!target) {
937
- throw new Error(`Nothing on the screen is [${id}] now: app_look again.`);
1354
+ throw new Error(key ? `Nothing on the screen is [${key}] now: app_look again.` : "Say what to do it to: an id of app_look, or find.");
938
1355
  }
1356
+ await this.host.quiet?.();
939
1357
  const answer = "part" in target ? await this.part(target, action, input, options) : await this.control(target.control, action, input, options);
940
1358
  this.cursor.rest();
941
1359
  return answer;
942
1360
  }
1361
+ async scroll(id, to, options = {}) {
1362
+ const screen = this.host.screen;
1363
+ if (typeof screen.scroll !== "function") {
1364
+ throw new Error("This Ta cannot scroll its screen yet: it needs an update.");
1365
+ }
1366
+ if (!this.targets.size) {
1367
+ this.read();
1368
+ }
1369
+ let part;
1370
+ let goal = to;
1371
+ const key = id.replace(/^\[|\]$/g, "");
1372
+ if (key) {
1373
+ const target = this.targets.get(key);
1374
+ if (!target || !("part" in target)) {
1375
+ throw new Error(target ? "A control does not scroll: name a part ([p1]), or none for the page." : `Nothing on the screen is [${key}] now: app_look again.`);
1376
+ }
1377
+ part = target.part;
1378
+ }
1379
+ if (typeof to === "object" && "item" in to) {
1380
+ const name = to.item.replace(/^\[|\]$/g, "");
1381
+ const row = this.targets.get(name);
1382
+ if (row && "part" in row && row.item) {
1383
+ part = row.part;
1384
+ goal = { item: row.item };
1385
+ } else if (/^p\d+\.\d+$/.test(name)) {
1386
+ const of = this.targets.get(name.split(".")[0]);
1387
+ const scrollable = of && "part" in of ? screen.parts().find((one) => one.id === of.part)?.scrollable : undefined;
1388
+ throw new Error(`[${name}] is not in sight${scrollable ? ` (${ScreenText.scrolled(scrollable)})` : ""}: scroll toward it a screen at a time, or to it by its words ({ "find": … }), then look again.`);
1389
+ }
1390
+ }
1391
+ const before = options.who !== undefined ? this.shown.get(options.who) : undefined;
1392
+ await this.host.quiet?.();
1393
+ const shown = part ?? screen.pageScroll?.()?.part;
1394
+ const element = shown ? screen.element(shown) : undefined;
1395
+ if (element && this.host.watched()) {
1396
+ await this.cursor.point(element, typeof to === "string" ? ARROWS[to] : "↕");
1397
+ }
1398
+ const done = await screen.scroll(part, goal);
1399
+ await this.settle(60, 450);
1400
+ const now = (done.part ? screen.parts().find((one) => one.id === done.part)?.scrollable : screen.pageScroll?.()?.scrollable) ?? done.scrollable;
1401
+ const after = this.look(undefined, options.who);
1402
+ const head = done.moved ? `Scrolled ${typeof to === "string" ? to : "to the row"}: ${ScreenText.scrolled(now)}.` : `Nothing moved, ${typeof to === "string" ? "it goes no further" : "the row is in sight"}: ${ScreenText.scrolled(now)}.`;
1403
+ return { text: `${head}
1404
+
1405
+ ${ScreenText.changes(before, after) ?? after}` };
1406
+ }
1407
+ static spot(input) {
1408
+ const unit = (value) => typeof value === "number" && value >= 0 && value <= 1;
1409
+ if (!unit(input.x)) {
1410
+ return;
1411
+ }
1412
+ return { x: input.x, y: unit(input.y) ? input.y : 0.5 };
1413
+ }
943
1414
  dispose() {
944
1415
  this.cursor.dispose();
945
1416
  }
@@ -957,8 +1428,9 @@ class ScreenDriver {
957
1428
  const show = options.show === true || !action.command;
958
1429
  if (show && this.host.watched()) {
959
1430
  const element = this.host.screen.element(target.part, target.item, action.name);
1431
+ const spot = ScreenDriver.spot(input);
960
1432
  if (element) {
961
- await this.cursor.point(element, says);
1433
+ await this.cursor.point(spot ? element.querySelector("canvas") ?? element : element, says, spot);
962
1434
  await this.cursor.press();
963
1435
  }
964
1436
  }
@@ -966,13 +1438,14 @@ class ScreenDriver {
966
1438
  if ("command" in done) {
967
1439
  return { text: says, command: done.command, input: done.input };
968
1440
  }
969
- await wait2(350);
1441
+ await this.settle(60, 450);
970
1442
  return { text: `${done.text ?? "Done."}
971
1443
 
972
1444
  ${this.look(target.part)}` };
973
1445
  }
974
1446
  async control(index, action, input, options) {
975
1447
  const element = this.elements[index];
1448
+ const before = options.who !== undefined ? this.shown.get(options.who) : undefined;
976
1449
  if (!element?.isConnected || !ScreenDom.visible(element)) {
977
1450
  throw new Error("It is not on the screen any more: app_look again.");
978
1451
  }
@@ -1014,10 +1487,66 @@ ${this.look(target.part)}` };
1014
1487
  } else {
1015
1488
  element.click();
1016
1489
  }
1017
- await wait2(380);
1018
- return { text: `Done.
1490
+ await this.settle(60, 500);
1491
+ const after = this.look(undefined, options.who);
1492
+ return { text: `Done: ${label}.
1019
1493
 
1020
- ${this.look()}` };
1494
+ ${ScreenText.changes(before, after) ?? after}` };
1495
+ }
1496
+ find(words2) {
1497
+ const norm = (text) => text.replace(/\s+/g, " ").trim().toLowerCase();
1498
+ const want = norm(words2);
1499
+ let best = 0;
1500
+ let found = [];
1501
+ for (const [id, name] of this.names) {
1502
+ const have = norm(name);
1503
+ const score = (have === want ? 6 : have.startsWith(want) ? 4 : have.includes(want) ? 2 : 0) + (score0(id) && have.includes(want) ? 1 : 0);
1504
+ if (score > best) {
1505
+ best = score;
1506
+ found = [id];
1507
+ } else if (score === best && score > 0) {
1508
+ found.push(id);
1509
+ }
1510
+ }
1511
+ if (found.length === 1) {
1512
+ return found[0];
1513
+ }
1514
+ const quoted = JSON.stringify(words2);
1515
+ return {
1516
+ why: found.length ? `More than one is ${quoted}: ${found.slice(0, 6).map((one) => `[${one}] “${this.names.get(one)}”`).join(" · ")}; app_do one by its id.` : `Nothing on the screen is ${quoted}: app_do by an id of what it shows.`
1517
+ };
1518
+ }
1519
+ async settle(min, max) {
1520
+ const document2 = this.window.document;
1521
+ const Observer = this.window.MutationObserver;
1522
+ if (!Observer || !document2.body) {
1523
+ await wait2(max);
1524
+ return;
1525
+ }
1526
+ const start = Date.now();
1527
+ let last = start;
1528
+ const observer = new Observer((records) => {
1529
+ for (const record of records) {
1530
+ const node = record.target;
1531
+ const element = node.nodeType === 1 ? node : node.parentElement;
1532
+ if (record.attributeName !== "style" && !element?.closest("svg, .ta-agent-layer, .ta-screen-pill, .ta-screen-box")) {
1533
+ last = Date.now();
1534
+ return;
1535
+ }
1536
+ }
1537
+ });
1538
+ observer.observe(document2.body, { subtree: true, childList: true, attributes: true, characterData: true });
1539
+ try {
1540
+ for (;; ) {
1541
+ await wait2(40);
1542
+ const now = Date.now();
1543
+ if (now - start >= max || now - start >= min && now - last >= 120) {
1544
+ return;
1545
+ }
1546
+ }
1547
+ } finally {
1548
+ observer.disconnect();
1549
+ }
1021
1550
  }
1022
1551
  async agree(what, options) {
1023
1552
  return options.confirm ? options.confirm(what) : false;
@@ -1037,6 +1566,7 @@ ${this.look()}` };
1037
1566
 
1038
1567
  // web-src/screen-window.ts
1039
1568
  var SCREEN_IDLE_MS = 2 * 60000;
1569
+ var INPUTS = ["pointerdown", "keydown", "wheel", "touchmove"];
1040
1570
  var STYLE2 = `
1041
1571
  .ta-screen-box { position: fixed; z-index: 2147483000; display: flex; flex-direction: column; overflow: hidden; border-radius: 14px; border: 1px solid var(--ta-border, rgba(0,0,0,.12)); background: var(--ta-side, #fff); color: var(--ta-text, #111); box-shadow: var(--ta-shadow-pop, 0 12px 32px rgba(0,0,0,.18)); font: 13px/1.4 var(--ta-sans, system-ui, sans-serif); transition: transform 260ms cubic-bezier(.2,.8,.2,1), opacity 200ms; }
1042
1572
  .ta-screen-box[data-hidden] { transform: translate3d(0, 24px, 0); opacity: 0; pointer-events: none; }
@@ -1052,9 +1582,10 @@ var STYLE2 = `
1052
1582
  .ta-screen-button.ta-screen-stop { color: var(--ta-red, #e5484d); }
1053
1583
  .ta-screen-view { position: relative; flex: 1; overflow: hidden; background: var(--ta-body, #f2f2f2); }
1054
1584
  .ta-screen-view iframe { position: absolute; left: 0; top: 0; border: 0; transform-origin: 0 0; pointer-events: none; background: var(--ta-main, #fff); }
1055
- .ta-screen-pill { position: fixed; z-index: 2147483001; left: 50%; top: calc(env(safe-area-inset-top, 0px) + 60px); transform: translate3d(-50%, 0, 0); display: flex; align-items: center; gap: 4px; height: 34px; padding: 0 4px 0 12px; border-radius: 17px; background: var(--ta-side, #fff); color: var(--ta-text, #111); border: 1px solid var(--ta-border, rgba(0,0,0,.12)); box-shadow: var(--ta-shadow-pop, 0 8px 24px rgba(0,0,0,.16)); font: 13px/1 var(--ta-sans, system-ui, sans-serif); transition: opacity 200ms, transform 260ms cubic-bezier(.2,.8,.2,1); }
1585
+ .ta-screen-pill { position: fixed; z-index: 2147483001; left: 50%; top: calc(env(safe-area-inset-top, 0px) + 52px); transform: translate3d(-50%, 0, 0); display: flex; align-items: center; gap: 4px; height: 34px; padding: 0 4px 0 12px; border-radius: 17px; background: var(--ta-side, #fff); color: var(--ta-text, #111); border: 1px solid var(--ta-border, rgba(0,0,0,.12)); box-shadow: var(--ta-shadow-pop, 0 8px 24px rgba(0,0,0,.16)); font: 13px/1 var(--ta-sans, system-ui, sans-serif); transition: opacity 200ms, transform 260ms cubic-bezier(.2,.8,.2,1); }
1056
1586
  .ta-screen-pill[data-hidden] { opacity: 0; transform: translate3d(-50%, -12px, 0); pointer-events: none; }
1057
1587
  .ta-screen-pill span { margin: 0 4px 0 2px; font-weight: 600; white-space: nowrap; }
1588
+ .ta-screen-pill .ta-screen-button[hidden] { display: none; }
1058
1589
  @media (prefers-reduced-motion: reduce) { .ta-screen-box, .ta-screen-pill { transition: none; } .ta-screen-dot { animation: none !important; } }
1059
1590
  `;
1060
1591
  var wait3 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
@@ -1077,26 +1608,30 @@ class ScreenWindow {
1077
1608
  restTimer;
1078
1609
  own;
1079
1610
  position;
1611
+ hereBusy = 0;
1612
+ hereTimer;
1613
+ lastInput = 0;
1614
+ input = (event) => {
1615
+ if (!event.target?.closest?.(".ta-screen-pill, .ta-screen-box, .ta-agent-layer")) {
1616
+ this.lastInput = Date.now();
1617
+ }
1618
+ };
1080
1619
  constructor(pi, words2) {
1081
1620
  this.pi = pi;
1082
1621
  this.words = words2;
1622
+ for (const type of INPUTS) {
1623
+ window.addEventListener(type, this.input, { capture: true, passive: true });
1624
+ }
1083
1625
  }
1084
1626
  async run(ask) {
1085
- if (ask.here) {
1627
+ if (!ask.apart) {
1086
1628
  return this.runHere(ask);
1087
1629
  }
1088
1630
  clearTimeout(this.idleTimer);
1089
1631
  this.begin(ask.from);
1090
1632
  const outer = document.activeElement;
1091
1633
  try {
1092
- const driver = await this.driver();
1093
- if (ask.kind === "look") {
1094
- return { text: driver.look(ask.part) };
1095
- }
1096
- if (ask.kind === "go") {
1097
- return { text: await driver.go(String(ask.to ?? "")) };
1098
- }
1099
- return await driver.do(String(ask.id ?? ""), ask.action, ask.input, { show: ask.show === true, confirm: (what) => this.confirm(what, ask) });
1634
+ return await this.step(await this.driver(), ask, ask.show === true);
1100
1635
  } finally {
1101
1636
  if (document.activeElement === this.frame && outer instanceof HTMLElement) {
1102
1637
  outer.focus({ preventScroll: true });
@@ -1105,19 +1640,91 @@ class ScreenWindow {
1105
1640
  }
1106
1641
  }
1107
1642
  dispose() {
1643
+ for (const type of INPUTS) {
1644
+ window.removeEventListener(type, this.input, { capture: true });
1645
+ }
1646
+ clearTimeout(this.hereTimer);
1108
1647
  this.close();
1648
+ this.pill?.remove();
1649
+ this.pill = undefined;
1109
1650
  this.own?.dispose();
1110
1651
  }
1111
1652
  async runHere(ask) {
1112
- const screen = this.pi.ui.screen;
1113
- this.own ??= new ScreenDriver(window, { screen, watched: () => document.visibilityState === "visible", device: () => this.device(), user: true });
1653
+ this.own ??= new ScreenDriver(window, {
1654
+ screen: this.pi.ui.screen,
1655
+ watched: () => document.visibilityState === "visible",
1656
+ device: () => this.device(),
1657
+ touch: () => typeof matchMedia === "function" && matchMedia("(pointer: coarse)").matches,
1658
+ quiet: () => this.quiet(),
1659
+ linger: 60000
1660
+ });
1661
+ const own = this.own;
1662
+ this.from = ask.from;
1663
+ const looking = ask.kind === "look";
1664
+ if (!looking) {
1665
+ this.hereBusy++;
1666
+ clearTimeout(this.hereTimer);
1667
+ this.showPill(true, false);
1668
+ }
1669
+ try {
1670
+ return await this.step(own, ask, true);
1671
+ } finally {
1672
+ if (!looking) {
1673
+ this.hereBusy = Math.max(0, this.hereBusy - 1);
1674
+ this.lingerHere();
1675
+ }
1676
+ }
1677
+ }
1678
+ async step(driver, ask, show) {
1679
+ const who = ask.from?.sessionId ?? "";
1114
1680
  if (ask.kind === "look") {
1115
- return { text: this.own.look(ask.part) };
1681
+ return { text: driver.look(ask.part, who) };
1116
1682
  }
1117
1683
  if (ask.kind === "go") {
1118
- return { text: await this.own.go(String(ask.to ?? "")) };
1684
+ return { text: await driver.go(String(ask.to ?? ""), who) };
1685
+ }
1686
+ if (ask.to) {
1687
+ await driver.go(ask.to);
1688
+ }
1689
+ if (ask.scroll) {
1690
+ return driver.scroll(String(ask.id ?? ""), ask.scroll, { who });
1691
+ }
1692
+ return driver.do(String(ask.id ?? ""), ask.action, ask.input, {
1693
+ show,
1694
+ who,
1695
+ ...ask.find ? { find: ask.find } : {},
1696
+ confirm: (what) => this.confirm(what, ask)
1697
+ });
1698
+ }
1699
+ lingerHere() {
1700
+ clearTimeout(this.hereTimer);
1701
+ const since = Date.now();
1702
+ const check = async () => {
1703
+ if (this.hereBusy > 0) {
1704
+ return;
1705
+ }
1706
+ const sessionId = this.from?.sessionId;
1707
+ const idle = Date.now() - since;
1708
+ const going = !!sessionId && idle < 30000 && await this.pi.server.listSessions().then((list) => list.some((one) => one.sessionId === sessionId && one.isStreaming), () => false);
1709
+ if (this.hereBusy > 0) {
1710
+ return;
1711
+ }
1712
+ if (going || idle < (sessionId ? 1200 : 2500)) {
1713
+ this.hereTimer = setTimeout(() => void check(), 1000);
1714
+ return;
1715
+ }
1716
+ if (this.busy === 0) {
1717
+ this.showPill(false);
1718
+ }
1719
+ this.own?.hideCursor();
1720
+ };
1721
+ this.hereTimer = setTimeout(() => void check(), 1200);
1722
+ }
1723
+ async quiet() {
1724
+ const until = Date.now() + 8000;
1725
+ while (Date.now() - this.lastInput < 1200 && Date.now() < until) {
1726
+ await wait3(150);
1119
1727
  }
1120
- return this.own.do(String(ask.id ?? ""), ask.action, ask.input, { show: true, confirm: (what) => this.confirm(what, ask) });
1121
1728
  }
1122
1729
  confirm(what, ask) {
1123
1730
  return this.pi.ui.confirm(`${ask.from?.name ?? "Ta"}: ${what}`, undefined, { danger: true });
@@ -1284,7 +1891,7 @@ class ScreenWindow {
1284
1891
  this.box?.setAttribute("data-busy", "");
1285
1892
  this.heading();
1286
1893
  if (this.device() === "phone") {
1287
- this.showPill(true);
1894
+ this.showPill(true, true);
1288
1895
  } else if (this.awayFor !== (from?.sessionId ?? "")) {
1289
1896
  this.away(false);
1290
1897
  }
@@ -1296,7 +1903,9 @@ class ScreenWindow {
1296
1903
  if (this.busy === 0) {
1297
1904
  this.box?.removeAttribute("data-busy");
1298
1905
  this.heading();
1299
- this.showPill(false);
1906
+ if (this.hereBusy === 0) {
1907
+ this.showPill(false);
1908
+ }
1300
1909
  }
1301
1910
  }, 4000);
1302
1911
  clearTimeout(this.idleTimer);
@@ -1319,11 +1928,12 @@ class ScreenWindow {
1319
1928
  this.shownOnPhone = false;
1320
1929
  this.away(true);
1321
1930
  if (this.device() === "phone" && this.busy > 0) {
1322
- this.showPill(true);
1931
+ this.showPill(true, true);
1323
1932
  }
1324
1933
  }
1325
- showPill(show) {
1934
+ showPill(show, watchable = false) {
1326
1935
  if (show && !this.pill) {
1936
+ this.style();
1327
1937
  const pill = document.createElement("div");
1328
1938
  pill.className = "ta-screen-pill";
1329
1939
  pill.dataset.hidden = "";
@@ -1331,20 +1941,25 @@ class ScreenWindow {
1331
1941
  dot.className = "ta-screen-dot";
1332
1942
  const words2 = document.createElement("span");
1333
1943
  words2.textContent = this.words().operating;
1334
- const watch = this.button(this.words().watch, () => {
1944
+ const watch2 = this.button(this.words().watch, () => {
1335
1945
  this.shownOnPhone = true;
1336
1946
  this.showPill(false);
1337
1947
  this.away(false);
1338
1948
  });
1339
1949
  const stop = this.button(this.words().stop, () => this.stop());
1340
1950
  stop.classList.add("ta-screen-stop");
1341
- pill.append(dot, words2, watch, stop);
1951
+ watch2.classList.add("ta-screen-watch");
1952
+ pill.append(dot, words2, watch2, stop);
1342
1953
  document.body.appendChild(pill);
1343
1954
  this.pill = pill;
1344
1955
  }
1345
1956
  if (!this.pill) {
1346
1957
  return;
1347
1958
  }
1959
+ const watch = this.pill.querySelector(".ta-screen-watch");
1960
+ if (watch && show) {
1961
+ watch.hidden = !watchable;
1962
+ }
1348
1963
  if (show && !this.shownOnPhone) {
1349
1964
  this.pill.getBoundingClientRect();
1350
1965
  this.pill.removeAttribute("data-hidden");
@@ -1360,20 +1975,26 @@ class ScreenWindow {
1360
1975
  });
1361
1976
  }
1362
1977
  this.busy = 0;
1978
+ this.hereBusy = 0;
1979
+ clearTimeout(this.hereTimer);
1980
+ this.own?.hideCursor();
1363
1981
  this.box?.removeAttribute("data-busy");
1364
1982
  this.heading();
1365
- this.putAway();
1983
+ if (this.box) {
1984
+ this.putAway();
1985
+ }
1366
1986
  this.showPill(false);
1367
1987
  }
1368
1988
  close() {
1369
1989
  clearTimeout(this.idleTimer);
1370
1990
  clearTimeout(this.restTimer);
1371
1991
  this.box?.remove();
1372
- this.pill?.remove();
1373
1992
  this.box = undefined;
1374
1993
  this.frame = undefined;
1375
- this.pill = undefined;
1376
1994
  this.busy = 0;
1995
+ if (this.hereBusy === 0) {
1996
+ this.showPill(false);
1997
+ }
1377
1998
  this.shownOnPhone = false;
1378
1999
  }
1379
2000
  style() {
@@ -1405,9 +2026,19 @@ var AppToolLine;
1405
2026
  const target = kind === "settings" ? rest ? `${words2.settings} · ${rest}` : words2.settings : kind === "discover" ? words2.discover : /^(chat|page|list)$/.test(kind) ? rest.replace(/^.*\//, "") : to;
1406
2027
  return { verb: words2.go, target };
1407
2028
  }
2029
+ if (tool === "app_do" && args.scroll !== undefined) {
2030
+ const scroll = args.scroll;
2031
+ const step = typeof scroll === "string" ? words2[`scroll.${scroll}`] : undefined;
2032
+ const find = scroll && typeof scroll === "object" ? text(scroll.find) : "";
2033
+ return { verb: words2.scroll, target: (step ?? find).slice(0, 40) };
2034
+ }
1408
2035
  if (tool === "app_do") {
1409
2036
  const input2 = args.input && typeof args.input === "object" ? args.input : {};
1410
- return { verb: words2.do, target: text(input2.text).slice(0, 40) };
2037
+ const game = words2[`game.${text(args.action)}`];
2038
+ if (game) {
2039
+ return { verb: game, target: text(input2.key) };
2040
+ }
2041
+ return { verb: words2.do, target: (text(args.find) || text(input2.text)).slice(0, 40) };
1411
2042
  }
1412
2043
  if (tool !== "app_run") {
1413
2044
  return;
@@ -1458,7 +2089,9 @@ function activate(pi) {
1458
2089
  const driver = new ScreenDriver(window, {
1459
2090
  screen: pi.ui.screen,
1460
2091
  watched: () => frame.taScreenWatched?.() ?? false,
1461
- device: () => window.innerWidth < 640 ? "phone" : "computer"
2092
+ device: () => window.innerWidth < 640 ? "phone" : "computer",
2093
+ apart: true,
2094
+ linger: 6000
1462
2095
  });
1463
2096
  frame.taScreen = driver;
1464
2097
  return () => {