@danceiny/gotry 0.0.1-rc.11 → 0.0.1-rc.13
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 +111 -146
- package/cordis.gotry-patch.yml +14 -7
- package/dist/capabilities/flyai.js +135 -30
- package/dist/capabilities/hbcli.js +14 -2
- package/dist/capabilities/session/benchmark.js +252 -0
- package/dist/capabilities/session/transport.js +25 -10
- package/dist/capabilities/session-consent.js +82 -0
- package/dist/capabilities/session-login.js +119 -0
- package/dist/capabilities/session-search.js +7 -4
- package/dist/capabilities/weather.js +72 -17
- package/dist/scripts/agent-reach-wrapper-tests.js +4 -0
- package/dist/scripts/async-collect.js +33 -5
- package/dist/scripts/hbcli-tests.js +25 -4
- package/dist/scripts/ledger-tests.js +134 -4
- package/dist/scripts/memory-value-report.js +379 -0
- package/dist/scripts/product-metrics.js +569 -0
- package/dist/scripts/session-benchmark.js +261 -0
- package/dist/scripts/session-login.js +28 -0
- package/dist/scripts/session-tests.js +262 -23
- package/dist/scripts/smoke.js +112 -14
- package/dist/scripts/weather-tests.js +8 -1
- package/dist/src/index.js +124 -5
- package/dist/src/loop.js +65 -14
- package/dist/src/state-ledger.js +30 -4
- package/package.json +4 -2
- package/ts/capabilities/flyai.ts +177 -22
- package/ts/capabilities/hbcli.ts +16 -4
- package/ts/capabilities/session/benchmark.ts +273 -0
- package/ts/capabilities/session/transport.ts +41 -9
- package/ts/capabilities/session-consent.ts +127 -0
- package/ts/capabilities/session-login.ts +146 -0
- package/ts/capabilities/session-search.ts +13 -5
- package/ts/capabilities/weather.ts +84 -19
- package/ts/scripts/async-collect.ts +48 -7
- package/ts/src/index.ts +107 -11
- package/ts/src/loop.ts +85 -12
- package/ts/src/state-ledger.ts +48 -4
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
export const SESSION_BENCHMARK_SCHEMA_VERSION = 'session-double-source.v1';
|
|
2
|
+
export const SESSION_FIELD_ACCURACY_THRESHOLD = 0.9;
|
|
3
|
+
export const REQUIRED_COMPARABLE_FIELDS = [
|
|
4
|
+
'query_id',
|
|
5
|
+
'route_segments',
|
|
6
|
+
'journey_type',
|
|
7
|
+
'route_segments[].departure_at',
|
|
8
|
+
'route_segments[].arrival_at',
|
|
9
|
+
'route_segments[].transport_number',
|
|
10
|
+
'currency',
|
|
11
|
+
'price',
|
|
12
|
+
'source',
|
|
13
|
+
'fetched_at',
|
|
14
|
+
'verdict'
|
|
15
|
+
];
|
|
16
|
+
function isMissing(value) {
|
|
17
|
+
return value === undefined || value === null || typeof value === 'string' && value.trim() === '';
|
|
18
|
+
}
|
|
19
|
+
function sameValue(left, right) {
|
|
20
|
+
if (typeof left !== typeof right) return false;
|
|
21
|
+
return typeof left === 'number' && typeof right === 'number' ? Object.is(left, right) : left === right;
|
|
22
|
+
}
|
|
23
|
+
function sameComparison(row) {
|
|
24
|
+
if (/\.(?:departure_at|arrival_at)$/.test(row.path) && typeof row.expected === 'string' && typeof row.actual === 'string') {
|
|
25
|
+
const expectedAt = Date.parse(row.expected);
|
|
26
|
+
const actualAt = Date.parse(row.actual);
|
|
27
|
+
if (Number.isFinite(expectedAt) && Number.isFinite(actualAt)) return expectedAt === actualAt;
|
|
28
|
+
}
|
|
29
|
+
return sameValue(row.expected, row.actual);
|
|
30
|
+
}
|
|
31
|
+
function fixtureComparisons(expected, actual) {
|
|
32
|
+
const rows = [
|
|
33
|
+
{
|
|
34
|
+
path: 'query_id',
|
|
35
|
+
expected: expected.query_id,
|
|
36
|
+
actual: actual.query_id
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
path: 'route_segments.length',
|
|
40
|
+
expected: expected.route_segments.length,
|
|
41
|
+
actual: actual.route_segments?.length
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
path: 'journey_type',
|
|
45
|
+
expected: expected.journey_type,
|
|
46
|
+
actual: actual.journey_type
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
path: 'currency',
|
|
50
|
+
expected: expected.currency,
|
|
51
|
+
actual: actual.currency
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
path: 'price',
|
|
55
|
+
expected: expected.price,
|
|
56
|
+
actual: actual.price
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
path: 'source',
|
|
60
|
+
expected: expected.source,
|
|
61
|
+
actual: actual.source
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
path: 'fetched_at',
|
|
65
|
+
expected: expected.fetched_at,
|
|
66
|
+
actual: actual.fetched_at
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
path: 'verdict',
|
|
70
|
+
expected: expected.verdict,
|
|
71
|
+
actual: actual.verdict
|
|
72
|
+
}
|
|
73
|
+
];
|
|
74
|
+
for(let index = 0; index < expected.route_segments.length; index += 1){
|
|
75
|
+
const exp = expected.route_segments[index];
|
|
76
|
+
const act = actual.route_segments?.[index];
|
|
77
|
+
rows.push({
|
|
78
|
+
path: `route_segments[${index}].from`,
|
|
79
|
+
expected: exp.from,
|
|
80
|
+
actual: act?.from
|
|
81
|
+
}, {
|
|
82
|
+
path: `route_segments[${index}].to`,
|
|
83
|
+
expected: exp.to,
|
|
84
|
+
actual: act?.to
|
|
85
|
+
}, {
|
|
86
|
+
path: `route_segments[${index}].departure_at`,
|
|
87
|
+
expected: exp.departure_at,
|
|
88
|
+
actual: act?.departure_at
|
|
89
|
+
}, {
|
|
90
|
+
path: `route_segments[${index}].arrival_at`,
|
|
91
|
+
expected: exp.arrival_at,
|
|
92
|
+
actual: act?.arrival_at
|
|
93
|
+
}, {
|
|
94
|
+
path: `route_segments[${index}].transport_number`,
|
|
95
|
+
expected: exp.transport_number,
|
|
96
|
+
actual: act?.transport_number
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return rows;
|
|
100
|
+
}
|
|
101
|
+
export function scoreSessionFixture(expected, actual, threshold = SESSION_FIELD_ACCURACY_THRESHOLD) {
|
|
102
|
+
const rows = fixtureComparisons(expected, actual);
|
|
103
|
+
const fixtureErrors = [
|
|
104
|
+
...rows.filter((row)=>isMissing(row.expected)).map((row)=>row.path),
|
|
105
|
+
...requiredMissing(expected)
|
|
106
|
+
];
|
|
107
|
+
const missing = [];
|
|
108
|
+
const incorrect = [];
|
|
109
|
+
let correct = 0;
|
|
110
|
+
for (const row of rows){
|
|
111
|
+
if (isMissing(row.actual)) {
|
|
112
|
+
missing.push(row.path);
|
|
113
|
+
} else if (!sameValue(row.expected, row.actual)) {
|
|
114
|
+
incorrect.push(row.path);
|
|
115
|
+
} else {
|
|
116
|
+
correct += 1;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const total = rows.length;
|
|
120
|
+
const accuracy = total > 0 ? correct / total : 0;
|
|
121
|
+
return {
|
|
122
|
+
pass: fixtureErrors.length === 0 && accuracy >= threshold,
|
|
123
|
+
threshold,
|
|
124
|
+
correct,
|
|
125
|
+
total,
|
|
126
|
+
accuracy,
|
|
127
|
+
missing,
|
|
128
|
+
incorrect,
|
|
129
|
+
fixture_errors: [
|
|
130
|
+
...new Set(fixtureErrors)
|
|
131
|
+
]
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function isIsoTimestamp(value) {
|
|
135
|
+
return value.trim() !== '' && Number.isFinite(Date.parse(value));
|
|
136
|
+
}
|
|
137
|
+
function requiredMissing(record) {
|
|
138
|
+
const missing = [];
|
|
139
|
+
if (!record.query_id.trim()) missing.push('query_id');
|
|
140
|
+
if (record.route_segments.length === 0) missing.push('route_segments');
|
|
141
|
+
if (!record.currency.trim()) missing.push('currency');
|
|
142
|
+
if (!(record.price > 0)) missing.push('price');
|
|
143
|
+
if (!record.source.trim()) missing.push('source');
|
|
144
|
+
if (!isIsoTimestamp(record.fetched_at)) missing.push('fetched_at');
|
|
145
|
+
if (record.journey_type === 'direct' && record.route_segments.length !== 1) missing.push('journey_type/route_segments');
|
|
146
|
+
if (record.journey_type === 'transfer' && record.route_segments.length < 2) missing.push('journey_type/route_segments');
|
|
147
|
+
for(let index = 0; index < record.route_segments.length; index += 1){
|
|
148
|
+
const segment = record.route_segments[index];
|
|
149
|
+
if (!segment.from.trim()) missing.push(`route_segments[${index}].from`);
|
|
150
|
+
if (!segment.to.trim()) missing.push(`route_segments[${index}].to`);
|
|
151
|
+
if (!isIsoTimestamp(segment.departure_at)) missing.push(`route_segments[${index}].departure_at`);
|
|
152
|
+
if (!isIsoTimestamp(segment.arrival_at)) missing.push(`route_segments[${index}].arrival_at`);
|
|
153
|
+
if (!segment.transport_number.trim()) missing.push(`route_segments[${index}].transport_number`);
|
|
154
|
+
}
|
|
155
|
+
return missing;
|
|
156
|
+
}
|
|
157
|
+
function baseEvaluation(state, quotaDisposition, extra = {}) {
|
|
158
|
+
return {
|
|
159
|
+
state,
|
|
160
|
+
retry_allowed: false,
|
|
161
|
+
quota_disposition: quotaDisposition,
|
|
162
|
+
mismatches: extra.mismatches ?? [],
|
|
163
|
+
missing: extra.missing ?? [],
|
|
164
|
+
...extra.price_delta === undefined ? {} : {
|
|
165
|
+
price_delta: extra.price_delta
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function alignmentComparisons(official, session) {
|
|
170
|
+
const rows = [
|
|
171
|
+
{
|
|
172
|
+
path: 'query_id',
|
|
173
|
+
expected: official.query_id,
|
|
174
|
+
actual: session.query_id
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
path: 'route_segments.length',
|
|
178
|
+
expected: official.route_segments.length,
|
|
179
|
+
actual: session.route_segments.length
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
path: 'journey_type',
|
|
183
|
+
expected: official.journey_type,
|
|
184
|
+
actual: session.journey_type
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
path: 'currency',
|
|
188
|
+
expected: official.currency,
|
|
189
|
+
actual: session.currency
|
|
190
|
+
}
|
|
191
|
+
];
|
|
192
|
+
const comparableSegments = Math.min(official.route_segments.length, session.route_segments.length);
|
|
193
|
+
for(let index = 0; index < comparableSegments; index += 1){
|
|
194
|
+
const exp = official.route_segments[index];
|
|
195
|
+
const act = session.route_segments[index];
|
|
196
|
+
rows.push({
|
|
197
|
+
path: `route_segments[${index}].from`,
|
|
198
|
+
expected: exp.from,
|
|
199
|
+
actual: act.from
|
|
200
|
+
}, {
|
|
201
|
+
path: `route_segments[${index}].to`,
|
|
202
|
+
expected: exp.to,
|
|
203
|
+
actual: act.to
|
|
204
|
+
}, {
|
|
205
|
+
path: `route_segments[${index}].departure_at`,
|
|
206
|
+
expected: exp.departure_at,
|
|
207
|
+
actual: act.departure_at
|
|
208
|
+
}, {
|
|
209
|
+
path: `route_segments[${index}].arrival_at`,
|
|
210
|
+
expected: exp.arrival_at,
|
|
211
|
+
actual: act.arrival_at
|
|
212
|
+
}, {
|
|
213
|
+
path: `route_segments[${index}].transport_number`,
|
|
214
|
+
expected: exp.transport_number,
|
|
215
|
+
actual: act.transport_number
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
return rows;
|
|
219
|
+
}
|
|
220
|
+
export function evaluateDoubleSource(input) {
|
|
221
|
+
const official = input.official;
|
|
222
|
+
const session = input.session;
|
|
223
|
+
if (session?.verdict === 'challenged' || official?.verdict === 'challenged') {
|
|
224
|
+
return baseEvaluation('challenge_stop', 'no_spend_stop');
|
|
225
|
+
}
|
|
226
|
+
if ((session?.read_guard_blocked ?? 0) !== 0 || (official?.read_guard_blocked ?? 0) !== 0) {
|
|
227
|
+
return baseEvaluation('guard_violation', 'no_spend_stop');
|
|
228
|
+
}
|
|
229
|
+
if (!session) return baseEvaluation('source_unavailable', 'no_spend_stop');
|
|
230
|
+
if (session.verdict === 'needs-attach') return baseEvaluation('waiting_attach', 'no_spend_waiting_user');
|
|
231
|
+
if (session.verdict === 'needs-login') return baseEvaluation('waiting_login', 'no_spend_waiting_user');
|
|
232
|
+
if (!official || official.verdict !== 'hit' || session.verdict !== 'hit') {
|
|
233
|
+
return baseEvaluation('source_unavailable', 'no_spend_stop');
|
|
234
|
+
}
|
|
235
|
+
const missing = [
|
|
236
|
+
...requiredMissing(official).map((path)=>`official.${path}`),
|
|
237
|
+
...requiredMissing(session).map((path)=>`session.${path}`)
|
|
238
|
+
];
|
|
239
|
+
if (missing.length > 0) {
|
|
240
|
+
return baseEvaluation('invalid_contract', 'no_spend_stop', {
|
|
241
|
+
missing
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
const mismatches = alignmentComparisons(official, session).filter((row)=>!sameComparison(row)).map((row)=>row.path);
|
|
245
|
+
return baseEvaluation(mismatches.length === 0 ? 'comparable' : 'divergent', 'evidence_ready', {
|
|
246
|
+
mismatches,
|
|
247
|
+
price_delta: session.price - official.price
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
//# sourceURL=/Users/bytedance/work/gotry/ts/capabilities/session/benchmark.ts
|
|
@@ -76,22 +76,37 @@ export async function openSession(opts = {}) {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
let guard;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
79
|
+
if (opts.guard !== false) {
|
|
80
|
+
try {
|
|
81
|
+
guard = await attachReadGuardPuppeteer(browser, opts.auditPath);
|
|
82
|
+
} catch (e) {
|
|
83
|
+
await (isCdp ? browser.disconnect() : browser.close()).catch(()=>{});
|
|
84
|
+
return {
|
|
85
|
+
ok: false,
|
|
86
|
+
summary: `read-guard attach failed: ${e instanceof Error ? e.message.split('\n')[0] : String(e)}`
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
let ownPage = false;
|
|
91
|
+
let page;
|
|
92
|
+
if (opts.newPage) {
|
|
93
|
+
page = await browser.newPage();
|
|
94
|
+
ownPage = true;
|
|
95
|
+
} else {
|
|
96
|
+
page = (await browser.pages())[0] ?? await browser.newPage();
|
|
87
97
|
}
|
|
88
|
-
const page = (await browser.pages())[0] ?? await browser.newPage();
|
|
89
98
|
return {
|
|
90
99
|
ok: true,
|
|
91
100
|
browser,
|
|
92
101
|
page,
|
|
93
|
-
guard
|
|
102
|
+
guard: guard ?? {
|
|
103
|
+
blockedCount: ()=>0,
|
|
104
|
+
requestCount: ()=>0
|
|
105
|
+
},
|
|
94
106
|
close: async ()=>{
|
|
107
|
+
if (ownPage && (opts.closeOwnPage ?? true)) {
|
|
108
|
+
await page.close().catch(()=>{});
|
|
109
|
+
}
|
|
95
110
|
await (isCdp ? browser.disconnect() : browser.close()).catch(()=>{});
|
|
96
111
|
}
|
|
97
112
|
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export const ACCOUNT_TOOLS = {
|
|
2
|
+
gotry_session_search: 'ctrip-flight'
|
|
3
|
+
};
|
|
4
|
+
const SITE_LABEL = {
|
|
5
|
+
'ctrip-flight': '携程机票'
|
|
6
|
+
};
|
|
7
|
+
function reasonFor(toolName, site) {
|
|
8
|
+
return `${toolName} 将使用你本人已登录的浏览器会话做「${SITE_LABEL[site] ?? site}」只读检索` + '(ReadGuard 物理只读:写请求网络层中止,agent 永不碰凭证与验证码);本次批准在你本会话内有效';
|
|
9
|
+
}
|
|
10
|
+
export function createConsentGate(opts) {
|
|
11
|
+
const store = opts.store ?? new WeakMap();
|
|
12
|
+
const approvalOf = opts.approval ?? (()=>undefined);
|
|
13
|
+
return async (exec, next)=>{
|
|
14
|
+
const site = exec.name && ACCOUNT_TOOLS[exec.name];
|
|
15
|
+
if (!site) return next();
|
|
16
|
+
if ((opts.access() ?? 'ask') === 'off') {
|
|
17
|
+
return {
|
|
18
|
+
kind: 'deny',
|
|
19
|
+
reason: `${exec.name} 已被配置关闭(sessionAccess=off);需要账号会话检索时由用户开启`
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const state = exec.agent && store.get(exec.agent) || undefined;
|
|
23
|
+
if (state?.denied.has(site)) {
|
|
24
|
+
return {
|
|
25
|
+
kind: 'deny',
|
|
26
|
+
reason: `你在本会话已拒绝过「${SITE_LABEL[site] ?? site}」的账号会话检索——本会话不再请求授权,请改走其他工具推进`
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
if (state?.granted.has(site) || opts.access() === 'allow' && exec.agent) return next();
|
|
30
|
+
const approval = opts.approval?.();
|
|
31
|
+
if (!approval || !exec.agent) return {
|
|
32
|
+
kind: 'ask',
|
|
33
|
+
reason: reasonFor(exec.name ?? '', site)
|
|
34
|
+
};
|
|
35
|
+
let outcome;
|
|
36
|
+
try {
|
|
37
|
+
outcome = await approval.request({
|
|
38
|
+
agent: exec.agent,
|
|
39
|
+
toolName: exec.name ?? '',
|
|
40
|
+
callId: exec.callId,
|
|
41
|
+
reason: reasonFor(exec.name ?? '', site)
|
|
42
|
+
});
|
|
43
|
+
} catch {
|
|
44
|
+
outcome = 'unavailable';
|
|
45
|
+
}
|
|
46
|
+
if (outcome === 'allowed-once') {
|
|
47
|
+
remember(store, exec.agent, site, 'grant');
|
|
48
|
+
return next();
|
|
49
|
+
}
|
|
50
|
+
if (outcome === 'rejected' || outcome === 'cancelled') {
|
|
51
|
+
remember(store, exec.agent, site, 'deny');
|
|
52
|
+
return {
|
|
53
|
+
kind: 'deny',
|
|
54
|
+
reason: `你拒绝了 ${exec.name} 的账号会话授权(本会话内生效);不再重复请求,请改走其他工具推进`
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
kind: 'deny',
|
|
59
|
+
reason: `${exec.name} 需要你授权,但当前没有可用的审批通道(headless 一问一答无审批界面;请在 web 会话中使用账号会话检索)`
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function remember(store, agent, site, mode) {
|
|
64
|
+
const prev = store.get(agent) ?? {
|
|
65
|
+
granted: new Set(),
|
|
66
|
+
denied: new Set()
|
|
67
|
+
};
|
|
68
|
+
if (mode === 'grant') prev.granted.add(site);
|
|
69
|
+
else prev.denied.add(site);
|
|
70
|
+
store.set(agent, prev);
|
|
71
|
+
}
|
|
72
|
+
export function approvalFromContext(ctx) {
|
|
73
|
+
return ()=>{
|
|
74
|
+
const get = ctx.get;
|
|
75
|
+
if (typeof get !== 'function') return undefined;
|
|
76
|
+
const approval = get.call(ctx, 'approval');
|
|
77
|
+
return approval && typeof approval.request === 'function' ? approval : undefined;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
//# sourceURL=/Users/bytedance/work/gotry/ts/capabilities/session-consent.ts
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { openSession } from './session/transport.js';
|
|
2
|
+
export const LOGIN_TARGETS = {
|
|
3
|
+
'ctrip-flight': {
|
|
4
|
+
domain: 'ctrip.com',
|
|
5
|
+
names: [
|
|
6
|
+
'cticket',
|
|
7
|
+
'uid',
|
|
8
|
+
'uname',
|
|
9
|
+
'passport'
|
|
10
|
+
],
|
|
11
|
+
label: '携程机票',
|
|
12
|
+
entryUrl: 'https://flights.ctrip.com/'
|
|
13
|
+
},
|
|
14
|
+
'meituan-hotel': {
|
|
15
|
+
domain: 'meituan.com',
|
|
16
|
+
names: [
|
|
17
|
+
'lt',
|
|
18
|
+
'u',
|
|
19
|
+
'token',
|
|
20
|
+
'n'
|
|
21
|
+
],
|
|
22
|
+
label: '美团酒店',
|
|
23
|
+
entryUrl: 'https://hotel.meituan.com/'
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function err(site, verdict, error, started, ts) {
|
|
27
|
+
return {
|
|
28
|
+
ok: false,
|
|
29
|
+
via: 'session-login-error',
|
|
30
|
+
evidence: `[会话:login-error@${ts}] ${error.slice(0, 200)}`,
|
|
31
|
+
latencyMs: Date.now() - started,
|
|
32
|
+
verdict,
|
|
33
|
+
site,
|
|
34
|
+
error: error.slice(0, 200)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export async function pollTicketNames(browser, target) {
|
|
38
|
+
const cookies = await browser.cookies().catch(()=>[]);
|
|
39
|
+
if (!Array.isArray(cookies)) return [];
|
|
40
|
+
return cookies.filter((c)=>(c.domain ?? '').includes(target.domain) && c.name != null && target.names.includes(c.name)).map((c)=>c.name);
|
|
41
|
+
}
|
|
42
|
+
export async function sessionLogin(q = {}) {
|
|
43
|
+
const started = Date.now();
|
|
44
|
+
const ts = new Date().toISOString();
|
|
45
|
+
const site = q.site ?? 'ctrip-flight';
|
|
46
|
+
const target = LOGIN_TARGETS[site];
|
|
47
|
+
if (!target) {
|
|
48
|
+
return err(site, 'error', `未知站点 ${site}(可选 ${Object.keys(LOGIN_TARGETS).join('/')})`, started, ts);
|
|
49
|
+
}
|
|
50
|
+
const t = await openSession({
|
|
51
|
+
mode: 'cdp',
|
|
52
|
+
guard: false
|
|
53
|
+
});
|
|
54
|
+
if (!t.ok) {
|
|
55
|
+
const needsAttach = /chrome:\/\/inspect|DevToolsActivePort|cdp attach 失败/.test(t.summary);
|
|
56
|
+
return err(site, needsAttach ? 'needs-attach' : 'error', needsAttach ? `${t.summary};开启方法:你的 Chrome 打开 chrome://inspect/#remote-debugging → 打开开关(Chrome 144+,一次性),开启后直接再说一声即可` : t.summary, started, ts);
|
|
57
|
+
}
|
|
58
|
+
const evidenceTag = `[会话:${site}-login@${ts}]`;
|
|
59
|
+
try {
|
|
60
|
+
const pre = await pollTicketNames(t.browser, target);
|
|
61
|
+
if (pre.length > 0) {
|
|
62
|
+
await t.close();
|
|
63
|
+
return {
|
|
64
|
+
ok: true,
|
|
65
|
+
via: 'session-login',
|
|
66
|
+
latencyMs: Date.now() - started,
|
|
67
|
+
verdict: 'logged-in',
|
|
68
|
+
site,
|
|
69
|
+
tickets: pre,
|
|
70
|
+
evidence: `${evidenceTag} 自动检测:票据 cookie 已在(先前登录已生效)——[${pre.join(', ')}](只读名字,0 网页交互)`
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const page = await t.browser.newPage();
|
|
74
|
+
await page.bringToFront().catch(()=>{});
|
|
75
|
+
try {
|
|
76
|
+
await page.goto(target.entryUrl, {
|
|
77
|
+
waitUntil: 'domcontentloaded',
|
|
78
|
+
timeout: 30_000
|
|
79
|
+
});
|
|
80
|
+
} catch {}
|
|
81
|
+
console.log(`[session-login] ${target.label} 登录入口已在新标签页置前打开`);
|
|
82
|
+
const waitMs = Math.min(Math.max(q.waitMs ?? 90_000, 0), 300_000);
|
|
83
|
+
const pollMs = Math.min(Math.max(q.pollMs ?? 3_000, 500), 10_000);
|
|
84
|
+
const deadline = Date.now() + waitMs;
|
|
85
|
+
let tickets = [];
|
|
86
|
+
while(Date.now() < deadline){
|
|
87
|
+
await new Promise((resolve)=>setTimeout(resolve, pollMs));
|
|
88
|
+
tickets = await pollTicketNames(t.browser, target);
|
|
89
|
+
if (tickets.length > 0) break;
|
|
90
|
+
}
|
|
91
|
+
await t.close();
|
|
92
|
+
if (tickets.length > 0) {
|
|
93
|
+
return {
|
|
94
|
+
ok: true,
|
|
95
|
+
via: 'session-login',
|
|
96
|
+
latencyMs: Date.now() - started,
|
|
97
|
+
verdict: 'logged-in',
|
|
98
|
+
site,
|
|
99
|
+
tickets,
|
|
100
|
+
evidence: `${evidenceTag} 票据 cookie 已检出 [${tickets.join(', ')}](只读名字;登录在你自己的浏览器里完成,gotry 全程未接触任何密码/验证码/cookie 值)`
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
ok: true,
|
|
105
|
+
via: 'session-login',
|
|
106
|
+
latencyMs: Date.now() - started,
|
|
107
|
+
verdict: 'pending',
|
|
108
|
+
site,
|
|
109
|
+
tickets: [],
|
|
110
|
+
evidence: `${evidenceTag} 登录入口已在你的 Chrome 打开(${target.label});在标签页里正常登录完成后再说一声「继续查」即可——gotry 只检查"是否已登录",永不收集你的账号信息`
|
|
111
|
+
};
|
|
112
|
+
} catch (e) {
|
|
113
|
+
await t.close().catch(()=>{});
|
|
114
|
+
return err(site, 'error', e instanceof Error ? e.message : String(e), started, ts);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
//# sourceURL=/Users/bytedance/work/gotry/ts/capabilities/session-login.ts
|
|
@@ -6,6 +6,9 @@ export function __resetRateLimiterForTest() {
|
|
|
6
6
|
lastCallAt.clear();
|
|
7
7
|
}
|
|
8
8
|
const CHALLENGE_RE = /验证|滑块|captcha|verify/i;
|
|
9
|
+
export function classifyTransportFailure(summary, cdpMode) {
|
|
10
|
+
return cdpMode && /日常 Chrome 未开调试端口|cdp attach 失败/.test(summary) ? 'needs-attach' : 'error';
|
|
11
|
+
}
|
|
9
12
|
export async function sessionFlightSearch(q) {
|
|
10
13
|
const started = Date.now();
|
|
11
14
|
const ts = new Date().toISOString();
|
|
@@ -31,11 +34,11 @@ export async function sessionFlightSearch(q) {
|
|
|
31
34
|
profileDir: q.profileDir,
|
|
32
35
|
headless: q.headless,
|
|
33
36
|
auditPath: q.auditPath,
|
|
34
|
-
mode: q.profileDir ? 'persistent' : 'cdp'
|
|
37
|
+
mode: q.profileDir ? 'persistent' : 'cdp',
|
|
38
|
+
newPage: true
|
|
35
39
|
});
|
|
36
40
|
if (!t.ok) {
|
|
37
|
-
|
|
38
|
-
return err('error', t.summary);
|
|
41
|
+
return err(classifyTransportFailure(t.summary, q.profileDir === undefined), t.summary);
|
|
39
42
|
}
|
|
40
43
|
try {
|
|
41
44
|
const loggedIn = async ()=>{
|
|
@@ -43,7 +46,7 @@ export async function sessionFlightSearch(q) {
|
|
|
43
46
|
return cookies.some((c)=>c.domain.includes(SITE_DOMAIN.replace(/^\./, '')) && LOGIN_COOKIE_NAMES.includes(c.name));
|
|
44
47
|
};
|
|
45
48
|
if (!await loggedIn() && !q.allowAnonymous) {
|
|
46
|
-
return err('needs-login', '
|
|
49
|
+
return err('needs-login', '未检出你本人登录态——调用 gotry_session_login 为用户打开携程登录入口(登录在携程官网完成;gotry 永不经手密码/验证码/cookie 值)');
|
|
47
50
|
}
|
|
48
51
|
let settled = false;
|
|
49
52
|
let body = '';
|
|
@@ -32,14 +32,15 @@ const WMO_ZH = {
|
|
|
32
32
|
export function wmoLabel(code) {
|
|
33
33
|
return WMO_ZH[code] ?? `天气码${code}`;
|
|
34
34
|
}
|
|
35
|
-
async function fetchJson(url, timeoutMs) {
|
|
35
|
+
async function fetchJson(url, timeoutMs, headers = {}) {
|
|
36
36
|
const ctrl = new AbortController();
|
|
37
37
|
const timer = setTimeout(()=>ctrl.abort(), timeoutMs);
|
|
38
38
|
try {
|
|
39
39
|
const res = await fetch(url, {
|
|
40
40
|
signal: ctrl.signal,
|
|
41
41
|
headers: {
|
|
42
|
-
Accept: 'application/json'
|
|
42
|
+
Accept: 'application/json',
|
|
43
|
+
...headers
|
|
43
44
|
}
|
|
44
45
|
});
|
|
45
46
|
if (!res.ok) return {
|
|
@@ -59,33 +60,87 @@ async function fetchJson(url, timeoutMs) {
|
|
|
59
60
|
clearTimeout(timer);
|
|
60
61
|
}
|
|
61
62
|
}
|
|
63
|
+
const NOMINATIM_BASE = 'https://nominatim.openstreetmap.org/search';
|
|
64
|
+
const MAJOR_FEATURE = /^(PPLC|PPLA|PPLB|PPLX)/;
|
|
65
|
+
function stripHits(hits) {
|
|
66
|
+
return hits.map(({ name, latitude, longitude, country, admin1 })=>({
|
|
67
|
+
...country ? {
|
|
68
|
+
country
|
|
69
|
+
} : {},
|
|
70
|
+
...admin1 ? {
|
|
71
|
+
admin1
|
|
72
|
+
} : {},
|
|
73
|
+
name,
|
|
74
|
+
latitude,
|
|
75
|
+
longitude
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
62
78
|
export async function geocodePlace(name, opts = {}) {
|
|
63
79
|
const ts = new Date().toISOString();
|
|
64
80
|
const count = opts.count ?? 5;
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
ok: false,
|
|
70
|
-
evidence: `[实时API:open-meteo-geo@error@${ts}]`,
|
|
71
|
-
results: [],
|
|
72
|
-
error: r.error
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
const results = (r.data?.results ?? []).map((item)=>{
|
|
81
|
+
const query = name.trim();
|
|
82
|
+
const omUrl = `${GEOCODE_BASE}?name=${encodeURIComponent(query)}&count=${count}&language=zh&format=json`;
|
|
83
|
+
const r = await fetchJson(omUrl, opts.timeoutMs ?? 15_000);
|
|
84
|
+
const omResults = r.ok ? (r.data?.results ?? []).map((item)=>{
|
|
76
85
|
const it = item;
|
|
77
86
|
return {
|
|
78
87
|
name: String(it['name'] ?? ''),
|
|
79
88
|
latitude: Number(it['latitude']),
|
|
80
89
|
longitude: Number(it['longitude']),
|
|
81
90
|
country: it['country'] ? String(it['country']) : undefined,
|
|
82
|
-
admin1: it['admin1'] ? String(it['admin1']) : undefined
|
|
91
|
+
admin1: it['admin1'] ? String(it['admin1']) : undefined,
|
|
92
|
+
population: Number(it['population'] ?? 0) || 0,
|
|
93
|
+
featureCode: String(it['feature_code'] ?? '')
|
|
94
|
+
};
|
|
95
|
+
}).filter((h)=>h.name && Number.isFinite(h.latitude) && Number.isFinite(h.longitude)) : [];
|
|
96
|
+
omResults.sort((a, b)=>b.population - a.population || Number(MAJOR_FEATURE.test(b.featureCode)) - Number(MAJOR_FEATURE.test(a.featureCode)));
|
|
97
|
+
const top = omResults[0];
|
|
98
|
+
if (top && (top.population > 0 || MAJOR_FEATURE.test(top.featureCode))) {
|
|
99
|
+
return {
|
|
100
|
+
ok: true,
|
|
101
|
+
evidence: `[实时API:open-meteo-geo@${ts}]`,
|
|
102
|
+
via: 'open-meteo',
|
|
103
|
+
results: stripHits(omResults)
|
|
83
104
|
};
|
|
105
|
+
}
|
|
106
|
+
const nomTs = new Date().toISOString();
|
|
107
|
+
const nomUrl = `${NOMINATIM_BASE}?q=${encodeURIComponent(query)}&format=jsonv2&limit=${count}&accept-language=zh&addressdetails=1`;
|
|
108
|
+
const nom = await fetchJson(nomUrl, opts.timeoutMs ?? 15_000, {
|
|
109
|
+
'User-Agent': 'gotry-travel-agent/0.1 (+https://github.com/Danceiny/gotry)'
|
|
84
110
|
});
|
|
111
|
+
const nomResults = nom.ok ? (nom.data ?? []).map((item)=>{
|
|
112
|
+
const it = item;
|
|
113
|
+
const addr = it['address'] ?? {};
|
|
114
|
+
return {
|
|
115
|
+
name: String(it['name'] ?? ''),
|
|
116
|
+
latitude: Number(it['lat']),
|
|
117
|
+
longitude: Number(it['lon']),
|
|
118
|
+
country: addr['country'],
|
|
119
|
+
admin1: addr['province'] ?? addr['state'] ?? addr['county']
|
|
120
|
+
};
|
|
121
|
+
}).filter((h)=>h.name && Number.isFinite(h.latitude) && Number.isFinite(h.longitude)) : [];
|
|
122
|
+
if (nomResults.length) {
|
|
123
|
+
const omNote = r.ok ? `open-meteo ${omResults.length} 条弱命中(无人口/行政级,不足采信)` : `open-meteo 失败:${r.error ?? 'HTTP error'}`;
|
|
124
|
+
return {
|
|
125
|
+
ok: true,
|
|
126
|
+
evidence: `[实时API:nominatim@${nomTs}](兜底层: ${omNote})`,
|
|
127
|
+
via: 'nominatim',
|
|
128
|
+
results: nomResults
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (omResults.length) {
|
|
132
|
+
return {
|
|
133
|
+
ok: true,
|
|
134
|
+
evidence: `[实时API:open-meteo-geo@${ts}]`,
|
|
135
|
+
via: 'open-meteo',
|
|
136
|
+
results: stripHits(omResults)
|
|
137
|
+
};
|
|
138
|
+
}
|
|
85
139
|
return {
|
|
86
|
-
ok:
|
|
87
|
-
evidence: `[实时API:open-meteo-geo@${ts}]`,
|
|
88
|
-
results
|
|
140
|
+
ok: false,
|
|
141
|
+
evidence: `[实时API:open-meteo-geo@error@${ts}];[实时API:nominatim@error@${nomTs}]`,
|
|
142
|
+
results: [],
|
|
143
|
+
error: r.ok ? `双源无结果:${nom.error ?? 'nominatim empty'}` : r.error
|
|
89
144
|
};
|
|
90
145
|
}
|
|
91
146
|
export async function getForecast(point, opts = {}) {
|
|
@@ -2,6 +2,10 @@ import assert from 'node:assert/strict';
|
|
|
2
2
|
import { reach, reachStatus } from '../capabilities/agent-reach.js';
|
|
3
3
|
{
|
|
4
4
|
const st = await reachStatus(90_000);
|
|
5
|
+
if (!st.ok) {
|
|
6
|
+
console.log(`SKIP: agent-reach doctor 未就绪(未安装/needs-setup),7 断言整体跳过 — via=${st.via} output=${String(st.output).slice(0, 120)}`);
|
|
7
|
+
process.exit(0);
|
|
8
|
+
}
|
|
5
9
|
assert.equal(st.ok, true);
|
|
6
10
|
assert.equal(st.via, 'agent-reach-cli');
|
|
7
11
|
assert.ok(st.output.length > 50, 'doctor 输出非空(上游原样)');
|