npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dify-chat/core

v0.6.7

Published

Dify Chat 的核心包

Readme

@dify-chat/core

version NPM Last Update NPM Downloads

@dify-chat/coreDify Chat 项目中的核心包,它提供了应用、对话等全局上下文的注入和获取功能。

下面将介绍如何在你的应用中集成和使用它。

安装

通过 npm/yarn/pnpm 安装:

# npm
npm install @dify-chat/core
# yarn
yarn add @dify-chat/core
# pnpm
pnpm add @dify-chat/core

使用方法

全局上下文

AppContext

AppContext 是应用上下文,提供了当前应用配置的获取和更新功能。

AppContextProvider

在应用切换功能的最上层组件中使用 AppContextProvider 提供应用上下文:

import { AppContextProvider, ICurrentApp } from '@dify-chat/core';
import { createDifyApiInstance } from '@dify-chat/api';
import { generateUuidV4 } from '@dify-chat/helpers'

const YourChatComponent = () => {

  const { user } = useDifyChat();
  const [appList, setAppList] = useState<ICurrentApp[]>([])

  // 实现获取应用列表的逻辑
  const getAppList = async () => {
    setAppList([...])
  }

  const [appLoading, setAppLoading] = useState(true)
  const [ currentApp, setCurrentApp ] = useState<ICurrentApp[]>([]);
  const [currentAppId, setCurrentAppId] = useState('')
  const [difyApi] = useState(() => createDifyApiInstance({
    user,
    apiBase: '',
    apiKey: '',
  }))

  // 定义获取应用参数的函数
  const getAppInfo = async() => {
    // 先更新 difyApi 的参数
    difyApi.updateOptions({
      user,
      apiBase: newApp.requestConfig.apiBase,
      apiKey: newApp.requestConfig.apiKey,
    })
    setAppLoading(true)
    // 根据新的 currentAppId 获取新的应用信息
    const appConfig = appList.find(item => item.id === currentAppId)
    const difyAppInfo = await difyApi.getAppInfo()
		const appParameters = await getAppParameters(difyApi)
    setAppLoading(false)
    setCurrentApp({
			config: {
				id: generateUuidV4(),
				info: difyAppInfo,
        requestConfig: appConfig.requestConfig,
        answerForm: appConfig.answerForm,
			},
			parameters: appParameters,
		})
  }

  // 初始化时获取应用列表
  useEffect(()=>{
    getAppList()
  }, [])

  // 监听 currentAppId 变化,更新当前应用配置
  useEffect(() => {
    updateAppInfo()
  }, [currentAppId])

  return (
    <AppContextProvider
			value={{
				appLoading,
				currentAppId,
				setCurrentAppId,
				currentApp: currentApp,
				setCurrentApp,
			}}
		>
      Your Chat Inner Component
      <Button onClick={()=>setCurrentAppId('new-app-id')}>切换应用</Button>
    </>
  )
}

useAppContext hook

在你的子组件中使用 useAppContext 钩子获取应用上下文:

import { useAppContext } from '@dify-chat/core'

const YourInnerComponent = () => {
  const { currentApp, currentAppId } = useAppContext()
  console.log(`当前应用ID:${currentAppId}`, `当前应用:${currentApp}`)
}

对话上下文

ConversationContext 是对话上下文,提供了对话相关的功能,包括获取/更新对话列表、获取/更新当前对话 ID、获取当前对话信息等。

ConversationContextProvider

在具备对话切换功能的最上层组件中使用 ConversationContextProvider 提供对话上下文:

import { ConversationsContextProvider } from '@dify-chat/core';

const YourChatComponent = () => {
  const { user } = useDifyChat();
  const [conversations, setConversations] = useState([])
  const [currentConversationId, setCurrentConversationId] = useState('')

  // 实现获取对话列表的逻辑
  const listConversations = async () => {
    setConversations([...])
  }

  useEffect(()=>{
    listConversations()
  }, [])

  return (
    <ConversationsContextProvider value={{
      conversations,
      setConversations,
      currentConversationId,
      setCurrentConversationId,
    }}>
      <YourInnerComponent />
    </ConversationContextProvider>
  )
}

useConversationsContext hook

在你的子组件中使用 useConversationsContext 钩子获取对话上下文:

import { useConversationsContext } from '@dify-chat/core'

const YourInnerComponent = () => {
  const { conversations, currentConversationId } = useConversationsContext()
  console.log(`当前对话ID:${currentConversationId}`, `对话列表:${conversations}`)
}