@deepagents/text2sql 0.17.0 → 0.18.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.
package/dist/index.js CHANGED
@@ -510,7 +510,6 @@ var fragments = [
510
510
  hint("Support both natural language questions AND raw SQL input"),
511
511
  hint("When validating user SQL, explain any errors clearly")
512
512
  ];
513
- var developer_agent_default = { tools, fragments };
514
513
 
515
514
  // packages/text2sql/src/lib/agents/result-tools.ts
516
515
  import { tool as tool2 } from "ai";
@@ -802,19 +801,21 @@ async function toSql(options) {
802
801
  const sqlOutput = structuredOutput2({
803
802
  model,
804
803
  context,
805
- schema: z4.union([
806
- z4.object({
807
- sql: z4.string().describe("The SQL query that answers the question"),
808
- reasoning: z4.string().optional().describe("The reasoning steps taken to generate the SQL")
809
- }),
810
- z4.object({
811
- error: z4.string().describe(
812
- "Error message explaining why the question cannot be answered with the given schema"
813
- )
814
- })
815
- ])
804
+ schema: z4.object({
805
+ result: z4.union([
806
+ z4.object({
807
+ sql: z4.string().describe("The SQL query that answers the question"),
808
+ reasoning: z4.string().describe("The reasoning steps taken to generate the SQL")
809
+ }),
810
+ z4.object({
811
+ error: z4.string().describe(
812
+ "Error message explaining why the question cannot be answered with the given schema"
813
+ )
814
+ })
815
+ ])
816
+ })
816
817
  });
817
- const output = await sqlOutput.generate();
818
+ const { result: output } = await sqlOutput.generate();
818
819
  if ("error" in output) {
819
820
  throw new UnanswerableSQLError(output.error);
820
821
  }
@@ -844,6 +845,16 @@ function formatErrorMessage(error) {
844
845
  }
845
846
  return error.message;
846
847
  }
848
+ function isModelUnavailableError(error) {
849
+ if (!APICallError.isInstance(error)) {
850
+ return false;
851
+ }
852
+ const message2 = error.message.toLowerCase();
853
+ const responseBody = (error.responseBody ?? "").toLowerCase();
854
+ const is404ModelError = error.statusCode === 404 && (message2.includes("model") || responseBody.includes("model_not_found"));
855
+ const errorCode = typeof error.data === "object" && error.data !== null && "error" in error.data && typeof error.data.error === "object" && error.data.error !== null && "code" in error.data.error && typeof error.data.error.code === "string" ? error.data.error.code.toLowerCase() : void 0;
856
+ return is404ModelError || errorCode === "model_not_found" || responseBody.includes('"code":"model_not_found"') || message2.includes("model") && message2.includes("does not exist or you do not have access to it");
857
+ }
847
858
  async function withRetry(computation, options = { retries: 3 }) {
848
859
  const errors = [];
849
860
  let attempts = 0;
@@ -857,6 +868,9 @@ async function withRetry(computation, options = { retries: 3 }) {
857
868
  if (UnanswerableSQLError.isInstance(context.error)) {
858
869
  return false;
859
870
  }
871
+ if (isModelUnavailableError(context.error)) {
872
+ return false;
873
+ }
860
874
  if (SQLValidationError.isInstance(context.error)) {
861
875
  return true;
862
876
  }
@@ -4346,15 +4360,24 @@ var Text2Sql = class {
4346
4360
  return producer.toPairs();
4347
4361
  }
4348
4362
  async chat(messages) {
4363
+ if (messages.length === 0) {
4364
+ throw new Error("messages must not be empty");
4365
+ }
4349
4366
  const trackedFs = new TrackedFs(this.#config.filesystem);
4350
4367
  const context = this.#config.context(
4351
4368
  ...guidelines(this.#config.teachingsOptions),
4352
4369
  ...await this.index()
4353
4370
  );
4354
- const userMsg = messages.at(-1);
4355
- if (userMsg) {
4356
- context.set(message(userMsg));
4371
+ const lastMessage = messages[messages.length - 1];
4372
+ let assistantMsgId;
4373
+ if (lastMessage.role === "assistant") {
4374
+ context.set(message(lastMessage));
4375
+ await context.save({ branch: false });
4376
+ assistantMsgId = lastMessage.id;
4377
+ } else {
4378
+ context.set(message(lastMessage));
4357
4379
  await context.save();
4380
+ assistantMsgId = generateId();
4358
4381
  }
4359
4382
  const { mounts: skillMounts } = context.getSkillMounts();
4360
4383
  const { tools: tools2 } = await createResultTools({
@@ -4362,7 +4385,6 @@ var Text2Sql = class {
4362
4385
  skillMounts,
4363
4386
  filesystem: trackedFs
4364
4387
  });
4365
- const assistantMsgId = generateId();
4366
4388
  const chatAgent = agent2({
4367
4389
  name: "text2sql",
4368
4390
  model: this.#config.model,
@@ -4429,40 +4451,6 @@ var Text2Sql = class {
4429
4451
  }
4430
4452
  });
4431
4453
  }
4432
- async developer(messages) {
4433
- const context = this.#config.context(
4434
- ...guidelines(this.#config.teachingsOptions),
4435
- ...developer_agent_default.fragments,
4436
- ...await this.index()
4437
- );
4438
- const userMsg = messages.at(-1);
4439
- if (userMsg) {
4440
- context.set(message(userMsg));
4441
- await context.save();
4442
- }
4443
- const developerAgent = agent2({
4444
- name: "developer",
4445
- model: this.#config.model,
4446
- context,
4447
- tools: developer_agent_default.tools
4448
- });
4449
- const result = await developerAgent.stream({
4450
- adapter: this.#config.adapter
4451
- });
4452
- return result.toUIMessageStream({
4453
- onError: (error) => this.#formatError(error),
4454
- sendStart: true,
4455
- sendFinish: true,
4456
- sendReasoning: true,
4457
- sendSources: true,
4458
- generateMessageId: generateId,
4459
- onFinish: async ({ responseMessage }) => {
4460
- context.set(assistant(responseMessage));
4461
- await context.save();
4462
- await context.trackUsage(await result.totalUsage);
4463
- }
4464
- });
4465
- }
4466
4454
  #formatError(error) {
4467
4455
  if (NoSuchToolError.isInstance(error)) {
4468
4456
  return "The model tried to call an unknown tool.";