monora-ai 2.1.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 +333 -159
- 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 +85 -0
- package/dist/assessment.d.ts.map +1 -1
- package/dist/assessment.js +506 -13
- package/dist/attribution.d.ts +44 -3
- package/dist/attribution.d.ts.map +1 -1
- package/dist/attribution.js +197 -10
- package/dist/autodetect.d.ts +68 -0
- package/dist/autodetect.d.ts.map +1 -1
- package/dist/autodetect.js +639 -0
- package/dist/bias.d.ts +130 -0
- package/dist/bias.d.ts.map +1 -0
- package/dist/bias.js +223 -0
- package/dist/cli/diagnostics.d.ts +5 -1
- package/dist/cli/diagnostics.d.ts.map +1 -1
- package/dist/cli/diagnostics.js +23 -6
- 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 +1550 -176
- 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 +261 -16
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +381 -32
- package/dist/config_migrations.d.ts.map +1 -1
- package/dist/config_migrations.js +38 -1
- package/dist/config_schema.d.ts +2490 -1035
- package/dist/config_schema.d.ts.map +1 -1
- package/dist/config_schema.js +233 -64
- 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.map +1 -1
- package/dist/dispatcher.js +17 -5
- 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 +15 -0
- package/dist/executiveSummary.d.ts.map +1 -1
- package/dist/executiveSummary.js +135 -22
- 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 +20 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +247 -5
- package/dist/instrumentation.d.ts +1 -1
- package/dist/instrumentation.d.ts.map +1 -1
- package/dist/instrumentation.js +123 -22
- package/dist/integrations/anthropic.d.ts +3 -0
- package/dist/integrations/anthropic.d.ts.map +1 -1
- package/dist/integrations/anthropic.js +282 -80
- 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 +4 -0
- package/dist/integrations/langchain.d.ts.map +1 -1
- package/dist/integrations/langchain.js +362 -142
- 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 +9 -16
- package/dist/middleware/express.d.ts.map +1 -1
- package/dist/middleware/express.js +18 -3
- package/dist/middleware/nextjs.js +2 -2
- 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/presets.js +7 -7
- 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 +192 -2
- 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 +73 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +415 -18
- 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 +118 -4
- 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.map +1 -1
- package/dist/standards_ingest.js +1 -4
- package/dist/telemetry.d.ts +16 -2
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +77 -14
- package/dist/templates/controls/iso42001_control_catalog.json +1443 -0
- package/dist/traced_emitter.d.ts.map +1 -1
- package/dist/traced_emitter.js +19 -9
- package/dist/trust_package.d.ts +19 -1
- package/dist/trust_package.d.ts.map +1 -1
- package/dist/trust_package.js +89 -2
- 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 +105 -210
- package/templates/controls/iso42001_control_catalog.json +1443 -0
|
@@ -22,7 +22,10 @@
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
exports.MonoraCallbackHandler = void 0;
|
|
24
24
|
const runtime_1 = require("../runtime");
|
|
25
|
+
const context_1 = require("../context");
|
|
25
26
|
const lineage_1 = require("../lineage");
|
|
27
|
+
const governance_1 = require("./governance");
|
|
28
|
+
const ids_1 = require("../ids");
|
|
26
29
|
/**
|
|
27
30
|
* LangChain callback handler that traces operations to Monora.
|
|
28
31
|
*
|
|
@@ -32,28 +35,92 @@ class MonoraCallbackHandler {
|
|
|
32
35
|
constructor(options = {}) {
|
|
33
36
|
this.name = 'MonoraCallbackHandler';
|
|
34
37
|
this.runMap = new Map();
|
|
38
|
+
this.runContexts = new Map();
|
|
39
|
+
this.llmRuns = new Map();
|
|
35
40
|
this.dataClassification = options.dataClassification ?? 'internal';
|
|
36
41
|
this.purpose = options.purpose ?? 'general';
|
|
37
42
|
this.capturePrompts = options.capturePrompts ?? true;
|
|
38
43
|
this.captureCompletions = options.captureCompletions ?? true;
|
|
39
44
|
}
|
|
45
|
+
ensureRunContext(runId, parentRunId, spanName) {
|
|
46
|
+
const existing = this.runContexts.get(runId);
|
|
47
|
+
if (existing) {
|
|
48
|
+
return existing;
|
|
49
|
+
}
|
|
50
|
+
let baseContext;
|
|
51
|
+
if (parentRunId) {
|
|
52
|
+
const parent = this.runContexts.get(parentRunId);
|
|
53
|
+
if (parent) {
|
|
54
|
+
baseContext = parent.context;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!baseContext) {
|
|
58
|
+
baseContext = (0, context_1.captureContext)();
|
|
59
|
+
}
|
|
60
|
+
if (!baseContext) {
|
|
61
|
+
const startTime = Date.now();
|
|
62
|
+
const span = {
|
|
63
|
+
traceId: (0, ids_1.generateUlid)('trc'),
|
|
64
|
+
spanId: (0, ids_1.generateUlid)('spn'),
|
|
65
|
+
parentSpanId: null,
|
|
66
|
+
name: spanName,
|
|
67
|
+
metadata: {},
|
|
68
|
+
};
|
|
69
|
+
const context = {
|
|
70
|
+
currentSpan: span,
|
|
71
|
+
spanStack: [span],
|
|
72
|
+
startTime,
|
|
73
|
+
stepCounter: 0,
|
|
74
|
+
eventCounter: 0,
|
|
75
|
+
};
|
|
76
|
+
(0, context_1.recordTraceStart)(span.traceId, startTime);
|
|
77
|
+
const runContext = { context, span, created: true };
|
|
78
|
+
this.runContexts.set(runId, runContext);
|
|
79
|
+
return runContext;
|
|
80
|
+
}
|
|
81
|
+
const span = (0, context_1.runInContext)(baseContext, () => (0, context_1.startSpan)(spanName));
|
|
82
|
+
const runContext = { context: baseContext, span, created: false };
|
|
83
|
+
this.runContexts.set(runId, runContext);
|
|
84
|
+
return runContext;
|
|
85
|
+
}
|
|
86
|
+
async finalizeRunContext(runId) {
|
|
87
|
+
const runContext = this.runContexts.get(runId);
|
|
88
|
+
if (!runContext) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
this.runContexts.delete(runId);
|
|
92
|
+
if (runContext.created) {
|
|
93
|
+
const result = (0, context_1.runInContext)(runContext.context, () => (0, context_1.completeTrace)(runContext.span));
|
|
94
|
+
if (result && typeof result.then === 'function') {
|
|
95
|
+
await result;
|
|
96
|
+
}
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
(0, context_1.runInContext)(runContext.context, () => {
|
|
100
|
+
(0, context_1.popSpan)();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
40
103
|
// Chain callbacks
|
|
41
104
|
async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata) {
|
|
42
105
|
const chainType = chain.name || chain.id?.[chain.id.length - 1] || 'unknown';
|
|
43
|
-
const
|
|
44
|
-
const event =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
106
|
+
const runContext = this.ensureRunContext(runId, parentRunId, 'langchain.chain');
|
|
107
|
+
const event = (0, context_1.runInContext)(runContext.context, () => {
|
|
108
|
+
const state = (0, runtime_1.ensureState)();
|
|
109
|
+
const built = state.eventBuilder.build('agent_step', {
|
|
110
|
+
step_type: 'chain_start',
|
|
111
|
+
chain_type: chainType,
|
|
112
|
+
inputs: this.capturePrompts ? inputs : { _truncated: true },
|
|
113
|
+
run_id: runId,
|
|
114
|
+
parent_run_id: parentRunId || null,
|
|
115
|
+
tags: tags || [],
|
|
116
|
+
metadata: metadata || {},
|
|
117
|
+
}, {
|
|
118
|
+
dataClassification: this.dataClassification,
|
|
119
|
+
purpose: this.purpose,
|
|
120
|
+
});
|
|
121
|
+
(0, runtime_1.emitEvent)(built);
|
|
122
|
+
return built;
|
|
55
123
|
});
|
|
56
|
-
(0, runtime_1.emitEvent)(event);
|
|
57
124
|
this.runMap.set(runId, event.event_id);
|
|
58
125
|
(0, lineage_1.setCurrentEvent)(event.event_id);
|
|
59
126
|
}
|
|
@@ -62,35 +129,57 @@ class MonoraCallbackHandler {
|
|
|
62
129
|
if (parentEventId) {
|
|
63
130
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
64
131
|
}
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
132
|
+
const runContext = this.runContexts.get(runId);
|
|
133
|
+
const ctx = runContext?.context;
|
|
134
|
+
const emit = () => {
|
|
135
|
+
const state = (0, runtime_1.ensureState)();
|
|
136
|
+
const event = state.eventBuilder.build('agent_step', {
|
|
137
|
+
step_type: 'chain_end',
|
|
138
|
+
outputs: this.captureCompletions ? outputs : { _truncated: true },
|
|
139
|
+
run_id: runId,
|
|
140
|
+
}, {
|
|
141
|
+
dataClassification: this.dataClassification,
|
|
142
|
+
purpose: this.purpose,
|
|
143
|
+
});
|
|
144
|
+
(0, runtime_1.emitEvent)(event);
|
|
145
|
+
};
|
|
146
|
+
if (ctx) {
|
|
147
|
+
(0, context_1.runInContext)(ctx, emit);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
emit();
|
|
151
|
+
}
|
|
75
152
|
this.runMap.delete(runId);
|
|
153
|
+
await this.finalizeRunContext(runId);
|
|
76
154
|
}
|
|
77
155
|
async handleChainError(error, runId, parentRunId) {
|
|
78
156
|
const parentEventId = this.runMap.get(runId);
|
|
79
157
|
if (parentEventId) {
|
|
80
158
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
81
159
|
}
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
160
|
+
const runContext = this.runContexts.get(runId);
|
|
161
|
+
const ctx = runContext?.context;
|
|
162
|
+
const emit = () => {
|
|
163
|
+
const state = (0, runtime_1.ensureState)();
|
|
164
|
+
const event = state.eventBuilder.build('agent_step', {
|
|
165
|
+
step_type: 'chain_error',
|
|
166
|
+
error: error.message || String(error),
|
|
167
|
+
error_type: error.constructor?.name || 'Error',
|
|
168
|
+
run_id: runId,
|
|
169
|
+
}, {
|
|
170
|
+
dataClassification: this.dataClassification,
|
|
171
|
+
purpose: this.purpose,
|
|
172
|
+
});
|
|
173
|
+
(0, runtime_1.emitEvent)(event);
|
|
174
|
+
};
|
|
175
|
+
if (ctx) {
|
|
176
|
+
(0, context_1.runInContext)(ctx, emit);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
emit();
|
|
180
|
+
}
|
|
93
181
|
this.runMap.delete(runId);
|
|
182
|
+
await this.finalizeRunContext(runId);
|
|
94
183
|
}
|
|
95
184
|
// LLM callbacks
|
|
96
185
|
async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata) {
|
|
@@ -104,24 +193,42 @@ class MonoraCallbackHandler {
|
|
|
104
193
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
105
194
|
}
|
|
106
195
|
}
|
|
107
|
-
const
|
|
108
|
-
|
|
196
|
+
const requestPayload = {
|
|
197
|
+
prompts,
|
|
198
|
+
metadata,
|
|
199
|
+
tags,
|
|
200
|
+
extra: extraParams,
|
|
201
|
+
};
|
|
202
|
+
const { state, trace } = (0, governance_1.preflightLlmCall)({
|
|
109
203
|
model,
|
|
110
|
-
|
|
111
|
-
prompts: this.capturePrompts
|
|
112
|
-
? prompts
|
|
113
|
-
: prompts.map((p) => `<${p.length} chars>`),
|
|
114
|
-
num_prompts: prompts.length,
|
|
115
|
-
run_id: runId,
|
|
116
|
-
parent_run_id: parentRunId || null,
|
|
117
|
-
tags: tags || [],
|
|
118
|
-
metadata: metadata || {},
|
|
119
|
-
}, {
|
|
204
|
+
requestPayload,
|
|
120
205
|
dataClassification: this.dataClassification,
|
|
121
206
|
purpose: this.purpose,
|
|
207
|
+
provider: 'langchain',
|
|
208
|
+
spanName: 'llm_call:langchain',
|
|
209
|
+
});
|
|
210
|
+
const event = (0, context_1.runInContext)(trace.context, () => {
|
|
211
|
+
const built = state.eventBuilder.build('llm_call', {
|
|
212
|
+
model,
|
|
213
|
+
provider: 'langchain',
|
|
214
|
+
prompts: this.capturePrompts
|
|
215
|
+
? prompts
|
|
216
|
+
: prompts.map((p) => `<${p.length} chars>`),
|
|
217
|
+
num_prompts: prompts.length,
|
|
218
|
+
run_id: runId,
|
|
219
|
+
parent_run_id: parentRunId || null,
|
|
220
|
+
tags: tags || [],
|
|
221
|
+
metadata: metadata || {},
|
|
222
|
+
status: 'started',
|
|
223
|
+
}, {
|
|
224
|
+
dataClassification: this.dataClassification,
|
|
225
|
+
purpose: this.purpose,
|
|
226
|
+
});
|
|
227
|
+
(0, runtime_1.emitEvent)(built);
|
|
228
|
+
return built;
|
|
122
229
|
});
|
|
123
|
-
(0, runtime_1.emitEvent)(event);
|
|
124
230
|
this.runMap.set(runId, event.event_id);
|
|
231
|
+
this.llmRuns.set(runId, { state, trace, model });
|
|
125
232
|
(0, lineage_1.setCurrentEvent)(event.event_id);
|
|
126
233
|
}
|
|
127
234
|
async handleLLMEnd(output, runId, parentRunId) {
|
|
@@ -168,17 +275,45 @@ class MonoraCallbackHandler {
|
|
|
168
275
|
}
|
|
169
276
|
}
|
|
170
277
|
}
|
|
171
|
-
const
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
278
|
+
const runInfo = this.llmRuns.get(runId);
|
|
279
|
+
if (runInfo) {
|
|
280
|
+
(0, context_1.runInContext)(runInfo.trace.context, () => {
|
|
281
|
+
const event = runInfo.state.eventBuilder.build('llm_call', {
|
|
282
|
+
completions,
|
|
283
|
+
num_completions: completions.length,
|
|
284
|
+
token_usage: tokenUsage,
|
|
285
|
+
run_id: runId,
|
|
286
|
+
status: 'success',
|
|
287
|
+
}, {
|
|
288
|
+
dataClassification: this.dataClassification,
|
|
289
|
+
purpose: this.purpose,
|
|
290
|
+
});
|
|
291
|
+
(0, runtime_1.emitEvent)(event);
|
|
292
|
+
});
|
|
293
|
+
const responseStub = Object.keys(tokenUsage).length > 0 ? { usage: tokenUsage } : null;
|
|
294
|
+
await (0, governance_1.postflightLlmCall)({
|
|
295
|
+
state: runInfo.state,
|
|
296
|
+
trace: runInfo.trace,
|
|
297
|
+
model: runInfo.model,
|
|
298
|
+
response: responseStub,
|
|
299
|
+
dataClassification: this.dataClassification,
|
|
300
|
+
purpose: this.purpose,
|
|
301
|
+
});
|
|
302
|
+
this.llmRuns.delete(runId);
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
const state = (0, runtime_1.ensureState)();
|
|
306
|
+
const event = state.eventBuilder.build('llm_call', {
|
|
307
|
+
completions,
|
|
308
|
+
num_completions: completions.length,
|
|
309
|
+
token_usage: tokenUsage,
|
|
310
|
+
run_id: runId,
|
|
311
|
+
}, {
|
|
312
|
+
dataClassification: this.dataClassification,
|
|
313
|
+
purpose: this.purpose,
|
|
314
|
+
});
|
|
315
|
+
(0, runtime_1.emitEvent)(event);
|
|
316
|
+
}
|
|
182
317
|
this.runMap.delete(runId);
|
|
183
318
|
}
|
|
184
319
|
async handleLLMError(error, runId, parentRunId) {
|
|
@@ -186,16 +321,43 @@ class MonoraCallbackHandler {
|
|
|
186
321
|
if (parentEventId) {
|
|
187
322
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
188
323
|
}
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
324
|
+
const runInfo = this.llmRuns.get(runId);
|
|
325
|
+
if (runInfo) {
|
|
326
|
+
(0, context_1.runInContext)(runInfo.trace.context, () => {
|
|
327
|
+
const event = runInfo.state.eventBuilder.build('llm_call', {
|
|
328
|
+
error: error.message || String(error),
|
|
329
|
+
error_type: error.constructor?.name || 'Error',
|
|
330
|
+
run_id: runId,
|
|
331
|
+
status: 'error',
|
|
332
|
+
}, {
|
|
333
|
+
dataClassification: this.dataClassification,
|
|
334
|
+
purpose: this.purpose,
|
|
335
|
+
});
|
|
336
|
+
(0, runtime_1.emitEvent)(event);
|
|
337
|
+
});
|
|
338
|
+
await (0, governance_1.postflightLlmCall)({
|
|
339
|
+
state: runInfo.state,
|
|
340
|
+
trace: runInfo.trace,
|
|
341
|
+
model: runInfo.model,
|
|
342
|
+
response: null,
|
|
343
|
+
dataClassification: this.dataClassification,
|
|
344
|
+
purpose: this.purpose,
|
|
345
|
+
error: true,
|
|
346
|
+
});
|
|
347
|
+
this.llmRuns.delete(runId);
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
const state = (0, runtime_1.ensureState)();
|
|
351
|
+
const event = state.eventBuilder.build('llm_call', {
|
|
352
|
+
error: error.message || String(error),
|
|
353
|
+
error_type: error.constructor?.name || 'Error',
|
|
354
|
+
run_id: runId,
|
|
355
|
+
}, {
|
|
356
|
+
dataClassification: this.dataClassification,
|
|
357
|
+
purpose: this.purpose,
|
|
358
|
+
});
|
|
359
|
+
(0, runtime_1.emitEvent)(event);
|
|
360
|
+
}
|
|
199
361
|
this.runMap.delete(runId);
|
|
200
362
|
}
|
|
201
363
|
// Tool callbacks
|
|
@@ -207,19 +369,23 @@ class MonoraCallbackHandler {
|
|
|
207
369
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
208
370
|
}
|
|
209
371
|
}
|
|
210
|
-
const
|
|
211
|
-
const event =
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
372
|
+
const runContext = this.ensureRunContext(runId, parentRunId, 'langchain.tool');
|
|
373
|
+
const event = (0, context_1.runInContext)(runContext.context, () => {
|
|
374
|
+
const state = (0, runtime_1.ensureState)();
|
|
375
|
+
const built = state.eventBuilder.build('tool_call', {
|
|
376
|
+
tool_name: toolName,
|
|
377
|
+
input: this.capturePrompts ? input : `<${input.length} chars>`,
|
|
378
|
+
run_id: runId,
|
|
379
|
+
parent_run_id: parentRunId || null,
|
|
380
|
+
tags: tags || [],
|
|
381
|
+
metadata: metadata || {},
|
|
382
|
+
}, {
|
|
383
|
+
dataClassification: this.dataClassification,
|
|
384
|
+
purpose: this.purpose,
|
|
385
|
+
});
|
|
386
|
+
(0, runtime_1.emitEvent)(built);
|
|
387
|
+
return built;
|
|
221
388
|
});
|
|
222
|
-
(0, runtime_1.emitEvent)(event);
|
|
223
389
|
this.runMap.set(runId, event.event_id);
|
|
224
390
|
(0, lineage_1.setCurrentEvent)(event.event_id);
|
|
225
391
|
}
|
|
@@ -228,33 +394,55 @@ class MonoraCallbackHandler {
|
|
|
228
394
|
if (parentEventId) {
|
|
229
395
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
230
396
|
}
|
|
231
|
-
const
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
397
|
+
const runContext = this.runContexts.get(runId);
|
|
398
|
+
const ctx = runContext?.context;
|
|
399
|
+
const emit = () => {
|
|
400
|
+
const state = (0, runtime_1.ensureState)();
|
|
401
|
+
const event = state.eventBuilder.build('tool_call', {
|
|
402
|
+
output: this.captureCompletions ? output : `<${output.length} chars>`,
|
|
403
|
+
run_id: runId,
|
|
404
|
+
}, {
|
|
405
|
+
dataClassification: this.dataClassification,
|
|
406
|
+
purpose: this.purpose,
|
|
407
|
+
});
|
|
408
|
+
(0, runtime_1.emitEvent)(event);
|
|
409
|
+
};
|
|
410
|
+
if (ctx) {
|
|
411
|
+
(0, context_1.runInContext)(ctx, emit);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
emit();
|
|
415
|
+
}
|
|
240
416
|
this.runMap.delete(runId);
|
|
417
|
+
await this.finalizeRunContext(runId);
|
|
241
418
|
}
|
|
242
419
|
async handleToolError(error, runId, parentRunId) {
|
|
243
420
|
const parentEventId = this.runMap.get(runId);
|
|
244
421
|
if (parentEventId) {
|
|
245
422
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
246
423
|
}
|
|
247
|
-
const
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
424
|
+
const runContext = this.runContexts.get(runId);
|
|
425
|
+
const ctx = runContext?.context;
|
|
426
|
+
const emit = () => {
|
|
427
|
+
const state = (0, runtime_1.ensureState)();
|
|
428
|
+
const event = state.eventBuilder.build('tool_call', {
|
|
429
|
+
error: error.message || String(error),
|
|
430
|
+
error_type: error.constructor?.name || 'Error',
|
|
431
|
+
run_id: runId,
|
|
432
|
+
}, {
|
|
433
|
+
dataClassification: this.dataClassification,
|
|
434
|
+
purpose: this.purpose,
|
|
435
|
+
});
|
|
436
|
+
(0, runtime_1.emitEvent)(event);
|
|
437
|
+
};
|
|
438
|
+
if (ctx) {
|
|
439
|
+
(0, context_1.runInContext)(ctx, emit);
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
emit();
|
|
443
|
+
}
|
|
257
444
|
this.runMap.delete(runId);
|
|
445
|
+
await this.finalizeRunContext(runId);
|
|
258
446
|
}
|
|
259
447
|
// Retriever callbacks
|
|
260
448
|
async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata) {
|
|
@@ -264,19 +452,23 @@ class MonoraCallbackHandler {
|
|
|
264
452
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
265
453
|
}
|
|
266
454
|
}
|
|
267
|
-
const
|
|
268
|
-
const event =
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
455
|
+
const runContext = this.ensureRunContext(runId, parentRunId, 'langchain.retriever');
|
|
456
|
+
const event = (0, context_1.runInContext)(runContext.context, () => {
|
|
457
|
+
const state = (0, runtime_1.ensureState)();
|
|
458
|
+
const built = state.eventBuilder.build('custom', {
|
|
459
|
+
custom_type: 'retriever_query',
|
|
460
|
+
query: this.capturePrompts ? query : `<${query.length} chars>`,
|
|
461
|
+
run_id: runId,
|
|
462
|
+
parent_run_id: parentRunId || null,
|
|
463
|
+
tags: tags || [],
|
|
464
|
+
metadata: metadata || {},
|
|
465
|
+
}, {
|
|
466
|
+
dataClassification: this.dataClassification,
|
|
467
|
+
purpose: this.purpose,
|
|
468
|
+
});
|
|
469
|
+
(0, runtime_1.emitEvent)(built);
|
|
470
|
+
return built;
|
|
278
471
|
});
|
|
279
|
-
(0, runtime_1.emitEvent)(event);
|
|
280
472
|
this.runMap.set(runId, event.event_id);
|
|
281
473
|
(0, lineage_1.setCurrentEvent)(event.event_id);
|
|
282
474
|
}
|
|
@@ -285,46 +477,74 @@ class MonoraCallbackHandler {
|
|
|
285
477
|
if (parentEventId) {
|
|
286
478
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
287
479
|
}
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
480
|
+
const runContext = this.runContexts.get(runId);
|
|
481
|
+
const ctx = runContext?.context;
|
|
482
|
+
const emit = () => {
|
|
483
|
+
for (const doc of documents) {
|
|
484
|
+
const source = doc.metadata?.source;
|
|
485
|
+
if (typeof source === 'string' && source.trim()) {
|
|
486
|
+
(0, lineage_1.addDataSource)(source);
|
|
487
|
+
}
|
|
295
488
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
489
|
+
const docSummaries = documents.slice(0, 10).map((doc) => {
|
|
490
|
+
const summary = {
|
|
491
|
+
page_content_length: doc.pageContent?.length || 0,
|
|
492
|
+
metadata: doc.metadata || {},
|
|
493
|
+
};
|
|
494
|
+
if (this.captureCompletions && doc.pageContent) {
|
|
495
|
+
summary.page_content = doc.pageContent.slice(0, 500);
|
|
496
|
+
}
|
|
497
|
+
return summary;
|
|
498
|
+
});
|
|
499
|
+
const state = (0, runtime_1.ensureState)();
|
|
500
|
+
const event = state.eventBuilder.build('custom', {
|
|
501
|
+
custom_type: 'retriever_result',
|
|
502
|
+
num_documents: documents.length,
|
|
503
|
+
documents: docSummaries,
|
|
504
|
+
run_id: runId,
|
|
505
|
+
}, {
|
|
506
|
+
dataClassification: this.dataClassification,
|
|
507
|
+
purpose: this.purpose,
|
|
508
|
+
});
|
|
509
|
+
(0, runtime_1.emitEvent)(event);
|
|
510
|
+
};
|
|
511
|
+
if (ctx) {
|
|
512
|
+
(0, context_1.runInContext)(ctx, emit);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
emit();
|
|
516
|
+
}
|
|
309
517
|
this.runMap.delete(runId);
|
|
518
|
+
await this.finalizeRunContext(runId);
|
|
310
519
|
}
|
|
311
520
|
async handleRetrieverError(error, runId, parentRunId) {
|
|
312
521
|
const parentEventId = this.runMap.get(runId);
|
|
313
522
|
if (parentEventId) {
|
|
314
523
|
(0, lineage_1.addInputEvent)(parentEventId);
|
|
315
524
|
}
|
|
316
|
-
const
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
525
|
+
const runContext = this.runContexts.get(runId);
|
|
526
|
+
const ctx = runContext?.context;
|
|
527
|
+
const emit = () => {
|
|
528
|
+
const state = (0, runtime_1.ensureState)();
|
|
529
|
+
const event = state.eventBuilder.build('custom', {
|
|
530
|
+
custom_type: 'retriever_error',
|
|
531
|
+
error: error.message || String(error),
|
|
532
|
+
error_type: error.constructor?.name || 'Error',
|
|
533
|
+
run_id: runId,
|
|
534
|
+
}, {
|
|
535
|
+
dataClassification: this.dataClassification,
|
|
536
|
+
purpose: this.purpose,
|
|
537
|
+
});
|
|
538
|
+
(0, runtime_1.emitEvent)(event);
|
|
539
|
+
};
|
|
540
|
+
if (ctx) {
|
|
541
|
+
(0, context_1.runInContext)(ctx, emit);
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
emit();
|
|
545
|
+
}
|
|
327
546
|
this.runMap.delete(runId);
|
|
547
|
+
await this.finalizeRunContext(runId);
|
|
328
548
|
}
|
|
329
549
|
}
|
|
330
550
|
exports.MonoraCallbackHandler = MonoraCallbackHandler;
|
|
@@ -35,10 +35,19 @@ interface OpenAIClient {
|
|
|
35
35
|
embeddings?: {
|
|
36
36
|
create: (...args: any[]) => any;
|
|
37
37
|
};
|
|
38
|
+
responses?: {
|
|
39
|
+
create: (...args: any[]) => any;
|
|
40
|
+
stream?: (...args: any[]) => any;
|
|
41
|
+
};
|
|
38
42
|
}
|
|
39
43
|
/**
|
|
40
44
|
* Patch an OpenAI client to automatically trace all API calls.
|
|
41
45
|
*/
|
|
42
46
|
export declare function patchOpenAI(client: OpenAIClient, options?: PatchOpenAIOptions): void;
|
|
47
|
+
export declare function wrapChatCompletions(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
48
|
+
export declare function wrapCompletions(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
49
|
+
export declare function wrapEmbeddings(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
50
|
+
export declare function wrapResponsesStreamMethod(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
51
|
+
export declare function wrapResponses(originalFn: (...args: any[]) => any, dataClassification: string, purpose: string, reason?: string): (...args: any[]) => any;
|
|
43
52
|
export {};
|
|
44
53
|
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/integrations/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/integrations/openai.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAUH,MAAM,WAAW,kBAAkB;IACjC,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,UAAU,YAAY;IACpB,IAAI,CAAC,EAAE;QACL,WAAW,EAAE;YACX,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;SACjC,CAAC;KACH,CAAC;IACF,WAAW,CAAC,EAAE;QACZ,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;KACjC,CAAC;IACF,UAAU,CAAC,EAAE;QACX,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;KACjC,CAAC;IACF,SAAS,CAAC,EAAE;QACV,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;CACH;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,YAAY,EACpB,OAAO,GAAE,kBAAuB,GAC/B,IAAI,CA2DN;AAUD,wBAAgB,mBAAmB,CACjC,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,CAkJzB;AA+YD,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,CA2HzB;AAED,wBAAgB,cAAc,CAC5B,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,CAwHzB;AAED,wBAAgB,yBAAyB,CACvC,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,CAyDzB;AAED,wBAAgB,aAAa,CAC3B,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,CAqIzB"}
|