@cloudflare/think 0.12.1 → 0.13.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/README.md +9 -8
- package/dist/{chat-sdk-Dm0R3pdR.js → chat-sdk-iuBa-CGM.js} +39 -7
- package/dist/chat-sdk-iuBa-CGM.js.map +1 -0
- package/dist/{index-WF0HQmkk.d.ts → index-Cm0jriTY.d.ts} +216 -137
- package/dist/messengers/index.d.ts +44 -38
- package/dist/messengers/index.js +2 -2
- package/dist/messengers/telegram.d.ts +2 -2
- package/dist/messengers/telegram.js +1 -1
- package/dist/think.d.ts +1 -1
- package/dist/think.js +56 -14
- package/dist/think.js.map +1 -1
- package/dist/workflows.d.ts +1 -1
- package/docs/messengers.md +52 -0
- package/package.json +3 -3
- package/dist/chat-sdk-Dm0R3pdR.js.map +0 -1
package/dist/think.js
CHANGED
|
@@ -2,7 +2,7 @@ import { a as _classPrivateFieldSet2, i as _classPrivateFieldGet2, n as sanitize
|
|
|
2
2
|
import { createWorkspaceTools } from "./tools/workspace.js";
|
|
3
3
|
import { createFetchTools } from "./tools/fetch.js";
|
|
4
4
|
import { truncatePausedExecutionOutput } from "./tools/execute.js";
|
|
5
|
-
import { t as ThinkMessengerRuntime } from "./chat-sdk-
|
|
5
|
+
import { t as ThinkMessengerRuntime } from "./chat-sdk-iuBa-CGM.js";
|
|
6
6
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
7
7
|
import { convertToModelMessages, hasToolCall, jsonSchema, stepCountIs, streamText, tool } from "ai";
|
|
8
8
|
import { createWorkersAI } from "workers-ai-provider";
|
|
@@ -740,6 +740,12 @@ function isAction(value) {
|
|
|
740
740
|
const admittedTurnContext = new AsyncLocalStorage();
|
|
741
741
|
const THINK_WORKFLOW_PROMPT_METADATA_KEY = "__thinkWorkflowPrompt";
|
|
742
742
|
/**
|
|
743
|
+
* Message-metadata keys that are server-written turn context (stamped by
|
|
744
|
+
* `_stampChannel`) and trusted by hooks and recovery. They are stripped from
|
|
745
|
+
* client-supplied messages at intake so a client can never forge them.
|
|
746
|
+
*/
|
|
747
|
+
const RESERVED_MESSAGE_METADATA_KEYS = ["channel", "turnMetadata"];
|
|
748
|
+
/**
|
|
743
749
|
* Reserved name for the synthetic tool a workflow `step.prompt` turn uses to
|
|
744
750
|
* deliver its structured final answer. The agent runs a full multi-step,
|
|
745
751
|
* tool-using turn and ends it by calling this tool with arguments matching the
|
|
@@ -1660,6 +1666,16 @@ var Think = class Think extends Agent {
|
|
|
1660
1666
|
get activeChannel() {
|
|
1661
1667
|
return this._activeChannelContext;
|
|
1662
1668
|
}
|
|
1669
|
+
/**
|
|
1670
|
+
* The server-supplied metadata stamped on the active turn's user message
|
|
1671
|
+
* ({@link ChatOptions.metadata}). Like the channel stamp, it is persisted on
|
|
1672
|
+
* the durable user message, so recovered/continued turns re-resolve the same
|
|
1673
|
+
* value; client-supplied message metadata can never populate it (reserved
|
|
1674
|
+
* keys are stripped at intake). `undefined` when the turn carried none.
|
|
1675
|
+
*/
|
|
1676
|
+
get activeTurnMetadata() {
|
|
1677
|
+
return this._turnMetadataFromMessages(this.messages);
|
|
1678
|
+
}
|
|
1663
1679
|
/** Resolve a channel id to a turn-scoped {@link ChannelContext}, if registered. */
|
|
1664
1680
|
_resolveChannelContext(channel) {
|
|
1665
1681
|
if (!channel) return;
|
|
@@ -1700,29 +1716,38 @@ var Think = class Think extends Agent {
|
|
|
1700
1716
|
}
|
|
1701
1717
|
}
|
|
1702
1718
|
/**
|
|
1703
|
-
* Stamp the channel id
|
|
1704
|
-
* re-resolve
|
|
1719
|
+
* Stamp the channel id — and, when supplied, the caller's turn metadata —
|
|
1720
|
+
* onto user messages so a recovered/continued turn can re-resolve both from
|
|
1721
|
+
* durable history.
|
|
1705
1722
|
*/
|
|
1706
|
-
_stampChannel(messages, channel) {
|
|
1707
|
-
if (!channel) return messages;
|
|
1723
|
+
_stampChannel(messages, channel, turnMetadata) {
|
|
1724
|
+
if (!channel && !turnMetadata) return messages;
|
|
1708
1725
|
return messages.map((message) => message.role === "user" ? {
|
|
1709
1726
|
...message,
|
|
1710
1727
|
metadata: {
|
|
1711
1728
|
...message.metadata,
|
|
1712
|
-
channel
|
|
1729
|
+
...channel ? { channel } : {},
|
|
1730
|
+
...turnMetadata ? { turnMetadata } : {}
|
|
1713
1731
|
}
|
|
1714
1732
|
} : message);
|
|
1715
1733
|
}
|
|
1716
|
-
/**
|
|
1717
|
-
|
|
1734
|
+
/** Metadata of the latest user message in the given list, if any. */
|
|
1735
|
+
_latestUserMessageMetadata(messages) {
|
|
1718
1736
|
for (let i = messages.length - 1; i >= 0; i--) {
|
|
1719
1737
|
const message = messages[i];
|
|
1720
|
-
if (message.role === "user")
|
|
1721
|
-
const channel = message.metadata?.channel;
|
|
1722
|
-
return typeof channel === "string" ? channel : void 0;
|
|
1723
|
-
}
|
|
1738
|
+
if (message.role === "user") return message.metadata;
|
|
1724
1739
|
}
|
|
1725
1740
|
}
|
|
1741
|
+
/** The channel stamped on the latest user message in the given list, if any. */
|
|
1742
|
+
_channelFromMessages(messages) {
|
|
1743
|
+
const channel = this._latestUserMessageMetadata(messages)?.channel;
|
|
1744
|
+
return typeof channel === "string" ? channel : void 0;
|
|
1745
|
+
}
|
|
1746
|
+
/** The turn metadata stamped on the latest user message, if any. */
|
|
1747
|
+
_turnMetadataFromMessages(messages) {
|
|
1748
|
+
const turnMetadata = this._latestUserMessageMetadata(messages)?.turnMetadata;
|
|
1749
|
+
return turnMetadata && typeof turnMetadata === "object" && !Array.isArray(turnMetadata) ? turnMetadata : void 0;
|
|
1750
|
+
}
|
|
1726
1751
|
/** Re-resolve the channel for a continuation from the latest user message. */
|
|
1727
1752
|
_channelFromLatestUserMessage() {
|
|
1728
1753
|
return this._channelFromMessages(this.messages);
|
|
@@ -3349,7 +3374,7 @@ var Think = class Think extends Agent {
|
|
|
3349
3374
|
channel: options?.channel,
|
|
3350
3375
|
execute: async () => {
|
|
3351
3376
|
const resolved = typeof userMessage === "function" ? await userMessage(this.messages) : this._normalizeChatMessages(userMessage);
|
|
3352
|
-
for (const msg of this._stampChannel(resolved, options?.channel)) await this._appendMessageToHistory(msg);
|
|
3377
|
+
for (const msg of this._stampChannel(resolved, options?.channel, options?.metadata)) await this._appendMessageToHistory(msg);
|
|
3353
3378
|
this._broadcastMessages();
|
|
3354
3379
|
const chatBody = async () => {
|
|
3355
3380
|
for (let attempt = 0;; attempt++) {
|
|
@@ -6799,9 +6824,26 @@ var Think = class Think extends Agent {
|
|
|
6799
6824
|
* instead of inserting an orphan duplicate.
|
|
6800
6825
|
*/
|
|
6801
6826
|
async _persistIncomingMessage(msg, serverMessages) {
|
|
6802
|
-
const
|
|
6827
|
+
const sanitized = this._stripReservedMessageMetadata(msg);
|
|
6828
|
+
const resolved = sanitized.role === "assistant" ? resolveToolMergeId(sanitized, serverMessages) : sanitized;
|
|
6803
6829
|
await this._upsertMessageInHistory(resolved);
|
|
6804
6830
|
}
|
|
6831
|
+
/**
|
|
6832
|
+
* Strip {@link RESERVED_MESSAGE_METADATA_KEYS} from a client-supplied message
|
|
6833
|
+
* at intake. Those keys are server-written turn context (stamped by
|
|
6834
|
+
* `_stampChannel`) that hooks and recovery re-resolve and trust, so a client
|
|
6835
|
+
* must never be able to forge them.
|
|
6836
|
+
*/
|
|
6837
|
+
_stripReservedMessageMetadata(msg) {
|
|
6838
|
+
const metadata = msg.metadata;
|
|
6839
|
+
if (!metadata || !RESERVED_MESSAGE_METADATA_KEYS.some((key) => key in metadata)) return msg;
|
|
6840
|
+
const rest = { ...metadata };
|
|
6841
|
+
for (const key of RESERVED_MESSAGE_METADATA_KEYS) delete rest[key];
|
|
6842
|
+
return {
|
|
6843
|
+
...msg,
|
|
6844
|
+
metadata: rest
|
|
6845
|
+
};
|
|
6846
|
+
}
|
|
6805
6847
|
_persistClientTools() {
|
|
6806
6848
|
if (this._lastClientTools) this._configSet("lastClientTools", JSON.stringify(this._lastClientTools));
|
|
6807
6849
|
else this._configDelete("lastClientTools");
|