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