@withgateway/sdk 0.1.4 → 0.1.6

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.
Files changed (41) hide show
  1. package/dist/index-DnowA8MG.d.mts +447 -0
  2. package/dist/index-DnowA8MG.d.ts +447 -0
  3. package/dist/index.d.mts +6 -1
  4. package/dist/index.d.ts +6 -1
  5. package/dist/index.js +1551 -33
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.mjs +1511 -27
  8. package/dist/index.mjs.map +1 -1
  9. package/dist/personas/index.d.mts +102 -0
  10. package/dist/personas/index.d.ts +102 -0
  11. package/dist/personas/index.js +205 -0
  12. package/dist/personas/index.js.map +1 -0
  13. package/dist/personas/index.mjs +180 -0
  14. package/dist/personas/index.mjs.map +1 -0
  15. package/dist/replay/index.d.mts +351 -0
  16. package/dist/replay/index.d.ts +351 -0
  17. package/dist/replay/index.js +1094 -0
  18. package/dist/replay/index.js.map +1 -0
  19. package/dist/replay/index.mjs +1057 -0
  20. package/dist/replay/index.mjs.map +1 -0
  21. package/dist/sessions/index.d.mts +143 -0
  22. package/dist/sessions/index.d.ts +143 -0
  23. package/dist/sessions/index.js +320 -0
  24. package/dist/sessions/index.js.map +1 -0
  25. package/dist/sessions/index.mjs +288 -0
  26. package/dist/sessions/index.mjs.map +1 -0
  27. package/dist/tracing/index.d.mts +3 -1
  28. package/dist/tracing/index.d.ts +3 -1
  29. package/dist/tracing/index.js +277 -31
  30. package/dist/tracing/index.js.map +1 -1
  31. package/dist/tracing/index.mjs +266 -26
  32. package/dist/tracing/index.mjs.map +1 -1
  33. package/dist/users/index.d.mts +81 -0
  34. package/dist/users/index.d.ts +81 -0
  35. package/dist/users/index.js +168 -0
  36. package/dist/users/index.js.map +1 -0
  37. package/dist/users/index.mjs +144 -0
  38. package/dist/users/index.mjs.map +1 -0
  39. package/package.json +21 -1
  40. package/dist/index-yJgb8wh6.d.mts +0 -273
  41. package/dist/index-yJgb8wh6.d.ts +0 -273
package/dist/index.mjs CHANGED
@@ -927,6 +927,1218 @@ var memory = {
927
927
  fromEnv: GatewayMemory.fromEnv
928
928
  };
929
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
+
930
2142
  // src/security/index.ts
931
2143
  var SECURITY_ATTRIBUTES = {
932
2144
  AGENT_ID: "gateway.agent.id",
@@ -1188,38 +2400,242 @@ var gateway = {
1188
2400
  fromEnv: GatewaySecurity.fromEnv
1189
2401
  };
1190
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
+
1191
2604
  // src/tracing/index.ts
1192
2605
  var tracing_exports = {};
1193
2606
  __export(tracing_exports, {
2607
+ IdentitySpanProcessor: () => IdentitySpanProcessor,
2608
+ USER_ID_ATTRIBUTE: () => USER_ID_ATTRIBUTE,
1194
2609
  flush: () => flush,
2610
+ identify: () => identify,
1195
2611
  init: () => init2,
2612
+ initIsolated: () => initIsolated,
1196
2613
  isGatewayConfigured: () => isGatewayConfigured,
1197
2614
  isInitialized: () => isInitialized,
1198
2615
  observe: () => observe,
1199
2616
  observeAnthropic: () => observeAnthropic,
1200
2617
  observeClaude: () => observeClaude,
1201
2618
  observeOpenAI: () => observeOpenAI,
2619
+ resetAmbientUser: () => resetAmbientUser,
2620
+ resolveUserId: () => resolveUserId,
1202
2621
  shutdown: () => shutdown2
1203
2622
  });
1204
2623
 
1205
2624
  // src/tracing/init.ts
1206
2625
  var ENV_VARS = {
1207
- HOST: ["GATEWAY_HOST", "GATEWAY_HOST"],
1208
- PUBLIC_KEY: ["GATEWAY_PUBLIC_KEY", "GATEWAY_PUBLIC_KEY"],
1209
- SECRET_KEY: ["GATEWAY_SECRET_KEY", "GATEWAY_SECRET_KEY"],
1210
- ENDPOINT: ["GATEWAY_OTLP_ENDPOINT", "GATEWAY_OTLP_ENDPOINT"]
2626
+ HOST: "GATEWAY_HOST",
2627
+ PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
2628
+ SECRET_KEY: "GATEWAY_SECRET_KEY",
2629
+ ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
1211
2630
  };
1212
2631
  var DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
1213
2632
  var state = {
1214
2633
  initialized: false,
1215
2634
  provider: null
1216
2635
  };
1217
- function getEnv(keys) {
1218
- for (const key of keys) {
1219
- const value = process.env[key];
1220
- if (value) return value;
1221
- }
1222
- return void 0;
2636
+ function getEnv(key) {
2637
+ const value = process.env[key];
2638
+ return value ? value : void 0;
1223
2639
  }
1224
2640
  function log(message, ...args) {
1225
2641
  if (state.debug) {
@@ -1229,12 +2645,7 @@ function log(message, ...args) {
1229
2645
  function warn(message, ...args) {
1230
2646
  console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
1231
2647
  }
1232
- function init2(config = {}) {
1233
- if (state.initialized) {
1234
- log("Tracing already initialized. Call shutdown() first to reinitialize.");
1235
- return;
1236
- }
1237
- state.debug = config.debug ?? false;
2648
+ function buildProvider(config, logFn) {
1238
2649
  const host = config.host ?? getEnv(ENV_VARS.HOST);
1239
2650
  const publicKey = config.publicKey ?? getEnv(ENV_VARS.PUBLIC_KEY);
1240
2651
  const secretKey = config.secretKey ?? getEnv(ENV_VARS.SECRET_KEY);
@@ -1311,17 +2722,18 @@ function init2(config = {}) {
1311
2722
  Authorization: authHeader
1312
2723
  }
1313
2724
  });
1314
- log("Configured Gateway exporter: endpoint=%s", endpoint);
2725
+ logFn("Configured Gateway exporter: endpoint=%s", endpoint);
1315
2726
  const spanProcessors = [];
1316
2727
  if (config.customSpanProcessors) {
1317
2728
  for (const sp of config.customSpanProcessors) {
1318
2729
  spanProcessors.push(sp);
1319
- log(
2730
+ logFn(
1320
2731
  "Added custom span processor: %s",
1321
2732
  sp.constructor?.name ?? "unknown"
1322
2733
  );
1323
2734
  }
1324
2735
  }
2736
+ spanProcessors.push(new IdentitySpanProcessor());
1325
2737
  const batchConfig = {};
1326
2738
  if (config.batch?.maxQueueSize) {
1327
2739
  batchConfig.maxQueueSize = config.batch.maxQueueSize;
@@ -1346,6 +2758,15 @@ function init2(config = {}) {
1346
2758
  spanProcessors
1347
2759
  });
1348
2760
  }
2761
+ return provider;
2762
+ }
2763
+ function init2(config = {}) {
2764
+ if (state.initialized) {
2765
+ log("Tracing already initialized. Call shutdown() first to reinitialize.");
2766
+ return;
2767
+ }
2768
+ state.debug = config.debug ?? false;
2769
+ const provider = buildProvider(config, log);
1349
2770
  provider.register();
1350
2771
  state.provider = provider;
1351
2772
  state.initialized = true;
@@ -1354,6 +2775,33 @@ function init2(config = {}) {
1354
2775
  config.serviceName ?? "gatewaysdk-tracing"
1355
2776
  );
1356
2777
  }
2778
+ function initIsolated(config = {}) {
2779
+ const debugLog2 = config.debug ? (message, ...args) => console.log(`[gatewaysdk.tracing:isolated] ${message}`, ...args) : () => {
2780
+ };
2781
+ const provider = buildProvider(config, debugLog2);
2782
+ const tracer = provider.getTracer("@withgateway/sdk");
2783
+ debugLog2(
2784
+ "Isolated tracing provider created (service=%s)",
2785
+ config.serviceName ?? "gatewaysdk-tracing"
2786
+ );
2787
+ return {
2788
+ tracer,
2789
+ async flush() {
2790
+ try {
2791
+ await provider.forceFlush();
2792
+ } catch (e) {
2793
+ warn("Error during isolated flush:", e);
2794
+ }
2795
+ },
2796
+ async shutdown() {
2797
+ try {
2798
+ await provider.shutdown();
2799
+ } catch (e) {
2800
+ warn("Error during isolated shutdown:", e);
2801
+ }
2802
+ }
2803
+ };
2804
+ }
1357
2805
  async function flush() {
1358
2806
  if (!state.provider) {
1359
2807
  log("No provider to flush");
@@ -1391,21 +2839,22 @@ function isGatewayConfigured() {
1391
2839
  }
1392
2840
 
1393
2841
  // src/tracing/observe.ts
1394
- import { trace, context as context2, SpanStatusCode } from "@opentelemetry/api";
2842
+ import { trace as trace2, context as context2, SpanStatusCode } from "@opentelemetry/api";
1395
2843
  function asAttr(value) {
1396
2844
  return typeof value === "string" ? value : JSON.stringify(value);
1397
2845
  }
1398
2846
  async function observe(name, fn, options = {}) {
1399
- const tracer = trace.getTracer("@withgateway/sdk");
2847
+ const tracer = options.tracer ?? trace2.getTracer("@withgateway/sdk");
1400
2848
  const span = tracer.startSpan(name, {
1401
2849
  attributes: {
1402
2850
  "openinference.span.kind": options.kind ?? "AGENT",
1403
- ...options.input !== void 0 ? { "input.value": asAttr(options.input) } : {}
2851
+ ...options.input !== void 0 ? { "input.value": asAttr(options.input) } : {},
2852
+ ...options.attributes ?? {}
1404
2853
  }
1405
2854
  });
1406
2855
  try {
1407
2856
  const result = await context2.with(
1408
- trace.setSpan(context2.active(), span),
2857
+ trace2.setSpan(context2.active(), span),
1409
2858
  fn
1410
2859
  );
1411
2860
  if (options.captureOutput !== false && result !== void 0) {
@@ -1463,7 +2912,7 @@ function observeAnthropic(client2) {
1463
2912
 
1464
2913
  // src/tracing/claude.ts
1465
2914
  import {
1466
- trace as trace2,
2915
+ trace as trace3,
1467
2916
  context as context3,
1468
2917
  SpanStatusCode as SpanStatusCode2
1469
2918
  } from "@opentelemetry/api";
@@ -1479,14 +2928,15 @@ function toolResultText(content) {
1479
2928
  return JSON.stringify(content ?? "");
1480
2929
  }
1481
2930
  async function* observeClaude(stream, options) {
1482
- const tracer = trace2.getTracer("@withgateway/sdk");
2931
+ const tracer = options.tracer ?? trace3.getTracer("@withgateway/sdk");
1483
2932
  const root = tracer.startSpan(options.name, {
1484
2933
  attributes: {
1485
2934
  "openinference.span.kind": "AGENT",
1486
- ...options.input !== void 0 ? { "input.value": options.input } : {}
2935
+ ...options.input !== void 0 ? { "input.value": options.input } : {},
2936
+ ...options.attributes ?? {}
1487
2937
  }
1488
2938
  });
1489
- const rootCtx = trace2.setSpan(context3.active(), root);
2939
+ const rootCtx = trace3.setSpan(context3.active(), root);
1490
2940
  const openTools = /* @__PURE__ */ new Map();
1491
2941
  const model = options.model ?? "claude";
1492
2942
  let finalText = "";
@@ -1582,6 +3032,11 @@ var tracing = {
1582
3032
  }
1583
3033
  };
1584
3034
  export {
3035
+ DEFAULT_TIMEOUT_SECONDS,
3036
+ DriftError,
3037
+ ENV_GATEWAY_HOST,
3038
+ ENV_GATEWAY_PUBLIC_KEY,
3039
+ ENV_GATEWAY_SECRET_KEY,
1585
3040
  EXPERIMENT_ATTRIBUTES,
1586
3041
  ExperimentClient,
1587
3042
  ExperimentSpanProcessor,
@@ -1596,8 +3051,27 @@ export {
1596
3051
  GatewaySecurityError,
1597
3052
  GatewayToolApprovalRequiredError,
1598
3053
  GatewayToolBlockedError,
3054
+ Persona,
3055
+ PersonaError,
3056
+ PersonasClient,
3057
+ REPLAY_ENVIRONMENT,
3058
+ ReplayCase,
3059
+ ReplayCheck,
3060
+ ReplayError,
3061
+ ReplayReport,
3062
+ ReplaySuite,
3063
+ ReplayTask,
3064
+ SessionLoadError,
3065
+ SessionRecording,
3066
+ SessionTrace,
3067
+ StubMissError,
3068
+ ToolStubRegistry,
3069
+ TraceDiff,
3070
+ UserProfileError,
3071
+ UsersClient,
1599
3072
  addExperimentAttributes,
1600
3073
  assignVariant,
3074
+ basicAuthHeader,
1601
3075
  clearExperimentContext,
1602
3076
  computeHash,
1603
3077
  context,
@@ -1605,10 +3079,20 @@ export {
1605
3079
  singleton_exports as experiments,
1606
3080
  gateway,
1607
3081
  getExperimentContext,
3082
+ identify,
3083
+ loadSession,
1608
3084
  memory,
3085
+ personas,
1609
3086
  renderAgentSystem,
3087
+ replay,
3088
+ resolveUserId,
3089
+ runSuite,
1610
3090
  runWithExperimentContext,
3091
+ sessionObservationFromApi,
3092
+ sessions,
1611
3093
  setExperimentContext,
1612
- tracing
3094
+ suite,
3095
+ tracing,
3096
+ users
1613
3097
  };
1614
3098
  //# sourceMappingURL=index.mjs.map