agentid-sdk 0.1.16 → 0.1.18
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/chunk-6YR4ECGB.mjs +424 -0
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +207 -57
- package/dist/index.mjs +25 -33
- package/dist/{langchain-CmUK7sna.d.mts → langchain-BykeB2WB.d.mts} +8 -6
- package/dist/{langchain-CmUK7sna.d.ts → langchain-BykeB2WB.d.ts} +8 -6
- package/dist/langchain.d.mts +2 -1
- package/dist/langchain.d.ts +2 -1
- package/dist/langchain.js +183 -25
- package/dist/langchain.mjs +1 -1
- package/package.json +6 -2
- package/dist/chunk-DXUA5DKG.mjs +0 -266
package/dist/chunk-DXUA5DKG.mjs
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
// src/langchain.ts
|
|
2
|
-
function safeString(val) {
|
|
3
|
-
return typeof val === "string" ? val : "";
|
|
4
|
-
}
|
|
5
|
-
function extractPromptFromPrompts(prompts) {
|
|
6
|
-
if (Array.isArray(prompts) && prompts.length > 0) {
|
|
7
|
-
return safeString(prompts[prompts.length - 1]);
|
|
8
|
-
}
|
|
9
|
-
return "";
|
|
10
|
-
}
|
|
11
|
-
function extractPromptFromMessages(messages) {
|
|
12
|
-
const flat = [];
|
|
13
|
-
if (Array.isArray(messages)) {
|
|
14
|
-
for (const item of messages) {
|
|
15
|
-
if (Array.isArray(item)) {
|
|
16
|
-
flat.push(...item);
|
|
17
|
-
} else {
|
|
18
|
-
flat.push(item);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
let last = null;
|
|
23
|
-
for (const msg of flat) {
|
|
24
|
-
const typed = msg;
|
|
25
|
-
const role = typed?.role ?? typed?.type;
|
|
26
|
-
if (role === "user" || role === "human") {
|
|
27
|
-
last = typed;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (!last || typeof last !== "object") {
|
|
31
|
-
return "";
|
|
32
|
-
}
|
|
33
|
-
const typedLast = last;
|
|
34
|
-
return safeString(typedLast.content ?? typedLast.text);
|
|
35
|
-
}
|
|
36
|
-
function setPromptInPrompts(prompts, sanitizedInput) {
|
|
37
|
-
if (!Array.isArray(prompts) || prompts.length === 0) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
prompts[prompts.length - 1] = sanitizedInput;
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
function setPromptInMessages(messages, sanitizedInput) {
|
|
44
|
-
if (!Array.isArray(messages)) {
|
|
45
|
-
return false;
|
|
46
|
-
}
|
|
47
|
-
const flat = [];
|
|
48
|
-
for (const item of messages) {
|
|
49
|
-
if (Array.isArray(item)) {
|
|
50
|
-
flat.push(...item);
|
|
51
|
-
} else {
|
|
52
|
-
flat.push(item);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
for (let i = flat.length - 1; i >= 0; i -= 1) {
|
|
56
|
-
const candidate = flat[i];
|
|
57
|
-
if (!candidate || typeof candidate !== "object") {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
const typed = candidate;
|
|
61
|
-
const role = typed.role ?? typed.type;
|
|
62
|
-
if (role !== "user" && role !== "human") {
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if ("content" in typed) {
|
|
66
|
-
typed.content = sanitizedInput;
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
if ("text" in typed) {
|
|
70
|
-
typed.text = sanitizedInput;
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
typed.content = sanitizedInput;
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
function extractModel(serialized, kwargs) {
|
|
79
|
-
const kw = (kwargs && typeof kwargs === "object" ? kwargs : null) ?? null;
|
|
80
|
-
const model = kw?.model ?? kw?.model_name ?? kw?.modelName;
|
|
81
|
-
if (typeof model === "string" && model) return model;
|
|
82
|
-
const ser = (serialized && typeof serialized === "object" ? serialized : null) ?? null;
|
|
83
|
-
const name = ser?.name ?? ser?.id;
|
|
84
|
-
if (typeof name === "string" && name) return name;
|
|
85
|
-
return void 0;
|
|
86
|
-
}
|
|
87
|
-
function extractOutputText(output) {
|
|
88
|
-
const gens = output?.generations;
|
|
89
|
-
const first = gens?.[0]?.[0];
|
|
90
|
-
const text = first?.text ?? first?.message?.content;
|
|
91
|
-
return typeof text === "string" ? text : "";
|
|
92
|
-
}
|
|
93
|
-
function extractTokenUsage(output) {
|
|
94
|
-
const llmOutput = output?.llmOutput ?? output?.llm_output;
|
|
95
|
-
const usage = llmOutput?.tokenUsage ?? llmOutput?.token_usage ?? llmOutput?.usage ?? void 0;
|
|
96
|
-
return usage && typeof usage === "object" ? usage : void 0;
|
|
97
|
-
}
|
|
98
|
-
function readBooleanField(value) {
|
|
99
|
-
return typeof value === "boolean" ? value : null;
|
|
100
|
-
}
|
|
101
|
-
function extractStreamFlag(serialized, extraParams) {
|
|
102
|
-
const extras = extraParams && typeof extraParams === "object" ? extraParams : null;
|
|
103
|
-
const direct = readBooleanField(extras?.stream) ?? readBooleanField(extras?.streaming);
|
|
104
|
-
if (direct !== null) {
|
|
105
|
-
return direct;
|
|
106
|
-
}
|
|
107
|
-
const invocation = extras?.invocation_params && typeof extras.invocation_params === "object" ? extras.invocation_params : null;
|
|
108
|
-
const invocationStream = readBooleanField(invocation?.stream) ?? readBooleanField(invocation?.streaming);
|
|
109
|
-
if (invocationStream !== null) {
|
|
110
|
-
return invocationStream;
|
|
111
|
-
}
|
|
112
|
-
const serializedRecord = serialized && typeof serialized === "object" ? serialized : null;
|
|
113
|
-
const kwargs = serializedRecord?.kwargs && typeof serializedRecord.kwargs === "object" ? serializedRecord.kwargs : null;
|
|
114
|
-
return readBooleanField(kwargs?.stream) ?? readBooleanField(kwargs?.streaming) ?? false;
|
|
115
|
-
}
|
|
116
|
-
var AgentIDCallbackHandler = class {
|
|
117
|
-
constructor(agent, options) {
|
|
118
|
-
this.runs = /* @__PURE__ */ new Map();
|
|
119
|
-
this.agent = agent;
|
|
120
|
-
this.systemId = options.system_id;
|
|
121
|
-
this.apiKeyOverride = options.apiKey?.trim() || options.api_key?.trim() || void 0;
|
|
122
|
-
}
|
|
123
|
-
get requestOptions() {
|
|
124
|
-
return this.apiKeyOverride ? { apiKey: this.apiKeyOverride } : void 0;
|
|
125
|
-
}
|
|
126
|
-
getLangchainCapabilities() {
|
|
127
|
-
const piiMaskingEnabled = Boolean(
|
|
128
|
-
this.agent.piiMasking
|
|
129
|
-
);
|
|
130
|
-
return {
|
|
131
|
-
capabilities: {
|
|
132
|
-
has_feedback_handler: true,
|
|
133
|
-
pii_masking_enabled: piiMaskingEnabled,
|
|
134
|
-
framework: "langchain"
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
async preflight(input, stream) {
|
|
139
|
-
await this.agent.scanPromptInjection(input, this.requestOptions);
|
|
140
|
-
const prepared = await this.agent.prepareInputForDispatch({
|
|
141
|
-
input,
|
|
142
|
-
systemId: this.systemId,
|
|
143
|
-
stream,
|
|
144
|
-
skipInjectionScan: true
|
|
145
|
-
}, this.requestOptions);
|
|
146
|
-
return prepared.sanitizedInput;
|
|
147
|
-
}
|
|
148
|
-
async handleLLMStart(serialized, prompts, runId, _parentRunId, extraParams) {
|
|
149
|
-
const input = extractPromptFromPrompts(prompts);
|
|
150
|
-
const id = String(runId ?? "");
|
|
151
|
-
if (!input) {
|
|
152
|
-
throw new Error("AgentID: No prompt found. Security guard requires string input.");
|
|
153
|
-
}
|
|
154
|
-
const stream = extractStreamFlag(serialized, extraParams);
|
|
155
|
-
const sanitizedInput = await this.preflight(input, stream);
|
|
156
|
-
if (sanitizedInput !== input) {
|
|
157
|
-
const mutated = setPromptInPrompts(prompts, sanitizedInput);
|
|
158
|
-
if (!mutated) {
|
|
159
|
-
throw new Error(
|
|
160
|
-
"AgentID: Strict PII mode requires mutable LangChain prompt payload."
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
const verdict = await this.agent.guard({
|
|
165
|
-
input: sanitizedInput,
|
|
166
|
-
system_id: this.systemId,
|
|
167
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
168
|
-
}, this.requestOptions);
|
|
169
|
-
if (!verdict.allowed) {
|
|
170
|
-
throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
|
|
171
|
-
}
|
|
172
|
-
const transformedInput = typeof verdict.transformed_input === "string" && verdict.transformed_input.length > 0 ? verdict.transformed_input : sanitizedInput;
|
|
173
|
-
if (transformedInput !== sanitizedInput) {
|
|
174
|
-
const mutated = setPromptInPrompts(prompts, transformedInput);
|
|
175
|
-
if (!mutated) {
|
|
176
|
-
throw new Error(
|
|
177
|
-
"AgentID: Guard transformed input could not be applied to LangChain prompt payload."
|
|
178
|
-
);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
this.runs.set(id, {
|
|
182
|
-
input: transformedInput,
|
|
183
|
-
startedAtMs: Date.now(),
|
|
184
|
-
model: extractModel(serialized, extraParams)
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
async handleChatModelStart(serialized, messages, runId, _parentRunId, extraParams) {
|
|
188
|
-
const input = extractPromptFromMessages(messages);
|
|
189
|
-
const id = String(runId ?? "");
|
|
190
|
-
if (!input) {
|
|
191
|
-
throw new Error("AgentID: No user message found. Security guard requires string input.");
|
|
192
|
-
}
|
|
193
|
-
const stream = extractStreamFlag(serialized, extraParams);
|
|
194
|
-
const sanitizedInput = await this.preflight(input, stream);
|
|
195
|
-
if (sanitizedInput !== input) {
|
|
196
|
-
const mutated = setPromptInMessages(messages, sanitizedInput);
|
|
197
|
-
if (!mutated) {
|
|
198
|
-
throw new Error(
|
|
199
|
-
"AgentID: Strict PII mode requires mutable LangChain message payload."
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
const verdict = await this.agent.guard({
|
|
204
|
-
input: sanitizedInput,
|
|
205
|
-
system_id: this.systemId,
|
|
206
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
207
|
-
}, this.requestOptions);
|
|
208
|
-
if (!verdict.allowed) {
|
|
209
|
-
throw new Error(`AgentID: Security Blocked (${verdict.reason ?? "guard_denied"})`);
|
|
210
|
-
}
|
|
211
|
-
const transformedInput = typeof verdict.transformed_input === "string" && verdict.transformed_input.length > 0 ? verdict.transformed_input : sanitizedInput;
|
|
212
|
-
if (transformedInput !== sanitizedInput) {
|
|
213
|
-
const mutated = setPromptInMessages(messages, transformedInput);
|
|
214
|
-
if (!mutated) {
|
|
215
|
-
throw new Error(
|
|
216
|
-
"AgentID: Guard transformed input could not be applied to LangChain message payload."
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
this.runs.set(id, {
|
|
221
|
-
input: transformedInput,
|
|
222
|
-
startedAtMs: Date.now(),
|
|
223
|
-
model: extractModel(serialized, extraParams)
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
async handleLLMEnd(output, runId) {
|
|
227
|
-
const id = String(runId ?? "");
|
|
228
|
-
const state = this.runs.get(id);
|
|
229
|
-
if (!state) return;
|
|
230
|
-
this.runs.delete(id);
|
|
231
|
-
const latency = Date.now() - state.startedAtMs;
|
|
232
|
-
const outText = extractOutputText(output);
|
|
233
|
-
const usage = extractTokenUsage(output);
|
|
234
|
-
this.agent.log({
|
|
235
|
-
system_id: this.systemId,
|
|
236
|
-
input: state.input,
|
|
237
|
-
output: outText,
|
|
238
|
-
model: state.model ?? "unknown",
|
|
239
|
-
usage,
|
|
240
|
-
latency,
|
|
241
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
242
|
-
}, this.requestOptions);
|
|
243
|
-
}
|
|
244
|
-
async handleLLMError(err, runId) {
|
|
245
|
-
const id = String(runId ?? "");
|
|
246
|
-
const state = this.runs.get(id);
|
|
247
|
-
if (state) this.runs.delete(id);
|
|
248
|
-
const message = err && typeof err === "object" && "message" in err ? String(err.message) : String(err ?? "");
|
|
249
|
-
this.agent.log({
|
|
250
|
-
system_id: this.systemId,
|
|
251
|
-
input: state?.input ?? "",
|
|
252
|
-
output: "",
|
|
253
|
-
model: state?.model ?? "unknown",
|
|
254
|
-
event_type: "error",
|
|
255
|
-
severity: "error",
|
|
256
|
-
metadata: {
|
|
257
|
-
error_message: message
|
|
258
|
-
},
|
|
259
|
-
client_capabilities: this.getLangchainCapabilities()
|
|
260
|
-
}, this.requestOptions);
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
|
|
264
|
-
export {
|
|
265
|
-
AgentIDCallbackHandler
|
|
266
|
-
};
|