@reconcrap/boss-recommend-mcp 1.2.9 → 1.3.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/README.md +82 -1
- package/package.json +2 -1
- package/skills/boss-chat/README.md +5 -0
- package/skills/boss-chat/SKILL.md +69 -0
- package/skills/boss-recommend-pipeline/SKILL.md +40 -4
- package/src/adapters.js +19 -5
- package/src/boss-chat.js +436 -0
- package/src/cli.js +294 -129
- package/src/index.js +459 -108
- package/src/parser.js +4 -5
- package/src/pipeline.js +605 -8
- package/src/run-state.js +5 -0
- package/src/test-adapters-runtime.js +69 -0
- package/src/test-boss-chat.js +399 -0
- package/src/test-index-async.js +238 -4
- package/src/test-parser.js +33 -6
- package/src/test-pipeline.js +408 -1
- package/vendor/boss-chat-cli/README.md +134 -0
- package/vendor/boss-chat-cli/package.json +53 -0
- package/vendor/boss-chat-cli/src/app.js +769 -0
- package/vendor/boss-chat-cli/src/browser/chat-page.js +2681 -0
- package/vendor/boss-chat-cli/src/cli.js +1350 -0
- package/vendor/boss-chat-cli/src/mcp/server.js +149 -0
- package/vendor/boss-chat-cli/src/mcp/tool-runtime.js +193 -0
- package/vendor/boss-chat-cli/src/runtime/async-run-state.js +260 -0
- package/vendor/boss-chat-cli/src/runtime/interaction.js +102 -0
- package/vendor/boss-chat-cli/src/runtime/run-control.js +102 -0
- package/vendor/boss-chat-cli/src/services/chrome-client.js +97 -0
- package/vendor/boss-chat-cli/src/services/llm.js +352 -0
- package/vendor/boss-chat-cli/src/services/profile-store.js +157 -0
- package/vendor/boss-chat-cli/src/services/report-store.js +19 -0
- package/vendor/boss-chat-cli/src/services/resume-capture.js +554 -0
- package/vendor/boss-chat-cli/src/services/state-store.js +217 -0
- package/vendor/boss-chat-cli/src/utils/customer-key.js +82 -0
- package/vendor/boss-recommend-screen-cli/boss-recommend-screen-cli.cjs +902 -56
- package/vendor/boss-recommend-screen-cli/test-recoverable-resume-failures.cjs +387 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
function timestampToken(date = new Date()) {
|
|
5
|
+
return date.toISOString().replace(/[:.]/g, '-');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class ReportStore {
|
|
9
|
+
constructor(baseDir) {
|
|
10
|
+
this.reportsDir = path.join(baseDir, 'reports');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async write(summary) {
|
|
14
|
+
await mkdir(this.reportsDir, { recursive: true });
|
|
15
|
+
const filePath = path.join(this.reportsDir, `run-${timestampToken()}.json`);
|
|
16
|
+
await writeFile(filePath, `${JSON.stringify(summary, null, 2)}\n`, 'utf8');
|
|
17
|
+
return filePath;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import sharp from 'sharp';
|
|
4
|
+
|
|
5
|
+
function sleep(ms) {
|
|
6
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function clamp(value, low, high) {
|
|
10
|
+
return Math.max(low, Math.min(high, value));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function browserProbeResumeContext(options = {}) {
|
|
14
|
+
const INIT = Boolean(options.init);
|
|
15
|
+
const TARGET_SCROLL =
|
|
16
|
+
typeof options.targetScroll === 'number' && Number.isFinite(options.targetScroll)
|
|
17
|
+
? options.targetScroll
|
|
18
|
+
: null;
|
|
19
|
+
|
|
20
|
+
const absRect = (el) => {
|
|
21
|
+
const rect = el.getBoundingClientRect();
|
|
22
|
+
let x = rect.left;
|
|
23
|
+
let y = rect.top;
|
|
24
|
+
let win = el.ownerDocument.defaultView;
|
|
25
|
+
while (win && win !== win.parent) {
|
|
26
|
+
const frameEl = win.frameElement;
|
|
27
|
+
if (!frameEl) break;
|
|
28
|
+
const frameRect = frameEl.getBoundingClientRect();
|
|
29
|
+
x += frameRect.left;
|
|
30
|
+
y += frameRect.top;
|
|
31
|
+
win = win.parent;
|
|
32
|
+
}
|
|
33
|
+
return { x, y, width: rect.width, height: rect.height };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const isVisible = (el) => {
|
|
37
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
38
|
+
const style = getComputedStyle(el);
|
|
39
|
+
if (style.display === 'none' || style.visibility === 'hidden' || Number(style.opacity || '1') < 0.01) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const rect = el.getBoundingClientRect();
|
|
43
|
+
return rect.width > 30 && rect.height > 30;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const canScroll = (el) => Boolean(el && (el.scrollHeight || 0) > (el.clientHeight || 0) + 8);
|
|
47
|
+
|
|
48
|
+
const chooseScrollableAncestor = (startEl) => {
|
|
49
|
+
const candidates = [];
|
|
50
|
+
let current = startEl;
|
|
51
|
+
let depth = 0;
|
|
52
|
+
while (current && depth < 24) {
|
|
53
|
+
if ((current.clientHeight || 0) > 40 && canScroll(current)) {
|
|
54
|
+
const style = getComputedStyle(current);
|
|
55
|
+
const overflowY = String(style.overflowY || '').toLowerCase();
|
|
56
|
+
const key = `${current.id || ''} ${current.className || ''}`.toLowerCase();
|
|
57
|
+
let score = 0;
|
|
58
|
+
if (/auto|scroll|overlay/.test(overflowY)) score += 1000;
|
|
59
|
+
if (key.includes('resume')) score += 500;
|
|
60
|
+
if (key.includes('detail')) score += 220;
|
|
61
|
+
score += Math.min(250, Math.floor(((current.scrollHeight || 0) - (current.clientHeight || 0)) / 2));
|
|
62
|
+
score -= depth * 10;
|
|
63
|
+
candidates.push({ el: current, score });
|
|
64
|
+
}
|
|
65
|
+
current = current.parentElement;
|
|
66
|
+
depth += 1;
|
|
67
|
+
}
|
|
68
|
+
if (candidates.length === 0) return null;
|
|
69
|
+
candidates.sort((left, right) => right.score - left.score);
|
|
70
|
+
return candidates[0].el;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const locateInlineResumeContainer = (scopes) => {
|
|
74
|
+
const selectors = [
|
|
75
|
+
'.resume-detail.resume-detail-chat.resume-content-wrap.iframe-resume-detail',
|
|
76
|
+
'.resume-content-wrap.iframe-resume-detail',
|
|
77
|
+
'.resume-content-wrap',
|
|
78
|
+
'.resume-common-wrap',
|
|
79
|
+
'.resume-recommend',
|
|
80
|
+
'.resume-detail',
|
|
81
|
+
'.resume-container .resume-content-wrap',
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
for (const scope of scopes) {
|
|
85
|
+
for (const selector of selectors) {
|
|
86
|
+
const found = scope.querySelector(selector);
|
|
87
|
+
if (found && isVisible(found)) {
|
|
88
|
+
return found;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return null;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const locateContext = () => {
|
|
97
|
+
const scopes = Array.from(
|
|
98
|
+
document.querySelectorAll(
|
|
99
|
+
'.dialog-wrap.active, .boss-popup__wrapper, .boss-dialog, .geek-detail-modal, .modal, .boss-popup_wrapper',
|
|
100
|
+
),
|
|
101
|
+
).filter(isVisible);
|
|
102
|
+
const allResumeFrames = Array.from(
|
|
103
|
+
document.querySelectorAll('iframe[src*="/web/frame/c-resume/"], iframe[name*="resume"]'),
|
|
104
|
+
);
|
|
105
|
+
const visibleResumeFrames = allResumeFrames.filter(isVisible);
|
|
106
|
+
|
|
107
|
+
let resumeFrame = null;
|
|
108
|
+
for (const scope of scopes) {
|
|
109
|
+
const found = scope.querySelector('iframe[src*="/web/frame/c-resume/"], iframe[name*="resume"]');
|
|
110
|
+
if (found && isVisible(found)) {
|
|
111
|
+
resumeFrame = found;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!resumeFrame) {
|
|
116
|
+
resumeFrame = visibleResumeFrames[0] || null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!resumeFrame) {
|
|
120
|
+
const inlineResumeContainer = locateInlineResumeContainer(scopes);
|
|
121
|
+
if (!inlineResumeContainer) {
|
|
122
|
+
return {
|
|
123
|
+
ok: false,
|
|
124
|
+
reason: 'NO_RESUME_IFRAME',
|
|
125
|
+
debug: {
|
|
126
|
+
scopeCount: scopes.length,
|
|
127
|
+
totalResumeIframes: allResumeFrames.length,
|
|
128
|
+
visibleResumeIframes: visibleResumeFrames.length,
|
|
129
|
+
inlineResumeFound: false,
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const inlineScroller =
|
|
135
|
+
chooseScrollableAncestor(inlineResumeContainer) ||
|
|
136
|
+
inlineResumeContainer;
|
|
137
|
+
if (!inlineScroller || !isVisible(inlineScroller)) {
|
|
138
|
+
return {
|
|
139
|
+
ok: false,
|
|
140
|
+
reason: 'NO_SCROLL_CONTAINER',
|
|
141
|
+
debug: {
|
|
142
|
+
scopeCount: scopes.length,
|
|
143
|
+
totalResumeIframes: allResumeFrames.length,
|
|
144
|
+
visibleResumeIframes: visibleResumeFrames.length,
|
|
145
|
+
inlineResumeFound: true,
|
|
146
|
+
scrollerFound: Boolean(inlineScroller),
|
|
147
|
+
scrollerVisible: Boolean(inlineScroller && isVisible(inlineScroller)),
|
|
148
|
+
scrollerClass: inlineScroller ? String(inlineScroller.className || '') : '',
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
ok: true,
|
|
155
|
+
mode: 'inline',
|
|
156
|
+
frame: null,
|
|
157
|
+
canvas: null,
|
|
158
|
+
scroller: inlineScroller,
|
|
159
|
+
clipEl: inlineScroller,
|
|
160
|
+
debug: {
|
|
161
|
+
scopeCount: scopes.length,
|
|
162
|
+
totalResumeIframes: allResumeFrames.length,
|
|
163
|
+
visibleResumeIframes: visibleResumeFrames.length,
|
|
164
|
+
inlineResumeFound: true,
|
|
165
|
+
inlineResumeClass: String(inlineResumeContainer.className || ''),
|
|
166
|
+
scrollerClass: String(inlineScroller.className || ''),
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const resumeDoc = resumeFrame.contentDocument;
|
|
172
|
+
const canvas = resumeDoc ? resumeDoc.querySelector('canvas#resume') || resumeDoc.querySelector('canvas') : null;
|
|
173
|
+
const scroller =
|
|
174
|
+
chooseScrollableAncestor(resumeFrame.parentElement || resumeFrame) ||
|
|
175
|
+
document.querySelector('.resume-detail-wrap') ||
|
|
176
|
+
chooseScrollableAncestor(resumeFrame) ||
|
|
177
|
+
resumeFrame.parentElement ||
|
|
178
|
+
resumeFrame;
|
|
179
|
+
|
|
180
|
+
if (!scroller || !isVisible(scroller)) {
|
|
181
|
+
return {
|
|
182
|
+
ok: false,
|
|
183
|
+
reason: 'NO_SCROLL_CONTAINER',
|
|
184
|
+
debug: {
|
|
185
|
+
scopeCount: scopes.length,
|
|
186
|
+
totalResumeIframes: allResumeFrames.length,
|
|
187
|
+
visibleResumeIframes: visibleResumeFrames.length,
|
|
188
|
+
resumeFrameSrc: String(resumeFrame.src || ''),
|
|
189
|
+
scrollerFound: Boolean(scroller),
|
|
190
|
+
scrollerVisible: Boolean(scroller && isVisible(scroller)),
|
|
191
|
+
scrollerClass: scroller ? String(scroller.className || '') : '',
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
ok: true,
|
|
198
|
+
mode: 'iframe',
|
|
199
|
+
frame: resumeFrame,
|
|
200
|
+
canvas,
|
|
201
|
+
scroller,
|
|
202
|
+
clipEl: scroller,
|
|
203
|
+
debug: {
|
|
204
|
+
resumeFrameSrc: String(resumeFrame.src || ''),
|
|
205
|
+
scrollerClass: String(scroller.className || ''),
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
if (
|
|
211
|
+
INIT ||
|
|
212
|
+
!window.__bossChatResumeCtx ||
|
|
213
|
+
!window.__bossChatResumeCtx.scroller ||
|
|
214
|
+
!window.__bossChatResumeCtx.scroller.isConnected
|
|
215
|
+
) {
|
|
216
|
+
const located = locateContext();
|
|
217
|
+
if (!located.ok) {
|
|
218
|
+
return located;
|
|
219
|
+
}
|
|
220
|
+
window.__bossChatResumeCtx = located;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const ctx = window.__bossChatResumeCtx;
|
|
224
|
+
if (typeof TARGET_SCROLL === 'number' && Number.isFinite(TARGET_SCROLL)) {
|
|
225
|
+
try {
|
|
226
|
+
ctx.scroller.scrollTop = TARGET_SCROLL;
|
|
227
|
+
if (typeof ctx.scroller.scrollTo === 'function') {
|
|
228
|
+
ctx.scroller.scrollTo({ top: TARGET_SCROLL, left: 0, behavior: 'instant' });
|
|
229
|
+
}
|
|
230
|
+
ctx.scroller.dispatchEvent(new Event('scroll', { bubbles: true }));
|
|
231
|
+
} catch {}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const scrollTop = Number(ctx.scroller.scrollTop || 0);
|
|
235
|
+
const scrollHeight = Number(ctx.scroller.scrollHeight || 0);
|
|
236
|
+
const clientHeight = Number(ctx.scroller.clientHeight || 0);
|
|
237
|
+
const maxScroll = Math.max(0, scrollHeight - clientHeight);
|
|
238
|
+
const clipRaw = absRect(ctx.clipEl);
|
|
239
|
+
const baseClipHeight = Math.max(
|
|
240
|
+
1,
|
|
241
|
+
Math.min(clipRaw.height, Number(ctx.scroller.clientHeight || clipRaw.height)),
|
|
242
|
+
);
|
|
243
|
+
const baseClipTop = Number(clipRaw.y || 0);
|
|
244
|
+
|
|
245
|
+
let noiseCutoffHeight = null;
|
|
246
|
+
try {
|
|
247
|
+
const noiseSelectors = [
|
|
248
|
+
'.resume-anonymous-geek-card.v2',
|
|
249
|
+
'.resume-anonymous-geek-card',
|
|
250
|
+
'.resume-anonymous-geek-card .card-container',
|
|
251
|
+
'.resume-warning',
|
|
252
|
+
];
|
|
253
|
+
const noiseNodes = Array.from(ctx.scroller.querySelectorAll(noiseSelectors.join(','))).filter(
|
|
254
|
+
(node) => node instanceof HTMLElement && isVisible(node),
|
|
255
|
+
);
|
|
256
|
+
for (const node of noiseNodes) {
|
|
257
|
+
const rect = absRect(node);
|
|
258
|
+
if (!(rect.width > 8 && rect.height > 8)) continue;
|
|
259
|
+
const relTop = rect.y - baseClipTop;
|
|
260
|
+
if (relTop <= 80) continue;
|
|
261
|
+
const candidateCutoff = Math.max(1, Math.floor(relTop - 6));
|
|
262
|
+
if (candidateCutoff < baseClipHeight) {
|
|
263
|
+
noiseCutoffHeight = candidateCutoff;
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
} catch {}
|
|
268
|
+
const finalClipHeight =
|
|
269
|
+
typeof noiseCutoffHeight === 'number' && Number.isFinite(noiseCutoffHeight)
|
|
270
|
+
? Math.max(1, Math.min(baseClipHeight, noiseCutoffHeight))
|
|
271
|
+
: baseClipHeight;
|
|
272
|
+
|
|
273
|
+
return {
|
|
274
|
+
ok: true,
|
|
275
|
+
mode: ctx.mode || 'unknown',
|
|
276
|
+
scrollTop,
|
|
277
|
+
scrollHeight,
|
|
278
|
+
clientHeight,
|
|
279
|
+
maxScroll,
|
|
280
|
+
clip: {
|
|
281
|
+
x: clipRaw.x,
|
|
282
|
+
y: clipRaw.y,
|
|
283
|
+
width: Math.max(1, Math.min(clipRaw.width, Number(ctx.scroller.clientWidth || clipRaw.width))),
|
|
284
|
+
height: finalClipHeight,
|
|
285
|
+
},
|
|
286
|
+
canvas: ctx.canvas
|
|
287
|
+
? {
|
|
288
|
+
width: Number(ctx.canvas.width || 0),
|
|
289
|
+
height: Number(ctx.canvas.height || 0),
|
|
290
|
+
}
|
|
291
|
+
: null,
|
|
292
|
+
debug: ctx.debug || {},
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async function stitchWithSharp(chunks, stitchedImage) {
|
|
297
|
+
const sorted = chunks
|
|
298
|
+
.map((chunk, index) => ({
|
|
299
|
+
...chunk,
|
|
300
|
+
index: Number.isInteger(chunk.index) ? chunk.index : index,
|
|
301
|
+
scrollTop: Number(chunk.scrollTop || 0),
|
|
302
|
+
clipHeightCss: Number(chunk.clipHeightCss || 0),
|
|
303
|
+
}))
|
|
304
|
+
.sort((left, right) => {
|
|
305
|
+
if (left.scrollTop !== right.scrollTop) return left.scrollTop - right.scrollTop;
|
|
306
|
+
return left.index - right.index;
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
const composites = [];
|
|
310
|
+
const used = [];
|
|
311
|
+
let outWidth = 1;
|
|
312
|
+
let outHeight = 0;
|
|
313
|
+
let prevChunk = null;
|
|
314
|
+
|
|
315
|
+
for (const chunk of sorted) {
|
|
316
|
+
const info = await sharp(chunk.file).metadata();
|
|
317
|
+
const width = Number(info?.width || 0);
|
|
318
|
+
const height = Number(info?.height || 0);
|
|
319
|
+
if (width <= 0 || height <= 0) {
|
|
320
|
+
throw new Error(`Invalid chunk image size: ${chunk.file}`);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (prevChunk) {
|
|
324
|
+
const deltaCss = chunk.scrollTop - prevChunk.scrollTop;
|
|
325
|
+
if (!(deltaCss > 0.5)) {
|
|
326
|
+
prevChunk = chunk;
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
const clipHeightCss = chunk.clipHeightCss > 1 ? chunk.clipHeightCss : prevChunk.clipHeightCss;
|
|
330
|
+
const ratio = clipHeightCss > 1 ? height / clipHeightCss : 1;
|
|
331
|
+
const newPixels = clamp(Math.round(deltaCss * ratio), 1, height);
|
|
332
|
+
const cropTop = clamp(height - newPixels, 0, height - 1);
|
|
333
|
+
const segHeight = height - cropTop;
|
|
334
|
+
const segment = await sharp(chunk.file)
|
|
335
|
+
.removeAlpha()
|
|
336
|
+
.extract({
|
|
337
|
+
left: 0,
|
|
338
|
+
top: cropTop,
|
|
339
|
+
width,
|
|
340
|
+
height: segHeight,
|
|
341
|
+
})
|
|
342
|
+
.png()
|
|
343
|
+
.toBuffer();
|
|
344
|
+
composites.push({
|
|
345
|
+
input: segment,
|
|
346
|
+
top: outHeight,
|
|
347
|
+
left: 0,
|
|
348
|
+
});
|
|
349
|
+
used.push({
|
|
350
|
+
file: chunk.file,
|
|
351
|
+
scrollTop: chunk.scrollTop,
|
|
352
|
+
cropTopPx: cropTop,
|
|
353
|
+
keptHeightPx: segHeight,
|
|
354
|
+
});
|
|
355
|
+
outWidth = Math.max(outWidth, width);
|
|
356
|
+
outHeight += segHeight;
|
|
357
|
+
prevChunk = chunk;
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const segment = await sharp(chunk.file).removeAlpha().png().toBuffer();
|
|
362
|
+
composites.push({
|
|
363
|
+
input: segment,
|
|
364
|
+
top: outHeight,
|
|
365
|
+
left: 0,
|
|
366
|
+
});
|
|
367
|
+
used.push({
|
|
368
|
+
file: chunk.file,
|
|
369
|
+
scrollTop: chunk.scrollTop,
|
|
370
|
+
cropTopPx: 0,
|
|
371
|
+
keptHeightPx: height,
|
|
372
|
+
});
|
|
373
|
+
outWidth = Math.max(outWidth, width);
|
|
374
|
+
outHeight += height;
|
|
375
|
+
prevChunk = chunk;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (composites.length === 0 || outHeight <= 0 || outWidth <= 0) {
|
|
379
|
+
throw new Error('No valid segments to stitch with sharp.');
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
await sharp({
|
|
383
|
+
create: {
|
|
384
|
+
width: outWidth,
|
|
385
|
+
height: outHeight,
|
|
386
|
+
channels: 3,
|
|
387
|
+
background: { r: 255, g: 255, b: 255 },
|
|
388
|
+
},
|
|
389
|
+
})
|
|
390
|
+
.composite(composites)
|
|
391
|
+
.png()
|
|
392
|
+
.toFile(stitchedImage);
|
|
393
|
+
|
|
394
|
+
return {
|
|
395
|
+
segments: composites.length,
|
|
396
|
+
size: {
|
|
397
|
+
width: outWidth,
|
|
398
|
+
height: outHeight,
|
|
399
|
+
},
|
|
400
|
+
used,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async function detectLikelyBlankImage(imagePath) {
|
|
405
|
+
const stats = await sharp(imagePath).stats();
|
|
406
|
+
const channels = stats?.channels || [];
|
|
407
|
+
if (channels.length < 3) {
|
|
408
|
+
return { likelyBlank: false, luma: 0, avgStd: 0 };
|
|
409
|
+
}
|
|
410
|
+
const meanR = Number(channels[0]?.mean || 0);
|
|
411
|
+
const meanG = Number(channels[1]?.mean || 0);
|
|
412
|
+
const meanB = Number(channels[2]?.mean || 0);
|
|
413
|
+
const stdR = Number(channels[0]?.stdev || 0);
|
|
414
|
+
const stdG = Number(channels[1]?.stdev || 0);
|
|
415
|
+
const stdB = Number(channels[2]?.stdev || 0);
|
|
416
|
+
const luma = 0.299 * meanR + 0.587 * meanG + 0.114 * meanB;
|
|
417
|
+
const avgStd = (stdR + stdG + stdB) / 3;
|
|
418
|
+
const likelyBlank = luma >= 244 && avgStd <= 9;
|
|
419
|
+
return {
|
|
420
|
+
likelyBlank,
|
|
421
|
+
luma: Number(luma.toFixed(2)),
|
|
422
|
+
avgStd: Number(avgStd.toFixed(2)),
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export class ResumeCaptureService {
|
|
427
|
+
constructor({ chromeClient, logger = console } = {}) {
|
|
428
|
+
this.chromeClient = chromeClient;
|
|
429
|
+
this.logger = logger;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async waitForProbe({ waitResumeMs = 30000, pollMs = 700 } = {}) {
|
|
433
|
+
const start = Date.now();
|
|
434
|
+
let lastProbe = null;
|
|
435
|
+
while (Date.now() - start < waitResumeMs) {
|
|
436
|
+
const probe = await this.chromeClient.callFunction(browserProbeResumeContext, {
|
|
437
|
+
init: true,
|
|
438
|
+
targetScroll: 0,
|
|
439
|
+
});
|
|
440
|
+
if (probe && typeof probe === 'object') {
|
|
441
|
+
lastProbe = probe;
|
|
442
|
+
}
|
|
443
|
+
if (probe?.ok && probe?.clip?.height > 80 && probe?.clip?.width > 120) {
|
|
444
|
+
return probe;
|
|
445
|
+
}
|
|
446
|
+
await sleep(pollMs);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const reason = lastProbe?.reason || 'UNKNOWN';
|
|
450
|
+
throw new Error(`Resume context probe timeout: reason=${reason}`);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
async captureResume({ artifactDir, waitResumeMs = 30000, scrollSettleMs = 500 } = {}) {
|
|
454
|
+
if (!artifactDir) {
|
|
455
|
+
throw new Error('artifactDir is required for resume capture');
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
await mkdir(artifactDir, { recursive: true });
|
|
459
|
+
const chunkDir = path.join(artifactDir, 'chunks');
|
|
460
|
+
await mkdir(chunkDir, { recursive: true });
|
|
461
|
+
const metadataFile = path.join(artifactDir, 'chunks.json');
|
|
462
|
+
const stitchedImage = path.join(artifactDir, 'resume.png');
|
|
463
|
+
|
|
464
|
+
const probe = await this.waitForProbe({ waitResumeMs });
|
|
465
|
+
const maxScroll = Math.max(0, Number(probe.maxScroll || 0));
|
|
466
|
+
const step = Math.max(120, Math.floor(Number(probe.clientHeight || probe.clip?.height || 800)));
|
|
467
|
+
const positions = [];
|
|
468
|
+
for (let pos = 0; pos <= maxScroll; pos += step) {
|
|
469
|
+
positions.push(Math.min(pos, maxScroll));
|
|
470
|
+
}
|
|
471
|
+
if (positions.length === 0 || positions[positions.length - 1] !== maxScroll) {
|
|
472
|
+
positions.push(maxScroll);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const uniquePositions = [...new Set(positions.map((value) => Math.round(value)))].sort((a, b) => a - b);
|
|
476
|
+
const chunks = [];
|
|
477
|
+
const seenScroll = [];
|
|
478
|
+
|
|
479
|
+
for (let index = 0; index < uniquePositions.length; index += 1) {
|
|
480
|
+
const targetScroll = uniquePositions[index];
|
|
481
|
+
await this.chromeClient.callFunction(browserProbeResumeContext, {
|
|
482
|
+
init: false,
|
|
483
|
+
targetScroll,
|
|
484
|
+
});
|
|
485
|
+
await sleep(scrollSettleMs);
|
|
486
|
+
|
|
487
|
+
const current = await this.chromeClient.callFunction(browserProbeResumeContext, {
|
|
488
|
+
init: false,
|
|
489
|
+
targetScroll: null,
|
|
490
|
+
});
|
|
491
|
+
if (!current?.ok) continue;
|
|
492
|
+
|
|
493
|
+
const actualScroll = Number(current.scrollTop || 0);
|
|
494
|
+
if (seenScroll.some((value) => Math.abs(value - actualScroll) < 1)) {
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const clip = current.clip || {};
|
|
499
|
+
const width = Number(clip.width || 0);
|
|
500
|
+
const height = Number(clip.height || 0);
|
|
501
|
+
if (width < 50 || height < 50) {
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const shot = await this.chromeClient.Page.captureScreenshot({
|
|
506
|
+
format: 'png',
|
|
507
|
+
captureBeyondViewport: true,
|
|
508
|
+
clip: {
|
|
509
|
+
x: Number(clip.x.toFixed(2)),
|
|
510
|
+
y: Number(clip.y.toFixed(2)),
|
|
511
|
+
width: Number(width.toFixed(2)),
|
|
512
|
+
height: Number(height.toFixed(2)),
|
|
513
|
+
scale: 1,
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
const file = path.resolve(chunkDir, `chunk_${String(chunks.length).padStart(3, '0')}.png`);
|
|
517
|
+
await writeFile(file, Buffer.from(shot.data, 'base64'));
|
|
518
|
+
seenScroll.push(actualScroll);
|
|
519
|
+
chunks.push({
|
|
520
|
+
index: chunks.length,
|
|
521
|
+
file,
|
|
522
|
+
scrollTop: actualScroll,
|
|
523
|
+
clipHeightCss: height,
|
|
524
|
+
clipWidthCss: width,
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if (chunks.length === 0) {
|
|
529
|
+
throw new Error('No screenshot chunks captured from resume modal');
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const metadata = {
|
|
533
|
+
createdAt: new Date().toISOString(),
|
|
534
|
+
probe,
|
|
535
|
+
chunks,
|
|
536
|
+
};
|
|
537
|
+
await writeFile(metadataFile, `${JSON.stringify(metadata, null, 2)}\n`, 'utf8');
|
|
538
|
+
const stitched = await stitchWithSharp(chunks, stitchedImage);
|
|
539
|
+
const blank = await detectLikelyBlankImage(stitchedImage);
|
|
540
|
+
this.logger.log(
|
|
541
|
+
`简历截图完成: chunks=${chunks.length}, stitched=${stitchedImage}, size=${stitched.size.width}x${stitched.size.height}, likelyBlank=${blank.likelyBlank}, luma=${blank.luma}, std=${blank.avgStd}`,
|
|
542
|
+
);
|
|
543
|
+
|
|
544
|
+
return {
|
|
545
|
+
stitchedImage,
|
|
546
|
+
metadataFile,
|
|
547
|
+
chunkDir,
|
|
548
|
+
chunkCount: chunks.length,
|
|
549
|
+
stitchEngine: 'sharp',
|
|
550
|
+
stitched,
|
|
551
|
+
quality: blank,
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
}
|