@hiper2d/ai-agents 0.1.2 → 0.1.3

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.mjs CHANGED
@@ -705,6 +705,14 @@ var ModelQuotaExceededError = class extends ModelError {
705
705
  this.name = "ModelQuotaExceededError";
706
706
  }
707
707
  };
708
+ var ModelInvalidResponseError = class extends ModelError {
709
+ truncated;
710
+ constructor(modelType, detail, truncated = false) {
711
+ super(`${modelType} failed to produce a valid response: ${detail}`, modelType);
712
+ this.name = "ModelInvalidResponseError";
713
+ this.truncated = truncated;
714
+ }
715
+ };
708
716
  var ModelRefusalError = class extends ModelError {
709
717
  constructor(modelType, message = `${modelType} refused to answer (stop_reason: refusal)`) {
710
718
  super(message, modelType);
@@ -1772,18 +1780,37 @@ var Gpt5Agent = class extends AbstractAgent {
1772
1780
  thinking: z2.string().describe("Your internal chain-of-thought reasoning process used to arrive at the final answer.")
1773
1781
  });
1774
1782
  }
1775
- const response = await this.client.responses.parse({
1776
- model: this.model,
1777
- instructions: this.instruction,
1778
- input,
1779
- max_output_tokens: this.maxOutputTokens,
1780
- text: {
1781
- format: zodTextFormat(schemaToSend, "response_schema")
1783
+ let response;
1784
+ try {
1785
+ response = await this.client.responses.parse({
1786
+ model: this.model,
1787
+ instructions: this.instruction,
1788
+ input,
1789
+ max_output_tokens: this.maxOutputTokens,
1790
+ text: {
1791
+ format: zodTextFormat(schemaToSend, "response_schema")
1792
+ }
1793
+ });
1794
+ } catch (error) {
1795
+ if (error instanceof SyntaxError) {
1796
+ throw new ModelInvalidResponseError(
1797
+ this.model,
1798
+ `malformed JSON output \u2014 the generation was cut off at the ${this.maxOutputTokens}-token output cap or went off the rails (${error.message})`
1799
+ );
1782
1800
  }
1783
- });
1801
+ throw error;
1802
+ }
1803
+ if (response.status === "incomplete") {
1804
+ const reason = response.incomplete_details?.reason ?? "unknown";
1805
+ throw new ModelInvalidResponseError(
1806
+ this.model,
1807
+ `response incomplete (${reason}) at max_output_tokens=${this.maxOutputTokens}`,
1808
+ reason === "max_output_tokens"
1809
+ );
1810
+ }
1784
1811
  if (!response.output_parsed) {
1785
1812
  this.logger(`Parsing failed. Raw content: ${response.output_text}`);
1786
- throw new Error(this.errorMessages.invalidFormat);
1813
+ throw new ModelInvalidResponseError(this.model, this.errorMessages.invalidFormat);
1787
1814
  }
1788
1815
  let reasoningContent = "";
1789
1816
  if (this.enableThinking && response.output_parsed.thinking) {
@@ -1822,6 +1849,9 @@ var Gpt5Agent = class extends AbstractAgent {
1822
1849
  return [response.output_parsed, reasoningContent, tokenUsage];
1823
1850
  } catch (error) {
1824
1851
  this.logger(this.logTemplates.error(this.name, error));
1852
+ if (error instanceof ModelError) {
1853
+ throw error;
1854
+ }
1825
1855
  throw new Error(this.errorMessages.apiError(error));
1826
1856
  }
1827
1857
  }
@@ -1847,6 +1877,14 @@ var Gpt5Agent = class extends AbstractAgent {
1847
1877
  });
1848
1878
  const content = response.output_text;
1849
1879
  if (!content) {
1880
+ if (response.status === "incomplete") {
1881
+ const reason = response.incomplete_details?.reason ?? "unknown";
1882
+ throw new ModelInvalidResponseError(
1883
+ this.model,
1884
+ `empty response, incomplete (${reason}) at max_output_tokens=${this.maxOutputTokens}`,
1885
+ reason === "max_output_tokens"
1886
+ );
1887
+ }
1850
1888
  throw new Error(this.errorMessages.emptyResponse);
1851
1889
  }
1852
1890
  let tokenUsage;
@@ -1879,6 +1917,9 @@ var Gpt5Agent = class extends AbstractAgent {
1879
1917
  return [content, "", tokenUsage];
1880
1918
  } catch (error) {
1881
1919
  this.logger(this.logTemplates.error(this.name, error));
1920
+ if (error instanceof ModelError) {
1921
+ throw error;
1922
+ }
1882
1923
  throw new Error(this.errorMessages.apiError(error));
1883
1924
  }
1884
1925
  }
@@ -4294,6 +4335,7 @@ export {
4294
4335
  MistralAgent,
4295
4336
  ModelAuthenticationError,
4296
4337
  ModelError,
4338
+ ModelInvalidResponseError,
4297
4339
  ModelOverloadError,
4298
4340
  ModelQuotaExceededError,
4299
4341
  ModelRateLimitError,