@yu_robotics/remote-cli-router 1.6.176 → 1.6.183
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/README.md +62 -5
- package/dist/binding/BindingManager.d.ts +1 -0
- package/dist/binding/BindingManager.d.ts.map +1 -1
- package/dist/binding/BindingManager.js +3 -0
- package/dist/binding/BindingManager.js.map +1 -1
- package/dist/feishu/FeishuLongConnHandler.d.ts +7 -0
- package/dist/feishu/FeishuLongConnHandler.d.ts.map +1 -1
- package/dist/feishu/FeishuLongConnHandler.js +52 -0
- package/dist/feishu/FeishuLongConnHandler.js.map +1 -1
- package/dist/server.d.ts +1 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +30 -0
- package/dist/server.js.map +1 -1
- package/dist/storage/JsonStore.d.ts +2 -0
- package/dist/storage/JsonStore.d.ts.map +1 -1
- package/dist/storage/JsonStore.js +4 -0
- package/dist/storage/JsonStore.js.map +1 -1
- package/dist/tips/TipCards.d.ts +16 -0
- package/dist/tips/TipCards.d.ts.map +1 -0
- package/dist/tips/TipCards.js +64 -0
- package/dist/tips/TipCards.js.map +1 -0
- package/dist/tips/TipCatalog.d.ts +6 -0
- package/dist/tips/TipCatalog.d.ts.map +1 -0
- package/dist/tips/TipCatalog.js +43 -0
- package/dist/tips/TipCatalog.js.map +1 -0
- package/dist/tips/TipImages.d.ts +12 -0
- package/dist/tips/TipImages.d.ts.map +1 -0
- package/dist/tips/TipImages.js +55 -0
- package/dist/tips/TipImages.js.map +1 -0
- package/dist/tips/TipService.d.ts +38 -0
- package/dist/tips/TipService.d.ts.map +1 -0
- package/dist/tips/TipService.js +445 -0
- package/dist/tips/TipService.js.map +1 -0
- package/dist/tips/TipStore.d.ts +53 -0
- package/dist/tips/TipStore.d.ts.map +1 -0
- package/dist/tips/TipStore.js +212 -0
- package/dist/tips/TipStore.js.map +1 -0
- package/dist/tips/TipTypes.d.ts +32 -0
- package/dist/tips/TipTypes.d.ts.map +1 -0
- package/dist/tips/TipTypes.js +3 -0
- package/dist/tips/TipTypes.js.map +1 -0
- package/dist/tips/assets/abort-consequences.png +0 -0
- package/dist/tips/assets/backend-current-thread.png +0 -0
- package/dist/tips/assets/clear-thread-context.png +0 -0
- package/dist/tips/assets/codex-banked-reset-notice.png +0 -0
- package/dist/tips/assets/codex-model-speed.png +0 -0
- package/dist/tips/assets/codex-native-goal.png +0 -0
- package/dist/tips/assets/codex-weekly-reset-notice.png +0 -0
- package/dist/tips/assets/compact-backend.png +0 -0
- package/dist/tips/assets/cross-backend-review.png +0 -0
- package/dist/tips/assets/delegation-allowance.png +0 -0
- package/dist/tips/assets/delegation-by-instruction.png +0 -0
- package/dist/tips/assets/delegation-independent-checks.png +0 -0
- package/dist/tips/assets/delegation-parallel-changes.png +0 -0
- package/dist/tips/assets/delegation-reset-backend.png +0 -0
- package/dist/tips/assets/delegation-resolve-disagreement.png +0 -0
- package/dist/tips/assets/delegation-toggle.png +0 -0
- package/dist/tips/assets/device-selection.png +0 -0
- package/dist/tips/assets/directory-browser.png +0 -0
- package/dist/tips/assets/effort-native-options.png +0 -0
- package/dist/tips/assets/file-needs-instruction.png +0 -0
- package/dist/tips/assets/image-with-question.png +0 -0
- package/dist/tips/assets/model-within-backend.png +0 -0
- package/dist/tips/assets/named-project-thread.png +0 -0
- package/dist/tips/assets/queue-confirm.png +0 -0
- package/dist/tips/assets/sandbox-thread-controls.png +0 -0
- package/dist/tips/assets/skills-by-backend.png +0 -0
- package/dist/tips/assets/tool-row-details.png +0 -0
- package/dist/tips/catalog.zh-CN.json +623 -0
- package/dist/tips/ui.zh-CN.json +67 -0
- package/dist/types/config.d.ts +6 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/config.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TIPS = void 0;
|
|
7
|
+
exports.tipImagePath = tipImagePath;
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const catalog_zh_CN_json_1 = __importDefault(require("./catalog.zh-CN.json"));
|
|
10
|
+
const ID = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
11
|
+
const FIELDS = { id: 64, topic: 32, title: 40, problem: 100, benefit: 100,
|
|
12
|
+
action: 180, example: 240, detail: 240, image: 68 };
|
|
13
|
+
function imageName(tip) {
|
|
14
|
+
if (!ID.test(tip.id) || tip.id.length > FIELDS.id || tip.image !== `${tip.id}.png`) {
|
|
15
|
+
throw new Error('Invalid tip image reference');
|
|
16
|
+
}
|
|
17
|
+
return tip.image;
|
|
18
|
+
}
|
|
19
|
+
const ids = new Set();
|
|
20
|
+
if (!Array.isArray(catalog_zh_CN_json_1.default) || catalog_zh_CN_json_1.default.length !== 27)
|
|
21
|
+
throw new Error('The tip catalog must contain 27 entries');
|
|
22
|
+
/** Only reviewed contract fields leave the resource; illustration data is build-time input. */
|
|
23
|
+
exports.TIPS = Object.freeze(catalog_zh_CN_json_1.default.map(entry => {
|
|
24
|
+
const tip = {};
|
|
25
|
+
for (const key of Object.keys(FIELDS)) {
|
|
26
|
+
const value = entry[key];
|
|
27
|
+
if (typeof value !== 'string' || !value.trim() || value !== value.trim()
|
|
28
|
+
|| [...value].length > FIELDS[key] || /[\u0000-\u0008\u000b-\u001f\u007f]/.test(value)) {
|
|
29
|
+
throw new Error(`Invalid tip catalog field: ${key}`);
|
|
30
|
+
}
|
|
31
|
+
tip[key] = value;
|
|
32
|
+
}
|
|
33
|
+
if (!ID.test(tip.id) || !ID.test(tip.topic) || ids.has(tip.id))
|
|
34
|
+
throw new Error('Invalid or duplicate tip ID/topic');
|
|
35
|
+
imageName(tip);
|
|
36
|
+
ids.add(tip.id);
|
|
37
|
+
return Object.freeze(tip);
|
|
38
|
+
}));
|
|
39
|
+
/** Works from both src/tips and the packaged dist/tips directory. */
|
|
40
|
+
function tipImagePath(tip) {
|
|
41
|
+
return path_1.default.join(__dirname, 'assets', imageName(tip));
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=TipCatalog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TipCatalog.js","sourceRoot":"","sources":["../../src/tips/TipCatalog.ts"],"names":[],"mappings":";;;;;;AAoCA,oCAEC;AAtCD,gDAAwB;AACxB,8EAA2C;AAG3C,MAAM,EAAE,GAAG,iCAAiC,CAAC;AAC7C,MAAM,MAAM,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG;IACvE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,EAAW,CAAC;AAE/D,SAAS,SAAS,CAAC,GAAwC;IACzD,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,IAAI,GAAG,CAAC,KAAK,KAAK,GAAG,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC;QACnF,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACjD,CAAC;IACD,OAAO,GAAG,CAAC,KAAK,CAAC;AACnB,CAAC;AAED,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;AAC9B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,4BAAO,CAAC,IAAI,4BAAO,CAAC,MAAM,KAAK,EAAE;IAAE,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;AAEjH,+FAA+F;AAClF,QAAA,IAAI,GAA6B,MAAM,CAAC,MAAM,CAAC,4BAAO,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE;IAC9E,MAAM,GAAG,GAAG,EAAmB,CAAC;IAChC,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAA4B,EAAE,CAAC;QACjE,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QACzB,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,KAAK,KAAK,KAAK,CAAC,IAAI,EAAE;eACnE,CAAC,GAAG,KAAK,CAAC,CAAC,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,oCAAoC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACzF,MAAM,IAAI,KAAK,CAAC,8BAA8B,GAAG,EAAE,CAAC,CAAC;QACvD,CAAC;QACD,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACnB,CAAC;IACD,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACrH,SAAS,CAAC,GAAG,CAAC,CAAC;IACf,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAChB,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC,CAAC,CAAC,CAAC;AAEJ,qEAAqE;AACrE,SAAgB,YAAY,CAAC,GAAwC;IACnE,OAAO,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;AACxD,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TipDefinition } from './TipTypes';
|
|
2
|
+
export declare const TIP_IMAGE_TIMEOUT = 10000;
|
|
3
|
+
/** Cache successful uploads only. A failed upload can be retried by a later request. */
|
|
4
|
+
export declare class TipImages {
|
|
5
|
+
private readonly upload;
|
|
6
|
+
private readonly uploads;
|
|
7
|
+
constructor(upload: (data: string, mimeType: string) => Promise<string | null>);
|
|
8
|
+
get(tip: TipDefinition): Promise<string | null>;
|
|
9
|
+
private boundedLoad;
|
|
10
|
+
private load;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=TipImages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TipImages.d.ts","sourceRoot":"","sources":["../../src/tips/TipImages.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,eAAO,MAAM,iBAAiB,QAAS,CAAC;AAExC,wFAAwF;AACxF,qBAAa,SAAS;IAGR,OAAO,CAAC,QAAQ,CAAC,MAAM;IAFnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA6C;gBAExC,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAE/F,GAAG,CAAC,GAAG,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;YASjC,WAAW;YAWX,IAAI;CASnB"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.TipImages = exports.TIP_IMAGE_TIMEOUT = void 0;
|
|
7
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
8
|
+
const TipCatalog_1 = require("./TipCatalog");
|
|
9
|
+
exports.TIP_IMAGE_TIMEOUT = 10_000;
|
|
10
|
+
/** Cache successful uploads only. A failed upload can be retried by a later request. */
|
|
11
|
+
class TipImages {
|
|
12
|
+
upload;
|
|
13
|
+
uploads = new Map();
|
|
14
|
+
constructor(upload) {
|
|
15
|
+
this.upload = upload;
|
|
16
|
+
}
|
|
17
|
+
get(tip) {
|
|
18
|
+
const pending = this.uploads.get(tip.id);
|
|
19
|
+
if (pending)
|
|
20
|
+
return pending;
|
|
21
|
+
const attempt = this.boundedLoad(tip);
|
|
22
|
+
this.uploads.set(tip.id, attempt);
|
|
23
|
+
void attempt.then(key => { if (!key && this.uploads.get(tip.id) === attempt)
|
|
24
|
+
this.uploads.delete(tip.id); });
|
|
25
|
+
return attempt;
|
|
26
|
+
}
|
|
27
|
+
async boundedLoad(tip) {
|
|
28
|
+
let timer;
|
|
29
|
+
let active = true;
|
|
30
|
+
try {
|
|
31
|
+
return await Promise.race([this.load(tip, () => active), new Promise(resolve => {
|
|
32
|
+
timer = setTimeout(() => resolve(null), exports.TIP_IMAGE_TIMEOUT);
|
|
33
|
+
timer.unref();
|
|
34
|
+
})]);
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
active = false;
|
|
38
|
+
if (timer)
|
|
39
|
+
clearTimeout(timer);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async load(tip, active) {
|
|
43
|
+
try {
|
|
44
|
+
const image = await promises_1.default.readFile((0, TipCatalog_1.tipImagePath)(tip));
|
|
45
|
+
if (!active() || !image.length || image.length > 2 * 1024 * 1024)
|
|
46
|
+
return null;
|
|
47
|
+
return await this.upload(image.toString('base64'), 'image/png');
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.TipImages = TipImages;
|
|
55
|
+
//# sourceMappingURL=TipImages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TipImages.js","sourceRoot":"","sources":["../../src/tips/TipImages.ts"],"names":[],"mappings":";;;;;;AAAA,2DAA6B;AAC7B,6CAA4C;AAG/B,QAAA,iBAAiB,GAAG,MAAM,CAAC;AAExC,wFAAwF;AACxF,MAAa,SAAS;IAGS;IAFZ,OAAO,GAAG,IAAI,GAAG,EAAkC,CAAC;IAErE,YAA6B,MAAkE;QAAlE,WAAM,GAAN,MAAM,CAA4D;IAAG,CAAC;IAEnG,GAAG,CAAC,GAAkB;QACpB,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QACzC,IAAI,OAAO;YAAE,OAAO,OAAO,CAAC;QAC5B,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACtC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;QAClC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,OAAO;YAAE,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7G,OAAO,OAAO,CAAC;IACjB,CAAC;IAEO,KAAK,CAAC,WAAW,CAAC,GAAkB;QAC1C,IAAI,KAAgD,CAAC;QACrD,IAAI,MAAM,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC;YACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,EAAE,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE;oBACnF,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,yBAAiB,CAAC,CAAC;oBAC3D,KAAK,CAAC,KAAK,EAAE,CAAC;gBAChB,CAAC,CAAC,CAAC,CAAC,CAAC;QACP,CAAC;gBAAS,CAAC;YAAC,MAAM,GAAG,KAAK,CAAC;YAAC,IAAI,KAAK;gBAAE,YAAY,CAAC,KAAK,CAAC,CAAC;QAAC,CAAC;IAC/D,CAAC;IAEO,KAAK,CAAC,IAAI,CAAC,GAAkB,EAAE,MAAqB;QAC1D,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,kBAAE,CAAC,QAAQ,CAAC,IAAA,yBAAY,EAAC,GAAG,CAAC,CAAC,CAAC;YACnD,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,GAAG,IAAI,GAAG,IAAI;gBAAE,OAAO,IAAI,CAAC;YAC9E,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,WAAW,CAAC,CAAC;QAClE,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;CACF;AAlCD,8BAkCC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { TipServiceOptions } from './TipTypes';
|
|
2
|
+
export declare const TIP_TRANSPORT_TIMEOUT = 15000;
|
|
3
|
+
/** Single-process, user-scoped educational delivery. No executor or device API is used. */
|
|
4
|
+
export declare class TipService {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly store;
|
|
7
|
+
private readonly now;
|
|
8
|
+
private readonly locks;
|
|
9
|
+
private readonly pendingActivity;
|
|
10
|
+
private timer?;
|
|
11
|
+
private ticking?;
|
|
12
|
+
private stopped;
|
|
13
|
+
constructor(options: TipServiceOptions);
|
|
14
|
+
start(): void;
|
|
15
|
+
stop(): Promise<void>;
|
|
16
|
+
private external;
|
|
17
|
+
private lock;
|
|
18
|
+
private observeActivity;
|
|
19
|
+
private bound;
|
|
20
|
+
private warning;
|
|
21
|
+
activity(openId: string): Promise<void>;
|
|
22
|
+
private prune;
|
|
23
|
+
private nextTip;
|
|
24
|
+
private quiet;
|
|
25
|
+
private eligible;
|
|
26
|
+
private deliver;
|
|
27
|
+
private settings;
|
|
28
|
+
private showTip;
|
|
29
|
+
private change;
|
|
30
|
+
private off;
|
|
31
|
+
command(openId: string, content: string): Promise<void>;
|
|
32
|
+
private validAction;
|
|
33
|
+
action(openId: string, cardId: string, payload: unknown): Promise<string>;
|
|
34
|
+
private update;
|
|
35
|
+
tick(): Promise<void>;
|
|
36
|
+
private scheduledTick;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=TipService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TipService.d.ts","sourceRoot":"","sources":["../../src/tips/TipService.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAA4B,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAS9E,eAAO,MAAM,qBAAqB,QAAS,CAAC;AAU5C,2FAA2F;AAC3F,qBAAa,UAAU;IAST,OAAO,CAAC,QAAQ,CAAC,OAAO;IARpC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAW;IACjC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAoC;IAC1D,OAAO,CAAC,QAAQ,CAAC,eAAe,CAA6B;IAC7D,OAAO,CAAC,KAAK,CAAC,CAAiC;IAC/C,OAAO,CAAC,OAAO,CAAC,CAAgB;IAChC,OAAO,CAAC,OAAO,CAAS;gBAEK,OAAO,EAAE,iBAAiB;IAOvD,KAAK,IAAI,IAAI;IAMP,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;YASb,QAAQ;IActB,OAAO,CAAC,IAAI;IAaZ,OAAO,CAAC,eAAe;YAKT,KAAK;YAIL,OAAO;IAQf,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAa7C,OAAO,CAAC,KAAK;IAKb,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,KAAK;IAMb,OAAO,CAAC,QAAQ;YASF,OAAO;YAqCP,QAAQ;YAIR,OAAO;YAOP,MAAM;YAQN,GAAG;IASX,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA4C7D,OAAO,CAAC,WAAW;IAQb,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;YAkFjE,MAAM;IAKpB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;YASP,aAAa;CAmB5B"}
|
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TipService = exports.TIP_TRANSPORT_TIMEOUT = void 0;
|
|
4
|
+
const crypto_1 = require("crypto");
|
|
5
|
+
const TipStore_1 = require("./TipStore");
|
|
6
|
+
const TipCards_1 = require("./TipCards");
|
|
7
|
+
const DAY = 86400_000;
|
|
8
|
+
const NAVIGATION_LIFETIME = DAY;
|
|
9
|
+
const CONTROL_LIFETIME = 30 * DAY;
|
|
10
|
+
exports.TIP_TRANSPORT_TIMEOUT = 15_000;
|
|
11
|
+
const TICK_INTERVAL = 60_000;
|
|
12
|
+
function localTime(time, timezone) {
|
|
13
|
+
const parts = new Intl.DateTimeFormat('en-US', { timeZone: timezone, year: 'numeric', month: '2-digit', day: '2-digit',
|
|
14
|
+
hour: '2-digit', minute: '2-digit', hourCycle: 'h23', weekday: 'short' }).formatToParts(time);
|
|
15
|
+
const part = (name) => parts.find(value => value.type === name).value;
|
|
16
|
+
return { day: `${part('year')}-${part('month')}-${part('day')}`, minute: Number(part('hour')) * 60 + Number(part('minute')), weekday: part('weekday') };
|
|
17
|
+
}
|
|
18
|
+
/** Single-process, user-scoped educational delivery. No executor or device API is used. */
|
|
19
|
+
class TipService {
|
|
20
|
+
options;
|
|
21
|
+
store;
|
|
22
|
+
now;
|
|
23
|
+
locks = new Map();
|
|
24
|
+
pendingActivity = new Map();
|
|
25
|
+
timer;
|
|
26
|
+
ticking;
|
|
27
|
+
stopped = false;
|
|
28
|
+
constructor(options) {
|
|
29
|
+
this.options = options;
|
|
30
|
+
this.now = options.now ?? Date.now;
|
|
31
|
+
this.store = new TipStore_1.TipStore(options.directory, options.timezone ?? 'Asia/Shanghai');
|
|
32
|
+
if (new Set(options.catalog.map(tip => tip.id)).size !== options.catalog.length
|
|
33
|
+
|| options.catalog.some(tip => !/^[a-z0-9][a-z0-9._-]{0,79}$/.test(tip.id)))
|
|
34
|
+
throw new Error('Invalid Tips catalog.');
|
|
35
|
+
}
|
|
36
|
+
start() {
|
|
37
|
+
if (this.timer || this.stopped)
|
|
38
|
+
return;
|
|
39
|
+
this.timer = setInterval(() => { void this.tick(); }, TICK_INTERVAL);
|
|
40
|
+
this.timer.unref();
|
|
41
|
+
}
|
|
42
|
+
async stop() {
|
|
43
|
+
this.stopped = true;
|
|
44
|
+
if (this.timer)
|
|
45
|
+
clearInterval(this.timer);
|
|
46
|
+
this.timer = undefined;
|
|
47
|
+
// Injected transport is bounded here too. Late transport results cannot write or send again.
|
|
48
|
+
await this.ticking;
|
|
49
|
+
await Promise.all([...this.locks.values()]);
|
|
50
|
+
}
|
|
51
|
+
async external(work) {
|
|
52
|
+
if (this.stopped)
|
|
53
|
+
throw new Error('Tips service is stopped.');
|
|
54
|
+
let timer;
|
|
55
|
+
try {
|
|
56
|
+
return await Promise.race([Promise.resolve().then(() => {
|
|
57
|
+
if (this.stopped)
|
|
58
|
+
throw new Error('Tips service is stopped.');
|
|
59
|
+
return work();
|
|
60
|
+
}), new Promise((_, reject) => {
|
|
61
|
+
timer = setTimeout(() => reject(new Error('Tips transport timed out.')), exports.TIP_TRANSPORT_TIMEOUT);
|
|
62
|
+
timer.unref();
|
|
63
|
+
})]);
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
if (timer)
|
|
67
|
+
clearTimeout(timer);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
lock(openId, work) {
|
|
71
|
+
const next = (this.locks.get(openId) ?? Promise.resolve()).then(work);
|
|
72
|
+
const tail = next.then(() => { }, () => { });
|
|
73
|
+
this.locks.set(openId, tail);
|
|
74
|
+
void tail.then(() => {
|
|
75
|
+
if (this.locks.get(openId) === tail) {
|
|
76
|
+
this.locks.delete(openId);
|
|
77
|
+
this.pendingActivity.delete(openId);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
return next;
|
|
81
|
+
}
|
|
82
|
+
observeActivity(openId) {
|
|
83
|
+
// Do not wait behind an image upload to observe a new user interaction.
|
|
84
|
+
if (this.locks.has(openId))
|
|
85
|
+
this.pendingActivity.set(openId, this.now());
|
|
86
|
+
}
|
|
87
|
+
async bound(openId) {
|
|
88
|
+
return !this.stopped && await this.external(() => this.options.isBound(openId)) && !this.stopped;
|
|
89
|
+
}
|
|
90
|
+
async warning(openId, message) {
|
|
91
|
+
try {
|
|
92
|
+
if (!await this.bound(openId))
|
|
93
|
+
return;
|
|
94
|
+
const card = (0, TipCards_1.messageCard)(message);
|
|
95
|
+
await this.external(() => this.options.create(openId, card.elements, card.header, (0, crypto_1.randomUUID)()));
|
|
96
|
+
}
|
|
97
|
+
catch { /* Educational failures never interrupt ordinary task handling. */ }
|
|
98
|
+
}
|
|
99
|
+
async activity(openId) {
|
|
100
|
+
if (this.stopped)
|
|
101
|
+
return;
|
|
102
|
+
this.observeActivity(openId);
|
|
103
|
+
await this.lock(openId, async () => {
|
|
104
|
+
try {
|
|
105
|
+
if (!await this.bound(openId))
|
|
106
|
+
return;
|
|
107
|
+
const state = await this.store.load(openId);
|
|
108
|
+
state.lastActivity = this.now();
|
|
109
|
+
await this.store.save(openId, state);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
console.warn('Tips activity storage is unavailable.');
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
prune(state) {
|
|
117
|
+
state.cards = state.cards.filter(card => this.now() - card.createdAt < CONTROL_LIFETIME).slice(-(TipStore_1.TIP_HISTORY_LIMIT - 1));
|
|
118
|
+
state.deliveries = state.deliveries.slice(-(TipStore_1.TIP_HISTORY_LIMIT - 1));
|
|
119
|
+
}
|
|
120
|
+
nextTip(state) {
|
|
121
|
+
return this.options.catalog.find(tip => !state.seen.includes(tip.id));
|
|
122
|
+
}
|
|
123
|
+
quiet(openId, state) {
|
|
124
|
+
const activity = Math.max(state.lastActivity, this.pendingActivity.get(openId) ?? 0);
|
|
125
|
+
const age = this.now() - activity;
|
|
126
|
+
return activity > 0 && age >= 120_000 && age <= 7 * DAY;
|
|
127
|
+
}
|
|
128
|
+
eligible(openId, state) {
|
|
129
|
+
const now = this.now();
|
|
130
|
+
if (state.subscription === 'off' || state.muteUntil > now || !this.quiet(openId, state) || !this.nextTip(state))
|
|
131
|
+
return false;
|
|
132
|
+
const local = localTime(now, state.timezone);
|
|
133
|
+
if (local.minute < 630 || local.minute >= 1140 || state.subscription === 'weekly' && !['Tue', 'Thu'].includes(local.weekday))
|
|
134
|
+
return false;
|
|
135
|
+
return !state.lastAutomaticAt || now - state.lastAutomaticAt >= 20 * 3600_000
|
|
136
|
+
&& localTime(state.lastAutomaticAt, state.timezone).day !== local.day;
|
|
137
|
+
}
|
|
138
|
+
async deliver(openId, state, kind, render, tip, automatic = false, beforeDispatch) {
|
|
139
|
+
if (!await this.bound(openId) || automatic && (!this.eligible(openId, state) || !await this.external(() => this.options.canDeliver(openId))))
|
|
140
|
+
return false;
|
|
141
|
+
this.prune(state);
|
|
142
|
+
const receipt = { id: (0, crypto_1.randomUUID)(), cardId: null, kind, ...(tip ? { tipId: tip.id } : {}), createdAt: this.now(), revision: state.revision, used: [] };
|
|
143
|
+
const delivery = { id: receipt.id, ...(tip ? { tipId: tip.id } : {}), automatic, at: this.now(), state: 'reserved' };
|
|
144
|
+
state.cards.push(receipt);
|
|
145
|
+
state.deliveries.push(delivery);
|
|
146
|
+
await this.store.save(openId, state);
|
|
147
|
+
let image = '';
|
|
148
|
+
if (tip) {
|
|
149
|
+
try {
|
|
150
|
+
image = await this.external(() => this.options.image(tip)) ?? '';
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (!image)
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
if (!await this.bound(openId) || automatic && (!this.eligible(openId, state) || !await this.external(() => this.options.canDeliver(openId))))
|
|
159
|
+
return false;
|
|
160
|
+
const card = render(receipt, image);
|
|
161
|
+
const intent = state.deliveries.find(item => item.id === receipt.id);
|
|
162
|
+
intent.state = 'dispatching';
|
|
163
|
+
intent.at = this.now();
|
|
164
|
+
if (tip && !state.seen.includes(tip.id))
|
|
165
|
+
state.seen.push(tip.id);
|
|
166
|
+
if (automatic)
|
|
167
|
+
state.lastAutomaticAt = intent.at;
|
|
168
|
+
beforeDispatch?.();
|
|
169
|
+
// A failed write forbids invocation. A crash after this write is an uncertain send, never a replay.
|
|
170
|
+
await this.store.save(openId, state);
|
|
171
|
+
let cardId = null;
|
|
172
|
+
try {
|
|
173
|
+
if (await this.bound(openId) && (!automatic || this.quiet(openId, state)
|
|
174
|
+
&& await this.external(() => this.options.canDeliver(openId)) && this.quiet(openId, state))) {
|
|
175
|
+
cardId = await this.external(() => this.options.create(openId, card.elements, card.header, receipt.id));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch { /* The remote side may have accepted the card. */ }
|
|
179
|
+
intent.state = cardId ? 'confirmed' : 'unknown';
|
|
180
|
+
receipt.cardId = cardId;
|
|
181
|
+
await this.store.save(openId, state);
|
|
182
|
+
return Boolean(cardId);
|
|
183
|
+
}
|
|
184
|
+
async settings(openId, state) {
|
|
185
|
+
await this.deliver(openId, state, 'settings', receipt => (0, TipCards_1.settingsCard)(state, receipt, this.now()));
|
|
186
|
+
}
|
|
187
|
+
async showTip(openId, state, tip = this.nextTip(state), beforeDispatch) {
|
|
188
|
+
if (!tip)
|
|
189
|
+
return this.deliver(openId, state, 'help', () => (0, TipCards_1.messageCard)(TipCards_1.ui.exhausted), undefined, false, beforeDispatch);
|
|
190
|
+
const sent = await this.deliver(openId, state, 'tip', (receipt, image) => (0, TipCards_1.tipCard)(tip, receipt, image), tip, false, beforeDispatch);
|
|
191
|
+
if (!sent && !state.seen.includes(tip.id))
|
|
192
|
+
await this.warning(openId, TipCards_1.ui.imageFailed);
|
|
193
|
+
return sent;
|
|
194
|
+
}
|
|
195
|
+
async change(openId, state, apply, enroll = false) {
|
|
196
|
+
apply();
|
|
197
|
+
state.revision = (0, crypto_1.randomUUID)();
|
|
198
|
+
await this.store.save(openId, state);
|
|
199
|
+
// Persist enrollment first; an existing stop switch wins through every failure/crash gap.
|
|
200
|
+
if (enroll)
|
|
201
|
+
await this.store.clearOptOut(openId);
|
|
202
|
+
}
|
|
203
|
+
async off(openId) {
|
|
204
|
+
await this.store.optOut(openId);
|
|
205
|
+
try {
|
|
206
|
+
const state = await this.store.load(openId);
|
|
207
|
+
state.subscription = 'off';
|
|
208
|
+
state.revision = (0, crypto_1.randomUUID)();
|
|
209
|
+
state.lastActivity = this.now();
|
|
210
|
+
await this.store.save(openId, state);
|
|
211
|
+
}
|
|
212
|
+
catch { /* The independent stop switch is already durable. */ }
|
|
213
|
+
}
|
|
214
|
+
async command(openId, content) {
|
|
215
|
+
if (this.stopped)
|
|
216
|
+
return;
|
|
217
|
+
this.observeActivity(openId);
|
|
218
|
+
await this.lock(openId, async () => {
|
|
219
|
+
try {
|
|
220
|
+
if (!await this.bound(openId))
|
|
221
|
+
return;
|
|
222
|
+
const parts = content.trim().split(/\s+/);
|
|
223
|
+
if (parts.shift()?.toLowerCase() !== '/tips')
|
|
224
|
+
return;
|
|
225
|
+
const [op = 'next', value, ...extra] = parts;
|
|
226
|
+
if (op === 'off' && value === undefined) {
|
|
227
|
+
await this.off(openId);
|
|
228
|
+
await this.warning(openId, TipCards_1.ui.stopped);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const state = await this.store.load(openId);
|
|
232
|
+
state.lastActivity = this.now();
|
|
233
|
+
await this.store.save(openId, state);
|
|
234
|
+
if (extra.length || !['mute', 'timezone'].includes(op) && value !== undefined) {
|
|
235
|
+
await this.deliver(openId, state, 'help', () => (0, TipCards_1.messageCard)(TipCards_1.ui.help));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
switch (op) {
|
|
239
|
+
case 'next':
|
|
240
|
+
await this.showTip(openId, state);
|
|
241
|
+
break;
|
|
242
|
+
case 'list':
|
|
243
|
+
await this.deliver(openId, state, 'catalog', receipt => (0, TipCards_1.catalogCard)(this.options.catalog, receipt));
|
|
244
|
+
break;
|
|
245
|
+
case 'settings':
|
|
246
|
+
await this.settings(openId, state);
|
|
247
|
+
break;
|
|
248
|
+
case 'daily':
|
|
249
|
+
case 'weekly':
|
|
250
|
+
await this.change(openId, state, () => { state.subscription = op; state.muteUntil = 0; }, true);
|
|
251
|
+
await this.settings(openId, state);
|
|
252
|
+
break;
|
|
253
|
+
case 'mute':
|
|
254
|
+
if (!value || !/^\d{1,3}$/.test(value) || Number(value) < 1 || Number(value) > 365) {
|
|
255
|
+
await this.deliver(openId, state, 'help', () => (0, TipCards_1.messageCard)(TipCards_1.ui.invalidDays));
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
await this.change(openId, state, () => { state.muteUntil = this.now() + Number(value) * DAY; });
|
|
259
|
+
await this.settings(openId, state);
|
|
260
|
+
break;
|
|
261
|
+
case 'unmute':
|
|
262
|
+
await this.change(openId, state, () => { state.muteUntil = 0; });
|
|
263
|
+
await this.settings(openId, state);
|
|
264
|
+
break;
|
|
265
|
+
case 'timezone':
|
|
266
|
+
if (!(0, TipStore_1.validTimezone)(value)) {
|
|
267
|
+
await this.deliver(openId, state, 'help', () => (0, TipCards_1.messageCard)(TipCards_1.ui.invalidTimezone));
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
await this.change(openId, state, () => { state.timezone = value; });
|
|
271
|
+
await this.settings(openId, state);
|
|
272
|
+
break;
|
|
273
|
+
default: await this.deliver(openId, state, 'help', () => (0, TipCards_1.messageCard)(TipCards_1.ui.help));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
catch {
|
|
277
|
+
await this.warning(openId, TipCards_1.ui.unavailable);
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
validAction(payload) {
|
|
282
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload))
|
|
283
|
+
return false;
|
|
284
|
+
const value = payload;
|
|
285
|
+
return Object.keys(value).every(key => ['action', 'id', 'op', 'value'].includes(key)) && value.action === 'tips'
|
|
286
|
+
&& typeof value.id === 'string' && value.id.length === 36 && typeof value.op === 'string' && value.op.length < 30
|
|
287
|
+
&& (value.value === undefined || typeof value.value === 'string' && value.value.length <= 100);
|
|
288
|
+
}
|
|
289
|
+
async action(openId, cardId, payload) {
|
|
290
|
+
if (this.stopped)
|
|
291
|
+
return TipCards_1.ui.unavailable;
|
|
292
|
+
this.observeActivity(openId);
|
|
293
|
+
return this.lock(openId, async () => {
|
|
294
|
+
try {
|
|
295
|
+
if (!await this.bound(openId))
|
|
296
|
+
return TipCards_1.ui.unbound;
|
|
297
|
+
if (!this.validAction(payload))
|
|
298
|
+
return TipCards_1.ui.invalid;
|
|
299
|
+
const state = await this.store.load(openId);
|
|
300
|
+
const receipt = state.cards.find(card => card.id === payload.id && card.cardId === cardId);
|
|
301
|
+
if (!receipt || this.now() - receipt.createdAt >= CONTROL_LIFETIME || receipt.createdAt > this.now())
|
|
302
|
+
return TipCards_1.ui.stale;
|
|
303
|
+
const { op, value } = payload;
|
|
304
|
+
const navigation = ['detail', 'example', 'next', 'mastered', 'show', 'page'].includes(op);
|
|
305
|
+
if (navigation && this.now() - receipt.createdAt >= NAVIGATION_LIFETIME)
|
|
306
|
+
return TipCards_1.ui.stale;
|
|
307
|
+
if (op === 'settings' && value === undefined) {
|
|
308
|
+
state.lastActivity = this.now();
|
|
309
|
+
await this.settings(openId, state);
|
|
310
|
+
return TipCards_1.ui.delivered;
|
|
311
|
+
}
|
|
312
|
+
if (receipt.kind === 'settings') {
|
|
313
|
+
if (op === 'off' && value === undefined) {
|
|
314
|
+
await this.off(openId);
|
|
315
|
+
return TipCards_1.ui.stopped;
|
|
316
|
+
}
|
|
317
|
+
if (op === 'subscribe' && ['daily', 'weekly'].includes(value ?? '') || op === 'unmute' && value === undefined) {
|
|
318
|
+
if (receipt.revision !== state.revision)
|
|
319
|
+
return TipCards_1.ui.stale;
|
|
320
|
+
state.lastActivity = this.now();
|
|
321
|
+
await this.change(openId, state, () => {
|
|
322
|
+
if (op === 'subscribe')
|
|
323
|
+
state.subscription = value;
|
|
324
|
+
state.muteUntil = 0;
|
|
325
|
+
}, op === 'subscribe');
|
|
326
|
+
await this.settings(openId, state);
|
|
327
|
+
return TipCards_1.ui.saved;
|
|
328
|
+
}
|
|
329
|
+
if (op === 'mute' && ['1', '3', '7', '14'].includes(value ?? '')) {
|
|
330
|
+
const token = `${op}:${value}`;
|
|
331
|
+
if (receipt.used.includes(token))
|
|
332
|
+
return TipCards_1.ui.already;
|
|
333
|
+
const until = this.now() + Number(value) * DAY;
|
|
334
|
+
if (receipt.revision !== state.revision && until < state.muteUntil)
|
|
335
|
+
return TipCards_1.ui.stale;
|
|
336
|
+
await this.change(openId, state, () => {
|
|
337
|
+
receipt.used.push(token);
|
|
338
|
+
state.lastActivity = this.now();
|
|
339
|
+
state.muteUntil = until;
|
|
340
|
+
});
|
|
341
|
+
await this.settings(openId, state);
|
|
342
|
+
return TipCards_1.ui.saved;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (receipt.kind === 'catalog') {
|
|
346
|
+
if (op === 'page' && value !== undefined && /^\d{1,4}$/.test(value) && Number(value) < Math.ceil(this.options.catalog.length / TipCards_1.TIP_PAGE_SIZE)) {
|
|
347
|
+
state.lastActivity = this.now();
|
|
348
|
+
await this.store.save(openId, state);
|
|
349
|
+
const card = (0, TipCards_1.catalogCard)(this.options.catalog, receipt, Number(value));
|
|
350
|
+
await this.update(openId, cardId, card);
|
|
351
|
+
return TipCards_1.ui.delivered;
|
|
352
|
+
}
|
|
353
|
+
if (op === 'show' && value !== undefined) {
|
|
354
|
+
const tip = this.options.catalog.find(item => item.id === value);
|
|
355
|
+
if (!tip)
|
|
356
|
+
return TipCards_1.ui.invalid;
|
|
357
|
+
const token = `show:${value}`;
|
|
358
|
+
if (receipt.used.includes(token))
|
|
359
|
+
return TipCards_1.ui.already;
|
|
360
|
+
if (receipt.used.length >= 32)
|
|
361
|
+
return TipCards_1.ui.stale;
|
|
362
|
+
state.lastActivity = this.now();
|
|
363
|
+
await this.store.save(openId, state);
|
|
364
|
+
return await this.showTip(openId, state, tip, () => receipt.used.push(token)) ? TipCards_1.ui.delivered : TipCards_1.ui.unavailable;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (receipt.kind === 'tip' && value === undefined) {
|
|
368
|
+
const tip = this.options.catalog.find(item => item.id === receipt.tipId);
|
|
369
|
+
if (!tip)
|
|
370
|
+
return TipCards_1.ui.stale;
|
|
371
|
+
if (op === 'next' || op === 'mastered') {
|
|
372
|
+
if (receipt.used.includes(op))
|
|
373
|
+
return TipCards_1.ui.already;
|
|
374
|
+
state.lastActivity = this.now();
|
|
375
|
+
if (op === 'next') {
|
|
376
|
+
await this.store.save(openId, state);
|
|
377
|
+
return await this.showTip(openId, state, undefined, () => receipt.used.push(op)) ? TipCards_1.ui.delivered : TipCards_1.ui.unavailable;
|
|
378
|
+
}
|
|
379
|
+
receipt.used.push(op);
|
|
380
|
+
if (op === 'mastered' && !state.mastered.includes(tip.id))
|
|
381
|
+
state.mastered.push(tip.id);
|
|
382
|
+
await this.store.save(openId, state);
|
|
383
|
+
return TipCards_1.ui.learned;
|
|
384
|
+
}
|
|
385
|
+
if (op === 'detail' || op === 'example') {
|
|
386
|
+
const image = await this.external(() => this.options.image(tip));
|
|
387
|
+
if (!image)
|
|
388
|
+
return TipCards_1.ui.imageFailed;
|
|
389
|
+
state.lastActivity = this.now();
|
|
390
|
+
await this.store.save(openId, state);
|
|
391
|
+
// Previously delivered detail and example buttons refresh the same inline tutorial.
|
|
392
|
+
await this.update(openId, cardId, (0, TipCards_1.tipCard)(tip, receipt, image));
|
|
393
|
+
return TipCards_1.ui.delivered;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return TipCards_1.ui.invalid;
|
|
397
|
+
}
|
|
398
|
+
catch {
|
|
399
|
+
return TipCards_1.ui.unavailable;
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
async update(openId, cardId, card) {
|
|
404
|
+
if (!await this.bound(openId))
|
|
405
|
+
throw new Error('Tips user is no longer bound.');
|
|
406
|
+
await this.external(() => this.options.update(cardId, card.elements, card.header));
|
|
407
|
+
}
|
|
408
|
+
tick() {
|
|
409
|
+
if (this.stopped)
|
|
410
|
+
return Promise.resolve();
|
|
411
|
+
if (this.ticking)
|
|
412
|
+
return this.ticking;
|
|
413
|
+
const work = this.scheduledTick().catch(() => { console.warn('Tips scheduler is unavailable.'); });
|
|
414
|
+
this.ticking = work;
|
|
415
|
+
void work.then(() => { if (this.ticking === work)
|
|
416
|
+
this.ticking = undefined; });
|
|
417
|
+
return work;
|
|
418
|
+
}
|
|
419
|
+
async scheduledTick() {
|
|
420
|
+
const users = [...new Set(await this.external(() => this.options.users()))];
|
|
421
|
+
// Bound concurrency prevents a large binding list from flooding image uploads or storage.
|
|
422
|
+
let index = 0;
|
|
423
|
+
await Promise.all(Array.from({ length: Math.min(4, users.length) }, async () => {
|
|
424
|
+
while (!this.stopped && index < users.length) {
|
|
425
|
+
const openId = users[index++];
|
|
426
|
+
await this.lock(openId, async () => {
|
|
427
|
+
try {
|
|
428
|
+
if (!await this.bound(openId))
|
|
429
|
+
return;
|
|
430
|
+
const state = await this.store.load(openId);
|
|
431
|
+
if (!this.eligible(openId, state))
|
|
432
|
+
return;
|
|
433
|
+
const tip = this.nextTip(state);
|
|
434
|
+
await this.deliver(openId, state, 'tip', (receipt, image) => (0, TipCards_1.tipCard)(tip, receipt, image), tip, true);
|
|
435
|
+
}
|
|
436
|
+
catch {
|
|
437
|
+
console.warn('Tips delivery is unavailable.');
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}));
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
exports.TipService = TipService;
|
|
445
|
+
//# sourceMappingURL=TipService.js.map
|