@withgateway/sdk 0.1.3 → 0.1.5
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/dist/context/index.mjs +3 -0
- package/dist/context/index.mjs.map +1 -1
- package/dist/experiments/index.mjs +2 -0
- package/dist/experiments/index.mjs.map +1 -1
- package/dist/index-DnowA8MG.d.mts +447 -0
- package/dist/index-DnowA8MG.d.ts +447 -0
- package/dist/index.d.mts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +1715 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1686 -19
- package/dist/index.mjs.map +1 -1
- package/dist/memory/index.mjs +3 -0
- package/dist/memory/index.mjs.map +1 -1
- package/dist/personas/index.d.mts +102 -0
- package/dist/personas/index.d.ts +102 -0
- package/dist/personas/index.js +205 -0
- package/dist/personas/index.js.map +1 -0
- package/dist/personas/index.mjs +180 -0
- package/dist/personas/index.mjs.map +1 -0
- package/dist/replay/index.d.mts +351 -0
- package/dist/replay/index.d.ts +351 -0
- package/dist/replay/index.js +1094 -0
- package/dist/replay/index.js.map +1 -0
- package/dist/replay/index.mjs +1057 -0
- package/dist/replay/index.mjs.map +1 -0
- package/dist/security/index.mjs +2 -0
- package/dist/security/index.mjs.map +1 -1
- package/dist/sessions/index.d.mts +143 -0
- package/dist/sessions/index.d.ts +143 -0
- package/dist/sessions/index.js +320 -0
- package/dist/sessions/index.js.map +1 -0
- package/dist/sessions/index.mjs +288 -0
- package/dist/sessions/index.mjs.map +1 -0
- package/dist/tracing/index.d.mts +3 -1
- package/dist/tracing/index.d.ts +3 -1
- package/dist/tracing/index.js +435 -8
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +431 -8
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/users/index.d.mts +81 -0
- package/dist/users/index.d.ts +81 -0
- package/dist/users/index.js +168 -0
- package/dist/users/index.js.map +1 -0
- package/dist/users/index.mjs +144 -0
- package/dist/users/index.mjs.map +1 -0
- package/package.json +25 -1
- package/dist/index-Caw3XVub.d.mts +0 -193
- package/dist/index-Caw3XVub.d.ts +0 -193
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createRequire as __gatewayCreateRequire } from "module";
|
|
2
|
+
const require = __gatewayCreateRequire(import.meta.url);
|
|
1
3
|
var __defProp = Object.defineProperty;
|
|
2
4
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
5
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -46,14 +48,14 @@ var experimentContextStorage = new AsyncLocalStorage();
|
|
|
46
48
|
function getExperimentContext() {
|
|
47
49
|
return experimentContextStorage.getStore();
|
|
48
50
|
}
|
|
49
|
-
function setExperimentContext(
|
|
50
|
-
experimentContextStorage.enterWith(
|
|
51
|
+
function setExperimentContext(context4) {
|
|
52
|
+
experimentContextStorage.enterWith(context4);
|
|
51
53
|
}
|
|
52
54
|
function clearExperimentContext() {
|
|
53
55
|
experimentContextStorage.enterWith(void 0);
|
|
54
56
|
}
|
|
55
|
-
function runWithExperimentContext(
|
|
56
|
-
return experimentContextStorage.run(
|
|
57
|
+
function runWithExperimentContext(context4, fn) {
|
|
58
|
+
return experimentContextStorage.run(context4, fn);
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
// src/experiments/exposure-logger.ts
|
|
@@ -233,17 +235,17 @@ var ExperimentClient = class {
|
|
|
233
235
|
variantKey: variant.key,
|
|
234
236
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
235
237
|
});
|
|
236
|
-
const
|
|
238
|
+
const context4 = {
|
|
237
239
|
experimentId: experimentKey,
|
|
238
240
|
variantKey: variant.key,
|
|
239
241
|
userId
|
|
240
242
|
};
|
|
241
|
-
setExperimentContext(
|
|
243
|
+
setExperimentContext(context4);
|
|
242
244
|
return {
|
|
243
245
|
variant,
|
|
244
246
|
inExperiment: true,
|
|
245
247
|
experimentKey,
|
|
246
|
-
run: (fn) => runWithExperimentContext(
|
|
248
|
+
run: (fn) => runWithExperimentContext(context4, fn)
|
|
247
249
|
};
|
|
248
250
|
}
|
|
249
251
|
getConfig(experimentKey) {
|
|
@@ -925,6 +927,1218 @@ var memory = {
|
|
|
925
927
|
fromEnv: GatewayMemory.fromEnv
|
|
926
928
|
};
|
|
927
929
|
|
|
930
|
+
// src/personas/index.ts
|
|
931
|
+
var PersonaError = class extends Error {
|
|
932
|
+
constructor(message, options) {
|
|
933
|
+
super(message);
|
|
934
|
+
this.name = "PersonaError";
|
|
935
|
+
this.cause = options?.cause;
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
var Persona = class {
|
|
939
|
+
id;
|
|
940
|
+
name;
|
|
941
|
+
description;
|
|
942
|
+
version;
|
|
943
|
+
instructions;
|
|
944
|
+
examples;
|
|
945
|
+
client;
|
|
946
|
+
constructor(fields) {
|
|
947
|
+
this.id = fields.id;
|
|
948
|
+
this.name = fields.name;
|
|
949
|
+
this.description = fields.description;
|
|
950
|
+
this.version = fields.version;
|
|
951
|
+
this.instructions = fields.instructions;
|
|
952
|
+
this.examples = fields.examples ?? [];
|
|
953
|
+
this.client = fields.client ?? null;
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Get the persona's next user message for a conversation.
|
|
957
|
+
*
|
|
958
|
+
* `messages` use your agent's perspective: role `user` for the simulated
|
|
959
|
+
* user's prior turns, `assistant` for your agent's turns.
|
|
960
|
+
* `done` is true when the persona considers its goal met.
|
|
961
|
+
*/
|
|
962
|
+
respond(messages) {
|
|
963
|
+
if (this.client === null) {
|
|
964
|
+
throw new PersonaError("Persona is not bound to a client.");
|
|
965
|
+
}
|
|
966
|
+
return this.client.chat(this.id, messages);
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
var PersonasClient = class _PersonasClient {
|
|
970
|
+
host;
|
|
971
|
+
publicKey;
|
|
972
|
+
secretKey;
|
|
973
|
+
timeoutSeconds;
|
|
974
|
+
debug;
|
|
975
|
+
constructor(config) {
|
|
976
|
+
this.host = config.host.replace(/\/$/, "");
|
|
977
|
+
this.publicKey = config.publicKey;
|
|
978
|
+
this.secretKey = config.secretKey;
|
|
979
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 120;
|
|
980
|
+
this.debug = config.debug ?? false;
|
|
981
|
+
}
|
|
982
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
983
|
+
static fromEnv(config = {}) {
|
|
984
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
985
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
986
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
987
|
+
const missing = [
|
|
988
|
+
["host", host],
|
|
989
|
+
["publicKey", publicKey],
|
|
990
|
+
["secretKey", secretKey]
|
|
991
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
992
|
+
if (missing.length > 0) {
|
|
993
|
+
throw new PersonaError(
|
|
994
|
+
`Missing Gateway persona configuration: ${missing.join(", ")}`
|
|
995
|
+
);
|
|
996
|
+
}
|
|
997
|
+
return new _PersonasClient({
|
|
998
|
+
...config,
|
|
999
|
+
host,
|
|
1000
|
+
publicKey,
|
|
1001
|
+
secretKey
|
|
1002
|
+
});
|
|
1003
|
+
}
|
|
1004
|
+
/** List the project's personas (without full instructions). */
|
|
1005
|
+
async list() {
|
|
1006
|
+
const payload = await this.requestJson("GET", "/api/public/personas");
|
|
1007
|
+
const items = Array.isArray(payload.data) ? payload.data : [];
|
|
1008
|
+
return items.filter(isRecord).map(
|
|
1009
|
+
(item) => new Persona({
|
|
1010
|
+
id: String(item.id),
|
|
1011
|
+
name: String(item.name),
|
|
1012
|
+
description: item.description ?? null,
|
|
1013
|
+
version: typeof item.version === "number" ? item.version : 1,
|
|
1014
|
+
instructions: "",
|
|
1015
|
+
client: this
|
|
1016
|
+
})
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
/** Fetch a persona (full definition) bound to this client. */
|
|
1020
|
+
async get(personaId) {
|
|
1021
|
+
const item = await this.requestJson(
|
|
1022
|
+
"GET",
|
|
1023
|
+
`/api/public/personas/${encodeURIComponent(personaId)}/chat`
|
|
1024
|
+
);
|
|
1025
|
+
return new Persona({
|
|
1026
|
+
id: String(item.id),
|
|
1027
|
+
name: String(item.name),
|
|
1028
|
+
description: item.description ?? null,
|
|
1029
|
+
version: typeof item.version === "number" ? item.version : 1,
|
|
1030
|
+
instructions: typeof item.instructions === "string" ? item.instructions : "",
|
|
1031
|
+
examples: Array.isArray(item.examples) ? item.examples.map((example) => String(example)) : [],
|
|
1032
|
+
client: this
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
/** Play one persona turn. See {@link Persona.respond}. */
|
|
1036
|
+
async chat(personaId, messages) {
|
|
1037
|
+
const payload = await this.requestJson(
|
|
1038
|
+
"POST",
|
|
1039
|
+
`/api/public/personas/${encodeURIComponent(personaId)}/chat`,
|
|
1040
|
+
{ messages }
|
|
1041
|
+
);
|
|
1042
|
+
return {
|
|
1043
|
+
content: typeof payload.content === "string" ? payload.content : "",
|
|
1044
|
+
done: Boolean(payload.done)
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
async requestJson(method, path, payload) {
|
|
1048
|
+
let response;
|
|
1049
|
+
try {
|
|
1050
|
+
response = await fetch(`${this.host}${path}`, {
|
|
1051
|
+
method,
|
|
1052
|
+
headers: {
|
|
1053
|
+
Authorization: this.authHeader(),
|
|
1054
|
+
"Content-Type": "application/json"
|
|
1055
|
+
},
|
|
1056
|
+
...payload ? { body: JSON.stringify(payload) } : {},
|
|
1057
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
1058
|
+
});
|
|
1059
|
+
} catch (error) {
|
|
1060
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
1061
|
+
throw new PersonaError(`${method} ${path} failed: ${reason}`, {
|
|
1062
|
+
cause: error
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
const text = await response.text();
|
|
1066
|
+
if (!response.ok) {
|
|
1067
|
+
const detail = text.slice(0, 500);
|
|
1068
|
+
throw new PersonaError(
|
|
1069
|
+
`${method} ${path} failed (${response.status}): ${detail}`
|
|
1070
|
+
);
|
|
1071
|
+
}
|
|
1072
|
+
this.log("%s %s -> %s", method, path, response.status);
|
|
1073
|
+
return safeJson3(text);
|
|
1074
|
+
}
|
|
1075
|
+
authHeader() {
|
|
1076
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
1077
|
+
"base64"
|
|
1078
|
+
)}`;
|
|
1079
|
+
}
|
|
1080
|
+
log(message, ...args) {
|
|
1081
|
+
if (this.debug) {
|
|
1082
|
+
console.log(`[gateway.personas] ${message}`, ...args);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
};
|
|
1086
|
+
function isRecord(value) {
|
|
1087
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
1088
|
+
}
|
|
1089
|
+
function safeJson3(value) {
|
|
1090
|
+
try {
|
|
1091
|
+
const parsed = JSON.parse(value);
|
|
1092
|
+
return isRecord(parsed) ? parsed : {};
|
|
1093
|
+
} catch {
|
|
1094
|
+
return {};
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
var personas = {
|
|
1098
|
+
fromEnv: PersonasClient.fromEnv
|
|
1099
|
+
};
|
|
1100
|
+
|
|
1101
|
+
// src/sessions/index.ts
|
|
1102
|
+
import { mkdirSync, writeFileSync } from "fs";
|
|
1103
|
+
import { dirname } from "path";
|
|
1104
|
+
var ENV_GATEWAY_HOST = "GATEWAY_HOST";
|
|
1105
|
+
var ENV_GATEWAY_PUBLIC_KEY = "GATEWAY_PUBLIC_KEY";
|
|
1106
|
+
var ENV_GATEWAY_SECRET_KEY = "GATEWAY_SECRET_KEY";
|
|
1107
|
+
var DEFAULT_TIMEOUT_SECONDS = 30;
|
|
1108
|
+
var SessionLoadError = class extends Error {
|
|
1109
|
+
constructor(message, options) {
|
|
1110
|
+
super(message);
|
|
1111
|
+
this.name = "SessionLoadError";
|
|
1112
|
+
this.cause = options?.cause;
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
function sessionObservationFromApi(raw) {
|
|
1116
|
+
return {
|
|
1117
|
+
id: String(raw.id ?? ""),
|
|
1118
|
+
type: asStringOrNull(raw.type),
|
|
1119
|
+
name: asStringOrNull(raw.name),
|
|
1120
|
+
level: asStringOrNull(raw.level),
|
|
1121
|
+
statusMessage: asStringOrNull(raw.statusMessage),
|
|
1122
|
+
model: asStringOrNull(raw.model),
|
|
1123
|
+
input: raw.input,
|
|
1124
|
+
output: raw.output,
|
|
1125
|
+
metadata: raw.metadata ?? null,
|
|
1126
|
+
parentObservationId: asStringOrNull(raw.parentObservationId),
|
|
1127
|
+
startTime: raw.startTime ? String(raw.startTime) : null,
|
|
1128
|
+
endTime: raw.endTime ? String(raw.endTime) : null
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
var SessionTrace = class _SessionTrace {
|
|
1132
|
+
id;
|
|
1133
|
+
name;
|
|
1134
|
+
timestamp;
|
|
1135
|
+
input;
|
|
1136
|
+
output;
|
|
1137
|
+
metadata;
|
|
1138
|
+
tags;
|
|
1139
|
+
observations;
|
|
1140
|
+
constructor(fields) {
|
|
1141
|
+
this.id = fields.id;
|
|
1142
|
+
this.name = fields.name;
|
|
1143
|
+
this.timestamp = fields.timestamp;
|
|
1144
|
+
this.input = fields.input;
|
|
1145
|
+
this.output = fields.output;
|
|
1146
|
+
this.metadata = fields.metadata ?? null;
|
|
1147
|
+
this.tags = fields.tags ?? [];
|
|
1148
|
+
this.observations = fields.observations ?? [];
|
|
1149
|
+
}
|
|
1150
|
+
get toolCalls() {
|
|
1151
|
+
return this.observations.filter(
|
|
1152
|
+
(o) => (o.type ?? "").toUpperCase() === "TOOL"
|
|
1153
|
+
);
|
|
1154
|
+
}
|
|
1155
|
+
get generations() {
|
|
1156
|
+
return this.observations.filter(
|
|
1157
|
+
(o) => (o.type ?? "").toUpperCase() === "GENERATION"
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* The user message that started this turn.
|
|
1162
|
+
*
|
|
1163
|
+
* Handles the common recorded shapes: a plain string input, an OpenAI
|
|
1164
|
+
* message list (last role=user entry wins — earlier ones are carried
|
|
1165
|
+
* history), or an object with a content/input field.
|
|
1166
|
+
*/
|
|
1167
|
+
userMessage() {
|
|
1168
|
+
const value = this.input;
|
|
1169
|
+
if (typeof value === "string") {
|
|
1170
|
+
return value;
|
|
1171
|
+
}
|
|
1172
|
+
if (Array.isArray(value)) {
|
|
1173
|
+
for (let i = value.length - 1; i >= 0; i--) {
|
|
1174
|
+
const entry = value[i];
|
|
1175
|
+
if (isPlainObject(entry) && entry.role === "user") {
|
|
1176
|
+
const content = entry.content;
|
|
1177
|
+
if (typeof content === "string") {
|
|
1178
|
+
return content;
|
|
1179
|
+
}
|
|
1180
|
+
if (Array.isArray(content)) {
|
|
1181
|
+
const parts = content.filter(isPlainObject).map((part) => typeof part.text === "string" ? part.text : "");
|
|
1182
|
+
return parts.join("") || null;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
return null;
|
|
1187
|
+
}
|
|
1188
|
+
if (isPlainObject(value)) {
|
|
1189
|
+
for (const key of ["content", "input", "text", "message"]) {
|
|
1190
|
+
if (typeof value[key] === "string") {
|
|
1191
|
+
return value[key];
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
return null;
|
|
1196
|
+
}
|
|
1197
|
+
static fromApi(raw) {
|
|
1198
|
+
return new _SessionTrace({
|
|
1199
|
+
id: String(raw.id ?? ""),
|
|
1200
|
+
name: asStringOrNull(raw.name),
|
|
1201
|
+
timestamp: raw.timestamp ? String(raw.timestamp) : null,
|
|
1202
|
+
input: raw.input,
|
|
1203
|
+
output: raw.output,
|
|
1204
|
+
metadata: raw.metadata ?? null,
|
|
1205
|
+
tags: Array.isArray(raw.tags) ? raw.tags.map(String) : [],
|
|
1206
|
+
observations: (Array.isArray(raw.observations) ? raw.observations : []).filter(isPlainObject).map(sessionObservationFromApi)
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
var SessionRecording = class _SessionRecording {
|
|
1211
|
+
sessionId;
|
|
1212
|
+
projectId;
|
|
1213
|
+
environment;
|
|
1214
|
+
traces;
|
|
1215
|
+
constructor(fields) {
|
|
1216
|
+
this.sessionId = fields.sessionId;
|
|
1217
|
+
this.projectId = fields.projectId;
|
|
1218
|
+
this.environment = fields.environment;
|
|
1219
|
+
this.traces = fields.traces ?? [];
|
|
1220
|
+
}
|
|
1221
|
+
/** Traces in chronological order — one per agent turn. */
|
|
1222
|
+
turns() {
|
|
1223
|
+
return [...this.traces].sort((a, b) => {
|
|
1224
|
+
const left = a.timestamp ?? "";
|
|
1225
|
+
const right = b.timestamp ?? "";
|
|
1226
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
1227
|
+
});
|
|
1228
|
+
}
|
|
1229
|
+
/** The nth chronological turn (negative indices allowed). */
|
|
1230
|
+
turn(index) {
|
|
1231
|
+
const ordered = this.turns();
|
|
1232
|
+
const found = ordered.at(index);
|
|
1233
|
+
if (!found) {
|
|
1234
|
+
throw new RangeError(
|
|
1235
|
+
`Turn index ${index} out of range (session has ${ordered.length} turns)`
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
1238
|
+
return found;
|
|
1239
|
+
}
|
|
1240
|
+
/**
|
|
1241
|
+
* The user message of each turn, in order — feed these back to an agent
|
|
1242
|
+
* to re-drive the recorded conversation.
|
|
1243
|
+
*/
|
|
1244
|
+
userMessages() {
|
|
1245
|
+
return this.turns().map((trace4) => trace4.userMessage());
|
|
1246
|
+
}
|
|
1247
|
+
/** Output of the last chronological turn (null when empty). */
|
|
1248
|
+
finalOutput() {
|
|
1249
|
+
const ordered = this.turns();
|
|
1250
|
+
return ordered.length > 0 ? ordered[ordered.length - 1].output : null;
|
|
1251
|
+
}
|
|
1252
|
+
/** Every TOOL observation across the session, chronological. */
|
|
1253
|
+
toolCalls() {
|
|
1254
|
+
return this.select({ type: "TOOL" });
|
|
1255
|
+
}
|
|
1256
|
+
/** Every GENERATION observation across the session, chronological. */
|
|
1257
|
+
generations() {
|
|
1258
|
+
return this.select({ type: "GENERATION" });
|
|
1259
|
+
}
|
|
1260
|
+
/** Filter observations across all turns (chronological order). */
|
|
1261
|
+
select(filter = {}) {
|
|
1262
|
+
const selected = [];
|
|
1263
|
+
for (const trace4 of this.turns()) {
|
|
1264
|
+
for (const observation of trace4.observations) {
|
|
1265
|
+
if (filter.type && (observation.type ?? "").toUpperCase() !== filter.type.toUpperCase()) {
|
|
1266
|
+
continue;
|
|
1267
|
+
}
|
|
1268
|
+
if (filter.name && observation.name !== filter.name) {
|
|
1269
|
+
continue;
|
|
1270
|
+
}
|
|
1271
|
+
if (filter.level && (observation.level ?? "").toUpperCase() !== filter.level.toUpperCase()) {
|
|
1272
|
+
continue;
|
|
1273
|
+
}
|
|
1274
|
+
selected.push(observation);
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
return selected;
|
|
1278
|
+
}
|
|
1279
|
+
toJSON() {
|
|
1280
|
+
return {
|
|
1281
|
+
sessionId: this.sessionId,
|
|
1282
|
+
projectId: this.projectId,
|
|
1283
|
+
environment: this.environment,
|
|
1284
|
+
traces: this.turns().map((trace4) => ({
|
|
1285
|
+
id: trace4.id,
|
|
1286
|
+
name: trace4.name,
|
|
1287
|
+
timestamp: trace4.timestamp,
|
|
1288
|
+
input: trace4.input,
|
|
1289
|
+
output: trace4.output,
|
|
1290
|
+
metadata: trace4.metadata,
|
|
1291
|
+
tags: trace4.tags,
|
|
1292
|
+
observations: trace4.observations.map((o) => ({ ...o }))
|
|
1293
|
+
}))
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
/** Dump the full session as a JSON fixture (for regression tests). */
|
|
1297
|
+
toFixture(path) {
|
|
1298
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
1299
|
+
writeFileSync(path, JSON.stringify(this.toJSON(), null, 2), "utf-8");
|
|
1300
|
+
return path;
|
|
1301
|
+
}
|
|
1302
|
+
static fromApi(raw) {
|
|
1303
|
+
return new _SessionRecording({
|
|
1304
|
+
sessionId: String(raw.id ?? ""),
|
|
1305
|
+
projectId: asStringOrNull(raw.projectId),
|
|
1306
|
+
environment: asStringOrNull(raw.environment),
|
|
1307
|
+
traces: (Array.isArray(raw.traces) ? raw.traces : []).filter(isPlainObject).map(SessionTrace.fromApi)
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1310
|
+
};
|
|
1311
|
+
async function loadSession(sessionId, options = {}) {
|
|
1312
|
+
const host = (options.host ?? process.env[ENV_GATEWAY_HOST] ?? "").replace(
|
|
1313
|
+
/\/+$/,
|
|
1314
|
+
""
|
|
1315
|
+
);
|
|
1316
|
+
const publicKey = options.publicKey ?? process.env[ENV_GATEWAY_PUBLIC_KEY] ?? "";
|
|
1317
|
+
const secretKey = options.secretKey ?? process.env[ENV_GATEWAY_SECRET_KEY] ?? "";
|
|
1318
|
+
const timeoutSeconds = options.timeoutSeconds ?? DEFAULT_TIMEOUT_SECONDS;
|
|
1319
|
+
if (!host || !publicKey || !secretKey) {
|
|
1320
|
+
throw new SessionLoadError(
|
|
1321
|
+
"Gateway is not configured \u2014 set GATEWAY_HOST, GATEWAY_PUBLIC_KEY and GATEWAY_SECRET_KEY (or pass host/publicKey/secretKey)."
|
|
1322
|
+
);
|
|
1323
|
+
}
|
|
1324
|
+
const url = `${host}/api/public/sessions/${encodeURIComponent(sessionId)}?includeIO=true`;
|
|
1325
|
+
let response;
|
|
1326
|
+
try {
|
|
1327
|
+
response = await fetch(url, {
|
|
1328
|
+
method: "GET",
|
|
1329
|
+
headers: { Authorization: basicAuthHeader(publicKey, secretKey) },
|
|
1330
|
+
signal: AbortSignal.timeout(timeoutSeconds * 1e3)
|
|
1331
|
+
});
|
|
1332
|
+
} catch (error) {
|
|
1333
|
+
throw new SessionLoadError(
|
|
1334
|
+
`Failed to load session ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
|
|
1335
|
+
{ cause: error }
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
if (response.status === 404) {
|
|
1339
|
+
throw new SessionLoadError(`Session not found: ${sessionId}`);
|
|
1340
|
+
}
|
|
1341
|
+
const text = await response.text();
|
|
1342
|
+
if (!response.ok) {
|
|
1343
|
+
throw new SessionLoadError(
|
|
1344
|
+
`Failed to load session ${sessionId}: HTTP ${response.status} \u2014 ${text.slice(0, 300)}`
|
|
1345
|
+
);
|
|
1346
|
+
}
|
|
1347
|
+
let payload;
|
|
1348
|
+
try {
|
|
1349
|
+
payload = JSON.parse(text);
|
|
1350
|
+
} catch (error) {
|
|
1351
|
+
throw new SessionLoadError(
|
|
1352
|
+
`Failed to load session ${sessionId}: response is not valid JSON`,
|
|
1353
|
+
{ cause: error }
|
|
1354
|
+
);
|
|
1355
|
+
}
|
|
1356
|
+
return SessionRecording.fromApi(
|
|
1357
|
+
isPlainObject(payload) ? payload : {}
|
|
1358
|
+
);
|
|
1359
|
+
}
|
|
1360
|
+
function basicAuthHeader(publicKey, secretKey) {
|
|
1361
|
+
return `Basic ${Buffer.from(`${publicKey}:${secretKey}`).toString("base64")}`;
|
|
1362
|
+
}
|
|
1363
|
+
function asStringOrNull(value) {
|
|
1364
|
+
return typeof value === "string" ? value : null;
|
|
1365
|
+
}
|
|
1366
|
+
function isPlainObject(value) {
|
|
1367
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1368
|
+
}
|
|
1369
|
+
var sessions = {
|
|
1370
|
+
load: loadSession
|
|
1371
|
+
};
|
|
1372
|
+
|
|
1373
|
+
// src/replay/index.ts
|
|
1374
|
+
import { join } from "path";
|
|
1375
|
+
var ReplayError = class extends Error {
|
|
1376
|
+
constructor(message, options) {
|
|
1377
|
+
super(message);
|
|
1378
|
+
this.name = "ReplayError";
|
|
1379
|
+
this.cause = options?.cause;
|
|
1380
|
+
}
|
|
1381
|
+
};
|
|
1382
|
+
var StubMissError = class extends Error {
|
|
1383
|
+
constructor(message) {
|
|
1384
|
+
super(message);
|
|
1385
|
+
this.name = "StubMissError";
|
|
1386
|
+
}
|
|
1387
|
+
};
|
|
1388
|
+
var DriftError = class extends ReplayError {
|
|
1389
|
+
constructor(message) {
|
|
1390
|
+
super(message);
|
|
1391
|
+
this.name = "DriftError";
|
|
1392
|
+
}
|
|
1393
|
+
};
|
|
1394
|
+
var WRAPPER_KEYS = ["args_json", "arguments", "kwargs", "args", "input"];
|
|
1395
|
+
function canonicalize(value) {
|
|
1396
|
+
if (typeof value === "string") {
|
|
1397
|
+
const stripped = value.trim();
|
|
1398
|
+
if (stripped.startsWith("{") || stripped.startsWith("[")) {
|
|
1399
|
+
try {
|
|
1400
|
+
return canonicalize(JSON.parse(stripped));
|
|
1401
|
+
} catch {
|
|
1402
|
+
return value;
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
return value;
|
|
1406
|
+
}
|
|
1407
|
+
if (Array.isArray(value)) {
|
|
1408
|
+
return value.map(canonicalize);
|
|
1409
|
+
}
|
|
1410
|
+
if (isPlainObject2(value)) {
|
|
1411
|
+
const entries = Object.entries(value);
|
|
1412
|
+
if (entries.length === 1 && WRAPPER_KEYS.includes(entries[0][0])) {
|
|
1413
|
+
return canonicalize(entries[0][1]);
|
|
1414
|
+
}
|
|
1415
|
+
return Object.fromEntries(
|
|
1416
|
+
entries.map(([key, inner]) => [key, canonicalize(inner)])
|
|
1417
|
+
);
|
|
1418
|
+
}
|
|
1419
|
+
return value;
|
|
1420
|
+
}
|
|
1421
|
+
function sortKeysDeep(value) {
|
|
1422
|
+
if (Array.isArray(value)) {
|
|
1423
|
+
return value.map(sortKeysDeep);
|
|
1424
|
+
}
|
|
1425
|
+
if (isPlainObject2(value)) {
|
|
1426
|
+
const sorted = {};
|
|
1427
|
+
for (const key of Object.keys(value).sort()) {
|
|
1428
|
+
sorted[key] = sortKeysDeep(value[key]);
|
|
1429
|
+
}
|
|
1430
|
+
return sorted;
|
|
1431
|
+
}
|
|
1432
|
+
return value;
|
|
1433
|
+
}
|
|
1434
|
+
function normalizeInput(value) {
|
|
1435
|
+
try {
|
|
1436
|
+
return JSON.stringify(sortKeysDeep(canonicalize(value ?? null))) ?? "null";
|
|
1437
|
+
} catch {
|
|
1438
|
+
return String(value);
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
var ToolStubRegistry = class _ToolStubRegistry {
|
|
1442
|
+
callLog = [];
|
|
1443
|
+
driftEvents = [];
|
|
1444
|
+
recorded;
|
|
1445
|
+
consumed = /* @__PURE__ */ new Set();
|
|
1446
|
+
consumptionOrder = [];
|
|
1447
|
+
missPolicy;
|
|
1448
|
+
fallback;
|
|
1449
|
+
local;
|
|
1450
|
+
constructor(calls, options = {}) {
|
|
1451
|
+
const missPolicy = options.missPolicy ?? "error";
|
|
1452
|
+
if (!["error", "reuse", "passthrough"].includes(missPolicy)) {
|
|
1453
|
+
throw new ReplayError(`Unknown missPolicy: ${missPolicy}`);
|
|
1454
|
+
}
|
|
1455
|
+
this.recorded = calls;
|
|
1456
|
+
this.missPolicy = missPolicy;
|
|
1457
|
+
this.fallback = options.fallback ?? null;
|
|
1458
|
+
this.local = { ...options.local ?? {} };
|
|
1459
|
+
}
|
|
1460
|
+
static fromSession(session, options = {}) {
|
|
1461
|
+
const calls = session.toolCalls().map(
|
|
1462
|
+
(observation) => ({
|
|
1463
|
+
name: observation.name ?? "",
|
|
1464
|
+
input: observation.input,
|
|
1465
|
+
output: observation.output,
|
|
1466
|
+
observationId: observation.id
|
|
1467
|
+
})
|
|
1468
|
+
);
|
|
1469
|
+
return new _ToolStubRegistry(calls, options);
|
|
1470
|
+
}
|
|
1471
|
+
/** Resolve a tool call against the recorded world. */
|
|
1472
|
+
lookup(toolName, toolInput = null) {
|
|
1473
|
+
if (toolName in this.local) {
|
|
1474
|
+
this.callLog.push({ tool: toolName, match: "local", input: toolInput });
|
|
1475
|
+
return this.local[toolName](toolInput);
|
|
1476
|
+
}
|
|
1477
|
+
const normalized = normalizeInput(toolInput);
|
|
1478
|
+
for (let index = 0; index < this.recorded.length; index++) {
|
|
1479
|
+
const record = this.recorded[index];
|
|
1480
|
+
if (!this.consumed.has(index) && record.name === toolName && normalizeInput(record.input) === normalized) {
|
|
1481
|
+
this.consumed.add(index);
|
|
1482
|
+
this.consumptionOrder.push(index);
|
|
1483
|
+
this.callLog.push({ tool: toolName, match: "exact", input: toolInput });
|
|
1484
|
+
return record.output;
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
for (let index = this.recorded.length - 1; index >= 0; index--) {
|
|
1488
|
+
const record = this.recorded[index];
|
|
1489
|
+
if (record.name === toolName && normalizeInput(record.input) === normalized) {
|
|
1490
|
+
this.callLog.push({
|
|
1491
|
+
tool: toolName,
|
|
1492
|
+
match: "exact-repeat",
|
|
1493
|
+
input: toolInput
|
|
1494
|
+
});
|
|
1495
|
+
return record.output;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
for (let index = 0; index < this.recorded.length; index++) {
|
|
1499
|
+
const record = this.recorded[index];
|
|
1500
|
+
if (!this.consumed.has(index) && record.name === toolName) {
|
|
1501
|
+
this.consumed.add(index);
|
|
1502
|
+
this.consumptionOrder.push(index);
|
|
1503
|
+
this.callLog.push({
|
|
1504
|
+
tool: toolName,
|
|
1505
|
+
match: "ordinal",
|
|
1506
|
+
input: toolInput
|
|
1507
|
+
});
|
|
1508
|
+
return record.output;
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
if (this.missPolicy === "reuse") {
|
|
1512
|
+
for (let index = this.recorded.length - 1; index >= 0; index--) {
|
|
1513
|
+
const record = this.recorded[index];
|
|
1514
|
+
if (record.name === toolName) {
|
|
1515
|
+
this.driftEvents.push(
|
|
1516
|
+
`recordings for tool '${toolName}' exhausted \u2014 replayed the last one again (reuse policy)`
|
|
1517
|
+
);
|
|
1518
|
+
this.callLog.push({
|
|
1519
|
+
tool: toolName,
|
|
1520
|
+
match: "reuse",
|
|
1521
|
+
input: toolInput
|
|
1522
|
+
});
|
|
1523
|
+
return record.output;
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
const recordedNames = [...new Set(this.recorded.map((r) => r.name))].sort();
|
|
1528
|
+
const drift = `no recording for tool '${toolName}' (recorded tools: ${JSON.stringify(recordedNames)})`;
|
|
1529
|
+
this.driftEvents.push(drift);
|
|
1530
|
+
this.callLog.push({ tool: toolName, match: "miss", input: toolInput });
|
|
1531
|
+
if (this.fallback !== null && (this.missPolicy === "passthrough" || this.missPolicy === "reuse")) {
|
|
1532
|
+
return this.fallback(toolName, toolInput);
|
|
1533
|
+
}
|
|
1534
|
+
throw new StubMissError(drift);
|
|
1535
|
+
}
|
|
1536
|
+
/**
|
|
1537
|
+
* Mapping of toolName → callable, drop-in for most agent tool layers.
|
|
1538
|
+
*
|
|
1539
|
+
* `local` merges in tools that should ALWAYS run live (new tools the
|
|
1540
|
+
* recording predates, or deterministic/pure helpers) — they're logged for
|
|
1541
|
+
* the contract check but never count as drift.
|
|
1542
|
+
*
|
|
1543
|
+
* UNKNOWN tool names also resolve (to a stub that routes through
|
|
1544
|
+
* `lookup()`), so a call to an un-recorded tool is LOGGED and follows the
|
|
1545
|
+
* miss policy instead of dying on an undefined lookup — that log is what
|
|
1546
|
+
* lets `check()` classify a forbidden-but-unrecorded call as FAIL.
|
|
1547
|
+
*/
|
|
1548
|
+
asRegistry(options = {}) {
|
|
1549
|
+
if (options.fallback) {
|
|
1550
|
+
this.fallback = options.fallback;
|
|
1551
|
+
this.missPolicy = "passthrough";
|
|
1552
|
+
}
|
|
1553
|
+
if (options.local) {
|
|
1554
|
+
Object.assign(this.local, options.local);
|
|
1555
|
+
}
|
|
1556
|
+
const make = (name) => (toolInput) => this.lookup(name, toolInput === void 0 ? {} : toolInput);
|
|
1557
|
+
const target = {};
|
|
1558
|
+
const names = /* @__PURE__ */ new Set([
|
|
1559
|
+
...this.recorded.map((record) => record.name),
|
|
1560
|
+
...Object.keys(this.local)
|
|
1561
|
+
]);
|
|
1562
|
+
for (const name of names) {
|
|
1563
|
+
if (name) {
|
|
1564
|
+
target[name] = make(name);
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
return new Proxy(target, {
|
|
1568
|
+
get(current, prop, receiver) {
|
|
1569
|
+
if (typeof prop !== "string" || prop in current) {
|
|
1570
|
+
return Reflect.get(current, prop, receiver);
|
|
1571
|
+
}
|
|
1572
|
+
return make(prop);
|
|
1573
|
+
}
|
|
1574
|
+
});
|
|
1575
|
+
}
|
|
1576
|
+
calledNames() {
|
|
1577
|
+
return this.callLog.map((entry) => entry.tool);
|
|
1578
|
+
}
|
|
1579
|
+
/**
|
|
1580
|
+
* 1:1 comparison of this replay against the recorded trace.
|
|
1581
|
+
*
|
|
1582
|
+
* The recording is the expectation: every recorded tool call should be
|
|
1583
|
+
* replayed exactly once, in order, with nothing extra. Use for strict
|
|
1584
|
+
* did-it-do-everything-the-original-did checks (`mode: "exact"`).
|
|
1585
|
+
*
|
|
1586
|
+
* `includeLocal`: count local-tool calls as extras (default false — local
|
|
1587
|
+
* tools are declared live on purpose; their recorded calls are likewise
|
|
1588
|
+
* excluded from "missing", since local bypasses recordings).
|
|
1589
|
+
*/
|
|
1590
|
+
compare(options = {}) {
|
|
1591
|
+
const includeLocal = options.includeLocal ?? false;
|
|
1592
|
+
const missing = this.recorded.map((record, index) => ({ record, index })).filter(
|
|
1593
|
+
({ record, index }) => !this.consumed.has(index) && (includeLocal || !(record.name in this.local))
|
|
1594
|
+
).map(({ record }) => `${record.name}(${normalizeInput(record.input)})`);
|
|
1595
|
+
const extra = [];
|
|
1596
|
+
for (const entry of this.callLog) {
|
|
1597
|
+
if (["miss", "reuse", "exact-repeat"].includes(entry.match) || includeLocal && entry.match === "local") {
|
|
1598
|
+
extra.push(
|
|
1599
|
+
`${entry.tool}(${normalizeInput(entry.input)}) [${entry.match}]`
|
|
1600
|
+
);
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
const outOfOrder = [];
|
|
1604
|
+
let last = -1;
|
|
1605
|
+
for (const index of this.consumptionOrder) {
|
|
1606
|
+
if (index < last) {
|
|
1607
|
+
const record = this.recorded[index];
|
|
1608
|
+
outOfOrder.push(`${record.name}(${normalizeInput(record.input)})`);
|
|
1609
|
+
} else {
|
|
1610
|
+
last = index;
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
const matched = this.consumptionOrder.map(
|
|
1614
|
+
(index) => this.recorded[index].name
|
|
1615
|
+
);
|
|
1616
|
+
return new TraceDiff({ matched, missing, extra, outOfOrder });
|
|
1617
|
+
}
|
|
1618
|
+
get drifted() {
|
|
1619
|
+
return this.driftEvents.length > 0;
|
|
1620
|
+
}
|
|
1621
|
+
};
|
|
1622
|
+
var TraceDiff = class {
|
|
1623
|
+
matched;
|
|
1624
|
+
missing;
|
|
1625
|
+
extra;
|
|
1626
|
+
outOfOrder;
|
|
1627
|
+
constructor(fields = {}) {
|
|
1628
|
+
this.matched = fields.matched ?? [];
|
|
1629
|
+
this.missing = fields.missing ?? [];
|
|
1630
|
+
this.extra = fields.extra ?? [];
|
|
1631
|
+
this.outOfOrder = fields.outOfOrder ?? [];
|
|
1632
|
+
}
|
|
1633
|
+
get identical() {
|
|
1634
|
+
return this.missing.length === 0 && this.extra.length === 0 && this.outOfOrder.length === 0;
|
|
1635
|
+
}
|
|
1636
|
+
explain() {
|
|
1637
|
+
if (this.identical) {
|
|
1638
|
+
return `1:1 replay \u2014 all ${this.matched.length} recorded tool calls reproduced in order`;
|
|
1639
|
+
}
|
|
1640
|
+
const lines = [
|
|
1641
|
+
`trace diverges from recording (${this.matched.length} matched):`
|
|
1642
|
+
];
|
|
1643
|
+
lines.push(...this.missing.map((item) => ` not reproduced: ${item}`));
|
|
1644
|
+
lines.push(...this.extra.map((item) => ` extra call: ${item}`));
|
|
1645
|
+
lines.push(...this.outOfOrder.map((item) => ` out of order: ${item}`));
|
|
1646
|
+
return lines.join("\n");
|
|
1647
|
+
}
|
|
1648
|
+
};
|
|
1649
|
+
var ReplayTask = class _ReplayTask {
|
|
1650
|
+
id;
|
|
1651
|
+
name;
|
|
1652
|
+
prompt;
|
|
1653
|
+
expectedOutput;
|
|
1654
|
+
metadata;
|
|
1655
|
+
constructor(fields) {
|
|
1656
|
+
this.id = fields.id;
|
|
1657
|
+
this.name = fields.name ?? null;
|
|
1658
|
+
this.prompt = fields.prompt;
|
|
1659
|
+
this.expectedOutput = fields.expectedOutput;
|
|
1660
|
+
this.metadata = fields.metadata ?? {};
|
|
1661
|
+
}
|
|
1662
|
+
get sourceSessionId() {
|
|
1663
|
+
const value = this.metadata.sourceSessionId;
|
|
1664
|
+
return value ? String(value) : null;
|
|
1665
|
+
}
|
|
1666
|
+
get sourceTraceId() {
|
|
1667
|
+
const value = this.metadata.sourceTraceId;
|
|
1668
|
+
return value ? String(value) : null;
|
|
1669
|
+
}
|
|
1670
|
+
static fromApi(raw) {
|
|
1671
|
+
const metadata = raw.metadata;
|
|
1672
|
+
return new _ReplayTask({
|
|
1673
|
+
id: String(raw.id ?? ""),
|
|
1674
|
+
name: typeof raw.name === "string" ? raw.name : null,
|
|
1675
|
+
prompt: raw.prompt,
|
|
1676
|
+
expectedOutput: raw.expectedOutput,
|
|
1677
|
+
metadata: isPlainObject2(metadata) ? { ...metadata } : {}
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
};
|
|
1681
|
+
var ReplayCheck = class {
|
|
1682
|
+
status;
|
|
1683
|
+
failures;
|
|
1684
|
+
drift;
|
|
1685
|
+
traceDiff;
|
|
1686
|
+
constructor(status, fields = {}) {
|
|
1687
|
+
this.status = status;
|
|
1688
|
+
this.failures = fields.failures ?? [];
|
|
1689
|
+
this.drift = fields.drift ?? [];
|
|
1690
|
+
this.traceDiff = fields.traceDiff ?? null;
|
|
1691
|
+
}
|
|
1692
|
+
get passed() {
|
|
1693
|
+
return this.status === "PASS";
|
|
1694
|
+
}
|
|
1695
|
+
get drifted() {
|
|
1696
|
+
return this.status === "DRIFT";
|
|
1697
|
+
}
|
|
1698
|
+
explain() {
|
|
1699
|
+
if (this.status === "PASS") {
|
|
1700
|
+
return "PASS";
|
|
1701
|
+
}
|
|
1702
|
+
const lines = [this.status];
|
|
1703
|
+
lines.push(...this.failures.map((failure) => ` FAIL: ${failure}`));
|
|
1704
|
+
for (const event of this.drift) {
|
|
1705
|
+
if (event.includes("reuse policy")) {
|
|
1706
|
+
lines.push(
|
|
1707
|
+
` DRIFT: ${event} \u2014 recording under-covers this run; re-record for exact coverage or keep the reuse policy.`
|
|
1708
|
+
);
|
|
1709
|
+
} else {
|
|
1710
|
+
lines.push(
|
|
1711
|
+
` DRIFT: ${event} \u2014 the test is stale, not the agent; re-record the session (or declare the tool local).`
|
|
1712
|
+
);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
return lines.join("\n");
|
|
1716
|
+
}
|
|
1717
|
+
};
|
|
1718
|
+
var ReplayCase = class {
|
|
1719
|
+
task;
|
|
1720
|
+
loader;
|
|
1721
|
+
_recording = null;
|
|
1722
|
+
_stubs = null;
|
|
1723
|
+
constructor(task, options) {
|
|
1724
|
+
this.task = task;
|
|
1725
|
+
this.loader = options.loader;
|
|
1726
|
+
}
|
|
1727
|
+
// -- identity ------------------------------------------------------------
|
|
1728
|
+
get id() {
|
|
1729
|
+
return this.task.id;
|
|
1730
|
+
}
|
|
1731
|
+
get name() {
|
|
1732
|
+
return this.task.name ?? this.task.id;
|
|
1733
|
+
}
|
|
1734
|
+
get prompt() {
|
|
1735
|
+
return this.task.prompt;
|
|
1736
|
+
}
|
|
1737
|
+
get expected() {
|
|
1738
|
+
return this.task.expectedOutput;
|
|
1739
|
+
}
|
|
1740
|
+
// -- recorded source session ----------------------------------------------
|
|
1741
|
+
/** The full recorded session this case was filed from. */
|
|
1742
|
+
async recording() {
|
|
1743
|
+
if (this._recording === null) {
|
|
1744
|
+
const source = this.task.sourceSessionId;
|
|
1745
|
+
if (!source) {
|
|
1746
|
+
throw new DriftError(
|
|
1747
|
+
`Task ${this.name} has no sourceSessionId provenance \u2014 file it from a session (add_session_to_task_set) or set metadata.sourceSessionId.`
|
|
1748
|
+
);
|
|
1749
|
+
}
|
|
1750
|
+
this._recording = await this.loader(source);
|
|
1751
|
+
}
|
|
1752
|
+
return this._recording;
|
|
1753
|
+
}
|
|
1754
|
+
async stubs(options = {}) {
|
|
1755
|
+
this._stubs = ToolStubRegistry.fromSession(await this.recording(), options);
|
|
1756
|
+
return this._stubs;
|
|
1757
|
+
}
|
|
1758
|
+
/**
|
|
1759
|
+
* The recorded conversation's user messages, one per turn — feed them to
|
|
1760
|
+
* your agent in order to re-drive the full session (each turn was one
|
|
1761
|
+
* trace in the recording).
|
|
1762
|
+
*/
|
|
1763
|
+
async conversation() {
|
|
1764
|
+
const messages = (await this.recording()).userMessages();
|
|
1765
|
+
const found = messages.filter(
|
|
1766
|
+
(message) => Boolean(message)
|
|
1767
|
+
);
|
|
1768
|
+
return found.length > 0 ? found : [String(this.prompt)];
|
|
1769
|
+
}
|
|
1770
|
+
// -- grading ---------------------------------------------------------------
|
|
1771
|
+
/**
|
|
1772
|
+
* Contract invariants + drift triage.
|
|
1773
|
+
*
|
|
1774
|
+
* Invariants come from task metadata (the workbench-style contract):
|
|
1775
|
+
* metadata.expectTools — every listed tool MUST have been called
|
|
1776
|
+
* metadata.mustNotCall — none of these may have been called
|
|
1777
|
+
* Semantic grading stays with the caller (assert on `output` /
|
|
1778
|
+
* `case.expected`, or run a platform judge on the replay trace).
|
|
1779
|
+
*
|
|
1780
|
+
* `mode` (or task `metadata.replayMode`):
|
|
1781
|
+
* "contract" (default) — grade only the invariants above
|
|
1782
|
+
* "exact" — true 1:1 replay: the agent must reproduce EVERY
|
|
1783
|
+
* recorded tool call, in order, with nothing extra
|
|
1784
|
+
* (see TraceDiff); any divergence is a FAIL. The
|
|
1785
|
+
* full diff lands in the failure reasons and
|
|
1786
|
+
* `traceDiff`.
|
|
1787
|
+
*/
|
|
1788
|
+
check(output = void 0, options = {}) {
|
|
1789
|
+
const registry = options.stubs ?? this._stubs;
|
|
1790
|
+
const drift = registry ? [...registry.driftEvents] : [];
|
|
1791
|
+
const called = options.calledTools ?? (registry ? registry.calledNames() : []);
|
|
1792
|
+
const violations = [];
|
|
1793
|
+
const omissions = [];
|
|
1794
|
+
const expectTools = this.task.metadata.expectTools;
|
|
1795
|
+
if (Array.isArray(expectTools)) {
|
|
1796
|
+
for (const toolName of expectTools) {
|
|
1797
|
+
if (!called.includes(toolName)) {
|
|
1798
|
+
omissions.push(`expected tool not called: ${toolName}`);
|
|
1799
|
+
}
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
const mustNot = this.task.metadata.mustNotCall;
|
|
1803
|
+
if (Array.isArray(mustNot)) {
|
|
1804
|
+
for (const toolName of mustNot) {
|
|
1805
|
+
if (called.includes(toolName)) {
|
|
1806
|
+
violations.push(`must_not_call violated: ${toolName}`);
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
if (typeof this.expected === "string" && this.expected.trim() && typeof output === "string" && this.task.metadata.outputExact === true && output.trim() !== this.expected.trim()) {
|
|
1811
|
+
violations.push("output does not exactly match expectedOutput");
|
|
1812
|
+
}
|
|
1813
|
+
const replayMode = options.mode ?? this.task.metadata.replayMode ?? "contract";
|
|
1814
|
+
let traceDiff = null;
|
|
1815
|
+
if (registry) {
|
|
1816
|
+
traceDiff = registry.compare();
|
|
1817
|
+
}
|
|
1818
|
+
if (replayMode === "exact" && traceDiff && !traceDiff.identical) {
|
|
1819
|
+
violations.push(
|
|
1820
|
+
...[
|
|
1821
|
+
...traceDiff.missing.map((item) => `not reproduced: ${item}`),
|
|
1822
|
+
...traceDiff.extra.map((item) => `extra call: ${item}`),
|
|
1823
|
+
...traceDiff.outOfOrder.map((item) => `out of order: ${item}`)
|
|
1824
|
+
].map((line) => `1:1 replay diverged \u2014 ${line}`)
|
|
1825
|
+
);
|
|
1826
|
+
}
|
|
1827
|
+
if (violations.length > 0) {
|
|
1828
|
+
return new ReplayCheck("FAIL", {
|
|
1829
|
+
failures: [...violations, ...drift.length > 0 ? [] : omissions],
|
|
1830
|
+
drift,
|
|
1831
|
+
traceDiff
|
|
1832
|
+
});
|
|
1833
|
+
}
|
|
1834
|
+
if (drift.length > 0) {
|
|
1835
|
+
return new ReplayCheck("DRIFT", { drift, traceDiff });
|
|
1836
|
+
}
|
|
1837
|
+
if (omissions.length > 0) {
|
|
1838
|
+
return new ReplayCheck("FAIL", { failures: omissions, traceDiff });
|
|
1839
|
+
}
|
|
1840
|
+
return new ReplayCheck("PASS", { traceDiff });
|
|
1841
|
+
}
|
|
1842
|
+
};
|
|
1843
|
+
var ReplaySuite = class _ReplaySuite {
|
|
1844
|
+
taskSetId;
|
|
1845
|
+
taskSetName;
|
|
1846
|
+
cases;
|
|
1847
|
+
/** Credentials for recording replay runs back to the platform. */
|
|
1848
|
+
credentials;
|
|
1849
|
+
constructor(fields) {
|
|
1850
|
+
this.taskSetId = fields.taskSetId;
|
|
1851
|
+
this.taskSetName = fields.taskSetName;
|
|
1852
|
+
this.cases = fields.cases;
|
|
1853
|
+
this.credentials = fields.credentials ?? null;
|
|
1854
|
+
}
|
|
1855
|
+
[Symbol.iterator]() {
|
|
1856
|
+
return this.cases[Symbol.iterator]();
|
|
1857
|
+
}
|
|
1858
|
+
get length() {
|
|
1859
|
+
return this.cases.length;
|
|
1860
|
+
}
|
|
1861
|
+
/** Look up a case by id or name (throws when absent). */
|
|
1862
|
+
get(key) {
|
|
1863
|
+
for (const testCase of this.cases) {
|
|
1864
|
+
if (testCase.id === key || testCase.name === key) {
|
|
1865
|
+
return testCase;
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
throw new ReplayError(
|
|
1869
|
+
`No case '${key}' in task set ${this.taskSetName} (cases: ${JSON.stringify(this.cases.map((c) => c.name))})`
|
|
1870
|
+
);
|
|
1871
|
+
}
|
|
1872
|
+
static async fromTaskSet(nameOrId, options = {}) {
|
|
1873
|
+
const host = (options.host ?? process.env[ENV_GATEWAY_HOST] ?? "").replace(
|
|
1874
|
+
/\/+$/,
|
|
1875
|
+
""
|
|
1876
|
+
);
|
|
1877
|
+
const publicKey = options.publicKey ?? process.env[ENV_GATEWAY_PUBLIC_KEY] ?? "";
|
|
1878
|
+
const secretKey = options.secretKey ?? process.env[ENV_GATEWAY_SECRET_KEY] ?? "";
|
|
1879
|
+
const timeoutSeconds = options.timeoutSeconds ?? DEFAULT_TIMEOUT_SECONDS;
|
|
1880
|
+
if (!host || !publicKey || !secretKey) {
|
|
1881
|
+
throw new SessionLoadError(
|
|
1882
|
+
"Gateway is not configured \u2014 set GATEWAY_HOST, GATEWAY_PUBLIC_KEY and GATEWAY_SECRET_KEY."
|
|
1883
|
+
);
|
|
1884
|
+
}
|
|
1885
|
+
const headers = { Authorization: basicAuthHeader(publicKey, secretKey) };
|
|
1886
|
+
const fetchTasks = async (datasetId) => {
|
|
1887
|
+
const tasks = [];
|
|
1888
|
+
let page = 0;
|
|
1889
|
+
for (; ; ) {
|
|
1890
|
+
const response = await fetch(
|
|
1891
|
+
`${host}/api/public/task-sets/${encodeURIComponent(datasetId)}/tasks?page=${page}&limit=200`,
|
|
1892
|
+
{ headers, signal: AbortSignal.timeout(timeoutSeconds * 1e3) }
|
|
1893
|
+
);
|
|
1894
|
+
if (response.status === 404) {
|
|
1895
|
+
return null;
|
|
1896
|
+
}
|
|
1897
|
+
const text = await response.text();
|
|
1898
|
+
if (!response.ok) {
|
|
1899
|
+
throw new ReplayError(
|
|
1900
|
+
`Failed to list tasks for ${datasetId}: HTTP ${response.status} \u2014 ${text.slice(0, 300)}`
|
|
1901
|
+
);
|
|
1902
|
+
}
|
|
1903
|
+
const payload = parseJsonObject(text);
|
|
1904
|
+
const pageTasks = Array.isArray(payload.tasks) ? payload.tasks : [];
|
|
1905
|
+
tasks.push(...pageTasks.filter(isPlainObject2));
|
|
1906
|
+
if (tasks.length >= Number(payload.totalTasks ?? 0)) {
|
|
1907
|
+
return tasks;
|
|
1908
|
+
}
|
|
1909
|
+
page += 1;
|
|
1910
|
+
}
|
|
1911
|
+
};
|
|
1912
|
+
let taskSetId = nameOrId;
|
|
1913
|
+
let taskSetName = nameOrId;
|
|
1914
|
+
let rawTasks = await fetchTasks(nameOrId);
|
|
1915
|
+
if (rawTasks === null) {
|
|
1916
|
+
const response = await fetch(
|
|
1917
|
+
`${host}/api/public/task-sets?name=${encodeURIComponent(nameOrId)}`,
|
|
1918
|
+
{ headers, signal: AbortSignal.timeout(timeoutSeconds * 1e3) }
|
|
1919
|
+
);
|
|
1920
|
+
const text = await response.text();
|
|
1921
|
+
if (!response.ok) {
|
|
1922
|
+
throw new ReplayError(
|
|
1923
|
+
`Failed to resolve task set '${nameOrId}': HTTP ${response.status} \u2014 ${text.slice(0, 300)}`
|
|
1924
|
+
);
|
|
1925
|
+
}
|
|
1926
|
+
const payload = parseJsonObject(text);
|
|
1927
|
+
const matches = Array.isArray(payload.taskSets) ? payload.taskSets.filter(isPlainObject2) : [];
|
|
1928
|
+
if (matches.length === 0) {
|
|
1929
|
+
throw new ReplayError(`Task set not found: ${nameOrId}`);
|
|
1930
|
+
}
|
|
1931
|
+
taskSetId = String(matches[0].datasetId);
|
|
1932
|
+
taskSetName = String(matches[0].name);
|
|
1933
|
+
rawTasks = await fetchTasks(taskSetId) ?? [];
|
|
1934
|
+
}
|
|
1935
|
+
const loader = (sessionId) => loadSession(sessionId, { host, publicKey, secretKey, timeoutSeconds });
|
|
1936
|
+
const cases = rawTasks.map(
|
|
1937
|
+
(raw) => new ReplayCase(ReplayTask.fromApi(raw), { loader })
|
|
1938
|
+
);
|
|
1939
|
+
return new _ReplaySuite({
|
|
1940
|
+
taskSetId,
|
|
1941
|
+
taskSetName,
|
|
1942
|
+
cases,
|
|
1943
|
+
credentials: { host, publicKey, secretKey, timeoutSeconds }
|
|
1944
|
+
});
|
|
1945
|
+
}
|
|
1946
|
+
/** Run the whole suite against `agent` — see `runSuite()`. */
|
|
1947
|
+
run(agent, options = {}) {
|
|
1948
|
+
return runSuite(this, agent, options);
|
|
1949
|
+
}
|
|
1950
|
+
/** Pin every case's recorded session into the repo as JSON fixtures. */
|
|
1951
|
+
async snapshot(directory) {
|
|
1952
|
+
const written = [];
|
|
1953
|
+
for (const testCase of this.cases) {
|
|
1954
|
+
if (!testCase.task.sourceSessionId) {
|
|
1955
|
+
continue;
|
|
1956
|
+
}
|
|
1957
|
+
const recording = await testCase.recording();
|
|
1958
|
+
written.push(
|
|
1959
|
+
recording.toFixture(join(directory, `${testCase.name}.recording.json`))
|
|
1960
|
+
);
|
|
1961
|
+
}
|
|
1962
|
+
return written;
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1965
|
+
var ReplayReport = class {
|
|
1966
|
+
results;
|
|
1967
|
+
runName;
|
|
1968
|
+
recordingErrors;
|
|
1969
|
+
constructor(results, fields = {}) {
|
|
1970
|
+
this.results = results;
|
|
1971
|
+
this.runName = fields.runName ?? null;
|
|
1972
|
+
this.recordingErrors = fields.recordingErrors ?? [];
|
|
1973
|
+
}
|
|
1974
|
+
get passed() {
|
|
1975
|
+
return this.count("PASS");
|
|
1976
|
+
}
|
|
1977
|
+
get failed() {
|
|
1978
|
+
return this.count("FAIL");
|
|
1979
|
+
}
|
|
1980
|
+
get drifted() {
|
|
1981
|
+
return this.count("DRIFT");
|
|
1982
|
+
}
|
|
1983
|
+
/** True when nothing FAILED (drift is stale-test signal, not failure). */
|
|
1984
|
+
get ok() {
|
|
1985
|
+
return this.failed === 0;
|
|
1986
|
+
}
|
|
1987
|
+
explain() {
|
|
1988
|
+
const lines = [
|
|
1989
|
+
`${this.passed} PASS \xB7 ${this.failed} FAIL \xB7 ${this.drifted} DRIFT` + (this.runName ? ` \xB7 run '${this.runName}'` : "")
|
|
1990
|
+
];
|
|
1991
|
+
for (const [name, check] of Object.entries(this.results)) {
|
|
1992
|
+
if (check.status !== "PASS") {
|
|
1993
|
+
lines.push(`${name}: ${check.explain()}`);
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
for (const error of this.recordingErrors) {
|
|
1997
|
+
lines.push(`recording: ${error}`);
|
|
1998
|
+
}
|
|
1999
|
+
return lines.join("\n");
|
|
2000
|
+
}
|
|
2001
|
+
count(status) {
|
|
2002
|
+
return Object.values(this.results).filter(
|
|
2003
|
+
(check) => check.status === status
|
|
2004
|
+
).length;
|
|
2005
|
+
}
|
|
2006
|
+
};
|
|
2007
|
+
var REPLAY_ENVIRONMENT = "gateway-replay";
|
|
2008
|
+
async function recordRunItem(suiteToRecord, testCase, runName, traceId, triage, options = {}) {
|
|
2009
|
+
const retries = options.retries ?? 10;
|
|
2010
|
+
const retryDelaySeconds = options.retryDelaySeconds ?? 3;
|
|
2011
|
+
const credentials = suiteToRecord.credentials;
|
|
2012
|
+
if (!credentials) {
|
|
2013
|
+
return "suite has no platform credentials";
|
|
2014
|
+
}
|
|
2015
|
+
const payload = {
|
|
2016
|
+
runName,
|
|
2017
|
+
runDescription: `gatewaysdk.replay run over '${suiteToRecord.taskSetName}'`,
|
|
2018
|
+
datasetItemId: testCase.id,
|
|
2019
|
+
traceId: traceId ?? randomHex(),
|
|
2020
|
+
metadata: {
|
|
2021
|
+
replay: true,
|
|
2022
|
+
status: triage.status,
|
|
2023
|
+
failures: triage.failures,
|
|
2024
|
+
drift: triage.drift,
|
|
2025
|
+
sourceSessionId: testCase.task.sourceSessionId
|
|
2026
|
+
}
|
|
2027
|
+
};
|
|
2028
|
+
let lastError = "unknown";
|
|
2029
|
+
for (let attempt = 0; attempt < retries; attempt++) {
|
|
2030
|
+
try {
|
|
2031
|
+
const response = await fetch(
|
|
2032
|
+
`${credentials.host}/api/public/dataset-run-items`,
|
|
2033
|
+
{
|
|
2034
|
+
method: "POST",
|
|
2035
|
+
headers: {
|
|
2036
|
+
Authorization: basicAuthHeader(
|
|
2037
|
+
credentials.publicKey,
|
|
2038
|
+
credentials.secretKey
|
|
2039
|
+
),
|
|
2040
|
+
"Content-Type": "application/json"
|
|
2041
|
+
},
|
|
2042
|
+
body: JSON.stringify(payload),
|
|
2043
|
+
signal: AbortSignal.timeout(DEFAULT_TIMEOUT_SECONDS * 1e3)
|
|
2044
|
+
}
|
|
2045
|
+
);
|
|
2046
|
+
if (response.status === 200) {
|
|
2047
|
+
return null;
|
|
2048
|
+
}
|
|
2049
|
+
const text = await response.text();
|
|
2050
|
+
lastError = `HTTP ${response.status}: ${text.slice(0, 200)}`;
|
|
2051
|
+
if (response.status !== 404) {
|
|
2052
|
+
return lastError;
|
|
2053
|
+
}
|
|
2054
|
+
} catch (error) {
|
|
2055
|
+
lastError = error instanceof Error ? error.message : String(error);
|
|
2056
|
+
}
|
|
2057
|
+
if (attempt < retries - 1) {
|
|
2058
|
+
await sleep(retryDelaySeconds * 1e3);
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
return lastError;
|
|
2062
|
+
}
|
|
2063
|
+
async function runSuite(suiteToRun, agent, options = {}) {
|
|
2064
|
+
const missPolicy = options.missPolicy ?? "error";
|
|
2065
|
+
const record = options.record ?? Boolean(suiteToRun.credentials);
|
|
2066
|
+
const runName = options.runName ?? `replay-${randomHex().slice(0, 8)}`;
|
|
2067
|
+
const results = {};
|
|
2068
|
+
const pendingRecords = [];
|
|
2069
|
+
for (const testCase of suiteToRun) {
|
|
2070
|
+
try {
|
|
2071
|
+
const stubs = await testCase.stubs({ missPolicy });
|
|
2072
|
+
const output = await agent(
|
|
2073
|
+
testCase.prompt,
|
|
2074
|
+
stubs.asRegistry(),
|
|
2075
|
+
testCase
|
|
2076
|
+
);
|
|
2077
|
+
results[testCase.name] = testCase.check(output);
|
|
2078
|
+
} catch (error) {
|
|
2079
|
+
if (error instanceof StubMissError) {
|
|
2080
|
+
let triage = testCase.check();
|
|
2081
|
+
if (triage.status !== "FAIL") {
|
|
2082
|
+
triage = new ReplayCheck("DRIFT", {
|
|
2083
|
+
drift: triage.drift.length > 0 ? triage.drift : [error.message]
|
|
2084
|
+
});
|
|
2085
|
+
}
|
|
2086
|
+
results[testCase.name] = triage;
|
|
2087
|
+
} else if (error instanceof DriftError) {
|
|
2088
|
+
results[testCase.name] = new ReplayCheck("DRIFT", {
|
|
2089
|
+
drift: [error.message]
|
|
2090
|
+
});
|
|
2091
|
+
} else {
|
|
2092
|
+
throw error;
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
if (record) {
|
|
2096
|
+
pendingRecords.push([testCase, null, results[testCase.name]]);
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
const recordingErrors = [];
|
|
2100
|
+
for (const [testCase, traceId, triage] of pendingRecords) {
|
|
2101
|
+
const error = await recordRunItem(
|
|
2102
|
+
suiteToRun,
|
|
2103
|
+
testCase,
|
|
2104
|
+
runName,
|
|
2105
|
+
traceId,
|
|
2106
|
+
triage
|
|
2107
|
+
);
|
|
2108
|
+
if (error) {
|
|
2109
|
+
recordingErrors.push(`${testCase.name}: ${error}`);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
return new ReplayReport(results, {
|
|
2113
|
+
runName: record ? runName : null,
|
|
2114
|
+
recordingErrors
|
|
2115
|
+
});
|
|
2116
|
+
}
|
|
2117
|
+
function suite(nameOrId, options = {}) {
|
|
2118
|
+
return ReplaySuite.fromTaskSet(nameOrId, options);
|
|
2119
|
+
}
|
|
2120
|
+
function isPlainObject2(value) {
|
|
2121
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2122
|
+
}
|
|
2123
|
+
function parseJsonObject(text) {
|
|
2124
|
+
try {
|
|
2125
|
+
const parsed = JSON.parse(text);
|
|
2126
|
+
return isPlainObject2(parsed) ? parsed : {};
|
|
2127
|
+
} catch {
|
|
2128
|
+
return {};
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
function randomHex() {
|
|
2132
|
+
return crypto.randomUUID().replace(/-/g, "");
|
|
2133
|
+
}
|
|
2134
|
+
function sleep(ms) {
|
|
2135
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
2136
|
+
}
|
|
2137
|
+
var replay = {
|
|
2138
|
+
suite,
|
|
2139
|
+
run: runSuite
|
|
2140
|
+
};
|
|
2141
|
+
|
|
928
2142
|
// src/security/index.ts
|
|
929
2143
|
var SECURITY_ATTRIBUTES = {
|
|
930
2144
|
AGENT_ID: "gateway.agent.id",
|
|
@@ -1146,10 +2360,10 @@ var GatewaySecurity = class _GatewaySecurity {
|
|
|
1146
2360
|
if (resolve.blockedToolIds.includes(toolId)) return false;
|
|
1147
2361
|
return true;
|
|
1148
2362
|
}
|
|
1149
|
-
normalizeContext(
|
|
2363
|
+
normalizeContext(context4) {
|
|
1150
2364
|
return {
|
|
1151
|
-
...
|
|
1152
|
-
metadata:
|
|
2365
|
+
...context4 ?? {},
|
|
2366
|
+
metadata: context4?.metadata ?? {}
|
|
1153
2367
|
};
|
|
1154
2368
|
}
|
|
1155
2369
|
authHeader() {
|
|
@@ -1186,13 +2400,224 @@ var gateway = {
|
|
|
1186
2400
|
fromEnv: GatewaySecurity.fromEnv
|
|
1187
2401
|
};
|
|
1188
2402
|
|
|
2403
|
+
// src/users/index.ts
|
|
2404
|
+
var UserProfileError = class extends Error {
|
|
2405
|
+
constructor(message, options) {
|
|
2406
|
+
super(message);
|
|
2407
|
+
this.name = "UserProfileError";
|
|
2408
|
+
this.cause = options?.cause;
|
|
2409
|
+
}
|
|
2410
|
+
};
|
|
2411
|
+
var UsersClient = class _UsersClient {
|
|
2412
|
+
host;
|
|
2413
|
+
publicKey;
|
|
2414
|
+
secretKey;
|
|
2415
|
+
timeoutSeconds;
|
|
2416
|
+
debug;
|
|
2417
|
+
constructor(config) {
|
|
2418
|
+
this.host = config.host.replace(/\/$/, "");
|
|
2419
|
+
this.publicKey = config.publicKey;
|
|
2420
|
+
this.secretKey = config.secretKey;
|
|
2421
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 15;
|
|
2422
|
+
this.debug = config.debug ?? false;
|
|
2423
|
+
}
|
|
2424
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
2425
|
+
static fromEnv(config = {}) {
|
|
2426
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
2427
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
2428
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
2429
|
+
const missing = [
|
|
2430
|
+
["host", host],
|
|
2431
|
+
["publicKey", publicKey],
|
|
2432
|
+
["secretKey", secretKey]
|
|
2433
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
2434
|
+
if (missing.length > 0) {
|
|
2435
|
+
throw new UserProfileError(
|
|
2436
|
+
`Missing Gateway user-profile configuration: ${missing.join(", ")}`
|
|
2437
|
+
);
|
|
2438
|
+
}
|
|
2439
|
+
return new _UsersClient({
|
|
2440
|
+
...config,
|
|
2441
|
+
host,
|
|
2442
|
+
publicKey,
|
|
2443
|
+
secretKey
|
|
2444
|
+
});
|
|
2445
|
+
}
|
|
2446
|
+
/** Fetch a user's stored profile. */
|
|
2447
|
+
async get(userId) {
|
|
2448
|
+
return parseProfile(
|
|
2449
|
+
await this.requestJson(
|
|
2450
|
+
"GET",
|
|
2451
|
+
`/api/public/user-profiles/${encodeURIComponent(userId)}`
|
|
2452
|
+
)
|
|
2453
|
+
);
|
|
2454
|
+
}
|
|
2455
|
+
/**
|
|
2456
|
+
* Upsert a user's profile. `attributes` merge with the stored set unless
|
|
2457
|
+
* `merge: false` (which replaces it).
|
|
2458
|
+
*/
|
|
2459
|
+
async update(userId, update = {}) {
|
|
2460
|
+
const body = {
|
|
2461
|
+
userId,
|
|
2462
|
+
attributes: update.attributes ?? {},
|
|
2463
|
+
mergeAttributes: update.merge ?? true,
|
|
2464
|
+
...update.displayName !== void 0 ? { displayName: update.displayName } : {},
|
|
2465
|
+
...update.email !== void 0 ? { email: update.email } : {},
|
|
2466
|
+
...update.notes !== void 0 ? { notes: update.notes } : {}
|
|
2467
|
+
};
|
|
2468
|
+
return parseProfile(
|
|
2469
|
+
await this.requestJson("POST", "/api/public/user-profiles", body)
|
|
2470
|
+
);
|
|
2471
|
+
}
|
|
2472
|
+
async requestJson(method, path, payload) {
|
|
2473
|
+
let response;
|
|
2474
|
+
try {
|
|
2475
|
+
response = await fetch(`${this.host}${path}`, {
|
|
2476
|
+
method,
|
|
2477
|
+
headers: {
|
|
2478
|
+
Authorization: this.authHeader(),
|
|
2479
|
+
"Content-Type": "application/json"
|
|
2480
|
+
},
|
|
2481
|
+
...payload ? { body: JSON.stringify(payload) } : {},
|
|
2482
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
2483
|
+
});
|
|
2484
|
+
} catch (error) {
|
|
2485
|
+
throw new UserProfileError(
|
|
2486
|
+
`${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
2487
|
+
{ cause: error }
|
|
2488
|
+
);
|
|
2489
|
+
}
|
|
2490
|
+
const text = await response.text();
|
|
2491
|
+
if (!response.ok) {
|
|
2492
|
+
throw new UserProfileError(
|
|
2493
|
+
`${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
|
|
2494
|
+
);
|
|
2495
|
+
}
|
|
2496
|
+
this.log("%s %s -> %s", method, path, response.status);
|
|
2497
|
+
return safeJson4(text);
|
|
2498
|
+
}
|
|
2499
|
+
authHeader() {
|
|
2500
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
2501
|
+
"base64"
|
|
2502
|
+
)}`;
|
|
2503
|
+
}
|
|
2504
|
+
log(message, ...args) {
|
|
2505
|
+
if (this.debug) {
|
|
2506
|
+
console.log(`[gateway.users] ${message}`, ...args);
|
|
2507
|
+
}
|
|
2508
|
+
}
|
|
2509
|
+
};
|
|
2510
|
+
function parseProfile(payload) {
|
|
2511
|
+
return {
|
|
2512
|
+
userId: String(payload.userId ?? ""),
|
|
2513
|
+
displayName: typeof payload.displayName === "string" ? payload.displayName : null,
|
|
2514
|
+
email: typeof payload.email === "string" ? payload.email : null,
|
|
2515
|
+
attributes: parseAttributes(payload.attributes),
|
|
2516
|
+
notes: typeof payload.notes === "string" ? payload.notes : null
|
|
2517
|
+
};
|
|
2518
|
+
}
|
|
2519
|
+
function parseAttributes(value) {
|
|
2520
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
|
2521
|
+
const attributes = {};
|
|
2522
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
2523
|
+
attributes[key] = String(entry);
|
|
2524
|
+
}
|
|
2525
|
+
return attributes;
|
|
2526
|
+
}
|
|
2527
|
+
function safeJson4(value) {
|
|
2528
|
+
try {
|
|
2529
|
+
const parsed = JSON.parse(value);
|
|
2530
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
2531
|
+
} catch {
|
|
2532
|
+
return {};
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
var users = {
|
|
2536
|
+
fromEnv: UsersClient.fromEnv
|
|
2537
|
+
};
|
|
2538
|
+
|
|
2539
|
+
// src/tracing/identity.ts
|
|
2540
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
2541
|
+
import { isSpanContextValid, trace } from "@opentelemetry/api";
|
|
2542
|
+
var USER_ID_ATTRIBUTE = "gateway.user.id";
|
|
2543
|
+
var currentUserStorage = new AsyncLocalStorage2();
|
|
2544
|
+
function resolveUserId() {
|
|
2545
|
+
const contextual = currentUserStorage.getStore();
|
|
2546
|
+
if (contextual) return contextual;
|
|
2547
|
+
const envUser = process.env.GATEWAY_USER_ID?.trim();
|
|
2548
|
+
return envUser ? envUser : void 0;
|
|
2549
|
+
}
|
|
2550
|
+
function identify(userId, options = {}) {
|
|
2551
|
+
const cleaned = userId.trim();
|
|
2552
|
+
if (!cleaned) return;
|
|
2553
|
+
currentUserStorage.enterWith(cleaned);
|
|
2554
|
+
try {
|
|
2555
|
+
const span = trace.getActiveSpan();
|
|
2556
|
+
if (span && isSpanContextValid(span.spanContext())) {
|
|
2557
|
+
span.setAttribute(USER_ID_ATTRIBUTE, cleaned);
|
|
2558
|
+
}
|
|
2559
|
+
} catch (error) {
|
|
2560
|
+
debugLog("identify(): no active span to stamp", error);
|
|
2561
|
+
}
|
|
2562
|
+
if (options.attributes || options.displayName || options.email) {
|
|
2563
|
+
void pushProfile(cleaned, options);
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
async function pushProfile(userId, options) {
|
|
2567
|
+
try {
|
|
2568
|
+
const attributes = {};
|
|
2569
|
+
for (const [key, value] of Object.entries(options.attributes ?? {})) {
|
|
2570
|
+
attributes[key] = String(value);
|
|
2571
|
+
}
|
|
2572
|
+
await UsersClient.fromEnv().update(userId, {
|
|
2573
|
+
...options.displayName !== void 0 ? { displayName: options.displayName } : {},
|
|
2574
|
+
...options.email !== void 0 ? { email: options.email } : {},
|
|
2575
|
+
attributes
|
|
2576
|
+
});
|
|
2577
|
+
} catch (error) {
|
|
2578
|
+
debugLog("identify(): profile update failed", error);
|
|
2579
|
+
}
|
|
2580
|
+
}
|
|
2581
|
+
function resetAmbientUser() {
|
|
2582
|
+
currentUserStorage.enterWith(void 0);
|
|
2583
|
+
}
|
|
2584
|
+
var IdentitySpanProcessor = class {
|
|
2585
|
+
onStart(span, _parentContext) {
|
|
2586
|
+
const userId = resolveUserId();
|
|
2587
|
+
if (userId) {
|
|
2588
|
+
span.setAttribute(USER_ID_ATTRIBUTE, userId);
|
|
2589
|
+
}
|
|
2590
|
+
}
|
|
2591
|
+
onEnd(_span) {
|
|
2592
|
+
}
|
|
2593
|
+
async shutdown() {
|
|
2594
|
+
}
|
|
2595
|
+
async forceFlush() {
|
|
2596
|
+
}
|
|
2597
|
+
};
|
|
2598
|
+
function debugLog(message, ...args) {
|
|
2599
|
+
if (process.env.GATEWAY_DEBUG) {
|
|
2600
|
+
console.debug(`[gatewaysdk.tracing] ${message}`, ...args);
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
|
|
1189
2604
|
// src/tracing/index.ts
|
|
1190
2605
|
var tracing_exports = {};
|
|
1191
2606
|
__export(tracing_exports, {
|
|
2607
|
+
IdentitySpanProcessor: () => IdentitySpanProcessor,
|
|
2608
|
+
USER_ID_ATTRIBUTE: () => USER_ID_ATTRIBUTE,
|
|
1192
2609
|
flush: () => flush,
|
|
2610
|
+
identify: () => identify,
|
|
1193
2611
|
init: () => init2,
|
|
2612
|
+
initIsolated: () => initIsolated,
|
|
1194
2613
|
isGatewayConfigured: () => isGatewayConfigured,
|
|
1195
2614
|
isInitialized: () => isInitialized,
|
|
2615
|
+
observe: () => observe,
|
|
2616
|
+
observeAnthropic: () => observeAnthropic,
|
|
2617
|
+
observeClaude: () => observeClaude,
|
|
2618
|
+
observeOpenAI: () => observeOpenAI,
|
|
2619
|
+
resetAmbientUser: () => resetAmbientUser,
|
|
2620
|
+
resolveUserId: () => resolveUserId,
|
|
1196
2621
|
shutdown: () => shutdown2
|
|
1197
2622
|
});
|
|
1198
2623
|
|
|
@@ -1223,12 +2648,7 @@ function log(message, ...args) {
|
|
|
1223
2648
|
function warn(message, ...args) {
|
|
1224
2649
|
console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
|
|
1225
2650
|
}
|
|
1226
|
-
function
|
|
1227
|
-
if (state.initialized) {
|
|
1228
|
-
log("Tracing already initialized. Call shutdown() first to reinitialize.");
|
|
1229
|
-
return;
|
|
1230
|
-
}
|
|
1231
|
-
state.debug = config.debug ?? false;
|
|
2651
|
+
function buildProvider(config, logFn) {
|
|
1232
2652
|
const host = config.host ?? getEnv(ENV_VARS.HOST);
|
|
1233
2653
|
const publicKey = config.publicKey ?? getEnv(ENV_VARS.PUBLIC_KEY);
|
|
1234
2654
|
const secretKey = config.secretKey ?? getEnv(ENV_VARS.SECRET_KEY);
|
|
@@ -1305,17 +2725,18 @@ function init2(config = {}) {
|
|
|
1305
2725
|
Authorization: authHeader
|
|
1306
2726
|
}
|
|
1307
2727
|
});
|
|
1308
|
-
|
|
2728
|
+
logFn("Configured Gateway exporter: endpoint=%s", endpoint);
|
|
1309
2729
|
const spanProcessors = [];
|
|
1310
2730
|
if (config.customSpanProcessors) {
|
|
1311
2731
|
for (const sp of config.customSpanProcessors) {
|
|
1312
2732
|
spanProcessors.push(sp);
|
|
1313
|
-
|
|
2733
|
+
logFn(
|
|
1314
2734
|
"Added custom span processor: %s",
|
|
1315
2735
|
sp.constructor?.name ?? "unknown"
|
|
1316
2736
|
);
|
|
1317
2737
|
}
|
|
1318
2738
|
}
|
|
2739
|
+
spanProcessors.push(new IdentitySpanProcessor());
|
|
1319
2740
|
const batchConfig = {};
|
|
1320
2741
|
if (config.batch?.maxQueueSize) {
|
|
1321
2742
|
batchConfig.maxQueueSize = config.batch.maxQueueSize;
|
|
@@ -1340,6 +2761,15 @@ function init2(config = {}) {
|
|
|
1340
2761
|
spanProcessors
|
|
1341
2762
|
});
|
|
1342
2763
|
}
|
|
2764
|
+
return provider;
|
|
2765
|
+
}
|
|
2766
|
+
function init2(config = {}) {
|
|
2767
|
+
if (state.initialized) {
|
|
2768
|
+
log("Tracing already initialized. Call shutdown() first to reinitialize.");
|
|
2769
|
+
return;
|
|
2770
|
+
}
|
|
2771
|
+
state.debug = config.debug ?? false;
|
|
2772
|
+
const provider = buildProvider(config, log);
|
|
1343
2773
|
provider.register();
|
|
1344
2774
|
state.provider = provider;
|
|
1345
2775
|
state.initialized = true;
|
|
@@ -1348,6 +2778,33 @@ function init2(config = {}) {
|
|
|
1348
2778
|
config.serviceName ?? "gatewaysdk-tracing"
|
|
1349
2779
|
);
|
|
1350
2780
|
}
|
|
2781
|
+
function initIsolated(config = {}) {
|
|
2782
|
+
const debugLog2 = config.debug ? (message, ...args) => console.log(`[gatewaysdk.tracing:isolated] ${message}`, ...args) : () => {
|
|
2783
|
+
};
|
|
2784
|
+
const provider = buildProvider(config, debugLog2);
|
|
2785
|
+
const tracer = provider.getTracer("@withgateway/sdk");
|
|
2786
|
+
debugLog2(
|
|
2787
|
+
"Isolated tracing provider created (service=%s)",
|
|
2788
|
+
config.serviceName ?? "gatewaysdk-tracing"
|
|
2789
|
+
);
|
|
2790
|
+
return {
|
|
2791
|
+
tracer,
|
|
2792
|
+
async flush() {
|
|
2793
|
+
try {
|
|
2794
|
+
await provider.forceFlush();
|
|
2795
|
+
} catch (e) {
|
|
2796
|
+
warn("Error during isolated flush:", e);
|
|
2797
|
+
}
|
|
2798
|
+
},
|
|
2799
|
+
async shutdown() {
|
|
2800
|
+
try {
|
|
2801
|
+
await provider.shutdown();
|
|
2802
|
+
} catch (e) {
|
|
2803
|
+
warn("Error during isolated shutdown:", e);
|
|
2804
|
+
}
|
|
2805
|
+
}
|
|
2806
|
+
};
|
|
2807
|
+
}
|
|
1351
2808
|
async function flush() {
|
|
1352
2809
|
if (!state.provider) {
|
|
1353
2810
|
log("No provider to flush");
|
|
@@ -1384,6 +2841,182 @@ function isGatewayConfigured() {
|
|
|
1384
2841
|
return !!(host && publicKey && secretKey);
|
|
1385
2842
|
}
|
|
1386
2843
|
|
|
2844
|
+
// src/tracing/observe.ts
|
|
2845
|
+
import { trace as trace2, context as context2, SpanStatusCode } from "@opentelemetry/api";
|
|
2846
|
+
function asAttr(value) {
|
|
2847
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
2848
|
+
}
|
|
2849
|
+
async function observe(name, fn, options = {}) {
|
|
2850
|
+
const tracer = options.tracer ?? trace2.getTracer("@withgateway/sdk");
|
|
2851
|
+
const span = tracer.startSpan(name, {
|
|
2852
|
+
attributes: {
|
|
2853
|
+
"openinference.span.kind": options.kind ?? "AGENT",
|
|
2854
|
+
...options.input !== void 0 ? { "input.value": asAttr(options.input) } : {},
|
|
2855
|
+
...options.attributes ?? {}
|
|
2856
|
+
}
|
|
2857
|
+
});
|
|
2858
|
+
try {
|
|
2859
|
+
const result = await context2.with(
|
|
2860
|
+
trace2.setSpan(context2.active(), span),
|
|
2861
|
+
fn
|
|
2862
|
+
);
|
|
2863
|
+
if (options.captureOutput !== false && result !== void 0) {
|
|
2864
|
+
span.setAttribute("output.value", asAttr(result));
|
|
2865
|
+
}
|
|
2866
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
2867
|
+
return result;
|
|
2868
|
+
} catch (error) {
|
|
2869
|
+
span.setStatus({
|
|
2870
|
+
code: SpanStatusCode.ERROR,
|
|
2871
|
+
message: error instanceof Error ? error.message : String(error)
|
|
2872
|
+
});
|
|
2873
|
+
throw error;
|
|
2874
|
+
} finally {
|
|
2875
|
+
span.end();
|
|
2876
|
+
}
|
|
2877
|
+
}
|
|
2878
|
+
|
|
2879
|
+
// src/tracing/instrument.ts
|
|
2880
|
+
function warn2(message, ...args) {
|
|
2881
|
+
console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
|
|
2882
|
+
}
|
|
2883
|
+
function observeOpenAI(client2) {
|
|
2884
|
+
try {
|
|
2885
|
+
const {
|
|
2886
|
+
OpenAIInstrumentation
|
|
2887
|
+
} = __require("@arizeai/openinference-instrumentation-openai");
|
|
2888
|
+
const OpenAIClass = client2.constructor;
|
|
2889
|
+
new OpenAIInstrumentation().manuallyInstrument({ OpenAI: OpenAIClass });
|
|
2890
|
+
} catch (error) {
|
|
2891
|
+
warn2(
|
|
2892
|
+
"observeOpenAI: could not load @arizeai/openinference-instrumentation-openai \u2014 OpenAI calls will not be traced.",
|
|
2893
|
+
error
|
|
2894
|
+
);
|
|
2895
|
+
}
|
|
2896
|
+
return client2;
|
|
2897
|
+
}
|
|
2898
|
+
function observeAnthropic(client2) {
|
|
2899
|
+
try {
|
|
2900
|
+
const {
|
|
2901
|
+
AnthropicInstrumentation
|
|
2902
|
+
} = __require("@arizeai/openinference-instrumentation-anthropic");
|
|
2903
|
+
const AnthropicClass = client2.constructor;
|
|
2904
|
+
new AnthropicInstrumentation().manuallyInstrument({
|
|
2905
|
+
Messages: AnthropicClass.Messages
|
|
2906
|
+
});
|
|
2907
|
+
} catch (error) {
|
|
2908
|
+
warn2(
|
|
2909
|
+
"observeAnthropic: could not load @arizeai/openinference-instrumentation-anthropic \u2014 Anthropic calls will not be traced.",
|
|
2910
|
+
error
|
|
2911
|
+
);
|
|
2912
|
+
}
|
|
2913
|
+
return client2;
|
|
2914
|
+
}
|
|
2915
|
+
|
|
2916
|
+
// src/tracing/claude.ts
|
|
2917
|
+
import {
|
|
2918
|
+
trace as trace3,
|
|
2919
|
+
context as context3,
|
|
2920
|
+
SpanStatusCode as SpanStatusCode2
|
|
2921
|
+
} from "@opentelemetry/api";
|
|
2922
|
+
function cleanToolName(name) {
|
|
2923
|
+
return name.replace(/^mcp__[^_]+__/, "");
|
|
2924
|
+
}
|
|
2925
|
+
function toolResultText(content) {
|
|
2926
|
+
if (typeof content === "string") return content;
|
|
2927
|
+
if (Array.isArray(content))
|
|
2928
|
+
return content.map(
|
|
2929
|
+
(b) => b && typeof b === "object" && "text" in b ? String(b.text) : JSON.stringify(b)
|
|
2930
|
+
).join("");
|
|
2931
|
+
return JSON.stringify(content ?? "");
|
|
2932
|
+
}
|
|
2933
|
+
async function* observeClaude(stream, options) {
|
|
2934
|
+
const tracer = options.tracer ?? trace3.getTracer("@withgateway/sdk");
|
|
2935
|
+
const root = tracer.startSpan(options.name, {
|
|
2936
|
+
attributes: {
|
|
2937
|
+
"openinference.span.kind": "AGENT",
|
|
2938
|
+
...options.input !== void 0 ? { "input.value": options.input } : {},
|
|
2939
|
+
...options.attributes ?? {}
|
|
2940
|
+
}
|
|
2941
|
+
});
|
|
2942
|
+
const rootCtx = trace3.setSpan(context3.active(), root);
|
|
2943
|
+
const openTools = /* @__PURE__ */ new Map();
|
|
2944
|
+
const model = options.model ?? "claude";
|
|
2945
|
+
let finalText = "";
|
|
2946
|
+
try {
|
|
2947
|
+
for await (const message of stream) {
|
|
2948
|
+
if (message?.type === "assistant") {
|
|
2949
|
+
let turnText = "";
|
|
2950
|
+
const toolUses = [];
|
|
2951
|
+
for (const block of message.message?.content ?? []) {
|
|
2952
|
+
if (block?.type === "text") {
|
|
2953
|
+
finalText += block.text;
|
|
2954
|
+
turnText += block.text;
|
|
2955
|
+
}
|
|
2956
|
+
if (block?.type === "tool_use") {
|
|
2957
|
+
toolUses.push({ id: block.id, name: block.name, input: block.input });
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
const gen = tracer.startSpan(
|
|
2961
|
+
"assistant",
|
|
2962
|
+
{
|
|
2963
|
+
attributes: {
|
|
2964
|
+
"openinference.span.kind": "LLM",
|
|
2965
|
+
"llm.model_name": model,
|
|
2966
|
+
"llm.provider": "anthropic",
|
|
2967
|
+
"output.value": turnText || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
|
|
2968
|
+
}
|
|
2969
|
+
},
|
|
2970
|
+
rootCtx
|
|
2971
|
+
);
|
|
2972
|
+
const usage = message.message?.usage;
|
|
2973
|
+
if (usage?.input_tokens != null)
|
|
2974
|
+
gen.setAttribute("llm.token_count.prompt", usage.input_tokens);
|
|
2975
|
+
if (usage?.output_tokens != null)
|
|
2976
|
+
gen.setAttribute("llm.token_count.completion", usage.output_tokens);
|
|
2977
|
+
gen.end();
|
|
2978
|
+
for (const t of toolUses) {
|
|
2979
|
+
const span = tracer.startSpan(
|
|
2980
|
+
`tool.${cleanToolName(t.name)}`,
|
|
2981
|
+
{
|
|
2982
|
+
attributes: {
|
|
2983
|
+
"openinference.span.kind": "TOOL",
|
|
2984
|
+
"tool.name": cleanToolName(t.name),
|
|
2985
|
+
"input.value": JSON.stringify(t.input)
|
|
2986
|
+
}
|
|
2987
|
+
},
|
|
2988
|
+
rootCtx
|
|
2989
|
+
);
|
|
2990
|
+
openTools.set(t.id, span);
|
|
2991
|
+
}
|
|
2992
|
+
}
|
|
2993
|
+
if (message?.type === "user") {
|
|
2994
|
+
const content = message.message?.content;
|
|
2995
|
+
if (Array.isArray(content)) {
|
|
2996
|
+
for (const block of content) {
|
|
2997
|
+
if (block?.type === "tool_result") {
|
|
2998
|
+
const span = openTools.get(block.tool_use_id);
|
|
2999
|
+
if (span) {
|
|
3000
|
+
span.setAttribute("output.value", toolResultText(block.content));
|
|
3001
|
+
span.setStatus({ code: SpanStatusCode2.OK });
|
|
3002
|
+
span.end();
|
|
3003
|
+
openTools.delete(block.tool_use_id);
|
|
3004
|
+
}
|
|
3005
|
+
}
|
|
3006
|
+
}
|
|
3007
|
+
}
|
|
3008
|
+
}
|
|
3009
|
+
yield message;
|
|
3010
|
+
}
|
|
3011
|
+
} finally {
|
|
3012
|
+
for (const span of openTools.values()) span.end();
|
|
3013
|
+
openTools.clear();
|
|
3014
|
+
root.setAttribute("output.value", finalText);
|
|
3015
|
+
root.setStatus({ code: SpanStatusCode2.OK });
|
|
3016
|
+
root.end();
|
|
3017
|
+
}
|
|
3018
|
+
}
|
|
3019
|
+
|
|
1387
3020
|
// src/index.ts
|
|
1388
3021
|
var tracing = {
|
|
1389
3022
|
...tracing_exports,
|
|
@@ -1402,6 +3035,11 @@ var tracing = {
|
|
|
1402
3035
|
}
|
|
1403
3036
|
};
|
|
1404
3037
|
export {
|
|
3038
|
+
DEFAULT_TIMEOUT_SECONDS,
|
|
3039
|
+
DriftError,
|
|
3040
|
+
ENV_GATEWAY_HOST,
|
|
3041
|
+
ENV_GATEWAY_PUBLIC_KEY,
|
|
3042
|
+
ENV_GATEWAY_SECRET_KEY,
|
|
1405
3043
|
EXPERIMENT_ATTRIBUTES,
|
|
1406
3044
|
ExperimentClient,
|
|
1407
3045
|
ExperimentSpanProcessor,
|
|
@@ -1416,8 +3054,27 @@ export {
|
|
|
1416
3054
|
GatewaySecurityError,
|
|
1417
3055
|
GatewayToolApprovalRequiredError,
|
|
1418
3056
|
GatewayToolBlockedError,
|
|
3057
|
+
Persona,
|
|
3058
|
+
PersonaError,
|
|
3059
|
+
PersonasClient,
|
|
3060
|
+
REPLAY_ENVIRONMENT,
|
|
3061
|
+
ReplayCase,
|
|
3062
|
+
ReplayCheck,
|
|
3063
|
+
ReplayError,
|
|
3064
|
+
ReplayReport,
|
|
3065
|
+
ReplaySuite,
|
|
3066
|
+
ReplayTask,
|
|
3067
|
+
SessionLoadError,
|
|
3068
|
+
SessionRecording,
|
|
3069
|
+
SessionTrace,
|
|
3070
|
+
StubMissError,
|
|
3071
|
+
ToolStubRegistry,
|
|
3072
|
+
TraceDiff,
|
|
3073
|
+
UserProfileError,
|
|
3074
|
+
UsersClient,
|
|
1419
3075
|
addExperimentAttributes,
|
|
1420
3076
|
assignVariant,
|
|
3077
|
+
basicAuthHeader,
|
|
1421
3078
|
clearExperimentContext,
|
|
1422
3079
|
computeHash,
|
|
1423
3080
|
context,
|
|
@@ -1425,10 +3082,20 @@ export {
|
|
|
1425
3082
|
singleton_exports as experiments,
|
|
1426
3083
|
gateway,
|
|
1427
3084
|
getExperimentContext,
|
|
3085
|
+
identify,
|
|
3086
|
+
loadSession,
|
|
1428
3087
|
memory,
|
|
3088
|
+
personas,
|
|
1429
3089
|
renderAgentSystem,
|
|
3090
|
+
replay,
|
|
3091
|
+
resolveUserId,
|
|
3092
|
+
runSuite,
|
|
1430
3093
|
runWithExperimentContext,
|
|
3094
|
+
sessionObservationFromApi,
|
|
3095
|
+
sessions,
|
|
1431
3096
|
setExperimentContext,
|
|
1432
|
-
|
|
3097
|
+
suite,
|
|
3098
|
+
tracing,
|
|
3099
|
+
users
|
|
1433
3100
|
};
|
|
1434
3101
|
//# sourceMappingURL=index.mjs.map
|