@withgateway/sdk 0.2.3 → 0.2.4
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/cli.js +12557 -5244
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +12557 -5244
- package/dist/cli.mjs.map +1 -1
- package/dist/files/index.js +65 -29
- package/dist/files/index.js.map +1 -1
- package/dist/files/index.mjs +53 -18
- package/dist/files/index.mjs.map +1 -1
- package/dist/hooks/index.d.mts +1 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/index.js +4355 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +4243 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/{index-Dpo-t8iO.d.ts → index-1xnxt0Cc.d.ts} +58 -2
- package/dist/index-B0R3IgsV.d.mts +1279 -0
- package/dist/index-B0R3IgsV.d.ts +1279 -0
- package/dist/{index-B5HUQQDE.d.mts → index-C1wDEwH4.d.mts} +58 -2
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +6538 -1549
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6628 -1652
- package/dist/index.mjs.map +1 -1
- package/dist/traces/index.js +54 -18
- package/dist/traces/index.js.map +1 -1
- package/dist/traces/index.mjs +45 -10
- package/dist/traces/index.mjs.map +1 -1
- package/dist/tracing/index.d.mts +1 -1
- package/dist/tracing/index.d.ts +1 -1
- package/dist/tracing/index.js +46 -3
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +41 -2
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/worlds/index.d.mts +365 -6
- package/dist/worlds/index.d.ts +365 -6
- package/dist/worlds/index.js +2037 -216
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +2026 -205
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +6 -1
- package/runtime/gatewaysdk/environment/_materialize.py +3 -3
- package/runtime/gatewaysdk/environment/schema/api.py +116 -2
- package/runtime/gatewaysdk/environment/schema/compiler.py +14 -0
- package/runtime/gatewaysdk/environment/schema/host.py +37 -11
- package/runtime/gatewaysdk/environment/schema/skills/hooks-getting-started/SKILL.md +154 -0
- package/runtime/gatewaysdk/environment/schema/snapshot.py +6 -5
- package/runtime/gatewaysdk/environment/schema/tasks.py +12 -2
- package/runtime/gatewaysdk/environment/schema/workers.py +30 -13
- package/skills/hooks-getting-started/SKILL.md +154 -0
package/dist/worlds/index.mjs
CHANGED
|
@@ -11,6 +11,9 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
11
11
|
var __esm = (fn, res) => function __init() {
|
|
12
12
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
13
13
|
};
|
|
14
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
15
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
+
};
|
|
14
17
|
var __export = (target, all) => {
|
|
15
18
|
for (var name in all)
|
|
16
19
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -24,9 +27,9 @@ function surfaceKinds(s) {
|
|
|
24
27
|
if (s.browser) kinds.push("browser");
|
|
25
28
|
return kinds;
|
|
26
29
|
}
|
|
27
|
-
function uiUrl(ui,
|
|
30
|
+
function uiUrl(ui, path9) {
|
|
28
31
|
const base = ui.url.replace(/\/+$/, "");
|
|
29
|
-
const rel =
|
|
32
|
+
const rel = path9.startsWith("/") ? path9 : `/${path9}`;
|
|
30
33
|
if (!ui.token) return base + rel;
|
|
31
34
|
const [beforeHash, hash] = splitHash(rel);
|
|
32
35
|
const joiner = beforeHash.includes("?") ? "&" : "?";
|
|
@@ -502,6 +505,7 @@ __export(session_exports, {
|
|
|
502
505
|
attachSession: () => attachSession,
|
|
503
506
|
isTaskIdRef: () => isTaskIdRef,
|
|
504
507
|
isTaskSpec: () => isTaskSpec,
|
|
508
|
+
modelToolName: () => modelToolName,
|
|
505
509
|
openSession: () => openSession,
|
|
506
510
|
runSessions: () => runSessions,
|
|
507
511
|
schemaFromSignature: () => schemaFromSignature,
|
|
@@ -560,12 +564,12 @@ function schemaFromSignature(spec) {
|
|
|
560
564
|
};
|
|
561
565
|
}
|
|
562
566
|
async function attachSession(sessionId, options = {}) {
|
|
563
|
-
const
|
|
567
|
+
const state2 = await apiRequest(
|
|
564
568
|
options,
|
|
565
569
|
"GET",
|
|
566
570
|
`/api/public/world-sessions/${encodeURIComponent(sessionId)}`
|
|
567
571
|
);
|
|
568
|
-
return new WorldSession(sessionId,
|
|
572
|
+
return new WorldSession(sessionId, state2, options);
|
|
569
573
|
}
|
|
570
574
|
async function openSession(slugOrRef, taskOrOpts, positionalOpts = {}) {
|
|
571
575
|
const { task, ...opts } = typeof taskOrOpts === "object" && "task" in taskOrOpts ? taskOrOpts : { task: taskOrOpts, ...positionalOpts };
|
|
@@ -583,7 +587,7 @@ async function openSession(slugOrRef, taskOrOpts, positionalOpts = {}) {
|
|
|
583
587
|
);
|
|
584
588
|
const at = slugOrRef.indexOf("@");
|
|
585
589
|
const slug = at === -1 ? slugOrRef : slugOrRef.slice(0, at);
|
|
586
|
-
const
|
|
590
|
+
const state2 = await apiRequest(
|
|
587
591
|
options,
|
|
588
592
|
"POST",
|
|
589
593
|
"/api/public/world-sessions",
|
|
@@ -594,7 +598,7 @@ async function openSession(slugOrRef, taskOrOpts, positionalOpts = {}) {
|
|
|
594
598
|
...opts.surfaces?.length ? { surfaces: opts.surfaces } : {}
|
|
595
599
|
}
|
|
596
600
|
);
|
|
597
|
-
return new WorldSession(
|
|
601
|
+
return new WorldSession(state2.sessionId, state2, options);
|
|
598
602
|
}
|
|
599
603
|
async function runSessions(opts) {
|
|
600
604
|
const options = opts.options ?? {};
|
|
@@ -786,18 +790,18 @@ var init_session = __esm({
|
|
|
786
790
|
}
|
|
787
791
|
/** The reporter `browser()` uses; exposed for a page you drive yourself. */
|
|
788
792
|
browserRecorder(agentId) {
|
|
789
|
-
const
|
|
793
|
+
const path9 = `/api/public/world-sessions/${encodeURIComponent(this.sessionId)}/browser-events`;
|
|
790
794
|
const id = agentId ?? process.env.GATEWAY_AGENT_ID ?? `agent-${randomUUID().replace(/-/g, "").slice(0, 8)}`;
|
|
791
795
|
return new BrowserActionRecorder(
|
|
792
796
|
async (events) => {
|
|
793
|
-
await apiRequest(this.options, "POST",
|
|
797
|
+
await apiRequest(this.options, "POST", path9, { events });
|
|
794
798
|
},
|
|
795
799
|
id,
|
|
796
800
|
{
|
|
797
801
|
// Continue this agent's timeline where the platform has it, so the
|
|
798
802
|
// same id reopened later never re-numbers from zero.
|
|
799
803
|
resumeFrom: async () => {
|
|
800
|
-
const summary = await apiRequest(this.options, "GET",
|
|
804
|
+
const summary = await apiRequest(this.options, "GET", path9);
|
|
801
805
|
return summary.agents.find((a) => a.agentId === id)?.nextSeq ?? 0;
|
|
802
806
|
}
|
|
803
807
|
}
|
|
@@ -830,15 +834,15 @@ var init_session = __esm({
|
|
|
830
834
|
async ready(opts = {}) {
|
|
831
835
|
const deadline = Date.now() + (opts.timeoutMs ?? READY_TIMEOUT_MS);
|
|
832
836
|
for (; ; ) {
|
|
833
|
-
const
|
|
834
|
-
if (
|
|
835
|
-
if (
|
|
837
|
+
const state2 = await this.status();
|
|
838
|
+
if (state2.ready) return state2;
|
|
839
|
+
if (state2.state === "FAILED" || state2.state === "STOPPED")
|
|
836
840
|
throw new WorldSessionCallError(
|
|
837
|
-
`World session ${
|
|
841
|
+
`World session ${state2.state.toLowerCase()} before it was ready.`
|
|
838
842
|
);
|
|
839
843
|
if (Date.now() > deadline)
|
|
840
844
|
throw new WorldSessionTimeout(
|
|
841
|
-
`World session still ${
|
|
845
|
+
`World session still ${state2.state} after ${Math.round((opts.timeoutMs ?? READY_TIMEOUT_MS) / 1e3)}s.`
|
|
842
846
|
);
|
|
843
847
|
await sleep(READY_POLL_MS);
|
|
844
848
|
}
|
|
@@ -914,10 +918,12 @@ var init_session = __esm({
|
|
|
914
918
|
* A rubric grade is finalized by the platform's judge, not the container:
|
|
915
919
|
* the platform answers the enqueue with `pending: true`, and this waits on
|
|
916
920
|
* the judge's budget (15 min) instead of a call's. `timeoutMs` overrides
|
|
917
|
-
* either.
|
|
921
|
+
* either. `report` is the agent's own account of what it did and found,
|
|
922
|
+
* handed to the judge as its claim beside the evidence (the state and the
|
|
923
|
+
* calls); assertions and python graders ignore it. */
|
|
918
924
|
async grade(opts = {}) {
|
|
919
925
|
const raw = await this.exchange(
|
|
920
|
-
{ kind: "grade" },
|
|
926
|
+
opts.report !== void 0 ? { kind: "grade", report: opts.report } : { kind: "grade" },
|
|
921
927
|
opts.timeoutMs
|
|
922
928
|
);
|
|
923
929
|
const record = raw && typeof raw === "object" ? raw : {};
|
|
@@ -1043,80 +1049,1862 @@ var init_session = __esm({
|
|
|
1043
1049
|
}
|
|
1044
1050
|
});
|
|
1045
1051
|
|
|
1046
|
-
// src/worlds/tasks.ts
|
|
1047
|
-
async function createTask(input, options = {}) {
|
|
1048
|
-
return apiRequest(
|
|
1049
|
-
options,
|
|
1050
|
-
"POST",
|
|
1051
|
-
TASKS,
|
|
1052
|
-
compact({
|
|
1053
|
-
spec: input.spec,
|
|
1054
|
-
name: input.name,
|
|
1055
|
-
world: input.world,
|
|
1056
|
-
message: input.message
|
|
1057
|
-
})
|
|
1058
|
-
);
|
|
1052
|
+
// src/worlds/tasks.ts
|
|
1053
|
+
async function createTask(input, options = {}) {
|
|
1054
|
+
return apiRequest(
|
|
1055
|
+
options,
|
|
1056
|
+
"POST",
|
|
1057
|
+
TASKS,
|
|
1058
|
+
compact({
|
|
1059
|
+
spec: input.spec,
|
|
1060
|
+
name: input.name,
|
|
1061
|
+
world: input.world,
|
|
1062
|
+
message: input.message
|
|
1063
|
+
})
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
async function updateTask(taskId, input, options = {}) {
|
|
1067
|
+
if (input.spec === void 0 && input.name === void 0 && input.world === void 0)
|
|
1068
|
+
throw new Error("updateTask: nothing to update \u2014 pass spec, name or world");
|
|
1069
|
+
return apiRequest(
|
|
1070
|
+
options,
|
|
1071
|
+
"PATCH",
|
|
1072
|
+
taskRoute(taskId),
|
|
1073
|
+
compact({
|
|
1074
|
+
spec: input.spec,
|
|
1075
|
+
name: input.name,
|
|
1076
|
+
world: input.world,
|
|
1077
|
+
message: input.message
|
|
1078
|
+
})
|
|
1079
|
+
);
|
|
1080
|
+
}
|
|
1081
|
+
async function listTasks(filter = {}, options = {}) {
|
|
1082
|
+
const params = new URLSearchParams();
|
|
1083
|
+
if (filter.world) params.set("world", filter.world);
|
|
1084
|
+
if (filter.page !== void 0) params.set("page", String(filter.page));
|
|
1085
|
+
if (filter.limit !== void 0) params.set("limit", String(filter.limit));
|
|
1086
|
+
for (const [key, value] of Object.entries(filter.metadata ?? {}))
|
|
1087
|
+
params.set(`metadata.${key}`, value);
|
|
1088
|
+
const query = params.toString();
|
|
1089
|
+
return apiRequest(
|
|
1090
|
+
options,
|
|
1091
|
+
"GET",
|
|
1092
|
+
query ? `${TASKS}?${query}` : TASKS
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
async function getTask(taskId, opts = {}, options = {}) {
|
|
1096
|
+
const query = opts.version === void 0 ? "" : `?version=${String(opts.version)}`;
|
|
1097
|
+
return apiRequest(
|
|
1098
|
+
options,
|
|
1099
|
+
"GET",
|
|
1100
|
+
`${taskRoute(taskId)}${query}`
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
async function deleteTask(taskId, options = {}) {
|
|
1104
|
+
return apiRequest(options, "DELETE", taskRoute(taskId));
|
|
1105
|
+
}
|
|
1106
|
+
async function validateTask(taskId, target = {}, options = {}) {
|
|
1107
|
+
return apiRequest(
|
|
1108
|
+
options,
|
|
1109
|
+
"POST",
|
|
1110
|
+
`${taskRoute(taskId)}/validate`,
|
|
1111
|
+
compact({
|
|
1112
|
+
version: target.version,
|
|
1113
|
+
slug: target.slug,
|
|
1114
|
+
versionId: target.versionId
|
|
1115
|
+
})
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
var TASKS, taskRoute, compact;
|
|
1119
|
+
var init_tasks = __esm({
|
|
1120
|
+
"src/worlds/tasks.ts"() {
|
|
1121
|
+
"use strict";
|
|
1122
|
+
init_worlds();
|
|
1123
|
+
TASKS = "/api/public/world-tasks";
|
|
1124
|
+
taskRoute = (id) => `${TASKS}/${encodeURIComponent(id)}`;
|
|
1125
|
+
compact = (body) => Object.fromEntries(Object.entries(body).filter(([, v]) => v !== void 0));
|
|
1126
|
+
}
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
// src/worlds/taskSessions.ts
|
|
1130
|
+
function aggregateTaskGrade(worlds) {
|
|
1131
|
+
const rewards = {};
|
|
1132
|
+
const ungraded = [];
|
|
1133
|
+
const graded = [];
|
|
1134
|
+
for (const [alias, grade] of Object.entries(worlds)) {
|
|
1135
|
+
if (typeof grade.reward === "number") {
|
|
1136
|
+
rewards[alias] = grade.reward;
|
|
1137
|
+
graded.push(grade.reward);
|
|
1138
|
+
} else ungraded.push(alias);
|
|
1139
|
+
for (const [key, value] of Object.entries(grade.rewards))
|
|
1140
|
+
rewards[`${alias}.${key}`] = value;
|
|
1141
|
+
}
|
|
1142
|
+
const reward = graded.length > 0 ? graded.reduce((a, b) => a + b, 0) / graded.length : null;
|
|
1143
|
+
return { reward, rewards, worlds, ungraded };
|
|
1144
|
+
}
|
|
1145
|
+
function fromResponse(answer, options) {
|
|
1146
|
+
const sessions = {};
|
|
1147
|
+
const worlds = {};
|
|
1148
|
+
for (const [alias, state2] of Object.entries(answer.sessions)) {
|
|
1149
|
+
sessions[alias] = new WorldSession(state2.sessionId, state2, options);
|
|
1150
|
+
worlds[alias] = { slug: state2.slug, ref: state2.ref };
|
|
1151
|
+
}
|
|
1152
|
+
return new TaskSessions(
|
|
1153
|
+
answer.taskId,
|
|
1154
|
+
answer.taskVersion,
|
|
1155
|
+
answer.name,
|
|
1156
|
+
answer.instruction,
|
|
1157
|
+
sessions,
|
|
1158
|
+
worlds,
|
|
1159
|
+
answer.hash ?? null
|
|
1160
|
+
);
|
|
1161
|
+
}
|
|
1162
|
+
async function openTask(ref, opts = {}) {
|
|
1163
|
+
const options = opts.options ?? {};
|
|
1164
|
+
const task = typeof ref === "string" ? {
|
|
1165
|
+
id: ref,
|
|
1166
|
+
...opts.version !== void 0 ? { version: opts.version } : {}
|
|
1167
|
+
} : isTaskIdRef(ref) && opts.version !== void 0 ? { ...ref, version: opts.version } : ref;
|
|
1168
|
+
if (isTaskIdRef(task) ? !task.id : !task.instruction)
|
|
1169
|
+
throw new Error(
|
|
1170
|
+
"openTask needs a task: a stored task id, { id, version? } or a spec with worlds"
|
|
1171
|
+
);
|
|
1172
|
+
if (!isTaskIdRef(task) && !task.worlds)
|
|
1173
|
+
throw new Error(
|
|
1174
|
+
"openTask needs a spec that names its worlds; a single-world spec opens with openSession"
|
|
1175
|
+
);
|
|
1176
|
+
const answer = await apiRequest(
|
|
1177
|
+
options,
|
|
1178
|
+
"POST",
|
|
1179
|
+
TASK_SESSIONS,
|
|
1180
|
+
{
|
|
1181
|
+
task,
|
|
1182
|
+
...opts.reuseWarm === void 0 ? {} : { reuseWarm: opts.reuseWarm },
|
|
1183
|
+
...opts.surfaces?.length ? { surfaces: opts.surfaces } : {}
|
|
1184
|
+
}
|
|
1185
|
+
);
|
|
1186
|
+
return fromResponse(answer, options);
|
|
1187
|
+
}
|
|
1188
|
+
async function listTaskSessions(taskId, options = {}) {
|
|
1189
|
+
const answer = await apiRequest(options, "GET", `${TASK_SESSIONS}?taskId=${encodeURIComponent(taskId)}`);
|
|
1190
|
+
return answer.sessions;
|
|
1191
|
+
}
|
|
1192
|
+
async function attachTask(taskId, options = {}) {
|
|
1193
|
+
const live = await listTaskSessions(taskId, options);
|
|
1194
|
+
if (live.length === 0)
|
|
1195
|
+
throw new Error(
|
|
1196
|
+
`task ${taskId} has no live sessions; run \`task up\` first`
|
|
1197
|
+
);
|
|
1198
|
+
const byAlias = /* @__PURE__ */ new Map();
|
|
1199
|
+
for (const session of live) {
|
|
1200
|
+
const alias = session.alias ?? session.sessionId;
|
|
1201
|
+
byAlias.set(alias, [...byAlias.get(alias) ?? [], session]);
|
|
1202
|
+
}
|
|
1203
|
+
const doubled = [...byAlias.entries()].filter(([, rows]) => rows.length > 1);
|
|
1204
|
+
if (doubled.length > 0)
|
|
1205
|
+
throw new Error(
|
|
1206
|
+
`task ${taskId} has more than one live session for ${doubled.map(
|
|
1207
|
+
([alias, rows]) => `'${alias}' (${rows.map((r) => r.sessionId).join(", ")})`
|
|
1208
|
+
).join(", ")}; pass the manifest from \`task up\`, or close the extras`
|
|
1209
|
+
);
|
|
1210
|
+
return attachManifest(
|
|
1211
|
+
{
|
|
1212
|
+
taskId,
|
|
1213
|
+
taskVersion: live[0].taskVersion,
|
|
1214
|
+
name: null,
|
|
1215
|
+
instruction: null,
|
|
1216
|
+
sessions: Object.fromEntries(
|
|
1217
|
+
[...byAlias.entries()].map(([alias, [row]]) => [
|
|
1218
|
+
alias,
|
|
1219
|
+
{
|
|
1220
|
+
slug: row.slug ?? "",
|
|
1221
|
+
ref: null,
|
|
1222
|
+
versionId: row.versionId,
|
|
1223
|
+
sessionId: row.sessionId,
|
|
1224
|
+
ready: row.ready,
|
|
1225
|
+
state: row.state,
|
|
1226
|
+
surfaces: ["tools"]
|
|
1227
|
+
}
|
|
1228
|
+
])
|
|
1229
|
+
)
|
|
1230
|
+
},
|
|
1231
|
+
options
|
|
1232
|
+
);
|
|
1233
|
+
}
|
|
1234
|
+
async function attachManifest(manifest, options = {}) {
|
|
1235
|
+
const entries = await Promise.all(
|
|
1236
|
+
Object.entries(manifest.sessions).map(
|
|
1237
|
+
async ([alias, entry]) => [alias, await attachSession(entry.sessionId, options)]
|
|
1238
|
+
)
|
|
1239
|
+
);
|
|
1240
|
+
const sessions = Object.fromEntries(entries);
|
|
1241
|
+
const instruction = manifest.instruction ?? entries[0]?.[1].instruction ?? null;
|
|
1242
|
+
return new TaskSessions(
|
|
1243
|
+
manifest.taskId,
|
|
1244
|
+
manifest.taskVersion,
|
|
1245
|
+
manifest.name,
|
|
1246
|
+
instruction,
|
|
1247
|
+
sessions,
|
|
1248
|
+
Object.fromEntries(
|
|
1249
|
+
Object.entries(manifest.sessions).map(([alias, entry]) => [
|
|
1250
|
+
alias,
|
|
1251
|
+
{ slug: entry.slug, ref: entry.ref }
|
|
1252
|
+
])
|
|
1253
|
+
)
|
|
1254
|
+
);
|
|
1255
|
+
}
|
|
1256
|
+
function isTaskManifest(value) {
|
|
1257
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.sessions === "object" && value.sessions !== null && !("instruction" in value && "worlds" in value);
|
|
1258
|
+
}
|
|
1259
|
+
var TASK_SESSIONS, manifestSession, TaskSessions;
|
|
1260
|
+
var init_taskSessions = __esm({
|
|
1261
|
+
"src/worlds/taskSessions.ts"() {
|
|
1262
|
+
"use strict";
|
|
1263
|
+
init_worlds();
|
|
1264
|
+
init_browser();
|
|
1265
|
+
init_session();
|
|
1266
|
+
TASK_SESSIONS = "/api/public/world-task-sessions";
|
|
1267
|
+
manifestSession = (s, state2, world) => ({
|
|
1268
|
+
slug: world.slug,
|
|
1269
|
+
ref: world.ref,
|
|
1270
|
+
versionId: state2.versionId,
|
|
1271
|
+
sessionId: state2.sessionId,
|
|
1272
|
+
ready: state2.ready,
|
|
1273
|
+
state: state2.state,
|
|
1274
|
+
surfaces: surfaceKinds(s),
|
|
1275
|
+
...s.api ? {
|
|
1276
|
+
api: { url: s.api.url, ...s.api.token ? { token: s.api.token } : {} }
|
|
1277
|
+
} : {},
|
|
1278
|
+
...s.ui ? {
|
|
1279
|
+
ui: {
|
|
1280
|
+
url: s.ui.url,
|
|
1281
|
+
entry: uiEntryUrl(s.ui),
|
|
1282
|
+
apiPath: s.ui.apiPath ?? null
|
|
1283
|
+
}
|
|
1284
|
+
} : {},
|
|
1285
|
+
...s.browser ? { browser: { wsUrl: s.browser.wsUrl, viewport: s.browser.viewport } } : {}
|
|
1286
|
+
});
|
|
1287
|
+
TaskSessions = class {
|
|
1288
|
+
constructor(taskId, taskVersion, name, instruction, sessions, worlds, hash = null) {
|
|
1289
|
+
this.taskId = taskId;
|
|
1290
|
+
this.taskVersion = taskVersion;
|
|
1291
|
+
this.name = name;
|
|
1292
|
+
this.instruction = instruction;
|
|
1293
|
+
this.sessions = sessions;
|
|
1294
|
+
this.worlds = worlds;
|
|
1295
|
+
this.hash = hash;
|
|
1296
|
+
}
|
|
1297
|
+
/** This alias's `{slug, ref}` as recorded at open/attach — the world a
|
|
1298
|
+
* `TaskRun` filing needs to name, without exposing the whole map. */
|
|
1299
|
+
worldRef(alias) {
|
|
1300
|
+
return this.worlds[alias] ?? { slug: "", ref: null };
|
|
1301
|
+
}
|
|
1302
|
+
/** The aliases, in the spec's order. */
|
|
1303
|
+
get aliases() {
|
|
1304
|
+
return Object.keys(this.sessions);
|
|
1305
|
+
}
|
|
1306
|
+
/** The alias's session, or a clear error naming the aliases there are. */
|
|
1307
|
+
session(alias) {
|
|
1308
|
+
const session = this.sessions[alias];
|
|
1309
|
+
if (!session)
|
|
1310
|
+
throw new Error(
|
|
1311
|
+
`task has no world '${alias}'; its worlds are ${this.aliases.join(", ")}`
|
|
1312
|
+
);
|
|
1313
|
+
return session;
|
|
1314
|
+
}
|
|
1315
|
+
async each(fn) {
|
|
1316
|
+
const entries = await Promise.all(
|
|
1317
|
+
Object.entries(this.sessions).map(
|
|
1318
|
+
async ([alias, session]) => [alias, await fn(session, alias)]
|
|
1319
|
+
)
|
|
1320
|
+
);
|
|
1321
|
+
return Object.fromEntries(entries);
|
|
1322
|
+
}
|
|
1323
|
+
/** Wait until every world answers ready. A world that never does throws
|
|
1324
|
+
* `WorldSessionTimeout` naming it; the others are left up. */
|
|
1325
|
+
async ready(opts = {}) {
|
|
1326
|
+
await this.each((session) => session.ready(opts));
|
|
1327
|
+
}
|
|
1328
|
+
/** Every world's current state, keyed by alias. */
|
|
1329
|
+
async status() {
|
|
1330
|
+
return this.each((session) => session.status());
|
|
1331
|
+
}
|
|
1332
|
+
/** The manifest: where every world answers, keyed by alias. */
|
|
1333
|
+
async manifest() {
|
|
1334
|
+
const sessions = await this.each(
|
|
1335
|
+
async (session, alias) => manifestSession(
|
|
1336
|
+
await session.surfaces(),
|
|
1337
|
+
{
|
|
1338
|
+
sessionId: session.sessionId,
|
|
1339
|
+
ready: session.isReady,
|
|
1340
|
+
state: (await session.status()).state,
|
|
1341
|
+
versionId: session.versionId
|
|
1342
|
+
},
|
|
1343
|
+
this.worlds[alias]
|
|
1344
|
+
)
|
|
1345
|
+
);
|
|
1346
|
+
return {
|
|
1347
|
+
taskId: this.taskId,
|
|
1348
|
+
taskVersion: this.taskVersion,
|
|
1349
|
+
name: this.name,
|
|
1350
|
+
instruction: this.instruction,
|
|
1351
|
+
sessions
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
/** Grade every world and aggregate — see `TaskGrade`. One `report` (the
|
|
1355
|
+
* agent's own account of the whole investigation) goes to every world's
|
|
1356
|
+
* judge; a world graded by assertions or python ignores it. */
|
|
1357
|
+
async grade(opts = {}) {
|
|
1358
|
+
return aggregateTaskGrade(
|
|
1359
|
+
await this.each((session) => session.grade(opts))
|
|
1360
|
+
);
|
|
1361
|
+
}
|
|
1362
|
+
/** Every world's call log, keyed by alias. */
|
|
1363
|
+
async export() {
|
|
1364
|
+
return this.each((session) => session.export());
|
|
1365
|
+
}
|
|
1366
|
+
/** Close every world. Closes are attempted for all of them even when one
|
|
1367
|
+
* fails; the error then names every world that did not close. */
|
|
1368
|
+
async close(opts = {}) {
|
|
1369
|
+
const settled = await Promise.allSettled(
|
|
1370
|
+
Object.entries(this.sessions).map(
|
|
1371
|
+
async ([alias, session]) => [alias, await session.close(opts)]
|
|
1372
|
+
)
|
|
1373
|
+
);
|
|
1374
|
+
const failed = [];
|
|
1375
|
+
const closed = {};
|
|
1376
|
+
settled.forEach((result, i) => {
|
|
1377
|
+
const alias = this.aliases[i];
|
|
1378
|
+
if (result.status === "fulfilled") closed[alias] = result.value[1];
|
|
1379
|
+
else
|
|
1380
|
+
failed.push(
|
|
1381
|
+
`${alias}: ${result.reason instanceof Error ? result.reason.message : String(result.reason)}`
|
|
1382
|
+
);
|
|
1383
|
+
});
|
|
1384
|
+
if (failed.length > 0)
|
|
1385
|
+
throw new Error(
|
|
1386
|
+
`${failed.length} of ${this.aliases.length} worlds did not close \u2014 ${failed.join("; ")}`
|
|
1387
|
+
);
|
|
1388
|
+
return closed;
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
}
|
|
1392
|
+
});
|
|
1393
|
+
|
|
1394
|
+
// src/worlds/evals.ts
|
|
1395
|
+
var EVALUATIONS, GatewayEvalsClient;
|
|
1396
|
+
var init_evals = __esm({
|
|
1397
|
+
"src/worlds/evals.ts"() {
|
|
1398
|
+
"use strict";
|
|
1399
|
+
init_worlds();
|
|
1400
|
+
EVALUATIONS = "/api/public/benchmark-containers/evaluations";
|
|
1401
|
+
GatewayEvalsClient = class {
|
|
1402
|
+
constructor(options = {}) {
|
|
1403
|
+
this.options = options;
|
|
1404
|
+
}
|
|
1405
|
+
/** Anchor a new evaluation to `versionId`; returns its id. */
|
|
1406
|
+
async createEvaluation(versionId, opts = {}) {
|
|
1407
|
+
const body = { versionId };
|
|
1408
|
+
if (opts.model !== void 0) body.model = opts.model;
|
|
1409
|
+
if (opts.rolloutsPerExample !== void 0)
|
|
1410
|
+
body.rolloutsPerExample = opts.rolloutsPerExample;
|
|
1411
|
+
if (opts.metadata !== void 0) body.metadata = opts.metadata;
|
|
1412
|
+
const answer = await apiRequest(
|
|
1413
|
+
this.options,
|
|
1414
|
+
"POST",
|
|
1415
|
+
EVALUATIONS,
|
|
1416
|
+
body
|
|
1417
|
+
);
|
|
1418
|
+
return answer.evaluationId;
|
|
1419
|
+
}
|
|
1420
|
+
/** Push one batch of samples; returns how many the platform inserted. */
|
|
1421
|
+
async pushSamples(evaluationId, samples) {
|
|
1422
|
+
const answer = await apiRequest(
|
|
1423
|
+
this.options,
|
|
1424
|
+
"POST",
|
|
1425
|
+
`${EVALUATIONS}/${encodeURIComponent(evaluationId)}/samples`,
|
|
1426
|
+
{ samples }
|
|
1427
|
+
);
|
|
1428
|
+
return answer.inserted ?? samples.length;
|
|
1429
|
+
}
|
|
1430
|
+
/** Close the evaluation out; returns its final status. */
|
|
1431
|
+
async finalizeEvaluation(evaluationId, opts = {}) {
|
|
1432
|
+
const body = {
|
|
1433
|
+
status: opts.status ?? "COMPLETED"
|
|
1434
|
+
};
|
|
1435
|
+
if (opts.metrics !== void 0) body.metrics = opts.metrics;
|
|
1436
|
+
const answer = await apiRequest(
|
|
1437
|
+
this.options,
|
|
1438
|
+
"POST",
|
|
1439
|
+
`${EVALUATIONS}/${encodeURIComponent(evaluationId)}/finalize`,
|
|
1440
|
+
body
|
|
1441
|
+
);
|
|
1442
|
+
return answer.status;
|
|
1443
|
+
}
|
|
1444
|
+
};
|
|
1445
|
+
}
|
|
1446
|
+
});
|
|
1447
|
+
|
|
1448
|
+
// src/users/index.ts
|
|
1449
|
+
function parseEvent(payload) {
|
|
1450
|
+
return {
|
|
1451
|
+
id: String(payload.id ?? ""),
|
|
1452
|
+
userId: String(payload.userId ?? ""),
|
|
1453
|
+
event: String(payload.event ?? ""),
|
|
1454
|
+
value: typeof payload.value === "number" ? payload.value : null,
|
|
1455
|
+
metadata: payload.metadata && typeof payload.metadata === "object" && !Array.isArray(payload.metadata) ? payload.metadata : {},
|
|
1456
|
+
traceId: typeof payload.traceId === "string" ? payload.traceId : null,
|
|
1457
|
+
sessionId: typeof payload.sessionId === "string" ? payload.sessionId : null,
|
|
1458
|
+
createdAt: typeof payload.createdAt === "string" ? new Date(payload.createdAt) : /* @__PURE__ */ new Date(0)
|
|
1459
|
+
};
|
|
1460
|
+
}
|
|
1461
|
+
function parseProfile(payload) {
|
|
1462
|
+
return {
|
|
1463
|
+
userId: String(payload.userId ?? ""),
|
|
1464
|
+
displayName: typeof payload.displayName === "string" ? payload.displayName : null,
|
|
1465
|
+
email: typeof payload.email === "string" ? payload.email : null,
|
|
1466
|
+
attributes: parseAttributes(payload.attributes),
|
|
1467
|
+
notes: typeof payload.notes === "string" ? payload.notes : null
|
|
1468
|
+
};
|
|
1469
|
+
}
|
|
1470
|
+
function parseAttributes(value) {
|
|
1471
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
|
1472
|
+
const attributes = {};
|
|
1473
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
1474
|
+
attributes[key] = String(entry);
|
|
1475
|
+
}
|
|
1476
|
+
return attributes;
|
|
1477
|
+
}
|
|
1478
|
+
function safeJson(value) {
|
|
1479
|
+
try {
|
|
1480
|
+
const parsed = JSON.parse(value);
|
|
1481
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
1482
|
+
} catch {
|
|
1483
|
+
return {};
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
var UserProfileError, UsersClient, users;
|
|
1487
|
+
var init_users = __esm({
|
|
1488
|
+
"src/users/index.ts"() {
|
|
1489
|
+
"use strict";
|
|
1490
|
+
UserProfileError = class extends Error {
|
|
1491
|
+
constructor(message, options) {
|
|
1492
|
+
super(message);
|
|
1493
|
+
this.name = "UserProfileError";
|
|
1494
|
+
this.cause = options?.cause;
|
|
1495
|
+
}
|
|
1496
|
+
};
|
|
1497
|
+
UsersClient = class _UsersClient {
|
|
1498
|
+
host;
|
|
1499
|
+
publicKey;
|
|
1500
|
+
secretKey;
|
|
1501
|
+
timeoutSeconds;
|
|
1502
|
+
debug;
|
|
1503
|
+
constructor(config) {
|
|
1504
|
+
this.host = config.host.replace(/\/$/, "");
|
|
1505
|
+
this.publicKey = config.publicKey;
|
|
1506
|
+
this.secretKey = config.secretKey;
|
|
1507
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 15;
|
|
1508
|
+
this.debug = config.debug ?? false;
|
|
1509
|
+
}
|
|
1510
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
1511
|
+
static fromEnv(config = {}) {
|
|
1512
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
1513
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
1514
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
1515
|
+
const missing = [
|
|
1516
|
+
["host", host],
|
|
1517
|
+
["publicKey", publicKey],
|
|
1518
|
+
["secretKey", secretKey]
|
|
1519
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
1520
|
+
if (missing.length > 0) {
|
|
1521
|
+
throw new UserProfileError(
|
|
1522
|
+
`Missing Gateway user-profile configuration: ${missing.join(", ")}`
|
|
1523
|
+
);
|
|
1524
|
+
}
|
|
1525
|
+
return new _UsersClient({
|
|
1526
|
+
...config,
|
|
1527
|
+
host,
|
|
1528
|
+
publicKey,
|
|
1529
|
+
secretKey
|
|
1530
|
+
});
|
|
1531
|
+
}
|
|
1532
|
+
/** Fetch a user's stored profile. */
|
|
1533
|
+
async get(userId) {
|
|
1534
|
+
return parseProfile(
|
|
1535
|
+
await this.requestJson(
|
|
1536
|
+
"GET",
|
|
1537
|
+
`/api/public/user-profiles/${encodeURIComponent(userId)}`
|
|
1538
|
+
)
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
/**
|
|
1542
|
+
* Upsert a user's profile. `attributes` merge with the stored set unless
|
|
1543
|
+
* `merge: false` (which replaces it).
|
|
1544
|
+
*/
|
|
1545
|
+
async update(userId, update = {}) {
|
|
1546
|
+
const body = {
|
|
1547
|
+
userId,
|
|
1548
|
+
attributes: update.attributes ?? {},
|
|
1549
|
+
mergeAttributes: update.merge ?? true,
|
|
1550
|
+
...update.displayName !== void 0 ? { displayName: update.displayName } : {},
|
|
1551
|
+
...update.email !== void 0 ? { email: update.email } : {},
|
|
1552
|
+
...update.notes !== void 0 ? { notes: update.notes } : {}
|
|
1553
|
+
};
|
|
1554
|
+
return parseProfile(
|
|
1555
|
+
await this.requestJson("POST", "/api/public/user-profiles", body)
|
|
1556
|
+
);
|
|
1557
|
+
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Record a VALUE EVENT for a user — the per-user ROI stream. `value` is
|
|
1560
|
+
* whatever the event is worth in your terms (minutes saved, revenue…);
|
|
1561
|
+
* Gateway sums it in the hours-saved/ROI rollups, and evals and task-set
|
|
1562
|
+
* replays can assert against the events. Link `traceId`/`sessionId` to tie
|
|
1563
|
+
* the event to the run that produced it.
|
|
1564
|
+
*
|
|
1565
|
+
* @example
|
|
1566
|
+
* ```ts
|
|
1567
|
+
* await client.trackEvent("user-123", "checkout_completed", {
|
|
1568
|
+
* value: 25,
|
|
1569
|
+
* metadata: { cartSize: 3 },
|
|
1570
|
+
* sessionId,
|
|
1571
|
+
* });
|
|
1572
|
+
* ```
|
|
1573
|
+
*/
|
|
1574
|
+
async trackEvent(userId, event, options = {}) {
|
|
1575
|
+
const body = {
|
|
1576
|
+
userId,
|
|
1577
|
+
event,
|
|
1578
|
+
metadata: options.metadata ?? {},
|
|
1579
|
+
...options.value !== void 0 ? { value: options.value } : {},
|
|
1580
|
+
...options.traceId !== void 0 ? { traceId: options.traceId } : {},
|
|
1581
|
+
...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {}
|
|
1582
|
+
};
|
|
1583
|
+
return parseEvent(
|
|
1584
|
+
await this.requestJson("POST", "/api/public/user-events", body)
|
|
1585
|
+
);
|
|
1586
|
+
}
|
|
1587
|
+
/** List a project's user events, optionally filtered by user/event/time. */
|
|
1588
|
+
async listEvents(filter = {}) {
|
|
1589
|
+
const params = new URLSearchParams();
|
|
1590
|
+
if (filter.userId) params.set("userId", filter.userId);
|
|
1591
|
+
if (filter.event) params.set("event", filter.event);
|
|
1592
|
+
if (filter.fromTimestamp)
|
|
1593
|
+
params.set("fromTimestamp", filter.fromTimestamp.toISOString());
|
|
1594
|
+
if (filter.toTimestamp)
|
|
1595
|
+
params.set("toTimestamp", filter.toTimestamp.toISOString());
|
|
1596
|
+
if (filter.page) params.set("page", String(filter.page));
|
|
1597
|
+
if (filter.limit) params.set("limit", String(filter.limit));
|
|
1598
|
+
const qs = params.toString();
|
|
1599
|
+
const payload = await this.requestJson(
|
|
1600
|
+
"GET",
|
|
1601
|
+
`/api/public/user-events${qs ? `?${qs}` : ""}`
|
|
1602
|
+
);
|
|
1603
|
+
const rows = Array.isArray(payload.data) ? payload.data : [];
|
|
1604
|
+
const meta = payload.meta && typeof payload.meta === "object" ? payload.meta : {};
|
|
1605
|
+
return {
|
|
1606
|
+
data: rows.map((row) => parseEvent(row)),
|
|
1607
|
+
totalItems: Number(meta.totalItems ?? rows.length)
|
|
1608
|
+
};
|
|
1609
|
+
}
|
|
1610
|
+
async requestJson(method, path9, payload) {
|
|
1611
|
+
let response;
|
|
1612
|
+
try {
|
|
1613
|
+
response = await fetch(`${this.host}${path9}`, {
|
|
1614
|
+
method,
|
|
1615
|
+
headers: {
|
|
1616
|
+
Authorization: this.authHeader(),
|
|
1617
|
+
"Content-Type": "application/json"
|
|
1618
|
+
},
|
|
1619
|
+
...payload ? { body: JSON.stringify(payload) } : {},
|
|
1620
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
1621
|
+
});
|
|
1622
|
+
} catch (error) {
|
|
1623
|
+
throw new UserProfileError(
|
|
1624
|
+
`${method} ${path9} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1625
|
+
{ cause: error }
|
|
1626
|
+
);
|
|
1627
|
+
}
|
|
1628
|
+
const text2 = await response.text();
|
|
1629
|
+
if (!response.ok) {
|
|
1630
|
+
throw new UserProfileError(
|
|
1631
|
+
`${method} ${path9} failed (${response.status}): ${text2.slice(0, 500)}`
|
|
1632
|
+
);
|
|
1633
|
+
}
|
|
1634
|
+
this.log("%s %s -> %s", method, path9, response.status);
|
|
1635
|
+
return safeJson(text2);
|
|
1636
|
+
}
|
|
1637
|
+
authHeader() {
|
|
1638
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
1639
|
+
"base64"
|
|
1640
|
+
)}`;
|
|
1641
|
+
}
|
|
1642
|
+
log(message, ...args) {
|
|
1643
|
+
if (this.debug) {
|
|
1644
|
+
console.log(`[gateway.users] ${message}`, ...args);
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
};
|
|
1648
|
+
users = {
|
|
1649
|
+
fromEnv: UsersClient.fromEnv
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1652
|
+
});
|
|
1653
|
+
|
|
1654
|
+
// src/tracing/identity.ts
|
|
1655
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
1656
|
+
import { isSpanContextValid, trace } from "@opentelemetry/api";
|
|
1657
|
+
function resolveUserId() {
|
|
1658
|
+
const contextual = currentUserStorage.getStore();
|
|
1659
|
+
if (contextual) return contextual;
|
|
1660
|
+
const envUser = process.env.GATEWAY_USER_ID?.trim();
|
|
1661
|
+
return envUser ? envUser : void 0;
|
|
1662
|
+
}
|
|
1663
|
+
var USER_ID_ATTRIBUTE, currentUserStorage, IdentitySpanProcessor;
|
|
1664
|
+
var init_identity = __esm({
|
|
1665
|
+
"src/tracing/identity.ts"() {
|
|
1666
|
+
"use strict";
|
|
1667
|
+
init_users();
|
|
1668
|
+
USER_ID_ATTRIBUTE = "gateway.user.id";
|
|
1669
|
+
currentUserStorage = new AsyncLocalStorage();
|
|
1670
|
+
IdentitySpanProcessor = class {
|
|
1671
|
+
onStart(span, _parentContext) {
|
|
1672
|
+
const userId = resolveUserId();
|
|
1673
|
+
if (userId) {
|
|
1674
|
+
span.setAttribute(USER_ID_ATTRIBUTE, userId);
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
onEnd(_span) {
|
|
1678
|
+
}
|
|
1679
|
+
async shutdown() {
|
|
1680
|
+
}
|
|
1681
|
+
async forceFlush() {
|
|
1682
|
+
}
|
|
1683
|
+
};
|
|
1684
|
+
}
|
|
1685
|
+
});
|
|
1686
|
+
|
|
1687
|
+
// src/tracing/session.ts
|
|
1688
|
+
var session_exports2 = {};
|
|
1689
|
+
__export(session_exports2, {
|
|
1690
|
+
SESSION_ID_ATTRIBUTE: () => SESSION_ID_ATTRIBUTE,
|
|
1691
|
+
SessionSpanProcessor: () => SessionSpanProcessor,
|
|
1692
|
+
getAmbientSessionId: () => getAmbientSessionId,
|
|
1693
|
+
withSession: () => withSession
|
|
1694
|
+
});
|
|
1695
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
1696
|
+
function getAmbientSessionId() {
|
|
1697
|
+
return currentSessionStorage.getStore();
|
|
1698
|
+
}
|
|
1699
|
+
function withSession(sessionId, fn) {
|
|
1700
|
+
return new Promise((resolve6, reject) => {
|
|
1701
|
+
currentSessionStorage.run(sessionId, () => {
|
|
1702
|
+
Promise.resolve().then(fn).then(resolve6, reject);
|
|
1703
|
+
});
|
|
1704
|
+
});
|
|
1705
|
+
}
|
|
1706
|
+
var SESSION_ID_ATTRIBUTE, currentSessionStorage, SessionSpanProcessor;
|
|
1707
|
+
var init_session2 = __esm({
|
|
1708
|
+
"src/tracing/session.ts"() {
|
|
1709
|
+
"use strict";
|
|
1710
|
+
SESSION_ID_ATTRIBUTE = "gateway.session.id";
|
|
1711
|
+
currentSessionStorage = new AsyncLocalStorage2();
|
|
1712
|
+
SessionSpanProcessor = class {
|
|
1713
|
+
onStart(span, _parentContext) {
|
|
1714
|
+
const sessionId = getAmbientSessionId();
|
|
1715
|
+
if (sessionId) {
|
|
1716
|
+
span.setAttribute(SESSION_ID_ATTRIBUTE, sessionId);
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
onEnd(_span) {
|
|
1720
|
+
}
|
|
1721
|
+
async shutdown() {
|
|
1722
|
+
}
|
|
1723
|
+
async forceFlush() {
|
|
1724
|
+
}
|
|
1725
|
+
};
|
|
1726
|
+
}
|
|
1727
|
+
});
|
|
1728
|
+
|
|
1729
|
+
// src/scores/index.ts
|
|
1730
|
+
import { isSpanContextValid as isSpanContextValid2, trace as trace2 } from "@opentelemetry/api";
|
|
1731
|
+
function currentTraceId() {
|
|
1732
|
+
try {
|
|
1733
|
+
const span = trace2.getActiveSpan();
|
|
1734
|
+
if (!span) return void 0;
|
|
1735
|
+
const ctx = span.spanContext();
|
|
1736
|
+
if (!ctx || !isSpanContextValid2(ctx)) return void 0;
|
|
1737
|
+
return ctx.traceId;
|
|
1738
|
+
} catch {
|
|
1739
|
+
return void 0;
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
function coerceSignalValue(value) {
|
|
1743
|
+
if (typeof value === "boolean") {
|
|
1744
|
+
return { numericValue: value ? 1 : 0, inferredType: "BOOLEAN" };
|
|
1745
|
+
}
|
|
1746
|
+
return { numericValue: value, inferredType: "NUMERIC" };
|
|
1747
|
+
}
|
|
1748
|
+
async function success(name, value = true, options = {}) {
|
|
1749
|
+
let client;
|
|
1750
|
+
try {
|
|
1751
|
+
client = ScoresClient.fromEnv();
|
|
1752
|
+
} catch (error) {
|
|
1753
|
+
console.warn(`[gateway.scores] success '${name}' skipped`, error);
|
|
1754
|
+
return;
|
|
1755
|
+
}
|
|
1756
|
+
await client.success(name, value, options);
|
|
1757
|
+
}
|
|
1758
|
+
var SUCCESS_SIGNAL_KEY, SUCCESS_METRIC_NAME_KEY, AGENT_ID_KEY, ScoreError, ScoresClient, scores;
|
|
1759
|
+
var init_scores = __esm({
|
|
1760
|
+
"src/scores/index.ts"() {
|
|
1761
|
+
"use strict";
|
|
1762
|
+
SUCCESS_SIGNAL_KEY = "gateway.success.signal";
|
|
1763
|
+
SUCCESS_METRIC_NAME_KEY = "gateway.success.metricName";
|
|
1764
|
+
AGENT_ID_KEY = "gateway.agent.id";
|
|
1765
|
+
ScoreError = class extends Error {
|
|
1766
|
+
constructor(message, options) {
|
|
1767
|
+
super(message);
|
|
1768
|
+
this.name = "ScoreError";
|
|
1769
|
+
this.cause = options?.cause;
|
|
1770
|
+
}
|
|
1771
|
+
};
|
|
1772
|
+
ScoresClient = class _ScoresClient {
|
|
1773
|
+
host;
|
|
1774
|
+
publicKey;
|
|
1775
|
+
secretKey;
|
|
1776
|
+
timeoutSeconds;
|
|
1777
|
+
debug;
|
|
1778
|
+
constructor(config) {
|
|
1779
|
+
this.host = config.host.replace(/\/$/, "");
|
|
1780
|
+
this.publicKey = config.publicKey;
|
|
1781
|
+
this.secretKey = config.secretKey;
|
|
1782
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 15;
|
|
1783
|
+
this.debug = config.debug ?? false;
|
|
1784
|
+
}
|
|
1785
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
1786
|
+
static fromEnv(config = {}) {
|
|
1787
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
1788
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
1789
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
1790
|
+
const missing = [
|
|
1791
|
+
["host", host],
|
|
1792
|
+
["publicKey", publicKey],
|
|
1793
|
+
["secretKey", secretKey]
|
|
1794
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
1795
|
+
if (missing.length > 0) {
|
|
1796
|
+
throw new ScoreError(
|
|
1797
|
+
`Missing Gateway scores configuration: ${missing.join(", ")}`
|
|
1798
|
+
);
|
|
1799
|
+
}
|
|
1800
|
+
return new _ScoresClient({
|
|
1801
|
+
...config,
|
|
1802
|
+
host,
|
|
1803
|
+
publicKey,
|
|
1804
|
+
secretKey
|
|
1805
|
+
});
|
|
1806
|
+
}
|
|
1807
|
+
/**
|
|
1808
|
+
* Post a score, throwing {@link ScoreError} on any failure. Builds the exact
|
|
1809
|
+
* `POST /api/public/scores` body the Python SDK sends: exactly one of
|
|
1810
|
+
* `sessionId` / `traceId` (+ optional `observationId`) / `datasetRunId`.
|
|
1811
|
+
* When no target is provided, the active OTel trace id is used; if none is
|
|
1812
|
+
* available the call is a no-op (returns without posting), matching Python.
|
|
1813
|
+
*/
|
|
1814
|
+
async postScore(name, value, options = {}) {
|
|
1815
|
+
const body = this.buildScoreBody(name, value, options);
|
|
1816
|
+
if (!body) {
|
|
1817
|
+
this.log(
|
|
1818
|
+
"score '%s' dropped: no sessionId, traceId or datasetRunId available",
|
|
1819
|
+
name
|
|
1820
|
+
);
|
|
1821
|
+
return;
|
|
1822
|
+
}
|
|
1823
|
+
await this.request("POST", "/api/public/scores", body);
|
|
1824
|
+
}
|
|
1825
|
+
/**
|
|
1826
|
+
* Build the score request body, or null when no target can be resolved.
|
|
1827
|
+
* Split out so it is unit-testable and shared by score()/success().
|
|
1828
|
+
*/
|
|
1829
|
+
buildScoreBody(name, value, options = {}) {
|
|
1830
|
+
const traceId = options.traceId ?? currentTraceId();
|
|
1831
|
+
const target = {};
|
|
1832
|
+
if (options.sessionId) {
|
|
1833
|
+
target.sessionId = options.sessionId;
|
|
1834
|
+
} else if (options.datasetRunId) {
|
|
1835
|
+
target.datasetRunId = options.datasetRunId;
|
|
1836
|
+
} else if (traceId) {
|
|
1837
|
+
target.traceId = traceId;
|
|
1838
|
+
if (options.observationId) target.observationId = options.observationId;
|
|
1839
|
+
} else {
|
|
1840
|
+
return null;
|
|
1841
|
+
}
|
|
1842
|
+
const metadata = {
|
|
1843
|
+
source: "gatewaysdk",
|
|
1844
|
+
...options.metadata ?? {}
|
|
1845
|
+
};
|
|
1846
|
+
return {
|
|
1847
|
+
name,
|
|
1848
|
+
value,
|
|
1849
|
+
dataType: options.dataType ?? "NUMERIC",
|
|
1850
|
+
environment: options.environment ?? "default",
|
|
1851
|
+
...options.comment ? { comment: options.comment } : {},
|
|
1852
|
+
metadata,
|
|
1853
|
+
...target
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
/**
|
|
1857
|
+
* Post a score, FAIL-SOFT: warns and resolves on any failure so a broken
|
|
1858
|
+
* transport never throws into (or rejects out of) the caller.
|
|
1859
|
+
*/
|
|
1860
|
+
async score(name, value, options = {}) {
|
|
1861
|
+
try {
|
|
1862
|
+
await this.postScore(name, value, options);
|
|
1863
|
+
} catch (error) {
|
|
1864
|
+
this.warn(`score '${name}' failed`, error);
|
|
1865
|
+
}
|
|
1866
|
+
}
|
|
1867
|
+
/**
|
|
1868
|
+
* Emit a SUCCESS SIGNAL that feeds a Gateway Success Metric — a score tagged
|
|
1869
|
+
* with `gateway.success.signal` + `gateway.success.metricName` (and the agent
|
|
1870
|
+
* identity when `agentId` is set). A boolean `value` records a 1/0 BOOLEAN
|
|
1871
|
+
* score (pass/fail); a number records a NUMERIC score. FAIL-SOFT.
|
|
1872
|
+
*
|
|
1873
|
+
* The metadata shape matches the Python SDK's `Run.success()` exactly, so the
|
|
1874
|
+
* platform's Success Metrics aggregation counts signals from either SDK
|
|
1875
|
+
* without change.
|
|
1876
|
+
*
|
|
1877
|
+
* @example
|
|
1878
|
+
* ```ts
|
|
1879
|
+
* await scores.success("resolved_ticket", true, { agentId: "support-triage", sessionId });
|
|
1880
|
+
* await scores.success("handle_time_s", 42, { agentId: "support-triage", sessionId });
|
|
1881
|
+
* ```
|
|
1882
|
+
*/
|
|
1883
|
+
async success(name, value = true, options = {}) {
|
|
1884
|
+
const { numericValue, inferredType } = coerceSignalValue(value);
|
|
1885
|
+
const { agentId, metricName, metadata, dataType, ...rest } = options;
|
|
1886
|
+
const signalMetadata = {
|
|
1887
|
+
...metadata ?? {},
|
|
1888
|
+
[SUCCESS_SIGNAL_KEY]: true,
|
|
1889
|
+
[SUCCESS_METRIC_NAME_KEY]: metricName ?? name,
|
|
1890
|
+
...agentId ? { [AGENT_ID_KEY]: agentId } : {}
|
|
1891
|
+
};
|
|
1892
|
+
await this.score(name, numericValue, {
|
|
1893
|
+
...rest,
|
|
1894
|
+
dataType: dataType ?? inferredType,
|
|
1895
|
+
metadata: signalMetadata
|
|
1896
|
+
});
|
|
1897
|
+
}
|
|
1898
|
+
async request(method, path9, payload) {
|
|
1899
|
+
let response;
|
|
1900
|
+
try {
|
|
1901
|
+
response = await fetch(`${this.host}${path9}`, {
|
|
1902
|
+
method,
|
|
1903
|
+
headers: {
|
|
1904
|
+
Authorization: this.authHeader(),
|
|
1905
|
+
"Content-Type": "application/json"
|
|
1906
|
+
},
|
|
1907
|
+
body: JSON.stringify(payload),
|
|
1908
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
1909
|
+
});
|
|
1910
|
+
} catch (error) {
|
|
1911
|
+
throw new ScoreError(
|
|
1912
|
+
`${method} ${path9} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1913
|
+
{ cause: error }
|
|
1914
|
+
);
|
|
1915
|
+
}
|
|
1916
|
+
const text2 = await response.text();
|
|
1917
|
+
if (!response.ok) {
|
|
1918
|
+
throw new ScoreError(
|
|
1919
|
+
`${method} ${path9} failed (${response.status}): ${text2.slice(0, 500)}`
|
|
1920
|
+
);
|
|
1921
|
+
}
|
|
1922
|
+
this.log("%s %s -> %s", method, path9, response.status);
|
|
1923
|
+
}
|
|
1924
|
+
authHeader() {
|
|
1925
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
1926
|
+
"base64"
|
|
1927
|
+
)}`;
|
|
1928
|
+
}
|
|
1929
|
+
log(message, ...args) {
|
|
1930
|
+
if (this.debug) {
|
|
1931
|
+
console.log(`[gateway.scores] ${message}`, ...args);
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
warn(message, ...args) {
|
|
1935
|
+
console.warn(`[gateway.scores] ${message}`, ...args);
|
|
1936
|
+
}
|
|
1937
|
+
};
|
|
1938
|
+
scores = {
|
|
1939
|
+
fromEnv: ScoresClient.fromEnv,
|
|
1940
|
+
success
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
});
|
|
1944
|
+
|
|
1945
|
+
// package.json
|
|
1946
|
+
var require_package = __commonJS({
|
|
1947
|
+
"package.json"(exports, module) {
|
|
1948
|
+
module.exports = {
|
|
1949
|
+
name: "@withgateway/sdk",
|
|
1950
|
+
version: "0.2.4",
|
|
1951
|
+
description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
|
|
1952
|
+
main: "./dist/index.js",
|
|
1953
|
+
module: "./dist/index.mjs",
|
|
1954
|
+
types: "./dist/index.d.ts",
|
|
1955
|
+
exports: {
|
|
1956
|
+
".": {
|
|
1957
|
+
types: "./dist/index.d.ts",
|
|
1958
|
+
import: "./dist/index.mjs",
|
|
1959
|
+
require: "./dist/index.js"
|
|
1960
|
+
},
|
|
1961
|
+
"./experiments": {
|
|
1962
|
+
types: "./dist/experiments/index.d.ts",
|
|
1963
|
+
import: "./dist/experiments/index.mjs",
|
|
1964
|
+
require: "./dist/experiments/index.js"
|
|
1965
|
+
},
|
|
1966
|
+
"./context": {
|
|
1967
|
+
types: "./dist/context/index.d.ts",
|
|
1968
|
+
import: "./dist/context/index.mjs",
|
|
1969
|
+
require: "./dist/context/index.js"
|
|
1970
|
+
},
|
|
1971
|
+
"./memory": {
|
|
1972
|
+
types: "./dist/memory/index.d.ts",
|
|
1973
|
+
import: "./dist/memory/index.mjs",
|
|
1974
|
+
require: "./dist/memory/index.js"
|
|
1975
|
+
},
|
|
1976
|
+
"./personas": {
|
|
1977
|
+
types: "./dist/personas/index.d.ts",
|
|
1978
|
+
import: "./dist/personas/index.mjs",
|
|
1979
|
+
require: "./dist/personas/index.js"
|
|
1980
|
+
},
|
|
1981
|
+
"./replay": {
|
|
1982
|
+
types: "./dist/replay/index.d.ts",
|
|
1983
|
+
import: "./dist/replay/index.mjs",
|
|
1984
|
+
require: "./dist/replay/index.js"
|
|
1985
|
+
},
|
|
1986
|
+
"./sessions": {
|
|
1987
|
+
types: "./dist/sessions/index.d.ts",
|
|
1988
|
+
import: "./dist/sessions/index.mjs",
|
|
1989
|
+
require: "./dist/sessions/index.js"
|
|
1990
|
+
},
|
|
1991
|
+
"./tracing": {
|
|
1992
|
+
types: "./dist/tracing/index.d.ts",
|
|
1993
|
+
import: "./dist/tracing/index.mjs",
|
|
1994
|
+
require: "./dist/tracing/index.js"
|
|
1995
|
+
},
|
|
1996
|
+
"./security": {
|
|
1997
|
+
types: "./dist/security/index.d.ts",
|
|
1998
|
+
import: "./dist/security/index.mjs",
|
|
1999
|
+
require: "./dist/security/index.js"
|
|
2000
|
+
},
|
|
2001
|
+
"./users": {
|
|
2002
|
+
types: "./dist/users/index.d.ts",
|
|
2003
|
+
import: "./dist/users/index.mjs",
|
|
2004
|
+
require: "./dist/users/index.js"
|
|
2005
|
+
},
|
|
2006
|
+
"./scores": {
|
|
2007
|
+
types: "./dist/scores/index.d.ts",
|
|
2008
|
+
import: "./dist/scores/index.mjs",
|
|
2009
|
+
require: "./dist/scores/index.js"
|
|
2010
|
+
},
|
|
2011
|
+
"./worlds": {
|
|
2012
|
+
types: "./dist/worlds/index.d.ts",
|
|
2013
|
+
import: "./dist/worlds/index.mjs",
|
|
2014
|
+
require: "./dist/worlds/index.js"
|
|
2015
|
+
},
|
|
2016
|
+
"./files": {
|
|
2017
|
+
types: "./dist/files/index.d.ts",
|
|
2018
|
+
import: "./dist/files/index.mjs",
|
|
2019
|
+
require: "./dist/files/index.js"
|
|
2020
|
+
},
|
|
2021
|
+
"./traces": {
|
|
2022
|
+
types: "./dist/traces/index.d.ts",
|
|
2023
|
+
import: "./dist/traces/index.mjs",
|
|
2024
|
+
require: "./dist/traces/index.js"
|
|
2025
|
+
},
|
|
2026
|
+
"./hooks": {
|
|
2027
|
+
types: "./dist/hooks/index.d.ts",
|
|
2028
|
+
import: "./dist/hooks/index.mjs",
|
|
2029
|
+
require: "./dist/hooks/index.js"
|
|
2030
|
+
},
|
|
2031
|
+
"./cli": {
|
|
2032
|
+
types: "./dist/cli.d.ts",
|
|
2033
|
+
import: "./dist/cli.mjs",
|
|
2034
|
+
require: "./dist/cli.js"
|
|
2035
|
+
}
|
|
2036
|
+
},
|
|
2037
|
+
files: [
|
|
2038
|
+
"dist",
|
|
2039
|
+
"skills",
|
|
2040
|
+
"runtime",
|
|
2041
|
+
"README.md"
|
|
2042
|
+
],
|
|
2043
|
+
publishConfig: {
|
|
2044
|
+
access: "public"
|
|
2045
|
+
},
|
|
2046
|
+
scripts: {
|
|
2047
|
+
prebuild: "node scripts/copy-skills.mjs && node scripts/copy-runtime.mjs",
|
|
2048
|
+
build: "tsup",
|
|
2049
|
+
dev: "tsup --watch",
|
|
2050
|
+
typecheck: "tsc --noEmit",
|
|
2051
|
+
"typecheck:examples": "tsc -p tsconfig.examples.json --noEmit",
|
|
2052
|
+
test: "vitest",
|
|
2053
|
+
"test:run": "vitest run",
|
|
2054
|
+
lint: "eslint src --ext .ts",
|
|
2055
|
+
clean: "rm -rf dist",
|
|
2056
|
+
example: "tsx examples/test-ab-experiment.ts",
|
|
2057
|
+
"test:crm": "tsx examples/test-crm-ab-experiment.ts",
|
|
2058
|
+
"test:crm:balanced": "tsx examples/test-crm-ab-experiment.ts --balanced",
|
|
2059
|
+
"test:crm:full": "tsx examples/test-crm-ab-experiment.ts --users 12 --all-tasks --concurrency 3"
|
|
2060
|
+
},
|
|
2061
|
+
keywords: [
|
|
2062
|
+
"gateway",
|
|
2063
|
+
"ab-testing",
|
|
2064
|
+
"experiments",
|
|
2065
|
+
"opentelemetry",
|
|
2066
|
+
"tracing",
|
|
2067
|
+
"observability",
|
|
2068
|
+
"llm"
|
|
2069
|
+
],
|
|
2070
|
+
author: "Gateway Team",
|
|
2071
|
+
license: "MIT",
|
|
2072
|
+
engines: {
|
|
2073
|
+
node: ">=18.0.0"
|
|
2074
|
+
},
|
|
2075
|
+
peerDependencies: {
|
|
2076
|
+
"@opentelemetry/api": "^1.0.0",
|
|
2077
|
+
"@opentelemetry/exporter-trace-otlp-http": ">=0.50.0",
|
|
2078
|
+
"@opentelemetry/resources": ">=1.0.0",
|
|
2079
|
+
"@opentelemetry/sdk-trace-base": ">=1.0.0",
|
|
2080
|
+
"@opentelemetry/sdk-trace-node": ">=1.0.0",
|
|
2081
|
+
playwright: ">=1.40.0"
|
|
2082
|
+
},
|
|
2083
|
+
peerDependenciesMeta: {
|
|
2084
|
+
"@opentelemetry/api": {
|
|
2085
|
+
optional: true
|
|
2086
|
+
},
|
|
2087
|
+
"@opentelemetry/sdk-trace-base": {
|
|
2088
|
+
optional: true
|
|
2089
|
+
},
|
|
2090
|
+
"@opentelemetry/sdk-trace-node": {
|
|
2091
|
+
optional: true
|
|
2092
|
+
},
|
|
2093
|
+
"@opentelemetry/exporter-trace-otlp-http": {
|
|
2094
|
+
optional: true
|
|
2095
|
+
},
|
|
2096
|
+
"@opentelemetry/resources": {
|
|
2097
|
+
optional: true
|
|
2098
|
+
},
|
|
2099
|
+
playwright: {
|
|
2100
|
+
optional: true
|
|
2101
|
+
}
|
|
2102
|
+
},
|
|
2103
|
+
optionalDependencies: {
|
|
2104
|
+
"@arizeai/openinference-instrumentation-anthropic": "^0.1.15",
|
|
2105
|
+
"@arizeai/openinference-instrumentation-openai": "^4.1.5"
|
|
2106
|
+
},
|
|
2107
|
+
devDependencies: {
|
|
2108
|
+
"@anthropic-ai/sdk": "^0.79.0",
|
|
2109
|
+
"@opentelemetry/api": "^1.9.0",
|
|
2110
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.212.0",
|
|
2111
|
+
"@opentelemetry/resources": "^2.6.0",
|
|
2112
|
+
"@opentelemetry/sdk-trace-base": "^1.30.1",
|
|
2113
|
+
"@opentelemetry/sdk-trace-node": "^2.6.0",
|
|
2114
|
+
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
2115
|
+
"@types/node": "^20.0.0",
|
|
2116
|
+
openai: "^4.70.0",
|
|
2117
|
+
tsup: "^8.0.0",
|
|
2118
|
+
tsx: "^4.0.0",
|
|
2119
|
+
typescript: "^5.0.0",
|
|
2120
|
+
vitest: "^3.2.6"
|
|
2121
|
+
},
|
|
2122
|
+
overrides: {
|
|
2123
|
+
protobufjs: "^8.4.1",
|
|
2124
|
+
"vite@6": "^6.4.3",
|
|
2125
|
+
postcss: "^8.5.18",
|
|
2126
|
+
"picomatch@4": "^4.0.4"
|
|
2127
|
+
},
|
|
2128
|
+
pnpm: {
|
|
2129
|
+
overrides: {
|
|
2130
|
+
protobufjs: "^8.4.1",
|
|
2131
|
+
vitest: "^3.2.6",
|
|
2132
|
+
"vite@6": "^6.4.3",
|
|
2133
|
+
postcss: "^8.5.18",
|
|
2134
|
+
"picomatch@4": "^4.0.4"
|
|
2135
|
+
}
|
|
2136
|
+
},
|
|
2137
|
+
bin: {
|
|
2138
|
+
gateway: "./dist/cli.js"
|
|
2139
|
+
}
|
|
2140
|
+
};
|
|
2141
|
+
}
|
|
2142
|
+
});
|
|
2143
|
+
|
|
2144
|
+
// src/build/index.ts
|
|
2145
|
+
import { createHash } from "crypto";
|
|
2146
|
+
function canonicalJson(value) {
|
|
2147
|
+
if (value === null || typeof value !== "object") {
|
|
2148
|
+
return JSON.stringify(value);
|
|
2149
|
+
}
|
|
2150
|
+
if (Array.isArray(value)) {
|
|
2151
|
+
return `[${value.map(canonicalJson).join(",")}]`;
|
|
2152
|
+
}
|
|
2153
|
+
const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
2154
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
|
|
2155
|
+
}
|
|
2156
|
+
function sha256(text2) {
|
|
2157
|
+
return `sha256:${createHash("sha256").update(text2).digest("hex")}`;
|
|
2158
|
+
}
|
|
2159
|
+
function getRegisteredTools() {
|
|
2160
|
+
return [...registeredTools.values()].sort(
|
|
2161
|
+
(a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0
|
|
2162
|
+
);
|
|
2163
|
+
}
|
|
2164
|
+
function getRegisteredArtifacts() {
|
|
2165
|
+
return [...registeredArtifacts.values()].sort(
|
|
2166
|
+
(a, b) => `${a.kind}:${a.name}` < `${b.kind}:${b.name}` ? -1 : 1
|
|
2167
|
+
);
|
|
2168
|
+
}
|
|
2169
|
+
function getRegisteredSkills() {
|
|
2170
|
+
return Object.fromEntries(
|
|
2171
|
+
[...registeredSkills.entries()].sort(
|
|
2172
|
+
([a], [b]) => a < b ? -1 : a > b ? 1 : 0
|
|
2173
|
+
)
|
|
2174
|
+
);
|
|
2175
|
+
}
|
|
2176
|
+
function getRegisteredPrompts() {
|
|
2177
|
+
return Object.fromEntries(
|
|
2178
|
+
[...registeredPrompts.entries()].sort(
|
|
2179
|
+
([a], [b]) => a < b ? -1 : a > b ? 1 : 0
|
|
2180
|
+
)
|
|
2181
|
+
);
|
|
2182
|
+
}
|
|
2183
|
+
function sdkVersion() {
|
|
2184
|
+
try {
|
|
2185
|
+
const pkg = require_package();
|
|
2186
|
+
return `@withgateway/sdk@${pkg.version ?? "unknown"}`;
|
|
2187
|
+
} catch {
|
|
2188
|
+
return "@withgateway/sdk";
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
function paasGitContext() {
|
|
2192
|
+
const env = process.env;
|
|
2193
|
+
if (env.VERCEL_GIT_COMMIT_SHA) {
|
|
2194
|
+
const isGithub = (env.VERCEL_GIT_PROVIDER ?? "github") === "github";
|
|
2195
|
+
return {
|
|
2196
|
+
provider: "vercel",
|
|
2197
|
+
sha: env.VERCEL_GIT_COMMIT_SHA,
|
|
2198
|
+
repo: isGithub && env.VERCEL_GIT_REPO_OWNER && env.VERCEL_GIT_REPO_SLUG ? `${env.VERCEL_GIT_REPO_OWNER}/${env.VERCEL_GIT_REPO_SLUG}` : void 0,
|
|
2199
|
+
branch: env.VERCEL_GIT_COMMIT_REF,
|
|
2200
|
+
login: env.VERCEL_GIT_COMMIT_AUTHOR_LOGIN
|
|
2201
|
+
};
|
|
2202
|
+
}
|
|
2203
|
+
if (env.RENDER_GIT_COMMIT) {
|
|
2204
|
+
return {
|
|
2205
|
+
provider: "render",
|
|
2206
|
+
sha: env.RENDER_GIT_COMMIT,
|
|
2207
|
+
repo: env.RENDER_GIT_REPO_SLUG,
|
|
2208
|
+
// already "owner/repo"
|
|
2209
|
+
branch: env.RENDER_GIT_BRANCH
|
|
2210
|
+
};
|
|
2211
|
+
}
|
|
2212
|
+
if (env.RAILWAY_GIT_COMMIT_SHA) {
|
|
2213
|
+
return {
|
|
2214
|
+
provider: "railway",
|
|
2215
|
+
sha: env.RAILWAY_GIT_COMMIT_SHA,
|
|
2216
|
+
repo: env.RAILWAY_GIT_REPO_OWNER && env.RAILWAY_GIT_REPO_NAME ? `${env.RAILWAY_GIT_REPO_OWNER}/${env.RAILWAY_GIT_REPO_NAME}` : void 0,
|
|
2217
|
+
branch: env.RAILWAY_GIT_BRANCH,
|
|
2218
|
+
login: env.RAILWAY_GIT_AUTHOR
|
|
2219
|
+
};
|
|
2220
|
+
}
|
|
2221
|
+
if (env.NETLIFY && env.COMMIT_REF) {
|
|
2222
|
+
const repoMatch = /github\.com[/:]([^/]+\/[^/.]+)/.exec(
|
|
2223
|
+
env.REPOSITORY_URL ?? ""
|
|
2224
|
+
);
|
|
2225
|
+
return {
|
|
2226
|
+
provider: "netlify",
|
|
2227
|
+
sha: env.COMMIT_REF,
|
|
2228
|
+
repo: repoMatch?.[1],
|
|
2229
|
+
branch: env.BRANCH
|
|
2230
|
+
};
|
|
2231
|
+
}
|
|
2232
|
+
const bareSha = env.HEROKU_SLUG_COMMIT ?? env.SOURCE_VERSION ?? env.GIT_COMMIT ?? env.COMMIT_SHA;
|
|
2233
|
+
if (bareSha) {
|
|
2234
|
+
return {
|
|
2235
|
+
provider: env.HEROKU_SLUG_COMMIT || env.SOURCE_VERSION ? "heroku" : "generic",
|
|
2236
|
+
sha: bareSha
|
|
2237
|
+
};
|
|
2238
|
+
}
|
|
2239
|
+
return null;
|
|
2240
|
+
}
|
|
2241
|
+
function codeRef() {
|
|
2242
|
+
const paas = paasGitContext();
|
|
2243
|
+
const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA ?? process.env.CI_COMMIT_SHA ?? // GitLab
|
|
2244
|
+
process.env.CIRCLE_SHA1 ?? paas?.sha;
|
|
2245
|
+
if (!sha) return void 0;
|
|
2246
|
+
const repo = process.env.GITHUB_REPOSITORY ?? process.env.CI_PROJECT_PATH ?? paas?.repo;
|
|
2247
|
+
return repo ? `git:${repo}@${sha}` : `git:${sha}`;
|
|
2248
|
+
}
|
|
2249
|
+
function assembleManifest(agentId) {
|
|
2250
|
+
return {
|
|
2251
|
+
agentId,
|
|
2252
|
+
...codeRef() ? { codeRef: codeRef() } : {},
|
|
2253
|
+
sdk: sdkVersion(),
|
|
2254
|
+
tools: getRegisteredTools(),
|
|
2255
|
+
prompts: getRegisteredPrompts(),
|
|
2256
|
+
skills: getRegisteredSkills(),
|
|
2257
|
+
artifacts: getRegisteredArtifacts()
|
|
2258
|
+
};
|
|
2259
|
+
}
|
|
2260
|
+
function computeBuildHash(manifest) {
|
|
2261
|
+
const identity = {
|
|
2262
|
+
...manifest,
|
|
2263
|
+
tools: manifest.tools.map(({ source: _source, ...tool }) => tool),
|
|
2264
|
+
artifacts: manifest.artifacts.map(
|
|
2265
|
+
({ source: _source, ...artifact }) => artifact
|
|
2266
|
+
)
|
|
2267
|
+
};
|
|
2268
|
+
return sha256(canonicalJson(identity));
|
|
2269
|
+
}
|
|
2270
|
+
function attributionFromEnv() {
|
|
2271
|
+
const attribution = { source: "runtime" };
|
|
2272
|
+
if (process.env.GITHUB_ACTIONS) attribution.source = "ci";
|
|
2273
|
+
if (process.env.GITHUB_ACTOR) attribution.githubLogin = process.env.GITHUB_ACTOR;
|
|
2274
|
+
const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA;
|
|
2275
|
+
if (sha) attribution.commitSha = sha;
|
|
2276
|
+
if (process.env.GITHUB_REPOSITORY) attribution.repo = process.env.GITHUB_REPOSITORY;
|
|
2277
|
+
const branch = process.env.GITHUB_HEAD_REF ?? process.env.GITHUB_REF_NAME;
|
|
2278
|
+
if (branch) attribution.branch = branch;
|
|
2279
|
+
const paas = paasGitContext();
|
|
2280
|
+
if (paas) {
|
|
2281
|
+
if (attribution.source === "runtime") attribution.source = `paas:${paas.provider}`;
|
|
2282
|
+
if (!attribution.commitSha && paas.sha) attribution.commitSha = paas.sha;
|
|
2283
|
+
if (!attribution.repo && paas.repo) attribution.repo = paas.repo;
|
|
2284
|
+
if (!attribution.branch && paas.branch) attribution.branch = paas.branch;
|
|
2285
|
+
if (!attribution.githubLogin && paas.login) attribution.githubLogin = paas.login;
|
|
2286
|
+
}
|
|
2287
|
+
const ref = process.env.GITHUB_REF ?? "";
|
|
2288
|
+
const prMatch = /refs\/pull\/(\d+)\//.exec(ref);
|
|
2289
|
+
if (prMatch) attribution.prNumber = Number(prMatch[1]);
|
|
2290
|
+
if (process.env.GATEWAY_PR_TITLE) {
|
|
2291
|
+
attribution.prTitle = process.env.GATEWAY_PR_TITLE.slice(0, 300);
|
|
2292
|
+
}
|
|
2293
|
+
if (process.env.GATEWAY_PR_BODY) {
|
|
2294
|
+
attribution.prDescription = process.env.GATEWAY_PR_BODY.slice(0, 2e3);
|
|
2295
|
+
}
|
|
2296
|
+
if (attribution.repo && attribution.prNumber && !("prUrl" in attribution)) {
|
|
2297
|
+
attribution.prUrl = `https://github.com/${String(attribution.repo)}/pull/${String(attribution.prNumber)}`;
|
|
2298
|
+
}
|
|
2299
|
+
return attribution;
|
|
2300
|
+
}
|
|
2301
|
+
function trackActiveBuild(context) {
|
|
2302
|
+
activeBuilds.push(context);
|
|
2303
|
+
}
|
|
2304
|
+
async function registerBuild(params) {
|
|
2305
|
+
const auth = Buffer.from(`${params.publicKey}:${params.secretKey}`).toString(
|
|
2306
|
+
"base64"
|
|
2307
|
+
);
|
|
2308
|
+
const response = await fetch(
|
|
2309
|
+
`${params.host.replace(/\/$/, "")}/api/public/agent-builds`,
|
|
2310
|
+
{
|
|
2311
|
+
method: "POST",
|
|
2312
|
+
headers: {
|
|
2313
|
+
Authorization: `Basic ${auth}`,
|
|
2314
|
+
"Content-Type": "application/json"
|
|
2315
|
+
},
|
|
2316
|
+
body: JSON.stringify({
|
|
2317
|
+
agentId: params.agentId,
|
|
2318
|
+
buildHash: params.buildHash,
|
|
2319
|
+
...params.version ? { semanticVer: params.version } : {},
|
|
2320
|
+
manifest: params.manifest,
|
|
2321
|
+
attribution: attributionFromEnv()
|
|
2322
|
+
}),
|
|
2323
|
+
signal: AbortSignal.timeout(15e3)
|
|
2324
|
+
}
|
|
2325
|
+
);
|
|
2326
|
+
if (!response.ok) {
|
|
2327
|
+
throw new Error(
|
|
2328
|
+
`build registration failed (${response.status}): ${(await response.text()).slice(0, 300)}`
|
|
2329
|
+
);
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
var registeredTools, registeredPrompts, registeredSkills, registeredArtifacts, activeBuilds;
|
|
2333
|
+
var init_build = __esm({
|
|
2334
|
+
"src/build/index.ts"() {
|
|
2335
|
+
"use strict";
|
|
2336
|
+
registeredTools = /* @__PURE__ */ new Map();
|
|
2337
|
+
registeredPrompts = /* @__PURE__ */ new Map();
|
|
2338
|
+
registeredSkills = /* @__PURE__ */ new Map();
|
|
2339
|
+
registeredArtifacts = /* @__PURE__ */ new Map();
|
|
2340
|
+
activeBuilds = [];
|
|
2341
|
+
}
|
|
2342
|
+
});
|
|
2343
|
+
|
|
2344
|
+
// src/tracing/init.ts
|
|
2345
|
+
var init_exports = {};
|
|
2346
|
+
__export(init_exports, {
|
|
2347
|
+
flush: () => flush,
|
|
2348
|
+
init: () => init,
|
|
2349
|
+
initIsolated: () => initIsolated,
|
|
2350
|
+
isGatewayConfigured: () => isGatewayConfigured,
|
|
2351
|
+
isInitialized: () => isInitialized,
|
|
2352
|
+
shutdown: () => shutdown
|
|
2353
|
+
});
|
|
2354
|
+
function getEnv(key) {
|
|
2355
|
+
const value = process.env[key];
|
|
2356
|
+
return value ? value : void 0;
|
|
1059
2357
|
}
|
|
1060
|
-
|
|
1061
|
-
if (
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
options,
|
|
1065
|
-
"PATCH",
|
|
1066
|
-
taskRoute(taskId),
|
|
1067
|
-
compact({
|
|
1068
|
-
spec: input.spec,
|
|
1069
|
-
name: input.name,
|
|
1070
|
-
world: input.world,
|
|
1071
|
-
message: input.message
|
|
1072
|
-
})
|
|
1073
|
-
);
|
|
2358
|
+
function log(message, ...args) {
|
|
2359
|
+
if (state.debug) {
|
|
2360
|
+
console.log(`[gatewaysdk.tracing] ${message}`, ...args);
|
|
2361
|
+
}
|
|
1074
2362
|
}
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
const
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
2363
|
+
function warn(message, ...args) {
|
|
2364
|
+
console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
|
|
2365
|
+
}
|
|
2366
|
+
function buildProvider(config, logFn) {
|
|
2367
|
+
const host = config.host ?? getEnv(ENV_VARS.HOST);
|
|
2368
|
+
const publicKey = config.publicKey ?? getEnv(ENV_VARS.PUBLIC_KEY);
|
|
2369
|
+
const secretKey = config.secretKey ?? getEnv(ENV_VARS.SECRET_KEY);
|
|
2370
|
+
const endpoint = config.endpoint ?? getEnv(ENV_VARS.ENDPOINT) ?? (host ? `${host.replace(/\/$/, "")}${DEFAULT_OTLP_PATH}` : void 0);
|
|
2371
|
+
const missing = [];
|
|
2372
|
+
if (!host) missing.push("host (or GATEWAY_HOST env var)");
|
|
2373
|
+
if (!publicKey) missing.push("publicKey (or GATEWAY_PUBLIC_KEY env var)");
|
|
2374
|
+
if (!secretKey) missing.push("secretKey (or GATEWAY_SECRET_KEY env var)");
|
|
2375
|
+
if (missing.length > 0) {
|
|
2376
|
+
throw new Error(
|
|
2377
|
+
`Missing required Gateway configuration: ${missing.join(", ")}. Provide these values via init() parameters or environment variables.`
|
|
2378
|
+
);
|
|
2379
|
+
}
|
|
2380
|
+
let NodeTracerProvider;
|
|
2381
|
+
let BatchSpanProcessor;
|
|
2382
|
+
let OTLPTraceExporter;
|
|
2383
|
+
let createResource;
|
|
2384
|
+
let ATTR_SERVICE_NAME;
|
|
2385
|
+
try {
|
|
2386
|
+
const traceNode = __require("@opentelemetry/sdk-trace-node");
|
|
2387
|
+
const traceBase = __require("@opentelemetry/sdk-trace-base");
|
|
2388
|
+
const otlpHttp = __require("@opentelemetry/exporter-trace-otlp-http");
|
|
2389
|
+
const resources = __require("@opentelemetry/resources");
|
|
2390
|
+
NodeTracerProvider = traceNode.NodeTracerProvider;
|
|
2391
|
+
BatchSpanProcessor = traceBase.BatchSpanProcessor;
|
|
2392
|
+
OTLPTraceExporter = otlpHttp.OTLPTraceExporter;
|
|
2393
|
+
if (resources.Resource) {
|
|
2394
|
+
createResource = (attrs) => new resources.Resource(attrs);
|
|
2395
|
+
} else if (resources.resourceFromAttributes) {
|
|
2396
|
+
createResource = resources.resourceFromAttributes;
|
|
2397
|
+
} else {
|
|
2398
|
+
createResource = () => resources.defaultResource();
|
|
2399
|
+
}
|
|
2400
|
+
try {
|
|
2401
|
+
const semconv = __require("@opentelemetry/semantic-conventions");
|
|
2402
|
+
ATTR_SERVICE_NAME = semconv.ATTR_SERVICE_NAME ?? semconv.SEMRESATTRS_SERVICE_NAME ?? "service.name";
|
|
2403
|
+
} catch {
|
|
2404
|
+
ATTR_SERVICE_NAME = "service.name";
|
|
2405
|
+
}
|
|
2406
|
+
} catch {
|
|
2407
|
+
throw new Error(
|
|
2408
|
+
"OpenTelemetry packages not found. Install them with:\nnpm install @opentelemetry/sdk-trace-node @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources"
|
|
2409
|
+
);
|
|
2410
|
+
}
|
|
2411
|
+
const resourceAttrs = {
|
|
2412
|
+
[ATTR_SERVICE_NAME]: config.serviceName ?? "gatewaysdk-tracing"
|
|
2413
|
+
};
|
|
2414
|
+
if (config.environment) {
|
|
2415
|
+
resourceAttrs["gateway.environment"] = config.environment;
|
|
2416
|
+
}
|
|
2417
|
+
if (config.version) {
|
|
2418
|
+
resourceAttrs["gateway.version"] = config.version;
|
|
2419
|
+
}
|
|
2420
|
+
let buildInfo = null;
|
|
2421
|
+
if (config.agent) {
|
|
2422
|
+
const manifest = assembleManifest(config.agent);
|
|
2423
|
+
const buildHash = computeBuildHash(manifest);
|
|
2424
|
+
buildInfo = { manifest, buildHash };
|
|
2425
|
+
resourceAttrs["gateway.agent.id"] = config.agent;
|
|
2426
|
+
resourceAttrs["gateway.build.hash"] = buildHash;
|
|
2427
|
+
if (config.version) {
|
|
2428
|
+
resourceAttrs["gateway.agent.version"] = config.version;
|
|
2429
|
+
}
|
|
2430
|
+
logFn(
|
|
2431
|
+
"Agent build identity: %s @ %s (%d tools, %d prompts)",
|
|
2432
|
+
config.agent,
|
|
2433
|
+
buildHash.slice(0, 15),
|
|
2434
|
+
manifest.tools.length,
|
|
2435
|
+
Object.keys(manifest.prompts).length
|
|
2436
|
+
);
|
|
2437
|
+
}
|
|
2438
|
+
let isLocalhost = false;
|
|
2439
|
+
if (endpoint) {
|
|
2440
|
+
try {
|
|
2441
|
+
const parsed = new URL(endpoint);
|
|
2442
|
+
isLocalhost = parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1";
|
|
2443
|
+
} catch {
|
|
2444
|
+
isLocalhost = false;
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
if (endpoint && !endpoint.startsWith("https://") && !isLocalhost) {
|
|
2448
|
+
throw new Error(
|
|
2449
|
+
`Refusing to send credentials over insecure HTTP to ${endpoint}. Use HTTPS or localhost for development.`
|
|
2450
|
+
);
|
|
2451
|
+
}
|
|
2452
|
+
const credentials = `${publicKey}:${secretKey}`;
|
|
2453
|
+
const encoded = Buffer.from(credentials).toString("base64");
|
|
2454
|
+
const authHeader = `Basic ${encoded}`;
|
|
2455
|
+
const exporter = new OTLPTraceExporter({
|
|
2456
|
+
url: endpoint,
|
|
2457
|
+
headers: {
|
|
2458
|
+
Authorization: authHeader
|
|
2459
|
+
}
|
|
2460
|
+
});
|
|
2461
|
+
logFn("Configured Gateway exporter: endpoint=%s", endpoint);
|
|
2462
|
+
const spanProcessors = [];
|
|
2463
|
+
if (config.customSpanProcessors) {
|
|
2464
|
+
for (const sp of config.customSpanProcessors) {
|
|
2465
|
+
spanProcessors.push(sp);
|
|
2466
|
+
logFn(
|
|
2467
|
+
"Added custom span processor: %s",
|
|
2468
|
+
sp.constructor?.name ?? "unknown"
|
|
2469
|
+
);
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
spanProcessors.push(new IdentitySpanProcessor());
|
|
2473
|
+
spanProcessors.push(new SessionSpanProcessor());
|
|
2474
|
+
const batchConfig = {};
|
|
2475
|
+
if (config.batch?.maxQueueSize) {
|
|
2476
|
+
batchConfig.maxQueueSize = config.batch.maxQueueSize;
|
|
2477
|
+
}
|
|
2478
|
+
if (config.batch?.scheduledDelayMillis) {
|
|
2479
|
+
batchConfig.scheduledDelayMillis = config.batch.scheduledDelayMillis;
|
|
2480
|
+
}
|
|
2481
|
+
if (config.batch?.maxExportBatchSize) {
|
|
2482
|
+
batchConfig.maxExportBatchSize = config.batch.maxExportBatchSize;
|
|
2483
|
+
}
|
|
2484
|
+
spanProcessors.push(new BatchSpanProcessor(exporter, batchConfig));
|
|
2485
|
+
let provider = new NodeTracerProvider({
|
|
2486
|
+
resource: createResource(resourceAttrs)
|
|
2487
|
+
});
|
|
2488
|
+
if (typeof provider.addSpanProcessor === "function") {
|
|
2489
|
+
for (const sp of spanProcessors) {
|
|
2490
|
+
provider.addSpanProcessor(sp);
|
|
2491
|
+
}
|
|
2492
|
+
} else {
|
|
2493
|
+
provider = new NodeTracerProvider({
|
|
2494
|
+
resource: createResource(resourceAttrs),
|
|
2495
|
+
spanProcessors
|
|
2496
|
+
});
|
|
2497
|
+
}
|
|
2498
|
+
return {
|
|
2499
|
+
provider,
|
|
2500
|
+
buildInfo,
|
|
2501
|
+
connection: { host, publicKey, secretKey }
|
|
2502
|
+
};
|
|
2503
|
+
}
|
|
2504
|
+
function scheduleBuildRegistration(config, built) {
|
|
2505
|
+
if (!config.agent || !built.buildInfo || config.registerBuild === false) {
|
|
2506
|
+
return;
|
|
2507
|
+
}
|
|
2508
|
+
const { manifest, buildHash } = built.buildInfo;
|
|
2509
|
+
trackActiveBuild({
|
|
2510
|
+
host: built.connection.host,
|
|
2511
|
+
publicKey: built.connection.publicKey,
|
|
2512
|
+
secretKey: built.connection.secretKey,
|
|
2513
|
+
agentId: config.agent,
|
|
2514
|
+
version: config.version,
|
|
2515
|
+
lastRegisteredHash: buildHash
|
|
2516
|
+
});
|
|
2517
|
+
void registerBuild({
|
|
2518
|
+
host: built.connection.host,
|
|
2519
|
+
publicKey: built.connection.publicKey,
|
|
2520
|
+
secretKey: built.connection.secretKey,
|
|
2521
|
+
agentId: config.agent,
|
|
2522
|
+
version: config.version,
|
|
2523
|
+
manifest,
|
|
2524
|
+
buildHash
|
|
2525
|
+
}).catch((err) => {
|
|
2526
|
+
warn("Build registration failed (agent keeps running):", err);
|
|
2527
|
+
});
|
|
2528
|
+
}
|
|
2529
|
+
function init(config = {}) {
|
|
2530
|
+
if (state.initialized) {
|
|
2531
|
+
log("Tracing already initialized. Call shutdown() first to reinitialize.");
|
|
2532
|
+
return;
|
|
2533
|
+
}
|
|
2534
|
+
state.debug = config.debug ?? false;
|
|
2535
|
+
const built = buildProvider(config, log);
|
|
2536
|
+
const provider = built.provider;
|
|
2537
|
+
provider.register();
|
|
2538
|
+
scheduleBuildRegistration(config, built);
|
|
2539
|
+
state.provider = provider;
|
|
2540
|
+
state.initialized = true;
|
|
2541
|
+
log(
|
|
2542
|
+
"GatewaySDK tracing initialized successfully (service=%s)",
|
|
2543
|
+
config.serviceName ?? "gatewaysdk-tracing"
|
|
1087
2544
|
);
|
|
1088
2545
|
}
|
|
1089
|
-
|
|
1090
|
-
const
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
2546
|
+
function initIsolated(config = {}) {
|
|
2547
|
+
const debugLog = config.debug ? (message, ...args) => console.log(`[gatewaysdk.tracing:isolated] ${message}`, ...args) : () => {
|
|
2548
|
+
};
|
|
2549
|
+
const built = buildProvider(config, debugLog);
|
|
2550
|
+
const provider = built.provider;
|
|
2551
|
+
scheduleBuildRegistration(config, built);
|
|
2552
|
+
const tracer = provider.getTracer("@withgateway/sdk");
|
|
2553
|
+
const scores2 = new ScoresClient({
|
|
2554
|
+
host: built.connection.host,
|
|
2555
|
+
publicKey: built.connection.publicKey,
|
|
2556
|
+
secretKey: built.connection.secretKey,
|
|
2557
|
+
debug: config.debug ?? false
|
|
2558
|
+
});
|
|
2559
|
+
debugLog(
|
|
2560
|
+
"Isolated tracing provider created (service=%s)",
|
|
2561
|
+
config.serviceName ?? "gatewaysdk-tracing"
|
|
1095
2562
|
);
|
|
2563
|
+
return {
|
|
2564
|
+
tracer,
|
|
2565
|
+
scores: scores2,
|
|
2566
|
+
async flush() {
|
|
2567
|
+
try {
|
|
2568
|
+
await provider.forceFlush();
|
|
2569
|
+
} catch (e) {
|
|
2570
|
+
warn("Error during isolated flush:", e);
|
|
2571
|
+
}
|
|
2572
|
+
},
|
|
2573
|
+
async shutdown() {
|
|
2574
|
+
try {
|
|
2575
|
+
await provider.shutdown();
|
|
2576
|
+
} catch (e) {
|
|
2577
|
+
warn("Error during isolated shutdown:", e);
|
|
2578
|
+
}
|
|
2579
|
+
}
|
|
2580
|
+
};
|
|
1096
2581
|
}
|
|
1097
|
-
async function
|
|
1098
|
-
|
|
2582
|
+
async function flush() {
|
|
2583
|
+
if (!state.provider) {
|
|
2584
|
+
log("No provider to flush");
|
|
2585
|
+
return;
|
|
2586
|
+
}
|
|
2587
|
+
try {
|
|
2588
|
+
await state.provider.forceFlush();
|
|
2589
|
+
log("Flush completed successfully");
|
|
2590
|
+
} catch (e) {
|
|
2591
|
+
warn("Error during flush:", e);
|
|
2592
|
+
}
|
|
1099
2593
|
}
|
|
1100
|
-
async function
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
2594
|
+
async function shutdown() {
|
|
2595
|
+
if (!state.initialized || !state.provider) {
|
|
2596
|
+
log("Tracing not initialized, nothing to shutdown");
|
|
2597
|
+
return;
|
|
2598
|
+
}
|
|
2599
|
+
try {
|
|
2600
|
+
await state.provider.shutdown();
|
|
2601
|
+
log("Shutdown complete");
|
|
2602
|
+
} catch (e) {
|
|
2603
|
+
warn("Error during shutdown:", e);
|
|
2604
|
+
}
|
|
2605
|
+
state.provider = null;
|
|
2606
|
+
state.initialized = false;
|
|
2607
|
+
}
|
|
2608
|
+
function isInitialized() {
|
|
2609
|
+
return state.initialized;
|
|
2610
|
+
}
|
|
2611
|
+
function isGatewayConfigured() {
|
|
2612
|
+
const host = getEnv(ENV_VARS.HOST);
|
|
2613
|
+
const publicKey = getEnv(ENV_VARS.PUBLIC_KEY);
|
|
2614
|
+
const secretKey = getEnv(ENV_VARS.SECRET_KEY);
|
|
2615
|
+
return !!(host && publicKey && secretKey);
|
|
2616
|
+
}
|
|
2617
|
+
var ENV_VARS, DEFAULT_OTLP_PATH, state;
|
|
2618
|
+
var init_init = __esm({
|
|
2619
|
+
"src/tracing/init.ts"() {
|
|
2620
|
+
"use strict";
|
|
2621
|
+
init_identity();
|
|
2622
|
+
init_session2();
|
|
2623
|
+
init_scores();
|
|
2624
|
+
init_build();
|
|
2625
|
+
ENV_VARS = {
|
|
2626
|
+
HOST: "GATEWAY_HOST",
|
|
2627
|
+
PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
|
|
2628
|
+
SECRET_KEY: "GATEWAY_SECRET_KEY",
|
|
2629
|
+
ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
|
|
2630
|
+
};
|
|
2631
|
+
DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
|
|
2632
|
+
state = {
|
|
2633
|
+
initialized: false,
|
|
2634
|
+
provider: null
|
|
2635
|
+
};
|
|
2636
|
+
}
|
|
2637
|
+
});
|
|
2638
|
+
|
|
2639
|
+
// src/worlds/taskRun.ts
|
|
2640
|
+
import { randomBytes } from "crypto";
|
|
2641
|
+
import * as fs from "fs";
|
|
2642
|
+
import * as path from "path";
|
|
2643
|
+
function worldRef(live, alias) {
|
|
2644
|
+
const { slug, ref } = live.worldRef(alias);
|
|
2645
|
+
const session = live.sessions[alias];
|
|
2646
|
+
return {
|
|
2647
|
+
slug,
|
|
2648
|
+
ref,
|
|
2649
|
+
versionId: session.versionId,
|
|
2650
|
+
sessionId: session.sessionId
|
|
2651
|
+
};
|
|
2652
|
+
}
|
|
2653
|
+
async function fileTaskRun(live, runId, model, report, grade, agentError, traced, runDir, options) {
|
|
2654
|
+
const aliases = live.aliases;
|
|
2655
|
+
const anchor = aliases[0];
|
|
2656
|
+
const worldsMeta = Object.fromEntries(
|
|
2657
|
+
aliases.map((alias) => [alias, worldRef(live, alias)])
|
|
1110
2658
|
);
|
|
2659
|
+
const evals = new GatewayEvalsClient(options);
|
|
2660
|
+
const evaluationId = await evals.createEvaluation(
|
|
2661
|
+
worldRef(live, anchor).versionId,
|
|
2662
|
+
{
|
|
2663
|
+
model,
|
|
2664
|
+
rolloutsPerExample: 1,
|
|
2665
|
+
metadata: {
|
|
2666
|
+
source: "worlds-task-run",
|
|
2667
|
+
task: {
|
|
2668
|
+
id: live.taskId,
|
|
2669
|
+
version: live.taskVersion,
|
|
2670
|
+
name: live.name,
|
|
2671
|
+
hash: live.hash
|
|
2672
|
+
},
|
|
2673
|
+
anchor,
|
|
2674
|
+
worlds: worldsMeta,
|
|
2675
|
+
runId
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
);
|
|
2679
|
+
const reward = grade.reward;
|
|
2680
|
+
const sample = {
|
|
2681
|
+
exampleId: live.name ?? live.taskId ?? runId,
|
|
2682
|
+
task: live.name ?? live.taskId ?? runId,
|
|
2683
|
+
rolloutIndex: 0,
|
|
2684
|
+
prompt: live.instruction,
|
|
2685
|
+
completion: report,
|
|
2686
|
+
rewards: grade.rewards,
|
|
2687
|
+
info: {
|
|
2688
|
+
sessionId: runId,
|
|
2689
|
+
worlds: Object.fromEntries(
|
|
2690
|
+
aliases.map((alias) => [
|
|
2691
|
+
alias,
|
|
2692
|
+
{
|
|
2693
|
+
...worldRef(live, alias),
|
|
2694
|
+
reward: grade.worlds[alias]?.reward ?? null
|
|
2695
|
+
}
|
|
2696
|
+
])
|
|
2697
|
+
),
|
|
2698
|
+
ungraded: grade.ungraded,
|
|
2699
|
+
traced,
|
|
2700
|
+
runDir,
|
|
2701
|
+
...agentError ? { agentError } : {}
|
|
2702
|
+
}
|
|
2703
|
+
};
|
|
2704
|
+
if (typeof reward === "number") {
|
|
2705
|
+
sample.reward = reward;
|
|
2706
|
+
sample.score = reward;
|
|
2707
|
+
sample.correct = reward >= 1;
|
|
2708
|
+
}
|
|
2709
|
+
await evals.pushSamples(evaluationId, [sample]);
|
|
2710
|
+
await evals.finalizeEvaluation(evaluationId, {
|
|
2711
|
+
status: agentError ? "FAILED" : "COMPLETED",
|
|
2712
|
+
metrics: {
|
|
2713
|
+
...typeof reward === "number" ? { avg_reward: reward } : {},
|
|
2714
|
+
reward,
|
|
2715
|
+
...Object.fromEntries(
|
|
2716
|
+
Object.entries(grade.rewards).map(([k, v]) => [`rewards.${k}`, v])
|
|
2717
|
+
)
|
|
2718
|
+
}
|
|
2719
|
+
});
|
|
2720
|
+
return evaluationId;
|
|
1111
2721
|
}
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
"
|
|
2722
|
+
function reportFromResult(result) {
|
|
2723
|
+
if (typeof result === "string") return { report: result, extra: {} };
|
|
2724
|
+
if (result && typeof result === "object") {
|
|
2725
|
+
const { report, ...extra } = result;
|
|
2726
|
+
return {
|
|
2727
|
+
report: report === void 0 || report === null ? null : typeof report === "string" ? report : String(report),
|
|
2728
|
+
extra
|
|
2729
|
+
};
|
|
2730
|
+
}
|
|
2731
|
+
if (result !== void 0 && result !== null)
|
|
2732
|
+
return { report: String(result), extra: {} };
|
|
2733
|
+
return { report: null, extra: {} };
|
|
2734
|
+
}
|
|
2735
|
+
async function runTask(ref, agent, opts) {
|
|
2736
|
+
const runId = `wtr-${randomBytes(6).toString("hex")}`;
|
|
2737
|
+
const outDir = opts.outDir ?? "runs";
|
|
2738
|
+
const runDir = path.join(outDir, runId);
|
|
2739
|
+
fs.mkdirSync(runDir, { recursive: true });
|
|
2740
|
+
const options = opts.options ?? {};
|
|
2741
|
+
const live = await openTask(ref, {
|
|
2742
|
+
surfaces: opts.surfaces,
|
|
2743
|
+
reuseWarm: opts.reuseWarm,
|
|
2744
|
+
options
|
|
2745
|
+
});
|
|
2746
|
+
const started = Date.now();
|
|
2747
|
+
try {
|
|
2748
|
+
await live.ready();
|
|
2749
|
+
let traced = false;
|
|
2750
|
+
if (opts.trace !== false) {
|
|
2751
|
+
try {
|
|
2752
|
+
const { isGatewayConfigured: isGatewayConfigured2, init: init2 } = await Promise.resolve().then(() => (init_init(), init_exports));
|
|
2753
|
+
if (isGatewayConfigured2()) {
|
|
2754
|
+
init2();
|
|
2755
|
+
traced = true;
|
|
2756
|
+
}
|
|
2757
|
+
} catch {
|
|
2758
|
+
traced = false;
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
const taskRun = new TaskRun(
|
|
2762
|
+
runId,
|
|
2763
|
+
live.instruction ?? "",
|
|
2764
|
+
opts.model,
|
|
2765
|
+
live.sessions,
|
|
2766
|
+
await live.manifest()
|
|
2767
|
+
);
|
|
2768
|
+
let agentError = null;
|
|
2769
|
+
let report = null;
|
|
2770
|
+
let extra = {};
|
|
2771
|
+
try {
|
|
2772
|
+
const invoke = () => agent(taskRun);
|
|
2773
|
+
const result = traced ? await (await Promise.resolve().then(() => (init_session2(), session_exports2))).withSession(runId, invoke) : await invoke();
|
|
2774
|
+
({ report, extra } = reportFromResult(result));
|
|
2775
|
+
} catch (error) {
|
|
2776
|
+
const message = (error instanceof Error ? error.message : String(error)) || "Unknown error";
|
|
2777
|
+
agentError = message.slice(0, 2e3);
|
|
2778
|
+
}
|
|
2779
|
+
const grade = report !== null ? await live.grade({ report }) : await live.grade();
|
|
2780
|
+
fs.writeFileSync(
|
|
2781
|
+
path.join(runDir, "manifest.json"),
|
|
2782
|
+
JSON.stringify(taskRun.manifest, null, 2)
|
|
2783
|
+
);
|
|
2784
|
+
fs.writeFileSync(
|
|
2785
|
+
path.join(runDir, "grades.json"),
|
|
2786
|
+
JSON.stringify(grade, null, 2)
|
|
2787
|
+
);
|
|
2788
|
+
if (report) fs.writeFileSync(path.join(runDir, "report.md"), report);
|
|
2789
|
+
const summary = {
|
|
2790
|
+
runId,
|
|
2791
|
+
taskId: live.taskId,
|
|
2792
|
+
taskVersion: live.taskVersion,
|
|
2793
|
+
name: live.name,
|
|
2794
|
+
instruction: taskRun.instruction,
|
|
2795
|
+
model: opts.model,
|
|
2796
|
+
aliases: live.aliases,
|
|
2797
|
+
reward: grade.reward,
|
|
2798
|
+
rewards: grade.rewards,
|
|
2799
|
+
ungraded: grade.ungraded,
|
|
2800
|
+
agentError,
|
|
2801
|
+
report,
|
|
2802
|
+
traced,
|
|
2803
|
+
runDir,
|
|
2804
|
+
durationSeconds: Math.round((Date.now() - started) / 10) / 100,
|
|
2805
|
+
notes: taskRun.notes,
|
|
2806
|
+
filed: false,
|
|
2807
|
+
...extra
|
|
2808
|
+
};
|
|
2809
|
+
if (opts.file !== false) {
|
|
2810
|
+
summary.evaluationId = await fileTaskRun(
|
|
2811
|
+
live,
|
|
2812
|
+
runId,
|
|
2813
|
+
opts.model,
|
|
2814
|
+
report,
|
|
2815
|
+
grade,
|
|
2816
|
+
agentError,
|
|
2817
|
+
traced,
|
|
2818
|
+
runDir,
|
|
2819
|
+
options
|
|
2820
|
+
);
|
|
2821
|
+
summary.filed = true;
|
|
2822
|
+
}
|
|
2823
|
+
fs.writeFileSync(
|
|
2824
|
+
path.join(runDir, "run.json"),
|
|
2825
|
+
JSON.stringify(summary, null, 2)
|
|
2826
|
+
);
|
|
2827
|
+
return summary;
|
|
2828
|
+
} finally {
|
|
2829
|
+
if (!opts.keepUp) {
|
|
2830
|
+
try {
|
|
2831
|
+
await live.close({ keepWarm: false });
|
|
2832
|
+
} catch {
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
var TaskRun;
|
|
2838
|
+
var init_taskRun = __esm({
|
|
2839
|
+
"src/worlds/taskRun.ts"() {
|
|
1115
2840
|
"use strict";
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
2841
|
+
init_browser();
|
|
2842
|
+
init_evals();
|
|
2843
|
+
init_session();
|
|
2844
|
+
init_taskSessions();
|
|
2845
|
+
TaskRun = class {
|
|
2846
|
+
constructor(runId, instruction, model, sessions, manifest) {
|
|
2847
|
+
this.runId = runId;
|
|
2848
|
+
this.instruction = instruction;
|
|
2849
|
+
this.model = model;
|
|
2850
|
+
this.sessions = sessions;
|
|
2851
|
+
this.manifest = manifest;
|
|
2852
|
+
}
|
|
2853
|
+
notes = {};
|
|
2854
|
+
/** The alias's session, or a clear error naming the aliases there are. */
|
|
2855
|
+
session(alias) {
|
|
2856
|
+
const session = this.sessions[alias];
|
|
2857
|
+
if (!session)
|
|
2858
|
+
throw new Error(
|
|
2859
|
+
`task run has no world '${alias}'; its worlds are ${Object.keys(this.sessions).join(", ")}`
|
|
2860
|
+
);
|
|
2861
|
+
return session;
|
|
2862
|
+
}
|
|
2863
|
+
/** `alias`'s HTTP API. Raises the same error the session itself would
|
|
2864
|
+
* when it was not opened with `surfaces: ["api"]`. */
|
|
2865
|
+
async api(alias) {
|
|
2866
|
+
const surfaces = await this.session(alias).surfaces();
|
|
2867
|
+
if (!surfaces.api)
|
|
2868
|
+
throw new WorldSurfaceUnavailable(
|
|
2869
|
+
"api",
|
|
2870
|
+
`open '${alias}' with surfaces: ['api']`
|
|
2871
|
+
);
|
|
2872
|
+
return { url: surfaces.api.url, token: surfaces.api.token };
|
|
2873
|
+
}
|
|
2874
|
+
/** Every world's tools merged into one toolkit, each name prefixed
|
|
2875
|
+
* `<alias>.<tool>` so a model can tell which world a call goes to. A
|
|
2876
|
+
* world that exposes no tools contributes nothing — there is nothing to
|
|
2877
|
+
* merge. */
|
|
2878
|
+
async toolkit() {
|
|
2879
|
+
const schemas = [];
|
|
2880
|
+
const impls = {};
|
|
2881
|
+
const specs = [];
|
|
2882
|
+
for (const [alias, session] of Object.entries(this.sessions)) {
|
|
2883
|
+
const kit = await session.toolkit();
|
|
2884
|
+
if (!kit.specs.length) continue;
|
|
2885
|
+
for (const spec of kit.specs) {
|
|
2886
|
+
const name = `${alias}.${spec.name}`;
|
|
2887
|
+
const prefixed = {
|
|
2888
|
+
name,
|
|
2889
|
+
signature: spec.signature,
|
|
2890
|
+
doc: spec.doc,
|
|
2891
|
+
inputSchema: spec.inputSchema
|
|
2892
|
+
};
|
|
2893
|
+
specs.push(prefixed);
|
|
2894
|
+
const schema = schemaFromSignature(prefixed);
|
|
2895
|
+
schema.function.name = name;
|
|
2896
|
+
schemas.push(schema);
|
|
2897
|
+
impls[name] = kit.impls[modelToolName(spec.name)];
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
return { schemas, impls, specs };
|
|
2901
|
+
}
|
|
2902
|
+
/** Log arbitrary notes into this run's `run.json` (merged; a later call
|
|
2903
|
+
* overrides a key an earlier one set). */
|
|
2904
|
+
record(notes) {
|
|
2905
|
+
Object.assign(this.notes, notes);
|
|
2906
|
+
}
|
|
2907
|
+
};
|
|
1120
2908
|
}
|
|
1121
2909
|
});
|
|
1122
2910
|
|
|
@@ -1133,14 +2921,15 @@ __export(hub_exports, {
|
|
|
1133
2921
|
propose: () => propose,
|
|
1134
2922
|
pull: () => pull,
|
|
1135
2923
|
push: () => push,
|
|
2924
|
+
pushSlug: () => pushSlug,
|
|
1136
2925
|
readLocalMeta: () => readLocalMeta,
|
|
1137
2926
|
snapshotTreeHashes: () => snapshotTreeHashes,
|
|
1138
2927
|
writeLocalMeta: () => writeLocalMeta
|
|
1139
2928
|
});
|
|
1140
|
-
import { createHash } from "crypto";
|
|
2929
|
+
import { createHash as createHash2 } from "crypto";
|
|
1141
2930
|
import { gzipSync, gunzipSync } from "zlib";
|
|
1142
|
-
import * as
|
|
1143
|
-
import * as
|
|
2931
|
+
import * as fs2 from "fs";
|
|
2932
|
+
import * as path2 from "path";
|
|
1144
2933
|
function isExcludedDirComponent(component) {
|
|
1145
2934
|
return component.startsWith(".") || ROOT_EXCLUDED_DIRS.has(component) || component.endsWith(".egg-info");
|
|
1146
2935
|
}
|
|
@@ -1156,18 +2945,18 @@ function isIncludedInContentHash(relPath, includeAllRootFiles) {
|
|
|
1156
2945
|
return !dirParts.some(isExcludedDirComponent);
|
|
1157
2946
|
}
|
|
1158
2947
|
function collectArchiveRelPaths(dir) {
|
|
1159
|
-
if (
|
|
2948
|
+
if (fs2.existsSync(path2.join(dir, ".gitignore"))) {
|
|
1160
2949
|
throw new Error(
|
|
1161
2950
|
"This bundle has a root .gitignore, which the Python CLI honors via gitignore_parser. Reimplementing that imperfectly here would silently produce a different content hash, so the TS CLI refuses. Remove the .gitignore (or bake its effect into the tree) and retry."
|
|
1162
2951
|
);
|
|
1163
2952
|
}
|
|
1164
2953
|
const out = [];
|
|
1165
2954
|
const includeAllRootFiles = BROAD_ROOT_MARKERS.some(
|
|
1166
|
-
(marker) =>
|
|
2955
|
+
(marker) => fs2.existsSync(path2.join(dir, marker))
|
|
1167
2956
|
);
|
|
1168
2957
|
const walk = (rel) => {
|
|
1169
|
-
const abs = rel ?
|
|
1170
|
-
for (const entry of
|
|
2958
|
+
const abs = rel ? path2.join(dir, rel) : dir;
|
|
2959
|
+
for (const entry of fs2.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
1171
2960
|
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
1172
2961
|
if (entry.isDirectory()) {
|
|
1173
2962
|
if (!isExcludedDirComponent(entry.name)) walk(childRel);
|
|
@@ -1182,10 +2971,10 @@ function collectArchiveRelPaths(dir) {
|
|
|
1182
2971
|
return out.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
1183
2972
|
}
|
|
1184
2973
|
function computeContentHash(dir) {
|
|
1185
|
-
const hasher =
|
|
2974
|
+
const hasher = createHash2("sha256");
|
|
1186
2975
|
for (const relPath of collectArchiveRelPaths(dir)) {
|
|
1187
2976
|
hasher.update(Buffer.from(`file:${relPath}`, "utf-8"));
|
|
1188
|
-
hasher.update(
|
|
2977
|
+
hasher.update(fs2.readFileSync(path2.join(dir, relPath)));
|
|
1189
2978
|
}
|
|
1190
2979
|
return hasher.digest("hex");
|
|
1191
2980
|
}
|
|
@@ -1235,28 +3024,34 @@ function tarMember(name, payload) {
|
|
|
1235
3024
|
function buildSourceTarball(dir) {
|
|
1236
3025
|
const parts = [];
|
|
1237
3026
|
for (const relPath of collectArchiveRelPaths(dir)) {
|
|
1238
|
-
parts.push(...tarMember(relPath,
|
|
3027
|
+
parts.push(...tarMember(relPath, fs2.readFileSync(path2.join(dir, relPath))));
|
|
1239
3028
|
}
|
|
1240
3029
|
parts.push(Buffer.alloc(1024));
|
|
1241
3030
|
return gzipSync(Buffer.concat(parts), { level: 9 });
|
|
1242
3031
|
}
|
|
1243
3032
|
function readLocalMeta(dir) {
|
|
1244
3033
|
try {
|
|
1245
|
-
const raw =
|
|
3034
|
+
const raw = fs2.readFileSync(path2.join(dir, META_RELPATH), "utf-8");
|
|
1246
3035
|
const parsed = JSON.parse(raw);
|
|
1247
3036
|
return parsed && typeof parsed === "object" ? parsed : null;
|
|
1248
3037
|
} catch {
|
|
1249
3038
|
return null;
|
|
1250
3039
|
}
|
|
1251
3040
|
}
|
|
3041
|
+
function pushSlug(dir, explicit) {
|
|
3042
|
+
if (explicit) return explicit;
|
|
3043
|
+
const meta = readLocalMeta(dir);
|
|
3044
|
+
if (typeof meta?.slug === "string" && meta.slug) return meta.slug;
|
|
3045
|
+
return readPyproject(dir).name ?? path2.basename(path2.resolve(dir));
|
|
3046
|
+
}
|
|
1252
3047
|
function writeLocalMeta(dir, meta) {
|
|
1253
|
-
|
|
1254
|
-
|
|
3048
|
+
fs2.mkdirSync(path2.join(dir, ".gateway"), { recursive: true });
|
|
3049
|
+
fs2.writeFileSync(path2.join(dir, META_RELPATH), JSON.stringify(meta, null, 2));
|
|
1255
3050
|
}
|
|
1256
3051
|
function snapshotTreeHashes(dir) {
|
|
1257
3052
|
const out = {};
|
|
1258
3053
|
for (const relPath of collectArchiveRelPaths(dir)) {
|
|
1259
|
-
out[relPath] =
|
|
3054
|
+
out[relPath] = createHash2("sha256").update(fs2.readFileSync(path2.join(dir, relPath))).digest("hex");
|
|
1260
3055
|
}
|
|
1261
3056
|
return out;
|
|
1262
3057
|
}
|
|
@@ -1274,7 +3069,7 @@ function localTreeState(dir) {
|
|
|
1274
3069
|
}
|
|
1275
3070
|
function readPyproject(dir) {
|
|
1276
3071
|
try {
|
|
1277
|
-
const text2 =
|
|
3072
|
+
const text2 = fs2.readFileSync(path2.join(dir, "pyproject.toml"), "utf-8");
|
|
1278
3073
|
const section = /\[project\]([\s\S]*?)(\n\[|$)/.exec(text2)?.[1] ?? "";
|
|
1279
3074
|
const name = /\bname\s*=\s*"([^"]+)"/.exec(section)?.[1];
|
|
1280
3075
|
const version = /\bversion\s*=\s*"([^"]+)"/.exec(section)?.[1];
|
|
@@ -1284,9 +3079,9 @@ function readPyproject(dir) {
|
|
|
1284
3079
|
}
|
|
1285
3080
|
}
|
|
1286
3081
|
function readWorldContractVersion(dir) {
|
|
1287
|
-
for (const rel of ["schema/world.json",
|
|
3082
|
+
for (const rel of ["schema/world.json", path2.join("server", "world", "schema", "world.json")]) {
|
|
1288
3083
|
try {
|
|
1289
|
-
const parsed = JSON.parse(
|
|
3084
|
+
const parsed = JSON.parse(fs2.readFileSync(path2.join(dir, rel), "utf-8"));
|
|
1290
3085
|
const pkg = parsed["x-gateway"]?.package;
|
|
1291
3086
|
if (pkg?.version) return { name: pkg.id, version: pkg.version };
|
|
1292
3087
|
} catch {
|
|
@@ -1297,20 +3092,20 @@ function readWorldContractVersion(dir) {
|
|
|
1297
3092
|
function buildFileManifest(dir) {
|
|
1298
3093
|
const entries = [];
|
|
1299
3094
|
const walk = (rel) => {
|
|
1300
|
-
const abs = rel ?
|
|
1301
|
-
for (const entry of
|
|
3095
|
+
const abs = rel ? path2.join(dir, rel) : dir;
|
|
3096
|
+
for (const entry of fs2.readdirSync(abs, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
1302
3097
|
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
1303
3098
|
if (entry.isDirectory()) {
|
|
1304
3099
|
if (!SKIP_DIRS.has(entry.name) && !entry.name.endsWith(".egg-info")) {
|
|
1305
3100
|
walk(childRel);
|
|
1306
3101
|
}
|
|
1307
3102
|
} else if (entry.isFile()) {
|
|
1308
|
-
const payload =
|
|
3103
|
+
const payload = fs2.readFileSync(path2.join(dir, childRel));
|
|
1309
3104
|
entries.push({
|
|
1310
3105
|
path: childRel,
|
|
1311
3106
|
isDirectory: false,
|
|
1312
3107
|
size: payload.length,
|
|
1313
|
-
sha256:
|
|
3108
|
+
sha256: createHash2("sha256").update(payload).digest("hex")
|
|
1314
3109
|
});
|
|
1315
3110
|
}
|
|
1316
3111
|
}
|
|
@@ -1333,7 +3128,7 @@ async function putObject(url, data, shaB64) {
|
|
|
1333
3128
|
}
|
|
1334
3129
|
async function push(dir, opts = {}) {
|
|
1335
3130
|
const options = opts.options ?? {};
|
|
1336
|
-
if (!
|
|
3131
|
+
if (!fs2.statSync(dir).isDirectory()) throw new Error(`Not a directory: ${dir}`);
|
|
1337
3132
|
const localMeta = readLocalMeta(dir) ?? {};
|
|
1338
3133
|
const branch = opts.branch ?? localMeta.branch ?? DEFAULT_BRANCH;
|
|
1339
3134
|
const baseVersionId = opts.baseVersionId ?? localMeta.versionId ?? null;
|
|
@@ -1341,7 +3136,7 @@ async function push(dir, opts = {}) {
|
|
|
1341
3136
|
const pyproject = readPyproject(dir);
|
|
1342
3137
|
const contractVersion = pyproject.version ? void 0 : readWorldContractVersion(dir).version;
|
|
1343
3138
|
if (contractVersion) pyproject.version = contractVersion;
|
|
1344
|
-
const slug = opts.slug ?? localMeta.slug ?? pyproject.name ??
|
|
3139
|
+
const slug = opts.slug ?? localMeta.slug ?? pyproject.name ?? path2.basename(path2.resolve(dir));
|
|
1345
3140
|
const resolve6 = await apiRequest(
|
|
1346
3141
|
options,
|
|
1347
3142
|
"POST",
|
|
@@ -1421,9 +3216,9 @@ function extractBundle(bundle, dir) {
|
|
|
1421
3216
|
longName = null;
|
|
1422
3217
|
if (typeflag !== "0" && typeflag !== "") continue;
|
|
1423
3218
|
if (name.startsWith("/") || name.split("/").includes("..")) continue;
|
|
1424
|
-
const target =
|
|
1425
|
-
|
|
1426
|
-
|
|
3219
|
+
const target = path2.join(dir, name);
|
|
3220
|
+
fs2.mkdirSync(path2.dirname(target), { recursive: true });
|
|
3221
|
+
fs2.writeFileSync(target, body);
|
|
1427
3222
|
written++;
|
|
1428
3223
|
}
|
|
1429
3224
|
return written;
|
|
@@ -1439,13 +3234,13 @@ async function pull(slugRef, target, options = {}) {
|
|
|
1439
3234
|
if (!details.downloadUrl) {
|
|
1440
3235
|
throw new Error(`${slug}@${ref} resolved but has no uploaded bundle`);
|
|
1441
3236
|
}
|
|
1442
|
-
let dir = target ??
|
|
3237
|
+
let dir = target ?? path2.join(process.cwd(), slug);
|
|
1443
3238
|
if (!target) {
|
|
1444
3239
|
let i = 1;
|
|
1445
3240
|
const base = dir;
|
|
1446
|
-
while (
|
|
3241
|
+
while (fs2.existsSync(dir)) dir = `${base}-${i++}`;
|
|
1447
3242
|
}
|
|
1448
|
-
|
|
3243
|
+
fs2.mkdirSync(dir, { recursive: true });
|
|
1449
3244
|
const resp = await fetch(details.downloadUrl);
|
|
1450
3245
|
if (!resp.ok) throw new Error(`bundle download -> ${resp.status}`);
|
|
1451
3246
|
const bundle = Buffer.from(await resp.arrayBuffer());
|
|
@@ -1472,15 +3267,15 @@ async function propose(dir, opts) {
|
|
|
1472
3267
|
if (!meta?.slug || !meta.baseFiles) {
|
|
1473
3268
|
throw new Error("This dir has no pull/push base \u2014 run pull first, then edit, then propose.");
|
|
1474
3269
|
}
|
|
1475
|
-
const
|
|
1476
|
-
if (
|
|
1477
|
-
const changes =
|
|
1478
|
-
const abs =
|
|
1479
|
-
if (!
|
|
3270
|
+
const state2 = localTreeState(dir);
|
|
3271
|
+
if (state2.clean) throw new Error("Nothing to propose \u2014 the tree matches its base.");
|
|
3272
|
+
const changes = state2.dirty.map((relPath) => {
|
|
3273
|
+
const abs = path2.join(dir, relPath);
|
|
3274
|
+
if (!fs2.existsSync(abs)) return { path: relPath, action: "delete" };
|
|
1480
3275
|
return {
|
|
1481
3276
|
path: relPath,
|
|
1482
3277
|
action: "write",
|
|
1483
|
-
newContent:
|
|
3278
|
+
newContent: fs2.readFileSync(abs, "utf-8")
|
|
1484
3279
|
};
|
|
1485
3280
|
});
|
|
1486
3281
|
const res = await apiRequest(
|
|
@@ -1512,7 +3307,7 @@ var init_hub = __esm({
|
|
|
1512
3307
|
"use strict";
|
|
1513
3308
|
init_worlds();
|
|
1514
3309
|
DEFAULT_BRANCH = "main";
|
|
1515
|
-
META_RELPATH =
|
|
3310
|
+
META_RELPATH = path2.join(".gateway", ".env-metadata.json");
|
|
1516
3311
|
SKIP_DIRS = /* @__PURE__ */ new Set([
|
|
1517
3312
|
".git",
|
|
1518
3313
|
"__pycache__",
|
|
@@ -1525,7 +3320,7 @@ var init_hub = __esm({
|
|
|
1525
3320
|
]);
|
|
1526
3321
|
ROOT_EXCLUDED_DIRS = /* @__PURE__ */ new Set(["dist", "build", "outputs", "__pycache__"]);
|
|
1527
3322
|
BROAD_ROOT_MARKERS = ["gateway-env.toml", "gateway-api-world.toml"];
|
|
1528
|
-
sha256b64 = (data) =>
|
|
3323
|
+
sha256b64 = (data) => createHash2("sha256").update(data).digest("base64");
|
|
1529
3324
|
}
|
|
1530
3325
|
});
|
|
1531
3326
|
|
|
@@ -1598,23 +3393,22 @@ async function validateWorld(dir, opts) {
|
|
|
1598
3393
|
throw new Error(
|
|
1599
3394
|
"No branch to validate on: pass --branch or run inside a git checkout / GitHub Actions."
|
|
1600
3395
|
);
|
|
1601
|
-
|
|
3396
|
+
const slug = pushSlug(dir, opts.slug);
|
|
3397
|
+
progress(`pushing ${dir} as ${slug}@${branch}\u2026`);
|
|
1602
3398
|
let tipVersionId = null;
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
tipVersionId = null;
|
|
1614
|
-
}
|
|
3399
|
+
try {
|
|
3400
|
+
const tip = await apiRequest(
|
|
3401
|
+
options,
|
|
3402
|
+
"POST",
|
|
3403
|
+
"/api/public/benchmark-containers/resolve-version",
|
|
3404
|
+
{ slug, ref: branch }
|
|
3405
|
+
);
|
|
3406
|
+
tipVersionId = tip.versionId ?? null;
|
|
3407
|
+
} catch {
|
|
3408
|
+
tipVersionId = null;
|
|
1615
3409
|
}
|
|
1616
3410
|
const pushed = await push(dir, {
|
|
1617
|
-
slug
|
|
3411
|
+
slug,
|
|
1618
3412
|
branch,
|
|
1619
3413
|
baseVersionId: tipVersionId,
|
|
1620
3414
|
message: `PR validation push (${provenance.commit?.slice(0, 12) ?? "local"})`,
|
|
@@ -2025,7 +3819,7 @@ var init_json = __esm({
|
|
|
2025
3819
|
});
|
|
2026
3820
|
|
|
2027
3821
|
// src/worlds/transforms.ts
|
|
2028
|
-
import { createHash as
|
|
3822
|
+
import { createHash as createHash3 } from "crypto";
|
|
2029
3823
|
function parseTransform(name) {
|
|
2030
3824
|
const simple = Object.prototype.hasOwnProperty.call(SIMPLE, name) ? SIMPLE[name] : void 0;
|
|
2031
3825
|
if (simple) return simple;
|
|
@@ -2063,7 +3857,7 @@ var init_transforms = __esm({
|
|
|
2063
3857
|
return at === -1 ? "" : value.slice(at + 1).toLowerCase();
|
|
2064
3858
|
};
|
|
2065
3859
|
stripPrefix = (prefix) => (value) => value.startsWith(prefix) ? value.slice(prefix.length) : value;
|
|
2066
|
-
hex = (algorithm, value) =>
|
|
3860
|
+
hex = (algorithm, value) => createHash3(algorithm).update(value, "utf8").digest("hex");
|
|
2067
3861
|
SIMPLE = {
|
|
2068
3862
|
lower: (value) => value.toLowerCase(),
|
|
2069
3863
|
upper: (value) => value.toUpperCase(),
|
|
@@ -2087,7 +3881,7 @@ var init_transforms = __esm({
|
|
|
2087
3881
|
});
|
|
2088
3882
|
|
|
2089
3883
|
// src/worlds/redaction.ts
|
|
2090
|
-
import { createHash as
|
|
3884
|
+
import { createHash as createHash4 } from "crypto";
|
|
2091
3885
|
function pyText(value) {
|
|
2092
3886
|
if (typeof value === "string") return value;
|
|
2093
3887
|
if (Number.isInteger(value)) return String(value);
|
|
@@ -2096,7 +3890,7 @@ function pyText(value) {
|
|
|
2096
3890
|
return pyFloatRepr(value);
|
|
2097
3891
|
}
|
|
2098
3892
|
function hashForm(slug, plaintext) {
|
|
2099
|
-
return "sha256:" +
|
|
3893
|
+
return "sha256:" + createHash4("sha256").update(`${slug}:${pyText(plaintext)}`, "utf8").digest("hex");
|
|
2100
3894
|
}
|
|
2101
3895
|
function preserveForm(slug, plaintext) {
|
|
2102
3896
|
const kind = typeof plaintext === "string" ? "str" : isInt(plaintext) ? "int" : "float";
|
|
@@ -2105,12 +3899,12 @@ function preserveForm(slug, plaintext) {
|
|
|
2105
3899
|
}
|
|
2106
3900
|
function shapeText(slug, text2, kind) {
|
|
2107
3901
|
const seed = Buffer.from(`${slug}:${text2}`, "utf8");
|
|
2108
|
-
let digest =
|
|
3902
|
+
let digest = createHash4("sha256").update(seed).digest();
|
|
2109
3903
|
let cursor = 0;
|
|
2110
3904
|
const out = [];
|
|
2111
3905
|
for (const char of text2) {
|
|
2112
3906
|
if (cursor === digest.length) {
|
|
2113
|
-
digest =
|
|
3907
|
+
digest = createHash4("sha256").update(Buffer.concat([digest, seed])).digest();
|
|
2114
3908
|
cursor = 0;
|
|
2115
3909
|
}
|
|
2116
3910
|
const byte = digest[cursor];
|
|
@@ -2235,12 +4029,12 @@ var init_router = __esm({
|
|
|
2235
4029
|
});
|
|
2236
4030
|
|
|
2237
4031
|
// src/cli/config.ts
|
|
2238
|
-
import * as
|
|
4032
|
+
import * as fs3 from "fs";
|
|
2239
4033
|
import * as os from "os";
|
|
2240
|
-
import * as
|
|
2241
|
-
async function api(creds, method, route, body,
|
|
4034
|
+
import * as path3 from "path";
|
|
4035
|
+
async function api(creds, method, route, body, init2 = {}) {
|
|
2242
4036
|
const controller = new AbortController();
|
|
2243
|
-
const timer = setTimeout(() => controller.abort(),
|
|
4037
|
+
const timer = setTimeout(() => controller.abort(), init2.timeoutMs ?? 12e4);
|
|
2244
4038
|
try {
|
|
2245
4039
|
const res = await fetch(`${creds.host}${route}`, {
|
|
2246
4040
|
method,
|
|
@@ -2278,9 +4072,9 @@ var init_config = __esm({
|
|
|
2278
4072
|
|
|
2279
4073
|
// src/cli/runtime/local.ts
|
|
2280
4074
|
import { spawn, spawnSync } from "child_process";
|
|
2281
|
-
import * as
|
|
4075
|
+
import * as fs4 from "fs";
|
|
2282
4076
|
import * as net from "net";
|
|
2283
|
-
import * as
|
|
4077
|
+
import * as path4 from "path";
|
|
2284
4078
|
function tailOf(output) {
|
|
2285
4079
|
return output.length > DIAGNOSTIC_TAIL ? `\u2026${output.slice(-DIAGNOSTIC_TAIL)}` : output;
|
|
2286
4080
|
}
|
|
@@ -2295,16 +4089,16 @@ function runtimeRoot() {
|
|
|
2295
4089
|
const candidates = [];
|
|
2296
4090
|
if (process.env.GATEWAY_RUNTIME_PATH)
|
|
2297
4091
|
candidates.push(process.env.GATEWAY_RUNTIME_PATH);
|
|
2298
|
-
const entry = process.argv[1] &&
|
|
4092
|
+
const entry = process.argv[1] && fs4.existsSync(process.argv[1]) ? path4.dirname(fs4.realpathSync(process.argv[1])) : null;
|
|
2299
4093
|
if (entry) {
|
|
2300
|
-
candidates.push(
|
|
2301
|
-
candidates.push(
|
|
2302
|
-
candidates.push(
|
|
4094
|
+
candidates.push(path4.resolve(entry, "..", "runtime"));
|
|
4095
|
+
candidates.push(path4.resolve(entry, "..", "..", "runtime"));
|
|
4096
|
+
candidates.push(path4.resolve(entry, "..", "..", "sdk", "src"));
|
|
2303
4097
|
}
|
|
2304
|
-
candidates.push(
|
|
4098
|
+
candidates.push(path4.resolve(process.cwd(), "runtime"));
|
|
2305
4099
|
for (const root of candidates)
|
|
2306
|
-
if (
|
|
2307
|
-
|
|
4100
|
+
if (fs4.existsSync(
|
|
4101
|
+
path4.join(root, "gatewaysdk", "environment", "schema", "platform.py")
|
|
2308
4102
|
))
|
|
2309
4103
|
return root;
|
|
2310
4104
|
return null;
|
|
@@ -2472,18 +4266,18 @@ runpy.run_module('gatewaysdk.environment.schema.platform',run_name='__main__')`;
|
|
|
2472
4266
|
});
|
|
2473
4267
|
|
|
2474
4268
|
// src/cli/runtime/client.ts
|
|
2475
|
-
import * as
|
|
2476
|
-
import * as
|
|
4269
|
+
import * as fs5 from "fs";
|
|
4270
|
+
import * as path5 from "path";
|
|
2477
4271
|
function isBuildOutput(relPath) {
|
|
2478
4272
|
return BUILD_OUTPUT.test(relPath);
|
|
2479
4273
|
}
|
|
2480
4274
|
function readWorldFiles(dir, opts = {}) {
|
|
2481
|
-
if (!
|
|
4275
|
+
if (!fs5.existsSync(dir) || !fs5.statSync(dir).isDirectory())
|
|
2482
4276
|
throw new CliError(`Not a directory: ${dir}`);
|
|
2483
4277
|
const out = [];
|
|
2484
4278
|
const walk = (rel) => {
|
|
2485
|
-
const abs = rel ?
|
|
2486
|
-
const entries =
|
|
4279
|
+
const abs = rel ? path5.join(dir, rel) : dir;
|
|
4280
|
+
const entries = fs5.readdirSync(abs, { withFileTypes: true });
|
|
2487
4281
|
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
2488
4282
|
for (const entry of entries) {
|
|
2489
4283
|
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
@@ -2493,7 +4287,7 @@ function readWorldFiles(dir, opts = {}) {
|
|
|
2493
4287
|
if (!opts.buildOutputs && isBuildOutput(childRel)) continue;
|
|
2494
4288
|
out.push({
|
|
2495
4289
|
path: childRel,
|
|
2496
|
-
content:
|
|
4290
|
+
content: fs5.readFileSync(path5.join(dir, childRel)).toString("base64")
|
|
2497
4291
|
});
|
|
2498
4292
|
}
|
|
2499
4293
|
}
|
|
@@ -2508,19 +4302,19 @@ function assertSafeRelPath(rel) {
|
|
|
2508
4302
|
}
|
|
2509
4303
|
function writeWorldFiles(dir, files, opts = {}) {
|
|
2510
4304
|
for (const file of files) assertSafeRelPath(file.path);
|
|
2511
|
-
if (opts.clean &&
|
|
4305
|
+
if (opts.clean && fs5.existsSync(dir)) {
|
|
2512
4306
|
const keep = new Set(files.map((f) => f.path));
|
|
2513
4307
|
for (const existing of readWorldFiles(dir)) {
|
|
2514
|
-
if (!keep.has(existing.path))
|
|
4308
|
+
if (!keep.has(existing.path)) fs5.rmSync(path5.join(dir, existing.path));
|
|
2515
4309
|
}
|
|
2516
4310
|
pruneEmptyDirs(dir);
|
|
2517
4311
|
}
|
|
2518
|
-
|
|
4312
|
+
fs5.mkdirSync(dir, { recursive: true });
|
|
2519
4313
|
const written = [];
|
|
2520
4314
|
for (const file of files) {
|
|
2521
|
-
const target =
|
|
2522
|
-
|
|
2523
|
-
|
|
4315
|
+
const target = path5.join(dir, file.path);
|
|
4316
|
+
fs5.mkdirSync(path5.dirname(target), { recursive: true });
|
|
4317
|
+
fs5.writeFileSync(target, Buffer.from(file.content, "base64"));
|
|
2524
4318
|
written.push(file.path);
|
|
2525
4319
|
}
|
|
2526
4320
|
return written;
|
|
@@ -2528,14 +4322,14 @@ function writeWorldFiles(dir, files, opts = {}) {
|
|
|
2528
4322
|
function pruneEmptyDirs(root) {
|
|
2529
4323
|
const walk = (abs) => {
|
|
2530
4324
|
let empty = true;
|
|
2531
|
-
for (const entry of
|
|
2532
|
-
const child =
|
|
4325
|
+
for (const entry of fs5.readdirSync(abs, { withFileTypes: true })) {
|
|
4326
|
+
const child = path5.join(abs, entry.name);
|
|
2533
4327
|
if (entry.isDirectory()) {
|
|
2534
4328
|
if (SKIP_DIRS2.has(entry.name)) {
|
|
2535
4329
|
empty = false;
|
|
2536
4330
|
continue;
|
|
2537
4331
|
}
|
|
2538
|
-
if (walk(child))
|
|
4332
|
+
if (walk(child)) fs5.rmdirSync(child);
|
|
2539
4333
|
else empty = false;
|
|
2540
4334
|
} else empty = false;
|
|
2541
4335
|
}
|
|
@@ -2627,8 +4421,8 @@ __export(data_exports, {
|
|
|
2627
4421
|
queryLocalWorldDataAcross: () => queryLocalWorldDataAcross,
|
|
2628
4422
|
showLocalWorldData: () => showLocalWorldData
|
|
2629
4423
|
});
|
|
2630
|
-
import * as
|
|
2631
|
-
import * as
|
|
4424
|
+
import * as fs6 from "fs";
|
|
4425
|
+
import * as path6 from "path";
|
|
2632
4426
|
async function importLocalWorldData(dir, rows, opts, creds) {
|
|
2633
4427
|
const res = await runtime(creds, {
|
|
2634
4428
|
command: "import-data",
|
|
@@ -2654,7 +4448,7 @@ function dropBuildOutputs(dir) {
|
|
|
2654
4448
|
const dropped = [];
|
|
2655
4449
|
for (const file of readWorldFiles(dir, { buildOutputs: true })) {
|
|
2656
4450
|
if (!BUILD_OUTPUT2.test(file.path)) continue;
|
|
2657
|
-
|
|
4451
|
+
fs6.rmSync(path6.join(dir, file.path), { force: true });
|
|
2658
4452
|
dropped.push(file.path);
|
|
2659
4453
|
}
|
|
2660
4454
|
return dropped;
|
|
@@ -2720,7 +4514,7 @@ async function queryLocalWorldDataAcross(dir, entity, where, opts) {
|
|
|
2720
4514
|
const payload = await localBridge(
|
|
2721
4515
|
{
|
|
2722
4516
|
command: "query",
|
|
2723
|
-
world:
|
|
4517
|
+
world: path6.resolve(dir),
|
|
2724
4518
|
entity,
|
|
2725
4519
|
across: true,
|
|
2726
4520
|
...opts.compare ? { compare: true } : {},
|
|
@@ -2791,9 +4585,9 @@ __export(transform_exports, {
|
|
|
2791
4585
|
transformRecords: () => transformRecords
|
|
2792
4586
|
});
|
|
2793
4587
|
import { spawn as spawn2 } from "child_process";
|
|
2794
|
-
import * as
|
|
4588
|
+
import * as fs7 from "fs";
|
|
2795
4589
|
import * as os2 from "os";
|
|
2796
|
-
import * as
|
|
4590
|
+
import * as path7 from "path";
|
|
2797
4591
|
import { createRequire } from "module";
|
|
2798
4592
|
function checkRows(value, where) {
|
|
2799
4593
|
if (!isObject(value)) throw new CliError(`${where} must return {entity: [rows]}; got ${value === null ? "null" : Array.isArray(value) ? "an array" : typeof value}`, 1);
|
|
@@ -2818,27 +4612,27 @@ function typescriptPrefix() {
|
|
|
2818
4612
|
}
|
|
2819
4613
|
}
|
|
2820
4614
|
function command(script, wrapperDir) {
|
|
2821
|
-
const suffix =
|
|
4615
|
+
const suffix = path7.extname(script).toLowerCase();
|
|
2822
4616
|
if (PYTHON_SUFFIXES.includes(suffix)) return { file: process.env.GATEWAY_PYTHON || "python3", args: ["-c", PY_WRAPPER, script] };
|
|
2823
4617
|
if (NODE_SUFFIXES.includes(suffix)) {
|
|
2824
|
-
const wrapper =
|
|
2825
|
-
|
|
4618
|
+
const wrapper = path7.join(wrapperDir, "transform-wrapper.mjs");
|
|
4619
|
+
fs7.writeFileSync(wrapper, NODE_WRAPPER);
|
|
2826
4620
|
const flags = suffix === ".ts" || suffix === ".mts" ? typescriptPrefix() : [];
|
|
2827
4621
|
return { file: process.env.GATEWAY_NODE || process.execPath, args: [...flags, wrapper, script] };
|
|
2828
4622
|
}
|
|
2829
|
-
throw new CliError(`${
|
|
4623
|
+
throw new CliError(`${path7.basename(script)}: a transform script is .py, .ts, .mjs, .js or .cjs`, 1);
|
|
2830
4624
|
}
|
|
2831
4625
|
async function runTransform(script, records, opts = {}) {
|
|
2832
|
-
if (!
|
|
2833
|
-
const resolved =
|
|
2834
|
-
const name =
|
|
4626
|
+
if (!fs7.existsSync(script) || !fs7.statSync(script).isFile()) throw new CliError(`no transform script at ${script}`, 1);
|
|
4627
|
+
const resolved = path7.resolve(script);
|
|
4628
|
+
const name = path7.basename(script);
|
|
2835
4629
|
const timeoutMs = opts.timeoutMs ?? DEFAULT_TRANSFORM_TIMEOUT_MS;
|
|
2836
|
-
const scratch =
|
|
4630
|
+
const scratch = fs7.mkdtempSync(path7.join(os2.tmpdir(), "gateway-transform-"));
|
|
2837
4631
|
try {
|
|
2838
4632
|
const { file, args } = command(resolved, scratch);
|
|
2839
4633
|
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !SCRUBBED.test(key)));
|
|
2840
4634
|
const done = await new Promise((resolve6, reject) => {
|
|
2841
|
-
const child = spawn2(file, args, { cwd:
|
|
4635
|
+
const child = spawn2(file, args, { cwd: path7.dirname(resolved), env, stdio: ["pipe", "pipe", "pipe"] });
|
|
2842
4636
|
let stdout = "";
|
|
2843
4637
|
let stderr = "";
|
|
2844
4638
|
let timedOut = false;
|
|
@@ -2869,7 +4663,7 @@ async function runTransform(script, records, opts = {}) {
|
|
|
2869
4663
|
}
|
|
2870
4664
|
return { rows: checkRows(value, name), stderr: done.stderr };
|
|
2871
4665
|
} finally {
|
|
2872
|
-
|
|
4666
|
+
fs7.rmSync(scratch, { recursive: true, force: true });
|
|
2873
4667
|
}
|
|
2874
4668
|
}
|
|
2875
4669
|
async function transformRecords(transform, records, opts = {}) {
|
|
@@ -2961,13 +4755,13 @@ if (fn) {
|
|
|
2961
4755
|
});
|
|
2962
4756
|
|
|
2963
4757
|
// src/cli/platform/worldData/batches.ts
|
|
2964
|
-
import { createHash as
|
|
2965
|
-
import * as
|
|
4758
|
+
import { createHash as createHash5 } from "crypto";
|
|
4759
|
+
import * as fs8 from "fs";
|
|
2966
4760
|
function uploadHeadersFor(gz) {
|
|
2967
4761
|
return {
|
|
2968
4762
|
"content-type": "application/gzip",
|
|
2969
4763
|
"content-length": String(gz.length),
|
|
2970
|
-
"x-amz-checksum-sha256":
|
|
4764
|
+
"x-amz-checksum-sha256": createHash5("sha256").update(gz).digest("base64")
|
|
2971
4765
|
};
|
|
2972
4766
|
}
|
|
2973
4767
|
async function forEachLimited(items, limit, fn) {
|
|
@@ -3094,7 +4888,7 @@ var init_batches = __esm({
|
|
|
3094
4888
|
});
|
|
3095
4889
|
|
|
3096
4890
|
// src/cli/platform/worldData/chunker.ts
|
|
3097
|
-
import { createHash as
|
|
4891
|
+
import { createHash as createHash6 } from "crypto";
|
|
3098
4892
|
import { deflateRawSync } from "zlib";
|
|
3099
4893
|
function chunkLine(row, declared) {
|
|
3100
4894
|
if (declared !== null) return dumps(row.row) + "\n";
|
|
@@ -3108,7 +4902,7 @@ async function* buildChunks(rows, plan) {
|
|
|
3108
4902
|
let rawBytes = 0;
|
|
3109
4903
|
let ordinal = 0;
|
|
3110
4904
|
let seen = 0;
|
|
3111
|
-
const
|
|
4905
|
+
const flush2 = () => {
|
|
3112
4906
|
const raw = Buffer.concat(lines);
|
|
3113
4907
|
const built = finishChunk(ordinal++, raw, lines.length, plan.entity, seen - lines.length);
|
|
3114
4908
|
lines = [];
|
|
@@ -3118,23 +4912,23 @@ async function* buildChunks(rows, plan) {
|
|
|
3118
4912
|
for await (const row of rows) {
|
|
3119
4913
|
const line = Buffer.from(chunkLine(row, plan.entity), "utf8");
|
|
3120
4914
|
const full = lines.length > 0 && (plan.chunkRows !== null && lines.length >= plan.chunkRows || rawBytes + line.length > plan.chunkBytes);
|
|
3121
|
-
if (full) yield
|
|
4915
|
+
if (full) yield flush2();
|
|
3122
4916
|
lines.push(line);
|
|
3123
4917
|
rawBytes += line.length;
|
|
3124
4918
|
seen++;
|
|
3125
4919
|
}
|
|
3126
|
-
if (lines.length) yield
|
|
4920
|
+
if (lines.length) yield flush2();
|
|
3127
4921
|
}
|
|
3128
4922
|
function finishChunk(ordinal, raw, rows, entity, firstRow = 0) {
|
|
3129
4923
|
const gz = gzipDeterministic(raw, { name: chunkName(ordinal) });
|
|
3130
4924
|
return {
|
|
3131
4925
|
ordinal,
|
|
3132
|
-
digest:
|
|
4926
|
+
digest: createHash6("sha256").update(gz).digest("hex"),
|
|
3133
4927
|
bytes: gz.length,
|
|
3134
4928
|
rows,
|
|
3135
4929
|
entity,
|
|
3136
4930
|
rawBytes: raw.length,
|
|
3137
|
-
contentSha256:
|
|
4931
|
+
contentSha256: createHash6("sha256").update(raw).digest("hex"),
|
|
3138
4932
|
firstRow,
|
|
3139
4933
|
gz
|
|
3140
4934
|
};
|
|
@@ -3217,9 +5011,9 @@ var init_push = __esm({
|
|
|
3217
5011
|
});
|
|
3218
5012
|
|
|
3219
5013
|
// src/worlds/ingest.ts
|
|
3220
|
-
import * as
|
|
5014
|
+
import * as fs9 from "fs";
|
|
3221
5015
|
import * as os3 from "os";
|
|
3222
|
-
import * as
|
|
5016
|
+
import * as path8 from "path";
|
|
3223
5017
|
function parseToolCallRecord(raw) {
|
|
3224
5018
|
const none = { record: null, skip: null };
|
|
3225
5019
|
if (!isObject2(raw)) return { ...none, refusal: "record is not an object" };
|
|
@@ -3240,7 +5034,7 @@ function parseToolCallRecord(raw) {
|
|
|
3240
5034
|
}
|
|
3241
5035
|
function readToolCallRecords(paths) {
|
|
3242
5036
|
const records = [];
|
|
3243
|
-
for (const file of paths) records.push(...toolCallRecordsIn(
|
|
5037
|
+
for (const file of paths) records.push(...toolCallRecordsIn(fs9.readFileSync(file, "utf8"), file));
|
|
3244
5038
|
return records;
|
|
3245
5039
|
}
|
|
3246
5040
|
function toolCallRecordsIn(text2, where) {
|
|
@@ -3276,7 +5070,7 @@ async function emit(sink, record) {
|
|
|
3276
5070
|
return;
|
|
3277
5071
|
}
|
|
3278
5072
|
if (typeof sink === "string") {
|
|
3279
|
-
|
|
5073
|
+
fs9.appendFileSync(sink, JSON.stringify(record) + "\n");
|
|
3280
5074
|
return;
|
|
3281
5075
|
}
|
|
3282
5076
|
if (typeof sink === "function") {
|
|
@@ -3365,7 +5159,7 @@ async function ingest(world, records, options = {}) {
|
|
|
3365
5159
|
const mode = options.mode ?? "append";
|
|
3366
5160
|
const dryRun = Boolean(options.dryRun);
|
|
3367
5161
|
const creds = credentialsOf(options);
|
|
3368
|
-
const local =
|
|
5162
|
+
const local = fs9.existsSync(world) && fs9.statSync(world).isDirectory();
|
|
3369
5163
|
const map = options.map === void 0 ? void 0 : options.map;
|
|
3370
5164
|
let rows = null;
|
|
3371
5165
|
let stderr = "";
|
|
@@ -3381,9 +5175,9 @@ async function ingest(world, records, options = {}) {
|
|
|
3381
5175
|
if (!creds) throw new Error("Gateway keys missing: set GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY (or pass publicKey/secretKey).");
|
|
3382
5176
|
const { pull: pull2 } = await Promise.resolve().then(() => (init_hub(), hub_exports));
|
|
3383
5177
|
const slug = world.split("@", 1)[0];
|
|
3384
|
-
const scratch =
|
|
5178
|
+
const scratch = fs9.mkdtempSync(path8.join(os3.tmpdir(), "gateway-ingest-"));
|
|
3385
5179
|
try {
|
|
3386
|
-
const pulled = await pull2(world,
|
|
5180
|
+
const pulled = await pull2(world, path8.join(scratch, slug), options);
|
|
3387
5181
|
let report;
|
|
3388
5182
|
let projected;
|
|
3389
5183
|
if (rows === null) {
|
|
@@ -3404,7 +5198,7 @@ async function ingest(world, records, options = {}) {
|
|
|
3404
5198
|
if (status.entityCounts) report.entityCounts = status.entityCounts;
|
|
3405
5199
|
return report;
|
|
3406
5200
|
} finally {
|
|
3407
|
-
|
|
5201
|
+
fs9.rmSync(scratch, { recursive: true, force: true });
|
|
3408
5202
|
}
|
|
3409
5203
|
}
|
|
3410
5204
|
async function ingestToolCalls(world, records, options = {}) {
|
|
@@ -3453,8 +5247,8 @@ async function sessionToolObservations(sessionId, options = {}) {
|
|
|
3453
5247
|
`/api/public/sessions/${encodeURIComponent(sessionId)}?includeIO=true`
|
|
3454
5248
|
);
|
|
3455
5249
|
const out = [];
|
|
3456
|
-
for (const
|
|
3457
|
-
for (const observation of
|
|
5250
|
+
for (const trace3 of session.traces ?? [])
|
|
5251
|
+
for (const observation of trace3.observations ?? []) out.push({ traceId: trace3.id ?? null, ...observation });
|
|
3458
5252
|
return out;
|
|
3459
5253
|
}
|
|
3460
5254
|
var isObject2, isoOf;
|
|
@@ -3478,9 +5272,12 @@ __export(worlds_exports, {
|
|
|
3478
5272
|
EVENT_BATCH_SIZE: () => EVENT_BATCH_SIZE,
|
|
3479
5273
|
EVENT_FLUSH_INTERVAL_MS: () => EVENT_FLUSH_INTERVAL_MS,
|
|
3480
5274
|
EVENT_MAX_RETRIES: () => EVENT_MAX_RETRIES,
|
|
5275
|
+
GatewayEvalsClient: () => GatewayEvalsClient,
|
|
3481
5276
|
STRIP_PREFIX: () => STRIP_PREFIX,
|
|
3482
5277
|
SURFACE_KINDS: () => SURFACE_KINDS,
|
|
3483
5278
|
TRANSFORMS: () => TRANSFORMS,
|
|
5279
|
+
TaskRun: () => TaskRun,
|
|
5280
|
+
TaskSessions: () => TaskSessions,
|
|
3484
5281
|
WHITESPACE: () => WHITESPACE,
|
|
3485
5282
|
WorldBrowser: () => WorldBrowser,
|
|
3486
5283
|
WorldSession: () => WorldSession,
|
|
@@ -3489,9 +5286,12 @@ __export(worlds_exports, {
|
|
|
3489
5286
|
WorldSet: () => WorldSet,
|
|
3490
5287
|
WorldSurfaceUnavailable: () => WorldSurfaceUnavailable,
|
|
3491
5288
|
WorldTask: () => WorldTask,
|
|
5289
|
+
aggregateTaskGrade: () => aggregateTaskGrade,
|
|
3492
5290
|
apiRequest: () => apiRequest,
|
|
3493
5291
|
applyTransforms: () => applyTransforms,
|
|
5292
|
+
attachManifest: () => attachManifest,
|
|
3494
5293
|
attachSession: () => attachSession,
|
|
5294
|
+
attachTask: () => attachTask,
|
|
3495
5295
|
buildSourceTarball: () => buildSourceTarball,
|
|
3496
5296
|
captureToolCalls: () => captureToolCalls,
|
|
3497
5297
|
collectArchiveRelPaths: () => collectArchiveRelPaths,
|
|
@@ -3510,15 +5310,19 @@ __export(worlds_exports, {
|
|
|
3510
5310
|
ingestToolCalls: () => ingestToolCalls,
|
|
3511
5311
|
isIncludedInContentHash: () => isIncludedInContentHash,
|
|
3512
5312
|
isTaskIdRef: () => isTaskIdRef,
|
|
5313
|
+
isTaskManifest: () => isTaskManifest,
|
|
3513
5314
|
isTaskSpec: () => isTaskSpec,
|
|
3514
5315
|
listEnvSecretKeys: () => listEnvSecretKeys,
|
|
3515
5316
|
listRunConfigs: () => listRunConfigs,
|
|
5317
|
+
listTaskSessions: () => listTaskSessions,
|
|
3516
5318
|
listTasks: () => listTasks,
|
|
3517
5319
|
localTreeState: () => localTreeState,
|
|
3518
5320
|
migrateProjectWorlds: () => migrateProjectWorlds,
|
|
3519
5321
|
migrateWorld: () => migrateWorld,
|
|
5322
|
+
modelToolName: () => modelToolName,
|
|
3520
5323
|
openBrowser: () => openBrowser,
|
|
3521
5324
|
openSession: () => openSession,
|
|
5325
|
+
openTask: () => openTask,
|
|
3522
5326
|
openWorld: () => openWorld,
|
|
3523
5327
|
parseToolCallRecord: () => parseToolCallRecord,
|
|
3524
5328
|
parseTransform: () => parseTransform,
|
|
@@ -3527,6 +5331,7 @@ __export(worlds_exports, {
|
|
|
3527
5331
|
propose: () => propose,
|
|
3528
5332
|
pull: () => pull,
|
|
3529
5333
|
push: () => push,
|
|
5334
|
+
pushSlug: () => pushSlug,
|
|
3530
5335
|
pyText: () => pyText,
|
|
3531
5336
|
queryRows: () => queryRows,
|
|
3532
5337
|
queryRowsSearch: () => queryRowsSearch,
|
|
@@ -3537,6 +5342,7 @@ __export(worlds_exports, {
|
|
|
3537
5342
|
resolveBrowserMode: () => resolveBrowserMode,
|
|
3538
5343
|
roundForm: () => roundForm,
|
|
3539
5344
|
runSessions: () => runSessions,
|
|
5345
|
+
runTask: () => runTask,
|
|
3540
5346
|
schemaFromSignature: () => schemaFromSignature,
|
|
3541
5347
|
sessionToolObservations: () => sessionToolObservations,
|
|
3542
5348
|
setEnvSecret: () => setEnvSecret,
|
|
@@ -3568,9 +5374,9 @@ function resolveEnv(options) {
|
|
|
3568
5374
|
}
|
|
3569
5375
|
return { host: host.replace(/\/+$/, ""), publicKey, secretKey };
|
|
3570
5376
|
}
|
|
3571
|
-
async function apiRequest(options, method,
|
|
5377
|
+
async function apiRequest(options, method, path9, body) {
|
|
3572
5378
|
const { host, publicKey, secretKey } = resolveEnv(options);
|
|
3573
|
-
const res = await fetch(`${host}${
|
|
5379
|
+
const res = await fetch(`${host}${path9}`, {
|
|
3574
5380
|
method,
|
|
3575
5381
|
headers: {
|
|
3576
5382
|
"content-type": "application/json",
|
|
@@ -3579,7 +5385,7 @@ async function apiRequest(options, method, path8, body) {
|
|
|
3579
5385
|
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
3580
5386
|
});
|
|
3581
5387
|
const text2 = await res.text();
|
|
3582
|
-
if (!res.ok) throw new ApiError2(res.status, method,
|
|
5388
|
+
if (!res.ok) throw new ApiError2(res.status, method, path9, text2.slice(0, 400));
|
|
3583
5389
|
return JSON.parse(text2);
|
|
3584
5390
|
}
|
|
3585
5391
|
async function setEnvSecret(slug, key, value, options = {}) {
|
|
@@ -3736,6 +5542,9 @@ var init_worlds = __esm({
|
|
|
3736
5542
|
"src/worlds/index.ts"() {
|
|
3737
5543
|
init_session();
|
|
3738
5544
|
init_tasks();
|
|
5545
|
+
init_taskSessions();
|
|
5546
|
+
init_taskRun();
|
|
5547
|
+
init_evals();
|
|
3739
5548
|
init_browser();
|
|
3740
5549
|
init_hub();
|
|
3741
5550
|
init_validate();
|
|
@@ -3746,8 +5555,8 @@ var init_worlds = __esm({
|
|
|
3746
5555
|
init_migrate();
|
|
3747
5556
|
init_ingest();
|
|
3748
5557
|
ApiError2 = class extends Error {
|
|
3749
|
-
constructor(status, method,
|
|
3750
|
-
super(`${method} ${
|
|
5558
|
+
constructor(status, method, path9, body) {
|
|
5559
|
+
super(`${method} ${path9} -> ${status}: ${body}`);
|
|
3751
5560
|
this.status = status;
|
|
3752
5561
|
this.name = "ApiError";
|
|
3753
5562
|
}
|
|
@@ -3826,9 +5635,12 @@ export {
|
|
|
3826
5635
|
EVENT_BATCH_SIZE,
|
|
3827
5636
|
EVENT_FLUSH_INTERVAL_MS,
|
|
3828
5637
|
EVENT_MAX_RETRIES,
|
|
5638
|
+
GatewayEvalsClient,
|
|
3829
5639
|
STRIP_PREFIX,
|
|
3830
5640
|
SURFACE_KINDS,
|
|
3831
5641
|
TRANSFORMS,
|
|
5642
|
+
TaskRun,
|
|
5643
|
+
TaskSessions,
|
|
3832
5644
|
WHITESPACE,
|
|
3833
5645
|
WorldBrowser,
|
|
3834
5646
|
WorldSession,
|
|
@@ -3837,9 +5649,12 @@ export {
|
|
|
3837
5649
|
WorldSet,
|
|
3838
5650
|
WorldSurfaceUnavailable,
|
|
3839
5651
|
WorldTask,
|
|
5652
|
+
aggregateTaskGrade,
|
|
3840
5653
|
apiRequest,
|
|
3841
5654
|
applyTransforms,
|
|
5655
|
+
attachManifest,
|
|
3842
5656
|
attachSession,
|
|
5657
|
+
attachTask,
|
|
3843
5658
|
buildSourceTarball,
|
|
3844
5659
|
captureToolCalls,
|
|
3845
5660
|
collectArchiveRelPaths,
|
|
@@ -3858,15 +5673,19 @@ export {
|
|
|
3858
5673
|
ingestToolCalls,
|
|
3859
5674
|
isIncludedInContentHash,
|
|
3860
5675
|
isTaskIdRef,
|
|
5676
|
+
isTaskManifest,
|
|
3861
5677
|
isTaskSpec,
|
|
3862
5678
|
listEnvSecretKeys,
|
|
3863
5679
|
listRunConfigs,
|
|
5680
|
+
listTaskSessions,
|
|
3864
5681
|
listTasks,
|
|
3865
5682
|
localTreeState,
|
|
3866
5683
|
migrateProjectWorlds,
|
|
3867
5684
|
migrateWorld,
|
|
5685
|
+
modelToolName,
|
|
3868
5686
|
openBrowser,
|
|
3869
5687
|
openSession,
|
|
5688
|
+
openTask,
|
|
3870
5689
|
openWorld,
|
|
3871
5690
|
parseToolCallRecord,
|
|
3872
5691
|
parseTransform,
|
|
@@ -3875,6 +5694,7 @@ export {
|
|
|
3875
5694
|
propose,
|
|
3876
5695
|
pull,
|
|
3877
5696
|
push,
|
|
5697
|
+
pushSlug,
|
|
3878
5698
|
pyText,
|
|
3879
5699
|
queryRows,
|
|
3880
5700
|
queryRowsSearch,
|
|
@@ -3885,6 +5705,7 @@ export {
|
|
|
3885
5705
|
resolveBrowserMode,
|
|
3886
5706
|
roundForm,
|
|
3887
5707
|
runSessions,
|
|
5708
|
+
runTask,
|
|
3888
5709
|
schemaFromSignature,
|
|
3889
5710
|
sessionToolObservations,
|
|
3890
5711
|
setEnvSecret,
|