Skip to content
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

Dify.AI Integration Block #1183

Merged
merged 11 commits into from
Jan 29, 2024
1 change: 1 addition & 0 deletions apps/docs/openapi/builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -26853,6 +26853,7 @@
"cal-com",
"chat-node",
"qr-code",
"dify-ai",
"mistral"
]
},
Expand Down
1 change: 1 addition & 0 deletions apps/docs/openapi/viewer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10354,6 +10354,7 @@
"cal-com",
"chat-node",
"qr-code",
"dify-ai",
"mistral"
]
},
Expand Down
80 changes: 80 additions & 0 deletions packages/forge/blocks/difyAi/actions/createChatMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { createAction, option } from '@typebot.io/forge'
import { isDefined, isEmpty } from '@typebot.io/lib'
import { got } from 'got'
import { auth } from '../auth'
import { DifyResponse } from '../types'
import { defaultBaseUrl } from '../constants'

export const createChatMessage = createAction({
auth,
name: 'Create Chat Message',
options: option.object({
query: option.string.layout({
label: 'Query',
placeholder: 'User input/question content',
inputType: 'textarea',
isRequired: true,
}),
conversation_id: option.string.layout({
label: 'Conversation ID',
moreInfoTooltip:
'Used to remember the conversation with the user. If empty, a new conversation id is created.',
}),
user: option.string.layout({
label: 'User',
moreInfoTooltip:
'The user identifier, defined by the developer, must ensure uniqueness within the app.',
}),
inputs: option.keyValueList.layout({
accordion: 'Inputs',
}),
responseMapping: option
.saveResponseArray(['Answer', 'Conversation ID', 'Total Tokens'])
.layout({
accordion: 'Save response',
}),
}),
getSetVariableIds: ({ responseMapping }) =>
responseMapping?.map((r) => r.variableId).filter(isDefined) ?? [],
run: {
server: async ({
credentials: { apiEndpoint, apiKey },
options: { conversation_id, query, user, inputs, responseMapping },
variables,
}) => {
const res: DifyResponse = await got
.post((apiEndpoint ?? defaultBaseUrl) + '/v1/chat-messages', {
headers: {
Authorization: `Bearer ${apiKey}`,
},
json: {
inputs: inputs?.reduce((acc, { key, value }) => {
if (isEmpty(key) || isEmpty(value)) return acc
return {
...acc,
[key]: value,
}
}, {}),
query,
response_mode: 'blocking',
conversation_id,
user,
},
})
.json()

responseMapping?.forEach((mapping) => {
if (!mapping.variableId) return

const item = mapping.item ?? 'Answer'
if (item === 'Answer') variables.set(mapping.variableId, res.answer)

if (item === 'Conversation ID')
variables.set(mapping.variableId, res.conversation_id)

if (item === 'Total Tokens')
variables.set(mapping.variableId, res.metadata.usage.total_tokens)
})
},
},
})
23 changes: 23 additions & 0 deletions packages/forge/blocks/difyAi/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { option, AuthDefinition } from '@typebot.io/forge'
import { defaultBaseUrl } from './constants'

export const auth = {
type: 'encryptedCredentials',
name: 'Dify.AI account',
schema: option.object({
apiEndpoint: option.string.layout({
label: 'API Endpoint',
isRequired: true,
helperText: 'URI where the Service API is hosted.',
withVariableButton: false,
defaultValue: defaultBaseUrl,
}),
apiKey: option.string.layout({
label: 'App API key',
isRequired: true,
helperText: 'API Secret Key for your Dify App.',
inputType: 'password',
withVariableButton: false,
}),
}),
} satisfies AuthDefinition
1 change: 1 addition & 0 deletions packages/forge/blocks/difyAi/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const defaultBaseUrl = 'https://api.dify.ai'
13 changes: 13 additions & 0 deletions packages/forge/blocks/difyAi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createBlock } from '@typebot.io/forge'
import { DifyAiLogo } from './logo'
import { auth } from './auth'
import { createChatMessage } from './actions/createChatMessage'

export const difyAi = createBlock({
id: 'dify-ai',
name: 'Dify.AI',
tags: ['dify', 'ai', 'documents', 'files', 'knowledge base'],
LightLogo: DifyAiLogo,
auth,
actions: [createChatMessage],
})
11 changes: 11 additions & 0 deletions packages/forge/blocks/difyAi/logo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'

export const DifyAiLogo = (props: React.SVGProps<SVGSVGElement>) => (
<svg viewBox="0 0 25 25" xmlns="http://www.w3.org/2000/svg" {...props}>
<image
href="https://framerusercontent.com/images/xRJ6vNo9mUYeVNxt0KITXCXEuSk.png"
height="25"
width="25"
/>
</svg>
)
16 changes: 16 additions & 0 deletions packages/forge/blocks/difyAi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@typebot.io/dify-ai-block",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"keywords": [],
"license": "ISC",
"devDependencies": {
"@typebot.io/forge": "workspace:*",
"@typebot.io/lib": "workspace:*",
"@typebot.io/tsconfig": "workspace:*",
"@types/react": "18.2.15",
"got": "12.6.0",
"typescript": "5.3.2"
}
}
10 changes: 10 additions & 0 deletions packages/forge/blocks/difyAi/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@typebot.io/tsconfig/base.json",
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"],
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"noEmit": true,
"jsx": "react"
}
}
9 changes: 9 additions & 0 deletions packages/forge/blocks/difyAi/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type DifyResponse = {
answer: string
metadata: {
usage: {
total_tokens: number
}
}
conversation_id: string
}
12 changes: 12 additions & 0 deletions packages/forge/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ export const option = {
z.enum(values).optional(),
number: z.number().or(variableStringSchema).optional(),
array: <T extends z.ZodTypeAny>(schema: T) => z.array(schema).optional(),
keyValueList: z
.array(
z.object({
key: z.string().optional().layout({
label: 'Key',
}),
value: z.string().optional().layout({
label: 'Value',
}),
})
)
.optional(),
discriminatedUnion: <
T extends string,
J extends [
Expand Down
1 change: 1 addition & 0 deletions packages/forge/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const enabledBlocks = [
'cal-com',
'chat-node',
'qr-code',
'dify-ai',
'mistral',
] as const
4 changes: 3 additions & 1 deletion packages/forge/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Do not edit this file manually
import { difyAi } from '@typebot.io/dify-ai-block'
import { mistral } from '@typebot.io/mistral-block'
import { qrCode } from '@typebot.io/qrcode-block'
import { chatNode } from '@typebot.io/chat-node-block'
Expand All @@ -19,7 +20,8 @@ export const forgedBlocks = [
calCom,
chatNode,
qrCode,
mistral,
difyAi,
mistral
] as BlockDefinition<(typeof enabledBlocks)[number], any, any>[]

export type ForgedBlockDefinition = (typeof forgedBlocks)[number]
Expand Down
1 change: 1 addition & 0 deletions packages/forge/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@typebot.io/cal-com-block": "workspace:*",
"@typebot.io/chat-node-block": "workspace:*",
"@typebot.io/qrcode-block": "workspace:*",
"@typebot.io/dify-ai-block": "workspace:*",
"@typebot.io/mistral-block": "workspace:*"
}
}
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.