Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

1.3 KB
Raw Blame
import { useMemo } from 'react'
import { useAppState } from '@/hooks/useAppState'
import { getProviderMeta, useWebSearchConfig } from '@/hooks/useWebSearchConfig'
import { resolveToolOrigin, type ToolOrigin } from '@/lib/toolOrigin'
import { AGENT_TOOL_NAMES, isRecordedShellCall } from '@/lib/agentTools'

/**
 * Resolve which family a tool call belongs to. Each selector returns a
 * primitive so a refreshed-but-equivalent tool list does not re-render every
 * card on screen.
 */
export const useToolOrigin = (toolName: string): ToolOrigin | undefined => {
  const mcpServer = useAppState(
    (s) => s.tools.find((tool) => tool.name === toolName)?.server
  )
  const isRagTool = useAppState((s) => s.ragToolNames.has(toolName))
  const isAgentTool =
    AGENT_TOOL_NAMES.has(toolName) || isRecordedShellCall(toolName)
  const webSearchProviderLabel = useWebSearchConfig(
    (s) => getProviderMeta(s.searchProvider).label
  )
  const webSearchEnabled = useWebSearchConfig((s) => s.webSearchEnabled)

  return useMemo(
    () =>
      resolveToolOrigin(toolName, {
        mcpServer,
        isRagTool,
        isAgentTool,
        webSearchProviderLabel,
        webSearchEnabled,
      }),
    [
      toolName,
      mcpServer,
      isRagTool,
      isAgentTool,
      webSearchProviderLabel,
      webSearchEnabled,
    ]
  )
}