-
Notifications
You must be signed in to change notification settings - Fork 311
feat(site): add the tiny-robot drawer to the official website. #3467
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
Changes from 4 commits
c0183a5
1bcc505
27b06c2
9c59f8a
41c679b
5182627
c54301c
2fee84b
b63202f
d8bfc13
0bdd7f5
56ea1a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,64 @@ | ||||||||||||||||||||||||
import type { ChatCompletionRequest } from '@opentiny/tiny-robot-kit' | ||||||||||||||||||||||||
import type { AIModelConfig } from '@opentiny/tiny-robot-kit' | ||||||||||||||||||||||||
import type { ChatCompletionResponse } from '@opentiny/tiny-robot-kit' | ||||||||||||||||||||||||
import type { StreamHandler } from '@opentiny/tiny-robot-kit' | ||||||||||||||||||||||||
import { BaseModelProvider, handleSSEStream, handleRequestError } from '@opentiny/tiny-robot-kit' | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** | ||||||||||||||||||||||||
* 对接AIClient的自定义 Dify 大模型服务 | ||||||||||||||||||||||||
* | ||||||||||||||||||||||||
* const client = new AIClient({ | ||||||||||||||||||||||||
* provider: 'custom', | ||||||||||||||||||||||||
* providerImplementation: new CustomModelProvider( config ) | ||||||||||||||||||||||||
* }); | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
export class DifyModelProvider extends BaseModelProvider { | ||||||||||||||||||||||||
constructor(config: AIModelConfig) { | ||||||||||||||||||||||||
super(config) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
/** 同步请示不需要实现 */ | ||||||||||||||||||||||||
chat(request: ChatCompletionRequest): Promise<ChatCompletionResponse> { | ||||||||||||||||||||||||
throw new Error('Method not implemented.') | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/** 异步流式请求 */ | ||||||||||||||||||||||||
async chatStream(request: ChatCompletionRequest, handler: StreamHandler): Promise<void> { | ||||||||||||||||||||||||
const { signal } = request | ||||||||||||||||||||||||
this.validateRequest(request) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
try { | ||||||||||||||||||||||||
// 验证请求的messages属性,必须是数组,且每个消息必须有role\content属性 | ||||||||||||||||||||||||
// const { onData, onDone, onError } = handler | ||||||||||||||||||||||||
const lastMessage = request.messages[request.messages.length - 1].content | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add bounds check before accessing array element. Accessing the last message without checking if the array is empty could cause a runtime error. try {
// 验证请求的messages属性,必须是数组,且每个消息必须有role\content属性
// const { onData, onDone, onError } = handler
+ if (!request.messages || request.messages.length === 0) {
+ throw new Error('Messages array is empty')
+ }
const lastMessage = request.messages[request.messages.length - 1].content 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||
// 模拟异步流式响应 | ||||||||||||||||||||||||
const response = await fetch(`${this.config.apiUrl}/chat-messages`, { | ||||||||||||||||||||||||
method: 'POST', | ||||||||||||||||||||||||
headers: { | ||||||||||||||||||||||||
'Content-Type': 'application/json', | ||||||||||||||||||||||||
'Authorization': `Bearer ${this.config.apiKey}`, | ||||||||||||||||||||||||
'Accept': 'text/event-stream' | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
body: JSON.stringify({ | ||||||||||||||||||||||||
query: lastMessage, | ||||||||||||||||||||||||
user: 'user', | ||||||||||||||||||||||||
response_mode: 'stream' | ||||||||||||||||||||||||
// conversation_id:'' | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
if (!response.ok) { | ||||||||||||||||||||||||
throw new Error(`HTTP error! status: ${response.status}`) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
await handleSSEStream(response, handler, signal) | ||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||
if (signal && signal.aborted) { | ||||||||||||||||||||||||
console.warn('Request was aborted:', error) | ||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
console.error('Error in chatStream:', error) | ||||||||||||||||||||||||
handler.onError(handleRequestError(error)) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
无用的代码可以删除
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已删除