@theokit/agents 0.42.0 → 0.44.0

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.
@@ -506,6 +506,107 @@ function generateAgentRoutes(ctx) {
506
506
  }
507
507
  __name(generateAgentRoutes, "generateAgentRoutes");
508
508
 
509
+ // src/bridge/define-agent.ts
510
+ var AGENT_BRAND = /* @__PURE__ */ Symbol.for("theokit.agent.definition");
511
+ function defineAgent(config) {
512
+ return {
513
+ ...config,
514
+ [AGENT_BRAND]: true
515
+ };
516
+ }
517
+ __name(defineAgent, "defineAgent");
518
+ function isAgentDefinition(value) {
519
+ return typeof value === "object" && value !== null && value[AGENT_BRAND] === true;
520
+ }
521
+ __name(isAgentDefinition, "isAgentDefinition");
522
+ function toCompiledTool(tool) {
523
+ const handler = tool.handler;
524
+ const compiled = {
525
+ name: tool.name,
526
+ description: tool.description,
527
+ inputSchema: tool.inputSchema,
528
+ handler: /* @__PURE__ */ __name((input, ctx) => handler(input, ctx), "handler")
529
+ };
530
+ for (const sym of Object.getOwnPropertySymbols(tool)) {
531
+ ;
532
+ compiled[sym] = tool[sym];
533
+ }
534
+ return compiled;
535
+ }
536
+ __name(toCompiledTool, "toCompiledTool");
537
+ function compileAgentDefinition(def) {
538
+ return {
539
+ model: def.model,
540
+ reasoningEffort: def.reasoningEffort,
541
+ systemPrompt: def.system,
542
+ tools: (def.tools ?? []).map(toCompiledTool),
543
+ agents: {},
544
+ stream: true,
545
+ // M7 — run-context flows to CompiledAgentOptions.runContext (distinct from the
546
+ // context-window `context` field the decorator path uses); absent ⇒ no key.
547
+ ...def.context !== void 0 ? {
548
+ runContext: def.context
549
+ } : {},
550
+ // M9 — guardrails flow through unchanged; the runner applies them at the input boundary.
551
+ ...def.guardrails !== void 0 ? {
552
+ guardrails: def.guardrails
553
+ } : {},
554
+ // M14 — HITL approvals compile into the same `hitl` map the decorator path produces.
555
+ ...def.approvals !== void 0 ? {
556
+ hitl: compileApprovals(def)
557
+ } : {},
558
+ // M13 — skills: a static list → SDK skills.enabled; a resolver → carried for the request path.
559
+ ...compileSkillsSelection(def.skills),
560
+ // theokit-file-based-config — the declared `.theokit/` sources flow to the run path, which
561
+ // projects them into `Agent.create({ local.settingSources })`; absent ⇒ inline config only.
562
+ ...def.settingSources !== void 0 ? {
563
+ settingSources: def.settingSources
564
+ } : {},
565
+ // MCP — builder/`defineAgent` servers converge on the same `mcpServers` field the `@MCP`
566
+ // decorator path populates; the SDK adapter forwards it to `Agent.create({ mcpServers })`.
567
+ ...def.mcpServers !== void 0 ? {
568
+ mcpServers: def.mcpServers
569
+ } : {}
570
+ };
571
+ }
572
+ __name(compileAgentDefinition, "compileAgentDefinition");
573
+ function compileSkillsSelection(skills) {
574
+ if (skills === void 0) return {};
575
+ if (typeof skills === "function") return {
576
+ skillsResolver: skills
577
+ };
578
+ const enabled = [];
579
+ const inline = [];
580
+ for (const entry of skills) {
581
+ if (typeof entry === "string") enabled.push(entry);
582
+ else inline.push(entry);
583
+ }
584
+ return {
585
+ skills: {
586
+ enabled,
587
+ autoInject: true,
588
+ ...inline.length > 0 ? {
589
+ inline
590
+ } : {}
591
+ }
592
+ };
593
+ }
594
+ __name(compileSkillsSelection, "compileSkillsSelection");
595
+ function compileApprovals(def) {
596
+ const toolNames = new Set((def.tools ?? []).map((t) => t.name));
597
+ const gates = /* @__PURE__ */ new Map();
598
+ for (const [toolName, options] of Object.entries(def.approvals ?? {})) {
599
+ if (!toolNames.has(toolName)) {
600
+ throw new Error(`[@theokit/agents] defineAgent approval references unknown tool "${toolName}". Declared tools: ${[
601
+ ...toolNames
602
+ ].join(", ") || "(none)"}.`);
603
+ }
604
+ gates.set(toolName, options);
605
+ }
606
+ return gates;
607
+ }
608
+ __name(compileApprovals, "compileApprovals");
609
+
509
610
  // src/bridge/event-translator.ts
510
611
  function asString(value, fallback) {
511
612
  if (typeof value === "string") return value;
@@ -1301,6 +1402,43 @@ async function* streamSdkAgent(rt, compiled, sdkTools, opts) {
1301
1402
  }
1302
1403
  }
1303
1404
  __name(streamSdkAgent, "streamSdkAgent");
1405
+ function toAgentFactory(def, opts) {
1406
+ const compiled = compileAgentDefinition(def);
1407
+ const overrides = opts.overrides ?? {};
1408
+ const model = overrides.model ?? compiled.model ?? "openai/gpt-4o-mini";
1409
+ const reasoningEffort = overrides.reasoningEffort ?? compiled.reasoningEffort;
1410
+ const runContext = overrides.runContext ?? compiled.runContext;
1411
+ return async (sessionId) => {
1412
+ const rt = await loadSdkRuntime();
1413
+ if (!rt) {
1414
+ throw new Error("[@theokit/agents] @theokit/sdk is not installed \u2014 run: pnpm add @theokit/sdk");
1415
+ }
1416
+ const sdkTools = buildSdkTools(compiled.tools, rt.defineTool, overrides.sdkTools, runContext);
1417
+ const inlineSkills = compiled.skills?.inline;
1418
+ if (inlineSkills !== void 0 && inlineSkills.length > 0 && rt.defineSkillReadTool !== void 0 && !compiled.tools.some((t) => t.name === "skill_read")) {
1419
+ sdkTools.push(rt.defineSkillReadTool(inlineSkills));
1420
+ }
1421
+ const { options: m8 } = assembleM8CreateOptions(compiled);
1422
+ if (overrides.cwd !== void 0) m8.local = {
1423
+ ...m8.local,
1424
+ cwd: overrides.cwd
1425
+ };
1426
+ if (overrides.baseDir !== void 0) m8.local = {
1427
+ ...m8.local,
1428
+ baseDir: overrides.baseDir
1429
+ };
1430
+ const extra = buildExtraCreateOptions(overrides, compiled);
1431
+ const agent2 = await rt.Agent.getOrCreate(sessionId, {
1432
+ apiKey: opts.apiKey,
1433
+ model: buildModelSelection(model, reasoningEffort),
1434
+ tools: sdkTools,
1435
+ ...m8,
1436
+ ...extra
1437
+ });
1438
+ return agent2;
1439
+ };
1440
+ }
1441
+ __name(toAgentFactory, "toAgentFactory");
1304
1442
 
1305
1443
  // src/bridge/ui-message-stream-translator.ts
1306
1444
  function* closeOpenBlock(state, textId) {
@@ -1319,6 +1457,41 @@ function* closeOpenBlock(state, textId) {
1319
1457
  state.reasoningId = null;
1320
1458
  }
1321
1459
  __name(closeOpenBlock, "closeOpenBlock");
1460
+ function* emitTextDelta(state, textId, content) {
1461
+ if (state.openBlock !== "text") {
1462
+ yield* closeOpenBlock(state, textId);
1463
+ yield {
1464
+ type: "text-start",
1465
+ id: textId
1466
+ };
1467
+ state.openBlock = "text";
1468
+ }
1469
+ yield {
1470
+ type: "text-delta",
1471
+ id: textId,
1472
+ delta: content
1473
+ };
1474
+ }
1475
+ __name(emitTextDelta, "emitTextDelta");
1476
+ function* emitReasoningDelta(state, textId, content) {
1477
+ let reasoningId = state.openBlock === "reasoning" ? state.reasoningId : null;
1478
+ if (reasoningId === null) {
1479
+ yield* closeOpenBlock(state, textId);
1480
+ reasoningId = crypto.randomUUID();
1481
+ state.reasoningId = reasoningId;
1482
+ yield {
1483
+ type: "reasoning-start",
1484
+ id: reasoningId
1485
+ };
1486
+ state.openBlock = "reasoning";
1487
+ }
1488
+ yield {
1489
+ type: "reasoning-delta",
1490
+ id: reasoningId,
1491
+ delta: content
1492
+ };
1493
+ }
1494
+ __name(emitReasoningDelta, "emitReasoningDelta");
1322
1495
  function* emitToolCall(event, seen) {
1323
1496
  seen.add(event.callId);
1324
1497
  yield {
@@ -1395,39 +1568,13 @@ async function* translateToUIMessageStream(events, opts) {
1395
1568
  reasoningId: null
1396
1569
  };
1397
1570
  const seenToolCallIds = /* @__PURE__ */ new Set();
1571
+ let turnMetadata;
1398
1572
  try {
1399
1573
  for await (const event of events) {
1400
1574
  if (event.type === "text_delta") {
1401
- if (state.openBlock !== "text") {
1402
- yield* closeOpenBlock(state, opts.textId);
1403
- yield {
1404
- type: "text-start",
1405
- id: opts.textId
1406
- };
1407
- state.openBlock = "text";
1408
- }
1409
- yield {
1410
- type: "text-delta",
1411
- id: opts.textId,
1412
- delta: event.content
1413
- };
1575
+ yield* emitTextDelta(state, opts.textId, event.content);
1414
1576
  } else if (event.type === "thinking") {
1415
- let reasoningId = state.openBlock === "reasoning" ? state.reasoningId : null;
1416
- if (reasoningId === null) {
1417
- yield* closeOpenBlock(state, opts.textId);
1418
- reasoningId = crypto.randomUUID();
1419
- state.reasoningId = reasoningId;
1420
- yield {
1421
- type: "reasoning-start",
1422
- id: reasoningId
1423
- };
1424
- state.openBlock = "reasoning";
1425
- }
1426
- yield {
1427
- type: "reasoning-delta",
1428
- id: reasoningId,
1429
- delta: event.content
1430
- };
1577
+ yield* emitReasoningDelta(state, opts.textId, event.content);
1431
1578
  } else if (event.type === "tool_call") {
1432
1579
  yield* closeOpenBlock(state, opts.textId);
1433
1580
  yield* emitToolCall(event, seenToolCallIds);
@@ -1447,6 +1594,7 @@ async function* translateToUIMessageStream(events, opts) {
1447
1594
  };
1448
1595
  break;
1449
1596
  } else if (event.type === "done") {
1597
+ turnMetadata = doneToMetadata(event);
1450
1598
  break;
1451
1599
  }
1452
1600
  }
@@ -1457,102 +1605,25 @@ async function* translateToUIMessageStream(events, opts) {
1457
1605
  };
1458
1606
  }
1459
1607
  yield* closeOpenBlock(state, opts.textId);
1460
- yield {
1608
+ yield turnMetadata ? {
1609
+ type: "finish",
1610
+ messageMetadata: turnMetadata
1611
+ } : {
1461
1612
  type: "finish"
1462
1613
  };
1463
1614
  }
1464
1615
  __name(translateToUIMessageStream, "translateToUIMessageStream");
1465
-
1466
- // src/bridge/define-agent.ts
1467
- var AGENT_BRAND = /* @__PURE__ */ Symbol.for("theokit.agent.definition");
1468
- function defineAgent(config) {
1469
- return {
1470
- ...config,
1471
- [AGENT_BRAND]: true
1472
- };
1473
- }
1474
- __name(defineAgent, "defineAgent");
1475
- function isAgentDefinition(value) {
1476
- return typeof value === "object" && value !== null && value[AGENT_BRAND] === true;
1477
- }
1478
- __name(isAgentDefinition, "isAgentDefinition");
1479
- function toCompiledTool(tool) {
1480
- const handler = tool.handler;
1481
- return {
1482
- name: tool.name,
1483
- description: tool.description,
1484
- inputSchema: tool.inputSchema,
1485
- handler: /* @__PURE__ */ __name((input, ctx) => handler(input, ctx), "handler")
1486
- };
1487
- }
1488
- __name(toCompiledTool, "toCompiledTool");
1489
- function compileAgentDefinition(def) {
1490
- return {
1491
- model: def.model,
1492
- reasoningEffort: def.reasoningEffort,
1493
- systemPrompt: def.system,
1494
- tools: (def.tools ?? []).map(toCompiledTool),
1495
- agents: {},
1496
- stream: true,
1497
- // M7 — run-context flows to CompiledAgentOptions.runContext (distinct from the
1498
- // context-window `context` field the decorator path uses); absent ⇒ no key.
1499
- ...def.context !== void 0 ? {
1500
- runContext: def.context
1501
- } : {},
1502
- // M9 — guardrails flow through unchanged; the runner applies them at the input boundary.
1503
- ...def.guardrails !== void 0 ? {
1504
- guardrails: def.guardrails
1505
- } : {},
1506
- // M14 — HITL approvals compile into the same `hitl` map the decorator path produces.
1507
- ...def.approvals !== void 0 ? {
1508
- hitl: compileApprovals(def)
1509
- } : {},
1510
- // M13 — skills: a static list → SDK skills.enabled; a resolver → carried for the request path.
1511
- ...compileSkillsSelection(def.skills),
1512
- // theokit-file-based-config — the declared `.theokit/` sources flow to the run path, which
1513
- // projects them into `Agent.create({ local.settingSources })`; absent ⇒ inline config only.
1514
- ...def.settingSources !== void 0 ? {
1515
- settingSources: def.settingSources
1516
- } : {}
1517
- };
1518
- }
1519
- __name(compileAgentDefinition, "compileAgentDefinition");
1520
- function compileSkillsSelection(skills) {
1521
- if (skills === void 0) return {};
1522
- if (typeof skills === "function") return {
1523
- skillsResolver: skills
1524
- };
1525
- const enabled = [];
1526
- const inline = [];
1527
- for (const entry of skills) {
1528
- if (typeof entry === "string") enabled.push(entry);
1529
- else inline.push(entry);
1530
- }
1531
- return {
1532
- skills: {
1533
- enabled,
1534
- autoInject: true,
1535
- ...inline.length > 0 ? {
1536
- inline
1537
- } : {}
1538
- }
1616
+ function doneToMetadata(event) {
1617
+ return event.cost === void 0 ? {
1618
+ usage: event.usage,
1619
+ durationMs: event.durationMs
1620
+ } : {
1621
+ usage: event.usage,
1622
+ durationMs: event.durationMs,
1623
+ cost: event.cost
1539
1624
  };
1540
1625
  }
1541
- __name(compileSkillsSelection, "compileSkillsSelection");
1542
- function compileApprovals(def) {
1543
- const toolNames = new Set((def.tools ?? []).map((t) => t.name));
1544
- const gates = /* @__PURE__ */ new Map();
1545
- for (const [toolName, options] of Object.entries(def.approvals ?? {})) {
1546
- if (!toolNames.has(toolName)) {
1547
- throw new Error(`[@theokit/agents] defineAgent approval references unknown tool "${toolName}". Declared tools: ${[
1548
- ...toolNames
1549
- ].join(", ") || "(none)"}.`);
1550
- }
1551
- gates.set(toolName, options);
1552
- }
1553
- return gates;
1554
- }
1555
- __name(compileApprovals, "compileApprovals");
1626
+ __name(doneToMetadata, "doneToMetadata");
1556
1627
 
1557
1628
  // src/bridge/agent-builder.ts
1558
1629
  function contextualTool(tool, _requiredContext) {
@@ -1618,6 +1689,10 @@ function makeBuilder(config) {
1618
1689
  ...config,
1619
1690
  settingSources: sources
1620
1691
  }), "settingSources"),
1692
+ mcp: /* @__PURE__ */ __name((servers) => makeBuilder({
1693
+ ...config,
1694
+ mcpServers: servers
1695
+ }), "mcp"),
1621
1696
  use: /* @__PURE__ */ __name((preset) => preset(runtime), "use"),
1622
1697
  build: /* @__PURE__ */ __name(() => defineAgent(config), "build")
1623
1698
  };
@@ -2942,15 +3017,16 @@ export {
2942
3017
  isError,
2943
3018
  isApprovalRequired,
2944
3019
  generateAgentRoutes,
3020
+ AGENT_BRAND,
3021
+ isAgentDefinition,
3022
+ compileAgentDefinition,
2945
3023
  translateSdkEvent,
2946
3024
  buildModelSelection,
2947
3025
  createThinkTagExtractor,
2948
3026
  extractThinkTagStream,
2949
3027
  createSdkAgentStream,
3028
+ toAgentFactory,
2950
3029
  translateToUIMessageStream,
2951
- AGENT_BRAND,
2952
- isAgentDefinition,
2953
- compileAgentDefinition,
2954
3030
  contextualTool,
2955
3031
  agent,
2956
3032
  AgentDefinitionError,
@@ -2993,4 +3069,4 @@ export {
2993
3069
  generateAgentManifest,
2994
3070
  agentsPlugin
2995
3071
  };
2996
- //# sourceMappingURL=chunk-OFPS2N3U.js.map
3072
+ //# sourceMappingURL=chunk-6WOLHCZB.js.map