Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sdream-ai-fe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
侯明涛
sdream-ai-fe
Commits
476172cd
Commit
476172cd
authored
Jul 08, 2026
by
Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:引入sdk逻辑&&向sdk传参数区分web
parent
af94f1e6
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
113 deletions
+39
-113
src/index.tsx
+14
-3
src/pages/Chat/Chat.tsx
+0
-21
src/pages/Home/HomeNew.tsx
+23
-3
src/pages/Home/components/QuestionList/QuestionList.tsx
+2
-20
src/utils/sdkLoader.ts
+0
-66
No files found.
src/index.tsx
View file @
476172cd
...
...
@@ -31,17 +31,28 @@ async function loadSDK(): Promise<void> {
const
ChatSDK
=
(
window
as
any
).
ChatSDK
if
(
ChatSDK
&&
ChatSDK
.
createChatSDK
)
{
const
toolId
=
typeof
window
!==
'undefined'
?
sessionStorage
.
getItem
(
'currentToolId'
)
:
null
console
.
log
(
'[SDK] toolId:'
,
toolId
)
// 读取缓存中的待发送问题(可能是 QuestionList 点击后设置的)
const
pendingQuestion
=
typeof
window
!==
'undefined'
?
sessionStorage
.
getItem
(
'__CHAT_SDK_PENDING_QUESTION__'
)
:
null
const
config
=
{
code
:
import
.
meta
.
env
.
VITE_SDK_CODE
||
'default_code'
,
channel
:
import
.
meta
.
env
.
VITE_SDK_CHANNEL
||
'default_channel'
,
toolId
,
mock
:
import
.
meta
.
env
.
VITE_SDK_MOCK
===
'true'
,
apiBaseUrl
:
import
.
meta
.
env
.
VITE_SDK_API_BASE_URL
,
initialQuestion
:
pendingQuestion
||
undefined
,
clientType
:
import
.
meta
.
env
.
VITE_OUTPUT_OBJ
||
'open'
,
}
console
.
log
(
'[SDK] [初始化] createChatSDK 配置:'
,
JSON
.
stringify
(
config
,
null
,
2
))
try
{
ChatSDK
.
createChatSDK
(
config
)
console
.
log
(
'[SDK] 初始化成功'
)
const
sdkInstance
=
ChatSDK
.
createChatSDK
(
config
)
// 缓存实例引用,方便后续复用
if
(
sdkInstance
)
{
window
.
__CHAT_SDK_INSTANCE__
=
sdkInstance
console
.
log
(
'[SDK] [初始化] SDK 实例创建成功并缓存到 window.__CHAT_SDK_INSTANCE__'
)
}
console
.
log
(
'[SDK] [初始化] 初始化成功'
)
}
catch
(
error
)
{
console
.
error
(
'[SDK] 初始化失败:'
,
error
)
...
...
src/pages/Chat/Chat.tsx
View file @
476172cd
...
...
@@ -222,17 +222,6 @@ export const Chat: React.FC = () => {
console
.
debug
(
'[Chat] 当前链接 / 缓存 toolId:'
,
debugInfo
)
},
[
debugInfo
])
// 调试:观察 shouldSendQuestion 在对话页的变化
useEffect
(()
=>
{
console
.
log
(
'[Chat] shouldSendQuestion changed 111111:'
,
{
conversationIdFromRoute
:
id
,
shouldSendQuestion
,
currentToolId
,
isLoading
,
allItemsLength
:
allItems
.
length
,
})
},
[
id
,
shouldSendQuestion
,
currentToolId
,
isLoading
,
allItems
.
length
])
// 历史记录点击时将 toolId 通过路由 state 传入,优先使用该值快速同步高亮
useEffect
(()
=>
{
// 保存从 location.state 传递的 toolId 到 ref
...
...
@@ -1103,15 +1092,6 @@ export const Chat: React.FC = () => {
}
:
null
)
if
(
wfAnswerForFollowUp
)
{
console
.
log
(
'[Chat WF] 流结束准备续问'
,
{
parsed
:
wfParsedCtrl
,
complete
:
isWorkflowControlPayloadComplete
(
wfParsedCtrl
),
willEnterSub
:
isWorkflowEnterSubPayload
(
wfParsedCtrl
),
packetQuestion
:
wfPacketQuestion
,
wfRawParseText
:
wfRawParseText
.
slice
(
0
,
120
),
hasRef
:
Boolean
(
streamSnapshotForWf
),
displayAnswer
:
wfAnswerForFollowUp
.
answer
,
})
processBusinessInsightWorkflowAfterStream
(
wfAnswerForFollowUp
)
}
else
{
...
...
@@ -1554,7 +1534,6 @@ export const Chat: React.FC = () => {
dispatch
(
clearShouldSendQuestion
())
// 确保历史记录加载完成后再发送问题
setTimeout
(()
=>
{
console
.
log
(
'[Chat] 执行自动发送问题:'
,
questionToSend
)
const
autoExtra
=
buildWorkflowContinueExtraFromRef
()
handleSubmitQuestion
(
questionToSend
,
undefined
,
currentToolId
,
autoExtra
)
},
100
)
...
...
src/pages/Home/HomeNew.tsx
View file @
476172cd
...
...
@@ -79,8 +79,25 @@ export const Home: React.FC = () => {
const
hasFetched
=
useRef
(
false
)
const
hasCalledBusinessApis
=
useRef
(
false
)
// 防止业务接口重复调用
const
prevPathRef
=
useRef
<
string
>
(
location
.
pathname
)
// 记录上一次路径,用于检测从收藏页面返回
// 使用 useState
const
[
otherQuestions
,
setOtherQuestions
]
=
useState
<
any
>
({
content
:
[]
})
// 默认 mock 数据 - 接口失败时回退到这个数据
const
DEFAULT_QUESTIONS
=
{
content
:
[
'润爱家投保前需要体检吗?'
,
'和谐喜乐一生少儿重疾险产品特色是什么?'
,
'家属没有医保,是否可以投保润爱家?'
,
'福寿双全A01产品保费要求是什么?'
,
'润爱家等待期是多久?'
,
'润爱家保障范围包括哪些疾病?'
,
'定期寿险和终身寿险有什么区别?'
,
'如何选择适合自己的重疾险保额?'
,
'保险等待期内出险能理赔吗?'
,
'什么是百万医疗险?适合哪些人群购买?'
,
],
configName
:
'您可以试着问我'
,
}
// 使用 useState - 添加默认 mock 数据
const
[
otherQuestions
,
setOtherQuestions
]
=
useState
<
any
>
(
DEFAULT_QUESTIONS
)
const
[
isToolBtnActive
,
setIsToolBtnActive
]
=
useState
<
boolean
>
(
false
)
const
[
shouldChangeStyle
,
setShouldChangeStyle
]
=
useState
<
boolean
>
(
true
)
const
[
isBusinessInsightMode
,
setIsBusinessInsightMode
]
=
useState
<
boolean
>
(
false
)
...
...
@@ -201,7 +218,8 @@ export const Home: React.FC = () => {
}
catch
(
error
)
{
console
.
error
(
'[HomeNew] handleRefreshQuestions - 刷新问题列表失败:'
,
error
)
throw
error
// 抛出错误,让 QuestionList 组件处理
// 接口失败时回退到 mock 数据
setOtherQuestions
(
DEFAULT_QUESTIONS
)
}
finally
{
setIsDataLoaded
(
true
)
// 无论成功失败都标记为已加载
...
...
@@ -265,6 +283,8 @@ export const Home: React.FC = () => {
}
catch
(
error
)
{
console
.
error
(
'获取工具相关问题失败:'
,
error
)
// 接口失败时回退到 mock 数据
setOtherQuestions
(
DEFAULT_QUESTIONS
)
}
finally
{
setIsDataLoaded
(
true
)
// 无论成功失败都标记为已加载
...
...
src/pages/Home/components/QuestionList/QuestionList.tsx
View file @
476172cd
...
...
@@ -109,28 +109,12 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
const
handleClick
=
(
item
:
string
)
=>
{
if
(
checkAuth
()
&&
!
isClicking
)
{
setIsClicking
(
true
)
// 获取当前的 toolId,优先从 Redux 获取,其次从 sessionStorage 获取
const
toolId
=
currentToolId
||
safeSessionStorageGetItem
(
'currentToolId'
)
||
null
// 优先使用当前会话,如果没有则使用第一个会话,如果都没有则创建新会话
const
targetConversationId
=
currentConversationId
||
conversations
[
0
]?.
conversationId
console
.
log
(
'[QuestionList] 点击常见问题:'
,
{
question
:
item
,
toolId
,
currentConversationId
,
targetConversationId
,
hasConversations
:
conversations
.
length
>
0
,
})
if
(
targetConversationId
)
{
// 使用现有会话
console
.
log
(
'[QuestionList] 使用现有会话,设置 shouldSendQuestion 并导航:'
,
{
conversationId
:
targetConversationId
,
question
:
item
,
toolId
,
})
dispatch
(
setCurrentConversation
(
targetConversationId
))
dispatch
(
setShouldSendQuestion
(
item
))
// 通过 location.state 传递 toolId,避免 Chat 页面清空 toolId
// 传递 skipHistoryLoad 标记,跳过历史记录加载,直接发送问题
navigate
(
`/chat/
${
targetConversationId
}
`
,
{
state
:
{
toolId
,
...
...
@@ -139,8 +123,6 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
})
}
else
{
// 如果没有现有会话,仍然创建新会话(向后兼容)
console
.
log
(
'[QuestionList] 没有现有会话,创建新会话:'
,
item
)
dispatch
(
createConversation
({
conversationData
:
{},
...
...
@@ -149,7 +131,7 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
}),
)
}
// 防止重复点击
setTimeout
(()
=>
{
setIsClicking
(
false
)
},
1000
)
...
...
src/utils/sdkLoader.ts
deleted
100644 → 0
View file @
af94f1e6
interface
ChatSDKConfig
{
code
:
string
channel
:
string
toolId
?:
string
apiBaseUrl
?:
string
mock
?:
boolean
isDesktop
?:
boolean
}
interface
ChatSDKInstance
{
speak
:
(
text
:
string
)
=>
void
stopSpeak
:
()
=>
void
destroy
:
()
=>
void
}
let
sdkInstance
:
ChatSDKInstance
|
null
=
null
let
sdkLoadingPromise
:
Promise
<
void
>
|
null
=
null
export
async
function
loadChatSDK
(
config
:
ChatSDKConfig
):
Promise
<
ChatSDKInstance
|
null
>
{
if
(
sdkInstance
)
{
return
sdkInstance
}
if
(
sdkLoadingPromise
)
{
await
sdkLoadingPromise
return
sdkInstance
}
sdkLoadingPromise
=
new
Promise
((
resolve
)
=>
{
const
script
=
document
.
createElement
(
'script'
)
script
.
src
=
'/sdream-ai/sdk/index.js'
script
.
onload
=
()
=>
{
try
{
const
ChatSDK
=
(
window
as
any
).
ChatSDK
if
(
ChatSDK
&&
ChatSDK
.
createChatSDK
)
{
sdkInstance
=
ChatSDK
.
createChatSDK
(
config
)
console
.
log
(
'[SDK] toolId:'
,
config
.
toolId
)
}
}
catch
(
error
)
{
console
.
error
(
'[SDK] 初始化失败:'
,
error
)
}
resolve
()
}
script
.
onerror
=
()
=>
{
console
.
error
(
'[SDK] ChatSDK 脚本加载失败'
)
resolve
()
}
document
.
body
.
appendChild
(
script
)
})
await
sdkLoadingPromise
return
sdkInstance
}
export
function
getSDKInstance
():
ChatSDKInstance
|
null
{
return
sdkInstance
}
export
function
destroySDK
():
void
{
if
(
sdkInstance
)
{
sdkInstance
.
destroy
()
sdkInstance
=
null
}
sdkLoadingPromise
=
null
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment