@opencraw/studio 0.0.5 → 0.0.7

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.
Files changed (40) hide show
  1. package/dist/index.esm.js +1393 -82
  2. package/dist/src/explain-why/index.d.ts +5 -0
  3. package/dist/src/explain-why/item-hint.algorithm.d.ts +13 -0
  4. package/dist/src/explain-why/why-sentence.mapper.d.ts +48 -0
  5. package/dist/src/explain-why/why.use-case.d.ts +26 -0
  6. package/dist/src/index.d.ts +4 -3
  7. package/dist/src/page-snapshot/hidden-marks.algorithm.d.ts +23 -0
  8. package/dist/src/page-snapshot/index.d.ts +7 -0
  9. package/dist/src/page-snapshot/rewrite-document.mapper.d.ts +27 -0
  10. package/dist/src/page-snapshot/snapshot.store.d.ts +14 -0
  11. package/dist/src/page-snapshot/take-snapshot.use-case.d.ts +34 -0
  12. package/dist/src/sample-run/index.d.ts +3 -1
  13. package/dist/src/sample-run/last-run.store.d.ts +17 -0
  14. package/dist/src/sample-run/run-sample.use-case.d.ts +15 -7
  15. package/dist/src/sample-run/sample-run-record.contract.d.ts +30 -1
  16. package/dist/src/selector-inference/class-token.policy.d.ts +35 -0
  17. package/dist/src/selector-inference/dom-path.model.d.ts +73 -0
  18. package/dist/src/selector-inference/index.d.ts +12 -0
  19. package/dist/src/selector-inference/list-inference.algorithm.d.ts +43 -0
  20. package/dist/src/selector-inference/selector-candidates.algorithm.d.ts +22 -0
  21. package/dist/src/selector-inference/selector-ranking.policy.d.ts +27 -0
  22. package/dist/src/selector-inference/shadow-dom.error.d.ts +11 -0
  23. package/dist/src/selector-inference/take-kind.policy.d.ts +12 -0
  24. package/dist/src/studio-api/command.contract.d.ts +103 -0
  25. package/dist/src/studio-api/event.contract.d.ts +41 -1
  26. package/dist/src/studio-api/index.d.ts +12 -4
  27. package/dist/src/studio-api/infer-selector-view.contract.d.ts +68 -0
  28. package/dist/src/studio-api/snapshot-view.contract.d.ts +9 -0
  29. package/dist/src/studio-api/verify-selector-view.contract.d.ts +18 -0
  30. package/dist/src/studio-api/why-view.contract.d.ts +16 -0
  31. package/dist/src/studio-server/explain-why.handler.d.ts +14 -0
  32. package/dist/src/studio-server/infer-selector.handler.d.ts +18 -0
  33. package/dist/src/studio-server/studio-http.use-case.d.ts +3 -0
  34. package/dist/src/studio-server/take-snapshot.handler.d.ts +16 -0
  35. package/dist/src/studio-server/verify-selector.handler.d.ts +18 -0
  36. package/dist/src/studio-server/workspace.store.d.ts +9 -3
  37. package/dist/ui/assets/index-DV8i4Zza.js +13 -0
  38. package/dist/ui/index.html +1 -1
  39. package/package.json +5 -3
  40. package/dist/ui/assets/index-DYb-tdmr.js +0 -12
@@ -0,0 +1,5 @@
1
+ export { explainWhy } from './why.use-case.js';
2
+ export { buildWhySentence } from './why-sentence.mapper.js';
3
+ export type { WhyFacts, WhyStepFact } from './why-sentence.mapper.js';
4
+ export { hintFor } from './item-hint.algorithm.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A best-effort hint about what a missing value's bound id actually held in
3
+ * the scope snapshot: the keys of an object, or the length of a list — the
4
+ * fallback when there is no per-item DOM snapshot to compare a selector
5
+ * against (see `why-sentence.mapper.ts`'s doc comment for why the richer,
6
+ * "the item has `.price_new`" hint is out of this phase's scope).
7
+ *
8
+ * @param scope - The record's scope snapshot (`SampleRunRecord.scope`/`SampleRunRejected.scope`).
9
+ * @param boundId - The leading id the rule's `from` reads.
10
+ * @returns A short clause (no leading punctuation), or `undefined` when the id is not in scope or nothing useful can be said about it.
11
+ */
12
+ export declare function hintFor(scope: Record<string, unknown> | undefined, boundId: string | undefined): string | undefined;
13
+ //# sourceMappingURL=item-hint.algorithm.d.ts.map
@@ -0,0 +1,48 @@
1
+ import type { FieldTrace } from '@opencraw/core';
2
+ /** One step of the recipe that may have bound the missing value's source id, and how it went over the whole sample run (`sample-run`'s `SampleStepSummary`). */
3
+ export interface WhyStepFact {
4
+ path: string;
5
+ stepType: string;
6
+ stepId?: string;
7
+ outcome: 'ran' | 'skipped' | 'retried' | 'not-found';
8
+ error?: string;
9
+ }
10
+ /** Everything `buildWhySentence` needs to explain one missing or rejected value; gathered by `why.use-case.ts` from the last sample run's kept trace, scope and step summary. */
11
+ export interface WhyFacts {
12
+ field: string;
13
+ outcome: 'missing' | 'rejected';
14
+ /** The engine's own rejection reason (`record:reject`'s `reason`), for a rejected record. */
15
+ reason?: string;
16
+ /** The resolved missing-value policy (`nullable`, `default`, `null`, `fail`, `skip-record`), when the field could be found in the output recipe. */
17
+ policy?: string;
18
+ /** The field's mapping trace, when the record itself was emitted (a `missing` outcome only — a rejected record never reached `data`). */
19
+ trace?: FieldTrace;
20
+ /** The leading id the rule's `from` reads (`item` of `item.description`), when the rule could be found. */
21
+ boundId?: string;
22
+ /** The step that binds `boundId`, when one was found in the recipe. */
23
+ step?: WhyStepFact;
24
+ /** A best-effort hint about what the bound value actually held (phase 2's item snapshot gives a richer one for a CSS pick; this is the scope-only fallback — see this module's doc comment). */
25
+ hint?: string;
26
+ }
27
+ /**
28
+ * Builds the Why? tab's sentence (studio plan §4.4, issue #92) from the
29
+ * facts `why.use-case.ts` gathers: the value the mapping read (and each
30
+ * transform's effect on it), the step that bound its source id and whether
31
+ * it ran, was skipped, or bound nothing this recipe knows of, the
32
+ * missing-value policy that applied, and a hint about what was actually
33
+ * there instead.
34
+ *
35
+ * This is the scope-snapshot-only half of the design: phase 2's per-item DOM
36
+ * snapshot (the CSS-selector "the item has `.price_new`" hint the issue's
37
+ * example shows) is not wired in here — `sample-run` keeps the mapping's
38
+ * scope snapshot (bound ids and their values), not a per-`forEach`-item DOM
39
+ * capture, and building that link is a bigger integration than this phase's
40
+ * budget covers (see this package's `page-snapshot` slice, which captures a
41
+ * step's page for the content pane, not per-iteration). `hint`, when given,
42
+ * is instead built from the scope snapshot itself (`item-hint.algorithm.ts`).
43
+ *
44
+ * @param facts - What is known about the value.
45
+ * @returns One sentence, always ending in a period.
46
+ */
47
+ export declare function buildWhySentence(facts: WhyFacts): string;
48
+ //# sourceMappingURL=why-sentence.mapper.d.ts.map
@@ -0,0 +1,26 @@
1
+ import type { InputRecipe, OutputRecipe } from '@opencraw/core';
2
+ import type { LastRunCache } from '../sample-run/index.js';
3
+ import type { WhyTarget, WhyView } from '../studio-api/index.js';
4
+ /**
5
+ * Explains one missing or rejected value from a recipe's last sample run
6
+ * (issue #92's Why? tab, studio plan §4.4): the mapping trace and scope
7
+ * snapshot `run-sample` kept, the recipe's own rule and field for the
8
+ * missing-value policy, and the step that bound the value's source id — did
9
+ * it run, was it skipped, or does no step in this recipe bind it at all.
10
+ *
11
+ * @param folder - The workspace folder (to re-read the recipe pair: the last
12
+ * run only kept the run's own data, not the recipe text, and a save may
13
+ * have happened since).
14
+ * @param cache - The server's last-run cache (`sample-run`'s `LastRunCache`).
15
+ * @param loadPair - Loads the input/output recipe pair; injected so this
16
+ * stays testable without a real workspace folder (`sample-run`'s
17
+ * `loadRecipePair` in production).
18
+ * @param target - What was clicked.
19
+ * @returns The explanation.
20
+ * @throws Error when no sample run has finished for this recipe yet, or the target does not name a real record/field.
21
+ */
22
+ export declare function explainWhy(folder: string, cache: LastRunCache, loadPair: (folder: string, recipeId: string) => Promise<{
23
+ input: InputRecipe;
24
+ output: OutputRecipe;
25
+ }>, target: WhyTarget): Promise<WhyView>;
26
+ //# sourceMappingURL=why.use-case.d.ts.map
@@ -3,8 +3,9 @@ export { startStudioServer } from './studio-server/index.js';
3
3
  export type { StudioServer, StudioServerOptions } from './studio-server/index.js';
4
4
  export { openWorkspace, saveRecipe } from './recipe-workspace/index.js';
5
5
  export { runSample, loadRecipePair } from './sample-run/index.js';
6
- export type { RecipePair, SampleRunCallbacks, SampleRunHandle, SampleRunRecord, SampleRunResult, SampleStoppedBy } from './sample-run/index.js';
6
+ export type { RecipePair, SampleRunCallbacks, SampleRunHandle, SampleRunRecord, SampleRunRejected, SampleRunResult, SampleStepSummary, SampleStoppedBy } from './sample-run/index.js';
7
7
  export { fetchStartPage } from './page-snapshot/index.js';
8
- export { sampleBudgetSchema, openWorkspaceCommandSchema, runSampleCommandSchema, stopRunCommandSchema, saveRecipeCommandSchema, fetchStartPageCommandSchema, saveOutlineCommandSchema, studioCommandSchema, stoppedBySchema, traceLineEventSchema, recordEventSchema, runFinishedEventSchema, workspaceChangedEventSchema, studioEventSchema, recipeIssueSchema, recipeListingSchema, workspaceViewSchema, sampleRunRecordSchema, sampleRunViewSchema, startPageViewSchema, sentencePartSchema, outlineCardSchema, outlineBracketSchema, outlineNodeSchema, outlineViewSchema, } from './studio-api/index.js';
9
- export type { SampleBudget, OpenWorkspaceCommand, RunSampleCommand, StopRunCommand, SaveRecipeCommand, FetchStartPageCommand, SaveOutlineCommand, StudioCommand, StoppedBy, TraceLineEvent, RecordEvent, RunFinishedEvent, WorkspaceChangedEvent, StudioEvent, RecipeIssue, RecipeListing, WorkspaceView, SampleRunView, StartPageView, SentencePart, OutlineCard, OutlineBracket, OutlineNode, OutlineView, } from './studio-api/index.js';
8
+ export type { WhyFacts, WhyStepFact } from './explain-why/index.js';
9
+ export { sampleBudgetSchema, openWorkspaceCommandSchema, runSampleCommandSchema, stopRunCommandSchema, saveRecipeCommandSchema, fetchStartPageCommandSchema, saveOutlineCommandSchema, takeSnapshotCommandSchema, verifySelectorCommandSchema, inferSelectorCommandSchema, missingWhyTargetSchema, rejectedWhyTargetSchema, whyTargetSchema, explainWhyCommandSchema, studioCommandSchema, stoppedBySchema, fieldTraceSchema, traceLineEventSchema, recordEventSchema, recordRejectedEventSchema, runFinishedEventSchema, workspaceChangedEventSchema, studioEventSchema, recipeIssueSchema, recipeListingSchema, workspaceViewSchema, sampleRunRecordSchema, sampleRunViewSchema, startPageViewSchema, snapshotViewSchema, verifySelectorViewSchema, fieldPickSchema, inferSelectorViewSchema, sentencePartSchema, outlineCardSchema, outlineBracketSchema, outlineNodeSchema, outlineViewSchema, whyViewSchema, } from './studio-api/index.js';
10
+ export type { SampleBudget, OpenWorkspaceCommand, RunSampleCommand, StopRunCommand, SaveRecipeCommand, FetchStartPageCommand, SaveOutlineCommand, TakeSnapshotCommand, VerifySelectorCommand, InferSelectorCommand, MissingWhyTarget, RejectedWhyTarget, WhyTarget, ExplainWhyCommand, StudioCommand, StoppedBy, FieldTraceView, TraceLineEvent, RecordEvent, RecordRejectedEvent, RunFinishedEvent, WorkspaceChangedEvent, StudioEvent, RecipeIssue, RecipeListing, WorkspaceView, SampleRunView, StartPageView, SnapshotView, VerifySelectorView, FieldPick, InferSelectorView, SentencePart, OutlineCard, OutlineBracket, OutlineNode, OutlineView, WhyView, } from './studio-api/index.js';
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,23 @@
1
+ /** The attribute `hiddenMarksScript` stamps on an element hidden on the live page. */
2
+ export declare const HIDDEN_ATTRIBUTE = "data-oc-hidden";
3
+ /**
4
+ * The in-page script `take-snapshot.use-case.ts` runs with `page.evaluate`
5
+ * *before* `page.content()`, so a `display: none`/`visibility: hidden`/
6
+ * zero-size element carries `data-oc-hidden="1"` into the captured markup
7
+ * (studio plan §3.1, issue #91) — the only way to know an element was
8
+ * hidden on the live page, since a static parse of the HTML afterwards has
9
+ * no computed style to ask.
10
+ *
11
+ * Kept as a plain string, not a TypeScript function passed to
12
+ * `page.evaluate`, because it references `window`/`document` as globals: `AGENTS.md`'s
13
+ * "things that bit before" calls out that in-page functions must not do
14
+ * that (the repo's `isolated-functions` lint rule), and every existing
15
+ * `page.evaluate` call with real DOM logic in this codebase (see
16
+ * `packages/core/src/captcha/resolve-captcha.use-case.ts`) already uses
17
+ * this same string-template shape.
18
+ *
19
+ * @param attribute - The attribute to stamp; defaults to `HIDDEN_ATTRIBUTE` (parameterised for tests and for a future caller that wants a different name).
20
+ * @returns A self-invoking script, ready for `page.evaluate(hiddenMarksScript())`.
21
+ */
22
+ export declare function hiddenMarksScript(attribute?: string): string;
23
+ //# sourceMappingURL=hidden-marks.algorithm.d.ts.map
@@ -1,2 +1,9 @@
1
1
  export { fetchStartPage } from './fetch-start-page.use-case.js';
2
+ export { takeSnapshot } from './take-snapshot.use-case.js';
3
+ export type { SnapshotResult } from './take-snapshot.use-case.js';
4
+ export { rewriteDocument } from './rewrite-document.mapper.js';
5
+ export type { RewrittenDocument } from './rewrite-document.mapper.js';
6
+ export { hiddenMarksScript, HIDDEN_ATTRIBUTE } from './hidden-marks.algorithm.js';
7
+ export { createSnapshotCache, cachedSnapshot, putSnapshot, invalidateRecipe } from './snapshot.store.js';
8
+ export type { SnapshotCache } from './snapshot.store.js';
2
9
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,27 @@
1
+ export interface RewrittenDocument {
2
+ /** The document, ready for the sandboxed iframe: absolute URLs, a `<base>`, no `<script>`, forms disabled, `<meta http-equiv="refresh">` stripped, every element carrying a `data-oc-node` id. */
3
+ html: string;
4
+ /** How many elements were stamped with a node id — the same count `page-inspector` (phase 4) and the picker's match counts can be checked against. */
5
+ nodeCount: number;
6
+ }
7
+ /**
8
+ * Rewrites captured HTML into what the content pane's sandboxed iframe shows
9
+ * (studio plan §3.1, issue #91): every URL made absolute against `baseUrl`,
10
+ * a `<base>` injected, every `<script>` removed, forms disabled (`onsubmit`
11
+ * stripped, fields made read-only), `<meta http-equiv="refresh">` stripped
12
+ * (it would otherwise navigate the sandboxed frame on its own), and every
13
+ * element given a stable `data-oc-node` id, in document order, for the
14
+ * picker overlay and `selector-inference` to address specific nodes by.
15
+ *
16
+ * Purely a markup transform: it does not know whether an element is hidden
17
+ * on the live page (`display: none`, `visibility: hidden`, zero size) — in
18
+ * web mode that is `hidden-marks.algorithm.ts`'s job, run against the live
19
+ * page *before* this function ever sees the markup, so a `data-oc-hidden`
20
+ * attribute it wrote just survives serialisation untouched here.
21
+ *
22
+ * @param html - The captured document (`page.content()` in web mode, the fetched body in api mode).
23
+ * @param baseUrl - The page's own URL, for resolving relative links and as the `<base>`.
24
+ * @returns The rewritten document and how many elements it stamped.
25
+ */
26
+ export declare function rewriteDocument(html: string, baseUrl: string): RewrittenDocument;
27
+ //# sourceMappingURL=rewrite-document.mapper.d.ts.map
@@ -0,0 +1,14 @@
1
+ import type { SnapshotResult } from './take-snapshot.use-case.js';
2
+ /** A cache of the last snapshot taken per step path, so switching the selected step in the Steps outline does not always re-run the recipe (studio plan §3.1, issue #91). One per open recipe: key by `${recipeId}:${path}`. */
3
+ export interface SnapshotCache {
4
+ entries: Map<string, SnapshotResult>;
5
+ }
6
+ /** @returns An empty cache, for a freshly opened workspace. */
7
+ export declare function createSnapshotCache(): SnapshotCache;
8
+ /** @returns The cached snapshot for `recipeId`/`path`, or `undefined` when none was taken yet. */
9
+ export declare function cachedSnapshot(cache: SnapshotCache, recipeId: string, path: string): SnapshotResult | undefined;
10
+ /** Records a freshly taken snapshot, replacing whatever was cached for the same recipe and step path. */
11
+ export declare function putSnapshot(cache: SnapshotCache, recipeId: string, path: string, snapshot: SnapshotResult): void;
12
+ /** Drops every cached snapshot of one recipe — called after a save, since the steps up to any path may now capture something different. */
13
+ export declare function invalidateRecipe(cache: SnapshotCache, recipeId: string): void;
14
+ //# sourceMappingURL=snapshot.store.d.ts.map
@@ -0,0 +1,34 @@
1
+ import type { BrowserSessionConfig, InputRecipe } from '@opencraw/core';
2
+ export interface SnapshotResult {
3
+ /** The rewritten document, ready for the content pane's sandboxed iframe. */
4
+ html: string;
5
+ /** How many elements carry a `data-oc-node` id. */
6
+ nodeCount: number;
7
+ /** The page's own URL, used to resolve every relative URL and as the `<base>`. */
8
+ baseUrl: string;
9
+ }
10
+ /**
11
+ * Captures the snapshot the content pane shows (studio plan §3.1, issue
12
+ * #91): runs the recipe up to `stepPath`, serialises the document, marks
13
+ * hidden elements in web mode, and hands it to `rewrite-document.mapper.ts`
14
+ * for display.
15
+ *
16
+ * `stepPath` is accepted (and cached against, by `snapshot.store.ts`) for
17
+ * the API the UI needs — one snapshot per selected step — but v1 only
18
+ * actually *captures* `"start"`: the first `start` point, before any step
19
+ * of the recipe has run, the same reach `fetch-start-page.use-case.ts` (phase
20
+ * 0) already had. Snapshotting mid-recipe (after an arbitrary later step)
21
+ * needs a "stop the crawl after step X and hand back the live page/document"
22
+ * hook in `crawl-execution` that does not exist yet; building one was out of
23
+ * this phase's budget (see this phase's final report for the tradeoff this
24
+ * was weighed against). Any other `stepPath` throws, clearly, rather than
25
+ * silently returning the start page under a misleading label.
26
+ *
27
+ * @param input - The input recipe; only `mode` and the first `start` point are used.
28
+ * @param stepPath - The step to snapshot after, e.g. `"start"` or `"steps.2"` (`scope-outline`'s path format).
29
+ * @param browser - Browser launch settings for web mode.
30
+ * @returns The rewritten snapshot.
31
+ * @throws Error when the recipe has no start point, or `stepPath` is not `"start"`.
32
+ */
33
+ export declare function takeSnapshot(input: InputRecipe, stepPath: string, browser?: BrowserSessionConfig): Promise<SnapshotResult>;
34
+ //# sourceMappingURL=take-snapshot.use-case.d.ts.map
@@ -2,5 +2,7 @@ export { loadRecipePair } from './load-recipe-pair.use-case.js';
2
2
  export type { RecipePair } from './load-recipe-pair.use-case.js';
3
3
  export { runSample } from './run-sample.use-case.js';
4
4
  export type { SampleRunCallbacks, SampleRunHandle } from './run-sample.use-case.js';
5
- export type { SampleRunRecord, SampleRunResult, SampleStoppedBy } from './sample-run-record.contract.js';
5
+ export type { SampleRunRecord, SampleRunRejected, SampleRunResult, SampleStepSummary, SampleStoppedBy } from './sample-run-record.contract.js';
6
+ export { createLastRunCache, lastRunOf, recordLastRun } from './last-run.store.js';
7
+ export type { LastRunCache, LastRunEntry } from './last-run.store.js';
6
8
  //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,17 @@
1
+ import type { SampleRunResult } from './sample-run-record.contract.js';
2
+ /** The last completed sample run of one input recipe: its records, rejections, mapping traces and step summary, kept for `explain-why` (issue #92) so a Why? click needs no second run. */
3
+ export interface LastRunEntry {
4
+ recipeId: string;
5
+ result: SampleRunResult;
6
+ }
7
+ /** One server's worth of last-run entries, by recipe id: a run replaces the previous one for the same recipe. */
8
+ export interface LastRunCache {
9
+ entries: Map<string, LastRunEntry>;
10
+ }
11
+ /** @returns A fresh, empty cache. */
12
+ export declare function createLastRunCache(): LastRunCache;
13
+ /** Records a finished sample run, replacing whatever this recipe's cache entry held before. */
14
+ export declare function recordLastRun(cache: LastRunCache, recipeId: string, result: SampleRunResult): void;
15
+ /** @returns The recipe's last completed sample run, or `undefined` when none has finished yet. */
16
+ export declare function lastRunOf(cache: LastRunCache, recipeId: string): LastRunEntry | undefined;
17
+ //# sourceMappingURL=last-run.store.d.ts.map
@@ -1,9 +1,12 @@
1
+ import type { BrowserSessionConfig } from '@opencraw/core';
1
2
  import type { SampleBudget } from '../studio-api/index.js';
2
- import type { SampleRunRecord, SampleRunResult } from './sample-run-record.contract.js';
3
+ import type { SampleRunRecord, SampleRunRejected, SampleRunResult } from './sample-run-record.contract.js';
3
4
  /** Where trace lines and records go as a sample run proceeds. */
4
5
  export interface SampleRunCallbacks {
5
6
  onTraceLine: (line: string) => void;
6
7
  onRecord: (record: SampleRunRecord) => void;
8
+ /** A record was rejected (a field's `skip-record` missing/coercion policy); optional, most callers only need the final `rejectedRecords` list. */
9
+ onRejected?: (rejected: SampleRunRejected) => void;
7
10
  }
8
11
  /** A sample run in progress. */
9
12
  export interface SampleRunHandle {
@@ -13,18 +16,23 @@ export interface SampleRunHandle {
13
16
  }
14
17
  /**
15
18
  * Runs one input recipe of a workspace folder as a sample: `debug: true` (so
16
- * each `record:emit` carries its data), an in-memory sink, and `budget` as
17
- * the crawler's `sample` (`@opencraw/core`'s `CrawlOptions.sample`, #89).
18
- * Every engine event becomes a trace line with the same `traceLine`
19
- * formatter the cli's `--trace` uses; every emitted record reaches
20
- * `callbacks.onRecord` as it happens, not only in the final result.
19
+ * each `record:emit` carries its scope snapshot and mapping trace, and each
20
+ * `record:reject` its scope), an in-memory sink, and `budget` as the
21
+ * crawler's `sample` (`@opencraw/core`'s `CrawlOptions.sample`, #89). Every
22
+ * engine event becomes a trace line with the same `traceLine` formatter the
23
+ * cli's `--trace` uses; every emitted record reaches `callbacks.onRecord` as
24
+ * it happens, not only in the final result. Step events (`step:start`,
25
+ * `step:skip`, `step:retry`) are aggregated by path into `result.steps`, so
26
+ * `explain-why` (#92) can later say whether the step that bound a mapping's
27
+ * source id ran, was skipped, or retried — without a second run.
21
28
  *
22
29
  * @param folder - The workspace folder the recipe lives in.
23
30
  * @param recipeId - The input recipe to run.
24
31
  * @param budget - The sample budget; unset runs the recipe to completion (still capped by its own `limits`).
25
32
  * @param callbacks - Where trace lines and records go as the run proceeds.
33
+ * @param browser - Browser launch settings for a web-mode recipe (an executable path override, headless…); the server-wide setting `studio-http.use-case.ts`'s `StudioServerOptions.browser` carries in, e.g. `OPENCRAW_CHROMIUM` in a sandbox with no full Playwright install.
26
34
  * @returns A handle: `stop` closes the crawler cleanly, `result` resolves when the run ends.
27
35
  * @throws RecipeValidationError, RecipeBindingError, Error when the recipe or its output cannot be loaded or bound.
28
36
  */
29
- export declare function runSample(folder: string, recipeId: string, budget: SampleBudget | undefined, callbacks: SampleRunCallbacks): Promise<SampleRunHandle>;
37
+ export declare function runSample(folder: string, recipeId: string, budget: SampleBudget | undefined, callbacks: SampleRunCallbacks, browser?: BrowserSessionConfig): Promise<SampleRunHandle>;
30
38
  //# sourceMappingURL=run-sample.use-case.d.ts.map
@@ -1,10 +1,37 @@
1
- /** One record streamed or kept from a sample run. */
1
+ import type { MappingTrace } from '@opencraw/core';
2
+ /**
3
+ * One record streamed or kept from a sample run. `scope` (the snapshot it
4
+ * was mapped from) and `mapping` (each field's source value and its value
5
+ * after every transform, `@opencraw/core`'s `record:emit.mapping`, core
6
+ * since 0.1.12) are always present: `runSample` runs with `debug: true`.
7
+ * Kept per record (studio plan §4.2/§4.4, issue #92) so the Record tab can
8
+ * show a transform chain's real values and the Why? tab can explain a
9
+ * missing one, without a second run.
10
+ */
2
11
  export interface SampleRunRecord {
3
12
  key: string | null;
4
13
  data: Record<string, unknown>;
14
+ scope?: Record<string, unknown>;
15
+ mapping?: MappingTrace;
16
+ }
17
+ /** One record a sample run rejected (`RecordRejectedError`, a field's `skip-record` policy), kept for the Why? tab. */
18
+ export interface SampleRunRejected {
19
+ field: string;
20
+ reason: string;
21
+ url: string;
22
+ scope?: Record<string, unknown>;
5
23
  }
6
24
  /** A sample run's stopping reason: mirrors `@opencraw/core`'s `RecipeReport.stoppedBy`. */
7
25
  export type SampleStoppedBy = 'sample-maxRecords' | 'sample-maxPages' | 'sample-maxMs';
26
+ /** One outcome of a step during a sample run, aggregated over every time its path ran: whether it ever ran cleanly, was skipped, or retried, kept for the Why? tab (issue #92) — which step bound a mapping's source id, and whether it found nothing. */
27
+ export interface SampleStepSummary {
28
+ path: string;
29
+ stepType: string;
30
+ stepId?: string;
31
+ as?: string;
32
+ outcome: 'ran' | 'skipped' | 'retried';
33
+ error?: string;
34
+ }
8
35
  /** What a sample run finished with. */
9
36
  export interface SampleRunResult {
10
37
  recipeId: string;
@@ -15,5 +42,7 @@ export interface SampleRunResult {
15
42
  error?: string;
16
43
  stoppedBy?: SampleStoppedBy;
17
44
  records: SampleRunRecord[];
45
+ rejectedRecords: SampleRunRejected[];
46
+ steps: SampleStepSummary[];
18
47
  }
19
48
  //# sourceMappingURL=sample-run-record.contract.d.ts.map
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Which class tokens are worth building a selector on. A hand-authored class
3
+ * (`product_pod`, `price_color`) survives a rebuild; a generated one
4
+ * (a content hash, a CSS-module suffix, a styled-components token) does not,
5
+ * so a candidate built on it looks fine on the snapshot it was picked from
6
+ * and breaks on the very next deploy. This is the "generated/hashed class
7
+ * names" tradeoff issue #91 calls out explicitly: the heuristics below are a
8
+ * reasoned judgement call, not a guarantee — see the doc comments on each
9
+ * pattern for what real-world tool it targets.
10
+ */
11
+ /**
12
+ * Whether a class token looks hand-authored and safe to build a selector on.
13
+ *
14
+ * @param token - One class, no surrounding whitespace.
15
+ * @returns `false` for anything matching a known generated-class shape.
16
+ */
17
+ export declare function isStableClassToken(token: string): boolean;
18
+ /**
19
+ * The subset of `tokens` worth ranking a selector candidate on, order kept.
20
+ *
21
+ * @param tokens - An element's class list.
22
+ * @returns Only the stable tokens.
23
+ */
24
+ export declare function stableClasses(tokens: readonly string[]): string[];
25
+ /**
26
+ * Whether two class lists describe "the same kind of element" for list
27
+ * inference: same stable classes, generated tokens (which often differ
28
+ * per-instance, e.g. a React `key`-derived class) ignored on both sides.
29
+ *
30
+ * @param a - One element's classes.
31
+ * @param b - Another element's classes.
32
+ * @returns Whether their stable class sets are equal, order ignored.
33
+ */
34
+ export declare function sameClassSignature(a: readonly string[], b: readonly string[]): boolean;
35
+ //# sourceMappingURL=class-token.policy.d.ts.map
@@ -0,0 +1,73 @@
1
+ import type { AnyNode } from 'domhandler';
2
+ import type { Cheerio, CheerioAPI } from 'cheerio';
3
+ /**
4
+ * One level of a node's ancestry, root first: the element's own shape (tag,
5
+ * id, classes, attributes) plus where it sits among its same-tag siblings.
6
+ * Built over parsed HTML (cheerio), never a live DOM: the studio infers
7
+ * selectors from the snapshot the server captured, not from a browser
8
+ * window in the UI process (studio plan §3.1/§3.2, issue #91).
9
+ */
10
+ export interface DomPathLevel {
11
+ tag: string;
12
+ id?: string;
13
+ classes: string[];
14
+ attrs: Record<string, string>;
15
+ /** 1-based position among same-tag siblings under the same parent (an `nth-of-type` index). */
16
+ index: number;
17
+ /** How many same-tag siblings share the parent, `index` included. */
18
+ siblingCount: number;
19
+ }
20
+ /** A node's whole ancestry, root first, the node itself last. */
21
+ export type DomPath = DomPathLevel[];
22
+ /**
23
+ * The path from the document root to `node`, one level per ancestor. Two
24
+ * elements that share an ancestor produce identical levels up to that
25
+ * ancestor and diverge exactly where their parents stop being the same node
26
+ * — `list-inference.algorithm.ts` leans on that to find the nearest pair of
27
+ * siblings two picks belong to, without ever touching cheerio again.
28
+ *
29
+ * @param $ - The parsed document.
30
+ * @param node - The element to describe.
31
+ * @returns The path, root first.
32
+ */
33
+ export declare function domPathOf($: CheerioAPI, node: Cheerio<AnyNode>): DomPath;
34
+ /**
35
+ * Whether `id` looks handed by a framework (`react-`, a CSS-module or a
36
+ * `radix-`/`mui-` autogenerated one) rather than by the site's own markup —
37
+ * `class-token.policy.ts` has the equivalent check for class tokens, kept
38
+ * separate because ids and classes are ranked at different tiers.
39
+ */
40
+ export declare function isStableId(id: string): boolean;
41
+ /**
42
+ * Whether `path` failed to reach the real document root. `domPathOf` walks
43
+ * up through cheerio's own `.parent()`, which (matching real DOM semantics)
44
+ * never crosses from a `<template>`'s content into the light DOM outside
45
+ * it — a `<template shadowrootmode>` is exactly declarative shadow DOM, the
46
+ * only shadow-DOM shape that can show up in static, server-captured markup
47
+ * at all (imperative `attachShadow()` content never serialises into
48
+ * `page.content()`/a fetched response in the first place, so that case
49
+ * cannot reach this slice and needs no separate detection). A path whose
50
+ * first level is not `html` was cut short at exactly this kind of boundary:
51
+ * v1 does not pierce it, so callers report a clear "shadow DOM is not
52
+ * supported" error instead of building ancestor-based candidates over a
53
+ * chain that stops partway up.
54
+ */
55
+ export declare function crossesShadowRoot(path: DomPath): boolean;
56
+ /** Parses `html` and returns cheerio's API, for callers that need more than one path from the same document. */
57
+ export declare function parseDocument(html: string): CheerioAPI;
58
+ /**
59
+ * The path to the element the snapshot stamped with `nodeId` (its
60
+ * `data-oc-node` attribute — `@opencraw/core`'s `NODE_ID_ATTRIBUTE`,
61
+ * `packages/studio/src/page-snapshot/rewrite-document.mapper.ts`). This is
62
+ * how a click in the content pane's iframe — which only ever needs to read
63
+ * that one attribute off the element the user clicked — turns into
64
+ * something `selector-inference`'s other algorithms can work with, without
65
+ * the browser-side picker script needing to know anything about ancestry,
66
+ * classes or candidate ranking itself.
67
+ *
68
+ * @param html - The snapshot document (already stamped with node ids).
69
+ * @param nodeId - The `data-oc-node` value to find.
70
+ * @returns The node's path, or `undefined` when no element carries that id.
71
+ */
72
+ export declare function pathToNode(html: string, nodeId: string): DomPath | undefined;
73
+ //# sourceMappingURL=dom-path.model.d.ts.map
@@ -0,0 +1,12 @@
1
+ export { domPathOf, isStableId, crossesShadowRoot, parseDocument, pathToNode } from './dom-path.model.js';
2
+ export type { DomPath, DomPathLevel } from './dom-path.model.js';
3
+ export { takeKindFor } from './take-kind.policy.js';
4
+ export { isStableClassToken, stableClasses, sameClassSignature } from './class-token.policy.js';
5
+ export { ShadowDomUnsupportedError } from './shadow-dom.error.js';
6
+ export { candidatesFor } from './selector-candidates.algorithm.js';
7
+ export type { SelectorCandidate, CandidateTier } from './selector-candidates.algorithm.js';
8
+ export { rankCandidates, bestCandidate } from './selector-ranking.policy.js';
9
+ export type { RankedCandidate } from './selector-ranking.policy.js';
10
+ export { inferList } from './list-inference.algorithm.js';
11
+ export type { ListInferenceResult } from './list-inference.algorithm.js';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,43 @@
1
+ import type { DomPath } from './dom-path.model.js';
2
+ export interface ListInferenceResult {
3
+ /**
4
+ * The item's shape, generalised across both picks (root first, item
5
+ * last): only the tag, the stable classes and the attributes the two
6
+ * picked items actually share survive the merge. An id or a `data-id`
7
+ * that differs per item (the ordinary case) is dropped here on purpose —
8
+ * built into a selector it would match one item and silently miss every
9
+ * other, which is a shifted-field-shaped bug in its own right even though
10
+ * it is not the one issue #91 names.
11
+ */
12
+ itemPath: DomPath;
13
+ /** The field's shape from the item down to the click, generalised the same way as `itemPath`, for a selector that reads `from` the item and still works on every item, not just the one it was picked from. */
14
+ fieldPath: DomPath;
15
+ /** The concrete path of the first click, item-relative (index 0 = the item), kept for provenance (e.g. the value the pick actually read). */
16
+ fieldPathA: DomPath;
17
+ /** Same, for the second click. */
18
+ fieldPathB: DomPath;
19
+ }
20
+ /**
21
+ * Infers "these two picks are the same field on two items of a list" from
22
+ * two node paths, by walking up from both to the nearest ancestors that are
23
+ * real siblings (the same parent) with the same tag and stable-class
24
+ * signature — studio plan §3.2, issue #91. That ancestor is the item; the
25
+ * remainder of each path is the field, relative to it.
26
+ *
27
+ * Because a `DomPath` is built root-down (`dom-path.model.ts`), two paths
28
+ * from the same document are byte-for-byte identical up to their real
29
+ * common ancestor and diverge exactly at its differing children — so the
30
+ * *first* level where they differ is always the nearest-sibling test point;
31
+ * there is no need to look further down. This also makes the "wrapper trap"
32
+ * (an extra single-child element around each item) resolve itself: a
33
+ * wrapper's own instances are never one another's siblings, only the
34
+ * genuine repeating element is, so the walk naturally lands there instead of
35
+ * on the wrapper (see the wrapper-trap fixture in this slice's tests).
36
+ *
37
+ * @param pathA - The first click's path.
38
+ * @param pathB - The second click's path.
39
+ * @returns The item and field shapes (generalised) plus each pick's raw path, or `null` when the two picks are not on a coherent list (different branches entirely, the same node twice, or one path nested inside the other).
40
+ * @throws ShadowDomUnsupportedError when either path crosses a declarative shadow root.
41
+ */
42
+ export declare function inferList(pathA: DomPath, pathB: DomPath): ListInferenceResult | null;
43
+ //# sourceMappingURL=list-inference.algorithm.d.ts.map
@@ -0,0 +1,22 @@
1
+ import type { DomPath } from './dom-path.model.js';
2
+ /** The engine only ever runs `css` selectors from the studio (studio plan §3.2); xpath is a separate, text-only fallback the UI builds by hand. */
3
+ export type CandidateTier = 'id' | 'data-attr' | 'class' | 'structure' | 'position';
4
+ export interface SelectorCandidate {
5
+ selector: string;
6
+ tier: CandidateTier;
7
+ }
8
+ /**
9
+ * Every selector candidate worth trying for the last node of `path`, ranked
10
+ * best-first: `id` > stable `data-*`/`itemprop` attributes > stable class
11
+ * tokens > ancestor structure (`parent > tag`) > position (`nth-of-type`) as
12
+ * a last resort (issue #91's ranking, studio plan §3.2). Structure and
13
+ * position candidates only look as far up as `path` itself goes — pass a
14
+ * sub-path (e.g. from an item's root down) to keep a field's selector
15
+ * relative to its item instead of absolute to the document.
16
+ *
17
+ * @param path - The node's ancestry, root (or scope root) first, itself last.
18
+ * @param includePosition - `false` for an item selector: a position candidate would only ever match one of the items, which is never what an item selector should do.
19
+ * @returns Candidates, best tier first; ties keep this function's own order.
20
+ */
21
+ export declare function candidatesFor(path: DomPath, includePosition?: boolean): SelectorCandidate[];
22
+ //# sourceMappingURL=selector-candidates.algorithm.d.ts.map
@@ -0,0 +1,27 @@
1
+ import type { SelectorCandidate } from './selector-candidates.algorithm.js';
2
+ export interface RankedCandidate extends SelectorCandidate {
3
+ /** How many elements this candidate matches in the document it was verified against. */
4
+ matches: number;
5
+ /** Whether it matches `targetNodeId` (when one was given); a candidate that misses its own target is worse than useless. */
6
+ hitsTarget: boolean;
7
+ }
8
+ /**
9
+ * Verifies and orders selector candidates against real markup, by running
10
+ * each one through the engine's own CSS matching (`@opencraw/core`'s
11
+ * `countMatches`/`matchesOf`) rather than reimplementing it — issue #91's
12
+ * "every candidate is verified" requirement.
13
+ *
14
+ * Ordering: candidates that do not match `targetNodeId` (when given) sort
15
+ * last; among the rest, best tier first, and within a tier the fewest
16
+ * matches first (the most specific selector that still hits the target).
17
+ * A candidate matching nothing is dropped entirely: it is not a real option.
18
+ *
19
+ * @param html - The document to verify against (the snapshot, or the live page for the second, cross-check pass).
20
+ * @param candidates - Unranked candidates, e.g. from `candidatesFor`.
21
+ * @param targetNodeId - The `data-oc-node` id the pick landed on, when the document carries those stamps (the snapshot does; a live page fetched separately does not).
22
+ * @returns Every candidate that matched at least one element, ranked best first.
23
+ */
24
+ export declare function rankCandidates(html: string, candidates: readonly SelectorCandidate[], targetNodeId?: string): RankedCandidate[];
25
+ /** The single best candidate, or `undefined` when none of them matched anything. */
26
+ export declare function bestCandidate(html: string, candidates: readonly SelectorCandidate[], targetNodeId?: string): RankedCandidate | undefined;
27
+ //# sourceMappingURL=selector-ranking.policy.d.ts.map
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Thrown when a pick or a list-inference walk crosses a declarative shadow
3
+ * root (`<template shadowrootmode>`). v1 does not pierce shadow DOM: rather
4
+ * than infer a selector that silently matches the wrong thing (or nothing,
5
+ * once the page hydrates), the studio surfaces this and stops.
6
+ */
7
+ export declare class ShadowDomUnsupportedError extends Error {
8
+ readonly path: string;
9
+ constructor(path: string);
10
+ }
11
+ //# sourceMappingURL=shadow-dom.error.d.ts.map
@@ -0,0 +1,12 @@
1
+ import type { DomPathLevel } from './dom-path.model.js';
2
+ /**
3
+ * What a `Read` card's `take` should be for the element a pick landed on
4
+ * (issue #91, studio plan §3.2): `attr:href` for an `<a>`, `attr:src` for an
5
+ * `<img>`, `attr:<name>` for the first `data-*` attribute otherwise, `json`
6
+ * for a `<script type="application/json">`, else the default, `text`.
7
+ *
8
+ * @param level - The clicked element's own level (the last entry of its `DomPath`).
9
+ * @returns The `take` value to put on the `extract` step.
10
+ */
11
+ export declare function takeKindFor(level: DomPathLevel): string;
12
+ //# sourceMappingURL=take-kind.policy.d.ts.map