Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🗳️ feat: Approval-Responded, Output-Denied and Ask-User Parts in the UI Message View (#16961)
Marco Beretta8829a3975c14
diff --git a/packages/chat/src/core/ask.ts b/packages/chat/src/core/ask.tsindex 5a58c5deac..bd0e7af3d6 100644--- a/packages/chat/src/core/ask.ts+++ b/packages/chat/src/core/ask.ts@@ -1,4 +1,5 @@-import type { Agents, TMessageContentParts } from 'librechat-data-provider';+import { ASK_USER_QUESTION } from 'librechat-data-provider';+import type { AskUserQuestionContentPart, TMessageContentParts } from 'librechat-data-provider';/*** UI-only content-part type used to render an `ask_user_question` pause inline@@ -7,21 +8,10 @@ import type { Agents, TMessageContentParts } from 'librechat-data-provider';* strings), so it survives SSE, sync, and DB rehydration without a new wire* type or an extra field on `TMessage`.*/-export const ASK_USER_QUESTION = 'ask_user_question' as const;+export { ASK_USER_QUESTION };/** Shape of the synthetic content part carrying an ask-user pending action. */-export interface AskUserQuestionPart {- type: typeof ASK_USER_QUESTION;- [ASK_USER_QUESTION]: {- actionId: string;- question: Agents.AskUserQuestionRequest;- questions?: Agents.AskUserQuestionBatchItem[];- /** The ask tool call that raised the pause (present from- * `@librechat/agents` > 3.3.8); lets the answer stamp target the exact- * tool-call part in multi-ask turns. */- tool_call_id?: string;- };-}+export type AskUserQuestionPart = AskUserQuestionContentPart;/*** The synthetic type isn't in the `ContentTypes` union, so this reads thediff --git a/packages/data-provider/src/parts.spec.ts b/packages/data-provider/src/parts.spec.tsindex 211dcdd9e4..6abe45da8b 100644--- a/packages/data-provider/src/parts.spec.ts+++ b/packages/data-provider/src/parts.spec.ts@@ -1,4 +1,10 @@-import type { UIMappingOptions, MappableContentPart, UIMessagePart, UIToolPart } from './parts';+import type {+ AskUserQuestionContentPart,+ MappableContentPart,+ UIMappingOptions,+ UIMessagePart,+ UIToolPart,+} from './parts';import type { TMessageContentParts, PartMetadata } from './types/content';import type { TAttachment, TMessage } from './schemas';import type { Agents } from './types/agents';@@ -403,7 +409,7 @@ describe('parts', () => {expect(parts).toHaveLength(content.length);for (let i = 0; i < content.length; i++) {- const part = content[i] as MappableContentPart | undefined;+ const part = content[i] as TMessageContentParts | undefined;expect(parts[i].type).toBe(part ? expectedUITypes[part.type] : 'step-start');}expect(fromUIParts(parts)).toStrictEqual(content);@@ -428,7 +434,7 @@ describe('parts', () => {const parts = toUIParts(content);expect(parts).toHaveLength(length);for (let i = 0; i < length; i++) {- const part = content[i] as MappableContentPart | undefined;+ const part = content[i] as TMessageContentParts | undefined;expect(parts[i].type).toBe(part ? expectedUITypes[part.type] : 'step-start');}expect(fromUIParts(parts)).toStrictEqual(content);@@ -1226,4 +1232,225 @@ describe('parts', () => {expect(parts.filter(isUIDataPart).map((part) => part.type)).toEqual(['data-agent-update']);expect(parts.filter(isUIToolPart).map((part) => part.toolCallId)).toEqual(['call-1']);});++ describe('approval decisions', () => {+ const approval = (+ fields: Partial<NonNullable<Agents.ToolCall['approval']>> = {},+ ): NonNullable<Agents.ToolCall['approval']> => ({+ actionId: 'action-1',+ allowed_decisions: ['approve', 'reject', 'edit', 'respond'],+ description: 'Run it?',+ ...fields,+ });+ const toolCall = (fields: Partial<Agents.ToolCall> & PartMetadata): TMessageContentParts => ({+ type: ContentTypes.TOOL_CALL,+ tool_call: {+ type: 'tool_call',+ name: 'search',+ id: 'call-1',+ args: '{"q":"cats"}',+ ...fields,+ },+ });+ const withoutMetadata = ({ callProviderMetadata: _stored, ...part }: UIToolPart) => part;++ it.each([+ ['approve', true],+ ['edit', true],+ ['reject', false],+ ['respond', false],+ ] as const)(+ 'reports the %s decision awaiting the resume as approval-responded',+ (decision, approved) => {+ const stored = toolCall({ approval: approval({ decision, reason: 'Checked' }) });++ expect(toUIPart(stored)).toMatchObject({+ state: 'approval-responded',+ input: { q: 'cats' },+ approval: { id: 'action-1', requestReason: 'Run it?', approved, reason: 'Checked' },+ });+ expect(fromUIPart(toUIPart(stored))).toStrictEqual(stored);+ },+ );++ it('reports a denied call that ended as output-denied, without its output', () => {+ const rejected = toolCall({+ approval: approval({ decision: 'reject', reason: 'Too broad' }),+ output: 'The user rejected this tool call.',+ progress: 1,+ });+ const failed = toolCall({+ approval: approval({ decision: 'reject' }),+ runStepStatus: 'failed',+ });++ const part = toUIPart(rejected);+ expect(withoutMetadata(part as UIToolPart)).toStrictEqual({+ type: 'tool-search',+ toolCallId: 'call-1',+ state: 'output-denied',+ input: { q: 'cats' },+ approval: {+ id: 'action-1',+ requestReason: 'Run it?',+ approved: false,+ reason: 'Too broad',+ },+ });+ expect(toUIPart(failed)).toMatchObject({+ state: 'output-denied',+ approval: { approved: false },+ });+ expect(fromUIPart(part)).toStrictEqual(rejected);+ expect(fromUIPart(toUIPart(failed))).toStrictEqual(failed);+ });++ it("reports a respond decision that ended as output-available with the reviewer's output", () => {+ const responded = toolCall({+ approval: approval({ decision: 'respond' }),+ output: 'human response',+ progress: 1,+ });++ const part = toUIPart(responded);+ expect(withoutMetadata(part as UIToolPart)).toStrictEqual({+ type: 'tool-search',+ toolCallId: 'call-1',+ state: 'output-available',+ input: { q: 'cats' },+ output: 'human response',+ });+ expect(fromUIPart(part)).toStrictEqual(responded);+ });++ it("keeps a respond decision's output successful even when it reads like an error", () => {+ const responded = toolCall({+ approval: approval({ decision: 'respond' }),+ output: 'Error: tool call failed: use the cached result instead',+ progress: 1,+ });+ const resolveToolFailure = jest.fn(() => 'failed');++ const part = toUIPart(responded, 0, { resolveToolFailure });+ expect(part).toMatchObject({+ state: 'output-available',+ output: 'Error: tool call failed: use the cached result instead',+ });+ expect(part).not.toHaveProperty('errorText');+ expect(resolveToolFailure).not.toHaveBeenCalled();+ expect(fromUIPart(part)).toStrictEqual(responded);+ expect(+ toUIPart(+ toolCall({ approval: approval({ decision: 'respond' }), runStepStatus: 'failed' }),+ ),+ ).toMatchObject({ state: 'output-error' });+ });++ it('keeps an approved call approved through its output or failure', () => {+ const succeeded = toolCall({ approval: approval({ decision: 'approve' }), output: '3' });+ const failed = toolCall({+ approval: approval({ decision: 'edit' }),+ output: 'Error: tool call failed: timeout',+ });++ expect(toUIPart(succeeded)).toMatchObject({+ state: 'output-available',+ output: '3',+ approval: { id: 'action-1', approved: true },+ });+ expect(toUIPart(failed)).toMatchObject({+ state: 'output-error',+ approval: { id: 'action-1', approved: true },+ });+ expect(fromUIPart(toUIPart(succeeded))).toStrictEqual(succeeded);+ expect(fromUIPart(toUIPart(failed))).toStrictEqual(failed);+ });++ it('drops an undecided approval once the call has output, as before', () => {+ const answered = toolCall({ approval: approval(), output: '3' });++ expect(toUIPart(answered)).not.toHaveProperty('approval');+ expect(toUIPart(answered)).toMatchObject({ state: 'output-available' });+ });++ it.each<UIToolPart>([+ {+ type: 'tool-search',+ toolCallId: 'call-9',+ state: 'approval-responded',+ input: { q: 'dogs' },+ approval: { id: 'action-9', approved: true },+ },+ {+ type: 'tool-search',+ toolCallId: 'call-9',+ state: 'approval-responded',+ input: { q: 'dogs' },+ approval: { id: 'action-9', requestReason: 'Search?', approved: false, reason: 'No' },+ },+ {+ type: 'tool-search',+ toolCallId: 'call-9',+ state: 'output-denied',+ input: { q: 'dogs' },+ approval: { id: 'action-9', approved: false, reason: 'No' },+ },+ {+ type: 'tool-search',+ toolCallId: 'call-9',+ state: 'output-available',+ input: { q: 'dogs' },+ output: 'found',+ approval: { id: 'action-9', approved: true },+ },+ ])('rebuilds a hand-built $state part without a stored base', (part) => {+ expect(withoutMetadata(toUIPart(fromUIPart(part)) as UIToolPart)).toStrictEqual(part);+ });+ });++ describe('ask-user pauses', () => {+ const ask: AskUserQuestionContentPart = {+ type: 'ask_user_question',+ ask_user_question: {+ actionId: 'action-2',+ question: { question: 'Which city?', options: [{ label: 'Rome', value: 'rome' }] },+ questions: [{ id: 'city', header: 'City', question: 'Which city?' }],+ tool_call_id: 'ask-1',+ },+ };+ const content: TMessageContentParts[] = [+ { type: ContentTypes.TEXT, text: 'Before I search' },+ ask as unknown as TMessageContentParts,+ ];++ it('maps the pause to a data-ask-user-question part and back', () => {+ const part = toUIPart(ask);++ expect(part).toStrictEqual({+ type: 'data-ask-user-question',+ data: { ask_user_question: ask.ask_user_question },+ });+ expect(isUIDataPart(part)).toBe(true);+ expect(fromUIPart(part)).toStrictEqual(ask);+ });++ it('keeps the pause in its content slot', () => {+ const parts = toUIParts([undefined, ask]);++ expect(parts.map((part) => part.type)).toEqual(['step-start', 'data-ask-user-question']);+ const restored = fromUIParts(parts);+ expect(restored).toHaveLength(2);+ expect(0 in restored).toBe(false);+ expect(restored[1]).toStrictEqual(ask);+ });++ it('round-trips a message carrying the pause, with and without a stored base', () => {+ const message = createMessage({ content, text: 'Before I search' });+ const view = toUIMessage(message);++ expect(view.parts.map((part) => part.type)).toEqual(['text', 'data-ask-user-question']);+ expect(fromUIMessage(view, message)).toStrictEqual(message);+ expect(fromUIMessage(view)).toStrictEqual(message);+ });+ });});diff --git a/packages/data-provider/src/parts.ts b/packages/data-provider/src/parts.tsindex a605a90070..84b715eded 100644--- a/packages/data-provider/src/parts.ts+++ b/packages/data-provider/src/parts.ts@@ -9,7 +9,7 @@ import { Tools } from './types/tools';/*** A UI parts view of LibreChat message content, modelled on the AI SDK `UIMessage`.*- * Matched against `[email protected]` (`UIMessage`, `UIMessagePart`, `UIToolInvocation`) and+ * Matched against `[email protected]` (`UIMessage`, `UIMessagePart`, `UIToolInvocation`) and* `@ai-sdk/[email protected]` (`useChat`). The shapes are structural copies, not imports: this* package does not depend on the AI SDK.*@@ -27,7 +27,10 @@ export type TextDeltaContentPart = {} & ContentMetadata;/** Every content part the mapping accepts. */-export type MappableContentPart = TMessageContentParts | TextDeltaContentPart;+export type MappableContentPart =+ | TMessageContentParts+ | TextDeltaContentPart+ | AskUserQuestionContentPart;type ContentPartOf<T extends ContentTypes> = Extract<MappableContentPart, { type: T }>;type ToolCallContentPart = ContentPartOf<ContentTypes.TOOL_CALL>;@@ -118,16 +121,27 @@ export type UISourceUrlPart = {/** AI SDK `StepStartUIPart`; stands in for a content slot no step has written yet. */export type UIStepStartPart = { type: 'step-start' };-/** The subset of AI SDK tool states LibreChat content can express. */+/** AI SDK tool states. */export type UIToolState =| 'input-streaming'| 'input-available'| 'approval-requested'+ | 'approval-responded'| 'output-available'- | 'output-error';+ | 'output-error'+ | 'output-denied';-/** AI SDK tool `approval`, for a call paused for human review. */-export type UIToolApproval = { id: string; requestReason?: string };+/**+ * AI SDK tool `approval`, for a call paused for human review. `approved` and `reason` are set once+ * the reviewer decided: `approve` and `edit` approve the call, `reject` and `respond` deny it. A+ * `respond` still ends with the reviewer's text as the call's output, so it never reads as denied.+ */+export type UIToolApproval = {+ id: string;+ requestReason?: string;+ approved?: boolean;+ reason?: string;+};export type UIToolInput = Agents.ToolCall['args'] | object;export type UIToolOutput = string | CodeInterpreterOutputs;@@ -144,7 +158,8 @@ export type UIToolPartMetadata = Omit<ToolCallContentPart, 'type' | 'tool_call'>/*** AI SDK `ToolUIPart`: `tool-<name>` with a lifecycle `state`. Cancelled, failed, rejected and* background-cancelled calls all surface as `output-error`; the exact marker stays on the stored- * call. A call paused for human review is `approval-requested`.+ * call. A call paused for human review is `approval-requested`, then `approval-responded` once the+ * reviewer decided, and `output-denied` when a denied call has ended.*/export type UIToolPart = {type: `tool-${string}`;@@ -159,14 +174,44 @@ export type UIToolPart = {type DataOf<T extends ContentTypes> = Omit<ContentPartOf<T>, 'type'>;-/** AI SDK `DataUIPart`: LibreChat parts with no AI SDK counterpart, carried whole. */-export type UIDataPart =+/** The content part type of an `ask_user_question` pause, which `ContentTypes` does not list. */+export const ASK_USER_QUESTION = 'ask_user_question' as const;++/** The pending question an `ask_user_question` pause shows inline with the message content. */+export type AskUserQuestionData = {+ actionId: string;+ question: Agents.AskUserQuestionRequest;+ questions?: Agents.AskUserQuestionBatchItem[];+ /** The ask tool call that raised the pause, when the server reported it. */+ tool_call_id?: string;+};++/**+ * Client-written content part for an `ask_user_question` pause. It rides in the content array+ * under a type outside `ContentTypes`, so it occupies a slot like any other part.+ */+export type AskUserQuestionContentPart = ContentMetadata & {+ type: typeof ASK_USER_QUESTION;+ [ASK_USER_QUESTION]: AskUserQuestionData;+};++/** Parts that map to a `data-*` part one to one through `ContentTypes`. */+type ContentDataPart =| { type: 'data-agent-update'; data: DataOf<ContentTypes.AGENT_UPDATE> }| { type: 'data-summary'; data: DataOf<ContentTypes.SUMMARY> }| { type: 'data-activity-label'; data: DataOf<ContentTypes.ACTIVITY_LABEL> }| { type: 'data-steer'; data: DataOf<ContentTypes.STEER> }| { type: 'data-error'; data: DataOf<ContentTypes.ERROR> };+/** A pending `ask_user_question` pause, carried whole. */+export type UIAskUserQuestionPart = {+ type: 'data-ask-user-question';+ data: Omit<AskUserQuestionContentPart, 'type'>;+};++/** AI SDK `DataUIPart`: LibreChat parts with no AI SDK counterpart, carried whole. */+export type UIDataPart = ContentDataPart | UIAskUserQuestionPart;+/** AI SDK `UIMessagePart`, narrowed to what LibreChat content produces. */export type UIMessagePart =| UITextPart@@ -220,7 +265,7 @@ export type UIMessage = {parts: UIMessagePart[];};-type DataPartType = UIDataPart['type'];+type DataPartType = ContentDataPart['type'];const dataPartTypes = {[ContentTypes.AGENT_UPDATE]: 'data-agent-update',@@ -431,12 +476,19 @@ const resolveFailure = (return reason ? { reason, detail: false } : undefined;};+const approvingDecisions = new Set<Agents.ToolApprovalDecisionType>(['approve', 'edit']);+const getToolApproval = (toolCall: ToolCallValue): UIToolApproval | undefined => {if (!('name' in toolCall) || !toolCall.approval) {return undefined;}- const { actionId, description } = toolCall.approval;- return { id: actionId, ...(description && { requestReason: description }) };+ const { actionId, description, decision, reason } = toolCall.approval;+ return {+ id: actionId,+ ...(description && { requestReason: description }),+ ...(decision && { approved: approvingDecisions.has(decision) }),+ ...(decision && reason !== undefined && { reason }),+ };};const toToolPart = (@@ -448,17 +500,26 @@ const toToolPart = (const { name, id, args, output, submitted } = readToolCall(toolCall);const { input, complete } = parseToolInput(args);const { runStepStatus, progress } = toolCall;- const failure = resolveFailure(toolCall, output, context);- const approval = submitted ? undefined : getToolApproval(toolCall);+ const responded = 'name' in toolCall && toolCall.approval?.decision === 'respond';+ const failure = responded ? getToolFailure(toolCall) : resolveFailure(toolCall, output, context);+ const approval = getToolApproval(toolCall);+ const ended = submitted || runStepStatus === 'completed' || (progress ?? 0) >= 1;let state: UIToolState = complete ? 'input-available' : 'input-streaming';- if (failure) {+ if (approval?.approved === false && !responded && (ended || failure)) {+ state = 'output-denied';+ } else if (failure) {state = 'output-error';- } else if (submitted || runStepStatus === 'completed' || (progress ?? 0) >= 1) {+ } else if (ended) {state = 'output-available';} else if (approval) {- state = 'approval-requested';+ state = approval.approved === undefined ? 'approval-requested' : 'approval-responded';}+ const showsApproval =+ state === 'approval-requested' ||+ state === 'approval-responded' ||+ state === 'output-denied' ||+ approval?.approved === true;return {type: `tool-${name}`,@@ -469,7 +530,7 @@ const toToolPart = (...(state === 'output-error' && {errorText: failure?.detail && typeof output === 'string' && output ? output : failure?.reason,}),- ...(state === 'approval-requested' && { approval }),+ ...(showsApproval && { approval }),callProviderMetadata: { librechat: { ...partMetadata, toolCall } },};};@@ -577,6 +638,10 @@ export function toUIPart(case ContentTypes.STEER:case ContentTypes.ERROR:return toDataPart(part);+ case ASK_USER_QUESTION: {+ const { type: _type, ...data } = part;+ return { type: 'data-ask-user-question', data };+ }}return stepStart;}@@ -651,6 +716,17 @@ const fromFilePart = (}};+const toStoredApproval = (approval: UIToolApproval): NonNullable<Agents.ToolCall['approval']> => ({+ actionId: approval.id,+ allowed_decisions: [],+ ...(approval.requestReason && { description: approval.requestReason }),+ ...(approval.approved !== undefined && {+ decision: approval.approved ? ('approve' as const) : ('reject' as const),+ }),+ ...(approval.approved !== undefined &&+ approval.reason !== undefined && { reason: approval.reason }),+});+const fromToolPart = (part: UIToolPart): TMessageContentParts => {const stored = part.callProviderMetadata?.librechat;if (stored) {@@ -685,14 +761,10 @@ const fromToolPart = (part: UIToolPart): TMessageContentParts => {}),...(output !== undefined && { output }),...(part.state === 'output-error' && { runStepStatus: 'failed' as const }),- ...(part.state === 'output-available' && { runStepStatus: 'completed' as const }),- ...(part.approval && {- approval: {- actionId: part.approval.id,- allowed_decisions: [],- ...(part.approval.requestReason && { description: part.approval.requestReason }),- },+ ...((part.state === 'output-available' || part.state === 'output-denied') && {+ runStepStatus: 'completed' as const,}),+ ...(part.approval && { approval: toStoredApproval(part.approval) }),},};};@@ -728,6 +800,9 @@ export function fromUIPart(part: UIMessagePart): TMessageContentParts | undefinecase 'source-url':return undefined;}+ if (part.type === 'data-ask-user-question') {+ return { type: ASK_USER_QUESTION, ...part.data } as unknown as TMessageContentParts;+ }if (isUIDataPart(part)) {return { type: contentTypesByDataPart[part.type], ...part.data } as TMessageContentParts;}diff --git a/packages/data-provider/src/types/agents.ts b/packages/data-provider/src/types/agents.tsindex f6ab352536..de003cb03e 100644--- a/packages/data-provider/src/types/agents.ts+++ b/packages/data-provider/src/types/agents.ts@@ -157,6 +157,10 @@ export namespace Agents {remember_unavailable?: 'connection' | 'disabled' | 'storage' | 'background';/** Server-authored: an `approve` may carry `scope: 'session'` for this call. */allow_always?: boolean;+ /** The reviewer's decision once submitted; absent while the call still awaits review. */+ decision?: ToolApprovalDecisionType;+ /** The reason the reviewer gave with the decision. */+ reason?: string;};};diff --git a/packages/data-provider/src/types/transport.ts b/packages/data-provider/src/types/transport.tsindex c9d3eef0bb..1f14fba902 100644--- a/packages/data-provider/src/types/transport.ts+++ b/packages/data-provider/src/types/transport.ts@@ -203,7 +203,11 @@ export type ChatEvent =| { type: 'context_usage'; data: TContextUsageEvent }/** AI SDK: `message-metadata`. */| { type: 'token_usage'; data: TTokenUsageEvent }- /** AI SDK: `tool-approval-request`. */+ /**+ * AI SDK: `tool-approval-request` for a `tool_approval` pause; an `ask_user_question` pause+ * becomes a `data-ask-user-question` part. The decision travels on the resume request, so+ * `approval-responded` and `output-denied` have no frame of their own.+ */| { type: 'pending_action'; data: Agents.PendingAction }/** No AI SDK equivalent: steering. */| { type: 'steer_applied'; data: TSteerAppliedEvent }