@intelli-1113/stratos-sdk 1.2.3 → 1.3.1

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.
@@ -0,0 +1,62 @@
1
+ // Vercel AI auto-telemetry orchestration for Stratos.
2
+ // Hooks are registered from register.js (custom ESM loader) before app code runs.
3
+
4
+ import { createRequire } from "node:module";
5
+ import { pathToFileURL } from "node:url";
6
+ import { join } from "node:path";
7
+ import { injectTelemetryOptions, TELEMETRY_CALL_NAMES } from "./vercel-ai-inject.js";
8
+
9
+ export { injectTelemetryOptions, TELEMETRY_CALL_NAMES };
10
+
11
+ function resolveFromCwd(pkg) {
12
+ try {
13
+ return createRequire(join(process.cwd(), "package.json")).resolve(pkg);
14
+ } catch {
15
+ return null;
16
+ }
17
+ }
18
+
19
+ function flagEnabled(name, defaultOn = true) {
20
+ const v = process.env[name];
21
+ if (v == null || v === "") return defaultOn;
22
+ return !["0", "false", "off", "no"].includes(String(v).toLowerCase());
23
+ }
24
+
25
+ /**
26
+ * Optional AI SDK v7 global registerTelemetry when @ai-sdk/otel is present.
27
+ * Safe no-op on v5/v6.
28
+ */
29
+ export async function enableVercelAiTelemetry() {
30
+ if (!flagEnabled("STRATOS_VERCEL_AI", true)) {
31
+ return { enabled: false, mode: "disabled" };
32
+ }
33
+
34
+ const aiPath = resolveFromCwd("ai");
35
+ if (!aiPath) return { enabled: false, mode: null };
36
+
37
+ try {
38
+ // Via our shim if loader is active, or direct file URL
39
+ const ai = await import(pathToFileURL(aiPath).href);
40
+
41
+ if (typeof ai.registerTelemetry === "function") {
42
+ const otelPath = resolveFromCwd("@ai-sdk/otel");
43
+ if (otelPath) {
44
+ try {
45
+ const otel = await import(pathToFileURL(otelPath).href);
46
+ const OpenTelemetry = otel.OpenTelemetry || otel.LegacyOpenTelemetry;
47
+ if (typeof OpenTelemetry === "function") {
48
+ ai.registerTelemetry(new OpenTelemetry());
49
+ return { enabled: true, mode: "registerTelemetry" };
50
+ }
51
+ } catch {
52
+ /* fall through */
53
+ }
54
+ }
55
+ }
56
+
57
+ // ESM loader path is the primary mechanism (stratos-sdk:ai shim)
58
+ return { enabled: true, mode: "esm-loader-shim" };
59
+ } catch {
60
+ return { enabled: true, mode: "esm-loader-shim" };
61
+ }
62
+ }