AI › AI Tools

Saving release drafts after approval with TanStack AI

Preview a TanStack AI tool call in React and inspect what browser storage contains after approval, denial, or a failed write.

Saving release drafts after approval with TanStack AI

I added a draft review step to my Jekyll release-note demo. React shows the Markdown from the proposed tool call, then writes it to browser storage after I approve it.

I ran the demo with Next.js 16.4.6, React 19.3.1, Zod 4.6.7, @tanstack/ai 1.10.1, @tanstack/ai-react 1.3.0, and @tanstack/ai-ollama 1.1.1. A mock Ollama endpoint supplied the tool call and the follow-up text. I exercised the approval UI, resume requests, and browser storage in Chrome.

Give the reviewer the exact draft

In the streaming demo I used a real model to summarize the Jekyll 4.4.0 release notes. Here I keep the source excerpt on the left and show the Markdown supplied to save_release_draft on the right. The question is when that string reaches storage. I fixed the model response so approval and denial would start with the same proposal. Client tools

I used a single localStorage key and displayed the value read from it underneath the approval card. That gave me a way to check the side effect separately from the follow-up sentence in the chat. The schema checks the shape and length of the Markdown string. Reviewing its content means comparing it with the release notes.

Storage requirementWhere I’d put the work
Keep one draft in the browser where it was approvedWrite to localStorage in a .client() implementation.
Share a draft with a team or access it across devicesUse a server tool that checks access to the target before writing to shared storage.
Pick up a pending approval after a refreshStore the conversation and run state with chat persistence.

Client and server tool placement · Chat persistence

Share the tool definition and attach the browser implementation

The route and the client import the same definition. It specifies the input, output, and approval requirement. The route passes the definition to chat(). The React module attaches the actual storage function with .client(). Import toolDefinition from @tanstack/ai and z from zod. The demo’s draft-saved event tells the page to read the key again after a successful write. Defining and implementing client tools

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// lib/draft.ts
export const storageKey = 'ai-tools.release-draft'

export const saveReleaseDraft = toolDefinition({
  name: 'save_release_draft',
  description: 'Save a release-draft Markdown string to this browser.',
  inputSchema: z.object({ markdown: z.string().min(1).max(4000) }),
  outputSchema: z.object({ saved: z.boolean() }),
  needsApproval: true,
})

// In the React client module
const saveReleaseDraftClient = saveReleaseDraft.client(({ markdown }) => {
  try {
    localStorage.setItem(storageKey, markdown)
    window.dispatchEvent(new Event('draft-saved'))
    return { saved: true }
  } catch {
    return { saved: false }
  }
})

Forward the resume data on the next POST

This excerpt goes inside POST in app/api/approval/route.ts. The main difference from the streaming route is that it forwards resume and registers the shared tool definition. Port 4423 points to the fixture. Its Ollama-compatible POST /api/chat streams NDJSON. The first response contains a save_release_draft tool call. After the tool result comes back, the fixture sends a short sentence that reflects the outcome. Ollama Chat API · TanStack AI tool approval

1
2
3
4
5
6
7
8
9
10
11
12
const params = await chatParamsFromRequest(request)
const stream = chat({
  stream: true,
  adapter: createOllamaChat('qwen3:8b', 'http://127.0.0.1:4423'),
  messages: params.messages,
  threadId: params.threadId,
  runId: params.runId,
  parentRunId: params.parentRunId,
  ...(params.resume ? { resume: params.resume } : {}),
  tools: [saveReleaseDraft],
})
return toServerSentEventsResponse(stream)

I made approvalConnection outside the component with fetchServerSentEvents('/api/approval'). The card reads originalArgs.markdown. Its buttons become available when canResolve is true and resuming is false. I passed undefined as the second argument to resolveInterrupt to satisfy the types in the tested package versions. Earlier examples use addToolApprovalResponse. Interrupt migration guide

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const { interrupts, resuming } = useChat({
  connection: approvalConnection,
  threadId: 'release-approval',
  tools: [saveReleaseDraftClient],
})
const approvals = interrupts.filter((entry) => entry.kind === 'tool-approval')

// Inside the return tree
{approvals.map((approval) => (
  <div key={approval.id}>
    <pre>{approval.originalArgs.markdown}</pre>
    <button disabled={!approval.canResolve || resuming}
      onClick={() => void approval.resolveInterrupt(true, undefined)}>
      Approve &amp; save
    </button>
    <button disabled={!approval.canResolve || resuming}
      onClick={() => void approval.resolveInterrupt(false, undefined)}>
      Deny
    </button>
  </div>
))}

One approval click produced two resume requests in the browser trace. The first carried { approved: true } and the second carried { saved: true } after the client function completed. A denial sent { approved: false }, and the server recorded a denied tool result. The requests make it possible to trace the user’s decision separately from the success or failure of the write.

Check the stored value through each outcome

React approval card with the mock Markdown proposal above an empty browser storage section Figure 1. The fixture’s draft is visible in the review card. The browser storage slot is empty.

React approval demo showing the reviewed draft in browser storage after approval Figure 2. After approval, the storage section shows the same text as the earlier preview.

React approval demo after denial with an existing draft still visible in browser storage Figure 3. I seeded a different draft for the denial case. It remained in the slot after I clicked Deny.

Scenario I exercised in ChromeObserved result
Wait for approval with an empty slotThe proposal appeared while the stored value was still null.
ApproveThe client stored the exact preview string once. It was still there after reload.
Deny with an existing draftThe existing value stayed in place. The fixture received the tool result User declined tool execution.
Approve with an injected setItem exceptionThe tool returned { saved: false } and the existing value remained.
Reload with a pending proposalThe approval card disappeared and the stored draft remained. This demo uses no chat persistence.

What I’d keep in a larger app

  • Put the full argument the user will approve in the review card. Read the tool result and the actual saved value when debugging the write.
  • Track the decision and the write as separate events. An approved write can still fail.
  • If the tool moves to shared storage, check the caller’s access to the target as well as the human approval.
This post is licensed under CC BY 4.0 by the author.