cortico-world-hands 0.20.68 → 0.20.70

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 +90 -2
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -54,6 +54,12 @@ var HANDS_DEFAULTS = {
54
54
  homeReturnMinutes: 10,
55
55
  /** 每隔几分钟查一次异常(手掉线、磁盘满等),有问题主动报告。0 关闭。 */
56
56
  watchMinutes: 3,
57
+ /** 控制台密码(只在本机用来读 /api/usage,不会外发)。 */
58
+ consolePassword: "",
59
+ /** 这个月的预算(美元),用来算“还剩多少”。 */
60
+ budgetUsd: 10,
61
+ /** 剩多少美元以下开始提醒。 */
62
+ warnBelowUsd: 1.5,
57
63
  /** 同一个问题最多隔多久再提醒一次(分钟)。 */
58
64
  alertRepeatMinutes: 30,
59
65
  /** 这些应用在前台时她闭嘴(只推事件不说话),逗号分隔的包名。 */
@@ -221,6 +227,24 @@ var HANDS_CONFIG_GROUP = {
221
227
  description: "\u624B\u6389\u7EBF\u3001\u78C1\u76D8\u5FEB\u6EE1\u3001\u63A7\u5236\u53F0\u5F02\u5E38\u65F6\uFF0C\u5979\u4F1A\u4E3B\u52A8\u62A5\u544A\u30020 \u5173\u95ED\u3002\u9ED8\u8BA4 3\u3002",
222
228
  "x-hot": true
223
229
  },
230
+ [`${K}.consolePassword`]: {
231
+ type: "string",
232
+ title: "\u63A7\u5236\u53F0\u5BC6\u7801\uFF08\u67E5\u7528\u91CF\u7528\uFF09",
233
+ description: "\u53EA\u5728\u672C\u673A\u767B\u5F55 127.0.0.1:17788 \u8BFB /api/usage\uFF0C\u4E0D\u4F1A\u5916\u53D1\u3002",
234
+ "x-secret": true
235
+ },
236
+ [`${K}.budgetUsd`]: {
237
+ type: "number",
238
+ title: "\u672C\u6708\u9884\u7B97\uFF08\u7F8E\u5143\uFF09",
239
+ description: "\u7528\u6765\u4F30\u7B97\u201C\u8FD8\u5269\u591A\u5C11\u201D\u3002\u9ED8\u8BA4 10\u3002",
240
+ "x-hot": true
241
+ },
242
+ [`${K}.warnBelowUsd`]: {
243
+ type: "number",
244
+ title: "\u5269\u591A\u5C11\u7F8E\u5143\u5F00\u59CB\u63D0\u9192",
245
+ description: "\u9ED8\u8BA4 1.5\u3002",
246
+ "x-hot": true
247
+ },
224
248
  [`${K}.alertRepeatMinutes`]: {
225
249
  type: "number",
226
250
  title: "\u540C\u4E00\u4E2A\u95EE\u9898\u9694\u591A\u4E45\u518D\u63D0\u9192",
@@ -1193,9 +1217,8 @@ var EyesClient = class {
1193
1217
  }
1194
1218
  /** 只截一块并放大:x,y,w,h 是真实像素。 */
1195
1219
  async zoom(x, y, w, h, maxWidth = 1080, quality = 80) {
1196
- const cfg = this.cfg();
1197
1220
  const path = `/screen.jpg?x=${Math.max(0, Math.round(x))}&y=${Math.max(0, Math.round(y))}&w=${Math.max(8, Math.round(w))}&h=${Math.max(8, Math.round(h))}&max=${Math.round(maxWidth)}&q=${Math.round(quality)}`;
1198
- const res = await get(this.handPort(), path, 12e3);
1221
+ const res = await get(this.port(), path, 12e3);
1199
1222
  if (res.status !== 200 || res.body.length === 0) return null;
1200
1223
  return {
1201
1224
  bytes: new Uint8Array(res.body),
@@ -1260,6 +1283,14 @@ var describeCommand = (command) => {
1260
1283
  var Hands = class {
1261
1284
  /** 「我们的日子」:新增一条。 */
1262
1285
  /** 整屏切四块放大:交给眼睛那边做。 */
1286
+ /** 健康检查:交给眼睛那边。 */
1287
+ async health() {
1288
+ return this.eyes.health();
1289
+ }
1290
+ /** 局部放大:交给眼睛那边。 */
1291
+ async zoom(x, y, w, h, maxWidth = 1080, quality = 80) {
1292
+ return this.eyes.zoom(x, y, w, h, maxWidth, quality);
1293
+ }
1263
1294
  async quadFrame(maxWidth = 1440, quality = 76) {
1264
1295
  return this.eyes.quadFrame(maxWidth, quality);
1265
1296
  }
@@ -2654,6 +2685,12 @@ var HANDS_TOOL_DECLS = [
2654
2685
  type: "object",
2655
2686
  properties: { max: { type: "integer", description: "\u8F93\u51FA\u5BBD\u5EA6\uFF0C\u9ED8\u8BA4 1440" } }
2656
2687
  }
2688
+ },
2689
+ {
2690
+ name: "token_status",
2691
+ tags: ["read"],
2692
+ description: "\u67E5\u8FD9\u4E2A\u6708\u82B1\u4E86\u591A\u5C11\u3001\u8FD8\u5269\u591A\u5C11\u989D\u5EA6\uFF08\u6309\u914D\u7F6E\u7684\u6708\u9884\u7B97\u4F30\u7B97\uFF09\uFF0C\u4EE5\u53CA\u4ECA\u5929\u82B1\u4E86\u591A\u5C11\u3002\u5FEB\u7528\u5B8C\u8981\u4E3B\u52A8\u63D0\u9192\u4E3B\u4EBA\u3002",
2693
+ parameters: { type: "object", properties: {} }
2657
2694
  }
2658
2695
  ];
2659
2696
 
@@ -3148,6 +3185,47 @@ ${lines.join("\n")}
3148
3185
  } catch {
3149
3186
  }
3150
3187
  }
3188
+ /** 读本月用量:登录本机控制台(只存内存 cookie)。 */
3189
+ async usageNow() {
3190
+ const password = String(this.opts.cfg.consolePassword || "");
3191
+ if (!password) return null;
3192
+ try {
3193
+ const login = await fetch("http://127.0.0.1:17788/api/auth/login", {
3194
+ method: "POST",
3195
+ headers: { "content-type": "application/json" },
3196
+ body: JSON.stringify({ password })
3197
+ });
3198
+ if (!login.ok) return null;
3199
+ const cookie = (login.headers.get("set-cookie") || "").split(";")[0];
3200
+ const res = await fetch("http://127.0.0.1:17788/api/usage?period=month", { headers: { cookie } });
3201
+ if (!res.ok) return null;
3202
+ const body = await res.json();
3203
+ const spend = Number(body?.totals?.cost ?? 0);
3204
+ const today = Number(body?.today?.cost ?? 0);
3205
+ return { spend, today, calls: Number(body?.totals?.calls ?? 0) };
3206
+ } catch {
3207
+ return null;
3208
+ }
3209
+ }
3210
+ /** 钱包:剩多少、今天花了多少;快见底时主动提醒。 */
3211
+ async checkWallet() {
3212
+ const budget = Number(this.opts.cfg.budgetUsd ?? 10);
3213
+ const warn = Number(this.opts.cfg.warnBelowUsd ?? 1.5);
3214
+ if (!Number.isFinite(budget) || budget <= 0) return;
3215
+ const u = await this.usageNow();
3216
+ if (!u) return;
3217
+ const left = budget - u.spend;
3218
+ if (left > warn) return;
3219
+ const key = "wallet-" + (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
3220
+ const last = this.lastAlertAt.get(key) ?? 0;
3221
+ if (Date.now() - last < 12 * 36e5) return;
3222
+ this.lastAlertAt.set(key, Date.now());
3223
+ await this.pushEvent(
3224
+ `[\u4F59\u989D] \u8FD9\u4E2A\u6708\u9884\u7B97 ${budget.toFixed(2)} \u7F8E\u5143\uFF0C\u5DF2\u7ECF\u82B1\u4E86 ${u.spend.toFixed(2)}\uFF0C\u8FD8\u5269 ${left.toFixed(2)}\uFF1B\u4ECA\u5929\u82B1\u4E86 ${u.today.toFixed(2)}\u3002\u7528 pet_say \u63D0\u9192\u4E3B\u4EBA\u4E00\u53E5\uFF0C\u8BF4\u6E05\u695A\u8FD8\u5269\u591A\u5C11\u3001\u8981\u4E0D\u8981\u5145/\u6362\u4FBF\u5B9C\u6A21\u578B\u3002`,
3225
+ "hands.wallet",
3226
+ "hands:wallet"
3227
+ );
3228
+ }
3151
3229
  /** 异常看护:手掉线、磁盘快满、健康检查失败时主动报告,别自己扛。 */
3152
3230
  async checkHealth() {
3153
3231
  const mins = Number(this.opts.cfg.watchMinutes ?? 3);
@@ -3322,6 +3400,7 @@ ${lines.join("\n")}
3322
3400
  await this.maybeRequest();
3323
3401
  await this.maybeDiscovery();
3324
3402
  await this.maybeWalkHome();
3403
+ await this.checkWallet();
3325
3404
  }
3326
3405
  /** 她问过「你那个修好了吗」之后,要记得回头追一下。 */
3327
3406
  async checkFollowups() {
@@ -3608,6 +3687,8 @@ ${lines.join("\n")}
3608
3687
  return await this.runSync(true);
3609
3688
  case "sync_status":
3610
3689
  return this.lastSync ? `\u4E0A\u6B21\u540C\u6B65 ${this.lastSync.at.slice(0, 19)}\uFF0C\u8BB0\u5FC6 ${this.lastSync.lines} \u6761\uFF0C\u4EBA\u683C ${this.lastSync.persona} \u5B57` : "\u8FD8\u6CA1\u540C\u6B65\u8FC7";
3690
+ case "token_status":
3691
+ return await this.tokenStatus();
3611
3692
  case "hand_quad":
3612
3693
  return await this.handQuad(args);
3613
3694
  case "hand_read_page": {
@@ -4095,6 +4176,13 @@ ${text.slice(0, 2200)}`;
4095
4176
  if (!["system", "secure", "global"].includes(ns) || !key) return failed("ns \u53D6 system/secure/global\uFF0Ckey \u5FC5\u586B\u3002");
4096
4177
  return await this.hands.sysSettingsGet(ns, key);
4097
4178
  }
4179
+ async tokenStatus() {
4180
+ const budget = Number(this.opts.cfg.budgetUsd ?? 10);
4181
+ const u = await this.usageNow();
4182
+ if (!u) return failed("\u8BFB\u4E0D\u5230\u7528\u91CF\uFF1A\u63A7\u5236\u53F0\u5BC6\u7801\u6CA1\u914D\uFF0C\u6216\u8005\u63A7\u5236\u53F0\u6CA1\u5F00\u3002");
4183
+ const left = budget - u.spend;
4184
+ return `\u672C\u6708\u9884\u7B97 ${budget.toFixed(2)} \u7F8E\u5143\uFF1B\u5DF2\u82B1 ${u.spend.toFixed(2)}\uFF08${u.calls} \u6B21\uFF09\uFF1B\u4ECA\u5929 ${u.today.toFixed(2)}\uFF1B\u4F30\u7B97\u8FD8\u5269 ${left.toFixed(2)} \u7F8E\u5143\u3002`;
4185
+ }
4098
4186
  async handQuad(args) {
4099
4187
  const max = args.max === void 0 ? 1440 : Math.max(640, Math.min(2160, Math.round(Number(args.max))));
4100
4188
  const f = await this.hands.quadFrame(max, 76);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-hands",
3
- "version": "0.20.68",
3
+ "version": "0.20.70",
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",