mesedi 0.2.0 → 0.7.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.
Files changed (99) hide show
  1. package/README.md +57 -5
  2. package/dist/_errorClassesGenerated.d.ts +18 -0
  3. package/dist/_errorClassesGenerated.d.ts.map +1 -0
  4. package/dist/_errorClassesGenerated.js +24 -0
  5. package/dist/_errorClassesGenerated.js.map +1 -0
  6. package/dist/anthropic_integration.d.ts.map +1 -1
  7. package/dist/anthropic_integration.js +51 -10
  8. package/dist/anthropic_integration.js.map +1 -1
  9. package/dist/client.d.ts +26 -6
  10. package/dist/client.d.ts.map +1 -1
  11. package/dist/client.js +35 -7
  12. package/dist/client.js.map +1 -1
  13. package/dist/cohere_integration.d.ts +42 -0
  14. package/dist/cohere_integration.d.ts.map +1 -0
  15. package/dist/cohere_integration.js +456 -0
  16. package/dist/cohere_integration.js.map +1 -0
  17. package/dist/compress.d.ts +69 -0
  18. package/dist/compress.d.ts.map +1 -0
  19. package/dist/compress.js +75 -0
  20. package/dist/compress.js.map +1 -0
  21. package/dist/context.d.ts +11 -2
  22. package/dist/context.d.ts.map +1 -1
  23. package/dist/context.js +13 -3
  24. package/dist/context.js.map +1 -1
  25. package/dist/errors.d.ts +137 -0
  26. package/dist/errors.d.ts.map +1 -0
  27. package/dist/errors.js +462 -0
  28. package/dist/errors.js.map +1 -0
  29. package/dist/events.d.ts +13 -0
  30. package/dist/events.d.ts.map +1 -1
  31. package/dist/events.js +26 -1
  32. package/dist/events.js.map +1 -1
  33. package/dist/gemini_integration.d.ts +33 -0
  34. package/dist/gemini_integration.d.ts.map +1 -0
  35. package/dist/gemini_integration.js +334 -0
  36. package/dist/gemini_integration.js.map +1 -0
  37. package/dist/halt_stream.d.ts.map +1 -1
  38. package/dist/halt_stream.js +11 -1
  39. package/dist/halt_stream.js.map +1 -1
  40. package/dist/index.d.ts +15 -2
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +9 -1
  43. package/dist/index.js.map +1 -1
  44. package/dist/integrations/langchain.d.ts +95 -0
  45. package/dist/integrations/langchain.d.ts.map +1 -0
  46. package/dist/integrations/langchain.js +549 -0
  47. package/dist/integrations/langchain.js.map +1 -0
  48. package/dist/integrations/langgraph.d.ts +99 -0
  49. package/dist/integrations/langgraph.d.ts.map +1 -0
  50. package/dist/integrations/langgraph.js +572 -0
  51. package/dist/integrations/langgraph.js.map +1 -0
  52. package/dist/integrations/mastra.d.ts +114 -0
  53. package/dist/integrations/mastra.d.ts.map +1 -0
  54. package/dist/integrations/mastra.js +354 -0
  55. package/dist/integrations/mastra.js.map +1 -0
  56. package/dist/integrations/openai_agents.d.ts +66 -0
  57. package/dist/integrations/openai_agents.d.ts.map +1 -0
  58. package/dist/integrations/openai_agents.js +243 -0
  59. package/dist/integrations/openai_agents.js.map +1 -0
  60. package/dist/integrations/promptfoo.d.ts +62 -0
  61. package/dist/integrations/promptfoo.d.ts.map +1 -0
  62. package/dist/integrations/promptfoo.js +76 -0
  63. package/dist/integrations/promptfoo.js.map +1 -0
  64. package/dist/integrations/ragas.d.ts +65 -0
  65. package/dist/integrations/ragas.d.ts.map +1 -0
  66. package/dist/integrations/ragas.js +78 -0
  67. package/dist/integrations/ragas.js.map +1 -0
  68. package/dist/integrations/vectara.d.ts +60 -0
  69. package/dist/integrations/vectara.d.ts.map +1 -0
  70. package/dist/integrations/vectara.js +59 -0
  71. package/dist/integrations/vectara.js.map +1 -0
  72. package/dist/observe.d.ts +355 -0
  73. package/dist/observe.d.ts.map +1 -1
  74. package/dist/observe.js +532 -0
  75. package/dist/observe.js.map +1 -1
  76. package/dist/ollama_integration.d.ts +126 -0
  77. package/dist/ollama_integration.d.ts.map +1 -0
  78. package/dist/ollama_integration.js +404 -0
  79. package/dist/ollama_integration.js.map +1 -0
  80. package/dist/openai_integration.d.ts +67 -0
  81. package/dist/openai_integration.d.ts.map +1 -0
  82. package/dist/openai_integration.js +574 -0
  83. package/dist/openai_integration.js.map +1 -0
  84. package/dist/tool.d.ts.map +1 -1
  85. package/dist/tool.js +189 -6
  86. package/dist/tool.js.map +1 -1
  87. package/dist/truncate.d.ts +84 -0
  88. package/dist/truncate.d.ts.map +1 -0
  89. package/dist/truncate.js +134 -0
  90. package/dist/truncate.js.map +1 -0
  91. package/dist/vertex_gemini_integration.d.ts +27 -0
  92. package/dist/vertex_gemini_integration.d.ts.map +1 -0
  93. package/dist/vertex_gemini_integration.js +229 -0
  94. package/dist/vertex_gemini_integration.js.map +1 -0
  95. package/dist/wrap.d.ts +36 -0
  96. package/dist/wrap.d.ts.map +1 -1
  97. package/dist/wrap.js +5 -2
  98. package/dist/wrap.js.map +1 -1
  99. package/package.json +36 -3
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Ollama SDK monkey-patch — auto-emit llm_call events for every
3
+ * `Ollama.chat()` call inside a wrap()'d execution.
4
+ *
5
+ * Mirrors instrumentOpenAI / instrumentAnthropic shape (same
6
+ * fail-open semantics, same payload contract, same idempotency
7
+ * guarantee), with Ollama-shape adjustments:
8
+ *
9
+ * - Token field names: prompt_eval_count / eval_count — translated
10
+ * to canonical input_tokens / output_tokens on payload.
11
+ * - Response text is `response.message.content` (not `choices[0]...`).
12
+ * - No API-key auth; customers point at localhost or a remote
13
+ * Ollama host. No throttling auto-emit in this sub-wave — for a
14
+ * local runtime there is no upstream rate-limit signal worth
15
+ * surfacing.
16
+ *
17
+ * Why a dedicated integration when Ollama exposes an OpenAI-
18
+ * compatible endpoint? Customers who chose Ollama for local
19
+ * inference also chose the native `ollama` npm package for its
20
+ * ergonomics. Forcing them to switch to the openai client just to
21
+ * be observable defeats the point.
22
+ *
23
+ * Patches one surface in this sub-wave:
24
+ * - ollama.Ollama.prototype.chat
25
+ *
26
+ * Out of scope (filed as follow-ups in the Ollama arc):
27
+ * - Streaming (stream: true returns AsyncIterable): observed via
28
+ * wrapStreamingResponse (Wave 2.5.8). Chunks pass through to the
29
+ * customer's `for await` loop unchanged; the llm_call event ships
30
+ * at stream-end with streaming=true in the payload. (The 2.5.1.a
31
+ * maybeWarnStreamingUnsupported helper is kept as dead code with
32
+ * a deprecation note for a future cleanup wave.)
33
+ * - Embeddings (ollama.embed)
34
+ * - Generate API (ollama.generate)
35
+ *
36
+ * Dependency injection: instrumentOllama accepts an optional
37
+ * ollamaClass parameter so this code path is testable without
38
+ * installing the actual `ollama` package.
39
+ */
40
+ declare function maybeWarnStreamingUnsupported(): void;
41
+ export interface OllamaClassLike {
42
+ prototype: {
43
+ chat: (...args: any[]) => Promise<any>;
44
+ };
45
+ }
46
+ interface OllamaMessage {
47
+ role?: string;
48
+ content?: string | Array<{
49
+ type?: string;
50
+ text?: string;
51
+ }>;
52
+ }
53
+ interface OllamaChatResponse {
54
+ model?: string;
55
+ message?: {
56
+ role?: string;
57
+ content?: string;
58
+ };
59
+ prompt_eval_count?: number;
60
+ eval_count?: number;
61
+ done?: boolean;
62
+ }
63
+ /**
64
+ * Patch the Ollama SDK's chat method to emit llm_call events.
65
+ * Returns true if the class was patched (or already patched).
66
+ * Returns false if the class wasn't provided AND the ollama
67
+ * package isn't installed.
68
+ */
69
+ export declare function instrumentOllama(ollamaClass?: OllamaClassLike): Promise<boolean>;
70
+ declare function extractFirstSystemMessage(messages: OllamaMessage[]): string;
71
+ declare function extractLastUserMessage(messages: OllamaMessage[]): string;
72
+ declare function contentToText(content: string | Array<{
73
+ type?: string;
74
+ text?: string;
75
+ }> | undefined): string;
76
+ declare function extractResponseFields(response: OllamaChatResponse): {
77
+ responseText: string;
78
+ inputTokens: number;
79
+ outputTokens: number;
80
+ };
81
+ declare function numberOrZero(value: unknown): number;
82
+ declare function truncate(s: string, limit: number): string;
83
+ /** One chunk yielded by the ollama client's streaming generator.
84
+ * Partial text on .message.content for non-final chunks; final
85
+ * chunk carries .prompt_eval_count + .eval_count + .done=true. */
86
+ interface OllamaChatChunk {
87
+ model?: string;
88
+ message?: {
89
+ role?: string;
90
+ content?: string;
91
+ };
92
+ done?: boolean;
93
+ prompt_eval_count?: number;
94
+ eval_count?: number;
95
+ }
96
+ /** Apply one streaming chunk to the wrapper's state. Defensive: any
97
+ * extraction failure degrades silently so a single malformed chunk
98
+ * does not break stream-level aggregation. */
99
+ declare function accumulateChunk(chunk: OllamaChatChunk, state: {
100
+ textParts: string[];
101
+ inputTokens: number;
102
+ outputTokens: number;
103
+ }): void;
104
+ export declare const _testing: {
105
+ PROVIDER: string;
106
+ extractFirstSystemMessage: typeof extractFirstSystemMessage;
107
+ extractLastUserMessage: typeof extractLastUserMessage;
108
+ contentToText: typeof contentToText;
109
+ extractResponseFields: typeof extractResponseFields;
110
+ numberOrZero: typeof numberOrZero;
111
+ truncate: typeof truncate;
112
+ /** Resets the one-time streaming-warning flag so tests can assert
113
+ * the warning fires on first stream call after a reset. Not part
114
+ * of the public API. */
115
+ resetStreamingWarningGuard: () => void;
116
+ /** The streaming-warning function itself, exposed so unit tests
117
+ * can verify the once-per-process contract without spinning up a
118
+ * full wrap() + chat() integration scaffold. Not part of the
119
+ * public API. */
120
+ maybeWarnStreamingUnsupported: typeof maybeWarnStreamingUnsupported;
121
+ /** Wave 2.5.8 — exposed for unit tests of the streaming chunk-
122
+ * aggregation logic. Not part of the public API. */
123
+ accumulateChunk: typeof accumulateChunk;
124
+ };
125
+ export {};
126
+ //# sourceMappingURL=ollama_integration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ollama_integration.d.ts","sourceRoot":"","sources":["../src/ollama_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AA4BH,iBAAS,6BAA6B,IAAI,IAAI,CAU7C;AAED,MAAM,WAAW,eAAe;IAE9B,SAAS,EAAE;QAAE,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;KAAE,CAAC;CACvD;AAID,UAAU,aAAa;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IAC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OAAO,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC9C,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAgGD;;8CAE8C;AAC9C,iBAAS,eAAe,CACtB,KAAK,EAAE,eAAe,EACtB,KAAK,EAAE;IAAE,SAAS,EAAE,MAAM,EAAE,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GACxE,IAAI,CAaN;AAID,eAAO,MAAM,QAAQ;;;;;;;;IAQnB;;4BAEwB;sCACQ,IAAI;IAGpC;;;qBAGiB;;IAEjB;wDACoD;;CAErD,CAAC"}
@@ -0,0 +1,404 @@
1
+ /**
2
+ * Ollama SDK monkey-patch — auto-emit llm_call events for every
3
+ * `Ollama.chat()` call inside a wrap()'d execution.
4
+ *
5
+ * Mirrors instrumentOpenAI / instrumentAnthropic shape (same
6
+ * fail-open semantics, same payload contract, same idempotency
7
+ * guarantee), with Ollama-shape adjustments:
8
+ *
9
+ * - Token field names: prompt_eval_count / eval_count — translated
10
+ * to canonical input_tokens / output_tokens on payload.
11
+ * - Response text is `response.message.content` (not `choices[0]...`).
12
+ * - No API-key auth; customers point at localhost or a remote
13
+ * Ollama host. No throttling auto-emit in this sub-wave — for a
14
+ * local runtime there is no upstream rate-limit signal worth
15
+ * surfacing.
16
+ *
17
+ * Why a dedicated integration when Ollama exposes an OpenAI-
18
+ * compatible endpoint? Customers who chose Ollama for local
19
+ * inference also chose the native `ollama` npm package for its
20
+ * ergonomics. Forcing them to switch to the openai client just to
21
+ * be observable defeats the point.
22
+ *
23
+ * Patches one surface in this sub-wave:
24
+ * - ollama.Ollama.prototype.chat
25
+ *
26
+ * Out of scope (filed as follow-ups in the Ollama arc):
27
+ * - Streaming (stream: true returns AsyncIterable): observed via
28
+ * wrapStreamingResponse (Wave 2.5.8). Chunks pass through to the
29
+ * customer's `for await` loop unchanged; the llm_call event ships
30
+ * at stream-end with streaming=true in the payload. (The 2.5.1.a
31
+ * maybeWarnStreamingUnsupported helper is kept as dead code with
32
+ * a deprecation note for a future cleanup wave.)
33
+ * - Embeddings (ollama.embed)
34
+ * - Generate API (ollama.generate)
35
+ *
36
+ * Dependency injection: instrumentOllama accepts an optional
37
+ * ollamaClass parameter so this code path is testable without
38
+ * installing the actual `ollama` package.
39
+ */
40
+ import { getClient } from "./client.js";
41
+ import { currentExecutionContext, newEventId } from "./context.js";
42
+ import { classifyOllamaException } from "./errors.js";
43
+ import { EventType, utcNowRfc3339 } from "./events.js";
44
+ const MAX_SYSTEM = 1000;
45
+ const MAX_USER_MSG = 1000;
46
+ const MAX_RESPONSE = 1000;
47
+ const MAX_EXC_MSG = 500;
48
+ /** Stable lowercase provider identifier shipped on every llm_call
49
+ * event. Backend's provider_incident detector clusters cross-tenant
50
+ * signals on (provider, error_class) — though for local runtimes the
51
+ * cross-tenant signal is necessarily quiet. */
52
+ const PROVIDER = "ollama";
53
+ const _patched = new WeakSet();
54
+ /** Wave 2.5.1.a — one-time guard for streaming calls. The
55
+ * chunk-aggregating wrapper for streaming responses ships in a
56
+ * follow-up sub-wave; until then, calls with stream: true emit NO
57
+ * llm_call event AND log a single console.info so customers
58
+ * understand why their streaming calls don't appear in the Mesedi
59
+ * dashboard. */
60
+ let _streamingWarningEmitted = false;
61
+ function maybeWarnStreamingUnsupported() {
62
+ if (_streamingWarningEmitted)
63
+ return;
64
+ _streamingWarningEmitted = true;
65
+ console.info("mesedi: instrumentOllama observed a chat({ stream: true }) call; " +
66
+ "the chunk-aggregating wrapper for streaming responses ships in " +
67
+ "a follow-up sub-wave, so no llm_call event will be recorded for " +
68
+ "streaming calls until then. Non-streaming calls are unaffected. " +
69
+ "Your stream still works; only Mesedi observation is deferred.");
70
+ }
71
+ /**
72
+ * Patch the Ollama SDK's chat method to emit llm_call events.
73
+ * Returns true if the class was patched (or already patched).
74
+ * Returns false if the class wasn't provided AND the ollama
75
+ * package isn't installed.
76
+ */
77
+ export async function instrumentOllama(ollamaClass) {
78
+ let cls = ollamaClass;
79
+ if (!cls) {
80
+ try {
81
+ // Dynamic import so the mesedi SDK has no hard runtime
82
+ // dependency on `ollama`. The class lives at module.Ollama
83
+ // on the default export.
84
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
85
+ // @ts-ignore — package may not be installed; this is by design.
86
+ const mod = (await import("ollama"));
87
+ cls = mod?.Ollama ?? mod?.default?.Ollama;
88
+ }
89
+ catch {
90
+ console.warn("mesedi: ollama package not installed; instrumentOllama() is a no-op. " +
91
+ "Install with `npm install ollama` to enable, or pass the Ollama class explicitly.");
92
+ return false;
93
+ }
94
+ }
95
+ if (!cls)
96
+ return false;
97
+ patchChat(cls);
98
+ return true;
99
+ }
100
+ function patchChat(cls) {
101
+ if (_patched.has(cls))
102
+ return;
103
+ const originalChat = cls.prototype.chat;
104
+ cls.prototype.chat = async function patchedChat(...args) {
105
+ const ctx = currentExecutionContext();
106
+ if (!ctx)
107
+ return originalChat.apply(this, args);
108
+ ctx.checkBudget();
109
+ if (ctx.budgetTracker)
110
+ ctx.budgetTracker.incrementSteps();
111
+ const client = getClient();
112
+ const sequence = ctx.nextSequence();
113
+ const eventId = newEventId();
114
+ const firstArg = (args[0] ?? {});
115
+ const model = firstArg.model ?? "unknown";
116
+ const messages = Array.isArray(firstArg.messages) ? firstArg.messages : [];
117
+ const systemText = extractFirstSystemMessage(messages);
118
+ const userMessage = extractLastUserMessage(messages);
119
+ // Wave 2.5.8 — chunk-aggregating wrapper for streaming calls
120
+ // (replaces the 2.5.1.a no-op). When stream:true, response is
121
+ // an AsyncIterable<OllamaChatChunk>; we wrap it so chunks pass
122
+ // through to the customer while accumulating response_text +
123
+ // token counts, then emit the llm_call event at stream-end.
124
+ const isStream = firstArg.stream === true;
125
+ const start = performance.now();
126
+ try {
127
+ const response = await originalChat.apply(this, args);
128
+ if (isStream) {
129
+ return wrapStreamingResponse({
130
+ inner: response,
131
+ client,
132
+ executionId: ctx.executionId,
133
+ eventId,
134
+ sequence,
135
+ model,
136
+ systemText,
137
+ userMessage,
138
+ start,
139
+ budgetTracker: ctx.budgetTracker ?? null,
140
+ });
141
+ }
142
+ const durationMs = Math.round(performance.now() - start);
143
+ const { responseText, inputTokens, outputTokens } = extractResponseFields(response);
144
+ if (ctx.budgetTracker) {
145
+ ctx.budgetTracker.addTokens(inputTokens, outputTokens);
146
+ }
147
+ client.submitEvent(buildOkEvent({
148
+ eventId,
149
+ executionId: ctx.executionId,
150
+ sequence,
151
+ durationMs,
152
+ model,
153
+ systemText,
154
+ userMessage,
155
+ responseText,
156
+ inputTokens,
157
+ outputTokens,
158
+ }));
159
+ return response;
160
+ }
161
+ catch (err) {
162
+ const durationMs = Math.round(performance.now() - start);
163
+ client.submitEvent(buildFailureEvent({
164
+ eventId,
165
+ executionId: ctx.executionId,
166
+ sequence,
167
+ durationMs,
168
+ model,
169
+ systemText,
170
+ userMessage,
171
+ err,
172
+ }));
173
+ // Wave 2.5.3 — intentional omission of the Wave 1.4
174
+ // _maybeEmitThrottlingEvent auto-emit call. The other four
175
+ // instrumentation modules call it here; instrumentOllama does
176
+ // not because Ollama is a local runtime — no per-minute rate
177
+ // limiting, no quota exhaustion. Wave 2.5.2 shipped a
178
+ // regression-guard test asserting classifyOllamaException
179
+ // NEVER returns RATE_LIMITED or QUOTA_EXHAUSTED, so this call
180
+ // would be guaranteed-dead code. instrument_throttling.test.ts
181
+ // contains a paired negative assertion that fails loudly if a
182
+ // future refactor adds the import without removing the guard.
183
+ throw err;
184
+ }
185
+ };
186
+ _patched.add(cls);
187
+ }
188
+ function buildOkEvent(a) {
189
+ return {
190
+ event_id: a.eventId,
191
+ execution_id: a.executionId,
192
+ event_type: EventType.LLM_CALL,
193
+ sequence: a.sequence,
194
+ timestamp: utcNowRfc3339(),
195
+ duration_ms: a.durationMs,
196
+ payload: {
197
+ provider: PROVIDER,
198
+ surface: "chat",
199
+ model: a.model,
200
+ system_prompt: truncate(a.systemText, MAX_SYSTEM),
201
+ user_message: truncate(a.userMessage, MAX_USER_MSG),
202
+ response_text: truncate(a.responseText, MAX_RESPONSE),
203
+ status: "ok",
204
+ input_tokens: a.inputTokens,
205
+ output_tokens: a.outputTokens,
206
+ },
207
+ };
208
+ }
209
+ function buildFailureEvent(a) {
210
+ const errObj = a.err;
211
+ const payload = {
212
+ provider: PROVIDER,
213
+ surface: "chat",
214
+ model: a.model,
215
+ system_prompt: truncate(a.systemText, MAX_SYSTEM),
216
+ user_message: truncate(a.userMessage, MAX_USER_MSG),
217
+ status: "failed",
218
+ error_class: classifyOllamaException(a.err),
219
+ exception_type: errObj?.name ?? "Error",
220
+ exception_message: truncate(String(errObj?.message ?? a.err), MAX_EXC_MSG),
221
+ };
222
+ if (typeof errObj?.status_code === "number") {
223
+ payload.http_status = errObj.status_code;
224
+ }
225
+ return {
226
+ event_id: a.eventId,
227
+ execution_id: a.executionId,
228
+ event_type: EventType.LLM_CALL,
229
+ sequence: a.sequence,
230
+ timestamp: utcNowRfc3339(),
231
+ duration_ms: a.durationMs,
232
+ payload,
233
+ };
234
+ }
235
+ function extractFirstSystemMessage(messages) {
236
+ for (const msg of messages) {
237
+ if (msg?.role !== "system")
238
+ continue;
239
+ return contentToText(msg.content);
240
+ }
241
+ return "";
242
+ }
243
+ function extractLastUserMessage(messages) {
244
+ for (let i = messages.length - 1; i >= 0; i--) {
245
+ const msg = messages[i];
246
+ if (msg?.role !== "user")
247
+ continue;
248
+ return contentToText(msg.content);
249
+ }
250
+ return "";
251
+ }
252
+ function contentToText(content) {
253
+ if (typeof content === "string")
254
+ return content;
255
+ if (Array.isArray(content)) {
256
+ return content
257
+ .map((b) => (typeof b?.text === "string" ? b.text : ""))
258
+ .join("\n");
259
+ }
260
+ return "";
261
+ }
262
+ function extractResponseFields(response) {
263
+ const responseText = response?.message?.content ?? "";
264
+ const inputTokens = numberOrZero(response?.prompt_eval_count);
265
+ const outputTokens = numberOrZero(response?.eval_count);
266
+ return { responseText, inputTokens, outputTokens };
267
+ }
268
+ function numberOrZero(value) {
269
+ if (typeof value === "number" && Number.isFinite(value))
270
+ return value;
271
+ if (typeof value === "string") {
272
+ const parsed = Number(value);
273
+ return Number.isFinite(parsed) ? parsed : 0;
274
+ }
275
+ return 0;
276
+ }
277
+ function truncate(s, limit) {
278
+ if (!s)
279
+ return "";
280
+ if (s.length <= limit)
281
+ return s;
282
+ return s.slice(0, limit) + "...[truncated]";
283
+ }
284
+ /** Wraps an Ollama AsyncIterable streaming response so chunks pass
285
+ * through to the customer while we accumulate response_text + tokens
286
+ * for the final llm_call event emission. The returned object is an
287
+ * AsyncIterable (Symbol.asyncIterator) the customer can `for await`
288
+ * over identically to the original response. */
289
+ function wrapStreamingResponse(args) {
290
+ const state = { textParts: [], inputTokens: 0, outputTokens: 0 };
291
+ let emitted = false;
292
+ const emitSuccess = () => {
293
+ if (emitted)
294
+ return;
295
+ emitted = true;
296
+ const durationMs = Math.round(performance.now() - args.start);
297
+ const responseText = state.textParts.join("");
298
+ if (args.budgetTracker) {
299
+ args.budgetTracker.addTokens(state.inputTokens, state.outputTokens);
300
+ }
301
+ args.client.submitEvent({
302
+ event_id: args.eventId,
303
+ execution_id: args.executionId,
304
+ event_type: EventType.LLM_CALL,
305
+ sequence: args.sequence,
306
+ timestamp: utcNowRfc3339(),
307
+ duration_ms: durationMs,
308
+ payload: {
309
+ provider: PROVIDER,
310
+ surface: "chat",
311
+ model: args.model,
312
+ system_prompt: truncate(args.systemText, MAX_SYSTEM),
313
+ user_message: truncate(args.userMessage, MAX_USER_MSG),
314
+ response_text: truncate(responseText, MAX_RESPONSE),
315
+ status: "ok",
316
+ input_tokens: state.inputTokens,
317
+ output_tokens: state.outputTokens,
318
+ streaming: true,
319
+ },
320
+ });
321
+ };
322
+ const emitFailure = (err) => {
323
+ if (emitted)
324
+ return;
325
+ emitted = true;
326
+ const durationMs = Math.round(performance.now() - args.start);
327
+ args.client.submitEvent(buildFailureEvent({
328
+ eventId: args.eventId,
329
+ executionId: args.executionId,
330
+ sequence: args.sequence,
331
+ durationMs,
332
+ model: args.model,
333
+ systemText: args.systemText,
334
+ userMessage: args.userMessage,
335
+ err,
336
+ }));
337
+ };
338
+ return {
339
+ [Symbol.asyncIterator]() {
340
+ const inner = args.inner[Symbol.asyncIterator]();
341
+ return {
342
+ async next() {
343
+ try {
344
+ const result = await inner.next();
345
+ if (result.done) {
346
+ emitSuccess();
347
+ return result;
348
+ }
349
+ accumulateChunk(result.value, state);
350
+ return result;
351
+ }
352
+ catch (err) {
353
+ emitFailure(err);
354
+ throw err;
355
+ }
356
+ },
357
+ };
358
+ },
359
+ };
360
+ }
361
+ /** Apply one streaming chunk to the wrapper's state. Defensive: any
362
+ * extraction failure degrades silently so a single malformed chunk
363
+ * does not break stream-level aggregation. */
364
+ function accumulateChunk(chunk, state) {
365
+ try {
366
+ const content = chunk?.message?.content;
367
+ if (typeof content === "string" && content.length > 0) {
368
+ state.textParts.push(content);
369
+ }
370
+ if (chunk?.done) {
371
+ state.inputTokens = numberOrZero(chunk.prompt_eval_count);
372
+ state.outputTokens = numberOrZero(chunk.eval_count);
373
+ }
374
+ }
375
+ catch {
376
+ // Swallow — one bad chunk shouldn't break stream-level aggregation.
377
+ }
378
+ }
379
+ // Exported helpers for unit tests. Not part of the public API; tests
380
+ // import these to verify field translation and message extraction.
381
+ export const _testing = {
382
+ PROVIDER,
383
+ extractFirstSystemMessage,
384
+ extractLastUserMessage,
385
+ contentToText,
386
+ extractResponseFields,
387
+ numberOrZero,
388
+ truncate,
389
+ /** Resets the one-time streaming-warning flag so tests can assert
390
+ * the warning fires on first stream call after a reset. Not part
391
+ * of the public API. */
392
+ resetStreamingWarningGuard: () => {
393
+ _streamingWarningEmitted = false;
394
+ },
395
+ /** The streaming-warning function itself, exposed so unit tests
396
+ * can verify the once-per-process contract without spinning up a
397
+ * full wrap() + chat() integration scaffold. Not part of the
398
+ * public API. */
399
+ maybeWarnStreamingUnsupported,
400
+ /** Wave 2.5.8 — exposed for unit tests of the streaming chunk-
401
+ * aggregation logic. Not part of the public API. */
402
+ accumulateChunk,
403
+ };
404
+ //# sourceMappingURL=ollama_integration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ollama_integration.js","sourceRoot":"","sources":["../src/ollama_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AACnE,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAS,SAAS,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE9D,MAAM,UAAU,GAAG,IAAI,CAAC;AACxB,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,WAAW,GAAG,GAAG,CAAC;AAExB;;;+CAG+C;AAC/C,MAAM,QAAQ,GAAG,QAAQ,CAAC;AAE1B,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAU,CAAC;AAEvC;;;;;gBAKgB;AAChB,IAAI,wBAAwB,GAAG,KAAK,CAAC;AAErC,SAAS,6BAA6B;IACpC,IAAI,wBAAwB;QAAE,OAAO;IACrC,wBAAwB,GAAG,IAAI,CAAC;IAChC,OAAO,CAAC,IAAI,CACV,mEAAmE;QACjE,iEAAiE;QACjE,kEAAkE;QAClE,kEAAkE;QAClE,+DAA+D,CAClE,CAAC;AACJ,CAAC;AA0BD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,WAA6B;IAE7B,IAAI,GAAG,GAAG,WAAW,CAAC;IAEtB,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,IAAI,C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AG,YAAY,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACtD,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,oEAAoE;IACtE,CAAC;AACH,CAAC;AAED,qEAAqE;AACrE,mEAAmE;AACnE,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,QAAQ;IACR,yBAAyB;IACzB,sBAAsB;IACtB,aAAa;IACb,qBAAqB;IACrB,YAAY;IACZ,QAAQ;IACR;;4BAEwB;IACxB,0BAA0B,EAAE,GAAS,EAAE;QACrC,wBAAwB,GAAG,KAAK,CAAC;IACnC,CAAC;IACD;;;qBAGiB;IACjB,6BAA6B;IAC7B;wDACoD;IACpD,eAAe;CAChB,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * OpenAI SDK monkey-patch — auto-emit llm_call events for every
3
+ * chat.completions.create() and responses.create() call inside a
4
+ * wrap()'d execution.
5
+ *
6
+ * Mirrors instrumentAnthropic shape (same fail-open semantics, same
7
+ * canonical error vocabulary, same idempotency guarantee), with
8
+ * OpenAI-shape adjustments:
9
+ *
10
+ * - System prompt lives inside `messages` (role: "system"), not
11
+ * a separate `system=` parameter like Anthropic.
12
+ * - Response text is `choices[0].message.content` for chat
13
+ * completions, OR `output_text` for the Responses API.
14
+ * - Token names: `prompt_tokens` / `completion_tokens` for chat,
15
+ * `input_tokens` / `output_tokens` for Responses — both
16
+ * normalized to the canonical input_tokens/output_tokens on
17
+ * payload.
18
+ *
19
+ * Patches two surfaces:
20
+ * - openai.OpenAI.Chat.Completions.prototype.create
21
+ * - openai.OpenAI.Responses.prototype.create (openai>=4.50)
22
+ *
23
+ * Out of scope for v1 (filed as #271 follow-ups):
24
+ * - AsyncOpenAI patching gap shared with instrumentAnthropic
25
+ * - Streaming responses
26
+ * - Embeddings / image generation / audio endpoints
27
+ *
28
+ * Dependency injection: instrumentOpenAI accepts optional
29
+ * completionsClass + responsesClass arguments so this code path
30
+ * is testable without installing the actual openai package.
31
+ */
32
+ export interface CompletionsClassLike {
33
+ prototype: {
34
+ create: (...args: any[]) => Promise<any>;
35
+ };
36
+ }
37
+ export interface ResponsesClassLike {
38
+ prototype: {
39
+ create: (...args: any[]) => Promise<any>;
40
+ };
41
+ }
42
+ /**
43
+ * Patch the OpenAI SDK's chat.completions.create and
44
+ * responses.create to emit llm_call events. Returns true if at
45
+ * least one surface was patched (or already patched on a prior
46
+ * call). Returns false if neither could be located AND the openai
47
+ * package isn't installed.
48
+ */
49
+ export declare function instrumentOpenAI(completionsClass?: CompletionsClassLike, responsesClass?: ResponsesClassLike, opts?: {
50
+ embeddingsClass?: EndpointClassLike;
51
+ imagesClass?: EndpointClassLike;
52
+ audioTranscriptionsClass?: EndpointClassLike;
53
+ audioTranslationsClass?: EndpointClassLike;
54
+ audioSpeechClass?: EndpointClassLike;
55
+ }): Promise<boolean>;
56
+ interface EndpointClassLike {
57
+ prototype: {
58
+ create: (...args: any[]) => Promise<any>;
59
+ };
60
+ }
61
+ export declare const patchEmbeddings: (cls: EndpointClassLike) => void;
62
+ export declare const patchImages: (cls: EndpointClassLike) => void;
63
+ export declare const patchAudioTranscriptions: (cls: EndpointClassLike) => void;
64
+ export declare const patchAudioTranslations: (cls: EndpointClassLike) => void;
65
+ export declare const patchAudioSpeech: (cls: EndpointClassLike) => void;
66
+ export {};
67
+ //# sourceMappingURL=openai_integration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai_integration.d.ts","sourceRoot":"","sources":["../src/openai_integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAyBH,MAAM,WAAW,oBAAoB;IAEnC,SAAS,EAAE;QAAE,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAA;KAAE,CAAC;CACzD;AACD,MAAM,WAAW,kBAAkB;IAEjC,SAAS,EAAE;QAAE,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EA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