@turingpulse/sdk 1.0.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.
- package/.github/dependabot.yml +38 -0
- package/.github/workflows/ci.yml +246 -0
- package/.github/workflows/framework-compat.yml +169 -0
- package/.github/workflows/security.yml +336 -0
- package/CHANGELOG.md +29 -0
- package/LICENSE +13 -0
- package/MIGRATION.md +30 -0
- package/README.md +221 -0
- package/dist/attachments.d.ts +28 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/attachments.js +59 -0
- package/dist/attachments.js.map +1 -0
- package/dist/config.d.ts +72 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +78 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +126 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +163 -0
- package/dist/context.js.map +1 -0
- package/dist/decorators.d.ts +6 -0
- package/dist/decorators.d.ts.map +1 -0
- package/dist/decorators.js +52 -0
- package/dist/decorators.js.map +1 -0
- package/dist/deploy.d.ts +89 -0
- package/dist/deploy.d.ts.map +1 -0
- package/dist/deploy.js +203 -0
- package/dist/deploy.js.map +1 -0
- package/dist/errors.d.ts +18 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +34 -0
- package/dist/errors.js.map +1 -0
- package/dist/eventBuilder.d.ts +21 -0
- package/dist/eventBuilder.d.ts.map +1 -0
- package/dist/eventBuilder.js +127 -0
- package/dist/eventBuilder.js.map +1 -0
- package/dist/fingerprint.d.ts +158 -0
- package/dist/fingerprint.d.ts.map +1 -0
- package/dist/fingerprint.js +339 -0
- package/dist/fingerprint.js.map +1 -0
- package/dist/governance.d.ts +47 -0
- package/dist/governance.d.ts.map +1 -0
- package/dist/governance.js +104 -0
- package/dist/governance.js.map +1 -0
- package/dist/http.d.ts +62 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +181 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/instrumentation.d.ts +40 -0
- package/dist/instrumentation.d.ts.map +1 -0
- package/dist/instrumentation.js +31 -0
- package/dist/instrumentation.js.map +1 -0
- package/dist/integrations/mastra.d.ts +64 -0
- package/dist/integrations/mastra.d.ts.map +1 -0
- package/dist/integrations/mastra.js +256 -0
- package/dist/integrations/mastra.js.map +1 -0
- package/dist/kpi.d.ts +21 -0
- package/dist/kpi.d.ts.map +1 -0
- package/dist/kpi.js +83 -0
- package/dist/kpi.js.map +1 -0
- package/dist/llmDetector.d.ts +22 -0
- package/dist/llmDetector.d.ts.map +1 -0
- package/dist/llmDetector.js +269 -0
- package/dist/llmDetector.js.map +1 -0
- package/dist/plugin.d.ts +33 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +312 -0
- package/dist/plugin.js.map +1 -0
- package/dist/registry.d.ts +13 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +18 -0
- package/dist/registry.js.map +1 -0
- package/dist/tracing.d.ts +10 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +30 -0
- package/dist/tracing.js.map +1 -0
- package/dist/triggerState.d.ts +5 -0
- package/dist/triggerState.d.ts.map +1 -0
- package/dist/triggerState.js +19 -0
- package/dist/triggerState.js.map +1 -0
- package/dist/utils.d.ts +27 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +72 -0
- package/dist/utils.js.map +1 -0
- package/package.json +37 -0
- package/packages/anthropic/package.json +16 -0
- package/packages/anthropic/src/index.ts +5 -0
- package/packages/anthropic/src/wrapper.ts +102 -0
- package/packages/anthropic/tsconfig.build.json +20 -0
- package/packages/langchain/package.json +16 -0
- package/packages/langchain/src/index.ts +7 -0
- package/packages/langchain/src/wrapper.ts +51 -0
- package/packages/mastra/package.json +17 -0
- package/packages/mastra/src/index.ts +8 -0
- package/packages/mastra/src/wrapper.ts +301 -0
- package/packages/openai/package.json +16 -0
- package/packages/openai/src/index.ts +8 -0
- package/packages/openai/src/wrapper.ts +103 -0
- package/packages/openai/tsconfig.build.json +20 -0
- package/packages/openclaw/openclaw.plugin.json +100 -0
- package/packages/openclaw/package.json +41 -0
- package/packages/openclaw/src/buffer.ts +99 -0
- package/packages/openclaw/src/config.ts +139 -0
- package/packages/openclaw/src/hooks/governance.ts +267 -0
- package/packages/openclaw/src/hooks/lifecycle.ts +75 -0
- package/packages/openclaw/src/hooks/telemetry.ts +207 -0
- package/packages/openclaw/src/index.ts +91 -0
- package/packages/openclaw/src/mapper.ts +233 -0
- package/packages/openclaw/src/session-tracker.ts +181 -0
- package/packages/openclaw/src/types.ts +220 -0
- package/packages/openclaw/tests/buffer.test.ts +148 -0
- package/packages/openclaw/tests/config.test.ts +122 -0
- package/packages/openclaw/tests/governance.test.ts +232 -0
- package/packages/openclaw/tests/mapper.test.ts +242 -0
- package/packages/openclaw/tests/session-tracker.test.ts +124 -0
- package/packages/openclaw/tsconfig.json +18 -0
- package/packages/openclaw/vitest.config.ts +8 -0
- package/packages/vercel-ai/package.json +16 -0
- package/packages/vercel-ai/src/index.ts +5 -0
- package/packages/vercel-ai/src/wrapper.ts +49 -0
- package/scripts/bump-version.sh +58 -0
- package/scripts/update-readme-compat.mjs +151 -0
- package/src/__tests__/fingerprint.test.ts +328 -0
- package/src/attachments.ts +88 -0
- package/src/config.ts +164 -0
- package/src/context.ts +258 -0
- package/src/decorators.ts +61 -0
- package/src/deploy.ts +260 -0
- package/src/errors.ts +44 -0
- package/src/eventBuilder.ts +153 -0
- package/src/fingerprint.ts +421 -0
- package/src/governance.ts +156 -0
- package/src/http.ts +241 -0
- package/src/index.ts +57 -0
- package/src/instrumentation.ts +68 -0
- package/src/integrations/mastra.ts +335 -0
- package/src/kpi.ts +112 -0
- package/src/llmDetector.ts +330 -0
- package/src/plugin.ts +384 -0
- package/src/registry.ts +27 -0
- package/src/tracing.ts +39 -0
- package/src/triggerState.ts +27 -0
- package/src/utils.ts +78 -0
- package/tests/compat/anthropic.test.ts +61 -0
- package/tests/compat/cohere.test.ts +57 -0
- package/tests/compat/google-genai.test.ts +61 -0
- package/tests/compat/langchain-openai.test.ts +41 -0
- package/tests/compat/langchain.test.ts +64 -0
- package/tests/compat/mistral.test.ts +58 -0
- package/tests/compat/openai.test.ts +71 -0
- package/tests/compat/vercel-ai.test.ts +56 -0
- package/tests/plugins/anthropic-wrapper.test.ts +120 -0
- package/tests/plugins/langchain-wrapper.test.ts +128 -0
- package/tests/plugins/openai-wrapper.test.ts +165 -0
- package/tsconfig.json +21 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Document attachment support for the TuringPulse TypeScript SDK.
|
|
3
|
+
*
|
|
4
|
+
* Provides AttachmentManager for uploading documents to the ingestion service
|
|
5
|
+
* and injecting attachment metadata into the current span.
|
|
6
|
+
*
|
|
7
|
+
* Design principle: NEVER crash customer code. All upload failures are logged
|
|
8
|
+
* and silently ignored.
|
|
9
|
+
*/
|
|
10
|
+
export class AttachmentManager {
|
|
11
|
+
constructor(config, enabled = true) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
this.baseUrl = config.endpoint;
|
|
14
|
+
this.enabled = enabled;
|
|
15
|
+
}
|
|
16
|
+
async attachBytes(data, filename, direction = 'input', mimeType) {
|
|
17
|
+
if (!this.enabled)
|
|
18
|
+
return undefined;
|
|
19
|
+
try {
|
|
20
|
+
const formData = new FormData();
|
|
21
|
+
const blob = new Blob([data], { type: mimeType ?? 'application/octet-stream' });
|
|
22
|
+
formData.append('file', blob, filename);
|
|
23
|
+
formData.append('direction', direction);
|
|
24
|
+
const fetchImpl = this.config.fetchImpl ?? globalThis.fetch;
|
|
25
|
+
const response = await fetchImpl(`${this.baseUrl}/api/v1/attachments`, {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
headers: {
|
|
28
|
+
'X-API-Key': this.config.apiKey,
|
|
29
|
+
},
|
|
30
|
+
body: formData,
|
|
31
|
+
});
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
// eslint-disable-next-line no-console
|
|
34
|
+
console.warn(`TuringPulse attachment upload failed: ${response.status}`);
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const result = (await response.json());
|
|
38
|
+
return {
|
|
39
|
+
attachmentId: result.attachment_id,
|
|
40
|
+
filename: result.filename,
|
|
41
|
+
mimeType: result.mime_type,
|
|
42
|
+
sizeBytes: result.size_bytes,
|
|
43
|
+
storagePath: result.storage_path,
|
|
44
|
+
direction,
|
|
45
|
+
deduplicated: Boolean(result.deduplicated),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
// eslint-disable-next-line no-console
|
|
50
|
+
console.warn('TuringPulse attachment upload failed:', err instanceof Error ? err.message : String(err));
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
async attachText(text, filename = 'document.txt', direction = 'input') {
|
|
55
|
+
const encoder = new TextEncoder();
|
|
56
|
+
return this.attachBytes(encoder.encode(text), filename, direction, 'text/plain');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=attachments.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attachments.js","sourceRoot":"","sources":["../src/attachments.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAcH,MAAM,OAAO,iBAAiB;IAK5B,YAAY,MAAyB,EAAE,OAAO,GAAG,IAAI;QACnD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,QAAQ,CAAC;QAC/B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,WAAW,CACf,IAAyB,EACzB,QAAgB,EAChB,YAAgC,OAAO,EACvC,QAAiB;QAEjB,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO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AC;YACb,sCAAsC;YACtC,OAAO,CAAC,IAAI,CAAC,uCAAuC,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;YACxG,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,UAAU,CACd,IAAY,EACZ,QAAQ,GAAG,cAAc,EACzB,YAAgC,OAAO;QAEvC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QAClC,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;IACnF,CAAC;CACF"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { FingerprintConfig } from './fingerprint';
|
|
2
|
+
export type FetchLike = (url: string | URL, init?: RequestInit) => Promise<Response>;
|
|
3
|
+
export interface GovernanceDefaults {
|
|
4
|
+
hitl?: boolean;
|
|
5
|
+
reviewers?: string[];
|
|
6
|
+
escalationChannels?: string[];
|
|
7
|
+
metadata?: Record<string, string>;
|
|
8
|
+
severity?: string;
|
|
9
|
+
autoEscalateAfterSeconds?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Configuration options for the TuringPulse SDK.
|
|
13
|
+
*
|
|
14
|
+
* Required:
|
|
15
|
+
* apiKey - SDK API key. The backend resolves tenant and project from it.
|
|
16
|
+
* workflowName - Display name for this workflow.
|
|
17
|
+
*
|
|
18
|
+
* Optional:
|
|
19
|
+
* endpoint - Base URL for the TuringPulse API (default: https://api.turingpulse.ai).
|
|
20
|
+
*/
|
|
21
|
+
export interface TuringPulseConfigOptions {
|
|
22
|
+
/** SDK API key (required). */
|
|
23
|
+
apiKey: string;
|
|
24
|
+
/** Workflow display name (required). */
|
|
25
|
+
workflowName: string;
|
|
26
|
+
/** Base URL for the TuringPulse API. Override for self-hosted or staging. */
|
|
27
|
+
endpoint?: string;
|
|
28
|
+
defaultLabels?: Record<string, string>;
|
|
29
|
+
traceEnabled?: boolean;
|
|
30
|
+
traceServiceName?: string;
|
|
31
|
+
triggerNamespace?: string;
|
|
32
|
+
timeoutMs?: number;
|
|
33
|
+
maxRetries?: number;
|
|
34
|
+
fetchImpl?: FetchLike;
|
|
35
|
+
governanceDefaults?: GovernanceDefaults;
|
|
36
|
+
fingerprint?: FingerprintConfig;
|
|
37
|
+
captureArguments?: boolean;
|
|
38
|
+
captureReturnValue?: boolean;
|
|
39
|
+
maxSerializedFieldLength?: number;
|
|
40
|
+
redactFields?: string[];
|
|
41
|
+
}
|
|
42
|
+
export declare const DEFAULT_ENDPOINT = "https://api.turingpulse.ai";
|
|
43
|
+
interface ResolvedGovernanceDefaults {
|
|
44
|
+
hitl: boolean;
|
|
45
|
+
reviewers: string[];
|
|
46
|
+
escalationChannels: string[];
|
|
47
|
+
metadata: Record<string, string>;
|
|
48
|
+
severity: string;
|
|
49
|
+
autoEscalateAfterSeconds: number | undefined;
|
|
50
|
+
}
|
|
51
|
+
export declare class TuringPulseConfig {
|
|
52
|
+
readonly apiKey: string;
|
|
53
|
+
readonly workflowName: string;
|
|
54
|
+
readonly endpoint: string;
|
|
55
|
+
readonly defaultLabels: Record<string, string>;
|
|
56
|
+
readonly traceEnabled: boolean;
|
|
57
|
+
readonly traceServiceName: string;
|
|
58
|
+
readonly triggerNamespace: string;
|
|
59
|
+
readonly timeoutMs: number;
|
|
60
|
+
readonly maxRetries: number;
|
|
61
|
+
readonly fetchImpl?: FetchLike;
|
|
62
|
+
readonly governanceDefaults: ResolvedGovernanceDefaults;
|
|
63
|
+
readonly fingerprint: Required<FingerprintConfig>;
|
|
64
|
+
readonly captureArguments: boolean;
|
|
65
|
+
readonly captureReturnValue: boolean;
|
|
66
|
+
readonly maxSerializedFieldLength: number;
|
|
67
|
+
readonly redactFields: string[];
|
|
68
|
+
constructor(options: TuringPulseConfigOptions);
|
|
69
|
+
mergedLabels(labels?: Record<string, string>): Record<string, string>;
|
|
70
|
+
}
|
|
71
|
+
export {};
|
|
72
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE9E,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAuBrF,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;IACrB,kBAAkB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC9B,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,wBAAwB,CAAC,EAAE,MAAM,CAAC;CACnC;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,wBAAwB;IACvC,8BAA8B;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,wCAAwC;IACxC,YAAY,EAAE,MAAM,CAAC;IACrB,6EAA6E;IAC7E,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACvC,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,kBAAkB,CAAC,EAAE,kBAAkB,CAA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C,QAAQ,CAAC,wBAAwB,EAAE,MAAM,CAAC;IAC1C,QAAQ,CAAC,YAAY,EAAE,MAAM,EAAE,CAAC;gBAEpB,OAAO,EAAE,wBAAwB;IAwD7C,YAAY,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;CAGtE"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { DEFAULT_FINGERPRINT_CONFIG } from './fingerprint';
|
|
2
|
+
const CRLF_RE = /[\r\n\x00]/g;
|
|
3
|
+
function stripControlChars(value) {
|
|
4
|
+
return value.replace(CRLF_RE, '');
|
|
5
|
+
}
|
|
6
|
+
const BLOCKED_HOSTS = /^(127\.\d{1,3}\.\d{1,3}\.\d{1,3}|10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3}|0\.0\.0\.0|localhost|\[::1\]|\[fd[0-9a-f]{2}:|\[fe80:)$/i;
|
|
7
|
+
function checkEndpointSsrf(endpoint) {
|
|
8
|
+
try {
|
|
9
|
+
const parsed = new URL(endpoint);
|
|
10
|
+
if (BLOCKED_HOSTS.test(parsed.hostname)) {
|
|
11
|
+
throw new Error('TuringPulse endpoint must not point to private/internal networks.');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
if (e instanceof Error && e.message.includes('private/internal'))
|
|
16
|
+
throw e;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export const DEFAULT_ENDPOINT = 'https://api.turingpulse.ai';
|
|
20
|
+
const DEFAULT_GOVERNANCE = {
|
|
21
|
+
hitl: false,
|
|
22
|
+
reviewers: [],
|
|
23
|
+
escalationChannels: [],
|
|
24
|
+
metadata: {},
|
|
25
|
+
severity: 'medium',
|
|
26
|
+
autoEscalateAfterSeconds: undefined,
|
|
27
|
+
};
|
|
28
|
+
export class TuringPulseConfig {
|
|
29
|
+
constructor(options) {
|
|
30
|
+
if (!options.apiKey || !options.apiKey.trim()) {
|
|
31
|
+
throw new Error('TuringPulse apiKey is required and cannot be empty. ' +
|
|
32
|
+
'Set the TP_API_KEY environment variable or pass apiKey to init().');
|
|
33
|
+
}
|
|
34
|
+
this.apiKey = stripControlChars(options.apiKey.trim());
|
|
35
|
+
if (!options.workflowName || !options.workflowName.trim()) {
|
|
36
|
+
throw new Error('TuringPulse workflowName is required and cannot be empty. ' +
|
|
37
|
+
'Pass workflowName to init(). Example: init({ apiKey: "...", workflowName: "My Agent" })');
|
|
38
|
+
}
|
|
39
|
+
this.workflowName = options.workflowName.trim();
|
|
40
|
+
const raw = stripControlChars(options.endpoint ?? DEFAULT_ENDPOINT);
|
|
41
|
+
this.endpoint = raw.replace(/\/$/, '');
|
|
42
|
+
if (this.endpoint && !/^https?:\/\//.test(this.endpoint)) {
|
|
43
|
+
throw new Error('TuringPulse endpoint must start with https:// or http://');
|
|
44
|
+
}
|
|
45
|
+
checkEndpointSsrf(this.endpoint);
|
|
46
|
+
if (this.endpoint.startsWith('http://')) {
|
|
47
|
+
// eslint-disable-next-line no-console
|
|
48
|
+
console.warn('TuringPulse endpoint uses http:// \u2014 API key will be sent in cleartext. Use https:// in production.');
|
|
49
|
+
}
|
|
50
|
+
this.defaultLabels = { ...(options.defaultLabels ?? {}) };
|
|
51
|
+
this.traceEnabled = options.traceEnabled ?? true;
|
|
52
|
+
this.traceServiceName = options.traceServiceName ?? 'turingpulse.sdk';
|
|
53
|
+
this.triggerNamespace = options.triggerNamespace ?? 'default';
|
|
54
|
+
this.timeoutMs = Math.min(options.timeoutMs ?? 10000, 120000);
|
|
55
|
+
this.maxRetries = options.maxRetries ?? 3;
|
|
56
|
+
this.fetchImpl = options.fetchImpl;
|
|
57
|
+
this.governanceDefaults = {
|
|
58
|
+
hitl: options.governanceDefaults?.hitl ?? DEFAULT_GOVERNANCE.hitl,
|
|
59
|
+
reviewers: options.governanceDefaults?.reviewers ?? DEFAULT_GOVERNANCE.reviewers,
|
|
60
|
+
escalationChannels: options.governanceDefaults?.escalationChannels ?? DEFAULT_GOVERNANCE.escalationChannels,
|
|
61
|
+
metadata: { ...DEFAULT_GOVERNANCE.metadata, ...(options.governanceDefaults?.metadata ?? {}) },
|
|
62
|
+
severity: options.governanceDefaults?.severity ?? DEFAULT_GOVERNANCE.severity,
|
|
63
|
+
autoEscalateAfterSeconds: options.governanceDefaults?.autoEscalateAfterSeconds ?? DEFAULT_GOVERNANCE.autoEscalateAfterSeconds,
|
|
64
|
+
};
|
|
65
|
+
this.fingerprint = {
|
|
66
|
+
...DEFAULT_FINGERPRINT_CONFIG,
|
|
67
|
+
...(options.fingerprint ?? {}),
|
|
68
|
+
};
|
|
69
|
+
this.captureArguments = options.captureArguments ?? false;
|
|
70
|
+
this.captureReturnValue = options.captureReturnValue ?? false;
|
|
71
|
+
this.maxSerializedFieldLength = options.maxSerializedFieldLength ?? 50000;
|
|
72
|
+
this.redactFields = options.redactFields ?? [];
|
|
73
|
+
}
|
|
74
|
+
mergedLabels(labels) {
|
|
75
|
+
return { ...this.defaultLabels, ...(labels ?? {}) };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAqB,MAAM,eAAe,CAAC;AAI9E,MAAM,OAAO,GAAG,aAAa,CAAC;AAE9B,SAAS,iBAAiB,CAAC,KAAa;IACtC,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AACpC,CAAC;AAED,MAAM,aAAa,GAAG,yNAAyN,CAAC;AAEhP,SAAS,iBAAiB,CAAC,QAAgB;IACzC,IAAI,CAAC;Q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OAAO,CAAC,kBAAkB,EAAE,kBAAkB,IAAI,kBAAkB,CAAC,kBAAkB;YAC3G,QAAQ,EAAE,EAAE,GAAG,kBAAkB,CAAC,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAC,kBAAkB,EAAE,QAAQ,IAAI,EAAE,CAAC,EAAE;YAC7F,QAAQ,EAAE,OAAO,CAAC,kBAAkB,EAAE,QAAQ,IAAI,kBAAkB,CAAC,QAAQ;YAC7E,wBAAwB,EACtB,OAAO,CAAC,kBAAkB,EAAE,wBAAwB,IAAI,kBAAkB,CAAC,wBAAwB;SACtG,CAAC;QACF,IAAI,CAAC,WAAW,GAAG;YACjB,GAAG,0BAA0B;YAC7B,GAAG,CAAC,OAAO,CAAC,WAAW,IAAI,EAAE,CAAC;SAC/B,CAAC;QACF,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,KAAK,CAAC;QAC1D,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC,kBAAkB,IAAI,KAAK,CAAC;QAC9D,IAAI,CAAC,wBAAwB,GAAG,OAAO,CAAC,wBAAwB,IAAI,KAAM,CAAC;QAC3E,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,EAAE,CAAC;IACjD,CAAC;IAED,YAAY,CAAC,MAA+B;QAC1C,OAAO,EAAE,GAAG,IAAI,CAAC,aAAa,EAAE,GAAG,CAAC,MAAM,IAAI,EAAE,CAAC,EAAE,CAAC;IACtD,CAAC;CACF"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { FingerprintBuilder } from './fingerprint';
|
|
2
|
+
export declare function generateUUID(): string;
|
|
3
|
+
/** Structured tool call record matching the shared ToolCallInfo model. */
|
|
4
|
+
export interface ToolCallRecord {
|
|
5
|
+
toolName: string;
|
|
6
|
+
toolArgs: Record<string, unknown>;
|
|
7
|
+
toolResult?: string;
|
|
8
|
+
toolId?: string;
|
|
9
|
+
success: boolean;
|
|
10
|
+
errorMessage?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Initialization options for ExecutionContext.
|
|
14
|
+
*
|
|
15
|
+
* REFACTORED:
|
|
16
|
+
* - Added workflowName for display name (from @instrument(name="..."))
|
|
17
|
+
* - agentId is kept for backward compatibility
|
|
18
|
+
* - Backend auto-registers workflows based on workflowName
|
|
19
|
+
*/
|
|
20
|
+
export interface ExecutionContextInit {
|
|
21
|
+
runId: string;
|
|
22
|
+
/** @deprecated Use workflowName instead */
|
|
23
|
+
agentId?: string;
|
|
24
|
+
operation: string;
|
|
25
|
+
triggerKey?: string;
|
|
26
|
+
hiddenEntrypoint?: boolean;
|
|
27
|
+
labels: Record<string, string>;
|
|
28
|
+
metadata: Record<string, string>;
|
|
29
|
+
args: unknown[];
|
|
30
|
+
fingerprintBuilder?: FingerprintBuilder;
|
|
31
|
+
/** @deprecated Backend assigns UUID */
|
|
32
|
+
workflowId?: string;
|
|
33
|
+
/** Display name from @instrument(name="...") */
|
|
34
|
+
workflowName?: string;
|
|
35
|
+
/** Unique identifier for this span (auto-generated if not provided). */
|
|
36
|
+
spanId?: string;
|
|
37
|
+
/** Parent span ID for trace hierarchy (root spans have no parent). */
|
|
38
|
+
parentSpanId?: string;
|
|
39
|
+
/** Nesting depth: 0 for root, parent.depth + 1 for children. */
|
|
40
|
+
depth?: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Execution context for instrumented functions.
|
|
44
|
+
*
|
|
45
|
+
* REFACTORED:
|
|
46
|
+
* - workflowName: Display name from @instrument(name="...") - sent to backend
|
|
47
|
+
* - agentId: DEPRECATED - kept for backward compatibility
|
|
48
|
+
* - Backend auto-registers workflows based on workflowName and assigns UUID
|
|
49
|
+
*/
|
|
50
|
+
export declare class ExecutionContext {
|
|
51
|
+
readonly runId: string;
|
|
52
|
+
/** @deprecated Use workflowName instead */
|
|
53
|
+
readonly agentId: string;
|
|
54
|
+
readonly operation: string;
|
|
55
|
+
readonly triggerKey?: string;
|
|
56
|
+
readonly hiddenEntrypoint: boolean;
|
|
57
|
+
readonly labels: Record<string, string>;
|
|
58
|
+
readonly metadata: Record<string, string>;
|
|
59
|
+
readonly args: unknown[];
|
|
60
|
+
readonly startedAt: Date;
|
|
61
|
+
readonly fingerprintBuilder?: FingerprintBuilder;
|
|
62
|
+
/** @deprecated Backend assigns UUID */
|
|
63
|
+
readonly workflowId?: string;
|
|
64
|
+
/** Display name from @instrument(name="...") - sent to backend */
|
|
65
|
+
readonly workflowName?: string;
|
|
66
|
+
completedAt?: Date;
|
|
67
|
+
error?: unknown;
|
|
68
|
+
result?: unknown;
|
|
69
|
+
status: 'pending' | 'success' | 'error' | 'blocked';
|
|
70
|
+
readonly spanId: string;
|
|
71
|
+
readonly parentSpanId?: string;
|
|
72
|
+
readonly depth: number;
|
|
73
|
+
tokensInput: number;
|
|
74
|
+
tokensOutput: number;
|
|
75
|
+
costUsd: number;
|
|
76
|
+
model?: string;
|
|
77
|
+
provider?: string;
|
|
78
|
+
nodeType?: string;
|
|
79
|
+
framework?: string;
|
|
80
|
+
promptText?: string;
|
|
81
|
+
systemPrompt?: string;
|
|
82
|
+
inputData?: string;
|
|
83
|
+
outputData?: string;
|
|
84
|
+
availableTools?: string[];
|
|
85
|
+
toolCalls: ToolCallRecord[];
|
|
86
|
+
attachments: Record<string, unknown>[];
|
|
87
|
+
inputContext?: string;
|
|
88
|
+
constructor(init: ExecutionContextInit);
|
|
89
|
+
finish(result?: unknown, error?: unknown): void;
|
|
90
|
+
get durationMs(): number | undefined;
|
|
91
|
+
/** Record token counts for this span. */
|
|
92
|
+
setTokens(input: number, output: number): void;
|
|
93
|
+
/** Record LLM cost in USD. */
|
|
94
|
+
setCost(cost: number): void;
|
|
95
|
+
/** Record the LLM model and provider used. */
|
|
96
|
+
setModel(model: string, provider?: string): void;
|
|
97
|
+
/** Record prompt and optional system prompt. */
|
|
98
|
+
setPrompt(prompt: string, systemPrompt?: string): void;
|
|
99
|
+
/** Record input/output data for this span. */
|
|
100
|
+
setIO(inputData?: string, outputData?: string): void;
|
|
101
|
+
/** Record RAG retrieval context for faithfulness evaluation. */
|
|
102
|
+
setInputContext(context: string): void;
|
|
103
|
+
/** Add a structured tool call record (capped at 200 per span). */
|
|
104
|
+
addToolCall(record: ToolCallRecord): void;
|
|
105
|
+
/**
|
|
106
|
+
* Record a node in the fingerprint.
|
|
107
|
+
*
|
|
108
|
+
* Called automatically during instrumentation to track workflow structure.
|
|
109
|
+
*
|
|
110
|
+
* @param name - Node name (typically the function name)
|
|
111
|
+
* @param nodeType - Type of node (llm, tool, function, retriever, etc.)
|
|
112
|
+
* @param config - Configuration dict for the node
|
|
113
|
+
* @param prompt - System prompt for LLM nodes
|
|
114
|
+
*/
|
|
115
|
+
recordNode(name: string, nodeType: string, config?: unknown, prompt?: string): void;
|
|
116
|
+
}
|
|
117
|
+
declare class ContextStorage {
|
|
118
|
+
private readonly storage;
|
|
119
|
+
private fallback?;
|
|
120
|
+
run<T>(context: ExecutionContext, fn: () => T): T;
|
|
121
|
+
get(): ExecutionContext | undefined;
|
|
122
|
+
}
|
|
123
|
+
export declare const contextStorage: ContextStorage;
|
|
124
|
+
export declare function currentContext(): ExecutionContext | undefined;
|
|
125
|
+
export {};
|
|
126
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAGxD,wBAAgB,YAAY,IAAI,MAAM,CAoBrC;AAED,0EAA0E;AAC1E,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;IACjB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S,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;IAK9C,8BAA8B;IAC9B,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAI3B,8CAA8C;IAC9C,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI;IAKhD,gDAAgD;IAChD,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI;IAKtD,8CAA8C;IAC9C,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI;IAKpD,gEAAgE;IAChE,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAItC,kEAAkE;IAClE,WAAW,CAAC,MAAM,EAAE,cAAc,GAAG,IAAI;IAKzC;;;;;;;;;OASG;IACH,UAAU,CACR,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,OAAO,EAChB,MAAM,CAAC,EAAE,MAAM,GACd,IAAI;CAKR;AAED,cAAM,cAAc;IAClB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqF;IAC7G,OAAO,CAAC,QAAQ,CAAC,CAAmB;IAEpC,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC;IAgBjD,GAAG,IAAI,gBAAgB,GAAG,SAAS;CAMpC;AAED,eAAO,MAAM,cAAc,gBAAuB,CAAC;AAEnD,wBAAgB,cAAc,IAAI,gBAAgB,GAAG,SAAS,CAE7D"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { GovernanceBlockedError } from './errors';
|
|
4
|
+
export function generateUUID() {
|
|
5
|
+
if (typeof globalThis.crypto?.randomUUID === 'function') {
|
|
6
|
+
return globalThis.crypto.randomUUID();
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
return randomUUID();
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
if (typeof globalThis.crypto?.getRandomValues === 'function') {
|
|
13
|
+
const bytes = new Uint8Array(16);
|
|
14
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
15
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
16
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
17
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
18
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
19
|
+
}
|
|
20
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
21
|
+
const r = (Math.random() * 16) | 0;
|
|
22
|
+
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const AsyncLocalStorageCtor = AsyncLocalStorage;
|
|
27
|
+
/**
|
|
28
|
+
* Execution context for instrumented functions.
|
|
29
|
+
*
|
|
30
|
+
* REFACTORED:
|
|
31
|
+
* - workflowName: Display name from @instrument(name="...") - sent to backend
|
|
32
|
+
* - agentId: DEPRECATED - kept for backward compatibility
|
|
33
|
+
* - Backend auto-registers workflows based on workflowName and assigns UUID
|
|
34
|
+
*/
|
|
35
|
+
export class ExecutionContext {
|
|
36
|
+
constructor(init) {
|
|
37
|
+
this.status = 'pending';
|
|
38
|
+
// ── Telemetry fields (parity with Python SDK) ──
|
|
39
|
+
this.tokensInput = 0;
|
|
40
|
+
this.tokensOutput = 0;
|
|
41
|
+
this.costUsd = 0;
|
|
42
|
+
this.toolCalls = [];
|
|
43
|
+
this.attachments = [];
|
|
44
|
+
this.runId = init.runId;
|
|
45
|
+
this.agentId = init.agentId ?? init.workflowName ?? '';
|
|
46
|
+
this.operation = init.operation;
|
|
47
|
+
this.triggerKey = init.triggerKey;
|
|
48
|
+
this.hiddenEntrypoint = Boolean(init.hiddenEntrypoint);
|
|
49
|
+
this.labels = init.labels;
|
|
50
|
+
this.metadata = init.metadata;
|
|
51
|
+
this.args = init.args;
|
|
52
|
+
this.startedAt = new Date();
|
|
53
|
+
this.fingerprintBuilder = init.fingerprintBuilder;
|
|
54
|
+
this.workflowId = init.workflowId ?? init.agentId;
|
|
55
|
+
this.workflowName = init.workflowName ?? init.agentId;
|
|
56
|
+
// Structural fields
|
|
57
|
+
this.spanId = init.spanId ?? generateUUID();
|
|
58
|
+
this.parentSpanId = init.parentSpanId;
|
|
59
|
+
this.depth = init.depth ?? 0;
|
|
60
|
+
}
|
|
61
|
+
finish(result, error) {
|
|
62
|
+
this.completedAt = new Date();
|
|
63
|
+
this.result = result;
|
|
64
|
+
this.error = error;
|
|
65
|
+
if (error) {
|
|
66
|
+
this.status = error instanceof GovernanceBlockedError ? 'blocked' : 'error';
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.status = 'success';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
get durationMs() {
|
|
73
|
+
if (!this.completedAt) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
return this.completedAt.getTime() - this.startedAt.getTime();
|
|
77
|
+
}
|
|
78
|
+
// ── Telemetry setters (matching Python SDK API) ──
|
|
79
|
+
/** Record token counts for this span. */
|
|
80
|
+
setTokens(input, output) {
|
|
81
|
+
this.tokensInput = input;
|
|
82
|
+
this.tokensOutput = output;
|
|
83
|
+
}
|
|
84
|
+
/** Record LLM cost in USD. */
|
|
85
|
+
setCost(cost) {
|
|
86
|
+
this.costUsd = cost;
|
|
87
|
+
}
|
|
88
|
+
/** Record the LLM model and provider used. */
|
|
89
|
+
setModel(model, provider) {
|
|
90
|
+
this.model = model;
|
|
91
|
+
if (provider)
|
|
92
|
+
this.provider = provider;
|
|
93
|
+
}
|
|
94
|
+
/** Record prompt and optional system prompt. */
|
|
95
|
+
setPrompt(prompt, systemPrompt) {
|
|
96
|
+
this.promptText = prompt;
|
|
97
|
+
if (systemPrompt)
|
|
98
|
+
this.systemPrompt = systemPrompt;
|
|
99
|
+
}
|
|
100
|
+
/** Record input/output data for this span. */
|
|
101
|
+
setIO(inputData, outputData) {
|
|
102
|
+
if (inputData !== undefined)
|
|
103
|
+
this.inputData = inputData;
|
|
104
|
+
if (outputData !== undefined)
|
|
105
|
+
this.outputData = outputData;
|
|
106
|
+
}
|
|
107
|
+
/** Record RAG retrieval context for faithfulness evaluation. */
|
|
108
|
+
setInputContext(context) {
|
|
109
|
+
this.inputContext = context.slice(0, 500000);
|
|
110
|
+
}
|
|
111
|
+
/** Add a structured tool call record (capped at 200 per span). */
|
|
112
|
+
addToolCall(record) {
|
|
113
|
+
if (this.toolCalls.length >= 200)
|
|
114
|
+
return;
|
|
115
|
+
this.toolCalls.push(record);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Record a node in the fingerprint.
|
|
119
|
+
*
|
|
120
|
+
* Called automatically during instrumentation to track workflow structure.
|
|
121
|
+
*
|
|
122
|
+
* @param name - Node name (typically the function name)
|
|
123
|
+
* @param nodeType - Type of node (llm, tool, function, retriever, etc.)
|
|
124
|
+
* @param config - Configuration dict for the node
|
|
125
|
+
* @param prompt - System prompt for LLM nodes
|
|
126
|
+
*/
|
|
127
|
+
recordNode(name, nodeType, config, prompt) {
|
|
128
|
+
if (this.fingerprintBuilder) {
|
|
129
|
+
this.fingerprintBuilder.recordNode(name, nodeType, config, prompt);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
class ContextStorage {
|
|
134
|
+
constructor() {
|
|
135
|
+
this.storage = AsyncLocalStorageCtor ? new AsyncLocalStorageCtor() : undefined;
|
|
136
|
+
}
|
|
137
|
+
run(context, fn) {
|
|
138
|
+
if (this.storage) {
|
|
139
|
+
return this.storage.run(context, fn);
|
|
140
|
+
}
|
|
141
|
+
const previous = this.fallback;
|
|
142
|
+
this.fallback = context;
|
|
143
|
+
const result = fn();
|
|
144
|
+
if (typeof result?.then === 'function') {
|
|
145
|
+
return result.finally(() => {
|
|
146
|
+
this.fallback = previous;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
this.fallback = previous;
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
get() {
|
|
153
|
+
if (this.storage) {
|
|
154
|
+
return this.storage.getStore() ?? undefined;
|
|
155
|
+
}
|
|
156
|
+
return this.fallback;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export const contextStorage = new ContextStorage();
|
|
160
|
+
export function currentContext() {
|
|
161
|
+
return contextStorage.get();
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAElD,MAAM,UAAU,YAAY;IAC1B,IAAI,OAAO,UAAU,CAAC,MAAM,EAAE,UAAU,KAAK,UAAU,EAAE,CAAC;QACxD,OAAO,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;IACxC,CAAC;IACD,IAAI,CAAC;QACH,OAAO,UAAU,EAAE,CAAC;IACtB,CAAC;IAAC,MAAM,CAAC;QACP,IAAI,OAAO,UAAU,CAAC,MAAM,EAAE,eAAe,KAAK,UAAU,EAAE,CAAC;YAC7D,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;YACjC,UAAU,CAAC,M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Q,MAAsC,CAAC,OAAO,CAAC,GAAG,EAAE;gBAC1D,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;YAC3B,CAAC,CAAM,CAAC;QACV,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,GAAG;QACD,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,SAAS,CAAC;QAC9C,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,cAAc,EAAE,CAAC;AAEnD,MAAM,UAAU,cAAc;IAC5B,OAAO,cAAc,CAAC,GAAG,EAAE,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { InstrumentationOptions } from './instrumentation';
|
|
2
|
+
type Method = (...args: unknown[]) => unknown;
|
|
3
|
+
export declare function instrument(options: InstrumentationOptions): (_target: unknown, _propertyKey: string | symbol, descriptor: PropertyDescriptor) => void;
|
|
4
|
+
export declare function withInstrumentation<T extends Method>(fn: T, options: InstrumentationOptions): T;
|
|
5
|
+
export {};
|
|
6
|
+
//# sourceMappingURL=decorators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.d.ts","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAmB,MAAM,mBAAmB,CAAC;AAK5E,KAAK,MAAM,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC;AAE9C,wBAAgB,UAAU,CAAC,OAAO,EAAE,sBAAsB,IAEtD,SAAS,OAAO,EAChB,cAAc,MAAM,GAAG,MAAM,EAC7B,YAAY,kBAAkB,KAC7B,IAAI,CAMR;AAED,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,MAAM,EAAE,EAAE,EAAE,CAAC,EAAE,OAAO,EAAE,sBAAsB,GAAG,CAAC,CAE/F"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { PluginNotInitializedError } from './errors';
|
|
2
|
+
import { validateOptions } from './instrumentation';
|
|
3
|
+
import { getInstance } from './plugin';
|
|
4
|
+
import { queueTrigger } from './triggerState';
|
|
5
|
+
import { isTriggerRegistered, markTriggerRegistered } from './utils';
|
|
6
|
+
export function instrument(options) {
|
|
7
|
+
return function instrumentDecorator(_target, _propertyKey, descriptor) {
|
|
8
|
+
if (!descriptor || typeof descriptor.value !== 'function') {
|
|
9
|
+
throw new Error('@instrument can only be applied to methods');
|
|
10
|
+
}
|
|
11
|
+
descriptor.value = createInstrumentedFunction(descriptor.value, options);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function withInstrumentation(fn, options) {
|
|
15
|
+
return createInstrumentedFunction(fn, options);
|
|
16
|
+
}
|
|
17
|
+
function createInstrumentedFunction(fn, options) {
|
|
18
|
+
validateOptions(options);
|
|
19
|
+
const wrapper = function instrumented(...args) {
|
|
20
|
+
const plugin = getInstance();
|
|
21
|
+
ensureTriggerRegistration(plugin, options, wrapper);
|
|
22
|
+
return plugin.execute(fn, this, args, options);
|
|
23
|
+
};
|
|
24
|
+
maybeRegisterTrigger(options, wrapper);
|
|
25
|
+
return wrapper;
|
|
26
|
+
}
|
|
27
|
+
function maybeRegisterTrigger(options, wrapper) {
|
|
28
|
+
if (!options.triggerKey) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const plugin = getInstance();
|
|
33
|
+
plugin.registerTrigger(options.triggerKey, wrapper, options.triggerDescription);
|
|
34
|
+
markTriggerRegistered(wrapper);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error instanceof PluginNotInitializedError) {
|
|
38
|
+
queueTrigger(options.triggerKey, wrapper, options.triggerDescription);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function ensureTriggerRegistration(plugin, options, wrapper) {
|
|
46
|
+
if (!options.triggerKey || isTriggerRegistered(wrapper)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
plugin.registerTrigger(options.triggerKey, wrapper, options.triggerDescription);
|
|
50
|
+
markTriggerRegistered(wrapper);
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAA0B,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAqB,WAAW,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAIrE,MAAM,UAAU,UAAU,CAAC,OAA+B;IACxD,OAAO,SAAS,mBAAmB,CACjC,OAAgB,EAChB,YAA6B,EAC7B,UAA8B;QAE9B,IAAI,CAAC,UAAU,IAAI,OAAO,UAAU,CAAC,KAAK,KAAK,UAAU,EAAE,CAAC;YAC1D,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;QAChE,CAAC;QACD,UAAU,CAAC,KAAK,GAAG,0BAA0B,CAAC,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC3E,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAmB,EAAK,EAAE,OAA+B;IAC1F,OAAO,0BAA0B,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;AACjD,CAAC;AAED,SAAS,0BAA0B,CAAmB,EAAK,EAAE,OA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