Repository navigation
Expand file tree
/
Copy patheve.ts
More file actions
83 lines (77 loc) · 3.65 KB
/
Copy patheve.ts
File metadata and controls
83 lines (77 loc) · 3.65 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { type Integration, setConversationId } from '@sentry/core';
import { eveIntegration, eveTracePolicy } from '@sentry/server-utils';
import { init } from './sdk';
import type { NodeOptions } from './types';
/**
* Options for {@link eveInstrumentation}: every `Sentry.init` option, plus how to derive the
* conversation id.
*/
export interface EveInstrumentationOptions extends NodeOptions {
/**
* Derive the Sentry conversation id from the eve session. Defaults to the durable session id
* (`session.id`), which is stable across every turn of a session and so groups them into one
* conversation. Return `null`/`undefined` to skip setting it for a turn.
*/
getConversationId?: (context: { session: { id: string } }) => string | null | undefined;
}
/**
* The `defineInstrumentation` provider shape this returns. Typed structurally rather than importing
* from `eve`, so the SDK carries no dependency on the framework — the shape is checked at the
* `defineInstrumentation(...)` call site in the user's app instead.
*/
interface EveInstrumentationProvider {
setup: () => void;
tracePolicy: () => { emit: true; recordInputs: boolean; recordOutputs: boolean };
events: {
'turn.started': (event: { sessionId: string }) => void;
'step.attempt.started': (event: { scope: { sessionId: string } }) => void;
'model.started': (event: { scope: { sessionId: string } }) => void;
};
}
/**
* All-in-one Sentry setup for an [eve](https://eve.dev) agent, wired into a single
* `agent/instrumentation/*.ts` provider file.
*
* @example
* ```ts
* // agent/instrumentation/sentry.ts
* import * as Sentry from '@sentry/node';
* import { defineInstrumentation } from 'eve/instrumentation';
*
* export default defineInstrumentation(Sentry.eveInstrumentation({ dsn: '__DSN__' }));
* ```
*
* It:
* - runs `Sentry.init` with the given options at server startup (`setup`), adding {@link eveIntegration};
* - asks eve for full gen_ai results ({@link eveTracePolicy}); Sentry filters content by `dataCollection.genAI`
* when it builds spans. On eve >= 1.0.0-rc.10 this also sets the AI SDK's `recordInputs`/`recordOutputs` flags,
* which apply to every AI SDK telemetry integration in the app;
* - tags each turn's AI spans with a Sentry conversation id via `turn.started` and `step.attempt.started`
* (eve < 0.84) / `model.started` (eve >= 0.84) events — the instrumentation-provider equivalent of `eveConversationHook`. Provider event handlers
* receive the session id on the event (`event.sessionId` / `event.scope.sessionId`), unlike hook
* handlers, which read it from their context.
*/
export function eveInstrumentation(options: EveInstrumentationOptions = {}): EveInstrumentationProvider {
const { getConversationId, ...initOptions } = options;
const setConversationIdFromSession = (sessionId: string): void => {
setConversationId(getConversationId ? getConversationId({ session: { id: sessionId } }) : sessionId);
};
return {
setup() {
init({ ...initOptions, integrations: withEveIntegration(initOptions.integrations) });
},
tracePolicy: eveTracePolicy,
events: {
'turn.started': event => setConversationIdFromSession(event.sessionId),
'step.attempt.started': event => setConversationIdFromSession(event.scope.sessionId),
'model.started': event => setConversationIdFromSession(event.scope.sessionId),
},
};
}
function withEveIntegration(integrations: NodeOptions['integrations']): NodeOptions['integrations'] {
const eve = eveIntegration();
if (typeof integrations === 'function') {
return (defaults: Integration[]) => [...integrations(defaults), eve];
}
return [...(integrations ?? []), eve];
}