@traceai/codex-sdk 0.0.0-stage → 0.1.1
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 +172 -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/codexWrapper.d.ts +5 -0
- package/dist/esm/codexWrapper.d.ts.map +1 -0
- package/dist/esm/codexWrapper.js +63 -0
- package/dist/esm/codexWrapper.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 +85 -0
- package/dist/esm/instrumentation.d.ts.map +1 -0
- package/dist/esm/instrumentation.js +195 -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 +208 -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/codexWrapper.d.ts +5 -0
- package/dist/esnext/codexWrapper.d.ts.map +1 -0
- package/dist/esnext/codexWrapper.js +65 -0
- package/dist/esnext/codexWrapper.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 +85 -0
- package/dist/esnext/instrumentation.d.ts.map +1 -0
- package/dist/esnext/instrumentation.js +200 -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 +206 -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/codexWrapper.d.ts +5 -0
- package/dist/src/codexWrapper.d.ts.map +1 -0
- package/dist/src/codexWrapper.js +68 -0
- package/dist/src/codexWrapper.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 +85 -0
- package/dist/src/instrumentation.d.ts.map +1 -0
- package/dist/src/instrumentation.js +222 -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 +229 -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 +76 -3
- package/src/attributes.ts +113 -0
- package/src/codexWrapper.ts +71 -0
- package/src/index.ts +27 -0
- package/src/instrumentation.ts +227 -0
- package/src/spans.ts +729 -0
- package/src/threadWrapper.ts +221 -0
- package/src/types.ts +113 -0
- package/src/version.ts +1 -0
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
export const SpanNames = {
|
|
10
|
+
TURN: "codex.turn",
|
|
11
|
+
AGENT_MESSAGE: "codex.agent_message",
|
|
12
|
+
REASONING: "codex.reasoning",
|
|
13
|
+
COMMAND_EXECUTION: "codex.command_execution",
|
|
14
|
+
FILE_CHANGE: "codex.file_change",
|
|
15
|
+
MCP_TOOL_CALL: "codex.mcp_tool_call",
|
|
16
|
+
WEB_SEARCH: "codex.web_search",
|
|
17
|
+
TODO_LIST: "codex.todo_list",
|
|
18
|
+
ERROR: "codex.error",
|
|
19
|
+
};
|
|
20
|
+
/** SDK item type -> span name and Future AGI span kind. */
|
|
21
|
+
export const ITEM_SPANS = {
|
|
22
|
+
agent_message: { name: SpanNames.AGENT_MESSAGE, kind: FISpanKind.LLM, tool: false },
|
|
23
|
+
reasoning: { name: SpanNames.REASONING, kind: FISpanKind.CHAIN, tool: false },
|
|
24
|
+
command_execution: { name: SpanNames.COMMAND_EXECUTION, kind: FISpanKind.TOOL, tool: true },
|
|
25
|
+
file_change: { name: SpanNames.FILE_CHANGE, kind: FISpanKind.TOOL, tool: true },
|
|
26
|
+
mcp_tool_call: { name: SpanNames.MCP_TOOL_CALL, kind: FISpanKind.TOOL, tool: true },
|
|
27
|
+
web_search: { name: SpanNames.WEB_SEARCH, kind: FISpanKind.TOOL, tool: true },
|
|
28
|
+
todo_list: { name: SpanNames.TODO_LIST, kind: FISpanKind.CHAIN, tool: false },
|
|
29
|
+
// Non-fatal: status UNSET with codex.warning=true.
|
|
30
|
+
error: { name: SpanNames.ERROR, kind: FISpanKind.CHAIN, tool: false },
|
|
31
|
+
};
|
|
32
|
+
/** Fallback for an item type a newer SDK adds. */
|
|
33
|
+
export const UNKNOWN_ITEM_SPAN = { kind: FISpanKind.CHAIN, tool: false };
|
|
34
|
+
export const CodexAttributes = {
|
|
35
|
+
THREAD_ID: "codex.thread.id",
|
|
36
|
+
THREAD_RESUMED: "codex.thread.resumed",
|
|
37
|
+
SANDBOX_MODE: "codex.sandbox_mode",
|
|
38
|
+
APPROVAL_POLICY: "codex.approval_policy",
|
|
39
|
+
REASONING_EFFORT: "codex.model_reasoning_effort",
|
|
40
|
+
WEB_SEARCH_MODE: "codex.web_search_mode",
|
|
41
|
+
INPUT_IMAGE_COUNT: "codex.input.image_count",
|
|
42
|
+
INPUT_LENGTH: "codex.input.length",
|
|
43
|
+
ITEM_ID: "codex.item.id",
|
|
44
|
+
ITEM_TYPE: "codex.item.type",
|
|
45
|
+
ITEM_STATUS: "codex.item.status",
|
|
46
|
+
/** false when the SDK sent only `item.completed`; the span then starts at the previous event. */
|
|
47
|
+
ITEM_STARTED_EVENT: "codex.item.started_event",
|
|
48
|
+
ITEM_INCOMPLETE: "codex.item.incomplete",
|
|
49
|
+
COMMAND_EXIT_CODE: "codex.command.exit_code",
|
|
50
|
+
COMMAND_OUTPUT_LENGTH: "codex.command.output_length",
|
|
51
|
+
FILE_CHANGE_COUNT: "codex.file_change.count",
|
|
52
|
+
FILE_CHANGE_KINDS: "codex.file_change.kinds",
|
|
53
|
+
MCP_SERVER: "codex.mcp.server",
|
|
54
|
+
MCP_TOOL: "codex.mcp.tool",
|
|
55
|
+
TODO_COUNT: "codex.todo.count",
|
|
56
|
+
TODO_COMPLETED: "codex.todo.completed",
|
|
57
|
+
TEXT_LENGTH: "codex.text.length",
|
|
58
|
+
USAGE_CACHED_INPUT_TOKENS: "codex.usage.cached_input_tokens",
|
|
59
|
+
USAGE_CACHE_WRITE_INPUT_TOKENS: "codex.usage.cache_write_input_tokens",
|
|
60
|
+
USAGE_REASONING_OUTPUT_TOKENS: "codex.usage.reasoning_output_tokens",
|
|
61
|
+
/** A resumed thread's first turn in this process: no earlier total, so nothing promoted. */
|
|
62
|
+
USAGE_BASELINE_UNKNOWN: "codex.usage.baseline_unknown",
|
|
63
|
+
/** The running total went down; the reported value was promoted as is. */
|
|
64
|
+
USAGE_NON_MONOTONIC: "codex.usage.non_monotonic",
|
|
65
|
+
/** Prefix for the thread's running totals, e.g. `codex.cumulative.input_tokens`. */
|
|
66
|
+
CUMULATIVE_PREFIX: "codex.cumulative.",
|
|
67
|
+
/** Fixed summary unless outputs are visible; see README "Privacy". */
|
|
68
|
+
ERROR_MESSAGE: "codex.error.message",
|
|
69
|
+
/** Error class name of a thrown error, e.g. `Error`, `TypeError`. */
|
|
70
|
+
ERROR_TYPE: "codex.error.type",
|
|
71
|
+
/** `error` events in the turn. Codex sends them for errors it retries; the turn can still complete. */
|
|
72
|
+
STREAM_ERROR_COUNT: "codex.stream_error.count",
|
|
73
|
+
/** On `codex.error` spans: a non-fatal error item (warning, deprecation, model reroute). */
|
|
74
|
+
WARNING: "codex.warning",
|
|
75
|
+
CANCELLED: "codex.cancelled",
|
|
76
|
+
};
|
|
77
|
+
/** Standard keys used by this package, resolved from fi-semantic-conventions. */
|
|
78
|
+
export const StandardAttributes = {
|
|
79
|
+
FI_SPAN_KIND: SemanticConventions.FI_SPAN_KIND,
|
|
80
|
+
/** Same value as `fi.span.kind`; the Python fi_instrumentation key the collector also reads. */
|
|
81
|
+
GEN_AI_SPAN_KIND: "gen_ai.span.kind",
|
|
82
|
+
SESSION_ID: SemanticConventions.SESSION_ID,
|
|
83
|
+
CONVERSATION_ID: SemanticConventions.GEN_AI_CONVERSATION_ID,
|
|
84
|
+
INPUT_VALUE: SemanticConventions.INPUT_VALUE,
|
|
85
|
+
INPUT_MIME_TYPE: SemanticConventions.INPUT_MIME_TYPE,
|
|
86
|
+
OUTPUT_VALUE: SemanticConventions.OUTPUT_VALUE,
|
|
87
|
+
OUTPUT_MIME_TYPE: SemanticConventions.OUTPUT_MIME_TYPE,
|
|
88
|
+
MODEL_NAME: SemanticConventions.LLM_MODEL_NAME,
|
|
89
|
+
PROVIDER: SemanticConventions.LLM_PROVIDER,
|
|
90
|
+
TOOL_NAME: SemanticConventions.TOOL_NAME,
|
|
91
|
+
GEN_AI_TOOL_NAME: "gen_ai.tool.name",
|
|
92
|
+
TOOL_CALL_ID: "gen_ai.tool.call.id",
|
|
93
|
+
AGENT_NAME: "gen_ai.agent.name",
|
|
94
|
+
INPUT_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_PROMPT,
|
|
95
|
+
OUTPUT_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_COMPLETION,
|
|
96
|
+
TOTAL_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_TOTAL,
|
|
97
|
+
CACHE_READ_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_READ,
|
|
98
|
+
CACHE_WRITE_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_PROMPT_DETAILS_CACHE_WRITE,
|
|
99
|
+
REASONING_TOKENS: SemanticConventions.LLM_TOKEN_COUNT_COMPLETION_DETAILS_REASONING,
|
|
100
|
+
};
|
|
101
|
+
export const AGENT_NAME = "codex";
|
|
102
|
+
/** Codex talks to the OpenAI Responses API (or an OpenAI-compatible proxy). */
|
|
103
|
+
export const PROVIDER_NAME = "openai";
|
|
104
|
+
//# sourceMappingURL=attributes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attributes.js","sourceRoot":"","sources":["../../src/attributes.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAEnF,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,IAAI,EAAE,YAAY;IAClB,aAAa,EAAE,qBAAqB;IACpC,SAAS,EAAE,iBAAiB;IAC5B,iBAAiB,EAAE,yBAAyB;IAC5C,WAAW,EAAE,mBAAmB;IAChC,aAAa,EAAE,qBAAqB;IACpC,UAAU,EAAE,kBAAkB;IAC9B,SAAS,EAAE,iBAAiB;IAC5B,KAAK,EAAE,aAAa;CACZ,CAAC;AAEX,2DAA2D;AAC3D,MAAM,CAAC,MAAM,UAAU,GAAsE;IAC3F,aAAa,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,aAAa,EAAE,IAAI,EAAE,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE;IACnF,SAAS,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,SAAS,EAAE,IAAI,EAAE,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE;IAC7E,iBAAiB,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,iBAAiB,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;IAC3F,WAAW,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,WAAW,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;IAC/E,aAAa,EAAE,E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AAkB,EAAE,mBAAmB,CAAC,0CAA0C;IAClF,gBAAgB,EAAE,mBAAmB,CAAC,4CAA4C;CAC1E,CAAC;AAEX,MAAM,CAAC,MAAM,UAAU,GAAG,OAAO,CAAC;AAClC,+EAA+E;AAC/E,MAAM,CAAC,MAAM,aAAa,GAAG,QAAQ,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
type Client = Record<string | symbol, unknown>;
|
|
2
|
+
/** A client has one active owner, just like a Thread; duplicate owners do not stack. */
|
|
3
|
+
export declare function patchCodex(target: Client, wrapThread: (thread: object) => object): (() => void) | undefined;
|
|
4
|
+
export {};
|
|
5
|
+
//# sourceMappingURL=codexWrapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codexWrapper.d.ts","sourceRoot":"","sources":["../../src/codexWrapper.ts"],"names":[],"mappings":"AAIA,KAAK,MAAM,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,CAAC;AAwC/C,wFAAwF;AACxF,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,GAAG,SAAS,CAyB3G"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { diag } from "@opentelemetry/api";
|
|
2
|
+
const PATCH_RECORD = Symbol.for("@traceai/codex-sdk/client-patch-record");
|
|
3
|
+
const METHODS = ["startThread", "resumeThread"];
|
|
4
|
+
/** Retain neither the client nor a possibly bound original factory in this closure. */
|
|
5
|
+
function weakUndo(ref, owner) {
|
|
6
|
+
return () => {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
// Saved wrappers must remain inert even if the instrumentor is enabled again.
|
|
9
|
+
owner.active = false;
|
|
10
|
+
const target = ref.deref();
|
|
11
|
+
if (!target)
|
|
12
|
+
return;
|
|
13
|
+
const record = (_a = Object.getOwnPropertyDescriptor(target, PATCH_RECORD)) === null || _a === void 0 ? void 0 : _a.value;
|
|
14
|
+
if ((record === null || record === void 0 ? void 0 : record.owner) !== owner)
|
|
15
|
+
return;
|
|
16
|
+
for (const { method, originalDescriptor, wrapper } of record.factories) {
|
|
17
|
+
try {
|
|
18
|
+
// Inspect the descriptor, not the value: application accessors must not run.
|
|
19
|
+
if (((_b = Object.getOwnPropertyDescriptor(target, method)) === null || _b === void 0 ? void 0 : _b.value) !== wrapper)
|
|
20
|
+
continue;
|
|
21
|
+
if (originalDescriptor)
|
|
22
|
+
Object.defineProperty(target, method, originalDescriptor);
|
|
23
|
+
else
|
|
24
|
+
delete target[method];
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
diag.debug(`@traceai/codex-sdk: factory unpatch failed: ${error}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
delete target[PATCH_RECORD];
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** A client has one active owner, just like a Thread; duplicate owners do not stack. */
|
|
34
|
+
export function patchCodex(target, wrapThread) {
|
|
35
|
+
var _a;
|
|
36
|
+
if ((_a = target[PATCH_RECORD]) === null || _a === void 0 ? void 0 : _a.owner.active)
|
|
37
|
+
return undefined;
|
|
38
|
+
const owner = { active: true };
|
|
39
|
+
const record = { owner, factories: [] };
|
|
40
|
+
const undo = weakUndo(new WeakRef(target), owner);
|
|
41
|
+
Object.defineProperty(target, PATCH_RECORD, { value: record, configurable: true });
|
|
42
|
+
try {
|
|
43
|
+
for (const method of METHODS) {
|
|
44
|
+
const original = target[method];
|
|
45
|
+
if (typeof original !== "function")
|
|
46
|
+
continue;
|
|
47
|
+
const originalDescriptor = Object.getOwnPropertyDescriptor(target, method);
|
|
48
|
+
const wrapper = function tracedThreadFactory(...args) {
|
|
49
|
+
const thread = original.apply(this, args);
|
|
50
|
+
return owner.active ? wrapThread(thread) : thread;
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(target, method, { value: wrapper, writable: true, configurable: true });
|
|
53
|
+
record.factories.push({ method, originalDescriptor, wrapper });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
undo();
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
if (record.factories.length)
|
|
61
|
+
return undo;
|
|
62
|
+
undo();
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=codexWrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codexWrapper.js","sourceRoot":"","sources":["../../src/codexWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,MAAM,YAAY,GAAG,MAAM,CAAC,GAAG,CAAC,wCAAwC,CAAC,CAAC;AAC1E,MAAM,OAAO,GAAG,CAAC,aAAa,EAAE,cAAc,CAAU,CAAC;AAkBzD,uFAAuF;AACvF,SAAS,QAAQ,CAAC,GAAoB,EAAE,KAAY;IAClD,OAAO,GAAG,EAAE;;QACV,8EAA8E;QAC9E,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC;QACrB,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,EAAE,CAAC;QAC3B,IAAI,CAAC,MAAM;YAAE,OAAO;QACpB,MAAM,MAAM,GAAG,MAAA,MAAM,CAAC,wBAAwB,CAAC,MAAM,EAAE,YAAY,CAAC,0CAAE,KAAgC,CAAC;QACvG,IAAI,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,KAAK,MAAK,KAAK;YAAE,OAAO;QACpC,KAAK,MAAM,EAAE,MAAM,EAAE,kBAAkB,EAAE,OAAO,EAAE,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;YACvE,IAAI,CAAC;gBACH,6EAA6E;gBAC7E,IAAI,CAAA,MAAA,MAAM,CAAC,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,0CAAE,KAAK,MAAK,OAAO;oBAAE,SAAS;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CAAC,CAAC;gBAC1C,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;YACpD,CAAC,CAAC;YACF,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;YAC9F,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,kBAAkB,EAAE,OAAO,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,EAAE,CAAC;QACP,MAAM,KAAK,CAAC;IACd,CAAC;IACD,IAAI,MAAM,CAAC,SAAS,CAAC,MAAM;QAAE,OAAO,IAAI,CAAC;IACzC,IAAI,EAAE,CAAC;IACP,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { CodexSDKInstrumentation, INSTRUMENTATION_NAME, instrumentCodexSDK, resolveContentPolicy, shutdown, } from "./instrumentation";
|
|
2
|
+
export type { CodexSDKInstrumentationConfig } from "./instrumentation";
|
|
3
|
+
export { isPatched } from "./threadWrapper";
|
|
4
|
+
export type { ContentPolicy } from "./spans";
|
|
5
|
+
export { AGENT_NAME, CodexAttributes, ITEM_SPANS, PROVIDER_NAME, SpanNames, StandardAttributes, } from "./attributes";
|
|
6
|
+
export type { CodexModuleLike, ThreadEventLike, ThreadItemLike, ThreadLike, ThreadOptionsLike, UsageLike, } from "./types";
|
|
7
|
+
export { VERSION } from "./version";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACpB,QAAQ,GACT,MAAM,mBAAmB,CAAC;AAC3B,YAAY,EAAE,6BAA6B,EAAE,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC7C,OAAO,EACL,UAAU,EACV,eAAe,EACf,UAAU,EACV,aAAa,EACb,SAAS,EACT,kBAAkB,GACnB,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,eAAe,EACf,eAAe,EACf,cAAc,EACd,UAAU,EACV,iBAAiB,EACjB,SAAS,GACV,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CodexSDKInstrumentation, INSTRUMENTATION_NAME, instrumentCodexSDK, resolveContentPolicy, shutdown, } from "./instrumentation";
|
|
2
|
+
export { isPatched } from "./threadWrapper";
|
|
3
|
+
export { AGENT_NAME, CodexAttributes, ITEM_SPANS, PROVIDER_NAME, SpanNames, StandardAttributes, } from "./attributes";
|
|
4
|
+
export { VERSION } from "./version";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,oBAAoB,EACpB,kBAAkB,EAClB,oBAAoB,EACpB,QAAQ,GACT,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EACL,UAAU,EACV,eAAe,EACf,UAAU,EACV,aAAa,EACb,SAAS,EACT,kBAAkB,GACnB,MAAM,cAAc,CAAC;AAStB,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
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 } from "@opentelemetry/api";
|
|
7
|
+
import { TraceConfigOptions } from "@traceai/fi-core";
|
|
8
|
+
import { ContentPolicy } from "./spans";
|
|
9
|
+
import { CodexModuleLike } from "./types";
|
|
10
|
+
export declare const INSTRUMENTATION_NAME = "@traceai/codex-sdk";
|
|
11
|
+
export interface CodexSDKInstrumentationConfig {
|
|
12
|
+
/** Provider to create spans with. Defaults to the global provider at call time. */
|
|
13
|
+
tracerProvider?: TracerProvider;
|
|
14
|
+
/**
|
|
15
|
+
* fi-core trace config. Unlike fi-core, `hideInputs` and `hideOutputs`
|
|
16
|
+
* default to `true` here: prompts, shell commands, command output, file
|
|
17
|
+
* paths and MCP arguments can contain source code or secrets. Opt in with
|
|
18
|
+
* `false`, or with `FI_HIDE_INPUTS=false` / `FI_HIDE_OUTPUTS=false`.
|
|
19
|
+
*/
|
|
20
|
+
traceConfig?: TraceConfigOptions;
|
|
21
|
+
}
|
|
22
|
+
/** Precedence: explicit option > fi-core env var > this package's default (hidden). */
|
|
23
|
+
export declare function resolveContentPolicy(traceConfig?: TraceConfigOptions): ContentPolicy;
|
|
24
|
+
/**
|
|
25
|
+
* Traces `@openai/codex-sdk` turns.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import * as CodexSDK from "@openai/codex-sdk";
|
|
29
|
+
* const instrumentation = new CodexSDKInstrumentation({ tracerProvider });
|
|
30
|
+
* instrumentation.manuallyInstrument(CodexSDK);
|
|
31
|
+
* const thread = new CodexSDK.Codex().startThread();
|
|
32
|
+
* await thread.run("..."); // one codex.turn trace
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* The SDK is ESM-only, so its module namespace cannot be patched; its `Thread`
|
|
36
|
+
* class prototype can. `wrapCodex()` / `wrapThread()` patch single instances
|
|
37
|
+
* instead, for apps that prefer not to touch the shared class.
|
|
38
|
+
*
|
|
39
|
+
* This is not an OpenTelemetry `InstrumentationBase`: there is no module to
|
|
40
|
+
* hook on `require`/`import`, so it cannot be passed to
|
|
41
|
+
* `registerInstrumentations()`. Call `manuallyInstrument()` or the wrap methods.
|
|
42
|
+
*/
|
|
43
|
+
export declare class CodexSDKInstrumentation {
|
|
44
|
+
private tracerProvider?;
|
|
45
|
+
private readonly traceConfig?;
|
|
46
|
+
/** Undo functions for class patches. */
|
|
47
|
+
private readonly undo;
|
|
48
|
+
/** Client undo functions hold only WeakRefs; collected clients release their bookkeeping. */
|
|
49
|
+
private readonly clientUndo;
|
|
50
|
+
private readonly clientFinalizer;
|
|
51
|
+
/** Thread undo functions hold only WeakRefs; collected threads release their bookkeeping. */
|
|
52
|
+
private readonly threadUndo;
|
|
53
|
+
private readonly threadFinalizer;
|
|
54
|
+
private enabled;
|
|
55
|
+
constructor(config?: CodexSDKInstrumentationConfig);
|
|
56
|
+
setTracerProvider(tracerProvider: TracerProvider): void;
|
|
57
|
+
/** The content policy this instrumentation applies. */
|
|
58
|
+
get contentPolicy(): ContentPolicy;
|
|
59
|
+
/**
|
|
60
|
+
* Patch `module.Thread.prototype`. Pass the SDK module namespace
|
|
61
|
+
* (`import * as CodexSDK from "@openai/codex-sdk"`) or `{ Thread }`.
|
|
62
|
+
* Returns `true` when the class was patched by this call.
|
|
63
|
+
*/
|
|
64
|
+
manuallyInstrument(module: CodexModuleLike): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Patch one Thread instance. Returns the same instance. Only a weak reference
|
|
67
|
+
* is kept, so `unpatch()` can restore live threads without retaining dropped ones.
|
|
68
|
+
*/
|
|
69
|
+
wrapThread<T extends object>(thread: T): T;
|
|
70
|
+
/** Patch one Codex instance so every thread it starts or resumes is traced. Returns the same instance. */
|
|
71
|
+
wrapCodex<T extends object>(codex: T): T;
|
|
72
|
+
/** Remove every patch this instrumentation applied. */
|
|
73
|
+
unpatch(): void;
|
|
74
|
+
/** Flush the provider's span processors. Does not shut the provider down. */
|
|
75
|
+
forceFlush(): Promise<void>;
|
|
76
|
+
private tracingContext;
|
|
77
|
+
}
|
|
78
|
+
/** Python-style convenience constructor. */
|
|
79
|
+
export declare function instrumentCodexSDK(module: CodexModuleLike, config?: CodexSDKInstrumentationConfig): CodexSDKInstrumentation;
|
|
80
|
+
/**
|
|
81
|
+
* Flush spans before a short script exits. Export errors are logged, never
|
|
82
|
+
* thrown: export failure does not fail the agent.
|
|
83
|
+
*/
|
|
84
|
+
export declare function shutdown(tracerProvider?: TracerProvider): Promise<void>;
|
|
85
|
+
//# sourceMappingURL=instrumentation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"instrumentation.d.ts","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,cAAc,EAAe,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAY,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAEhE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,OAAO,EAAE,eAAe,EAAc,MAAM,SAAS,CAAC;AAGtD,eAAO,MAAM,oBAAoB,uBAAuB,CAAC;AAUzD,MAAM,WAAW,6BAA6B;IAC5C,mFAAmF;IACnF,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC;;;;;OAKG;IACH,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC;AAQD,uFAAuF;AACvF,wBAAgB,oBAAoB,CAAC,WAAW,CAAC,EAAE,kBAAkB,GAAG,aAAa,CAMpF;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,uBAAuB;IAClC,OAAO,CAAC,cAAc,CAAC,CAAiB;IACxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAqB;IAClD,wCAAwC;IACxC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAyB;IAC9C,6FAA6F;IAC7F,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAyB;IACpD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAI7B;IACH,6FAA6F;IAC7F,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAyB;IACpD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAE7B;IACH,OAAO,CAAC,OAAO,CAAQ;gBAEX,MAAM,GAAE,6BAAkC;IAKtD,iBAAiB,CAAC,cAAc,EAAE,cAAc,GAAG,IAAI;IAIvD,uDAAuD;IACvD,IAAI,aAAa,IAAI,aAAa,CAEjC;IAED;;;;OAIG;IACH,kBAAkB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO;IAYpD;;;OAGG;IACH,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC;IAU1C,0GAA0G;IAC1G,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC;IAWxC,uDAAuD;IACvD,OAAO,IAAI,IAAI;IA0Bf,6EAA6E;IACvE,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC,OAAO,CAAC,cAAc;CAUvB;AAED,4CAA4C;AAC5C,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,eAAe,EACvB,MAAM,GAAE,6BAAkC,GACzC,uBAAuB,CAIzB;AAED;;;GAGG;AACH,wBAAsB,QAAQ,CAAC,cAAc,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAE7E"}
|
|
@@ -0,0 +1,200 @@
|
|
|
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 { diag, trace } from "@opentelemetry/api";
|
|
7
|
+
import { FITracer } from "@traceai/fi-core";
|
|
8
|
+
import { patchCodex } from "./codexWrapper";
|
|
9
|
+
import { patchThread } from "./threadWrapper";
|
|
10
|
+
import { VERSION } from "./version";
|
|
11
|
+
export const INSTRUMENTATION_NAME = "@traceai/codex-sdk";
|
|
12
|
+
/**
|
|
13
|
+
* fi-core env keys. Read fail-closed, unlike fi-core (`value.toLowerCase() ===
|
|
14
|
+
* "true"`): only an explicit `false` (trimmed, any case) opts in to content.
|
|
15
|
+
*/
|
|
16
|
+
const FI_HIDE_INPUTS = "FI_HIDE_INPUTS";
|
|
17
|
+
const FI_HIDE_OUTPUTS = "FI_HIDE_OUTPUTS";
|
|
18
|
+
const FI_HIDE_ERROR_DETAILS = "FI_HIDE_ERROR_DETAILS";
|
|
19
|
+
/** `false` only for an explicit `false`; any other value (or none) keeps content hidden. */
|
|
20
|
+
function envHide(key) {
|
|
21
|
+
var _a;
|
|
22
|
+
const value = typeof process !== "undefined" ? (_a = process.env) === null || _a === void 0 ? void 0 : _a[key] : undefined;
|
|
23
|
+
return !(typeof value === "string" && value.trim().toLowerCase() === "false");
|
|
24
|
+
}
|
|
25
|
+
/** Precedence: explicit option > fi-core env var > this package's default (hidden). */
|
|
26
|
+
export function resolveContentPolicy(traceConfig) {
|
|
27
|
+
var _a, _b, _c;
|
|
28
|
+
return {
|
|
29
|
+
hideInputs: (_a = traceConfig === null || traceConfig === void 0 ? void 0 : traceConfig.hideInputs) !== null && _a !== void 0 ? _a : envHide(FI_HIDE_INPUTS),
|
|
30
|
+
hideOutputs: (_b = traceConfig === null || traceConfig === void 0 ? void 0 : traceConfig.hideOutputs) !== null && _b !== void 0 ? _b : envHide(FI_HIDE_OUTPUTS),
|
|
31
|
+
hideErrorDetails: (_c = traceConfig === null || traceConfig === void 0 ? void 0 : traceConfig.hideErrorDetails) !== null && _c !== void 0 ? _c : envHide(FI_HIDE_ERROR_DETAILS),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Traces `@openai/codex-sdk` turns.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import * as CodexSDK from "@openai/codex-sdk";
|
|
39
|
+
* const instrumentation = new CodexSDKInstrumentation({ tracerProvider });
|
|
40
|
+
* instrumentation.manuallyInstrument(CodexSDK);
|
|
41
|
+
* const thread = new CodexSDK.Codex().startThread();
|
|
42
|
+
* await thread.run("..."); // one codex.turn trace
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* The SDK is ESM-only, so its module namespace cannot be patched; its `Thread`
|
|
46
|
+
* class prototype can. `wrapCodex()` / `wrapThread()` patch single instances
|
|
47
|
+
* instead, for apps that prefer not to touch the shared class.
|
|
48
|
+
*
|
|
49
|
+
* This is not an OpenTelemetry `InstrumentationBase`: there is no module to
|
|
50
|
+
* hook on `require`/`import`, so it cannot be passed to
|
|
51
|
+
* `registerInstrumentations()`. Call `manuallyInstrument()` or the wrap methods.
|
|
52
|
+
*/
|
|
53
|
+
export class CodexSDKInstrumentation {
|
|
54
|
+
constructor(config = {}) {
|
|
55
|
+
/** Undo functions for class patches. */
|
|
56
|
+
this.undo = [];
|
|
57
|
+
/** Client undo functions hold only WeakRefs; collected clients release their bookkeeping. */
|
|
58
|
+
this.clientUndo = new Set();
|
|
59
|
+
this.clientFinalizer = new FinalizationRegistry((undo) => {
|
|
60
|
+
// Deactivate the owner too: saved factories must remain inert after collection.
|
|
61
|
+
undo();
|
|
62
|
+
this.clientUndo.delete(undo);
|
|
63
|
+
});
|
|
64
|
+
/** Thread undo functions hold only WeakRefs; collected threads release their bookkeeping. */
|
|
65
|
+
this.threadUndo = new Set();
|
|
66
|
+
this.threadFinalizer = new FinalizationRegistry((undo) => {
|
|
67
|
+
this.threadUndo.delete(undo);
|
|
68
|
+
});
|
|
69
|
+
this.enabled = true;
|
|
70
|
+
this.traceConfig = config.traceConfig;
|
|
71
|
+
this.tracerProvider = config.tracerProvider;
|
|
72
|
+
}
|
|
73
|
+
setTracerProvider(tracerProvider) {
|
|
74
|
+
this.tracerProvider = tracerProvider;
|
|
75
|
+
}
|
|
76
|
+
/** The content policy this instrumentation applies. */
|
|
77
|
+
get contentPolicy() {
|
|
78
|
+
return resolveContentPolicy(this.traceConfig);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Patch `module.Thread.prototype`. Pass the SDK module namespace
|
|
82
|
+
* (`import * as CodexSDK from "@openai/codex-sdk"`) or `{ Thread }`.
|
|
83
|
+
* Returns `true` when the class was patched by this call.
|
|
84
|
+
*/
|
|
85
|
+
manuallyInstrument(module) {
|
|
86
|
+
var _a;
|
|
87
|
+
const proto = (_a = module === null || module === void 0 ? void 0 : module.Thread) === null || _a === void 0 ? void 0 : _a.prototype;
|
|
88
|
+
if (!proto) {
|
|
89
|
+
diag.warn(`${INSTRUMENTATION_NAME}: manuallyInstrument() expects the @openai/codex-sdk module (no Thread export found)`);
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
this.enabled = true;
|
|
93
|
+
const undo = patchThread(proto, () => this.tracingContext());
|
|
94
|
+
if (undo)
|
|
95
|
+
this.undo.push(undo);
|
|
96
|
+
return !!undo;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Patch one Thread instance. Returns the same instance. Only a weak reference
|
|
100
|
+
* is kept, so `unpatch()` can restore live threads without retaining dropped ones.
|
|
101
|
+
*/
|
|
102
|
+
wrapThread(thread) {
|
|
103
|
+
this.enabled = true;
|
|
104
|
+
const undo = patchThread(thread, () => this.tracingContext());
|
|
105
|
+
if (undo) {
|
|
106
|
+
this.threadUndo.add(undo);
|
|
107
|
+
this.threadFinalizer.register(thread, undo, undo);
|
|
108
|
+
}
|
|
109
|
+
return thread;
|
|
110
|
+
}
|
|
111
|
+
/** Patch one Codex instance so every thread it starts or resumes is traced. Returns the same instance. */
|
|
112
|
+
wrapCodex(codex) {
|
|
113
|
+
if (!codex || typeof codex !== "object")
|
|
114
|
+
return codex;
|
|
115
|
+
this.enabled = true;
|
|
116
|
+
const undo = patchCodex(codex, (thread) => this.wrapThread(thread));
|
|
117
|
+
if (undo) {
|
|
118
|
+
this.clientUndo.add(undo);
|
|
119
|
+
this.clientFinalizer.register(codex, undo, undo);
|
|
120
|
+
}
|
|
121
|
+
return codex;
|
|
122
|
+
}
|
|
123
|
+
/** Remove every patch this instrumentation applied. */
|
|
124
|
+
unpatch() {
|
|
125
|
+
this.enabled = false;
|
|
126
|
+
for (const undo of this.threadUndo) {
|
|
127
|
+
this.threadFinalizer.unregister(undo);
|
|
128
|
+
undo();
|
|
129
|
+
}
|
|
130
|
+
this.threadUndo.clear();
|
|
131
|
+
for (const undo of this.clientUndo) {
|
|
132
|
+
this.clientFinalizer.unregister(undo);
|
|
133
|
+
try {
|
|
134
|
+
undo();
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
diag.debug(`${INSTRUMENTATION_NAME}: unpatch failed: ${error}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
this.clientUndo.clear();
|
|
141
|
+
while (this.undo.length) {
|
|
142
|
+
const fn = this.undo.pop();
|
|
143
|
+
try {
|
|
144
|
+
fn === null || fn === void 0 ? void 0 : fn();
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
diag.debug(`${INSTRUMENTATION_NAME}: unpatch failed: ${error}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Flush the provider's span processors. Does not shut the provider down. */
|
|
152
|
+
async forceFlush() {
|
|
153
|
+
var _a;
|
|
154
|
+
await flushProvider((_a = this.tracerProvider) !== null && _a !== void 0 ? _a : trace.getTracerProvider());
|
|
155
|
+
}
|
|
156
|
+
tracingContext() {
|
|
157
|
+
var _a;
|
|
158
|
+
if (!this.enabled)
|
|
159
|
+
return undefined;
|
|
160
|
+
const policy = this.contentPolicy;
|
|
161
|
+
const provider = (_a = this.tracerProvider) !== null && _a !== void 0 ? _a : trace.getTracerProvider();
|
|
162
|
+
const tracer = new FITracer({
|
|
163
|
+
tracer: provider.getTracer(INSTRUMENTATION_NAME, VERSION),
|
|
164
|
+
traceConfig: Object.assign(Object.assign({}, this.traceConfig), { hideInputs: policy.hideInputs, hideOutputs: policy.hideOutputs, hideErrorDetails: policy.hideErrorDetails }),
|
|
165
|
+
});
|
|
166
|
+
return { tracer, policy };
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** Python-style convenience constructor. */
|
|
170
|
+
export function instrumentCodexSDK(module, config = {}) {
|
|
171
|
+
const instrumentation = new CodexSDKInstrumentation(config);
|
|
172
|
+
instrumentation.manuallyInstrument(module);
|
|
173
|
+
return instrumentation;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Flush spans before a short script exits. Export errors are logged, never
|
|
177
|
+
* thrown: export failure does not fail the agent.
|
|
178
|
+
*/
|
|
179
|
+
export async function shutdown(tracerProvider) {
|
|
180
|
+
await flushProvider(tracerProvider !== null && tracerProvider !== void 0 ? tracerProvider : trace.getTracerProvider());
|
|
181
|
+
}
|
|
182
|
+
function resolveDelegate(provider) {
|
|
183
|
+
const maybeProxy = provider;
|
|
184
|
+
if (typeof maybeProxy.forceFlush !== "function" && typeof maybeProxy.getDelegate === "function") {
|
|
185
|
+
return maybeProxy.getDelegate();
|
|
186
|
+
}
|
|
187
|
+
return provider;
|
|
188
|
+
}
|
|
189
|
+
async function flushProvider(provider) {
|
|
190
|
+
const target = resolveDelegate(provider);
|
|
191
|
+
if (typeof target.forceFlush !== "function")
|
|
192
|
+
return;
|
|
193
|
+
try {
|
|
194
|
+
await target.forceFlush();
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
diag.warn(`${INSTRUMENTATION_NAME}: forceFlush failed: ${error}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=instrumentation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":"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