@@ -3,15 +3,26 @@ import { validateConfiguration } from "./validation";
3
3
import { startTracing } from "../tracing" ;
4
4
import { initializeRegistry } from "../prompts/registry" ;
5
5
import { diag , DiagConsoleLogger , DiagLogLevel } from "@opentelemetry/api" ;
6
+ import { TraceloopClient } from "../client/traceloop-client" ;
6
7
7
8
export let _configuration : InitializeOptions | undefined ;
9
+ let _client : TraceloopClient | undefined ;
8
10
9
11
/**
10
- * Initializes the Traceloop SDK.
12
+ * Initializes the Traceloop SDK and creates a singleton client instance if API key is provided .
11
13
* Must be called once before any other SDK methods.
12
14
*
13
15
* @param options - The options to initialize the SDK. See the {@link InitializeOptions} for details.
16
+ * @returns TraceloopClient - The singleton client instance if API key is provided, otherwise undefined.
14
17
* @throws {InitializationError } if the configuration is invalid or if failed to fetch feature data.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * initialize({
22
+ * apiKey: 'your-api-key',
23
+ * appName: 'your-app',
24
+ * });
25
+ * ```
15
26
*/
16
27
export const initialize = ( options : InitializeOptions ) => {
17
28
if ( _configuration ) {
@@ -77,6 +88,15 @@ export const initialize = (options: InitializeOptions) => {
77
88
78
89
startTracing ( _configuration ) ;
79
90
initializeRegistry ( _configuration ) ;
91
+ if ( options . apiKey ) {
92
+ _client = new TraceloopClient ( {
93
+ apiKey : options . apiKey ,
94
+ baseUrl : options . baseUrl ,
95
+ appName : options . appName ! ,
96
+ } ) ;
97
+ return _client ;
98
+ }
99
+ return ;
80
100
} ;
81
101
82
102
const logLevelToOtelLogLevel = (
@@ -93,3 +113,26 @@ const logLevelToOtelLogLevel = (
93
113
return DiagLogLevel . ERROR ;
94
114
}
95
115
} ;
116
+
117
+ /**
118
+ * Gets the singleton instance of the TraceloopClient.
119
+ * The SDK must be initialized with an API key before calling this function.
120
+ *
121
+ * @returns The TraceloopClient singleton instance
122
+ * @throws {Error } if the SDK hasn't been initialized or was initialized without an API key
123
+ *
124
+ * @example
125
+ * ```typescript
126
+ * const client = getClient();
127
+ * await client.annotation.create({ annotationTask: 'taskId', entityInstanceId: 'entityId', tags: { score: 0.9 } });
128
+ * ```
129
+ */
130
+ export const getClient = ( ) : TraceloopClient => {
131
+ if ( ! _client ) {
132
+ throw new Error (
133
+ "Traceloop must be initialized before getting client, Call initialize() first." +
134
+ "If you already called initialize(), make sure you have an api key." ,
135
+ ) ;
136
+ }
137
+ return _client ;
138
+ } ;
0 commit comments