@browserlessapi/inferlo 0.5.0 → 0.6.0

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 (83) hide show
  1. package/README.md +8 -2
  2. package/dist/ai/index.d.ts +7 -0
  3. package/dist/ai/index.js +68 -2
  4. package/dist/ai/index.js.map +1 -1
  5. package/dist/ai/public.d.ts +1 -0
  6. package/dist/ai/public.js.map +1 -1
  7. package/dist/api-version.d.ts +32 -0
  8. package/dist/api-version.js +45 -0
  9. package/dist/api-version.js.map +1 -0
  10. package/dist/app.d.ts +30 -3
  11. package/dist/app.js +210 -43
  12. package/dist/app.js.map +1 -1
  13. package/dist/audit.d.ts +50 -0
  14. package/dist/audit.js +92 -0
  15. package/dist/audit.js.map +1 -0
  16. package/dist/cache.d.ts +92 -0
  17. package/dist/cache.js +245 -0
  18. package/dist/cache.js.map +1 -0
  19. package/dist/cli/main.js +25 -4
  20. package/dist/cli/main.js.map +1 -1
  21. package/dist/describe.d.ts +42 -1
  22. package/dist/describe.js +25 -2
  23. package/dist/describe.js.map +1 -1
  24. package/dist/fastify.d.ts +10 -2
  25. package/dist/fastify.js +22 -4
  26. package/dist/fastify.js.map +1 -1
  27. package/dist/h3.d.ts +76 -0
  28. package/dist/h3.js +61 -0
  29. package/dist/h3.js.map +1 -0
  30. package/dist/hapi.d.ts +45 -0
  31. package/dist/hapi.js +57 -0
  32. package/dist/hapi.js.map +1 -0
  33. package/dist/index.d.ts +14 -1
  34. package/dist/index.js +5 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/jobs.d.ts +20 -0
  37. package/dist/jobs.js +19 -7
  38. package/dist/jobs.js.map +1 -1
  39. package/dist/koa.d.ts +51 -0
  40. package/dist/koa.js +45 -0
  41. package/dist/koa.js.map +1 -0
  42. package/dist/lambda.d.ts +110 -0
  43. package/dist/lambda.js +169 -0
  44. package/dist/lambda.js.map +1 -0
  45. package/dist/mcp.d.ts +1 -1
  46. package/dist/mcp.js +7 -3
  47. package/dist/mcp.js.map +1 -1
  48. package/dist/module.d.ts +52 -0
  49. package/dist/module.js +152 -0
  50. package/dist/module.js.map +1 -0
  51. package/dist/nextjs.d.ts +35 -0
  52. package/dist/nextjs.js +33 -0
  53. package/dist/nextjs.js.map +1 -0
  54. package/dist/openapi.d.ts +3 -0
  55. package/dist/openapi.js +12 -3
  56. package/dist/openapi.js.map +1 -1
  57. package/dist/permissions.d.ts +26 -0
  58. package/dist/permissions.js +78 -0
  59. package/dist/permissions.js.map +1 -0
  60. package/dist/react-router.d.ts +41 -0
  61. package/dist/react-router.js +42 -0
  62. package/dist/react-router.js.map +1 -0
  63. package/dist/request-matcher.d.ts +4 -0
  64. package/dist/request-matcher.js +14 -0
  65. package/dist/request-matcher.js.map +1 -0
  66. package/dist/route.d.ts +36 -1
  67. package/dist/route.js +24 -0
  68. package/dist/route.js.map +1 -1
  69. package/dist/serve.d.ts +20 -1
  70. package/dist/serve.js +48 -4
  71. package/dist/serve.js.map +1 -1
  72. package/dist/sessions.d.ts +72 -0
  73. package/dist/sessions.js +171 -0
  74. package/dist/sessions.js.map +1 -0
  75. package/dist/sveltekit.d.ts +72 -0
  76. package/dist/sveltekit.js +68 -0
  77. package/dist/sveltekit.js.map +1 -0
  78. package/dist/tracing.d.ts +118 -0
  79. package/dist/tracing.js +204 -0
  80. package/dist/tracing.js.map +1 -0
  81. package/llms.txt +7 -3
  82. package/package.json +44 -2
  83. package/templates/app/AGENTS.md +5 -3
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Inferlo inside SvelteKit (2 and 3) server routes, without a runtime dependency on SvelteKit:
3
+ *
4
+ * // src/routes/api/[...path]/+server.ts
5
+ * import { toSvelteKitHandlers } from "@browserlessapi/inferlo/sveltekit";
6
+ * import { app } from "$lib/server/inferlo";
7
+ *
8
+ * export const { GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, fallback } = toSvelteKitHandlers(app, { prefix: "/api" });
9
+ *
10
+ * // src/hooks.server.ts: event.locals.ai in load functions, form actions and +server.ts files
11
+ * import { inferloAI } from "@browserlessapi/inferlo/sveltekit";
12
+ * export const handle = inferloAI(ai);
13
+ *
14
+ * It never loads the router, database or jobs code.
15
+ */
16
+ import type { AI } from "./ai/index.ts";
17
+ import type { App as InferloApp } from "./app.ts";
18
+ declare global {
19
+ namespace App {
20
+ interface Locals {
21
+ /** The AI client from the inferloAI() handle hook of @browserlessapi/inferlo/sveltekit. */
22
+ ai: AI;
23
+ }
24
+ }
25
+ }
26
+ /** The parts of a SvelteKit RequestEvent Inferlo uses. */
27
+ export interface SvelteKitEventLike {
28
+ /** The incoming Web request. */
29
+ request: Request;
30
+ /** The client IP from the adapter; called once per request, and errors (some adapters have none) are ignored. */
31
+ getClientAddress?: () => string;
32
+ /** event.locals, where the inferloAI() hook puts ai. */
33
+ locals: object;
34
+ }
35
+ /** A SvelteKit request handler, to export from a +server.ts file. */
36
+ export type SvelteKitRequestHandler = (event: SvelteKitEventLike) => Promise<Response>;
37
+ /** One request handler per HTTP method and a fallback for every other method, to export from +server.ts. */
38
+ export interface SvelteKitHandlers {
39
+ GET: SvelteKitRequestHandler;
40
+ POST: SvelteKitRequestHandler;
41
+ PUT: SvelteKitRequestHandler;
42
+ PATCH: SvelteKitRequestHandler;
43
+ DELETE: SvelteKitRequestHandler;
44
+ HEAD: SvelteKitRequestHandler;
45
+ OPTIONS: SvelteKitRequestHandler;
46
+ fallback: SvelteKitRequestHandler;
47
+ }
48
+ export interface SvelteKitHandlersOptions {
49
+ /**
50
+ * The URL path of the route folder, removed before the app routes the request: "/api" for
51
+ * src/routes/api/[...path]/+server.ts. Requests outside it keep their path. Include SvelteKit's paths.base if set.
52
+ */
53
+ prefix?: string;
54
+ }
55
+ /** A SvelteKit handle hook: export const handle = inferloAI(ai), or sequence(inferloAI(ai), ...) to combine it. */
56
+ export type SvelteKitHandle = (input: {
57
+ event: {
58
+ locals: object;
59
+ };
60
+ resolve: (event: any, opts?: any) => Response | Promise<Response>;
61
+ }) => Response | Promise<Response>;
62
+ /**
63
+ * Request handlers that run a whole Inferlo app (routes, auth, validation, rate limits, OpenAPI and MCP) from a
64
+ * SvelteKit rest route: export const { GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, fallback } = toSvelteKitHandlers(app).
65
+ */
66
+ export declare function toSvelteKitHandlers(app: Pick<InferloApp<any>, "fetch">, options?: SvelteKitHandlersOptions): SvelteKitHandlers;
67
+ /**
68
+ * A SvelteKit handle hook that puts an AI client (createAI(...) or an Inferlo app's app.ai) on every request as
69
+ * event.locals.ai, for load functions, form actions and +server.ts files: export const handle = inferloAI(ai) in
70
+ * src/hooks.server.ts, or sequence(inferloAI(ai), yourHandle).
71
+ */
72
+ export declare function inferloAI(ai: AI): SvelteKitHandle;
@@ -0,0 +1,68 @@
1
+ import { assertAIClient } from "./ai-client.js";
2
+ import { clientIps } from "./client-ip.js";
3
+ import { InferloError } from "./errors.js";
4
+ import { normalizePrefix, stripPrefix } from "./serve.js";
5
+ const fix = 'Pass the app from createApp and the route folder\'s path: export const { GET, POST, fallback } = toSvelteKitHandlers(app, { prefix: "/api" }).';
6
+ /**
7
+ * Request handlers that run a whole Inferlo app (routes, auth, validation, rate limits, OpenAPI and MCP) from a
8
+ * SvelteKit rest route: export const { GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, fallback } = toSvelteKitHandlers(app).
9
+ */
10
+ export function toSvelteKitHandlers(app, options = {}) {
11
+ if (!app || typeof app.fetch !== "function") {
12
+ throw new InferloError("IL5022", "toSvelteKitHandlers() needs an Inferlo app (the value returned by createApp).", { fix });
13
+ }
14
+ if (options === null || typeof options !== "object" || (options.prefix !== undefined && (typeof options.prefix !== "string" || !options.prefix.startsWith("/")))) {
15
+ throw new InferloError("IL5022", 'toSvelteKitHandlers() prefix must be a path that starts with /, such as "/api".', { fix });
16
+ }
17
+ const prefix = normalizePrefix(options.prefix);
18
+ const handle = async (event) => {
19
+ const incoming = event?.request;
20
+ if (!(incoming instanceof Request)) {
21
+ throw new InferloError("IL5022", "toSvelteKitHandlers()'s handlers were called with something that is not a SvelteKit request event.", {
22
+ fix: "Export them from a +server.ts file so SvelteKit calls them: export const { GET, POST, fallback } = toSvelteKitHandlers(app, { prefix: \"/api\" }).",
23
+ });
24
+ }
25
+ let request = incoming;
26
+ if (prefix) {
27
+ const url = new URL(incoming.url);
28
+ const stripped = stripPrefix(url.pathname, prefix);
29
+ if (stripped !== null) {
30
+ url.pathname = stripped;
31
+ const hasBody = incoming.body !== null && incoming.method !== "GET" && incoming.method !== "HEAD";
32
+ const init = {
33
+ method: incoming.method,
34
+ headers: incoming.headers,
35
+ body: hasBody ? incoming.body : undefined,
36
+ signal: incoming.signal,
37
+ redirect: incoming.redirect,
38
+ };
39
+ if (hasBody)
40
+ init.duplex = "half";
41
+ request = new Request(url, init);
42
+ }
43
+ }
44
+ try {
45
+ const ip = event.getClientAddress?.();
46
+ if (typeof ip === "string" && ip)
47
+ clientIps.set(request, ip);
48
+ }
49
+ catch {
50
+ // The adapter cannot tell the client address (for example while prerendering).
51
+ }
52
+ return app.fetch(request);
53
+ };
54
+ return Object.freeze({ GET: handle, POST: handle, PUT: handle, PATCH: handle, DELETE: handle, HEAD: handle, OPTIONS: handle, fallback: handle });
55
+ }
56
+ /**
57
+ * A SvelteKit handle hook that puts an AI client (createAI(...) or an Inferlo app's app.ai) on every request as
58
+ * event.locals.ai, for load functions, form actions and +server.ts files: export const handle = inferloAI(ai) in
59
+ * src/hooks.server.ts, or sequence(inferloAI(ai), yourHandle).
60
+ */
61
+ export function inferloAI(ai) {
62
+ assertAIClient(ai, 'Pass createAI({ providers, models }) from "@browserlessapi/inferlo/ai", or an Inferlo app\'s app.ai: export const handle = inferloAI(ai).');
63
+ return ({ event, resolve }) => {
64
+ event.locals.ai = ai;
65
+ return resolve(event);
66
+ };
67
+ }
68
+ //# sourceMappingURL=sveltekit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sveltekit.js","sourceRoot":"","sources":["../src/svelt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GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;AACnJ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,EAAM;IAC9B,cAAc,CAAC,EAAE,EAAE,2IAA2I,CAAC,CAAC;IAChK,OAAO,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE;QAC3B,KAAK,CAAC,MAAsB,CAAC,EAAE,GAAG,EAAE,CAAC;QACtC,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * OpenTelemetry tracing, with no dependency: pass the OpenTelemetry API you already use.
3
+ *
4
+ * import * as otel from "@opentelemetry/api";
5
+ * createApp({ tracing: { api: otel } })
6
+ *
7
+ * Every request gets a server span ("GET /tickets/:id") that continues the caller's trace (W3C traceparent, through
8
+ * the propagator your OpenTelemetry SDK registers), runs the handler inside it so spans you or instrumentations
9
+ * start nest under it, and puts traceId and spanId on the request's log entries. AI calls get client spans with the
10
+ * model, tokens and cost (also with createAI({ tracing }) on its own), and database queries made while a request
11
+ * or job is traced get client spans with the table and operation. Without an SDK, the API's no-op tracer is used
12
+ * and nothing is recorded.
13
+ */
14
+ import type { Adapter } from "./db/types.ts";
15
+ /** The parts of a span Inferlo uses (an @opentelemetry/api Span). */
16
+ export interface OtelSpan {
17
+ spanContext(): {
18
+ traceId: string;
19
+ spanId: string;
20
+ traceFlags: number;
21
+ };
22
+ setAttribute(key: string, value: string | number | boolean): unknown;
23
+ setStatus(status: {
24
+ code: number;
25
+ message?: string;
26
+ }): unknown;
27
+ recordException(exception: unknown): void;
28
+ updateName(name: string): unknown;
29
+ end(): void;
30
+ isRecording(): boolean;
31
+ }
32
+ /** The parts of @opentelemetry/api Inferlo uses: import * as otel from "@opentelemetry/api", then { api: otel }. */
33
+ export interface OtelApi {
34
+ trace: {
35
+ getTracer(name: string, version?: string): {
36
+ startSpan(name: string, options?: {
37
+ kind?: number;
38
+ attributes?: Record<string, string | number | boolean>;
39
+ }, context?: unknown): OtelSpan;
40
+ };
41
+ setSpan(context: unknown, span: OtelSpan): unknown;
42
+ /** The span of a context. Database spans are only started inside one (a request or a job). */
43
+ getSpan?(context: unknown): OtelSpan | undefined;
44
+ };
45
+ context: {
46
+ active(): unknown;
47
+ with<T>(context: unknown, fn: () => T): T;
48
+ };
49
+ propagation: {
50
+ extract(context: unknown, carrier: unknown, getter?: unknown): unknown;
51
+ /** Writes the trace of a context into a carrier (traceparent), so a queued job continues it. */
52
+ inject?(context: unknown, carrier: unknown, setter?: unknown): void;
53
+ };
54
+ SpanKind: {
55
+ SERVER: number;
56
+ CLIENT?: number;
57
+ CONSUMER?: number;
58
+ };
59
+ SpanStatusCode: {
60
+ ERROR: number;
61
+ };
62
+ }
63
+ export interface TracingConfig {
64
+ /** The OpenTelemetry API: import * as otel from "@opentelemetry/api", then createApp({ tracing: { api: otel } }). */
65
+ api: OtelApi;
66
+ /** The tracer name. Default: the app's name (createApp), or "inferlo" (createAI on its own). */
67
+ tracerName?: string;
68
+ }
69
+ type Attributes = Record<string, string | number | boolean>;
70
+ export interface RequestTracing {
71
+ /** Starts the server span of a request and runs fn inside it. */
72
+ run<T>(req: Request, url: URL, fn: (span: OtelSpan) => Promise<T>): Promise<T>;
73
+ }
74
+ /** Checks a tracing option (createApp or createAI) and returns it, or undefined without tracing. */
75
+ export declare function checkTracing(option: unknown, where: "createApp" | "createAI"): TracingConfig | undefined;
76
+ /** Checks createApp({ tracing }) and returns the request tracer, or undefined without tracing. */
77
+ export declare function requestTracing(option: unknown, appName: string, appVersion: string): RequestTracing | undefined;
78
+ /** Client spans for calls Inferlo makes: AI models and the database. */
79
+ export interface ClientTracing {
80
+ /** Starts a client span under the active one. The caller ends it (with end or fail). */
81
+ start(name: string, attributes: Attributes): OtelSpan;
82
+ /** Runs fn inside a client span that is active while fn runs, and ends it; errors are recorded on it. */
83
+ run<T>(name: string, attributes: Attributes, fn: (span: OtelSpan) => Promise<T>): Promise<T>;
84
+ /** Records an error on a span and ends it. */
85
+ fail(span: OtelSpan, error: unknown): void;
86
+ /** True while a span (a request's or a job's) is active. */
87
+ inSpan(): boolean;
88
+ }
89
+ export declare function clientTracing(config: TracingConfig, defaultName: string, version?: string): ClientTracing;
90
+ /**
91
+ * Wraps a database adapter so its queries get client spans ("SELECT tickets") while a request or job is traced.
92
+ * Queries outside a trace (startup, the job worker polling for work) are not traced. Values are never recorded,
93
+ * only the parameterised SQL of raw queries.
94
+ */
95
+ export declare function traceAdapter(adapter: Adapter, tracing: ClientTracing): Adapter;
96
+ /** traceId and spanId for log entries, when the span belongs to a real trace. */
97
+ export declare function traceFields(span: OtelSpan | undefined): {
98
+ traceId?: string;
99
+ spanId?: string;
100
+ };
101
+ /** The trace a queued job carries from where it was enqueued (W3C trace context). */
102
+ export interface JobTraceContext {
103
+ traceparent?: string;
104
+ tracestate?: string;
105
+ }
106
+ export interface JobTracing {
107
+ /** The trace of the active span, to store with a job that is enqueued now. Empty outside a trace. */
108
+ capture(): JobTraceContext;
109
+ /** Runs one attempt of a job in a span that continues the trace it was enqueued in (cron jobs start a new trace). */
110
+ run<T extends string>(job: {
111
+ id: string;
112
+ name: string;
113
+ attempts: number;
114
+ trace?: JobTraceContext;
115
+ }, fn: (fail: (error: unknown) => void) => Promise<T>): Promise<T>;
116
+ }
117
+ export declare function jobTracing(config: TracingConfig, defaultName: string, version?: string): JobTracing;
118
+ export {};
@@ -0,0 +1,204 @@
1
+ import { InferloError } from "./errors.js";
2
+ /** CLIENT and CONSUMER in @opentelemetry/api's SpanKind (the same in every 1.x release). */
3
+ const CLIENT_KIND = 2;
4
+ const CONSUMER_KIND = 4;
5
+ /** Reads incoming trace headers (traceparent, tracestate, baggage) for the propagator. */
6
+ const headerGetter = {
7
+ get(carrier, key) {
8
+ return carrier.get(key) ?? undefined;
9
+ },
10
+ keys(carrier) {
11
+ return [...carrier.keys()];
12
+ },
13
+ };
14
+ const INVALID_TRACE = "00000000000000000000000000000000";
15
+ /** Checks a tracing option (createApp or createAI) and returns it, or undefined without tracing. */
16
+ export function checkTracing(option, where) {
17
+ if (option === undefined || option === false)
18
+ return undefined;
19
+ const config = option;
20
+ const api = config?.api;
21
+ const valid = config !== null &&
22
+ typeof config === "object" &&
23
+ typeof api?.trace?.getTracer === "function" &&
24
+ typeof api.trace.setSpan === "function" &&
25
+ typeof api.context?.with === "function" &&
26
+ typeof api.context.active === "function" &&
27
+ typeof api.propagation?.extract === "function" &&
28
+ typeof api.SpanKind?.SERVER === "number" &&
29
+ typeof api.SpanStatusCode?.ERROR === "number" &&
30
+ (config.tracerName === undefined || (typeof config.tracerName === "string" && config.tracerName.length > 0));
31
+ if (!valid) {
32
+ throw new InferloError("IL6030", "Invalid tracing options: tracing needs { api } with the OpenTelemetry API.", {
33
+ fix: `Install @opentelemetry/api (and an OpenTelemetry SDK that exports the spans), then: import * as otel from "@opentelemetry/api"; ${where}({ tracing: { api: otel } }).`,
34
+ });
35
+ }
36
+ return config;
37
+ }
38
+ /** Checks createApp({ tracing }) and returns the request tracer, or undefined without tracing. */
39
+ export function requestTracing(option, appName, appVersion) {
40
+ const config = checkTracing(option, "createApp");
41
+ if (!config)
42
+ return undefined;
43
+ const api = config.api;
44
+ const tracer = api.trace.getTracer(config.tracerName ?? appName, appVersion);
45
+ return {
46
+ run(req, url, fn) {
47
+ const method = req.method.toUpperCase();
48
+ const parent = api.propagation.extract(api.context.active(), req.headers, headerGetter);
49
+ const attributes = {
50
+ "http.request.method": method,
51
+ "url.path": url.pathname,
52
+ "url.scheme": url.protocol.replace(/:$/, ""),
53
+ "server.address": url.hostname,
54
+ };
55
+ if (url.port)
56
+ attributes["server.port"] = Number(url.port);
57
+ const agent = req.headers.get("user-agent");
58
+ if (agent)
59
+ attributes["user_agent.original"] = agent;
60
+ const span = tracer.startSpan(method, { kind: api.SpanKind.SERVER, attributes }, parent);
61
+ return api.context.with(api.trace.setSpan(parent, span), () => fn(span));
62
+ },
63
+ };
64
+ }
65
+ export function clientTracing(config, defaultName, version) {
66
+ const api = config.api;
67
+ const tracer = api.trace.getTracer(config.tracerName ?? defaultName, version);
68
+ const kind = api.SpanKind.CLIENT ?? CLIENT_KIND;
69
+ const fail = (span, error) => {
70
+ span.recordException(error instanceof Error ? error : String(error));
71
+ const code = error?.code;
72
+ if (typeof code === "string")
73
+ span.setAttribute("error.type", code);
74
+ else
75
+ span.setAttribute("error.type", error instanceof Error ? error.name : "Error");
76
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
77
+ span.end();
78
+ };
79
+ const start = (name, attributes) => tracer.startSpan(name, { kind, attributes }, api.context.active());
80
+ return {
81
+ start,
82
+ fail,
83
+ async run(name, attributes, fn) {
84
+ const span = start(name, attributes);
85
+ let result;
86
+ try {
87
+ result = await api.context.with(api.trace.setSpan(api.context.active(), span), () => fn(span));
88
+ }
89
+ catch (error) {
90
+ fail(span, error);
91
+ throw error;
92
+ }
93
+ span.end();
94
+ return result;
95
+ },
96
+ inSpan() {
97
+ if (!api.trace.getSpan)
98
+ return true;
99
+ const active = api.trace.getSpan(api.context.active());
100
+ return active !== undefined && active.spanContext().traceId !== INVALID_TRACE;
101
+ },
102
+ };
103
+ }
104
+ const SQL_OPERATION = /^\s*([a-z]+)/i;
105
+ /**
106
+ * Wraps a database adapter so its queries get client spans ("SELECT tickets") while a request or job is traced.
107
+ * Queries outside a trace (startup, the job worker polling for work) are not traced. Values are never recorded,
108
+ * only the parameterised SQL of raw queries.
109
+ */
110
+ export function traceAdapter(adapter, tracing) {
111
+ const system = adapter.dialect === "postgres" ? "postgresql" : adapter.dialect;
112
+ const traced = (operation, table, query, fn) => {
113
+ if (!tracing.inSpan())
114
+ return fn();
115
+ const attributes = { "db.system.name": system, "db.operation.name": operation };
116
+ if (table)
117
+ attributes["db.collection.name"] = table;
118
+ if (query)
119
+ attributes["db.query.text"] = query;
120
+ return tracing.run(table ? `${operation} ${table}` : operation, attributes, fn);
121
+ };
122
+ const wrapped = {
123
+ ...adapter,
124
+ insert: (model, row) => traced("INSERT", model.name, undefined, () => adapter.insert(model, row)),
125
+ select: (model, query) => traced("SELECT", model.name, undefined, () => adapter.select(model, query)),
126
+ update: (model, where, patch) => traced("UPDATE", model.name, undefined, () => adapter.update(model, where, patch)),
127
+ delete: (model, where) => traced("DELETE", model.name, undefined, () => adapter.delete(model, where)),
128
+ count: (model, where) => traced("COUNT", model.name, undefined, () => adapter.count(model, where)),
129
+ };
130
+ if (adapter.transaction)
131
+ wrapped.transaction = (fn) => adapter.transaction((tx) => fn(traceAdapter(tx, tracing)));
132
+ if (adapter.sql) {
133
+ wrapped.sql = (text, params) => traced((SQL_OPERATION.exec(text)?.[1] ?? "SQL").toUpperCase(), undefined, text, () => adapter.sql(text, params));
134
+ }
135
+ return wrapped;
136
+ }
137
+ /** traceId and spanId for log entries, when the span belongs to a real trace. */
138
+ export function traceFields(span) {
139
+ const context = span?.spanContext();
140
+ if (!context || context.traceId === INVALID_TRACE)
141
+ return {};
142
+ return { traceId: context.traceId, spanId: context.spanId };
143
+ }
144
+ const carrierGetter = {
145
+ get(carrier, key) {
146
+ return key === "traceparent" || key === "tracestate" ? carrier[key] : undefined;
147
+ },
148
+ keys(carrier) {
149
+ return Object.keys(carrier);
150
+ },
151
+ };
152
+ const TRACEPARENT = /^[0-9a-f]{2}-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$/;
153
+ export function jobTracing(config, defaultName, version) {
154
+ const api = config.api;
155
+ const tracer = api.trace.getTracer(config.tracerName ?? defaultName, version);
156
+ const kind = api.SpanKind.CONSUMER ?? CONSUMER_KIND;
157
+ return {
158
+ capture() {
159
+ if (!api.propagation.inject)
160
+ return {};
161
+ const carrier = {};
162
+ api.propagation.inject(api.context.active(), carrier);
163
+ const trace = {};
164
+ if (typeof carrier.traceparent === "string" && TRACEPARENT.test(carrier.traceparent)) {
165
+ trace.traceparent = carrier.traceparent;
166
+ if (typeof carrier.tracestate === "string" && carrier.tracestate.length <= 512)
167
+ trace.tracestate = carrier.tracestate;
168
+ }
169
+ return trace;
170
+ },
171
+ async run(job, fn) {
172
+ const parent = job.trace?.traceparent ? api.propagation.extract(api.context.active(), job.trace, carrierGetter) : api.context.active();
173
+ const span = tracer.startSpan(`process ${job.name}`, {
174
+ kind,
175
+ attributes: {
176
+ "messaging.system": "inferlo",
177
+ "messaging.operation.type": "process",
178
+ "messaging.destination.name": job.name,
179
+ "messaging.message.id": job.id,
180
+ "inferlo.job.attempt": job.attempts,
181
+ },
182
+ }, parent);
183
+ const fail = (error) => {
184
+ span.recordException(error instanceof Error ? error : String(error));
185
+ const code = error?.code;
186
+ span.setAttribute("error.type", typeof code === "string" ? code : error instanceof Error ? error.name : "Error");
187
+ span.setStatus({ code: api.SpanStatusCode.ERROR });
188
+ };
189
+ try {
190
+ const outcome = await api.context.with(api.trace.setSpan(parent, span), () => fn(fail));
191
+ span.setAttribute("inferlo.job.outcome", outcome);
192
+ return outcome;
193
+ }
194
+ catch (error) {
195
+ fail(error);
196
+ throw error;
197
+ }
198
+ finally {
199
+ span.end();
200
+ }
201
+ },
202
+ };
203
+ }
204
+ //# sourceMappingURL=tracing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracing.js","sourceRoot":"","sources":["../src/tracing.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAwC3C,4FAA4F;AAC5F,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB,MAAM,aAAa,GAAG,CAAC,CAAC;AAExB,0FAA0F;AAC1F,MAAM,YAAY,GAAG;IACnB,GAAG,CAAC,OAAgB,EAAE,GAAW;QAC/B,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,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,EAAE,CAAC,CAAC;YACrD,CAAC,CAAC;YACF,IAAI,CAAC;gBACH,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;gBACxF,IAAI,CAAC,YAAY,CAAC,qBAAqB,EAAE,OAAO,CAAC,CAAC;gBAClD,OAAO,OAAO,CAAC;YACjB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,KAAK,CAAC,CAAC;gBACZ,MAAM,KAAK,CAAC;YACd,CAAC;oBAAS,CAAC;gBACT,IAAI,CAAC,GAAG,EAAE,CAAC;YACb,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
package/llms.txt CHANGED
@@ -10,14 +10,18 @@ Key rules: routes are private unless `auth: "public"`; validate input with `t.*`
10
10
  - [Configuration](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/config.md): config({ schema, secrets, environments }), typed settings validated at startup, secrets redacted
11
11
  - [Schemas](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/schema.md): t.* types, modifiers, validation, JSON Schema
12
12
  - [Models and database](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/database.md): model(), queries, adapters, db push, transactions
13
- - [Routes](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/routes.md): options, handler context, return values, errors, middleware, deprecated routes (Deprecation and Sunset headers)
14
- - [Auth](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/auth.md): auth.resolve, roles, bearer API keys, passwords
13
+ - [Routes](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/routes.md): options, handler context, return values, errors, middleware, deprecated routes (Deprecation and Sunset headers), API versions with apiVersion("v1", routes) and an OpenAPI document per version
14
+ - [Auth](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/auth.md): auth.resolve, roles, permissions() with can() and authorize() and the route permission option, bearer API keys, passwords
15
15
  - [AI](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/ai.md): providers, aliases and fallback, structured output, streaming, embeddings, usage and budgets
16
+ - [Modules](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/modules.md): module("billing", { imports, models, routes, jobs, provides }) splits a large app into feature modules with explicit dependencies, createApp({ modules }), checked at startup and shown in describe
16
17
  - [MCP, OpenAPI and describe](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/mcp.md)
17
18
  - [CLI](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/cli.md)
18
19
  - [Testing](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/testing.md)
19
- - [Use with other frameworks](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/integrations.md): the AI module in any framework, toNodeHandler and fromNodeRequest to mount an app in Express, Fastify, NestJS or node:http, inferloAI() middleware for Hono (c.var.ai), Express (req.ai), Elysia (ai) and tRPC (ctx.ai), inferloPlugin for Fastify (request.ai, app under a prefix), InferloModule for NestJS, an AdonisJS provider (ctx.ai, config/ai.ts), InferloAI and inferloAI() for Ts.ED injection, trpcTools() for tRPC procedures as AI tools
20
+ - [Use with other frameworks](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/integrations.md): the AI module in any framework, toNodeHandler and fromNodeRequest to mount an app in Express, Fastify, NestJS or node:http (fallthrough: true passes unmatched requests to the host), inferloAI() middleware for Hono (c.var.ai), Express (req.ai), Koa (ctx.ai), Elysia (ai) and tRPC (ctx.ai), toKoaMiddleware() to mount an app in Koa, inferloPlugin for Fastify (request.ai, app under a prefix), InferloModule for NestJS, an AdonisJS provider (ctx.ai, config/ai.ts), InferloAI and inferloAI() for Ts.ED injection, trpcTools() for tRPC procedures as AI tools, toNextHandlers() for Next.js App Router route handlers, an Encore.ts raw endpoint serving the app with toNodeHandler(app, { prefix }) and createAI in Encore services
20
21
  - [Migrate from Express](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/migrate-from-express.md): move an Express app to Inferlo route by route at the same URLs, with a concept mapping table, shared auth and tests
22
+ - [Migrate from NestJS](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/migrate-from-nestjs.md): move a NestJS app to Inferlo route by route at the same URLs with toNodeHandler(app, { fallthrough: true }), Nest modules, providers, guards, pipes and DTOs mapped to Inferlo modules, plain functions, auth and schemas
23
+ - [Migrate from Hono](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/migrate-from-hono.md): move a Hono app to Inferlo route by route at the same URLs with hono.all("*", (c) => inferlo.fetch(c.req.raw)), Hono validators, middleware, sub-apps and basePath mapped to Inferlo schemas, createApp options, modules and apiVersion
24
+ - [Migrate from Koa](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/migrate-from-koa.md): move a Koa app to Inferlo route by route at the same URLs with toKoaMiddleware(app, { fallthrough: true }) before the Koa router, @koa/router routes, ctx.throw, body parsers and Koa middleware mapped to Inferlo routes, schemas, errors and createApp options
21
25
  - [Security](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/security.md): secure defaults and security headers (createApp({ secureHeaders }))
22
26
  - [Observability](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/observability.md): request ids (X-Request-Id, ctx.requestId), request logs and JSON logs with createApp({ logging: { format: "json", requests: true } }), your own logger, Prometheus metrics with createApp({ metrics: { token } })
23
27
  - [Deployment](https://github.com/BrowserlessAPI/Inferlo/blob/main/docs/deploy.md): Node, Bun, Deno, edge, health and readiness endpoints, graceful shutdown with app.close()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@browserlessapi/inferlo",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "The enterprise-ready TypeScript backend framework, with AI models built in: routing, validation, PostgreSQL and migrations, auth, rate limiting, jobs, OpenAPI and MCP, plus Anthropic, OpenAI, Gemini, Ollama and OpenAI-compatible models.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -55,14 +55,42 @@
55
55
  "types": "./dist/fastify.d.ts",
56
56
  "default": "./dist/fastify.js"
57
57
  },
58
+ "./h3": {
59
+ "types": "./dist/h3.d.ts",
60
+ "default": "./dist/h3.js"
61
+ },
62
+ "./hapi": {
63
+ "types": "./dist/hapi.d.ts",
64
+ "default": "./dist/hapi.js"
65
+ },
58
66
  "./hono": {
59
67
  "types": "./dist/hono.d.ts",
60
68
  "default": "./dist/hono.js"
61
69
  },
70
+ "./lambda": {
71
+ "types": "./dist/lambda.d.ts",
72
+ "default": "./dist/lambda.js"
73
+ },
74
+ "./koa": {
75
+ "types": "./dist/koa.d.ts",
76
+ "default": "./dist/koa.js"
77
+ },
78
+ "./nextjs": {
79
+ "types": "./dist/nextjs.d.ts",
80
+ "default": "./dist/nextjs.js"
81
+ },
62
82
  "./nestjs": {
63
83
  "types": "./dist/nestjs.d.ts",
64
84
  "default": "./dist/nestjs.js"
65
85
  },
86
+ "./react-router": {
87
+ "types": "./dist/react-router.d.ts",
88
+ "default": "./dist/react-router.js"
89
+ },
90
+ "./sveltekit": {
91
+ "types": "./dist/sveltekit.d.ts",
92
+ "default": "./dist/sveltekit.js"
93
+ },
66
94
  "./tsed": {
67
95
  "types": "./dist/tsed.d.ts",
68
96
  "default": "./dist/tsed.js"
@@ -93,7 +121,7 @@
93
121
  "scripts": {
94
122
  "build": "rm -rf dist && tsc -p tsconfig.build.json && chmod +x dist/cli/main.js",
95
123
  "prepare": "npm run build",
96
- "typecheck": "tsc --noEmit && tsc --noEmit -p examples/helpdesk && tsc --noEmit -p examples/with-hono && tsc --noEmit -p examples/with-express && tsc --noEmit -p examples/with-fastify && tsc --noEmit -p examples/with-elysia && tsc --noEmit -p examples/with-nestjs && tsc --noEmit -p examples/with-trpc && tsc --noEmit -p examples/with-adonisjs && tsc --noEmit -p examples/with-tsed",
124
+ "typecheck": "tsc --noEmit && tsc --noEmit -p examples/helpdesk && tsc --noEmit -p examples/with-hono && tsc --noEmit -p examples/with-express && tsc --noEmit -p examples/with-fastify && tsc --noEmit -p examples/with-elysia && tsc --noEmit -p examples/with-nestjs && tsc --noEmit -p examples/with-trpc && tsc --noEmit -p examples/with-adonisjs && tsc --noEmit -p examples/with-tsed && tsc --noEmit -p examples/with-encore && tsc --noEmit -p examples/with-koa && tsc --noEmit -p examples/with-nextjs && tsc --noEmit -p examples/with-nitro && tsc --noEmit -p examples/with-sveltekit && tsc --noEmit -p examples/with-react-router && tsc --noEmit -p examples/with-lambda && tsc --noEmit -p examples/with-hapi",
97
125
  "test": "node --test --test-reporter=spec \"test/**/*.test.ts\" \"examples/*/test/*.test.ts\"",
98
126
  "test:bun": "bun test test/",
99
127
  "llms": "node scripts/build-llms.ts",
@@ -101,18 +129,25 @@
101
129
  "bench": "node bench/run.ts"
102
130
  },
103
131
  "peerDependencies": {
132
+ "@opentelemetry/api": ">=1.3.0",
104
133
  "pg": ">=8"
105
134
  },
106
135
  "peerDependenciesMeta": {
136
+ "@opentelemetry/api": {
137
+ "optional": true
138
+ },
107
139
  "pg": {
108
140
  "optional": true
109
141
  }
110
142
  },
111
143
  "devDependencies": {
112
144
  "@adonisjs/core": "^6.21.0",
145
+ "@hapi/hapi": "^21.4.13",
113
146
  "@nestjs/common": "^12.3.3",
114
147
  "@nestjs/core": "^12.3.3",
115
148
  "@nestjs/platform-express": "^12.3.3",
149
+ "@opentelemetry/api": "^1.10.0",
150
+ "@sveltejs/kit": "^2.70.3",
116
151
  "@trpc/server": "^11.24.0",
117
152
  "@tsed/core": "^8.45.0",
118
153
  "@tsed/di": "^8.45.0",
@@ -121,13 +156,20 @@
121
156
  "@tsed/platform-params": "^8.45.0",
122
157
  "@tsed/schema": "^8.45.0",
123
158
  "@types/express": "^5.0.6",
159
+ "@types/koa": "^3.0.3",
124
160
  "@types/node": "^22.18.0",
125
161
  "@types/pg": "^8.15.0",
126
162
  "elysia": "^1.4.30",
163
+ "encore.dev": "^1.58.8",
127
164
  "express": "^5.3.0",
128
165
  "fastify": "^5.12.8",
166
+ "h3": "^2.0.8",
129
167
  "hono": "^4.13.16",
168
+ "koa": "^3.3.0",
169
+ "next": "^16.4.5",
130
170
  "pg": "^8.16.0",
171
+ "react": "^19.3.1",
172
+ "react-router": "^8.5.1",
131
173
  "reflect-metadata": "^0.2.2",
132
174
  "rxjs": "^7.8.2",
133
175
  "typescript": "^5.9.3"