@mastra/react 1.12.0-alpha.9 → 1.13.0-alpha.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/{accumulator-BQGsraqS.cjs → accumulator-BmD8-x9I.cjs} +52 -4
- package/dist/accumulator-BmD8-x9I.cjs.map +1 -0
- package/dist/{accumulator-C8kIc450.js → accumulator-D9jAph99.js} +52 -4
- package/dist/accumulator-D9jAph99.js.map +1 -0
- package/dist/chat/index.cjs +1 -1
- package/dist/chat/index.js +1 -2
- package/dist/combine-by-ids-D_lp6Dvq.cjs.map +1 -1
- package/dist/experiments-B7vRMK5T.js +306 -0
- package/dist/experiments-B7vRMK5T.js.map +1 -0
- package/dist/experiments-CJw1N3FB.cjs +353 -0
- package/dist/experiments-CJw1N3FB.cjs.map +1 -0
- package/dist/hooks/agents/index.cjs +2 -2
- package/dist/hooks/agents/index.cjs.map +1 -1
- package/dist/hooks/agents/index.js +2 -2
- package/dist/hooks/agents/index.js.map +1 -1
- package/dist/hooks/auth/index.cjs +2 -2
- package/dist/hooks/auth/index.cjs.map +1 -1
- package/dist/hooks/auth/index.js +2 -2
- package/dist/hooks/auth/index.js.map +1 -1
- package/dist/hooks/capabilities/index.cjs +58 -1
- package/dist/hooks/capabilities/index.cjs.map +1 -1
- package/dist/hooks/capabilities/index.d.ts +4 -0
- package/dist/hooks/capabilities/index.d.ts.map +1 -1
- package/dist/hooks/capabilities/index.js +55 -2
- package/dist/hooks/capabilities/index.js.map +1 -1
- package/dist/hooks/capabilities/use-span-aggregate-available.d.ts +9 -0
- package/dist/hooks/capabilities/use-span-aggregate-available.d.ts.map +1 -0
- package/dist/hooks/capabilities/use-span-query-available.d.ts +9 -0
- package/dist/hooks/capabilities/use-span-query-available.d.ts.map +1 -0
- package/dist/hooks/capabilities/use-thread-query-available.d.ts +9 -0
- package/dist/hooks/capabilities/use-thread-query-available.d.ts.map +1 -0
- package/dist/hooks/capabilities/use-trace-query-delta-polling-available.d.ts +10 -0
- package/dist/hooks/capabilities/use-trace-query-delta-polling-available.d.ts.map +1 -0
- package/dist/hooks/configuration/index.cjs.map +1 -1
- package/dist/hooks/datasets/index.cjs +25 -1
- package/dist/hooks/datasets/index.cjs.map +1 -1
- package/dist/hooks/datasets/index.js +25 -2
- package/dist/hooks/datasets/index.js.map +1 -1
- package/dist/hooks/datasets/use-datasets.d.ts +7 -0
- package/dist/hooks/datasets/use-datasets.d.ts.map +1 -1
- package/dist/hooks/embedders/index.cjs.map +1 -1
- package/dist/hooks/experiments/index.cjs +9 -235
- package/dist/hooks/experiments/index.d.ts +1 -0
- package/dist/hooks/experiments/index.d.ts.map +1 -1
- package/dist/hooks/experiments/index.js +1 -229
- package/dist/hooks/experiments/usage-accumulator.d.ts +25 -0
- package/dist/hooks/experiments/usage-accumulator.d.ts.map +1 -0
- package/dist/hooks/experiments/use-experiment-item-usage.d.ts +2 -7
- package/dist/hooks/experiments/use-experiment-item-usage.d.ts.map +1 -1
- package/dist/hooks/experiments/use-experiments-list-metrics.d.ts +19 -0
- package/dist/hooks/experiments/use-experiments-list-metrics.d.ts.map +1 -0
- package/dist/hooks/feedback/index.cjs.map +1 -1
- package/dist/hooks/llm/index.cjs.map +1 -1
- package/dist/hooks/logs/index.cjs.map +1 -1
- package/dist/hooks/mcps/index.cjs.map +1 -1
- package/dist/hooks/memory/index.cjs +10 -10
- package/dist/hooks/memory/index.cjs.map +1 -1
- package/dist/hooks/memory/index.js +10 -10
- package/dist/hooks/memory/index.js.map +1 -1
- package/dist/hooks/metrics/index.cjs +13 -11
- package/dist/hooks/metrics/index.cjs.map +1 -1
- package/dist/hooks/metrics/index.js +13 -11
- package/dist/hooks/metrics/index.js.map +1 -1
- package/dist/hooks/processors/index.cjs.map +1 -1
- package/dist/hooks/prompt-blocks/index.cjs.map +1 -1
- package/dist/hooks/query/index.cjs.map +1 -1
- package/dist/hooks/review/index.cjs +2 -2
- package/dist/hooks/review/index.cjs.map +1 -1
- package/dist/hooks/review/index.js +1 -1
- package/dist/hooks/schedules/index.cjs.map +1 -1
- package/dist/hooks/scores/index.cjs.map +1 -1
- package/dist/hooks/templates/index.cjs +20 -18
- package/dist/hooks/templates/index.cjs.map +1 -1
- package/dist/hooks/templates/index.js +20 -18
- package/dist/hooks/templates/index.js.map +1 -1
- package/dist/hooks/templates/use-templates.d.ts.map +1 -1
- package/dist/hooks/tool-providers/index.cjs +1 -1
- package/dist/hooks/tool-providers/index.cjs.map +1 -1
- package/dist/hooks/tool-providers/index.js +1 -1
- package/dist/hooks/tool-providers/index.js.map +1 -1
- package/dist/hooks/tools/index.cjs.map +1 -1
- package/dist/hooks/traces/index.cjs +292 -9
- package/dist/hooks/traces/index.cjs.map +1 -1
- package/dist/hooks/traces/index.d.ts +2 -0
- package/dist/hooks/traces/index.d.ts.map +1 -1
- package/dist/hooks/traces/index.js +285 -10
- package/dist/hooks/traces/index.js.map +1 -1
- package/dist/hooks/traces/use-query-spans.d.ts +27 -0
- package/dist/hooks/traces/use-query-spans.d.ts.map +1 -0
- package/dist/hooks/traces/use-query-threads.d.ts +30 -0
- package/dist/hooks/traces/use-query-threads.d.ts.map +1 -0
- package/dist/hooks/traces/use-trace-query.d.ts +50 -1
- package/dist/hooks/traces/use-trace-query.d.ts.map +1 -1
- package/dist/hooks/traces/use-traces.d.ts +37 -37
- package/dist/hooks/vectors/index.cjs.map +1 -1
- package/dist/hooks/voice/index.cjs.map +1 -1
- package/dist/hooks/workflows/index.cjs +1 -1
- package/dist/hooks/workflows/index.cjs.map +1 -1
- package/dist/hooks/workflows/index.js +1 -1
- package/dist/hooks/workspace/index.cjs +1 -1
- package/dist/hooks/workspace/index.cjs.map +1 -1
- package/dist/hooks/workspace/index.js +1 -1
- package/dist/hooks/workspace/index.js.map +1 -1
- package/dist/{hooks-CIutb3DO.cjs → hooks-C6CJtoOR.cjs} +6 -4
- package/dist/hooks-C6CJtoOR.cjs.map +1 -0
- package/dist/{hooks-0L0RNfau.js → hooks-DG-Y1KFe.js} +2 -2
- package/dist/{hooks-0L0RNfau.js.map → hooks-DG-Y1KFe.js.map} +1 -1
- package/dist/{hooks-FVbeHlov.js → hooks-DvLSZtUd.js} +6 -4
- package/dist/{hooks-FVbeHlov.js.map → hooks-DvLSZtUd.js.map} +1 -1
- package/dist/{hooks-_YevKidX.cjs → hooks-XYsmqFCz.cjs} +2 -2
- package/dist/hooks-XYsmqFCz.cjs.map +1 -0
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +3 -3
- package/dist/lib/mastra-db/accumulator.d.ts +0 -6
- package/dist/lib/mastra-db/accumulator.d.ts.map +1 -1
- package/dist/mastra-client-context-DzX3f0RF.cjs.map +1 -1
- package/dist/ui/index.cjs +4 -4
- package/dist/ui/index.cjs.map +1 -1
- package/dist/ui/index.js +3 -3
- package/dist/ui/index.js.map +1 -1
- package/dist/use-in-view-B6eJO9Zi.cjs.map +1 -1
- package/dist/voice-BOtUdMi8.cjs.map +1 -1
- package/package.json +7 -7
- package/dist/accumulator-BQGsraqS.cjs.map +0 -1
- package/dist/accumulator-C8kIc450.js.map +0 -1
- package/dist/hooks/experiments/index.cjs.map +0 -1
- package/dist/hooks/experiments/index.js.map +0 -1
- package/dist/hooks-CIutb3DO.cjs.map +0 -1
- package/dist/hooks-_YevKidX.cjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["useMastraClient","useMastraClient","useMastraClient","useMastraClient","useMastraClient","useMastraClient","useMastraClient"],"sources":["../../../src/hooks/mcps/use-mcp-app-html.ts","../../../src/hooks/mcps/use-mcp-app-tools.ts","../../../src/hooks/mcps/use-mcp-server-tool.ts","../../../src/hooks/mcps/use-mcp-server-tools.ts","../../../src/hooks/mcps/use-mcp-server-tools-by-id.ts","../../../src/hooks/mcps/use-mcp-servers.ts","../../../src/hooks/mcps/use-try-connect-mcp.ts"],"sourcesContent":["import type { MastraClient } from '@mastra/client-js';\nimport { useQuery } from '@tanstack/react-query';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\n/** The app HTML, or `null` when the resource has no text. */\ntype McpAppHtml = string | null;\n\nexport type ReadMcpServerResourceResponse = Awaited<ReturnType<MastraClient['readMcpServerResource']>>;\n\n/**\n * The HTML of an MCP tool's `ui://` app resource (from the tool's `_meta`), for rendering the tool's own UI.\n * Skips the fetch when the tool has no app resource.\n */\nexport const useMcpAppHtml = <TData = McpAppHtml>({\n serverId,\n appResourceUri,\n queryOptions,\n}: {\n serverId: string;\n appResourceUri?: string;\n queryOptions?: MastraQueryOptions<McpAppHtml, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-app-resource', serverId, appResourceUri],\n queryFn: async () => {\n if (!appResourceUri) return null;\n const response = await client.readMcpServerResource(serverId, appResourceUri);\n return response.contents[0]?.text ?? null;\n },\n enabled: appResourceUri !== undefined,\n ...queryOptions,\n });\n};\n","import type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport interface McpAppToolInfo {\n serverId: string;\n toolId: string;\n toolName: string;\n resourceUri: string;\n}\n\n/**\n * Builds a map of tool names → MCP App info for tools across all registered\n * MCP servers that have `_meta.ui.resourceUri` (i.e., tools with interactive\n * MCP App UIs).\n *\n * For each app tool, entries are created under both the original tool name\n * and the namespaced name (`${serverId}_${toolName}`) so that agent tools\n * sourced via `MCPClient.listTools()` (which namespaces) can be resolved.\n */\nexport function useMcpAppTools<TData = Record<string, McpAppToolInfo>>({\n queryOptions,\n}: { queryOptions?: MastraQueryOptions<Record<string, McpAppToolInfo>, TData> } = {}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-app-tools'],\n queryFn: async () => {\n const map: Record<string, McpAppToolInfo> = {};\n\n const { servers } = await client.getMcpServers();\n if (!servers?.length) return map;\n\n const results = await Promise.allSettled(\n servers.map(async server => {\n const { tools } = await client.getMcpServerTools(server.id);\n return { serverId: server.id, tools };\n }),\n );\n\n for (const result of results) {\n if (result.status !== 'fulfilled') continue;\n const { serverId, tools } = result.value;\n for (const tool of tools) {\n const meta = tool._meta as { ui?: { resourceUri?: string } } | undefined;\n const resourceUri = meta?.ui?.resourceUri;\n if (resourceUri) {\n const toolId = tool.name;\n const info: McpAppToolInfo = { serverId, toolId, toolName: tool.name, resourceUri };\n map[toolId] = info;\n if (tool.name !== toolId) {\n map[tool.name] = info;\n }\n // Also map the namespaced name used by MCPClient.listTools()\n map[`${serverId}_${tool.name}`] = info;\n }\n }\n }\n\n return map;\n },\n staleTime: 60_000,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { RequestContext } from '@mastra/core/request-context';\nimport { useMutation, useQuery } from '@tanstack/react-query';\nimport type { UseMutationResult, UseQueryResult } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions, MastraQueryOptions } from '../shared/query-options';\n\ntype MCPServerToolDetails = Awaited<ReturnType<ReturnType<MastraClient['getMcpServerTool']>['details']>>;\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMCPServerTool = <TData = MCPServerToolDetails>({\n serverId,\n toolId,\n requestContext,\n queryOptions,\n}: {\n serverId: string;\n toolId: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MCPServerToolDetails, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-server-tool', serverId, toolId],\n queryFn: () => {\n const instance = client.getMcpServerTool(serverId, toolId);\n return instance.details(requestContext);\n },\n ...queryOptions,\n });\n};\n\nexport type ExecuteMCPToolResponse = Awaited<ReturnType<ReturnType<MastraClient['getMcpServerTool']>['execute']>>;\ntype ExecuteMCPToolVariables = { data: any; requestContext?: Record<string, any> };\n\nexport const useExecuteMCPTool = ({\n serverId,\n toolId,\n queryOptions,\n}: {\n serverId: string;\n toolId: string;\n queryOptions?: MastraMutationOptions<ExecuteMCPToolResponse, ExecuteMCPToolVariables>;\n}): UseMutationResult<ExecuteMCPToolResponse, Error, ExecuteMCPToolVariables> => {\n const client = useMastraClient();\n\n return useMutation({\n mutationFn: ({ data, requestContext }: ExecuteMCPToolVariables) => {\n const instance = client.getMcpServerTool(serverId, toolId);\n return instance.execute({ data, requestContext: requestContext as RequestContext });\n },\n ...queryOptions,\n });\n};\n","import type { McpToolInfo as SdkMcpToolInfo } from '@mastra/client-js';\nimport type { ServerInfo } from '@mastra/core/mcp';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const useMCPServerTools = <TData = Record<string, SdkMcpToolInfo>>({\n selectedServer,\n queryOptions,\n}: {\n selectedServer: ServerInfo;\n queryOptions?: MastraQueryOptions<Record<string, SdkMcpToolInfo>, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcpserver-tools', selectedServer?.id],\n queryFn: async () => {\n const response = await client.getMcpServerTools(selectedServer?.id);\n const fetchedToolsArray: SdkMcpToolInfo[] = response.tools;\n const transformedTools: Record<string, SdkMcpToolInfo> = {};\n fetchedToolsArray.forEach((sdkToolInfo: SdkMcpToolInfo) => {\n transformedTools[sdkToolInfo.name] = sdkToolInfo;\n });\n\n return transformedTools;\n },\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\ntype MCPServerToolsById = Record<string, Awaited<ReturnType<MastraClient['getMcpServerTools']>>['tools'][number]>;\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMCPServerToolsById = <TData = MCPServerToolsById>({\n serverId,\n queryOptions,\n}: {\n serverId: string | null;\n queryOptions?: MastraQueryOptions<MCPServerToolsById, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcpserver-tools', serverId],\n queryFn: async (): Promise<MCPServerToolsById> => {\n const response = await client.getMcpServerTools(serverId!);\n return Object.fromEntries(response.tools.map(tool => [tool.name, tool]));\n },\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n","import type { McpServerListResponse } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const useMCPServers = <TData = McpServerListResponse['servers']>({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<McpServerListResponse['servers'], TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-servers'],\n queryFn: async () => {\n const mcpServers: McpServerListResponse['servers'] = (await client.getMcpServers(requestContext)).servers;\n return mcpServers;\n },\n ...queryOptions,\n });\n};\n","import { useMutation } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions } from '../shared/query-options';\n\ninterface McpTool {\n name: string;\n description?: string;\n inputSchema?: Record<string, unknown>;\n}\n\nexport interface TryConnectResult {\n tools: McpTool[];\n}\n\nasync function parseResponse(response: Response): Promise<unknown> {\n const contentType = response.headers.get('content-type') ?? '';\n\n if (contentType.includes('text/event-stream')) {\n const text = await response.text();\n const lines = text.split('\\n');\n\n for (const line of lines) {\n if (line.startsWith('data:')) {\n const data = line.slice('data:'.length).trim();\n if (data) {\n return JSON.parse(data);\n }\n }\n }\n\n throw new Error('No data found in SSE response');\n }\n\n return response.json();\n}\n\nasync function connectAndListTools(url: string, clientHeaders?: Record<string, string>): Promise<TryConnectResult> {\n const baseHeaders: Record<string, string> = {\n ...clientHeaders,\n 'Content-Type': 'application/json',\n Accept: 'application/json, text/event-stream',\n };\n\n // Step 1: Initialize\n const initResponse = await fetch(url, {\n method: 'POST',\n headers: baseHeaders,\n credentials: 'include',\n body: JSON.stringify({\n jsonrpc: '2.0',\n id: 1,\n method: 'initialize',\n params: {\n protocolVersion: '2025-03-26',\n capabilities: {},\n clientInfo: { name: 'mastra-playground', version: '1.0.0' },\n },\n }),\n });\n\n if (!initResponse.ok) {\n throw new Error(`Initialize failed: ${initResponse.status} ${initResponse.statusText}`);\n }\n\n const sessionId = initResponse.headers.get('Mcp-Session-Id');\n await parseResponse(initResponse);\n\n const sessionHeaders: Record<string, string> = {\n ...baseHeaders,\n };\n if (sessionId) {\n sessionHeaders['Mcp-Session-Id'] = sessionId;\n }\n\n // Step 2: Send initialized notification\n await fetch(url, {\n method: 'POST',\n headers: sessionHeaders,\n credentials: 'include',\n body: JSON.stringify({\n jsonrpc: '2.0',\n method: 'notifications/initialized',\n }),\n });\n\n // Step 3: List tools\n const toolsResponse = await fetch(url, {\n method: 'POST',\n headers: sessionHeaders,\n credentials: 'include',\n body: JSON.stringify({\n jsonrpc: '2.0',\n id: 2,\n method: 'tools/list',\n }),\n });\n\n if (!toolsResponse.ok) {\n throw new Error(`tools/list failed: ${toolsResponse.status} ${toolsResponse.statusText}`);\n }\n\n const toolsResult = (await parseResponse(toolsResponse)) as { result?: { tools?: McpTool[] } };\n\n return {\n tools: toolsResult.result?.tools ?? [],\n };\n}\n\nexport const useTryConnectMcp = ({\n queryOptions,\n}: { queryOptions?: MastraMutationOptions<TryConnectResult, string> } = {}) => {\n const client = useMastraClient();\n const clientHeaders = (client.options?.headers as Record<string, string>) ?? {};\n\n return useMutation({\n mutationFn: (url: string) => connectAndListTools(url, clientHeaders),\n ...queryOptions,\n });\n};\n\nexport type TryConnectMcpMutation = ReturnType<typeof useTryConnectMcp>;\n"],"mappings":";;;;;;;;AAeA,MAAa,iBAAqC,EAChD,UACA,gBACA,mBAKkC;CAClC,MAAM,SAASA,8BAAAA,gBAAgB;CAE/B,QAAA,GAAA,sBAAA,SAAA,CAAgB;EACd,UAAU;GAAC;GAAoB;GAAU;EAAc;EACvD,SAAS,YAAY;GACnB,IAAI,CAAC,gBAAgB,OAAO;GAE5B,QAAO,MADgB,OAAO,sBAAsB,UAAU,cAAc,EAAA,CAC5D,SAAS,EAAE,EAAE,QAAQ;EACvC;EACA,SAAS,mBAAmB,KAAA;EAC5B,GAAG;CACL,CAAC;AACH;;;;;;;;;;;;ACfA,SAAgB,eAAuD,EACrE,iBACgF,CAAC,GAAiC;CAClH,MAAM,SAASC,8BAAAA,gBAAgB;CAE/B,QAAA,GAAA,sBAAA,SAAA,CAAgB;EACd,UAAU,CAAC,eAAe;EAC1B,SAAS,YAAY;GACnB,MAAM,MAAsC,CAAC;GAE7C,MAAM,EAAE,YAAY,MAAM,OAAO,cAAc;GAC/C,IAAI,CAAC,SAAS,QAAQ,OAAO;GAE7B,MAAM,UAAU,MAAM,QAAQ,WAC5B,QAAQ,IAAI,OAAM,WAAU;IAC1B,MAAM,EAAE,UAAU,MAAM,OAAO,kBAAkB,OAAO,EAAE;IAC1D,OAAO;KAAE,UAAU,OAAO;KAAI;IAAM;GACtC,CAAC,CACH;GAEA,KAAK,MAAM,UAAU,SAAS;IAC5B,IAAI,OAAO,WAAW,aAAa;IACnC,MAAM,EAAE,UAAU,UAAU,OAAO;IACnC,KAAK,MAAM,QAAQ,OAAO;KAExB,MAAM,cADO,KAAK,OACQ,IAAI;KAC9B,IAAI,aAAa;MACf,MAAM,SAAS,KAAK;MACpB,MAAM,OAAuB;OAAE;OAAU;OAAQ,UAAU,KAAK;OAAM;MAAY;M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AAqB,SAAS;IAAQ;GAC5D;EACF,CAAC;CACH,CAAC;CAED,IAAI,CAAC,aAAa,IAChB,MAAM,IAAI,MAAM,sBAAsB,aAAa,OAAO,GAAG,aAAa,YAAY;CAGxF,MAAM,YAAY,aAAa,QAAQ,IAAI,gBAAgB;CAC3D,MAAM,cAAc,YAAY;CAEhC,MAAM,iBAAyC,EAC7C,GAAG,YACL;CACA,IAAI,WACF,eAAe,oBAAoB;CAIrC,MAAM,MAAM,KAAK;EACf,QAAQ;EACR,SAAS;EACT,aAAa;EACb,MAAM,KAAK,UAAU;GACnB,SAAS;GACT,QAAQ;EACV,CAAC;CACH,CAAC;CAGD,MAAM,gBAAgB,MAAM,MAAM,KAAK;EACrC,QAAQ;EACR,SAAS;EACT,aAAa;EACb,MAAM,KAAK,UAAU;GACnB,SAAS;GACT,IAAI;GACJ,QAAQ;EACV,CAAC;CACH,CAAC;CAED,IAAI,CAAC,cAAc,IACjB,MAAM,IAAI,MAAM,sBAAsB,cAAc,OAAO,GAAG,cAAc,YAAY;CAK1F,OAAO,EACL,QAAO,MAHkB,cAAc,aAAa,EAAA,CAGjC,QAAQ,SAAS,CAAC,EACvC;AACF;AAEA,MAAa,oBAAoB,EAC/B,iBACsE,CAAC,MAAM;CAE7E,MAAM,gBADSC,8BAAAA,gBACa,CAAC,CAAC,SAAS,WAAsC,CAAC;CAE9E,QAAA,GAAA,sBAAA,YAAA,CAAmB;EACjB,aAAa,QAAgB,oBAAoB,KAAK,aAAa;EACnE,GAAG;CACL,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["useMastraClient","useQuery","useMastraClient","useQuery","useMastraClient","useQuery","useMutation","useMastraClient","useQuery","useMastraClient","useQuery","useMastraClient","useQuery","useMastraClient","useMutation"],"sources":["../../../src/hooks/mcps/use-mcp-app-html.ts","../../../src/hooks/mcps/use-mcp-app-tools.ts","../../../src/hooks/mcps/use-mcp-server-tool.ts","../../../src/hooks/mcps/use-mcp-server-tools.ts","../../../src/hooks/mcps/use-mcp-server-tools-by-id.ts","../../../src/hooks/mcps/use-mcp-servers.ts","../../../src/hooks/mcps/use-try-connect-mcp.ts"],"sourcesContent":["import type { MastraClient } from '@mastra/client-js';\nimport { useQuery } from '@tanstack/react-query';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\n/** The app HTML, or `null` when the resource has no text. */\ntype McpAppHtml = string | null;\n\nexport type ReadMcpServerResourceResponse = Awaited<ReturnType<MastraClient['readMcpServerResource']>>;\n\n/**\n * The HTML of an MCP tool's `ui://` app resource (from the tool's `_meta`), for rendering the tool's own UI.\n * Skips the fetch when the tool has no app resource.\n */\nexport const useMcpAppHtml = <TData = McpAppHtml>({\n serverId,\n appResourceUri,\n queryOptions,\n}: {\n serverId: string;\n appResourceUri?: string;\n queryOptions?: MastraQueryOptions<McpAppHtml, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-app-resource', serverId, appResourceUri],\n queryFn: async () => {\n if (!appResourceUri) return null;\n const response = await client.readMcpServerResource(serverId, appResourceUri);\n return response.contents[0]?.text ?? null;\n },\n enabled: appResourceUri !== undefined,\n ...queryOptions,\n });\n};\n","import type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport interface McpAppToolInfo {\n serverId: string;\n toolId: string;\n toolName: string;\n resourceUri: string;\n}\n\n/**\n * Builds a map of tool names → MCP App info for tools across all registered\n * MCP servers that have `_meta.ui.resourceUri` (i.e., tools with interactive\n * MCP App UIs).\n *\n * For each app tool, entries are created under both the original tool name\n * and the namespaced name (`${serverId}_${toolName}`) so that agent tools\n * sourced via `MCPClient.listTools()` (which namespaces) can be resolved.\n */\nexport function useMcpAppTools<TData = Record<string, McpAppToolInfo>>({\n queryOptions,\n}: { queryOptions?: MastraQueryOptions<Record<string, McpAppToolInfo>, TData> } = {}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-app-tools'],\n queryFn: async () => {\n const map: Record<string, McpAppToolInfo> = {};\n\n const { servers } = await client.getMcpServers();\n if (!servers?.length) return map;\n\n const results = await Promise.allSettled(\n servers.map(async server => {\n const { tools } = await client.getMcpServerTools(server.id);\n return { serverId: server.id, tools };\n }),\n );\n\n for (const result of results) {\n if (result.status !== 'fulfilled') continue;\n const { serverId, tools } = result.value;\n for (const tool of tools) {\n const meta = tool._meta as { ui?: { resourceUri?: string } } | undefined;\n const resourceUri = meta?.ui?.resourceUri;\n if (resourceUri) {\n const toolId = tool.name;\n const info: McpAppToolInfo = { serverId, toolId, toolName: tool.name, resourceUri };\n map[toolId] = info;\n if (tool.name !== toolId) {\n map[tool.name] = info;\n }\n // Also map the namespaced name used by MCPClient.listTools()\n map[`${serverId}_${tool.name}`] = info;\n }\n }\n }\n\n return map;\n },\n staleTime: 60_000,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { RequestContext } from '@mastra/core/request-context';\nimport { useMutation, useQuery } from '@tanstack/react-query';\nimport type { UseMutationResult, UseQueryResult } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions, MastraQueryOptions } from '../shared/query-options';\n\ntype MCPServerToolDetails = Awaited<ReturnType<ReturnType<MastraClient['getMcpServerTool']>['details']>>;\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMCPServerTool = <TData = MCPServerToolDetails>({\n serverId,\n toolId,\n requestContext,\n queryOptions,\n}: {\n serverId: string;\n toolId: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MCPServerToolDetails, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-server-tool', serverId, toolId],\n queryFn: () => {\n const instance = client.getMcpServerTool(serverId, toolId);\n return instance.details(requestContext);\n },\n ...queryOptions,\n });\n};\n\nexport type ExecuteMCPToolResponse = Awaited<ReturnType<ReturnType<MastraClient['getMcpServerTool']>['execute']>>;\ntype ExecuteMCPToolVariables = { data: any; requestContext?: Record<string, any> };\n\nexport const useExecuteMCPTool = ({\n serverId,\n toolId,\n queryOptions,\n}: {\n serverId: string;\n toolId: string;\n queryOptions?: MastraMutationOptions<ExecuteMCPToolResponse, ExecuteMCPToolVariables>;\n}): UseMutationResult<ExecuteMCPToolResponse, Error, ExecuteMCPToolVariables> => {\n const client = useMastraClient();\n\n return useMutation({\n mutationFn: ({ data, requestContext }: ExecuteMCPToolVariables) => {\n const instance = client.getMcpServerTool(serverId, toolId);\n return instance.execute({ data, requestContext: requestContext as RequestContext });\n },\n ...queryOptions,\n });\n};\n","import type { McpToolInfo as SdkMcpToolInfo } from '@mastra/client-js';\nimport type { ServerInfo } from '@mastra/core/mcp';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const useMCPServerTools = <TData = Record<string, SdkMcpToolInfo>>({\n selectedServer,\n queryOptions,\n}: {\n selectedServer: ServerInfo;\n queryOptions?: MastraQueryOptions<Record<string, SdkMcpToolInfo>, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcpserver-tools', selectedServer?.id],\n queryFn: async () => {\n const response = await client.getMcpServerTools(selectedServer?.id);\n const fetchedToolsArray: SdkMcpToolInfo[] = response.tools;\n const transformedTools: Record<string, SdkMcpToolInfo> = {};\n fetchedToolsArray.forEach((sdkToolInfo: SdkMcpToolInfo) => {\n transformedTools[sdkToolInfo.name] = sdkToolInfo;\n });\n\n return transformedTools;\n },\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\ntype MCPServerToolsById = Record<string, Awaited<ReturnType<MastraClient['getMcpServerTools']>>['tools'][number]>;\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMCPServerToolsById = <TData = MCPServerToolsById>({\n serverId,\n queryOptions,\n}: {\n serverId: string | null;\n queryOptions?: MastraQueryOptions<MCPServerToolsById, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcpserver-tools', serverId],\n queryFn: async (): Promise<MCPServerToolsById> => {\n const response = await client.getMcpServerTools(serverId!);\n return Object.fromEntries(response.tools.map(tool => [tool.name, tool]));\n },\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n","import type { McpServerListResponse } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const useMCPServers = <TData = McpServerListResponse['servers']>({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<McpServerListResponse['servers'], TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['mcp-servers'],\n queryFn: async () => {\n const mcpServers: McpServerListResponse['servers'] = (await client.getMcpServers(requestContext)).servers;\n return mcpServers;\n },\n ...queryOptions,\n });\n};\n","import { useMutation } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions } from '../shared/query-options';\n\ninterface McpTool {\n name: string;\n description?: string;\n inputSchema?: Record<string, unknown>;\n}\n\nexport interface TryConnectResult {\n tools: McpTool[];\n}\n\nasync function parseResponse(response: Response): Promise<unknown> {\n const contentType = response.headers.get('content-type') ?? '';\n\n if (contentType.includes('text/event-stream')) {\n const text = await response.text();\n const lines = text.split('\\n');\n\n for (const line of lines) {\n if (line.startsWith('data:')) {\n const data = line.slice('data:'.length).trim();\n if (data) {\n return JSON.parse(data);\n }\n }\n }\n\n throw new Error('No data found in SSE response');\n }\n\n return response.json();\n}\n\nasync function connectAndListTools(url: string, clientHeaders?: Record<string, string>): Promise<TryConnectResult> {\n const baseHeaders: Record<string, string> = {\n ...clientHeaders,\n 'Content-Type': 'application/json',\n Accept: 'application/json, text/event-stream',\n };\n\n // Step 1: Initialize\n const initResponse = await fetch(url, {\n method: 'POST',\n headers: baseHeaders,\n credentials: 'include',\n body: JSON.stringify({\n jsonrpc: '2.0',\n id: 1,\n method: 'initialize',\n params: {\n protocolVersion: '2025-03-26',\n capabilities: {},\n clientInfo: { name: 'mastra-playground', version: '1.0.0' },\n },\n }),\n });\n\n if (!initResponse.ok) {\n throw new Error(`Initialize failed: ${initResponse.status} ${initResponse.statusText}`);\n }\n\n const sessionId = initResponse.headers.get('Mcp-Session-Id');\n await parseResponse(initResponse);\n\n const sessionHeaders: Record<string, string> = {\n ...baseHeaders,\n };\n if (sessionId) {\n sessionHeaders['Mcp-Session-Id'] = sessionId;\n }\n\n // Step 2: Send initialized notification\n await fetch(url, {\n method: 'POST',\n headers: sessionHeaders,\n credentials: 'include',\n body: JSON.stringify({\n jsonrpc: '2.0',\n method: 'notifications/initialized',\n }),\n });\n\n // Step 3: List tools\n const toolsResponse = await fetch(url, {\n method: 'POST',\n headers: sessionHeaders,\n credentials: 'include',\n body: JSON.stringify({\n jsonrpc: '2.0',\n id: 2,\n method: 'tools/list',\n }),\n });\n\n if (!toolsResponse.ok) {\n throw new Error(`tools/list failed: ${toolsResponse.status} ${toolsResponse.statusText}`);\n }\n\n const toolsResult = (await parseResponse(toolsResponse)) as { result?: { tools?: McpTool[] } };\n\n return {\n tools: toolsResult.result?.tools ?? [],\n };\n}\n\nexport const useTryConnectMcp = ({\n queryOptions,\n}: { queryOptions?: MastraMutationOptions<TryConnectResult, string> } = {}) => {\n const client = useMastraClient();\n const clientHeaders = (client.options?.headers as Record<string, string>) ?? {};\n\n return useMutation({\n mutationFn: (url: string) => connectAndListTools(url, clientHeaders),\n ...queryOptions,\n });\n};\n\nexport type TryConnectMcpMutation = ReturnType<typeof useTryConnectMcp>;\n"],"mappings":";;;;;;;;AAeA,MAAa,iBAAqC,EAChD,UACA,gBACA,mBAKkC;CAClC,MAAM,SAASA,8BAAAA,gBAAgB;CAE/B,QAAA,GAAOC,sBAAAA,SAAAA,CAAS;EACd,UAAU;GAAC;GAAoB;GAAU;EAAc;EACvD,SAAS,YAAY;GACnB,IAAI,CAAC,gBAAgB,OAAO;GAE5B,QAAO,MADgB,OAAO,sBAAsB,UAAU,cAAc,EAAA,CAC5D,SAAS,EAAE,EAAE,QAAQ;EACvC;EACA,SAAS,mBAAmB,KAAA;EAC5B,GAAG;CACL,CAAC;AACH;;;;;;;;;;;;ACfA,SAAgB,eAAuD,EACrE,iBACgF,CAAC,GAAiC;CAClH,MAAM,SAASC,8BAAAA,gBAAgB;CAE/B,QAAA,GAAOC,sBAAAA,SAAAA,CAAS;EACd,UAAU,CAAC,eAAe;EAC1B,SAAS,YAAY;GACnB,MAAM,MAAsC,CAAC;GAE7C,MAAM,EAAE,YAAY,MAAM,OAAO,cAAc;GAC/C,IAAI,CAAC,SAAS,QAAQ,OAAO;GAE7B,MAAM,UAAU,MAAM,QAAQ,WAC5B,QAAQ,IAAI,OAAM,WAAU;IAC1B,MAAM,EAAE,UAAU,MAAM,OAAO,kBAAkB,OAAO,EAAE;IAC1D,OAAO;KAAE,UAAU,OAAO;KAAI;IAAM;GACtC,CAAC,CACH;GAEA,KAAK,MAAM,UAAU,SAAS;IAC5B,IAAI,OAAO,WAAW,aAAa;IACnC,MAAM,EAAE,UAAU,UAAU,OAAO;IACnC,KAAK,MAAM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Fn: () => agentId ? client.getMemoryStatus(agentId, requestContext) : null,
|
|
75
|
-
staleTime:
|
|
76
|
-
gcTime:
|
|
75
|
+
staleTime: 3e5,
|
|
76
|
+
gcTime: 6e5,
|
|
77
77
|
retry: false,
|
|
78
78
|
...queryOptions
|
|
79
79
|
});
|
|
@@ -94,8 +94,8 @@ const useMemoryConfig = ({ agentId, requestContext, queryOptions } = {}) => {
|
|
|
94
94
|
agentId,
|
|
95
95
|
requestContext
|
|
96
96
|
}) : null,
|
|
97
|
-
staleTime:
|
|
98
|
-
gcTime:
|
|
97
|
+
staleTime: 3e5,
|
|
98
|
+
gcTime: 6e5,
|
|
99
99
|
retry: false,
|
|
100
100
|
refetchOnWindowFocus: false,
|
|
101
101
|
...queryOptions
|
|
@@ -118,8 +118,8 @@ const useThread = ({ threadId, agentId, requestContext, queryOptions }) => {
|
|
|
118
118
|
threadId,
|
|
119
119
|
agentId
|
|
120
120
|
}).get({ requestContext }),
|
|
121
|
-
staleTime:
|
|
122
|
-
gcTime:
|
|
121
|
+
staleTime: 3e5,
|
|
122
|
+
gcTime: 6e5,
|
|
123
123
|
retry: false,
|
|
124
124
|
refetchOnWindowFocus: false,
|
|
125
125
|
...queryOptions
|
|
@@ -261,8 +261,8 @@ const useObservationalMemoryWithHistory = ({ agentId, resourceId, threadId, isAc
|
|
|
261
261
|
requestContext
|
|
262
262
|
});
|
|
263
263
|
},
|
|
264
|
-
staleTime: isActive ? 1e3 :
|
|
265
|
-
gcTime:
|
|
264
|
+
staleTime: isActive ? 1e3 : 3e4,
|
|
265
|
+
gcTime: 3e5,
|
|
266
266
|
retry: false,
|
|
267
267
|
refetchOnWindowFocus: false,
|
|
268
268
|
refetchInterval: isActive ? 2e3 : false,
|
|
@@ -292,8 +292,8 @@ const useMemoryWithOMStatus = ({ agentId, resourceId, threadId, pollWhenActive =
|
|
|
292
292
|
resourceId,
|
|
293
293
|
threadId
|
|
294
294
|
}) : null,
|
|
295
|
-
staleTime: isActive && pollWhenActive ? 1e3 :
|
|
296
|
-
gcTime:
|
|
295
|
+
staleTime: isActive && pollWhenActive ? 1e3 : 3e4,
|
|
296
|
+
gcTime: 3e5,
|
|
297
297
|
retry: false,
|
|
298
298
|
refetchOnWindowFocus: false,
|
|
299
299
|
refetchInterval: isActive && pollWhenActive ? 2e3 : false,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["useMastraClient","skipToken","useMastraClient","skipToken","useMastraClient","skipToken","useMastraClient"],"sources":["../../../src/hooks/memory/use-memory-thread-messages.ts","../../../src/hooks/memory/use-observational-memory.ts","../../../src/hooks/memory/use-memory-status.ts","../../../src/hooks/memory/use-memory.ts"],"sourcesContent":["import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryThreadMessagesQueryKey = (threadId: string | undefined, page?: number) =>\n ['memory', 'thread', threadId, 'messages', page ?? 0] as const;\n\ntype MemoryThreadMessagesResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['listMessages']>>;\n\nexport function useMemoryThreadMessages<TData = MemoryThreadMessagesResponse>({\n threadId,\n page = 0,\n perPage = 100,\n queryOptions,\n}: {\n threadId: string | undefined;\n page?: number;\n perPage?: number;\n queryOptions?: MastraQueryOptions<MemoryThreadMessagesResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryThreadMessagesQueryKey(threadId, page),\n queryFn: threadId ? () => client.getMemoryThread({ threadId }).listMessages({ page, perPage }) : skipToken,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const observationalMemoryQueryKey = (agentId: string | undefined, threadId: string | undefined) =>\n ['memory', 'observational-memory', agentId, threadId] as const;\n\ntype ObservationalMemoryResponse = Awaited<ReturnType<MastraClient['getObservationalMemory']>>;\n\nexport function useObservationalMemory<TData = ObservationalMemoryResponse>({\n agentId,\n threadId,\n resourceId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId: string | undefined;\n resourceId?: string;\n queryOptions?: MastraQueryOptions<ObservationalMemoryResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: observationalMemoryQueryKey(agentId, threadId),\n queryFn:\n agentId && threadId\n ? () =>\n client.getObservationalMemory({\n agentId,\n threadId,\n resourceId,\n })\n : skipToken,\n // The record is read by several surfaces at once (collapsed memory bar, OM\n // section, detail panel). A short stale window lets a newly mounted consumer\n // reuse the cached record instead of refiring the request; freshness still\n // comes from the explicit refetch on stream finish/observation signals.\n staleTime: 5_000,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryStatusQueryKey = (agentId: string | undefined, threadId?: string) =>\n ['memory', 'status', agentId, threadId] as const;\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>>;\n\nexport function useMemoryStatus<TData = MemoryStatusResponse>({\n agentId,\n threadId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId?: string;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryStatusQueryKey(agentId, threadId),\n queryFn: agentId\n ? () =>\n client.getMemoryStatus(agentId, undefined, {\n threadId,\n })\n : skipToken,\n ...queryOptions,\n });\n}\n","import type { GetObservationalMemoryResponse, GetMemoryStatusResponse, MastraClient } from '@mastra/client-js';\nimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';\nimport type { UseMutationResult, UseQueryResult } from '@tanstack/react-query';\nimport { useState, useEffect } from 'react';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions, MastraQueryOptions } from '../shared/query-options';\n\nexport interface MemorySearchParams {\n lastMessages?: number | false;\n}\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>> | null;\ntype MemoryConfigResponse = Awaited<ReturnType<MastraClient['getMemoryConfig']>> | null;\ntype ThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['get']>>;\ntype ThreadsResponse = Awaited<ReturnType<MastraClient['listMemoryThreads']>>['threads'] | null;\ntype UpdateThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['update']>>;\ntype CloneThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['clone']>>;\ntype SearchMemoryResponse = Awaited<ReturnType<MastraClient['searchMemory']>>;\ntype ThreadVariables = { threadId: string; agentId: string };\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemory = <TData = MemoryStatusResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryStatus(agentId, requestContext) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryConfig = <TData = MemoryConfigResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryConfigResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'config', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryConfig({ agentId, requestContext }) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThread = <TData = ThreadResponse>({\n threadId,\n agentId,\n requestContext,\n queryOptions,\n}: {\n threadId?: string;\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'thread', threadId, agentId, requestContext],\n queryFn: () => client.getMemoryThread({ threadId: threadId!, agentId }).get({ requestContext }),\n staleTime: 5 * 60 * 1000,\n gcTime: 10 * 60 * 1000,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThreads = <TData = ThreadsResponse>({\n resourceId,\n agentId,\n isMemoryEnabled,\n requestContext,\n queryOptions,\n}: {\n resourceId: string;\n agentId: string;\n isMemoryEnabled: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadsResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'threads', resourceId, agentId, requestContext],\n queryFn: async () => {\n if (!isMemoryEnabled) return null;\n const result = await client.listMemoryThreads({ resourceId, agentId, requestContext });\n return result.threads;\n },\n staleTime: 0,\n gcTime: 0,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\nexport type DeleteThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['delete']>>;\n\nexport const useDeleteThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<DeleteThreadResponse, ThreadVariables>;\n} = {}): UseMutationResult<DeleteThreadResponse, Error, ThreadVariables> => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId }: { threadId: string; agentId: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.delete({ requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\nexport const useUpdateThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<UpdateThreadResponse, ThreadVariables & { title: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId, title }: { threadId: string; agentId: string; title: string }) =>\n client.getMemoryThread({ threadId, agentId }).update({ title, agentId, requestContext }),\n onSuccess: () => {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads'] });\n void queryClient.invalidateQueries({ queryKey: ['memory', 'thread'] });\n },\n ...queryOptions,\n });\n};\n\nexport const useMemorySearch = ({\n agentId,\n resourceId,\n threadId,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId: string;\n threadId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<\n SearchMemoryResponse,\n { searchQuery: string; memoryConfig?: MemorySearchParams }\n >;\n}) => {\n const client = useMastraClient();\n return useMutation({\n mutationFn: async ({ searchQuery, memoryConfig }: { searchQuery: string; memoryConfig?: MemorySearchParams }) => {\n return client.searchMemory({ agentId, resourceId, threadId, searchQuery, memoryConfig, requestContext });\n },\n ...queryOptions,\n });\n};\n\nexport const useCloneThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<CloneThreadResponse, ThreadVariables & { title?: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: async ({ threadId, agentId, title }: { threadId: string; agentId: string; title?: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.clone({ title, requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\n/**\n * Hook to fetch Observational Memory data for an agent\n * Returns the current OM record and history for a given resource/thread\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useObservationalMemoryWithHistory = <TData = GetObservationalMemoryResponse | null>({\n agentId,\n resourceId,\n threadId,\n isActive = false,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId?: string;\n threadId?: string;\n isActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetObservationalMemoryResponse | null, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['observational-memory', agentId, resourceId, threadId, requestContext],\n queryFn: async (): Promise<GetObservationalMemoryResponse | null> => {\n if (!resourceId && !threadId) return null;\n return client.getObservationalMemory({\n agentId,\n resourceId,\n threadId,\n requestContext,\n });\n },\n staleTime: isActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n};\n\n/**\n * Hook to get OM-aware memory status\n * Extends useMemory with OM-specific status information\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryWithOMStatus = ({\n agentId,\n resourceId,\n threadId,\n pollWhenActive = true,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n resourceId?: string;\n threadId?: string;\n pollWhenActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetMemoryStatusResponse | null>;\n}) => {\n const client = useMastraClient();\n const [isActive, setIsActive] = useState(false);\n\n const query = useQuery<GetMemoryStatusResponse | null>({\n queryKey: ['memory-status', agentId, resourceId, threadId, requestContext],\n queryFn: () =>\n agentId\n ? client.getMemoryStatus(agentId, requestContext, {\n resourceId,\n threadId,\n })\n : null,\n staleTime: isActive && pollWhenActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive && pollWhenActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n\n // Update isActive state when data changes\n const isObserving = query.data?.observationalMemory?.isObserving;\n const isReflecting = query.data?.observationalMemory?.isReflecting;\n\n useEffect(() => {\n const newIsActive = isObserving || isReflecting || false;\n setIsActive(newIsActive);\n }, [isObserving, isReflecting]);\n\n return query;\n};\n"],"mappings":";;;;;AAMA,MAAa,gCAAgC,UAA8B,SACzE;CAAC;CAAU;CAAU;CAAU;CAAY,QAAQ;AAAC;AAItD,SAAgB,wBAA8D,EAC5E,UACA,OAAO,GACP,UAAU,KACV,gBAM+B;CAC/B,MAAM,SAASA,8BAAAA,gBAAgB;CAE/B,QAAA,GAAA,sBAAA,SAAA,CAAgB;EACd,UAAU,6BAA6B,UAAU,IAAI;EACrD,SAAS,iBAAiB,OAAO,gBAAgB,EAAE,SAAS,CAAC,CAAC,CAAC,aAAa;GAAE;GAAM;EAAQ,CAAC,IAAIC,sBAAAA;EACjG,GAAG;CACL,CAAC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AE9B,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["useMastraClient","useQuery","skipToken","useMastraClient","useQuery","skipToken","useMastraClient","useQuery","skipToken","useMastraClient","useQuery","useQueryClient","useMutation","useState"],"sources":["../../../src/hooks/memory/use-memory-thread-messages.ts","../../../src/hooks/memory/use-observational-memory.ts","../../../src/hooks/memory/use-memory-status.ts","../../../src/hooks/memory/use-memory.ts"],"sourcesContent":["import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryThreadMessagesQueryKey = (threadId: string | undefined, page?: number) =>\n ['memory', 'thread', threadId, 'messages', page ?? 0] as const;\n\ntype MemoryThreadMessagesResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['listMessages']>>;\n\nexport function useMemoryThreadMessages<TData = MemoryThreadMessagesResponse>({\n threadId,\n page = 0,\n perPage = 100,\n queryOptions,\n}: {\n threadId: string | undefined;\n page?: number;\n perPage?: number;\n queryOptions?: MastraQueryOptions<MemoryThreadMessagesResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryThreadMessagesQueryKey(threadId, page),\n queryFn: threadId ? () => client.getMemoryThread({ threadId }).listMessages({ page, perPage }) : skipToken,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const observationalMemoryQueryKey = (agentId: string | undefined, threadId: string | undefined) =>\n ['memory', 'observational-memory', agentId, threadId] as const;\n\ntype ObservationalMemoryResponse = Awaited<ReturnType<MastraClient['getObservationalMemory']>>;\n\nexport function useObservationalMemory<TData = ObservationalMemoryResponse>({\n agentId,\n threadId,\n resourceId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId: string | undefined;\n resourceId?: string;\n queryOptions?: MastraQueryOptions<ObservationalMemoryResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: observationalMemoryQueryKey(agentId, threadId),\n queryFn:\n agentId && threadId\n ? () =>\n client.getObservationalMemory({\n agentId,\n threadId,\n resourceId,\n })\n : skipToken,\n // The record is read by several surfaces at once (collapsed memory bar, OM\n // section, detail panel). A short stale window lets a newly mounted consumer\n // reuse the cached record instead of refiring the request; freshness still\n // comes from the explicit refetch on stream finish/observation signals.\n staleTime: 5_000,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryStatusQueryKey = (agentId: string | undefined, threadId?: string) =>\n ['memory', 'status', agentId, threadId] as const;\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>>;\n\nexport function useMemoryStatus<TData = MemoryStatusResponse>({\n agentId,\n threadId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId?: string;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryStatusQueryKey(agentId, threadId),\n queryFn: agentId\n ? () =>\n client.getMemoryStatus(agentId, undefined, {\n threadId,\n })\n : skipToken,\n ...queryOptions,\n });\n}\n","import type { GetObservationalMemoryResponse, GetMemoryStatusResponse, MastraClient } from '@mastra/client-js';\nimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';\nimport type { UseMutationResult, UseQueryResult } from '@tanstack/react-query';\nimport { useState, useEffect } from 'react';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions, MastraQueryOptions } from '../shared/query-options';\n\nexport interface MemorySearchParams {\n lastMessages?: number | false;\n}\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>> | null;\ntype MemoryConfigResponse = Awaited<ReturnType<MastraClient['getMemoryConfig']>> | null;\ntype ThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['get']>>;\ntype ThreadsResponse = Awaited<ReturnType<MastraClient['listMemoryThreads']>>['threads'] | null;\ntype UpdateThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['update']>>;\ntype CloneThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['clone']>>;\ntype SearchMemoryResponse = Awaited<ReturnType<MastraClient['searchMemory']>>;\ntype ThreadVariables = { threadId: string; agentId: string };\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemory = <TData = MemoryStatusResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryStatus(agentId, requestContext) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryConfig = <TData = MemoryConfigResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryConfigResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'config', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryConfig({ agentId, requestContext }) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThread = <TData = ThreadResponse>({\n threadId,\n agentId,\n requestContext,\n queryOptions,\n}: {\n threadId?: string;\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'thread', threadId, agentId, requestContext],\n queryFn: () => client.getMemoryThread({ threadId: threadId!, agentId }).get({ requestContext }),\n staleTime: 5 * 60 * 1000,\n gcTime: 10 * 60 * 1000,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThreads = <TData = ThreadsResponse>({\n resourceId,\n agentId,\n isMemoryEnabled,\n requestContext,\n queryOptions,\n}: {\n resourceId: string;\n agentId: string;\n isMemoryEnabled: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadsResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'threads', resourceId, agentId, requestContext],\n queryFn: async () => {\n if (!isMemoryEnabled) return null;\n const result = await client.listMemoryThreads({ resourceId, agentId, requestContext });\n return result.threads;\n },\n staleTime: 0,\n gcTime: 0,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\nexport type DeleteThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['delete']>>;\n\nexport const useDeleteThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<DeleteThreadResponse, ThreadVariables>;\n} = {}): UseMutationResult<DeleteThreadResponse, Error, ThreadVariables> => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId }: { threadId: string; agentId: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.delete({ requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\nexport const useUpdateThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<UpdateThreadResponse, ThreadVariables & { title: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId, title }: { threadId: string; agentId: string; title: string }) =>\n client.getMemoryThread({ threadId, agentId }).update({ title, agentId, requestContext }),\n onSuccess: () => {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads'] });\n void queryClient.invalidateQueries({ queryKey: ['memory', 'thread'] });\n },\n ...queryOptions,\n });\n};\n\nexport const useMemorySearch = ({\n agentId,\n resourceId,\n threadId,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId: string;\n threadId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<\n SearchMemoryResponse,\n { searchQuery: string; memoryConfig?: MemorySearchParams }\n >;\n}) => {\n const client = useMastraClient();\n return useMutation({\n mutationFn: async ({ searchQuery, memoryConfig }: { searchQuery: string; memoryConfig?: MemorySearchParams }) => {\n return client.searchMemory({ agentId, resourceId, threadId, searchQuery, memoryConfig, requestContext });\n },\n ...queryOptions,\n });\n};\n\nexport const useCloneThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<CloneThreadResponse, ThreadVariables & { title?: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: async ({ threadId, agentId, title }: { threadId: string; agentId: string; title?: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.clone({ title, requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\n/**\n * Hook to fetch Observational Memory data for an agent\n * Returns the current OM record and history for a given resource/thread\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useObservationalMemoryWithHistory = <TData = GetObservationalMemoryResponse | null>({\n agentId,\n resourceId,\n threadId,\n isActive = false,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId?: string;\n threadId?: string;\n isActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetObservationalMemoryResponse | null, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['observational-memory', agentId, resourceId, threadId, requestContext],\n queryFn: async (): Promise<GetObservationalMemoryResponse | null> => {\n if (!resourceId && !threadId) return null;\n return client.getObservationalMemory({\n agentId,\n resourceId,\n threadId,\n requestContext,\n });\n },\n staleTime: isActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n};\n\n/**\n * Hook to get OM-aware memory status\n * Extends useMemory with OM-specific status information\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryWithOMStatus = ({\n agentId,\n resourceId,\n threadId,\n pollWhenActive = true,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n resourceId?: string;\n threadId?: string;\n pollWhenActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetMemoryStatusResponse | null>;\n}) => {\n const client = useMastraClient();\n const [isActive, setIsActive] = useState(false);\n\n const query = useQuery<GetMemoryStatusResponse | null>({\n queryKey: ['memory-status', agentId, resourceId, threadId, requestContext],\n queryFn: () =>\n agentId\n ? client.getMemoryStatus(agentId, requestContext, {\n resourceId,\n threadId,\n })\n : null,\n staleTime: isActive && pollWhenActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive && pollWhenActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n\n // Update isActive state when data changes\n const isObserving = query.data?.observationalMemory?.isObserving;\n const isReflecting = query.data?.observationalMemory?.isReflecting;\n\n useEffect(() => {\n const newIsActive = isObserving || isReflecting || false;\n setIsActive(newIsActive);\n }, [isObserving, isReflecting]);\n\n return query;\n};\n"],"mappings":";;;;;AAMA,MAAa,gCAAgC,UAA8B,SACzE;CAAC;CAAU;CAAU;CAAU;CAAY,QAAQ;AAAC;AAItD,SAAgB,wBAA8D,EAC5E,UACA,OAAO,GACP,UAAU,KACV,gBAM+B;CAC/B,MAAM,SAASA,8BAAAA,gBAAgB;CAE/B,QAAA,GAAOC,sBAAAA,SAAAA,CAAS;EACd,UAAU,6BAA6B,UAAU,IAAI;EACrD,SAAS,iBAAiB,OAAO,gBAAgB,EAAE,SAAS,CAAC,CAAC,CAAC,aAAa;GAAE;GAAM;EAAQ,CAAC,IAAIC,sBAAAA;EACjG,GAAG;CACL,CAAC;AACH;;;ACvBA,MAAa,+BAA+B,SAA6B,aACvE;CAAC;CAAU;CAAwB;CAAS;AAAQ;AAItD,SAAgB,uBAA4D,EAC1E,SACA,UACA,YACA,gBAM+B;CAC/B,MAAM,SAASC,8BAAAA,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AA,gBAAgB;CAE/B,QAAA,GAAOC,sBAAAA,SAAAA,CAAS;EACd,UAAU;GAAC;GAAwB;GAAS;GAAY;GAAU;EAAc;EAChF,SAAS,YAA4D;GACnE,IAAI,CAAC,cAAc,CAAC,UAAU,OAAO;GACrC,OAAO,OAAO,uBAAuB;IACnC;IACA;IACA;IACA;GACF,CAAC;EACH;EACA,WAAW,WAAW,MAAO;EAC7B,QAAQ;EACR,OAAO;EACP,sBAAsB;EACtB,iBAAiB,WAAW,MAAO;EACnC,kBAAiB,iBAAgB;EACjC,GAAG;CACL,CAAC;AACH;;;;;;;;AASA,MAAa,yBAAyB,EACpC,SACA,YACA,UACA,iBAAiB,MACjB,gBACA,mBAQI;CACJ,MAAM,SAASD,8BAAAA,gBAAgB;CAC/B,MAAM,CAAC,UAAU,gBAAA,GAAeI,MAAAA,SAAAA,CAAS,KAAK;CAE9C,MAAM,SAAA,GAAQH,sBAAAA,SAAAA,CAAyC;EACrD,UAAU;GAAC;GAAiB;GAAS;GAAY;GAAU;EAAc;EACzE,eACE,UACI,OAAO,gBAAgB,SAAS,gBAAgB;GAC9C;GACA;EACF,CAAC,IACD;EACN,WAAW,YAAY,iBAAiB,MAAO;EAC/C,QAAQ;EACR,OAAO;EACP,sBAAsB;EACtB,iBAAiB,YAAY,iBAAiB,MAAO;EACrD,kBAAiB,iBAAgB;EACjC,GAAG;CACL,CAAC;CAGD,MAAM,cAAc,MAAM,MAAM,qBAAqB;CACrD,MAAM,eAAe,MAAM,MAAM,qBAAqB;CAEtD,CAAA,GAAA,MAAA,UAAA,OAAgB;EAEd,YADoB,eAAe,gBAAgB,KAC5B;CACzB,GAAG,CAAC,aAAa,YAAY,CAAC;CAE9B,OAAO;AACT"}
|
|
@@ -71,8 +71,8 @@ const useMemory = ({ agentId, requestContext, queryOptions } = {}) => {
|
|
|
71
71
|
requestContext
|
|
72
72
|
],
|
|
73
73
|
queryFn: () => agentId ? client.getMemoryStatus(agentId, requestContext) : null,
|
|
74
|
-
staleTime:
|
|
75
|
-
gcTime:
|
|
74
|
+
staleTime: 3e5,
|
|
75
|
+
gcTime: 6e5,
|
|
76
76
|
retry: false,
|
|
77
77
|
...queryOptions
|
|
78
78
|
});
|
|
@@ -93,8 +93,8 @@ const useMemoryConfig = ({ agentId, requestContext, queryOptions } = {}) => {
|
|
|
93
93
|
agentId,
|
|
94
94
|
requestContext
|
|
95
95
|
}) : null,
|
|
96
|
-
staleTime:
|
|
97
|
-
gcTime:
|
|
96
|
+
staleTime: 3e5,
|
|
97
|
+
gcTime: 6e5,
|
|
98
98
|
retry: false,
|
|
99
99
|
refetchOnWindowFocus: false,
|
|
100
100
|
...queryOptions
|
|
@@ -117,8 +117,8 @@ const useThread = ({ threadId, agentId, requestContext, queryOptions }) => {
|
|
|
117
117
|
threadId,
|
|
118
118
|
agentId
|
|
119
119
|
}).get({ requestContext }),
|
|
120
|
-
staleTime:
|
|
121
|
-
gcTime:
|
|
120
|
+
staleTime: 3e5,
|
|
121
|
+
gcTime: 6e5,
|
|
122
122
|
retry: false,
|
|
123
123
|
refetchOnWindowFocus: false,
|
|
124
124
|
...queryOptions
|
|
@@ -260,8 +260,8 @@ const useObservationalMemoryWithHistory = ({ agentId, resourceId, threadId, isAc
|
|
|
260
260
|
requestContext
|
|
261
261
|
});
|
|
262
262
|
},
|
|
263
|
-
staleTime: isActive ? 1e3 :
|
|
264
|
-
gcTime:
|
|
263
|
+
staleTime: isActive ? 1e3 : 3e4,
|
|
264
|
+
gcTime: 3e5,
|
|
265
265
|
retry: false,
|
|
266
266
|
refetchOnWindowFocus: false,
|
|
267
267
|
refetchInterval: isActive ? 2e3 : false,
|
|
@@ -291,8 +291,8 @@ const useMemoryWithOMStatus = ({ agentId, resourceId, threadId, pollWhenActive =
|
|
|
291
291
|
resourceId,
|
|
292
292
|
threadId
|
|
293
293
|
}) : null,
|
|
294
|
-
staleTime: isActive && pollWhenActive ? 1e3 :
|
|
295
|
-
gcTime:
|
|
294
|
+
staleTime: isActive && pollWhenActive ? 1e3 : 3e4,
|
|
295
|
+
gcTime: 3e5,
|
|
296
296
|
retry: false,
|
|
297
297
|
refetchOnWindowFocus: false,
|
|
298
298
|
refetchInterval: isActive && pollWhenActive ? 2e3 : false,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../src/hooks/memory/use-memory-thread-messages.ts","../../../src/hooks/memory/use-observational-memory.ts","../../../src/hooks/memory/use-memory-status.ts","../../../src/hooks/memory/use-memory.ts"],"sourcesContent":["import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryThreadMessagesQueryKey = (threadId: string | undefined, page?: number) =>\n ['memory', 'thread', threadId, 'messages', page ?? 0] as const;\n\ntype MemoryThreadMessagesResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['listMessages']>>;\n\nexport function useMemoryThreadMessages<TData = MemoryThreadMessagesResponse>({\n threadId,\n page = 0,\n perPage = 100,\n queryOptions,\n}: {\n threadId: string | undefined;\n page?: number;\n perPage?: number;\n queryOptions?: MastraQueryOptions<MemoryThreadMessagesResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryThreadMessagesQueryKey(threadId, page),\n queryFn: threadId ? () => client.getMemoryThread({ threadId }).listMessages({ page, perPage }) : skipToken,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const observationalMemoryQueryKey = (agentId: string | undefined, threadId: string | undefined) =>\n ['memory', 'observational-memory', agentId, threadId] as const;\n\ntype ObservationalMemoryResponse = Awaited<ReturnType<MastraClient['getObservationalMemory']>>;\n\nexport function useObservationalMemory<TData = ObservationalMemoryResponse>({\n agentId,\n threadId,\n resourceId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId: string | undefined;\n resourceId?: string;\n queryOptions?: MastraQueryOptions<ObservationalMemoryResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: observationalMemoryQueryKey(agentId, threadId),\n queryFn:\n agentId && threadId\n ? () =>\n client.getObservationalMemory({\n agentId,\n threadId,\n resourceId,\n })\n : skipToken,\n // The record is read by several surfaces at once (collapsed memory bar, OM\n // section, detail panel). A short stale window lets a newly mounted consumer\n // reuse the cached record instead of refiring the request; freshness still\n // comes from the explicit refetch on stream finish/observation signals.\n staleTime: 5_000,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryStatusQueryKey = (agentId: string | undefined, threadId?: string) =>\n ['memory', 'status', agentId, threadId] as const;\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>>;\n\nexport function useMemoryStatus<TData = MemoryStatusResponse>({\n agentId,\n threadId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId?: string;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryStatusQueryKey(agentId, threadId),\n queryFn: agentId\n ? () =>\n client.getMemoryStatus(agentId, undefined, {\n threadId,\n })\n : skipToken,\n ...queryOptions,\n });\n}\n","import type { GetObservationalMemoryResponse, GetMemoryStatusResponse, MastraClient } from '@mastra/client-js';\nimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';\nimport type { UseMutationResult, UseQueryResult } from '@tanstack/react-query';\nimport { useState, useEffect } from 'react';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions, MastraQueryOptions } from '../shared/query-options';\n\nexport interface MemorySearchParams {\n lastMessages?: number | false;\n}\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>> | null;\ntype MemoryConfigResponse = Awaited<ReturnType<MastraClient['getMemoryConfig']>> | null;\ntype ThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['get']>>;\ntype ThreadsResponse = Awaited<ReturnType<MastraClient['listMemoryThreads']>>['threads'] | null;\ntype UpdateThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['update']>>;\ntype CloneThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['clone']>>;\ntype SearchMemoryResponse = Awaited<ReturnType<MastraClient['searchMemory']>>;\ntype ThreadVariables = { threadId: string; agentId: string };\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemory = <TData = MemoryStatusResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryStatus(agentId, requestContext) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryConfig = <TData = MemoryConfigResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryConfigResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'config', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryConfig({ agentId, requestContext }) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThread = <TData = ThreadResponse>({\n threadId,\n agentId,\n requestContext,\n queryOptions,\n}: {\n threadId?: string;\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'thread', threadId, agentId, requestContext],\n queryFn: () => client.getMemoryThread({ threadId: threadId!, agentId }).get({ requestContext }),\n staleTime: 5 * 60 * 1000,\n gcTime: 10 * 60 * 1000,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThreads = <TData = ThreadsResponse>({\n resourceId,\n agentId,\n isMemoryEnabled,\n requestContext,\n queryOptions,\n}: {\n resourceId: string;\n agentId: string;\n isMemoryEnabled: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadsResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'threads', resourceId, agentId, requestContext],\n queryFn: async () => {\n if (!isMemoryEnabled) return null;\n const result = await client.listMemoryThreads({ resourceId, agentId, requestContext });\n return result.threads;\n },\n staleTime: 0,\n gcTime: 0,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\nexport type DeleteThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['delete']>>;\n\nexport const useDeleteThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<DeleteThreadResponse, ThreadVariables>;\n} = {}): UseMutationResult<DeleteThreadResponse, Error, ThreadVariables> => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId }: { threadId: string; agentId: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.delete({ requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\nexport const useUpdateThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<UpdateThreadResponse, ThreadVariables & { title: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId, title }: { threadId: string; agentId: string; title: string }) =>\n client.getMemoryThread({ threadId, agentId }).update({ title, agentId, requestContext }),\n onSuccess: () => {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads'] });\n void queryClient.invalidateQueries({ queryKey: ['memory', 'thread'] });\n },\n ...queryOptions,\n });\n};\n\nexport const useMemorySearch = ({\n agentId,\n resourceId,\n threadId,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId: string;\n threadId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<\n SearchMemoryResponse,\n { searchQuery: string; memoryConfig?: MemorySearchParams }\n >;\n}) => {\n const client = useMastraClient();\n return useMutation({\n mutationFn: async ({ searchQuery, memoryConfig }: { searchQuery: string; memoryConfig?: MemorySearchParams }) => {\n return client.searchMemory({ agentId, resourceId, threadId, searchQuery, memoryConfig, requestContext });\n },\n ...queryOptions,\n });\n};\n\nexport const useCloneThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<CloneThreadResponse, ThreadVariables & { title?: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: async ({ threadId, agentId, title }: { threadId: string; agentId: string; title?: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.clone({ title, requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\n/**\n * Hook to fetch Observational Memory data for an agent\n * Returns the current OM record and history for a given resource/thread\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useObservationalMemoryWithHistory = <TData = GetObservationalMemoryResponse | null>({\n agentId,\n resourceId,\n threadId,\n isActive = false,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId?: string;\n threadId?: string;\n isActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetObservationalMemoryResponse | null, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['observational-memory', agentId, resourceId, threadId, requestContext],\n queryFn: async (): Promise<GetObservationalMemoryResponse | null> => {\n if (!resourceId && !threadId) return null;\n return client.getObservationalMemory({\n agentId,\n resourceId,\n threadId,\n requestContext,\n });\n },\n staleTime: isActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n};\n\n/**\n * Hook to get OM-aware memory status\n * Extends useMemory with OM-specific status information\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryWithOMStatus = ({\n agentId,\n resourceId,\n threadId,\n pollWhenActive = true,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n resourceId?: string;\n threadId?: string;\n pollWhenActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetMemoryStatusResponse | null>;\n}) => {\n const client = useMastraClient();\n const [isActive, setIsActive] = useState(false);\n\n const query = useQuery<GetMemoryStatusResponse | null>({\n queryKey: ['memory-status', agentId, resourceId, threadId, requestContext],\n queryFn: () =>\n agentId\n ? client.getMemoryStatus(agentId, requestContext, {\n resourceId,\n threadId,\n })\n : null,\n staleTime: isActive && pollWhenActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive && pollWhenActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n\n // Update isActive state when data changes\n const isObserving = query.data?.observationalMemory?.isObserving;\n const isReflecting = query.data?.observationalMemory?.isReflecting;\n\n useEffect(() => {\n const newIsActive = isObserving || isReflecting || false;\n setIsActive(newIsActive);\n }, [isObserving, isReflecting]);\n\n return query;\n};\n"],"mappings":";;;;AAMA,MAAa,gCAAgC,UAA8B,SACzE;CAAC;CAAU;CAAU;CAAU;CAAY,QAAQ;AAAC;AAItD,SAAgB,wBAA8D,EAC5E,UACA,OAAO,GACP,UAAU,KACV,gBAM+B;CAC/B,MAAM,SAAS,gBAAgB;CAE/B,OAAO,SAAS;EACd,UAAU,6BAA6B,UAAU,IAAI;EACrD,SAAS,iBAAiB,OAAO,gBAAgB,EAAE,SAAS,CAAC,CAAC,CAAC,aAAa;GAAE;GAAM;EAAQ,CAAC,IAAI;EACjG,GAAG;CACL,CAAC;AACH;;;ACvBA,MAAa,+BAA+B,SAA6B,aACvE;CAAC;CAAU;CAAwB;CAAS;AAAQ;AAItD,SAAgB,uBAA4D,EAC1E,SACA,UACA,YACA,gBAM+B;CAC/B,MAAM,SAAS,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;;;;AASA,MAAa,yBAAyB,EACpC,SACA,YACA,UACA,iBAAiB,MACjB,gBACA,mBAQI;CACJ,MAAM,SAAS,gBAAgB;CAC/B,MAAM,CAAC,UAAU,eAAe,SAAS,KAAK;CAE9C,MAAM,QAAQ,SAAyC;EACrD,UAAU;GAAC;GAAiB;GAAS;GAAY;GAAU;EAAc;EACzE,eACE,UACI,OAAO,gBAAgB,SAAS,gBAAgB;GAC9C;GACA;EACF,CAAC,IACD;EACN,WAAW,YAAY,iBAAiB,MAAO,KAAK;EACpD,QAAQ,MAAS;EACjB,OAAO;EACP,sBAAsB;EACtB,iBAAiB,YAAY,iBAAiB,MAAO;EACrD,kBAAiB,iBAAgB;EACjC,GAAG;CACL,CAAC;CAGD,MAAM,cAAc,MAAM,MAAM,qBAAqB;CACrD,MAAM,eAAe,MAAM,MAAM,qBAAqB;CAEtD,gBAAgB;EAEd,YADoB,eAAe,gBAAgB,KAC5B;CACzB,GAAG,CAAC,aAAa,YAAY,CAAC;CAE9B,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/hooks/memory/use-memory-thread-messages.ts","../../../src/hooks/memory/use-observational-memory.ts","../../../src/hooks/memory/use-memory-status.ts","../../../src/hooks/memory/use-memory.ts"],"sourcesContent":["import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryThreadMessagesQueryKey = (threadId: string | undefined, page?: number) =>\n ['memory', 'thread', threadId, 'messages', page ?? 0] as const;\n\ntype MemoryThreadMessagesResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['listMessages']>>;\n\nexport function useMemoryThreadMessages<TData = MemoryThreadMessagesResponse>({\n threadId,\n page = 0,\n perPage = 100,\n queryOptions,\n}: {\n threadId: string | undefined;\n page?: number;\n perPage?: number;\n queryOptions?: MastraQueryOptions<MemoryThreadMessagesResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryThreadMessagesQueryKey(threadId, page),\n queryFn: threadId ? () => client.getMemoryThread({ threadId }).listMessages({ page, perPage }) : skipToken,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const observationalMemoryQueryKey = (agentId: string | undefined, threadId: string | undefined) =>\n ['memory', 'observational-memory', agentId, threadId] as const;\n\ntype ObservationalMemoryResponse = Awaited<ReturnType<MastraClient['getObservationalMemory']>>;\n\nexport function useObservationalMemory<TData = ObservationalMemoryResponse>({\n agentId,\n threadId,\n resourceId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId: string | undefined;\n resourceId?: string;\n queryOptions?: MastraQueryOptions<ObservationalMemoryResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: observationalMemoryQueryKey(agentId, threadId),\n queryFn:\n agentId && threadId\n ? () =>\n client.getObservationalMemory({\n agentId,\n threadId,\n resourceId,\n })\n : skipToken,\n // The record is read by several surfaces at once (collapsed memory bar, OM\n // section, detail panel). A short stale window lets a newly mounted consumer\n // reuse the cached record instead of refiring the request; freshness still\n // comes from the explicit refetch on stream finish/observation signals.\n staleTime: 5_000,\n ...queryOptions,\n });\n}\n","import type { MastraClient } from '@mastra/client-js';\nimport type { UseQueryResult } from '@tanstack/react-query';\nimport { skipToken, useQuery } from '@tanstack/react-query';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraQueryOptions } from '../shared/query-options';\n\nexport const memoryStatusQueryKey = (agentId: string | undefined, threadId?: string) =>\n ['memory', 'status', agentId, threadId] as const;\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>>;\n\nexport function useMemoryStatus<TData = MemoryStatusResponse>({\n agentId,\n threadId,\n queryOptions,\n}: {\n agentId: string | undefined;\n threadId?: string;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n}): UseQueryResult<TData, Error> {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: memoryStatusQueryKey(agentId, threadId),\n queryFn: agentId\n ? () =>\n client.getMemoryStatus(agentId, undefined, {\n threadId,\n })\n : skipToken,\n ...queryOptions,\n });\n}\n","import type { GetObservationalMemoryResponse, GetMemoryStatusResponse, MastraClient } from '@mastra/client-js';\nimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';\nimport type { UseMutationResult, UseQueryResult } from '@tanstack/react-query';\nimport { useState, useEffect } from 'react';\nimport { useMastraClient } from '../../mastra-client-context';\nimport type { MastraMutationOptions, MastraQueryOptions } from '../shared/query-options';\n\nexport interface MemorySearchParams {\n lastMessages?: number | false;\n}\n\ntype MemoryStatusResponse = Awaited<ReturnType<MastraClient['getMemoryStatus']>> | null;\ntype MemoryConfigResponse = Awaited<ReturnType<MastraClient['getMemoryConfig']>> | null;\ntype ThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['get']>>;\ntype ThreadsResponse = Awaited<ReturnType<MastraClient['listMemoryThreads']>>['threads'] | null;\ntype UpdateThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['update']>>;\ntype CloneThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['clone']>>;\ntype SearchMemoryResponse = Awaited<ReturnType<MastraClient['searchMemory']>>;\ntype ThreadVariables = { threadId: string; agentId: string };\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemory = <TData = MemoryStatusResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryStatusResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryStatus(agentId, requestContext) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryConfig = <TData = MemoryConfigResponse>({\n agentId,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<MemoryConfigResponse, TData>;\n} = {}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'config', agentId, requestContext],\n queryFn: () => (agentId ? client.getMemoryConfig({ agentId, requestContext }) : null),\n staleTime: 5 * 60 * 1000, // 5 minutes\n gcTime: 10 * 60 * 1000, // 10 minutes\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThread = <TData = ThreadResponse>({\n threadId,\n agentId,\n requestContext,\n queryOptions,\n}: {\n threadId?: string;\n agentId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'thread', threadId, agentId, requestContext],\n queryFn: () => client.getMemoryThread({ threadId: threadId!, agentId }).get({ requestContext }),\n staleTime: 5 * 60 * 1000,\n gcTime: 10 * 60 * 1000,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\n/**\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useThreads = <TData = ThreadsResponse>({\n resourceId,\n agentId,\n isMemoryEnabled,\n requestContext,\n queryOptions,\n}: {\n resourceId: string;\n agentId: string;\n isMemoryEnabled: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<ThreadsResponse, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['memory', 'threads', resourceId, agentId, requestContext],\n queryFn: async () => {\n if (!isMemoryEnabled) return null;\n const result = await client.listMemoryThreads({ resourceId, agentId, requestContext });\n return result.threads;\n },\n staleTime: 0,\n gcTime: 0,\n retry: false,\n refetchOnWindowFocus: false,\n ...queryOptions,\n });\n};\n\nexport type DeleteThreadResponse = Awaited<ReturnType<ReturnType<MastraClient['getMemoryThread']>['delete']>>;\n\nexport const useDeleteThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<DeleteThreadResponse, ThreadVariables>;\n} = {}): UseMutationResult<DeleteThreadResponse, Error, ThreadVariables> => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId }: { threadId: string; agentId: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.delete({ requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\nexport const useUpdateThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<UpdateThreadResponse, ThreadVariables & { title: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: ({ threadId, agentId, title }: { threadId: string; agentId: string; title: string }) =>\n client.getMemoryThread({ threadId, agentId }).update({ title, agentId, requestContext }),\n onSuccess: () => {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads'] });\n void queryClient.invalidateQueries({ queryKey: ['memory', 'thread'] });\n },\n ...queryOptions,\n });\n};\n\nexport const useMemorySearch = ({\n agentId,\n resourceId,\n threadId,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId: string;\n threadId?: string;\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<\n SearchMemoryResponse,\n { searchQuery: string; memoryConfig?: MemorySearchParams }\n >;\n}) => {\n const client = useMastraClient();\n return useMutation({\n mutationFn: async ({ searchQuery, memoryConfig }: { searchQuery: string; memoryConfig?: MemorySearchParams }) => {\n return client.searchMemory({ agentId, resourceId, threadId, searchQuery, memoryConfig, requestContext });\n },\n ...queryOptions,\n });\n};\n\nexport const useCloneThread = ({\n requestContext,\n queryOptions,\n}: {\n requestContext?: Record<string, any>;\n queryOptions?: MastraMutationOptions<CloneThreadResponse, ThreadVariables & { title?: string }>;\n} = {}) => {\n const client = useMastraClient();\n const queryClient = useQueryClient();\n\n return useMutation({\n mutationFn: async ({ threadId, agentId, title }: { threadId: string; agentId: string; title?: string }) => {\n const thread = client.getMemoryThread({ threadId, agentId });\n return thread.clone({ title, requestContext });\n },\n onSuccess: (_, variables) => {\n const { agentId } = variables;\n if (agentId) {\n void queryClient.invalidateQueries({ queryKey: ['memory', 'threads', agentId, agentId] });\n }\n },\n ...queryOptions,\n });\n};\n\n/**\n * Hook to fetch Observational Memory data for an agent\n * Returns the current OM record and history for a given resource/thread\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useObservationalMemoryWithHistory = <TData = GetObservationalMemoryResponse | null>({\n agentId,\n resourceId,\n threadId,\n isActive = false,\n requestContext,\n queryOptions,\n}: {\n agentId: string;\n resourceId?: string;\n threadId?: string;\n isActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetObservationalMemoryResponse | null, TData>;\n}): UseQueryResult<TData, Error> => {\n const client = useMastraClient();\n\n return useQuery({\n queryKey: ['observational-memory', agentId, resourceId, threadId, requestContext],\n queryFn: async (): Promise<GetObservationalMemoryResponse | null> => {\n if (!resourceId && !threadId) return null;\n return client.getObservationalMemory({\n agentId,\n resourceId,\n threadId,\n requestContext,\n });\n },\n staleTime: isActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n};\n\n/**\n * Hook to get OM-aware memory status\n * Extends useMemory with OM-specific status information\n * Polls more frequently when observing/reflecting is in progress\n *\n * Does not guard on empty ids; pass `queryOptions: { enabled }` to skip the fetch.\n */\nexport const useMemoryWithOMStatus = ({\n agentId,\n resourceId,\n threadId,\n pollWhenActive = true,\n requestContext,\n queryOptions,\n}: {\n agentId?: string;\n resourceId?: string;\n threadId?: string;\n pollWhenActive?: boolean;\n requestContext?: Record<string, any>;\n queryOptions?: MastraQueryOptions<GetMemoryStatusResponse | null>;\n}) => {\n const client = useMastraClient();\n const [isActive, setIsActive] = useState(false);\n\n const query = useQuery<GetMemoryStatusResponse | null>({\n queryKey: ['memory-status', agentId, resourceId, threadId, requestContext],\n queryFn: () =>\n agentId\n ? client.getMemoryStatus(agentId, requestContext, {\n resourceId,\n threadId,\n })\n : null,\n staleTime: isActive && pollWhenActive ? 1000 : 30 * 1000, // 1 second when active, 30 seconds otherwise\n gcTime: 5 * 60 * 1000, // 5 minutes\n retry: false,\n refetchOnWindowFocus: false,\n refetchInterval: isActive && pollWhenActive ? 2000 : false, // Poll every 2 seconds when active\n placeholderData: previousData => previousData, // Keep previous data during refetch to prevent skeleton flash\n ...queryOptions,\n });\n\n // Update isActive state when data changes\n const isObserving = query.data?.observationalMemory?.isObserving;\n const isReflecting = query.data?.observationalMemory?.isReflecting;\n\n useEffect(() => {\n const newIsActive = isObserving || isReflecting || false;\n setIsActive(newIsActive);\n }, [isObserving, isReflecting]);\n\n return query;\n};\n"],"mappings":";;;;AAMA,MAAa,gCAAgC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qCAAoF,EAC/F,SACA,YACA,UACA,WAAW,OACX,gBACA,mBAQkC;CAClC,MAAM,SAAS,gBAAgB;CAE/B,OAAO,SAAS;EACd,UAAU;GAAC;GAAwB;GAAS;GAAY;GAAU;EAAc;EAChF,SAAS,YAA4D;GACnE,IAAI,CAAC,cAAc,CAAC,UAAU,OAAO;GACrC,OAAO,OAAO,uBAAuB;IACnC;IACA;IACA;IACA;GACF,CAAC;EACH;EACA,WAAW,WAAW,MAAO;EAC7B,QAAQ;EACR,OAAO;EACP,sBAAsB;EACtB,iBAAiB,WAAW,MAAO;EACnC,kBAAiB,iBAAgB;EACjC,GAAG;CACL,CAAC;AACH;;;;;;;;AASA,MAAa,yBAAyB,EACpC,SACA,YACA,UACA,iBAAiB,MACjB,gBACA,mBAQI;CACJ,MAAM,SAAS,gBAAgB;CAC/B,MAAM,CAAC,UAAU,eAAe,SAAS,KAAK;CAE9C,MAAM,QAAQ,SAAyC;EACrD,UAAU;GAAC;GAAiB;GAAS;GAAY;GAAU;EAAc;EACzE,eACE,UACI,OAAO,gBAAgB,SAAS,gBAAgB;GAC9C;GACA;EACF,CAAC,IACD;EACN,WAAW,YAAY,iBAAiB,MAAO;EAC/C,QAAQ;EACR,OAAO;EACP,sBAAsB;EACtB,iBAAiB,YAAY,iBAAiB,MAAO;EACrD,kBAAiB,iBAAgB;EACjC,GAAG;CACL,CAAC;CAGD,MAAM,cAAc,MAAM,MAAM,qBAAqB;CACrD,MAAM,eAAe,MAAM,MAAM,qBAAqB;CAEtD,gBAAgB;EAEd,YADoB,eAAe,gBAAgB,KAC5B;CACzB,GAAG,CAAC,aAAa,YAAY,CAAC;CAE9B,OAAO;AACT"}
|
|
@@ -4,13 +4,13 @@ const require_map = require("../../map-wjmL1y5O.cjs");
|
|
|
4
4
|
let react = require("react");
|
|
5
5
|
let _tanstack_react_query = require("@tanstack/react-query");
|
|
6
6
|
//#region src/hooks/metrics/metrics-query-filters.ts
|
|
7
|
-
const DEFAULT_WINDOW_MS =
|
|
7
|
+
const DEFAULT_WINDOW_MS = 864e5;
|
|
8
8
|
const PRESET_MS = {
|
|
9
9
|
"24h": DEFAULT_WINDOW_MS,
|
|
10
|
-
"3d":
|
|
11
|
-
"7d":
|
|
12
|
-
"14d":
|
|
13
|
-
"30d":
|
|
10
|
+
"3d": 2592e5,
|
|
11
|
+
"7d": 6048e5,
|
|
12
|
+
"14d": 12096e5,
|
|
13
|
+
"30d": 2592e6
|
|
14
14
|
};
|
|
15
15
|
function buildTimestamp(preset, customRange, anchor) {
|
|
16
16
|
if (preset !== "custom") {
|
|
@@ -161,9 +161,9 @@ function formatShortDate(value, { locale, now, timeZone } = {}) {
|
|
|
161
161
|
return getFormatter(calendarDate(date, timeZone).year === calendarDate(reference, timeZone).year ? "dayMonth" : "dayMonthYear", locale, timeZone).format(date);
|
|
162
162
|
}
|
|
163
163
|
const LONG_RELATIVE_UNITS = [
|
|
164
|
-
["year",
|
|
165
|
-
["month",
|
|
166
|
-
["week",
|
|
164
|
+
["year", 31536e3],
|
|
165
|
+
["month", 2592e3],
|
|
166
|
+
["week", 604800],
|
|
167
167
|
["day", 86400],
|
|
168
168
|
["hour", 3600],
|
|
169
169
|
["minute", 60]
|
|
@@ -213,7 +213,7 @@ function formatDate(value, preset, options = {}) {
|
|
|
213
213
|
unit: "h"
|
|
214
214
|
},
|
|
215
215
|
{
|
|
216
|
-
limit:
|
|
216
|
+
limit: 6048e5,
|
|
217
217
|
size: 864e5,
|
|
218
218
|
unit: "d"
|
|
219
219
|
}
|
|
@@ -232,7 +232,7 @@ function formatDate(value, preset, options = {}) {
|
|
|
232
232
|
}
|
|
233
233
|
//#endregion
|
|
234
234
|
//#region src/hooks/metrics/metrics-interval.ts
|
|
235
|
-
const HOURLY_MAX_RANGE_MS =
|
|
235
|
+
const HOURLY_MAX_RANGE_MS = 1728e5;
|
|
236
236
|
/** Hourly buckets up to 48h, daily buckets beyond, so long ranges stay readable. */
|
|
237
237
|
function chooseMetricsInterval({ start, end }) {
|
|
238
238
|
return end.getTime() - start.getTime() <= HOURLY_MAX_RANGE_MS ? "1h" : "1d";
|
|
@@ -583,7 +583,9 @@ function useScoresMetrics(params) {
|
|
|
583
583
|
const series = timeSeriesResults[i]?.series ?? [];
|
|
584
584
|
for (const s of series) for (const point of s.points) {
|
|
585
585
|
const ts = new Date(point.timestamp);
|
|
586
|
-
const
|
|
586
|
+
const hourKey = ts.getHours() * 60 + ts.getMinutes();
|
|
587
|
+
const scorerMap = require_map.getOrCreate(hourBuckets, hourKey, () => /* @__PURE__ */ new Map());
|
|
588
|
+
const acc = require_map.getOrCreate(scorerMap, scorerId, () => ({
|
|
587
589
|
sum: 0,
|
|
588
590
|
count: 0
|
|
589
591
|
}));
|