@trustgateai/sdk 1.0.0 → 1.1.0

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.
Files changed (38) hide show
  1. package/dist/{chunk-UUGIIO3Q.mjs → chunk-HWMHN2XV.mjs} +1 -1
  2. package/dist/chunk-OCDNSTSM.mjs +31 -0
  3. package/dist/chunk-QJUVF52Z.mjs +31 -0
  4. package/dist/{chunk-R2YXTU4L.mjs → chunk-STJ53FX3.mjs} +1 -1
  5. package/dist/{chunk-SV5FTXAH.mjs → chunk-Z2D2AATH.mjs} +1 -1
  6. package/dist/index.d.mts +6 -2
  7. package/dist/index.d.ts +6 -2
  8. package/dist/index.js +70 -3
  9. package/dist/index.mjs +32 -5
  10. package/dist/middleware/anthropic.d.mts +38 -0
  11. package/dist/middleware/anthropic.d.ts +38 -0
  12. package/dist/middleware/anthropic.js +158 -0
  13. package/dist/middleware/anthropic.mjs +9 -0
  14. package/dist/middleware/google.d.mts +45 -0
  15. package/dist/middleware/google.d.ts +45 -0
  16. package/dist/middleware/google.js +158 -0
  17. package/dist/middleware/google.mjs +9 -0
  18. package/dist/middleware/langchain.d.mts +1 -1
  19. package/dist/middleware/langchain.d.ts +1 -1
  20. package/dist/middleware/langchain.js +1 -1
  21. package/dist/middleware/langchain.mjs +2 -2
  22. package/dist/middleware/vercel-ai.d.mts +1 -1
  23. package/dist/middleware/vercel-ai.d.ts +1 -1
  24. package/dist/middleware/vercel-ai.js +1 -1
  25. package/dist/middleware/vercel-ai.mjs +2 -2
  26. package/dist/{types-BxyYog9f.d.ts → types-QFksyF5p.d.mts} +7 -1
  27. package/dist/{types-BxyYog9f.d.mts → types-QFksyF5p.d.ts} +7 -1
  28. package/package.json +70 -56
  29. package/src/client.ts +141 -125
  30. package/src/context.ts +132 -132
  31. package/src/index.ts +41 -31
  32. package/src/middleware/anthropic.ts +56 -0
  33. package/src/middleware/google.ts +63 -0
  34. package/src/middleware/langchain.ts +57 -57
  35. package/src/middleware/vercel-ai.ts +78 -78
  36. package/src/n8n.ts +51 -51
  37. package/src/types.ts +75 -68
  38. package/src/version.ts +1 -1
package/src/client.ts CHANGED
@@ -1,125 +1,141 @@
1
- import type { TrustGateConfig, RequestOptions, WorkflowContext } from "./types.js";
2
- import { getWorkflowContextFromEnv, buildContextHeaders } from "./context.js";
3
-
4
- function resolveContext(config: TrustGateConfig, options?: RequestOptions): WorkflowContext {
5
- return (
6
- options?.workflowContext ??
7
- config.workflowContext ??
8
- getWorkflowContextFromEnv()
9
- );
10
- }
11
-
12
- function mergeHeaders(
13
- config: TrustGateConfig,
14
- options?: RequestOptions,
15
- context?: WorkflowContext
16
- ): Record<string, string> {
17
- const out: Record<string, string> = {
18
- "content-type": "application/json",
19
- ...config.headers,
20
- ...options?.headers,
21
- };
22
- if (config.apiKey) {
23
- out["authorization"] = `Bearer ${config.apiKey}`;
24
- out["x-api-key"] = config.apiKey;
25
- }
26
- if (context) {
27
- Object.assign(out, buildContextHeaders(context));
28
- }
29
- return out;
30
- }
31
-
32
- /**
33
- * TrustGate Node.js client. Forwards requests to the TrustGate gateway with
34
- * workflow context for Shadow AI detection and observability.
35
- *
36
- * Context is sent in lowercase headers (e.g. x-trustgate-source) and as JSON
37
- * in x-trustgate-source-tool for parity with the Python SDK. The `workflow_step`
38
- * field in context is the key used to generate Gantt chart bars in the
39
- * Agent Intelligence dashboard.
40
- */
41
- export class TrustGate {
42
- private readonly config: TrustGateConfig;
43
-
44
- constructor(config: TrustGateConfig) {
45
- const baseUrl = config.baseUrl.replace(/\/$/, "");
46
- this.config = { ...config, baseUrl };
47
- }
48
-
49
- /** Base URL of the TrustGate gateway. */
50
- get baseUrl(): string {
51
- return this.config.baseUrl;
52
- }
53
-
54
- /**
55
- * Get the current effective workflow context (from config override or process.env).
56
- * Never null; defaults to source "local_script" when no managed env is detected.
57
- */
58
- getWorkflowContext(options?: RequestOptions): WorkflowContext {
59
- return resolveContext(this.config, options);
60
- }
61
-
62
- /**
63
- * Perform a non-streaming JSON request through TrustGate.
64
- */
65
- async fetch(path: string, init: RequestInit = {}, options?: RequestOptions): Promise<Response> {
66
- const context = resolveContext(this.config, options);
67
- const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
68
- const headers = mergeHeaders(this.config, options, context);
69
- const timeout = options?.timeout ?? 60_000;
70
-
71
- const controller = new AbortController();
72
- const id = setTimeout(() => controller.abort(), timeout);
73
- try {
74
- const res = await fetch(url, {
75
- ...init,
76
- headers: { ...headers, ...(init.headers as Record<string, string>) },
77
- signal: init.signal ?? controller.signal,
78
- });
79
- return res;
80
- } finally {
81
- clearTimeout(id);
82
- }
83
- }
84
-
85
- /**
86
- * Perform a streaming request (SSE). Forwards the response stream and ensures
87
- * workflow_id (and other context) is sent in the initial request so the
88
- * background task is attributed correctly. Set Accept: text/event-stream
89
- * when calling for SSE endpoints.
90
- */
91
- async fetchStream(
92
- path: string,
93
- init: RequestInit = {},
94
- options?: RequestOptions
95
- ): Promise<Response> {
96
- const context = resolveContext(this.config, options);
97
- const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
98
- const headers = mergeHeaders(this.config, options, context);
99
- const mergedHeaders = {
100
- accept: "text/event-stream",
101
- ...headers,
102
- ...(init.headers as Record<string, string>),
103
- };
104
-
105
- const res = await fetch(url, {
106
- ...init,
107
- headers: mergedHeaders,
108
- signal: init.signal,
109
- });
110
-
111
- if (!res.ok || !res.body) {
112
- return res;
113
- }
114
-
115
- if (!res.headers.get("content-type")?.includes("text/event-stream")) {
116
- return res;
117
- }
118
-
119
- return new Response(res.body, {
120
- status: res.status,
121
- statusText: res.statusText,
122
- headers: res.headers,
123
- });
124
- }
125
- }
1
+ import type { TrustGateConfig, RequestOptions, WorkflowContext, Provider } from "./types.js";
2
+ import { getWorkflowContextFromEnv, buildContextHeaders } from "./context.js";
3
+
4
+ function resolveContext(config: TrustGateConfig, options?: RequestOptions): WorkflowContext {
5
+ return (
6
+ options?.workflowContext ??
7
+ config.workflowContext ??
8
+ getWorkflowContextFromEnv()
9
+ );
10
+ }
11
+
12
+ function authHeadersForProvider(apiKey: string, provider: Provider | undefined): Record<string, string> {
13
+ switch (provider) {
14
+ case "anthropic":
15
+ case "google":
16
+ return { "x-api-key": apiKey };
17
+ case "openai":
18
+ default:
19
+ return {
20
+ authorization: `Bearer ${apiKey}`,
21
+ "x-api-key": apiKey,
22
+ };
23
+ }
24
+ }
25
+
26
+ function mergeHeaders(
27
+ config: TrustGateConfig,
28
+ options?: RequestOptions,
29
+ context?: WorkflowContext
30
+ ): Record<string, string> {
31
+ const out: Record<string, string> = {
32
+ "content-type": "application/json",
33
+ ...config.headers,
34
+ ...options?.headers,
35
+ };
36
+ if (config.apiKey) {
37
+ const provider = options?.provider ?? config.provider;
38
+ Object.assign(out, authHeadersForProvider(config.apiKey, provider));
39
+ }
40
+ if (context) {
41
+ Object.assign(out, buildContextHeaders(context));
42
+ }
43
+ return out;
44
+ }
45
+
46
+ /**
47
+ * TrustGate Node.js client. Forwards requests to the TrustGate gateway with
48
+ * workflow context for Shadow AI detection and observability.
49
+ *
50
+ * Context is sent in lowercase headers (e.g. x-trustgate-source) and as JSON
51
+ * in x-trustgate-source-tool for parity with the Python SDK. The `workflow_step`
52
+ * field in context is the key used to generate Gantt chart bars in the
53
+ * Agent Intelligence dashboard.
54
+ */
55
+ export class TrustGate {
56
+ private readonly config: TrustGateConfig;
57
+
58
+ constructor(config: TrustGateConfig) {
59
+ const baseUrl = config.baseUrl.replace(/\/$/, "");
60
+ this.config = { ...config, baseUrl };
61
+ }
62
+
63
+ /** Base URL of the TrustGate gateway. */
64
+ get baseUrl(): string {
65
+ return this.config.baseUrl;
66
+ }
67
+
68
+ /**
69
+ * Get the current effective workflow context (from config override or process.env).
70
+ * Never null; defaults to source "local_script" when no managed env is detected.
71
+ */
72
+ getWorkflowContext(options?: RequestOptions): WorkflowContext {
73
+ return resolveContext(this.config, options);
74
+ }
75
+
76
+ /**
77
+ * Perform a non-streaming JSON request through TrustGate.
78
+ * @param options.provider - Optional provider for this request ('openai' | 'anthropic' | 'google');
79
+ * controls auth header format (e.g. x-api-key only for anthropic/google).
80
+ */
81
+ async fetch(path: string, init: RequestInit = {}, options?: RequestOptions): Promise<Response> {
82
+ const context = resolveContext(this.config, options);
83
+ const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
84
+ const headers = mergeHeaders(this.config, options, context);
85
+ const timeout = options?.timeout ?? 60_000;
86
+
87
+ const controller = new AbortController();
88
+ const id = setTimeout(() => controller.abort(), timeout);
89
+ try {
90
+ const res = await fetch(url, {
91
+ ...init,
92
+ headers: { ...headers, ...(init.headers as Record<string, string>) },
93
+ signal: init.signal ?? controller.signal,
94
+ });
95
+ return res;
96
+ } finally {
97
+ clearTimeout(id);
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Perform a streaming request (SSE). Forwards the response stream and ensures
103
+ * workflow_id (and other context) is sent in the initial request so the
104
+ * background task is attributed correctly. Set Accept: text/event-stream
105
+ * when calling for SSE endpoints.
106
+ */
107
+ async fetchStream(
108
+ path: string,
109
+ init: RequestInit = {},
110
+ options?: RequestOptions
111
+ ): Promise<Response> {
112
+ const context = resolveContext(this.config, options);
113
+ const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
114
+ const headers = mergeHeaders(this.config, options, context);
115
+ const mergedHeaders = {
116
+ accept: "text/event-stream",
117
+ ...headers,
118
+ ...(init.headers as Record<string, string>),
119
+ };
120
+
121
+ const res = await fetch(url, {
122
+ ...init,
123
+ headers: mergedHeaders,
124
+ signal: init.signal,
125
+ });
126
+
127
+ if (!res.ok || !res.body) {
128
+ return res;
129
+ }
130
+
131
+ if (!res.headers.get("content-type")?.includes("text/event-stream")) {
132
+ return res;
133
+ }
134
+
135
+ return new Response(res.body, {
136
+ status: res.status,
137
+ statusText: res.statusText,
138
+ headers: res.headers,
139
+ });
140
+ }
141
+ }
package/src/context.ts CHANGED
@@ -1,132 +1,132 @@
1
- import type { WorkflowContext, RuntimeMetadata } from "./types.js";
2
- import { SDK_VERSION } from "./version.js";
3
-
4
- /** Lowercase header prefix for proxy/gateway parity with Python SDK. */
5
- export const HEADER_PREFIX = "x-trustgate";
6
- /** Header carrying the full context JSON (structurally identical to Python SDK). */
7
- export const SOURCE_TOOL_HEADER = "x-trustgate-source-tool";
8
-
9
- /** Env keys we check for workflow/source metadata (Shadow AI context). */
10
- const N8N_KEYS = [
11
- "N8N_WORKFLOW_ID",
12
- "N8N_EXECUTION_ID",
13
- "N8N_WORKFLOW_NAME",
14
- "N8N_EXECUTION_MODE",
15
- ] as const;
16
-
17
- const VERCEL_KEYS = [
18
- "VERCEL",
19
- "VERCEL_ENV",
20
- "VERCEL_URL",
21
- "VERCEL_GIT_COMMIT_REF",
22
- "VERCEL_GIT_REPO_ID",
23
- "VERCEL_GIT_COMMIT_SHA",
24
- ] as const;
25
-
26
- const GITHUB_KEYS = [
27
- "GITHUB_ACTION",
28
- "GITHUB_ACTIONS",
29
- "GITHUB_WORKFLOW",
30
- "GITHUB_RUN_ID",
31
- "GITHUB_RUN_NUMBER",
32
- "GITHUB_REPOSITORY",
33
- "GITHUB_SHA",
34
- ] as const;
35
-
36
- function pickEnv(keys: readonly string[]): Record<string, string> {
37
- const out: Record<string, string> = {};
38
- for (const key of keys) {
39
- const v = process.env[key];
40
- if (v !== undefined && v !== "") out[key] = v;
41
- }
42
- return out;
43
- }
44
-
45
- /** Base runtime metadata (sdk_version, node_version, os, arch) for Python parity. */
46
- function getBaseMetadata(): RuntimeMetadata {
47
- return {
48
- sdk_version: SDK_VERSION,
49
- node_version: process.version,
50
- os: process.platform,
51
- arch: process.arch,
52
- };
53
- }
54
-
55
- /**
56
- * Searches process.env for workflow metadata from n8n, Vercel, or GitHub.
57
- * If no managed environment is detected, returns context with source "local_script"
58
- * so local developer usage is correctly tagged as Shadow AI in the dashboard.
59
- * Never returns null.
60
- */
61
- export function getWorkflowContextFromEnv(): WorkflowContext {
62
- const baseMeta = getBaseMetadata();
63
-
64
- const n8n = pickEnv(N8N_KEYS as unknown as string[]);
65
- if (Object.keys(n8n).length > 0) {
66
- return {
67
- source: "n8n",
68
- workflow_id: n8n.N8N_WORKFLOW_ID,
69
- execution_id: n8n.N8N_EXECUTION_ID,
70
- metadata: { ...baseMeta, ...n8n },
71
- };
72
- }
73
-
74
- const vercel = pickEnv(VERCEL_KEYS as unknown as string[]);
75
- if (Object.keys(vercel).length > 0) {
76
- return {
77
- source: "vercel",
78
- workflow_id: vercel.VERCEL_GIT_REPO_ID || vercel.VERCEL_URL,
79
- execution_id: vercel.VERCEL_GIT_COMMIT_SHA,
80
- metadata: { ...baseMeta, ...vercel },
81
- };
82
- }
83
-
84
- const github = pickEnv(GITHUB_KEYS as unknown as string[]);
85
- if (Object.keys(github).length > 0) {
86
- return {
87
- source: "github",
88
- workflow_id: github.GITHUB_WORKFLOW || github.GITHUB_REPOSITORY,
89
- execution_id: github.GITHUB_RUN_ID || github.GITHUB_RUN_NUMBER,
90
- metadata: { ...baseMeta, ...github },
91
- };
92
- }
93
-
94
- return {
95
- source: "local_script",
96
- metadata: baseMeta,
97
- };
98
- }
99
-
100
- /**
101
- * JSON payload for x-trustgate-source-tool header (structurally identical to Python SDK).
102
- */
103
- export function buildSourceToolJson(ctx: WorkflowContext): string {
104
- const meta = ctx.metadata ?? getBaseMetadata();
105
- const payload: Record<string, unknown> = {
106
- source: ctx.source,
107
- metadata: meta,
108
- };
109
- if (ctx.workflow_id !== undefined) payload.workflow_id = ctx.workflow_id;
110
- if (ctx.execution_id !== undefined) payload.execution_id = ctx.execution_id;
111
- if (ctx.workflow_step !== undefined) payload.workflow_step = ctx.workflow_step;
112
- return JSON.stringify(payload);
113
- }
114
-
115
- /**
116
- * Builds lowercase TrustGate context headers plus x-trustgate-source-tool JSON.
117
- * Use for proxy/gateway parity and Python SDK contract.
118
- */
119
- export function buildContextHeaders(ctx: WorkflowContext): Record<string, string> {
120
- const meta = ctx.metadata ?? getBaseMetadata();
121
- const h: Record<string, string> = {
122
- [SOURCE_TOOL_HEADER]: buildSourceToolJson({ ...ctx, metadata: meta }),
123
- [`${HEADER_PREFIX}-source`]: ctx.source,
124
- };
125
- if (ctx.workflow_id) h[`${HEADER_PREFIX}-workflow-id`] = ctx.workflow_id;
126
- if (ctx.execution_id) h[`${HEADER_PREFIX}-execution-id`] = ctx.execution_id;
127
- if (ctx.workflow_step) h[`${HEADER_PREFIX}-workflow-step`] = ctx.workflow_step;
128
- for (const [k, v] of Object.entries(meta)) {
129
- if (v) h[`${HEADER_PREFIX}-meta-${k.toLowerCase()}`] = String(v);
130
- }
131
- return h;
132
- }
1
+ import type { WorkflowContext, RuntimeMetadata } from "./types.js";
2
+ import { SDK_VERSION } from "./version.js";
3
+
4
+ /** Lowercase header prefix for proxy/gateway parity with Python SDK. */
5
+ export const HEADER_PREFIX = "x-trustgate";
6
+ /** Header carrying the full context JSON (structurally identical to Python SDK). */
7
+ export const SOURCE_TOOL_HEADER = "x-trustgate-source-tool";
8
+
9
+ /** Env keys we check for workflow/source metadata (Shadow AI context). */
10
+ const N8N_KEYS = [
11
+ "N8N_WORKFLOW_ID",
12
+ "N8N_EXECUTION_ID",
13
+ "N8N_WORKFLOW_NAME",
14
+ "N8N_EXECUTION_MODE",
15
+ ] as const;
16
+
17
+ const VERCEL_KEYS = [
18
+ "VERCEL",
19
+ "VERCEL_ENV",
20
+ "VERCEL_URL",
21
+ "VERCEL_GIT_COMMIT_REF",
22
+ "VERCEL_GIT_REPO_ID",
23
+ "VERCEL_GIT_COMMIT_SHA",
24
+ ] as const;
25
+
26
+ const GITHUB_KEYS = [
27
+ "GITHUB_ACTION",
28
+ "GITHUB_ACTIONS",
29
+ "GITHUB_WORKFLOW",
30
+ "GITHUB_RUN_ID",
31
+ "GITHUB_RUN_NUMBER",
32
+ "GITHUB_REPOSITORY",
33
+ "GITHUB_SHA",
34
+ ] as const;
35
+
36
+ function pickEnv(keys: readonly string[]): Record<string, string> {
37
+ const out: Record<string, string> = {};
38
+ for (const key of keys) {
39
+ const v = process.env[key];
40
+ if (v !== undefined && v !== "") out[key] = v;
41
+ }
42
+ return out;
43
+ }
44
+
45
+ /** Base runtime metadata (sdk_version, node_version, os, arch) for Python parity. */
46
+ function getBaseMetadata(): RuntimeMetadata {
47
+ return {
48
+ sdk_version: SDK_VERSION,
49
+ node_version: process.version,
50
+ os: process.platform,
51
+ arch: process.arch,
52
+ };
53
+ }
54
+
55
+ /**
56
+ * Searches process.env for workflow metadata from n8n, Vercel, or GitHub.
57
+ * If no managed environment is detected, returns context with source "local_script"
58
+ * so local developer usage is correctly tagged as Shadow AI in the dashboard.
59
+ * Never returns null.
60
+ */
61
+ export function getWorkflowContextFromEnv(): WorkflowContext {
62
+ const baseMeta = getBaseMetadata();
63
+
64
+ const n8n = pickEnv(N8N_KEYS as unknown as string[]);
65
+ if (Object.keys(n8n).length > 0) {
66
+ return {
67
+ source: "n8n",
68
+ workflow_id: n8n.N8N_WORKFLOW_ID,
69
+ execution_id: n8n.N8N_EXECUTION_ID,
70
+ metadata: { ...baseMeta, ...n8n },
71
+ };
72
+ }
73
+
74
+ const vercel = pickEnv(VERCEL_KEYS as unknown as string[]);
75
+ if (Object.keys(vercel).length > 0) {
76
+ return {
77
+ source: "vercel",
78
+ workflow_id: vercel.VERCEL_GIT_REPO_ID || vercel.VERCEL_URL,
79
+ execution_id: vercel.VERCEL_GIT_COMMIT_SHA,
80
+ metadata: { ...baseMeta, ...vercel },
81
+ };
82
+ }
83
+
84
+ const github = pickEnv(GITHUB_KEYS as unknown as string[]);
85
+ if (Object.keys(github).length > 0) {
86
+ return {
87
+ source: "github",
88
+ workflow_id: github.GITHUB_WORKFLOW || github.GITHUB_REPOSITORY,
89
+ execution_id: github.GITHUB_RUN_ID || github.GITHUB_RUN_NUMBER,
90
+ metadata: { ...baseMeta, ...github },
91
+ };
92
+ }
93
+
94
+ return {
95
+ source: "local_script",
96
+ metadata: baseMeta,
97
+ };
98
+ }
99
+
100
+ /**
101
+ * JSON payload for x-trustgate-source-tool header (structurally identical to Python SDK).
102
+ */
103
+ export function buildSourceToolJson(ctx: WorkflowContext): string {
104
+ const meta = ctx.metadata ?? getBaseMetadata();
105
+ const payload: Record<string, unknown> = {
106
+ source: ctx.source,
107
+ metadata: meta,
108
+ };
109
+ if (ctx.workflow_id !== undefined) payload.workflow_id = ctx.workflow_id;
110
+ if (ctx.execution_id !== undefined) payload.execution_id = ctx.execution_id;
111
+ if (ctx.workflow_step !== undefined) payload.workflow_step = ctx.workflow_step;
112
+ return JSON.stringify(payload);
113
+ }
114
+
115
+ /**
116
+ * Builds lowercase TrustGate context headers plus x-trustgate-source-tool JSON.
117
+ * Use for proxy/gateway parity and Python SDK contract.
118
+ */
119
+ export function buildContextHeaders(ctx: WorkflowContext): Record<string, string> {
120
+ const meta = ctx.metadata ?? getBaseMetadata();
121
+ const h: Record<string, string> = {
122
+ [SOURCE_TOOL_HEADER]: buildSourceToolJson({ ...ctx, metadata: meta }),
123
+ [`${HEADER_PREFIX}-source`]: ctx.source,
124
+ };
125
+ if (ctx.workflow_id) h[`${HEADER_PREFIX}-workflow-id`] = ctx.workflow_id;
126
+ if (ctx.execution_id) h[`${HEADER_PREFIX}-execution-id`] = ctx.execution_id;
127
+ if (ctx.workflow_step) h[`${HEADER_PREFIX}-workflow-step`] = ctx.workflow_step;
128
+ for (const [k, v] of Object.entries(meta)) {
129
+ if (v) h[`${HEADER_PREFIX}-meta-${k.toLowerCase()}`] = String(v);
130
+ }
131
+ return h;
132
+ }
package/src/index.ts CHANGED
@@ -1,31 +1,41 @@
1
- /**
2
- * TrustGate Node.js SDK
3
- *
4
- * Provides a TypeScript client, workflow context from n8n/Vercel/GitHub env vars,
5
- * middleware for Vercel AI SDK and LangChain, n8n header snippet helper, and
6
- * SSE streaming with workflow_id preserved for Shadow AI detection.
7
- */
8
-
9
- export { TrustGate } from "./client.js";
10
- export {
11
- getWorkflowContextFromEnv,
12
- buildContextHeaders,
13
- buildSourceToolJson,
14
- HEADER_PREFIX,
15
- SOURCE_TOOL_HEADER,
16
- } from "./context.js";
17
- export { getN8nHeaderSnippet } from "./n8n.js";
18
- export {
19
- getTrustGateHeaders,
20
- createTrustGateOpenAIOptions,
21
- type TrustGateOpenAIOptions,
22
- } from "./middleware/vercel-ai.js";
23
- export { createTrustGateLangChainConfig } from "./middleware/langchain.js";
24
-
25
- export type {
26
- WorkflowContext,
27
- RuntimeMetadata,
28
- TrustGateConfig,
29
- RequestOptions,
30
- N8nHeaderSnippet,
31
- } from "./types.js";
1
+ /**
2
+ * TrustGate Node.js SDK
3
+ *
4
+ * Provides a TypeScript client, workflow context from n8n/Vercel/GitHub env vars,
5
+ * middleware for Vercel AI SDK and LangChain, n8n header snippet helper, and
6
+ * SSE streaming with workflow_id preserved for Shadow AI detection.
7
+ */
8
+
9
+ export { TrustGate } from "./client.js";
10
+ export {
11
+ getWorkflowContextFromEnv,
12
+ buildContextHeaders,
13
+ buildSourceToolJson,
14
+ HEADER_PREFIX,
15
+ SOURCE_TOOL_HEADER,
16
+ } from "./context.js";
17
+ export { getN8nHeaderSnippet } from "./n8n.js";
18
+ export {
19
+ getTrustGateHeaders,
20
+ createTrustGateOpenAIOptions,
21
+ type TrustGateOpenAIOptions,
22
+ } from "./middleware/vercel-ai.js";
23
+ export { createTrustGateLangChainConfig } from "./middleware/langchain.js";
24
+
25
+ export {
26
+ getAnthropicHeaders,
27
+ createTrustGateAnthropicConfig,
28
+ } from "./middleware/anthropic.js";
29
+ export {
30
+ getGeminiHeaders,
31
+ createTrustGateGeminiConfig,
32
+ } from "./middleware/google.js";
33
+
34
+ export type {
35
+ WorkflowContext,
36
+ RuntimeMetadata,
37
+ TrustGateConfig,
38
+ RequestOptions,
39
+ N8nHeaderSnippet,
40
+ Provider,
41
+ } from "./types.js";
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Anthropic middleware: use TrustGate as the base URL for @anthropic-ai/sdk.
3
+ *
4
+ * Pass the returned config to the Anthropic client so all requests go through
5
+ * TrustGate with workflow context and x-api-key for Shadow AI detection.
6
+ *
7
+ * Example:
8
+ *
9
+ * import Anthropic from '@anthropic-ai/sdk';
10
+ * import { createTrustGateAnthropicConfig } from '@trustgateai/sdk/middleware/anthropic';
11
+ *
12
+ * const config = createTrustGateAnthropicConfig({
13
+ * baseUrl: process.env.TRUSTGATE_BASE_URL!,
14
+ * apiKey: process.env.TRUSTGATE_API_KEY,
15
+ * });
16
+ *
17
+ * const anthropic = new Anthropic(config);
18
+ * const message = await anthropic.messages.create({ model: 'claude-3-5-sonnet-20241022', ... });
19
+ */
20
+
21
+ import type { TrustGateConfig } from "../types.js";
22
+ import { getWorkflowContextFromEnv, buildContextHeaders } from "../context.js";
23
+
24
+ /**
25
+ * Returns headers compatible with @anthropic-ai/sdk. Uses x-api-key only (no
26
+ * Authorization header). Use with defaultHeaders or when building requests manually.
27
+ */
28
+ export function getAnthropicHeaders(config: TrustGateConfig): Record<string, string> {
29
+ const ctx = config.workflowContext ?? getWorkflowContextFromEnv();
30
+ const headers: Record<string, string> = {
31
+ ...config.headers,
32
+ ...buildContextHeaders(ctx),
33
+ };
34
+ if (config.apiKey) {
35
+ headers["x-api-key"] = config.apiKey;
36
+ }
37
+ return headers;
38
+ }
39
+
40
+ /**
41
+ * Returns a config object for the Anthropic SDK (baseURL, apiKey, defaultHeaders).
42
+ * Injects TrustGate context headers and x-api-key so requests go through the gateway.
43
+ */
44
+ export function createTrustGateAnthropicConfig(config: TrustGateConfig): {
45
+ baseURL: string;
46
+ apiKey: string | null;
47
+ defaultHeaders?: Record<string, string>;
48
+ } {
49
+ const baseURL = config.baseUrl.replace(/\/$/, "");
50
+ const headers = getAnthropicHeaders(config);
51
+ return {
52
+ baseURL,
53
+ apiKey: config.apiKey ?? null,
54
+ defaultHeaders: Object.keys(headers).length ? headers : undefined,
55
+ };
56
+ }