Skip to content

Commit 8b95a98

Browse files
committed
Updates
1 parent cc6d524 commit 8b95a98

File tree

2,261 files changed

+33063
-25889
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,261 files changed

+33063
-25889
lines changed

404.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<script defer src="/static/js/lib-react.016d396d.js"></script>
1010
<script defer src="/static/js/lib-router.db7238c9.js"></script>
1111
<script defer src="/static/js/14995.320c8577.js"></script>
12-
<script defer src="/static/js/index.847f21d1.js"></script>
12+
<script defer src="/static/js/index.c2eb31bd.js"></script>
1313
<meta http-equiv="X-UA-Compatible" content="IE=edge">
1414
<meta name="generator" content="Rspress v2.0.2">
1515
<link rel="icon" href="/icon.png">

TestFlight/guide/AppIntent.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

TestFlight/guide/Assistant/Assistant Conversation APIs.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

TestFlight/guide/Assistant/Assistant Quick Start.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

TestFlight/guide/Assistant/requestStreaming.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

TestFlight/guide/Assistant/requestStructuredData.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

TestFlight/guide/AssistantTool/Assistant Tool With User Approval.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

TestFlight/guide/AssistantTool/Assistant Tool Without User Approval.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

TestFlight/guide/AssistantTool/AssistantTool User-Initiated Cancellation.html

Lines changed: 224 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
# AssistantTool User-Initiated Cancellation
2+
3+
To improve the user experience of long-running tools, AssistantTool introduces support for **user-initiated cancellation**.
4+
When a user cancels a tool while it is executing, developers may optionally provide an `onCancel` callback to return partially completed results. If `onCancel` is not implemented, cancellation is handled automatically by the system and no additional logic is required.
5+
6+
This mechanism is particularly suitable for search, analysis, crawling, batch processing, and other multi-step or time-consuming tools.
7+
8+
***
9+
10+
## Capability Overview
11+
12+
The cancellation mechanism introduces the following APIs:
13+
14+
```ts
15+
type OnCancel = () => string | null | undefined
16+
17+
var onCancel: OnCancel | null | undefined
18+
19+
const isCancelled: boolean
20+
```
21+
22+
***
23+
24+
## Core Semantics
25+
26+
### onCancel Is Optional
27+
28+
- Implementing `onCancel` is optional
29+
- Not implementing `onCancel` is a fully valid and supported usage
30+
31+
When `onCancel` is not set:
32+
33+
- If the user clicks Cancel, the tool is marked as cancelled
34+
- Any results returned by the execution function after cancellation are automatically ignored
35+
- The Assistant does not consume or process those results
36+
37+
The outcome is that developers do not need to write any additional logic to handle cancellation unless they explicitly want to return partial results.
38+
39+
***
40+
41+
### Purpose of onCancel
42+
43+
The sole purpose of implementing `onCancel` is to proactively return **partially completed results** when a user cancels execution.
44+
45+
It is an experience optimization, not a required responsibility.
46+
47+
***
48+
49+
## Semantics of isCancelled
50+
51+
- `AssistantTool.isCancelled` becomes `true` immediately after the user cancels
52+
- It can be read at any point during execution
53+
- It is intended to control whether subsequent steps should continue running
54+
55+
Typical uses include breaking loops, skipping expensive operations, and releasing resources early.
56+
57+
***
58+
59+
## When and Where to Register onCancel
60+
61+
`onCancel` must be registered **inside the tool execution function**.
62+
63+
Reasons:
64+
65+
- Each tool execution has its own lifecycle
66+
- After execution completes, the system automatically resets `onCancel` to `null`
67+
- Registering `onCancel` outside the execution function has no effect
68+
69+
Valid locations include:
70+
71+
- `registerExecuteTool`
72+
- `registerExecuteToolWithApproval`
73+
74+
***
75+
76+
## Minimal Example Without onCancel
77+
78+
```ts
79+
const executeTool = async () => {
80+
await doSomethingSlow()
81+
82+
return {
83+
success: true,
84+
message: "Operation completed."
85+
}
86+
}
87+
```
88+
89+
Behavior:
90+
91+
- If the user cancels, the tool is marked as cancelled
92+
- The returned result is automatically ignored
93+
- No additional cancellation handling is required
94+
95+
***
96+
97+
## Example With onCancel and Partial Results
98+
99+
```ts
100+
const executeTool = async () => {
101+
const partialResults: string[] = []
102+
103+
AssistantTool.onCancel = () => {
104+
return [
105+
"Operation was cancelled by the user.",
106+
"Partial results:",
107+
...partialResults
108+
].join("\n")
109+
}
110+
111+
for (const item of items) {
112+
if (AssistantTool.isCancelled) break
113+
const result = await process(item)
114+
partialResults.push(result)
115+
}
116+
117+
return {
118+
success: true,
119+
message: partialResults.join("\n")
120+
}
121+
}
122+
```
123+
124+
Behavior:
125+
126+
- When the user cancels, `onCancel` is invoked immediately
127+
- Partially completed results are returned to the Assistant
128+
- Subsequent execution stops based on `isCancelled`
129+
130+
***
131+
132+
## Typical Use Cases
133+
134+
Good candidates for implementing `onCancel` include:
135+
136+
- Multi-source search and aggregation
137+
- Crawling and parsing multiple documents
138+
- Project-wide scanning and analysis
139+
- Batch computation or generation tasks
140+
- Long-running reasoning or processing pipelines
141+
142+
Cases where `onCancel` is usually unnecessary include:
143+
144+
- Tools that complete almost instantly
145+
- Operations with no meaningful intermediate results
146+
- Tasks where cancellation produces no useful partial output
147+
148+
***
149+
150+
## Return Value Guidelines for onCancel
151+
152+
Return values from `onCancel` follow these rules:
153+
154+
- Returning a `string`
155+
The string is used as the cancellation message sent to the Assistant
156+
157+
- Returning `null` or `undefined`
158+
Indicates that no message should be returned (valid but generally not recommended)
159+
160+
Recommended practices:
161+
162+
- Clearly state that the operation was cancelled by the user
163+
- Explicitly label partial output as partial results when applicable
164+
165+
***
166+
167+
## Relationship to the Approval Flow
168+
169+
- `onCancel` applies only during the execution phase
170+
- It does not affect the Approval Request phase
171+
- It works for both manually approved tools and auto-approved tools
172+
173+
Important distinction:
174+
175+
- `secondaryConfirmed`
176+
Indicates the user declined execution during the approval phase
177+
178+
- `onCancel`
179+
Indicates the user cancelled during execution after approval
180+
181+
***
182+
183+
## Common Mistakes and Caveats
184+
185+
### Registering onCancel Outside the Execution Function
186+
187+
This is ineffective because the execution context has already ended.
188+
189+
***
190+
191+
### Performing Expensive or Side-Effect Operations in onCancel
192+
193+
`onCancel` should return quickly and must not initiate network requests, file writes, or other side effects.
194+
195+
***
196+
197+
### Ignoring isCancelled During Execution
198+
199+
Even with `onCancel` implemented, long-running logic should explicitly check `isCancelled` to avoid unnecessary resource usage.
200+
201+
***
202+
203+
## Recommended Execution Structure
204+
205+
```ts
206+
const executeTool = async () => {
207+
const partial = []
208+
209+
AssistantTool.onCancel = () => {
210+
return formatPartialResult(partial)
211+
}
212+
213+
for (const item of items) {
214+
if (AssistantTool.isCancelled) break
215+
const r = await process(item)
216+
partial.push(r)
217+
}
218+
219+
if (AssistantTool.isCancelled) {
220+
return
221+
}
222+
223+
return {
224+
success: true,
225+
message: formatFinalResult(partial)
226+
}
227+
}
228+
```
229+
230+
***
231+
232+
## Design Philosophy
233+
234+
- User cancellation is a normal interaction, not an error
235+
- `onCancel` is an optional enhancement, not a requirement
236+
- Not implementing `onCancel` does not break tool behavior
237+
- The system automatically ignores results after cancellation
238+
- Implement `onCancel` only to provide a more graceful completion experience
239+
240+
***
241+
242+
## Summary
243+
244+
- AssistantTool supports user-initiated cancellation during execution
245+
- `onCancel` allows returning partially completed results
246+
- `isCancelled` enables early termination of execution logic
247+
- Cancellation is safely handled even without custom logic
248+
- Developers are not required to manage cancellation explicitly

0 commit comments

Comments
 (0)