@iqai/adk 0.1.20 → 0.1.21

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/index.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27; var _class28; var _class29; var _class30; var _class31; var _class32; var _class33;var __defProp = Object.defineProperty;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class; var _class2; var _class3; var _class4; var _class5; var _class6; var _class7; var _class8; var _class9; var _class10; var _class11; var _class12; var _class13; var _class14; var _class15; var _class16; var _class17; var _class18; var _class19; var _class20; var _class21; var _class22; var _class23; var _class24; var _class25; var _class26; var _class27; var _class28; var _class29; var _class30; var _class31; var _class32; var _class33; var _class34;var __defProp = Object.defineProperty;
2
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
@@ -1353,7 +1353,7 @@ var BaseLlm = (_class5 = class {
1353
1353
  * @param llmRequest LlmRequest, the request to send to the LLM.
1354
1354
  * @returns BaseLLMConnection, the connection to the LLM.
1355
1355
  */
1356
- connect(llmRequest) {
1356
+ connect(_llmRequest) {
1357
1357
  throw new Error(`Live connection is not supported for ${this.model}.`);
1358
1358
  }
1359
1359
  }, _class5);
@@ -8088,6 +8088,9 @@ var IdentityLlmRequestProcessor = class extends BaseLlmRequestProcessor {
8088
8088
  };
8089
8089
  var requestProcessor5 = new IdentityLlmRequestProcessor();
8090
8090
 
8091
+ // src/flows/llm-flows/instructions.ts
8092
+
8093
+
8091
8094
  // src/utils/instructions-utils.ts
8092
8095
  async function injectSessionState(template, readonlyContext) {
8093
8096
  const invocationContext = readonlyContext._invocationContext;
@@ -8202,6 +8205,22 @@ var InstructionsLlmRequestProcessor = class extends BaseLlmRequestProcessor {
8202
8205
  }
8203
8206
  llmRequest.appendInstructions([instruction]);
8204
8207
  }
8208
+ if (agent.outputSchema) {
8209
+ try {
8210
+ const raw = _zodtojsonschema.zodToJsonSchema.call(void 0, agent.outputSchema, {
8211
+ target: "jsonSchema7",
8212
+ $refStrategy: "none"
8213
+ });
8214
+ const { $schema, ...json } = raw || {};
8215
+ llmRequest.appendInstructions([
8216
+ "You must respond with application/json that validates against this JSON Schema:",
8217
+ "```json",
8218
+ JSON.stringify(json, null, 2),
8219
+ "```"
8220
+ ]);
8221
+ } catch (e5) {
8222
+ }
8223
+ }
8205
8224
  for await (const _ of []) {
8206
8225
  yield _;
8207
8226
  }
@@ -8493,12 +8512,87 @@ function removeThoughtFromRequest(llmRequest) {
8493
8512
  var requestProcessor7 = new NlPlanningRequestProcessor();
8494
8513
  var responseProcessor2 = new NlPlanningResponseProcessor();
8495
8514
 
8515
+ // src/flows/llm-flows/output-schema.ts
8516
+ init_logger();
8517
+ var OutputSchemaResponseProcessor = (_class26 = class extends BaseLlmResponseProcessor {constructor(...args2) { super(...args2); _class26.prototype.__init46.call(this); }
8518
+ __init46() {this.logger = new Logger({ name: "OutputSchemaResponseProcessor" })}
8519
+ async *runAsync(invocationContext, llmResponse) {
8520
+ if (!llmResponse || !llmResponse.content || !llmResponse.content.parts || llmResponse.content.parts.length === 0) {
8521
+ return;
8522
+ }
8523
+ const agent = invocationContext.agent;
8524
+ if (!("outputSchema" in agent) || !agent.outputSchema) {
8525
+ return;
8526
+ }
8527
+ let textContent = llmResponse.content.parts.map((part) => {
8528
+ if (part && typeof part === "object" && "text" in part) {
8529
+ return part.text || "";
8530
+ }
8531
+ return "";
8532
+ }).join("");
8533
+ if (!textContent.trim()) {
8534
+ return;
8535
+ }
8536
+ try {
8537
+ const parsed = JSON.parse(textContent);
8538
+ const validated = agent.outputSchema.parse(parsed);
8539
+ textContent = JSON.stringify(validated, null, 2);
8540
+ llmResponse.content.parts = llmResponse.content.parts.map((part) => {
8541
+ if (part && typeof part === "object" && "text" in part) {
8542
+ return {
8543
+ ...part,
8544
+ text: textContent
8545
+ };
8546
+ }
8547
+ return part;
8548
+ });
8549
+ this.logger.debug("Output schema validation successful", {
8550
+ agent: agent.name,
8551
+ originalLength: textContent.length,
8552
+ validatedKeys: Object.keys(validated)
8553
+ });
8554
+ } catch (error) {
8555
+ const errorMessage = error instanceof Error ? error.message : String(error);
8556
+ const detailedError = `Output schema validation failed for agent '${agent.name}': ${errorMessage}`;
8557
+ this.logger.error(detailedError, {
8558
+ agent: agent.name,
8559
+ responseContent: textContent.substring(0, 200) + (textContent.length > 200 ? "..." : ""),
8560
+ error: errorMessage
8561
+ });
8562
+ llmResponse.errorCode = "OUTPUT_SCHEMA_VALIDATION_FAILED";
8563
+ llmResponse.errorMessage = detailedError;
8564
+ llmResponse.error = new Error(detailedError);
8565
+ const errorEvent = new Event({
8566
+ id: Event.newId(),
8567
+ invocationId: invocationContext.invocationId,
8568
+ author: agent.name,
8569
+ branch: invocationContext.branch,
8570
+ content: {
8571
+ role: "assistant",
8572
+ parts: [
8573
+ {
8574
+ text: `Error: ${detailedError}`
8575
+ }
8576
+ ]
8577
+ }
8578
+ });
8579
+ errorEvent.errorCode = "OUTPUT_SCHEMA_VALIDATION_FAILED";
8580
+ errorEvent.errorMessage = detailedError;
8581
+ errorEvent.error = new Error(detailedError);
8582
+ yield errorEvent;
8583
+ }
8584
+ }
8585
+ }, _class26);
8586
+ var responseProcessor3 = new OutputSchemaResponseProcessor();
8587
+
8496
8588
  // src/flows/llm-flows/shared-memory.ts
8497
8589
  var SharedMemoryRequestProcessor = class extends BaseLlmRequestProcessor {
8498
8590
  async *runAsync(invocationContext, llmRequest) {
8499
8591
  const memoryService = invocationContext.memoryService;
8500
8592
  if (!memoryService) return;
8501
- const lastUserEvent = invocationContext.session.events.findLast((e) => e.author === "user" && _optionalChain([e, 'access', _230 => _230.content, 'optionalAccess', _231 => _231.parts, 'optionalAccess', _232 => _232.length]));
8593
+ const lastUserEvent = invocationContext.session.events.findLast(
8594
+ (e) => e.author === "user" && _optionalChain([e, 'access', _230 => _230.content, 'optionalAccess', _231 => _231.parts, 'optionalAccess', _232 => _232.length])
8595
+ );
8502
8596
  if (!lastUserEvent) return;
8503
8597
  const query = (_nullishCoalesce(lastUserEvent.content.parts, () => ( []))).map((p) => p.text || "").join(" ");
8504
8598
  const results = await memoryService.searchMemory({
@@ -8557,8 +8651,10 @@ var SingleFlow = class extends BaseLlmFlow {
8557
8651
  this.responseProcessors.push(
8558
8652
  responseProcessor2,
8559
8653
  // Phase 5: NL Planning
8654
+ responseProcessor3,
8655
+ // Phase 6: Output Schema validation and parsing - validates response against agent's output schema
8560
8656
  responseProcessor
8561
- // Phase 5: Code Execution (placeholder)
8657
+ // Phase 7: Code Execution (placeholder)
8562
8658
  );
8563
8659
  this.logger.debug("SingleFlow initialized with processors");
8564
8660
  }
@@ -8660,7 +8756,7 @@ var AutoFlow = class extends SingleFlow {
8660
8756
 
8661
8757
  // src/agents/llm-agent.ts
8662
8758
  init_function_tool();
8663
- var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8759
+ var LlmAgent = (_class27 = class _LlmAgent extends BaseAgent {
8664
8760
  /**
8665
8761
  * The model to use for the agent
8666
8762
  * When not set, the agent will inherit the model from its ancestor
@@ -8731,13 +8827,27 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8731
8827
  * The input schema when agent is used as a tool
8732
8828
  */
8733
8829
 
8734
- // Schema type - depends on specific implementation
8735
8830
  /**
8736
8831
  * The output schema when agent replies
8737
8832
  */
8738
8833
 
8739
- // Schema type - depends on specific implementation
8740
- __init46() {this.logger = new Logger({ name: "LlmAgent" })}
8834
+ /**
8835
+ * Callback or list of callbacks to be called before calling the LLM
8836
+ */
8837
+
8838
+ /**
8839
+ * Callback or list of callbacks to be called after calling the LLM
8840
+ */
8841
+
8842
+ /**
8843
+ * Callback or list of callbacks to be called before calling a tool
8844
+ */
8845
+
8846
+ /**
8847
+ * Callback or list of callbacks to be called after calling a tool
8848
+ */
8849
+
8850
+ __init47() {this.logger = new Logger({ name: "LlmAgent" })}
8741
8851
  /**
8742
8852
  * Constructor for LlmAgent
8743
8853
  */
@@ -8748,7 +8858,7 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8748
8858
  subAgents: config.subAgents,
8749
8859
  beforeAgentCallback: config.beforeAgentCallback,
8750
8860
  afterAgentCallback: config.afterAgentCallback
8751
- });_class26.prototype.__init46.call(this);;
8861
+ });_class27.prototype.__init47.call(this);;
8752
8862
  this.model = config.model || "";
8753
8863
  this.instruction = config.instruction || "";
8754
8864
  this.globalInstruction = config.globalInstruction || "";
@@ -8767,6 +8877,11 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8767
8877
  this.generateContentConfig = config.generateContentConfig;
8768
8878
  this.inputSchema = config.inputSchema;
8769
8879
  this.outputSchema = config.outputSchema;
8880
+ this.beforeModelCallback = config.beforeModelCallback;
8881
+ this.afterModelCallback = config.afterModelCallback;
8882
+ this.beforeToolCallback = config.beforeToolCallback;
8883
+ this.afterToolCallback = config.afterToolCallback;
8884
+ this.validateOutputSchemaConfig();
8770
8885
  }
8771
8886
  /**
8772
8887
  * The resolved model field as BaseLLM
@@ -8783,13 +8898,15 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8783
8898
  return new AiSdkLlm(this.model);
8784
8899
  }
8785
8900
  let ancestorAgent = this.parentAgent;
8786
- while (ancestorAgent !== null) {
8901
+ while (ancestorAgent !== null && ancestorAgent !== void 0) {
8787
8902
  if (ancestorAgent instanceof _LlmAgent) {
8788
8903
  return ancestorAgent.canonicalModel;
8789
8904
  }
8790
8905
  ancestorAgent = ancestorAgent.parentAgent;
8791
8906
  }
8792
- throw new Error(`No model found for ${this.name}.`);
8907
+ throw new Error(
8908
+ `No model found for agent "${this.name}". Please specify a model directly on this agent using the 'model' property`
8909
+ );
8793
8910
  }
8794
8911
  /**
8795
8912
  * The resolved instruction field to construct instruction for this agent
@@ -8829,6 +8946,80 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8829
8946
  }
8830
8947
  return resolvedTools;
8831
8948
  }
8949
+ /**
8950
+ * Gets the canonical before model callbacks as an array
8951
+ */
8952
+ get canonicalBeforeModelCallbacks() {
8953
+ if (!this.beforeModelCallback) {
8954
+ return [];
8955
+ }
8956
+ if (Array.isArray(this.beforeModelCallback)) {
8957
+ return this.beforeModelCallback;
8958
+ }
8959
+ return [this.beforeModelCallback];
8960
+ }
8961
+ /**
8962
+ * Gets the canonical after model callbacks as an array
8963
+ */
8964
+ get canonicalAfterModelCallbacks() {
8965
+ if (!this.afterModelCallback) {
8966
+ return [];
8967
+ }
8968
+ if (Array.isArray(this.afterModelCallback)) {
8969
+ return this.afterModelCallback;
8970
+ }
8971
+ return [this.afterModelCallback];
8972
+ }
8973
+ /**
8974
+ * Gets the canonical before tool callbacks as an array
8975
+ */
8976
+ get canonicalBeforeToolCallbacks() {
8977
+ if (!this.beforeToolCallback) {
8978
+ return [];
8979
+ }
8980
+ if (Array.isArray(this.beforeToolCallback)) {
8981
+ return this.beforeToolCallback;
8982
+ }
8983
+ return [this.beforeToolCallback];
8984
+ }
8985
+ /**
8986
+ * Gets the canonical after tool callbacks as an array
8987
+ */
8988
+ get canonicalAfterToolCallbacks() {
8989
+ if (!this.afterToolCallback) {
8990
+ return [];
8991
+ }
8992
+ if (Array.isArray(this.afterToolCallback)) {
8993
+ return this.afterToolCallback;
8994
+ }
8995
+ return [this.afterToolCallback];
8996
+ }
8997
+ /**
8998
+ * Validates output schema configuration
8999
+ * This matches the Python implementation's __check_output_schema
9000
+ */
9001
+ validateOutputSchemaConfig() {
9002
+ if (!this.outputSchema) {
9003
+ return;
9004
+ }
9005
+ if (!this.disallowTransferToParent || !this.disallowTransferToPeers) {
9006
+ this.logger.warn(
9007
+ `Invalid config for agent ${this.name}: output_schema cannot co-exist with agent transfer configurations. Setting disallow_transfer_to_parent=true, disallow_transfer_to_peers=true`
9008
+ );
9009
+ this.disallowTransferToParent = true;
9010
+ this.disallowTransferToPeers = true;
9011
+ }
9012
+ if (this.subAgents && this.subAgents.length > 0) {
9013
+ throw new Error(
9014
+ `Invalid config for agent ${this.name}: if output_schema is set, sub_agents must be empty to disable agent transfer.`
9015
+ );
9016
+ }
9017
+ if (this.tools && this.tools.length > 0) {
9018
+ throw new Error(
9019
+ `Invalid config for agent ${this.name}: if output_schema is set, tools must be empty`
9020
+ );
9021
+ }
9022
+ }
8832
9023
  /**
8833
9024
  * Gets the appropriate LLM flow for this agent
8834
9025
  * This matches the Python implementation's _llm_flow property
@@ -8844,8 +9035,28 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8844
9035
  * This matches the Python implementation's __maybe_save_output_to_state
8845
9036
  */
8846
9037
  maybeSaveOutputToState(event) {
9038
+ if (event.author !== this.name) {
9039
+ this.logger.debug(
9040
+ `Skipping output save for agent ${this.name}: event authored by ${event.author}`
9041
+ );
9042
+ return;
9043
+ }
8847
9044
  if (this.outputKey && event.isFinalResponse() && _optionalChain([event, 'access', _238 => _238.content, 'optionalAccess', _239 => _239.parts])) {
8848
- const result = event.content.parts.map((part) => part.text || "").join("");
9045
+ let result = event.content.parts.map((part) => part.text || "").join("");
9046
+ if (this.outputSchema) {
9047
+ if (!result.trim()) {
9048
+ return;
9049
+ }
9050
+ try {
9051
+ const parsed = JSON.parse(result);
9052
+ result = this.outputSchema.parse(parsed);
9053
+ } catch (error) {
9054
+ this.logger.error("Failed to validate output with schema:", error);
9055
+ throw new Error(
9056
+ `Output validation failed: ${error instanceof Error ? error.message : String(error)}`
9057
+ );
9058
+ }
9059
+ }
8849
9060
  if (result) {
8850
9061
  if (!event.actions.stateDelta) {
8851
9062
  event.actions.stateDelta = {};
@@ -8884,7 +9095,7 @@ var LlmAgent = (_class26 = class _LlmAgent extends BaseAgent {
8884
9095
  yield errorEvent;
8885
9096
  }
8886
9097
  }
8887
- }, _class26);
9098
+ }, _class27);
8888
9099
 
8889
9100
  // src/agents/sequential-agent.ts
8890
9101
  var SequentialAgent = class extends BaseAgent {
@@ -9076,7 +9287,7 @@ var LoopAgent = class extends BaseAgent {
9076
9287
 
9077
9288
  // src/agents/lang-graph-agent.ts
9078
9289
  init_logger();
9079
- var LangGraphAgent = (_class27 = class extends BaseAgent {
9290
+ var LangGraphAgent = (_class28 = class extends BaseAgent {
9080
9291
  /**
9081
9292
  * Graph nodes (agents and their connections)
9082
9293
  */
@@ -9092,8 +9303,8 @@ var LangGraphAgent = (_class27 = class extends BaseAgent {
9092
9303
  /**
9093
9304
  * Results from node executions
9094
9305
  */
9095
- __init47() {this.results = []}
9096
- __init48() {this.logger = new Logger({ name: "LangGraphAgent" })}
9306
+ __init48() {this.results = []}
9307
+ __init49() {this.logger = new Logger({ name: "LangGraphAgent" })}
9097
9308
  /**
9098
9309
  * Constructor for LangGraphAgent
9099
9310
  */
@@ -9101,7 +9312,7 @@ var LangGraphAgent = (_class27 = class extends BaseAgent {
9101
9312
  super({
9102
9313
  name: config.name,
9103
9314
  description: config.description
9104
- });_class27.prototype.__init47.call(this);_class27.prototype.__init48.call(this);;
9315
+ });_class28.prototype.__init48.call(this);_class28.prototype.__init49.call(this);;
9105
9316
  this.nodes = /* @__PURE__ */ new Map();
9106
9317
  for (const node of config.nodes) {
9107
9318
  if (this.nodes.has(node.name)) {
@@ -9297,7 +9508,7 @@ var LangGraphAgent = (_class27 = class extends BaseAgent {
9297
9508
  }
9298
9509
  this.maxSteps = maxSteps;
9299
9510
  }
9300
- }, _class27);
9511
+ }, _class28);
9301
9512
 
9302
9513
  // src/agents/agent-builder.ts
9303
9514
 
@@ -9400,8 +9611,8 @@ var RunConfig = class {
9400
9611
  };
9401
9612
 
9402
9613
  // src/artifacts/in-memory-artifact-service.ts
9403
- var InMemoryArtifactService = (_class28 = class {constructor() { _class28.prototype.__init49.call(this); }
9404
- __init49() {this.artifacts = /* @__PURE__ */ new Map()}
9614
+ var InMemoryArtifactService = (_class29 = class {constructor() { _class29.prototype.__init50.call(this); }
9615
+ __init50() {this.artifacts = /* @__PURE__ */ new Map()}
9405
9616
  fileHasUserNamespace(filename) {
9406
9617
  return filename.startsWith("user:");
9407
9618
  }
@@ -9474,7 +9685,7 @@ var InMemoryArtifactService = (_class28 = class {constructor() { _class28.protot
9474
9685
  }
9475
9686
  return Array.from({ length: versions.length }, (_, i) => i);
9476
9687
  }
9477
- }, _class28);
9688
+ }, _class29);
9478
9689
 
9479
9690
  // src/runners.ts
9480
9691
  init_logger();
@@ -9501,15 +9712,15 @@ function _extractWordsLower(text) {
9501
9712
  const words = text.match(/[A-Za-z]+/g) || [];
9502
9713
  return new Set(words.map((word) => word.toLowerCase()));
9503
9714
  }
9504
- var InMemoryMemoryService = (_class29 = class {
9715
+ var InMemoryMemoryService = (_class30 = class {
9505
9716
  /**
9506
9717
  * Keys are app_name/user_id, session_id. Values are session event lists.
9507
9718
  */
9508
- __init50() {this._sessionEvents = /* @__PURE__ */ new Map()}
9719
+ __init51() {this._sessionEvents = /* @__PURE__ */ new Map()}
9509
9720
  /**
9510
9721
  * Constructor for InMemoryMemoryService
9511
9722
  */
9512
- constructor() {;_class29.prototype.__init50.call(this);
9723
+ constructor() {;_class30.prototype.__init51.call(this);
9513
9724
  this._sessionEvents = /* @__PURE__ */ new Map();
9514
9725
  }
9515
9726
  /**
@@ -9593,7 +9804,7 @@ var InMemoryMemoryService = (_class29 = class {
9593
9804
  clear() {
9594
9805
  this._sessionEvents.clear();
9595
9806
  }
9596
- }, _class29);
9807
+ }, _class30);
9597
9808
 
9598
9809
  // src/sessions/in-memory-session-service.ts
9599
9810
  var _crypto = require('crypto');
@@ -9635,19 +9846,19 @@ var BaseSessionService = class {
9635
9846
  };
9636
9847
 
9637
9848
  // src/sessions/in-memory-session-service.ts
9638
- var InMemorySessionService = (_class30 = class extends BaseSessionService {constructor(...args2) { super(...args2); _class30.prototype.__init51.call(this);_class30.prototype.__init52.call(this);_class30.prototype.__init53.call(this); }
9849
+ var InMemorySessionService = (_class31 = class extends BaseSessionService {constructor(...args3) { super(...args3); _class31.prototype.__init52.call(this);_class31.prototype.__init53.call(this);_class31.prototype.__init54.call(this); }
9639
9850
  /**
9640
9851
  * A map from app name to a map from user ID to a map from session ID to session.
9641
9852
  */
9642
- __init51() {this.sessions = /* @__PURE__ */ new Map()}
9853
+ __init52() {this.sessions = /* @__PURE__ */ new Map()}
9643
9854
  /**
9644
9855
  * A map from app name to a map from user ID to a map from key to the value.
9645
9856
  */
9646
- __init52() {this.userState = /* @__PURE__ */ new Map()}
9857
+ __init53() {this.userState = /* @__PURE__ */ new Map()}
9647
9858
  /**
9648
9859
  * A map from app name to a map from key to the value.
9649
9860
  */
9650
- __init53() {this.appState = /* @__PURE__ */ new Map()}
9861
+ __init54() {this.appState = /* @__PURE__ */ new Map()}
9651
9862
  /**
9652
9863
  * Creates a new session.
9653
9864
  */
@@ -9844,7 +10055,7 @@ var InMemorySessionService = (_class30 = class extends BaseSessionService {const
9844
10055
  storageSession.lastUpdateTime = event.timestamp;
9845
10056
  return event;
9846
10057
  }
9847
- }, _class30);
10058
+ }, _class31);
9848
10059
 
9849
10060
  // src/runners.ts
9850
10061
  function _findFunctionCallEventIfLastEventIsFunctionResponse(session) {
@@ -9870,7 +10081,7 @@ function _findFunctionCallEventIfLastEventIsFunctionResponse(session) {
9870
10081
  }
9871
10082
  return null;
9872
10083
  }
9873
- var Runner = (_class31 = class {
10084
+ var Runner = (_class32 = class {
9874
10085
  /**
9875
10086
  * The app name of the runner.
9876
10087
  */
@@ -9891,7 +10102,7 @@ var Runner = (_class31 = class {
9891
10102
  * The memory service for the runner.
9892
10103
  */
9893
10104
 
9894
- __init54() {this.logger = new Logger({ name: "Runner" })}
10105
+ __init55() {this.logger = new Logger({ name: "Runner" })}
9895
10106
  /**
9896
10107
  * Initializes the Runner.
9897
10108
  */
@@ -9901,7 +10112,7 @@ var Runner = (_class31 = class {
9901
10112
  artifactService,
9902
10113
  sessionService,
9903
10114
  memoryService
9904
- }) {;_class31.prototype.__init54.call(this);
10115
+ }) {;_class32.prototype.__init55.call(this);
9905
10116
  this.appName = appName;
9906
10117
  this.agent = agent;
9907
10118
  this.artifactService = artifactService;
@@ -10108,7 +10319,7 @@ var Runner = (_class31 = class {
10108
10319
  runConfig
10109
10320
  });
10110
10321
  }
10111
- }, _class31);
10322
+ }, _class32);
10112
10323
  var InMemoryRunner = class extends Runner {
10113
10324
  /**
10114
10325
  * Deprecated. Please don't use. The in-memory session service for the runner.
@@ -10131,18 +10342,18 @@ var InMemoryRunner = class extends Runner {
10131
10342
  };
10132
10343
 
10133
10344
  // src/agents/agent-builder.ts
10134
- var AgentBuilder = (_class32 = class _AgentBuilder {
10345
+ var AgentBuilder = (_class33 = class _AgentBuilder {
10135
10346
 
10136
10347
 
10137
10348
 
10138
10349
 
10139
10350
 
10140
- __init55() {this.agentType = "llm"}
10351
+ __init56() {this.agentType = "llm"}
10141
10352
 
10142
10353
  /**
10143
10354
  * Private constructor - use static create() method
10144
10355
  */
10145
- constructor(name) {;_class32.prototype.__init55.call(this);
10356
+ constructor(name) {;_class33.prototype.__init56.call(this);
10146
10357
  this.config = { name };
10147
10358
  }
10148
10359
  /**
@@ -10188,6 +10399,14 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
10188
10399
  this.config.instruction = instruction;
10189
10400
  return this;
10190
10401
  }
10402
+ withInputSchema(schema) {
10403
+ this.config.inputSchema = schema;
10404
+ return this;
10405
+ }
10406
+ withOutputSchema(schema) {
10407
+ this.config.outputSchema = schema;
10408
+ return this;
10409
+ }
10191
10410
  /**
10192
10411
  * Add tools to the agent
10193
10412
  * @param tools Tools to add to the agent
@@ -10394,6 +10613,14 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
10394
10613
  }
10395
10614
  return { agent, runner, session };
10396
10615
  }
10616
+ /**
10617
+ * Type-safe build method for agents with output schemas
10618
+ * Provides better type inference for the ask method return type
10619
+ */
10620
+ async buildWithSchema() {
10621
+ const result = await this.build();
10622
+ return result;
10623
+ }
10397
10624
  /**
10398
10625
  * Quick execution helper - build and run a message
10399
10626
  * @param message Message to send to the agent (string or full message object)
@@ -10428,7 +10655,9 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
10428
10655
  memoryService: this.memoryService,
10429
10656
  artifactService: this.artifactService,
10430
10657
  outputKey: this.config.outputKey,
10431
- sessionService: this.sessionService
10658
+ sessionService: this.sessionService,
10659
+ inputSchema: this.config.inputSchema,
10660
+ outputSchema: this.config.outputSchema
10432
10661
  });
10433
10662
  }
10434
10663
  case "sequential":
@@ -10487,14 +10716,16 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
10487
10716
  return `app-${this.config.name}`;
10488
10717
  }
10489
10718
  /**
10490
- * Create enhanced runner with simplified API
10719
+ * Create enhanced runner with simplified API and proper typing
10491
10720
  * @param baseRunner The base runner instance
10492
10721
  * @param session The session instance
10493
10722
  * @returns Enhanced runner with simplified API
10494
10723
  */
10495
10724
  createEnhancedRunner(baseRunner, session) {
10496
10725
  const sessionOptions = this.sessionOptions;
10726
+ const outputSchema = this.config.outputSchema;
10497
10727
  return {
10728
+ __outputSchema: outputSchema,
10498
10729
  async ask(message) {
10499
10730
  const newMessage = typeof message === "string" ? { parts: [{ text: message }] } : typeof message === "object" && "contents" in message ? { parts: message.contents[message.contents.length - 1].parts } : message;
10500
10731
  let response = "";
@@ -10515,6 +10746,18 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
10515
10746
  }
10516
10747
  }
10517
10748
  }
10749
+ if (outputSchema) {
10750
+ try {
10751
+ const parsed = JSON.parse(response);
10752
+ return outputSchema.parse(parsed);
10753
+ } catch (parseError) {
10754
+ try {
10755
+ return outputSchema.parse(response);
10756
+ } catch (validationError) {
10757
+ return response.trim();
10758
+ }
10759
+ }
10760
+ }
10518
10761
  return response.trim();
10519
10762
  },
10520
10763
  runAsync(params) {
@@ -10522,7 +10765,7 @@ var AgentBuilder = (_class32 = class _AgentBuilder {
10522
10765
  }
10523
10766
  };
10524
10767
  }
10525
- }, _class32);
10768
+ }, _class33);
10526
10769
 
10527
10770
  // src/memory/index.ts
10528
10771
  var memory_exports = {};
@@ -10856,11 +11099,11 @@ var VertexAiSessionService = class extends BaseSessionService {
10856
11099
 
10857
11100
  // src/sessions/database-session-service.ts
10858
11101
  var _kysely = require('kysely');
10859
- var DatabaseSessionService = (_class33 = class extends BaseSessionService {
11102
+ var DatabaseSessionService = (_class34 = class extends BaseSessionService {
10860
11103
 
10861
- __init56() {this.initialized = false}
11104
+ __init57() {this.initialized = false}
10862
11105
  constructor(config) {
10863
- super();_class33.prototype.__init56.call(this);;
11106
+ super();_class34.prototype.__init57.call(this);;
10864
11107
  this.db = config.db;
10865
11108
  if (!config.skipTableCreation) {
10866
11109
  this.initializeDatabase().catch((error) => {
@@ -10935,7 +11178,7 @@ var DatabaseSessionService = (_class33 = class extends BaseSessionService {
10935
11178
  if (!jsonString) return defaultValue;
10936
11179
  try {
10937
11180
  return JSON.parse(jsonString);
10938
- } catch (e5) {
11181
+ } catch (e6) {
10939
11182
  return defaultValue;
10940
11183
  }
10941
11184
  }
@@ -11254,7 +11497,7 @@ var DatabaseSessionService = (_class33 = class extends BaseSessionService {
11254
11497
  }
11255
11498
  }
11256
11499
  }
11257
- }, _class33);
11500
+ }, _class34);
11258
11501
 
11259
11502
  // src/sessions/database-factories.ts
11260
11503