Skip to content

feat:集成Dify #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions typescript-sdk/apps/dojo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"@ag-ui/agno": "workspace:*",
"@ag-ui/dify": "workspace:*",
"@ag-ui/langgraph": "workspace:*",
"@ag-ui/mastra": "workspace:*",
"@ag-ui/middleware-starter": "workspace:*",
Expand Down
31 changes: 31 additions & 0 deletions typescript-sdk/apps/dojo/src/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { VercelAISDKAgent } from "@ag-ui/vercel-ai-sdk";
import { openai } from "@ai-sdk/openai";
import { LangGraphAgent } from "@ag-ui/langgraph";
import { AgnoAgent } from "@ag-ui/agno";
import { DifyAgent } from "@ag-ui/dify";

// 检查必要的环境变量
if (!process.env.DIFY_API_KEY) {
console.warn("警告: DIFY_API_KEY 环境变量未设置。Dify 集成将无法正常工作。");
}

export const agentsIntegrations: AgentIntegrationConfig[] = [
{
Expand Down Expand Up @@ -112,4 +118,29 @@ export const agentsIntegrations: AgentIntegrationConfig[] = [
};
},
},
{
id: "dify",
agents: async () => {
// 检查环境变量
if (!process.env.DIFY_API_KEY) {
throw new Error("DIFY_API_KEY 环境变量未设置");
}

console.log("Dify 环境变量:", {
DIFY_API_KEY: process.env.DIFY_API_KEY,
DIFY_API_BASE_URL: process.env.DIFY_API_BASE_URL
});

return {
agentic_chat: new DifyAgent({
apiKey: process.env.DIFY_API_KEY,
baseUrl: process.env.DIFY_API_BASE_URL,
}),
tool_based_generative_ui: new DifyAgent({
apiKey: process.env.DIFY_API_KEY,
baseUrl: process.env.DIFY_API_BASE_URL,
}),
};
},
},
];
8 changes: 8 additions & 0 deletions typescript-sdk/apps/dojo/src/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ export const menuIntegrations: MenuIntegrationConfig[] = [
name: "Agno",
features: ["agentic_chat"],
},
{
id: "dify",
name: "Dify",
features: [
"agentic_chat",
"tool_based_generative_ui",
],
},
];
55 changes: 55 additions & 0 deletions typescript-sdk/integrations/agno/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
declare module "@ag-ui/client" {
export interface Tool {
name: string;
description: string;
parameters: Record<string, any>;
}

export interface RunAgentInput {
threadId: string;
runId: string;
messages: any[];
tools?: Tool[];
context?: any[];
state?: any;
forwardedProps?: any;
}

export enum EventType {
RUN_STARTED = "run_started",
RUN_FINISHED = "run_finished",
TEXT_MESSAGE_START = "text_message_start",
TEXT_MESSAGE_CONTENT = "text_message_content",
TEXT_MESSAGE_END = "text_message_end",
TOOL_CALL_START = "tool_call_start",
TOOL_CALL_ARGS = "tool_call_args",
TOOL_CALL_END = "tool_call_end",
}

export interface AGUIEvent {
type: EventType;
timestamp: Date;
threadId?: string;
runId?: string;
messageId?: string;
role?: string;
delta?: string;
toolCallId?: string;
toolCallName?: string;
result?: string;
}

export abstract class AbstractAgent {
constructor(config?: any);
abstract stream(input: RunAgentInput): AsyncGenerator<AGUIEvent>;
}

export class HttpAgent extends AbstractAgent {
constructor(config?: any);
stream(input: RunAgentInput): AsyncGenerator<AGUIEvent>;
}

export interface AgentConfig {
agentId?: string;
}
}
56 changes: 56 additions & 0 deletions typescript-sdk/integrations/dify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# AG-UI Dify Integration

This package provides integration between AG-UI and Dify, allowing you to use Dify's AI agents with AG-UI's frontend components.

## Installation

```bash
pnpm add @ag-ui/dify
```

## Usage

```typescript
import { DifyAgent } from "@ag-ui/dify";

// Create a Dify agent
const agent = new DifyAgent({
apiKey: "your-dify-api-key",
baseUrl: "https://api.dify.ai/v1", // optional
});

// Use the agent with AG-UI components
```

## Features

- Seamless integration with AG-UI's frontend components
- Support for streaming responses
- Tool calling support
- Message format conversion between AG-UI and Dify

## API Reference

### DifyAgent

The main class for integrating Dify with AG-UI.

#### Constructor

```typescript
constructor(config: DifyClientConfig)
```

Parameters:
- `config`: Configuration object
- `apiKey`: Your Dify API key
- `baseUrl`: (optional) Dify API base URL, defaults to "https://api.dify.ai/v1"

#### Methods

- `stream(input: RunAgentInput)`: Streams the agent's response
- Returns: AsyncGenerator of AG-UI events

## License

MIT
22 changes: 22 additions & 0 deletions typescript-sdk/integrations/dify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@ag-ui/dify",
"version": "0.1.0",
"private": true,
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"clean": "rimraf dist"
},
"dependencies": {
"@ag-ui/client": "workspace:*",
"rxjs": "7.8.1"
},
"devDependencies": {
"rimraf": "^5.0.0",
"tsup": "^8.0.0",
"typescript": "^5.0.0"
}
}
43 changes: 43 additions & 0 deletions typescript-sdk/integrations/dify/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
declare module "@ag-ui/client" {
export interface Tool {
name: string;
description: string;
parameters: Record<string, any>;
}

export interface RunAgentInput {
threadId: string;
runId: string;
messages: any[];
tools?: Tool[];
}

export enum EventType {
RUN_STARTED = "run_started",
RUN_FINISHED = "run_finished",
TEXT_MESSAGE_START = "text_message_start",
TEXT_MESSAGE_CONTENT = "text_message_content",
TEXT_MESSAGE_END = "text_message_end",
TOOL_CALL_START = "tool_call_start",
TOOL_CALL_ARGS = "tool_call_args",
TOOL_CALL_END = "tool_call_end",
}

export interface AGUIEvent {
type: EventType;
timestamp: Date;
threadId?: string;
runId?: string;
messageId?: string;
role?: string;
delta?: string;
toolCallId?: string;
toolCallName?: string;
result?: string;
}

export abstract class AbstractAgent {
constructor(config?: any);
abstract stream(input: RunAgentInput): AsyncGenerator<AGUIEvent>;
}
}
Loading