@kenkaiiii/gg-agent 5.19.6 → 5.20.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.d.cts CHANGED
@@ -87,6 +87,19 @@ interface AgentMaxTurnsEvent {
87
87
  totalTurns: number;
88
88
  maxTurns: number;
89
89
  }
90
+ /**
91
+ * Warning signal emitted when a turn ended on a non-clean stop reason —
92
+ * `max_tokens` (output clipped at the model's output-token limit), `refusal`,
93
+ * or a provider-reported `error` stop. Distinguishes a truncated/degraded
94
+ * completion from a clean one so hosts can warn the user instead of silently
95
+ * presenting incomplete output as done.
96
+ */
97
+ interface AgentTruncatedEvent {
98
+ type: "truncated";
99
+ reason: "max_tokens" | "refusal" | "provider_error";
100
+ /** True when the loop injected a continuation and will keep going. */
101
+ continued: boolean;
102
+ }
90
103
  interface AgentRetryEvent {
91
104
  type: "retry";
92
105
  reason: "overloaded" | "rate_limit" | "provider_error" | "empty_response" | "stream_stall" | "overflow_compact" | "tool_argument_glitch";
@@ -135,7 +148,7 @@ interface AgentFollowUpMessageEvent {
135
148
  type: "follow_up_message";
136
149
  content: Message["content"];
137
150
  }
138
- type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentTurnEndEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentErrorEvent;
151
+ type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentTurnEndEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentTruncatedEvent | AgentErrorEvent;
139
152
  interface TransformContextOptions {
140
153
  /** Force a transform after the provider reports context overflow. */
141
154
  force?: boolean;
package/dist/index.d.ts CHANGED
@@ -87,6 +87,19 @@ interface AgentMaxTurnsEvent {
87
87
  totalTurns: number;
88
88
  maxTurns: number;
89
89
  }
90
+ /**
91
+ * Warning signal emitted when a turn ended on a non-clean stop reason —
92
+ * `max_tokens` (output clipped at the model's output-token limit), `refusal`,
93
+ * or a provider-reported `error` stop. Distinguishes a truncated/degraded
94
+ * completion from a clean one so hosts can warn the user instead of silently
95
+ * presenting incomplete output as done.
96
+ */
97
+ interface AgentTruncatedEvent {
98
+ type: "truncated";
99
+ reason: "max_tokens" | "refusal" | "provider_error";
100
+ /** True when the loop injected a continuation and will keep going. */
101
+ continued: boolean;
102
+ }
90
103
  interface AgentRetryEvent {
91
104
  type: "retry";
92
105
  reason: "overloaded" | "rate_limit" | "provider_error" | "empty_response" | "stream_stall" | "overflow_compact" | "tool_argument_glitch";
@@ -135,7 +148,7 @@ interface AgentFollowUpMessageEvent {
135
148
  type: "follow_up_message";
136
149
  content: Message["content"];
137
150
  }
138
- type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentTurnEndEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentErrorEvent;
151
+ type AgentEvent = AgentTextDeltaEvent | AgentThinkingDeltaEvent | AgentToolCallStartEvent | AgentToolCallUpdateEvent | AgentToolCallEndEvent | AgentToolCallDeltaEvent | AgentServerToolCallEvent | AgentServerToolResultEvent | AgentSteeringMessageEvent | AgentFollowUpMessageEvent | AgentRetryEvent | AgentTurnEndEvent | AgentDoneEvent | AgentMaxTurnsEvent | AgentTruncatedEvent | AgentErrorEvent;
139
152
  interface TransformContextOptions {
140
153
  /** Force a transform after the provider reports context overflow. */
141
154
  force?: boolean;
package/dist/index.js CHANGED
@@ -213,6 +213,9 @@ async function* agentLoop(messages, options) {
213
213
  const STALL_DELAY_MS = 1e3;
214
214
  const MIN_PARTIAL_PRESERVE_CHARS = 200;
215
215
  const PARTIAL_CONTINUATION_PROMPT = "[Your previous response was cut off by a connection failure. The text above is what was already delivered to the user. Continue exactly from where it stopped \u2014 do not repeat or restart it.]";
216
+ const MAX_OUTPUT_CONTINUATIONS = 2;
217
+ const MAX_TOKENS_CONTINUATION_PROMPT = "[Your previous response hit the output-token limit and was cut off. The text above is what was already delivered to the user. Continue exactly from where it stopped \u2014 do not repeat or restart it.]";
218
+ let maxTokensContinuations = 0;
216
219
  const OVERLOAD_BASE_DELAY_MS = 2e3;
217
220
  const OVERLOAD_MAX_DELAY_MS = 3e4;
218
221
  const STREAM_FIRST_EVENT_TIMEOUT_MS = 45e3;
@@ -785,6 +788,27 @@ async function* agentLoop(messages, options) {
785
788
  consecutivePauses = 0;
786
789
  const allToolCalls = extractToolCalls(response.message.content);
787
790
  if (response.stopReason !== "tool_use" && allToolCalls.length === 0) {
791
+ if (response.stopReason === "max_tokens") {
792
+ if (maxTokensContinuations < MAX_OUTPUT_CONTINUATIONS) {
793
+ maxTokensContinuations++;
794
+ diag("max_tokens_continuation", {
795
+ attempt: maxTokensContinuations,
796
+ maxAttempts: MAX_OUTPUT_CONTINUATIONS,
797
+ provider: options.provider,
798
+ model: options.model
799
+ });
800
+ yield { type: "truncated", reason: "max_tokens", continued: true };
801
+ messages.push({ role: "user", content: MAX_TOKENS_CONTINUATION_PROMPT });
802
+ continue;
803
+ }
804
+ yield { type: "truncated", reason: "max_tokens", continued: false };
805
+ } else if (response.stopReason === "refusal" || response.stopReason === "error") {
806
+ yield {
807
+ type: "truncated",
808
+ reason: response.stopReason === "refusal" ? "refusal" : "provider_error",
809
+ continued: false
810
+ };
811
+ }
788
812
  if (options.getSteeringMessages) {
789
813
  const steering = await options.getSteeringMessages();
790
814
  if (steering && steering.length > 0) {