cortico-world-hands 0.1.4 → 0.1.6

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 (2) hide show
  1. package/dist/index.js +36 -6
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -673,7 +673,8 @@ var Hands = class {
673
673
  /** 留言板:写一条。 */
674
674
  async noteAdd(text, kind) {
675
675
  const cfg = this.cfg();
676
- const token = cfg.handToken ? `&t=${encodeURIComponent(cfg.handToken)}` : "";
676
+ const credential = this.readToken();
677
+ const token = credential ? `&t=${encodeURIComponent(credential)}` : "";
677
678
  const k = kind ? `&kind=${encodeURIComponent(kind)}` : "";
678
679
  const path = `/hand/note?text=${encodeURIComponent(text)}${k}${token}`;
679
680
  const res = await get(this.port(cfg.handPort), path, ACTION_TIMEOUT_MS);
@@ -687,7 +688,8 @@ var Hands = class {
687
688
  /** 留言板:全部留言。 */
688
689
  async noteList() {
689
690
  const cfg = this.cfg();
690
- const token = cfg.handToken ? `?t=${encodeURIComponent(cfg.handToken)}` : "";
691
+ const credential = this.readToken();
692
+ const token = credential ? `?t=${encodeURIComponent(credential)}` : "";
691
693
  const res = await get(this.port(cfg.handPort), `/hand/notes${token}`, ACTION_TIMEOUT_MS);
692
694
  try {
693
695
  const body = JSON.parse(res.body.toString("utf8"));
@@ -696,10 +698,20 @@ var Hands = class {
696
698
  return [];
697
699
  }
698
700
  }
701
+ /** 留言板:未完成的待办条数(状态灯里显示)。 */
702
+ async notePending() {
703
+ try {
704
+ const notes = await this.noteList();
705
+ return notes.filter((n) => !n.done).length;
706
+ } catch {
707
+ return 0;
708
+ }
709
+ }
699
710
  /** 留言板:划掉一条。 */
700
711
  async noteDone(id) {
701
712
  const cfg = this.cfg();
702
- const token = cfg.handToken ? `&t=${encodeURIComponent(cfg.handToken)}` : "";
713
+ const credential = this.readToken();
714
+ const token = credential ? `&t=${encodeURIComponent(credential)}` : "";
703
715
  const res = await get(this.port(cfg.handPort), `/hand/note/done?id=${encodeURIComponent(id)}${token}`, ACTION_TIMEOUT_MS);
704
716
  return res.status === 200;
705
717
  }
@@ -708,7 +720,8 @@ var Hands = class {
708
720
  const cfg = this.cfg();
709
721
  const tries = Math.max(0, Math.min(12, Math.round(Number(opts.tries ?? 5))));
710
722
  const scroll = opts.scroll === false ? "0" : "1";
711
- const token = cfg.handToken ? `&t=${encodeURIComponent(cfg.handToken)}` : "";
723
+ const credential = this.readToken();
724
+ const token = credential ? `&t=${encodeURIComponent(credential)}` : "";
712
725
  const path = `/hand/find?text=${encodeURIComponent(text)}&tries=${tries}&scroll=${scroll}${token}`;
713
726
  const res = await get(this.port(cfg.handPort), path, ACTION_TIMEOUT_MS);
714
727
  let body = {};
@@ -737,6 +750,7 @@ var Hands = class {
737
750
  if (!state.ok) throw new HandError(`\u624B\u73B0\u5728\u52A8\u4E0D\u4E86\uFF1A${state.detail}`);
738
751
  const where = await this.screenGuard();
739
752
  await this.throttle();
753
+ this.lookCache = null;
740
754
  const described = describeCommand(command);
741
755
  const cfg = this.cfg();
742
756
  if (cfg.dryRun) {
@@ -760,7 +774,18 @@ var Hands = class {
760
774
  }
761
775
  }
762
776
  /** 看一眼屏幕。detail: both(画面加文字,默认)、screen(只要画面)、text(只要文字)。 */
777
+ lookCache = null;
763
778
  async look(detail = "both") {
779
+ const now = Date.now();
780
+ if (this.lookCache && this.lookCache.detail === detail && now - this.lookCache.at < 1200) {
781
+ this.looks += 1;
782
+ return this.lookCache.report;
783
+ }
784
+ const report = await this.lookFresh(detail);
785
+ this.lookCache = { at: Date.now(), detail, report };
786
+ return report;
787
+ }
788
+ async lookFresh(detail = "both") {
764
789
  const cfg = this.cfg();
765
790
  const health = await this.eyes.health();
766
791
  if (!health?.ok) {
@@ -1074,7 +1099,7 @@ var HandsWorld = class {
1074
1099
  case "hand_key":
1075
1100
  return await this.key(args);
1076
1101
  case "hand_status":
1077
- return this.statusText();
1102
+ return await this.statusText();
1078
1103
  case "note_add":
1079
1104
  return await this.noteAdd(args);
1080
1105
  case "note_list":
@@ -1226,7 +1251,7 @@ ${list}`);
1226
1251
  this.note(receipt);
1227
1252
  return receipt.text;
1228
1253
  }
1229
- statusText() {
1254
+ async statusText() {
1230
1255
  const cfg = this.opts.cfg;
1231
1256
  const state = this.hands.state();
1232
1257
  const stats = this.hands.stats();
@@ -1237,6 +1262,11 @@ ${list}`);
1237
1262
  `\u8FD9\u4E00\u5206\u949F\uFF1A${stats.inWindow}\uFF0F${Math.max(1, Math.round(cfg.maxActionsPerMinute) || 30)} \u4E0B\uFF1B\u7D2F\u8BA1\u52A8\u8FC7 ${stats.acts} \u6B21\u3001\u770B\u8FC7 ${stats.looks} \u6B21`
1238
1263
  ];
1239
1264
  if (this.pausedUntil > Date.now()) lines.push(`\u53EB\u505C\u4E2D\uFF1A\u8FD8\u6709 ${Math.ceil((this.pausedUntil - Date.now()) / 1e3)} \u79D2`);
1265
+ try {
1266
+ const pending = await this.hands.notePending();
1267
+ if (pending > 0) lines.push(`\u7559\u8A00\u677F\uFF1A\u8FD8\u6709 ${pending} \u6761\u6CA1\u5212\u6389`);
1268
+ } catch {
1269
+ }
1240
1270
  if (this.recent.length) lines.push(`\u6700\u8FD1\uFF1A
1241
1271
  ${this.recent.map((r) => `- ${r}`).join("\n")}`);
1242
1272
  return lines.join("\n");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-hands",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "description": "Cortico World: hands for the desktop pet — looks at the phone screen, then taps, swipes, pinches, drags, types and presses keys through whatever actuator the device actually has",
6
6
  "license": "MIT",