@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.d.mts CHANGED
@@ -257,6 +257,19 @@ declare class ModelAuthenticationError extends ModelError {
257
257
  declare class ModelQuotaExceededError extends ModelError {
258
258
  constructor(message: string, modelType: string);
259
259
  }
260
+ /**
261
+ * The model failed to produce a valid response: the output was malformed (unparseable
262
+ * JSON from a structured-output ask) or cut off at the output-token cap before the
263
+ * answer was complete (`status: "incomplete"` on the OpenAI Responses API). Not a
264
+ * transport or availability problem — the request worked, the generation went wrong.
265
+ * A retry with the same prompt often succeeds; `truncated` distinguishes a cap hit
266
+ * (raise maxOutputTokens if legitimate responses genuinely need more room) from a
267
+ * degenerate/runaway generation (a bigger cap only makes failures slower).
268
+ */
269
+ declare class ModelInvalidResponseError extends ModelError {
270
+ truncated: boolean;
271
+ constructor(modelType: string, detail: string, truncated?: boolean);
272
+ }
260
273
  /**
261
274
  * The model declined to answer: Anthropic returns `stop_reason: "refusal"` with no content
262
275
  * blocks when its safety layer rejects the request as a whole. Not retryable as-is — the
@@ -1188,4 +1201,4 @@ declare class MiniMaxAgent extends AbstractAgent {
1188
1201
  doAskText(messages: AIMessage[]): Promise<[string, string, TokenUsage$1?, string?]>;
1189
1202
  }
1190
1203
 
1191
- export { type AIMessage, ANTHROPIC_REASONING_EFFORTS, API_KEY_CONSTANTS, AbstractAgent, type AgentActivityData, AgentFactory, type AgentLoggingConfig, type AnthropicReasoningEffort, type AnthropicTokenUsage, type ApiKeyMap, BotResponseError, CACHE_TIER_MARKER, ClaudeAgent, type CostCalculationOptions, DEEPSEEK_PEAK_SCHEDULE, DEEPSEEK_REASONING_EFFORTS, DEFAULT_LOGGING_CONFIG, DEFAULT_MAX_OUTPUT_TOKENS, type DeepSeekReasoningEffort, type DeepSeekTokenUsage, DeepSeekV2Agent, FUGU_REASONING_EFFORTS, FuguAgent, type FuguReasoningEffort, GEMINI_REASONING_EFFORTS, GLM_REASONING_EFFORTS, type GeminiReasoningEffort, GlmAgent, type GlmReasoningEffort, GoogleAgent, type GoogleTokenUsage, Gpt5Agent, GrokAgent, type GrokTokenUsage, type JsonSchemaOptions, KimiAgent, type KimiTokenUsage, type LLMModel, LLM_CONSTANTS, type LlmLogger, type LoggingConfig, MESSAGE_ROLE, MODEL_PRICING, MiniMaxAgent, MistralAgent, type MistralTokenUsage, type Modality, ModelAuthenticationError, type ModelConfig, ModelError, ModelOverloadError, type ModelPricing, ModelQuotaExceededError, ModelRateLimitError, ModelRefusalError, type ModelTag, ModelUnavailableError, OPENAI_REASONING_EFFORTS, type OpenAIReasoningEffort, type OpenAITokenUsage, type PeakPricing, type PricingUnit, type ProviderSchema, type TokenUsage as ProviderTokenUsage, type ProviderType, QwenAgent, REASONING_EFFORT_SCALE, type ReasoningEffort, SupportedAiKeyNames, SupportedAiModels, type TokenUsage$1 as TokenUsage, ZodSchemaConverter, calculateAnthropicCost, calculateCost, calculateDeepSeekCost, calculateGoogleCost, calculateGrokCost, calculateKimiCost, calculateMistralCost, calculateModelCost, calculateOpenAICost, clampReasoningEffort, cleanResponse, createCatalog, extractAnthropicTokenUsage, extractTokenUsageFromResponse$2 as extractAnthropicTokenUsageFromResponse, extractDeepSeekTokenUsage, extractTokenUsageFromResponse$5 as extractDeepSeekTokenUsageFromResponse, extractFirstJsonObject, extractGoogleTokenUsage, extractTokenUsageFromResponse$1 as extractGoogleTokenUsageFromResponse, extractGrokTokenUsage, extractTokenUsageFromResponse$3 as extractGrokTokenUsageFromResponse, extractKimiTokenUsage, extractTokenUsageFromResponse$4 as extractKimiTokenUsageFromResponse, extractMistralTokenUsage, extractTokenUsageFromResponse as extractMistralTokenUsageFromResponse, extractOpenAITokenUsage, extractTokenUsageFromResponse$6 as extractOpenAITokenUsageFromResponse, extractTokenUsage, extractUsageAndCalculateCost, generateSchemaInstructions, getModelConfigByApiName, getModelDisplayName, getModelProviderName, getModelTags, getProviderSignatureFields, isHybridThinkingModel, isInPeakWindow, isPeakBilling, isWeekendAt, logger, mergeThinking, modelHasTag, modelIsFast, needsPromptBasedSchema, parseAndValidateLlmJson, safeValidateResponse, setLlmLogger, stableHashHex, stripInlineThinking, supportsNativeJsonSchema, toAnthropicEffort, toDeepSeekEffort, toFuguEffort, toGeminiEffort, toGlmEffort, toOpenAIEffort, validateResponse };
1204
+ export { type AIMessage, ANTHROPIC_REASONING_EFFORTS, API_KEY_CONSTANTS, AbstractAgent, type AgentActivityData, AgentFactory, type AgentLoggingConfig, type AnthropicReasoningEffort, type AnthropicTokenUsage, type ApiKeyMap, BotResponseError, CACHE_TIER_MARKER, ClaudeAgent, type CostCalculationOptions, DEEPSEEK_PEAK_SCHEDULE, DEEPSEEK_REASONING_EFFORTS, DEFAULT_LOGGING_CONFIG, DEFAULT_MAX_OUTPUT_TOKENS, type DeepSeekReasoningEffort, type DeepSeekTokenUsage, DeepSeekV2Agent, FUGU_REASONING_EFFORTS, FuguAgent, type FuguReasoningEffort, GEMINI_REASONING_EFFORTS, GLM_REASONING_EFFORTS, type GeminiReasoningEffort, GlmAgent, type GlmReasoningEffort, GoogleAgent, type GoogleTokenUsage, Gpt5Agent, GrokAgent, type GrokTokenUsage, type JsonSchemaOptions, KimiAgent, type KimiTokenUsage, type LLMModel, LLM_CONSTANTS, type LlmLogger, type LoggingConfig, MESSAGE_ROLE, MODEL_PRICING, MiniMaxAgent, MistralAgent, type MistralTokenUsage, type Modality, ModelAuthenticationError, type ModelConfig, ModelError, ModelInvalidResponseError, ModelOverloadError, type ModelPricing, ModelQuotaExceededError, ModelRateLimitError, ModelRefusalError, type ModelTag, ModelUnavailableError, OPENAI_REASONING_EFFORTS, type OpenAIReasoningEffort, type OpenAITokenUsage, type PeakPricing, type PricingUnit, type ProviderSchema, type TokenUsage as ProviderTokenUsage, type ProviderType, QwenAgent, REASONING_EFFORT_SCALE, type ReasoningEffort, SupportedAiKeyNames, SupportedAiModels, type TokenUsage$1 as TokenUsage, ZodSchemaConverter, calculateAnthropicCost, calculateCost, calculateDeepSeekCost, calculateGoogleCost, calculateGrokCost, calculateKimiCost, calculateMistralCost, calculateModelCost, calculateOpenAICost, clampReasoningEffort, cleanResponse, createCatalog, extractAnthropicTokenUsage, extractTokenUsageFromResponse$2 as extractAnthropicTokenUsageFromResponse, extractDeepSeekTokenUsage, extractTokenUsageFromResponse$5 as extractDeepSeekTokenUsageFromResponse, extractFirstJsonObject, extractGoogleTokenUsage, extractTokenUsageFromResponse$1 as extractGoogleTokenUsageFromResponse, extractGrokTokenUsage, extractTokenUsageFromResponse$3 as extractGrokTokenUsageFromResponse, extractKimiTokenUsage, extractTokenUsageFromResponse$4 as extractKimiTokenUsageFromResponse, extractMistralTokenUsage, extractTokenUsageFromResponse as extractMistralTokenUsageFromResponse, extractOpenAITokenUsage, extractTokenUsageFromResponse$6 as extractOpenAITokenUsageFromResponse, extractTokenUsage, extractUsageAndCalculateCost, generateSchemaInstructions, getModelConfigByApiName, getModelDisplayName, getModelProviderName, getModelTags, getProviderSignatureFields, isHybridThinkingModel, isInPeakWindow, isPeakBilling, isWeekendAt, logger, mergeThinking, modelHasTag, modelIsFast, needsPromptBasedSchema, parseAndValidateLlmJson, safeValidateResponse, setLlmLogger, stableHashHex, stripInlineThinking, supportsNativeJsonSchema, toAnthropicEffort, toDeepSeekEffort, toFuguEffort, toGeminiEffort, toGlmEffort, toOpenAIEffort, validateResponse };
package/dist/index.d.ts CHANGED
@@ -257,6 +257,19 @@ declare class ModelAuthenticationError extends ModelError {
257
257
  declare class ModelQuotaExceededError extends ModelError {
258
258
  constructor(message: string, modelType: string);
259
259
  }
260
+ /**
261
+ * The model failed to produce a valid response: the output was malformed (unparseable
262
+ * JSON from a structured-output ask) or cut off at the output-token cap before the
263
+ * answer was complete (`status: "incomplete"` on the OpenAI Responses API). Not a
264
+ * transport or availability problem — the request worked, the generation went wrong.
265
+ * A retry with the same prompt often succeeds; `truncated` distinguishes a cap hit
266
+ * (raise maxOutputTokens if legitimate responses genuinely need more room) from a
267
+ * degenerate/runaway generation (a bigger cap only makes failures slower).
268
+ */
269
+ declare class ModelInvalidResponseError extends ModelError {
270
+ truncated: boolean;
271
+ constructor(modelType: string, detail: string, truncated?: boolean);
272
+ }
260
273
  /**
261
274
  * The model declined to answer: Anthropic returns `stop_reason: "refusal"` with no content
262
275
  * blocks when its safety layer rejects the request as a whole. Not retryable as-is — the
@@ -1188,4 +1201,4 @@ declare class MiniMaxAgent extends AbstractAgent {
1188
1201
  doAskText(messages: AIMessage[]): Promise<[string, string, TokenUsage$1?, string?]>;
1189
1202
  }
1190
1203
 
1191
- export { type AIMessage, ANTHROPIC_REASONING_EFFORTS, API_KEY_CONSTANTS, AbstractAgent, type AgentActivityData, AgentFactory, type AgentLoggingConfig, type AnthropicReasoningEffort, type AnthropicTokenUsage, type ApiKeyMap, BotResponseError, CACHE_TIER_MARKER, ClaudeAgent, type CostCalculationOptions, DEEPSEEK_PEAK_SCHEDULE, DEEPSEEK_REASONING_EFFORTS, DEFAULT_LOGGING_CONFIG, DEFAULT_MAX_OUTPUT_TOKENS, type DeepSeekReasoningEffort, type DeepSeekTokenUsage, DeepSeekV2Agent, FUGU_REASONING_EFFORTS, FuguAgent, type FuguReasoningEffort, GEMINI_REASONING_EFFORTS, GLM_REASONING_EFFORTS, type GeminiReasoningEffort, GlmAgent, type GlmReasoningEffort, GoogleAgent, type GoogleTokenUsage, Gpt5Agent, GrokAgent, type GrokTokenUsage, type JsonSchemaOptions, KimiAgent, type KimiTokenUsage, type LLMModel, LLM_CONSTANTS, type LlmLogger, type LoggingConfig, MESSAGE_ROLE, MODEL_PRICING, MiniMaxAgent, MistralAgent, type MistralTokenUsage, type Modality, ModelAuthenticationError, type ModelConfig, ModelError, ModelOverloadError, type ModelPricing, ModelQuotaExceededError, ModelRateLimitError, ModelRefusalError, type ModelTag, ModelUnavailableError, OPENAI_REASONING_EFFORTS, type OpenAIReasoningEffort, type OpenAITokenUsage, type PeakPricing, type PricingUnit, type ProviderSchema, type TokenUsage as ProviderTokenUsage, type ProviderType, QwenAgent, REASONING_EFFORT_SCALE, type ReasoningEffort, SupportedAiKeyNames, SupportedAiModels, type TokenUsage$1 as TokenUsage, ZodSchemaConverter, calculateAnthropicCost, calculateCost, calculateDeepSeekCost, calculateGoogleCost, calculateGrokCost, calculateKimiCost, calculateMistralCost, calculateModelCost, calculateOpenAICost, clampReasoningEffort, cleanResponse, createCatalog, extractAnthropicTokenUsage, extractTokenUsageFromResponse$2 as extractAnthropicTokenUsageFromResponse, extractDeepSeekTokenUsage, extractTokenUsageFromResponse$5 as extractDeepSeekTokenUsageFromResponse, extractFirstJsonObject, extractGoogleTokenUsage, extractTokenUsageFromResponse$1 as extractGoogleTokenUsageFromResponse, extractGrokTokenUsage, extractTokenUsageFromResponse$3 as extractGrokTokenUsageFromResponse, extractKimiTokenUsage, extractTokenUsageFromResponse$4 as extractKimiTokenUsageFromResponse, extractMistralTokenUsage, extractTokenUsageFromResponse as extractMistralTokenUsageFromResponse, extractOpenAITokenUsage, extractTokenUsageFromResponse$6 as extractOpenAITokenUsageFromResponse, extractTokenUsage, extractUsageAndCalculateCost, generateSchemaInstructions, getModelConfigByApiName, getModelDisplayName, getModelProviderName, getModelTags, getProviderSignatureFields, isHybridThinkingModel, isInPeakWindow, isPeakBilling, isWeekendAt, logger, mergeThinking, modelHasTag, modelIsFast, needsPromptBasedSchema, parseAndValidateLlmJson, safeValidateResponse, setLlmLogger, stableHashHex, stripInlineThinking, supportsNativeJsonSchema, toAnthropicEffort, toDeepSeekEffort, toFuguEffort, toGeminiEffort, toGlmEffort, toOpenAIEffort, validateResponse };
1204
+ export { type AIMessage, ANTHROPIC_REASONING_EFFORTS, API_KEY_CONSTANTS, AbstractAgent, type AgentActivityData, AgentFactory, type AgentLoggingConfig, type AnthropicReasoningEffort, type AnthropicTokenUsage, type ApiKeyMap, BotResponseError, CACHE_TIER_MARKER, ClaudeAgent, type CostCalculationOptions, DEEPSEEK_PEAK_SCHEDULE, DEEPSEEK_REASONING_EFFORTS, DEFAULT_LOGGING_CONFIG, DEFAULT_MAX_OUTPUT_TOKENS, type DeepSeekReasoningEffort, type DeepSeekTokenUsage, DeepSeekV2Agent, FUGU_REASONING_EFFORTS, FuguAgent, type FuguReasoningEffort, GEMINI_REASONING_EFFORTS, GLM_REASONING_EFFORTS, type GeminiReasoningEffort, GlmAgent, type GlmReasoningEffort, GoogleAgent, type GoogleTokenUsage, Gpt5Agent, GrokAgent, type GrokTokenUsage, type JsonSchemaOptions, KimiAgent, type KimiTokenUsage, type LLMModel, LLM_CONSTANTS, type LlmLogger, type LoggingConfig, MESSAGE_ROLE, MODEL_PRICING, MiniMaxAgent, MistralAgent, type MistralTokenUsage, type Modality, ModelAuthenticationError, type ModelConfig, ModelError, ModelInvalidResponseError, ModelOverloadError, type ModelPricing, ModelQuotaExceededError, ModelRateLimitError, ModelRefusalError, type ModelTag, ModelUnavailableError, OPENAI_REASONING_EFFORTS, type OpenAIReasoningEffort, type OpenAITokenUsage, type PeakPricing, type PricingUnit, type ProviderSchema, type TokenUsage as ProviderTokenUsage, type ProviderType, QwenAgent, REASONING_EFFORT_SCALE, type ReasoningEffort, SupportedAiKeyNames, SupportedAiModels, type TokenUsage$1 as TokenUsage, ZodSchemaConverter, calculateAnthropicCost, calculateCost, calculateDeepSeekCost, calculateGoogleCost, calculateGrokCost, calculateKimiCost, calculateMistralCost, calculateModelCost, calculateOpenAICost, clampReasoningEffort, cleanResponse, createCatalog, extractAnthropicTokenUsage, extractTokenUsageFromResponse$2 as extractAnthropicTokenUsageFromResponse, extractDeepSeekTokenUsage, extractTokenUsageFromResponse$5 as extractDeepSeekTokenUsageFromResponse, extractFirstJsonObject, extractGoogleTokenUsage, extractTokenUsageFromResponse$1 as extractGoogleTokenUsageFromResponse, extractGrokTokenUsage, extractTokenUsageFromResponse$3 as extractGrokTokenUsageFromResponse, extractKimiTokenUsage, extractTokenUsageFromResponse$4 as extractKimiTokenUsageFromResponse, extractMistralTokenUsage, extractTokenUsageFromResponse as extractMistralTokenUsageFromResponse, extractOpenAITokenUsage, extractTokenUsageFromResponse$6 as extractOpenAITokenUsageFromResponse, extractTokenUsage, extractUsageAndCalculateCost, generateSchemaInstructions, getModelConfigByApiName, getModelDisplayName, getModelProviderName, getModelTags, getProviderSignatureFields, isHybridThinkingModel, isInPeakWindow, isPeakBilling, isWeekendAt, logger, mergeThinking, modelHasTag, modelIsFast, needsPromptBasedSchema, parseAndValidateLlmJson, safeValidateResponse, setLlmLogger, stableHashHex, stripInlineThinking, supportsNativeJsonSchema, toAnthropicEffort, toDeepSeekEffort, toFuguEffort, toGeminiEffort, toGlmEffort, toOpenAIEffort, validateResponse };
package/dist/index.js CHANGED
@@ -58,6 +58,7 @@ __export(index_exports, {
58
58
  MistralAgent: () => MistralAgent,
59
59
  ModelAuthenticationError: () => ModelAuthenticationError,
60
60
  ModelError: () => ModelError,
61
+ ModelInvalidResponseError: () => ModelInvalidResponseError,
61
62
  ModelOverloadError: () => ModelOverloadError,
62
63
  ModelQuotaExceededError: () => ModelQuotaExceededError,
63
64
  ModelRateLimitError: () => ModelRateLimitError,
@@ -836,6 +837,14 @@ var ModelQuotaExceededError = class extends ModelError {
836
837
  this.name = "ModelQuotaExceededError";
837
838
  }
838
839
  };
840
+ var ModelInvalidResponseError = class extends ModelError {
841
+ truncated;
842
+ constructor(modelType, detail, truncated = false) {
843
+ super(`${modelType} failed to produce a valid response: ${detail}`, modelType);
844
+ this.name = "ModelInvalidResponseError";
845
+ this.truncated = truncated;
846
+ }
847
+ };
839
848
  var ModelRefusalError = class extends ModelError {
840
849
  constructor(modelType, message = `${modelType} refused to answer (stop_reason: refusal)`) {
841
850
  super(message, modelType);
@@ -1903,18 +1912,37 @@ var Gpt5Agent = class extends AbstractAgent {
1903
1912
  thinking: import_zod2.z.string().describe("Your internal chain-of-thought reasoning process used to arrive at the final answer.")
1904
1913
  });
1905
1914
  }
1906
- const response = await this.client.responses.parse({
1907
- model: this.model,
1908
- instructions: this.instruction,
1909
- input,
1910
- max_output_tokens: this.maxOutputTokens,
1911
- text: {
1912
- format: (0, import_zod3.zodTextFormat)(schemaToSend, "response_schema")
1915
+ let response;
1916
+ try {
1917
+ response = await this.client.responses.parse({
1918
+ model: this.model,
1919
+ instructions: this.instruction,
1920
+ input,
1921
+ max_output_tokens: this.maxOutputTokens,
1922
+ text: {
1923
+ format: (0, import_zod3.zodTextFormat)(schemaToSend, "response_schema")
1924
+ }
1925
+ });
1926
+ } catch (error) {
1927
+ if (error instanceof SyntaxError) {
1928
+ throw new ModelInvalidResponseError(
1929
+ this.model,
1930
+ `malformed JSON output \u2014 the generation was cut off at the ${this.maxOutputTokens}-token output cap or went off the rails (${error.message})`
1931
+ );
1913
1932
  }
1914
- });
1933
+ throw error;
1934
+ }
1935
+ if (response.status === "incomplete") {
1936
+ const reason = response.incomplete_details?.reason ?? "unknown";
1937
+ throw new ModelInvalidResponseError(
1938
+ this.model,
1939
+ `response incomplete (${reason}) at max_output_tokens=${this.maxOutputTokens}`,
1940
+ reason === "max_output_tokens"
1941
+ );
1942
+ }
1915
1943
  if (!response.output_parsed) {
1916
1944
  this.logger(`Parsing failed. Raw content: ${response.output_text}`);
1917
- throw new Error(this.errorMessages.invalidFormat);
1945
+ throw new ModelInvalidResponseError(this.model, this.errorMessages.invalidFormat);
1918
1946
  }
1919
1947
  let reasoningContent = "";
1920
1948
  if (this.enableThinking && response.output_parsed.thinking) {
@@ -1953,6 +1981,9 @@ var Gpt5Agent = class extends AbstractAgent {
1953
1981
  return [response.output_parsed, reasoningContent, tokenUsage];
1954
1982
  } catch (error) {
1955
1983
  this.logger(this.logTemplates.error(this.name, error));
1984
+ if (error instanceof ModelError) {
1985
+ throw error;
1986
+ }
1956
1987
  throw new Error(this.errorMessages.apiError(error));
1957
1988
  }
1958
1989
  }
@@ -1978,6 +2009,14 @@ var Gpt5Agent = class extends AbstractAgent {
1978
2009
  });
1979
2010
  const content = response.output_text;
1980
2011
  if (!content) {
2012
+ if (response.status === "incomplete") {
2013
+ const reason = response.incomplete_details?.reason ?? "unknown";
2014
+ throw new ModelInvalidResponseError(
2015
+ this.model,
2016
+ `empty response, incomplete (${reason}) at max_output_tokens=${this.maxOutputTokens}`,
2017
+ reason === "max_output_tokens"
2018
+ );
2019
+ }
1981
2020
  throw new Error(this.errorMessages.emptyResponse);
1982
2021
  }
1983
2022
  let tokenUsage;
@@ -2010,6 +2049,9 @@ var Gpt5Agent = class extends AbstractAgent {
2010
2049
  return [content, "", tokenUsage];
2011
2050
  } catch (error) {
2012
2051
  this.logger(this.logTemplates.error(this.name, error));
2052
+ if (error instanceof ModelError) {
2053
+ throw error;
2054
+ }
2013
2055
  throw new Error(this.errorMessages.apiError(error));
2014
2056
  }
2015
2057
  }
@@ -4426,6 +4468,7 @@ var AgentFactory = class {
4426
4468
  MistralAgent,
4427
4469
  ModelAuthenticationError,
4428
4470
  ModelError,
4471
+ ModelInvalidResponseError,
4429
4472
  ModelOverloadError,
4430
4473
  ModelQuotaExceededError,
4431
4474
  ModelRateLimitError,