holomime 1.8.0 → 1.9.1

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.
@@ -0,0 +1,164 @@
1
+ import { z } from 'zod';
2
+
3
+ declare const severitySchema: z.ZodEnum<["info", "warning", "concern"]>;
4
+ type Severity = z.infer<typeof severitySchema>;
5
+ interface DetectedPattern {
6
+ id: string;
7
+ name: string;
8
+ severity: Severity;
9
+ count: number;
10
+ percentage: number;
11
+ description: string;
12
+ examples: string[];
13
+ prescription?: string;
14
+ }
15
+
16
+ /**
17
+ * Composable Guard API — chain behavioral detectors like Guardrails AI chains validators.
18
+ *
19
+ * Usage:
20
+ * import { Guard } from "holomime";
21
+ *
22
+ * const guard = Guard.create("my-agent")
23
+ * .use(detectApologies)
24
+ * .use(detectHedging)
25
+ * .use(detectSentiment);
26
+ *
27
+ * const result = guard.run(messages);
28
+ * if (!result.passed) {
29
+ * console.log(result.patterns); // failing patterns
30
+ * }
31
+ */
32
+
33
+ interface GuardResult {
34
+ /** Whether all detectors passed (no warning/concern patterns). */
35
+ passed: boolean;
36
+ /** Agent name. */
37
+ agent: string;
38
+ /** Total messages analyzed. */
39
+ messagesAnalyzed: number;
40
+ /** Patterns that triggered (warning or concern severity). */
41
+ patterns: DetectedPattern[];
42
+ /** Healthy patterns (info severity). */
43
+ healthy: DetectedPattern[];
44
+ /** All detectors that were run. */
45
+ detectorsRun: number;
46
+ /** Timestamp. */
47
+ timestamp: string;
48
+ /** Overall severity: "clean" | "warning" | "concern". */
49
+ severity: "clean" | "warning" | "concern";
50
+ }
51
+
52
+ /**
53
+ * LangChain / CrewAI / LlamaIndex Callback Handler
54
+ *
55
+ * Monitors LLM responses in real-time and detects behavioral anti-patterns.
56
+ * Works with any LangChain-compatible framework that supports callback handlers.
57
+ *
58
+ * Usage:
59
+ * import { HolomimeCallbackHandler } from "holomime/integrations/langchain";
60
+ *
61
+ * const handler = new HolomimeCallbackHandler({
62
+ * mode: "monitor", // "monitor" | "enforce" | "strict"
63
+ * personality: ".personality.json",
64
+ * onViolation: (v) => console.warn("Behavioral drift:", v),
65
+ * });
66
+ *
67
+ * // LangChain
68
+ * const chain = prompt.pipe(model).pipe(parser);
69
+ * await chain.invoke({ input: "hello" }, { callbacks: [handler] });
70
+ *
71
+ * // CrewAI — pass as LangChain callback on the underlying LLM
72
+ * // LlamaIndex — use as a callback handler on the LLM
73
+ */
74
+
75
+ type CallbackMode = "monitor" | "enforce" | "strict";
76
+ interface CallbackViolation {
77
+ patterns: DetectedPattern[];
78
+ severity: "warning" | "concern";
79
+ response: string;
80
+ runId?: string;
81
+ timestamp: string;
82
+ }
83
+ interface HolomimeCallbackOptions {
84
+ /** Guard mode. Default: "monitor". */
85
+ mode?: CallbackMode;
86
+ /** Path to .personality.json or inline spec object. */
87
+ personality?: string | object;
88
+ /** Minimum severity to trigger. Default: "warning". */
89
+ minSeverity?: "warning" | "concern";
90
+ /** Callback fired on every violation. */
91
+ onViolation?: (violation: CallbackViolation) => void;
92
+ /** Agent name for reports. Default: "langchain-agent". */
93
+ name?: string;
94
+ /** Buffer size — number of messages to retain for context. Default: 50. */
95
+ bufferSize?: number;
96
+ }
97
+ interface CallbackStats {
98
+ totalResponses: number;
99
+ passed: number;
100
+ violated: number;
101
+ blocked: number;
102
+ patternCounts: Record<string, number>;
103
+ }
104
+ /**
105
+ * HolomimeCallbackHandler — behavioral alignment monitor for LangChain-compatible frameworks.
106
+ *
107
+ * Implements the LangChain BaseCallbackHandler interface without importing langchain,
108
+ * keeping it as an optional peer dependency. Works with LangChain, CrewAI, and any
109
+ * framework that accepts { handleLLMEnd, handleLLMStart, handleLLMError } callbacks.
110
+ */
111
+ declare class HolomimeCallbackHandler {
112
+ readonly name = "holomime";
113
+ readonly lc_serializable = false;
114
+ private guard;
115
+ private mode;
116
+ private minSeverity;
117
+ private onViolation?;
118
+ private messageBuffer;
119
+ private bufferSize;
120
+ private currentRunMessages;
121
+ private _stats;
122
+ constructor(options?: HolomimeCallbackOptions);
123
+ /**
124
+ * Called when an LLM starts generating.
125
+ * Captures the input messages for context.
126
+ */
127
+ handleLLMStart(_llm: any, prompts: string[], runId?: string): void;
128
+ /**
129
+ * Called when an LLM finishes generating.
130
+ * Runs behavioral analysis on the response.
131
+ */
132
+ handleLLMEnd(output: any, runId?: string): void;
133
+ /**
134
+ * Called on LLM errors. Clean up run tracking.
135
+ */
136
+ handleLLMError(_error: any, runId?: string): void;
137
+ /**
138
+ * Called when a chain starts. Captures input for context.
139
+ */
140
+ handleChainStart(_chain: any, inputs: Record<string, any>, runId?: string): void;
141
+ /**
142
+ * Get cumulative stats.
143
+ */
144
+ stats(): CallbackStats;
145
+ /**
146
+ * Reset the message buffer and stats.
147
+ */
148
+ reset(): void;
149
+ /**
150
+ * Get the current guard result for the buffered conversation.
151
+ */
152
+ diagnose(): GuardResult;
153
+ private severityMeetsMin;
154
+ private extractResponseText;
155
+ }
156
+ /**
157
+ * Error thrown in strict mode when a concern-level violation is detected.
158
+ */
159
+ declare class HolomimeViolationError extends Error {
160
+ readonly violation: CallbackViolation;
161
+ constructor(violation: CallbackViolation);
162
+ }
163
+
164
+ export { type CallbackMode, type CallbackStats, type CallbackViolation, HolomimeCallbackHandler, type HolomimeCallbackOptions, HolomimeViolationError };