agentid-sdk 0.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.
- package/LICENSE +21 -0
- package/README.md +51 -0
- package/dist/chunk-5VHWMLV2.mjs +232 -0
- package/dist/index.d.mts +55 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +1538 -0
- package/dist/index.mjs +1270 -0
- package/dist/langchain-BdIOZZVq.d.mts +128 -0
- package/dist/langchain-BdIOZZVq.d.ts +128 -0
- package/dist/langchain.d.mts +1 -0
- package/dist/langchain.d.ts +1 -0
- package/dist/langchain.js +256 -0
- package/dist/langchain.mjs +6 -0
- package/package.json +54 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
type CapabilityConfig = {
|
|
2
|
+
block_pii_leakage: boolean;
|
|
3
|
+
block_db_access: boolean;
|
|
4
|
+
block_code_execution: boolean;
|
|
5
|
+
block_toxicity: boolean;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
interface GuardParams {
|
|
9
|
+
input: string;
|
|
10
|
+
system_id: string;
|
|
11
|
+
user_id?: string;
|
|
12
|
+
}
|
|
13
|
+
interface GuardResponse {
|
|
14
|
+
allowed: boolean;
|
|
15
|
+
reason?: string;
|
|
16
|
+
detected_pii?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface RequestOptions {
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
}
|
|
21
|
+
interface LogParams {
|
|
22
|
+
event_id?: string;
|
|
23
|
+
system_id?: string;
|
|
24
|
+
input: string;
|
|
25
|
+
output: string;
|
|
26
|
+
model: string;
|
|
27
|
+
usage?: Record<string, number>;
|
|
28
|
+
tokens?: Record<string, number>;
|
|
29
|
+
latency?: number;
|
|
30
|
+
user_id?: string;
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
event_type?: "start" | "complete" | "error" | "human_override" | "security_alert" | "security_block" | "security_policy_violation";
|
|
33
|
+
severity?: "info" | "warning" | "error" | "high";
|
|
34
|
+
timestamp?: string;
|
|
35
|
+
}
|
|
36
|
+
type AgentIDConfig = {
|
|
37
|
+
apiKey: string;
|
|
38
|
+
baseUrl?: string;
|
|
39
|
+
piiMasking?: boolean;
|
|
40
|
+
checkInjection?: boolean;
|
|
41
|
+
aiScanEnabled?: boolean;
|
|
42
|
+
storePii?: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type PreparedInput = {
|
|
46
|
+
sanitizedInput: string;
|
|
47
|
+
capabilityConfig: CapabilityConfig;
|
|
48
|
+
};
|
|
49
|
+
declare class AgentID {
|
|
50
|
+
private baseUrl;
|
|
51
|
+
private apiKey;
|
|
52
|
+
private piiMasking;
|
|
53
|
+
private checkInjection;
|
|
54
|
+
private aiScanEnabled;
|
|
55
|
+
private storePii;
|
|
56
|
+
private pii;
|
|
57
|
+
private localEnforcer;
|
|
58
|
+
private injectionScanner;
|
|
59
|
+
constructor(config: AgentIDConfig);
|
|
60
|
+
private resolveApiKey;
|
|
61
|
+
getCapabilityConfig(force?: boolean, options?: RequestOptions): Promise<CapabilityConfig>;
|
|
62
|
+
private getCachedCapabilityConfig;
|
|
63
|
+
prepareInputForDispatch(params: {
|
|
64
|
+
input: string;
|
|
65
|
+
systemId: string;
|
|
66
|
+
stream: boolean;
|
|
67
|
+
skipInjectionScan?: boolean;
|
|
68
|
+
}, options?: RequestOptions): Promise<PreparedInput>;
|
|
69
|
+
scanPromptInjection(input: string, options?: RequestOptions): Promise<void>;
|
|
70
|
+
private withMaskedOpenAIRequest;
|
|
71
|
+
private logSecurityPolicyViolation;
|
|
72
|
+
/**
|
|
73
|
+
* GUARD: Checks limits, PII, and security before execution.
|
|
74
|
+
* FAIL-CLOSED: Returns allowed=false if the API fails.
|
|
75
|
+
*/
|
|
76
|
+
guard(params: GuardParams, options?: RequestOptions): Promise<GuardResponse>;
|
|
77
|
+
/**
|
|
78
|
+
* LOG: Sends telemetry after execution.
|
|
79
|
+
* Non-blocking / Fire-and-forget.
|
|
80
|
+
*/
|
|
81
|
+
log(params: LogParams, options?: RequestOptions): void;
|
|
82
|
+
/**
|
|
83
|
+
* Analytics alias for telemetry logging.
|
|
84
|
+
*/
|
|
85
|
+
analytics(params: LogParams, options?: RequestOptions): void;
|
|
86
|
+
/**
|
|
87
|
+
* Trace alias for telemetry logging.
|
|
88
|
+
*/
|
|
89
|
+
trace(params: LogParams, options?: RequestOptions): void;
|
|
90
|
+
/**
|
|
91
|
+
* Wrap an OpenAI client once; AgentID will automatically:
|
|
92
|
+
* - run guard() before chat.completions.create
|
|
93
|
+
* - measure latency
|
|
94
|
+
* - fire-and-forget ingest logging
|
|
95
|
+
*/
|
|
96
|
+
wrapOpenAI<T>(openai: T, options: {
|
|
97
|
+
system_id: string;
|
|
98
|
+
apiKey?: string;
|
|
99
|
+
api_key?: string;
|
|
100
|
+
resolveApiKey?: (request: Record<string, unknown>) => string | undefined;
|
|
101
|
+
}): T;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* LangChainJS callback handler (dependency-free shape).
|
|
106
|
+
*
|
|
107
|
+
* Usage (LangChain):
|
|
108
|
+
* callbacks: [new AgentIDCallbackHandler(agent, { system_id: "..." })]
|
|
109
|
+
*/
|
|
110
|
+
declare class AgentIDCallbackHandler {
|
|
111
|
+
private agent;
|
|
112
|
+
private systemId;
|
|
113
|
+
private apiKeyOverride?;
|
|
114
|
+
private runs;
|
|
115
|
+
constructor(agent: AgentID, options: {
|
|
116
|
+
system_id: string;
|
|
117
|
+
apiKey?: string;
|
|
118
|
+
api_key?: string;
|
|
119
|
+
});
|
|
120
|
+
private get requestOptions();
|
|
121
|
+
private preflight;
|
|
122
|
+
handleLLMStart(serialized: unknown, prompts: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
123
|
+
handleChatModelStart(serialized: unknown, messages: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
124
|
+
handleLLMEnd(output: unknown, runId?: string): Promise<void>;
|
|
125
|
+
handleLLMError(err: unknown, runId?: string): Promise<void>;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { AgentID as A, type GuardParams as G, type LogParams as L, type PreparedInput as P, type RequestOptions as R, AgentIDCallbackHandler as a, type GuardResponse as b };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
type CapabilityConfig = {
|
|
2
|
+
block_pii_leakage: boolean;
|
|
3
|
+
block_db_access: boolean;
|
|
4
|
+
block_code_execution: boolean;
|
|
5
|
+
block_toxicity: boolean;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
interface GuardParams {
|
|
9
|
+
input: string;
|
|
10
|
+
system_id: string;
|
|
11
|
+
user_id?: string;
|
|
12
|
+
}
|
|
13
|
+
interface GuardResponse {
|
|
14
|
+
allowed: boolean;
|
|
15
|
+
reason?: string;
|
|
16
|
+
detected_pii?: boolean;
|
|
17
|
+
}
|
|
18
|
+
interface RequestOptions {
|
|
19
|
+
apiKey?: string;
|
|
20
|
+
}
|
|
21
|
+
interface LogParams {
|
|
22
|
+
event_id?: string;
|
|
23
|
+
system_id?: string;
|
|
24
|
+
input: string;
|
|
25
|
+
output: string;
|
|
26
|
+
model: string;
|
|
27
|
+
usage?: Record<string, number>;
|
|
28
|
+
tokens?: Record<string, number>;
|
|
29
|
+
latency?: number;
|
|
30
|
+
user_id?: string;
|
|
31
|
+
metadata?: Record<string, unknown>;
|
|
32
|
+
event_type?: "start" | "complete" | "error" | "human_override" | "security_alert" | "security_block" | "security_policy_violation";
|
|
33
|
+
severity?: "info" | "warning" | "error" | "high";
|
|
34
|
+
timestamp?: string;
|
|
35
|
+
}
|
|
36
|
+
type AgentIDConfig = {
|
|
37
|
+
apiKey: string;
|
|
38
|
+
baseUrl?: string;
|
|
39
|
+
piiMasking?: boolean;
|
|
40
|
+
checkInjection?: boolean;
|
|
41
|
+
aiScanEnabled?: boolean;
|
|
42
|
+
storePii?: boolean;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type PreparedInput = {
|
|
46
|
+
sanitizedInput: string;
|
|
47
|
+
capabilityConfig: CapabilityConfig;
|
|
48
|
+
};
|
|
49
|
+
declare class AgentID {
|
|
50
|
+
private baseUrl;
|
|
51
|
+
private apiKey;
|
|
52
|
+
private piiMasking;
|
|
53
|
+
private checkInjection;
|
|
54
|
+
private aiScanEnabled;
|
|
55
|
+
private storePii;
|
|
56
|
+
private pii;
|
|
57
|
+
private localEnforcer;
|
|
58
|
+
private injectionScanner;
|
|
59
|
+
constructor(config: AgentIDConfig);
|
|
60
|
+
private resolveApiKey;
|
|
61
|
+
getCapabilityConfig(force?: boolean, options?: RequestOptions): Promise<CapabilityConfig>;
|
|
62
|
+
private getCachedCapabilityConfig;
|
|
63
|
+
prepareInputForDispatch(params: {
|
|
64
|
+
input: string;
|
|
65
|
+
systemId: string;
|
|
66
|
+
stream: boolean;
|
|
67
|
+
skipInjectionScan?: boolean;
|
|
68
|
+
}, options?: RequestOptions): Promise<PreparedInput>;
|
|
69
|
+
scanPromptInjection(input: string, options?: RequestOptions): Promise<void>;
|
|
70
|
+
private withMaskedOpenAIRequest;
|
|
71
|
+
private logSecurityPolicyViolation;
|
|
72
|
+
/**
|
|
73
|
+
* GUARD: Checks limits, PII, and security before execution.
|
|
74
|
+
* FAIL-CLOSED: Returns allowed=false if the API fails.
|
|
75
|
+
*/
|
|
76
|
+
guard(params: GuardParams, options?: RequestOptions): Promise<GuardResponse>;
|
|
77
|
+
/**
|
|
78
|
+
* LOG: Sends telemetry after execution.
|
|
79
|
+
* Non-blocking / Fire-and-forget.
|
|
80
|
+
*/
|
|
81
|
+
log(params: LogParams, options?: RequestOptions): void;
|
|
82
|
+
/**
|
|
83
|
+
* Analytics alias for telemetry logging.
|
|
84
|
+
*/
|
|
85
|
+
analytics(params: LogParams, options?: RequestOptions): void;
|
|
86
|
+
/**
|
|
87
|
+
* Trace alias for telemetry logging.
|
|
88
|
+
*/
|
|
89
|
+
trace(params: LogParams, options?: RequestOptions): void;
|
|
90
|
+
/**
|
|
91
|
+
* Wrap an OpenAI client once; AgentID will automatically:
|
|
92
|
+
* - run guard() before chat.completions.create
|
|
93
|
+
* - measure latency
|
|
94
|
+
* - fire-and-forget ingest logging
|
|
95
|
+
*/
|
|
96
|
+
wrapOpenAI<T>(openai: T, options: {
|
|
97
|
+
system_id: string;
|
|
98
|
+
apiKey?: string;
|
|
99
|
+
api_key?: string;
|
|
100
|
+
resolveApiKey?: (request: Record<string, unknown>) => string | undefined;
|
|
101
|
+
}): T;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* LangChainJS callback handler (dependency-free shape).
|
|
106
|
+
*
|
|
107
|
+
* Usage (LangChain):
|
|
108
|
+
* callbacks: [new AgentIDCallbackHandler(agent, { system_id: "..." })]
|
|
109
|
+
*/
|
|
110
|
+
declare class AgentIDCallbackHandler {
|
|
111
|
+
private agent;
|
|
112
|
+
private systemId;
|
|
113
|
+
private apiKeyOverride?;
|
|
114
|
+
private runs;
|
|
115
|
+
constructor(agent: AgentID, options: {
|
|
116
|
+
system_id: string;
|
|
117
|
+
apiKey?: string;
|
|
118
|
+
api_key?: string;
|
|
119
|
+
});
|
|
120
|
+
private get requestOptions();
|
|
121
|
+
private preflight;
|
|
122
|
+
handleLLMStart(serialized: unknown, prompts: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
123
|
+
handleChatModelStart(serialized: unknown, messages: unknown, runId?: string, _parentRunId?: string, extraParams?: unknown): Promise<void>;
|
|
124
|
+
handleLLMEnd(output: unknown, runId?: string): Promise<void>;
|
|
125
|
+
handleLLMError(err: unknown, runId?: string): Promise<void>;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { AgentID as A, type GuardParams as G, type LogParams as L, type PreparedInput as P, type RequestOptions as R, AgentIDCallbackHandler as a, type GuardResponse as b };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as AgentIDCallbackHandler } from './langchain-BdIOZZVq.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { a as AgentIDCallbackHandler } from './langchain-BdIOZZVq.js';
|
|
@@ -0,0 +1,256 @@
|
|
|
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/langchain.ts
|
|
21
|
+
var langchain_exports = {};
|
|
22
|
+
__export(langchain_exports, {
|
|
23
|
+
AgentIDCallbackHandler: () => AgentIDCallbackHandler
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(langchain_exports);
|
|
26
|
+
function safeString(val) {
|
|
27
|
+
return typeof val === "string" ? val : "";
|
|
28
|
+
}
|
|
29
|
+
function extractPromptFromPrompts(prompts) {
|
|
30
|
+
if (Array.isArray(prompts) && prompts.length > 0) {
|
|
31
|
+
return safeString(prompts[prompts.length - 1]);
|
|
32
|
+
}
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
function extractPromptFromMessages(messages) {
|
|
36
|
+
const flat = [];
|
|
37
|
+
if (Array.isArray(messages)) {
|
|
38
|
+
for (const item of messages) {
|
|
39
|
+
if (Array.isArray(item)) {
|
|
40
|
+
flat.push(...item);
|
|
41
|
+
} else {
|
|
42
|
+
flat.push(item);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
let last = null;
|
|
47
|
+
for (const msg of flat) {
|
|
48
|
+
const typed = msg;
|
|
49
|
+
const role = typed?.role ?? typed?.type;
|
|
50
|
+
if (role === "user" || role === "human") {
|
|
51
|
+
last = typed;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (!last || typeof last !== "object") {
|
|
55
|
+
return "";
|
|
56
|
+
}
|
|
57
|
+
const typedLast = last;
|
|
58
|
+
return safeString(typedLast.content ?? typedLast.text);
|
|
59
|
+
}
|
|
60
|
+
function setPromptInPrompts(prompts, sanitizedInput) {
|
|
61
|
+
if (!Array.isArray(prompts) || prompts.length === 0) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
prompts[prompts.length - 1] = sanitizedInput;
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
function setPromptInMessages(messages, sanitizedInput) {
|
|
68
|
+
if (!Array.isArray(messages)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
const flat = [];
|
|
72
|
+
for (const item of messages) {
|
|
73
|
+
if (Array.isArray(item)) {
|
|
74
|
+
flat.push(...item);
|
|
75
|
+
} else {
|
|
76
|
+
flat.push(item);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (let i = flat.length - 1; i >= 0; i -= 1) {
|
|
80
|
+
const candidate = flat[i];
|
|
81
|
+
if (!candidate || typeof candidate !== "object") {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const typed = candidate;
|
|
85
|
+
const role = typed.role ?? typed.type;
|
|
86
|
+
if (role !== "user" && role !== "human") {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if ("content" in typed) {
|
|
90
|
+
typed.content = sanitizedInput;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
if ("text" in typed) {
|
|
94
|
+
typed.text = sanitizedInput;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
typed.content = sanitizedInput;
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
function extractModel(serialized, kwargs) {
|
|
103
|
+
const kw = (kwargs && typeof kwargs === "object" ? kwargs : null) ?? null;
|
|
104
|
+
const model = kw?.model ?? kw?.model_name ?? kw?.modelName;
|
|
105
|
+
if (typeof model === "string" && model) return model;
|
|
106
|
+
const ser = (serialized && typeof serialized === "object" ? serialized : null) ?? null;
|
|
107
|
+
const name = ser?.name ?? ser?.id;
|
|
108
|
+
if (typeof name === "string" && name) return name;
|
|
109
|
+
return void 0;
|
|
110
|
+
}
|
|
111
|
+
function extractOutputText(output) {
|
|
112
|
+
const gens = output?.generations;
|
|
113
|
+
const first = gens?.[0]?.[0];
|
|
114
|
+
const text = first?.text ?? first?.message?.content;
|
|
115
|
+
return typeof text === "string" ? text : "";
|
|
116
|
+
}
|
|
117
|
+
function extractTokenUsage(output) {
|
|
118
|
+
const llmOutput = output?.llmOutput ?? output?.llm_output;
|
|
119
|
+
const usage = llmOutput?.tokenUsage ?? llmOutput?.token_usage ?? llmOutput?.usage ?? void 0;
|
|
120
|
+
return usage && typeof usage === "object" ? usage : void 0;
|
|
121
|
+
}
|
|
122
|
+
function readBooleanField(value) {
|
|
123
|
+
return typeof value === "boolean" ? value : null;
|
|
124
|
+
}
|
|
125
|
+
function extractStreamFlag(serialized, extraParams) {
|
|
126
|
+
const extras = extraParams && typeof extraParams === "object" ? extraParams : null;
|
|
127
|
+
const direct = readBooleanField(extras?.stream) ?? readBooleanField(extras?.streaming);
|
|
128
|
+
if (direct !== null) {
|
|
129
|
+
return direct;
|
|
130
|
+
}
|
|
131
|
+
const invocation = extras?.invocation_params && typeof extras.invocation_params === "object" ? extras.invocation_params : null;
|
|
132
|
+
const invocationStream = readBooleanField(invocation?.stream) ?? readBooleanField(invocation?.streaming);
|
|
133
|
+
if (invocationStream !== null) {
|
|
134
|
+
return invocationStream;
|
|
135
|
+
}
|
|
136
|
+
const serializedRecord = serialized && typeof serialized === "object" ? serialized : null;
|
|
137
|
+
const kwargs = serializedRecord?.kwargs && typeof serializedRecord.kwargs === "object" ? serializedRecord.kwargs : null;
|
|
138
|
+
return readBooleanField(kwargs?.stream) ?? readBooleanField(kwargs?.streaming) ?? false;
|
|
139
|
+
}
|
|
140
|
+
var AgentIDCallbackHandler = class {
|
|
141
|
+
constructor(agent, options) {
|
|
142
|
+
this.runs = /* @__PURE__ */ new Map();
|
|
143
|
+
this.agent = agent;
|
|
144
|
+
this.systemId = options.system_id;
|
|
145
|
+
this.apiKeyOverride = options.apiKey?.trim() || options.api_key?.trim() || void 0;
|
|
146
|
+
}
|
|
147
|
+
get requestOptions() {
|
|
148
|
+
return this.apiKeyOverride ? { apiKey: this.apiKeyOverride } : void 0;
|
|
149
|
+
}
|
|
150
|
+
async preflight(input, stream) {
|
|
151
|
+
await this.agent.scanPromptInjection(input, this.requestOptions);
|
|
152
|
+
const prepared = await this.agent.prepareInputForDispatch({
|
|
153
|
+
input,
|
|
154
|
+
systemId: this.systemId,
|
|
155
|
+
stream,
|
|
156
|
+
skipInjectionScan: true
|
|
157
|
+
}, this.requestOptions);
|
|
158
|
+
return prepared.sanitizedInput;
|
|
159
|
+
}
|
|
160
|
+
async handleLLMStart(serialized, prompts, runId, _parentRunId, extraParams) {
|
|
161
|
+
const input = extractPromptFromPrompts(prompts);
|
|
162
|
+
const id = String(runId ?? "");
|
|
163
|
+
if (!input) {
|
|
164
|
+
throw new Error("AgentID: No prompt found. Security guard requires string input.");
|
|
165
|
+
}
|
|
166
|
+
const stream = extractStreamFlag(serialized, extraParams);
|
|
167
|
+
const sanitizedInput = await this.preflight(input, stream);
|
|
168
|
+
if (sanitizedInput !== input) {
|
|
169
|
+
const mutated = setPromptInPrompts(prompts, sanitizedInput);
|
|
170
|
+
if (!mutated) {
|
|
171
|
+
throw new Error(
|
|
172
|
+
"AgentID: Strict PII mode requires mutable LangChain prompt payload."
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const verdict = await this.agent.guard({
|
|
177
|
+
input: sanitizedInput,
|
|
178
|
+
system_id: this.systemId
|
|
179
|
+
}, this.requestOptions);
|
|
180
|
+
if (!verdict.allowed) {
|
|
181
|
+
throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
|
|
182
|
+
}
|
|
183
|
+
this.runs.set(id, {
|
|
184
|
+
input: sanitizedInput,
|
|
185
|
+
startedAtMs: Date.now(),
|
|
186
|
+
model: extractModel(serialized, extraParams)
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async handleChatModelStart(serialized, messages, runId, _parentRunId, extraParams) {
|
|
190
|
+
const input = extractPromptFromMessages(messages);
|
|
191
|
+
const id = String(runId ?? "");
|
|
192
|
+
if (!input) {
|
|
193
|
+
throw new Error("AgentID: No user message found. Security guard requires string input.");
|
|
194
|
+
}
|
|
195
|
+
const stream = extractStreamFlag(serialized, extraParams);
|
|
196
|
+
const sanitizedInput = await this.preflight(input, stream);
|
|
197
|
+
if (sanitizedInput !== input) {
|
|
198
|
+
const mutated = setPromptInMessages(messages, sanitizedInput);
|
|
199
|
+
if (!mutated) {
|
|
200
|
+
throw new Error(
|
|
201
|
+
"AgentID: Strict PII mode requires mutable LangChain message payload."
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const verdict = await this.agent.guard({
|
|
206
|
+
input: sanitizedInput,
|
|
207
|
+
system_id: this.systemId
|
|
208
|
+
}, this.requestOptions);
|
|
209
|
+
if (!verdict.allowed) {
|
|
210
|
+
throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
|
|
211
|
+
}
|
|
212
|
+
this.runs.set(id, {
|
|
213
|
+
input: sanitizedInput,
|
|
214
|
+
startedAtMs: Date.now(),
|
|
215
|
+
model: extractModel(serialized, extraParams)
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
async handleLLMEnd(output, runId) {
|
|
219
|
+
const id = String(runId ?? "");
|
|
220
|
+
const state = this.runs.get(id);
|
|
221
|
+
if (!state) return;
|
|
222
|
+
this.runs.delete(id);
|
|
223
|
+
const latency = Date.now() - state.startedAtMs;
|
|
224
|
+
const outText = extractOutputText(output);
|
|
225
|
+
const usage = extractTokenUsage(output);
|
|
226
|
+
this.agent.log({
|
|
227
|
+
system_id: this.systemId,
|
|
228
|
+
input: state.input,
|
|
229
|
+
output: outText,
|
|
230
|
+
model: state.model ?? "unknown",
|
|
231
|
+
usage,
|
|
232
|
+
latency
|
|
233
|
+
}, this.requestOptions);
|
|
234
|
+
}
|
|
235
|
+
async handleLLMError(err, runId) {
|
|
236
|
+
const id = String(runId ?? "");
|
|
237
|
+
const state = this.runs.get(id);
|
|
238
|
+
if (state) this.runs.delete(id);
|
|
239
|
+
const message = err && typeof err === "object" && "message" in err ? String(err.message) : String(err ?? "");
|
|
240
|
+
this.agent.log({
|
|
241
|
+
system_id: this.systemId,
|
|
242
|
+
input: state?.input ?? "",
|
|
243
|
+
output: "",
|
|
244
|
+
model: state?.model ?? "unknown",
|
|
245
|
+
event_type: "error",
|
|
246
|
+
severity: "error",
|
|
247
|
+
metadata: {
|
|
248
|
+
error_message: message
|
|
249
|
+
}
|
|
250
|
+
}, this.requestOptions);
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
254
|
+
0 && (module.exports = {
|
|
255
|
+
AgentIDCallbackHandler
|
|
256
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "agentid-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "AgentID JavaScript/TypeScript SDK for guard, ingest, tracing, and analytics.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://agentid.ai",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/ondrejsukac-rgb/agentid.git",
|
|
10
|
+
"directory": "js-sdk"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/ondrejsukac-rgb/agentid/issues"
|
|
14
|
+
},
|
|
15
|
+
"main": "dist/index.js",
|
|
16
|
+
"module": "dist/index.mjs",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./langchain": {
|
|
25
|
+
"types": "./dist/langchain.d.ts",
|
|
26
|
+
"import": "./dist/langchain.mjs",
|
|
27
|
+
"require": "./dist/langchain.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup src/index.ts src/langchain.ts --format esm,cjs --dts --clean",
|
|
36
|
+
"prepublishOnly": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"openai": "^4.0.0",
|
|
43
|
+
"langchain": "^0.1.0"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"langchain": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"tsup": "^8.3.5",
|
|
52
|
+
"typescript": "^5.0.0"
|
|
53
|
+
}
|
|
54
|
+
}
|