monora-ai 2.0.0 → 2.1.3
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/README.md +441 -150
- package/dist/aims_governance.d.ts +238 -0
- package/dist/aims_governance.d.ts.map +1 -0
- package/dist/aims_governance.js +922 -0
- package/dist/alerts.d.ts +16 -0
- package/dist/alerts.d.ts.map +1 -1
- package/dist/alerts.js +16 -0
- package/dist/api.d.ts +6 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +6 -0
- package/dist/assessment.d.ts +269 -0
- package/dist/assessment.d.ts.map +1 -0
- package/dist/assessment.js +1232 -0
- package/dist/attestation.js +23 -1
- package/dist/attribution.d.ts +349 -0
- package/dist/attribution.d.ts.map +1 -0
- package/dist/attribution.js +987 -0
- package/dist/autodetect.d.ts +69 -1
- package/dist/autodetect.d.ts.map +1 -1
- package/dist/autodetect.js +644 -1
- package/dist/bias.d.ts +130 -0
- package/dist/bias.d.ts.map +1 -0
- package/dist/bias.js +223 -0
- package/dist/circuit_breaker.js +3 -3
- package/dist/cli/diagnostics.d.ts +5 -1
- package/dist/cli/diagnostics.d.ts.map +1 -1
- package/dist/cli/diagnostics.js +31 -8
- package/dist/cli/doctor.d.ts +25 -0
- package/dist/cli/doctor.d.ts.map +1 -0
- package/dist/cli/doctor.js +381 -0
- package/dist/cli/fix.d.ts +16 -0
- package/dist/cli/fix.d.ts.map +1 -0
- package/dist/cli/fix.js +284 -0
- package/dist/cli/init.d.ts +57 -0
- package/dist/cli/init.d.ts.map +1 -0
- package/dist/cli/init.js +205 -0
- package/dist/cli.js +1611 -126
- package/dist/complianceTargets.d.ts +111 -0
- package/dist/complianceTargets.d.ts.map +1 -0
- package/dist/complianceTargets.js +521 -0
- package/dist/config.d.ts +301 -17
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +428 -36
- package/dist/config_migrations.d.ts +41 -0
- package/dist/config_migrations.d.ts.map +1 -1
- package/dist/config_migrations.js +205 -0
- package/dist/config_schema.d.ts +2900 -731
- package/dist/config_schema.d.ts.map +1 -1
- package/dist/config_schema.js +257 -55
- package/dist/context.d.ts +34 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +118 -7
- package/dist/control_backbone.d.ts +122 -0
- package/dist/control_backbone.d.ts.map +1 -0
- package/dist/control_backbone.js +698 -0
- package/dist/data-governance.d.ts +187 -0
- package/dist/data-governance.d.ts.map +1 -0
- package/dist/data-governance.js +424 -0
- package/dist/dataResidency.d.ts +44 -0
- package/dist/dataResidency.d.ts.map +1 -0
- package/dist/dataResidency.js +203 -0
- package/dist/dispatcher.d.ts +32 -0
- package/dist/dispatcher.d.ts.map +1 -1
- package/dist/dispatcher.js +91 -4
- package/dist/events.d.ts.map +1 -1
- package/dist/events.js +38 -0
- package/dist/evidence_store.d.ts +103 -0
- package/dist/evidence_store.d.ts.map +1 -0
- package/dist/evidence_store.js +459 -0
- package/dist/executiveSummary.d.ts +65 -8
- package/dist/executiveSummary.d.ts.map +1 -1
- package/dist/executiveSummary.js +289 -26
- package/dist/identity.d.ts +143 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +231 -0
- package/dist/impact-assessment.d.ts +350 -0
- package/dist/impact-assessment.d.ts.map +1 -0
- package/dist/impact-assessment.js +580 -0
- package/dist/index.d.ts +25 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +300 -4
- package/dist/instrumentation.d.ts +1 -1
- package/dist/instrumentation.d.ts.map +1 -1
- package/dist/instrumentation.js +243 -27
- package/dist/integrations/anthropic.d.ts +3 -0
- package/dist/integrations/anthropic.d.ts.map +1 -1
- package/dist/integrations/anthropic.js +284 -79
- package/dist/integrations/governance.d.ts +33 -0
- package/dist/integrations/governance.d.ts.map +1 -0
- package/dist/integrations/governance.js +208 -0
- package/dist/integrations/langchain.d.ts +7 -0
- package/dist/integrations/langchain.d.ts.map +1 -1
- package/dist/integrations/langchain.js +387 -143
- package/dist/integrations/openai.d.ts +9 -0
- package/dist/integrations/openai.d.ts.map +1 -1
- package/dist/integrations/openai.js +673 -73
- package/dist/iso42001_consolidation.d.ts +16 -0
- package/dist/iso42001_consolidation.d.ts.map +1 -0
- package/dist/iso42001_consolidation.js +413 -0
- package/dist/iso42001_workflows.d.ts +263 -0
- package/dist/iso42001_workflows.d.ts.map +1 -0
- package/dist/iso42001_workflows.js +781 -0
- package/dist/lifecycle.d.ts +299 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +624 -0
- package/dist/lineage.d.ts +2 -2
- package/dist/lineage.d.ts.map +1 -1
- package/dist/lineage.js +12 -17
- package/dist/middleware/express.d.ts.map +1 -1
- package/dist/middleware/express.js +33 -3
- package/dist/middleware/nextjs.d.ts.map +1 -1
- package/dist/middleware/nextjs.js +42 -68
- package/dist/model.d.ts +143 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +371 -0
- package/dist/onboarding.d.ts +42 -0
- package/dist/onboarding.d.ts.map +1 -0
- package/dist/onboarding.js +1022 -0
- package/dist/oversight.d.ts +264 -0
- package/dist/oversight.d.ts.map +1 -0
- package/dist/oversight.js +497 -0
- package/dist/pdf_report.d.ts.map +1 -1
- package/dist/pdf_report.js +42 -21
- package/dist/presets.d.ts +88 -0
- package/dist/presets.d.ts.map +1 -0
- package/dist/presets.js +520 -0
- package/dist/propagation.d.ts.map +1 -1
- package/dist/propagation.js +34 -2
- package/dist/quotas.d.ts +171 -0
- package/dist/quotas.d.ts.map +1 -0
- package/dist/quotas.js +259 -0
- package/dist/register.d.ts +13 -0
- package/dist/register.d.ts.map +1 -0
- package/dist/register.js +99 -0
- package/dist/registry.d.ts +1 -0
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +7 -0
- package/dist/registryData.json +43 -6
- package/dist/report.d.ts +2 -1
- package/dist/report.d.ts.map +1 -1
- package/dist/report.js +189 -2
- package/dist/reporting.d.ts +125 -0
- package/dist/reporting.d.ts.map +1 -1
- package/dist/reporting.js +196 -5
- package/dist/resources.d.ts +285 -0
- package/dist/resources.d.ts.map +1 -0
- package/dist/resources.js +643 -0
- package/dist/risk.d.ts +120 -0
- package/dist/risk.d.ts.map +1 -0
- package/dist/risk.js +220 -0
- package/dist/runtime.d.ts +74 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +598 -22
- package/dist/schemaInference.d.ts +92 -0
- package/dist/schemaInference.d.ts.map +1 -0
- package/dist/schemaInference.js +466 -0
- package/dist/schema_validation.js +2 -2
- package/dist/schemas/config.schema.json +169 -6
- package/dist/schemas/event.schema.json +4 -0
- package/dist/security_report.js +4 -4
- package/dist/signing.d.ts +1 -1
- package/dist/signing.d.ts.map +1 -1
- package/dist/signing.js +4 -0
- package/dist/sinks/file.d.ts +19 -1
- package/dist/sinks/file.d.ts.map +1 -1
- package/dist/sinks/file.js +82 -13
- package/dist/sinks/https.d.ts +10 -0
- package/dist/sinks/https.d.ts.map +1 -1
- package/dist/sinks/https.js +76 -16
- package/dist/sinks/stdout.d.ts +1 -0
- package/dist/sinks/stdout.d.ts.map +1 -1
- package/dist/sinks/stdout.js +12 -1
- package/dist/spec.d.ts +159 -0
- package/dist/spec.d.ts.map +1 -0
- package/dist/spec.js +391 -0
- package/dist/stakeholders.d.ts +199 -0
- package/dist/stakeholders.d.ts.map +1 -0
- package/dist/stakeholders.js +398 -0
- package/dist/standards.d.ts.map +1 -1
- package/dist/standards.js +160 -2
- package/dist/standards_ingest.d.ts +2 -2
- package/dist/standards_ingest.d.ts.map +1 -1
- package/dist/standards_ingest.js +105 -23
- package/dist/streaming.d.ts.map +1 -1
- package/dist/streaming.js +7 -2
- package/dist/telemetry.d.ts +16 -2
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +79 -14
- package/dist/templates/controls/iso42001_control_catalog.json +1443 -0
- package/dist/traced_emitter.d.ts +3 -0
- package/dist/traced_emitter.d.ts.map +1 -1
- package/dist/traced_emitter.js +142 -25
- package/dist/trust_package.d.ts +21 -1
- package/dist/trust_package.d.ts.map +1 -1
- package/dist/trust_package.js +101 -4
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +9 -2
- package/dist/wal.d.ts.map +1 -1
- package/dist/wal.js +2 -1
- package/package.json +14 -1
- package/scripts/postinstall.js +119 -97
- package/templates/controls/iso42001_control_catalog.json +1443 -0
package/dist/instrumentation.js
CHANGED
|
@@ -10,7 +10,6 @@ exports.InstrumentationError = void 0;
|
|
|
10
10
|
exports.autoInstrument = autoInstrument;
|
|
11
11
|
exports.uninstrument = uninstrument;
|
|
12
12
|
const logger_1 = require("./logger");
|
|
13
|
-
const runtime_1 = require("./runtime");
|
|
14
13
|
class InstrumentationError extends Error {
|
|
15
14
|
constructor(message) {
|
|
16
15
|
super(message);
|
|
@@ -18,6 +17,18 @@ class InstrumentationError extends Error {
|
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
19
|
exports.InstrumentationError = InstrumentationError;
|
|
20
|
+
const patchRegistry = new WeakMap();
|
|
21
|
+
function registerPatch(target, methodName, original) {
|
|
22
|
+
let targetPatches = patchRegistry.get(target);
|
|
23
|
+
if (!targetPatches) {
|
|
24
|
+
targetPatches = new Map();
|
|
25
|
+
patchRegistry.set(target, targetPatches);
|
|
26
|
+
}
|
|
27
|
+
if (targetPatches.has(methodName)) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
targetPatches.set(methodName, original);
|
|
31
|
+
}
|
|
21
32
|
/**
|
|
22
33
|
* Apply optional auto-instrumentation for supported SDKs.
|
|
23
34
|
*/
|
|
@@ -28,8 +39,8 @@ function autoInstrument(config, options) {
|
|
|
28
39
|
}
|
|
29
40
|
const targets = settings.targets || ['openai', 'anthropic'];
|
|
30
41
|
const purpose = settings.default_purpose || config.defaults?.purpose || 'general';
|
|
31
|
-
const dataClassification = settings.data_classification;
|
|
32
|
-
const reason = settings.reason;
|
|
42
|
+
const dataClassification = settings.data_classification ?? undefined;
|
|
43
|
+
const reason = settings.reason ?? undefined;
|
|
33
44
|
const effectiveFailFast = options?.failFast ?? settings.fail_fast ?? false;
|
|
34
45
|
const report = {
|
|
35
46
|
enabled: true,
|
|
@@ -45,6 +56,12 @@ function autoInstrument(config, options) {
|
|
|
45
56
|
else if (target === 'anthropic') {
|
|
46
57
|
[patched, errors] = instrumentAnthropic({ purpose, reason, dataClassification });
|
|
47
58
|
}
|
|
59
|
+
else if (target === 'langchain') {
|
|
60
|
+
const langchainConfig = settings && typeof settings.langchain === 'object'
|
|
61
|
+
? settings.langchain
|
|
62
|
+
: {};
|
|
63
|
+
[patched, errors] = instrumentLangChain({ purpose, reason, dataClassification }, langchainConfig);
|
|
64
|
+
}
|
|
48
65
|
else {
|
|
49
66
|
patched = [];
|
|
50
67
|
errors = [`Unknown instrumentation target '${target}'`];
|
|
@@ -84,6 +101,8 @@ function instrumentOpenAI(opts) {
|
|
|
84
101
|
{ path: 'chat.completions', method: 'create' },
|
|
85
102
|
{ path: 'completions', method: 'create' },
|
|
86
103
|
{ path: 'responses', method: 'create' },
|
|
104
|
+
{ path: 'responses', method: 'stream' },
|
|
105
|
+
{ path: 'embeddings', method: 'create' },
|
|
87
106
|
];
|
|
88
107
|
for (const target of patchTargets) {
|
|
89
108
|
try {
|
|
@@ -121,13 +140,28 @@ function patchOpenAIMethod(OpenAI, resourcePath, methodName, opts) {
|
|
|
121
140
|
// Try to find the resource class
|
|
122
141
|
// OpenAI v4 structure: OpenAI instance has _client with resource namespaces
|
|
123
142
|
try {
|
|
124
|
-
|
|
125
|
-
|
|
143
|
+
let current;
|
|
144
|
+
let inspectionSource = 'instance';
|
|
145
|
+
try {
|
|
146
|
+
// Create a temporary instance to inspect structure (without making API calls)
|
|
147
|
+
const instance = new OpenAI({ apiKey: 'sk-test', dangerouslyAllowBrowser: true });
|
|
148
|
+
current = instance;
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
logger_1.logger.warning('OpenAI inspection instance failed; falling back to prototype: %s', err);
|
|
152
|
+
current = OpenAI?.prototype;
|
|
153
|
+
inspectionSource = 'prototype';
|
|
154
|
+
}
|
|
155
|
+
if (!current) {
|
|
156
|
+
return { success: false, error: 'OpenAI inspection failed: no instance or prototype available' };
|
|
157
|
+
}
|
|
126
158
|
// Navigate to the resource
|
|
127
|
-
let current = instance;
|
|
128
159
|
for (const part of pathParts) {
|
|
129
160
|
if (!current[part]) {
|
|
130
|
-
return {
|
|
161
|
+
return {
|
|
162
|
+
success: false,
|
|
163
|
+
error: `Resource ${resourcePath} not found on OpenAI ${inspectionSource}`,
|
|
164
|
+
};
|
|
131
165
|
}
|
|
132
166
|
current = current[part];
|
|
133
167
|
}
|
|
@@ -140,17 +174,19 @@ function patchOpenAIMethod(OpenAI, resourcePath, methodName, opts) {
|
|
|
140
174
|
if (resourceClass[methodName].__monora_wrapped__) {
|
|
141
175
|
return { success: true };
|
|
142
176
|
}
|
|
177
|
+
const wrapperFactory = getOpenAIWrapperFactory(resourcePath, methodName, opts);
|
|
178
|
+
if (!wrapperFactory) {
|
|
179
|
+
return { success: false, error: `No wrapper available for ${resourcePath}.${methodName}` };
|
|
180
|
+
}
|
|
143
181
|
// Store original method
|
|
144
182
|
const originalMethod = resourceClass[methodName];
|
|
145
|
-
|
|
183
|
+
registerPatch(resourceClass, methodName, originalMethod);
|
|
184
|
+
// Create wrapped method once at patch time
|
|
185
|
+
const wrappedFn = wrapperFactory(function (...args) {
|
|
186
|
+
return originalMethod.apply(this, args);
|
|
187
|
+
});
|
|
146
188
|
resourceClass[methodName] = function (...args) {
|
|
147
|
-
|
|
148
|
-
const wrappedFn = (0, runtime_1.llmCall)({
|
|
149
|
-
purpose: opts.purpose,
|
|
150
|
-
reason: opts.reason,
|
|
151
|
-
dataClassification: opts.dataClassification,
|
|
152
|
-
})(originalMethod.bind(this));
|
|
153
|
-
return wrappedFn(...args);
|
|
189
|
+
return wrappedFn.apply(this, args);
|
|
154
190
|
};
|
|
155
191
|
// Mark as wrapped
|
|
156
192
|
resourceClass[methodName].__monora_wrapped__ = true;
|
|
@@ -160,6 +196,38 @@ function patchOpenAIMethod(OpenAI, resourcePath, methodName, opts) {
|
|
|
160
196
|
return { success: false, error: `Patch error for ${resourcePath}.${methodName}: ${err}` };
|
|
161
197
|
}
|
|
162
198
|
}
|
|
199
|
+
function getOpenAIWrapperFactory(resourcePath, methodName, opts) {
|
|
200
|
+
let wrappers;
|
|
201
|
+
try {
|
|
202
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
203
|
+
wrappers = require('./integrations/openai');
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
const buildFactory = (wrapper) => {
|
|
209
|
+
if (typeof wrapper !== 'function') {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
return (originalFn) => wrapper(originalFn, opts.dataClassification || 'internal', opts.purpose, opts.reason);
|
|
213
|
+
};
|
|
214
|
+
if (resourcePath === 'chat.completions' && methodName === 'create') {
|
|
215
|
+
return buildFactory(wrappers.wrapChatCompletions);
|
|
216
|
+
}
|
|
217
|
+
if (resourcePath === 'completions' && methodName === 'create') {
|
|
218
|
+
return buildFactory(wrappers.wrapCompletions);
|
|
219
|
+
}
|
|
220
|
+
if (resourcePath === 'embeddings' && methodName === 'create') {
|
|
221
|
+
return buildFactory(wrappers.wrapEmbeddings);
|
|
222
|
+
}
|
|
223
|
+
if (resourcePath === 'responses' && methodName === 'create') {
|
|
224
|
+
return buildFactory(wrappers.wrapResponses);
|
|
225
|
+
}
|
|
226
|
+
if (resourcePath === 'responses' && methodName === 'stream') {
|
|
227
|
+
return buildFactory(wrappers.wrapResponsesStreamMethod);
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
163
231
|
/**
|
|
164
232
|
* Instrument Anthropic SDK methods.
|
|
165
233
|
*/
|
|
@@ -186,6 +254,7 @@ function instrumentAnthropic(opts) {
|
|
|
186
254
|
const Anthropic = anthropicModule.default || anthropicModule.Anthropic || anthropicModule;
|
|
187
255
|
const patchTargets = [
|
|
188
256
|
{ path: 'messages', method: 'create' },
|
|
257
|
+
{ path: 'messages', method: 'stream' },
|
|
189
258
|
{ path: 'completions', method: 'create' },
|
|
190
259
|
];
|
|
191
260
|
for (const target of patchTargets) {
|
|
@@ -208,6 +277,112 @@ function instrumentAnthropic(opts) {
|
|
|
208
277
|
}
|
|
209
278
|
return [patched, errors];
|
|
210
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Instrument LangChain callback manager to attach Monora handler.
|
|
282
|
+
*/
|
|
283
|
+
function instrumentLangChain(opts, langchainConfig) {
|
|
284
|
+
if (!moduleAvailable('langchain')) {
|
|
285
|
+
return [[], ['LangChain SDK not installed; skipping auto-instrumentation']];
|
|
286
|
+
}
|
|
287
|
+
const patched = [];
|
|
288
|
+
const errors = [];
|
|
289
|
+
let managerModule;
|
|
290
|
+
try {
|
|
291
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
292
|
+
managerModule = require('langchain/callbacks/manager');
|
|
293
|
+
}
|
|
294
|
+
catch (err) {
|
|
295
|
+
errors.push(`Failed to load LangChain callback manager: ${err}`);
|
|
296
|
+
return [patched, errors];
|
|
297
|
+
}
|
|
298
|
+
let MonoraCallbackHandler;
|
|
299
|
+
try {
|
|
300
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
301
|
+
({ MonoraCallbackHandler } = require('./integrations/langchain'));
|
|
302
|
+
}
|
|
303
|
+
catch (err) {
|
|
304
|
+
errors.push(`Failed to load Monora LangChain handler: ${err}`);
|
|
305
|
+
return [patched, errors];
|
|
306
|
+
}
|
|
307
|
+
const capturePrompts = langchainConfig.capture_prompts ??
|
|
308
|
+
langchainConfig.capturePrompts ??
|
|
309
|
+
true;
|
|
310
|
+
const captureCompletions = langchainConfig.capture_completions ??
|
|
311
|
+
langchainConfig.captureCompletions ??
|
|
312
|
+
true;
|
|
313
|
+
const handler = new MonoraCallbackHandler({
|
|
314
|
+
dataClassification: opts.dataClassification,
|
|
315
|
+
purpose: opts.purpose,
|
|
316
|
+
capturePrompts,
|
|
317
|
+
captureCompletions,
|
|
318
|
+
});
|
|
319
|
+
const CallbackManager = managerModule.CallbackManager ||
|
|
320
|
+
managerModule.default?.CallbackManager ||
|
|
321
|
+
managerModule.default;
|
|
322
|
+
if (!CallbackManager) {
|
|
323
|
+
errors.push('LangChain CallbackManager not found');
|
|
324
|
+
return [patched, errors];
|
|
325
|
+
}
|
|
326
|
+
const factoryNames = ['configure', 'fromHandlers', 'fromMethods', 'fromCallbacks'];
|
|
327
|
+
for (const factoryName of factoryNames) {
|
|
328
|
+
const factory = CallbackManager[factoryName];
|
|
329
|
+
if (typeof factory !== 'function') {
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
if (factory.__monora_wrapped__) {
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
registerPatch(CallbackManager, factoryName, factory);
|
|
336
|
+
const wrapped = function (...args) {
|
|
337
|
+
const manager = factory.apply(this, args);
|
|
338
|
+
attachLangChainHandler(manager, handler);
|
|
339
|
+
return manager;
|
|
340
|
+
};
|
|
341
|
+
wrapped.__monora_wrapped__ = true;
|
|
342
|
+
CallbackManager[factoryName] = wrapped;
|
|
343
|
+
patched.push(`langchain.callbacks.manager.CallbackManager.${factoryName}`);
|
|
344
|
+
}
|
|
345
|
+
if (patched.length === 0) {
|
|
346
|
+
errors.push('LangChain CallbackManager factories not found or already instrumented');
|
|
347
|
+
}
|
|
348
|
+
return [patched, errors];
|
|
349
|
+
}
|
|
350
|
+
function attachLangChainHandler(manager, handler) {
|
|
351
|
+
if (!manager || hasLangChainHandler(manager)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
if (typeof manager.addHandler === 'function') {
|
|
355
|
+
try {
|
|
356
|
+
manager.addHandler(handler);
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
catch {
|
|
360
|
+
// fall through to array-based attach
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (Array.isArray(manager.handlers)) {
|
|
364
|
+
manager.handlers.push(handler);
|
|
365
|
+
}
|
|
366
|
+
if (Array.isArray(manager.inheritableHandlers)) {
|
|
367
|
+
manager.inheritableHandlers.push(handler);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function hasLangChainHandler(manager) {
|
|
371
|
+
const candidates = [];
|
|
372
|
+
if (Array.isArray(manager?.handlers)) {
|
|
373
|
+
candidates.push(...manager.handlers);
|
|
374
|
+
}
|
|
375
|
+
if (Array.isArray(manager?.inheritableHandlers)) {
|
|
376
|
+
candidates.push(...manager.inheritableHandlers);
|
|
377
|
+
}
|
|
378
|
+
return candidates.some((candidate) => {
|
|
379
|
+
if (!candidate) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
const name = candidate.name || candidate.constructor?.name;
|
|
383
|
+
return name === 'MonoraCallbackHandler';
|
|
384
|
+
});
|
|
385
|
+
}
|
|
211
386
|
/**
|
|
212
387
|
* Patch a specific Anthropic method.
|
|
213
388
|
*/
|
|
@@ -233,16 +408,19 @@ function patchAnthropicMethod(Anthropic, resourcePath, methodName, opts) {
|
|
|
233
408
|
if (resourceClass[methodName].__monora_wrapped__) {
|
|
234
409
|
return { success: true };
|
|
235
410
|
}
|
|
411
|
+
const wrapperFactory = getAnthropicWrapperFactory(resourcePath, methodName, opts);
|
|
412
|
+
if (!wrapperFactory) {
|
|
413
|
+
return { success: false, error: `No wrapper available for ${resourcePath}.${methodName}` };
|
|
414
|
+
}
|
|
236
415
|
// Store original method
|
|
237
416
|
const originalMethod = resourceClass[methodName];
|
|
238
|
-
|
|
417
|
+
registerPatch(resourceClass, methodName, originalMethod);
|
|
418
|
+
// Create wrapped method once at patch time
|
|
419
|
+
const wrappedFn = wrapperFactory(function (...args) {
|
|
420
|
+
return originalMethod.apply(this, args);
|
|
421
|
+
});
|
|
239
422
|
resourceClass[methodName] = function (...args) {
|
|
240
|
-
|
|
241
|
-
purpose: opts.purpose,
|
|
242
|
-
reason: opts.reason,
|
|
243
|
-
dataClassification: opts.dataClassification,
|
|
244
|
-
})(originalMethod.bind(this));
|
|
245
|
-
return wrappedFn(...args);
|
|
423
|
+
return wrappedFn.apply(this, args);
|
|
246
424
|
};
|
|
247
425
|
// Mark as wrapped
|
|
248
426
|
resourceClass[methodName].__monora_wrapped__ = true;
|
|
@@ -252,6 +430,32 @@ function patchAnthropicMethod(Anthropic, resourcePath, methodName, opts) {
|
|
|
252
430
|
return { success: false, error: `Patch error for ${resourcePath}.${methodName}: ${err}` };
|
|
253
431
|
}
|
|
254
432
|
}
|
|
433
|
+
function getAnthropicWrapperFactory(resourcePath, methodName, opts) {
|
|
434
|
+
let wrappers;
|
|
435
|
+
try {
|
|
436
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
437
|
+
wrappers = require('./integrations/anthropic');
|
|
438
|
+
}
|
|
439
|
+
catch {
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
const buildFactory = (wrapper) => {
|
|
443
|
+
if (typeof wrapper !== 'function') {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
return (originalFn) => wrapper(originalFn, opts.dataClassification || 'internal', opts.purpose, opts.reason);
|
|
447
|
+
};
|
|
448
|
+
if (resourcePath === 'messages' && methodName === 'create') {
|
|
449
|
+
return buildFactory(wrappers.wrapMessagesCreate);
|
|
450
|
+
}
|
|
451
|
+
if (resourcePath === 'messages' && methodName === 'stream') {
|
|
452
|
+
return buildFactory(wrappers.wrapMessagesStream);
|
|
453
|
+
}
|
|
454
|
+
if (resourcePath === 'completions' && methodName === 'create') {
|
|
455
|
+
return buildFactory(wrappers.wrapCompletions);
|
|
456
|
+
}
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
255
459
|
/**
|
|
256
460
|
* Check if a module is available.
|
|
257
461
|
*/
|
|
@@ -267,9 +471,21 @@ function moduleAvailable(name) {
|
|
|
267
471
|
/**
|
|
268
472
|
* Uninstrument all patched methods (useful for testing).
|
|
269
473
|
*/
|
|
270
|
-
function uninstrument() {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
474
|
+
function uninstrument(target) {
|
|
475
|
+
if (!target) {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
const targetPatches = patchRegistry.get(target);
|
|
479
|
+
if (!targetPatches) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
for (const [methodName, original] of targetPatches.entries()) {
|
|
483
|
+
try {
|
|
484
|
+
target[methodName] = original;
|
|
485
|
+
}
|
|
486
|
+
catch (error) {
|
|
487
|
+
logger_1.logger.error('Failed to restore %s: %s', methodName, error);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
patchRegistry.delete(target);
|
|
275
491
|
}
|
|
@@ -37,5 +37,8 @@ interface AnthropicClient {
|
|
|
37
37
|
* Patch an Anthropic client to automatically trace all API calls.
|
|
38
38
|
*/
|
|
39
39
|
export declare function patchAnthropic(client: AnthropicClient, options?: PatchAnthropicOptions): void;
|
|
40
|
+
export declare function wrapMessagesCreate(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
41
|
+
export declare function wrapMessagesStream(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
42
|
+
export declare function wrapCompletions(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
40
43
|
export {};
|
|
41
44
|
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/integrations/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/integrations/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAUH,MAAM,WAAW,qBAAqB;IACpC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,UAAU,eAAe;IACvB,QAAQ,CAAC,EAAE;QACT,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;QAChC,MAAM,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;KAClC,CAAC;IACF,WAAW,CAAC,EAAE;QACZ,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;KACjC,CAAC;CACH;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,eAAe,EACvB,OAAO,GAAE,qBAA0B,GAClC,IAAI,CAuCN;AAUD,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EACnC,kBAAkB,EAAE,MAAM,EAC1B,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,GACd,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CA0HzB;AAED,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EACnC,kBAAkB,EAAE,MAAM,EAC1B,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,GACd,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAuDzB;AA+QD,wBAAgB,eAAe,CAC7B,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EACnC,kBAAkB,EAAE,MAAM,EAC1B,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,MAAM,GACd,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAgHzB"}
|