@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
@@ -1,4 +1,4 @@
1
- export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as SESSION_ID_ATTRIBUTE, d as SessionSpanProcessor, T as TracingConfig, e as TracingHandle, f as TracingState, U as USER_ID_ATTRIBUTE, g as flush, h as getAmbientSessionId, i as identify, j as init, k as initIsolated, l as isGatewayConfigured, m as isInitialized, o as observe, n as observeAnthropic, p as observeClaude, q as observeOpenAI, s as resetAmbientUser, r as resolveUserId, t as shutdown, w as withSession } from '../index-C1wDEwH4.mjs';
1
+ export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as SESSION_ID_ATTRIBUTE, d as SessionSpanProcessor, T as TracingConfig, e as TracingHandle, f as TracingState, U as USER_ID_ATTRIBUTE, g as flush, h as getAmbientSessionId, i as identify, j as init, k as initIsolated, l as isGatewayConfigured, m as isInitialized, o as observe, n as observeAnthropic, p as observeClaude, q as observeOpenAI, s as resetAmbientUser, r as resolveUserId, t as shutdown, w as withSession } from '../index-Cz8Q6Keo.mjs';
2
2
  import '@opentelemetry/api';
3
3
  import '../scores/index.mjs';
4
4
  import '@opentelemetry/sdk-trace-base';
@@ -1,4 +1,4 @@
1
- export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as SESSION_ID_ATTRIBUTE, d as SessionSpanProcessor, T as TracingConfig, e as TracingHandle, f as TracingState, U as USER_ID_ATTRIBUTE, g as flush, h as getAmbientSessionId, i as identify, j as init, k as initIsolated, l as isGatewayConfigured, m as isInitialized, o as observe, n as observeAnthropic, p as observeClaude, q as observeOpenAI, s as resetAmbientUser, r as resolveUserId, t as shutdown, w as withSession } from '../index-1xnxt0Cc.js';
1
+ export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, S as SESSION_ID_ATTRIBUTE, d as SessionSpanProcessor, T as TracingConfig, e as TracingHandle, f as TracingState, U as USER_ID_ATTRIBUTE, g as flush, h as getAmbientSessionId, i as identify, j as init, k as initIsolated, l as isGatewayConfigured, m as isInitialized, o as observe, n as observeAnthropic, p as observeClaude, q as observeOpenAI, s as resetAmbientUser, r as resolveUserId, t as shutdown, w as withSession } from '../index-BO2SNSgy.js';
2
2
  import '@opentelemetry/api';
3
3
  import '../scores/index.js';
4
4
  import '@opentelemetry/sdk-trace-base';
@@ -25,7 +25,7 @@ var require_package = __commonJS({
25
25
  "package.json"(exports2, module2) {
26
26
  module2.exports = {
27
27
  name: "@withgateway/sdk",
28
- version: "0.2.5",
28
+ version: "0.2.6",
29
29
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
30
30
  main: "./dist/index.js",
31
31
  module: "./dist/index.mjs",
@@ -1306,10 +1306,28 @@ function toolResultText(content) {
1306
1306
  ).join("");
1307
1307
  return JSON.stringify(content ?? "");
1308
1308
  }
1309
+ function toolSummary(names) {
1310
+ return names.map((n) => `\u2192 ${n}`).join(", ");
1311
+ }
1312
+ var USAGE_ATTRIBUTES = [
1313
+ ["input_tokens", "llm.token_count.prompt"],
1314
+ ["output_tokens", "llm.token_count.completion"],
1315
+ ["cache_read_input_tokens", "llm.token_count.prompt_details.cache_read"],
1316
+ ["cache_creation_input_tokens", "llm.token_count.prompt_details.cache_write"]
1317
+ ];
1318
+ function setUsageAttributes(span, usage) {
1319
+ if (!usage || typeof usage !== "object") return;
1320
+ const u = usage;
1321
+ for (const [field, attribute] of USAGE_ATTRIBUTES) {
1322
+ const value = u[field];
1323
+ if (typeof value === "number" && Number.isFinite(value))
1324
+ span.setAttribute(attribute, value);
1325
+ }
1326
+ }
1309
1327
  var TURN_INPUT_MAX = 4e3;
1310
1328
  async function* observeClaude(stream, options) {
1311
1329
  const tracer = options.tracer ?? import_api4.trace.getTracer("@withgateway/sdk");
1312
- const model = options.model ?? "claude";
1330
+ const fallbackModel = options.model ?? "claude";
1313
1331
  const openTools = /* @__PURE__ */ new Map();
1314
1332
  let root = null;
1315
1333
  let rootCtx = null;
@@ -1327,85 +1345,102 @@ async function* observeClaude(stream, options) {
1327
1345
  let turnCtx = null;
1328
1346
  let turnIndex = 0;
1329
1347
  let turnText = "";
1348
+ let turnTools = [];
1330
1349
  let pendingTurnInput = options.input ?? "";
1350
+ let gen = null;
1351
+ let genMessageId;
1352
+ let genText = "";
1353
+ let genTools = [];
1354
+ let genUsage;
1331
1355
  let finalText = "";
1356
+ const endGeneration = () => {
1357
+ if (!gen) return;
1358
+ gen.setAttribute("output.value", genText || toolSummary(genTools));
1359
+ setUsageAttributes(gen, genUsage);
1360
+ gen.setStatus({ code: import_api4.SpanStatusCode.OK });
1361
+ gen.end();
1362
+ gen = null;
1363
+ genMessageId = void 0;
1364
+ genText = "";
1365
+ genTools = [];
1366
+ genUsage = void 0;
1367
+ };
1332
1368
  const endTurn = () => {
1333
1369
  if (!turnRoot) return;
1334
- if (turnText) turnRoot.setAttribute("output.value", turnText);
1370
+ const output = turnText || toolSummary(turnTools);
1371
+ if (output) turnRoot.setAttribute("output.value", output);
1335
1372
  turnRoot.setStatus({ code: import_api4.SpanStatusCode.OK });
1336
1373
  turnRoot.end();
1337
1374
  turnRoot = null;
1338
1375
  turnCtx = null;
1339
1376
  };
1377
+ const startTurn = () => {
1378
+ for (const span of openTools.values()) span.end();
1379
+ openTools.clear();
1380
+ endTurn();
1381
+ turnIndex += 1;
1382
+ turnText = "";
1383
+ turnTools = [];
1384
+ turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
1385
+ root: true,
1386
+ attributes: {
1387
+ "openinference.span.kind": "AGENT",
1388
+ ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
1389
+ ...options.attributes ?? {}
1390
+ }
1391
+ });
1392
+ turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
1393
+ pendingTurnInput = "";
1394
+ };
1395
+ const startGeneration = (message) => {
1396
+ endGeneration();
1397
+ if (options.perTurn) startTurn();
1398
+ const childCtx = options.perTurn ? turnCtx : rootCtx;
1399
+ genMessageId = message.message?.id;
1400
+ gen = tracer.startSpan(
1401
+ "assistant",
1402
+ {
1403
+ attributes: {
1404
+ "openinference.span.kind": "LLM",
1405
+ "llm.model_name": message.message?.model ?? fallbackModel,
1406
+ "llm.provider": "anthropic"
1407
+ }
1408
+ },
1409
+ childCtx
1410
+ );
1411
+ };
1340
1412
  try {
1341
1413
  for await (const message of stream) {
1342
1414
  if (message?.type === "assistant") {
1343
- if (options.perTurn) {
1344
- for (const span of openTools.values()) span.end();
1345
- openTools.clear();
1346
- endTurn();
1347
- turnIndex += 1;
1348
- turnText = "";
1349
- turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
1350
- root: true,
1351
- attributes: {
1352
- "openinference.span.kind": "AGENT",
1353
- ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
1354
- ...options.attributes ?? {}
1355
- }
1356
- });
1357
- turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
1358
- pendingTurnInput = "";
1359
- }
1415
+ const messageId = message.message?.id;
1416
+ const continuesGeneration = gen !== null && messageId !== void 0 && messageId === genMessageId;
1417
+ if (!continuesGeneration) startGeneration(message);
1360
1418
  const childCtx = options.perTurn ? turnCtx : rootCtx;
1361
- let text = "";
1362
- const toolUses = [];
1363
1419
  for (const block of message.message?.content ?? []) {
1364
1420
  if (block?.type === "text") {
1365
1421
  finalText += block.text;
1366
- text += block.text;
1422
+ turnText += block.text;
1423
+ genText += block.text;
1367
1424
  }
1368
1425
  if (block?.type === "tool_use") {
1369
- toolUses.push({
1370
- id: block.id,
1371
- name: block.name,
1372
- input: block.input
1373
- });
1426
+ const name = cleanToolName(block.name);
1427
+ genTools.push(name);
1428
+ turnTools.push(name);
1429
+ const span = tracer.startSpan(
1430
+ `tool.${name}`,
1431
+ {
1432
+ attributes: {
1433
+ "openinference.span.kind": "TOOL",
1434
+ "tool.name": name,
1435
+ "input.value": JSON.stringify(block.input)
1436
+ }
1437
+ },
1438
+ childCtx
1439
+ );
1440
+ openTools.set(block.id, span);
1374
1441
  }
1375
1442
  }
1376
- turnText += text;
1377
- const gen = tracer.startSpan(
1378
- "assistant",
1379
- {
1380
- attributes: {
1381
- "openinference.span.kind": "LLM",
1382
- "llm.model_name": model,
1383
- "llm.provider": "anthropic",
1384
- "output.value": text || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
1385
- }
1386
- },
1387
- childCtx
1388
- );
1389
- const usage = message.message?.usage;
1390
- if (usage?.input_tokens != null)
1391
- gen.setAttribute("llm.token_count.prompt", usage.input_tokens);
1392
- if (usage?.output_tokens != null)
1393
- gen.setAttribute("llm.token_count.completion", usage.output_tokens);
1394
- gen.end();
1395
- for (const t of toolUses) {
1396
- const span = tracer.startSpan(
1397
- `tool.${cleanToolName(t.name)}`,
1398
- {
1399
- attributes: {
1400
- "openinference.span.kind": "TOOL",
1401
- "tool.name": cleanToolName(t.name),
1402
- "input.value": JSON.stringify(t.input)
1403
- }
1404
- },
1405
- childCtx
1406
- );
1407
- openTools.set(t.id, span);
1408
- }
1443
+ if (message.message?.usage) genUsage = message.message.usage;
1409
1444
  }
1410
1445
  if (message?.type === "user") {
1411
1446
  const content = message.message?.content;
@@ -1430,6 +1465,7 @@ async function* observeClaude(stream, options) {
1430
1465
  yield message;
1431
1466
  }
1432
1467
  } finally {
1468
+ endGeneration();
1433
1469
  for (const span of openTools.values()) span.end();
1434
1470
  openTools.clear();
1435
1471
  if (options.perTurn) {