@cultured_computer/sight 0.5.0 → 0.6.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 +43 -28
- package/dist/cli.js +10 -4
- package/dist/index.cjs +360 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -1
- package/dist/index.d.ts +37 -1
- package/dist/index.js +360 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -5
package/dist/index.d.cts
CHANGED
|
@@ -1422,4 +1422,40 @@ declare class Probe {
|
|
|
1422
1422
|
private static shouldSample;
|
|
1423
1423
|
}
|
|
1424
1424
|
|
|
1425
|
-
|
|
1425
|
+
/**
|
|
1426
|
+
* Explicit OpenAI client wrapping — the autocapture path that cannot miss.
|
|
1427
|
+
*
|
|
1428
|
+
* Module patching (registerOpenAI → @traceloop/instrumentation-openai) only
|
|
1429
|
+
* attaches under CommonJS, only when `openai` is loaded after Probe.init(),
|
|
1430
|
+
* and only for openai <5 (the instrumentation's declared supportedVersions).
|
|
1431
|
+
* A modern ESM app on openai v5+ therefore captures nothing through the
|
|
1432
|
+
* patch path, and did so silently until registerOpenAI grew its post-enable
|
|
1433
|
+
* verification. wrapOpenAI() is the documented default instead: it wraps
|
|
1434
|
+
* the client INSTANCE the customer already holds, which is immune to module
|
|
1435
|
+
* format, load order, and library version.
|
|
1436
|
+
*
|
|
1437
|
+
* import OpenAI from 'openai';
|
|
1438
|
+
* import { Probe, wrapOpenAI } from '@cultured_computer/sight';
|
|
1439
|
+
* const openai = wrapOpenAI(new OpenAI());
|
|
1440
|
+
*
|
|
1441
|
+
* Covers chat.completions.create and responses.create, streaming included:
|
|
1442
|
+
* the stream is teed, chunks reach the caller unchanged, and the turn is
|
|
1443
|
+
* emitted when the stream completes. Turns flow through the same session
|
|
1444
|
+
* resolution and captureTurn path as the OTel processor, so payload keys
|
|
1445
|
+
* are identical downstream.
|
|
1446
|
+
*
|
|
1447
|
+
* Binding: registerAllInstrumentations() binds this module to live Probe
|
|
1448
|
+
* state at init. wrapOpenAI() may be called before init — the wrap is
|
|
1449
|
+
* inert (pure passthrough) until the binding lands, and warns once if a
|
|
1450
|
+
* call happens while unbound so a missing init is never silent.
|
|
1451
|
+
*/
|
|
1452
|
+
|
|
1453
|
+
/**
|
|
1454
|
+
* Wrap an OpenAI client instance so every chat.completions.create and
|
|
1455
|
+
* responses.create call is captured as a Sight turn. Returns the same
|
|
1456
|
+
* client. Idempotent; safe to call before Probe.init() (capture starts
|
|
1457
|
+
* once init binds).
|
|
1458
|
+
*/
|
|
1459
|
+
declare function wrapOpenAI<T extends object>(client: T): T;
|
|
1460
|
+
|
|
1461
|
+
export { type AlignmentProvenance, type CapturedTurn, type EnvironmentEventInput, type OutcomeEventEmit, type OutcomeHook, type OutcomeResult, type PersonaConfig, Probe, type ProbeConfig, type ProbeConfigInput, Probe as Sight, type AlignmentProvenance as SightAlignmentProvenance, type CapturedTurn as SightCapturedTurn, type ProbeConfig as SightConfig, type ProbeConfigInput as SightConfigInput, type EnvironmentEventInput as SightEnvironmentEventInput, type OutcomeEventEmit as SightOutcomeEventEmit, type OutcomeHook as SightOutcomeHook, type OutcomeResult as SightOutcomeResult, type PersonaConfig as SightPersonaConfig, type SpanInput as SightSpanInput, type SpansBatch as SightSpansBatch, type SpanInput, type SpansBatch, wrapOpenAI };
|
package/dist/index.d.ts
CHANGED
|
@@ -1422,4 +1422,40 @@ declare class Probe {
|
|
|
1422
1422
|
private static shouldSample;
|
|
1423
1423
|
}
|
|
1424
1424
|
|
|
1425
|
-
|
|
1425
|
+
/**
|
|
1426
|
+
* Explicit OpenAI client wrapping — the autocapture path that cannot miss.
|
|
1427
|
+
*
|
|
1428
|
+
* Module patching (registerOpenAI → @traceloop/instrumentation-openai) only
|
|
1429
|
+
* attaches under CommonJS, only when `openai` is loaded after Probe.init(),
|
|
1430
|
+
* and only for openai <5 (the instrumentation's declared supportedVersions).
|
|
1431
|
+
* A modern ESM app on openai v5+ therefore captures nothing through the
|
|
1432
|
+
* patch path, and did so silently until registerOpenAI grew its post-enable
|
|
1433
|
+
* verification. wrapOpenAI() is the documented default instead: it wraps
|
|
1434
|
+
* the client INSTANCE the customer already holds, which is immune to module
|
|
1435
|
+
* format, load order, and library version.
|
|
1436
|
+
*
|
|
1437
|
+
* import OpenAI from 'openai';
|
|
1438
|
+
* import { Probe, wrapOpenAI } from '@cultured_computer/sight';
|
|
1439
|
+
* const openai = wrapOpenAI(new OpenAI());
|
|
1440
|
+
*
|
|
1441
|
+
* Covers chat.completions.create and responses.create, streaming included:
|
|
1442
|
+
* the stream is teed, chunks reach the caller unchanged, and the turn is
|
|
1443
|
+
* emitted when the stream completes. Turns flow through the same session
|
|
1444
|
+
* resolution and captureTurn path as the OTel processor, so payload keys
|
|
1445
|
+
* are identical downstream.
|
|
1446
|
+
*
|
|
1447
|
+
* Binding: registerAllInstrumentations() binds this module to live Probe
|
|
1448
|
+
* state at init. wrapOpenAI() may be called before init — the wrap is
|
|
1449
|
+
* inert (pure passthrough) until the binding lands, and warns once if a
|
|
1450
|
+
* call happens while unbound so a missing init is never silent.
|
|
1451
|
+
*/
|
|
1452
|
+
|
|
1453
|
+
/**
|
|
1454
|
+
* Wrap an OpenAI client instance so every chat.completions.create and
|
|
1455
|
+
* responses.create call is captured as a Sight turn. Returns the same
|
|
1456
|
+
* client. Idempotent; safe to call before Probe.init() (capture starts
|
|
1457
|
+
* once init binds).
|
|
1458
|
+
*/
|
|
1459
|
+
declare function wrapOpenAI<T extends object>(client: T): T;
|
|
1460
|
+
|
|
1461
|
+
export { type AlignmentProvenance, type CapturedTurn, type EnvironmentEventInput, type OutcomeEventEmit, type OutcomeHook, type OutcomeResult, type PersonaConfig, Probe, type ProbeConfig, type ProbeConfigInput, Probe as Sight, type AlignmentProvenance as SightAlignmentProvenance, type CapturedTurn as SightCapturedTurn, type ProbeConfig as SightConfig, type ProbeConfigInput as SightConfigInput, type EnvironmentEventInput as SightEnvironmentEventInput, type OutcomeEventEmit as SightOutcomeEventEmit, type OutcomeHook as SightOutcomeHook, type OutcomeResult as SightOutcomeResult, type PersonaConfig as SightPersonaConfig, type SpanInput as SightSpanInput, type SpansBatch as SightSpansBatch, type SpanInput, type SpansBatch, wrapOpenAI };
|
package/dist/index.js
CHANGED
|
@@ -731,8 +731,10 @@ function extractSystemPromptText(attrs) {
|
|
|
731
731
|
return void 0;
|
|
732
732
|
}
|
|
733
733
|
async function maybeAutoStampEnvironmentConfig(session, attrs) {
|
|
734
|
+
await autoStampFromSystemText(session, extractSystemPromptText(attrs));
|
|
735
|
+
}
|
|
736
|
+
async function autoStampFromSystemText(session, systemText) {
|
|
734
737
|
if (session.environmentConfigHash != null) return;
|
|
735
|
-
const systemText = extractSystemPromptText(attrs);
|
|
736
738
|
if (!systemText) return;
|
|
737
739
|
const hash = await hashEnvironmentConfig(systemText);
|
|
738
740
|
if (session.environmentConfigHash != null) return;
|
|
@@ -837,6 +839,342 @@ var ProbeSpanProcessor = class {
|
|
|
837
839
|
}
|
|
838
840
|
};
|
|
839
841
|
|
|
842
|
+
// src/instrumentation/wrap-openai.ts
|
|
843
|
+
var bound = null;
|
|
844
|
+
var warnedUnbound = false;
|
|
845
|
+
function bindWrapOpenAI(state2, captureFn) {
|
|
846
|
+
bound = { state: state2, captureFn };
|
|
847
|
+
}
|
|
848
|
+
function unbindWrapOpenAI() {
|
|
849
|
+
bound = null;
|
|
850
|
+
}
|
|
851
|
+
var WRAPPED = /* @__PURE__ */ Symbol.for("sight.wrapOpenAI");
|
|
852
|
+
function asRecord(v) {
|
|
853
|
+
return v !== null && typeof v === "object" ? v : void 0;
|
|
854
|
+
}
|
|
855
|
+
function asString2(v) {
|
|
856
|
+
return typeof v === "string" ? v : void 0;
|
|
857
|
+
}
|
|
858
|
+
function asNumber2(v) {
|
|
859
|
+
return typeof v === "number" ? v : void 0;
|
|
860
|
+
}
|
|
861
|
+
function messageText2(content) {
|
|
862
|
+
if (typeof content === "string") return content;
|
|
863
|
+
if (Array.isArray(content)) {
|
|
864
|
+
const parts = [];
|
|
865
|
+
for (const part of content) {
|
|
866
|
+
if (typeof part === "string") parts.push(part);
|
|
867
|
+
else {
|
|
868
|
+
const p = asRecord(part);
|
|
869
|
+
const text = p?.["text"] ?? p?.["content"];
|
|
870
|
+
if (typeof text === "string") parts.push(text);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
return parts.length > 0 ? parts.join("\n") : void 0;
|
|
874
|
+
}
|
|
875
|
+
return void 0;
|
|
876
|
+
}
|
|
877
|
+
function systemTextFromParams(params) {
|
|
878
|
+
if (!params) return void 0;
|
|
879
|
+
const instructions = asString2(params["instructions"]);
|
|
880
|
+
if (instructions && instructions.length > 0) return instructions;
|
|
881
|
+
const messages = params["messages"];
|
|
882
|
+
if (Array.isArray(messages)) {
|
|
883
|
+
for (const raw of messages) {
|
|
884
|
+
const m = asRecord(raw);
|
|
885
|
+
if (m && (m["role"] === "system" || m["role"] === "developer")) {
|
|
886
|
+
const text = messageText2(m["content"]);
|
|
887
|
+
if (text) return text;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
return void 0;
|
|
892
|
+
}
|
|
893
|
+
function providerFor(model) {
|
|
894
|
+
return model?.toLowerCase().startsWith("mercury") ? "inception" : "openai";
|
|
895
|
+
}
|
|
896
|
+
function payloadFromChatResponse(r, requestModel) {
|
|
897
|
+
const choice = Array.isArray(r["choices"]) ? asRecord(r["choices"][0]) : void 0;
|
|
898
|
+
const message = asRecord(choice?.["message"]);
|
|
899
|
+
const usage = asRecord(r["usage"]);
|
|
900
|
+
const promptDetails = asRecord(usage?.["prompt_tokens_details"]);
|
|
901
|
+
const toolCalls = Array.isArray(message?.["tool_calls"]) ? message["tool_calls"] : void 0;
|
|
902
|
+
const toolNames = toolCalls?.map((t) => asString2(asRecord(asRecord(t)?.["function"])?.["name"])).filter((n) => typeof n === "string");
|
|
903
|
+
return {
|
|
904
|
+
model: asString2(r["model"]) ?? requestModel,
|
|
905
|
+
text: messageText2(message?.["content"]),
|
|
906
|
+
tokensInput: asNumber2(usage?.["prompt_tokens"]),
|
|
907
|
+
tokensOutput: asNumber2(usage?.["completion_tokens"]),
|
|
908
|
+
cachedTokens: asNumber2(promptDetails?.["cached_tokens"]),
|
|
909
|
+
finishReason: asString2(choice?.["finish_reason"]),
|
|
910
|
+
toolNames
|
|
911
|
+
};
|
|
912
|
+
}
|
|
913
|
+
function payloadFromResponsesResponse(r, requestModel) {
|
|
914
|
+
const usage = asRecord(r["usage"]);
|
|
915
|
+
const inputDetails = asRecord(usage?.["input_tokens_details"]);
|
|
916
|
+
let text = asString2(r["output_text"]);
|
|
917
|
+
const toolNames = [];
|
|
918
|
+
if (Array.isArray(r["output"])) {
|
|
919
|
+
const texts = [];
|
|
920
|
+
for (const raw of r["output"]) {
|
|
921
|
+
const item = asRecord(raw);
|
|
922
|
+
if (!item) continue;
|
|
923
|
+
if (item["type"] === "function_call") {
|
|
924
|
+
const name = asString2(item["name"]);
|
|
925
|
+
if (name) toolNames.push(name);
|
|
926
|
+
}
|
|
927
|
+
if (item["type"] === "message" && Array.isArray(item["content"])) {
|
|
928
|
+
for (const c of item["content"]) {
|
|
929
|
+
const cr = asRecord(c);
|
|
930
|
+
if (cr?.["type"] === "output_text") {
|
|
931
|
+
const t = asString2(cr["text"]);
|
|
932
|
+
if (t) texts.push(t);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
if (!text && texts.length > 0) text = texts.join("\n");
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
model: asString2(r["model"]) ?? requestModel,
|
|
941
|
+
text,
|
|
942
|
+
tokensInput: asNumber2(usage?.["input_tokens"]),
|
|
943
|
+
tokensOutput: asNumber2(usage?.["output_tokens"]),
|
|
944
|
+
cachedTokens: asNumber2(inputDetails?.["cached_tokens"]),
|
|
945
|
+
finishReason: asString2(r["status"]),
|
|
946
|
+
toolNames: toolNames.length > 0 ? toolNames : void 0
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
function buildTurnPayload(x, latencyMs, api) {
|
|
950
|
+
return {
|
|
951
|
+
probe_tokens_input: x.tokensInput,
|
|
952
|
+
probe_tokens_output: x.tokensOutput,
|
|
953
|
+
probe_latency_ms: latencyMs,
|
|
954
|
+
probe_model: x.model,
|
|
955
|
+
probe_provider: providerFor(x.model),
|
|
956
|
+
probe_tool_calls: x.toolNames?.length,
|
|
957
|
+
probe_tool_names: x.toolNames,
|
|
958
|
+
probe_error: void 0,
|
|
959
|
+
probe_finish_reason: x.finishReason,
|
|
960
|
+
response: x.text ? { text: x.text } : void 0,
|
|
961
|
+
probe_cache_read_input_tokens: x.cachedTokens,
|
|
962
|
+
_wrap: { api, adapter: "wrapOpenAI" }
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
function emit(params, payload) {
|
|
966
|
+
if (!bound) {
|
|
967
|
+
if (!warnedUnbound) {
|
|
968
|
+
warnedUnbound = true;
|
|
969
|
+
console.warn(
|
|
970
|
+
"[sight] wrapOpenAI: call observed before Probe.init(); nothing captured until init runs."
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
return;
|
|
974
|
+
}
|
|
975
|
+
const { state: state2, captureFn } = bound;
|
|
976
|
+
const sessionId = getCurrentSessionId();
|
|
977
|
+
if (!sessionId) {
|
|
978
|
+
if (state2.config.debug) {
|
|
979
|
+
console.warn(
|
|
980
|
+
"[sight] wrapOpenAI: call with no session context; skipped (wrap calls in Probe.withSession())."
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
const { session, sessionExternalId, turnIndex, personaSlug } = resolveSessionAndTurn(
|
|
986
|
+
state2,
|
|
987
|
+
sessionId
|
|
988
|
+
);
|
|
989
|
+
const captured = {
|
|
990
|
+
deploymentSlug: state2.config.deployment,
|
|
991
|
+
sessionExternalId,
|
|
992
|
+
turnIndex,
|
|
993
|
+
personaSlug: personaSlug ?? void 0,
|
|
994
|
+
payload
|
|
995
|
+
};
|
|
996
|
+
void (async () => {
|
|
997
|
+
await autoStampFromSystemText(session, systemTextFromParams(params));
|
|
998
|
+
await captureFn(captured);
|
|
999
|
+
})().catch((err) => {
|
|
1000
|
+
if (state2.config.debug) {
|
|
1001
|
+
console.error(`[sight] wrapOpenAI capture error: ${err instanceof Error ? err.message : err}`);
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
function teeStream(stream, onChunk, onDone) {
|
|
1006
|
+
let done = false;
|
|
1007
|
+
const finish = () => {
|
|
1008
|
+
if (!done) {
|
|
1009
|
+
done = true;
|
|
1010
|
+
onDone();
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
return new Proxy(stream, {
|
|
1014
|
+
get(target, prop, receiver) {
|
|
1015
|
+
if (prop === Symbol.asyncIterator) {
|
|
1016
|
+
return function() {
|
|
1017
|
+
const inner = target[Symbol.asyncIterator]();
|
|
1018
|
+
return {
|
|
1019
|
+
async next() {
|
|
1020
|
+
try {
|
|
1021
|
+
const r = await inner.next();
|
|
1022
|
+
if (r.done) finish();
|
|
1023
|
+
else onChunk(r.value);
|
|
1024
|
+
return r;
|
|
1025
|
+
} catch (err) {
|
|
1026
|
+
finish();
|
|
1027
|
+
throw err;
|
|
1028
|
+
}
|
|
1029
|
+
},
|
|
1030
|
+
async return(value2) {
|
|
1031
|
+
finish();
|
|
1032
|
+
return inner.return ? inner.return(value2) : { done: true, value: value2 };
|
|
1033
|
+
},
|
|
1034
|
+
async throw(err) {
|
|
1035
|
+
finish();
|
|
1036
|
+
if (inner.throw) return inner.throw(err);
|
|
1037
|
+
throw err;
|
|
1038
|
+
}
|
|
1039
|
+
};
|
|
1040
|
+
};
|
|
1041
|
+
}
|
|
1042
|
+
const value = Reflect.get(target, prop, receiver);
|
|
1043
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
}
|
|
1047
|
+
function chatStreamAccumulator(requestModel) {
|
|
1048
|
+
const texts = [];
|
|
1049
|
+
let model = requestModel;
|
|
1050
|
+
let finishReason;
|
|
1051
|
+
let tokensInput;
|
|
1052
|
+
let tokensOutput;
|
|
1053
|
+
let cachedTokens;
|
|
1054
|
+
return {
|
|
1055
|
+
onChunk(chunk) {
|
|
1056
|
+
const c = asRecord(chunk);
|
|
1057
|
+
if (!c) return;
|
|
1058
|
+
model = asString2(c["model"]) ?? model;
|
|
1059
|
+
const choice = Array.isArray(c["choices"]) ? asRecord(c["choices"][0]) : void 0;
|
|
1060
|
+
const delta = asRecord(choice?.["delta"]);
|
|
1061
|
+
const t = asString2(delta?.["content"]);
|
|
1062
|
+
if (t) texts.push(t);
|
|
1063
|
+
finishReason = asString2(choice?.["finish_reason"]) ?? finishReason;
|
|
1064
|
+
const usage = asRecord(c["usage"]);
|
|
1065
|
+
if (usage) {
|
|
1066
|
+
tokensInput = asNumber2(usage["prompt_tokens"]) ?? tokensInput;
|
|
1067
|
+
tokensOutput = asNumber2(usage["completion_tokens"]) ?? tokensOutput;
|
|
1068
|
+
cachedTokens = asNumber2(asRecord(usage["prompt_tokens_details"])?.["cached_tokens"]) ?? cachedTokens;
|
|
1069
|
+
}
|
|
1070
|
+
},
|
|
1071
|
+
result: () => ({
|
|
1072
|
+
model,
|
|
1073
|
+
text: texts.length > 0 ? texts.join("") : void 0,
|
|
1074
|
+
tokensInput,
|
|
1075
|
+
tokensOutput,
|
|
1076
|
+
cachedTokens,
|
|
1077
|
+
finishReason,
|
|
1078
|
+
toolNames: void 0
|
|
1079
|
+
})
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
function responsesStreamAccumulator(requestModel) {
|
|
1083
|
+
const texts = [];
|
|
1084
|
+
let final = null;
|
|
1085
|
+
return {
|
|
1086
|
+
onChunk(chunk) {
|
|
1087
|
+
const c = asRecord(chunk);
|
|
1088
|
+
if (!c) return;
|
|
1089
|
+
if (c["type"] === "response.output_text.delta") {
|
|
1090
|
+
const t = asString2(c["delta"]);
|
|
1091
|
+
if (t) texts.push(t);
|
|
1092
|
+
}
|
|
1093
|
+
if (c["type"] === "response.completed") {
|
|
1094
|
+
const response = asRecord(c["response"]);
|
|
1095
|
+
if (response) final = payloadFromResponsesResponse(response, requestModel);
|
|
1096
|
+
}
|
|
1097
|
+
},
|
|
1098
|
+
result: () => {
|
|
1099
|
+
if (final) {
|
|
1100
|
+
return { ...final, text: final.text ?? (texts.length > 0 ? texts.join("") : void 0) };
|
|
1101
|
+
}
|
|
1102
|
+
return {
|
|
1103
|
+
model: requestModel,
|
|
1104
|
+
text: texts.length > 0 ? texts.join("") : void 0,
|
|
1105
|
+
tokensInput: void 0,
|
|
1106
|
+
tokensOutput: void 0,
|
|
1107
|
+
cachedTokens: void 0,
|
|
1108
|
+
finishReason: void 0,
|
|
1109
|
+
toolNames: void 0
|
|
1110
|
+
};
|
|
1111
|
+
}
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
function wrapCreate(target, api, makeAccumulator, extractResponse) {
|
|
1115
|
+
const original = target["create"];
|
|
1116
|
+
if (typeof original !== "function") return;
|
|
1117
|
+
target["create"] = function(...args) {
|
|
1118
|
+
const params = asRecord(args[0]);
|
|
1119
|
+
const requestModel = asString2(params?.["model"]);
|
|
1120
|
+
const startedAt = Date.now();
|
|
1121
|
+
const result = original.apply(this, args);
|
|
1122
|
+
if (!result || typeof result.then !== "function") return result;
|
|
1123
|
+
return result.then(
|
|
1124
|
+
(resolved) => {
|
|
1125
|
+
try {
|
|
1126
|
+
const isStream = resolved !== null && typeof resolved === "object" && Symbol.asyncIterator in resolved;
|
|
1127
|
+
if (isStream) {
|
|
1128
|
+
const acc = makeAccumulator(requestModel);
|
|
1129
|
+
return teeStream(resolved, acc.onChunk, () => {
|
|
1130
|
+
emit(params, buildTurnPayload(acc.result(), Date.now() - startedAt, api));
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
const r = asRecord(resolved);
|
|
1134
|
+
if (r) {
|
|
1135
|
+
emit(params, buildTurnPayload(extractResponse(r, requestModel), Date.now() - startedAt, api));
|
|
1136
|
+
}
|
|
1137
|
+
return resolved;
|
|
1138
|
+
} catch {
|
|
1139
|
+
return resolved;
|
|
1140
|
+
}
|
|
1141
|
+
},
|
|
1142
|
+
(err) => {
|
|
1143
|
+
try {
|
|
1144
|
+
const x = {
|
|
1145
|
+
model: requestModel,
|
|
1146
|
+
text: void 0,
|
|
1147
|
+
tokensInput: void 0,
|
|
1148
|
+
tokensOutput: void 0,
|
|
1149
|
+
cachedTokens: void 0,
|
|
1150
|
+
finishReason: void 0,
|
|
1151
|
+
toolNames: void 0
|
|
1152
|
+
};
|
|
1153
|
+
const payload = buildTurnPayload(x, Date.now() - startedAt, api);
|
|
1154
|
+
payload.probe_error = err instanceof Error ? err.message : String(err);
|
|
1155
|
+
emit(params, payload);
|
|
1156
|
+
} catch {
|
|
1157
|
+
}
|
|
1158
|
+
throw err;
|
|
1159
|
+
}
|
|
1160
|
+
);
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
function wrapOpenAI(client) {
|
|
1164
|
+
const c = client;
|
|
1165
|
+
if (c[WRAPPED]) return client;
|
|
1166
|
+
c[WRAPPED] = true;
|
|
1167
|
+
const completions = asRecord(asRecord(c["chat"])?.["completions"]);
|
|
1168
|
+
if (completions) {
|
|
1169
|
+
wrapCreate(completions, "chat", chatStreamAccumulator, payloadFromChatResponse);
|
|
1170
|
+
}
|
|
1171
|
+
const responses = asRecord(c["responses"]);
|
|
1172
|
+
if (responses) {
|
|
1173
|
+
wrapCreate(responses, "responses", responsesStreamAccumulator, payloadFromResponsesResponse);
|
|
1174
|
+
}
|
|
1175
|
+
return client;
|
|
1176
|
+
}
|
|
1177
|
+
|
|
840
1178
|
// src/instrumentation/setup.ts
|
|
841
1179
|
var installedProcessor = null;
|
|
842
1180
|
function setupOtelProvider(state2, captureFn) {
|
|
@@ -859,6 +1197,7 @@ function setupOtelProvider(state2, captureFn) {
|
|
|
859
1197
|
}
|
|
860
1198
|
function detachOtelState() {
|
|
861
1199
|
installedProcessor?.rebind(null);
|
|
1200
|
+
unbindWrapOpenAI();
|
|
862
1201
|
}
|
|
863
1202
|
|
|
864
1203
|
// src/instrumentation/openai.ts
|
|
@@ -880,6 +1219,7 @@ async function registerOpenAI(state2) {
|
|
|
880
1219
|
});
|
|
881
1220
|
instrumentation.enable();
|
|
882
1221
|
registered = true;
|
|
1222
|
+
void verifyPatchAttached();
|
|
883
1223
|
} catch (err) {
|
|
884
1224
|
if (state2.config.debug) {
|
|
885
1225
|
const msg = err instanceof Error ? err.message : String(err);
|
|
@@ -887,6 +1227,23 @@ async function registerOpenAI(state2) {
|
|
|
887
1227
|
}
|
|
888
1228
|
}
|
|
889
1229
|
}
|
|
1230
|
+
function isPatchDetached(mod) {
|
|
1231
|
+
const OpenAIClass = mod?.default ?? mod?.OpenAI;
|
|
1232
|
+
const create = OpenAIClass?.Chat?.Completions?.prototype?.create;
|
|
1233
|
+
if (typeof create !== "function") return true;
|
|
1234
|
+
return create.__wrapped !== true;
|
|
1235
|
+
}
|
|
1236
|
+
async function verifyPatchAttached() {
|
|
1237
|
+
try {
|
|
1238
|
+
const mod = await import('openai');
|
|
1239
|
+
if (!isPatchDetached(mod)) return;
|
|
1240
|
+
const version = mod?.default?.VERSION ?? mod?.VERSION;
|
|
1241
|
+
console.warn(
|
|
1242
|
+
`[sight] OpenAI autocapture via module patching did not attach` + (version ? ` (openai v${version})` : "") + `. This happens on openai >=5, under ESM, or when 'openai' loads before init. Wrap your client instead: import { wrapOpenAI } from '@cultured_computer/sight'; const client = wrapOpenAI(new OpenAI()). Until then, no turns are captured from this client.`
|
|
1243
|
+
);
|
|
1244
|
+
} catch {
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
890
1247
|
|
|
891
1248
|
// src/instrumentation/anthropic.ts
|
|
892
1249
|
var registered2 = false;
|
|
@@ -995,6 +1352,7 @@ async function registerMercury(state2) {
|
|
|
995
1352
|
// src/instrumentation/index.ts
|
|
996
1353
|
function registerAllInstrumentations(state2, captureFn) {
|
|
997
1354
|
setupOtelProvider(state2, captureFn);
|
|
1355
|
+
bindWrapOpenAI(state2, captureFn);
|
|
998
1356
|
const registrations = [
|
|
999
1357
|
registerOpenAI(state2),
|
|
1000
1358
|
registerAnthropic(state2),
|
|
@@ -2098,6 +2456,6 @@ var Probe = class {
|
|
|
2098
2456
|
}
|
|
2099
2457
|
};
|
|
2100
2458
|
|
|
2101
|
-
export { Probe, Probe as Sight };
|
|
2459
|
+
export { Probe, Probe as Sight, wrapOpenAI };
|
|
2102
2460
|
//# sourceMappingURL=index.js.map
|
|
2103
2461
|
//# sourceMappingURL=index.js.map
|