Files
windmill/chat-sdk/src/react.ts
T
hugocasaandClaude Opus 5.5 c6a93a9b56 feat: chat with a saved agent from a raw app through an agent runnable (#11921)
* feat(chat-sdk): chat through a raw app's backend runnable

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(chat-sdk): finish a lost stream and wait out a stopped turn in streamChatTurn

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(chat-sdk): show the answer when a stream is lost before a tool call

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(chat-sdk): send attachments through the raw app runnable

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(chat-sdk): point sandboxed attachments at the runnable section

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: agent runnables for raw apps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: build agent runnables without the parquet feature, cache the agent check query

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: address review nits on the agent picker and the CLI schema doc

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: run an agent runnable once when it is given no memory_id

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: accept previous_messages on agent runs, read-checking the files it quotes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: refuse an anonymous agent turn while one with the same memory still runs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: show the agent's settings in the raw-app runnable panel, size its button like the row

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: offer an agent runnable memory_id or previous_messages after the agent's memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: take previous_messages in a memory-off agent's run form, type agent attachments as s3object

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: file an anonymous agent turn as a conversation, so its memory is collected and its turns serialized

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: own anonymous agent conversations under a fixed name that fits created_by

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: own anonymous agent conversations as anonymous and keep them out of conversation lists

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: point file_chat_turn at the anonymous conversation owner

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: title the runnable picker after what it lists, in sentence case

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: start a chat with local history listing its stored conversations

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat: chat through a raw app's agent runnable with { wmill, runnable }

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: key an agent runnable chat's local history by its app as well

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: address review findings on agent runnables

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: offer memory_id to an agent runnable over an older auto memory

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: drop inherited static inputs from an agent runnable, show the memory-off note only when it is off

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 21:01:09 +02:00

83 lines
3.5 KiB
TypeScript

import { useEffect, useMemo, useRef, useSyncExternalStore } from 'react'
import { createChat } from './chat'
import type { Chat, ChatOptions, ChatState, RawAppWmill, TypedChatOptions } from './types'
export type UseWindmillChat = ChatState &
Pick<
Chat,
| 'sendMessage'
| 'resumeTurn'
| 'stop'
| 'newConversation'
| 'selectConversation'
| 'loadConversations'
| 'deleteConversation'
| 'renameConversation'
| 'loadOlderMessages'
| 'refreshMessages'
> & { chat: Chat }
/**
* A chat on a chat-mode flow. The chat is created once per `flowPath`, `baseUrl`,
* `workspace`, `history`, `storageKey`, credential and presence of `run` and `follow`, and
* destroyed on unmount. A credential change is a new user, whose chat must not
* carry the previous one's state: a different token string, or a switch between no
* token, a token string and a token function, all recreate it. A token function is
* read through a ref on every call, so a new closure per render changes what the
* next call runs and nothing else; pass a `storageKey` per user when local history
* must not be shared. `run`, `follow`, the callbacks and `inputs` are read the same way: the
* latest render's values go with the next message.
*/
export function useWindmillChat<W extends RawAppWmill>(options: TypedChatOptions<W>): UseWindmillChat {
const latest = useRef<ChatOptions>(options)
latest.current = options
const credential =
typeof options.token === 'function' ? 'fn' : typeof options.token === 'string' ? `str:${options.token}` : 'none'
// A custom runner replaces the deployed flow call, so its presence is part of what the chat is.
const customRun = options.run !== undefined
const customFollow = options.follow !== undefined
const chat = useMemo(
() =>
createChat({
...options,
// Sent per message from the latest render instead, so a removed key stays removed.
inputs: undefined,
token:
typeof options.token === 'function'
? () => {
const token = latest.current.token
return typeof token === 'function' ? token() : (token ?? '')
}
: options.token,
run: customRun ? (args, turn) => (latest.current.run ?? options.run!)(args, turn) : undefined,
follow: customFollow
? (jobId, onStream, turn) => (latest.current.follow ?? options.follow!)(jobId, onStream, turn)
: undefined,
onFinish: (turn) => latest.current.onFinish?.(turn),
onError: (error, turn) => latest.current.onError?.(error, turn)
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[options.flowPath, options.runnable, options.wmill, options.baseUrl, options.workspace, options.history, options.storageKey, credential, customRun, customFollow]
)
useEffect(() => () => chat.destroy(), [chat])
const state = useSyncExternalStore(chat.subscribe, chat.getState, chat.getState)
return useMemo(
() => ({
...state,
chat,
sendMessage: (text, options) =>
chat.sendMessage(text, { ...options, inputs: { ...latest.current.inputs, ...options?.inputs } }),
resumeTurn: chat.resumeTurn,
stop: chat.stop,
newConversation: chat.newConversation,
selectConversation: chat.selectConversation,
loadConversations: chat.loadConversations,
deleteConversation: chat.deleteConversation,
renameConversation: chat.renameConversation,
loadOlderMessages: chat.loadOlderMessages,
refreshMessages: chat.refreshMessages
}),
[state, chat]
)
}