@traceai/codex-sdk 0.0.0-stage → 0.1.0
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.
- package/README.md +164 -2
- package/dist/esm/attributes.d.ts +101 -0
- package/dist/esm/attributes.d.ts.map +1 -0
- package/dist/esm/attributes.js +104 -0
- package/dist/esm/attributes.js.map +1 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/instrumentation.d.ts +81 -0
- package/dist/esm/instrumentation.d.ts.map +1 -0
- package/dist/esm/instrumentation.js +186 -0
- package/dist/esm/instrumentation.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/spans.d.ts +152 -0
- package/dist/esm/spans.d.ts.map +1 -0
- package/dist/esm/spans.js +678 -0
- package/dist/esm/spans.js.map +1 -0
- package/dist/esm/threadWrapper.d.ts +35 -0
- package/dist/esm/threadWrapper.d.ts.map +1 -0
- package/dist/esm/threadWrapper.js +186 -0
- package/dist/esm/threadWrapper.js.map +1 -0
- package/dist/esm/types.d.ts +123 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +10 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/esnext/attributes.d.ts +101 -0
- package/dist/esnext/attributes.d.ts.map +1 -0
- package/dist/esnext/attributes.js +104 -0
- package/dist/esnext/attributes.js.map +1 -0
- package/dist/esnext/index.d.ts +8 -0
- package/dist/esnext/index.d.ts.map +1 -0
- package/dist/esnext/index.js +5 -0
- package/dist/esnext/index.js.map +1 -0
- package/dist/esnext/instrumentation.d.ts +81 -0
- package/dist/esnext/instrumentation.d.ts.map +1 -0
- package/dist/esnext/instrumentation.js +191 -0
- package/dist/esnext/instrumentation.js.map +1 -0
- package/dist/esnext/spans.d.ts +152 -0
- package/dist/esnext/spans.d.ts.map +1 -0
- package/dist/esnext/spans.js +688 -0
- package/dist/esnext/spans.js.map +1 -0
- package/dist/esnext/threadWrapper.d.ts +35 -0
- package/dist/esnext/threadWrapper.d.ts.map +1 -0
- package/dist/esnext/threadWrapper.js +183 -0
- package/dist/esnext/threadWrapper.js.map +1 -0
- package/dist/esnext/types.d.ts +123 -0
- package/dist/esnext/types.d.ts.map +1 -0
- package/dist/esnext/types.js +10 -0
- package/dist/esnext/types.js.map +1 -0
- package/dist/esnext/version.d.ts +2 -0
- package/dist/esnext/version.d.ts.map +1 -0
- package/dist/esnext/version.js +2 -0
- package/dist/esnext/version.js.map +1 -0
- package/dist/src/attributes.d.ts +101 -0
- package/dist/src/attributes.d.ts.map +1 -0
- package/dist/src/attributes.js +107 -0
- package/dist/src/attributes.js.map +1 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/instrumentation.d.ts +81 -0
- package/dist/src/instrumentation.d.ts.map +1 -0
- package/dist/src/instrumentation.js +213 -0
- package/dist/src/instrumentation.js.map +1 -0
- package/dist/src/spans.d.ts +152 -0
- package/dist/src/spans.d.ts.map +1 -0
- package/dist/src/spans.js +698 -0
- package/dist/src/spans.js.map +1 -0
- package/dist/src/threadWrapper.d.ts +35 -0
- package/dist/src/threadWrapper.d.ts.map +1 -0
- package/dist/src/threadWrapper.js +206 -0
- package/dist/src/threadWrapper.js.map +1 -0
- package/dist/src/types.d.ts +123 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +11 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/version.d.ts +2 -0
- package/dist/src/version.d.ts.map +1 -0
- package/dist/src/version.js +5 -0
- package/dist/src/version.js.map +1 -0
- package/package.json +75 -3
- package/src/attributes.ts +113 -0
- package/src/index.ts +27 -0
- package/src/instrumentation.ts +215 -0
- package/src/spans.ts +729 -0
- package/src/threadWrapper.ts +200 -0
- package/src/types.ts +113 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Span names and attribute keys for Codex SDK spans.
|
|
3
|
+
*
|
|
4
|
+
* Standard keys (`fi.span.kind`, `session.id`, `input.value`, token counts...)
|
|
5
|
+
* come from `@traceai/fi-semantic-conventions` so the Future AGI collector
|
|
6
|
+
* promotes them. `codex.*` keys carry the SDK's own structural fields.
|
|
7
|
+
*/
|
|
8
|
+
import { FISpanKind, SemanticConventions } from "@traceai/fi-semantic-conventions";
|
|
9
|
+
|
|
10
|
+
export const SpanNames = {
|
|
11
|
+
TURN: "codex.turn",
|
|
12
|
+
AGENT_MESSAGE: "codex.agent_message",
|
|
13
|
+
REASONING: "codex.reasoning",
|
|
14
|
+
COMMAND_EXECUTION: "codex.command_execution",
|
|
15
|
+
FILE_CHANGE: "codex.file_change",
|
|
16
|
+
MCP_TOOL_CALL: "codex.mcp_tool_call",
|
|
17
|
+
WEB_SEARCH: "codex.web_search",
|
|
18
|
+
TODO_LIST: "codex.todo_list",
|
|
19
|
+
ERROR: "codex.error",
|
|
20
|
+
} as const;
|
|
21
|
+
|
|
22
|
+
/** SDK item type -> span name and Future AGI span kind. */
|
|
23
|
+
export const ITEM_SPANS: Record<string, { name: string; kind: FISpanKind; tool: boolean }> = {
|
|
24
|
+
agent_message: { name: SpanNames.AGENT_MESSAGE, kind: FISpanKind.LLM, tool: false },
|
|
25
|
+
reasoning: { name: SpanNames.REASONING, kind: FISpanKind.CHAIN, tool: false },
|
|
26
|
+
command_execution: { name: SpanNames.COMMAND_EXECUTION, kind: FISpanKind.TOOL, tool: true },
|
|
27
|
+
file_change: { name: SpanNames.FILE_CHANGE, kind: FISpanKind.TOOL, tool: true },
|
|
28
|
+
mcp_tool_call: { name: SpanNames.MCP_TOOL_CALL, kind: FISpanKind.TOOL, tool: true },
|
|
29
|
+
web_search: { name: SpanNames.WEB_SEARCH, kind: FISpanKind.TOOL, tool: true },
|
|
30
|
+
todo_list: { name: SpanNames.TODO_LIST, kind: FISpanKind.CHAIN, tool: false },
|
|
31
|
+
// Non-fatal: status UNSET with codex.warning=true.
|
|
32
|
+
error: { name: SpanNames.ERROR, kind: FISpanKind.CHAIN, tool: false },
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Fallback for an item type a newer SDK adds. */
|
|
36
|
+
export const UNKNOWN_ITEM_SPAN = { kind: FISpanKind.CHAIN, tool: false };
|
|
37
|
+
|
|
38
|
+
export const CodexAttributes = {
|
|
39
|
+
THREAD_ID: "codex.thread.id",
|
|
40
|
+
THREAD_RESUMED: "codex.thread.resumed",
|
|
41
|
+
SANDBOX_MODE: "codex.sandbox_mode",
|
|
42
|
+
APPROVAL_POLICY: "codex.approval_policy",
|
|
43
|
+
REASONING_EFFORT: "codex.model_reasoning_effort",
|
|
44
|
+
WEB_SEARCH_MODE: "codex.web_search_mode",
|
|
45
|
+
INPUT_IMAGE_COUNT: "codex.input.image_count",
|
|
46
|
+
INPUT_LENGTH: "codex.input.length",
|
|
47
|
+
|
|
48
|
+
ITEM_ID: "codex.item.id",
|
|
49
|
+
ITEM_TYPE: "codex.item.type",
|
|
50
|
+
ITEM_STATUS: "codex.item.status",
|
|
51
|
+
/** false when the SDK sent only `item.completed`; the span then starts at the previous event. */
|
|
52
|
+
ITEM_STARTED_EVENT: "codex.item.started_event",
|
|
53
|
+
ITEM_INCOMPLETE: "codex.item.incomplete",
|
|
54
|
+
|
|
55
|
+
COMMAND_EXIT_CODE: "codex.command.exit_code",
|
|
56
|
+
COMMAND_OUTPUT_LENGTH: "codex.command.output_length",
|
|
57
|
+
FILE_CHANGE_COUNT: "codex.file_change.count",
|
|
58
|
+
FILE_CHANGE_KINDS: "codex.file_change.kinds",
|
|
59
|
+
MCP_SERVER: "codex.mcp.server",
|
|
60
|
+
MCP_TOOL: "codex.mcp.tool",
|
|
61
|
+
TODO_COUNT: "codex.todo.count",
|
|
62
|
+
TODO_COMPLETED: "codex.todo.completed",
|
|
63
|
+
TEXT_LENGTH: "codex.text.length",
|
|
64
|
+
|
|
65
|
+
USAGE_CACHED_INPUT_TOKENS: "codex.usage.cached_input_tokens",
|
|
66
|
+
USAGE_CACHE_WRITE_INPUT_TOKENS: "codex.usage.cache_write_input_tokens",
|
|
67
|
+
USAGE_REASONING_OUTPUT_TOKENS: "codex.usage.reasoning_output_tokens",
|
|
68
|
+
/** A resumed thread's first turn in this process: no earlier total, so nothing promoted. */
|
|
69
|
+
USAGE_BASELINE_UNKNOWN: "codex.usage.baseline_unknown",
|
|
70
|
+
/** The running total went down; the reported value was promoted as is. */
|
|
71
|
+
USAGE_NON_MONOTONIC: "codex.usage.non_monotonic",
|
|
72
|
+
/** Prefix for the thread's running totals, e.g. `codex.cumulative.input_tokens`. */
|
|
73
|
+
CUMULATIVE_PREFIX: "codex.cumulative.",
|
|
74
|
+
|
|
75
|
+
/** Fixed summary unless outputs are visible; see README "Privacy". */
|
|
76
|
+
ERROR_MESSAGE: "codex.error.message",
|
|
77
|
+
/** Error class name of a thrown error, e.g. `Error`, `TypeError`. */
|
|
78
|
+
ERROR_TYPE: "codex.error.type",
|
|
79
|
+
/** `error` events in the turn. Codex sends them for errors it retries; the turn can still complete. */
|
|
80
|
+
STREAM_ERROR_COUNT: "codex.stream_error.count",
|
|
81
|
+
/** On `codex.error` spans: a non-fatal error item (warning, deprecation, model reroute). */
|
|
82
|
+
WARNING: "codex.warning",
|
|
83
|
+
CANCELLED: "codex.cancelled",
|
|
84
|
+
} as const;
|
|
85
|
+
|
|
86
|
+
/** Standard keys used by this package, resolved from fi-semantic-conventions. */
|
|
87
|
+
export const StandardAttributes = {
|
|
88
|
+
FI_SPAN_KIND: SemanticConventions.FI_SPAN_KIND,
|
|
89
|
+
/** Same value as `fi.span.kind`; the Python fi_instrumentation key the collector also reads. */
|
|
90
|
+
GEN_AI_SPAN_KIND: "gen_ai.span.kind",
|
|
91
|
+
SESSION_ID: SemanticConventions.SESSION_ID,
|
|
92
|
+
CONVERSATION_ID: SemanticConventions.GEN_AI_CONVERSATION_ID,
|
|
93
|
+
INPUT_VALUE: SemanticConventions.INPUT_VALUE,
|
|
94
|
+
INPUT_MIME_TYPE: SemanticConventions.INPUT_MIME_TYPE,
|
|
95
|
+
OUTPUT_VALUE: SemanticConventions.OUTPUT_VALUE,
|
|
96
|
+
OUTPUT_MIME_TYPE: SemanticConventions.OUTPUT_MIME_TYPE,
|
|
97
|
+
MODEL_NAME: SemanticConventions.LLM_MODEL_NAME,
|
|
98
|
+
PROVIDER: SemanticConventions.LLM_PROVIDER,
|
|
99
|
+
TOOL_NAME: SemanticConventions.TOOL_NAME,
|
|
100
|
+
GEN_AI_TOOL_NAME: "gen_ai.tool.name",
|
|
101
|
+
TOOL_CALL_ID: "gen_ai.tool.call.id",
|
|
102
|
+
AGENT_NAME: "gen_ai.agent.name",
|
|
103
|
+
INPUT_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_PROMPT,
|
|
104
|
+
OUTPUT_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_COMPLETION,
|
|
105
|
+
TOTAL_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_TOTAL,
|
|
106
|
+
CACHE_READ_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ,
|
|
107
|
+
CACHE_WRITE_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE,
|
|
108
|
+
REASONING_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_COMPLETION_DETAILS_REASONING,
|
|
109
|
+
} as const;
|
|
110
|
+
|
|
111
|
+
export const AGENT_NAME = "codex";
|
|
112
|
+
/** Codex talks to the OpenAI Responses API (or an OpenAI-compatible proxy). */
|
|
113
|
+
export const PROVIDER_NAME = "openai";
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export {
|
|
2
|
+
CodexSDKInstrumentation,
|
|
3
|
+
INSTRUMENTATION_NAME,
|
|
4
|
+
instrumentCodexSDK,
|
|
5
|
+
resolveContentPolicy,
|
|
6
|
+
shutdown,
|
|
7
|
+
} from "./instrumentation";
|
|
8
|
+
export type { CodexSDKInstrumentationConfig } from "./instrumentation";
|
|
9
|
+
export { isPatched } from "./threadWrapper";
|
|
10
|
+
export type { ContentPolicy } from "./spans";
|
|
11
|
+
export {
|
|
12
|
+
AGENT_NAME,
|
|
13
|
+
CodexAttributes,
|
|
14
|
+
ITEM_SPANS,
|
|
15
|
+
PROVIDER_NAME,
|
|
16
|
+
SpanNames,
|
|
17
|
+
StandardAttributes,
|
|
18
|
+
} from "./attributes";
|
|
19
|
+
export type {
|
|
20
|
+
CodexModuleLike,
|
|
21
|
+
ThreadEventLike,
|
|
22
|
+
ThreadItemLike,
|
|
23
|
+
ThreadLike,
|
|
24
|
+
ThreadOptionsLike,
|
|
25
|
+
UsageLike,
|
|
26
|
+
} from "./types";
|
|
27
|
+
export { VERSION } from "./version";
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public entry points: an instrumentation object that patches the SDK's
|
|
3
|
+
* `Thread` class (or single `Codex` / `Thread` instances), and a `shutdown()`
|
|
4
|
+
* that flushes the tracer provider before a short script exits.
|
|
5
|
+
*/
|
|
6
|
+
import { TracerProvider, diag, trace } from "@opentelemetry/api";
|
|
7
|
+
import { FITracer, TraceConfigOptions } from "@traceai/fi-core";
|
|
8
|
+
import { ContentPolicy } from "./spans";
|
|
9
|
+
import { TurnTracingContext, patchThread } from "./threadWrapper";
|
|
10
|
+
import { CodexModuleLike, ThreadLike } from "./types";
|
|
11
|
+
import { VERSION } from "./version";
|
|
12
|
+
|
|
13
|
+
export const INSTRUMENTATION_NAME = "@traceai/codex-sdk";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* fi-core env keys. Read fail-closed, unlike fi-core (`value.toLowerCase() ===
|
|
17
|
+
* "true"`): only an explicit `false` (trimmed, any case) opts in to content.
|
|
18
|
+
*/
|
|
19
|
+
const FI_HIDE_INPUTS = "FI_HIDE_INPUTS";
|
|
20
|
+
const FI_HIDE_OUTPUTS = "FI_HIDE_OUTPUTS";
|
|
21
|
+
const FI_HIDE_ERROR_DETAILS = "FI_HIDE_ERROR_DETAILS";
|
|
22
|
+
|
|
23
|
+
export interface CodexSDKInstrumentationConfig {
|
|
24
|
+
/** Provider to create spans with. Defaults to the global provider at call time. */
|
|
25
|
+
tracerProvider?: TracerProvider;
|
|
26
|
+
/**
|
|
27
|
+
* fi-core trace config. Unlike fi-core, `hideInputs` and `hideOutputs`
|
|
28
|
+
* default to `true` here: prompts, shell commands, command output, file
|
|
29
|
+
* paths and MCP arguments can contain source code or secrets. Opt in with
|
|
30
|
+
* `false`, or with `FI_HIDE_INPUTS=false` / `FI_HIDE_OUTPUTS=false`.
|
|
31
|
+
*/
|
|
32
|
+
traceConfig?: TraceConfigOptions;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `false` only for an explicit `false`; any other value (or none) keeps content hidden. */
|
|
36
|
+
function envHide(key: string): boolean {
|
|
37
|
+
const value = typeof process !== "undefined" ? process.env?.[key] : undefined;
|
|
38
|
+
return !(typeof value === "string" && value.trim().toLowerCase() === "false");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Precedence: explicit option > fi-core env var > this package's default (hidden). */
|
|
42
|
+
export function resolveContentPolicy(traceConfig?: TraceConfigOptions): ContentPolicy {
|
|
43
|
+
return {
|
|
44
|
+
hideInputs: traceConfig?.hideInputs ?? envHide(FI_HIDE_INPUTS),
|
|
45
|
+
hideOutputs: traceConfig?.hideOutputs ?? envHide(FI_HIDE_OUTPUTS),
|
|
46
|
+
hideErrorDetails: traceConfig?.hideErrorDetails ?? envHide(FI_HIDE_ERROR_DETAILS),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Traces `@openai/codex-sdk` turns.
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* import * as CodexSDK from "@openai/codex-sdk";
|
|
55
|
+
* const instrumentation = new CodexSDKInstrumentation({ tracerProvider });
|
|
56
|
+
* instrumentation.manuallyInstrument(CodexSDK);
|
|
57
|
+
* const thread = new CodexSDK.Codex().startThread();
|
|
58
|
+
* await thread.run("..."); // one codex.turn trace
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* The SDK is ESM-only, so its module namespace cannot be patched; its `Thread`
|
|
62
|
+
* class prototype can. `wrapCodex()` / `wrapThread()` patch single instances
|
|
63
|
+
* instead, for apps that prefer not to touch the shared class.
|
|
64
|
+
*
|
|
65
|
+
* This is not an OpenTelemetry `InstrumentationBase`: there is no module to
|
|
66
|
+
* hook on `require`/`import`, so it cannot be passed to
|
|
67
|
+
* `registerInstrumentations()`. Call `manuallyInstrument()` or the wrap methods.
|
|
68
|
+
*/
|
|
69
|
+
export class CodexSDKInstrumentation {
|
|
70
|
+
private tracerProvider?: TracerProvider;
|
|
71
|
+
private readonly traceConfig?: TraceConfigOptions;
|
|
72
|
+
/** Undo functions for the class patch and wrapCodex() factories (never one per thread). */
|
|
73
|
+
private readonly undo: Array<() => void> = [];
|
|
74
|
+
/** False after unpatch(): instance patches stay in place but pass events through untraced. */
|
|
75
|
+
private enabled = true;
|
|
76
|
+
|
|
77
|
+
constructor(config: CodexSDKInstrumentationConfig = {}) {
|
|
78
|
+
this.traceConfig = config.traceConfig;
|
|
79
|
+
this.tracerProvider = config.tracerProvider;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
setTracerProvider(tracerProvider: TracerProvider): void {
|
|
83
|
+
this.tracerProvider = tracerProvider;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The content policy this instrumentation applies. */
|
|
87
|
+
get contentPolicy(): ContentPolicy {
|
|
88
|
+
return resolveContentPolicy(this.traceConfig);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Patch `module.Thread.prototype`. Pass the SDK module namespace
|
|
93
|
+
* (`import * as CodexSDK from "@openai/codex-sdk"`) or `{ Thread }`.
|
|
94
|
+
* Returns `true` when the class was patched by this call.
|
|
95
|
+
*/
|
|
96
|
+
manuallyInstrument(module: CodexModuleLike): boolean {
|
|
97
|
+
const proto = (module?.Thread as { prototype?: ThreadLike } | undefined)?.prototype;
|
|
98
|
+
if (!proto) {
|
|
99
|
+
diag.warn(`${INSTRUMENTATION_NAME}: manuallyInstrument() expects the @openai/codex-sdk module (no Thread export found)`);
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
this.enabled = true;
|
|
103
|
+
const undo = patchThread(proto, () => this.tracingContext());
|
|
104
|
+
if (undo) this.undo.push(undo);
|
|
105
|
+
return !!undo;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Patch one Thread instance. Returns the same instance. No reference to the
|
|
110
|
+
* thread is kept: after `unpatch()` the patched method passes events through
|
|
111
|
+
* untraced instead of being removed.
|
|
112
|
+
*/
|
|
113
|
+
wrapThread<T extends object>(thread: T): T {
|
|
114
|
+
this.enabled = true;
|
|
115
|
+
patchThread(thread as ThreadLike, () => this.tracingContext());
|
|
116
|
+
return thread;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Patch one Codex instance so every thread it starts or resumes is traced. Returns the same instance. */
|
|
120
|
+
wrapCodex<T extends object>(codex: T): T {
|
|
121
|
+
if (!codex || typeof codex !== "object") return codex;
|
|
122
|
+
this.enabled = true;
|
|
123
|
+
const target = codex as Record<string, unknown>;
|
|
124
|
+
// Held weakly: a wrapped client the app drops can be collected before unpatch().
|
|
125
|
+
const ref = new WeakRef(target);
|
|
126
|
+
for (const method of ["startThread", "resumeThread"] as const) {
|
|
127
|
+
const original = target[method] as ((...args: unknown[]) => object) | undefined;
|
|
128
|
+
if (typeof original !== "function") continue;
|
|
129
|
+
const hadOwn = Object.prototype.hasOwnProperty.call(codex, method);
|
|
130
|
+
const self = this;
|
|
131
|
+
Object.defineProperty(codex, method, {
|
|
132
|
+
value: function tracedThreadFactory(this: unknown, ...args: unknown[]) {
|
|
133
|
+
return self.wrapThread(original.apply(this, args));
|
|
134
|
+
},
|
|
135
|
+
writable: true,
|
|
136
|
+
configurable: true,
|
|
137
|
+
});
|
|
138
|
+
this.undo.push(() => {
|
|
139
|
+
const live = ref.deref();
|
|
140
|
+
if (!live) return;
|
|
141
|
+
if (hadOwn) Object.defineProperty(live, method, { value: original, writable: true, configurable: true });
|
|
142
|
+
else delete live[method];
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return codex;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Remove every patch this instrumentation applied. */
|
|
149
|
+
unpatch(): void {
|
|
150
|
+
this.enabled = false;
|
|
151
|
+
while (this.undo.length) {
|
|
152
|
+
const fn = this.undo.pop();
|
|
153
|
+
try {
|
|
154
|
+
fn?.();
|
|
155
|
+
} catch (error) {
|
|
156
|
+
diag.debug(`${INSTRUMENTATION_NAME}: unpatch failed: ${error}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Flush the provider's span processors. Does not shut the provider down. */
|
|
162
|
+
async forceFlush(): Promise<void> {
|
|
163
|
+
await flushProvider(this.tracerProvider ?? trace.getTracerProvider());
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private tracingContext(): TurnTracingContext | undefined {
|
|
167
|
+
if (!this.enabled) return undefined;
|
|
168
|
+
const policy = this.contentPolicy;
|
|
169
|
+
const provider = this.tracerProvider ?? trace.getTracerProvider();
|
|
170
|
+
const tracer = new FITracer({
|
|
171
|
+
tracer: provider.getTracer(INSTRUMENTATION_NAME, VERSION),
|
|
172
|
+
traceConfig: { ...this.traceConfig, hideInputs: policy.hideInputs, hideOutputs: policy.hideOutputs, hideErrorDetails: policy.hideErrorDetails },
|
|
173
|
+
});
|
|
174
|
+
return { tracer, policy };
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Python-style convenience constructor. */
|
|
179
|
+
export function instrumentCodexSDK(
|
|
180
|
+
module: CodexModuleLike,
|
|
181
|
+
config: CodexSDKInstrumentationConfig = {},
|
|
182
|
+
): CodexSDKInstrumentation {
|
|
183
|
+
const instrumentation = new CodexSDKInstrumentation(config);
|
|
184
|
+
instrumentation.manuallyInstrument(module);
|
|
185
|
+
return instrumentation;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Flush spans before a short script exits. Export errors are logged, never
|
|
190
|
+
* thrown: export failure does not fail the agent.
|
|
191
|
+
*/
|
|
192
|
+
export async function shutdown(tracerProvider?: TracerProvider): Promise<void> {
|
|
193
|
+
await flushProvider(tracerProvider ?? trace.getTracerProvider());
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function resolveDelegate(provider: TracerProvider): TracerProvider {
|
|
197
|
+
const maybeProxy = provider as TracerProvider & {
|
|
198
|
+
getDelegate?: () => TracerProvider;
|
|
199
|
+
forceFlush?: () => Promise<void>;
|
|
200
|
+
};
|
|
201
|
+
if (typeof maybeProxy.forceFlush !== "function" && typeof maybeProxy.getDelegate === "function") {
|
|
202
|
+
return maybeProxy.getDelegate();
|
|
203
|
+
}
|
|
204
|
+
return provider;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async function flushProvider(provider: TracerProvider): Promise<void> {
|
|
208
|
+
const target = resolveDelegate(provider) as TracerProvider & { forceFlush?: () => Promise<void> };
|
|
209
|
+
if (typeof target.forceFlush !== "function") return;
|
|
210
|
+
try {
|
|
211
|
+
await target.forceFlush();
|
|
212
|
+
} catch (error) {
|
|
213
|
+
diag.warn(`${INSTRUMENTATION_NAME}: forceFlush failed: ${error}`);
|
|
214
|
+
}
|
|
215
|
+
}
|