Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 121 lines 3.48 KB
Raw
1 import type { BaseComposioProvider } from '../provider/BaseProvider';
2 // Resolve against the installed package, including slim's copy of the core build.
3 import manifest from '#client_identity' with { type: 'json' };
4 import type { ClientIdentity } from '@composio/client';
5
6 /**
7 * Extended globalThis interface for runtime detection
8 */
9 interface ExtendedGlobalThis {
10 Bun?: unknown;
11 Deno?: unknown;
12 caches?: unknown;
13 WebSocketPair?: unknown;
14 EdgeRuntime?: unknown;
15 ServiceWorkerGlobalScope?: new () => unknown;
16 WorkerGlobalScope?: new () => unknown;
17 importScripts?: (url: string) => void;
18 }
19
20 /**
21 * Detect the JavaScript runtime environment without importing any external modules.
22 * Uses only built-in globals to avoid breaking in different environments.
23 */
24 function detectRuntime(): string {
25 const global = globalThis as ExtendedGlobalThis;
26
27 // Check for Bun
28 if ('Bun' in global && typeof global.Bun !== 'undefined') {
29 return 'BUN';
30 }
31
32 // Check for Deno
33 if ('Deno' in global && typeof global.Deno !== 'undefined') {
34 return 'DENO';
35 }
36
37 // Check for Node.js
38 if (typeof process !== 'undefined' && process.versions && process.versions.node) {
39 return 'NODEJS';
40 }
41
42 // Check for Cloudflare Workers
43 if (
44 'caches' in global &&
45 typeof global.caches !== 'undefined' &&
46 'WebSocketPair' in global &&
47 typeof global.WebSocketPair !== 'undefined'
48 ) {
49 return 'CLOUDFLARE_WORKERS';
50 }
51
52 // Check for Vercel Edge Runtime
53 if ('EdgeRuntime' in global && typeof global.EdgeRuntime !== 'undefined') {
54 return 'VERCEL_EDGE';
55 }
56
57 // Check for Service Worker
58 if (
59 'ServiceWorkerGlobalScope' in global &&
60 typeof global.ServiceWorkerGlobalScope !== 'undefined'
61 ) {
62 const ServiceWorkerScope = global.ServiceWorkerGlobalScope;
63 if (ServiceWorkerScope && global instanceof ServiceWorkerScope) {
64 return 'SERVICE_WORKER';
65 }
66 }
67
68 // Check for Web Worker
69 if (
70 'WorkerGlobalScope' in global &&
71 typeof global.WorkerGlobalScope !== 'undefined' &&
72 'importScripts' in global &&
73 typeof global.importScripts === 'function'
74 ) {
75 return 'WEB_WORKER';
76 }
77
78 // Check for React Native
79 if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
80 return 'REACT_NATIVE';
81 }
82
83 // Check for Browser
84 if (typeof window !== 'undefined' && typeof document !== 'undefined') {
85 return 'BROWSER';
86 }
87
88 // Fallback
89 return 'UNKNOWN';
90 }
91
92 // Detect once at module initialization
93 const RUNTIME_ENV = detectRuntime();
94
95 /**
96 * An identity value is sent as a header, so the client accepts only non-empty
97 * printable ASCII with no leading or trailing whitespace.
98 */
99 const HEADER_SAFE = /^[\x21-\x7e](?:[\x20-\x7e]*[\x21-\x7e])?$/;
100
101 /**
102 * The identity the API client reports for this SDK: its product headers
103 * (`x-client-provenance`, `x-client-version`, `x-sdk-version`) and the legacy
104 * `x-source`, `x-framework` and `x-runtime` labels. The client adds the
105 * library headers and detects the runtime and its version itself.
106 *
107 * A custom provider whose name cannot be sent as a header value is reported
108 * as `unknown` rather than making the SDK fail to construct.
109 */
110 export function getClientIdentity(
111 provider: BaseComposioProvider<unknown, unknown, unknown> | undefined
112 ): ClientIdentity {
113 const framework = provider?.name;
114 return {
115 product: manifest.name,
116 version: manifest.version,
117 source: 'TYPESCRIPT_SDK',
118 framework: framework && HEADER_SAFE.test(framework) ? framework : 'unknown',
119 runtime: RUNTIME_ENV,
120 };
121 }