@withgateway/sdk 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/dist/cli.js +3121 -1283
  2. package/dist/cli.js.map +1 -1
  3. package/dist/cli.mjs +3185 -1347
  4. package/dist/cli.mjs.map +1 -1
  5. package/dist/files/index.js +9 -0
  6. package/dist/files/index.js.map +1 -1
  7. package/dist/files/index.mjs +9 -0
  8. package/dist/files/index.mjs.map +1 -1
  9. package/dist/hooks/index.d.mts +1 -1
  10. package/dist/hooks/index.d.ts +1 -1
  11. package/dist/hooks/index.js +280 -8
  12. package/dist/hooks/index.js.map +1 -1
  13. package/dist/hooks/index.mjs +272 -8
  14. package/dist/hooks/index.mjs.map +1 -1
  15. package/dist/{index-1xnxt0Cc.d.ts → index-BO2SNSgy.d.ts} +12 -1
  16. package/dist/{index-B0R3IgsV.d.mts → index-BaKu5ZSU.d.mts} +124 -2
  17. package/dist/{index-B0R3IgsV.d.ts → index-BaKu5ZSU.d.ts} +124 -2
  18. package/dist/{index-C1wDEwH4.d.mts → index-Cz8Q6Keo.d.mts} +12 -1
  19. package/dist/index.d.mts +4 -4
  20. package/dist/index.d.ts +4 -4
  21. package/dist/index.js +726 -167
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +718 -167
  24. package/dist/index.mjs.map +1 -1
  25. package/dist/traces/index.js +9 -0
  26. package/dist/traces/index.js.map +1 -1
  27. package/dist/traces/index.mjs +9 -0
  28. package/dist/traces/index.mjs.map +1 -1
  29. package/dist/tracing/index.d.mts +1 -1
  30. package/dist/tracing/index.d.ts +1 -1
  31. package/dist/tracing/index.js +97 -61
  32. package/dist/tracing/index.js.map +1 -1
  33. package/dist/tracing/index.mjs +97 -61
  34. package/dist/tracing/index.mjs.map +1 -1
  35. package/dist/worlds/index.d.mts +154 -4
  36. package/dist/worlds/index.d.ts +154 -4
  37. package/dist/worlds/index.js +348 -97
  38. package/dist/worlds/index.js.map +1 -1
  39. package/dist/worlds/index.mjs +348 -97
  40. package/dist/worlds/index.mjs.map +1 -1
  41. package/package.json +1 -1
  42. package/runtime/gatewaysdk/environment/_bundle.py +1 -1
  43. package/runtime/gatewaysdk/environment/_materialize.py +53 -9
  44. package/runtime/gatewaysdk/environment/_toml.py +13 -0
  45. package/runtime/gatewaysdk/environment/schema/api.py +3 -7
  46. package/runtime/gatewaysdk/environment/schema/batch.py +15 -11
  47. package/runtime/gatewaysdk/environment/schema/bundle_runtime.py +24 -0
  48. package/runtime/gatewaysdk/environment/schema/compiler.py +60 -28
  49. package/runtime/gatewaysdk/environment/schema/conform.py +1 -1
  50. package/runtime/gatewaysdk/environment/schema/connector.py +1 -1
  51. package/runtime/gatewaysdk/environment/schema/connectors/google-drive/readers/merge.ingest.toml +25 -0
  52. package/runtime/gatewaysdk/environment/schema/connectors/google-drive/readers/merge.toml +17 -0
  53. package/runtime/gatewaysdk/environment/schema/extract.py +1 -5
  54. package/runtime/gatewaysdk/environment/schema/host_surfaces.py +1 -5
  55. package/runtime/gatewaysdk/environment/schema/mapping.py +1 -1
  56. package/runtime/gatewaysdk/environment/schema/platform.py +16 -4
  57. package/runtime/gatewaysdk/environment/schema/redaction.py +1 -1
  58. package/runtime/gatewaysdk/environment/schema/scaffold.py +9 -2
  59. package/runtime/gatewaysdk/environment/schema/skills/benchmarks-getting-started/SKILL.md +67 -9
  60. package/runtime/gatewaysdk/environment/schema/skills/hooks-getting-started/SKILL.md +61 -2
  61. package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +4 -1
  62. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +11 -11
  63. package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/references/cli-mcp-map.md +17 -1
  64. package/runtime/gatewaysdk/environment/schema/snapshot.py +1 -5
  65. package/runtime/gatewaysdk/environment/schema/tools_world.py +1 -5
  66. package/runtime/gatewaysdk/environment/schema/ui.py +1 -5
  67. package/runtime/gatewaysdk/environment/schema/world_tests.py +1 -1
  68. package/runtime/gatewaysdk/environment/world.py +120 -4
  69. package/skills/benchmarks-getting-started/SKILL.md +67 -9
  70. package/skills/connector-schema-authoring/connector-worlds.md +8 -1
  71. package/skills/connector-schema-authoring/world-schema.md +1 -1
  72. package/skills/hooks-getting-started/SKILL.md +61 -2
  73. package/skills/world-data-ingestion/SKILL.md +4 -1
  74. package/skills/worlds-getting-started/SKILL.md +11 -11
  75. package/skills/worlds-getting-started/references/cli-mcp-map.md +17 -1
@@ -16,7 +16,7 @@ var require_package = __commonJS({
16
16
  "package.json"(exports, module) {
17
17
  module.exports = {
18
18
  name: "@withgateway/sdk",
19
- version: "0.2.5",
19
+ version: "0.2.6",
20
20
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
21
21
  main: "./dist/index.js",
22
22
  module: "./dist/index.mjs",
@@ -1276,10 +1276,28 @@ function toolResultText(content) {
1276
1276
  ).join("");
1277
1277
  return JSON.stringify(content ?? "");
1278
1278
  }
1279
+ function toolSummary(names) {
1280
+ return names.map((n) => `\u2192 ${n}`).join(", ");
1281
+ }
1282
+ var USAGE_ATTRIBUTES = [
1283
+ ["input_tokens", "llm.token_count.prompt"],
1284
+ ["output_tokens", "llm.token_count.completion"],
1285
+ ["cache_read_input_tokens", "llm.token_count.prompt_details.cache_read"],
1286
+ ["cache_creation_input_tokens", "llm.token_count.prompt_details.cache_write"]
1287
+ ];
1288
+ function setUsageAttributes(span, usage) {
1289
+ if (!usage || typeof usage !== "object") return;
1290
+ const u = usage;
1291
+ for (const [field, attribute] of USAGE_ATTRIBUTES) {
1292
+ const value = u[field];
1293
+ if (typeof value === "number" && Number.isFinite(value))
1294
+ span.setAttribute(attribute, value);
1295
+ }
1296
+ }
1279
1297
  var TURN_INPUT_MAX = 4e3;
1280
1298
  async function* observeClaude(stream, options) {
1281
1299
  const tracer = options.tracer ?? trace4.getTracer("@withgateway/sdk");
1282
- const model = options.model ?? "claude";
1300
+ const fallbackModel = options.model ?? "claude";
1283
1301
  const openTools = /* @__PURE__ */ new Map();
1284
1302
  let root = null;
1285
1303
  let rootCtx = null;
@@ -1297,85 +1315,102 @@ async function* observeClaude(stream, options) {
1297
1315
  let turnCtx = null;
1298
1316
  let turnIndex = 0;
1299
1317
  let turnText = "";
1318
+ let turnTools = [];
1300
1319
  let pendingTurnInput = options.input ?? "";
1320
+ let gen = null;
1321
+ let genMessageId;
1322
+ let genText = "";
1323
+ let genTools = [];
1324
+ let genUsage;
1301
1325
  let finalText = "";
1326
+ const endGeneration = () => {
1327
+ if (!gen) return;
1328
+ gen.setAttribute("output.value", genText || toolSummary(genTools));
1329
+ setUsageAttributes(gen, genUsage);
1330
+ gen.setStatus({ code: SpanStatusCode2.OK });
1331
+ gen.end();
1332
+ gen = null;
1333
+ genMessageId = void 0;
1334
+ genText = "";
1335
+ genTools = [];
1336
+ genUsage = void 0;
1337
+ };
1302
1338
  const endTurn = () => {
1303
1339
  if (!turnRoot) return;
1304
- if (turnText) turnRoot.setAttribute("output.value", turnText);
1340
+ const output = turnText || toolSummary(turnTools);
1341
+ if (output) turnRoot.setAttribute("output.value", output);
1305
1342
  turnRoot.setStatus({ code: SpanStatusCode2.OK });
1306
1343
  turnRoot.end();
1307
1344
  turnRoot = null;
1308
1345
  turnCtx = null;
1309
1346
  };
1347
+ const startTurn = () => {
1348
+ for (const span of openTools.values()) span.end();
1349
+ openTools.clear();
1350
+ endTurn();
1351
+ turnIndex += 1;
1352
+ turnText = "";
1353
+ turnTools = [];
1354
+ turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
1355
+ root: true,
1356
+ attributes: {
1357
+ "openinference.span.kind": "AGENT",
1358
+ ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
1359
+ ...options.attributes ?? {}
1360
+ }
1361
+ });
1362
+ turnCtx = trace4.setSpan(context2.active(), turnRoot);
1363
+ pendingTurnInput = "";
1364
+ };
1365
+ const startGeneration = (message) => {
1366
+ endGeneration();
1367
+ if (options.perTurn) startTurn();
1368
+ const childCtx = options.perTurn ? turnCtx : rootCtx;
1369
+ genMessageId = message.message?.id;
1370
+ gen = tracer.startSpan(
1371
+ "assistant",
1372
+ {
1373
+ attributes: {
1374
+ "openinference.span.kind": "LLM",
1375
+ "llm.model_name": message.message?.model ?? fallbackModel,
1376
+ "llm.provider": "anthropic"
1377
+ }
1378
+ },
1379
+ childCtx
1380
+ );
1381
+ };
1310
1382
  try {
1311
1383
  for await (const message of stream) {
1312
1384
  if (message?.type === "assistant") {
1313
- if (options.perTurn) {
1314
- for (const span of openTools.values()) span.end();
1315
- openTools.clear();
1316
- endTurn();
1317
- turnIndex += 1;
1318
- turnText = "";
1319
- turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
1320
- root: true,
1321
- attributes: {
1322
- "openinference.span.kind": "AGENT",
1323
- ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
1324
- ...options.attributes ?? {}
1325
- }
1326
- });
1327
- turnCtx = trace4.setSpan(context2.active(), turnRoot);
1328
- pendingTurnInput = "";
1329
- }
1385
+ const messageId = message.message?.id;
1386
+ const continuesGeneration = gen !== null && messageId !== void 0 && messageId === genMessageId;
1387
+ if (!continuesGeneration) startGeneration(message);
1330
1388
  const childCtx = options.perTurn ? turnCtx : rootCtx;
1331
- let text = "";
1332
- const toolUses = [];
1333
1389
  for (const block of message.message?.content ?? []) {
1334
1390
  if (block?.type === "text") {
1335
1391
  finalText += block.text;
1336
- text += block.text;
1392
+ turnText += block.text;
1393
+ genText += block.text;
1337
1394
  }
1338
1395
  if (block?.type === "tool_use") {
1339
- toolUses.push({
1340
- id: block.id,
1341
- name: block.name,
1342
- input: block.input
1343
- });
1396
+ const name = cleanToolName(block.name);
1397
+ genTools.push(name);
1398
+ turnTools.push(name);
1399
+ const span = tracer.startSpan(
1400
+ `tool.${name}`,
1401
+ {
1402
+ attributes: {
1403
+ "openinference.span.kind": "TOOL",
1404
+ "tool.name": name,
1405
+ "input.value": JSON.stringify(block.input)
1406
+ }
1407
+ },
1408
+ childCtx
1409
+ );
1410
+ openTools.set(block.id, span);
1344
1411
  }
1345
1412
  }
1346
- turnText += text;
1347
- const gen = tracer.startSpan(
1348
- "assistant",
1349
- {
1350
- attributes: {
1351
- "openinference.span.kind": "LLM",
1352
- "llm.model_name": model,
1353
- "llm.provider": "anthropic",
1354
- "output.value": text || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
1355
- }
1356
- },
1357
- childCtx
1358
- );
1359
- const usage = message.message?.usage;
1360
- if (usage?.input_tokens != null)
1361
- gen.setAttribute("llm.token_count.prompt", usage.input_tokens);
1362
- if (usage?.output_tokens != null)
1363
- gen.setAttribute("llm.token_count.completion", usage.output_tokens);
1364
- gen.end();
1365
- for (const t of toolUses) {
1366
- const span = tracer.startSpan(
1367
- `tool.${cleanToolName(t.name)}`,
1368
- {
1369
- attributes: {
1370
- "openinference.span.kind": "TOOL",
1371
- "tool.name": cleanToolName(t.name),
1372
- "input.value": JSON.stringify(t.input)
1373
- }
1374
- },
1375
- childCtx
1376
- );
1377
- openTools.set(t.id, span);
1378
- }
1413
+ if (message.message?.usage) genUsage = message.message.usage;
1379
1414
  }
1380
1415
  if (message?.type === "user") {
1381
1416
  const content = message.message?.content;
@@ -1400,6 +1435,7 @@ async function* observeClaude(stream, options) {
1400
1435
  yield message;
1401
1436
  }
1402
1437
  } finally {
1438
+ endGeneration();
1403
1439
  for (const span of openTools.values()) span.end();
1404
1440
  openTools.clear();
1405
1441
  if (options.perTurn) {