-
Notifications
You must be signed in to change notification settings - Fork 38
feat(chatBI): add initial implementation of chatBI tool #182
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { defineTool } from '@tool/type'; | ||
import { | ||
FlowNodeInputTypeEnum, | ||
FlowNodeOutputTypeEnum, | ||
WorkflowIOValueTypeEnum | ||
} from '@tool/type/fastgpt'; | ||
import { ToolTypeEnum } from '@tool/type/tool'; | ||
|
||
export default defineTool({ | ||
name: { | ||
'zh-CN': 'chatBI', | ||
en: 'Template tool' | ||
}, | ||
type: ToolTypeEnum.tools, | ||
description: { | ||
'zh-CN': 'chatBI 工具', | ||
FinleyGe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
en: 'chatBI Tool' | ||
}, | ||
toolDescription: | ||
'send user natural language instructions to ChatBI application for execution, sse stream interface returns results', | ||
secretInputConfig: [ | ||
{ | ||
key: 'chatBIUrl', | ||
label: 'chatBI 服务根地址', | ||
description: '根地址,例如:http://example.com', | ||
required: true, | ||
inputType: 'secret' | ||
}, | ||
{ | ||
key: 'sysAccessKey', | ||
label: 'chatBI 系统AccessKey', | ||
description: '系统AccessKey', | ||
required: true, | ||
inputType: 'secret' | ||
}, | ||
{ | ||
key: 'corpId', | ||
label: 'chatBI 企业ID', | ||
description: '企业ID', | ||
required: true, | ||
inputType: 'secret' | ||
} | ||
], | ||
versionList: [ | ||
{ | ||
value: '0.1.0', | ||
description: 'Default version', | ||
inputs: [ | ||
{ | ||
key: 'query', | ||
label: '用户提问内容', | ||
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference], | ||
valueType: WorkflowIOValueTypeEnum.string, | ||
required: true | ||
}, | ||
{ | ||
key: 'appId', | ||
label: 'chatBI 应用ID', | ||
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference], | ||
valueType: WorkflowIOValueTypeEnum.string, | ||
required: true | ||
}, | ||
{ | ||
key: 'appAccessKey', | ||
label: 'chatBI 应用AccessKey', | ||
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference], | ||
valueType: WorkflowIOValueTypeEnum.string, | ||
required: true | ||
}, | ||
{ | ||
key: 'sessionId', | ||
label: '会话id', | ||
renderTypeList: [FlowNodeInputTypeEnum.input, FlowNodeInputTypeEnum.reference], | ||
valueType: WorkflowIOValueTypeEnum.string, | ||
required: false | ||
} | ||
], | ||
outputs: [ | ||
{ | ||
valueType: WorkflowIOValueTypeEnum.arrayAny, | ||
key: 'displayContentList', | ||
label: '展示内容列表', | ||
description: 'ChatBI返回的核心展示内容,包含文本、图表、表格等多种类型' | ||
} | ||
] | ||
} | ||
] | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import config from './config'; | ||
import { InputType, OutputType, tool as toolCb } from './src'; | ||
import { exportTool } from '@tool/utils/tool'; | ||
|
||
export default exportTool({ | ||
toolCb, | ||
InputType, | ||
OutputType, | ||
config | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "@fastgpt-plugins/tool-chat-bi", | ||
"module": "index.ts", | ||
"type": "module", | ||
"scripts": { | ||
"build": "bun ../../../../scripts/build.ts" | ||
}, | ||
"devDependencies": { | ||
"@types/bun": "latest" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"zod": "^3.24.2" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { z } from 'zod'; | ||
import type { RunToolSecondParamsType } from '@tool/type/tool'; | ||
import { StreamDataAnswerTypeEnum } from '@tool/type/tool'; | ||
import { getErrText } from '@tool/utils/err'; | ||
|
||
type DataType = { | ||
isFinished: boolean; | ||
isSuccess: boolean; | ||
errorMessage?: string; | ||
processList: unknown[]; | ||
displayContentList: ContentType[]; | ||
streamData: string; | ||
}; | ||
|
||
enum ContentTypeEnum { | ||
text = 'TEXT' | ||
} | ||
type ContentType = { | ||
content: string; | ||
type: ContentTypeEnum; | ||
}; | ||
|
||
export const InputType = z.object({ | ||
query: z.string(), | ||
appId: z.string(), | ||
appAccessKey: z.string(), | ||
sessionId: z.string().optional(), | ||
chatBIUrl: z.string(), | ||
sysAccessKey: z.string(), | ||
corpId: z.string() | ||
}); | ||
|
||
export const OutputType = z.object({ | ||
// displayContentList: z.array(z.any()) | ||
}); | ||
|
||
export async function tool( | ||
{ | ||
query, | ||
appId, | ||
appAccessKey, | ||
sessionId, | ||
chatBIUrl, | ||
sysAccessKey, | ||
corpId | ||
}: z.infer<typeof InputType>, | ||
{ systemVar, streamResponse }: RunToolSecondParamsType | ||
): Promise<z.infer<typeof OutputType>> { | ||
try { | ||
const url = new URL('/v2/open/api/common/stream/chatbi/chatNew', chatBIUrl); | ||
|
||
// userID get from systemVar | ||
const userId = systemVar.user.username.split('-')[1]; | ||
|
||
if (!sessionId) { | ||
sessionId = userId; | ||
} | ||
|
||
const response = await fetch(url.toString(), { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'text/event-stream', | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
sysAccessKey, | ||
corpId, | ||
appId, | ||
appAccessKey, | ||
userId, | ||
query, | ||
sessionId | ||
}) | ||
}); | ||
|
||
if (!response.ok) { | ||
return Promise.reject(`HTTP ${response.status}: ${response.statusText}`); | ||
} | ||
|
||
const reader = response.body?.getReader(); | ||
|
||
if (!reader) { | ||
return Promise.reject('无法获取响应流'); | ||
} | ||
|
||
const decoder = new TextDecoder(); | ||
let sentListLen = 0; | ||
let buffer = ''; | ||
let isFinished = false; | ||
|
||
while (!isFinished) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
|
||
buffer += decoder.decode(value, { stream: true }); | ||
|
||
const events = buffer.split('\n\n'); | ||
|
||
buffer = events.pop() || ''; | ||
|
||
for (const event of events) { | ||
if (!event.trim()) continue; | ||
|
||
const lines = event.split('\n'); | ||
let eventData = ''; | ||
|
||
for (const line of lines) { | ||
if (line.startsWith('data:')) { | ||
// remove "data:" and trim | ||
eventData = line.slice(5).trim(); | ||
break; | ||
} | ||
} | ||
|
||
if (eventData && eventData !== '') { | ||
try { | ||
const data: DataType = JSON.parse(eventData); | ||
|
||
if (data.displayContentList && data.displayContentList.length > sentListLen) { | ||
// only send the last items | ||
const sendList = data.displayContentList.slice(sentListLen); | ||
const texts = sendList | ||
.filter((item) => item.type === 'TEXT') | ||
.map((item) => item.content) | ||
.join('\n'); | ||
const unTexts = sendList.filter((item) => item.type !== 'TEXT'); | ||
const content = | ||
texts + | ||
'\n' + | ||
(unTexts.length > 0 ? `\`\`\`RENDER\n${JSON.stringify(unTexts)}\n\`\`\`\n` : ''); | ||
|
||
streamResponse({ | ||
content, | ||
type: StreamDataAnswerTypeEnum.answer | ||
}); | ||
sentListLen = data.displayContentList.length; | ||
} | ||
|
||
if (data.streamData) { | ||
streamResponse({ | ||
content: data.streamData, | ||
type: StreamDataAnswerTypeEnum.answer | ||
}); | ||
} | ||
|
||
if (data.isFinished) { | ||
isFinished = true; | ||
break; | ||
} | ||
} catch (e) { | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { | ||
// displayContentList | ||
}; | ||
} catch (error) { | ||
return Promise.reject(getErrText(error)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { expect, test } from 'vitest'; | ||
import tool from '..'; | ||
|
||
test(async () => { | ||
expect(tool.name).toBeDefined(); | ||
expect(tool.description).toBeDefined(); | ||
expect(tool.cb).toBeDefined(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.