Some sources put a text captcha in front of their data: an image with a few distorted letters. A Scraper for such a source requires a TextCaptchaSolver and leaves the choice of solver to whoever provides it.
Like every Capability, this keeps the Scraper honest about what it needs. It also keeps the cost decision out of the Scraper: a free local solver, a hosted model, or both in sequence.
The port
export class TextCaptchaSolver extends Context.Service<
TextCaptchaSolver,
{
readonly read: (
captcha: TextCaptcha
) => Effect.Effect<TextCaptchaReadingType, CaptchaUnreadable>;
}
>()("@scwrap/domain/TextCaptchaSolver") {}read takes a captcha and returns a reading, or fails with CaptchaUnreadable.
| Type | Fields |
|---|---|
TextCaptcha | image (bytes), mediaType (image/jpeg or image/png), optional length (how many characters), optional alphabet (which characters can occur). |
TextCaptchaReading | text, confidence from 0 to 1, solver (the name of the solver that read it). |
CaptchaUnreadable | reason, solver. |
Tell the solver what you know. length and alphabet are optional, but a solver reads better when it knows a captcha has exactly four capital letters.
Use it in a Scraper
A reading is a guess. The site decides whether it was right, so the Scraper submits it and retries with a new image on a miss.
Example (Reading one captcha)
const solver = yield* TextCaptchaSolver;
const reading = yield* solver.read(
new TextCaptcha({ image, length: 4, mediaType: "image/jpeg" })
);Wrap one attempt, fetching a new image, reading it and submitting the answer, in an Effect, and retry that Effect. Three rules:
- Bound the attempts.
Effect.retrywithtimesgives the Run a budget. Without one, a broken solver loops forever. - Retry only captcha misses. Pass a
whilepredicate that accepts a rejected or unreadable captcha and nothing else. ABlockedmust not be retried twelve times. - End with a declared error.
CaptchaUnreadableis the solver's error, not the Scraper's. Turn an exhausted budget into a Scraper's own error, such as aCaptchaUnsolvedwith the number of attempts, and declare it in the Definition.
Annotate each attempt's span, for example with Effect.annotateCurrentSpan("captcha.attempt", attempt), so a trace shows how many attempts a Run needed.
Solvers
| Solver | From | Cost | Good at |
|---|---|---|---|
| Template engine | @scwrap/captcha-templates | Free, local, fast | Fixed-font captchas of one site, after training on its samples. |
| Decision model | TextCaptchaSolver.fromDecisionModel | One model call per captcha | Captchas the templates are unsure about. |
TextCaptchaSolver.fromDecisionModel
Builds a solver from any DecisionModel, Effect's port for models that answer fixed questions with a confidence. It asks one question per character: "which character is the first, leftmost one?"
const clefSolver = yield* TextCaptchaSolver.fromDecisionModel({
prepare: upscaleCaptcha(4),
solver: "clef",
});| Option | Meaning |
|---|---|
solver | The name reported in readings and spans. |
prepare | Optional. Transforms the captcha before the model sees it, for example upscaling. |
It needs a DecisionModel in its requirements and a captcha with a length. The reading's confidence is the lowest confidence among its characters.
firstConfident
firstConfident chains solvers. It asks each stage in turn and returns the first reading that reaches the stage's minimumConfidence.
Example (Templates first, a model when they are unsure)
import { glyphModelSolver, upscaleCaptcha } from "@scwrap/captcha-templates";
import { firstConfident, TextCaptchaSolver } from "@scwrap/domain";
import { Effect, Layer } from "effect";
export const templatesThenClef = Layer.effect(
TextCaptchaSolver,
Effect.gen(function* templatesThenClef() {
const templates = yield* glyphModelSolver(siteGlyphModel, "templates");
const clef = yield* TextCaptchaSolver.fromDecisionModel({
prepare: upscaleCaptcha(4),
solver: "clef",
});
return firstConfident([
{ minimumConfidence: 0.85, solver: templates },
{ minimumConfidence: 0, solver: clef },
]);
})
).pipe(Layer.provide(Clef));siteGlyphModel is a glyph model trained for the site (Captcha templates), and Clef is the DecisionModel Layer from Workers AI.
How it decides:
- A stage that reads with enough confidence wins, and later stages are not asked.
- A stage that reads below its threshold is remembered as a fallback, and the next stage is asked.
- A stage that fails is skipped.
- When no stage is confident, the last reading below a threshold is returned. When every stage failed, the result is
CaptchaUnreadablewith every stage's reason.
On one site's 49 server-verified four-letter holdout captchas, the template engine read at least 95% and the Clef model 87.8%. Templates go first because they are free, and the model is asked only below a confidence of 0.85.
Tracing
Solvers annotate the current span with captcha.solver and captcha.confidence. A trace of a Run shows which solver read each captcha and how sure it was.
Source
- text-captcha-solver.ts, text-captcha.ts, text-captcha-reading.ts
- text-captcha-solver.test.ts: the
firstConfidentmodel test