openlayer 0.13.0 → 0.13.2

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,306 @@
1
+ // Make imports optional with try/catch
2
+ let BedrockAgentRuntimeClient: any;
3
+ let InvokeAgentCommand: any;
4
+ let InvokeAgentCommandInput: any;
5
+ let InvokeAgentCommandOutput: any;
6
+
7
+ try {
8
+ const bedrockModule = require('@aws-sdk/client-bedrock-agent-runtime');
9
+ BedrockAgentRuntimeClient = bedrockModule.BedrockAgentRuntimeClient;
10
+ InvokeAgentCommand = bedrockModule.InvokeAgentCommand;
11
+ InvokeAgentCommandInput = bedrockModule.InvokeAgentCommandInput;
12
+ InvokeAgentCommandOutput = bedrockModule.InvokeAgentCommandOutput;
13
+ } catch (error) {
14
+ // AWS SDK not available
15
+ }
16
+
17
+ import { addChatCompletionStepToTrace } from '../tracing/tracer';
18
+
19
+ export function traceBedrockAgent(client: any): any {
20
+ if (!BedrockAgentRuntimeClient || !InvokeAgentCommand) {
21
+ throw new Error(
22
+ 'AWS SDK for Bedrock Agent Runtime is not installed. Please install it with: npm install @aws-sdk/client-bedrock-agent-runtime',
23
+ );
24
+ }
25
+
26
+ const originalSend = client.send.bind(client);
27
+
28
+ client.send = async function (this: any, command: any, options?: any): Promise<any> {
29
+ // Only trace InvokeAgentCommand
30
+ if (!(command instanceof InvokeAgentCommand)) {
31
+ return originalSend(command, options);
32
+ }
33
+
34
+ const startTime = performance.now();
35
+ const input = command.input;
36
+
37
+ try {
38
+ // Call the original send method
39
+ const response = await originalSend(command, options);
40
+
41
+ if (!response.completion) {
42
+ throw new Error('Completion is undefined');
43
+ }
44
+
45
+ // Create a traced async iterator that preserves the original
46
+ const tracedCompletion = createTracedCompletion(response.completion, input, startTime);
47
+
48
+ // Return the response with the traced completion
49
+ return {
50
+ ...response,
51
+ completion: tracedCompletion,
52
+ };
53
+ } catch (error) {
54
+ console.error('Failed to trace the Bedrock agent invocation with Openlayer', error);
55
+ throw error;
56
+ }
57
+ };
58
+
59
+ return client;
60
+ }
61
+
62
+ // Create a traced completion that collects data while yielding original events
63
+ function createTracedCompletion(
64
+ originalCompletion: AsyncIterable<any>,
65
+ input: any,
66
+ startTime: number,
67
+ ): AsyncIterable<any> {
68
+ return {
69
+ async *[Symbol.asyncIterator]() {
70
+ let firstTokenTime: number | undefined;
71
+ let totalTokens = 0;
72
+ let promptTokens = 0;
73
+ let completionTokens = 0;
74
+ let collectedOutput = '';
75
+ const rawOutputChunks: any[] = [];
76
+ let agentModel: string | null = null;
77
+ let citations: any[] = [];
78
+ let traceData: any[] = [];
79
+ let chunkCount = 0;
80
+
81
+ try {
82
+ for await (const chunkEvent of originalCompletion) {
83
+ // Yield first - ensure user gets data immediately
84
+ yield chunkEvent;
85
+
86
+ // Then collect tracing data
87
+ if (chunkCount === 0) {
88
+ firstTokenTime = performance.now();
89
+ }
90
+ chunkCount++;
91
+
92
+ // Handle chunk events
93
+ if (chunkEvent.chunk) {
94
+ const chunk = chunkEvent.chunk;
95
+ rawOutputChunks.push(chunk);
96
+
97
+ if (chunk.bytes) {
98
+ const decodedResponse = new TextDecoder('utf-8').decode(chunk.bytes);
99
+ collectedOutput += decodedResponse;
100
+ completionTokens += 1;
101
+ }
102
+
103
+ if (chunk.attribution && chunk.attribution.citations) {
104
+ citations.push(...chunk.attribution.citations);
105
+ }
106
+ }
107
+
108
+ // Handle trace events
109
+ if (chunkEvent.trace) {
110
+ traceData.push(chunkEvent.trace);
111
+
112
+ if (chunkEvent.trace.trace) {
113
+ const trace = chunkEvent.trace.trace;
114
+
115
+ // Extract tokens and model info
116
+ if (
117
+ 'orchestrationTrace' in trace &&
118
+ trace.orchestrationTrace?.modelInvocationOutput?.metadata?.usage
119
+ ) {
120
+ const usage = trace.orchestrationTrace.modelInvocationOutput.metadata.usage;
121
+ promptTokens += usage.inputTokens || 0;
122
+ completionTokens += usage.outputTokens || 0;
123
+ }
124
+
125
+ if (
126
+ 'orchestrationTrace' in trace &&
127
+ trace.orchestrationTrace?.modelInvocationInput?.foundationModel
128
+ ) {
129
+ agentModel = trace.orchestrationTrace.modelInvocationInput.foundationModel;
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ // After the stream is complete, send trace data
136
+ const endTime = performance.now();
137
+ totalTokens = promptTokens + completionTokens;
138
+
139
+ // Send trace data to Openlayer
140
+ const inputs = extractInputs(input, traceData);
141
+ const metadata: Record<string, any> = {
142
+ agentId: input.agentId,
143
+ agentAliasId: input.agentAliasId,
144
+ sessionId: input.sessionId,
145
+ timeToFirstToken: firstTokenTime ? firstTokenTime - startTime : null,
146
+ };
147
+
148
+ if (citations.length > 0) {
149
+ metadata['citations'] = citations;
150
+ }
151
+
152
+ const reasoning = extractReasoning(traceData);
153
+ if (reasoning && reasoning.length > 0) {
154
+ metadata['reasoning'] = reasoning;
155
+ }
156
+
157
+ if (input.sessionState) {
158
+ metadata['sessionState'] = {
159
+ hasSessionAttributes: !!input.sessionState.sessionAttributes,
160
+ hasPromptSessionAttributes: !!input.sessionState.promptSessionAttributes,
161
+ hasFiles: !!input.sessionState.files && input.sessionState.files.length > 0,
162
+ hasKnowledgeBaseConfigurations:
163
+ !!input.sessionState.knowledgeBaseConfigurations &&
164
+ input.sessionState.knowledgeBaseConfigurations.length > 0,
165
+ };
166
+ }
167
+
168
+ const traceStepData = {
169
+ name: 'AWS Bedrock Agent Invocation',
170
+ inputs: inputs,
171
+ output: collectedOutput,
172
+ latency: endTime - startTime,
173
+ tokens: totalTokens > 0 ? totalTokens : null,
174
+ promptTokens: promptTokens > 0 ? promptTokens : null,
175
+ completionTokens: completionTokens > 0 ? completionTokens : null,
176
+ model: agentModel || `${input.agentId}:${input.agentAliasId}`,
177
+ modelParameters: extractModelParameters(input),
178
+ rawOutput: JSON.stringify(rawOutputChunks, null, 2),
179
+ metadata: metadata,
180
+ provider: 'Bedrock',
181
+ };
182
+
183
+ addChatCompletionStepToTrace(traceStepData);
184
+ } catch (error) {
185
+ console.error('Error in traced completion:', error);
186
+ // Don't rethrow - we don't want tracing errors to break the user's stream
187
+ }
188
+ },
189
+ };
190
+ }
191
+
192
+ function extractInputs(input: any, traceData: any[]): Record<string, any> {
193
+ const inputs: Record<string, any> = {};
194
+
195
+ // Build the prompt in OpenAI-compatible format
196
+ const prompt: Array<{ role: string; content: string }> = [];
197
+
198
+ // Add the main user message
199
+ if (input.inputText) {
200
+ prompt.push({
201
+ role: 'user',
202
+ content: input.inputText,
203
+ });
204
+ }
205
+
206
+ // Add conversation history if present
207
+ if (input.sessionState?.conversationHistory?.messages) {
208
+ for (const message of input.sessionState.conversationHistory.messages) {
209
+ const content =
210
+ message.content ?
211
+ message.content.map((block: any) => ('text' in block ? block.text || '' : '')).join('')
212
+ : '';
213
+
214
+ const role = message.role || 'user';
215
+
216
+ prompt.unshift({
217
+ role: role,
218
+ content: content,
219
+ });
220
+ }
221
+ }
222
+
223
+ // Extract system prompt from trace data if available
224
+ const systemPrompt = extractSystemPrompt(traceData);
225
+ if (systemPrompt) {
226
+ prompt.unshift({
227
+ role: 'system',
228
+ content: systemPrompt,
229
+ });
230
+ }
231
+
232
+ inputs['prompt'] = prompt;
233
+
234
+ // Add additional context as separate fields
235
+ if (input.sessionState?.sessionAttributes) {
236
+ inputs['sessionAttributes'] = input.sessionState.sessionAttributes;
237
+ }
238
+
239
+ if (input.sessionState?.promptSessionAttributes) {
240
+ inputs['promptSessionAttributes'] = input.sessionState.promptSessionAttributes;
241
+ }
242
+
243
+ if (input.sessionState?.files && input.sessionState.files.length > 0) {
244
+ inputs['files'] = input.sessionState.files.map((file: any) => ({
245
+ name: file.name,
246
+ useCase: file.useCase,
247
+ sourceType: file.source?.sourceType,
248
+ }));
249
+ }
250
+
251
+ return inputs;
252
+ }
253
+
254
+ function extractSystemPrompt(traceData: any[]): string | null {
255
+ for (const trace of traceData) {
256
+ if (trace.trace?.orchestrationTrace?.modelInvocationInput?.text) {
257
+ try {
258
+ const parsed = JSON.parse(trace.trace.orchestrationTrace.modelInvocationInput.text);
259
+ if (parsed.system) {
260
+ return parsed.system;
261
+ }
262
+ } catch (e) {
263
+ // If parsing fails, continue
264
+ }
265
+ }
266
+ }
267
+ return null;
268
+ }
269
+
270
+ function extractReasoning(traceData: any[]): string[] | undefined {
271
+ const reasoning: string[] = [];
272
+
273
+ for (const trace of traceData) {
274
+ if (trace.trace?.orchestrationTrace?.rationale?.text) {
275
+ reasoning.push(trace.trace.orchestrationTrace.rationale.text);
276
+ }
277
+ }
278
+
279
+ return reasoning.length > 0 ? reasoning : undefined;
280
+ }
281
+
282
+ function extractModelParameters(input: any): Record<string, any> {
283
+ const params: Record<string, any> = {};
284
+
285
+ if (input.enableTrace !== undefined) {
286
+ params['enableTrace'] = input.enableTrace;
287
+ }
288
+
289
+ if (input.endSession !== undefined) {
290
+ params['endSession'] = input.endSession;
291
+ }
292
+
293
+ if (input.bedrockModelConfigurations) {
294
+ params['bedrockModelConfigurations'] = input.bedrockModelConfigurations;
295
+ }
296
+
297
+ if (input.streamingConfigurations) {
298
+ params['streamingConfigurations'] = input.streamingConfigurations;
299
+ }
300
+
301
+ if (input.promptCreationConfigurations) {
302
+ params['promptCreationConfigurations'] = input.promptCreationConfigurations;
303
+ }
304
+
305
+ return params;
306
+ }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.13.0'; // x-release-please-version
1
+ export const VERSION = '0.13.2'; // x-release-please-version
package/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export declare const VERSION = "0.13.0";
1
+ export declare const VERSION = "0.13.2";
2
2
  //# sourceMappingURL=version.d.ts.map
package/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.VERSION = void 0;
4
- exports.VERSION = '0.13.0'; // x-release-please-version
4
+ exports.VERSION = '0.13.2'; // x-release-please-version
5
5
  //# sourceMappingURL=version.js.map
package/version.mjs CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '0.13.0'; // x-release-please-version
1
+ export const VERSION = '0.13.2'; // x-release-please-version
2
2
  //# sourceMappingURL=version.mjs.map