better-mastra 0.2.0 → 0.3.0
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.
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +1 -0
- package/dist/client/index.js.map +1 -1
- package/dist/client/messages.d.ts +10 -0
- package/dist/client/messages.d.ts.map +1 -1
- package/dist/client/messages.js +30 -0
- package/dist/client/messages.js.map +1 -1
- package/dist/client/resume.d.ts +27 -0
- package/dist/client/resume.d.ts.map +1 -0
- package/dist/client/resume.js +46 -0
- package/dist/client/resume.js.map +1 -0
- package/dist/client/session.d.ts +5 -0
- package/dist/client/session.d.ts.map +1 -1
- package/dist/client/session.js +36 -1
- package/dist/client/session.js.map +1 -1
- package/dist/client/turn.d.ts +4 -0
- package/dist/client/turn.d.ts.map +1 -1
- package/dist/client/turn.js +16 -0
- package/dist/client/turn.js.map +1 -1
- package/dist/dictation/react/index.d.ts +4 -2
- package/dist/dictation/react/index.d.ts.map +1 -1
- package/dist/dictation/react/index.js +9 -1
- package/dist/dictation/react/index.js.map +1 -1
- package/dist/dictation/session.d.ts +5 -1
- package/dist/dictation/session.d.ts.map +1 -1
- package/dist/dictation/session.js +25 -5
- package/dist/dictation/session.js.map +1 -1
- package/dist/dictation/vue/index.d.ts +4 -2
- package/dist/dictation/vue/index.d.ts.map +1 -1
- package/dist/dictation/vue/index.js +9 -1
- package/dist/dictation/vue/index.js.map +1 -1
- package/dist/evals/checks.d.ts +2 -1
- package/dist/evals/checks.d.ts.map +1 -1
- package/dist/evals/checks.js +1 -1
- package/dist/evals/checks.js.map +1 -1
- package/dist/evals/cli.js +21 -2
- package/dist/evals/cli.js.map +1 -1
- package/dist/evals/judge.d.ts +16 -2
- package/dist/evals/judge.d.ts.map +1 -1
- package/dist/evals/judge.js +13 -4
- package/dist/evals/judge.js.map +1 -1
- package/dist/evals/mastra.d.ts.map +1 -1
- package/dist/evals/mastra.js +17 -2
- package/dist/evals/mastra.js.map +1 -1
- package/dist/evals/run.d.ts +6 -3
- package/dist/evals/run.d.ts.map +1 -1
- package/dist/evals/run.js +6 -8
- package/dist/evals/run.js.map +1 -1
- package/dist/hono/chatRoute.d.ts +7 -2
- package/dist/hono/chatRoute.d.ts.map +1 -1
- package/dist/hono/chatRoute.js +10 -6
- package/dist/hono/chatRoute.js.map +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +1 -0
- package/dist/react/index.js.map +1 -1
- package/dist/turn/agent.d.ts.map +1 -1
- package/dist/turn/agent.js +4 -1
- package/dist/turn/agent.js.map +1 -1
- package/dist/turn/index.d.ts +3 -3
- package/dist/turn/index.d.ts.map +1 -1
- package/dist/turn/index.js +1 -1
- package/dist/turn/index.js.map +1 -1
- package/dist/turn/runChatTurn.d.ts +10 -5
- package/dist/turn/runChatTurn.d.ts.map +1 -1
- package/dist/turn/runChatTurn.js +38 -2
- package/dist/turn/runChatTurn.js.map +1 -1
- package/dist/turn/stream.d.ts +8 -0
- package/dist/turn/stream.d.ts.map +1 -1
- package/dist/turn/stream.js +11 -0
- package/dist/turn/stream.js.map +1 -1
- package/dist/turn/threads.d.ts +17 -2
- package/dist/turn/threads.d.ts.map +1 -1
- package/dist/turn/threads.js +18 -5
- package/dist/turn/threads.js.map +1 -1
- package/dist/turn/types.d.ts +23 -4
- package/dist/turn/types.d.ts.map +1 -1
- package/dist/turn/types.js.map +1 -1
- package/dist/vue/index.d.ts +1 -1
- package/dist/vue/index.d.ts.map +1 -1
- package/dist/vue/index.js +1 -0
- package/dist/vue/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client/index.ts +1 -0
- package/src/client/messages.ts +41 -0
- package/src/client/resume.ts +82 -0
- package/src/client/session.ts +53 -1
- package/src/client/turn.ts +19 -0
- package/src/dictation/react/index.ts +16 -3
- package/src/dictation/session.ts +38 -5
- package/src/dictation/vue/index.ts +16 -3
- package/src/evals/checks.ts +3 -3
- package/src/evals/cli.ts +24 -2
- package/src/evals/judge.ts +38 -6
- package/src/evals/mastra.ts +23 -4
- package/src/evals/run.ts +8 -8
- package/src/hono/chatRoute.ts +18 -9
- package/src/react/index.ts +2 -0
- package/src/turn/agent.ts +6 -2
- package/src/turn/index.ts +3 -0
- package/src/turn/runChatTurn.ts +61 -5
- package/src/turn/stream.ts +15 -0
- package/src/turn/threads.ts +47 -6
- package/src/turn/types.ts +25 -4
- package/src/vue/index.ts +2 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/client/session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAEN,oBAAoB,EAEpB,WAAW,EACX,YAAY,EACZ,mDAAmD,EAEnD,MAAM,IAAI,CAAA;AAEX,OAAO,EAIN,eAAe,EACf,gBAAgB,EAChB,MAAM,cAAc,CAAA;AACrB,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAC1C,OAAO,EAEN,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EACjB,MAAM,aAAa,CAAA;AAEpB,OAAO,EACN,eAAe,EACf,qBAAqB,EACrB,gBAAgB,EAChB,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,qBAAqB,EAErB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAsB,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACrE,OAAO,EAKN,oBAAoB,EACpB,cAAc,EAEd,MAAM,WAAW,CAAA;AAyIlB,YAAY;AAEZ;;;GAGG;AACH,MAAM,qBAAqB,GAAG,GAAG,CAAA;AAEjC,MAAM,eAAe,GAAoC,IAAI,GAAG,EAAE,CAAA;AAElE,MAAM,oBAAoB,GAAsB,EAAE,CAAA;AAElD,MAAM,UAAU,GAA4B,EAAE,CAAA;AAE9C,YAAY;AAEZ,qCAAqC;AACrC,MAAM,UAAU,iBAAiB,CAChC,OAAwC;IAExC,M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AAC","sourcesContent":["/**\n * Owns one conversation: the AI SDK `Chat`, the turn reducer around it, and\n * the effects the reducer asks for. It lives outside any framework, so every\n * screen that shows the thread shows the same running answer. The reducer,\n * not the chat's request status, decides when a user turn is over, and every\n * request is one of its effects, so a thread loaded from history never\n * continues on its own.\n *\n * Client actions the answer calls are carried out when its request ends, and\n * their results go back as the user turn's follow-up request, together with\n * an error for each server tool call the request left without a result.\n * Changes the answer leaves waiting go to the reducer: the AI SDK sends the\n * answers to its approvals once the reducer waits for them, and a resume goes\n * out as a request with `resume`.\n *\n * With `resume`, the server keeps a run going when the connection drops: a\n * dropped request reattaches to its stream once instead of failing, Stop\n * also tells the server to stop, and `resumeStream()` reattaches to a running\n * answer on mount, when the app comes back to the foreground and when the\n * network returns.\n */\n\nimport {\n\ttype ChatInit,\n\tDefaultChatTransport,\n\ttype FileUIPart,\n\tgetToolName,\n\tisToolUIPart,\n\tlastAssistantMessageIsCompleteWithApprovalResponses,\n\ttype UIMessage\n} from 'ai'\n\nimport {\n\ttype ClientAction,\n\ttype ClientToolCall,\n\ttype ClientToolDef,\n\trunClientAction,\n\ttoClientToolDefs\n} from './actions.js'\nimport { ObservableChat } from './chat.js'\nimport {\n\ttype ClassifyChatError,\n\tclassifyChatError,\n\tisConnectionLost,\n\treadErrorResponse\n} from './errors.js'\nimport type { ChatFetch } from './fetch.js'\nimport {\n\tcountShownParts,\n\tfindStrandedToolCalls,\n\tisUserFacingPart,\n\tREPLY_SUGGESTIONS_PART,\n\treadReplySuggestions,\n\tSTRANDED_TOOL_ERROR,\n\tselectRequestMessages,\n\ttype ToolPart\n} from './messages.js'\nimport { type PendingChange, readWaitingChanges } from './pending.js'\nimport {\n\ttype ChangeMark,\n\ttype ChatTurnEffect,\n\ttype ChatTurnEvent,\n\ttype ChatTurnState,\n\tinitialChatTurnState,\n\treduceChatTurn,\n\ttype RunResume\n} from './turn.js'\n\n// TYPES\n\n/** What a client renders from a session besides the messages. */\nexport interface ChatSessionSnapshot {\n\treadonly turn: ChatTurnState\n\t/** A message the composer gets back, numbered so each one is applied once. */\n\treadonly restoredDraft: RestoredDraft | undefined\n\t/** What became of each change that stopped waiting without an answer, by tool call id. */\n\treadonly changeMarks: ReadonlyMap<string, ChangeMark>\n\t/** The replies offered after the newest answer; empty once the user writes. */\n\treadonly replySuggestions: readonly string[]\n}\n\nexport interface RestoredDraft {\n\treadonly text: string\n\treadonly sequence: number\n}\n\n/** Everything one request carries, before `prepareBody` shapes it. */\nexport interface ChatRequest<TResumeData = unknown> {\n\treadonly threadId: string\n\t/** The messages `selectRequestMessages` picked. */\n\treadonly messages: UIMessage[]\n\treadonly trigger: 'submit-message' | 'regenerate-message'\n\t/** What a resumed suspended run continues with. */\n\treadonly resume: RunResume<TResumeData> | undefined\n\t/** The client actions this request offers, as definitions. */\n\treadonly clientTools: readonly ClientToolDef[]\n}\n\n/** How a failed request is reported for analytics: counts and enums only. */\nexport interface ChatFailureReport {\n\t/** A refusal code, `network`, `server_error` for any other failed response, `stream_error` when the stream carried the failure. */\n\treadonly code: string\n\t/** Whether the session tries again on its own. */\n\treadonly retrying: boolean\n}\n\n/** A signal that the app may have missed a running answer: it came to the foreground, the network returned. */\nexport type ResumeSignal = (resume: () => void) => () => void\n\n/** `TResumeData` types what `resumeRun` takes and `prepareBody` reads back. */\nexport interface ChatSessionOptions<TResumeData = unknown> {\n\t/** The thread; the chat's id, and the `:threadId` of the stream and stop routes. */\n\treadonly threadId: string\n\treadonly transport: {\n\t\t/** The chat route. Reattaching reads `GET {api}/:threadId/stream`, Stop posts `POST {api}/:threadId/stop`. */\n\t\treadonly api: string\n\t\t/** Usually `createChatFetch(...)`. */\n\t\treadonly fetch?: ChatFetch\n\t\treadonly credentials?: RequestInit['credentials']\n\t}\n\t/** Messages to start with, such as the thread's stored page. */\n\treadonly messages?: UIMessage[]\n\t/** The JSON body of one request; the request as it is when left out. */\n\treadonly prepareBody?: (\n\t\trequest: ChatRequest<TResumeData>\n\t) => object | Promise<object>\n\t/** The client actions requests offer and the session carries out, read per request: the screen on top may change. */\n\treadonly clientActions?:\n\t\t| readonly ClientAction[]\n\t\t| (() => readonly ClientAction[])\n\t/** Reads a refusal from a response that is not OK. */\n\treadonly classifyError?: ClassifyChatError\n\t/** Which parts count as shown; observational memory's never do. */\n\treadonly isUserFacingPart?: (part: UIMessage['parts'][number]) => boolean\n\t/** The server keeps a run going when the connection drops, and serves its stream and stop routes. */\n\treadonly resume?: boolean\n\t/** When to reattach to a running answer, with `resume`. */\n\treadonly resumeOn?: readonly ResumeSignal[]\n\t/** A tool's result arrived on the live stream, once per call: refresh the app's data after a write. */\n\treadonly onToolResult?: (result: {\n\t\treadonly toolCallId: string\n\t\treadonly toolName: string\n\t\treadonly output: unknown\n\t}) => void\n\treadonly onFailure?: (report: ChatFailureReport) => void\n\treadonly onData?: ChatInit<UIMessage>['onData']\n\treadonly generateId?: () => string\n}\n\n/** The user's answer to one waiting approval. */\nexport interface ApprovalAnswer {\n\treadonly approvalId: string\n\treadonly approved: boolean\n\t/** What the model reads in place of the result when the user declines. */\n\treadonly reason?: string\n}\n\nexport interface ChatSession<TResumeData = unknown> {\n\treadonly threadId: string\n\treadonly chat: ObservableChat\n\treadonly getSnapshot: () => ChatSessionSnapshot\n\treadonly subscribe: (listener: () => void) => () => void\n\t/** Sends a message, or queues it while a user turn runs. A message with files cannot queue: false, and the composer keeps it. */\n\treadonly send: (\n\t\ttext: string,\n\t\toptions?: { readonly files?: readonly FileUIPart[] }\n\t) => boolean\n\treadonly stop: () => void\n\treadonly tryAgain: () => void\n\t/** Answers an approval the user turn waits for; any other approval is left alone. */\n\treadonly answerApproval: (answer: ApprovalAnswer) => void\n\t/** Resumes a suspended run the user turn waits for, once. */\n\tresumeRun(resume: RunResume<TResumeData>): void\n\t/** The composer has put the restored message in its input; it is not handed back again. */\n\treadonly clearRestoredDraft: (sequence: number) => void\n\t/**\n\t * Replaces the chat's messages with the thread's stored page, only while\n\t * no user turn runs. Its pending changes then wait for the user, and its\n\t * tool results never count as new.\n\t */\n\treadonly showStored: (page: {\n\t\t/** The page's messages, with `rebuildPendingApprovals` applied. */\n\t\treadonly messages: readonly UIMessage[]\n\t\treadonly pendingChanges: readonly PendingChange[]\n\t}) => void\n\t/** Reattaches to the thread's running answer, with `resume`, when nothing runs here. */\n\treadonly resumeStream: () => void\n\t/** A session that is not on screen stops its answer and sends nothing: a message it held goes back to its composer. */\n\treadonly setFocused: (focused: boolean) => void\n\t/** Stops the answer and every resume signal. */\n\treadonly dispose: () => void\n}\n\n/** How the AI SDK said one request ended. */\ninterface RequestEnd {\n\treadonly isAbort: boolean\n\treadonly isDisconnect: boolean\n\treadonly isError: boolean\n\treadonly error: Error | undefined\n}\n\ntype RequestKind = 'send' | 'resume'\n\n// CONSTANTS\n\n/**\n * The status a failure gets when the response was OK and its stream carried\n * the server's error part: the server already retried before any answer text.\n */\nconst STREAM_FAILURE_STATUS = 200\n\nconst NO_CHANGE_MARKS: ReadonlyMap<string, ChangeMark> = new Map()\n\nconst NO_REPLY_SUGGESTIONS: readonly string[] = []\n\nconst NO_ACTIONS: readonly ClientAction[] = []\n\n// FUNCTIONS\n\n/** A conversation for one thread. */\nexport function createChatSession<TResumeData = unknown>(\n\toptions: ChatSessionOptions<TResumeData>\n): ChatSession<TResumeData> {\n\tconst { threadId } = options\n\tconst classifyError = options.classifyError ?? classifyChatError\n\tconst isUserFacing = options.isUserFacingPart ?? isUserFacingPart\n\tconst resumable = options.resume === true\n\tconst listeners = new Set<() => void>()\n\tlet snapshot: ChatSessionSnapshot = {\n\t\tturn: initialChatTurnState,\n\t\trestoredDraft: undefined,\n\t\tchangeMarks: NO_CHANGE_MARKS,\n\t\treplySuggestions: NO_REPLY_SUGGESTIONS\n\t}\n\tlet focused = true\n\t/** Tool results already reported or loaded from history, by tool call id: each is reported once, as it arrives. */\n\tconst knownResults = new Set<string>()\n\t/** Errors the session posted for stranded server calls; a follow-up carries them like client results. */\n\tconst postedErrors = new Set<string>()\n\t/** The AI SDK sends the approval answers itself, without a promise to wait on, so its `onFinish` ends that request. */\n\tlet sendsApprovalAnswers = false\n\t/** The client action calls of the request in flight, carried out once it ends. */\n\tlet actionCalls: ClientToolCall[] = []\n\t/** Counts the requests, so actions of an earlier request never send a later turn's follow-up. */\n\tlet requestCount = 0\n\t/** The parts the answer showed when the request started; only new ones count as shown. */\n\tlet shownPartsBefore = 0\n\t/** Try again writes the answer again from the user's message, rather than going on with a failed one. */\n\tlet startsOver = false\n\t/** Once the user writes, the replies still arriving come too late to show. */\n\tlet offersReplies = true\n\t/** The resume the request in flight sends, read by the transport. */\n\tlet pendingResume: RunResume | undefined\n\tlet requestError: Error | undefined\n\tlet requestEnd: RequestEnd | undefined\n\n\tconst clientActions = (): readonly ClientAction[] => {\n\t\tconst actions = options.clientActions\n\n\t\treturn typeof actions === 'function' ? actions() : (actions ?? NO_ACTIONS)\n\t}\n\tconst isClientAction = (toolName: string) =>\n\t\tclientActions().some((action) => action.name === toolName)\n\tconst isClientResult = (part: ToolPart) =>\n\t\tpostedErrors.has(part.toolCallId) || isClientAction(getToolName(part))\n\n\tconst chat = new ObservableChat<UIMessage>({\n\t\tid: threadId,\n\t\t...(options.messages === undefined ? {} : { messages: options.messages }),\n\t\t...(options.generateId === undefined\n\t\t\t? {}\n\t\t\t: { generateId: options.generateId }),\n\t\ttransport: new DefaultChatTransport<UIMessage>({\n\t\t\tapi: options.transport.api,\n\t\t\t...(options.transport.fetch === undefined\n\t\t\t\t? {}\n\t\t\t\t: { fetch: options.transport.fetch as typeof fetch }),\n\t\t\t...(options.transport.credentials === undefined\n\t\t\t\t? {}\n\t\t\t\t: { credentials: options.transport.credentials }),\n\t\t\t// The server's stream route replays the run from its start.\n\t\t\tresumeStreamIsReplay: true,\n\t\t\tprepareSendMessagesRequest: async ({ messages, trigger }) => {\n\t\t\t\tconst request: ChatRequest<TResumeData> = {\n\t\t\t\t\tthreadId,\n\t\t\t\t\tmessages: selectRequestMessages(messages, { isClientResult }),\n\t\t\t\t\ttrigger,\n\t\t\t\t\t// The session only stores what `resumeRun` was given.\n\t\t\t\t\tresume: pendingResume as RunResume<TResumeData> | undefined,\n\t\t\t\t\tclientTools: toClientToolDefs(clientActions())\n\t\t\t\t}\n\n\t\t\t\treturn { body: await (options.prepareBody ?? defaultBody)(request) }\n\t\t\t}\n\t\t}),\n\t\tonError: (error) => {\n\t\t\trequestError = error\n\t\t},\n\t\tonData: (part) => {\n\t\t\toptions.onData?.(part)\n\n\t\t\tif (part.type !== REPLY_SUGGESTIONS_PART || !offersReplies) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tconst replies = readReplySuggestions(part.data)\n\n\t\t\tif (replies !== undefined) {\n\t\t\t\tpublish({ ...snapshot, replySuggestions: replies })\n\t\t\t}\n\t\t},\n\t\t// Never awaits the action: the AI SDK runs this callback on the queue\n\t\t// `addToolOutput` waits on, so awaiting deadlocks the stream. The call\n\t\t// is carried out once the request ends.\n\t\tonToolCall: ({ toolCall }) => {\n\t\t\tif (isClientAction(toolCall.toolName)) {\n\t\t\t\tactionCalls.push({\n\t\t\t\t\ttoolCallId: toolCall.toolCallId,\n\t\t\t\t\ttoolName: toolCall.toolName,\n\t\t\t\t\tinput: toolCall.input\n\t\t\t\t})\n\t\t\t}\n\t\t},\n\t\tonFinish: ({ isAbort, isDisconnect, isError }) => {\n\t\t\trequestEnd = { isAbort, isDisconnect, isError, error: requestError }\n\n\t\t\tif (sendsApprovalAnswers) {\n\t\t\t\tsendsApprovalAnswers = false\n\t\t\t\tqueueMicrotask(() => finishRequest('send'))\n\t\t\t}\n\t\t},\n\t\t// Once the user has answered every approval of the step, the AI SDK\n\t\t// sends the answers on its own, but only for a user turn that waits.\n\t\tsendAutomaticallyWhen: ({ messages }) =>\n\t\t\tlastAssistantMessageIsCompleteWithApprovalResponses({ messages }) &&\n\t\t\tstartApprovalRequest()\n\t})\n\n\tchat.subscribe(() => {\n\t\tif (countShownParts(chat.lastMessage, isUserFacing) > shownPartsBefore) {\n\t\t\tdispatch({ kind: 'content-shown' })\n\t\t}\n\n\t\treportNewToolResults()\n\t})\n\n\tconst unsubscribeSignals = resumable\n\t\t? (options.resumeOn ?? []).map((signal) => signal(() => resumeStream()))\n\t\t: []\n\n\tfunction dispatch(event: ChatTurnEvent): readonly ChatTurnEffect[] {\n\t\tconst transition = reduceChatTurn(snapshot.turn, event)\n\n\t\tif (transition.state !== snapshot.turn) {\n\t\t\tpublish({ ...snapshot, turn: transition.state })\n\t\t}\n\n\t\tfor (const effect of transition.effects) {\n\t\t\trunEffect(effect)\n\t\t}\n\n\t\treturn transition.effects\n\t}\n\n\tfunction runEffect(effect: ChatTurnEffect): void {\n\t\tswitch (effect.kind) {\n\t\t\tcase 'send-message':\n\t\t\t\tif (focused) {\n\t\t\t\t\tshownPartsBefore = 0\n\t\t\t\t\tstartRequest('send', () =>\n\t\t\t\t\t\tchat.sendMessage(\n\t\t\t\t\t\t\teffect.files === undefined\n\t\t\t\t\t\t\t\t? { text: effect.text }\n\t\t\t\t\t\t\t\t: { text: effect.text, files: [...effect.files] }\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\trestoreDraft(effect.text)\n\t\t\t\t\tendUnsentRequest()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\tcase 'request':\n\t\t\t\tif (focused) {\n\t\t\t\t\tstartNextRequest()\n\t\t\t\t} else {\n\t\t\t\t\tendUnsentRequest()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\tcase 'resume-stream':\n\t\t\t\tstartRequest('resume', () => chat.resumeStream())\n\t\t\t\treturn\n\t\t\tcase 'abort-request':\n\t\t\t\tvoid chat.stop()\n\t\t\t\treturn\n\t\t\tcase 'restore-draft':\n\t\t\t\trestoreDraft(effect.text)\n\t\t\t\treturn\n\t\t\tcase 'resume-run':\n\t\t\t\tif (focused) {\n\t\t\t\t\tsendResume(effect.resume)\n\t\t\t\t} else {\n\t\t\t\t\tendUnsentRequest()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\tcase 'mark-changes':\n\t\t\t\tmarkChanges(effect.mark, effect.changes)\n\t\t\t\treturn\n\t\t}\n\n\t\teffect satisfies never\n\t}\n\n\tfunction restoreDraft(text: string): void {\n\t\tpublish({\n\t\t\t...snapshot,\n\t\t\trestoredDraft: {\n\t\t\t\ttext,\n\t\t\t\tsequence: (snapshot.restoredDraft?.sequence ?? 0) + 1\n\t\t\t}\n\t\t})\n\t}\n\n\t/**\n\t * A request that goes on with the answer on screen when its trailing\n\t * message holds the client's results (a follow-up, or a retry of one), and\n\t * writes it again otherwise.\n\t */\n\tfunction startNextRequest(): void {\n\t\tconst continues =\n\t\t\t!startsOver &&\n\t\t\tselectRequestMessages(chat.messages, { isClientResult }).at(-1)?.role ===\n\t\t\t\t'assistant'\n\n\t\tshownPartsBefore = continues\n\t\t\t? countShownParts(chat.lastMessage, isUserFacing)\n\t\t\t: 0\n\t\tstartRequest('send', () =>\n\t\t\tcontinues ? chat.sendMessage() : chat.regenerate()\n\t\t)\n\t}\n\n\t/** A resume goes on with the answer that shows the suspended run. */\n\tfunction sendResume(resume: RunResume): void {\n\t\tshownPartsBefore = countShownParts(chat.lastMessage, isUserFacing)\n\t\tstartRequest('send', () => chat.sendMessage(), resume)\n\t}\n\n\t/**\n\t * The AI SDK is about to send the user's answers to the approvals. The\n\t * reducer counts the request; it goes out only when the user turn on screen\n\t * waits for them.\n\t */\n\tfunction startApprovalRequest(): boolean {\n\t\tif (!focused) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst waited = snapshot.turn.kind === 'waiting'\n\t\tdispatch({ kind: 'approval-sent' })\n\n\t\tif (!waited || snapshot.turn.kind !== 'requesting') {\n\t\t\treturn false\n\t\t}\n\n\t\tresetRequest()\n\t\tshownPartsBefore = countShownParts(chat.lastMessage, isUserFacing)\n\t\tsendsApprovalAnswers = true\n\n\t\treturn true\n\t}\n\n\tfunction markChanges(\n\t\tmark: ChangeMark,\n\t\tchanges: readonly PendingChange[]\n\t): void {\n\t\tconst changeMarks = new Map(snapshot.changeMarks)\n\n\t\tfor (const change of changes) {\n\t\t\tchangeMarks.set(change.toolCallId, mark)\n\t\t}\n\n\t\tpublish({ ...snapshot, changeMarks })\n\t}\n\n\tfunction reportNewToolResults(): void {\n\t\tfor (const part of chat.lastMessage?.parts ?? []) {\n\t\t\tif (\n\t\t\t\t!isToolUIPart(part) ||\n\t\t\t\tpart.state !== 'output-available' ||\n\t\t\t\tknownResults.has(part.toolCallId)\n\t\t\t) {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tknownResults.add(part.toolCallId)\n\n\t\t\tconst toolName = getToolName(part)\n\n\t\t\tif (!isClientAction(toolName)) {\n\t\t\t\toptions.onToolResult?.({\n\t\t\t\t\ttoolCallId: part.toolCallId,\n\t\t\t\t\ttoolName,\n\t\t\t\t\toutput: part.output\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Ends a user turn whose request was never sent, the way Stop would. */\n\tfunction endUnsentRequest(): void {\n\t\tdispatch({ kind: 'stop-pressed' })\n\t\tdispatch({ kind: 'request-finished', continues: false })\n\t}\n\n\t/** Runs one request and turns how it ended into the reducer's event once the AI SDK is done with it. */\n\tfunction startRequest(\n\t\tkind: RequestKind,\n\t\trequest: () => Promise<void>,\n\t\tresume?: RunResume\n\t): void {\n\t\tresetRequest()\n\t\tpendingResume = resume\n\n\t\tvoid request().then(\n\t\t\t() => finishRequest(kind),\n\t\t\t(error: unknown) => finishRequest(kind, toError(error))\n\t\t)\n\t}\n\n\tfunction resetRequest(): void {\n\t\ttakeReplySuggestions()\n\t\toffersReplies = true\n\t\tpendingResume = undefined\n\t\trequestError = undefined\n\t\trequestEnd = undefined\n\t\tactionCalls = []\n\t\trequestCount += 1\n\t}\n\n\t/**\n\t * Changes the answer leaves waiting come first: the user answers them, and\n\t * a client action called beside one waits with the step.\n\t */\n\tfunction finishRequest(kind: RequestKind, rejection?: Error): void {\n\t\tconst end = endOf(kind, rejection)\n\n\t\tif (end === 'nothing-to-resume') {\n\t\t\tdispatch({ kind: 'nothing-to-resume' })\n\n\t\t\treturn\n\t\t}\n\n\t\tconst isAnswered = !end.isAbort && !end.isError\n\t\tconst calls = isAnswered ? actionCalls : []\n\t\tconst waiting =\n\t\t\tisAnswered && chat.lastMessage?.role === 'assistant'\n\t\t\t\t? readWaitingChanges(chat.lastMessage)\n\t\t\t\t: []\n\n\t\tactionCalls = []\n\n\t\tif (waiting.length > 0) {\n\t\t\tdispatch({ kind: 'changes-waiting', changes: waiting })\n\n\t\t\treturn\n\t\t}\n\n\t\tconst stranded = isAnswered\n\t\t\t? findStrandedToolCalls(chat.lastMessage, isClientAction)\n\t\t\t: []\n\n\t\tif (calls.length > 0 || stranded.length > 0) {\n\t\t\tdispatch({ kind: 'request-finished', continues: true })\n\n\t\t\t// Stop or a focus change ended the user turn as its request ended.\n\t\t\tif (snapshot.turn.kind === 'continuing') {\n\t\t\t\tvoid carryOut(calls, stranded, requestCount)\n\t\t\t}\n\n\t\t\treturn\n\t\t}\n\n\t\tconst event = toTurnEvent(end)\n\t\tconst effects = dispatch(event)\n\t\tconst code = failureCodeOf(event, end)\n\n\t\tif (code !== undefined) {\n\t\t\toptions.onFailure?.({\n\t\t\t\tcode,\n\t\t\t\tretrying: effects.some(\n\t\t\t\t\t(effect) =>\n\t\t\t\t\t\teffect.kind === 'request' ||\n\t\t\t\t\t\teffect.kind === 'resume-stream' ||\n\t\t\t\t\t\teffect.kind === 'resume-run'\n\t\t\t\t)\n\t\t\t})\n\t\t}\n\t}\n\n\t/**\n\t * How the request ended. A request the AI SDK never started (Stop while\n\t * the message was being prepared) still settles the reducer; a reattach\n\t * that found no stream ends without `onFinish`.\n\t */\n\tfunction endOf(\n\t\tkind: RequestKind,\n\t\trejection: Error | undefined\n\t): RequestEnd | 'nothing-to-resume' {\n\t\tif (rejection !== undefined) {\n\t\t\treturn {\n\t\t\t\tisAbort: false,\n\t\t\t\tisDisconnect: false,\n\t\t\t\tisError: true,\n\t\t\t\terror: rejection\n\t\t\t}\n\t\t}\n\n\t\tif (requestEnd !== undefined) {\n\t\t\treturn requestEnd\n\t\t}\n\n\t\tif (requestError !== undefined) {\n\t\t\treturn {\n\t\t\t\tisAbort: false,\n\t\t\t\tisDisconnect: false,\n\t\t\t\tisError: true,\n\t\t\t\terror: requestError\n\t\t\t}\n\t\t}\n\n\t\treturn kind === 'resume'\n\t\t\t? 'nothing-to-resume'\n\t\t\t: { isAbort: true, isDisconnect: false, isError: false, error: undefined }\n\t}\n\n\tfunction toTurnEvent(end: RequestEnd): ChatTurnEvent {\n\t\tif (end.isAbort || !end.isError) {\n\t\t\treturn { kind: 'request-finished', continues: false }\n\t\t}\n\n\t\tconst response = readErrorResponse(end.error)\n\n\t\tif (response !== undefined) {\n\t\t\treturn {\n\t\t\t\tkind: 'error-response',\n\t\t\t\tstatus: response.status,\n\t\t\t\trefusal: classifyError(response)\n\t\t\t}\n\t\t}\n\n\t\treturn isConnectionLost(end.error, end)\n\t\t\t? { kind: 'connection-lost', resumable }\n\t\t\t: {\n\t\t\t\t\tkind: 'error-response',\n\t\t\t\t\tstatus: STREAM_FAILURE_STATUS,\n\t\t\t\t\trefusal: undefined\n\t\t\t\t}\n\t}\n\n\t/**\n\t * Carries out what the answer asked for, posts every result and an error\n\t * for each stranded server call, then lets the reducer send the follow-up,\n\t * unless the user turn has moved on meanwhile. One at a time, so a repeat\n\t * in the same batch sees the call before it.\n\t */\n\tasync function carryOut(\n\t\tcalls: readonly ClientToolCall[],\n\t\tstranded: readonly {\n\t\t\treadonly toolCallId: string\n\t\t\treadonly toolName: string\n\t\t}[],\n\t\trequest: number\n\t): Promise<void> {\n\t\tfor (const call of stranded) {\n\t\t\tpostedErrors.add(call.toolCallId)\n\t\t\tawait settle(\n\t\t\t\tchat.addToolOutput({\n\t\t\t\t\tstate: 'output-error',\n\t\t\t\t\ttool: call.toolName,\n\t\t\t\t\ttoolCallId: call.toolCallId,\n\t\t\t\t\terrorText: STRANDED_TOOL_ERROR\n\t\t\t\t})\n\t\t\t)\n\t\t}\n\n\t\tfor (const call of calls) {\n\t\t\tawait settle(runClientAction(chat, clientActions(), call))\n\t\t}\n\n\t\tif (request === requestCount && snapshot.turn.kind === 'continuing') {\n\t\t\tdispatch({ kind: 'follow-up-ready' })\n\t\t}\n\t}\n\n\tfunction takeReplySuggestions(): void {\n\t\toffersReplies = false\n\n\t\tif (snapshot.replySuggestions.length > 0) {\n\t\t\tpublish({ ...snapshot, replySuggestions: NO_REPLY_SUGGESTIONS })\n\t\t}\n\t}\n\n\tfunction publish(next: ChatSessionSnapshot): void {\n\t\tsnapshot = next\n\n\t\tfor (const listener of listeners) {\n\t\t\tlistener()\n\t\t}\n\t}\n\n\t/** Stop pressed: the request is aborted, and a server that keeps runs going is told to stop. */\n\tfunction stop(): void {\n\t\tconst effects = dispatch({ kind: 'stop-pressed' })\n\n\t\tif (\n\t\t\tresumable &&\n\t\t\teffects.some((effect) => effect.kind === 'abort-request')\n\t\t) {\n\t\t\tvoid postStop()\n\t\t}\n\t}\n\n\tasync function postStop(): Promise<void> {\n\t\tconst send =\n\t\t\toptions.transport.fetch ??\n\t\t\t((input, init) => globalThis.fetch(input, init))\n\t\tconst url = `${options.transport.api.replace(/\\/$/, '')}/${encodeURIComponent(threadId)}/stop`\n\n\t\ttry {\n\t\t\tawait send(url, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\t...(options.transport.credentials === undefined\n\t\t\t\t\t? {}\n\t\t\t\t\t: { credentials: options.transport.credentials })\n\t\t\t})\n\t\t} catch {\n\t\t\t// The run ends on its own; Stop already ended the turn here.\n\t\t}\n\t}\n\n\tfunction resumeStream(): void {\n\t\tif (\n\t\t\t!resumable ||\n\t\t\t!focused ||\n\t\t\tchat.status === 'submitted' ||\n\t\t\tchat.status === 'streaming'\n\t\t) {\n\t\t\treturn\n\t\t}\n\n\t\tconst before = snapshot.turn\n\t\tdispatch({ kind: 'stream-watched' })\n\n\t\tif (snapshot.turn !== before) {\n\t\t\tstartRequest('resume', () => chat.resumeStream())\n\t\t}\n\t}\n\n\treturn {\n\t\tthreadId,\n\t\tchat,\n\t\tgetSnapshot: () => snapshot,\n\t\tsubscribe: (listener) => {\n\t\t\tlisteners.add(listener)\n\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener)\n\t\t\t}\n\t\t},\n\t\tsend: (text, sendOptions) => {\n\t\t\tconst before = snapshot.turn\n\t\t\tconst files = sendOptions?.files ?? []\n\n\t\t\tif (text.trim() !== '' || files.length > 0) {\n\t\t\t\ttakeReplySuggestions()\n\t\t\t}\n\n\t\t\tconst effects = dispatch(\n\t\t\t\tfiles.length === 0\n\t\t\t\t\t? { kind: 'message-submitted', text }\n\t\t\t\t\t: { kind: 'message-submitted', text, files }\n\t\t\t)\n\n\t\t\treturn snapshot.turn !== before || effects.length > 0\n\t\t},\n\t\tstop,\n\t\ttryAgain: () => {\n\t\t\tstartsOver = true\n\t\t\tdispatch({ kind: 'try-again-pressed' })\n\t\t\tstartsOver = false\n\t\t},\n\t\tanswerApproval: ({ approvalId, approved, reason }) => {\n\t\t\tconst turn = snapshot.turn\n\t\t\tconst waits =\n\t\t\t\tturn.kind === 'waiting' &&\n\t\t\t\tturn.changes.some(\n\t\t\t\t\t(change) =>\n\t\t\t\t\t\tchange.kind === 'approval' && change.approvalId === approvalId\n\t\t\t\t)\n\n\t\t\tif (waits) {\n\t\t\t\tvoid chat.addToolApprovalResponse({\n\t\t\t\t\tid: approvalId,\n\t\t\t\t\tapproved,\n\t\t\t\t\t...(reason === undefined ? {} : { reason })\n\t\t\t\t})\n\t\t\t}\n\t\t},\n\t\tresumeRun: (resume) => {\n\t\t\tdispatch({ kind: 'resume-pressed', resume })\n\t\t},\n\t\tclearRestoredDraft: (sequence) => {\n\t\t\tif (snapshot.restoredDraft?.sequence === sequence) {\n\t\t\t\tpublish({ ...snapshot, restoredDraft: undefined })\n\t\t\t}\n\t\t},\n\t\t// A failed turn keeps its messages: Try again sends them again.\n\t\tshowStored: ({ messages, pendingChanges }) => {\n\t\t\tif (snapshot.turn.kind !== 'idle') {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tfor (const part of messages.flatMap((message) => message.parts)) {\n\t\t\t\tif (isToolUIPart(part)) {\n\t\t\t\t\tknownResults.add(part.toolCallId)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchat.messages = [...messages]\n\n\t\t\tconst last = messages.at(-1)\n\t\t\tconst waiting = (\n\t\t\t\tlast?.role === 'assistant' ? readWaitingChanges(last) : []\n\t\t\t).filter((change) =>\n\t\t\t\tpendingChanges.some(\n\t\t\t\t\t(pending) =>\n\t\t\t\t\t\tpending.kind === change.kind &&\n\t\t\t\t\t\tpending.runId === change.runId &&\n\t\t\t\t\t\tpending.toolCallId === change.toolCallId\n\t\t\t\t)\n\t\t\t)\n\n\t\t\tif (waiting.length > 0) {\n\t\t\t\tdispatch({ kind: 'changes-waiting', changes: waiting })\n\t\t\t}\n\t\t},\n\t\tresumeStream,\n\t\tsetFocused: (isFocused) => {\n\t\t\tfocused = isFocused\n\n\t\t\tif (!isFocused) {\n\t\t\t\tstop()\n\t\t\t}\n\t\t},\n\t\tdispose: () => {\n\t\t\tfor (const unsubscribe of unsubscribeSignals) {\n\t\t\t\tunsubscribe()\n\t\t\t}\n\n\t\t\tvoid chat.stop()\n\t\t}\n\t}\n}\n\n/** The sessions of an app session by key (a thread, a workspace), created on first use and shared by every screen. */\nexport interface ChatSessions {\n\t/** The key's session, created the first time it is asked for. */\n\treadonly get: (key: string) => ChatSession\n\t/** Makes this the session on screen: every other one stops and sends nothing until it is on screen again. */\n\treadonly focus: (key: string) => void\n\t/** Stops the key's session and forgets it; the next screen that asks gets a new one. */\n\treadonly forget: (key: string) => void\n\t/** Stops and forgets every session, as on sign-out. */\n\treadonly clear: () => void\n}\n\nexport function createChatSessions(\n\tcreate: (key: string) => ChatSession\n): ChatSessions {\n\tconst sessions = new Map<string, ChatSession>()\n\n\treturn {\n\t\tget: (key) => {\n\t\t\tlet session = sessions.get(key)\n\n\t\t\tif (session === undefined) {\n\t\t\t\tsession = create(key)\n\t\t\t\tsessions.set(key, session)\n\t\t\t}\n\n\t\t\treturn session\n\t\t},\n\t\tfocus: (key) => {\n\t\t\tfor (const [sessionKey, session] of sessions) {\n\t\t\t\tsession.setFocused(sessionKey === key)\n\t\t\t}\n\t\t},\n\t\tforget: (key) => {\n\t\t\tsessions.get(key)?.dispose()\n\t\t\tsessions.delete(key)\n\t\t},\n\t\tclear: () => {\n\t\t\tfor (const session of sessions.values()) {\n\t\t\t\tsession.dispose()\n\t\t\t}\n\n\t\t\tsessions.clear()\n\t\t}\n\t}\n}\n\n/** The request as the body, with `resume` and `clientTools` only when there are any. */\nfunction defaultBody(request: ChatRequest): object {\n\treturn {\n\t\tthreadId: request.threadId,\n\t\tmessages: request.messages,\n\t\ttrigger: request.trigger,\n\t\t...(request.resume === undefined ? {} : { resume: request.resume }),\n\t\t...(request.clientTools.length === 0\n\t\t\t? {}\n\t\t\t: { clientTools: request.clientTools })\n\t}\n}\n\nfunction failureCodeOf(\n\tevent: ChatTurnEvent,\n\tend: RequestEnd\n): string | undefined {\n\tswitch (event.kind) {\n\t\tcase 'connection-lost':\n\t\t\treturn 'network'\n\t\tcase 'error-response':\n\t\t\tif (event.refusal !== undefined) {\n\t\t\t\treturn event.refusal.code\n\t\t\t}\n\n\t\t\treturn readErrorResponse(end.error) === undefined\n\t\t\t\t? 'stream_error'\n\t\t\t\t: 'server_error'\n\t\tdefault:\n\t\t\treturn undefined\n\t}\n}\n\n/** Waits for a result to be posted; one that fails to post still lets the turn go on. */\nasync function settle(posting: void | PromiseLike<void> | null): Promise<void> {\n\ttry {\n\t\tawait posting\n\t} catch {\n\t\t// The server reads the call as unanswered.\n\t}\n}\n\nfunction toError(reason: unknown): Error {\n\treturn reason instanceof Error ? reason : new Error(String(reason))\n}\n"]}
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/client/session.ts"],"names":[],"mappings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ducer asks for. It lives outside any framework, so every\n * screen that shows the thread shows the same running answer. The reducer,\n * not the chat's request status, decides when a user turn is over, and every\n * request is one of its effects, so a thread loaded from history never\n * continues on its own.\n *\n * Client actions the answer calls are carried out when its request ends, and\n * their results go back as the user turn's follow-up request, together with\n * an error for each server tool call the request left without a result.\n * Changes the answer leaves waiting go to the reducer: the AI SDK sends the\n * answers to its approvals once the reducer waits for them, and a resume goes\n * out as a request with `resume`.\n *\n * With `resume`, the server keeps a run going when the connection drops: a\n * dropped request reattaches to its stream once instead of failing, Stop\n * also tells the server to stop, and `resumeStream()` reattaches to a running\n * answer on mount, when the app comes back to the foreground and when the\n * network returns.\n */\n\nimport {\n\ttype ChatInit,\n\tDefaultChatTransport,\n\ttype FileUIPart,\n\tgetToolName,\n\tisToolUIPart,\n\tlastAssistantMessageIsCompleteWithApprovalResponses,\n\ttype UIMessage\n} from 'ai'\n\nimport {\n\ttype ClientAction,\n\ttype ClientToolCall,\n\ttype ClientToolDef,\n\trunClientAction,\n\ttoClientToolDefs\n} from './actions.js'\nimport { ObservableChat } from './chat.js'\nimport {\n\ttype ClassifyChatError,\n\tclassifyChatError,\n\tisConnectionLost,\n\treadErrorResponse\n} from './errors.js'\nimport type { ChatFetch } from './fetch.js'\nimport {\n\tcountShownParts,\n\tfindStrandedToolCalls,\n\tisUserFacingPart,\n\tREPLY_SUGGESTIONS_PART,\n\treadReplySuggestions,\n\treadUserMessage,\n\tSTRANDED_TOOL_ERROR,\n\tselectRequestMessages,\n\ttype ToolPart,\n\tUSER_MESSAGE_PART\n} from './messages.js'\nimport { type PendingChange, readWaitingChanges } from './pending.js'\nimport {\n\ttype ChangeMark,\n\ttype ChatTurnEffect,\n\ttype ChatTurnEvent,\n\ttype ChatTurnState,\n\tinitialChatTurnState,\n\treduceChatTurn,\n\ttype RunResume\n} from './turn.js'\n\n// TYPES\n\n/** What a client renders from a session besides the messages. */\nexport interface ChatSessionSnapshot {\n\treadonly turn: ChatTurnState\n\t/** A message the composer gets back, numbered so each one is applied once. */\n\treadonly restoredDraft: RestoredDraft | undefined\n\t/** What became of each change that stopped waiting without an answer, by tool call id. */\n\treadonly changeMarks: ReadonlyMap<string, ChangeMark>\n\t/** The replies offered after the newest answer; empty once the user writes. */\n\treadonly replySuggestions: readonly string[]\n}\n\nexport interface RestoredDraft {\n\treadonly text: string\n\treadonly sequence: number\n}\n\n/** Everything one request carries, before `prepareBody` shapes it. */\nexport interface ChatRequest<TResumeData = unknown> {\n\treadonly threadId: string\n\t/** The messages `selectRequestMessages` picked. */\n\treadonly messages: UIMessage[]\n\treadonly trigger: 'submit-message' | 'regenerate-message'\n\t/** What a resumed suspended run continues with. */\n\treadonly resume: RunResume<TResumeData> | undefined\n\t/** The client actions this request offers, as definitions. */\n\treadonly clientTools: readonly ClientToolDef[]\n}\n\n/** How a failed request is reported for analytics: counts and enums only. */\nexport interface ChatFailureReport {\n\t/** A refusal code, `network`, `server_error` for any other failed response, `stream_error` when the stream carried the failure. */\n\treadonly code: string\n\t/** Whether the session tries again on its own. */\n\treadonly retrying: boolean\n}\n\n/** A signal that the app may have missed a running answer: it came to the foreground, the network returned. */\nexport type ResumeSignal = (resume: () => void) => () => void\n\n/** `TResumeData` types what `resumeRun` takes and `prepareBody` reads back. */\nexport interface ChatSessionOptions<TResumeData = unknown> {\n\t/** The thread; the chat's id, and the `:threadId` of the stream and stop routes. */\n\treadonly threadId: string\n\treadonly transport: {\n\t\t/** The chat route. Reattaching reads `GET {api}/:threadId/stream`, Stop posts `POST {api}/:threadId/stop`. */\n\t\treadonly api: string\n\t\t/** Usually `createChatFetch(...)`. */\n\t\treadonly fetch?: ChatFetch\n\t\treadonly credentials?: RequestInit['credentials']\n\t}\n\t/** Messages to start with, such as the thread's stored page. */\n\treadonly messages?: UIMessage[]\n\t/** The JSON body of one request; the request as it is when left out. */\n\treadonly prepareBody?: (\n\t\trequest: ChatRequest<TResumeData>\n\t) => object | Promise<object>\n\t/** The client actions requests offer and the session carries out, read per request: the screen on top may change. */\n\treadonly clientActions?:\n\t\t| readonly ClientAction[]\n\t\t| (() => readonly ClientAction[])\n\t/** Reads a refusal from a response that is not OK. */\n\treadonly classifyError?: ClassifyChatError\n\t/** Which parts count as shown; observational memory's never do. */\n\treadonly isUserFacingPart?: (part: UIMessage['parts'][number]) => boolean\n\t/** The server keeps a run going when the connection drops, and serves its stream and stop routes. */\n\treadonly resume?: boolean\n\t/** When to reattach to a running answer, with `resume`. */\n\treadonly resumeOn?: readonly ResumeSignal[]\n\t/** A tool's result arrived on the live stream, once per call: refresh the app's data after a write. */\n\treadonly onToolResult?: (result: {\n\t\treadonly toolCallId: string\n\t\treadonly toolName: string\n\t\treadonly output: unknown\n\t}) => void\n\treadonly onFailure?: (report: ChatFailureReport) => void\n\treadonly onData?: ChatInit<UIMessage>['onData']\n\treadonly generateId?: () => string\n}\n\n/** The user's answer to one waiting approval. */\nexport interface ApprovalAnswer {\n\treadonly approvalId: string\n\treadonly approved: boolean\n\t/** What the model reads in place of the result when the user declines. */\n\treadonly reason?: string\n}\n\nexport interface ChatSession<TResumeData = unknown> {\n\treadonly threadId: string\n\treadonly chat: ObservableChat\n\treadonly getSnapshot: () => ChatSessionSnapshot\n\treadonly subscribe: (listener: () => void) => () => void\n\t/** Sends a message, or queues it while a user turn runs. A message with files cannot queue: false, and the composer keeps it. */\n\treadonly send: (\n\t\ttext: string,\n\t\toptions?: { readonly files?: readonly FileUIPart[] }\n\t) => boolean\n\treadonly stop: () => void\n\treadonly tryAgain: () => void\n\t/**\n\t * Writes the newest answer again from the user's message, when no user\n\t * turn runs or waits; the server replaces the stored answer.\n\t */\n\treadonly regenerate: () => void\n\t/** Answers an approval the user turn waits for; any other approval is left alone. */\n\treadonly answerApproval: (answer: ApprovalAnswer) => void\n\t/** Resumes a suspended run the user turn waits for, once. */\n\tresumeRun(resume: RunResume<TResumeData>): void\n\t/** The composer has put the restored message in its input; it is not handed back again. */\n\treadonly clearRestoredDraft: (sequence: number) => void\n\t/**\n\t * Replaces the chat's messages with the thread's stored page, only while\n\t * no user turn runs. Its pending changes then wait for the user, and its\n\t * tool results never count as new.\n\t */\n\treadonly showStored: (page: {\n\t\t/** The page's messages, with `rebuildPendingApprovals` applied. */\n\t\treadonly messages: readonly UIMessage[]\n\t\treadonly pendingChanges: readonly PendingChange[]\n\t}) => void\n\t/** Reattaches to the thread's running answer, with `resume`, when nothing runs here. */\n\treadonly resumeStream: () => void\n\t/** A session that is not on screen stops its answer and sends nothing: a message it held goes back to its composer. */\n\treadonly setFocused: (focused: boolean) => void\n\t/** Stops the answer and every resume signal. */\n\treadonly dispose: () => void\n}\n\n/** How the AI SDK said one request ended. */\ninterface RequestEnd {\n\treadonly isAbort: boolean\n\treadonly isDisconnect: boolean\n\treadonly isError: boolean\n\treadonly error: Error | undefined\n}\n\ntype RequestKind = 'send' | 'resume'\n\n// CONSTANTS\n\n/**\n * The status a failure gets when the response was OK and its stream carried\n * the server's error part: the server already retried before any answer text.\n */\nconst STREAM_FAILURE_STATUS = 200\n\nconst NO_CHANGE_MARKS: ReadonlyMap<string, ChangeMark> = new Map()\n\nconst NO_REPLY_SUGGESTIONS: readonly string[] = []\n\nconst NO_ACTIONS: readonly ClientAction[] = []\n\n// FUNCTIONS\n\n/** A conversation for one thread. */\nexport function createChatSession<TResumeData = unknown>(\n\toptions: ChatSessionOptions<TResumeData>\n): ChatSession<TResumeData> {\n\tconst { threadId } = options\n\tconst classifyError = options.classifyError ?? classifyChatError\n\tconst isUserFacing = options.isUserFacingPart ?? isUserFacingPart\n\tconst resumable = options.resume === true\n\tconst listeners = new Set<() => void>()\n\tlet snapshot: ChatSessionSnapshot = {\n\t\tturn: initialChatTurnState,\n\t\trestoredDraft: undefined,\n\t\tchangeMarks: NO_CHANGE_MARKS,\n\t\treplySuggestions: NO_REPLY_SUGGESTIONS\n\t}\n\tlet focused = true\n\t/** Tool results already reported or loaded from history, by tool call id: each is reported once, as it arrives. */\n\tconst knownResults = new Set<string>()\n\t/** Errors the session posted for stranded server calls; a follow-up carries them like client results. */\n\tconst postedErrors = new Set<string>()\n\t/** The AI SDK sends the approval answers itself, without a promise to wait on, so its `onFinish` ends that request. */\n\tlet sendsApprovalAnswers = false\n\t/** The client action calls of the request in flight, carried out once it ends. */\n\tlet actionCalls: ClientToolCall[] = []\n\t/** Counts the requests, so actions of an earlier request never send a later turn's follow-up. */\n\tlet requestCount = 0\n\t/** The parts the answer showed when the request started; only new ones count as shown. */\n\tlet shownPartsBefore = 0\n\t/** Try again writes the answer again from the user's message, rather than going on with a failed one. */\n\tlet startsOver = false\n\t/** Once the user writes, the replies still arriving come too late to show. */\n\tlet offersReplies = true\n\t/** The resume the request in flight sends, read by the transport. */\n\tlet pendingResume: RunResume | undefined\n\t/** The messages on screen when the request started; the answer it streams is not among them. */\n\tlet shownBeforeRequest = new Set<string>()\n\tlet requestError: Error | undefined\n\tlet requestEnd: RequestEnd | undefined\n\n\tconst clientActions = (): readonly ClientAction[] => {\n\t\tconst actions = options.clientActions\n\n\t\treturn typeof actions === 'function' ? actions() : (actions ?? NO_ACTIONS)\n\t}\n\tconst isClientAction = (toolName: string) =>\n\t\tclientActions().some((action) => action.name === toolName)\n\tconst isClientResult = (part: ToolPart) =>\n\t\tpostedErrors.has(part.toolCallId) || isClientAction(getToolName(part))\n\n\tconst chat = new ObservableChat<UIMessage>({\n\t\tid: threadId,\n\t\t...(options.messages === undefined ? {} : { messages: options.messages }),\n\t\t...(options.generateId === undefined\n\t\t\t? {}\n\t\t\t: { generateId: options.generateId }),\n\t\ttransport: new DefaultChatTransport<UIMessage>({\n\t\t\tapi: options.transport.api,\n\t\t\t...(options.transport.fetch === undefined\n\t\t\t\t? {}\n\t\t\t\t: { fetch: options.transport.fetch as typeof fetch }),\n\t\t\t...(options.transport.credentials === undefined\n\t\t\t\t? {}\n\t\t\t\t: { credentials: options.transport.credentials }),\n\t\t\t// The server's stream route replays the run from its start.\n\t\t\tresumeStreamIsReplay: true,\n\t\t\tprepareSendMessagesRequest: async ({ messages, trigger }) => {\n\t\t\t\tconst request: ChatRequest<TResumeData> = {\n\t\t\t\t\tthreadId,\n\t\t\t\t\tmessages: selectRequestMessages(messages, { isClientResult }),\n\t\t\t\t\ttrigger,\n\t\t\t\t\t// The session only stores what `resumeRun` was given.\n\t\t\t\t\tresume: pendingResume as RunResume<TResumeData> | undefined,\n\t\t\t\t\tclientTools: toClientToolDefs(clientActions())\n\t\t\t\t}\n\n\t\t\t\treturn { body: await (options.prepareBody ?? defaultBody)(request) }\n\t\t\t}\n\t\t}),\n\t\tonError: (error) => {\n\t\t\trequestError = error\n\t\t},\n\t\tonData: (part) => {\n\t\t\toptions.onData?.(part)\n\n\t\t\tif (part.type === USER_MESSAGE_PART) {\n\t\t\t\tshowQuestion(readUserMessage(part.data))\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tif (part.type !== REPLY_SUGGESTIONS_PART || !offersReplies) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tconst replies = readReplySuggestions(part.data)\n\n\t\t\tif (replies !== undefined) {\n\t\t\t\tpublish({ ...snapshot, replySuggestions: replies })\n\t\t\t}\n\t\t},\n\t\t// Never awaits the action: the AI SDK runs this callback on the queue\n\t\t// `addToolOutput` waits on, so awaiting deadlocks the stream. The call\n\t\t// is carried out once the request ends.\n\t\tonToolCall: ({ toolCall }) => {\n\t\t\tif (isClientAction(toolCall.toolName)) {\n\t\t\t\tactionCalls.push({\n\t\t\t\t\ttoolCallId: toolCall.toolCallId,\n\t\t\t\t\ttoolName: toolCall.toolName,\n\t\t\t\t\tinput: toolCall.input\n\t\t\t\t})\n\t\t\t}\n\t\t},\n\t\tonFinish: ({ isAbort, isDisconnect, isError }) => {\n\t\t\trequestEnd = { isAbort, isDisconnect, isError, error: requestError }\n\n\t\t\tif (sendsApprovalAnswers) {\n\t\t\t\tsendsApprovalAnswers = false\n\t\t\t\tqueueMicrotask(() => finishRequest('send'))\n\t\t\t}\n\t\t},\n\t\t// Once the user has answered every approval of the step, the AI SDK\n\t\t// sends the answers on its own, but only for a user turn that waits.\n\t\tsendAutomaticallyWhen: ({ messages }) =>\n\t\t\tlastAssistantMessageIsCompleteWithApprovalResponses({ messages }) &&\n\t\t\tstartApprovalRequest()\n\t})\n\n\tchat.subscribe(() => {\n\t\tif (countShownParts(chat.lastMessage, isUserFacing) > shownPartsBefore) {\n\t\t\tdispatch({ kind: 'content-shown' })\n\t\t}\n\n\t\treportNewToolResults()\n\t})\n\n\tconst unsubscribeSignals = resumable\n\t\t? (options.resumeOn ?? []).map((signal) => signal(() => resumeStream()))\n\t\t: []\n\n\tfunction dispatch(event: ChatTurnEvent): readonly ChatTurnEffect[] {\n\t\tconst transition = reduceChatTurn(snapshot.turn, event)\n\n\t\tif (transition.state !== snapshot.turn) {\n\t\t\tpublish({ ...snapshot, turn: transition.state })\n\t\t}\n\n\t\tfor (const effect of transition.effects) {\n\t\t\trunEffect(effect)\n\t\t}\n\n\t\treturn transition.effects\n\t}\n\n\tfunction runEffect(effect: ChatTurnEffect): void {\n\t\tswitch (effect.kind) {\n\t\t\tcase 'send-message':\n\t\t\t\tif (focused) {\n\t\t\t\t\tshownPartsBefore = 0\n\t\t\t\t\tstartRequest('send', () =>\n\t\t\t\t\t\tchat.sendMessage(\n\t\t\t\t\t\t\teffect.files === undefined\n\t\t\t\t\t\t\t\t? { text: effect.text }\n\t\t\t\t\t\t\t\t: { text: effect.text, files: [...effect.files] }\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t} else {\n\t\t\t\t\trestoreDraft(effect.text)\n\t\t\t\t\tendUnsentRequest()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\tcase 'request':\n\t\t\t\tif (focused) {\n\t\t\t\t\tstartNextRequest()\n\t\t\t\t} else {\n\t\t\t\t\tendUnsentRequest()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\tcase 'resume-stream':\n\t\t\t\tstartRequest('resume', () => chat.resumeStream())\n\t\t\t\treturn\n\t\t\tcase 'abort-request':\n\t\t\t\tvoid chat.stop()\n\t\t\t\treturn\n\t\t\tcase 'restore-draft':\n\t\t\t\trestoreDraft(effect.text)\n\t\t\t\treturn\n\t\t\tcase 'resume-run':\n\t\t\t\tif (focused) {\n\t\t\t\t\tsendResume(effect.resume)\n\t\t\t\t} else {\n\t\t\t\t\tendUnsentRequest()\n\t\t\t\t}\n\t\t\t\treturn\n\t\t\tcase 'mark-changes':\n\t\t\t\tmarkChanges(effect.mark, effect.changes)\n\t\t\t\treturn\n\t\t}\n\n\t\teffect satisfies never\n\t}\n\n\t/**\n\t * A reattached answer's question, when the page on screen was read before\n\t * the thread stored it: it goes before the answer the stream started.\n\t */\n\tfunction showQuestion(message: UIMessage | undefined): void {\n\t\tif (\n\t\t\tmessage === undefined ||\n\t\t\tchat.messages.some((shown) => shown.id === message.id)\n\t\t) {\n\t\t\treturn\n\t\t}\n\n\t\tconst messages = [...chat.messages]\n\t\tconst last = messages.at(-1)\n\t\tconst answerIndex =\n\t\t\tlast?.role === 'assistant' && !shownBeforeRequest.has(last.id)\n\t\t\t\t? messages.length - 1\n\t\t\t\t: messages.length\n\n\t\tmessages.splice(answerIndex, 0, message)\n\t\tchat.messages = messages\n\t}\n\n\tfunction restoreDraft(text: string): void {\n\t\tpublish({\n\t\t\t...snapshot,\n\t\t\trestoredDraft: {\n\t\t\t\ttext,\n\t\t\t\tsequence: (snapshot.restoredDraft?.sequence ?? 0) + 1\n\t\t\t}\n\t\t})\n\t}\n\n\t/**\n\t * A request that goes on with the answer on screen when its trailing\n\t * message holds the client's results (a follow-up, or a retry of one), and\n\t * writes it again otherwise.\n\t */\n\tfunction startNextRequest(): void {\n\t\tconst continues =\n\t\t\t!startsOver &&\n\t\t\tselectRequestMessages(chat.messages, { isClientResult }).at(-1)?.role ===\n\t\t\t\t'assistant'\n\n\t\tshownPartsBefore = continues\n\t\t\t? countShownParts(chat.lastMessage, isUserFacing)\n\t\t\t: 0\n\t\tstartRequest('send', () =>\n\t\t\tcontinues ? chat.sendMessage() : chat.regenerate()\n\t\t)\n\t}\n\n\t/** A resume goes on with the answer that shows the suspended run. */\n\tfunction sendResume(resume: RunResume): void {\n\t\tshownPartsBefore = countShownParts(chat.lastMessage, isUserFacing)\n\t\tstartRequest('send', () => chat.sendMessage(), resume)\n\t}\n\n\t/**\n\t * The AI SDK is about to send the user's answers to the approvals. The\n\t * reducer counts the request; it goes out only when the user turn on screen\n\t * waits for them.\n\t */\n\tfunction startApprovalRequest(): boolean {\n\t\tif (!focused) {\n\t\t\treturn false\n\t\t}\n\n\t\tconst waited = snapshot.turn.kind === 'waiting'\n\t\tdispatch({ kind: 'approval-sent' })\n\n\t\tif (!waited || snapshot.turn.kind !== 'requesting') {\n\t\t\treturn false\n\t\t}\n\n\t\tresetRequest()\n\t\tshownPartsBefore = countShownParts(chat.lastMessage, isUserFacing)\n\t\tsendsApprovalAnswers = true\n\n\t\treturn true\n\t}\n\n\tfunction markChanges(\n\t\tmark: ChangeMark,\n\t\tchanges: readonly PendingChange[]\n\t): void {\n\t\tconst changeMarks = new Map(snapshot.changeMarks)\n\n\t\tfor (const change of changes) {\n\t\t\tchangeMarks.set(change.toolCallId, mark)\n\t\t}\n\n\t\tpublish({ ...snapshot, changeMarks })\n\t}\n\n\tfunction reportNewToolResults(): void {\n\t\tfor (const part of chat.lastMessage?.parts ?? []) {\n\t\t\tif (\n\t\t\t\t!isToolUIPart(part) ||\n\t\t\t\tpart.state !== 'output-available' ||\n\t\t\t\tknownResults.has(part.toolCallId)\n\t\t\t) {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tknownResults.add(part.toolCallId)\n\n\t\t\tconst toolName = getToolName(part)\n\n\t\t\tif (!isClientAction(toolName)) {\n\t\t\t\toptions.onToolResult?.({\n\t\t\t\t\ttoolCallId: part.toolCallId,\n\t\t\t\t\ttoolName,\n\t\t\t\t\toutput: part.output\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t}\n\n\t/** Ends a user turn whose request was never sent, the way Stop would. */\n\tfunction endUnsentRequest(): void {\n\t\tdispatch({ kind: 'stop-pressed' })\n\t\tdispatch({ kind: 'request-finished', continues: false })\n\t}\n\n\t/** Runs one request and turns how it ended into the reducer's event once the AI SDK is done with it. */\n\tfunction startRequest(\n\t\tkind: RequestKind,\n\t\trequest: () => Promise<void>,\n\t\tresume?: RunResume\n\t): void {\n\t\tresetRequest()\n\t\tpendingResume = resume\n\n\t\tvoid request().then(\n\t\t\t() => finishRequest(kind),\n\t\t\t(error: unknown) => finishRequest(kind, toError(error))\n\t\t)\n\t}\n\n\tfunction resetRequest(): void {\n\t\tshownBeforeRequest = new Set(chat.messages.map((message) => message.id))\n\t\ttakeReplySuggestions()\n\t\toffersReplies = true\n\t\tpendingResume = undefined\n\t\trequestError = undefined\n\t\trequestEnd = undefined\n\t\tactionCalls = []\n\t\trequestCount += 1\n\t}\n\n\t/**\n\t * Changes the answer leaves waiting come first: the user answers them, and\n\t * a client action called beside one waits with the step.\n\t */\n\tfunction finishRequest(kind: RequestKind, rejection?: Error): void {\n\t\tconst end = endOf(kind, rejection)\n\n\t\tif (end === 'nothing-to-resume') {\n\t\t\tdispatch({ kind: 'nothing-to-resume' })\n\n\t\t\treturn\n\t\t}\n\n\t\tconst isAnswered = !end.isAbort && !end.isError\n\t\tconst calls = isAnswered ? actionCalls : []\n\t\tconst waiting =\n\t\t\tisAnswered && chat.lastMessage?.role === 'assistant'\n\t\t\t\t? readWaitingChanges(chat.lastMessage)\n\t\t\t\t: []\n\n\t\tactionCalls = []\n\n\t\tif (waiting.length > 0) {\n\t\t\tdispatch({ kind: 'changes-waiting', changes: waiting })\n\n\t\t\treturn\n\t\t}\n\n\t\tconst stranded = isAnswered\n\t\t\t? findStrandedToolCalls(chat.lastMessage, isClientAction)\n\t\t\t: []\n\n\t\tif (calls.length > 0 || stranded.length > 0) {\n\t\t\tdispatch({ kind: 'request-finished', continues: true })\n\n\t\t\t// Stop or a focus change ended the user turn as its request ended.\n\t\t\tif (snapshot.turn.kind === 'continuing') {\n\t\t\t\tvoid carryOut(calls, stranded, requestCount)\n\t\t\t}\n\n\t\t\treturn\n\t\t}\n\n\t\tconst event = toTurnEvent(end)\n\t\tconst effects = dispatch(event)\n\t\tconst code = failureCodeOf(event, end)\n\n\t\tif (code !== undefined) {\n\t\t\toptions.onFailure?.({\n\t\t\t\tcode,\n\t\t\t\tretrying: effects.some(\n\t\t\t\t\t(effect) =>\n\t\t\t\t\t\teffect.kind === 'request' ||\n\t\t\t\t\t\teffect.kind === 'resume-stream' ||\n\t\t\t\t\t\teffect.kind === 'resume-run'\n\t\t\t\t)\n\t\t\t})\n\t\t}\n\t}\n\n\t/**\n\t * How the request ended. A request the AI SDK never started (Stop while\n\t * the message was being prepared) still settles the reducer; a reattach\n\t * that found no stream ends without `onFinish`.\n\t */\n\tfunction endOf(\n\t\tkind: RequestKind,\n\t\trejection: Error | undefined\n\t): RequestEnd | 'nothing-to-resume' {\n\t\tif (rejection !== undefined) {\n\t\t\treturn {\n\t\t\t\tisAbort: false,\n\t\t\t\tisDisconnect: false,\n\t\t\t\tisError: true,\n\t\t\t\terror: rejection\n\t\t\t}\n\t\t}\n\n\t\tif (requestEnd !== undefined) {\n\t\t\treturn requestEnd\n\t\t}\n\n\t\tif (requestError !== undefined) {\n\t\t\treturn {\n\t\t\t\tisAbort: false,\n\t\t\t\tisDisconnect: false,\n\t\t\t\tisError: true,\n\t\t\t\terror: requestError\n\t\t\t}\n\t\t}\n\n\t\treturn kind === 'resume'\n\t\t\t? 'nothing-to-resume'\n\t\t\t: { isAbort: true, isDisconnect: false, isError: false, error: undefined }\n\t}\n\n\tfunction toTurnEvent(end: RequestEnd): ChatTurnEvent {\n\t\tif (end.isAbort || !end.isError) {\n\t\t\treturn { kind: 'request-finished', continues: false }\n\t\t}\n\n\t\tconst response = readErrorResponse(end.error)\n\n\t\tif (response !== undefined) {\n\t\t\treturn {\n\t\t\t\tkind: 'error-response',\n\t\t\t\tstatus: response.status,\n\t\t\t\trefusal: classifyError(response)\n\t\t\t}\n\t\t}\n\n\t\treturn isConnectionLost(end.error, end)\n\t\t\t? { kind: 'connection-lost', resumable }\n\t\t\t: {\n\t\t\t\t\tkind: 'error-response',\n\t\t\t\t\tstatus: STREAM_FAILURE_STATUS,\n\t\t\t\t\trefusal: undefined\n\t\t\t\t}\n\t}\n\n\t/**\n\t * Carries out what the answer asked for, posts every result and an error\n\t * for each stranded server call, then lets the reducer send the follow-up,\n\t * unless the user turn has moved on meanwhile. One at a time, so a repeat\n\t * in the same batch sees the call before it.\n\t */\n\tasync function carryOut(\n\t\tcalls: readonly ClientToolCall[],\n\t\tstranded: readonly {\n\t\t\treadonly toolCallId: string\n\t\t\treadonly toolName: string\n\t\t}[],\n\t\trequest: number\n\t): Promise<void> {\n\t\tfor (const call of stranded) {\n\t\t\tpostedErrors.add(call.toolCallId)\n\t\t\tawait settle(\n\t\t\t\tchat.addToolOutput({\n\t\t\t\t\tstate: 'output-error',\n\t\t\t\t\ttool: call.toolName,\n\t\t\t\t\ttoolCallId: call.toolCallId,\n\t\t\t\t\terrorText: STRANDED_TOOL_ERROR\n\t\t\t\t})\n\t\t\t)\n\t\t}\n\n\t\tfor (const call of calls) {\n\t\t\tawait settle(runClientAction(chat, clientActions(), call))\n\t\t}\n\n\t\tif (request === requestCount && snapshot.turn.kind === 'continuing') {\n\t\t\tdispatch({ kind: 'follow-up-ready' })\n\t\t}\n\t}\n\n\tfunction takeReplySuggestions(): void {\n\t\toffersReplies = false\n\n\t\tif (snapshot.replySuggestions.length > 0) {\n\t\t\tpublish({ ...snapshot, replySuggestions: NO_REPLY_SUGGESTIONS })\n\t\t}\n\t}\n\n\tfunction publish(next: ChatSessionSnapshot): void {\n\t\tsnapshot = next\n\n\t\tfor (const listener of listeners) {\n\t\t\tlistener()\n\t\t}\n\t}\n\n\t/** Stop pressed: the request is aborted, and a server that keeps runs going is told to stop. */\n\tfunction stop(): void {\n\t\tconst effects = dispatch({ kind: 'stop-pressed' })\n\n\t\tif (\n\t\t\tresumable &&\n\t\t\teffects.some((effect) => effect.kind === 'abort-request')\n\t\t) {\n\t\t\tvoid postStop()\n\t\t}\n\t}\n\n\tasync function postStop(): Promise<void> {\n\t\tconst send =\n\t\t\toptions.transport.fetch ??\n\t\t\t((input, init) => globalThis.fetch(input, init))\n\t\tconst url = `${options.transport.api.replace(/\\/$/, '')}/${encodeURIComponent(threadId)}/stop`\n\n\t\ttry {\n\t\t\tawait send(url, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\t...(options.transport.credentials === undefined\n\t\t\t\t\t? {}\n\t\t\t\t\t: { credentials: options.transport.credentials })\n\t\t\t})\n\t\t} catch {\n\t\t\t// The run ends on its own; Stop already ended the turn here.\n\t\t}\n\t}\n\n\tfunction resumeStream(): void {\n\t\tif (\n\t\t\t!resumable ||\n\t\t\t!focused ||\n\t\t\tchat.status === 'submitted' ||\n\t\t\tchat.status === 'streaming'\n\t\t) {\n\t\t\treturn\n\t\t}\n\n\t\tconst before = snapshot.turn\n\t\tdispatch({ kind: 'stream-watched' })\n\n\t\tif (snapshot.turn !== before) {\n\t\t\tstartRequest('resume', () => chat.resumeStream())\n\t\t}\n\t}\n\n\treturn {\n\t\tthreadId,\n\t\tchat,\n\t\tgetSnapshot: () => snapshot,\n\t\tsubscribe: (listener) => {\n\t\t\tlisteners.add(listener)\n\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener)\n\t\t\t}\n\t\t},\n\t\tsend: (text, sendOptions) => {\n\t\t\tconst before = snapshot.turn\n\t\t\tconst files = sendOptions?.files ?? []\n\n\t\t\tif (text.trim() !== '' || files.length > 0) {\n\t\t\t\ttakeReplySuggestions()\n\t\t\t}\n\n\t\t\tconst effects = dispatch(\n\t\t\t\tfiles.length === 0\n\t\t\t\t\t? { kind: 'message-submitted', text }\n\t\t\t\t\t: { kind: 'message-submitted', text, files }\n\t\t\t)\n\n\t\t\treturn snapshot.turn !== before || effects.length > 0\n\t\t},\n\t\tstop,\n\t\ttryAgain: () => {\n\t\t\tstartsOver = true\n\t\t\tdispatch({ kind: 'try-again-pressed' })\n\t\t\tstartsOver = false\n\t\t},\n\t\tregenerate: () => {\n\t\t\tconst { messages } = chat\n\t\t\tconst hasAnswer =\n\t\t\t\tmessages.at(-1)?.role === 'assistant' &&\n\t\t\t\tmessages.some((message) => message.role === 'user')\n\n\t\t\tif (!hasAnswer) {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tstartsOver = true\n\t\t\tdispatch({ kind: 'regenerate-pressed' })\n\t\t\tstartsOver = false\n\t\t},\n\t\tanswerApproval: ({ approvalId, approved, reason }) => {\n\t\t\tconst turn = snapshot.turn\n\t\t\tconst waits =\n\t\t\t\tturn.kind === 'waiting' &&\n\t\t\t\tturn.changes.some(\n\t\t\t\t\t(change) =>\n\t\t\t\t\t\tchange.kind === 'approval' && change.approvalId === approvalId\n\t\t\t\t)\n\n\t\t\tif (waits) {\n\t\t\t\tvoid chat.addToolApprovalResponse({\n\t\t\t\t\tid: approvalId,\n\t\t\t\t\tapproved,\n\t\t\t\t\t...(reason === undefined ? {} : { reason })\n\t\t\t\t})\n\t\t\t}\n\t\t},\n\t\tresumeRun: (resume) => {\n\t\t\tdispatch({ kind: 'resume-pressed', resume })\n\t\t},\n\t\tclearRestoredDraft: (sequence) => {\n\t\t\tif (snapshot.restoredDraft?.sequence === sequence) {\n\t\t\t\tpublish({ ...snapshot, restoredDraft: undefined })\n\t\t\t}\n\t\t},\n\t\t// A failed turn keeps its messages: Try again sends them again.\n\t\tshowStored: ({ messages, pendingChanges }) => {\n\t\t\tif (snapshot.turn.kind !== 'idle') {\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tfor (const part of messages.flatMap((message) => message.parts)) {\n\t\t\t\tif (isToolUIPart(part)) {\n\t\t\t\t\tknownResults.add(part.toolCallId)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchat.messages = [...messages]\n\n\t\t\tconst last = messages.at(-1)\n\t\t\tconst waiting = (\n\t\t\t\tlast?.role === 'assistant' ? readWaitingChanges(last) : []\n\t\t\t).filter((change) =>\n\t\t\t\tpendingChanges.some(\n\t\t\t\t\t(pending) =>\n\t\t\t\t\t\tpending.kind === change.kind &&\n\t\t\t\t\t\tpending.runId === change.runId &&\n\t\t\t\t\t\tpending.toolCallId === change.toolCallId\n\t\t\t\t)\n\t\t\t)\n\n\t\t\tif (waiting.length > 0) {\n\t\t\t\tdispatch({ kind: 'changes-waiting', changes: waiting })\n\t\t\t}\n\t\t},\n\t\tresumeStream,\n\t\tsetFocused: (isFocused) => {\n\t\t\tfocused = isFocused\n\n\t\t\tif (!isFocused) {\n\t\t\t\tstop()\n\t\t\t}\n\t\t},\n\t\tdispose: () => {\n\t\t\tfor (const unsubscribe of unsubscribeSignals) {\n\t\t\t\tunsubscribe()\n\t\t\t}\n\n\t\t\tvoid chat.stop()\n\t\t}\n\t}\n}\n\n/** The sessions of an app session by key (a thread, a workspace), created on first use and shared by every screen. */\nexport interface ChatSessions {\n\t/** The key's session, created the first time it is asked for. */\n\treadonly get: (key: string) => ChatSession\n\t/** Makes this the session on screen: every other one stops and sends nothing until it is on screen again. */\n\treadonly focus: (key: string) => void\n\t/** Stops the key's session and forgets it; the next screen that asks gets a new one. */\n\treadonly forget: (key: string) => void\n\t/** Stops and forgets every session, as on sign-out. */\n\treadonly clear: () => void\n}\n\nexport function createChatSessions(\n\tcreate: (key: string) => ChatSession\n): ChatSessions {\n\tconst sessions = new Map<string, ChatSession>()\n\n\treturn {\n\t\tget: (key) => {\n\t\t\tlet session = sessions.get(key)\n\n\t\t\tif (session === undefined) {\n\t\t\t\tsession = create(key)\n\t\t\t\tsessions.set(key, session)\n\t\t\t}\n\n\t\t\treturn session\n\t\t},\n\t\tfocus: (key) => {\n\t\t\tfor (const [sessionKey, session] of sessions) {\n\t\t\t\tsession.setFocused(sessionKey === key)\n\t\t\t}\n\t\t},\n\t\tforget: (key) => {\n\t\t\tsessions.get(key)?.dispose()\n\t\t\tsessions.delete(key)\n\t\t},\n\t\tclear: () => {\n\t\t\tfor (const session of sessions.values()) {\n\t\t\t\tsession.dispose()\n\t\t\t}\n\n\t\t\tsessions.clear()\n\t\t}\n\t}\n}\n\n/** The request as the body, with `resume` and `clientTools` only when there are any. */\nfunction defaultBody(request: ChatRequest): object {\n\treturn {\n\t\tthreadId: request.threadId,\n\t\tmessages: request.messages,\n\t\ttrigger: request.trigger,\n\t\t...(request.resume === undefined ? {} : { resume: request.resume }),\n\t\t...(request.clientTools.length === 0\n\t\t\t? {}\n\t\t\t: { clientTools: request.clientTools })\n\t}\n}\n\nfunction failureCodeOf(\n\tevent: ChatTurnEvent,\n\tend: RequestEnd\n): string | undefined {\n\tswitch (event.kind) {\n\t\tcase 'connection-lost':\n\t\t\treturn 'network'\n\t\tcase 'error-response':\n\t\t\tif (event.refusal !== undefined) {\n\t\t\t\treturn event.refusal.code\n\t\t\t}\n\n\t\t\treturn readErrorResponse(end.error) === undefined\n\t\t\t\t? 'stream_error'\n\t\t\t\t: 'server_error'\n\t\tdefault:\n\t\t\treturn undefined\n\t}\n}\n\n/** Waits for a result to be posted; one that fails to post still lets the turn go on. */\nasync function settle(posting: void | PromiseLike<void> | null): Promise<void> {\n\ttry {\n\t\tawait posting\n\t} catch {\n\t\t// The server reads the call as unanswered.\n\t}\n}\n\nfunction toError(reason: unknown): Error {\n\treturn reason instanceof Error ? reason : new Error(String(reason))\n}\n"]}
|
package/dist/client/turn.d.ts
CHANGED
|
@@ -119,6 +119,10 @@ export type ChatTurnEvent = {
|
|
|
119
119
|
} | {
|
|
120
120
|
readonly kind: 'try-again-pressed';
|
|
121
121
|
}
|
|
122
|
+
/** The user asked for the newest answer written again. */
|
|
123
|
+
| {
|
|
124
|
+
readonly kind: 'regenerate-pressed';
|
|
125
|
+
}
|
|
122
126
|
/**
|
|
123
127
|
* Changes wait for the user: the request ended answered with them in its
|
|
124
128
|
* last assistant message, or a thread loaded from history shows them.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turn.d.ts","sourceRoot":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-84,6 +84,8 @@ function reduceIdle(state, event) {
|
|
|
84
84
|
return refuseUnaskedRequest(state);
|
|
85
85
|
case 'stream-watched':
|
|
86
86
|
return watchStream('idle');
|
|
87
|
+
case 'regenerate-pressed':
|
|
88
|
+
return regenerate();
|
|
87
89
|
case 'content-shown':
|
|
88
90
|
case 'request-finished':
|
|
89
91
|
case 'error-response':
|
|
@@ -132,6 +134,7 @@ function reduceRequesting(state, event) {
|
|
|
132
134
|
return abortRequest(userTurn);
|
|
133
135
|
case 'follow-up-ready':
|
|
134
136
|
case 'try-again-pressed':
|
|
137
|
+
case 'regenerate-pressed':
|
|
135
138
|
case 'approval-sent':
|
|
136
139
|
case 'resume-pressed':
|
|
137
140
|
case 'stream-watched':
|
|
@@ -164,6 +167,7 @@ function reduceStreaming(state, event) {
|
|
|
164
167
|
case 'content-shown':
|
|
165
168
|
case 'follow-up-ready':
|
|
166
169
|
case 'try-again-pressed':
|
|
170
|
+
case 'regenerate-pressed':
|
|
167
171
|
case 'approval-sent':
|
|
168
172
|
case 'resume-pressed':
|
|
169
173
|
case 'stream-watched':
|
|
@@ -187,6 +191,7 @@ function reduceContinuing(state, event) {
|
|
|
187
191
|
case 'error-response':
|
|
188
192
|
case 'connection-lost':
|
|
189
193
|
case 'try-again-pressed':
|
|
194
|
+
case 'regenerate-pressed':
|
|
190
195
|
case 'changes-waiting':
|
|
191
196
|
case 'approval-sent':
|
|
192
197
|
case 'resume-pressed':
|
|
@@ -210,6 +215,7 @@ function reduceStopping(state, event) {
|
|
|
210
215
|
case 'follow-up-ready':
|
|
211
216
|
case 'stop-pressed':
|
|
212
217
|
case 'try-again-pressed':
|
|
218
|
+
case 'regenerate-pressed':
|
|
213
219
|
case 'approval-sent':
|
|
214
220
|
case 'resume-pressed':
|
|
215
221
|
case 'stream-watched':
|
|
@@ -237,6 +243,7 @@ function reduceWaiting(state, event) {
|
|
|
237
243
|
case 'follow-up-ready':
|
|
238
244
|
case 'stop-pressed':
|
|
239
245
|
case 'try-again-pressed':
|
|
246
|
+
case 'regenerate-pressed':
|
|
240
247
|
case 'changes-waiting':
|
|
241
248
|
case 'stream-watched':
|
|
242
249
|
case 'nothing-to-resume':
|
|
@@ -251,6 +258,8 @@ function reduceFailed(state, event) {
|
|
|
251
258
|
return canTryAgain(state.failure)
|
|
252
259
|
? tryAgain(state.resume)
|
|
253
260
|
: unchanged(state);
|
|
261
|
+
case 'regenerate-pressed':
|
|
262
|
+
return regenerate();
|
|
254
263
|
case 'changes-waiting':
|
|
255
264
|
return waitForUser(newUserTurn(), event.changes);
|
|
256
265
|
case 'approval-sent':
|
|
@@ -288,6 +297,13 @@ function startUserTurn(message) {
|
|
|
288
297
|
]
|
|
289
298
|
};
|
|
290
299
|
}
|
|
300
|
+
/** A new user turn that writes the newest answer again from the user's message. */
|
|
301
|
+
function regenerate() {
|
|
302
|
+
return {
|
|
303
|
+
state: { kind: 'requesting', userTurn: newUserTurn() },
|
|
304
|
+
effects: [{ kind: 'request' }]
|
|
305
|
+
};
|
|
306
|
+
}
|
|
291
307
|
function watchStream(from) {
|
|
292
308
|
return {
|
|
293
309
|
state: {
|
package/dist/client/turn.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"turn.js","sourceRoot":"","sources":["../../src/client/turn.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,YAAY;AAEZ;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,CAAA;AAEtC,gDAAgD;AAChD,MAAM,CAAC,MAAM,oBAAoB,GAAkB,EAAE,IAAI,EAAE,MAAM,EAAE,CAAA;AAEnE,MAAM,UAAU,GAA8B,EAAE,CAAA;AAwIhD,YAAY;AAEZ;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC7B,KAAoB,EACpB,KAAoB;IAEpB,IAAI,KAAK,CAAC,IAAI,KAAK,mBAAmB,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1D,OAAO,SAAS,C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,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAA;IACtD,MAAM,eAAe,GAAG,QAAQ,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,SAAS,CAAA;IAEnE,OAAO;QACN,KAAK,EAAE,eAAe;YACrB,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,MAAM,EAAE;YACtD,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE;QAC9B,OAAO,EAAE,SAAS;YACjB,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,GAAG,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC,SAAS,CAAC,CAAC;YAC3D,CAAC,CAAC,KAAK;KACR,CAAA;AACF,CAAC;AAED,SAAS,SAAS,CACjB,KAAyD;IAEzD,OAAO,KAAK,CAAC,OAAO,KAAK,SAAS;QACjC,CAAC,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE;QAChD,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAA;AAC/C,CAAC;AAED,SAAS,SAAS,CAAC,KAAoB;IACtC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,CAAA;AACtC,CAAC","sourcesContent":["/**\n * The turn reducer: a pure reducer for one user turn, from the user's message\n * until the answer is finished, across every request it takes. The session\n * feeds it events and runs the effects it returns; every request the session\n * sends is one of those effects, except the answer to an approval, which the\n * AI SDK sends on its own and the reducer is told about. A thread loaded from\n * history never continues on its own.\n */\n\nimport type { FileUIPart } from 'ai'\n\nimport type { PendingChange } from './pending.js'\n\n// CONSTANTS\n\n/**\n * The most automatic follow-up requests one user turn may send: room for\n * several client actions in a row, while a model that keeps calling them\n * still stops. The server's own step budget still ends a runaway request.\n */\nexport const CHAT_FOLLOW_UP_BUDGET = 8\n\n/** Before any user turn: nothing is running. */\nexport const initialChatTurnState: ChatTurnState = { kind: 'idle' }\n\nconst NO_EFFECTS: readonly ChatTurnEffect[] = []\n\n// TYPES\n\n/** What Apply on a suspended run sends: the run and the data it resumes with. */\nexport interface RunResume<TData = unknown> {\n\treadonly runId: string\n\treadonly data: TData\n}\n\n/** A refusal the chat route answered, as the session's `classifyError` reads it. */\nexport interface ChatRefusal {\n\treadonly code: string\n\treadonly canTryAgain: boolean\n\t/** The approval or suspended run the request answered no longer waits. */\n\treadonly expired?: boolean\n}\n\n/** The counters of the user turn that is running. */\nexport interface ChatUserTurn {\n\t/** Text the user sent while this user turn runs; it goes out as the next user turn when this one ends answered or stopped. */\n\treadonly queued: string | undefined\n\t/** Whether this user turn has used its one automatic retry. */\n\treadonly retried: boolean\n\t/** How many automatic follow-up requests this user turn has sent: client action results, approval answers and resumes. */\n\treadonly followUps: number\n\t/** The waiting changes the request in flight answers; an expired refusal marks them expired. */\n\treadonly answering: readonly PendingChange[]\n\t/** The suspended runs this user turn has resumed: each is resumed once. */\n\treadonly resumedRunIds: readonly string[]\n\t/** The resume the request in flight sends, so a retry of the request sends it again. */\n\treadonly resume: RunResume | undefined\n\t/**\n\t * The session reattached to a stream nobody asked for here (mount,\n\t * foreground, network back), from this state: until it shows something it\n\t * ends quietly, back where it was.\n\t */\n\treadonly watching: false | 'idle' | 'failed'\n}\n\n/** What a change card says once it no longer waits without being answered. */\nexport type ChangeMark =\n\t/** The user wrote a new message instead, so the server declined it. */\n\t| 'movedOn'\n\t/** The server no longer had it waiting when the user answered. */\n\t| 'expired'\n\n/** Why a user turn ended without its answer. */\nexport type ChatTurnFailure =\n\t| { readonly kind: 'refused'; readonly refusal: ChatRefusal }\n\t| { readonly kind: 'server-error'; readonly status: number }\n\t| { readonly kind: 'connection-lost' }\n\t| { readonly kind: 'follow-up-budget-exhausted' }\n\nexport type ChatTurnState =\n\t| { readonly kind: 'idle' }\n\t| { readonly kind: 'requesting'; readonly userTurn: ChatUserTurn }\n\t| { readonly kind: 'streaming'; readonly userTurn: ChatUserTurn }\n\t| { readonly kind: 'continuing'; readonly userTurn: ChatUserTurn }\n\t| { readonly kind: 'stopping'; readonly userTurn: ChatUserTurn }\n\t/** The answer left changes for the user to answer: approvals to confirm, suspended runs to resume. Stop is not offered. */\n\t| {\n\t\t\treadonly kind: 'waiting'\n\t\t\treadonly userTurn: ChatUserTurn\n\t\t\treadonly changes: readonly PendingChange[]\n\t }\n\t| {\n\t\t\treadonly kind: 'failed'\n\t\t\treadonly failure: ChatTurnFailure\n\t\t\t/** The resume the failed request sent, which Try again sends again. */\n\t\t\treadonly resume?: RunResume\n\t }\n\nexport type ChatTurnEvent =\n\t| {\n\t\t\treadonly kind: 'message-submitted'\n\t\t\treadonly text: string\n\t\t\treadonly files?: readonly FileUIPart[]\n\t }\n\t| { readonly kind: 'content-shown' }\n\t| { readonly kind: 'request-finished'; readonly continues: boolean }\n\t| {\n\t\t\treadonly kind: 'error-response'\n\t\t\treadonly status: number\n\t\t\t/** Undefined for a response the chat route did not refuse, such as a proxy's 502. */\n\t\t\treadonly refusal: ChatRefusal | undefined\n\t }\n\t/** `resumable`: the server keeps the run going, so the stream can be reattached instead of sent again. */\n\t| { readonly kind: 'connection-lost'; readonly resumable: boolean }\n\t| { readonly kind: 'follow-up-ready' }\n\t| { readonly kind: 'stop-pressed' }\n\t| { readonly kind: 'try-again-pressed' }\n\t/**\n\t * Changes wait for the user: the request ended answered with them in its\n\t * last assistant message, or a thread loaded from history shows them.\n\t */\n\t| {\n\t\t\treadonly kind: 'changes-waiting'\n\t\t\treadonly changes: readonly PendingChange[]\n\t }\n\t/** The AI SDK started the request that carries the user's answers to the waiting approvals. */\n\t| { readonly kind: 'approval-sent' }\n\t/** The user pressed Apply on a waiting suspended run. */\n\t| { readonly kind: 'resume-pressed'; readonly resume: RunResume }\n\t/** The session reattached to the thread's running stream on its own. */\n\t| { readonly kind: 'stream-watched' }\n\t/** A reattach found no running stream. */\n\t| { readonly kind: 'nothing-to-resume' }\n\nexport type ChatTurnEffect =\n\t| {\n\t\t\treadonly kind: 'send-message'\n\t\t\treadonly text: string\n\t\t\treadonly files?: readonly FileUIPart[]\n\t }\n\t| { readonly kind: 'request' }\n\t/** Reattaches to the run the server keeps going after the connection dropped. */\n\t| { readonly kind: 'resume-stream' }\n\t| { readonly kind: 'abort-request' }\n\t| { readonly kind: 'restore-draft'; readonly text: string }\n\t/** Sends a request with `resume`, which continues the answer of the suspended run. */\n\t| { readonly kind: 'resume-run'; readonly resume: RunResume }\n\t/** Tells the cards of these changes what became of them. */\n\t| {\n\t\t\treadonly kind: 'mark-changes'\n\t\t\treadonly mark: ChangeMark\n\t\t\treadonly changes: readonly PendingChange[]\n\t }\n\nexport interface ChatTurnTransition {\n\treadonly state: ChatTurnState\n\treadonly effects: readonly ChatTurnEffect[]\n}\n\ntype UserTurnState = Extract<ChatTurnState, { userTurn: unknown }>\n\n// FUNCTIONS\n\n/**\n * A pure reducer for one user turn; the session runs its effects. An event\n * that does not apply returns the given state object and no effect, and the\n * input is never changed.\n */\nexport function reduceChatTurn(\n\tstate: ChatTurnState,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tif (event.kind === 'message-submitted' && isBlank(event)) {\n\t\treturn unchanged(state)\n\t}\n\n\tswitch (state.kind) {\n\t\tcase 'idle':\n\t\t\treturn reduceIdle(state, event)\n\t\tcase 'requesting':\n\t\t\treturn reduceRequesting(state, event)\n\t\tcase 'streaming':\n\t\t\treturn reduceStreaming(state, event)\n\t\tcase 'continuing':\n\t\t\treturn reduceContinuing(state, event)\n\t\tcase 'stopping':\n\t\t\treturn reduceStopping(state, event)\n\t\tcase 'waiting':\n\t\t\treturn reduceWaiting(state, event)\n\t\tcase 'failed':\n\t\t\treturn reduceFailed(state, event)\n\t}\n}\n\n/** Whether Try again is offered for a failure. The reducer ignores Try again when it is not. */\nexport function canTryAgain(failure: ChatTurnFailure): boolean {\n\tswitch (failure.kind) {\n\t\tcase 'refused':\n\t\t\treturn failure.refusal.canTryAgain\n\t\tcase 'server-error':\n\t\tcase 'connection-lost':\n\t\t\treturn true\n\t\tcase 'follow-up-budget-exhausted':\n\t\t\treturn false\n\t}\n}\n\n/** Whether a user turn is running: Stop is offered and a new message queues. */\nexport function isChatTurnBusy(state: ChatTurnState): boolean {\n\tswitch (state.kind) {\n\t\tcase 'requesting':\n\t\tcase 'streaming':\n\t\tcase 'continuing':\n\t\tcase 'stopping':\n\t\t\treturn true\n\t\tcase 'idle':\n\t\tcase 'waiting':\n\t\tcase 'failed':\n\t\t\treturn false\n\t}\n}\n\n/**\n * Only a message from the user starts anything: a thread loaded from history\n * never continues on its own. Its waiting changes wait for the user, and a\n * request the AI SDK starts by itself is stopped.\n */\nfunction reduceIdle(\n\tstate: Extract<ChatTurnState, { kind: 'idle' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn startUserTurn(event)\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(newUserTurn(), event.changes)\n\t\tcase 'approval-sent':\n\t\t\treturn refuseUnaskedRequest(state)\n\t\tcase 'stream-watched':\n\t\t\treturn watchStream('idle')\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'try-again-pressed':\n\t\tcase 'resume-pressed':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\nfunction reduceRequesting(\n\tstate: Extract<ChatTurnState, { kind: 'requesting' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'content-shown':\n\t\t\treturn {\n\t\t\t\tstate: {\n\t\t\t\t\tkind: 'streaming',\n\t\t\t\t\tuserTurn: { ...userTurn, watching: false }\n\t\t\t\t},\n\t\t\t\teffects: NO_EFFECTS\n\t\t\t}\n\t\tcase 'request-finished':\n\t\t\treturn finishRequest(userTurn, event.continues)\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(userTurn, event.changes)\n\t\tcase 'connection-lost':\n\t\t\tif (userTurn.watching !== false) {\n\t\t\t\treturn stopWatching(userTurn)\n\t\t\t}\n\n\t\t\treturn userTurn.retried\n\t\t\t\t? failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\t\t\t: retryRequest(state, event.resumable)\n\t\tcase 'error-response':\n\t\t\treturn userTurn.watching === false\n\t\t\t\t? failUserTurn(userTurn, failureOf(event))\n\t\t\t\t: stopWatching(userTurn)\n\t\tcase 'nothing-to-resume':\n\t\t\treturn userTurn.watching === false\n\t\t\t\t? failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\t\t\t: stopWatching(userTurn)\n\t\tcase 'stop-pressed':\n\t\t\treturn abortRequest(userTurn)\n\t\tcase 'follow-up-ready':\n\t\tcase 'try-again-pressed':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/**\n * As while requesting, except that part of the answer is on screen: a lost\n * connection fails at once unless the stream can be reattached.\n */\nfunction reduceStreaming(\n\tstate: Extract<ChatTurnState, { kind: 'streaming' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'request-finished':\n\t\t\treturn finishRequest(userTurn, event.continues)\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(userTurn, event.changes)\n\t\tcase 'connection-lost':\n\t\t\treturn event.resumable && !userTurn.retried\n\t\t\t\t? retryRequest(state, true)\n\t\t\t\t: failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\tcase 'nothing-to-resume':\n\t\t\treturn failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\tcase 'error-response':\n\t\t\treturn failUserTurn(userTurn, failureOf(event))\n\t\tcase 'stop-pressed':\n\t\t\treturn abortRequest(userTurn)\n\t\tcase 'content-shown':\n\t\tcase 'follow-up-ready':\n\t\tcase 'try-again-pressed':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/** Between two requests, while the session carries out client actions. No request is in flight. */\nfunction reduceContinuing(\n\tstate: Extract<ChatTurnState, { kind: 'continuing' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'follow-up-ready':\n\t\t\treturn userTurn.followUps < CHAT_FOLLOW_UP_BUDGET\n\t\t\t\t? sendFollowUp(userTurn)\n\t\t\t\t: failUserTurn(userTurn, { kind: 'follow-up-budget-exhausted' })\n\t\tcase 'stop-pressed':\n\t\t\treturn endUserTurn(userTurn)\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'try-again-pressed':\n\t\tcase 'changes-waiting':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/** Stop was pressed: however the request in flight settles, the user turn ends. */\nfunction reduceStopping(\n\tstate: Extract<ChatTurnState, { kind: 'stopping' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'request-finished':\n\t\tcase 'connection-lost':\n\t\tcase 'error-response':\n\t\tcase 'changes-waiting':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn endUserTurn(state.userTurn)\n\t\tcase 'content-shown':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'try-again-pressed':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/**\n * The answer waits for the user, with no request in flight. Their answers to\n * the approvals and a resume go on with the same user turn; a message they\n * write goes out at once, because the server declines what waits, and the\n * waiting cards learn that the user moved on.\n */\nfunction reduceWaiting(\n\tstate: Extract<ChatTurnState, { kind: 'waiting' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn moveOn(state.changes, event)\n\t\tcase 'approval-sent':\n\t\t\treturn sendApprovalAnswers(state)\n\t\tcase 'resume-pressed':\n\t\t\treturn resumeRun(state, event.resume)\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'try-again-pressed':\n\t\tcase 'changes-waiting':\n\t\tcase 'stream-watched':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\nfunction reduceFailed(\n\tstate: Extract<ChatTurnState, { kind: 'failed' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn startUserTurn(event)\n\t\tcase 'try-again-pressed':\n\t\t\treturn canTryAgain(state.failure)\n\t\t\t\t? tryAgain(state.resume)\n\t\t\t\t: unchanged(state)\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(newUserTurn(), event.changes)\n\t\tcase 'approval-sent':\n\t\t\treturn refuseUnaskedRequest(state)\n\t\tcase 'stream-watched':\n\t\t\t// Only a dropped connection leaves a run on the server to reattach to.\n\t\t\treturn state.failure.kind === 'connection-lost'\n\t\t\t\t? watchStream('failed')\n\t\t\t\t: unchanged(state)\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'resume-pressed':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\nfunction isBlank(event: Extract<ChatTurnEvent, { kind: 'message-submitted' }>) {\n\treturn event.text.trim() === '' && (event.files?.length ?? 0) === 0\n}\n\nfunction startUserTurn(\n\tmessage: Extract<ChatTurnEvent, { kind: 'message-submitted' }>\n): ChatTurnTransition {\n\treturn {\n\t\tstate: { kind: 'requesting', userTurn: newUserTurn() },\n\t\teffects: [\n\t\t\tmessage.files === undefined || message.files.length === 0\n\t\t\t\t? { kind: 'send-message', text: message.text.trim() }\n\t\t\t\t: {\n\t\t\t\t\t\tkind: 'send-message',\n\t\t\t\t\t\ttext: message.text.trim(),\n\t\t\t\t\t\tfiles: message.files\n\t\t\t\t\t}\n\t\t]\n\t}\n}\n\nfunction watchStream(from: 'idle' | 'failed'): ChatTurnTransition {\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: { ...newUserTurn(), retried: true, watching: from }\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\n/** A reattach that showed nothing ends where it started, or with the user's queued message. */\nfunction stopWatching(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn userTurn.watching === 'failed' && userTurn.queued === undefined\n\t\t? {\n\t\t\t\tstate: { kind: 'failed', failure: { kind: 'connection-lost' } },\n\t\t\t\teffects: NO_EFFECTS\n\t\t\t}\n\t\t: endUserTurn(userTurn)\n}\n\nfunction tryAgain(resume: RunResume | undefined): ChatTurnTransition {\n\tconst userTurn =\n\t\tresume === undefined\n\t\t\t? newUserTurn()\n\t\t\t: { ...newUserTurn(), resume, resumedRunIds: [resume.runId] }\n\n\treturn {\n\t\tstate: { kind: 'requesting', userTurn },\n\t\teffects: [requestEffectOf(userTurn)]\n\t}\n}\n\nfunction newUserTurn(): ChatUserTurn {\n\treturn {\n\t\tqueued: undefined,\n\t\tretried: false,\n\t\tfollowUps: 0,\n\t\tanswering: [],\n\t\tresumedRunIds: [],\n\t\tresume: undefined,\n\t\twatching: false\n\t}\n}\n\n/**\n * A message typed mid-turn waits for the user turn to end; a second one joins\n * it, so one message goes out. Files cannot wait: such a message is refused\n * and stays in the composer.\n */\nfunction queueMessage(\n\tstate: UserTurnState,\n\tmessage: Extract<ChatTurnEvent, { kind: 'message-submitted' }>\n): ChatTurnTransition {\n\tif ((message.files?.length ?? 0) > 0) {\n\t\treturn unchanged(state)\n\t}\n\n\tconst { queued } = state.userTurn\n\tconst trimmed = message.text.trim()\n\n\treturn {\n\t\tstate: {\n\t\t\t...state,\n\t\t\tuserTurn: {\n\t\t\t\t...state.userTurn,\n\t\t\t\tqueued: queued === undefined ? trimmed : `${queued}\\n\\n${trimmed}`\n\t\t\t}\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\nfunction finishRequest(\n\tuserTurn: ChatUserTurn,\n\tcontinues: boolean\n): ChatTurnTransition {\n\treturn continues\n\t\t? {\n\t\t\t\tstate: {\n\t\t\t\t\tkind: 'continuing',\n\t\t\t\t\tuserTurn: {\n\t\t\t\t\t\t...userTurn,\n\t\t\t\t\t\tanswering: [],\n\t\t\t\t\t\tresume: undefined,\n\t\t\t\t\t\twatching: false\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\teffects: NO_EFFECTS\n\t\t\t}\n\t\t: endUserTurn(userTurn)\n}\n\n/** The request ended with changes waiting; a message the user wrote meanwhile goes out now, and the server declines them. */\nfunction waitForUser(\n\tuserTurn: ChatUserTurn,\n\tchanges: readonly PendingChange[]\n): ChatTurnTransition {\n\tif (userTurn.queued !== undefined) {\n\t\treturn moveOn(changes, { kind: 'message-submitted', text: userTurn.queued })\n\t}\n\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'waiting',\n\t\t\tuserTurn: {\n\t\t\t\t...userTurn,\n\t\t\t\tanswering: [],\n\t\t\t\tresume: undefined,\n\t\t\t\twatching: false\n\t\t\t},\n\t\t\tchanges\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\n/** The user wrote instead of answering: the message starts a new user turn, and the waiting cards say the user moved on. */\nfunction moveOn(\n\tchanges: readonly PendingChange[],\n\tmessage: Extract<ChatTurnEvent, { kind: 'message-submitted' }>\n): ChatTurnTransition {\n\tconst next = startUserTurn(message)\n\n\treturn {\n\t\tstate: next.state,\n\t\teffects: [...next.effects, ...markChanges('movedOn', changes)]\n\t}\n}\n\n/** The AI SDK sends the answers on its own; the reducer counts the request, and stops it past the budget. */\nfunction sendApprovalAnswers(\n\tstate: Extract<ChatTurnState, { kind: 'waiting' }>\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tif (userTurn.followUps >= CHAT_FOLLOW_UP_BUDGET) {\n\t\tconst failed = failUserTurn(userTurn, {\n\t\t\tkind: 'follow-up-budget-exhausted'\n\t\t})\n\n\t\treturn {\n\t\t\tstate: failed.state,\n\t\t\teffects: [{ kind: 'abort-request' }, ...failed.effects]\n\t\t}\n\t}\n\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: {\n\t\t\t\t...userTurn,\n\t\t\t\tfollowUps: userTurn.followUps + 1,\n\t\t\t\tanswering: state.changes.filter((change) => change.kind === 'approval')\n\t\t\t}\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\n/** A resume goes out once per suspended run, for a run that waits, within the budget. */\nfunction resumeRun(\n\tstate: Extract<ChatTurnState, { kind: 'waiting' }>,\n\tresume: RunResume\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\tconst run = state.changes.find(\n\t\t(change) => change.kind === 'suspended' && change.runId === resume.runId\n\t)\n\n\tif (run === undefined || userTurn.resumedRunIds.includes(resume.runId)) {\n\t\treturn unchanged(state)\n\t}\n\n\tif (userTurn.followUps >= CHAT_FOLLOW_UP_BUDGET) {\n\t\treturn failUserTurn(userTurn, { kind: 'follow-up-budget-exhausted' })\n\t}\n\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: {\n\t\t\t\t...userTurn,\n\t\t\t\tfollowUps: userTurn.followUps + 1,\n\t\t\t\tanswering: [run],\n\t\t\t\tresumedRunIds: [...userTurn.resumedRunIds, resume.runId],\n\t\t\t\tresume\n\t\t\t}\n\t\t},\n\t\teffects: [{ kind: 'resume-run', resume }]\n\t}\n}\n\n/** A request nobody asked for, such as the AI SDK answering approvals of a thread loaded from history. */\nfunction refuseUnaskedRequest(state: ChatTurnState): ChatTurnTransition {\n\treturn { state, effects: [{ kind: 'abort-request' }] }\n}\n\nfunction markChanges(\n\tmark: ChangeMark,\n\tchanges: readonly PendingChange[]\n): readonly ChatTurnEffect[] {\n\treturn changes.length === 0\n\t\t? NO_EFFECTS\n\t\t: [{ kind: 'mark-changes', mark, changes }]\n}\n\n/**\n * The one automatic retry: the stream reattached when the server keeps the\n * run going, the request sent again otherwise.\n */\nfunction retryRequest(\n\tstate: UserTurnState,\n\tresumable: boolean\n): ChatTurnTransition {\n\treturn {\n\t\tstate: { ...state, userTurn: { ...state.userTurn, retried: true } },\n\t\teffects: [\n\t\t\tresumable ? { kind: 'resume-stream' } : requestEffectOf(state.userTurn)\n\t\t]\n\t} as ChatTurnTransition\n}\n\n/** The request again: with its resume when it sent one, so the suspended run still resumes. */\nfunction requestEffectOf(userTurn: ChatUserTurn): ChatTurnEffect {\n\treturn userTurn.resume === undefined\n\t\t? { kind: 'request' }\n\t\t: { kind: 'resume-run', resume: userTurn.resume }\n}\n\nfunction sendFollowUp(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: { ...userTurn, followUps: userTurn.followUps + 1 }\n\t\t},\n\t\teffects: [{ kind: 'request' }]\n\t}\n}\n\nfunction abortRequest(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn {\n\t\tstate: { kind: 'stopping', userTurn },\n\t\teffects: [{ kind: 'abort-request' }]\n\t}\n}\n\n/** The user turn ended answered or stopped: a queued message goes out as the next user turn. */\nfunction endUserTurn(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn userTurn.queued === undefined\n\t\t? { state: initialChatTurnState, effects: NO_EFFECTS }\n\t\t: startUserTurn({ kind: 'message-submitted', text: userTurn.queued })\n}\n\n/**\n * Nothing goes out behind a failure: a queued message goes back to the\n * composer. An expired refusal marks what the request answered expired.\n */\nfunction failUserTurn(\n\tuserTurn: ChatUserTurn,\n\tfailure: ChatTurnFailure\n): ChatTurnTransition {\n\tconst isExpired =\n\t\tfailure.kind === 'refused' && failure.refusal.expired === true\n\tconst draft: readonly ChatTurnEffect[] =\n\t\tuserTurn.queued === undefined\n\t\t\t? NO_EFFECTS\n\t\t\t: [{ kind: 'restore-draft', text: userTurn.queued }]\n\tconst canResendResume = userTurn.resume !== undefined && !isExpired\n\n\treturn {\n\t\tstate: canResendResume\n\t\t\t? { kind: 'failed', failure, resume: userTurn.resume }\n\t\t\t: { kind: 'failed', failure },\n\t\teffects: isExpired\n\t\t\t? [...draft, ...markChanges('expired', userTurn.answering)]\n\t\t\t: draft\n\t}\n}\n\nfunction failureOf(\n\tevent: Extract<ChatTurnEvent, { kind: 'error-response' }>\n): ChatTurnFailure {\n\treturn event.refusal === undefined\n\t\t? { kind: 'server-error', status: event.status }\n\t\t: { kind: 'refused', refusal: event.refusal }\n}\n\nfunction unchanged(state: ChatTurnState): ChatTurnTransition {\n\treturn { state, effects: NO_EFFECTS }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"turn.js","sourceRoot":"","sources":["../../src/client/turn.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,YAAY;AAEZ;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,CAAA;AAEtC,gDAAgD;AAChD,MAAM,CAAC,MAAM,oBAAoB,GAAkB,EAAE,IAAI,EAAE,MAAM,EAAE,CAA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,KAAK,SAAS;QACjC,CAAC,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE;QAChD,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAA;AAC/C,CAAC;AAED,SAAS,SAAS,CAAC,KAAoB;IACtC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,CAAA;AACtC,CAAC","sourcesContent":["/**\n * The turn reducer: a pure reducer for one user turn, from the user's message\n * until the answer is finished, across every request it takes. The session\n * feeds it events and runs the effects it returns; every request the session\n * sends is one of those effects, except the answer to an approval, which the\n * AI SDK sends on its own and the reducer is told about. A thread loaded from\n * history never continues on its own.\n */\n\nimport type { FileUIPart } from 'ai'\n\nimport type { PendingChange } from './pending.js'\n\n// CONSTANTS\n\n/**\n * The most automatic follow-up requests one user turn may send: room for\n * several client actions in a row, while a model that keeps calling them\n * still stops. The server's own step budget still ends a runaway request.\n */\nexport const CHAT_FOLLOW_UP_BUDGET = 8\n\n/** Before any user turn: nothing is running. */\nexport const initialChatTurnState: ChatTurnState = { kind: 'idle' }\n\nconst NO_EFFECTS: readonly ChatTurnEffect[] = []\n\n// TYPES\n\n/** What Apply on a suspended run sends: the run and the data it resumes with. */\nexport interface RunResume<TData = unknown> {\n\treadonly runId: string\n\treadonly data: TData\n}\n\n/** A refusal the chat route answered, as the session's `classifyError` reads it. */\nexport interface ChatRefusal {\n\treadonly code: string\n\treadonly canTryAgain: boolean\n\t/** The approval or suspended run the request answered no longer waits. */\n\treadonly expired?: boolean\n}\n\n/** The counters of the user turn that is running. */\nexport interface ChatUserTurn {\n\t/** Text the user sent while this user turn runs; it goes out as the next user turn when this one ends answered or stopped. */\n\treadonly queued: string | undefined\n\t/** Whether this user turn has used its one automatic retry. */\n\treadonly retried: boolean\n\t/** How many automatic follow-up requests this user turn has sent: client action results, approval answers and resumes. */\n\treadonly followUps: number\n\t/** The waiting changes the request in flight answers; an expired refusal marks them expired. */\n\treadonly answering: readonly PendingChange[]\n\t/** The suspended runs this user turn has resumed: each is resumed once. */\n\treadonly resumedRunIds: readonly string[]\n\t/** The resume the request in flight sends, so a retry of the request sends it again. */\n\treadonly resume: RunResume | undefined\n\t/**\n\t * The session reattached to a stream nobody asked for here (mount,\n\t * foreground, network back), from this state: until it shows something it\n\t * ends quietly, back where it was.\n\t */\n\treadonly watching: false | 'idle' | 'failed'\n}\n\n/** What a change card says once it no longer waits without being answered. */\nexport type ChangeMark =\n\t/** The user wrote a new message instead, so the server declined it. */\n\t| 'movedOn'\n\t/** The server no longer had it waiting when the user answered. */\n\t| 'expired'\n\n/** Why a user turn ended without its answer. */\nexport type ChatTurnFailure =\n\t| { readonly kind: 'refused'; readonly refusal: ChatRefusal }\n\t| { readonly kind: 'server-error'; readonly status: number }\n\t| { readonly kind: 'connection-lost' }\n\t| { readonly kind: 'follow-up-budget-exhausted' }\n\nexport type ChatTurnState =\n\t| { readonly kind: 'idle' }\n\t| { readonly kind: 'requesting'; readonly userTurn: ChatUserTurn }\n\t| { readonly kind: 'streaming'; readonly userTurn: ChatUserTurn }\n\t| { readonly kind: 'continuing'; readonly userTurn: ChatUserTurn }\n\t| { readonly kind: 'stopping'; readonly userTurn: ChatUserTurn }\n\t/** The answer left changes for the user to answer: approvals to confirm, suspended runs to resume. Stop is not offered. */\n\t| {\n\t\t\treadonly kind: 'waiting'\n\t\t\treadonly userTurn: ChatUserTurn\n\t\t\treadonly changes: readonly PendingChange[]\n\t }\n\t| {\n\t\t\treadonly kind: 'failed'\n\t\t\treadonly failure: ChatTurnFailure\n\t\t\t/** The resume the failed request sent, which Try again sends again. */\n\t\t\treadonly resume?: RunResume\n\t }\n\nexport type ChatTurnEvent =\n\t| {\n\t\t\treadonly kind: 'message-submitted'\n\t\t\treadonly text: string\n\t\t\treadonly files?: readonly FileUIPart[]\n\t }\n\t| { readonly kind: 'content-shown' }\n\t| { readonly kind: 'request-finished'; readonly continues: boolean }\n\t| {\n\t\t\treadonly kind: 'error-response'\n\t\t\treadonly status: number\n\t\t\t/** Undefined for a response the chat route did not refuse, such as a proxy's 502. */\n\t\t\treadonly refusal: ChatRefusal | undefined\n\t }\n\t/** `resumable`: the server keeps the run going, so the stream can be reattached instead of sent again. */\n\t| { readonly kind: 'connection-lost'; readonly resumable: boolean }\n\t| { readonly kind: 'follow-up-ready' }\n\t| { readonly kind: 'stop-pressed' }\n\t| { readonly kind: 'try-again-pressed' }\n\t/** The user asked for the newest answer written again. */\n\t| { readonly kind: 'regenerate-pressed' }\n\t/**\n\t * Changes wait for the user: the request ended answered with them in its\n\t * last assistant message, or a thread loaded from history shows them.\n\t */\n\t| {\n\t\t\treadonly kind: 'changes-waiting'\n\t\t\treadonly changes: readonly PendingChange[]\n\t }\n\t/** The AI SDK started the request that carries the user's answers to the waiting approvals. */\n\t| { readonly kind: 'approval-sent' }\n\t/** The user pressed Apply on a waiting suspended run. */\n\t| { readonly kind: 'resume-pressed'; readonly resume: RunResume }\n\t/** The session reattached to the thread's running stream on its own. */\n\t| { readonly kind: 'stream-watched' }\n\t/** A reattach found no running stream. */\n\t| { readonly kind: 'nothing-to-resume' }\n\nexport type ChatTurnEffect =\n\t| {\n\t\t\treadonly kind: 'send-message'\n\t\t\treadonly text: string\n\t\t\treadonly files?: readonly FileUIPart[]\n\t }\n\t| { readonly kind: 'request' }\n\t/** Reattaches to the run the server keeps going after the connection dropped. */\n\t| { readonly kind: 'resume-stream' }\n\t| { readonly kind: 'abort-request' }\n\t| { readonly kind: 'restore-draft'; readonly text: string }\n\t/** Sends a request with `resume`, which continues the answer of the suspended run. */\n\t| { readonly kind: 'resume-run'; readonly resume: RunResume }\n\t/** Tells the cards of these changes what became of them. */\n\t| {\n\t\t\treadonly kind: 'mark-changes'\n\t\t\treadonly mark: ChangeMark\n\t\t\treadonly changes: readonly PendingChange[]\n\t }\n\nexport interface ChatTurnTransition {\n\treadonly state: ChatTurnState\n\treadonly effects: readonly ChatTurnEffect[]\n}\n\ntype UserTurnState = Extract<ChatTurnState, { userTurn: unknown }>\n\n// FUNCTIONS\n\n/**\n * A pure reducer for one user turn; the session runs its effects. An event\n * that does not apply returns the given state object and no effect, and the\n * input is never changed.\n */\nexport function reduceChatTurn(\n\tstate: ChatTurnState,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tif (event.kind === 'message-submitted' && isBlank(event)) {\n\t\treturn unchanged(state)\n\t}\n\n\tswitch (state.kind) {\n\t\tcase 'idle':\n\t\t\treturn reduceIdle(state, event)\n\t\tcase 'requesting':\n\t\t\treturn reduceRequesting(state, event)\n\t\tcase 'streaming':\n\t\t\treturn reduceStreaming(state, event)\n\t\tcase 'continuing':\n\t\t\treturn reduceContinuing(state, event)\n\t\tcase 'stopping':\n\t\t\treturn reduceStopping(state, event)\n\t\tcase 'waiting':\n\t\t\treturn reduceWaiting(state, event)\n\t\tcase 'failed':\n\t\t\treturn reduceFailed(state, event)\n\t}\n}\n\n/** Whether Try again is offered for a failure. The reducer ignores Try again when it is not. */\nexport function canTryAgain(failure: ChatTurnFailure): boolean {\n\tswitch (failure.kind) {\n\t\tcase 'refused':\n\t\t\treturn failure.refusal.canTryAgain\n\t\tcase 'server-error':\n\t\tcase 'connection-lost':\n\t\t\treturn true\n\t\tcase 'follow-up-budget-exhausted':\n\t\t\treturn false\n\t}\n}\n\n/** Whether a user turn is running: Stop is offered and a new message queues. */\nexport function isChatTurnBusy(state: ChatTurnState): boolean {\n\tswitch (state.kind) {\n\t\tcase 'requesting':\n\t\tcase 'streaming':\n\t\tcase 'continuing':\n\t\tcase 'stopping':\n\t\t\treturn true\n\t\tcase 'idle':\n\t\tcase 'waiting':\n\t\tcase 'failed':\n\t\t\treturn false\n\t}\n}\n\n/**\n * Only a message from the user starts anything: a thread loaded from history\n * never continues on its own. Its waiting changes wait for the user, and a\n * request the AI SDK starts by itself is stopped.\n */\nfunction reduceIdle(\n\tstate: Extract<ChatTurnState, { kind: 'idle' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn startUserTurn(event)\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(newUserTurn(), event.changes)\n\t\tcase 'approval-sent':\n\t\t\treturn refuseUnaskedRequest(state)\n\t\tcase 'stream-watched':\n\t\t\treturn watchStream('idle')\n\t\tcase 'regenerate-pressed':\n\t\t\treturn regenerate()\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'try-again-pressed':\n\t\tcase 'resume-pressed':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\nfunction reduceRequesting(\n\tstate: Extract<ChatTurnState, { kind: 'requesting' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'content-shown':\n\t\t\treturn {\n\t\t\t\tstate: {\n\t\t\t\t\tkind: 'streaming',\n\t\t\t\t\tuserTurn: { ...userTurn, watching: false }\n\t\t\t\t},\n\t\t\t\teffects: NO_EFFECTS\n\t\t\t}\n\t\tcase 'request-finished':\n\t\t\treturn finishRequest(userTurn, event.continues)\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(userTurn, event.changes)\n\t\tcase 'connection-lost':\n\t\t\tif (userTurn.watching !== false) {\n\t\t\t\treturn stopWatching(userTurn)\n\t\t\t}\n\n\t\t\treturn userTurn.retried\n\t\t\t\t? failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\t\t\t: retryRequest(state, event.resumable)\n\t\tcase 'error-response':\n\t\t\treturn userTurn.watching === false\n\t\t\t\t? failUserTurn(userTurn, failureOf(event))\n\t\t\t\t: stopWatching(userTurn)\n\t\tcase 'nothing-to-resume':\n\t\t\treturn userTurn.watching === false\n\t\t\t\t? failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\t\t\t: stopWatching(userTurn)\n\t\tcase 'stop-pressed':\n\t\t\treturn abortRequest(userTurn)\n\t\tcase 'follow-up-ready':\n\t\tcase 'try-again-pressed':\n\t\tcase 'regenerate-pressed':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/**\n * As while requesting, except that part of the answer is on screen: a lost\n * connection fails at once unless the stream can be reattached.\n */\nfunction reduceStreaming(\n\tstate: Extract<ChatTurnState, { kind: 'streaming' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'request-finished':\n\t\t\treturn finishRequest(userTurn, event.continues)\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(userTurn, event.changes)\n\t\tcase 'connection-lost':\n\t\t\treturn event.resumable && !userTurn.retried\n\t\t\t\t? retryRequest(state, true)\n\t\t\t\t: failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\tcase 'nothing-to-resume':\n\t\t\treturn failUserTurn(userTurn, { kind: 'connection-lost' })\n\t\tcase 'error-response':\n\t\t\treturn failUserTurn(userTurn, failureOf(event))\n\t\tcase 'stop-pressed':\n\t\t\treturn abortRequest(userTurn)\n\t\tcase 'content-shown':\n\t\tcase 'follow-up-ready':\n\t\tcase 'try-again-pressed':\n\t\tcase 'regenerate-pressed':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/** Between two requests, while the session carries out client actions. No request is in flight. */\nfunction reduceContinuing(\n\tstate: Extract<ChatTurnState, { kind: 'continuing' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'follow-up-ready':\n\t\t\treturn userTurn.followUps < CHAT_FOLLOW_UP_BUDGET\n\t\t\t\t? sendFollowUp(userTurn)\n\t\t\t\t: failUserTurn(userTurn, { kind: 'follow-up-budget-exhausted' })\n\t\tcase 'stop-pressed':\n\t\t\treturn endUserTurn(userTurn)\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'try-again-pressed':\n\t\tcase 'regenerate-pressed':\n\t\tcase 'changes-waiting':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/** Stop was pressed: however the request in flight settles, the user turn ends. */\nfunction reduceStopping(\n\tstate: Extract<ChatTurnState, { kind: 'stopping' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn queueMessage(state, event)\n\t\tcase 'request-finished':\n\t\tcase 'connection-lost':\n\t\tcase 'error-response':\n\t\tcase 'changes-waiting':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn endUserTurn(state.userTurn)\n\t\tcase 'content-shown':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'try-again-pressed':\n\t\tcase 'regenerate-pressed':\n\t\tcase 'approval-sent':\n\t\tcase 'resume-pressed':\n\t\tcase 'stream-watched':\n\t\t\treturn unchanged(state)\n\t}\n}\n\n/**\n * The answer waits for the user, with no request in flight. Their answers to\n * the approvals and a resume go on with the same user turn; a message they\n * write goes out at once, because the server declines what waits, and the\n * waiting cards learn that the user moved on.\n */\nfunction reduceWaiting(\n\tstate: Extract<ChatTurnState, { kind: 'waiting' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn moveOn(state.changes, event)\n\t\tcase 'approval-sent':\n\t\t\treturn sendApprovalAnswers(state)\n\t\tcase 'resume-pressed':\n\t\t\treturn resumeRun(state, event.resume)\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'try-again-pressed':\n\t\tcase 'regenerate-pressed':\n\t\tcase 'changes-waiting':\n\t\tcase 'stream-watched':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\nfunction reduceFailed(\n\tstate: Extract<ChatTurnState, { kind: 'failed' }>,\n\tevent: ChatTurnEvent\n): ChatTurnTransition {\n\tswitch (event.kind) {\n\t\tcase 'message-submitted':\n\t\t\treturn startUserTurn(event)\n\t\tcase 'try-again-pressed':\n\t\t\treturn canTryAgain(state.failure)\n\t\t\t\t? tryAgain(state.resume)\n\t\t\t\t: unchanged(state)\n\t\tcase 'regenerate-pressed':\n\t\t\treturn regenerate()\n\t\tcase 'changes-waiting':\n\t\t\treturn waitForUser(newUserTurn(), event.changes)\n\t\tcase 'approval-sent':\n\t\t\treturn refuseUnaskedRequest(state)\n\t\tcase 'stream-watched':\n\t\t\t// Only a dropped connection leaves a run on the server to reattach to.\n\t\t\treturn state.failure.kind === 'connection-lost'\n\t\t\t\t? watchStream('failed')\n\t\t\t\t: unchanged(state)\n\t\tcase 'content-shown':\n\t\tcase 'request-finished':\n\t\tcase 'error-response':\n\t\tcase 'connection-lost':\n\t\tcase 'follow-up-ready':\n\t\tcase 'stop-pressed':\n\t\tcase 'resume-pressed':\n\t\tcase 'nothing-to-resume':\n\t\t\treturn unchanged(state)\n\t}\n}\n\nfunction isBlank(event: Extract<ChatTurnEvent, { kind: 'message-submitted' }>) {\n\treturn event.text.trim() === '' && (event.files?.length ?? 0) === 0\n}\n\nfunction startUserTurn(\n\tmessage: Extract<ChatTurnEvent, { kind: 'message-submitted' }>\n): ChatTurnTransition {\n\treturn {\n\t\tstate: { kind: 'requesting', userTurn: newUserTurn() },\n\t\teffects: [\n\t\t\tmessage.files === undefined || message.files.length === 0\n\t\t\t\t? { kind: 'send-message', text: message.text.trim() }\n\t\t\t\t: {\n\t\t\t\t\t\tkind: 'send-message',\n\t\t\t\t\t\ttext: message.text.trim(),\n\t\t\t\t\t\tfiles: message.files\n\t\t\t\t\t}\n\t\t]\n\t}\n}\n\n/** A new user turn that writes the newest answer again from the user's message. */\nfunction regenerate(): ChatTurnTransition {\n\treturn {\n\t\tstate: { kind: 'requesting', userTurn: newUserTurn() },\n\t\teffects: [{ kind: 'request' }]\n\t}\n}\n\nfunction watchStream(from: 'idle' | 'failed'): ChatTurnTransition {\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: { ...newUserTurn(), retried: true, watching: from }\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\n/** A reattach that showed nothing ends where it started, or with the user's queued message. */\nfunction stopWatching(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn userTurn.watching === 'failed' && userTurn.queued === undefined\n\t\t? {\n\t\t\t\tstate: { kind: 'failed', failure: { kind: 'connection-lost' } },\n\t\t\t\teffects: NO_EFFECTS\n\t\t\t}\n\t\t: endUserTurn(userTurn)\n}\n\nfunction tryAgain(resume: RunResume | undefined): ChatTurnTransition {\n\tconst userTurn =\n\t\tresume === undefined\n\t\t\t? newUserTurn()\n\t\t\t: { ...newUserTurn(), resume, resumedRunIds: [resume.runId] }\n\n\treturn {\n\t\tstate: { kind: 'requesting', userTurn },\n\t\teffects: [requestEffectOf(userTurn)]\n\t}\n}\n\nfunction newUserTurn(): ChatUserTurn {\n\treturn {\n\t\tqueued: undefined,\n\t\tretried: false,\n\t\tfollowUps: 0,\n\t\tanswering: [],\n\t\tresumedRunIds: [],\n\t\tresume: undefined,\n\t\twatching: false\n\t}\n}\n\n/**\n * A message typed mid-turn waits for the user turn to end; a second one joins\n * it, so one message goes out. Files cannot wait: such a message is refused\n * and stays in the composer.\n */\nfunction queueMessage(\n\tstate: UserTurnState,\n\tmessage: Extract<ChatTurnEvent, { kind: 'message-submitted' }>\n): ChatTurnTransition {\n\tif ((message.files?.length ?? 0) > 0) {\n\t\treturn unchanged(state)\n\t}\n\n\tconst { queued } = state.userTurn\n\tconst trimmed = message.text.trim()\n\n\treturn {\n\t\tstate: {\n\t\t\t...state,\n\t\t\tuserTurn: {\n\t\t\t\t...state.userTurn,\n\t\t\t\tqueued: queued === undefined ? trimmed : `${queued}\\n\\n${trimmed}`\n\t\t\t}\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\nfunction finishRequest(\n\tuserTurn: ChatUserTurn,\n\tcontinues: boolean\n): ChatTurnTransition {\n\treturn continues\n\t\t? {\n\t\t\t\tstate: {\n\t\t\t\t\tkind: 'continuing',\n\t\t\t\t\tuserTurn: {\n\t\t\t\t\t\t...userTurn,\n\t\t\t\t\t\tanswering: [],\n\t\t\t\t\t\tresume: undefined,\n\t\t\t\t\t\twatching: false\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\teffects: NO_EFFECTS\n\t\t\t}\n\t\t: endUserTurn(userTurn)\n}\n\n/** The request ended with changes waiting; a message the user wrote meanwhile goes out now, and the server declines them. */\nfunction waitForUser(\n\tuserTurn: ChatUserTurn,\n\tchanges: readonly PendingChange[]\n): ChatTurnTransition {\n\tif (userTurn.queued !== undefined) {\n\t\treturn moveOn(changes, { kind: 'message-submitted', text: userTurn.queued })\n\t}\n\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'waiting',\n\t\t\tuserTurn: {\n\t\t\t\t...userTurn,\n\t\t\t\tanswering: [],\n\t\t\t\tresume: undefined,\n\t\t\t\twatching: false\n\t\t\t},\n\t\t\tchanges\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\n/** The user wrote instead of answering: the message starts a new user turn, and the waiting cards say the user moved on. */\nfunction moveOn(\n\tchanges: readonly PendingChange[],\n\tmessage: Extract<ChatTurnEvent, { kind: 'message-submitted' }>\n): ChatTurnTransition {\n\tconst next = startUserTurn(message)\n\n\treturn {\n\t\tstate: next.state,\n\t\teffects: [...next.effects, ...markChanges('movedOn', changes)]\n\t}\n}\n\n/** The AI SDK sends the answers on its own; the reducer counts the request, and stops it past the budget. */\nfunction sendApprovalAnswers(\n\tstate: Extract<ChatTurnState, { kind: 'waiting' }>\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\n\tif (userTurn.followUps >= CHAT_FOLLOW_UP_BUDGET) {\n\t\tconst failed = failUserTurn(userTurn, {\n\t\t\tkind: 'follow-up-budget-exhausted'\n\t\t})\n\n\t\treturn {\n\t\t\tstate: failed.state,\n\t\t\teffects: [{ kind: 'abort-request' }, ...failed.effects]\n\t\t}\n\t}\n\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: {\n\t\t\t\t...userTurn,\n\t\t\t\tfollowUps: userTurn.followUps + 1,\n\t\t\t\tanswering: state.changes.filter((change) => change.kind === 'approval')\n\t\t\t}\n\t\t},\n\t\teffects: NO_EFFECTS\n\t}\n}\n\n/** A resume goes out once per suspended run, for a run that waits, within the budget. */\nfunction resumeRun(\n\tstate: Extract<ChatTurnState, { kind: 'waiting' }>,\n\tresume: RunResume\n): ChatTurnTransition {\n\tconst { userTurn } = state\n\tconst run = state.changes.find(\n\t\t(change) => change.kind === 'suspended' && change.runId === resume.runId\n\t)\n\n\tif (run === undefined || userTurn.resumedRunIds.includes(resume.runId)) {\n\t\treturn unchanged(state)\n\t}\n\n\tif (userTurn.followUps >= CHAT_FOLLOW_UP_BUDGET) {\n\t\treturn failUserTurn(userTurn, { kind: 'follow-up-budget-exhausted' })\n\t}\n\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: {\n\t\t\t\t...userTurn,\n\t\t\t\tfollowUps: userTurn.followUps + 1,\n\t\t\t\tanswering: [run],\n\t\t\t\tresumedRunIds: [...userTurn.resumedRunIds, resume.runId],\n\t\t\t\tresume\n\t\t\t}\n\t\t},\n\t\teffects: [{ kind: 'resume-run', resume }]\n\t}\n}\n\n/** A request nobody asked for, such as the AI SDK answering approvals of a thread loaded from history. */\nfunction refuseUnaskedRequest(state: ChatTurnState): ChatTurnTransition {\n\treturn { state, effects: [{ kind: 'abort-request' }] }\n}\n\nfunction markChanges(\n\tmark: ChangeMark,\n\tchanges: readonly PendingChange[]\n): readonly ChatTurnEffect[] {\n\treturn changes.length === 0\n\t\t? NO_EFFECTS\n\t\t: [{ kind: 'mark-changes', mark, changes }]\n}\n\n/**\n * The one automatic retry: the stream reattached when the server keeps the\n * run going, the request sent again otherwise.\n */\nfunction retryRequest(\n\tstate: UserTurnState,\n\tresumable: boolean\n): ChatTurnTransition {\n\treturn {\n\t\tstate: { ...state, userTurn: { ...state.userTurn, retried: true } },\n\t\teffects: [\n\t\t\tresumable ? { kind: 'resume-stream' } : requestEffectOf(state.userTurn)\n\t\t]\n\t} as ChatTurnTransition\n}\n\n/** The request again: with its resume when it sent one, so the suspended run still resumes. */\nfunction requestEffectOf(userTurn: ChatUserTurn): ChatTurnEffect {\n\treturn userTurn.resume === undefined\n\t\t? { kind: 'request' }\n\t\t: { kind: 'resume-run', resume: userTurn.resume }\n}\n\nfunction sendFollowUp(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn {\n\t\tstate: {\n\t\t\tkind: 'requesting',\n\t\t\tuserTurn: { ...userTurn, followUps: userTurn.followUps + 1 }\n\t\t},\n\t\teffects: [{ kind: 'request' }]\n\t}\n}\n\nfunction abortRequest(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn {\n\t\tstate: { kind: 'stopping', userTurn },\n\t\teffects: [{ kind: 'abort-request' }]\n\t}\n}\n\n/** The user turn ended answered or stopped: a queued message goes out as the next user turn. */\nfunction endUserTurn(userTurn: ChatUserTurn): ChatTurnTransition {\n\treturn userTurn.queued === undefined\n\t\t? { state: initialChatTurnState, effects: NO_EFFECTS }\n\t\t: startUserTurn({ kind: 'message-submitted', text: userTurn.queued })\n}\n\n/**\n * Nothing goes out behind a failure: a queued message goes back to the\n * composer. An expired refusal marks what the request answered expired.\n */\nfunction failUserTurn(\n\tuserTurn: ChatUserTurn,\n\tfailure: ChatTurnFailure\n): ChatTurnTransition {\n\tconst isExpired =\n\t\tfailure.kind === 'refused' && failure.refusal.expired === true\n\tconst draft: readonly ChatTurnEffect[] =\n\t\tuserTurn.queued === undefined\n\t\t\t? NO_EFFECTS\n\t\t\t: [{ kind: 'restore-draft', text: userTurn.queued }]\n\tconst canResendResume = userTurn.resume !== undefined && !isExpired\n\n\treturn {\n\t\tstate: canResendResume\n\t\t\t? { kind: 'failed', failure, resume: userTurn.resume }\n\t\t\t: { kind: 'failed', failure },\n\t\teffects: isExpired\n\t\t\t? [...draft, ...markChanges('expired', userTurn.answering)]\n\t\t\t: draft\n\t}\n}\n\nfunction failureOf(\n\tevent: Extract<ChatTurnEvent, { kind: 'error-response' }>\n): ChatTurnFailure {\n\treturn event.refusal === undefined\n\t\t? { kind: 'server-error', status: event.status }\n\t\t: { kind: 'refused', refusal: event.refusal }\n}\n\nfunction unchanged(state: ChatTurnState): ChatTurnTransition {\n\treturn { state, effects: NO_EFFECTS }\n}\n"]}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { type DictationEnd, type DictationOptions } from '../index.js';
|
|
2
2
|
/** Where the dictation is: asking for the microphone and a key, listening, or waiting for the last words. */
|
|
3
3
|
export type DictationPhase = 'idle' | 'starting' | 'listening' | 'finishing';
|
|
4
|
-
/** Why the last dictation gave no text: the microphone was refused, or dictation failed. */
|
|
5
|
-
export type DictationNotice = 'permission_denied' | 'unavailable';
|
|
4
|
+
/** Why the last dictation gave no text: the microphone was refused or is missing, or dictation failed. */
|
|
5
|
+
export type DictationNotice = 'permission_denied' | 'no_microphone' | 'unavailable';
|
|
6
6
|
/** The live text, published per token. */
|
|
7
7
|
export interface DictationTranscriptChannel {
|
|
8
8
|
readonly subscribe: (listener: () => void) => () => void;
|
|
@@ -12,6 +12,8 @@ export interface UseDictationOptions extends Omit<DictationOptions, 'signal' | '
|
|
|
12
12
|
/** The text, when a dictation that heard something stops or fails; never mid-way and never after `cancel`. */
|
|
13
13
|
readonly onDictated?: (text: string) => void;
|
|
14
14
|
readonly onEnd?: (end: DictationEnd) => void;
|
|
15
|
+
/** The notice, once, when a dictation gives no text for one of its reasons: show a toast here. */
|
|
16
|
+
readonly onNotice?: (notice: DictationNotice) => void;
|
|
15
17
|
}
|
|
16
18
|
export declare function useDictation(options: UseDictationOptions): {
|
|
17
19
|
phase: DictationPhase;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/dictation/react/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,gBAAgB,EAIrB,MAAM,aAAa,CAAA;AAEpB,6GAA6G;AAC7G,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,CAAA;AAE5E,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/dictation/react/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACN,KAAK,YAAY,EACjB,KAAK,gBAAgB,EAIrB,MAAM,aAAa,CAAA;AAEpB,6GAA6G;AAC7G,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,CAAA;AAE5E,0GAA0G;AAC1G,MAAM,MAAM,eAAe,GACxB,mBAAmB,GACnB,eAAe,GACf,aAAa,CAAA;AAEhB,0CAA0C;AAC1C,MAAM,WAAW,0BAA0B;IAC1C,QAAQ,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAA;IACxD,QAAQ,CAAC,IAAI,EAAE,MAAM,MAAM,CAAA;CAC3B;AAED,MAAM,WAAW,mBAChB,SAAQ,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,OAAO,CAAC;IAClD,8GAA8G;IAC9G,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAC5C,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,YAAY,KAAK,IAAI,CAAA;IAC5C,kGAAkG;IAClG,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,KAAK,IAAI,CAAA;CACrD;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,mBAAmB;;;;8BA2FjC,MAAM,IAAI;;wBAQhB,MAAM;;;;;EApBvB;AAED,4FAA4F;AAC5F,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,0BAA0B,GAAG,MAAM,CAE5E"}
|
|
@@ -51,6 +51,8 @@ export function useDictation(options) {
|
|
|
51
51
|
latest.current.onEnd?.(end);
|
|
52
52
|
setPhase('idle');
|
|
53
53
|
setNotice(end.kind === 'failed' ? 'unavailable' : undefined);
|
|
54
|
+
if (end.kind === 'failed')
|
|
55
|
+
latest.current.onNotice?.('unavailable');
|
|
54
56
|
}
|
|
55
57
|
});
|
|
56
58
|
starting.current = undefined;
|
|
@@ -62,7 +64,13 @@ export function useDictation(options) {
|
|
|
62
64
|
return;
|
|
63
65
|
}
|
|
64
66
|
setPhase('idle');
|
|
65
|
-
|
|
67
|
+
if (started.kind === 'cancelled') {
|
|
68
|
+
setNotice(undefined);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
setNotice(started.kind);
|
|
72
|
+
latest.current.onNotice?.(started.kind);
|
|
73
|
+
}
|
|
66
74
|
}, [transcript]);
|
|
67
75
|
useEffect(() => cancel, [cancel]);
|
|
68
76
|
return { phase, notice, transcript, start, stop, cancel };
|