@cloudflare/think 0.10.0 → 0.11.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.
Files changed (138) hide show
  1. package/README.md +73 -17
  2. package/dist/{chat-sdk-Bb93yaZh.js → chat-sdk-D6cUghsQ.js} +33 -8
  3. package/dist/chat-sdk-D6cUghsQ.js.map +1 -0
  4. package/dist/cli/index.js +224 -55
  5. package/dist/cli/index.js.map +1 -1
  6. package/dist/{codegen-CuPdJRpV.js → codegen-DlA2QQuU.js} +6 -38
  7. package/dist/codegen-DlA2QQuU.js.map +1 -0
  8. package/dist/{config-D512sQYg.d.ts → config-CD7hqncK.d.ts} +5 -5
  9. package/dist/extensions/index.js +1 -1
  10. package/dist/fetch-By3eIQ-a.d.ts +147 -0
  11. package/dist/framework/index.d.ts +17 -62
  12. package/dist/framework/index.js +3 -57
  13. package/dist/{think-i1Pkgb1R.d.ts → index-CR4-ffm_.d.ts} +1734 -736
  14. package/dist/{manager-BPndMP_6.js → manager-CSk5htym.js} +7 -7
  15. package/dist/{manager-BPndMP_6.js.map → manager-CSk5htym.js.map} +1 -1
  16. package/dist/{manifest-CpB-xngB.d.ts → manifest-DK5HuGga.d.ts} +16 -18
  17. package/dist/messengers/index.d.ts +59 -55
  18. package/dist/messengers/index.js +2 -2
  19. package/dist/messengers/telegram.d.ts +4 -4
  20. package/dist/messengers/telegram.js +1 -1
  21. package/dist/react.d.ts +79 -0
  22. package/dist/react.js +28 -0
  23. package/dist/react.js.map +1 -0
  24. package/dist/server-entry.d.ts +1 -1
  25. package/dist/server-entry.js +1 -1
  26. package/dist/server-entry.js.map +1 -1
  27. package/dist/studio/assets/{angular-html-z-TUogET.js → angular-html-D27LthTc.js} +1 -1
  28. package/dist/studio/assets/{angular-ts-DMGuEVeg.js → angular-ts-DtNL2gQ7.js} +1 -1
  29. package/dist/studio/assets/{apl-KvrEROqd.js → apl-BgEcDqM0.js} +1 -1
  30. package/dist/studio/assets/{astro-Bm_woEti.js → astro-CstbRF9e.js} +1 -1
  31. package/dist/studio/assets/{blade-BdCF3r89.js → blade-CLG-7epG.js} +1 -1
  32. package/dist/studio/assets/{c-CDqDtkvZ.js → c-CssQqwxO.js} +1 -1
  33. package/dist/studio/assets/{cobol-CeFM1vZz.js → cobol-CjmzHCjg.js} +1 -1
  34. package/dist/studio/assets/{coffee-2ed6LBym.js → coffee-B_rqYPkp.js} +1 -1
  35. package/dist/studio/assets/{cpp-Dj3nX0J4.js → cpp-5YyZJlb7.js} +1 -1
  36. package/dist/studio/assets/{crystal-BsIYZMQ3.js → crystal-D7uKze0i.js} +1 -1
  37. package/dist/studio/assets/{css-B48fnuoH.js → css-k9oPgg7G.js} +1 -1
  38. package/dist/studio/assets/{edge-C7qt1aza.js → edge-CGgsSt0x.js} +1 -1
  39. package/dist/studio/assets/{elixir-CiTmwoQ3.js → elixir-BX_d5Evo.js} +1 -1
  40. package/dist/studio/assets/{elm-exDZ-PRY.js → elm-DENxDm5e.js} +1 -1
  41. package/dist/studio/assets/{erb-3fmfgt-Q.js → erb-DvYOup03.js} +1 -1
  42. package/dist/studio/assets/{git-rebase-75I-C7zW.js → git-rebase-BsAdTS5A.js} +1 -1
  43. package/dist/studio/assets/{glimmer-js-JjRzYRv8.js → glimmer-js-e0h98Z67.js} +1 -1
  44. package/dist/studio/assets/{glimmer-ts-DcC_g6bi.js → glimmer-ts-C9s5Kv8m.js} +1 -1
  45. package/dist/studio/assets/{glsl-CHo__hpu.js → glsl-CCiyeTRs.js} +1 -1
  46. package/dist/studio/assets/{graphql-J3cHpSdo.js → graphql-BRtHwsqk.js} +1 -1
  47. package/dist/studio/assets/{hack-or1FCnPg.js → hack-Dnt0QXHh.js} +1 -1
  48. package/dist/studio/assets/{haml-uv3vtlo6.js → haml-EXGmzuB1.js} +1 -1
  49. package/dist/studio/assets/{handlebars--xrOP0sp.js → handlebars-CzyBIDJE.js} +1 -1
  50. package/dist/studio/assets/{highlighted-body-OFNGDK62-D5UBAw6b.js → highlighted-body-OFNGDK62-CQUFKLNu.js} +1 -1
  51. package/dist/studio/assets/{html-CWnTqWKh.js → html-BAT8b_VM.js} +1 -1
  52. package/dist/studio/assets/{html-derivative-DWyLco-z.js → html-derivative-7tkS-r6F.js} +1 -1
  53. package/dist/studio/assets/{http-BGB8mLqf.js → http-Cgka02Qq.js} +1 -1
  54. package/dist/studio/assets/{hurl-BWdvIVUG.js → hurl-Cbkh8WEw.js} +1 -1
  55. package/dist/studio/assets/index-D6n8vpkp.js +390 -0
  56. package/dist/studio/assets/index-DkGWw6ES.css +2 -0
  57. package/dist/studio/assets/{java-BqyrPpKF.js → java-DrRKiUno.js} +1 -1
  58. package/dist/studio/assets/{javascript-D8Aej0xg.js → javascript-9LAnHOxD.js} +1 -1
  59. package/dist/studio/assets/{jinja-SIORrXov.js → jinja-CqRt4prs.js} +1 -1
  60. package/dist/studio/assets/{jison-B2T4v51n.js → jison-BX-IOQpJ.js} +1 -1
  61. package/dist/studio/assets/{json-BFdVRj9r.js → json-dOrE-En8.js} +1 -1
  62. package/dist/studio/assets/{jsx-w4r1xieS.js → jsx-BSTPfqWX.js} +1 -1
  63. package/dist/studio/assets/{julia-BFBtDmmM.js → julia-Dvz-BPmf.js} +1 -1
  64. package/dist/studio/assets/{just-CjAPsU_y.js → just-BHQm0Atq.js} +1 -1
  65. package/dist/studio/assets/{latex-CghDKqKz.js → latex-CxdhxuXv.js} +1 -1
  66. package/dist/studio/assets/{liquid-Cp9loVkE.js → liquid-D-hM-Q_F.js} +1 -1
  67. package/dist/studio/assets/{lua-HNnIDxnY.js → lua-5qtqjLHq.js} +1 -1
  68. package/dist/studio/assets/{marko-BXT10Apm.js → marko-5FJaM2co.js} +1 -1
  69. package/dist/studio/assets/{mdc-CaiOz4_A.js → mdc-BhYW7lCG.js} +1 -1
  70. package/dist/studio/assets/mermaid-GHXKKRXX-CWM2EB-u.js +1 -0
  71. package/dist/studio/assets/{nginx-DgYZ5SY9.js → nginx-rtAXrRye.js} +1 -1
  72. package/dist/studio/assets/{nim-CAMVX-MV.js → nim-DQSmaN9U.js} +1 -1
  73. package/dist/studio/assets/{perl-Dc6Wp7SD.js → perl-B54AdcaE.js} +1 -1
  74. package/dist/studio/assets/{php-DgLi8LNU.js → php-DpvTxCny.js} +1 -1
  75. package/dist/studio/assets/{pug-ZHFv4Vgd.js → pug-Cyffw0Ho.js} +1 -1
  76. package/dist/studio/assets/{qml-DU62cSZp.js → qml-CxRetSkO.js} +1 -1
  77. package/dist/studio/assets/{r-BwU69RSh.js → r-CXWRxrXc.js} +1 -1
  78. package/dist/studio/assets/{razor-BUgi_XZM.js → razor-BJzI-zc9.js} +1 -1
  79. package/dist/studio/assets/{regexp-DuNhqfkW.js → regexp-dqxdfrnE.js} +1 -1
  80. package/dist/studio/assets/{rst-BB3BeaSy.js → rst-BW7eKYj4.js} +1 -1
  81. package/dist/studio/assets/{ruby-T0tkEfUZ.js → ruby-D5Npyj8-.js} +1 -1
  82. package/dist/studio/assets/{sas-CMhZa3S0.js → sas-DZLppjul.js} +1 -1
  83. package/dist/studio/assets/{scss-DtXjZ3vZ.js → scss-CbqoxyAe.js} +1 -1
  84. package/dist/studio/assets/{shellscript-BESt-FLs.js → shellscript-CFHqBeXu.js} +1 -1
  85. package/dist/studio/assets/{shellsession-8irtvYxL.js → shellsession-CbZDA4Bf.js} +1 -1
  86. package/dist/studio/assets/{soy-CIvhOgLR.js → soy-PImjtV58.js} +1 -1
  87. package/dist/studio/assets/{sql-obAOXh1G.js → sql-DhGo8urW.js} +1 -1
  88. package/dist/studio/assets/{stata-D3OCK8xW.js → stata-CjHWbU2v.js} +1 -1
  89. package/dist/studio/assets/{surrealql-Dk_bguB0.js → surrealql-o8Qr4t56.js} +1 -1
  90. package/dist/studio/assets/{svelte-C0-KQt-t.js → svelte-DlSIyEV1.js} +1 -1
  91. package/dist/studio/assets/{templ-lg4nvF9H.js → templ-qizfwU1w.js} +1 -1
  92. package/dist/studio/assets/{tex-BNcQSX7R.js → tex-Ca7T661H.js} +1 -1
  93. package/dist/studio/assets/{ts-tags-AgW9jVv0.js → ts-tags-DUmjLkgl.js} +1 -1
  94. package/dist/studio/assets/{tsx-CL8XyR3d.js → tsx-CfwgIqHW.js} +1 -1
  95. package/dist/studio/assets/{twig-VkSbsH0Y.js → twig-D7NAYUBY.js} +1 -1
  96. package/dist/studio/assets/{typescript-BfD1znAG.js → typescript-Bbz9oX-P.js} +1 -1
  97. package/dist/studio/assets/{vue-html-w3sbjuft.js → vue-html-CcxTb_Jn.js} +1 -1
  98. package/dist/studio/assets/{vue-gv_2i8Vk.js → vue-vYpz06zQ.js} +1 -1
  99. package/dist/studio/assets/{vue-vine-C9sgcRlL.js → vue-vine-DW4RqCKF.js} +1 -1
  100. package/dist/studio/assets/{xml-nNPxMKnG.js → xml-R4LZ_u4f.js} +1 -1
  101. package/dist/studio/assets/{xsl-C7iHP36x.js → xsl-CS-xzp3L.js} +1 -1
  102. package/dist/studio/assets/{yaml-Cq3YaG6o.js → yaml-CqDWbYXd.js} +1 -1
  103. package/dist/studio/index.html +2 -2
  104. package/dist/think.d.ts +3 -2
  105. package/dist/think.js +8406 -2
  106. package/dist/think.js.map +1 -0
  107. package/dist/tools/extensions.js +1 -1
  108. package/dist/tools/fetch.d.ts +22 -0
  109. package/dist/tools/fetch.js +646 -0
  110. package/dist/tools/fetch.js.map +1 -0
  111. package/dist/tools/workspace.js +2 -2
  112. package/dist/tools/workspace.js.map +1 -1
  113. package/dist/{types-codegen-B6c5KpYH.js → types-codegen-C-9yeFao.js} +9 -33
  114. package/dist/types-codegen-C-9yeFao.js.map +1 -0
  115. package/dist/vite.d.ts +5 -5
  116. package/dist/vite.js +1 -1
  117. package/dist/workflows.d.ts +1 -1
  118. package/docs/actions.md +297 -0
  119. package/docs/channels.md +185 -0
  120. package/docs/client-tools.md +233 -0
  121. package/docs/getting-started.md +314 -0
  122. package/docs/index.md +1324 -0
  123. package/docs/lifecycle-hooks.md +850 -0
  124. package/docs/messengers.md +197 -0
  125. package/docs/programmatic-submissions.md +206 -0
  126. package/docs/sub-agents.md +524 -0
  127. package/docs/tools.md +582 -0
  128. package/docs/workflows.md +217 -0
  129. package/package.json +50 -17
  130. package/dist/chat-sdk-Bb93yaZh.js.map +0 -1
  131. package/dist/codegen-CuPdJRpV.js.map +0 -1
  132. package/dist/framework/index.js.map +0 -1
  133. package/dist/studio/assets/index-Cbb3GlZY.js +0 -496
  134. package/dist/studio/assets/index-DN9_cL01.css +0 -2
  135. package/dist/studio/assets/mermaid-GHXKKRXX-I2ZcYo7k.js +0 -1
  136. package/dist/think-BYZXQ-vm.js +0 -6513
  137. package/dist/think-BYZXQ-vm.js.map +0 -1
  138. package/dist/types-codegen-B6c5KpYH.js.map +0 -1
@@ -0,0 +1,197 @@
1
+ # Think Messengers
2
+
3
+ Use messengers when a Think agent should receive and reply to Chat SDK webhooks
4
+ directly. Think owns the webhook route, durable reply fiber, conversation
5
+ routing, and streamed delivery back to the provider.
6
+
7
+ ## Install
8
+
9
+ Install the Think package and the provider adapter you use:
10
+
11
+ ```bash
12
+ npm install @cloudflare/think agents ai @chat-adapter/telegram
13
+ ```
14
+
15
+ Provider adapters are exported from provider-specific subpaths so unused
16
+ adapters are not bundled into your Worker.
17
+
18
+ ## Telegram
19
+
20
+ ```typescript
21
+ import { Think } from "@cloudflare/think";
22
+ import { ThinkMessengerStateAgent } from "@cloudflare/think/messengers";
23
+ import telegramMessenger from "@cloudflare/think/messengers/telegram";
24
+
25
+ export { ThinkMessengerStateAgent };
26
+
27
+ export class SupportAgent extends Think<Env> {
28
+ getMessengers() {
29
+ return {
30
+ telegram: telegramMessenger({
31
+ token: this.env.TELEGRAM_BOT_TOKEN,
32
+ userName: "support_bot",
33
+ secretToken: this.env.TELEGRAM_WEBHOOK_SECRET_TOKEN
34
+ })
35
+ };
36
+ }
37
+ }
38
+ ```
39
+
40
+ With the default `telegram` key, register the Telegram webhook at:
41
+
42
+ ```text
43
+ https://<your-worker>/messengers/telegram/webhook
44
+ ```
45
+
46
+ `telegramMessenger()` requires `secretToken` in webhook mode unless you pass a
47
+ custom `verifyWebhook` function or explicitly opt out with
48
+ `verifyWebhook: false`.
49
+
50
+ If one Think agent owns multiple Telegram bots, give each provider a distinct
51
+ Chat SDK adapter name:
52
+
53
+ ```typescript
54
+ {
55
+ support: telegramMessenger({
56
+ adapterName: "support-telegram",
57
+ token: this.env.SUPPORT_TELEGRAM_BOT_TOKEN,
58
+ userName: "support_bot",
59
+ secretToken: this.env.SUPPORT_TELEGRAM_WEBHOOK_SECRET_TOKEN
60
+ }),
61
+ sales: telegramMessenger({
62
+ adapterName: "sales-telegram",
63
+ token: this.env.SALES_TELEGRAM_BOT_TOKEN,
64
+ userName: "sales_bot",
65
+ secretToken: this.env.SALES_TELEGRAM_WEBHOOK_SECRET_TOKEN
66
+ })
67
+ };
68
+ ```
69
+
70
+ Duplicate adapter names fail during startup so providers cannot overwrite each
71
+ other in the shared Chat SDK runtime.
72
+
73
+ ## Routing
74
+
75
+ The root Think agent handles messenger webhook routes after framework sub-agent
76
+ routing and Think internal routes, but before user-defined `onRequest` fallback.
77
+ Messenger routes are root-only. Defining `getMessengers()` on a sub-agent class
78
+ does not create webhook routes for that sub-agent.
79
+
80
+ By default, Think replies to direct messages and mentions. New mentions subscribe
81
+ the Chat SDK thread so later mentions in the same thread are still observed, but
82
+ ordinary subscribed-thread messages and button actions are ignored unless you
83
+ opt in:
84
+
85
+ ```typescript
86
+ telegramMessenger({
87
+ token: this.env.TELEGRAM_BOT_TOKEN,
88
+ userName: "support_bot",
89
+ secretToken: this.env.TELEGRAM_WEBHOOK_SECRET_TOKEN,
90
+ respondTo: ["direct-message", "mention", "subscribed-thread", "action"]
91
+ });
92
+ ```
93
+
94
+ Action events are converted into Think user messages with the action id, value,
95
+ source message id, and initiating user. Use `getMessengerContext()?.action`
96
+ inside hooks or tools when you need provider-specific action details. Actions
97
+ are opt-in so interactive cards do not accidentally trigger model turns.
98
+
99
+ ## Conversation Targets
100
+
101
+ The default conversation mode is one Think sub-agent per Chat SDK thread. This
102
+ keeps group chats, direct messages, and channels from sharing memory
103
+ accidentally.
104
+
105
+ Use the root agent as the conversation when all messenger traffic should share
106
+ one Think session:
107
+
108
+ ```typescript
109
+ telegramMessenger({
110
+ token: this.env.TELEGRAM_BOT_TOKEN,
111
+ userName: "support_bot",
112
+ secretToken: this.env.TELEGRAM_WEBHOOK_SECRET_TOKEN,
113
+ conversation: "self"
114
+ });
115
+ ```
116
+
117
+ Use a resolver when routing depends on tenant, channel, thread, or user:
118
+
119
+ ```typescript
120
+ telegramMessenger({
121
+ token: this.env.TELEGRAM_BOT_TOKEN,
122
+ userName: "support_bot",
123
+ secretToken: this.env.TELEGRAM_WEBHOOK_SECRET_TOKEN,
124
+ conversation(event) {
125
+ return {
126
+ target: "subagent",
127
+ name: `tenant:${event.thread.channelId ?? event.thread.id}`
128
+ };
129
+ }
130
+ });
131
+ ```
132
+
133
+ ## State
134
+
135
+ Messenger state is backed by `agents/chat-sdk`. Export
136
+ `ThinkMessengerStateAgent` from the Worker module so sub-agent routing can
137
+ resolve it. Production applications do not need a separate Durable Object
138
+ binding or migration for this facet-only state class. Test harnesses may still
139
+ need explicit bindings.
140
+
141
+ ## Delivery and Recovery
142
+
143
+ Think replies with the streamed `chat()` path. The root agent starts an
144
+ idempotent managed fiber, resolves the conversation target, calls
145
+ `target.chat(message, callback)`, and lets the provider delivery policy post or
146
+ edit visible messages.
147
+
148
+ Recovery snapshots store only serializable event and Chat SDK thread data. If a
149
+ restart happens before streaming starts, Think can replay the answer. If a
150
+ restart happens after streaming starts, Think posts the configured interruption
151
+ message instead of risking a duplicate partial answer.
152
+
153
+ Delivery errors use a generic user-facing message by default so internal
154
+ exception details are not posted into external chats. Override
155
+ `delivery.errorResponseText` when you want a custom safe message.
156
+
157
+ ## Messenger Context
158
+
159
+ During a messenger turn, `getMessengerContext()` returns provider, thread,
160
+ author, message, capabilities, and attachment metadata for the initiating event.
161
+ Use it from prompts, tools, or hooks that need channel-specific behavior.
162
+
163
+ ```typescript
164
+ const messenger = this.getMessengerContext();
165
+ if (messenger?.thread.isDirectMessage === false) {
166
+ // Adjust behavior for group chats.
167
+ }
168
+ ```
169
+
170
+ ## Custom Chat SDK Adapters
171
+
172
+ Use `chatSdkMessenger()` for providers that do not have a Think helper yet:
173
+
174
+ ```typescript
175
+ chatSdkMessenger({
176
+ adapter,
177
+ provider: "custom",
178
+ userName: "custom_bot",
179
+ verifyWebhook(request) {
180
+ return request.headers.get("x-custom-signature") === expectedSignature;
181
+ }
182
+ });
183
+ ```
184
+
185
+ Every custom messenger must provide `verifyWebhook` or explicitly use
186
+ `verifyWebhook: false`.
187
+
188
+ ## Advanced Manual Ingress
189
+
190
+ The `examples/think-chat-sdk` example demonstrates the Think-native
191
+ `getMessengers()` path with a small Vite dashboard that inspects the root Think
192
+ conversation over the Agent websocket.
193
+
194
+ The `examples/chat-sdk-messenger` example demonstrates a larger manual ingress
195
+ agent with an admin dashboard, menu handling, and application-owned reply
196
+ fibers. Use `getMessengers()` for the simple Think-native path. Use the example
197
+ when you need to own the Chat SDK runtime and control-plane UI yourself.
@@ -0,0 +1,206 @@
1
+ # Programmatic Submissions
2
+
3
+ Use `submitMessages()` when a caller needs to start a Think turn, return
4
+ quickly, retry safely, and inspect the result later.
5
+
6
+ `saveMessages()` waits for the turn to finish. `submitMessages()` durably accepts
7
+ the turn and returns a submission record before inference runs.
8
+
9
+ For a broader comparison with `chat()`, `saveMessages()`, and agent tools, see
10
+ [Choosing a turn API](./index.md#choosing-a-turn-api).
11
+
12
+ Declarative scheduled prompt tasks use the same durable submission path under
13
+ the hood. Use `getScheduledTasks()` when the trigger is recurring and
14
+ code-declared; use `submitMessages()` directly when an external caller or
15
+ webhook creates one-off work.
16
+
17
+ ## Why this exists
18
+
19
+ Webhook handlers, RPC callers, and parent Workers often have strict timeout
20
+ limits. If they call `saveMessages()` and time out, they cannot tell whether the
21
+ turn was never accepted, is queued, is running, or already completed. Retrying
22
+ can duplicate a user message and start a second turn.
23
+
24
+ `submitMessages()` creates a durable acceptance boundary:
25
+
26
+ ```typescript
27
+ const submission = await this.submitMessages(
28
+ [
29
+ {
30
+ id: crypto.randomUUID(),
31
+ role: "user",
32
+ parts: [{ type: "text", text: "Process webhook event 123" }]
33
+ }
34
+ ],
35
+ { idempotencyKey: "webhook-event-123" }
36
+ );
37
+ ```
38
+
39
+ The caller can return `submission.submissionId` immediately. Later, call
40
+ `inspectSubmission(submissionId)` or `listSubmissions()` to inspect status.
41
+
42
+ ## API
43
+
44
+ ```typescript
45
+ const submission = await this.submitMessages(messages, {
46
+ submissionId: "optional-stable-id",
47
+ idempotencyKey: "external-job-id",
48
+ metadata: { source: "webhook" }
49
+ });
50
+ ```
51
+
52
+ `submitMessages()` accepts serializable `UIMessage[]` values. It does not accept
53
+ the function form supported by `saveMessages((messages) => ...)`, because
54
+ durable submissions persist work before execution and cannot store closures.
55
+ The array must contain at least one message.
56
+
57
+ ## Statuses
58
+
59
+ | Status | Meaning |
60
+ | ----------- | ---------------------------------------------- |
61
+ | `pending` | Accepted and waiting for its turn |
62
+ | `running` | Claimed by the agent and executing |
63
+ | `completed` | The Think turn completed successfully |
64
+ | `aborted` | The submission was cancelled |
65
+ | `skipped` | Turn state was reset before the submission ran |
66
+ | `error` | Execution failed or recovery was unsafe |
67
+
68
+ ## Idempotent retries
69
+
70
+ Pass an `idempotencyKey` from your external system. Retrying with the same key
71
+ returns the existing submission with `accepted: false` instead of inserting
72
+ duplicate messages:
73
+
74
+ ```typescript
75
+ const first = await this.submitMessages(messages, {
76
+ idempotencyKey: payload.id
77
+ });
78
+
79
+ const retry = await this.submitMessages(messages, {
80
+ idempotencyKey: payload.id
81
+ });
82
+
83
+ // retry.submissionId === first.submissionId
84
+ // retry.accepted === false
85
+ ```
86
+
87
+ If you pass both `submissionId` and `idempotencyKey`, they must identify the same
88
+ submission. If they point at different existing rows, `submitMessages()` throws
89
+ instead of choosing one identity over the other.
90
+
91
+ ## Inspect, list, and cancel
92
+
93
+ ```typescript
94
+ const current = await this.inspectSubmission(submission.submissionId);
95
+
96
+ const active = await this.listSubmissions({
97
+ status: ["pending", "running"]
98
+ });
99
+
100
+ await this.cancelSubmission(submission.submissionId, "No longer needed");
101
+ ```
102
+
103
+ Use `cancelSubmission(submissionId)` for durable cancellation. This works across
104
+ Worker and Durable Object RPC boundaries, unlike `AbortSignal`.
105
+
106
+ Completed submission records are retained until you delete them:
107
+
108
+ ```typescript
109
+ await this.deleteSubmissions({
110
+ status: ["completed", "error", "aborted"],
111
+ completedBefore: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
112
+ });
113
+ ```
114
+
115
+ ## Session behavior
116
+
117
+ Think stores accepted submissions in a submission ledger first. It appends the
118
+ submitted messages to the conversation `Session` only when the submission starts
119
+ executing. This preserves FIFO turn semantics: later accepted submissions are
120
+ not visible to the model until their own turn starts.
121
+
122
+ If you cancel a submission before its messages have been applied, including one
123
+ that has been claimed but is still waiting for its turn, those messages are not
124
+ persisted to the conversation.
125
+
126
+ If the chat is cleared or turn state is reset before a pending submission runs,
127
+ that submission is marked `skipped`.
128
+
129
+ ## Compared with `saveMessages`
130
+
131
+ Use `saveMessages()` when the caller can wait for the full turn:
132
+
133
+ ```typescript
134
+ const result = await this.saveMessages(messages);
135
+ // result.status is final
136
+ ```
137
+
138
+ Use `submitMessages()` when timeout ambiguity would make retries unsafe:
139
+
140
+ ```typescript
141
+ const result = await this.submitMessages(messages, { idempotencyKey });
142
+ // result.status is the accepted submission state
143
+ ```
144
+
145
+ `waitUntilStable()` is still useful when the caller needs to avoid accepting new
146
+ work while the current chat UI is mid-turn. It is not required for durable
147
+ admission: accepted submissions are serialized by Think and do not append their
148
+ messages to the session until their own turn starts.
149
+
150
+ ## Compared with Workflows
151
+
152
+ Use `submitMessages()` for one durable Think chat turn.
153
+
154
+ Use Workflows for multi-step orchestration: retries per step, long waits,
155
+ external events, human approvals, or pipelines that may trigger Think as one
156
+ part of a larger process.
157
+
158
+ Use `startFiber()` when the durable unit is agent-owned side-effect work around
159
+ a turn, such as accepting a webhook once, restoring a serialized chat thread,
160
+ and posting a visible reply. `submitMessages()` owns Think's message/session
161
+ admission; `startFiber()` owns the surrounding application job.
162
+
163
+ ## Layering with `startFiber()`
164
+
165
+ Use the two APIs together when the external job is larger than the Think turn
166
+ itself:
167
+
168
+ ```typescript
169
+ await this.startFiber(
170
+ "reply-to-webhook",
171
+ async (ctx) => {
172
+ ctx.stash({ webhookId, threadId });
173
+
174
+ const submission = await this.submitMessages(messages, {
175
+ idempotencyKey: `turn:${webhookId}`,
176
+ metadata: { threadId }
177
+ });
178
+
179
+ await postVisibleReply(threadId, submission.submissionId);
180
+ },
181
+ {
182
+ idempotencyKey: `webhook:${webhookId}`,
183
+ waitForCompletion: true
184
+ }
185
+ );
186
+ ```
187
+
188
+ The outer managed fiber answers: "Did this webhook job get accepted, recovered,
189
+ cancelled, or resolved?" The inner submission answers: "Did this Think turn get
190
+ admitted to the session and complete?" Keep those boundaries separate. Do not
191
+ replace Think's internal `chatRecovery` fibers with managed fibers unless the
192
+ work is an application-owned job that callers need to inspect or dedupe.
193
+
194
+ Workflows can compose with this API:
195
+
196
+ ```typescript
197
+ const submission = await this.agent.submitMessages(messages, {
198
+ idempotencyKey: event.payload.jobId
199
+ });
200
+ ```
201
+
202
+ ## Example
203
+
204
+ See [Think Durable Submissions](https://github.com/cloudflare/agents/tree/main/examples/think-submissions/README.md) for
205
+ a full dashboard that shows immediate ACKs, idempotent retry, queue status, and
206
+ cancellation.