@cloudflare/think 0.8.3 → 0.8.4

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License Copyright (c) 2025 Cloudflare, Inc.
2
+
3
+ Permission is hereby granted, free of
4
+ charge, to any person obtaining a copy of this software and associated
5
+ documentation files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use, copy, modify, merge,
7
+ publish, distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to the
9
+ following conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ (including the next paragraph) shall be included in all copies or substantial
13
+ portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18
+ EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/dist/think.js CHANGED
@@ -5,7 +5,7 @@ import { Output, convertToModelMessages, jsonSchema, stepCountIs, streamText } f
5
5
  import * as skills from "agents/skills";
6
6
  import { SkillRegistry } from "agents/skills";
7
7
  import { Agent, __DO_NOT_USE_WILL_BREAK__agentContext } from "agents";
8
- import { AbortRegistry, CHAT_MESSAGE_TYPES, ContinuationState, ResumableStream, StreamAccumulator, SubmitConcurrencyController, TurnQueue, applyChunkToParts, applyToolUpdate, createChatFiberSnapshot, createToolsFromClientSchemas, crossMessageToolResultUpdate, enforceRowSizeLimit, normalizeToolInput, parseProtocolMessage, reconcileMessages, resolveToolMergeId, sanitizeMessage, toolApprovalUpdate, toolResultUpdate, unwrapChatFiberSnapshot, wrapChatFiberSnapshot } from "agents/chat";
8
+ import { AbortRegistry, CHAT_MESSAGE_TYPES, ContinuationState, MAX_BOUND_PARAMS, ResumableStream, StreamAccumulator, SubmitConcurrencyController, TurnQueue, applyChunkToParts, applyToolUpdate, buildInClauseStrings, createChatFiberSnapshot, createToolsFromClientSchemas, crossMessageToolResultUpdate, enforceRowSizeLimit, normalizeToolInput, parseProtocolMessage, reconcileMessages, resolveToolMergeId, sanitizeMessage, toolApprovalUpdate, toolResultUpdate, unwrapChatFiberSnapshot, wrapChatFiberSnapshot } from "agents/chat";
9
9
  import { Session, Session as Session$1 } from "agents/experimental/memory/session";
10
10
  import { truncateOlderMessages } from "agents/experimental/memory/utils";
11
11
  import { Workspace, Workspace as Workspace$1 } from "@cloudflare/shell";
@@ -296,6 +296,7 @@ const DEFAULT_CHAT_RECOVERY_STABLE_TIMEOUT_MS = 1e4;
296
296
  const CHAT_RECOVERY_STABLE_RETRY_DELAY_SECONDS = 3;
297
297
  const DEFAULT_CHAT_RECOVERY_TERMINAL_MESSAGE = "The assistant was interrupted and could not recover. Please try again.";
298
298
  const CHAT_RECOVERY_INCIDENT_TTL_MS = 3600 * 1e3;
299
+ const KV_DELETE_MAX_KEYS = 128;
299
300
  const DEFAULT_CHAT_RECOVERY_NO_PROGRESS_TIMEOUT_MS = 300 * 1e3;
300
301
  const CHAT_RECOVERY_ALARM_DEBOUNCE_MS = 30 * 1e3;
301
302
  const CHAT_RECOVERING_FLAG_TTL_MS = 900 * 1e3;
@@ -2840,16 +2841,13 @@ var Think = class Think extends Agent {
2840
2841
  ]);
2841
2842
  const limit = Math.min(Math.max(options?.limit ?? 100, 1), 500);
2842
2843
  const completedBefore = options?.completedBefore?.getTime();
2843
- const rows = [...statuses].flatMap((status) => this._listTerminalSubmissionRowsForDelete(status, limit, completedBefore)).sort((a, b) => (a.completed_at ?? a.created_at) === (b.completed_at ?? b.created_at) ? a.created_at - b.created_at : (a.completed_at ?? a.created_at) - (b.completed_at ?? b.created_at)).slice(0, limit);
2844
+ const idsToDelete = [...statuses].flatMap((status) => this._listTerminalSubmissionRowsForDelete(status, limit, completedBefore)).sort((a, b) => (a.completed_at ?? a.created_at) === (b.completed_at ?? b.created_at) ? a.created_at - b.created_at : (a.completed_at ?? a.created_at) - (b.completed_at ?? b.created_at)).slice(0, limit).filter((row) => this._isTerminalSubmissionStatus(row.status)).map((row) => row.submission_id);
2844
2845
  let deleted = 0;
2845
- for (const row of rows) {
2846
- if (!this._isTerminalSubmissionStatus(row.status)) continue;
2847
- this.sql`
2848
- DELETE FROM cf_think_submissions
2849
- WHERE submission_id = ${row.submission_id}
2850
- AND status IN ('completed', 'aborted', 'skipped', 'error')
2851
- `;
2852
- deleted++;
2846
+ for (let i = 0; i < idsToDelete.length; i += MAX_BOUND_PARAMS) {
2847
+ const batch = idsToDelete.slice(i, i + MAX_BOUND_PARAMS);
2848
+ const strings = buildInClauseStrings("DELETE FROM cf_think_submissions WHERE status IN ('completed', 'aborted', 'skipped', 'error') AND submission_id IN ", batch.length);
2849
+ this.sql(strings, ...batch);
2850
+ deleted += batch.length;
2853
2851
  }
2854
2852
  return deleted;
2855
2853
  }
@@ -4567,7 +4565,7 @@ var Think = class Think extends Agent {
4567
4565
  const entries = await this.ctx.storage.list({ prefix: CHAT_RECOVERY_INCIDENT_KEY_PREFIX });
4568
4566
  const staleKeys = [];
4569
4567
  for (const [key, incident] of entries) if (now - (incident?.lastAttemptAt ?? incident?.firstSeenAt ?? 0) > CHAT_RECOVERY_INCIDENT_TTL_MS) staleKeys.push(key);
4570
- for (const key of staleKeys) await this.ctx.storage.delete(key);
4568
+ for (let i = 0; i < staleKeys.length; i += KV_DELETE_MAX_KEYS) await this.ctx.storage.delete(staleKeys.slice(i, i + KV_DELETE_MAX_KEYS));
4571
4569
  }
4572
4570
  async _beginChatRecoveryIncident(input) {
4573
4571
  const config = this._resolveChatRecoveryConfig();