Kitsune

Plugin API

A plugin is PluginMeta & PluginHooks. Missing hooks are no-ops. The package entrypoint exports the types and helpers below, along with PLUGIN_API_VERSION = 1.

import type { KitsunePlugin } from 'kitsune'

declare function definePlugin(plugin: KitsunePlugin): KitsunePlugin

definePlugin returns the same object and throws if name is missing.

Metadata

FieldTypeMeaning
namestringRequired stable identity; duplicate names are rejected by the host.
versionstring (optional)Displayed by kitsune plugins.
enforce'pre' | 'post' (optional)Pre hooks first, omitted normal hooks next, post hooks last.
descriptionstring (optional)Human description in the plugin listing.
sitePluginSite (optional)URL ownership; bypasses the generic crawl ladder.
solverboolean (optional)Marks a solver eligible to become the single active solver.
type PluginSite = {
  match: (url: URL) => boolean
  crawl: (ctx: { kit: PluginApi; cdp: Cdp; url: string; value?: string }) => Promise<{
    verdict: Verdict
    reason: string
    tier?: string
    data: unknown
  }>
}

The first matching site plugin in host order takes over. Its default record tier is plugin:${plugin.name}; a returned tier overrides it. The shipped repuve plugin returns ui.

Hooks

Hook signatureWhen it runsReturns
launch(args: string[], opts: { headless: boolean })Before Chrome starts. May mutate args or replace the array.string[] | void
setup(ctx: PluginContext)During prepare after launch; also immediately for kit.use / kit.useSolver.void | Promise<void>
navigate(info: NavigateInfo, ctx: PluginContext)After kit.goto (including reload); the generic crawler's direct CDP navigations do not call it.void | Promise<void>
response(info: ResponseInfo, ctx: PluginContext)Browser attempt after classification and any solver re-extraction. Not called for HTTP or site takeover.void | Promise<void>
challenge(info: ChallengeInfo, ctx: PluginContext)Only the active solver; generic crawl calls it for a challenge with a known kind.Promise<SolveResult> | SolveResult
record(record: Record_, ctx: PluginContext)Finished site/browser record, in host order. Successful HTTP returns bypass this hook.Record_ | void
close(ctx: PluginContext)Session close, reverse host order. Errors are logged and teardown continues.void | Promise<void>

All hooks are optional. Record hooks can mutate the record, return a replacement, or return nothing to keep the current record. They are typed as synchronous; the crawler awaits their result. Other non-close hook errors propagate from the host; the generic browser crawl catches attempt errors and can retry or return an error record.

type NavigateInfo = { url: string; status: number; settleMs: number }
type ResponseInfo = {
  url: string
  status: number
  verdict: Verdict
  reason: string
  text: string
}

kit.goto reports status 200. Browser response info also uses 200 and limits text to 4000 characters.

PluginContext and PluginApi

Context fieldTypeMeaning
kitPluginApiStable subset of the live kit, listed below.
cdpCdpLive protocol driver.
proxyProxyConfig (optional)Session proxy when supplied by the caller. kit.context() includes it; the generic crawl currently builds its context without it.
challengeChallengeInfo (optional)Filled by PluginHost.challenge for the active solver.
log(message: string, data?: unknown) => voidKit/crawler logger writes [plugin] messages to stderr.
type PluginApi = {
  goto(url: string, opts?: { settleMs?: number }): Promise<unknown>
  evaluate<T = any>(expression: string): Promise<T>
  click(selector: string, opts?: { timeoutMs?: number; force?: boolean }): Promise<unknown>
  type(selector: string, value: string, opts?: { timeoutMs?: number }): Promise<unknown>
  waitFor(selector: string, timeoutMs?: number): Promise<boolean>
  box(selector: string, timeoutMs?: number): Promise<{ x: number; y: number } | null>
  text(selector: string): Promise<string | null>
  exists(selector: string): Promise<boolean>
  url(): Promise<string>
  screenshot(file?: string): Promise<string>
  cdp: Cdp
}
type ProxyScheme = 'http' | 'https' | 'socks4' | 'socks5'
type ProxyConfig = {
  scheme: ProxyScheme
  host: string
  port: number
  username?: string
  password?: string
  raw: string
}

Plugins should use the passed kit/context. Token providers that need the same IP can accept an explicit proxy; the shipped solvers otherwise use ctx.proxy.

PluginHost

new PluginHost(plugins?: KitsunePlugin[]) defaults to an empty list. Ordering is stable within each enforce group. Registration chooses the first solver before ordering, so enforce: 'pre' on a later solver does not activate it. Only the active solver receives challenge calls, with no fallback to dormant vendors. prepare skips dormant solver setup; launch, ordinary run and close iterate their applicable hooks across registered plugins.

SignatureBehavior
add(plugin: KitsunePlugin): thisRejects missing/duplicate names, registers and sorts.
get all(): readonly KitsunePlugin[]Ordered registry.
get solver(): KitsunePlugin | undefinedActive solver.
get solvers(): readonly KitsunePlugin[]Registered solvers, active first.
useSolver(plugin: KitsunePlugin): thisRequires solver: true, registers if needed, selects it. Does not itself run setup.
siteFor(url: string): { plugin: KitsunePlugin; site: PluginSite } | undefinedFirst URL match; invalid URLs return undefined.
run<K extends HookName>(hook: K, ctx: PluginContext, ...args: any[]): Promise<void>HookName = keyof PluginHooks; invokes hooks with args followed by context, stops at first failure; ignores returns.
challenge(info: ChallengeInfo, ctx: PluginContext): Promise<SolveResult | undefined>Calls only active solver; undefined if it has no challenge hook.
launchArgs(args: string[], headless: boolean): string[]Applies launch hooks in order.
prepare(ctx: PluginContext): Promise<void>Runs setup once, except dormant solvers.
close(ctx: PluginContext): Promise<void>Reverse-order close, logs errors.

A solved result gets via: plugin.name if it omitted via. A failed result with a detail is logged. Switching through kit.useSolver runs setup immediately. The Kitsune.launch solver option only inserts that plugin into the temporary launch-hook host; the live host uses plugins. Include it in plugins and place it first for first-registration activation, or use kit.useSolver afterward.

Challenges and results

type ChallengeKind =
  | 'recaptcha-v2'
  | 'recaptcha-v3'
  | 'hcaptcha'
  | 'turnstile'
  | 'datadome'
  | 'cloudflare'
  | 'unknown'

type ChallengeInfo = {
  kind: ChallengeKind
  url: string
  status: number
  verdict: Verdict
  reason: string
  sitekey?: string
  action?: string
  matched?: string
}
type SolveStatus = 'solved' | 'skipped' | 'failed'
type SolveResult = {
  status: SolveStatus
  via?: string
  detail?: string
  costUsd?: number
  ms?: number
  token?: string
}

sitekey, action and matched hold widget metadata and the classification marker when available. The DOM reader is heuristic: it stores the first captured grecaptcha.execute/render argument in action; do not assume it is a verified reCAPTCHA action. Solvers report solved, skipped or failed; the host does not inject a returned token automatically. The current crawler re-extracts after any returned SolveResult, including skipped/failed, and records via (or solver) as meta.solved if the resulting classification is accepted. That metadata alone is not proof of a solve.

Challenge helpers

declare function readChallenge(
  cdp: Cdp,
  base: { url: string; status: number; verdict: Verdict; reason: string; matched?: string },
): Promise<ChallengeInfo>
declare function findTokenField(cdp: Cdp, kind: ChallengeKind): Promise<string | null>
declare function injectToken(cdp: Cdp, selector: string, token: string): Promise<boolean>
HelperBehavior
readChallengeInspects widgets, markup and iframe URLs; returns unknown when no kind is found or evaluation fails.
findTokenFieldReturns the first existing hCaptcha, reCAPTCHA or Turnstile response-field selector; null for unsupported kinds or absent fields.
injectTokenUses the native input/textarea setter when applicable and fires input/change. Returns false if absent or evaluation fails; does not submit or verify server acceptance.

The shipped capmonster, capsolver and twocaptcha factories mark themselves as solvers, read their respective API-key environment variables, request tokens, inject and verify the field, and optionally click a configured submit selector. No solver is loaded by default. repuve is a site plugin rather than a solver.

Example: tag a record

import { definePlugin } from 'kitsune'

export default definePlugin({
  name: 'acme:tag',
  record: (record) => ({
    ...record,
    meta: { ...record.meta, team: 'acme' },
  }),
})

This tags records that pass through record hooks (site/browser paths).

Example: add a launch flag

import { definePlugin } from 'kitsune'

export default definePlugin({
  name: 'acme:locale',
  enforce: 'pre',
  launch: (args) => [...args, '--lang=es-MX'],
})