@apifuse/provider-sdk 2.2.0-beta.63 → 2.2.0-beta.64

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/src/types.ts CHANGED
@@ -246,6 +246,25 @@ export interface OcrCaptchaResult {
246
246
  }
247
247
 
248
248
  export interface OcrContext {
249
+ /**
250
+ * Whether this context is backed by a configured OCR backend.
251
+ *
252
+ * The SDK owns this verdict: clients built by `createOcrClientFromEnv` for a
253
+ * configured backend report `true`; the unsupported/error clients handed to
254
+ * undeclared or misconfigured providers report `false`. Providers branch on
255
+ * this flag (for example, "require manual CAPTCHA input") instead of probing
256
+ * the engine-owned `APIFUSE__OCR__*` environment names themselves.
257
+ *
258
+ * `true` promises a configured backend, not a successful call: upstream
259
+ * failures still surface as errors from `recognize` / `extractCaptchaText`.
260
+ *
261
+ * Implementations must expose this as an own enumerable data property (not a
262
+ * getter or prototype member) so that wrappers which spread the context, such
263
+ * as `bindOcrTelemetry`, preserve it. Custom `OcrContext` overrides passed to
264
+ * `serve({ ocr })`, `createFlowContext({ ocr })`, or test contexts must set it
265
+ * explicitly.
266
+ */
267
+ readonly available: boolean;
249
268
  recognize(request: OcrRecognizeRequest): Promise<OcrResult>;
250
269
  extractCaptchaText(
251
270
  image: OcrImageInput,
@@ -796,6 +815,16 @@ export type HealthCheckCase<TInput = unknown, TOutput = unknown> = {
796
815
  *
797
816
  * MUST NOT access scheduler, recorder, or any runtime type — pure data
798
817
  * + lambda only.
818
+ *
819
+ * NOTE: a closure cannot cross the registry's serialization boundary, so
820
+ * the platform health monitor cannot execute this hook — it runs a case's
821
+ * serialized `scenario` and nothing else. A case that carries only
822
+ * `assertions` is still published as a probe, but its outcome is
823
+ * permanently `unknown` / `monitoring_unavailable`, so the operation
824
+ * reads as monitored while nothing is being checked. This hook runs only
825
+ * in the provider runtime's own self-test
826
+ * (`/internal/health/self-test`). Express anything that must be
827
+ * monitored in production as a `scenario`.
799
828
  */
800
829
  assertions: (
801
830
  ctx: HealthCheckAssertionContext<TOutput>,
@@ -0,0 +1,100 @@
1
+ import { createRequire } from "node:module";
2
+
3
+ const requireModule = createRequire(import.meta.url);
4
+
5
+ /** The TypeScript compiler API surface the source-level lint rules parse with. */
6
+ export type TypeScriptModule = typeof import("typescript");
7
+
8
+ /**
9
+ * What a caller hands over as a compiler module. Typed structurally so the
10
+ * option does not depend on which `typescript` package the caller's own type
11
+ * resolution lands on: under a `typescript@7` pin `typeof import("typescript")`
12
+ * is the native shell's version module, not the compiler API. The members are
13
+ * optional for the same reason — the shell has none of them — and
14
+ * `isTypeScriptCompilerModule` decides at runtime whether the module can parse.
15
+ */
16
+ export type TypeScriptCompilerModuleLike = {
17
+ readonly createSourceFile?: (...parameters: never[]) => unknown;
18
+ readonly ScriptTarget?: object;
19
+ readonly version?: string;
20
+ };
21
+
22
+ /**
23
+ * Package specifiers that may carry the compiler API, in resolution order.
24
+ *
25
+ * `typescript@7` (the Go-native toolchain) resolves but its JavaScript entry
26
+ * exports only `version`/`getExePath` — no `createSourceFile`, so the lint
27
+ * rules cannot parse with it. The compiler API of that generation is published
28
+ * as `@typescript/typescript6`, which is what a `typescript@7` dependency tree
29
+ * (for example the APIFuse monorepo root) installs alongside the shell.
30
+ */
31
+ export const TYPESCRIPT_COMPILER_MODULE_SPECIFIERS = [
32
+ "typescript",
33
+ "@typescript/typescript6",
34
+ ] as const;
35
+
36
+ /**
37
+ * True when `candidate` is a loaded module that exposes the parser the lint
38
+ * rules need. Internal (narrows to the `typescript` module type, which is not
39
+ * part of the package surface); `lintRuntimeBoundarySources` applies it to the
40
+ * caller-supplied option.
41
+ */
42
+ export function isTypeScriptCompilerModule(candidate: unknown): candidate is TypeScriptModule {
43
+ return (
44
+ typeof candidate === "object" &&
45
+ candidate !== null &&
46
+ typeof (candidate as { createSourceFile?: unknown }).createSourceFile === "function" &&
47
+ typeof (candidate as { ScriptTarget?: unknown }).ScriptTarget === "object"
48
+ );
49
+ }
50
+
51
+ function describeCandidate(candidate: unknown): string {
52
+ if (typeof candidate === "object" && candidate !== null) {
53
+ const version = (candidate as { version?: unknown }).version;
54
+ if (typeof version === "string") return `resolved ${version} without a compiler API`;
55
+ }
56
+ return "resolved without a compiler API";
57
+ }
58
+
59
+ /**
60
+ * Resolve the compiler API through `load`, trying each specifier in order and
61
+ * accepting the first module that exposes `createSourceFile`. Exposed for the
62
+ * tests; production code uses `getTypeScript()`.
63
+ */
64
+ export function loadTypeScriptModule(
65
+ load: (specifier: string) => unknown,
66
+ specifiers: readonly string[] = TYPESCRIPT_COMPILER_MODULE_SPECIFIERS,
67
+ ): TypeScriptModule {
68
+ const attempts: string[] = [];
69
+ for (const specifier of specifiers) {
70
+ let candidate: unknown;
71
+ try {
72
+ candidate = load(specifier);
73
+ } catch {
74
+ attempts.push(`${specifier}: not resolvable`);
75
+ continue;
76
+ }
77
+ if (isTypeScriptCompilerModule(candidate)) return candidate;
78
+ attempts.push(`${specifier}: ${describeCandidate(candidate)}`);
79
+ }
80
+ throw new Error(
81
+ `The provider source lint needs the TypeScript compiler API (createSourceFile) and none of the resolvable packages provide it — ${attempts.join("; ")}. Install typescript 6.x as a devDependency, or @typescript/typescript6 next to a typescript 7 toolchain, in the dependency tree that installs @apifuse/provider-sdk.`,
82
+ );
83
+ }
84
+
85
+ /**
86
+ * Lazily loaded TypeScript compiler API shared by the source-level lint rules.
87
+ *
88
+ * `typeof import(...)` keeps the type without emitting a static import: the
89
+ * typescript package is a devDependency of the SDK and only the CLI and the
90
+ * lint rules need it at runtime, which the typescript-import-boundary test
91
+ * enforces for production sources. Resolution starts at the installed SDK, so
92
+ * the caller's dependency tree must provide one of
93
+ * `TYPESCRIPT_COMPILER_MODULE_SPECIFIERS`.
94
+ */
95
+ let typeScriptModule: TypeScriptModule | undefined;
96
+
97
+ export function getTypeScript(): TypeScriptModule {
98
+ typeScriptModule ??= loadTypeScriptModule(requireModule);
99
+ return typeScriptModule;
100
+ }