Skip to content

Commit cfd4bee

Browse files
committed
Improve typing so we don't walk over openai's types when using the
instrumentation directly in cloudflare/vercel-edge
1 parent 6e6f43d commit cfd4bee

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

dev-packages/cloudflare-integration-tests/suites/tracing/openai/mocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { OpenAiClient } from '@sentry/core';
22

33
export class MockOpenAi implements OpenAiClient {
4-
public chat?: Record<string, unknown> | undefined;
5-
public responses?: {
4+
public chat: Record<string, unknown>;
5+
public responses: {
66
create: (...args: unknown[]) => Promise<unknown>;
77
};
88

dev-packages/cloudflare-integration-tests/suites/tracing/openai/test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { expect, it } from 'vitest';
22
import { createRunner } from '../../../runner';
33

4+
// These tests are not exhaustive because the instrumentation is
5+
// already tested in the node integration tests and we merely
6+
// want to test that the instrumentation does not break in our
7+
// cloudflare SDK.
8+
49
it('traces a basic chat completion request', async () => {
510
const runner = createRunner(__dirname)
611
.ignore('event')

packages/core/src/utils/openai/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import type {
2424
ChatCompletionChunk,
2525
InstrumentedMethod,
2626
OpenAiChatCompletionObject,
27-
OpenAiClient,
2827
OpenAiIntegration,
2928
OpenAiOptions,
3029
OpenAiResponse,
@@ -294,7 +293,7 @@ function instrumentMethod<T extends unknown[], R>(
294293
/**
295294
* Create a deep proxy for OpenAI client instrumentation
296295
*/
297-
function createDeepProxy(target: object, currentPath = '', options?: OpenAiOptions): OpenAiClient {
296+
function createDeepProxy<T extends object>(target: T, currentPath = '', options?: OpenAiOptions): T {
298297
return new Proxy(target, {
299298
get(obj: object, prop: string): unknown {
300299
const value = (obj as Record<string, unknown>)[prop];
@@ -316,13 +315,13 @@ function createDeepProxy(target: object, currentPath = '', options?: OpenAiOptio
316315

317316
return value;
318317
},
319-
});
318+
}) as T;
320319
}
321320

322321
/**
323322
* Instrument an OpenAI client with Sentry tracing
324323
* Can be used across Node.js, Cloudflare Workers, and Vercel Edge
325324
*/
326-
export function instrumentOpenAiClient(client: OpenAiClient, options?: OpenAiOptions): OpenAiClient {
325+
export function instrumentOpenAiClient<T extends object>(client: T, options?: OpenAiOptions): T {
327326
return createDeepProxy(client, '', options);
328327
}

packages/core/src/utils/openai/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ export type AttributeValue =
1515

1616
export interface OpenAiOptions {
1717
/**
18-
* Enable or disable input recording. Enabled if `sendDefaultPii` is `true`
18+
* Enable or disable input recording.
1919
*/
2020
recordInputs?: boolean;
2121
/**
22-
* Enable or disable output recording. Enabled if `sendDefaultPii` is `true`
22+
* Enable or disable output recording.
2323
*/
2424
recordOutputs?: boolean;
2525
}
2626

2727
export interface OpenAiClient {
28-
responses?: {
28+
responses: {
2929
create: (...args: unknown[]) => Promise<unknown>;
3030
};
31-
chat?: {
31+
chat: {
3232
completions?: {
3333
create: (...args: unknown[]) => Promise<unknown>;
3434
};

0 commit comments

Comments
 (0)