@cloudflare/ai-chat 0.8.2 → 0.8.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/LICENSE +21 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +24 -22
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
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/index.d.ts
CHANGED
|
@@ -972,6 +972,13 @@ declare class AIChatAgent<
|
|
|
972
972
|
private static _shouldPreserveProviderToolPayload;
|
|
973
973
|
private static _getToolNameFromPart;
|
|
974
974
|
private static _isOpaqueReplayFieldKey;
|
|
975
|
+
/**
|
|
976
|
+
* Delete the given message rows from SQLite in batched `IN (...)` queries
|
|
977
|
+
* and evict them from the persistence cache. Batches stay within the SQLite
|
|
978
|
+
* 100 bound-parameter limit. No-op for an empty list.
|
|
979
|
+
* @internal
|
|
980
|
+
*/
|
|
981
|
+
private _deleteMessagesByIds;
|
|
975
982
|
/**
|
|
976
983
|
* Deletes oldest messages from SQLite when the count exceeds maxPersistedMessages.
|
|
977
984
|
* Called after each persist to keep storage bounded.
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import "./types.js";
|
|
2
2
|
import { autoTransformMessages } from "./ai-chat-v5-migration.js";
|
|
3
3
|
import { Agent, __DO_NOT_USE_WILL_BREAK__agentContext } from "agents";
|
|
4
|
-
import { AbortRegistry, ContinuationState, ROW_MAX_BYTES, ResumableStream, SubmitConcurrencyController, TurnQueue, applyChunkToParts, byteLength, createChatFiberSnapshot, createToolsFromClientSchemas, isReplayChunk, reconcileMessages, resolveToolMergeId, sanitizeMessage, unwrapChatFiberSnapshot, wrapChatFiberSnapshot } from "agents/chat";
|
|
4
|
+
import { AbortRegistry, ContinuationState, MAX_BOUND_PARAMS, ROW_MAX_BYTES, ResumableStream, SubmitConcurrencyController, TurnQueue, applyChunkToParts, buildInClauseStrings, byteLength, createChatFiberSnapshot, createToolsFromClientSchemas, isReplayChunk, reconcileMessages, resolveToolMergeId, sanitizeMessage, unwrapChatFiberSnapshot, wrapChatFiberSnapshot } from "agents/chat";
|
|
5
5
|
import { nanoid } from "nanoid";
|
|
6
6
|
//#region src/index.ts
|
|
7
7
|
const CHAT_RECOVERY_INCIDENT_KEY_PREFIX = "cf:chat-recovery:incident:";
|
|
@@ -14,6 +14,7 @@ const CHAT_RECOVERY_STABLE_RETRY_DELAY_SECONDS = 3;
|
|
|
14
14
|
const DEFAULT_CHAT_RECOVERY_TERMINAL_MESSAGE = "The assistant was interrupted and could not recover. Please try again.";
|
|
15
15
|
const CHAT_RECOVERING_KEY = "cf:chat:recovering";
|
|
16
16
|
const CHAT_RECOVERY_INCIDENT_TTL_MS = 3600 * 1e3;
|
|
17
|
+
const KV_DELETE_MAX_KEYS = 128;
|
|
17
18
|
const DEFAULT_CHAT_RECOVERY_NO_PROGRESS_TIMEOUT_MS = 300 * 1e3;
|
|
18
19
|
const CHAT_RECOVERY_ALARM_DEBOUNCE_MS = 30 * 1e3;
|
|
19
20
|
const CHAT_RECOVERING_FLAG_TTL_MS = 900 * 1e3;
|
|
@@ -1577,14 +1578,9 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1577
1578
|
_getAgentToolStoredChunks(requestId, afterSequence = -1) {
|
|
1578
1579
|
const streamId = this._getAgentToolStreamId(requestId);
|
|
1579
1580
|
if (!streamId) return [];
|
|
1580
|
-
return this.
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
where stream_id = ${streamId} and chunk_index > ${afterSequence}
|
|
1584
|
-
order by chunk_index asc
|
|
1585
|
-
`.map((row) => ({
|
|
1586
|
-
sequence: row.chunk_index,
|
|
1587
|
-
body: row.body
|
|
1581
|
+
return this._resumableStream.getStreamChunks(streamId).filter((chunk) => chunk.chunk_index > afterSequence).map((chunk) => ({
|
|
1582
|
+
sequence: chunk.chunk_index,
|
|
1583
|
+
body: chunk.body
|
|
1588
1584
|
}));
|
|
1589
1585
|
}
|
|
1590
1586
|
_getAgentToolMessagesAfterStart(runId) {
|
|
@@ -1864,7 +1860,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
1864
1860
|
const entries = await this.ctx.storage.list({ prefix: CHAT_RECOVERY_INCIDENT_KEY_PREFIX });
|
|
1865
1861
|
const staleKeys = [];
|
|
1866
1862
|
for (const [key, incident] of entries) if (now - (incident?.lastAttemptAt ?? incident?.firstSeenAt ?? 0) > CHAT_RECOVERY_INCIDENT_TTL_MS) staleKeys.push(key);
|
|
1867
|
-
for (
|
|
1863
|
+
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));
|
|
1868
1864
|
}
|
|
1869
1865
|
async _beginChatRecoveryIncident(input) {
|
|
1870
1866
|
const config = this._resolveChatRecoveryConfig();
|
|
@@ -2353,15 +2349,10 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
2353
2349
|
const serverIds = new Set(this.messages.map((m) => m.id));
|
|
2354
2350
|
if (mergedMessages.every((m) => serverIds.has(m.id))) {
|
|
2355
2351
|
const keepIds = new Set(mergedMessages.map((m) => m.id));
|
|
2356
|
-
const
|
|
2352
|
+
const staleIds = (this.sql`
|
|
2357
2353
|
select id from cf_ai_chat_agent_messages
|
|
2358
|
-
` || [];
|
|
2359
|
-
|
|
2360
|
-
this.sql`
|
|
2361
|
-
delete from cf_ai_chat_agent_messages where id = ${row.id}
|
|
2362
|
-
`;
|
|
2363
|
-
this._persistedMessageCache.delete(row.id);
|
|
2364
|
-
}
|
|
2354
|
+
` || []).map((row) => row.id).filter((id) => !keepIds.has(id));
|
|
2355
|
+
this._deleteMessagesByIds(staleIds);
|
|
2365
2356
|
}
|
|
2366
2357
|
}
|
|
2367
2358
|
if (this.maxPersistedMessages != null) this._enforceMaxPersistedMessages();
|
|
@@ -2493,6 +2484,20 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
2493
2484
|
return key.startsWith(PROVIDER_TOOL_OPAQUE_STRING_KEY_PREFIX);
|
|
2494
2485
|
}
|
|
2495
2486
|
/**
|
|
2487
|
+
* Delete the given message rows from SQLite in batched `IN (...)` queries
|
|
2488
|
+
* and evict them from the persistence cache. Batches stay within the SQLite
|
|
2489
|
+
* 100 bound-parameter limit. No-op for an empty list.
|
|
2490
|
+
* @internal
|
|
2491
|
+
*/
|
|
2492
|
+
_deleteMessagesByIds(ids) {
|
|
2493
|
+
for (let i = 0; i < ids.length; i += MAX_BOUND_PARAMS) {
|
|
2494
|
+
const batch = ids.slice(i, i + MAX_BOUND_PARAMS);
|
|
2495
|
+
const strings = buildInClauseStrings("delete from cf_ai_chat_agent_messages where id in ", batch.length);
|
|
2496
|
+
this.sql(strings, ...batch);
|
|
2497
|
+
for (const id of batch) this._persistedMessageCache.delete(id);
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
/**
|
|
2496
2501
|
* Deletes oldest messages from SQLite when the count exceeds maxPersistedMessages.
|
|
2497
2502
|
* Called after each persist to keep storage bounded.
|
|
2498
2503
|
*/
|
|
@@ -2508,10 +2513,7 @@ var AIChatAgent = class AIChatAgent extends Agent {
|
|
|
2508
2513
|
order by created_at asc
|
|
2509
2514
|
limit ${excess}
|
|
2510
2515
|
`;
|
|
2511
|
-
if (toDelete && toDelete.length > 0)
|
|
2512
|
-
this.sql`delete from cf_ai_chat_agent_messages where id = ${row.id}`;
|
|
2513
|
-
this._persistedMessageCache.delete(row.id);
|
|
2514
|
-
}
|
|
2516
|
+
if (toDelete && toDelete.length > 0) this._deleteMessagesByIds(toDelete.map((row) => row.id));
|
|
2515
2517
|
}
|
|
2516
2518
|
/**
|
|
2517
2519
|
* Enforces SQLite row size limits by compacting tool outputs and text parts
|