@adminide-stack/yantra-mobile 12.0.28-alpha.7 → 12.0.28-alpha.71
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/lib/api/stt.js +54 -0
- package/lib/api/stt.js.map +1 -0
- package/lib/assets/icon.png +0 -0
- package/lib/components/CustomDrawer.js +479 -0
- package/lib/components/CustomDrawer.js.map +1 -0
- package/lib/components/GatewayConnector/GatewayConnector.js +18 -0
- package/lib/components/GatewayConnector/GatewayConnector.js.map +1 -0
- package/lib/components/GatewayToolbarButtonMobile.js +84 -0
- package/lib/components/GatewayToolbarButtonMobile.js.map +1 -0
- package/lib/components/NavigationHeader/NavigationHeader.js +214 -0
- package/lib/components/NavigationHeader/NavigationHeader.js.map +1 -0
- package/lib/components/ThinkingIndicator.js +55 -0
- package/lib/components/ThinkingIndicator.js.map +1 -0
- package/lib/components/YantraBrandLoader.js +94 -0
- package/lib/components/YantraBrandLoader.js.map +1 -0
- package/lib/compute.js +114 -5
- package/lib/compute.js.map +1 -1
- package/lib/config/constants.js +16 -0
- package/lib/config/constants.js.map +1 -0
- package/lib/config/env-config.js +74 -19
- package/lib/config/env-config.js.map +1 -1
- package/lib/contexts/CdecliConnectionContext.js +47 -0
- package/lib/contexts/CdecliConnectionContext.js.map +1 -0
- package/lib/contexts/GatewayContext.js +77 -0
- package/lib/contexts/GatewayContext.js.map +1 -0
- package/lib/features/audio-input/AudioRecorderPanel.js +231 -0
- package/lib/features/audio-input/AudioRecorderPanel.js.map +1 -0
- package/lib/features/audio-input/MicErrorBoundary.js +34 -0
- package/lib/features/audio-input/MicErrorBoundary.js.map +1 -0
- package/lib/graphql/agentGatewayDocuments.js +53 -0
- package/lib/graphql/agentGatewayDocuments.js.map +1 -0
- package/lib/hooks/useCdecliAutoConnect.js +242 -0
- package/lib/hooks/useCdecliAutoConnect.js.map +1 -0
- package/lib/hooks/useCdecliChannel.js +226 -0
- package/lib/hooks/useCdecliChannel.js.map +1 -0
- package/lib/hooks/useChatApi.js +338 -171
- package/lib/hooks/useChatApi.js.map +1 -1
- package/lib/hooks/useChatStream.js +281 -64
- package/lib/hooks/useChatStream.js.map +1 -1
- package/lib/hooks/useGatewayConnection.js +123 -0
- package/lib/hooks/useGatewayConnection.js.map +1 -0
- package/lib/hooks/useGatewayRegistry.js +28 -0
- package/lib/hooks/useGatewayRegistry.js.map +1 -0
- package/lib/hooks/usePrerequisiteIds.js +181 -0
- package/lib/hooks/usePrerequisiteIds.js.map +1 -0
- package/lib/hooks/useWorkspaceProvisioner.js +236 -0
- package/lib/hooks/useWorkspaceProvisioner.js.map +1 -0
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/routes.json +120 -5
- package/lib/screens/Chat/index.js +423 -0
- package/lib/screens/Chat/index.js.map +1 -0
- package/lib/screens/ChatHistory/index.js +56 -0
- package/lib/screens/ChatHistory/index.js.map +1 -0
- package/lib/screens/Home/HomeScreen.js +413 -140
- package/lib/screens/Home/HomeScreen.js.map +1 -1
- package/lib/screens/Home/components/ChatHistoryLanding.js +487 -0
- package/lib/screens/Home/components/ChatHistoryLanding.js.map +1 -0
- package/lib/screens/Home/components/DeepSearchModal.js +349 -0
- package/lib/screens/Home/components/DeepSearchModal.js.map +1 -0
- package/lib/screens/Home/deepSearchUtils.js +41 -0
- package/lib/screens/Home/deepSearchUtils.js.map +1 -0
- package/lib/screens/NewChat/index.js +79 -0
- package/lib/screens/NewChat/index.js.map +1 -0
- package/lib/services/agentSessionManager.js +451 -0
- package/lib/services/agentSessionManager.js.map +1 -0
- package/lib/services/gatewayApiKeyBridge.js +4 -0
- package/lib/services/gatewayApiKeyBridge.js.map +1 -0
- package/lib/services/gatewayClient.js +470 -0
- package/lib/services/gatewayClient.js.map +1 -0
- package/lib/theme/mobileTokens.js +18 -0
- package/lib/theme/mobileTokens.js.map +1 -0
- package/lib/utils/gatewaySelectionStorage.js +21 -0
- package/lib/utils/gatewaySelectionStorage.js.map +1 -0
- package/package.json +7 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useChatApi.js","sources":["../../src/hooks/useChatApi.ts"],"sourcesContent":["import type { ApolloCache } from '@apollo/client';\nimport {\n GetAccountChatMessagesDocument,\n GetAccountChatSessionsDocument,\n useCreateAccountChatSessionMutation,\n useGetAccountChatMessagesQuery,\n useGetAccountChatSessionsQuery,\n useSaveAccountChatMessagesMutation,\n} from 'common/graphql';\nimport type { IAccountCreateChatSessionInput, IAccountSaveChatMessagesInput } from 'common/server';\nimport { useCallback, useMemo } from 'react';\n\nconst SESSIONS_QUERY_VARS = { first: 25, includeArchived: false };\n\nfunction readSessionsCache(cache: ApolloCache<unknown>) {\n try {\n return cache.readQuery({\n query: GetAccountChatSessionsDocument,\n variables: SESSIONS_QUERY_VARS,\n }) as { getAccountChatSessions?: { edges: unknown[]; totalCount?: number } } | null;\n } catch {\n return null;\n }\n}\n\nfunction readMessagesCache(cache: ApolloCache<unknown>, sessionId: string) {\n try {\n return cache.readQuery({\n query: GetAccountChatMessagesDocument,\n variables: { sessionId, first: 100 },\n }) as { getAccountChatMessages?: { edges: unknown[]; totalCount?: number } } | null;\n } catch {\n return null;\n }\n}\n\nfunction buildSessionEdge(node: Record<string, unknown>) {\n return {\n __typename: 'AccountChatSessionEdge',\n cursor: '',\n node: {\n __typename: 'AccountChatSession',\n ...node,\n },\n };\n}\n\nfunction buildMessageEdge(node: Record<string, unknown>) {\n return {\n __typename: 'AccountChatMessageEdge',\n cursor: '',\n node: {\n __typename: 'AccountChatMessage',\n ...node,\n },\n };\n}\n\nexport interface ChatSessionUI {\n id: string;\n title: string;\n createdAt: Date;\n updatedAt: Date;\n}\n\nexport function useChatSessions(options?: { includeArchived?: boolean; skip?: boolean }) {\n const { data, loading, error, refetch } = useGetAccountChatSessionsQuery({\n variables: {\n first: 25,\n includeArchived: options?.includeArchived ?? false,\n },\n skip: options?.skip,\n fetchPolicy: 'cache-first',\n });\n\n const sessions: ChatSessionUI[] = useMemo(() => {\n if (!data?.getAccountChatSessions?.edges) return [];\n return data.getAccountChatSessions.edges.map((edge: { node: Record<string, unknown> }) => ({\n id: edge.node.id as string,\n title: (edge.node.title as string) ?? 'New Chat',\n createdAt: new Date(edge.node.createdAt as string),\n updatedAt: new Date(edge.node.updatedAt as string),\n }));\n }, [data]);\n\n return { sessions, loading, error, refetch };\n}\n\nexport interface ChatMessageUI {\n id: string;\n sessionId: string;\n role: 'user' | 'assistant' | 'system';\n content: string;\n attachments?: Array<{ id: string; name: string; type: string; mimeType?: string; size?: number; url?: string }>;\n tokenCount?: number;\n model?: string;\n createdAt: Date;\n updatedAt: Date;\n}\n\nexport function useChatMessages(sessionId: string | null, options?: { skip?: boolean }) {\n const { data, loading, error, refetch } = useGetAccountChatMessagesQuery({\n variables: { sessionId: sessionId!, first: 100 },\n skip: !sessionId || options?.skip,\n fetchPolicy: 'cache-first',\n });\n\n const messages: ChatMessageUI[] = useMemo(() => {\n if (!data?.getAccountChatMessages?.edges) return [];\n return data.getAccountChatMessages.edges.map((edge: { node: Record<string, unknown> }) => {\n const n = edge.node;\n return {\n id: n.id as string,\n sessionId: n.sessionId as string,\n role: n.role as 'user' | 'assistant' | 'system',\n content: (n.content as string) ?? '',\n attachments: (n.attachments as ChatMessageUI['attachments']) ?? undefined,\n tokenCount: n.tokenCount as number | undefined,\n model: n.model as string | undefined,\n createdAt: new Date(n.createdAt as string),\n updatedAt: new Date(n.updatedAt as string),\n };\n });\n }, [data]);\n\n return { messages, loading, error, refetch };\n}\n\nexport function useChatMutations() {\n const [createSessionMutation, { loading: createLoading }] = useCreateAccountChatSessionMutation();\n const [saveMessagesMutation, { loading: saveMessagesLoading }] = useSaveAccountChatMessagesMutation();\n\n const createSession = useCallback(\n async (input?: IAccountCreateChatSessionInput): Promise<ChatSessionUI> => {\n const inputWithId = {\n id: input?.id,\n title: input?.title ?? 'New Chat',\n ...(input?.model && { model: input.model }),\n ...(input?.systemPrompt && { systemPrompt: input.systemPrompt }),\n };\n\n const { data } = await createSessionMutation({\n variables: { input: inputWithId },\n update: (cache, { data: result }) => {\n if (!result?.createAccountChatSession) return;\n try {\n const existing = readSessionsCache(cache);\n if (existing?.getAccountChatSessions) {\n const newEdge = buildSessionEdge(result.createAccountChatSession);\n cache.writeQuery({\n query: GetAccountChatSessionsDocument,\n variables: SESSIONS_QUERY_VARS,\n data: {\n getAccountChatSessions: {\n ...existing.getAccountChatSessions,\n edges: [newEdge, ...existing.getAccountChatSessions.edges],\n totalCount: (existing.getAccountChatSessions.totalCount ?? 0) + 1,\n },\n },\n });\n }\n } catch (err) {\n console.warn('[useChatApi] createSession cache update failed:', err);\n }\n },\n });\n\n if (!data?.createAccountChatSession) {\n throw new Error('Failed to create session');\n }\n\n const s = data.createAccountChatSession;\n return {\n id: s.id,\n title: s.title ?? 'New Chat',\n createdAt: new Date(s.createdAt),\n updatedAt: new Date(s.updatedAt),\n };\n },\n [createSessionMutation],\n );\n\n const saveMessages = useCallback(\n async (input: IAccountSaveChatMessagesInput): Promise<void> => {\n const { data } = await saveMessagesMutation({\n variables: { input },\n update: (cache, { data: result }) => {\n if (!result?.saveAccountChatMessages) return;\n const res = result.saveAccountChatMessages;\n try {\n const existingMessages = readMessagesCache(cache, input.sessionId);\n if (existingMessages?.getAccountChatMessages) {\n const userEdge = buildMessageEdge(res.userMessage);\n const assistantEdge = buildMessageEdge(res.assistantMessage);\n cache.writeQuery({\n query: GetAccountChatMessagesDocument,\n variables: { sessionId: input.sessionId, first: 100 },\n data: {\n getAccountChatMessages: {\n ...existingMessages.getAccountChatMessages,\n edges: [\n ...existingMessages.getAccountChatMessages.edges,\n userEdge,\n assistantEdge,\n ],\n totalCount: (existingMessages.getAccountChatMessages.totalCount ?? 0) + 2,\n },\n },\n });\n }\n } catch (err) {\n console.warn('[useChatApi] saveMessages cache update failed:', err);\n }\n try {\n const existingSessions = readSessionsCache(cache);\n if (existingSessions?.getAccountChatSessions) {\n const sessionData = res.session;\n const updatedEdges = existingSessions.getAccountChatSessions.edges.map((edge: any) =>\n edge.node.id === sessionData.id\n ? { ...edge, node: { ...edge.node, ...sessionData } }\n : edge,\n );\n cache.writeQuery({\n query: GetAccountChatSessionsDocument,\n variables: SESSIONS_QUERY_VARS,\n data: {\n getAccountChatSessions: {\n ...existingSessions.getAccountChatSessions,\n edges: updatedEdges,\n },\n },\n });\n }\n } catch (err) {\n console.warn('[useChatApi] saveMessages session cache update failed:', err);\n }\n },\n });\n if (!data?.saveAccountChatMessages) {\n throw new Error('Failed to save messages');\n }\n },\n [saveMessagesMutation],\n );\n\n return {\n createSession,\n saveMessages,\n loading: { create: createLoading, saveMessages: saveMessagesLoading },\n };\n}\n"],"names":["_a"],"mappings":";;;;;;;;;;;;;;;;;;;AAIA,MAAM,mBAAsB,GAAA;AAAA,EAC1B,KAAO,EAAA,EAAA;AAAA,EACP,eAAiB,EAAA;AACnB,CAAA;AACA,SAAS,kBAAkB,KAA6B,EAAA;AACtD,EAAI,IAAA;AACF,IAAA,OAAO,MAAM,SAAU,CAAA;AAAA,MACrB,KAAO,EAAA,8BAAA;AAAA,MACP,SAAW,EAAA;AAAA,KACZ,CAAA;AAAA,GAMK,CAAA,OAAA,CAAA,EAAA;AACN,IAAO,OAAA,IAAA;AAAA;AAEX;AACA,SAAS,iBAAA,CAAkB,OAA6B,SAAmB,EAAA;AACzE,EAAI,IAAA;AACF,IAAA,OAAO,MAAM,SAAU,CAAA;AAAA,MACrB,KAAO,EAAA,8BAAA;AAAA,MACP,SAAW,EAAA;AAAA,QACT,SAAA;AAAA,QACA,KAAO,EAAA;AAAA;AACT,KACD,CAAA;AAAA,GAMK,CAAA,OAAA,CAAA,EAAA;AACN,IAAO,OAAA,IAAA;AAAA;AAEX;AACA,SAAS,iBAAiB,IAA+B,EAAA;AACvD,EAAO,OAAA;AAAA,IACL,UAAY,EAAA,wBAAA;AAAA,IACZ,MAAQ,EAAA,EAAA;AAAA,IACR,IAAM,EAAA,cAAA,CAAA;AAAA,MACJ,UAAY,EA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,EAAO,OAAA;AAAA,IACL,aAAA;AAAA,IACA,YAAA;AAAA,IACA,OAAS,EAAA;AAAA,MACP,MAAQ,EAAA,aAAA;AAAA,MACR,YAAc,EAAA;AAAA;AAChB,GACF;AACF"}
|
|
1
|
+
{"version":3,"file":"useChatApi.js","sources":["../../src/hooks/useChatApi.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { AiAgentMessageRole, PostTypeEnum, RoomType, SortEnum } from 'common';\nimport {\n GetChannelsByUserWithLastMessageDocument,\n useAddChannelMutation,\n useGetChannelsByUserWithLastMessageQuery,\n useMessagesQuery,\n useSendMessagesMutation,\n} from 'common/graphql';\nimport type {\n IAccountCreateChatSessionInput,\n IAccountSaveChatMessagesInput as IAccountSaveChatMessagesInputBase,\n} from 'common/server';\nimport { useCallback, useMemo } from 'react';\nimport { v4 as uuidv4 } from 'uuid';\n\nconst MESSAGES_PAGE_LIMIT = 100;\n\n/** Same variables as web Inbox / AIAgent: list AI assistant channels for the current user. */\nexport const AI_ASSISTANT_CHANNELS_QUERY_VARS = {\n criteria: { type: RoomType.Aiassistant },\n limit: 100,\n skip: 0,\n sort: { key: 'updatedAt', value: SortEnum.Desc },\n};\n\nexport const HISTORY_PAGE_SIZE = 20;\n\n/**\n * Variables for the channels-based chat history list (`ChatHistoryLanding`).\n *\n * IMPORTANT: pass the user's org `slug` (e.g. `tarun-upadhyay-qraw`) so the\n * `channelsByUser` resolver's `$in: [criteria.orgName, orgId]` includes\n * channels stored under the slug. When `criteria.orgName` is omitted, the\n * `$in` collapses to just `[orgId]` and channels saved with the slug as\n * `orgName` (the mobile create path) never match. That's the cause of the\n * silently-empty history screen.\n *\n * Kept as a builder (not a constant) because the slug varies per user and\n * because Apollo treats different `variables` as different cached entries —\n * if the create mutation refetches a no-orgName variant, the orgName-variant\n * the screen watches stays stale.\n */\nexport function getHistoryChannelsQueryVariables(orgName?: string | null, options?: { skip?: number; limit?: number }) {\n const trimmedOrg = typeof orgName === 'string' && orgName.trim().length > 0 ? orgName.trim() : undefined;\n return {\n criteria: {\n type: RoomType.Aiassistant,\n ...(trimmedOrg ? { orgName: trimmedOrg } : {}),\n },\n limit: options?.limit ?? HISTORY_PAGE_SIZE,\n skip: options?.skip ?? 0,\n sort: { key: 'updatedAt' as const, value: SortEnum.Desc },\n };\n}\n\n/** @deprecated Use {@link getHistoryChannelsQueryVariables} so `orgName` is included. */\nexport const HISTORY_QUERY_BASE = getHistoryChannelsQueryVariables();\n\n/**\n * Variables for the chat-history feed on the mobile `ChatHistory` screen.\n *\n * First page uses `HISTORY_PAGE_SIZE` (20) posts; \"Load older\" paginates by post\n * `skip` in increments of 20. Rows are still one per channel (collapsed from posts).\n *\n * This query intentionally hits `messages` (not `channelsByUser`) — the same\n * pattern the web sidebar uses (`useNewChatPageHistorySessions`). Reasons:\n * 1. `channelsByUser` is server-side gated by an injected `orgName` /\n * `members` filter that can mismatch when channels were stored with an\n * org slug while the resolver injects the org id. The `messages`\n * resolver filters Post directly by `editedBy: accountId`, so the\n * user's own AI-assistant posts always show up.\n * 2. The user-authored post IS the conversation's natural title — no need\n * to walk `lastMessage.propsConfiguration.contents.parent` to recover\n * the original prompt.\n *\n * - `type: AIASSISTANT` scopes to AI chat (not direct/service rooms).\n * - `props.role: 'user'` selects only the user's turns. The assistant reply\n * is ignored here; it would override the title with the agent's words.\n * - `editedBy: accountUserId` scopes to the current user's own posts,\n * independent of channel membership / org name.\n */\nexport function getChatHistoryMessagesQueryVariables(\n accountUserId: string,\n options?: { skip?: number; limit?: number },\n) {\n return {\n limit: options?.limit ?? HISTORY_PAGE_SIZE,\n skip: options?.skip ?? 0,\n sort: { key: 'updatedAt' as const, value: SortEnum.Desc },\n type: PostTypeEnum.Aiassistant,\n editedBy: accountUserId,\n props: { role: 'user' },\n };\n}\n\nexport type ChatHistoryMode = 'chat' | 'deep-search';\n\nexport interface ChatHistorySession {\n /** Channel id — used to navigate to the Chat screen. */\n channelId: string;\n /** User's most recent prompt in this channel (used as the row title). */\n title: string;\n /** Second line: latest post `message` field (same as Manus subtitle). */\n preview: string;\n /** Conversation mode (chat / deep-search), inferred from channel display name / title. */\n mode: ChatHistoryMode;\n /** Most-recent activity timestamp (sort key + bucket key for Today/Yesterday/…). */\n updatedAt: Date;\n createdAt: Date;\n /**\n * True when no concrete prompt was recoverable (no parent, no user turn, no\n * meaningful channel.title). UI renders the title in a quieter style.\n */\n isPlaceholder: boolean;\n}\n\n/**\n * Collapse a list of user-authored AI assistant posts (newest first) into one\n * row per channel. First post per `channel.id` wins the title, which — because\n * the query is sorted by `updatedAt DESC` — is the user's latest prompt in\n * that channel. Posts whose `channel.id` is missing (unexpected, but possible\n * if the resolver omits the relation) are skipped.\n */\nexport function chatHistorySessionsFromMessages(\n data: { messages?: { data?: unknown[] | null } | null } | null | undefined,\n): ChatHistorySession[] {\n const rows = data?.messages?.data;\n if (!rows?.length) return [];\n\n const seen = new Set<string>();\n const sessions: ChatHistorySession[] = [];\n\n for (const post of rows as any[]) {\n const channelId = post?.channel?.id ? String(post.channel.id) : '';\n if (channelId && !seen.has(channelId)) {\n seen.add(channelId);\n\n const rawTitle = String(post?.message ?? '')\n .replace(/\\s+/g, ' ')\n .trim();\n const updatedAt = post?.updatedAt ? new Date(post.updatedAt) : new Date();\n const createdAt = post?.createdAt ? new Date(post.createdAt) : updatedAt;\n const title = rawTitle || 'New chat';\n\n sessions.push({\n channelId,\n title,\n preview: '',\n mode: 'chat',\n updatedAt,\n createdAt,\n isPlaceholder: !rawTitle,\n });\n }\n }\n\n return sessions;\n}\n\n/**\n * Mobile parity hook for the web sidebar's `useNewChatPageHistorySessions`.\n *\n * Returns one row per channel where the user has authored at least one AI assistant\n * post, with the title taken from that user's latest prompt. Skipped when\n * `accountUserId` isn't known yet (we don't want to fire a query without it).\n */\nexport function useChatHistorySessionsFromMessages(\n accountUserId: string | null | undefined,\n options?: { skip?: boolean },\n) {\n const skip = options?.skip || !accountUserId;\n const { data, loading, error, refetch } = useMessagesQuery({\n variables: accountUserId ? getChatHistoryMessagesQueryVariables(accountUserId) : undefined,\n skip,\n fetchPolicy: 'cache-and-network',\n notifyOnNetworkStatusChange: true,\n context: { cacheKey: 'new-chat-history-messages' },\n });\n\n const sessions = useMemo(() => chatHistorySessionsFromMessages(data), [data]);\n const sourcePostCount = data?.messages?.data?.length ?? 0;\n\n return { sessions, loading, error, refetch, sourcePostCount };\n}\n\n/* -------------------------------------------------------------------------- */\n/* Channels-based chat history (parent + lastMessage) */\n/* -------------------------------------------------------------------------- */\n\ninterface ChannelLastMessageLike {\n message?: string | null;\n createdAt?: string | null;\n updatedAt?: string | null;\n props?: { role?: string | null } | null;\n propsConfiguration?: {\n contents?: {\n role?: string | null;\n parent?: {\n message?: string | null;\n props?: { role?: string | null } | null;\n } | null;\n } | null;\n content?: { role?: string | null } | null;\n } | null;\n}\n\ninterface ChannelLike {\n id?: string | null;\n type?: string | null;\n displayName?: string | null;\n title?: string | null;\n createdAt?: string | null;\n updatedAt?: string | null;\n lastMessage?: ChannelLastMessageLike | null;\n}\n\nfunction getLastMessageRole(lastMessage: ChannelLastMessageLike | null | undefined): string {\n return String(\n lastMessage?.propsConfiguration?.contents?.role ??\n lastMessage?.propsConfiguration?.content?.role ??\n lastMessage?.props?.role ??\n '',\n ).toUpperCase();\n}\n\nfunction getParentUserPrompt(lastMessage: ChannelLastMessageLike | null | undefined): string {\n const parent = lastMessage?.propsConfiguration?.contents?.parent;\n if (!parent) return '';\n return String(parent?.message ?? '').trim();\n}\n\nfunction detectMode(channel: ChannelLike): ChatHistoryMode {\n const display = String(channel?.displayName ?? '').toLowerCase();\n const title = String(channel?.title ?? '').toLowerCase();\n if (display.startsWith('deep-search') || title.startsWith('deep search') || title.startsWith('deep-search')) {\n return 'deep-search';\n }\n return 'chat';\n}\n\n/**\n * Drop the internal slug we use as `channel.title` for server-side mode detection\n * (`chat-channel-<uuid>` / `deep-search-<uuid>`) so it doesn't leak into the row\n * label. The server sometimes also reformats this slug into `displayName` as\n * title-cased text with spaces (e.g. `\"Deep Search b64be4f7 9fe6 …\"`) — those\n * are equally machine-generated and must NOT be shown to the user.\n *\n * Pattern accepts:\n * • `deep-search-<uuid>` / `chat-channel-<uuid>` (raw, mode signal)\n * • `Deep Search <uuid>` / `Chat Channel <uuid>` (display-cased, server-reformatted)\n * • UUID may use hyphens, spaces, or be partially truncated as long as the\n * trailing chunk is hex.\n */\nfunction cleanChannelTitle(rawTitle: string | null | undefined): string {\n const t = String(rawTitle ?? '').trim();\n if (!t) return '';\n if (/^(chat[\\s-]channel|deep[\\s-]search)[\\s-][0-9a-f][0-9a-f\\s-]{7,}\\.?$/i.test(t)) return '';\n return t;\n}\n\n/**\n * Map one channel into a session row (Manus-style two-line list):\n *\n * **Title (line 1):** `lastMessage.propsConfiguration.contents.parent.message`\n * when present (the user turn the thread is anchored on). Otherwise fall back\n * to the last post body if that post is still a user message, then a\n * machine-slug-stripped channel title, then a machine-slug-stripped\n * `displayName`, then `\"Thinking…\"`. Machine slugs (`deep-search-<uuid>`,\n * `chat-channel-<uuid>`, and their server-reformatted display variants)\n * are filtered via `cleanChannelTitle`, so they never reach the row.\n *\n * **Preview (line 2):** `lastMessage.message` as returned by the API (trimmed,\n * whitespace collapsed). Agent errors that start with `⚠` stay visible in the\n * subtitle, matching the reference UI.\n *\n * If preview text equals the title after trim, the second line is omitted\n * (single-line channel, no duplicate).\n */\nexport function buildSessionFromChannel(channel: ChannelLike): ChatHistorySession | null {\n const channelId = String(channel?.id ?? '');\n if (!channelId) return null;\n\n const lastMessage = channel?.lastMessage ?? null;\n const lastMessageRole = getLastMessageRole(lastMessage);\n const parentUserPrompt = getParentUserPrompt(lastMessage);\n const parentTrimmed = parentUserPrompt.trim();\n\n const userTurn = lastMessageRole === 'USER' && lastMessage?.message ? String(lastMessage.message).trim() : '';\n const cleanedChannelTitle = cleanChannelTitle(channel?.title);\n /**\n * Apply the same machine-slug cleaner to `displayName`. The server\n * sometimes derives displayName from the title (`\"deep-search-<uuid>\"`\n * → `\"Deep Search <uuid>\"`), so without this the slug bypasses\n * `cleanedChannelTitle` and lands directly in the row label.\n */\n const cleanedDisplayName = cleanChannelTitle(channel?.displayName);\n\n let resolvedTitle = parentTrimmed || userTurn || cleanedChannelTitle || cleanedDisplayName;\n const isPlaceholder = !resolvedTitle;\n /**\n * When every signal fails — typical when a send just landed and the\n * server hasn't yet persisted the message that would populate\n * `propsConfiguration.contents.parent.message` — show `\"Thinking…\"`\n * instead of an empty label or the raw machine slug. The row's mode\n * tile still signals chat vs deep-search, so the user has enough\n * context to recognize the entry without leaking implementation noise.\n */\n if (isPlaceholder) resolvedTitle = 'Thinking…';\n\n const rawPreview = String(lastMessage?.message ?? '')\n .replace(/\\s+/g, ' ')\n .trim();\n let preview = rawPreview;\n if (preview && preview === parentTrimmed) preview = '';\n\n const sortAt = (() => {\n const candidates = [channel?.updatedAt, lastMessage?.updatedAt, lastMessage?.createdAt, channel?.createdAt];\n for (const candidate of candidates) {\n if (candidate) {\n const t = new Date(candidate).getTime();\n if (Number.isFinite(t) && t > 0) return t;\n }\n }\n return Date.now();\n })();\n\n const createdAtRaw = channel?.createdAt ? new Date(channel.createdAt).getTime() : sortAt;\n\n return {\n channelId,\n title: resolvedTitle,\n preview,\n mode: detectMode(channel),\n updatedAt: new Date(sortAt),\n createdAt: new Date(Number.isFinite(createdAtRaw) ? createdAtRaw : sortAt),\n isPlaceholder,\n };\n}\n\n/** Collapse a `channelsByUser` response into session rows for the history list. */\nexport function chatHistorySessionsFromChannels(\n data: { channelsByUser?: (ChannelLike | null)[] | null } | null | undefined,\n): ChatHistorySession[] {\n const channels = (data?.channelsByUser ?? []) as (ChannelLike | null)[];\n return channels\n .filter((c): c is ChannelLike => Boolean(c?.id) && (!c?.type || c.type === RoomType.Aiassistant))\n .map((c) => buildSessionFromChannel(c))\n .filter((row): row is ChatHistorySession => row !== null)\n .sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());\n}\n\n/**\n * Chat history feed driven by `channelsByUser`. Each row: parent prompt as title,\n * `lastMessage.message` as subtitle (Manus-style), when the API provides them.\n *\n * Caller MUST pass `orgName` (the org slug). Without it, the resolver only\n * matches channels whose `orgName` field equals the user's `orgId`, which is\n * never the case for mobile-created channels (those store the slug). The hook\n * suspends the query (`skip: true`) until `orgName` is known, so we never\n * fire a guaranteed-empty request.\n *\n * Pagination is by channel count (one channel = one row in this query),\n * unlike the messages-based path which paginates by raw post rows.\n */\nexport function useChatHistorySessionsFromChannels(orgName: string | null | undefined, options?: { skip?: boolean }) {\n const skip = options?.skip || !orgName;\n const variables = useMemo(() => getHistoryChannelsQueryVariables(orgName), [orgName]);\n\n const { data, loading, error, refetch } = useGetChannelsByUserWithLastMessageQuery({\n variables,\n skip,\n fetchPolicy: 'cache-and-network',\n notifyOnNetworkStatusChange: true,\n context: { cacheKey: 'chat-history-channels-list' },\n });\n\n const sessions = useMemo(() => chatHistorySessionsFromChannels(data), [data]);\n const sourceChannelCount = data?.channelsByUser?.length ?? 0;\n /**\n * `sessionsLoaded` means \"we have data we can show\", regardless of whether a\n * background revalidation is still in flight. Same contract as `messagesLoaded`\n * in `useChatMessages` — lets the screen show the cached list (or empty state)\n * immediately on revisit instead of flashing a loader for the network round-trip.\n */\n const sessionsLoaded = data !== undefined;\n\n return { sessions, loading, sessionsLoaded, error, refetch, sourceChannelCount };\n}\n\nfunction stripModelCostHeader(content: string): string {\n const normalized = content.replace(/\\r\\n/g, '\\n');\n return normalized.replace(\n /^\\s*(?:[^\\w\\n]+\\s*)?[a-z0-9][a-z0-9._-]*\\s*\\(\\s*\\$[\\d.]+\\s*\\/\\s*MTok\\s+in\\s*\\)\\s*\\n+/i,\n '',\n );\n}\n\nfunction sanitizeAssistantContentByRole(role: unknown, content: string): string {\n return role === 'assistant' ? stripModelCostHeader(content) : content;\n}\n\nfunction omitContent<T extends Record<string, any>>(value: T | null | undefined): Omit<T, 'content'> {\n const clone = { ...(value ?? {}) } as Record<string, any>;\n delete clone.content;\n return clone as Omit<T, 'content'>;\n}\n\nexport interface ChatSessionUI {\n id: string;\n title: string;\n createdAt: Date;\n updatedAt: Date;\n messageCount?: number;\n}\n\nexport function useChatSessions(options?: { includeArchived?: boolean; skip?: boolean }) {\n const { data, loading, error, refetch } = useGetChannelsByUserWithLastMessageQuery({\n variables: AI_ASSISTANT_CHANNELS_QUERY_VARS,\n skip: options?.skip,\n fetchPolicy: 'cache-and-network',\n });\n\n const sessions: ChatSessionUI[] = useMemo(() => {\n const channels = data?.channelsByUser ?? [];\n return channels\n .filter((c): c is NonNullable<typeof c> => Boolean(c?.id))\n .map((channel) => ({\n id: channel.id,\n title: channel.title || channel.displayName || 'New Chat',\n createdAt: new Date(channel.createdAt ?? Date.now()),\n updatedAt: new Date(channel.updatedAt ?? Date.now()),\n messageCount: undefined,\n }));\n }, [data]);\n\n return { sessions, loading, error, refetch };\n}\n\nexport interface ChatMessageUI {\n id: string;\n sessionId: string;\n role: 'user' | 'assistant' | 'system';\n content: string;\n attachments?: Array<{ id: string; name: string; type: string; mimeType?: string; size?: number; url?: string }>;\n tokenCount?: number;\n model?: string;\n createdAt: Date;\n updatedAt: Date;\n}\n\ninterface MinimalFile {\n id?: string | null;\n name?: string | null;\n mimeType?: string | null;\n size?: number | null;\n url?: string | null;\n}\n\ninterface MinimalPost {\n id?: string | null;\n message?: string | null;\n createdAt?: string | null;\n updatedAt?: string | null;\n channel?: { id?: string | null } | null;\n files?: { data?: MinimalFile[] | null } | null;\n props?: { role?: string | null; tokenCount?: number | null; model?: string | null } | null;\n propsConfiguration?: {\n contents?: { role?: string | null } | null;\n content?: { role?: string | null } | null;\n } | null;\n}\n\nfunction mapPostToChatMessageUI(post: MinimalPost, fallbackChannelId: string): ChatMessageUI {\n const props = (post.props ?? {}) as Record<string, any>;\n const contents = post.propsConfiguration?.contents ?? {};\n const roleRaw = props.role ?? contents.role ?? post.propsConfiguration?.content?.role;\n let role: ChatMessageUI['role'] = 'assistant';\n if (roleRaw === 'user' || roleRaw === AiAgentMessageRole.User) role = 'user';\n else if (roleRaw === 'assistant' || roleRaw === AiAgentMessageRole.Assistant) role = 'assistant';\n else if (roleRaw === 'system') role = 'system';\n\n const rawContent = post?.message ?? '';\n const content = sanitizeAssistantContentByRole(role, rawContent);\n\n return {\n id: post?.id ?? uuidv4(),\n sessionId: post?.channel?.id ?? fallbackChannelId,\n role,\n content,\n attachments: (post?.files?.data ?? []).map((file) => ({\n id: file.id ?? uuidv4(),\n name: file.name ?? 'file',\n type: typeof file.mimeType === 'string' && file.mimeType.startsWith('image/') ? 'screenshot' : 'file',\n mimeType: file.mimeType ?? undefined,\n size: file.size ?? undefined,\n url: file.url ?? undefined,\n })),\n tokenCount: props.tokenCount ?? undefined,\n model: props.model ?? undefined,\n createdAt: post?.createdAt ? new Date(post.createdAt) : new Date(),\n updatedAt: post?.updatedAt ? new Date(post.updatedAt) : new Date(),\n };\n}\n\nexport function useChatMessages(sessionId: string | null, options?: { skip?: boolean }) {\n const { data, loading, error, refetch } = useMessagesQuery({\n variables: {\n channelId: sessionId ?? undefined,\n parentId: null,\n limit: MESSAGES_PAGE_LIMIT,\n skip: 0,\n },\n skip: !sessionId || options?.skip,\n fetchPolicy: 'cache-and-network',\n notifyOnNetworkStatusChange: true,\n /**\n * Cache key is per-channel so switching sessions doesn't read another channel's response.\n * Keeping this as a constant ('messages-list') used to cause cross-session bleed in the\n * custom Apollo link before reaching the normalized cache.\n */\n context: { cacheKey: sessionId ? `messages-list:${sessionId}` : 'messages-list' },\n });\n\n /**\n * `messagesLoaded` means \"we have data we can show\", regardless of whether a\n * background revalidation is in flight. With `cache-and-network`, Apollo\n * fills `data` from cache synchronously while `loading` is still true for\n * the network round-trip — gating hydration on `!loading` made every chat\n * revisit show a loader for the network latency even though the messages\n * were already cached. The chat stream re-hydrates whenever `data` changes,\n * so the fresh network response still swaps in silently afterward.\n */\n const messagesLoaded = data !== undefined;\n\n const messages: ChatMessageUI[] = useMemo(() => {\n if (!sessionId) return [];\n const rows = data?.messages?.data ?? [];\n return rows\n .map((post) => mapPostToChatMessageUI(post as MinimalPost, sessionId))\n .sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());\n }, [data, sessionId]);\n\n return { messages, loading, messagesLoaded, error, refetch };\n}\n\nexport type IAccountSaveChatMessagesInput = IAccountSaveChatMessagesInputBase & {\n mode?: 'plan' | 'build';\n createdBy?: string;\n};\n\nexport function useChatMutations() {\n const [addChannelMutation, { loading: createChannelLoading }] = useAddChannelMutation();\n const [sendMessagesMutation, { loading: sendMessagesLoading }] = useSendMessagesMutation();\n\n const createChannel = useCallback(\n async (input?: IAccountCreateChatSessionInput): Promise<ChatSessionUI> => {\n const {\n orgName: orgNameArg,\n projectId: projectIdArg,\n ...inputForSettings\n } = (input ?? {}) as IAccountCreateChatSessionInput & {\n orgName?: string | null;\n projectId?: string | null;\n };\n const orgName =\n typeof orgNameArg === 'string' && orgNameArg.trim().length > 0 ? orgNameArg.trim() : undefined;\n const projectId =\n typeof projectIdArg === 'string' && projectIdArg.trim().length > 0 ? projectIdArg.trim() : undefined;\n\n const settings = {\n title: input?.title ?? 'New Chat',\n description: input?.systemPrompt ?? null,\n isShared: false,\n sharedSlug: null,\n isArchived: false,\n isPinned: false,\n model: input?.model ?? null,\n systemPrompt: input?.systemPrompt ?? null,\n ...inputForSettings,\n };\n\n const { data } = await addChannelMutation({\n variables: {\n channelId: input?.id ?? null,\n name: input?.title ?? 'New Chat',\n description: input?.systemPrompt ?? null,\n type: RoomType.Aiassistant,\n settings,\n ...(orgName ? { orgName } : {}),\n ...(projectId ? { projectId } : {}),\n },\n /**\n * Refetch every cache slot the rest of the app is watching for\n * channel lists. Apollo keys cached results by `variables`, so each\n * shape we emit here must match a watched query exactly.\n *\n * Variants:\n * 1. Legacy limit:100 list (web parity, no `criteria.orgName`).\n * 2. Mobile history-screen variant WITH `criteria.orgName` so the\n * resolver's `$in: [slug, orgId]` matches mobile-created channels.\n * 3. Same variant WITHOUT orgName, in case the screen mounted before\n * org settings resolved.\n */\n refetchQueries: [\n { query: GetChannelsByUserWithLastMessageDocument, variables: AI_ASSISTANT_CHANNELS_QUERY_VARS },\n ...(orgName\n ? [\n {\n query: GetChannelsByUserWithLastMessageDocument,\n variables: getHistoryChannelsQueryVariables(orgName),\n },\n ]\n : []),\n { query: GetChannelsByUserWithLastMessageDocument, variables: HISTORY_QUERY_BASE },\n ],\n awaitRefetchQueries: true,\n });\n\n if (!data?.createChannel?.id) {\n throw new Error('Failed to create channel');\n }\n\n const channel = data.createChannel;\n const title = channel.title ?? input?.title ?? 'New Chat';\n const now = new Date();\n\n return {\n id: channel.id,\n title,\n createdAt: now,\n updatedAt: now,\n };\n },\n [addChannelMutation],\n );\n\n const saveMessages = useCallback(\n async (\n input: IAccountSaveChatMessagesInput,\n ): Promise<{\n userMessage: ChatMessageUI;\n assistantMessage: ChatMessageUI;\n session: ChatSessionUI;\n }> => {\n const sanitizedAssistantContent = stripModelCostHeader(input.assistantMessage.content ?? '');\n const assistantMessageWithoutContent = omitContent((input.assistantMessage as any) ?? {});\n const userResult = await sendMessagesMutation({\n variables: {\n channelId: input.sessionId,\n type: RoomType.Aiassistant,\n content: input.userMessage.content ?? '',\n ...(input.createdBy ? { createdBy: input.createdBy } : {}),\n extraProps: {\n type: PostTypeEnum.Aiassistant,\n role: 'user',\n attachments: input.userMessage.attachments ?? [],\n isActive: true,\n status: 'complete',\n ...(input.userMessage as any),\n ...(input.mode ? { mode: input.mode } : {}),\n },\n } as any,\n });\n /**\n * Link the assistant reply to its prompting user message via `postId` (the parentId\n * on the schema). Without this, channel.lastMessage.propsConfiguration.contents.parent\n * stays null, the chat history can't recover the original prompt, and rows fall back\n * to the assistant reply as a label.\n */\n const userPostId = userResult.data?.sendMessage?.id ?? null;\n const assistantResult = await sendMessagesMutation({\n variables: {\n channelId: input.sessionId,\n type: RoomType.Aiassistant,\n content: sanitizedAssistantContent,\n ...(userPostId ? { postId: userPostId } : {}),\n extraProps: {\n type: PostTypeEnum.Aiassistant,\n role: 'assistant',\n attachments: input.assistantMessage.attachments ?? [],\n tokenCount: input.assistantMessage.tokenCount,\n latencyMs: input.assistantMessage.latencyMs,\n model: input.assistantMessage.model,\n finishReason: (input.assistantMessage as any).finishReason ?? null,\n isActive: true,\n status: 'complete',\n ...(userPostId ? { parentId: userPostId } : {}),\n ...assistantMessageWithoutContent,\n ...(input.mode ? { mode: input.mode } : {}),\n },\n } as any,\n });\n\n const userPost = userResult.data?.sendMessage;\n const assistantPost = assistantResult.data?.sendMessage;\n if (!userPost || !assistantPost) {\n throw new Error('Failed to send messages');\n }\n\n const now = new Date();\n return {\n userMessage: mapPostToChatMessageUI(userPost as MinimalPost, input.sessionId),\n assistantMessage: mapPostToChatMessageUI(assistantPost as MinimalPost, input.sessionId),\n session: {\n id: input.sessionId,\n title: 'Chat',\n createdAt: now,\n updatedAt: now,\n },\n };\n },\n [sendMessagesMutation],\n );\n\n return {\n createChannel,\n createSession: createChannel,\n saveMessages,\n loading: { create: createChannelLoading, saveMessages: sendMessagesLoading },\n };\n}\n"],"names":["uuidv4","_a","_b","_c","_d","_e"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,MAAM,mBAAsB,GAAA,GAAA;AAGrB,MAAM,gCAAmC,GAAA;AAAA,EAC9C,QAAU,EAAA;AAAA,IACR,MAAM,QAAS,CAAA;AAAA,GACjB;AAAA,EACA,KAAO,EAAA,GAAA;AAAA,EACP,IAAM,EAAA,CAAA;AAAA,EACN,IAAM,EAAA;AAAA,IACJ,GAAK,EAAA,WAAA;AAAA,IACL,OAAO,QAAS,CAAA;AAAA;AAEpB;AACO,MAAM,iBAAoB,GAAA;AAiBjB,SAAA,gCAAA,CAAiC,SAAyB,OAGvE,EAAA;AAxCH,EAAA,IAAA,EAAA,EAAA,EAAA;AAyCE,EAAM,MAAA,UAAA,GAAa,OAAO,OAAA,KAAY,QAAY,IAAA,OAAA,CAAQ,IAAK,EAAA,CAAE,MAAS,GAAA,CAAA,GAAI,OAAQ,C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 +1,32 @@
|
|
|
1
|
-
import {useState,useRef,useEffect
|
|
1
|
+
import {useState,useRef,useCallback,useMemo,useEffect}from'react';import {useChatMutations,useChatMessages}from'./useChatApi.js';import {useCdecliChannel}from'./useCdecliChannel.js';import {usePrerequisiteIds}from'./usePrerequisiteIds.js';import {streamChatResponse}from'../api/chatApi.js';var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
const REVEAL_CHUNK_SIZE = 6;
|
|
2
21
|
const REVEAL_INTERVAL_MS = 20;
|
|
3
|
-
function
|
|
22
|
+
function isCdecliAgentFailureOutput(text) {
|
|
23
|
+
const t = text.trim();
|
|
24
|
+
if (!t) return false;
|
|
25
|
+
return /⚠\s*Error:/.test(t) || /LLM stream error/i.test(t) || /Incorrect API key/i.test(t) || /invalid_api_key/i.test(t) || /authentication_error/i.test(t);
|
|
26
|
+
}
|
|
27
|
+
function useChatStream(sessionId, routing = {
|
|
28
|
+
kind: "groq"
|
|
29
|
+
}) {
|
|
4
30
|
const [messages, setMessages] = useState([]);
|
|
5
31
|
const [response, setResponse] = useState("");
|
|
6
32
|
const [isLoading, setIsLoading] = useState(false);
|
|
@@ -9,68 +35,32 @@ function useChatStream(sessionId) {
|
|
|
9
35
|
const abortRef = useRef(null);
|
|
10
36
|
const isSendingRef = useRef(false);
|
|
11
37
|
const revealIntervalRef = useRef(null);
|
|
38
|
+
const [cdecliStreamOverrideId, setCdecliStreamOverrideId] = useState(void 0);
|
|
39
|
+
const userMsgForSaveRef = useRef(null);
|
|
40
|
+
const saveSessionIdRef = useRef(null);
|
|
41
|
+
const cdecliRoundHandledRef = useRef(false);
|
|
42
|
+
const cdecliFallbackConversationRef = useRef(null);
|
|
43
|
+
const routingRef = useRef(routing);
|
|
44
|
+
routingRef.current = routing;
|
|
12
45
|
const {
|
|
13
46
|
saveMessages
|
|
14
47
|
} = useChatMutations();
|
|
15
48
|
const {
|
|
16
|
-
|
|
49
|
+
accountUserId
|
|
50
|
+
} = usePrerequisiteIds();
|
|
51
|
+
const {
|
|
52
|
+
messages: backendMessages,
|
|
53
|
+
loading: backendMessagesLoading,
|
|
54
|
+
messagesLoaded: backendMessagesLoaded
|
|
17
55
|
} = useChatMessages(sessionId);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
clearInterval(revealIntervalRef.current);
|
|
21
|
-
revealIntervalRef.current = null;
|
|
22
|
-
}
|
|
23
|
-
}, []);
|
|
24
|
-
useEffect(() => {
|
|
25
|
-
if (isSendingRef.current) return;
|
|
26
|
-
if (!sessionId) {
|
|
27
|
-
setMessages([]);
|
|
28
|
-
setResponse("");
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
if (backendMessages && backendMessages.length > 0) {
|
|
32
|
-
const formatted = backendMessages.map((msg) => {
|
|
33
|
-
var _a;
|
|
34
|
-
return {
|
|
35
|
-
role: msg.role,
|
|
36
|
-
content: (_a = msg.content) != null ? _a : "",
|
|
37
|
-
metadata: msg.tokenCount ? {
|
|
38
|
-
tokenUsage: {
|
|
39
|
-
inputTokens: 0,
|
|
40
|
-
outputTokens: msg.tokenCount,
|
|
41
|
-
totalTokens: msg.tokenCount
|
|
42
|
-
},
|
|
43
|
-
model: msg.model
|
|
44
|
-
} : void 0
|
|
45
|
-
};
|
|
46
|
-
});
|
|
47
|
-
setMessages(formatted);
|
|
48
|
-
setResponse("");
|
|
49
|
-
}
|
|
50
|
-
}, [sessionId, backendMessages]);
|
|
51
|
-
const sendMessage = useCallback(async (content, attachments, sessionIdOverride) => {
|
|
56
|
+
const lastHydratedSessionIdRef = useRef(null);
|
|
57
|
+
const executeGroqStream = useCallback(async (conv, effectiveSessionId, userMessage) => {
|
|
52
58
|
var _a, _b;
|
|
53
|
-
if (!content.trim()) return;
|
|
54
|
-
const effectiveSessionId = sessionIdOverride !== void 0 ? sessionIdOverride : sessionId;
|
|
55
|
-
const userMessage = {
|
|
56
|
-
role: "user",
|
|
57
|
-
content: content.trim(),
|
|
58
|
-
attachments
|
|
59
|
-
};
|
|
60
|
-
isSendingRef.current = true;
|
|
61
|
-
setMessages((prev) => [...prev, userMessage]);
|
|
62
|
-
setResponse("");
|
|
63
|
-
setIsLoading(true);
|
|
64
|
-
setIsStreaming(true);
|
|
65
|
-
setError(null);
|
|
66
59
|
abortRef.current = new AbortController();
|
|
67
|
-
const chatMessages =
|
|
60
|
+
const chatMessages = conv.map((m) => ({
|
|
68
61
|
role: m.role,
|
|
69
62
|
content: m.content
|
|
70
|
-
}))
|
|
71
|
-
role: "user",
|
|
72
|
-
content: content.trim()
|
|
73
|
-
}];
|
|
63
|
+
}));
|
|
74
64
|
let doingChunkedReveal = false;
|
|
75
65
|
try {
|
|
76
66
|
const g = global;
|
|
@@ -109,8 +99,11 @@ function useChatStream(sessionId) {
|
|
|
109
99
|
setMessages((prev) => [...prev, assistantMessage2]);
|
|
110
100
|
setResponse("");
|
|
111
101
|
if (effectiveSessionId) {
|
|
112
|
-
saveMessages({
|
|
113
|
-
sessionId: effectiveSessionId
|
|
102
|
+
saveMessages(__spreadProps(__spreadValues({
|
|
103
|
+
sessionId: effectiveSessionId
|
|
104
|
+
}, accountUserId ? {
|
|
105
|
+
createdBy: accountUserId
|
|
106
|
+
} : {}), {
|
|
114
107
|
userMessage: {
|
|
115
108
|
content: userMessage.content,
|
|
116
109
|
attachments: (_a2 = userMessage.attachments) == null ? void 0 : _a2.map((a) => ({
|
|
@@ -127,12 +120,14 @@ function useChatStream(sessionId) {
|
|
|
127
120
|
tokenCount: (_b2 = result.tokenUsage) == null ? void 0 : _b2.totalTokens,
|
|
128
121
|
model: void 0
|
|
129
122
|
}
|
|
130
|
-
}).catch((e) => console.error("[useChatStream] saveMessages:", e));
|
|
123
|
+
})).catch((e) => console.error("[useChatStream] saveMessages (fallback):", e));
|
|
131
124
|
}
|
|
132
125
|
isSendingRef.current = false;
|
|
133
126
|
setIsLoading(false);
|
|
134
127
|
setIsStreaming(false);
|
|
135
128
|
abortRef.current = null;
|
|
129
|
+
userMsgForSaveRef.current = null;
|
|
130
|
+
saveSessionIdRef.current = null;
|
|
136
131
|
}
|
|
137
132
|
}, REVEAL_INTERVAL_MS);
|
|
138
133
|
return;
|
|
@@ -151,8 +146,11 @@ function useChatStream(sessionId) {
|
|
|
151
146
|
setMessages((prev) => [...prev, assistantMessage]);
|
|
152
147
|
setResponse("");
|
|
153
148
|
if (effectiveSessionId) {
|
|
154
|
-
await saveMessages({
|
|
155
|
-
sessionId: effectiveSessionId
|
|
149
|
+
await saveMessages(__spreadProps(__spreadValues({
|
|
150
|
+
sessionId: effectiveSessionId
|
|
151
|
+
}, accountUserId ? {
|
|
152
|
+
createdBy: accountUserId
|
|
153
|
+
} : {}), {
|
|
156
154
|
userMessage: {
|
|
157
155
|
content: userMessage.content,
|
|
158
156
|
attachments: (_a = userMessage.attachments) == null ? void 0 : _a.map((a) => ({
|
|
@@ -169,12 +167,11 @@ function useChatStream(sessionId) {
|
|
|
169
167
|
tokenCount: (_b = result.tokenUsage) == null ? void 0 : _b.totalTokens,
|
|
170
168
|
model: void 0
|
|
171
169
|
}
|
|
172
|
-
});
|
|
170
|
+
}));
|
|
173
171
|
}
|
|
174
172
|
} catch (err) {
|
|
175
173
|
const msg = err instanceof Error ? err.message : String(err);
|
|
176
|
-
if (msg.includes("abort"))
|
|
177
|
-
setError(msg);
|
|
174
|
+
if (!msg.includes("abort")) setError(msg);
|
|
178
175
|
setResponse("");
|
|
179
176
|
} finally {
|
|
180
177
|
if (!doingChunkedReveal) {
|
|
@@ -182,9 +179,227 @@ function useChatStream(sessionId) {
|
|
|
182
179
|
setIsLoading(false);
|
|
183
180
|
setIsStreaming(false);
|
|
184
181
|
abortRef.current = null;
|
|
182
|
+
userMsgForSaveRef.current = null;
|
|
183
|
+
saveSessionIdRef.current = null;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}, [saveMessages, accountUserId]);
|
|
187
|
+
const streamChannelId = sessionId || cdecliStreamOverrideId;
|
|
188
|
+
const isCdecliActive = routing.kind === "cdecli" && routing.channelConnected;
|
|
189
|
+
const cdecliCallbacks = useMemo(() => ({
|
|
190
|
+
onChunk: (text) => {
|
|
191
|
+
if (routingRef.current.kind !== "cdecli") return;
|
|
192
|
+
setResponse((r) => r + text);
|
|
193
|
+
},
|
|
194
|
+
onComplete: (text) => {
|
|
195
|
+
var _a;
|
|
196
|
+
if (routingRef.current.kind !== "cdecli") return;
|
|
197
|
+
if (cdecliRoundHandledRef.current) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (isCdecliAgentFailureOutput(text)) {
|
|
201
|
+
cdecliRoundHandledRef.current = true;
|
|
202
|
+
console.warn("[useChatStream] CDeCLI agent error output; retrying via direct chat API");
|
|
203
|
+
const sid2 = saveSessionIdRef.current;
|
|
204
|
+
const um2 = userMsgForSaveRef.current;
|
|
205
|
+
const conv = cdecliFallbackConversationRef.current;
|
|
206
|
+
setResponse("");
|
|
207
|
+
setError(null);
|
|
208
|
+
userMsgForSaveRef.current = um2;
|
|
209
|
+
saveSessionIdRef.current = sid2;
|
|
210
|
+
setIsLoading(true);
|
|
211
|
+
setIsStreaming(true);
|
|
212
|
+
isSendingRef.current = true;
|
|
213
|
+
if ((conv == null ? void 0 : conv.length) && um2) {
|
|
214
|
+
void executeGroqStream(conv, sid2, um2);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
setMessages((prev) => [...prev, {
|
|
218
|
+
role: "assistant",
|
|
219
|
+
content: text
|
|
220
|
+
}]);
|
|
221
|
+
setIsLoading(false);
|
|
222
|
+
setIsStreaming(false);
|
|
223
|
+
isSendingRef.current = false;
|
|
224
|
+
userMsgForSaveRef.current = null;
|
|
225
|
+
saveSessionIdRef.current = null;
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
cdecliRoundHandledRef.current = true;
|
|
229
|
+
const sid = saveSessionIdRef.current;
|
|
230
|
+
const um = userMsgForSaveRef.current;
|
|
231
|
+
saveSessionIdRef.current = null;
|
|
232
|
+
userMsgForSaveRef.current = null;
|
|
233
|
+
setMessages((prev) => [...prev, {
|
|
234
|
+
role: "assistant",
|
|
235
|
+
content: text
|
|
236
|
+
}]);
|
|
237
|
+
setResponse("");
|
|
238
|
+
setIsLoading(false);
|
|
239
|
+
setIsStreaming(false);
|
|
240
|
+
isSendingRef.current = false;
|
|
241
|
+
const rt = routingRef.current;
|
|
242
|
+
const persistClientSide = rt.kind === "cdecli" && rt.persistenceMode !== "backend";
|
|
243
|
+
if (sid && um && persistClientSide) {
|
|
244
|
+
void saveMessages(__spreadProps(__spreadValues({
|
|
245
|
+
sessionId: sid
|
|
246
|
+
}, accountUserId ? {
|
|
247
|
+
createdBy: accountUserId
|
|
248
|
+
} : {}), {
|
|
249
|
+
userMessage: {
|
|
250
|
+
content: um.content,
|
|
251
|
+
attachments: (_a = um.attachments) == null ? void 0 : _a.map((a) => ({
|
|
252
|
+
id: a.id,
|
|
253
|
+
name: a.name,
|
|
254
|
+
type: a.type,
|
|
255
|
+
mimeType: a.mimeType,
|
|
256
|
+
size: a.size,
|
|
257
|
+
url: a.url
|
|
258
|
+
}))
|
|
259
|
+
},
|
|
260
|
+
assistantMessage: {
|
|
261
|
+
content: text,
|
|
262
|
+
tokenCount: void 0,
|
|
263
|
+
model: "cdecli-serve"
|
|
264
|
+
}
|
|
265
|
+
})).catch((e) => console.error("[useChatStream] saveMessages (cdecli):", e));
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
onError: (err) => {
|
|
269
|
+
if (routingRef.current.kind !== "cdecli") return;
|
|
270
|
+
if (cdecliRoundHandledRef.current) return;
|
|
271
|
+
const conv = cdecliFallbackConversationRef.current;
|
|
272
|
+
const um = userMsgForSaveRef.current;
|
|
273
|
+
const sid = saveSessionIdRef.current;
|
|
274
|
+
if ((conv == null ? void 0 : conv.length) && um) {
|
|
275
|
+
cdecliRoundHandledRef.current = true;
|
|
276
|
+
console.warn("[useChatStream] CDeCLI transport/stream error; falling back to direct chat API:", err);
|
|
277
|
+
setResponse("");
|
|
278
|
+
setError(null);
|
|
279
|
+
userMsgForSaveRef.current = um;
|
|
280
|
+
saveSessionIdRef.current = sid;
|
|
281
|
+
setIsLoading(true);
|
|
282
|
+
setIsStreaming(true);
|
|
283
|
+
isSendingRef.current = true;
|
|
284
|
+
void executeGroqStream(conv, sid, um);
|
|
285
|
+
return;
|
|
185
286
|
}
|
|
287
|
+
cdecliRoundHandledRef.current = false;
|
|
288
|
+
setError(err);
|
|
289
|
+
setResponse("");
|
|
290
|
+
setIsLoading(false);
|
|
291
|
+
setIsStreaming(false);
|
|
292
|
+
isSendingRef.current = false;
|
|
293
|
+
userMsgForSaveRef.current = null;
|
|
294
|
+
saveSessionIdRef.current = null;
|
|
295
|
+
}
|
|
296
|
+
}), [saveMessages, executeGroqStream, accountUserId]);
|
|
297
|
+
const {
|
|
298
|
+
sendMessage: sendCdecliMessage
|
|
299
|
+
} = useCdecliChannel(routing.kind === "cdecli", isCdecliActive, routing.kind === "cdecli" ? routing.accountId : "default", streamChannelId, cdecliCallbacks);
|
|
300
|
+
useEffect(() => () => {
|
|
301
|
+
if (revealIntervalRef.current) {
|
|
302
|
+
clearInterval(revealIntervalRef.current);
|
|
303
|
+
revealIntervalRef.current = null;
|
|
304
|
+
}
|
|
305
|
+
}, []);
|
|
306
|
+
useEffect(() => {
|
|
307
|
+
if (sessionId && cdecliStreamOverrideId && sessionId === cdecliStreamOverrideId) {
|
|
308
|
+
setCdecliStreamOverrideId(void 0);
|
|
309
|
+
}
|
|
310
|
+
}, [sessionId, cdecliStreamOverrideId]);
|
|
311
|
+
useEffect(() => {
|
|
312
|
+
if (isSendingRef.current) return;
|
|
313
|
+
if (!sessionId) {
|
|
314
|
+
setMessages([]);
|
|
315
|
+
setResponse("");
|
|
316
|
+
lastHydratedSessionIdRef.current = null;
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const sessionChanged = lastHydratedSessionIdRef.current !== sessionId;
|
|
320
|
+
if (sessionChanged) {
|
|
321
|
+
setMessages([]);
|
|
322
|
+
setResponse("");
|
|
323
|
+
lastHydratedSessionIdRef.current = sessionId;
|
|
324
|
+
}
|
|
325
|
+
if (backendMessagesLoaded) {
|
|
326
|
+
const formatted = (backendMessages != null ? backendMessages : []).map((msg) => {
|
|
327
|
+
var _a;
|
|
328
|
+
return {
|
|
329
|
+
role: msg.role,
|
|
330
|
+
content: (_a = msg.content) != null ? _a : "",
|
|
331
|
+
metadata: msg.tokenCount ? {
|
|
332
|
+
tokenUsage: {
|
|
333
|
+
inputTokens: 0,
|
|
334
|
+
outputTokens: msg.tokenCount,
|
|
335
|
+
totalTokens: msg.tokenCount
|
|
336
|
+
},
|
|
337
|
+
model: msg.model
|
|
338
|
+
} : void 0
|
|
339
|
+
};
|
|
340
|
+
});
|
|
341
|
+
setMessages(formatted);
|
|
342
|
+
setResponse("");
|
|
343
|
+
}
|
|
344
|
+
}, [sessionId, backendMessages, backendMessagesLoaded]);
|
|
345
|
+
const sendMessage = useCallback(async (content, attachments, sessionIdOverride) => {
|
|
346
|
+
if (!content.trim()) return;
|
|
347
|
+
const effectiveSessionId = sessionIdOverride !== void 0 ? sessionIdOverride : sessionId;
|
|
348
|
+
const userMessage = {
|
|
349
|
+
role: "user",
|
|
350
|
+
content: content.trim(),
|
|
351
|
+
attachments
|
|
352
|
+
};
|
|
353
|
+
isSendingRef.current = true;
|
|
354
|
+
setMessages((prev) => [...prev, userMessage]);
|
|
355
|
+
setResponse("");
|
|
356
|
+
setIsLoading(true);
|
|
357
|
+
setIsStreaming(true);
|
|
358
|
+
setError(null);
|
|
359
|
+
userMsgForSaveRef.current = userMessage;
|
|
360
|
+
saveSessionIdRef.current = effectiveSessionId != null ? effectiveSessionId : null;
|
|
361
|
+
cdecliRoundHandledRef.current = false;
|
|
362
|
+
const rt = routingRef.current;
|
|
363
|
+
if (rt.kind === "cdecli") {
|
|
364
|
+
if (!rt.channelConnected) {
|
|
365
|
+
setError("CDeCLI is not connected yet. Check gateway status in the header.");
|
|
366
|
+
setMessages((p) => p.slice(0, -1));
|
|
367
|
+
isSendingRef.current = false;
|
|
368
|
+
setIsLoading(false);
|
|
369
|
+
setIsStreaming(false);
|
|
370
|
+
userMsgForSaveRef.current = null;
|
|
371
|
+
saveSessionIdRef.current = null;
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
if (!effectiveSessionId) {
|
|
375
|
+
setError("Missing chat session id.");
|
|
376
|
+
setMessages((p) => p.slice(0, -1));
|
|
377
|
+
isSendingRef.current = false;
|
|
378
|
+
setIsLoading(false);
|
|
379
|
+
setIsStreaming(false);
|
|
380
|
+
userMsgForSaveRef.current = null;
|
|
381
|
+
saveSessionIdRef.current = null;
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
if (sessionIdOverride) {
|
|
385
|
+
setCdecliStreamOverrideId(sessionIdOverride);
|
|
386
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
387
|
+
}
|
|
388
|
+
cdecliFallbackConversationRef.current = [...messages, userMessage];
|
|
389
|
+
const ok = await sendCdecliMessage(content.trim(), effectiveSessionId);
|
|
390
|
+
if (!ok && !cdecliRoundHandledRef.current) {
|
|
391
|
+
const conv = cdecliFallbackConversationRef.current;
|
|
392
|
+
console.warn("[useChatStream] CDeCLI send returned false; falling back to direct chat API");
|
|
393
|
+
cdecliRoundHandledRef.current = true;
|
|
394
|
+
userMsgForSaveRef.current = userMessage;
|
|
395
|
+
saveSessionIdRef.current = effectiveSessionId != null ? effectiveSessionId : null;
|
|
396
|
+
void executeGroqStream(conv != null ? conv : [], effectiveSessionId != null ? effectiveSessionId : null, userMessage);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
return;
|
|
186
400
|
}
|
|
187
|
-
|
|
401
|
+
await executeGroqStream([...messages, userMessage], effectiveSessionId != null ? effectiveSessionId : null, userMessage);
|
|
402
|
+
}, [messages, sessionId, sendCdecliMessage, executeGroqStream]);
|
|
188
403
|
const cancel = useCallback(() => {
|
|
189
404
|
if (abortRef.current) {
|
|
190
405
|
abortRef.current.abort();
|
|
@@ -195,12 +410,14 @@ function useChatStream(sessionId) {
|
|
|
195
410
|
setResponse("");
|
|
196
411
|
setError(null);
|
|
197
412
|
}, []);
|
|
413
|
+
const messagesLoading = !!sessionId && backendMessagesLoading && !backendMessagesLoaded;
|
|
198
414
|
return {
|
|
199
415
|
messages,
|
|
200
416
|
response,
|
|
201
417
|
error,
|
|
202
418
|
isLoading,
|
|
203
419
|
isStreaming,
|
|
420
|
+
messagesLoading,
|
|
204
421
|
hasMessages: messages.length > 0 || !!response,
|
|
205
422
|
sendMessage,
|
|
206
423
|
cancel,
|