Skip to content

Commit 7992972

Browse files
committed
Add strict OpenCLI browser query and capture results
1 parent cdd9609 commit 7992972

15 files changed

Lines changed: 575 additions & 1 deletion

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# OpenCLI 1.8.8 的查找、采集与分析结果
2+
3+
使用 npm 发布包 `@jackwener/opencli@1.8.8`、Node 24.18.0 和 macOS arm64。调用 [锁定版 CLI action](https://github.com/jackwener/OpenCLI/blob/8271afc67e8504bda94c147f446ee29775d08274/src/cli.ts)。页面调用由隔离的 Browser Bridge 桩提供,命令层的 JSON 包装、采集归一化、分析计算和缓存逻辑使用发布包原实现。固定时间为 2025-01-01T00:00:00Z,测试数据来自本地桩。
4+
5+
| 叶子 | typed 入口 | 1.8.8 输出 | 验收范围 |
6+
|---|---|---|---|
7+
| tab list | `tabListTyped()` | 标签数组 | 普通、空列表 |
8+
| open | `openTyped(url, tab)` | 导航对象 | 页 ID 与可选上下文 |
9+
| state | `snapshotTyped(tab, source)` | URL 头和正文 | DOM、AX |
10+
| state --compare-sources | `compareStateSourcesTyped(tab)` | 两侧指标对象 | 成功、AX 单侧失败 |
11+
| find | `findTyped(locator, tab, options)` | 总匹配数和条目 | CSS、限量及精确 ref |
12+
| console | `consoleTyped(tab, options)` | 单次采集对象 | 记录与合法空数组 |
13+
| network | `networkTyped(options, tab)` | 单次采集对象 | 请求条目、形状和扩展元数据 |
14+
| analyze | `analyzeTyped(url, tab)` | 报告对象 | URL、标题、分类与扩展报告 |
15+
| console/network --follow、network --detail、其余叶子 | raw 会话 API | 流或其它形状 | 保持 raw |
16+
17+
原始输出、SHA-256 与采集 preload 保存在 [样本目录](../../../../src/test/resources/opencli-contracts/v1/browser-output-1.8.8/)。沿用 [启动方式](2026-11-19-open-and-tab-list.md) 并设置 `OPENCLI_FIXTURE_FIXED_CLOCK=1`。调用 `browser fixture find --css '#fixture' --limit 1`、`browser fixture console`、`browser fixture network` 和 `browser fixture analyze http://127.0.0.1:18765/basic.html`。实际浏览器、网络请求和账户状态未参与本次验收。
18+
19+
typed 捕获返回原始执行结果。对现有异常中的 partial result,可按 leaf 显式调用 `OpenCliBrowserDecoder`。分析提示、网络详情提示与 sitemap 路径保留为数据,由调用者明确决定后续动作。流式和详情参数可继续交给现有 raw 方法。

‎openspec/changes/add-opencli-browser-result-models/tasks.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
## OC-BROWSER-001/002/004 state 与 AX
3333

3434
`OpenCliBrowserResultTest` 在缺少 state 类型模型和门面入口时无法编译。实现后验收 DOM 与 AX 文本、对比 JSON、单侧失败、无法识别的输出及 typed null source 拒绝。原有 String source 和 compareSources 继续由既有 Options 传递。见 [四份上游入口样本](evidence/2026-11-19-state-dom-and-ax.md)。
35+
36+
## OC-BROWSER-001/002/003/005 find、console、network 与 analyze
37+
38+
`OpenCliBrowserEvidenceResultTest` 首先因缺少模型和门面而无法编译。实现后验收有限的 find 条目、整数 ref 保真、采集 count 与数组一致性、分析扩展信息和原有执行失败。流式采集及网络详情在 typed 入口启动前拒绝,保留 raw 方法。见 [输出证据、支持表和迁移方式](evidence/2026-11-19-find-console-network-analyze.md)。

‎src/main/java/io/github/easy4j/opencli/browser/OpenCliBrowserSession.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package io.github.easy4j.opencli.browser;
22

3+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserAnalysisResult;
4+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserConsoleResult;
35
import io.github.easy4j.opencli.browser.result.OpenCliBrowserDecoder;
6+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserFindResult;
7+
import io.github.easy4j.opencli.browser.result.OpenCliBrowserNetworkResult;
48
import io.github.easy4j.opencli.browser.result.OpenCliBrowserOpenResult;
59
import io.github.easy4j.opencli.browser.result.OpenCliBrowserResult;
610
import io.github.easy4j.opencli.browser.result.OpenCliBrowserSnapshot;
@@ -301,6 +305,19 @@ public OpenCliResult console(OpenCliBrowserTabOptions tab, OpenCliBrowserConsole
301305
return invokeSub(args);
302306
}
303307

308+
/**
309+
* @param tab optional target tab
310+
* @param options one-shot console collection options
311+
* @return strict console capture
312+
*/
313+
public OpenCliBrowserResult<OpenCliBrowserConsoleResult> consoleTyped(OpenCliBrowserTabOptions tab,
314+
OpenCliBrowserConsoleOptions options) {
315+
if (Objects.nonNull(options) && Boolean.TRUE.equals(options.getFollow())) {
316+
throw new IllegalArgumentException("Use the raw console API for --follow");
317+
}
318+
return OpenCliBrowserDecoder.console(console(tab, options));
319+
}
320+
304321
/**
305322
* @deprecated 使用 {@link #console(OpenCliBrowserTabOptions, OpenCliBrowserConsoleOptions)};
306323
* CLI 无 {@code --limit}
@@ -322,6 +339,15 @@ public OpenCliResult analyze(String url, OpenCliBrowserTabOptions tab) {
322339
return invokeSub(args);
323340
}
324341

342+
/**
343+
* @param url requested analysis URL
344+
* @param tab optional target tab
345+
* @return strict analysis observation
346+
*/
347+
public OpenCliBrowserResult<OpenCliBrowserAnalysisResult> analyzeTyped(String url, OpenCliBrowserTabOptions tab) {
348+
return OpenCliBrowserDecoder.analyze(analyze(url, tab));
349+
}
350+
325351
/**
326352
* {@code browser find},对齐 {@code --css}、{@code --limit}、{@code --text-max}。
327353
*/
@@ -338,6 +364,17 @@ public OpenCliResult find(
338364
return invokeSub(args);
339365
}
340366

367+
/**
368+
* @param locator optional semantic locator
369+
* @param tab optional target tab
370+
* @param options selector and entry limits
371+
* @return strict selector matches
372+
*/
373+
public OpenCliBrowserResult<OpenCliBrowserFindResult> findTyped(OpenCliBrowserSemanticLocator locator,
374+
OpenCliBrowserTabOptions tab, OpenCliBrowserFindOptions options) {
375+
return OpenCliBrowserDecoder.find(find(locator, tab, options));
376+
}
377+
341378
/**
342379
* @deprecated 使用 {@link #find(OpenCliBrowserSemanticLocator, OpenCliBrowserTabOptions, OpenCliBrowserFindOptions)};
343380
* CLI 无 {@code --source}/{@code --nth}
@@ -751,6 +788,20 @@ public OpenCliResult network(OpenCliBrowserNetworkOptions options, OpenCliBrowse
751788
return invokeSub(args);
752789
}
753790

791+
/**
792+
* @param options one-shot network listing options
793+
* @param tab optional target tab
794+
* @return strict network capture
795+
*/
796+
public OpenCliBrowserResult<OpenCliBrowserNetworkResult> networkTyped(OpenCliBrowserNetworkOptions options,
797+
OpenCliBrowserTabOptions tab) {
798+
if (Objects.nonNull(options) && (Boolean.TRUE.equals(options.getFollow())
799+
|| Objects.nonNull(options.getDetailKey()))) {
800+
throw new IllegalArgumentException("Use the raw network API for --follow or --detail");
801+
}
802+
return OpenCliBrowserDecoder.network(network(options, tab));
803+
}
804+
754805
/** {@code browser init <site/command>} */
755806
public OpenCliResult init(String siteCommandName) {
756807
List<String> args = new ArrayList<>();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import tools.jackson.databind.JsonNode;
4+
import lombok.AccessLevel;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
8+
/** Reported browser analysis metadata. Advice and paths are returned as data. */
9+
@Getter
10+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
11+
public final class OpenCliBrowserAnalysisResult {
12+
private final String requestedUrl;
13+
private final String finalUrl;
14+
private final String title;
15+
private final String pattern;
16+
private final JsonNode metadata;
17+
18+
/** @return a copy of the complete report, including optional sitemap metadata */
19+
public JsonNode getMetadata() {
20+
return metadata.deepCopy();
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import tools.jackson.databind.JsonNode;
4+
import java.util.List;
5+
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
9+
/** One bounded browser console capture. */
10+
@Getter
11+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
12+
public final class OpenCliBrowserConsoleResult {
13+
private final String session;
14+
private final String capturedAt;
15+
private final List<Message> messages;
16+
private final JsonNode metadata;
17+
18+
/** @return a copy of the capture envelope */
19+
public JsonNode getMetadata() {
20+
return metadata.deepCopy();
21+
}
22+
23+
/** A console message with its original browser-supplied fields. */
24+
@Getter
25+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
26+
public static final class Message {
27+
private final String type;
28+
private final String text;
29+
private final String timestamp;
30+
private final JsonNode metadata;
31+
32+
/** @return a copy of the original console record */
33+
public JsonNode getMetadata() {
34+
return metadata.deepCopy();
35+
}
36+
}
37+
}

‎src/main/java/io/github/easy4j/opencli/browser/result/OpenCliBrowserDecoder.java‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.github.easy4j.opencli.browser.result.OpenCliBrowserSourceComparison.Metrics;
1010
import io.github.easy4j.opencli.core.OpenCliExecutionDetails;
1111
import io.github.easy4j.opencli.core.OpenCliResult;
12+
import java.math.BigInteger;
1213
import java.util.ArrayList;
1314
import java.util.Collections;
1415
import java.util.List;
@@ -72,6 +73,26 @@ public static OpenCliBrowserResult<OpenCliBrowserSourceComparison> compareSource
7273
return decode(raw, OpenCliBrowserDecoder::readComparison);
7374
}
7475

76+
/** @param raw browser find result @return strict selector matches */
77+
public static OpenCliBrowserResult<OpenCliBrowserFindResult> find(OpenCliResult raw) {
78+
return decode(raw, OpenCliBrowserDecoder::readFind);
79+
}
80+
81+
/** @param raw one-shot browser console result @return strict console capture */
82+
public static OpenCliBrowserResult<OpenCliBrowserConsoleResult> console(OpenCliResult raw) {
83+
return decode(raw, OpenCliBrowserDecoder::readConsole);
84+
}
85+
86+
/** @param raw one-shot browser network listing @return strict network capture */
87+
public static OpenCliBrowserResult<OpenCliBrowserNetworkResult> network(OpenCliResult raw) {
88+
return decode(raw, OpenCliBrowserDecoder::readNetwork);
89+
}
90+
91+
/** @param raw browser analyze result @return strict analysis observation */
92+
public static OpenCliBrowserResult<OpenCliBrowserAnalysisResult> analyze(OpenCliResult raw) {
93+
return decode(raw, OpenCliBrowserDecoder::readAnalysis);
94+
}
95+
7596
private static Status unavailableStatus(OpenCliResult raw) {
7697
Objects.requireNonNull(raw, "raw");
7798
OpenCliExecutionDetails execution = raw.getExecutionDetails();
@@ -164,6 +185,94 @@ private static Metrics readMetrics(JsonNode json) {
164185
counter(json.path("frame_sections")), interactive.isMissingNode() ? null : counter(interactive), null, null);
165186
}
166187

188+
private static OpenCliBrowserFindResult readFind(JsonNode json) {
189+
long total = counter(json.path("matches_n"));
190+
JsonNode reported = json.path("entries");
191+
if (total < reported.size()) {
192+
throw invalidShape();
193+
}
194+
List<OpenCliBrowserFindResult.Entry> entries = entries(reported, item -> {
195+
JsonNode ref = item.path("ref");
196+
JsonNode visible = item.path("visible");
197+
BigInteger numericRef = ref.isNull() ? null : unsignedInteger(ref);
198+
if (!visible.isBoolean() || !item.path("attrs").isObject()) {
199+
throw invalidShape();
200+
}
201+
return new OpenCliBrowserFindResult.Entry(counter(item.path("nth")), numericRef, text(item.path("tag")),
202+
text(item.path("role")), text(item.path("text")), visible.booleanValue(), item);
203+
});
204+
return new OpenCliBrowserFindResult(total, entries, json);
205+
}
206+
207+
private static OpenCliBrowserConsoleResult readConsole(JsonNode json) {
208+
JsonNode reported = json.path("messages");
209+
validateCount(json, reported);
210+
List<OpenCliBrowserConsoleResult.Message> messages = entries(reported, item ->
211+
new OpenCliBrowserConsoleResult.Message(optionalText(item.path("type")), optionalText(item.path("text")),
212+
text(item.path("timestamp")), item));
213+
return new OpenCliBrowserConsoleResult(text(json.path("session")), text(json.path("captured_at")), messages, json);
214+
}
215+
216+
private static OpenCliBrowserNetworkResult readNetwork(JsonNode json) {
217+
JsonNode reported = json.path("entries");
218+
validateCount(json, reported);
219+
List<OpenCliBrowserNetworkResult.Entry> entries = entries(reported, item -> {
220+
JsonNode status = item.path("status");
221+
if (!status.isIntegralNumber() || !status.canConvertToInt() || status.intValue() < 0) {
222+
throw invalidShape();
223+
}
224+
return new OpenCliBrowserNetworkResult.Entry(text(item.path("key")), text(item.path("method")),
225+
status.intValue(), text(item.path("url")), text(item.path("ct")), counter(item.path("size")),
226+
optionalText(item.path("timestamp")), item);
227+
});
228+
return new OpenCliBrowserNetworkResult(text(json.path("session")), text(json.path("captured_at")),
229+
counter(json.path("filtered_out")), entries, json);
230+
}
231+
232+
private static OpenCliBrowserAnalysisResult readAnalysis(JsonNode json) {
233+
JsonNode pattern = json.path("pattern");
234+
if (!json.path("anti_bot").isObject() || !json.path("initial_state").isObject()
235+
|| !json.path("api_candidates").isArray() || !json.path("recommended_next_step").isTextual()) {
236+
throw invalidShape();
237+
}
238+
return new OpenCliBrowserAnalysisResult(text(json.path("requested_url")), text(json.path("final_url")),
239+
text(json.path("title")), text(pattern.path("pattern")), json);
240+
}
241+
242+
private static <T> List<T> entries(JsonNode array, Function<JsonNode, T> entry) {
243+
if (!array.isArray()) {
244+
throw invalidShape();
245+
}
246+
List<T> result = new ArrayList<>(array.size());
247+
for (JsonNode item : array) {
248+
if (!item.isObject()) {
249+
throw invalidShape();
250+
}
251+
result.add(entry.apply(item));
252+
}
253+
return Collections.unmodifiableList(result);
254+
}
255+
256+
private static void validateCount(JsonNode json, JsonNode entries) {
257+
if (counter(json.path("count")) != entries.size()) {
258+
throw invalidShape();
259+
}
260+
}
261+
262+
private static BigInteger unsignedInteger(JsonNode node) {
263+
if (!node.isIntegralNumber() || node.bigIntegerValue().signum() < 0) {
264+
throw invalidShape();
265+
}
266+
return node.bigIntegerValue();
267+
}
268+
269+
private static String text(JsonNode node) {
270+
if (!node.isTextual()) {
271+
throw invalidShape();
272+
}
273+
return node.textValue();
274+
}
275+
167276
private static long counter(JsonNode node) {
168277
if (!node.isIntegralNumber() || !node.canConvertToLong() || node.longValue() < 0) {
169278
throw invalidShape();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import tools.jackson.databind.JsonNode;
4+
import java.math.BigInteger;
5+
import java.util.List;
6+
import lombok.AccessLevel;
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
10+
/** Browser selector matches, including the total before the entry limit. */
11+
@Getter
12+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
13+
public final class OpenCliBrowserFindResult {
14+
private final long matchesN;
15+
private final List<Entry> entries;
16+
private final JsonNode metadata;
17+
18+
/** @return a copy of the original result with all reported fields */
19+
public JsonNode getMetadata() {
20+
return metadata.deepCopy();
21+
}
22+
23+
/** A matched element. The optional numeric reference retains its exact value. */
24+
@Getter
25+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
26+
public static final class Entry {
27+
private final long nth;
28+
private final BigInteger ref;
29+
private final String tag;
30+
private final String role;
31+
private final String text;
32+
private final boolean visible;
33+
private final JsonNode metadata;
34+
35+
/** @return a copy including attributes, compound data and extensions */
36+
public JsonNode getMetadata() {
37+
return metadata.deepCopy();
38+
}
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.github.easy4j.opencli.browser.result;
2+
3+
import tools.jackson.databind.JsonNode;
4+
import java.util.List;
5+
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
9+
/** One network capture, with any upstream filtering, cache and truncation metadata. */
10+
@Getter
11+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
12+
public final class OpenCliBrowserNetworkResult {
13+
private final String session;
14+
private final String capturedAt;
15+
private final long filteredOut;
16+
private final List<Entry> entries;
17+
private final JsonNode metadata;
18+
19+
/** @return a copy of the capture envelope */
20+
public JsonNode getMetadata() {
21+
return metadata.deepCopy();
22+
}
23+
24+
/** An observed request. Metadata includes the preview or body supplied by OpenCLI. */
25+
@Getter
26+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
27+
public static final class Entry {
28+
private final String key;
29+
private final String method;
30+
private final int status;
31+
private final String url;
32+
private final String contentType;
33+
private final long size;
34+
private final String timestamp;
35+
private final JsonNode metadata;
36+
37+
/** @return a copy including any body, shape, truncation and request metadata */
38+
public JsonNode getMetadata() {
39+
return metadata.deepCopy();
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)