@visibe.ai/node 0.1.7 → 0.1.9
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/dist/cjs/api.js +29 -0
- package/dist/cjs/client.js +7 -0
- package/dist/cjs/index.js +11 -2
- package/dist/esm/api.js +29 -0
- package/dist/esm/client.js +7 -0
- package/dist/esm/index.js +11 -2
- package/dist/types/api.d.ts +2 -1
- package/package.json +1 -1
package/dist/cjs/api.js
CHANGED
|
@@ -12,6 +12,35 @@ class APIClient {
|
|
|
12
12
|
if (!this._enabled) {
|
|
13
13
|
process.emitWarning('[Visibe] No API key provided — tracing is disabled. Set VISIBE_API_KEY or pass apiKey= to enable.', { type: 'VisibleSDKWarning', code: 'VISIBE_NO_API_KEY' });
|
|
14
14
|
}
|
|
15
|
+
else if (options.apiKey &&
|
|
16
|
+
!options.apiKey.startsWith('sk_live_') &&
|
|
17
|
+
!options.apiKey.startsWith('sk_test_')) {
|
|
18
|
+
process.emitWarning('[Visibe] API key format unrecognized (expected sk_live_* or sk_test_*).', { type: 'VisibleSDKWarning', code: 'VISIBE_INVALID_KEY_FORMAT' });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
// validateKey() — called once at startup in the background.
|
|
23
|
+
// Hits GET /api/auth/validate with the API key.
|
|
24
|
+
// On 401 (invalid/revoked key): disables tracing and prints a clear warning.
|
|
25
|
+
// On network error or non-401 failure: stays enabled (lenient — don't punish
|
|
26
|
+
// users for flaky networks at startup).
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
async validateKey() {
|
|
29
|
+
try {
|
|
30
|
+
const response = await fetch(`${this.apiUrl}/api/auth/validate`, {
|
|
31
|
+
method: 'GET',
|
|
32
|
+
headers: { 'Authorization': `Bearer ${this.apiKey}` },
|
|
33
|
+
signal: AbortSignal.timeout(5000),
|
|
34
|
+
});
|
|
35
|
+
if (response.status === 401) {
|
|
36
|
+
this._enabled = false;
|
|
37
|
+
process.emitWarning('[Visibe] Invalid API key — tracing is disabled. Check your VISIBE_API_KEY.', { type: 'VisibleSDKWarning', code: 'VISIBE_INVALID_API_KEY' });
|
|
38
|
+
}
|
|
39
|
+
// 200 → valid, stay enabled. Other status codes → lenient, stay enabled.
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Network error / timeout → lenient, stay enabled.
|
|
43
|
+
}
|
|
15
44
|
}
|
|
16
45
|
async _request(method, path, body) {
|
|
17
46
|
if (!this._enabled)
|
package/dist/cjs/client.js
CHANGED
|
@@ -20,6 +20,13 @@ class Visibe {
|
|
|
20
20
|
this.debug = debug;
|
|
21
21
|
this.apiClient = new api_1.APIClient({ apiKey, apiUrl });
|
|
22
22
|
this.batcher = new api_1.SpanBatcher(this.apiClient);
|
|
23
|
+
// Fire-and-forget background key validation.
|
|
24
|
+
// Runs ~80ms after startup — well before the 2s batcher flush window.
|
|
25
|
+
// On invalid key: disables tracing and prints a warning.
|
|
26
|
+
// On network error: stays enabled (lenient).
|
|
27
|
+
if (this.apiClient._enabled) {
|
|
28
|
+
this.apiClient.validateKey().catch(() => { });
|
|
29
|
+
}
|
|
23
30
|
}
|
|
24
31
|
// ---------------------------------------------------------------------------
|
|
25
32
|
// instrument() — wrap a client so each call creates its own trace.
|
package/dist/cjs/index.js
CHANGED
|
@@ -53,7 +53,7 @@ function patchFramework(framework, client) {
|
|
|
53
53
|
case 'openai': {
|
|
54
54
|
const openaiModule = require('openai');
|
|
55
55
|
_originalOpenAI = openaiModule.OpenAI;
|
|
56
|
-
|
|
56
|
+
const PatchedOpenAI = class extends _originalOpenAI {
|
|
57
57
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
58
|
constructor(...args) {
|
|
59
59
|
super(...args);
|
|
@@ -63,12 +63,16 @@ function patchFramework(framework, client) {
|
|
|
63
63
|
catch { /* never crash new OpenAI() */ }
|
|
64
64
|
}
|
|
65
65
|
};
|
|
66
|
+
openaiModule.OpenAI = PatchedOpenAI;
|
|
67
|
+
// Also patch .default so that `import OpenAI from 'openai'` (esModuleInterop)
|
|
68
|
+
// picks up the instrumented class — TypeScript compiles default imports to .default.
|
|
69
|
+
openaiModule.default = PatchedOpenAI;
|
|
66
70
|
break;
|
|
67
71
|
}
|
|
68
72
|
case 'anthropic': {
|
|
69
73
|
const anthropicModule = require('@anthropic-ai/sdk');
|
|
70
74
|
_originalAnthropic = anthropicModule.Anthropic;
|
|
71
|
-
|
|
75
|
+
const PatchedAnthropic = class extends _originalAnthropic {
|
|
72
76
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
77
|
constructor(...args) {
|
|
74
78
|
super(...args);
|
|
@@ -78,6 +82,9 @@ function patchFramework(framework, client) {
|
|
|
78
82
|
catch { /* never crash new Anthropic() */ }
|
|
79
83
|
}
|
|
80
84
|
};
|
|
85
|
+
anthropicModule.Anthropic = PatchedAnthropic;
|
|
86
|
+
// Also patch .default for esModuleInterop default import support.
|
|
87
|
+
anthropicModule.default = PatchedAnthropic;
|
|
81
88
|
break;
|
|
82
89
|
}
|
|
83
90
|
case 'bedrock': {
|
|
@@ -173,6 +180,7 @@ async function shutdown() {
|
|
|
173
180
|
try {
|
|
174
181
|
if (_originalOpenAI) {
|
|
175
182
|
require('openai').OpenAI = _originalOpenAI;
|
|
183
|
+
require('openai').default = _originalOpenAI;
|
|
176
184
|
_originalOpenAI = null;
|
|
177
185
|
}
|
|
178
186
|
}
|
|
@@ -180,6 +188,7 @@ async function shutdown() {
|
|
|
180
188
|
try {
|
|
181
189
|
if (_originalAnthropic) {
|
|
182
190
|
require('@anthropic-ai/sdk').Anthropic = _originalAnthropic;
|
|
191
|
+
require('@anthropic-ai/sdk').default = _originalAnthropic;
|
|
183
192
|
_originalAnthropic = null;
|
|
184
193
|
}
|
|
185
194
|
}
|
package/dist/esm/api.js
CHANGED
|
@@ -9,6 +9,35 @@ export class APIClient {
|
|
|
9
9
|
if (!this._enabled) {
|
|
10
10
|
process.emitWarning('[Visibe] No API key provided — tracing is disabled. Set VISIBE_API_KEY or pass apiKey= to enable.', { type: 'VisibleSDKWarning', code: 'VISIBE_NO_API_KEY' });
|
|
11
11
|
}
|
|
12
|
+
else if (options.apiKey &&
|
|
13
|
+
!options.apiKey.startsWith('sk_live_') &&
|
|
14
|
+
!options.apiKey.startsWith('sk_test_')) {
|
|
15
|
+
process.emitWarning('[Visibe] API key format unrecognized (expected sk_live_* or sk_test_*).', { type: 'VisibleSDKWarning', code: 'VISIBE_INVALID_KEY_FORMAT' });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// validateKey() — called once at startup in the background.
|
|
20
|
+
// Hits GET /api/auth/validate with the API key.
|
|
21
|
+
// On 401 (invalid/revoked key): disables tracing and prints a clear warning.
|
|
22
|
+
// On network error or non-401 failure: stays enabled (lenient — don't punish
|
|
23
|
+
// users for flaky networks at startup).
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
async validateKey() {
|
|
26
|
+
try {
|
|
27
|
+
const response = await fetch(`${this.apiUrl}/api/auth/validate`, {
|
|
28
|
+
method: 'GET',
|
|
29
|
+
headers: { 'Authorization': `Bearer ${this.apiKey}` },
|
|
30
|
+
signal: AbortSignal.timeout(5000),
|
|
31
|
+
});
|
|
32
|
+
if (response.status === 401) {
|
|
33
|
+
this._enabled = false;
|
|
34
|
+
process.emitWarning('[Visibe] Invalid API key — tracing is disabled. Check your VISIBE_API_KEY.', { type: 'VisibleSDKWarning', code: 'VISIBE_INVALID_API_KEY' });
|
|
35
|
+
}
|
|
36
|
+
// 200 → valid, stay enabled. Other status codes → lenient, stay enabled.
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Network error / timeout → lenient, stay enabled.
|
|
40
|
+
}
|
|
12
41
|
}
|
|
13
42
|
async _request(method, path, body) {
|
|
14
43
|
if (!this._enabled)
|
package/dist/esm/client.js
CHANGED
|
@@ -17,6 +17,13 @@ export class Visibe {
|
|
|
17
17
|
this.debug = debug;
|
|
18
18
|
this.apiClient = new APIClient({ apiKey, apiUrl });
|
|
19
19
|
this.batcher = new SpanBatcher(this.apiClient);
|
|
20
|
+
// Fire-and-forget background key validation.
|
|
21
|
+
// Runs ~80ms after startup — well before the 2s batcher flush window.
|
|
22
|
+
// On invalid key: disables tracing and prints a warning.
|
|
23
|
+
// On network error: stays enabled (lenient).
|
|
24
|
+
if (this.apiClient._enabled) {
|
|
25
|
+
this.apiClient.validateKey().catch(() => { });
|
|
26
|
+
}
|
|
20
27
|
}
|
|
21
28
|
// ---------------------------------------------------------------------------
|
|
22
29
|
// instrument() — wrap a client so each call creates its own trace.
|
package/dist/esm/index.js
CHANGED
|
@@ -47,7 +47,7 @@ function patchFramework(framework, client) {
|
|
|
47
47
|
case 'openai': {
|
|
48
48
|
const openaiModule = require('openai');
|
|
49
49
|
_originalOpenAI = openaiModule.OpenAI;
|
|
50
|
-
|
|
50
|
+
const PatchedOpenAI = class extends _originalOpenAI {
|
|
51
51
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
52
|
constructor(...args) {
|
|
53
53
|
super(...args);
|
|
@@ -57,12 +57,16 @@ function patchFramework(framework, client) {
|
|
|
57
57
|
catch { /* never crash new OpenAI() */ }
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
|
+
openaiModule.OpenAI = PatchedOpenAI;
|
|
61
|
+
// Also patch .default so that `import OpenAI from 'openai'` (esModuleInterop)
|
|
62
|
+
// picks up the instrumented class — TypeScript compiles default imports to .default.
|
|
63
|
+
openaiModule.default = PatchedOpenAI;
|
|
60
64
|
break;
|
|
61
65
|
}
|
|
62
66
|
case 'anthropic': {
|
|
63
67
|
const anthropicModule = require('@anthropic-ai/sdk');
|
|
64
68
|
_originalAnthropic = anthropicModule.Anthropic;
|
|
65
|
-
|
|
69
|
+
const PatchedAnthropic = class extends _originalAnthropic {
|
|
66
70
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
67
71
|
constructor(...args) {
|
|
68
72
|
super(...args);
|
|
@@ -72,6 +76,9 @@ function patchFramework(framework, client) {
|
|
|
72
76
|
catch { /* never crash new Anthropic() */ }
|
|
73
77
|
}
|
|
74
78
|
};
|
|
79
|
+
anthropicModule.Anthropic = PatchedAnthropic;
|
|
80
|
+
// Also patch .default for esModuleInterop default import support.
|
|
81
|
+
anthropicModule.default = PatchedAnthropic;
|
|
75
82
|
break;
|
|
76
83
|
}
|
|
77
84
|
case 'bedrock': {
|
|
@@ -167,6 +174,7 @@ export async function shutdown() {
|
|
|
167
174
|
try {
|
|
168
175
|
if (_originalOpenAI) {
|
|
169
176
|
require('openai').OpenAI = _originalOpenAI;
|
|
177
|
+
require('openai').default = _originalOpenAI;
|
|
170
178
|
_originalOpenAI = null;
|
|
171
179
|
}
|
|
172
180
|
}
|
|
@@ -174,6 +182,7 @@ export async function shutdown() {
|
|
|
174
182
|
try {
|
|
175
183
|
if (_originalAnthropic) {
|
|
176
184
|
require('@anthropic-ai/sdk').Anthropic = _originalAnthropic;
|
|
185
|
+
require('@anthropic-ai/sdk').default = _originalAnthropic;
|
|
177
186
|
_originalAnthropic = null;
|
|
178
187
|
}
|
|
179
188
|
}
|
package/dist/types/api.d.ts
CHANGED
|
@@ -2,12 +2,13 @@ export declare class APIClient {
|
|
|
2
2
|
readonly apiUrl: string;
|
|
3
3
|
private readonly apiKey;
|
|
4
4
|
private readonly timeout;
|
|
5
|
-
|
|
5
|
+
_enabled: boolean;
|
|
6
6
|
constructor(options: {
|
|
7
7
|
apiUrl?: string;
|
|
8
8
|
apiKey?: string;
|
|
9
9
|
timeout?: number;
|
|
10
10
|
});
|
|
11
|
+
validateKey(): Promise<void>;
|
|
11
12
|
private _request;
|
|
12
13
|
createTrace(data: object): Promise<boolean>;
|
|
13
14
|
sendSpan(traceId: string, span: object): Promise<boolean>;
|
package/package.json
CHANGED