-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathutils.ts
More file actions
42 lines (35 loc) · 1.46 KB
/
utils.ts
File metadata and controls
42 lines (35 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { captureException } from '../../exports';
import { SPAN_STATUS_ERROR } from '../../tracing';
import type { Span } from '../../types-hoist/span';
import { ANTHROPIC_AI_INSTRUMENTED_METHODS } from './constants';
import type { AnthropicAiInstrumentedMethod, AnthropicAiResponse } from './types';
/**
* Check if a method path should be instrumented
*/
export function shouldInstrument(methodPath: string): methodPath is AnthropicAiInstrumentedMethod {
return ANTHROPIC_AI_INSTRUMENTED_METHODS.includes(methodPath as AnthropicAiInstrumentedMethod);
}
/**
* Capture error information from the response
* @see https://docs.anthropic.com/en/api/errors#error-shapes
*/
export function handleResponseError(span: Span, response: AnthropicAiResponse): void {
if (response.error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: response.error.type || 'internal_error' });
captureException(response.error, {
mechanism: {
handled: false,
type: 'auto.ai.anthropic.anthropic_error',
},
});
}
}
/**
* Include the system prompt in the messages list, if available
*/
export function messagesFromParams(params: Record<string, unknown>): unknown[] {
const { system, messages } = params;
const systemMessages = typeof system === 'string' ? [{ role: 'system', content: params.system }] : [];
const userMessages = Array.isArray(messages) ? messages : messages != null ? [messages] : [];
return [...systemMessages, ...userMessages];
}