dsh-workbuddy-xdpool 1.0.1 → 1.2.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/lib/bin.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createHash } from "node:crypto";
1
+ import { createHash, randomUUID } from "node:crypto";
2
2
  import { copyFile, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
3
3
  import { homedir, platform } from "node:os";
4
4
  import { basename, join, resolve } from "node:path";
@@ -50,6 +50,15 @@ const HARD_CREDIT_MARKERS = [
50
50
  /** Session-invalidation markers that mean "sign in again in the WorkBuddy app". */
51
51
  const SESSION_DEAD_MARKERS = ["Offline user session not found", "12153"];
52
52
  /**
53
+ * Markers for "already checked in today".
54
+ *
55
+ * The upstream answers a non-zero business code (and HTTP 400) when the daily
56
+ * check-in is repeated. That is an idempotent success, not a failure: the
57
+ * reward for today is already collected. Matched against the message, since
58
+ * the code varies by realm.
59
+ */
60
+ const ALREADY_CHECKIN_MARKERS = ["已签到", "already"];
61
+ /**
53
62
  * Hosts the international product answers on, once each has been stripped of a
54
63
  * leading label. The WorkBuddy AI desktop app signs in at `workbuddy.ai` (and
55
64
  * the desktop client itself lists `workbuddy.cc` alongside it); the CodeBuddy
@@ -123,6 +132,43 @@ function refreshHeaders(credential) {
123
132
  if (credential.enterpriseId !== void 0 && credential.enterpriseId !== "") headers["X-Enterprise-Id"] = credential.enterpriseId;
124
133
  return headers;
125
134
  }
135
+ /** Desktop-client report endpoint and the UA it is fingerprinted by. */
136
+ const DESKTOP_REPORT_PATH = "/v2/report";
137
+ /** Theme-selection endpoint (Hp_Appearance). */
138
+ const APPEARANCE_SET_PATH = "/v2/user-asset/appearance/set";
139
+ /** Expert marketplace listing, used to look up REAL expert ids. */
140
+ const MARKET_EXPERT_LIST_PATH = "/portal/operation-platform/market/expert/list";
141
+ /** Cap on how long a chain waits for a conversation answer. */
142
+ const CHAT_TIMEOUT_MS = 9e4;
143
+ /** How far into an SSE stream to look for the server's request id. */
144
+ const SSE_SCAN_LIMIT = 1 << 20;
145
+ /** Server request ids look like `cmb-<32 hex>` or a bare 32 hex string. */
146
+ const SERVER_ID_PATTERN = /"id"\s*:\s*"((?:cmb-)?[0-9a-f]{32})"/;
147
+ const DESKTOP_TASK_UA = "WorkBuddy/5.5.6 WorkBuddy/5.5.6 CLI/2.137.1";
148
+ /**
149
+ * Derive a stable 36-hex device id from the account uid.
150
+ *
151
+ * The upstream keys desktop events to a device. Deriving it from the uid keeps
152
+ * the same account looking like the same machine across runs, instead of a
153
+ * new device appearing on every call.
154
+ */
155
+ function deriveDeviceId(credential, salt) {
156
+ return createHash("sha256").update(salt + ":" + (credential.uid ?? "")).digest("hex").slice(0, 36);
157
+ }
158
+ /** Narrow a loose upstream value to an object, so field reads cannot throw. */
159
+ function asRecord(value) {
160
+ return typeof value === "object" && value !== null ? value : {};
161
+ }
162
+ /** Read a numeric field, treating anything else as 0. */
163
+ function numOf(value) {
164
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
165
+ }
166
+ /** Local `YYYY-MM-DD`, matching how the heatmap keys its cells. */
167
+ function dayKeyLocal(date) {
168
+ const month = String(date.getMonth() + 1).padStart(2, "0");
169
+ const day = String(date.getDate()).padStart(2, "0");
170
+ return `${date.getFullYear()}-${month}-${day}`;
171
+ }
126
172
  /** Billing request headers. */
127
173
  function billingHeaders(credential) {
128
174
  const headers = {
@@ -190,6 +236,17 @@ function classifyUpstreamError(status, body) {
190
236
  if (status >= 500) return "server";
191
237
  return "client";
192
238
  }
239
+ /**
240
+ * Whether an error means "today is already checked in".
241
+ *
242
+ * Callers treat this as success: the credit for the day is already banked, so
243
+ * reporting it as a failure would both alarm the user and hide a healthy
244
+ * account behind a false negative.
245
+ */
246
+ function isAlreadyCheckin(error) {
247
+ const message = error instanceof Error ? error.message : String(error);
248
+ return ALREADY_CHECKIN_MARKERS.some((marker) => message.includes(marker));
249
+ }
193
250
  /** Parse the upstream's `credits` string into a multiplier. */
194
251
  function parseCreditMultiplier(value) {
195
252
  if (typeof value !== "string") return void 0;
@@ -286,6 +343,199 @@ function parseUpstreamModel(value) {
286
343
  ...supportsImages === void 0 ? {} : { supportsImages }
287
344
  };
288
345
  }
346
+ /**
347
+ * Flatten one upstream task entry.
348
+ *
349
+ * Progress is reported two ways depending on the task: flat `current`/`target`
350
+ * fields, or a nested `progress: {current, target}`. The nested form wins when
351
+ * it carries anything, because a task that reports both puts the live counter
352
+ * there. Entries without a usable `task_code` are dropped — without one the
353
+ * claim path cannot be built.
354
+ */
355
+ /**
356
+ * The event chain that scores the two Buddy-app tasks.
357
+ *
358
+ * Both tasks accept the same chain, and the chain is a pure value: building it
359
+ * needs no client, so callers (and tests) can hold one without a live upstream.
360
+ *
361
+ * The task text says "upgrade to the desktop client and open it from the app
362
+ * launcher". The scorer does not watch the UI — it watches this event sequence
363
+ * with the desktop fingerprint, which is why the sequence is what gets sent.
364
+ *
365
+ * Measured against the live upstream: progress 0/1 → 1/1 claimable in ~8s.
366
+ */
367
+ /**
368
+ * A complete "desktop client ran a request successfully" event chain.
369
+ *
370
+ * Six events in the order the real client emits them: task created, message
371
+ * send, request send, message response, message status, request response.
372
+ * Several tasks are scored off this chain (or one that embeds it), because what
373
+ * they measure is "a real request completed", which the client only proves
374
+ * through this exact sequence.
375
+ *
376
+ * Measured: this chain alone lights up `RichMeow_Chat`.
377
+ */
378
+ function desktopChatEvents(conversationId, requestId, messageId, modelId = "fast-model", modelName = "fast-model") {
379
+ const assistant = `${messageId}-assistant`;
380
+ const session = {
381
+ "codebuddy.session_id": conversationId,
382
+ "codebuddy.conversation_request_id": requestId
383
+ };
384
+ return [
385
+ {
386
+ eventCode: "agent_task_created",
387
+ source: "LOCAL",
388
+ name: "working",
389
+ task_target: "local",
390
+ mode: "craft",
391
+ requestModelId: modelId,
392
+ requestModelName: modelName,
393
+ has_repo: false,
394
+ repo_type: "none",
395
+ workspace_type: "empty",
396
+ has_connector: false,
397
+ connector_types: [],
398
+ has_mention: false,
399
+ mention_types: [],
400
+ has_template: false,
401
+ action: "",
402
+ template_name: "",
403
+ has_expert: false,
404
+ expert_id: "",
405
+ expert_name: "",
406
+ expert_industry_id: "",
407
+ has_skill: false,
408
+ skill_names: [],
409
+ conversationId,
410
+ messageId,
411
+ buddyId: "",
412
+ buddyName: ""
413
+ },
414
+ {
415
+ eventCode: "chat_message_send",
416
+ messageId: assistant,
417
+ historyCount: 0,
418
+ isContextTruncated: false,
419
+ currentStepCount: 1,
420
+ traceId: requestId,
421
+ rootRequestId: requestId,
422
+ parentConversationId: conversationId,
423
+ agentName: "cli",
424
+ agentType: "main"
425
+ },
426
+ {
427
+ eventCode: "chat_request_send",
428
+ inputLength: 24,
429
+ isPlan: false,
430
+ isAutoExecuteTerminal: false,
431
+ isAutoModify: false,
432
+ codebaseEnable: false,
433
+ maxToken: 0,
434
+ maxSteps: 500,
435
+ temperature: 0,
436
+ maxRetries: 0,
437
+ mentionContexts: [],
438
+ knowledgeId: [],
439
+ knowledgeName: [],
440
+ codebaseId: "",
441
+ mentionContextCount: 0,
442
+ command: "",
443
+ recommendId: "",
444
+ skillId: "",
445
+ skillCount: 0,
446
+ totalCount: 0,
447
+ traceId: requestId,
448
+ rootRequestId: requestId,
449
+ parentConversationId: conversationId,
450
+ agentName: "cli",
451
+ agentType: "main",
452
+ ...session
453
+ },
454
+ {
455
+ eventCode: "chat_message_response",
456
+ messageId: assistant,
457
+ responseModelId: modelId,
458
+ inputToken: 120,
459
+ outputToken: 80,
460
+ totalToken: 200,
461
+ cachedTokens: 0,
462
+ cachedWriteTokens: 0,
463
+ cachedMissTokens: 0,
464
+ isSuccessful: true,
465
+ messageErrorCode: "",
466
+ finishReason: "stop",
467
+ firstTokenAt: Date.now(),
468
+ traceId: requestId,
469
+ conversationId,
470
+ rootRequestId: requestId,
471
+ parentConversationId: conversationId,
472
+ agentName: "cli",
473
+ agentType: "main",
474
+ ...session
475
+ },
476
+ {
477
+ eventCode: "chat_message_status",
478
+ messageId: assistant,
479
+ messageErrorCode: "0",
480
+ traceId: requestId,
481
+ rootRequestId: requestId,
482
+ parentConversationId: conversationId,
483
+ agentName: "cli",
484
+ agentType: "main"
485
+ },
486
+ {
487
+ eventCode: "chat_request_response",
488
+ mode: "craft",
489
+ toolCallCount: 0,
490
+ inputToken: 120,
491
+ outputToken: 80,
492
+ totalToken: 200,
493
+ cachedTokens: 0,
494
+ cachedWriteTokens: 0,
495
+ cachedMissTokens: 0,
496
+ isSuccessful: true,
497
+ messageErrorCode: "",
498
+ finishReason: "stop",
499
+ rootRequestId: requestId,
500
+ parentConversationId: conversationId
501
+ }
502
+ ];
503
+ }
504
+ function parseTask(value) {
505
+ if (typeof value !== "object" || value === null) return void 0;
506
+ const raw = value;
507
+ const taskCode = typeof raw["task_code"] === "string" ? raw["task_code"] : "";
508
+ if (taskCode === "") return void 0;
509
+ const num = (key) => typeof raw[key] === "number" ? raw[key] : 0;
510
+ let current = num("current");
511
+ let target = num("target");
512
+ const progress = raw["progress"];
513
+ if (typeof progress === "object" && progress !== null) {
514
+ const nested = progress;
515
+ const nestedCurrent = typeof nested["current"] === "number" ? nested["current"] : 0;
516
+ const nestedTarget = typeof nested["target"] === "number" ? nested["target"] : 0;
517
+ if (nestedTarget > 0 || nestedCurrent > 0) {
518
+ current = nestedCurrent;
519
+ target = nestedTarget;
520
+ }
521
+ }
522
+ const acceptStatus = typeof raw["accept_status"] === "string" ? raw["accept_status"] : "";
523
+ const claimed = acceptStatus === "claimed";
524
+ return {
525
+ taskCode,
526
+ title: typeof raw["title"] === "string" && raw["title"] !== "" ? raw["title"] : taskCode,
527
+ credit: num("reward_credit"),
528
+ energy: num("reward_energy"),
529
+ hasReward: raw["has_reward"] === true,
530
+ target,
531
+ current,
532
+ acceptStatus,
533
+ status: typeof raw["status"] === "string" ? raw["status"] : "",
534
+ claimable: !claimed && target > 0 && current >= target,
535
+ claimed,
536
+ locked: raw["locked"] === true
537
+ };
538
+ }
289
539
  var WorkBuddyUpstreamClient = class {
290
540
  fetchImpl;
291
541
  clientVersion;
@@ -562,6 +812,531 @@ var WorkBuddyUpstreamClient = class {
562
812
  isStreakDay: data["is_streak_day"] === true
563
813
  };
564
814
  }
815
+ /**
816
+ * Public fingerprint fields every desktop event carries.
817
+ *
818
+ * The upstream scores a desktop-fingerprinted task only when the event looks
819
+ * like it came from the desktop client: the same field set, the same stable
820
+ * device ids, the same build. A partial map is accepted with 200 and scores
821
+ * nothing, so these are copied wholesale rather than trimmed.
822
+ *
823
+ * `machineId`/`sessionId` are DERIVED from the account uid, never random: a
824
+ * new device id on every call is itself a signal that the traffic is not a
825
+ * real client.
826
+ */
827
+ desktopFingerprint(credential) {
828
+ const now = Date.now();
829
+ return {
830
+ timezone: "Asia/Shanghai",
831
+ reportDelay: 2e3,
832
+ userId: credential.uid ?? "",
833
+ username: credential.nickname ?? "",
834
+ userNickname: credential.nickname ?? "",
835
+ product: "SaaS",
836
+ releaseDate: 1789036585355,
837
+ commit: "5f9692923c93033111c51ad7b003eb80204a9b75",
838
+ ideName: "WorkBuddy",
839
+ ideType: "WorkBuddy",
840
+ ideVersion: "5.5.6",
841
+ machineId: deriveDeviceId(credential, "machine"),
842
+ sessionId: deriveDeviceId(credential, "session"),
843
+ extName: "workbuddy-desktop",
844
+ extVersion: "5.5.6",
845
+ os: "win32",
846
+ arch: "x64",
847
+ osVersion: "10.0.26220",
848
+ cpuCores: 20,
849
+ memorySize: 24,
850
+ timestamp: now,
851
+ presentAt: now
852
+ };
853
+ }
854
+ /**
855
+ * Send desktop-fingerprinted events to the growth system.
856
+ *
857
+ * The body is an ARRAY of events, and every event carries the full desktop
858
+ * fingerprint plus its own business fields. Different tasks recognise
859
+ * different fingerprint families (CLI / desktop / web), which is why this is
860
+ * separate from {@link reportActivity}: they are not interchangeable.
861
+ *
862
+ * Business fields win over the fingerprint, so a caller can override a device
863
+ * id to align with a real install.
864
+ */
865
+ async reportDesktopEvents(credential, events) {
866
+ if (events.length === 0) return;
867
+ const fingerprint = this.desktopFingerprint(credential);
868
+ const body = events.map((event) => ({
869
+ ...fingerprint,
870
+ ...event
871
+ }));
872
+ const response = await this.fetchImpl(`${chatBase(credential)}${DESKTOP_REPORT_PATH}`, {
873
+ method: "POST",
874
+ headers: {
875
+ "Authorization": `Bearer ${credential.accessToken}`,
876
+ "Accept": "application/json, text/plain, */*",
877
+ "Content-Type": "application/json;charset=UTF-8",
878
+ "User-Agent": DESKTOP_TASK_UA,
879
+ "X-Product": "SaaS",
880
+ "X-Request-ID": deriveDeviceId(credential, "req") + String(Date.now() % 1e6),
881
+ ...credential.uid === void 0 || credential.uid === "" ? {} : { "X-User-Id": credential.uid },
882
+ ...credential.domain === "" ? {} : { "X-Domain": credential.domain }
883
+ },
884
+ body: JSON.stringify(body),
885
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
886
+ });
887
+ const envelope = await readEnvelope(response);
888
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
889
+ }
890
+ /**
891
+ * Report one chat-activity event to the growth system.
892
+ *
893
+ * The body is an ARRAY holding a single `chat_request_send` event, and every
894
+ * field is filled in: a three-field minimal event is accepted with 200 and
895
+ * then silently dropped, so the full shape is load-bearing rather than
896
+ * cosmetic. `userId` is the one field the server actually keys on — without
897
+ * it the request still answers 200 and scores nothing.
898
+ *
899
+ * One report per account per day is the quota the reference panel settled on;
900
+ * a single report lights the growth streak and unlocks the `first_buddy`
901
+ * family, which is why this runs before the task-centre pass.
902
+
903
+ /**
904
+ * Send a WEB-fingerprinted event.
905
+ *
906
+ * A third fingerprint family, alongside CLI and desktop: a browser shape
907
+ * posted to the web origin with x-client-platform: web. Page-behaviour
908
+ * tasks such as `Library_read` are scored on it. Measured:
909
+ * `library_doc_intro_click` scored about four seconds after landing.
910
+ */
911
+ async reportWebEvent(credential, eventCode, pageUrl, elementId, elementName) {
912
+ const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/152.0.0.0 Safari/537.36";
913
+ const event = {
914
+ eventCode,
915
+ timestamp: Date.now(),
916
+ reportDelay: 0,
917
+ pageURL: pageUrl,
918
+ elementId,
919
+ elementName,
920
+ os: "Win32",
921
+ arch: "",
922
+ osVersion: "10.0",
923
+ userAgent: ua,
924
+ machineId: deriveDeviceId(credential, "webmachine"),
925
+ userId: credential.uid ?? "",
926
+ userNickname: credential.nickname ?? "",
927
+ enterpriseId: credential.enterpriseId ?? ""
928
+ };
929
+ const response = await this.fetchImpl(`https://www.workbuddy.cn/v2/report`, {
930
+ method: "POST",
931
+ headers: {
932
+ "Authorization": `Bearer ${credential.accessToken}`,
933
+ "Content-Type": "application/json",
934
+ "Accept": "application/json",
935
+ "x-client-platform": "web",
936
+ "Origin": "https://www.workbuddy.cn",
937
+ "Referer": pageUrl,
938
+ "User-Agent": ua,
939
+ ...credential.uid === void 0 || credential.uid === "" ? {} : { "X-User-Id": credential.uid }
940
+ },
941
+ body: JSON.stringify([event]),
942
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
943
+ });
944
+ const envelope = await readEnvelope(response);
945
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
946
+ }
947
+ /**
948
+ * Apply an appearance theme on the account.
949
+ *
950
+ * The theme task is scored on the `appearance_skin_apply` event, not on this
951
+ * call — but the event alone is not enough either. The pair is what a real
952
+ * client produces: it PATCHes the account's selected skin, then reports the
953
+ * event as the settings page closes. Measured on the reference panel after
954
+ * the earlier "the API alone does not score" reading was corrected.
955
+ */
956
+ async setAppearanceTheme(credential, resourceKey) {
957
+ const response = await this.fetchImpl(`${chatBase(credential)}${APPEARANCE_SET_PATH}`, {
958
+ method: "POST",
959
+ headers: {
960
+ "Authorization": `Bearer ${credential.accessToken}`,
961
+ "Accept": "application/json, text/plain, */*",
962
+ "Content-Type": "application/json;charset=UTF-8",
963
+ "User-Agent": DESKTOP_TASK_UA,
964
+ "X-Product": "SaaS",
965
+ ...credential.uid === void 0 || credential.uid === "" ? {} : { "X-User-Id": credential.uid }
966
+ },
967
+ body: JSON.stringify({
968
+ kind: "theme",
969
+ resource_key: resourceKey
970
+ }),
971
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
972
+ });
973
+ const envelope = await readEnvelope(response);
974
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
975
+ }
976
+ /**
977
+ * The platform's expert marketplace.
978
+ *
979
+ * Needed before any expert can be summoned: the scorer verifies that the
980
+ * expert id exists on the platform, so a made-up id scores nothing. The
981
+ * response carries the display fields the summon events replay.
982
+ */
983
+ async marketExpertList(credential, expertType = "") {
984
+ const request = {
985
+ page: 1,
986
+ page_size: 20,
987
+ sort_by: "reco_rank",
988
+ sort_order: "desc"
989
+ };
990
+ if (expertType !== "") request["expert_type"] = expertType;
991
+ const response = await this.fetchImpl(`${chatBase(credential)}${MARKET_EXPERT_LIST_PATH}`, {
992
+ method: "POST",
993
+ headers: {
994
+ ...chatHeaders(credential),
995
+ "User-Agent": DESKTOP_TASK_UA
996
+ },
997
+ body: JSON.stringify(request),
998
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
999
+ });
1000
+ const envelope = await readEnvelope(response);
1001
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
1002
+ const data = asRecord(envelope.data);
1003
+ const raw = Array.isArray(data["experts"]) ? data["experts"] : [];
1004
+ const out = [];
1005
+ for (const value of raw) {
1006
+ const expert = asRecord(value);
1007
+ const expertId = typeof expert["expert_id"] === "string" ? expert["expert_id"] : "";
1008
+ if (expertId === "") continue;
1009
+ out.push({
1010
+ expertId,
1011
+ expertType: typeof expert["expert_type"] === "string" ? expert["expert_type"] : "",
1012
+ displayName: typeof expert["display_name_zh"] === "string" ? expert["display_name_zh"] : "",
1013
+ profession: typeof expert["profession_zh"] === "string" ? expert["profession_zh"] : "",
1014
+ version: typeof expert["version"] === "string" ? expert["version"] : "",
1015
+ categories: Array.isArray(expert["categories"]) ? expert["categories"].filter((item) => typeof item === "string") : []
1016
+ });
1017
+ }
1018
+ return out;
1019
+ }
1020
+ /**
1021
+ * Open a REAL desktop conversation and return the ids the server assigned.
1022
+ *
1023
+ * The expert and skill tasks join their events to a conversation the server
1024
+ * has actually seen, so a locally invented `requestId` scores nothing. This
1025
+ * starts a chat, reads the server's id out of the SSE stream, then drops the
1026
+ * rest of the stream — the answer itself is irrelevant, only its identity is.
1027
+ *
1028
+ * Returns `undefined` instead of throwing when the conversation cannot be
1029
+ * opened or carries no recognisable id, because every caller is a best-effort
1030
+ * task chain.
1031
+ */
1032
+ async openConversation(credential, expertId = "", signal) {
1033
+ const conversationId = `wb2auto-conv-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
1034
+ const body = JSON.stringify({
1035
+ model: "fast-model",
1036
+ messages: [{
1037
+ role: "system",
1038
+ content: "You are a helpful assistant. 当前处于中文环境,使用简体中文回答。"
1039
+ }, {
1040
+ role: "user",
1041
+ content: "1+1等于几?直接回答。"
1042
+ }],
1043
+ agent: "cli",
1044
+ temperature: 1,
1045
+ stream: true,
1046
+ stream_options: { include_usage: true }
1047
+ });
1048
+ let response;
1049
+ try {
1050
+ response = await this.fetchImpl(`${chatBase(credential)}/v2/chat/completions`, {
1051
+ method: "POST",
1052
+ headers: {
1053
+ ...chatHeaders(credential),
1054
+ "Accept": "text/event-stream",
1055
+ "User-Agent": DESKTOP_TASK_UA,
1056
+ "X-Conversation-ID": conversationId,
1057
+ "X-Request-ID": String(Date.now()) + "000000",
1058
+ "X-Agent-Intent": "craft",
1059
+ "X-Agent-Type": "main",
1060
+ "X-IDE-Name": "WorkBuddy",
1061
+ "X-IDE-Type": "WorkBuddy",
1062
+ "X-IDE-Version": "5.5.6",
1063
+ "x-codebuddy-request": "1",
1064
+ ...expertId === "" ? {} : { "X-Expert-Id": expertId }
1065
+ },
1066
+ body,
1067
+ signal: signal ?? AbortSignal.timeout(CHAT_TIMEOUT_MS)
1068
+ });
1069
+ } catch {
1070
+ return;
1071
+ }
1072
+ if (!response.ok || response.body === null) {
1073
+ await response.body?.cancel().catch(() => {});
1074
+ return;
1075
+ }
1076
+ const reader = response.body.getReader();
1077
+ const decoder = new TextDecoder();
1078
+ let buffer = "";
1079
+ try {
1080
+ while (buffer.length < SSE_SCAN_LIMIT) {
1081
+ const chunk = await reader.read();
1082
+ if (chunk.done) break;
1083
+ buffer += decoder.decode(chunk.value, { stream: true });
1084
+ const match = SERVER_ID_PATTERN.exec(buffer);
1085
+ if (match !== null) return {
1086
+ conversationId,
1087
+ requestId: match[1] ?? ""
1088
+ };
1089
+ }
1090
+ } catch {} finally {
1091
+ await reader.cancel().catch(() => {});
1092
+ }
1093
+ }
1094
+ /**
1095
+ * Report one chat-activity event to the growth system.
1096
+ *
1097
+ * The body is an ARRAY holding a single chat_request_send event, and every
1098
+ * field is filled in: a three-field minimal event is accepted with 200 and
1099
+ * then silently dropped, so the full shape is load-bearing rather than
1100
+ * cosmetic. userId is the one field the server actually keys on.
1101
+ *
1102
+ * One report per account per day is the quota the reference panel settled
1103
+ * on; a single report lights the growth streak and unlocks the first_buddy
1104
+ * family, which is why this runs before the task-centre pass.
1105
+ */
1106
+ async reportActivity(credential, conversationId) {
1107
+ const conversationID = conversationId ?? `wb2api-${Date.now()}`;
1108
+ const requestID = conversationID;
1109
+ const now = Date.now();
1110
+ const event = {
1111
+ eventCode: "chat_request_send",
1112
+ timestamp: now,
1113
+ reportDelay: 0,
1114
+ mode: "craft",
1115
+ conversationId: conversationID,
1116
+ requestId: requestID,
1117
+ inputLength: 12,
1118
+ requestModelId: "deepseek-v4-flash",
1119
+ requestModelName: "DeepSeek V4 Flash",
1120
+ isPlan: false,
1121
+ isAutoExecuteTerminal: false,
1122
+ isAutoModify: false,
1123
+ codebaseEnable: false,
1124
+ maxToken: 0,
1125
+ maxSteps: 0,
1126
+ temperature: 0,
1127
+ maxRetries: 0,
1128
+ mentionContexts: [],
1129
+ knowledgeId: [],
1130
+ knowledgeName: [],
1131
+ codebaseId: "",
1132
+ mentionContextCount: 0,
1133
+ command: "",
1134
+ expertId: "",
1135
+ recommendId: "",
1136
+ skillId: "",
1137
+ skillCount: 0,
1138
+ totalCount: 0,
1139
+ fileUri: "",
1140
+ presentAt: now,
1141
+ traceId: "",
1142
+ rootRequestId: requestID,
1143
+ parentConversationId: conversationID,
1144
+ agentName: "default",
1145
+ agentType: "conversation",
1146
+ userId: credential.uid ?? ""
1147
+ };
1148
+ const response = await this.fetchImpl(`${billingBase(credential)}/v2/report`, {
1149
+ method: "POST",
1150
+ headers: billingHeaders(credential),
1151
+ body: JSON.stringify([event]),
1152
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
1153
+ });
1154
+ const envelope = await readEnvelope(response);
1155
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
1156
+ }
1157
+ /**
1158
+ * Read back the growth streak in days.
1159
+ *
1160
+ * This is the read-only oracle for {@link reportActivity}: a report that
1161
+ * returned 200 yet left the streak untouched was silently dropped (a missing
1162
+ * `userId` is the usual cause), so callers verify instead of trusting the
1163
+ * status code.
1164
+ *
1165
+ * Two shape traps, both measured against the live upstream:
1166
+ *
1167
+ * - The path carries NO `/v2` prefix, unlike its sibling task endpoints under
1168
+ * `/v2/activity/growth/*`. Asking for the `/v2` form does not 404; it
1169
+ * answers with a body that carries no `streak` object at all.
1170
+ * - The counter is nested as `data.streak.days`, not `data.days`. Reading the
1171
+ * flat field yields a constant 0, which would make every successful report
1172
+ * look like a silent drop.
1173
+ *
1174
+ * Returns 0 only when the field is genuinely absent.
1175
+ */
1176
+ async growthStreakDays(credential) {
1177
+ const response = await this.fetchImpl(`${chatBase(credential)}/activity/growth/streak`, {
1178
+ headers: billingHeaders(credential),
1179
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
1180
+ });
1181
+ const envelope = await readEnvelope(response);
1182
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
1183
+ const data = typeof envelope.data === "object" && envelope.data !== null ? envelope.data : {};
1184
+ const streak = typeof data["streak"] === "object" && data["streak"] !== null ? data["streak"] : {};
1185
+ return typeof streak["days"] === "number" ? streak["days"] : 0;
1186
+ }
1187
+ /**
1188
+ * The full streak picture: days, tier unlock state, and what each tier pays.
1189
+ *
1190
+ * Read before redeeming, because the tier state is the only honest answer to
1191
+ * "is there anything to claim": the redeem endpoint answers 403 for a locked
1192
+ * tier, which is indistinguishable from a real failure once the response is
1193
+ * just an error.
1194
+ */
1195
+ async growthStreakFull(credential) {
1196
+ const data = await this.growthJson(credential, "GET", "/activity/growth/streak");
1197
+ const streak = asRecord(data["streak"]);
1198
+ const redemption = asRecord(data["redemption_status"]);
1199
+ const cards = asRecord(data["makeup_cards"]);
1200
+ const tiers = [];
1201
+ if (Array.isArray(redemption["tiers"])) for (const entry of redemption["tiers"]) {
1202
+ const tier = asRecord(entry);
1203
+ const name = typeof tier["tier"] === "string" ? tier["tier"] : "";
1204
+ if (name === "") continue;
1205
+ tiers.push({
1206
+ tier: name,
1207
+ days: numOf(tier["days"]),
1208
+ credit: numOf(tier["credit"]),
1209
+ energy: numOf(tier["energy"]),
1210
+ cards: numOf(tier["cards"]),
1211
+ chances: numOf(tier["chances"]),
1212
+ status: String(redemption[`tier_${name}_status`] ?? "")
1213
+ });
1214
+ }
1215
+ return {
1216
+ days: numOf(streak["days"]),
1217
+ monthTotalDays: numOf(streak["month_total_days"]),
1218
+ nextTier: typeof streak["next_tier"] === "string" ? streak["next_tier"] : "",
1219
+ nextTierRemaining: numOf(streak["next_tier_remaining"]),
1220
+ makeupCards: numOf(cards["balance"]),
1221
+ tiers
1222
+ };
1223
+ }
1224
+ /**
1225
+ * Redeem one unlocked streak tier.
1226
+ *
1227
+ * A locked tier answers 403 ("连续登录天数不足"); callers check the status from
1228
+ * {@link growthStreakFull} first, so this only throws for genuine failures.
1229
+ * The client token is the upstream's idempotency key — a fresh one per attempt
1230
+ * keeps a retry from being read as a duplicate of the last one.
1231
+ */
1232
+ async redeemStreakTier(credential, tier) {
1233
+ await this.growthJson(credential, "POST", "/activity/growth/redeem", {
1234
+ tier,
1235
+ client_token: randomUUID()
1236
+ });
1237
+ }
1238
+ /** How many lottery draws are available right now. */
1239
+ async lotteryChances(credential) {
1240
+ return numOf((await this.growthJson(credential, "GET", "/activity/growth/lottery/summary"))["chances"]);
1241
+ }
1242
+ /**
1243
+ * Draw the lottery once.
1244
+ *
1245
+ * Returns the raw prize payload: its shape is set by the running campaign, so
1246
+ * it is passed through rather than modelled.
1247
+ */
1248
+ async lotteryDraw(credential) {
1249
+ return this.growthJson(credential, "POST", "/activity/growth/lottery/draw", { client_token: randomUUID() });
1250
+ }
1251
+ /**
1252
+ * The buddy profile, or undefined when the account has no buddy yet.
1253
+ *
1254
+ * `data.buddy` is null / absent / an empty object depending on how far the
1255
+ * account got, and all three mean the same thing to a caller: adopt first.
1256
+ */
1257
+ async buddyInfo(credential) {
1258
+ const buddy = asRecord((await this.growthJson(credential, "GET", "/activity/growth/buddy/info"))["buddy"]);
1259
+ if (Object.keys(buddy).length === 0) return void 0;
1260
+ return {
1261
+ instanceId: numOf(buddy["instance_id"]),
1262
+ name: String(buddy["name"] ?? "")
1263
+ };
1264
+ }
1265
+ /** Agree to the buddy terms. Idempotent upstream. */
1266
+ async buddyAgree(credential) {
1267
+ await this.growthJson(credential, "POST", "/activity/growth/buddy/agreement", { agree: true });
1268
+ }
1269
+ /**
1270
+ * Adopt the first buddy.
1271
+ *
1272
+ * Gated upstream on having reported activity that day: without it the answer
1273
+ * is 400 "first_buddy task not completed yet". Callers treat that as "not yet"
1274
+ * rather than an error, which is why it is thrown as-is for them to classify.
1275
+ */
1276
+ async buddyAdoptFirst(credential) {
1277
+ await this.growthJson(credential, "POST", "/activity/growth/buddy/first", {});
1278
+ }
1279
+ /** Current travel state for the account's buddy. */
1280
+ async buddyTravelStatus(credential) {
1281
+ const data = await this.growthJson(credential, "GET", "/activity/growth/buddy/travel/status");
1282
+ return {
1283
+ state: typeof data["state"] === "string" ? data["state"] : "",
1284
+ recordId: numOf(data["record_id"]),
1285
+ dailyLimitReached: data["daily_limit_reached"] === true,
1286
+ rewardCredit: numOf(data["reward_credit"])
1287
+ };
1288
+ }
1289
+ /**
1290
+ * Send the buddy travelling.
1291
+ *
1292
+ * The location is always 4 (古镇客栈): the four locations have identical
1293
+ * reward and duration ranges, so there is nothing to optimise.
1294
+ */
1295
+ async buddyTravelDepart(credential, locationId = 4) {
1296
+ await this.growthJson(credential, "POST", "/activity/growth/buddy/travel/depart", { location_id: locationId });
1297
+ }
1298
+ /**
1299
+ * Collect an arrived trip's reward.
1300
+ *
1301
+ * `recordId` is required and comes from the status read; the upstream rejects
1302
+ * a claim without it.
1303
+ */
1304
+ async buddyTravelClaim(credential, recordId) {
1305
+ return numOf((await this.growthJson(credential, "POST", "/activity/growth/buddy/travel/claim", { record_id: recordId }))["reward_credit"]);
1306
+ }
1307
+ /** Whether yesterday is a gap in the activity heatmap. */
1308
+ async heatmapYesterdayMissed(credential) {
1309
+ const data = await this.growthJson(credential, "GET", "/activity/growth/heatmap");
1310
+ if (!Array.isArray(data["cells"])) return false;
1311
+ const key = dayKeyLocal(/* @__PURE__ */ new Date(Date.now() - 864e5));
1312
+ for (const entry of data["cells"]) {
1313
+ const cell = asRecord(entry);
1314
+ if (cell["date"] === key) return numOf(cell["score"]) === 0;
1315
+ }
1316
+ return false;
1317
+ }
1318
+ /** Spend one makeup card on a date. Idempotent for an already-filled date. */
1319
+ async useMakeupCard(credential, date) {
1320
+ await this.growthJson(credential, "POST", "/activity/growth/makeup-cards/use", { date });
1321
+ }
1322
+ /**
1323
+ * Call a growth-domain endpoint and return its unwrapped `data`.
1324
+ *
1325
+ * These endpoints live on the chat host with the billing header set, and
1326
+ * carry the same envelope as everything else. Centralised here because every
1327
+ * growth call needs the identical envelope check.
1328
+ */
1329
+ async growthJson(credential, method, path, body) {
1330
+ const response = await this.fetchImpl(`${chatBase(credential)}${path}`, {
1331
+ method,
1332
+ headers: billingHeaders(credential),
1333
+ ...body === void 0 ? {} : { body: JSON.stringify(body) },
1334
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
1335
+ });
1336
+ const envelope = await readEnvelope(response);
1337
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
1338
+ return asRecord(envelope.data);
1339
+ }
565
1340
  /** Legacy thin wrapper kept for `status`/`doctor`: returns raw envelope data. */
566
1341
  async credits(credential) {
567
1342
  try {
@@ -576,6 +1351,87 @@ var WorkBuddyUpstreamClient = class {
576
1351
  };
577
1352
  }
578
1353
  }
1354
+ /**
1355
+ * Fetch the growth task list for one account.
1356
+ *
1357
+ * The upstream answers `data.tasks[]`, and `claimable` is derived locally —
1358
+ * the upstream does not mark it. Only a task whose progress reached its
1359
+ * target and that is not already claimed counts as eligible.
1360
+ */
1361
+ async listTasks(credential) {
1362
+ const response = await this.fetchImpl(`${chatBase(credential)}/v2/activity/growth/tasks`, {
1363
+ headers: chatHeaders(credential),
1364
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
1365
+ });
1366
+ const envelope = await readEnvelope(response);
1367
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
1368
+ const data = typeof envelope.data === "object" && envelope.data !== null ? envelope.data : {};
1369
+ const raw = Array.isArray(data["tasks"]) ? data["tasks"] : [];
1370
+ const out = [];
1371
+ for (const entry of raw) {
1372
+ const parsed = parseTask(entry);
1373
+ if (parsed !== void 0) out.push(parsed);
1374
+ }
1375
+ return out;
1376
+ }
1377
+ /**
1378
+ * Accept (enrol in) tasks by code.
1379
+ *
1380
+ * Accepting is the "sign up" half: it produces no progress by itself, and the
1381
+ * upstream answers success for an already-accepted task, so replaying this is
1382
+ * safe. Progress is lit by real activity (a chat, an activity report).
1383
+ */
1384
+ async acceptTasks(credential, taskCodes) {
1385
+ if (taskCodes.length === 0) return;
1386
+ const response = await this.fetchImpl(`${chatBase(credential)}/v2/activity/growth/tasks/accept`, {
1387
+ method: "POST",
1388
+ headers: chatHeaders(credential),
1389
+ body: JSON.stringify({ task_codes: [...taskCodes] }),
1390
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
1391
+ });
1392
+ const envelope = await readEnvelope(response);
1393
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
1394
+ }
1395
+ /**
1396
+ * Claim one task's reward.
1397
+ *
1398
+ * Two details differ from list/accept and are load-bearing:
1399
+ *
1400
+ * - The task code rides the PATH, not the body.
1401
+ * - It is served by the web origin, not the chat host, and only when the
1402
+ * request carries the growth-centre Origin/Referer plus
1403
+ * `x-client-platform: web`. The chat host's `/reward/claim` path does not
1404
+ * exist and answers 400 "task not completed".
1405
+ *
1406
+ * A repeat claim answers `already_claimed` with zero credit, which is treated
1407
+ * as success so the caller can stay idempotent.
1408
+ */
1409
+ async claimTaskReward(credential, taskCode) {
1410
+ const headers = {
1411
+ ...billingHeaders(credential),
1412
+ "Accept": "application/json, text/plain, */*",
1413
+ "Origin": "https://www.workbuddy.cn",
1414
+ "Referer": "https://www.workbuddy.cn/profile/growth-center",
1415
+ "x-client-platform": "web",
1416
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
1417
+ };
1418
+ const response = await this.fetchImpl(`https://www.workbuddy.cn/activity/growth/tasks/${encodeURIComponent(taskCode)}/claim`, {
1419
+ method: "POST",
1420
+ headers,
1421
+ signal: AbortSignal.timeout(JSON_TIMEOUT_MS)
1422
+ });
1423
+ const envelope = await readEnvelope(response);
1424
+ if (!response.ok || envelope.code !== 0) throw envelopeError(response.status, envelope);
1425
+ const data = typeof envelope.data === "object" && envelope.data !== null ? envelope.data : {};
1426
+ if (data["already_claimed"] === true) return {
1427
+ credit: 0,
1428
+ energy: 0
1429
+ };
1430
+ return {
1431
+ credit: typeof data["credit"] === "number" ? data["credit"] : 0,
1432
+ energy: typeof data["energy"] === "number" ? data["energy"] : 0
1433
+ };
1434
+ }
579
1435
  };
580
1436
  //#endregion
581
1437
  //#region src/accounts.ts
@@ -807,6 +1663,25 @@ var WorkBuddyAccountPool = class {
807
1663
  */
808
1664
  disabledIds = /* @__PURE__ */ new Set();
809
1665
  /**
1666
+ * Per-account credit floor, keyed by account id. 0 (or absent) means "spend
1667
+ * it all".
1668
+ *
1669
+ * A reserved balance is protection, not a hard limit the upstream knows
1670
+ * about: the pool simply stops picking that account once its last known
1671
+ * balance is at or below the floor, so the user keeps a cushion instead of
1672
+ * draining every account to zero.
1673
+ */
1674
+ creditReserves = /* @__PURE__ */ new Map();
1675
+ /**
1676
+ * Last known credit balance per account, epoch ms aside.
1677
+ *
1678
+ * Refreshed in the background after a successful request, so a pick can
1679
+ * consult it. An account with no reading is treated as usable: refusing to
1680
+ * pick an account just because its balance has not been checked yet would
1681
+ * strand a healthy pool, and the first 402 still cools it as before.
1682
+ */
1683
+ creditBalances = /* @__PURE__ */ new Map();
1684
+ /**
810
1685
  * Last time each account served a request, epoch ms. Drives the idle term
811
1686
  * of the priority-mode weighting below: an account that just served loses to
812
1687
  * one that has been idle, so a small pool stops hammering a single account.
@@ -837,6 +1712,7 @@ var WorkBuddyAccountPool = class {
837
1712
  if (options.exhaustCooldownMs !== void 0 && options.exhaustCooldownMs >= 1e3) this.exhaustCooldownMs = options.exhaustCooldownMs;
838
1713
  if (options.distribution !== void 0) this.distribution = options.distribution;
839
1714
  if (options.disabledAccountIds !== void 0) this.disabledIds = new Set(options.disabledAccountIds);
1715
+ if (options.creditReserves !== void 0) this.setCreditReserves(options.creditReserves);
840
1716
  }
841
1717
  /** Rescan the auth directories and merge newly discovered accounts. */
842
1718
  async scan() {
@@ -890,6 +1766,11 @@ var WorkBuddyAccountPool = class {
890
1766
  available(now, modelId, region) {
891
1767
  return this.accounts.filter((account) => {
892
1768
  if (this.disabledIds.has(account.id)) return false;
1769
+ const reserve = this.creditReserves.get(account.id);
1770
+ if (reserve !== void 0 && reserve > 0) {
1771
+ const balance = this.creditBalances.get(account.id);
1772
+ if (balance !== void 0 && balance <= reserve) return false;
1773
+ }
893
1774
  if (account.cooldownUntilMs > now) return false;
894
1775
  if (modelId !== void 0 && (account.modelCooldowns[modelId] ?? 0) > now) return false;
895
1776
  if (region !== void 0 && regionOf(account.credential.domain) !== region) return false;
@@ -999,6 +1880,57 @@ var WorkBuddyAccountPool = class {
999
1880
  this.lastUsedAt.set(accountId, Date.now());
1000
1881
  }
1001
1882
  /**
1883
+ * Record an account latest known credit balance.
1884
+ *
1885
+ * Called after a request and by the card balance refresh, so the reserve
1886
+ * check has something to compare against. A reading for an unknown account is
1887
+ * dropped: `scan()` rebuilds the account list and a stale id would otherwise
1888
+ * accumulate forever.
1889
+ */
1890
+ noteCredits(accountId, balance) {
1891
+ if (!Number.isFinite(balance)) return;
1892
+ if (!this.accounts.some((account) => account.id === accountId)) return;
1893
+ this.creditBalances.set(accountId, balance);
1894
+ }
1895
+ /** Last known balance for one account, or undefined when never read. */
1896
+ creditsOf(accountId) {
1897
+ return this.creditBalances.get(accountId);
1898
+ }
1899
+ /** The credit floor the user set for one account; 0 when unset. */
1900
+ creditReserveOf(accountId) {
1901
+ return this.creditReserves.get(accountId) ?? 0;
1902
+ }
1903
+ /**
1904
+ * Replace every reserve. Called from settings on each apply, so the map
1905
+ * mirrors the saved document exactly instead of accumulating old keys.
1906
+ */
1907
+ setCreditReserves(reserves) {
1908
+ const next = /* @__PURE__ */ new Map();
1909
+ for (const [id, value] of Object.entries(reserves)) if (Number.isFinite(value) && value > 0) next.set(id, Math.floor(value));
1910
+ this.creditReserves = next;
1911
+ }
1912
+ /** Every reserve currently in force, keyed by account id. */
1913
+ creditReservesInOrder() {
1914
+ const out = {};
1915
+ for (const account of this.accounts) {
1916
+ const reserve = this.creditReserves.get(account.id);
1917
+ if (reserve !== void 0 && reserve > 0) out[account.id] = reserve;
1918
+ }
1919
+ return out;
1920
+ }
1921
+ /**
1922
+ * Whether an account is held back only by its reserve.
1923
+ *
1924
+ * Separates "resting to protect credits" from every other reason an account
1925
+ * is out of rotation, which is what the card shows the user.
1926
+ */
1927
+ isReserved(accountId) {
1928
+ const reserve = this.creditReserves.get(accountId);
1929
+ if (reserve === void 0 || reserve <= 0) return false;
1930
+ const balance = this.creditBalances.get(accountId);
1931
+ return balance !== void 0 && balance <= reserve;
1932
+ }
1933
+ /**
1002
1934
  * The account that served the most recent request, if any.
1003
1935
  *
1004
1936
  * Distinct from "who would serve the next one": this is a record of what
@@ -1285,10 +2217,1244 @@ function catalogFromUpstream(models) {
1285
2217
  return models.map(toModelInfo);
1286
2218
  }
1287
2219
  //#endregion
1288
- //#region src/status.ts
1289
- /** Format the per-model credit multipliers. */
1290
- function formatRates(status) {
1291
- const lines = ["Model credit multipliers:"];
2220
+ //#region src/task-events.ts
2221
+ /**
2222
+ * The app the buddy chain enters.
2223
+ *
2224
+ * One chain lights up two tasks: `Buddy_App` (open any app) and `Buddy_App_QQ`
2225
+ * (the QQ-specific one), because this is a QQ-hosted app. Measured 0/1 → 1/1 on
2226
+ * both from a single chain.
2227
+ */
2228
+ const BUDDY_APP_ID = "cb_y5Dy46tPQGGWtueMxXbe";
2229
+ const BUDDY_APP_NAME = "企鹅教师助手";
2230
+ /** A stable-ish id for a synthetic conversation, unique per call. */
2231
+ function syntheticId(prefix) {
2232
+ return `wb2auto-${prefix}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
2233
+ }
2234
+ /**
2235
+ * A chat chain seeded with a placeholder conversation/request id.
2236
+ *
2237
+ * Several tasks do not verify that the conversation exists — they only check
2238
+ * that the events in the chain carry *a* well-formed id — so a synthetic pair is
2239
+ * enough. `RichMeow_Chat`, `create_canvas` and `automation_1` were all measured
2240
+ * lighting up from exactly this.
2241
+ */
2242
+ function syntheticChatChain(prefix) {
2243
+ const conversationId = syntheticId(prefix);
2244
+ const requestId = syntheticId(`${prefix}-req`);
2245
+ return {
2246
+ conversationId,
2247
+ requestId,
2248
+ events: desktopChatEvents(conversationId, requestId, `msg-${prefix}`)
2249
+ };
2250
+ }
2251
+ /**
2252
+ * The buddy-app chain (two tasks).
2253
+ *
2254
+ * Five clicks in the order a user would make them: discover the app, see it,
2255
+ * enter it, confirm the account link, skip the second binding step.
2256
+ */
2257
+ function buddyAppChain() {
2258
+ const base = {
2259
+ mode: "LOCAL",
2260
+ buddyId: BUDDY_APP_ID,
2261
+ buddyName: BUDDY_APP_NAME
2262
+ };
2263
+ return {
2264
+ transport: "desktop",
2265
+ events: [
2266
+ {
2267
+ ...base,
2268
+ eventCode: "buddyapp_discover_click"
2269
+ },
2270
+ {
2271
+ ...base,
2272
+ eventCode: "buddyapp_show",
2273
+ elementId: BUDDY_APP_ID,
2274
+ elementName: BUDDY_APP_NAME,
2275
+ position: 2
2276
+ },
2277
+ {
2278
+ ...base,
2279
+ eventCode: "buddyapp_enter_click",
2280
+ elementId: BUDDY_APP_ID,
2281
+ elementName: BUDDY_APP_NAME,
2282
+ position: 2,
2283
+ isFirstPage: "1"
2284
+ },
2285
+ {
2286
+ ...base,
2287
+ eventCode: "buddyapp_auth_confirm_click",
2288
+ elementId: BUDDY_APP_ID,
2289
+ elementName: BUDDY_APP_NAME
2290
+ },
2291
+ {
2292
+ ...base,
2293
+ eventCode: "buddyapp_bindaccount_skip_click",
2294
+ elementId: BUDDY_APP_ID,
2295
+ elementName: BUDDY_APP_NAME
2296
+ }
2297
+ ]
2298
+ };
2299
+ }
2300
+ /**
2301
+ * The design-canvas chain (`create_canvas`, +300 — the joint largest reward).
2302
+ *
2303
+ * The two canvas events ride the same metrics channel as everything else, so no
2304
+ * real canvas is ever created; the chat chain in front supplies the ids they
2305
+ * reference. Measured 1/1 on three accounts.
2306
+ */
2307
+ function canvasChain() {
2308
+ const { conversationId, requestId, events } = syntheticChatChain("canvas");
2309
+ return {
2310
+ transport: "desktop",
2311
+ events: [
2312
+ ...events,
2313
+ {
2314
+ eventCode: "wbx_design_canvas_task_create",
2315
+ conversationId,
2316
+ requestId,
2317
+ source: "summon_keyword",
2318
+ cost: 12e3,
2319
+ isSuccessful: true
2320
+ },
2321
+ {
2322
+ eventCode: "wbx_design_canvas_open",
2323
+ conversationId,
2324
+ requestId,
2325
+ id: `ardot-file-${requestId.slice(-8)}`,
2326
+ source: "summon_keyword",
2327
+ type: "page",
2328
+ cost: 13e3,
2329
+ isSuccessful: true
2330
+ }
2331
+ ]
2332
+ };
2333
+ }
2334
+ /**
2335
+ * The scheduled-task event (`automation_1`).
2336
+ *
2337
+ * One event is the whole chain — measured 1/1 on two accounts. The name is only
2338
+ * for the server's own records, so a generated one is fine.
2339
+ */
2340
+ function automationChain() {
2341
+ return {
2342
+ transport: "desktop",
2343
+ events: [{
2344
+ eventCode: "automated_task_create_suc",
2345
+ name: `定时任务-${Date.now().toString(36)}`,
2346
+ source: "manually",
2347
+ modelId: "fast-model",
2348
+ modelIsThinking: true,
2349
+ connectorCount: 0,
2350
+ skills: "",
2351
+ skillCount: 0,
2352
+ scheduleType: "once",
2353
+ mode: "LOCAL"
2354
+ }]
2355
+ };
2356
+ }
2357
+ /**
2358
+ * The plain chat chain (`RichMeow_Chat`, and the base of the template chain).
2359
+ *
2360
+ * Measured: this chain alone lights `RichMeow_Chat`.
2361
+ */
2362
+ function chatChain() {
2363
+ const { events } = syntheticChatChain("chat");
2364
+ return {
2365
+ transport: "desktop",
2366
+ events
2367
+ };
2368
+ }
2369
+ /**
2370
+ * The "same as this case" chain (`playbook_prompt`).
2371
+ *
2372
+ * The scorer watches `playbook_prompt_send` — sending the prompt that the
2373
+ * inspiration case pre-fills — not the card impression or the button click, so
2374
+ * the whole click path is replayed for realism but the send is what counts.
2375
+ */
2376
+ function playbookChain(caseId = PLAYBOOK_CASE_ID, caseName = PLAYBOOK_CASE_NAME) {
2377
+ const { conversationId, requestId, events } = syntheticChatChain("pb");
2378
+ const payload = {
2379
+ id: caseId,
2380
+ name: caseName,
2381
+ type: "document",
2382
+ categoryId: "",
2383
+ categoryName: ""
2384
+ };
2385
+ return {
2386
+ transport: "desktop",
2387
+ events: [
2388
+ ...events,
2389
+ {
2390
+ eventCode: "web_element_click",
2391
+ pageName: "playbook_detail",
2392
+ elementId: "playbook_ctaClick",
2393
+ elementName: caseName,
2394
+ source: "discover"
2395
+ },
2396
+ {
2397
+ eventCode: "playbook_cta_click",
2398
+ source: "discover",
2399
+ position: 0,
2400
+ ...payload
2401
+ },
2402
+ {
2403
+ eventCode: "playbook_prompt_send",
2404
+ conversationId,
2405
+ requestId,
2406
+ ...payload
2407
+ }
2408
+ ]
2409
+ };
2410
+ }
2411
+ /** The inspiration case the reference panel sends a prompt for. */
2412
+ const PLAYBOOK_CASE_ID = "pm-gtm-launch-plan";
2413
+ const PLAYBOOK_CASE_NAME = "新产品上市 GTM 发布计划一页纸";
2414
+ /**
2415
+ * The five templates the reference panel cycles through, as `[id, name]`.
2416
+ *
2417
+ * The upstream does not check that these templates exist — only that five
2418
+ * distinct `template_used` events arrive — so they are the reference set.
2419
+ */
2420
+ const TEMPLATE_PRESETS = [
2421
+ ["1", "深度研究"],
2422
+ ["2", "周报生成"],
2423
+ ["3", "竞品分析"],
2424
+ ["4", "活动策划"],
2425
+ ["5", "代码评审"]
2426
+ ];
2427
+ /**
2428
+ * One "created a task from a template" chain (`template_5`, +100 for five).
2429
+ *
2430
+ * Each group is a chat chain (which supplies the ids the template events join
2431
+ * on) plus `agent_task_created_with_template` and `template_used`. Measured:
2432
+ * five groups in one report scored 5/5.
2433
+ */
2434
+ function templateChain(templateId, templateName) {
2435
+ const { conversationId, requestId, events } = syntheticChatChain(`tpl${templateId}`);
2436
+ return {
2437
+ transport: "desktop",
2438
+ events: [
2439
+ ...events,
2440
+ {
2441
+ eventCode: "agent_task_created_with_template",
2442
+ mode: "working",
2443
+ isCustomModel: false,
2444
+ id: templateId,
2445
+ name: templateName,
2446
+ requestId
2447
+ },
2448
+ {
2449
+ eventCode: "template_used",
2450
+ template_id: templateId,
2451
+ task_mode: "working"
2452
+ }
2453
+ ]
2454
+ };
2455
+ }
2456
+ /** Every template group, ready to send in order. */
2457
+ function templateChains() {
2458
+ return TEMPLATE_PRESETS.map(([id, name]) => templateChain(id, name));
2459
+ }
2460
+ /**
2461
+ * The library-introduction click (`Library_read`).
2462
+ *
2463
+ * Scored on the WEB fingerprint — the same event posted with the desktop
2464
+ * fingerprint scores nothing — so this chain returns a web transport and the
2465
+ * scheduler routes it through `reportWebEvent`.
2466
+ */
2467
+ function libraryReadChain() {
2468
+ return {
2469
+ transport: "web",
2470
+ web: {
2471
+ eventCode: "web_element_click",
2472
+ pageUrl: LIBRARY_DOC_URL,
2473
+ elementId: "library_doc_intro_click",
2474
+ elementName: "WorkBuddy资料库介绍"
2475
+ }
2476
+ };
2477
+ }
2478
+ /** The document the library click is reported against. */
2479
+ const LIBRARY_DOC_URL = "https://www.workbuddy.cn/space/d/o0KWYeynteVv06UnAZqIFm";
2480
+ /** The theme key `Hp_Appearance` is scored on (和平精英激战金秋). */
2481
+ const APPEARANCE_THEME_KEY = "theme-tkmw7j";
2482
+ /** The skill `skill_1` is scored on. */
2483
+ const SKILL_ID = "skill_2097350077599879168";
2484
+ const SKILL_NAME = "润泽小馆·日报撰写";
2485
+ const SKILL_VERSION = "1.0.0";
2486
+ /** The 腾讯轻量云 expert `Expert_lighthouse` is scored on. */
2487
+ const LIGHTHOUSE_EXPERT_ID = "ex_2cvvUZQhDyeJ";
2488
+ /**
2489
+ * Rewrite the chain so the chatting half claims a tool call happened.
2490
+ *
2491
+ * A skills task is only credited when the response reports
2492
+ * `finishReason: 'tool_calls'` — that is, the model loaded the skill as a tool —
2493
+ * rather than a plain text answer.
2494
+ */
2495
+ function markToolCall(events) {
2496
+ return events.map((event) => event["eventCode"] === "chat_message_response" ? {
2497
+ ...event,
2498
+ finishReason: "tool_calls"
2499
+ } : event);
2500
+ }
2501
+ /**
2502
+ * Build the `skill_1` chain from a REAL conversation.
2503
+ *
2504
+ * Unlike the template and canvas chains, this one is verified against the
2505
+ * conversation it names, so the caller must first open a real chat and hand the
2506
+ * server-side ids in.
2507
+ */
2508
+ function skillChain(conversationId, requestId) {
2509
+ const messageId = `msg-${requestId.slice(-8)}`;
2510
+ return {
2511
+ transport: "desktop",
2512
+ events: [...markToolCall(desktopChatEvents(conversationId, requestId, messageId)), {
2513
+ eventCode: "skill_info",
2514
+ id: SKILL_NAME,
2515
+ skillId: SKILL_ID,
2516
+ skillVersion: SKILL_VERSION,
2517
+ toolStatus: "success",
2518
+ fileCount: 56,
2519
+ source: "workbuddy-desktop",
2520
+ conversationId,
2521
+ requestId,
2522
+ messageId,
2523
+ requestModelId: "fast-model",
2524
+ requestModelName: "fast-model",
2525
+ traceId: requestId
2526
+ }]
2527
+ };
2528
+ }
2529
+ /** The theme-apply event `Hp_Appearance` is scored on. */
2530
+ function appearanceChain(themeKey = APPEARANCE_THEME_KEY) {
2531
+ return {
2532
+ transport: "desktop",
2533
+ events: [{
2534
+ eventCode: "appearance_skin_apply",
2535
+ action: "apply",
2536
+ source: "settings_close",
2537
+ id: themeKey,
2538
+ vipLevel: 0,
2539
+ series: "",
2540
+ type: "unknown"
2541
+ }]
2542
+ };
2543
+ }
2544
+ /**
2545
+ * The three "summon an expert" events (`expert_summon_click` and friends).
2546
+ *
2547
+ * Paid before the conversation, in the order the app emits them.
2548
+ */
2549
+ function expertSummonEvents(expert) {
2550
+ const category = expert.categories[0] ?? "expert-all";
2551
+ const version = expert.version === "" ? "1.0.0" : expert.version;
2552
+ return [
2553
+ {
2554
+ eventCode: "web_element_click",
2555
+ source: expert.expertId,
2556
+ type: category,
2557
+ version,
2558
+ elementId: "expert_summon_click",
2559
+ elementName: "立即召唤",
2560
+ pageURL: "/C:/Program%20Files/WorkBuddy/resources/app.asar/renderer/index.html"
2561
+ },
2562
+ {
2563
+ eventCode: "expert_summon_click",
2564
+ id: expert.expertId,
2565
+ name: expert.displayName,
2566
+ expertTitle: expert.profession,
2567
+ type: "expert-all",
2568
+ position: 0,
2569
+ expertType: expert.expertType,
2570
+ version,
2571
+ mode: "LOCAL"
2572
+ },
2573
+ {
2574
+ eventCode: "expert_summoned",
2575
+ id: expert.expertId,
2576
+ name: expert.displayName,
2577
+ expertTitle: expert.profession,
2578
+ type: "expert-all"
2579
+ }
2580
+ ];
2581
+ }
2582
+ /**
2583
+ * The "an expert really answered" event, which is what the expert tasks count.
2584
+ *
2585
+ * The `requestId` must be the SERVER's id for a real chat: a made-up one scores
2586
+ * nothing, because the scorer looks the conversation up.
2587
+ */
2588
+ function expertActualUseEvent(expert, conversationId, requestId, mode = "craft") {
2589
+ const category = expert.categories[0] ?? "expert-all";
2590
+ const version = expert.version === "" ? "1.0.0" : expert.version;
2591
+ return {
2592
+ eventCode: "expert_actual_use",
2593
+ id: expert.expertId,
2594
+ name: expert.displayName,
2595
+ expertTitle: expert.profession,
2596
+ type: category,
2597
+ expertType: expert.expertType,
2598
+ source: "builtin",
2599
+ version,
2600
+ cost: 9e3,
2601
+ characterCount: 14,
2602
+ mode,
2603
+ conversationId,
2604
+ requestId,
2605
+ messageId: `msg-${requestId.slice(-8)}`,
2606
+ requestModelId: "fast-model",
2607
+ requestModelName: "fast-model"
2608
+ };
2609
+ }
2610
+ /**
2611
+ * The chat chain for an expert conversation.
2612
+ *
2613
+ * `agent_task_created` carries the expert fields the scorer reads to attribute
2614
+ * the conversation to that expert.
2615
+ */
2616
+ function expertChatEvents(expert, conversationId, requestId) {
2617
+ return desktopChatEvents(conversationId, requestId, `msg-${requestId.slice(-8)}`).map((event) => event["eventCode"] === "agent_task_created" ? {
2618
+ ...event,
2619
+ has_expert: true,
2620
+ expert_id: expert.expertId,
2621
+ expert_name: expert.displayName,
2622
+ expert_industry_id: ""
2623
+ } : event);
2624
+ }
2625
+ /**
2626
+ * The fallback record for the 腾讯轻量云 expert, used when the marketplace
2627
+ * listing cannot be read. The id is the one the task is scored against.
2628
+ */
2629
+ const LIGHTHOUSE_EXPERT = {
2630
+ expertId: LIGHTHOUSE_EXPERT_ID,
2631
+ expertType: "agent",
2632
+ displayName: "腾讯轻量云专家",
2633
+ profession: "腾讯轻量云专家",
2634
+ version: "1.0.2",
2635
+ categories: []
2636
+ };
2637
+ /**
2638
+ * Tasks with a chain in this module.
2639
+ *
2640
+ * Membership is the filter the pass uses before it tries to build one, so a
2641
+ * task with no chain costs no upstream call. It has to be kept in step with
2642
+ * `chainsFor`'s switch: a code listed here with no case would be read and then
2643
+ * silently skipped.
2644
+ */
2645
+ const EVENT_CHAIN_BUILDERS = {
2646
+ Buddy_App: true,
2647
+ Buddy_App_QQ: true,
2648
+ create_canvas: true,
2649
+ automation_1: true,
2650
+ RichMeow_Chat: true,
2651
+ playbook_prompt: true,
2652
+ template_5: true,
2653
+ Hp_Appearance: true,
2654
+ Library_read: true,
2655
+ skill_1: true,
2656
+ expert_5: true,
2657
+ Expert_team_use_3: true,
2658
+ Expert_lighthouse: true
2659
+ };
2660
+ const AUTOMATION_TICK_MS = 6e4;
2661
+ const JOB_KINDS = [
2662
+ "checkin",
2663
+ "report",
2664
+ "tasks",
2665
+ "streak",
2666
+ "travel"
2667
+ ];
2668
+ const EMPTY_JOB_STATE = {
2669
+ ok: 0,
2670
+ failed: 0,
2671
+ credit: 0,
2672
+ energy: 0,
2673
+ claimed: 0
2674
+ };
2675
+ /** `YYYY-MM-DD` in local time, the day key every job resets on. */
2676
+ /**
2677
+ * What the ledger gained between two snapshots, per account and in total.
2678
+ *
2679
+ * A diff rather than an absolute read: the card asks "what did this run do",
2680
+ * and the ledger holds the whole day, so reporting totals would re-count
2681
+ * everything an earlier run had already claimed.
2682
+ */
2683
+ function diffEarnings(before, after) {
2684
+ const accounts = {};
2685
+ let credit = 0;
2686
+ let energy = 0;
2687
+ let claimed = 0;
2688
+ for (const [id, entry] of Object.entries(after)) {
2689
+ const was = before[id];
2690
+ const gain = {
2691
+ credit: entry.credit - (was?.credit ?? 0),
2692
+ energy: entry.energy - (was?.energy ?? 0),
2693
+ claimed: entry.claimed - (was?.claimed ?? 0),
2694
+ checkinCredit: entry.checkinCredit - (was?.checkinCredit ?? 0),
2695
+ bonusCredit: entry.bonusCredit - (was?.bonusCredit ?? 0),
2696
+ travelCredit: entry.travelCredit - (was?.travelCredit ?? 0)
2697
+ };
2698
+ if (gain.credit === 0 && gain.energy === 0 && gain.claimed === 0 && gain.checkinCredit === 0 && gain.bonusCredit === 0 && gain.travelCredit === 0) continue;
2699
+ accounts[id] = gain;
2700
+ credit += gain.credit;
2701
+ energy += gain.energy;
2702
+ claimed += gain.claimed;
2703
+ }
2704
+ return {
2705
+ credit,
2706
+ energy,
2707
+ claimed,
2708
+ accounts
2709
+ };
2710
+ }
2711
+ function dayKey(date) {
2712
+ const month = String(date.getMonth() + 1).padStart(2, "0");
2713
+ const day = String(date.getDate()).padStart(2, "0");
2714
+ return `${date.getFullYear()}-${month}-${day}`;
2715
+ }
2716
+ /**
2717
+ * Whether `now`'s local hour is one of `hours`.
2718
+ *
2719
+ * The reference panel computes a `nextFire` instant and sleeps until it; this
2720
+ * loop instead wakes every minute and asks "is any job due now". Both fire at
2721
+ * the top of a configured hour, but the polling form cannot miss a slot to a
2722
+ * suspended process — a laptop that slept through 10:00 still runs the job the
2723
+ * moment it wakes, on the same day.
2724
+ */
2725
+ function isFireHour(now, hours) {
2726
+ return hours.includes(now.getHours());
2727
+ }
2728
+ /** Whether this account may be used: not switched off, not cooling. */
2729
+ function eligible(account, now) {
2730
+ if (account.cooldownUntilMs > now) return false;
2731
+ if (regionOf(account.credential.domain) === "global") return false;
2732
+ return true;
2733
+ }
2734
+ const sleep = (ms) => new Promise((resolve) => {
2735
+ setTimeout(resolve, ms);
2736
+ });
2737
+ /**
2738
+ * The points automation.
2739
+ *
2740
+ * Owns a single timer loop. Construction is inert — nothing runs until
2741
+ * {@link start}, and {@link stop} is idempotent so a plugin teardown that fires
2742
+ * twice is harmless.
2743
+ */
2744
+ var WorkBuddyScheduler = class {
2745
+ pool;
2746
+ client;
2747
+ logger;
2748
+ now;
2749
+ delayMs;
2750
+ /**
2751
+ * How long to wait for event scoring before re-reading the task list.
2752
+ * Tests set 0 so a pass does not spend nine real seconds per account.
2753
+ */
2754
+ eventScoreWaitMs;
2755
+ /**
2756
+ * Gap between two expert summon chains.
2757
+ * Tests set 0 so a pass does not spend six real seconds per expert.
2758
+ */
2759
+ expertGapMs;
2760
+ enabled;
2761
+ checkinHours;
2762
+ taskHours;
2763
+ reportHours;
2764
+ streakHours;
2765
+ travelHours;
2766
+ timer;
2767
+ running = false;
2768
+ /** Guards against a slow run overlapping the next tick. */
2769
+ busy = false;
2770
+ /** True while a manual run is in flight, so the card can poll it. */
2771
+ runInFlight = false;
2772
+ /**
2773
+ * Set once {@link stop} is called.
2774
+ *
2775
+ * Deliberately false before `start`: the loop is not running yet, but a
2776
+ * manual `tick` must still work. `stop` is what makes a run abandon the
2777
+ * accounts it has not reached yet.
2778
+ */
2779
+ stopped = false;
2780
+ states = {
2781
+ checkin: { ...EMPTY_JOB_STATE },
2782
+ report: { ...EMPTY_JOB_STATE },
2783
+ tasks: { ...EMPTY_JOB_STATE },
2784
+ streak: { ...EMPTY_JOB_STATE },
2785
+ travel: { ...EMPTY_JOB_STATE }
2786
+ };
2787
+ claimableSeen = 0;
2788
+ /**
2789
+ * Credits/energy/tasks earned per account TODAY, keyed by account id.
2790
+ *
2791
+ * Cleared whenever the day key rolls over, so the card always answers
2792
+ * "what did the automation get for THIS account today".
2793
+ */
2794
+ earnings = /* @__PURE__ */ new Map();
2795
+ /** Day key the counters above belong to. */
2796
+ earningsDate = "";
2797
+ /** Host hooks that persist the ledger across restarts. */
2798
+ loadEarnings;
2799
+ saveEarningsFn;
2800
+ constructor(pool, client, options = {}) {
2801
+ this.pool = pool;
2802
+ this.client = client;
2803
+ this.logger = options.logger ?? {};
2804
+ this.now = options.now ?? (() => /* @__PURE__ */ new Date());
2805
+ this.delayMs = options.accountDelayMs ?? 800;
2806
+ this.eventScoreWaitMs = options.eventScoreWaitMs ?? 9e3;
2807
+ this.expertGapMs = options.expertGapMs ?? 6e3;
2808
+ this.loadEarnings = options.loadEarnings;
2809
+ this.saveEarningsFn = options.saveEarnings;
2810
+ const restored = options.loadEarnings?.();
2811
+ const today = dayKey(this.now());
2812
+ if (restored !== void 0 && restored.date === today) {
2813
+ for (const [id, entry] of Object.entries(restored.accounts)) this.earnings.set(id, entry);
2814
+ this.earningsDate = today;
2815
+ }
2816
+ this.enabled = options.enabled ?? false;
2817
+ this.checkinHours = options.checkinHours ?? [9];
2818
+ this.reportHours = options.reportHours ?? [10];
2819
+ this.taskHours = options.taskHours ?? [11];
2820
+ this.streakHours = options.streakHours ?? [12];
2821
+ this.travelHours = options.travelHours ?? [12];
2822
+ }
2823
+ /** Apply a new configuration; safe to call while running. */
2824
+ /**
2825
+ * Install the persistence hook once the host settings service is available.
2826
+ *
2827
+ * Separate from the constructor because the scheduler is built with the pool,
2828
+ * long before the settings section exists; a ledger written before that point
2829
+ * would have nowhere to go.
2830
+ */
2831
+ setEarningsPersistence(save) {
2832
+ this.saveEarningsFn = save;
2833
+ }
2834
+ /**
2835
+ * Fold a previously persisted ledger back in, when it belongs to today.
2836
+ *
2837
+ * Used after the settings document becomes readable, which happens after
2838
+ * construction; a ledger from an earlier day is ignored so the counters never
2839
+ * claim yesterday as today.
2840
+ */
2841
+ applyEarningsLedger(ledger) {
2842
+ const today = dayKey(this.now());
2843
+ if (ledger.date !== today) return;
2844
+ for (const [id, entry] of Object.entries(ledger.accounts)) this.earnings.set(id, entry);
2845
+ this.earningsDate = today;
2846
+ }
2847
+ applyConfig(options) {
2848
+ if (options.enabled !== void 0) this.enabled = options.enabled;
2849
+ if (options.checkinHours !== void 0) this.checkinHours = options.checkinHours;
2850
+ if (options.reportHours !== void 0) this.reportHours = options.reportHours;
2851
+ if (options.taskHours !== void 0) this.taskHours = options.taskHours;
2852
+ if (options.streakHours !== void 0) this.streakHours = options.streakHours;
2853
+ if (options.travelHours !== void 0) this.travelHours = options.travelHours;
2854
+ }
2855
+ /** Hours for one job, used by the loop and the status document. */
2856
+ hoursOf(kind) {
2857
+ switch (kind) {
2858
+ case "checkin": return this.checkinHours;
2859
+ case "report": return this.reportHours;
2860
+ case "tasks": return this.taskHours;
2861
+ case "streak": return this.streakHours;
2862
+ case "travel": return this.travelHours;
2863
+ }
2864
+ }
2865
+ /** Start the loop. Idempotent. */
2866
+ start() {
2867
+ if (this.timer !== void 0) return;
2868
+ this.stopped = false;
2869
+ this.running = true;
2870
+ this.timer = setInterval(() => {
2871
+ this.tick();
2872
+ }, AUTOMATION_TICK_MS);
2873
+ this.timer.unref?.();
2874
+ }
2875
+ /** Stop the loop. Idempotent, and safe before `start`. */
2876
+ stop() {
2877
+ this.stopped = true;
2878
+ this.running = false;
2879
+ if (this.timer !== void 0) {
2880
+ clearInterval(this.timer);
2881
+ this.timer = void 0;
2882
+ }
2883
+ }
2884
+ /** Snapshot for the status document. */
2885
+ /**
2886
+ * Run one job immediately, regardless of the clock.
2887
+ *
2888
+ * Exists so the automation can be verified from the card without waiting for
2889
+ * its hour. A manual run is recorded exactly like a scheduled one, so the
2890
+ * timer will not repeat it later the same day: every job is idempotent, but a
2891
+ * second pass would still be wasted upstream calls.
2892
+ *
2893
+ * `force` ignores the already-ran-today guard, which is what pressing the
2894
+ * button a second time means.
2895
+ */
2896
+ async runNow(kind, force = false) {
2897
+ const today = dayKey(this.now());
2898
+ const state = this.states[kind];
2899
+ if (!force && state.lastRunDate === today) return { ...state };
2900
+ this.busy = true;
2901
+ try {
2902
+ await this.runJob(kind, today);
2903
+ } finally {
2904
+ this.busy = false;
2905
+ }
2906
+ return { ...this.states[kind] };
2907
+ }
2908
+ /**
2909
+ * Run every job once, in the scheduled order.
2910
+ *
2911
+ * Order matters and is not configurable: the activity report has to land
2912
+ * before the task pass reads task progress, or the pass sees counters the
2913
+ * report would have moved. This is what the card's single button calls.
2914
+ */
2915
+ /**
2916
+ * Start a full pass in the background and return immediately.
2917
+ *
2918
+ * A pass takes tens of seconds - one upstream round trip per account per job,
2919
+ * plus the scoring wait - which is far too long to hold the card request open:
2920
+ * the browser or the host web server would time out, and the user would see
2921
+ * a hung button for a run that is actually working.
2922
+ *
2923
+ * Returns whether a run started. A second call while one is in flight is
2924
+ * ignored rather than queued: pressing the button twice means hurry up, and
2925
+ * the run already under way covers it.
2926
+ */
2927
+ startRunAll() {
2928
+ if (this.runInFlight) return false;
2929
+ this.runInFlight = true;
2930
+ this.runAll().catch((error) => {
2931
+ this.logger.warn?.("dsh-workbuddy-xdpool automation run failed:", error);
2932
+ }).finally(() => {
2933
+ this.runInFlight = false;
2934
+ });
2935
+ return true;
2936
+ }
2937
+ async runAll() {
2938
+ const today = dayKey(this.now());
2939
+ const before = this.earningsSnapshot();
2940
+ let okCount = 0;
2941
+ let failed = 0;
2942
+ let jobsRun = 0;
2943
+ for (const kind of JOB_KINDS) {
2944
+ jobsRun += 1;
2945
+ this.busy = true;
2946
+ try {
2947
+ await this.runJob(kind, today);
2948
+ } finally {
2949
+ this.busy = false;
2950
+ }
2951
+ okCount += this.states[kind].ok;
2952
+ failed += this.states[kind].failed;
2953
+ }
2954
+ const gained = diffEarnings(before, this.earningsSnapshot());
2955
+ return {
2956
+ jobsRun,
2957
+ okCount,
2958
+ failed,
2959
+ credit: gained.credit,
2960
+ energy: gained.energy,
2961
+ claimed: gained.claimed,
2962
+ accounts: gained.accounts
2963
+ };
2964
+ }
2965
+ status() {
2966
+ const jobs = {};
2967
+ for (const kind of JOB_KINDS) jobs[kind] = { ...this.states[kind] };
2968
+ return {
2969
+ enabled: this.enabled,
2970
+ running: this.running,
2971
+ checkinHours: this.checkinHours,
2972
+ taskHours: this.taskHours,
2973
+ reportHours: this.reportHours,
2974
+ streakHours: this.streakHours,
2975
+ travelHours: this.travelHours,
2976
+ jobs,
2977
+ claimableSeen: this.claimableSeen,
2978
+ earningsToday: this.earningsSnapshot(),
2979
+ runInProgress: this.runInFlight
2980
+ };
2981
+ }
2982
+ /**
2983
+ * One poll: run every due job, serially.
2984
+ *
2985
+ * Serial by design — the jobs share the same accounts and the upstream
2986
+ * rate-limits per account, so overlapping passes would only trip that limit.
2987
+ * A job that throws is recorded and the loop continues.
2988
+ */
2989
+ async tick() {
2990
+ if (!this.enabled || this.stopped || this.busy) return;
2991
+ this.busy = true;
2992
+ try {
2993
+ const now = this.now();
2994
+ const today = dayKey(now);
2995
+ for (const kind of JOB_KINDS) {
2996
+ if (this.stopped) return;
2997
+ if (this.states[kind].lastRunDate === today) continue;
2998
+ if (!isFireHour(now, this.hoursOf(kind))) continue;
2999
+ await this.runJob(kind, today);
3000
+ }
3001
+ } catch (error) {
3002
+ this.logger.warn?.("dsh-workbuddy-xdpool automation tick failed:", error);
3003
+ } finally {
3004
+ this.busy = false;
3005
+ }
3006
+ }
3007
+ /** Run one job against every eligible account and record the outcome. */
3008
+ /**
3009
+ * Add one account's take to today's counters, resetting first if the day
3010
+ * rolled over. Called from the task pass, which is the only job that earns.
3011
+ */
3012
+ /**
3013
+ * Today's per-account earnings, as a plain object for the status document.
3014
+ *
3015
+ * Rolls the day first so a status read just after midnight does not report
3016
+ * yesterday's totals under today's date.
3017
+ */
3018
+ earningsSnapshot() {
3019
+ this.rollEarnings(dayKey(this.now()));
3020
+ const out = {};
3021
+ for (const [id, entry] of this.earnings) out[id] = { ...entry };
3022
+ return out;
3023
+ }
3024
+ /**
3025
+ * Add one account take to today counters, resetting first if the day rolled
3026
+ * over. Every source is tracked separately so the card can show what earned
3027
+ * what, rather than one opaque total.
3028
+ */
3029
+ recordEarnings(accountId, today, delta) {
3030
+ this.rollEarnings(today);
3031
+ const credit = delta.credit ?? 0;
3032
+ const energy = delta.energy ?? 0;
3033
+ const claimed = delta.claimed ?? 0;
3034
+ const checkinCredit = delta.checkinCredit ?? 0;
3035
+ const bonusCredit = delta.bonusCredit ?? 0;
3036
+ const travelCredit = delta.travelCredit ?? 0;
3037
+ if (credit === 0 && energy === 0 && claimed === 0 && checkinCredit === 0 && bonusCredit === 0 && travelCredit === 0) return;
3038
+ const existing = this.earnings.get(accountId);
3039
+ this.earnings.set(accountId, {
3040
+ credit: (existing?.credit ?? 0) + credit,
3041
+ energy: (existing?.energy ?? 0) + energy,
3042
+ claimed: (existing?.claimed ?? 0) + claimed,
3043
+ checkinCredit: (existing?.checkinCredit ?? 0) + checkinCredit,
3044
+ bonusCredit: (existing?.bonusCredit ?? 0) + bonusCredit,
3045
+ travelCredit: (existing?.travelCredit ?? 0) + travelCredit,
3046
+ date: today
3047
+ });
3048
+ this.persistEarnings();
3049
+ }
3050
+ /** Clear the per-account counters when the local day changes. */
3051
+ rollEarnings(today) {
3052
+ if (this.earningsDate === today) return;
3053
+ this.earningsDate = today;
3054
+ this.earnings.clear();
3055
+ this.claimableSeen = 0;
3056
+ this.persistEarnings();
3057
+ }
3058
+ /**
3059
+ * Write the ledger through the host hook, when one was supplied.
3060
+ *
3061
+ * Best effort on purpose: a failed save must never abort a run that has
3062
+ * already collected rewards, and the in-memory ledger keeps serving the card
3063
+ * for the rest of the session either way.
3064
+ */
3065
+ persistEarnings() {
3066
+ if (this.saveEarningsFn === void 0) return;
3067
+ try {
3068
+ const accounts = {};
3069
+ for (const [id, entry] of this.earnings) accounts[id] = entry;
3070
+ this.saveEarningsFn({
3071
+ date: this.earningsDate,
3072
+ accounts
3073
+ });
3074
+ } catch (error) {
3075
+ this.logger.warn?.("dsh-workbuddy-xdpool: could not persist automation earnings:", error);
3076
+ }
3077
+ }
3078
+ /**
3079
+ * Run one job against every eligible account and record the outcome.
3080
+ *
3081
+ * The task job runs in TWO passes. The first sends the event chains that light
3082
+ * up client-scored tasks; the second collects rewards. They are separate
3083
+ * because scoring lands asynchronously — a chain sent and claimed within the
3084
+ * same breath finds the task still un-scored — and because sending is fast
3085
+ * while claiming wants the whole pool to have been lit up first. Splitting
3086
+ * them costs one shared wait instead of one wait per account.
3087
+ */
3088
+ async runJob(kind, today) {
3089
+ const accountWord = kind === "report" ? "report" : kind;
3090
+ let ok = 0;
3091
+ let failed = 0;
3092
+ let credit = 0;
3093
+ let energy = 0;
3094
+ let claimed = 0;
3095
+ const accounts = this.accountsInOrder();
3096
+ if (kind === "tasks") await this.sendEventChains(accounts);
3097
+ for (let index = 0; index < accounts.length; index++) {
3098
+ if (this.stopped) break;
3099
+ const account = accounts[index];
3100
+ if (account === void 0) continue;
3101
+ try {
3102
+ switch (kind) {
3103
+ case "checkin":
3104
+ try {
3105
+ const claim = await this.client.claimDailyCheckin(account.credential);
3106
+ this.recordEarnings(account.id, today, { checkinCredit: claim.credit });
3107
+ if (claim.credit > 0) this.logger.info?.(`automation checkin ${account.label}: +${claim.credit} credit`);
3108
+ } catch (error) {
3109
+ if (!isAlreadyCheckin(error)) throw error;
3110
+ this.logger.info?.(`automation checkin ${account.label}: already done today`);
3111
+ }
3112
+ break;
3113
+ case "report": {
3114
+ const days = await this.reportOne(account);
3115
+ this.logger.info?.(`automation report ${account.label}: streak days=${days}`);
3116
+ break;
3117
+ }
3118
+ case "tasks": {
3119
+ const result = await this.runTasks(account);
3120
+ credit += result.credit;
3121
+ energy += result.energy;
3122
+ claimed += result.claimed;
3123
+ this.claimableSeen += result.claimableCount;
3124
+ this.recordEarnings(account.id, today, {
3125
+ credit: result.credit,
3126
+ energy: result.energy,
3127
+ claimed: result.claimed
3128
+ });
3129
+ this.logger.info?.(`automation tasks ${account.label}: ${result.claimed} claimed (+${result.credit} credit, +${result.energy} energy, ${result.claimableCount} claimable seen)`);
3130
+ break;
3131
+ }
3132
+ case "streak":
3133
+ await this.redeemStreak(account);
3134
+ break;
3135
+ case "travel": await this.runTravel(account);
3136
+ }
3137
+ ok++;
3138
+ } catch (error) {
3139
+ failed++;
3140
+ this.logger.warn?.(`automation ${accountWord} ${account.label} failed:`, error);
3141
+ }
3142
+ if (index < accounts.length - 1 && this.delayMs > 0 && !this.stopped) await sleep(this.delayMs);
3143
+ }
3144
+ const state = this.states[kind];
3145
+ state.lastRunDate = today;
3146
+ state.lastRunAtMs = this.now().getTime();
3147
+ state.ok = ok;
3148
+ state.failed = failed;
3149
+ state.credit = credit;
3150
+ state.energy = energy;
3151
+ state.claimed = claimed;
3152
+ state.message = this.summarise(kind, ok, failed, claimed, credit, energy);
3153
+ this.logger.info?.(`automation ${accountWord}: ${state.message}`);
3154
+ }
3155
+ /** Compose the one-line summary shown on the card. */
3156
+ summarise(kind, ok, failed, claimed, credit, energy) {
3157
+ if (kind === "tasks") return `${ok} accounts, ${claimed} tasks claimed (+${credit} credit, +${energy} energy)${failed > 0 ? `, ${failed} failed` : ""}`;
3158
+ return `${ok} accounts ok${failed > 0 ? `, ${failed} failed` : ""}`;
3159
+ }
3160
+ /**
3161
+ * Accounts to run against, in pool order.
3162
+ *
3163
+ * Disabled accounts are excluded here rather than filtered by the caller so a
3164
+ * card switch takes effect on the next pass without any event plumbing.
3165
+ */
3166
+ accountsInOrder() {
3167
+ const now = Date.now();
3168
+ return this.pool.list().filter((account) => {
3169
+ if (this.pool.isDisabled(account.id)) return false;
3170
+ return eligible(account, now);
3171
+ });
3172
+ }
3173
+ /**
3174
+ * Send one activity report, then verify it landed.
3175
+ *
3176
+ * The upstream answers 200 even when it drops the event, so the streak is
3177
+ * read back as the oracle: `days > 0` means it counted. A failed read-back is
3178
+ * logged and treated as a suspicious result, never as a retry — the report is
3179
+ * idempotent per day, and hammering it is exactly what the one-a-day quota
3180
+ * exists to avoid.
3181
+ */
3182
+ async reportOne(account) {
3183
+ await this.client.reportActivity(account.credential);
3184
+ try {
3185
+ const days = await this.client.growthStreakDays(account.credential);
3186
+ if (days === 0) this.logger.warn?.(`automation report ${account.label}: streak days=0 right after report (new account or scoring lag?)`);
3187
+ return days;
3188
+ } catch (error) {
3189
+ this.logger.warn?.(`automation report ${account.label}: streak read-back failed:`, error);
3190
+ return -1;
3191
+ }
3192
+ }
3193
+ async sendEventChains(accounts) {
3194
+ let sentAnything = false;
3195
+ for (const account of accounts) {
3196
+ if (this.stopped) return;
3197
+ let tasks;
3198
+ try {
3199
+ tasks = await this.client.listTasks(account.credential);
3200
+ } catch (error) {
3201
+ this.logger.warn?.(`automation events ${account.label}: list failed:`, error);
3202
+ continue;
3203
+ }
3204
+ const pending = tasks.filter((task) => task.taskCode in EVENT_CHAIN_BUILDERS && !task.claimable && !task.locked);
3205
+ for (const task of pending) {
3206
+ if (this.stopped) return;
3207
+ let chains;
3208
+ try {
3209
+ chains = await this.chainsFor(task, account.credential);
3210
+ } catch (error) {
3211
+ this.logger.warn?.(`automation events ${account.label}: ${task.taskCode} could not be built:`, error);
3212
+ continue;
3213
+ }
3214
+ for (const chain of chains) {
3215
+ if (this.stopped) return;
3216
+ try {
3217
+ await this.sendChain(account.credential, chain);
3218
+ this.logger.info?.(`automation events ${account.label}: ${task.taskCode} chain sent`);
3219
+ sentAnything = true;
3220
+ } catch (error) {
3221
+ this.logger.warn?.(`automation events ${account.label}: ${task.taskCode} failed:`, error);
3222
+ break;
3223
+ }
3224
+ if (this.delayMs > 0 && !this.stopped) await sleep(this.delayMs);
3225
+ }
3226
+ }
3227
+ }
3228
+ if (sentAnything && this.eventScoreWaitMs > 0 && !this.stopped) await sleep(this.eventScoreWaitMs);
3229
+ }
3230
+ /**
3231
+ * Send one chain on the channel it was built for.
3232
+ *
3233
+ * The transport is not a detail of the sender: the scorer keys different
3234
+ * tasks to different fingerprint families, so a web-scored event posted as a
3235
+ * desktop event is accepted and then ignored.
3236
+ */
3237
+ async sendChain(credential, chain) {
3238
+ if (chain.transport === "web") {
3239
+ const web = chain.web;
3240
+ if (web === void 0) throw new Error("web chain without a web event");
3241
+ await this.client.reportWebEvent(credential, web.eventCode, web.pageUrl, web.elementId, web.elementName);
3242
+ return;
3243
+ }
3244
+ await this.client.reportDesktopEvents(credential, chain.events ?? []);
3245
+ }
3246
+ /**
3247
+ * Build every chain that scores one task.
3248
+ *
3249
+ * Most tasks need a single chain; `template_5` needs five, because the scorer
3250
+ * counts distinct `template_used` events rather than a boolean. The two tasks
3251
+ * that join a conversation (skill, expert) open a real one first, which is why
3252
+ * this is async.
3253
+ */
3254
+ async chainsFor(task, credential) {
3255
+ switch (task.taskCode) {
3256
+ case "Buddy_App":
3257
+ case "Buddy_App_QQ": return [buddyAppChain()];
3258
+ case "create_canvas": return [canvasChain()];
3259
+ case "automation_1": return [automationChain()];
3260
+ case "RichMeow_Chat": return [chatChain()];
3261
+ case "playbook_prompt": return [playbookChain()];
3262
+ case "template_5": return templateChains();
3263
+ case "Hp_Appearance":
3264
+ await this.client.setAppearanceTheme(credential, APPEARANCE_THEME_KEY);
3265
+ if (this.delayMs > 0 && !this.stopped) await sleep(2e3);
3266
+ return [appearanceChain()];
3267
+ case "Library_read": return [libraryReadChain()];
3268
+ case "skill_1": {
3269
+ const chat = await this.client.openConversation(credential);
3270
+ if (chat === void 0) throw new Error("skill_1: no server conversation");
3271
+ return [skillChain(chat.conversationId, chat.requestId)];
3272
+ }
3273
+ case "expert_5": return this.expertChains(credential, "agent", task);
3274
+ case "Expert_team_use_3": return this.expertChains(credential, "team", task);
3275
+ case "Expert_lighthouse": return this.lighthouseChains(credential);
3276
+ default: return [];
3277
+ }
3278
+ }
3279
+ /**
3280
+ * The summon-and-use chains for the expert tasks.
3281
+ *
3282
+ * Two steps per expert, and both are load-bearing: the summon events alone are
3283
+ * impressions, and a use event on its own scores nothing because the scorer
3284
+ * looks the conversation up. Only a real chat with `X-Expert-Id` produces an
3285
+ * id it will accept.
3286
+ */
3287
+ async expertChains(credential, expertType, task) {
3288
+ const needed = Math.max(0, task.target - task.current);
3289
+ if (needed === 0) return [];
3290
+ const experts = await this.client.marketExpertList(credential, expertType);
3291
+ const out = [];
3292
+ for (const expert of experts) {
3293
+ if (out.length >= needed) break;
3294
+ if (this.stopped) break;
3295
+ try {
3296
+ await this.client.reportDesktopEvents(credential, expertSummonEvents(expert));
3297
+ if (this.delayMs > 0 && !this.stopped) await sleep(this.delayMs);
3298
+ const chat = await this.client.openConversation(credential, expert.expertId);
3299
+ if (chat === void 0) continue;
3300
+ out.push({
3301
+ transport: "desktop",
3302
+ events: [...expertChatEvents(expert, chat.conversationId, chat.requestId), expertActualUseEvent(expert, chat.conversationId, chat.requestId)]
3303
+ });
3304
+ } catch (error) {
3305
+ this.logger.warn?.(`automation events expert ${expert.expertId}:`, error);
3306
+ }
3307
+ if (this.expertGapMs > 0 && !this.stopped) await sleep(this.expertGapMs);
3308
+ }
3309
+ return out;
3310
+ }
3311
+ /**
3312
+ * The 腾讯轻量云 expert chain.
3313
+ *
3314
+ * Structurally the same as the expert task, with two differences the scorer
3315
+ * checks: `agent_task_created` has to name the expert, and the use event has
3316
+ * to report `mode: 'LOCAL'` with an empty type and zero cost — that is what
3317
+ * the lighthouse criterion looks for.
3318
+ */
3319
+ async lighthouseChains(credential) {
3320
+ let expert = LIGHTHOUSE_EXPERT;
3321
+ try {
3322
+ const found = (await this.client.marketExpertList(credential, "agent")).find((item) => item.expertId === LIGHTHOUSE_EXPERT_ID);
3323
+ if (found !== void 0) expert = found;
3324
+ } catch (error) {
3325
+ this.logger.warn?.("automation events lighthouse list:", error);
3326
+ }
3327
+ await this.client.reportDesktopEvents(credential, expertSummonEvents(expert));
3328
+ if (this.delayMs > 0 && !this.stopped) await sleep(this.delayMs);
3329
+ const chat = await this.client.openConversation(credential, expert.expertId);
3330
+ if (chat === void 0) throw new Error("Expert_lighthouse: no server conversation");
3331
+ const use = expertActualUseEvent(expert, chat.conversationId, chat.requestId, "LOCAL");
3332
+ use["type"] = "";
3333
+ use["cost"] = 0;
3334
+ return [{
3335
+ transport: "desktop",
3336
+ events: [...expertChatEvents(expert, chat.conversationId, chat.requestId), use]
3337
+ }];
3338
+ }
3339
+ /**
3340
+ * The task-centre pass for one account.
3341
+ *
3342
+ * Order matters: enrich first (enrol in everything open), then claim. Both
3343
+ * halves are idempotent — accepting an already-accepted task succeeds, and a
3344
+ * repeat claim answers `already_claimed` — so a pass that dies halfway is
3345
+ * safe to replay on the next tick.
3346
+ */
3347
+ async runTasks(account) {
3348
+ const credential = account.credential;
3349
+ const open = (await this.client.listTasks(credential)).filter((task) => task.acceptStatus === "not_accepted" && !task.locked).map((task) => task.taskCode);
3350
+ if (open.length > 0) await this.client.acceptTasks(credential, open);
3351
+ const claimable = (await this.client.listTasks(credential)).filter((task) => task.claimable && !task.locked);
3352
+ let credit = 0;
3353
+ let energy = 0;
3354
+ let claimed = 0;
3355
+ for (const task of claimable) {
3356
+ if (this.stopped) break;
3357
+ const reward = await this.client.claimTaskReward(credential, task.taskCode);
3358
+ credit += reward.credit;
3359
+ energy += reward.energy;
3360
+ if (reward.credit > 0 || reward.energy > 0) {
3361
+ claimed++;
3362
+ this.logger.info?.(`automation claim ${account.label}: ${task.title} +${reward.credit}c +${reward.energy}e`);
3363
+ } else this.logger.info?.(`automation claim ${account.label}: ${task.taskCode} already claimed`);
3364
+ if (this.delayMs > 0 && !this.stopped) await sleep(this.delayMs);
3365
+ }
3366
+ return {
3367
+ claimed,
3368
+ credit,
3369
+ energy,
3370
+ claimableCount: claimable.length
3371
+ };
3372
+ }
3373
+ /**
3374
+ * Streak redemption plus the lottery it unlocks.
3375
+ *
3376
+ * Tiers unlock on consecutive active days (7/14/28). Redeeming one pays
3377
+ * credits, energy, a makeup card and — the part nothing else grants — lottery
3378
+ * draws, so the draw runs straight after and only for the chances in hand.
3379
+ *
3380
+ * Everything here is idempotent: a tier already claimed is skipped by its
3381
+ * status, and a draw consumes one chance, so a replay cannot double-spend.
3382
+ */
3383
+ async redeemStreak(account) {
3384
+ const credential = account.credential;
3385
+ const status = await this.client.growthStreakFull(credential);
3386
+ for (const tier of status.tiers) {
3387
+ if (this.stopped) return;
3388
+ if (tier.status === "locked" || tier.status === "claimed") continue;
3389
+ try {
3390
+ await this.client.redeemStreakTier(credential, tier.tier);
3391
+ this.logger.info?.(`automation streak ${account.label}: redeemed ${tier.tier} (+${tier.credit}c +${tier.energy}e +${tier.chances} draw(s))`);
3392
+ this.recordEarnings(account.id, dayKey(this.now()), { bonusCredit: tier.credit });
3393
+ } catch (error) {
3394
+ this.logger.warn?.(`automation streak ${account.label}: redeem ${tier.tier} failed:`, error);
3395
+ }
3396
+ if (this.delayMs > 0 && !this.stopped) await sleep(this.delayMs);
3397
+ }
3398
+ const chances = await this.client.lotteryChances(credential);
3399
+ for (let draw = 0; draw < chances; draw += 1) {
3400
+ if (this.stopped) return;
3401
+ try {
3402
+ const prize = await this.client.lotteryDraw(credential);
3403
+ this.logger.info?.(`automation lottery ${account.label}: draw ${draw + 1}/${chances} -> ${JSON.stringify(prize).slice(0, 120)}`);
3404
+ } catch (error) {
3405
+ this.logger.warn?.(`automation lottery ${account.label}: draw failed:`, error);
3406
+ break;
3407
+ }
3408
+ if (this.delayMs > 0 && !this.stopped) await sleep(this.delayMs);
3409
+ }
3410
+ }
3411
+ /**
3412
+ * One trip through the buddy travel loop for an account.
3413
+ *
3414
+ * A single pass advances the state machine by at most one step: a trip
3415
+ * that has arrived is collected, and an idle buddy is sent out. A buddy
3416
+ * already travelling is left alone — there is nothing to do until it lands.
3417
+ *
3418
+ * Measured against the live upstream: the departed trip reports
3419
+ * `dailyLimitReached` immediately, so the once-a-day limit needs no local
3420
+ * bookkeeping.
3421
+ */
3422
+ async runTravel(account) {
3423
+ const credential = account.credential;
3424
+ if (await this.client.buddyInfo(credential) === void 0) {
3425
+ try {
3426
+ await this.client.buddyAgree(credential);
3427
+ await this.client.buddyAdoptFirst(credential);
3428
+ this.logger.info?.(`automation travel ${account.label}: adopted first buddy`);
3429
+ } catch (error) {
3430
+ this.logger.info?.(`automation travel ${account.label}: adoption not available yet (` + String(error).slice(0, 80) + ")");
3431
+ }
3432
+ return;
3433
+ }
3434
+ const travel = await this.client.buddyTravelStatus(credential);
3435
+ if (travel.state === "arrived") {
3436
+ if (travel.recordId === 0) {
3437
+ this.logger.warn?.(`automation travel ${account.label}: arrived but no record id`);
3438
+ return;
3439
+ }
3440
+ const reward = await this.client.buddyTravelClaim(credential, travel.recordId);
3441
+ this.recordEarnings(account.id, dayKey(this.now()), { travelCredit: reward });
3442
+ this.logger.info?.(`automation travel ${account.label}: claimed trip +${reward}c`);
3443
+ return;
3444
+ }
3445
+ if (travel.state === "idle" && !travel.dailyLimitReached) {
3446
+ await this.client.buddyTravelDepart(credential);
3447
+ this.logger.info?.(`automation travel ${account.label}: departed (arrives later, claimed next pass)`);
3448
+ return;
3449
+ }
3450
+ this.logger.info?.(`automation travel ${account.label}: nothing to do (state=${travel.state})`);
3451
+ }
3452
+ };
3453
+ //#endregion
3454
+ //#region src/status.ts
3455
+ /** Format the per-model credit multipliers. */
3456
+ function formatRates(status) {
3457
+ const lines = ["Model credit multipliers:"];
1292
3458
  for (const model of status.models) {
1293
3459
  const rate = model.multiplier === void 0 ? "x?" : `x${model.multiplier.toFixed(2)}`;
1294
3460
  lines.push(` ${model.name.padEnd(20)} ${rate}`);
@@ -1310,16 +3476,44 @@ const modelSelectionSchema = z.object({
1310
3476
  imageModelIds: z.array(z.string()).description("Model ids accepting image input in this region (absent = follow upstream)"),
1311
3477
  contextBudgets: z.dict(z.number().step(1).min(1)).description("Per-model context-window override for this region")
1312
3478
  });
3479
+ /**
3480
+ * Automation schema.
3481
+ *
3482
+ * `enabled` carries a real default (false) because the scheduler reads it on
3483
+ * every tick and a missing field must mean "off" rather than "undefined".
3484
+ * The hour lists fall back in the scheduler itself, so they stay optional here
3485
+ * and an absent list keeps the documented schedule.
3486
+ *
3487
+ * `exhaustCooldownMs` is mirrored from the pool options: the card offers it as
3488
+ * part of the automation block, since how long a spent account rests only
3489
+ * matters to the automation that has to work around it.
3490
+ */
3491
+ const automationSchema = z.object({
3492
+ enabled: z.boolean().default(false).description("Run the daily points automation"),
3493
+ checkinHours: z.array(z.number().step(1).min(0).max(23)).description("Local hours for the daily check-in"),
3494
+ reportHours: z.array(z.number().step(1).min(0).max(23)).description("Local hours for the activity report (runs before tasks)"),
3495
+ taskHours: z.array(z.number().step(1).min(0).max(23)).description("Local hours for task enrolment and claiming"),
3496
+ streakHours: z.array(z.number().step(1).min(0).max(23)).description("Local hours for streak redemption"),
3497
+ travelHours: z.array(z.number().step(1).min(0).max(23)).description("Local hours for the buddy travel loop"),
3498
+ exhaustCooldownMs: z.number().step(1).min(1e3).description("How long a spent account rests, in milliseconds")
3499
+ });
1313
3500
  z.object({
1314
3501
  authFile: z.string().description("WorkBuddy desktop auth file (defaults to the app own location)"),
1315
3502
  cooldownMs: z.number().step(1).min(1e3).default(6e4).description("Rate-limit cooldown per account, in milliseconds"),
1316
- distribution: z.union(["priority", "round-robin"]).default("priority").description("How requests are spread: priority (drain one) or round-robin"),
3503
+ distribution: z.union([
3504
+ "priority",
3505
+ "round-robin",
3506
+ "balanced"
3507
+ ]).default("priority").description("How requests are spread: priority (drain one), round-robin (in order), or balanced (idle-weighted random)"),
1317
3508
  disabledAccountIds: z.array(z.string()).default([]).description("Account ids excluded from the pool (empty = every discovered account participates)"),
3509
+ creditReserves: z.dict(z.number().step(1).min(0)).default({}).description("Per-account credit floor: stop using an account once its balance reaches this value"),
1318
3510
  enabledModelIds: z.array(z.string()).default([]).description("Legacy shared model-id list; used by a region that has no per-region selection yet"),
1319
3511
  imageModelIds: z.array(z.string()).default([]).description("Legacy shared image-id list; used by a region that has no per-region selection yet"),
1320
3512
  contextBudgets: z.dict(z.number().step(1).min(1)).default({}).description("Legacy shared context budgets; used by a region with no per-region selection yet"),
1321
3513
  modelSelectionCn: modelSelectionSchema.description("Model selection for the domestic gateway"),
1322
- modelSelectionGlobal: modelSelectionSchema.description("Model selection for the international gateway")
3514
+ modelSelectionGlobal: modelSelectionSchema.description("Model selection for the international gateway"),
3515
+ automation: automationSchema.description("Daily points automation (activity report, task claiming, check-in)"),
3516
+ automationEarnings: z.any().description("Automation earnings ledger (written by the scheduler)")
1323
3517
  });
1324
3518
  /** Assemble the runtime objects without registering anything. */
1325
3519
  /**
@@ -1332,16 +3526,18 @@ z.object({
1332
3526
  */
1333
3527
  function createCore(logger) {
1334
3528
  const client = new WorkBuddyUpstreamClient();
3529
+ const pool = new WorkBuddyAccountPool({
3530
+ ...logger === void 0 ? {} : { logger },
3531
+ client
3532
+ });
1335
3533
  return {
1336
- pool: new WorkBuddyAccountPool({
1337
- ...logger === void 0 ? {} : { logger },
1338
- client
1339
- }),
3534
+ pool,
1340
3535
  catalogs: {
1341
3536
  cn: new WorkBuddyCatalog(),
1342
3537
  global: new WorkBuddyCatalog()
1343
3538
  },
1344
- client
3539
+ client,
3540
+ scheduler: new WorkBuddyScheduler(pool, client, { ...logger === void 0 ? {} : { logger } })
1345
3541
  };
1346
3542
  }
1347
3543
  //#endregion