@cloudflare/think 0.19.0 → 0.20.1

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.
@@ -47,6 +47,11 @@ https://<your-worker>/messengers/telegram/webhook
47
47
  custom `verifyWebhook` function or explicitly opt out with
48
48
  `verifyWebhook: false`.
49
49
 
50
+ To stream private-chat replies as native Telegram drafts instead of posting a
51
+ message and editing it, pass `nativeStreaming: true`. With
52
+ `@chat-adapter/telegram` 4.38 or later it defaults to `false`; earlier versions
53
+ always use drafts in private chats. Group chats always post and edit.
54
+
50
55
  If one Think agent owns multiple Telegram bots, give each provider a distinct
51
56
  Chat SDK adapter name:
52
57
 
@@ -128,6 +133,70 @@ they get a speaker prefix in channels so the model can attribute interactive
128
133
  clicks, and no prefix in DMs. Returning `null` or an empty string from
129
134
  `channelSpeakerLabel` suppresses the channel label as well.
130
135
 
136
+ ## Message Bursts
137
+
138
+ People often send one thought as several quick messages. Think's messenger
139
+ runtime waits about 600 ms after a message for more to arrive, then answers the
140
+ whole burst once instead of starting a reply per message. Every message in the
141
+ burst reaches the model, oldest first, in a single user turn:
142
+
143
+ ```text
144
+ summarize the thread
145
+ for me
146
+ and keep it short
147
+ ```
148
+
149
+ In channels, speaker labels follow the same rules as above. Consecutive messages
150
+ from one person share a single label, and a new label starts whenever the
151
+ speaker changes:
152
+
153
+ ```text
154
+ Bob: @support_bot is the deploy done?
155
+ Ada: @support_bot what changed?
156
+ ```
157
+
158
+ The turn is keyed to the newest message, so its id, idempotency key, and
159
+ `getMessengerContext()?.message` all refer to that message. The earlier messages
160
+ are available as `getMessengerContext()?.skipped`. A custom `toEvent` receives
161
+ them as `input.skipped`, and the default event copies them onto
162
+ `event.skipped`. A custom `toEvent` that builds its own event must copy them
163
+ too, or start from `defaultChatSdkEvent(definition, input)`, otherwise the
164
+ model sees only the newest message.
165
+
166
+ A burst counts as a mention when any of its messages mentions the bot, in a
167
+ subscribed thread or not. With the default `respondTo`, a mention followed by
168
+ an ordinary line is still answered, and the thread is subscribed.
169
+
170
+ ### Change the Concurrency Strategy
171
+
172
+ Set `messengerConcurrency` to use another Chat SDK concurrency strategy. It
173
+ defaults to `DEFAULT_MESSENGER_CONCURRENCY`, which is
174
+ `{ strategy: "burst", debounceMs: 600 }`. One setting covers every messenger on
175
+ the agent, and it must be a class field, because the runtime reads it before
176
+ `onStart` runs.
177
+
178
+ ```ts
179
+ import { Think } from "@cloudflare/think";
180
+
181
+ export class SupportAgent extends Think<Env> {
182
+ // Answer the first message right away. Messages that arrive during the
183
+ // reply are answered together once it finishes.
184
+ messengerConcurrency = "queue" as const;
185
+ }
186
+ ```
187
+
188
+ Use `{ strategy: "burst", debounceMs: 1500 }` to wait longer for a burst. The
189
+ `"drop"` strategy discards a message that arrives while a reply is running.
190
+ `"concurrent"` hands every message to Think without a thread lock, so none are
191
+ batched or dropped. Think still runs the turns of one conversation one at a
192
+ time, so the replies arrive in order rather than in parallel.
193
+
194
+ Think keeps the thread lock alive for as long as a reply runs, and keeps queued
195
+ messages for 30 minutes (the Chat SDK default is 90 seconds), so a message that
196
+ arrives during a slow reply is still answered after it. Set `queueEntryTtlMs`
197
+ in a `ConcurrencyConfig` to change that. Messages still queued when the Durable
198
+ Object restarts are answered once the interrupted reply is recovered.
199
+
131
200
  ## Conversation Targets
132
201
 
133
202
  The default conversation mode is one Think sub-agent per Chat SDK thread. This
@@ -177,11 +246,30 @@ idempotent managed fiber, resolves the conversation target, calls
177
246
  `target.chat(message, callback)`, and lets the provider delivery policy post or
178
247
  edit visible messages.
179
248
 
249
+ While the model is working, Think shows the provider's typing indicator and
250
+ re-sends it every 4 seconds until the reply's first text is posted (set the
251
+ messenger's `delivery.typingRefreshMs` to change that; `0` sends it once). A
252
+ provider that fails to show the indicator does not stop the reply. On
253
+ providers with native streaming, the reply streams in place. On providers
254
+ without it, Think posts the reply once the first text arrives and edits that
255
+ message as the rest streams in. It never posts a `...` placeholder first, so
256
+ notification previews (for example, in Slack) show the start of the actual
257
+ reply. Replies delivered by recovery after a restart behave the same way.
258
+
180
259
  Recovery snapshots store only serializable event and Chat SDK thread data. If a
181
260
  restart happens before streaming starts, Think can replay the answer. If a
182
261
  restart happens after streaming starts, Think posts the configured interruption
183
262
  message instead of risking a duplicate partial answer.
184
263
 
264
+ A reply can also be interrupted without a restart, for example by the stream
265
+ stall watchdog or by a model error that `classifyChatError` marks as
266
+ `"transient"` or `"rate_limit"`. Think then continues the turn through chat
267
+ recovery and posts the rest of the answer to the thread as a new message once
268
+ the continuation finishes. This works whether the conversation target is the
269
+ root agent or a per-thread sub-agent. If recovery gives up, Think posts the
270
+ configured interruption message instead. A reset after recovery finishes but
271
+ before the post lands can post the recovered text twice.
272
+
185
273
  Delivery errors use a generic user-facing message by default so internal
186
274
  exception details are not posted into external chats. Override
187
275
  `delivery.errorResponseText` when you want a custom safe message.
@@ -190,7 +278,9 @@ exception details are not posted into external chats. Override
190
278
 
191
279
  During a messenger turn, `getMessengerContext()` returns provider, thread,
192
280
  author, message, capabilities, and attachment metadata for the initiating event.
193
- Use it from prompts, tools, or hooks that need channel-specific behavior.
281
+ When the turn answers a [burst](#message-bursts), `skipped` lists the earlier
282
+ messages in that burst, oldest first. Use it from prompts, tools, or hooks that
283
+ need channel-specific behavior.
194
284
 
195
285
  ```typescript
196
286
  const messenger = this.getMessengerContext();
@@ -199,6 +289,8 @@ if (messenger?.thread.isDirectMessage === false) {
199
289
  }
200
290
  ```
201
291
 
292
+ The context belongs to the turn, so concurrent messenger turns on one agent each see their own thread. In `beforeTurn`, `ctx.messenger` holds the same value. Unlike `getMessengerContext()`, it is `undefined` for a turn that is not a messenger turn, rather than falling back to the metadata on the latest message.
293
+
202
294
  ## Self-Mentions
203
295
 
204
296
  When a user @-mentions the bot, the triggering message leads with the bot's own