@trustgateai/sdk 1.0.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.
package/dist/index.js ADDED
@@ -0,0 +1,314 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ HEADER_PREFIX: () => HEADER_PREFIX,
24
+ SOURCE_TOOL_HEADER: () => SOURCE_TOOL_HEADER,
25
+ TrustGate: () => TrustGate,
26
+ buildContextHeaders: () => buildContextHeaders,
27
+ buildSourceToolJson: () => buildSourceToolJson,
28
+ createTrustGateLangChainConfig: () => createTrustGateLangChainConfig,
29
+ createTrustGateOpenAIOptions: () => createTrustGateOpenAIOptions,
30
+ getN8nHeaderSnippet: () => getN8nHeaderSnippet,
31
+ getTrustGateHeaders: () => getTrustGateHeaders,
32
+ getWorkflowContextFromEnv: () => getWorkflowContextFromEnv
33
+ });
34
+ module.exports = __toCommonJS(index_exports);
35
+
36
+ // src/version.ts
37
+ var SDK_VERSION = "1.0.0";
38
+
39
+ // src/context.ts
40
+ var HEADER_PREFIX = "x-trustgate";
41
+ var SOURCE_TOOL_HEADER = "x-trustgate-source-tool";
42
+ var N8N_KEYS = [
43
+ "N8N_WORKFLOW_ID",
44
+ "N8N_EXECUTION_ID",
45
+ "N8N_WORKFLOW_NAME",
46
+ "N8N_EXECUTION_MODE"
47
+ ];
48
+ var VERCEL_KEYS = [
49
+ "VERCEL",
50
+ "VERCEL_ENV",
51
+ "VERCEL_URL",
52
+ "VERCEL_GIT_COMMIT_REF",
53
+ "VERCEL_GIT_REPO_ID",
54
+ "VERCEL_GIT_COMMIT_SHA"
55
+ ];
56
+ var GITHUB_KEYS = [
57
+ "GITHUB_ACTION",
58
+ "GITHUB_ACTIONS",
59
+ "GITHUB_WORKFLOW",
60
+ "GITHUB_RUN_ID",
61
+ "GITHUB_RUN_NUMBER",
62
+ "GITHUB_REPOSITORY",
63
+ "GITHUB_SHA"
64
+ ];
65
+ function pickEnv(keys) {
66
+ const out = {};
67
+ for (const key of keys) {
68
+ const v = process.env[key];
69
+ if (v !== void 0 && v !== "") out[key] = v;
70
+ }
71
+ return out;
72
+ }
73
+ function getBaseMetadata() {
74
+ return {
75
+ sdk_version: SDK_VERSION,
76
+ node_version: process.version,
77
+ os: process.platform,
78
+ arch: process.arch
79
+ };
80
+ }
81
+ function getWorkflowContextFromEnv() {
82
+ const baseMeta = getBaseMetadata();
83
+ const n8n = pickEnv(N8N_KEYS);
84
+ if (Object.keys(n8n).length > 0) {
85
+ return {
86
+ source: "n8n",
87
+ workflow_id: n8n.N8N_WORKFLOW_ID,
88
+ execution_id: n8n.N8N_EXECUTION_ID,
89
+ metadata: { ...baseMeta, ...n8n }
90
+ };
91
+ }
92
+ const vercel = pickEnv(VERCEL_KEYS);
93
+ if (Object.keys(vercel).length > 0) {
94
+ return {
95
+ source: "vercel",
96
+ workflow_id: vercel.VERCEL_GIT_REPO_ID || vercel.VERCEL_URL,
97
+ execution_id: vercel.VERCEL_GIT_COMMIT_SHA,
98
+ metadata: { ...baseMeta, ...vercel }
99
+ };
100
+ }
101
+ const github = pickEnv(GITHUB_KEYS);
102
+ if (Object.keys(github).length > 0) {
103
+ return {
104
+ source: "github",
105
+ workflow_id: github.GITHUB_WORKFLOW || github.GITHUB_REPOSITORY,
106
+ execution_id: github.GITHUB_RUN_ID || github.GITHUB_RUN_NUMBER,
107
+ metadata: { ...baseMeta, ...github }
108
+ };
109
+ }
110
+ return {
111
+ source: "local_script",
112
+ metadata: baseMeta
113
+ };
114
+ }
115
+ function buildSourceToolJson(ctx) {
116
+ const meta = ctx.metadata ?? getBaseMetadata();
117
+ const payload = {
118
+ source: ctx.source,
119
+ metadata: meta
120
+ };
121
+ if (ctx.workflow_id !== void 0) payload.workflow_id = ctx.workflow_id;
122
+ if (ctx.execution_id !== void 0) payload.execution_id = ctx.execution_id;
123
+ if (ctx.workflow_step !== void 0) payload.workflow_step = ctx.workflow_step;
124
+ return JSON.stringify(payload);
125
+ }
126
+ function buildContextHeaders(ctx) {
127
+ const meta = ctx.metadata ?? getBaseMetadata();
128
+ const h = {
129
+ [SOURCE_TOOL_HEADER]: buildSourceToolJson({ ...ctx, metadata: meta }),
130
+ [`${HEADER_PREFIX}-source`]: ctx.source
131
+ };
132
+ if (ctx.workflow_id) h[`${HEADER_PREFIX}-workflow-id`] = ctx.workflow_id;
133
+ if (ctx.execution_id) h[`${HEADER_PREFIX}-execution-id`] = ctx.execution_id;
134
+ if (ctx.workflow_step) h[`${HEADER_PREFIX}-workflow-step`] = ctx.workflow_step;
135
+ for (const [k, v] of Object.entries(meta)) {
136
+ if (v) h[`${HEADER_PREFIX}-meta-${k.toLowerCase()}`] = String(v);
137
+ }
138
+ return h;
139
+ }
140
+
141
+ // src/client.ts
142
+ function resolveContext(config, options) {
143
+ return options?.workflowContext ?? config.workflowContext ?? getWorkflowContextFromEnv();
144
+ }
145
+ function mergeHeaders(config, options, context) {
146
+ const out = {
147
+ "content-type": "application/json",
148
+ ...config.headers,
149
+ ...options?.headers
150
+ };
151
+ if (config.apiKey) {
152
+ out["authorization"] = `Bearer ${config.apiKey}`;
153
+ out["x-api-key"] = config.apiKey;
154
+ }
155
+ if (context) {
156
+ Object.assign(out, buildContextHeaders(context));
157
+ }
158
+ return out;
159
+ }
160
+ var TrustGate = class {
161
+ constructor(config) {
162
+ const baseUrl = config.baseUrl.replace(/\/$/, "");
163
+ this.config = { ...config, baseUrl };
164
+ }
165
+ /** Base URL of the TrustGate gateway. */
166
+ get baseUrl() {
167
+ return this.config.baseUrl;
168
+ }
169
+ /**
170
+ * Get the current effective workflow context (from config override or process.env).
171
+ * Never null; defaults to source "local_script" when no managed env is detected.
172
+ */
173
+ getWorkflowContext(options) {
174
+ return resolveContext(this.config, options);
175
+ }
176
+ /**
177
+ * Perform a non-streaming JSON request through TrustGate.
178
+ */
179
+ async fetch(path, init = {}, options) {
180
+ const context = resolveContext(this.config, options);
181
+ const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
182
+ const headers = mergeHeaders(this.config, options, context);
183
+ const timeout = options?.timeout ?? 6e4;
184
+ const controller = new AbortController();
185
+ const id = setTimeout(() => controller.abort(), timeout);
186
+ try {
187
+ const res = await fetch(url, {
188
+ ...init,
189
+ headers: { ...headers, ...init.headers },
190
+ signal: init.signal ?? controller.signal
191
+ });
192
+ return res;
193
+ } finally {
194
+ clearTimeout(id);
195
+ }
196
+ }
197
+ /**
198
+ * Perform a streaming request (SSE). Forwards the response stream and ensures
199
+ * workflow_id (and other context) is sent in the initial request so the
200
+ * background task is attributed correctly. Set Accept: text/event-stream
201
+ * when calling for SSE endpoints.
202
+ */
203
+ async fetchStream(path, init = {}, options) {
204
+ const context = resolveContext(this.config, options);
205
+ const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
206
+ const headers = mergeHeaders(this.config, options, context);
207
+ const mergedHeaders = {
208
+ accept: "text/event-stream",
209
+ ...headers,
210
+ ...init.headers
211
+ };
212
+ const res = await fetch(url, {
213
+ ...init,
214
+ headers: mergedHeaders,
215
+ signal: init.signal
216
+ });
217
+ if (!res.ok || !res.body) {
218
+ return res;
219
+ }
220
+ if (!res.headers.get("content-type")?.includes("text/event-stream")) {
221
+ return res;
222
+ }
223
+ return new Response(res.body, {
224
+ status: res.status,
225
+ statusText: res.statusText,
226
+ headers: res.headers
227
+ });
228
+ }
229
+ };
230
+
231
+ // src/n8n.ts
232
+ function getN8nHeaderSnippet(apiKey, baseUrl) {
233
+ const key = apiKey ?? process.env.TRUSTGATE_API_KEY ?? "";
234
+ const url = baseUrl ?? process.env.TRUSTGATE_BASE_URL ?? "";
235
+ const ctx = getWorkflowContextFromEnv();
236
+ const contextHeaders = buildContextHeaders(ctx);
237
+ const headers = [];
238
+ if (url) {
239
+ headers.push({ name: `${HEADER_PREFIX}-gateway-url`, value: url });
240
+ }
241
+ if (key) {
242
+ headers.push({ name: "authorization", value: `Bearer ${key}` });
243
+ headers.push({ name: "x-api-key", value: key });
244
+ }
245
+ for (const [name, value] of Object.entries(contextHeaders)) {
246
+ headers.push({ name, value });
247
+ }
248
+ const json = JSON.stringify(
249
+ headers.reduce((acc, { name, value }) => {
250
+ acc[name] = value;
251
+ return acc;
252
+ }, {}),
253
+ null,
254
+ 2
255
+ );
256
+ return { headers, json };
257
+ }
258
+
259
+ // src/middleware/vercel-ai.ts
260
+ function getTrustGateHeaders(workflowContext) {
261
+ const ctx = workflowContext ?? getWorkflowContextFromEnv();
262
+ return buildContextHeaders(ctx);
263
+ }
264
+ function createTrustGateOpenAIOptions(options) {
265
+ const baseUrl = options.baseUrl.replace(/\/$/, "");
266
+ const headers = {
267
+ ...options.headers,
268
+ ...getTrustGateHeaders(options.workflowContext)
269
+ };
270
+ if (options.apiKey) {
271
+ headers["authorization"] = `Bearer ${options.apiKey}`;
272
+ headers["x-api-key"] = options.apiKey;
273
+ }
274
+ return {
275
+ baseURL: baseUrl,
276
+ apiKey: options.openaiApiKey ?? options.apiKey,
277
+ headers: Object.keys(headers).length ? headers : void 0
278
+ };
279
+ }
280
+
281
+ // src/middleware/langchain.ts
282
+ function createTrustGateLangChainConfig(config) {
283
+ const baseUrl = config.baseUrl.replace(/\/$/, "");
284
+ const ctx = config.workflowContext ?? getWorkflowContextFromEnv();
285
+ const headers = {
286
+ ...config.headers,
287
+ ...buildContextHeaders(ctx)
288
+ };
289
+ if (config.apiKey) {
290
+ headers["authorization"] = `Bearer ${config.apiKey}`;
291
+ headers["x-api-key"] = config.apiKey;
292
+ }
293
+ return {
294
+ baseURL: baseUrl,
295
+ defaultHeaders: Object.keys(headers).length ? headers : void 0,
296
+ configuration: {
297
+ baseURL: baseUrl,
298
+ defaultHeaders: Object.keys(headers).length ? headers : void 0
299
+ }
300
+ };
301
+ }
302
+ // Annotate the CommonJS export names for ESM import in node:
303
+ 0 && (module.exports = {
304
+ HEADER_PREFIX,
305
+ SOURCE_TOOL_HEADER,
306
+ TrustGate,
307
+ buildContextHeaders,
308
+ buildSourceToolJson,
309
+ createTrustGateLangChainConfig,
310
+ createTrustGateOpenAIOptions,
311
+ getN8nHeaderSnippet,
312
+ getTrustGateHeaders,
313
+ getWorkflowContextFromEnv
314
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,144 @@
1
+ import {
2
+ createTrustGateLangChainConfig
3
+ } from "./chunk-R2YXTU4L.mjs";
4
+ import {
5
+ createTrustGateOpenAIOptions,
6
+ getTrustGateHeaders
7
+ } from "./chunk-UUGIIO3Q.mjs";
8
+ import {
9
+ HEADER_PREFIX,
10
+ SOURCE_TOOL_HEADER,
11
+ buildContextHeaders,
12
+ buildSourceToolJson,
13
+ getWorkflowContextFromEnv
14
+ } from "./chunk-SV5FTXAH.mjs";
15
+
16
+ // src/client.ts
17
+ function resolveContext(config, options) {
18
+ return options?.workflowContext ?? config.workflowContext ?? getWorkflowContextFromEnv();
19
+ }
20
+ function mergeHeaders(config, options, context) {
21
+ const out = {
22
+ "content-type": "application/json",
23
+ ...config.headers,
24
+ ...options?.headers
25
+ };
26
+ if (config.apiKey) {
27
+ out["authorization"] = `Bearer ${config.apiKey}`;
28
+ out["x-api-key"] = config.apiKey;
29
+ }
30
+ if (context) {
31
+ Object.assign(out, buildContextHeaders(context));
32
+ }
33
+ return out;
34
+ }
35
+ var TrustGate = class {
36
+ constructor(config) {
37
+ const baseUrl = config.baseUrl.replace(/\/$/, "");
38
+ this.config = { ...config, baseUrl };
39
+ }
40
+ /** Base URL of the TrustGate gateway. */
41
+ get baseUrl() {
42
+ return this.config.baseUrl;
43
+ }
44
+ /**
45
+ * Get the current effective workflow context (from config override or process.env).
46
+ * Never null; defaults to source "local_script" when no managed env is detected.
47
+ */
48
+ getWorkflowContext(options) {
49
+ return resolveContext(this.config, options);
50
+ }
51
+ /**
52
+ * Perform a non-streaming JSON request through TrustGate.
53
+ */
54
+ async fetch(path, init = {}, options) {
55
+ const context = resolveContext(this.config, options);
56
+ const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
57
+ const headers = mergeHeaders(this.config, options, context);
58
+ const timeout = options?.timeout ?? 6e4;
59
+ const controller = new AbortController();
60
+ const id = setTimeout(() => controller.abort(), timeout);
61
+ try {
62
+ const res = await fetch(url, {
63
+ ...init,
64
+ headers: { ...headers, ...init.headers },
65
+ signal: init.signal ?? controller.signal
66
+ });
67
+ return res;
68
+ } finally {
69
+ clearTimeout(id);
70
+ }
71
+ }
72
+ /**
73
+ * Perform a streaming request (SSE). Forwards the response stream and ensures
74
+ * workflow_id (and other context) is sent in the initial request so the
75
+ * background task is attributed correctly. Set Accept: text/event-stream
76
+ * when calling for SSE endpoints.
77
+ */
78
+ async fetchStream(path, init = {}, options) {
79
+ const context = resolveContext(this.config, options);
80
+ const url = path.startsWith("http") ? path : `${this.config.baseUrl}/${path.replace(/^\//, "")}`;
81
+ const headers = mergeHeaders(this.config, options, context);
82
+ const mergedHeaders = {
83
+ accept: "text/event-stream",
84
+ ...headers,
85
+ ...init.headers
86
+ };
87
+ const res = await fetch(url, {
88
+ ...init,
89
+ headers: mergedHeaders,
90
+ signal: init.signal
91
+ });
92
+ if (!res.ok || !res.body) {
93
+ return res;
94
+ }
95
+ if (!res.headers.get("content-type")?.includes("text/event-stream")) {
96
+ return res;
97
+ }
98
+ return new Response(res.body, {
99
+ status: res.status,
100
+ statusText: res.statusText,
101
+ headers: res.headers
102
+ });
103
+ }
104
+ };
105
+
106
+ // src/n8n.ts
107
+ function getN8nHeaderSnippet(apiKey, baseUrl) {
108
+ const key = apiKey ?? process.env.TRUSTGATE_API_KEY ?? "";
109
+ const url = baseUrl ?? process.env.TRUSTGATE_BASE_URL ?? "";
110
+ const ctx = getWorkflowContextFromEnv();
111
+ const contextHeaders = buildContextHeaders(ctx);
112
+ const headers = [];
113
+ if (url) {
114
+ headers.push({ name: `${HEADER_PREFIX}-gateway-url`, value: url });
115
+ }
116
+ if (key) {
117
+ headers.push({ name: "authorization", value: `Bearer ${key}` });
118
+ headers.push({ name: "x-api-key", value: key });
119
+ }
120
+ for (const [name, value] of Object.entries(contextHeaders)) {
121
+ headers.push({ name, value });
122
+ }
123
+ const json = JSON.stringify(
124
+ headers.reduce((acc, { name, value }) => {
125
+ acc[name] = value;
126
+ return acc;
127
+ }, {}),
128
+ null,
129
+ 2
130
+ );
131
+ return { headers, json };
132
+ }
133
+ export {
134
+ HEADER_PREFIX,
135
+ SOURCE_TOOL_HEADER,
136
+ TrustGate,
137
+ buildContextHeaders,
138
+ buildSourceToolJson,
139
+ createTrustGateLangChainConfig,
140
+ createTrustGateOpenAIOptions,
141
+ getN8nHeaderSnippet,
142
+ getTrustGateHeaders,
143
+ getWorkflowContextFromEnv
144
+ };
@@ -0,0 +1,41 @@
1
+ import { T as TrustGateConfig } from '../types-BxyYog9f.mjs';
2
+
3
+ /**
4
+ * LangChain middleware: use TrustGate as the OpenAI endpoint with one line.
5
+ *
6
+ * ChatOpenAI (and other LangChain OpenAI integrations) support baseURL and
7
+ * defaultHeaders. Point them to your TrustGate gateway and inject workflow
8
+ * context so Shadow AI detection can attribute requests.
9
+ *
10
+ * Example:
11
+ *
12
+ * import { ChatOpenAI } from '@langchain/openai';
13
+ * import { createTrustGateLangChainConfig } from 'trustgate-node/middleware/langchain';
14
+ *
15
+ * const config = createTrustGateLangChainConfig({
16
+ * baseUrl: process.env.TRUSTGATE_BASE_URL!,
17
+ * apiKey: process.env.TRUSTGATE_API_KEY,
18
+ * });
19
+ *
20
+ * const llm = new ChatOpenAI({
21
+ * ...config,
22
+ * modelName: 'gpt-4',
23
+ * temperature: 0,
24
+ * });
25
+ */
26
+
27
+ /**
28
+ * Configuration to pass to LangChain's ChatOpenAI (or other OpenAI-based classes).
29
+ * Sets baseURL and defaultHeaders (lowercase keys) so requests go through TrustGate
30
+ * with workflow context and x-trustgate-source-tool JSON for Shadow AI detection.
31
+ */
32
+ declare function createTrustGateLangChainConfig(config: TrustGateConfig): {
33
+ configuration?: {
34
+ baseURL: string;
35
+ defaultHeaders?: Record<string, string>;
36
+ };
37
+ baseURL?: string;
38
+ defaultHeaders?: Record<string, string>;
39
+ };
40
+
41
+ export { createTrustGateLangChainConfig };
@@ -0,0 +1,41 @@
1
+ import { T as TrustGateConfig } from '../types-BxyYog9f.js';
2
+
3
+ /**
4
+ * LangChain middleware: use TrustGate as the OpenAI endpoint with one line.
5
+ *
6
+ * ChatOpenAI (and other LangChain OpenAI integrations) support baseURL and
7
+ * defaultHeaders. Point them to your TrustGate gateway and inject workflow
8
+ * context so Shadow AI detection can attribute requests.
9
+ *
10
+ * Example:
11
+ *
12
+ * import { ChatOpenAI } from '@langchain/openai';
13
+ * import { createTrustGateLangChainConfig } from 'trustgate-node/middleware/langchain';
14
+ *
15
+ * const config = createTrustGateLangChainConfig({
16
+ * baseUrl: process.env.TRUSTGATE_BASE_URL!,
17
+ * apiKey: process.env.TRUSTGATE_API_KEY,
18
+ * });
19
+ *
20
+ * const llm = new ChatOpenAI({
21
+ * ...config,
22
+ * modelName: 'gpt-4',
23
+ * temperature: 0,
24
+ * });
25
+ */
26
+
27
+ /**
28
+ * Configuration to pass to LangChain's ChatOpenAI (or other OpenAI-based classes).
29
+ * Sets baseURL and defaultHeaders (lowercase keys) so requests go through TrustGate
30
+ * with workflow context and x-trustgate-source-tool JSON for Shadow AI detection.
31
+ */
32
+ declare function createTrustGateLangChainConfig(config: TrustGateConfig): {
33
+ configuration?: {
34
+ baseURL: string;
35
+ defaultHeaders?: Record<string, string>;
36
+ };
37
+ baseURL?: string;
38
+ defaultHeaders?: Record<string, string>;
39
+ };
40
+
41
+ export { createTrustGateLangChainConfig };
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/middleware/langchain.ts
21
+ var langchain_exports = {};
22
+ __export(langchain_exports, {
23
+ createTrustGateLangChainConfig: () => createTrustGateLangChainConfig
24
+ });
25
+ module.exports = __toCommonJS(langchain_exports);
26
+
27
+ // src/version.ts
28
+ var SDK_VERSION = "1.0.0";
29
+
30
+ // src/context.ts
31
+ var HEADER_PREFIX = "x-trustgate";
32
+ var SOURCE_TOOL_HEADER = "x-trustgate-source-tool";
33
+ var N8N_KEYS = [
34
+ "N8N_WORKFLOW_ID",
35
+ "N8N_EXECUTION_ID",
36
+ "N8N_WORKFLOW_NAME",
37
+ "N8N_EXECUTION_MODE"
38
+ ];
39
+ var VERCEL_KEYS = [
40
+ "VERCEL",
41
+ "VERCEL_ENV",
42
+ "VERCEL_URL",
43
+ "VERCEL_GIT_COMMIT_REF",
44
+ "VERCEL_GIT_REPO_ID",
45
+ "VERCEL_GIT_COMMIT_SHA"
46
+ ];
47
+ var GITHUB_KEYS = [
48
+ "GITHUB_ACTION",
49
+ "GITHUB_ACTIONS",
50
+ "GITHUB_WORKFLOW",
51
+ "GITHUB_RUN_ID",
52
+ "GITHUB_RUN_NUMBER",
53
+ "GITHUB_REPOSITORY",
54
+ "GITHUB_SHA"
55
+ ];
56
+ function pickEnv(keys) {
57
+ const out = {};
58
+ for (const key of keys) {
59
+ const v = process.env[key];
60
+ if (v !== void 0 && v !== "") out[key] = v;
61
+ }
62
+ return out;
63
+ }
64
+ function getBaseMetadata() {
65
+ return {
66
+ sdk_version: SDK_VERSION,
67
+ node_version: process.version,
68
+ os: process.platform,
69
+ arch: process.arch
70
+ };
71
+ }
72
+ function getWorkflowContextFromEnv() {
73
+ const baseMeta = getBaseMetadata();
74
+ const n8n = pickEnv(N8N_KEYS);
75
+ if (Object.keys(n8n).length > 0) {
76
+ return {
77
+ source: "n8n",
78
+ workflow_id: n8n.N8N_WORKFLOW_ID,
79
+ execution_id: n8n.N8N_EXECUTION_ID,
80
+ metadata: { ...baseMeta, ...n8n }
81
+ };
82
+ }
83
+ const vercel = pickEnv(VERCEL_KEYS);
84
+ if (Object.keys(vercel).length > 0) {
85
+ return {
86
+ source: "vercel",
87
+ workflow_id: vercel.VERCEL_GIT_REPO_ID || vercel.VERCEL_URL,
88
+ execution_id: vercel.VERCEL_GIT_COMMIT_SHA,
89
+ metadata: { ...baseMeta, ...vercel }
90
+ };
91
+ }
92
+ const github = pickEnv(GITHUB_KEYS);
93
+ if (Object.keys(github).length > 0) {
94
+ return {
95
+ source: "github",
96
+ workflow_id: github.GITHUB_WORKFLOW || github.GITHUB_REPOSITORY,
97
+ execution_id: github.GITHUB_RUN_ID || github.GITHUB_RUN_NUMBER,
98
+ metadata: { ...baseMeta, ...github }
99
+ };
100
+ }
101
+ return {
102
+ source: "local_script",
103
+ metadata: baseMeta
104
+ };
105
+ }
106
+ function buildSourceToolJson(ctx) {
107
+ const meta = ctx.metadata ?? getBaseMetadata();
108
+ const payload = {
109
+ source: ctx.source,
110
+ metadata: meta
111
+ };
112
+ if (ctx.workflow_id !== void 0) payload.workflow_id = ctx.workflow_id;
113
+ if (ctx.execution_id !== void 0) payload.execution_id = ctx.execution_id;
114
+ if (ctx.workflow_step !== void 0) payload.workflow_step = ctx.workflow_step;
115
+ return JSON.stringify(payload);
116
+ }
117
+ function buildContextHeaders(ctx) {
118
+ const meta = ctx.metadata ?? getBaseMetadata();
119
+ const h = {
120
+ [SOURCE_TOOL_HEADER]: buildSourceToolJson({ ...ctx, metadata: meta }),
121
+ [`${HEADER_PREFIX}-source`]: ctx.source
122
+ };
123
+ if (ctx.workflow_id) h[`${HEADER_PREFIX}-workflow-id`] = ctx.workflow_id;
124
+ if (ctx.execution_id) h[`${HEADER_PREFIX}-execution-id`] = ctx.execution_id;
125
+ if (ctx.workflow_step) h[`${HEADER_PREFIX}-workflow-step`] = ctx.workflow_step;
126
+ for (const [k, v] of Object.entries(meta)) {
127
+ if (v) h[`${HEADER_PREFIX}-meta-${k.toLowerCase()}`] = String(v);
128
+ }
129
+ return h;
130
+ }
131
+
132
+ // src/middleware/langchain.ts
133
+ function createTrustGateLangChainConfig(config) {
134
+ const baseUrl = config.baseUrl.replace(/\/$/, "");
135
+ const ctx = config.workflowContext ?? getWorkflowContextFromEnv();
136
+ const headers = {
137
+ ...config.headers,
138
+ ...buildContextHeaders(ctx)
139
+ };
140
+ if (config.apiKey) {
141
+ headers["authorization"] = `Bearer ${config.apiKey}`;
142
+ headers["x-api-key"] = config.apiKey;
143
+ }
144
+ return {
145
+ baseURL: baseUrl,
146
+ defaultHeaders: Object.keys(headers).length ? headers : void 0,
147
+ configuration: {
148
+ baseURL: baseUrl,
149
+ defaultHeaders: Object.keys(headers).length ? headers : void 0
150
+ }
151
+ };
152
+ }
153
+ // Annotate the CommonJS export names for ESM import in node:
154
+ 0 && (module.exports = {
155
+ createTrustGateLangChainConfig
156
+ });
@@ -0,0 +1,7 @@
1
+ import {
2
+ createTrustGateLangChainConfig
3
+ } from "../chunk-R2YXTU4L.mjs";
4
+ import "../chunk-SV5FTXAH.mjs";
5
+ export {
6
+ createTrustGateLangChainConfig
7
+ };