Commit 4b7ed6c0 by DigHuang Committed by GitHub

feat(chat): helper bot ui & file upload support (#7245)

* fix(permission): handle sandbox checks without standard plan

* refactor(chat): unify helper bot with main chat flow

- route helper bot through the main chat schema and API endpoints

- align file upload, image handling, empty-output tips, and interactive payload support

- update related tests and pro submodule pointer

* chore(helperbot): clean unified chat flow leftovers

* feat: align helperbot workflow billing

* feat: optimize helper chat flow

* fix: handle helper chat resume cleanup
parent 9363d7a5
# 辅助生成统一抽象重构设计文档
## 背景
当前 Chat Agent 辅助配置生成能力为了复用 workflow 的余额校验、usage 记录、节点响应和 SSE 能力,被硬塞进 `dispatchWorkFlow`:
- API route 构造一个 `internalRuntimeNode`。
- `dispatchWorkFlow` 只执行这一个 internal node。
- internal node 再调用 Chat Agent 配置生成 processor。
- Chat Agent 配置生成 processor 内部再跑 `runUnifiedAgentLoop`。
这条链路能复用计费和运行状态,但语义上不准确:
- Chat Agent 辅助配置生成不是用户配置的 workflow,也不是 Agent 节点。
- workflow runtime 的节点、边、skip、interactive、nodeResponse 等能力大部分只是被借壳。
- 后续 Skill 辅助生成也需要类似能力,如果继续走 workflow,会把“辅助生成”与“工作流执行”绑定得更深。
辅助生成应作为独立通用抽象存在。Chat Agent 辅助配置、Skill 生成、未来的数据集配置建议等都应是辅助生成场景,而不是 workflow 特例。
## 目标
- 从 Chat Agent 辅助配置 completions 中移除 `dispatchWorkFlow` 和 `internalRuntimeNode`。
- 辅助生成核心模块完全去掉 workflow runtime 依赖:
- 不调用 `dispatchWorkFlow`。
- 不构造 runtime node / edge。
- 不复用 `createWorkflowStreamResponseContext`。
- 不使用 `WorkflowResponseType` 作为 processor 入参。
- 不使用 workflow nodeResponse writer。
- 不使用 workflow interactive 类型作为辅助生成领域类型。
- 封装统一辅助生成会话管理能力:
- 鉴权。
- 限频。
- 对话记录读取、预创建、失败回写、成功保存。
- 文件授权和文件内容解析。
- 余额校验和 usage 创建、追加、汇总。
- SSE 输出、恢复、错误收口。
- 可选虚拟机激活。
- 可选注入系统内置 Skill。
- 封装统一 agent loop 调用入口,场景只声明:
- 模型、系统提示词、消息。
- 是否允许 runtime tools。
- 是否需要 VM / system skills。
- 如何把 loop 结果解析成业务输出。
- 保留当前 Chat Agent 辅助配置行为:
- collection 阶段继续输出 interactive 表单。
- generation 阶段继续输出 Chat Agent 配置事件给前端应用。
- chat 记录、文件、安全校验、限频语义不回退。
- 为 Skill 详情页辅助生成迁移预留接口:
- 能在 Skill edit sandbox 中注入官方 `skill-creator`。
- 能在同一套辅助生成会话中读写当前 Skill 工作区文件。
- 当前变更不迁移 Skill 侧逻辑;Skill Debug 的正常 Agent 调试链路保持现状。
## 非目标
- 不重构标准 App chat completions。
- 不重构 Skill Debug 运行当前 Skill 的 `workflowStart -> agent` 调试链路。
- 不改变 chat 三表的 `sourceType/sourceId` 存储策略。
- 不把辅助生成能力暴露成用户可见 workflow 节点。
- 不把 workflow runtime 的 stream、interactive、nodeResponse 抽过来做“换壳复用”。
- 不在第一阶段强行统一 Chat Agent 辅助配置与 Skill Debug UI。
## 命名决策
新设计不继续使用 `HelperBot` 和 `TopAgent` 作为核心命名:
- Chat source type:`chatAgentHelper`。
- 前端场景名:`ChatAgentHelper`。
- 服务端 scene adapter:`chatAgentHelperSceneAdapter`。
- 业务 processor:`chatAgentConfigAssistantProcessor` 或 `chatAgentConfigProcessor`。
- SSE 配置事件:建议从 `topAgentConfig` 改为 `chatAgentConfig`。
- API 路径:建议从 `/proApi/core/chat/helperBot/completions` 改为 `/proApi/core/chat/chatAgentHelper/completions`,旧路径是否保留兼容由迁移策略决定。
`helperBot` 和 `topAgent` 只作为旧实现名出现在迁移说明中,新代码不再新增这两个命名。
## 当前迁移边界
本轮只重构 Chat Agent 辅助配置生成链路:
- 不迁移 Skill 详情页辅助生成逻辑。
- 不改变 Skill Debug 当前 `sourceType=skillEdit`。
- 不改变 Skill Debug 当前 `workflowStart -> agent` 调试运行链路。
- 仅预留 Skill 未来接入所需的 scene/runtime/agent loop 扩展点。
## 现状问题
### 1. Chat Agent 辅助配置运行语义与 workflow 不匹配
Chat Agent 辅助配置当前 runtime 只有一个 internal node,构造 workflow 的目的只是复用横切能力。这让调用链变长:
```txt
chatAgentHelper/completions
-> preChatRound
-> createWorkflowStreamResponseContext
-> internalRuntimeNode
-> dispatchWorkFlow
-> chatAgentConfigAssistant processor
-> runUnifiedAgentLoop
-> pushChatRecords
```
真实业务其实是:
```txt
chatAgentHelper/completions
-> 辅助生成会话管理
-> chatAgentConfigAssistant processor
-> agent loop
-> 保存结果
```
### 2. 计费能力没有独立服务边界
workflow 的 `usagePush` 同时做了:
- usage item 追加。
- usage 总额汇总。
- root/child runtime 隔离。
- node response 关联。
辅助生成只需要其中一部分,但为了复用只能进入 workflow。
### 3. Agent loop 入口还偏底层
`runUnifiedAgentLoop` 是合理的底层 loop,但场景直接调用时还需要自己处理:
- usage 聚合。
- SSE event 映射。
- runtime tools 是否允许。
- check stopping。
- JSON 修复。
- prompt / messages 组装。
Chat Agent 辅助配置已经包装了一层 `runTopAgentPlanningLoop`,但它仍是旧 Chat Agent 场景专属,并且名字仍绑定旧 `TopAgent` 语义,不能直接服务 Skill 辅助生成。
### 4. “辅助生成”缺少领域命名
现在核心模块叫 HelperBot,而它本质只是 App 编辑页上的 Chat Agent 辅助配置入口。后续 Skill 辅助生成如果复用 HelperBot 命名,会继续扩大语义偏差。因此新抽象必须统一收敛到 `auxiliaryGeneration`,Chat Agent 辅助配置只是其中一个 scene。
## 目标架构
新增领域:`Auxiliary Generation`。
建议先使用收敛结构,不提前为 Skill 迁入拆太多目录。核心约束:`packages/service/core/ai/auxiliaryGeneration/**` 禁止 import `packages/service/core/workflow/**`,`packages/global/core/ai/auxiliaryGeneration/**` 禁止 import `packages/global/core/workflow/**`。
```txt
packages/global/core/ai/auxiliaryGeneration/
constants.ts
type.ts
packages/service/core/ai/auxiliaryGeneration/
index.ts
type.ts
service.ts # runAuxiliaryGeneration,总生命周期编排
stream.ts # 自有 SSE + resume + stream error
usage.ts # 余额检查、usage 创建和追加
agentLoop.ts # runAuxiliaryAgentLoop
error.ts
utils.ts
pro/admin/src/service/core/ai/auxiliaryGeneration/
chatAgentHelper/
service.ts
type.ts
schema.ts
prompt.ts
processor.ts
```
公共 `packages/service` 只放稳定横切能力:stream、usage、agent loop wrapper、生命周期 runner。Pro 侧放具体场景逻辑,避免公共包反向依赖 Pro,也避免把差异化逻辑抽象成空泛接口。
### 为什么不拆 `chat/`、`file/`、`runtime/`
- `chat`:Chat Agent 辅助配置和未来 Skill 辅助生成的历史读取、interactive 续写、成功保存策略都不同。公共层只要求 scene 提供 `loadContext/saveSuccess/saveFailure` 回调,不单独建 `chat/`。
- `file`:Chat Agent 辅助配置只处理聊天上传文件;Skill 辅助生成会处理 workspace 文件读写、刷新、测试输出。差异过大,不提前抽公共 `file/`。
- `runtime`:Chat Agent 辅助配置不启 VM;Skill 辅助生成才需要 edit sandbox 和 system skill 注入。runtime 目录等 Skill 迁入时放到 Skill 场景内,确认复用后再上移。
### 文件职责
- `service.ts`:统一入口 `runAuxiliaryGeneration`,只负责编排固定横切流程。
- `stream.ts`:辅助生成自己的 SSE、resume、错误输出;不包装 workflow stream。
- `usage.ts`:余额检查、usage 创建、usage item push、汇总。
- `agentLoop.ts`:统一 agent loop wrapper,调用 `runUnifiedAgentLoop`,处理 usageSink、事件映射、JSON repair。
- `chatAgentHelper/service.ts`:Chat Agent 辅助配置场景 glue,包含鉴权、历史读取、文件授权、interactive 续写、记录保存。
- `chatAgentHelper/processor.ts`:业务生成逻辑,负责把模型输出解析为 collection/generation 结果。
### Skill 迁入后的结构调整
Skill 迁入时不改公共入口,只在 Pro 侧新增 Skill 场景目录:
```txt
pro/admin/src/service/core/ai/auxiliaryGeneration/
skillAssist/
service.ts # authSkill、历史读取、保存策略
runtime.ts # edit sandbox 激活、system skill 注入
workspaceFile.ts # 工作区文件读写、文件树刷新所需元数据
tools.ts # 给 agent loop 的文件/测试 runtime tools
processor.ts
prompt.ts
schema.ts
type.ts
```
只有当 `chatAgentHelper` 和 `skillAssist` 出现真实重复实现,才把对应能力上移到公共层。例如:
- 两边都需要同一种 stream resume 细节:留在公共 `stream.ts`。
- 两边都需要同一种 usage 统计:留在公共 `usage.ts`。
- 两边文件处理不同:继续留在各自 scene。
- 两边保存 interactive 不同:继续留在各自 scene。
## 核心抽象
### AuxiliaryGenerationSession
建议用类或闭包工厂,不建议用 React hook。这里的核心问题在服务端:鉴权、usage、chat round、stream、VM runtime 都是服务端能力。前端可以后续补一个轻量 hook,但不应承担主抽象。
建议入口:
```ts
export const runAuxiliaryGeneration = async <TInput, TResult>({
req,
res,
scene,
input,
processor
}: RunAuxiliaryGenerationProps<TInput, TResult>): Promise<TResult> => {
// 统一生命周期
};
```
生命周期:
```txt
parse input(route 层完成)
-> scene.authenticate()
-> scene.rateLimit()
-> scene.assertAccess()
-> usage.checkBalance()
-> scene.loadContext()
-> scene.prepareRound()
-> stream.create()
-> usage.createUsage()
-> scene.prepareRuntime(optional)
-> processor.run()
-> usage.flush()
-> scene.saveSuccess()
-> stream.done()
```
错误生命周期:
```txt
catch error
-> scene.saveFailure() // prepared round 后才执行
-> stream.error() // stream 已创建时执行
-> plain sse error // stream 未创建时执行
```
### Scene Adapter
不同场景差异通过 adapter 表达,不写死 Chat Agent 辅助配置。
```ts
export type AuxiliaryGenerationSceneAdapter<TInput, TAuth, TUserInput> = {
sceneType: AuxiliaryGenerationSceneEnum;
getSource: (input: TInput) => ChatSourceTarget;
authenticate: (props: AuthProps<TInput>) => Promise<TAuth>;
rateLimit?: (props: RateLimitProps<TInput, TAuth>) => Promise<void>;
assertChatAccess: (props: AssertChatAccessProps<TInput, TAuth>) => Promise<void>;
loadContext: (props: LoadContextProps<TInput, TAuth>) => Promise<AuxiliaryGenerationContext<TUserInput>>;
prepareRound: (props: PrepareRoundProps<TInput, TAuth, TUserInput>) => Promise<PreChatRoundResult>;
prepareRuntime?: (props: PrepareRuntimeProps<TInput, TAuth, TUserInput>) => Promise<unknown>;
saveSuccess: (props: SaveSuccessProps<TInput, TAuth, TUserInput, TResult>) => Promise<void>;
saveFailure: (props: SaveFailureProps<TInput, TAuth>) => Promise<void>;
};
```
Chat Agent 辅助配置场景:
- `sourceType=chatAgentHelper`
- `sourceId=appId`
- `authenticate=authApp(ReadPermissionVal)`
- 历史读取 40 条。
- 使用 Chat Agent 辅助配置专用 interactive append 保存逻辑。
Skill 辅助生成场景(本轮只预留,不迁移):
- 初始建议:`sourceType=skillEdit`
- `sourceId=skillId`
- `authenticate=authSkill(WritePermissionVal)`
- 历史可以复用 Skill edit chat。
- runtime prepare 负责获取 Skill edit sandbox 并注入官方 `skill-creator`。
### Usage Manager
实现辅助生成专用 usage 管理:
```ts
export type AuxiliaryGenerationUsageManager = {
checkBalance: () => Promise<void>;
createUsage: () => Promise<string | undefined>;
push: (usages: ChatNodeUsageType[]) => void;
flush: () => Promise<AuxiliaryGenerationUsageSummary>;
};
```
职责:
- 调用 `checkTeamAIPoints(teamId)`。
- 调用 `createChatUsageRecord` 创建 usage。
- 调用 `pushChatItemUsage` 追加 item。
- 汇总 `totalPoints/inputTokens/outputTokens/model`。
- 支持 `disabled` 或 `deferCreate`,方便某些场景只统计不扣费。
建议第一阶段沿用现有账单来源枚举:
- Chat Agent 辅助配置:`UsageSourceEnum.fastgpt`,`appId=sourceId`,`appName` 建议写真实 App 名称,并在 usage item 的 `moduleName` 中写 `Chat Agent helper`,方便账单归因。
- Skill 辅助生成:本轮不迁移;未来建议 `UsageSourceEnum.fastgpt`,`skillId=sourceId`,`appName` 写 Skill 名称。
余额不足行为:
1. 辅助生成会话在 LLM 调用前执行 `checkTeamAIPoints(teamId)`。
2. 如果抛出 `TeamErrEnum.aiPointsNotEnough`,直接通过 SSE error 或普通 API error 返回同名 `statusText`。
3. 前端沿用现有 request/stream 错误处理,触发 `NotSufficientModal`。
4. 不复用 workflow 的 `paymentPause` interactive。
原因:
- 当前普通 API 已经会把 `TeamErrEnum.aiPointsNotEnough` 映射到充值弹窗。
- workflow 的 `paymentPause` 是“节点执行暂停并可续跑”的语义,辅助生成不需要恢复到某个节点。
- 如果继续伪造 `paymentPause`,会把新抽象再次绑回 workflow interactive。
### Stream Context
新增辅助生成 stream context。它是独立实现,不包装 `createWorkflowStreamResponseContext`,也不暴露 `WorkflowResponseType`。
```ts
export type AuxiliaryGenerationStream = {
write: (event: AuxiliaryGenerationStreamEvent) => void;
writeAnswerDelta: (payload: AuxiliaryGenerationAnswerDelta) => void;
writeReasoningDelta: (payload: AuxiliaryGenerationReasoningDelta) => void;
writeInteractive: (interactive: AuxiliaryGenerationInteractive) => void;
writeConfig: (config: AuxiliaryGenerationConfigPayload) => void;
writeError: (error: AuxiliaryGenerationErrorPayload) => void;
close: () => void;
flushResume: () => Promise<void>;
};
```
事件定义放在 `packages/global/core/ai/auxiliaryGeneration/event.ts`:
```ts
export enum AuxiliaryGenerationEventEnum {
answer = 'answer',
reasoning = 'reasoning',
interactive = 'interactive',
config = 'config',
usage = 'usage',
error = 'error',
done = 'done'
}
```
底层 SSE 格式可以继续用标准 `event: xxx\ndata: xxx\n\n`,但事件枚举和 payload 都属于 auxiliary generation,不放在 workflow runtime constants 里。
### Interactive Model
辅助生成定义自己的交互模型,避免继续依赖 workflow interactive。
```ts
export type AuxiliaryGenerationInteractive =
| AuxiliaryGenerationFormInteractive
| AuxiliaryGenerationAskInteractive;
export type AuxiliaryGenerationFormInteractive = {
type: 'form';
id: string;
description?: string;
submitted?: boolean;
fields: AuxiliaryGenerationFormField[];
};
export type AuxiliaryGenerationFormField = {
key: string;
label: string;
inputType: 'text' | 'number' | 'select' | 'multipleSelect' | 'file';
required?: boolean;
value?: unknown;
options?: { label: string; value: string }[];
};
```
ChatBox 可以在前端做一层 adapter:
- 辅助生成消息渲染读取 `AuxiliaryGenerationInteractive`。
- 如果短期还复用现有交互 UI,adapter 只存在前端组件层,不允许服务端辅助生成模块 import workflow 类型。
- 保存到 chat item 时保存辅助生成自己的 interactive payload;不要伪装成 workflow interactive。
### Runtime Manager
辅助生成场景可选准备 VM 和系统 Skill。
```ts
export type AuxiliaryGenerationRuntimeManager = {
prepare?: (props: AuxiliaryGenerationRuntimePrepareProps) => Promise<AuxiliaryGenerationRuntime>;
cleanup?: (runtime: AuxiliaryGenerationRuntime) => Promise<void>;
};
```
返回值:
```ts
export type AuxiliaryGenerationRuntime = {
sandboxClient?: AgentSandboxClient;
currentWorkingDirectory?: string;
injectedSystemSkills?: SystemSkillInfo[];
runtimeTools?: AgentLoopRuntimeTool[];
};
```
Chat Agent 辅助配置:
- 默认不准备 VM。
- 默认不注入 system skill。
- 继续只生成配置建议。
Skill 辅助生成(本轮不迁移):
- 获取当前 Skill edit sandbox。
- 激活 VM。
- 注入官方 `skill-creator`。
- 可注册文件读写、运行测试、查看目录等 runtime tools。
### Agent Loop Runner
新增统一入口,不替代 `runUnifiedAgentLoop`,而是作为上层场景友好的 wrapper。
```ts
export const runAuxiliaryAgentLoop = async ({
teamId,
tmbId,
model,
systemPrompt,
messages,
tools,
runtime,
stream,
usage,
checkIsStopping,
output
}: RunAuxiliaryAgentLoopProps): Promise<AuxiliaryAgentLoopResult> => {
// 调 runUnifiedAgentLoop
};
```
统一处理:
- 模型能力读取:vision/audio/video。
- reasoning delta 到 SSE。
- answer delta 是否透传由场景决定。
- usageSink 汇总。
- runtime tools 开关。
- updatePlan tool 开关。
- ask tool 开关。
- abort/stopping。
- 可选 JSON parse + repair。
Chat Agent 辅助配置使用方式:
```ts
const result = await runAuxiliaryAgentLoop({
mode: 'planning',
tools: {
runtimeTools: [],
updatePlan: true,
ask: false
},
output: {
type: 'json',
schema: ChatAgentConfigAssistantAnswerSchema,
repair: true
}
});
```
Skill creator 使用方式:
```ts
const result = await runAuxiliaryAgentLoop({
mode: 'editing',
tools: {
runtimeTools: skillCreatorRuntimeTools,
updatePlan: true,
ask: true
},
runtime: skillEditRuntime,
output: {
type: 'assistantMessages'
}
});
```
## Chat Agent 辅助配置重构后流程
```txt
POST /proApi/core/chat/chatAgentHelper/completions
-> parseApiInput(ChatAgentHelperCompletionsParamsSchema)
-> runAuxiliaryGeneration({
scene: chatAgentHelperSceneAdapter,
processor: chatAgentConfigAssistantProcessor
})
```
内部:
```txt
authApp(ReadPermissionVal)
-> assert chatAgentHelper chat access
-> rate limit
-> checkTeamAIPoints
-> read chatAgentHelper histories
-> parse ChatBox messages
-> preChatRound
-> resolve authorized files
-> create auxiliary stream (own SSE context)
-> create usage record
-> chatAgentConfigAssistantProcessor.run
-> usage.flush
-> chatAgentHelper saveSuccess
-> stream.done
```
保留不变:
- 前端收到 Chat Agent 配置事件后 `onApply` 的机制。
- Chat Agent 辅助配置专用 `sourceType=chatAgentHelper`。
- Chat Agent 辅助配置专用文件 key 授权。
- Chat Agent 辅助配置 interactive append 保存行为。
移除:
- `internalRuntimeNode`。
- Chat Agent 辅助配置 route 中直接调用 `dispatchWorkFlow`。
- Chat Agent 辅助配置 processor 入参里的 `workflowResponseWrite` 命名。
- `createWorkflowStreamResponseContext`。
- `WorkflowResponseType`。
- workflow interactive payload。
- workflow nodeResponse 持久化。
## sourceType 迁移注意事项
`ChatSourceTypeEnum.helperBot` 改为 `ChatSourceTypeEnum.chatAgentHelper` 会影响多个边界:
- chat 三表查询与写入。
- chat file S3 key 授权。
- resume key。
- sandbox id/runtime guard 中对 helper source 的禁止逻辑。
- OpenAPI schema 与前端 `ChatSourceTarget`。
- 前端 `streamFetch` 的 resume header 白名单、resume query 拼接、SSE event 类型解析。
- `SseResponseEventEnum.topAgentConfig` 需要改名或新增 `chatAgentConfig`。
- 旧聊天记录读取。
建议实现策略:
1. 新代码只写入 `chatAgentHelper`。
2. 旧 `helperBot` enum 不再作为新 API 对外暴露。
3. 是否兼容历史数据需要产品确认:
- 如果历史辅助配置对话可丢弃:直接不兼容旧 `helperBot` 记录。
- 如果需要保留历史:查询层短期同时读取 `helperBot/chatAgentHelper`,保存层只写 `chatAgentHelper`,并提供一次性数据迁移。
4. S3 key 如果包含 `helperBot` 路径,也需要决定是否兼容旧文件预览;新上传统一写 `chatAgentHelper`。
## 进一步风险与处理
### 1. streamFetch 余额错误不会自动走 axios 拦截器
普通 `GET/POST` 请求已经会把 `TeamErrEnum.aiPointsNotEnough` 映射到充值弹窗,但辅助生成走 `streamFetch`。因此新 stream error 必须满足:
- 服务端 SSE error payload 或非 200 JSON payload 中保留 `statusText=TeamErrEnum.aiPointsNotEnough`。
- `streamFetch` 在 `SseResponseEventEnum.error` 和 `onopen(!ok)` 两条路径都识别该 statusText,并调用 `setNotSufficientModalType`。
- 如果 stream 已经创建且本轮 chat round 已 prepared,余额错误应写失败态,不写 AI 正文。
### 2. 断流恢复需要从旧 chat stream 白名单里接入
当前 `streamFetch` 只给旧 helper route 加 resume header,`streamResumeFetch` 也只识别 `sourceType=helperBot`。迁移时需要:
- 新 URL `/api/proApi/core/chat/chatAgentHelper/completions` 加入 resume header 白名单。
- resume query 支持 `sourceType=chatAgentHelper`。
- 旧 `helperBot` 是否可 resume 取决于历史兼容策略。
- resume 服务端实现使用辅助生成自己的 stream resume,不从 workflow stream context 引入。
### 3. 配置事件需要保留前端应用时序
旧链路收到 `topAgentConfig` 后立即 `onApply(formData)`。新事件 `chatAgentConfig` 需要保证:
- 配置事件先于最终 `[DONE]`。
- 配置事件不进入普通 answer 打字队列。
- generation 阶段保存的 AI 消息与前端已应用配置一致。
### 4. interactive 续写不能退化
Chat Agent 辅助配置 collection 阶段会生成表单,用户提交后不是普通新一轮问答,而是要回填上一条 AI 消息里的 interactive 表单并 append 新响应。新的 session runner 必须显式支持:
- `preparedRound.shouldFinalizePreparedRound=false` 的续写路径。
- 根据 `responseChatItemId` 找到旧 AI item。
- 表单提交态回填。
- 新 AI response append 到同一条 AI item。
### 5. nodeResponse 可以不作为第一阶段目标
旧链路通过 workflow 获得 nodeResponse writer,但 Chat Agent 辅助配置 UI 目前不展示运行节点明细。新设计不再提供 workflow nodeResponse。第一阶段建议:
- 保留 usage item。
- 保留 chatGenerateStatus、stream resume、错误状态。
- 不持久化 workflow nodeResponse 明细,也不构造伪 nodeResponse。
如果后续需要“辅助生成运行详情”,应设计独立 `auxiliary_generation_steps` 或轻量 metadata,不要重新依赖 workflow nodeResponse。
## Skill 辅助生成预留流程
未来 Skill Detail 左侧聊天可按意图拆分:
```txt
用户普通调试当前 Skill
-> 继续走 skill/debugChat
-> workflowStart -> agent
用户请求创建/修改 Skill 文件
-> 走 skill/assist 或 skill/debugChat 内部路由到辅助生成
-> runAuxiliaryGeneration(skillEditSceneAdapter, skillCreatorProcessor)
```
推荐先新增独立 API:
```txt
POST /proApi/core/ai/skill/assist
```
原因:
- 避免一开始在 `skill/debugChat` 里做复杂意图识别。
- 便于单独控制权限、限频、VM 注入和保存行为。
- 前端可以先用显式入口或按钮接入,等稳定后再融合到原聊天框。
后续再考虑自动路由:
- 用户输入命中“创建/修改 Skill 文件”意图,前端或服务端转辅助生成。
- 其他输入继续走 Skill Debug。
## 模块边界
### packages/global
放共享枚举和 schema:
- `AuxiliaryGenerationSceneEnum`
- 通用 stream event type。
- 前端需要识别的辅助生成事件类型。
不放服务端鉴权、VM、usage 实现。
### packages/service
放通用服务端能力:
- `runAuxiliaryGeneration`
- `createAuxiliaryGenerationUsageManager`
- `createAuxiliaryGenerationStreamContext`
- `runAuxiliaryAgentLoop`
- 辅助生成通用错误与事件 payload。
不能引用 Pro 目录。
### pro/admin
放具体场景和 processor:
- `chatAgentHelper/service.ts`
- `chatAgentHelper/processor.ts`
- 未来 `skillAssist/service.ts`
- 未来 `skillAssist/runtime.ts`
- 未来 `skillAssist/tools.ts`
- 官方 system skill 源码和注入列表。
## 迁移步骤
### 阶段 1:建立辅助生成基础设施
- 新增 `AuxiliaryGenerationEventEnum`、stream payload、interactive payload。
- 新增辅助生成专用 SSE stream context。
- 新增 `AuxiliaryGenerationUsageManager`。
- 新增 `runAuxiliaryGeneration` 生命周期编排。
- 新增 `runAuxiliaryAgentLoop`。
- 单测覆盖:
- stream answer/reasoning/config/interactive/error/done 事件。
- stream error 中 `TeamErrEnum.aiPointsNotEnough` payload。
- usage 创建、push、flush。
- session 成功/失败/abort 生命周期。
- runtime tools 为空时不会执行工具。
- updatePlan 可用。
- reasoning SSE 正常透传。
- JSON parse repair 成功/失败。
### 阶段 2:迁移 Chat Agent 辅助配置
- 新增 `/proApi/core/chat/chatAgentHelper/completions`。
- 新增 `ChatSourceTypeEnum.chatAgentHelper`。
- 新增 `chatAgentHelperSceneAdapter`。
- `chatAgentConfigAssistantProcessor` 改用 `runAuxiliaryAgentLoop`。
- 新 route 直接调用 `runAuxiliaryGeneration`,不再经过 workflow。
- 前端 `streamFetch` 支持辅助生成事件和余额错误。
- 对比旧行为:
- chat 记录一致。
- usage item 一致。
- Chat Agent 配置事件一致。
- interactive 提交和 append 一致。
### 阶段 3:清理旧命名和旧入口
- 删除或废弃 `/proApi/core/chat/helperBot/completions`。
- 删除 Chat Agent 辅助配置中的 `internalRuntimeNode` 构造代码。
- 删除旧 `topAgentConfig` 事件,或保留前端兼容分支一个版本周期。
- 删除新代码中的 HelperBot/TopAgent 命名。
- 按确认结果处理旧 `helperBot` chat/source/S3 数据兼容。
### 阶段 4:Skill 辅助生成接入
本轮不执行。这里只保留未来方向:
- 新增 Skill assist scene adapter。
- Runtime manager 激活 Skill edit sandbox。
- 注入官方 `skill-creator`。
- Processor 注册文件读写/测试 runtime tools。
- 前端 Skill Detail 接入辅助生成入口。
### 阶段 5:命名清理
- 新代码不再使用 HelperBot/TopAgent 命名。
- 服务端核心能力统一迁移到 `auxiliaryGeneration`。
- 删除 Chat Agent 辅助配置链路中的 `workflowResponseWrite` 类型引用,统一改为辅助生成 stream writer。
## 测试计划
### 单元测试
- `runAuxiliaryAgentLoop`
- 无 runtime tools。
- 有 runtime tools。
- usageSink 汇总。
- reasoning/answer event 映射。
- JSON schema parse + repair。
- abort/stopping。
- `AuxiliaryGenerationUsageManager`
- 余额不足。
- 创建 usage。
- push 多次 usage item。
- flush 汇总。
- `runAuxiliaryGeneration`
- 成功生命周期调用顺序。
- processor 抛错后 failChatRound。
- stream 创建前抛错。
- stream 创建后抛错。
- prepared round 之前抛错不写失败记录。
- `chatAgentHelperSceneAdapter`
- chat 权限校验。
- interactive append 保存。
- 文件 key 授权。
### 集成测试
- Chat Agent 辅助配置 collection 阶段返回 interactive。
- Chat Agent 辅助配置 generation 阶段返回配置事件并保存 AI 消息。
- Chat Agent 辅助配置 usage 写入与旧链路一致。
- Chat Agent 辅助配置余额不足时返回 `TeamErrEnum.aiPointsNotEnough`,前端触发余额弹窗。
- Chat Agent 辅助配置断流 resume 行为不退化。
### 后续 Skill 测试
- Skill assist 能激活 edit sandbox。
- system skill 注入幂等。
- 文件修改后右侧文件树可刷新。
- 发布版本不包含 system skill。
## 待确认问题
1. `helperBot` 历史聊天和旧 S3 文件是否需要兼容读取?如果不需要,迁移会简单很多。
2. Chat Agent 辅助配置 usage 的 `appName` 是否确认写真实 App 名称,`moduleName` 写 `Chat Agent helper`?
3. 新 SSE 事件名是否定为 `chatAgentConfig`?
4. 新 API 是否定为 `/proApi/core/chat/chatAgentHelper/completions`,旧 `/helperBot/completions` 是否保留一个版本周期?
5. 辅助生成是否需要保留 nodeResponse 明细展示?如果不需要,usage 和运行详情可以明显简化。
6. Skill 辅助生成的 API、sourceType、system skill 注入范围作为后续议题,本轮不迁移。
## TODO
- [ ] 确认待确认问题。
- [ ] 编写 `runAuxiliaryAgentLoop` 详细开发文档。
- [ ] 编写 `runAuxiliaryGeneration` 生命周期开发文档。
- [ ] 实现阶段 1:Chat Agent 配置生成 processor 迁移到统一 loop runner。
- [ ] 实现阶段 2:落地独立 usage/stream/session runner,并迁移 Chat Agent 辅助配置 route。
- [ ] 实现阶段 3:Chat Agent 辅助配置 completions 移除 workflow internal node。
- [ ] 补齐 Chat Agent 辅助配置行为回归测试。
- [ ] 设计 Skill assist API 和 Skill edit runtime 注入细节。
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -180,7 +180,6 @@ These variables are mainly validated by `packages/service/env.ts` and apply to `
| Variable | Default | Description |
| -------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AGENT_ENGINE` | `default` | Agent engine. Supported values are `default` and `pi`. |
| `HELPER_BOT_MODEL` | Empty | Helper generation model. The model must be enabled in the system. |
| `SKIP_FILE_TYPE_CHECK` | `false` | Whether upload file type checks are skipped. |
| `WECHAT_CHANNEL_CONCURRENCY` | `1000` | WeChat channel poll worker concurrency. Minimum value is `10`. |
| `PARSE_FILE_WORKERS` | `10` | Resident file parsing worker count. |
......
......@@ -180,7 +180,6 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
| 变量 | 默认值 | 说明 |
| -------------------------------------- | --------- | ---------------------------------------------------------------------------------------------- |
| `AGENT_ENGINE` | `default` | Agent 引擎,可选 `default` 或 `pi`。 |
| `HELPER_BOT_MODEL` | 空 | 辅助生成模型,需保证系统中已启用对应模型。 |
| `SKIP_FILE_TYPE_CHECK` | `false` | 是否跳过上传文件类型检查。 |
| `WECHAT_CHANNEL_CONCURRENCY` | `1000` | 微信渠道 poll worker 并发数,最小 `10`。 |
| `PARSE_FILE_WORKERS` | `10` | 文件解析 worker 常驻线程数。 |
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
......@@ -130,7 +130,6 @@ x-service-env-config: &x-service-env-config
MAX_LOGIN_SESSION:
ALLOWED_ORIGINS:
AGENT_ENGINE: default
HELPER_BOT_MODEL: qwen-max
CHAT_TITLE_MODEL:
SKIP_FILE_TYPE_CHECK: false
WECHAT_CHANNEL_CONCURRENCY: 1000
......
import {
AppTypeEnum,
defaultAppSelectFileConfig,
defaultQGConfig,
defaultWhisperConfig
} from '../../app/constants';
import type { AppQGConfigType, AppWhisperConfigType } from '../../app/type';
import type { AppFileSelectConfigType } from '../../app/type/config.schema';
export const ChatAgentHelperAppName = 'Chat Agent Helper';
export const ChatAgentHelperAvatar = '/imgs/bot.svg';
export const chatAgentHelperFileSelectConfig: AppFileSelectConfigType = {
...defaultAppSelectFileConfig,
maxFiles: 10,
canSelectFile: true,
canSelectImg: true,
canSelectVideo: true,
canSelectAudio: true,
customPdfParse: false,
canSelectCustomFileExtension: true,
customFileExtensionList: []
};
export const chatAgentHelperQuestionGuideConfig: AppQGConfigType = {
...defaultQGConfig,
open: false
};
export const chatAgentHelperWhisperConfig: AppWhisperConfigType = {
...defaultWhisperConfig,
open: false
};
/** 创建 ChatAgentHelper 专用 Chat 配置,避免调用方共享并误改全局对象。 */
export const createChatAgentHelperChatConfig = () => {
return {
fileSelectConfig: {
...chatAgentHelperFileSelectConfig,
customFileExtensionList: [...(chatAgentHelperFileSelectConfig.customFileExtensionList ?? [])]
},
questionGuide: {
...chatAgentHelperQuestionGuideConfig
},
whisperConfig: {
...chatAgentHelperWhisperConfig
}
};
};
/** 创建 ChatAgentHelper 在通用 ChatBox 中使用的轻量 App 信息。 */
export const createChatAgentHelperAppConfig = () => {
return {
chatConfig: createChatAgentHelperChatConfig(),
chatModels: [],
name: ChatAgentHelperAppName,
avatar: ChatAgentHelperAvatar,
intro: '',
type: AppTypeEnum.simple,
pluginInputs: []
};
};
export enum AuxiliaryGenerationEventEnum {
answer = 'answer',
error = 'error',
status = 'status',
interactive = 'interactive',
chatAgentConfig = 'chatAgentConfig'
}
export enum ChatAgentHelperTypeEnum {
chatAgent = 'chatAgent'
}
import z from 'zod';
import { ChatCompletionMessageParamSchema } from '../llm/type';
import { ChatFileTypeEnum } from '../../chat/constants';
import { SelectedAgentSkillItemTypeSchema } from '../../app/formEdit/type';
import { ObjectIdSchema } from '../../../common/type/mongo';
import { ChatAgentHelperTypeEnum } from './constants';
import { BoolSchema } from '../../../common/zod';
export const AuxiliaryGenerationChatFileSchema = z.object({
type: z.enum(ChatFileTypeEnum),
key: z.string(),
url: z.string().optional(),
name: z.string()
});
export type AuxiliaryGenerationChatFileType = z.infer<typeof AuxiliaryGenerationChatFileSchema>;
export const ChatAgentHelperMetadataSchema = z.object({
role: z.string().nullish(),
taskObject: z.string().nullish(),
systemPrompt: z.string().nullish(),
selectedTools: z.array(z.string()).nullish(),
selectedDatasets: z.array(z.string()).nullish(),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).nullish(),
fileUpload: z.boolean().nullish(),
enableSandbox: z.boolean().nullish(),
modelConfig: z
.object({
model: z.string().optional()
})
.optional()
});
export type ChatAgentHelperMetadataType = z.infer<typeof ChatAgentHelperMetadataSchema>;
export const AuxiliaryGenerationSelectedDatasetSchema = z.object({
datasetId: z.string().meta({
description: '可选知识库 ID'
}),
avatar: z.string().meta({
description: '可选知识库头像'
}),
name: z.string().meta({
description: '可选知识库名称'
}),
vectorModel: z.object({
model: z.string().meta({
description: '知识库使用的向量模型'
})
}),
isDeleted: BoolSchema.optional()
});
export type AuxiliaryGenerationSelectedDatasetType = z.infer<
typeof AuxiliaryGenerationSelectedDatasetSchema
>;
export const ChatAgentConfigFormDataSchema = z.object({
systemPrompt: z.string().optional(),
tools: z.array(z.string()).optional().default([]),
datasets: z.array(AuxiliaryGenerationSelectedDatasetSchema).optional().default([]),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).optional().default([]),
fileUploadEnabled: z.boolean().optional().default(false),
enableSandboxEnabled: z.boolean().optional().default(false),
executionPlan: z.any().optional()
});
export type ChatAgentConfigFormDataType = z.infer<typeof ChatAgentConfigFormDataSchema>;
export const ChatAgentHelperCompletionsParamsSchema = z
.object({
chatId: z.string(),
responseChatItemId: z.string(),
appId: ObjectIdSchema,
messages: z.array(ChatCompletionMessageParamSchema),
// ChatBox 当前仍复用 workflow interactive 数据结构;辅助生成 API 只透传给 chat round 准备逻辑。
interactive: z.any().optional(),
metadata: z.object({
type: z.literal(ChatAgentHelperTypeEnum.chatAgent),
data: ChatAgentHelperMetadataSchema
})
})
.superRefine(({ messages }, ctx) => {
const lastUserMessage = messages.findLast((message) => message.role === 'user');
if (!lastUserMessage?.dataId) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['messages'],
message: 'ChatAgentHelper messages requires a user message with dataId'
});
}
});
export type ChatAgentHelperCompletionsParamsType = z.infer<
typeof ChatAgentHelperCompletionsParamsSchema
>;
import type { LLMModelItemType } from '../model.schema';
import { ChatCompletionRequestMessageRoleEnum } from '../constants';
export const removeDatasetCiteText = (text: string, retainDatasetCite: boolean) => {
return retainDatasetCite
......@@ -15,6 +16,44 @@ export const removeDatasetCiteText = (text: string, retainDatasetCite: boolean)
export const normalizeToolResponseContent = (response?: string) =>
response === '' || response === undefined ? 'none' : response;
/**
* 构造 OpenAI Chat Completions 风格的流式 delta 响应片段。
*
* FastGPT 多个 SSE 场景都会向前端输出这种结构,统一放在 LLM 公共层避免各业务重复维护。
*/
export const createChatCompletionDeltaResponse = ({
text,
reasoningContent,
model = '',
finishReason = null,
extraData = {}
}: {
model?: string;
text?: string | null;
reasoningContent?: string | null;
finishReason?: null | 'stop';
extraData?: object;
}) => {
return {
...extraData,
id: '',
object: '',
created: 0,
model,
choices: [
{
delta: {
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: text,
...(reasoningContent ? { reasoning_content: reasoningContent } : {})
},
index: 0,
finish_reason: finishReason
}
]
};
};
export const getLLMSupportParams = (llm?: LLMModelItemType) => {
return {
vision: !!llm?.vision,
......
......@@ -81,7 +81,6 @@ export const LLMModelItemSchema = PriceTypeSchema.extend(BaseModelItemSchema.sha
isDefaultDatasetTextModel: z.boolean().optional(),
isDefaultDatasetImageModel: z.boolean().optional(),
isDefaultChatTitleModel: z.boolean().optional(),
isDefaultHelperBotModel: z.boolean().optional(),
/** @deprecated */
datasetProcess: z.boolean().optional(), // dataset
......
......@@ -45,7 +45,8 @@ export enum ChatSourceEnum {
*/
export enum ChatSourceTypeEnum {
app = 'app',
skillEdit = 'skillEdit'
skillEdit = 'skillEdit',
chatAgentHelper = 'chatAgentHelper'
}
export const ChatSourceMap = {
......
import { ChatCompletionRequestMessageRoleEnum } from '../../ai/constants';
import type { ChatCompletionContentPart, ChatCompletionMessageParam } from '../../ai/llm/type';
import { ChatFileTypeEnum, ChatRoleEnum } from '../constants';
import type { HelperBotChatItemType } from './type';
import { isFileUrlChatFileType, simpleUserContentPart } from '../adapt';
export const helperChats2GPTMessages = ({
messages
}: {
messages: HelperBotChatItemType[];
}): ChatCompletionMessageParam[] => {
let results: ChatCompletionMessageParam[] = [];
messages.forEach((item) => {
if (item.obj === ChatRoleEnum.System) {
const content = item.value?.[0]?.text?.content;
if (content) {
results.push({
role: ChatCompletionRequestMessageRoleEnum.System,
content
});
}
} else if (item.obj === ChatRoleEnum.Human) {
const value = item.value
.map((item) => {
if (item.text) {
return {
type: 'text',
text: item.text?.content || ''
};
}
if (item.file) {
if (item.file?.type === ChatFileTypeEnum.image) {
return {
type: 'image_url',
key: item.file.key,
image_url: {
url: item.file.url
}
};
} else if (isFileUrlChatFileType(item.file?.type)) {
return {
type: 'file_url',
name: item.file?.name || '',
url: item.file.url,
fileType: item.file.type,
key: item.file.key
};
}
}
})
.filter(Boolean) as ChatCompletionContentPart[];
results.push({
role: ChatCompletionRequestMessageRoleEnum.User,
content: simpleUserContentPart(value)
});
} else {
const aiResults: ChatCompletionMessageParam[] = [];
//AI: 只需要把根节点转化即可
item.value.forEach((value, i) => {
if ('collectionForm' in value) {
const text = JSON.stringify(
value.collectionForm.params.inputForm.map((item) => ({
label: item.label,
type: item.type
}))
);
// Concat text
const lastValue = item.value[i - 1];
const lastResult = aiResults[aiResults.length - 1];
if (lastValue && typeof lastResult?.content === 'string') {
lastResult.content += text;
} else {
aiResults.push({
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: text
});
}
} else if ('text' in value && typeof value.text?.content === 'string') {
if (!value.text.content && item.value.length > 1) {
return;
}
// Concat text
const lastValue = item.value[i - 1];
const lastResult = aiResults[aiResults.length - 1];
if (lastValue && typeof lastResult?.content === 'string') {
lastResult.content += value.text.content;
} else {
aiResults.push({
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: value.text.content
});
}
}
});
// Auto add empty assistant message
results = results.concat(
aiResults.length > 0
? aiResults
: [
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: ''
}
]
);
}
});
return results;
};
import z from 'zod';
import { SelectedDatasetSchema } from '../../../workflow/type/io';
import { SelectedAgentSkillItemTypeSchema } from '../../../app/formEdit/type';
// TopAgent 参数配置
export const topAgentParamsSchema = z.object({
role: z.string().nullish(),
taskObject: z.string().nullish(),
systemPrompt: z.string().nullish(),
selectedTools: z.array(z.string()).nullish(),
selectedDatasets: z.array(z.string()).nullish(),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).nullish(),
fileUpload: z.boolean().nullish(),
enableSandbox: z.boolean().nullish()
});
export type TopAgentParamsType = z.infer<typeof topAgentParamsSchema>;
export const TopAgentFormDataSchema = z.object({
systemPrompt: z.string().optional(),
tools: z.array(z.string()).optional().default([]),
datasets: z.array(SelectedDatasetSchema).optional().default([]),
selectedAgentSkills: z.array(SelectedAgentSkillItemTypeSchema).optional().default([]),
fileUploadEnabled: z.boolean().optional().default(false),
enableSandboxEnabled: z.boolean().optional().default(false),
executionPlan: z.any().optional()
});
export type TopAgentFormDataType = z.infer<typeof TopAgentFormDataSchema>;
import { ObjectIdSchema } from '../../../common/type/mongo';
import z from 'zod';
import { ChatRoleEnum } from '../constants';
import { UserChatItemSchema, SystemChatItemSchema } from '../type';
import { UserInputInteractiveSchema } from '../../workflow/template/system/interactive/type';
export enum HelperBotTypeEnum {
topAgent = 'topAgent'
}
export const HelperBotTypeEnumSchema = z.enum(Object.values(HelperBotTypeEnum));
export type HelperBotTypeEnumType = z.infer<typeof HelperBotTypeEnumSchema>;
export const HelperBotChatSchema = z.object({
_id: ObjectIdSchema,
chatId: z.string(),
type: HelperBotTypeEnum,
userId: z.string(),
createTime: z.coerce.date(),
updateTime: z.coerce.date(),
metadata: z.record(z.string(), z.any()).optional()
});
export type HelperBotChatType = z.infer<typeof HelperBotChatSchema>;
const AIChatContentValueSchema = z
.object({
text: z
.object({
content: z.string()
})
.optional(),
reasoning: z
.object({
content: z.string()
})
.optional(),
hideReason: z.boolean().optional()
})
.refine((value) => value.text || value.reasoning, {
message: 'HelperBot AI content value requires text or reasoning'
});
// AI schema
export const AIChatItemValueItemSchema = z.union([
AIChatContentValueSchema,
z.object({
collectionForm: UserInputInteractiveSchema
}),
z.object({
planHint: z.object({
type: z.literal('generation')
})
})
]);
export type AIChatItemValueItemType = z.infer<typeof AIChatItemValueItemSchema>;
const AIChatItemSchema = z.object({
obj: z.literal(ChatRoleEnum.AI),
value: z.array(AIChatItemValueItemSchema)
});
export type AIChatItemType = z.infer<typeof AIChatItemSchema>;
const HelperBotChatRoleSchema = z.union([
UserChatItemSchema,
SystemChatItemSchema,
AIChatItemSchema
]);
export const HelperBotChatItemSchema = z.intersection(
z.object({
_id: ObjectIdSchema,
userId: z.string(),
chatId: z.string(),
dataId: z.string(),
createTime: z.coerce.date(),
memories: z.record(z.string(), z.any()).nullish()
}),
HelperBotChatRoleSchema
);
export type HelperBotChatItemType = z.infer<typeof HelperBotChatItemSchema>;
/* 客户端 UI User Interface 展示的类型 */
export const HelperBotChatItemSiteSchema = z.intersection(
z.object({
_id: ObjectIdSchema,
dataId: z.string(),
createTime: z.coerce.date()
}),
HelperBotChatRoleSchema
);
export type HelperBotChatItemSiteType = z.infer<typeof HelperBotChatItemSiteSchema>;
......@@ -24,11 +24,7 @@ export enum SseResponseEventEnum {
// Sandbox lifecycle
sandboxStatus = 'sandboxStatus', // sandbox lifecycle phase notification
skillCall = 'skillCall', // skill invocation announce (when SKILL.md is loaded)
// Helperbot
collectionForm = 'collectionForm', // collection form for HelperBot
topAgentConfig = 'topAgentConfig' // form data for TopAgent
skillCall = 'skillCall' // skill invocation announce (when SKILL.md is loaded)
}
export const StreamResumePhaseEvent = 'resumePhase';
......
......@@ -2,7 +2,7 @@ import json5 from 'json5';
import { ChatRoleEnum } from '../../../core/chat/constants';
import type { ChatItemMiniType } from '../../../core/chat/type';
import type { NodeOutputItemType } from './type';
import { ChatCompletionRequestMessageRoleEnum } from '../../ai/constants';
import { createChatCompletionDeltaResponse } from '../../ai/llm/utils';
import {
NodeInputKeyEnum,
NodeOutputKeyEnum,
......@@ -373,24 +373,13 @@ export const textAdaptGptResponse = ({
finish_reason?: null | 'stop';
extraData?: object;
}) => {
return {
...extraData,
id: '',
object: '',
created: 0,
return createChatCompletionDeltaResponse({
text,
reasoningContent: reasoning_content,
model,
choices: [
{
delta: {
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: text,
...(reasoning_content && { reasoning_content })
},
index: 0,
finish_reason
}
]
};
finishReason: finish_reason,
extraData
});
};
/* Update runtimeNode's outputs with interactive data from history */
......
......@@ -19,12 +19,18 @@ const ChatTargetInputShape = {
skillId: ObjectIdSchema.optional().meta({
example: '68ad85a7463006c963799a06',
description: 'Skill Edit 调试 ID。appId、skillId 和 outLinkAuthData 必须且只能传一个。'
}),
sourceType: z.enum([ChatSourceTypeEnum.app, ChatSourceTypeEnum.chatAgentHelper]).optional().meta({
example: ChatSourceTypeEnum.chatAgentHelper,
description:
'appId 对应的会话资源类型。缺省为 App 会话;ChatAgentHelper 会话传 chatAgentHelper。'
})
};
type ChatTargetInput = {
appId?: unknown;
skillId?: unknown;
sourceType?: unknown;
outLinkAuthData?: unknown;
[key: string]: unknown;
};
......@@ -43,10 +49,17 @@ export type ChatTargetInputType =
| {
appId: string;
skillId?: never;
sourceType?: ChatSourceTypeEnum.app;
}
| {
appId?: never;
skillId: string;
sourceType?: never;
}
| {
appId: string;
skillId?: never;
sourceType: ChatSourceTypeEnum.chatAgentHelper;
};
export type ChatTargetResponseType = ChatTargetInputType;
......@@ -75,6 +88,13 @@ const getNormalizedOutLinkAuthData = (data: ChatTargetInput): OutLinkChatAuthPro
export const ChatTargetResponseSchema = z.union([
z.object({
appId: ObjectIdSchema.describe('应用 ID,仅 ChatAgentHelper 会话返回'),
skillId: z.undefined().optional(),
sourceType: z
.literal(ChatSourceTypeEnum.chatAgentHelper)
.describe('ChatAgentHelper 会话资源类型')
}),
z.object({
appId: ObjectIdSchema.describe('应用 ID,仅 App 会话返回'),
skillId: z.undefined().optional()
}),
......@@ -107,6 +127,10 @@ export const buildChatTargetResponse = ({
return { skillId: id };
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
return { appId: id, sourceType: ChatSourceTypeEnum.chatAgentHelper };
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
};
......@@ -119,6 +143,7 @@ export const refineRequiredChatTargetInput = (
data: {
appId?: unknown;
skillId?: unknown;
sourceType?: unknown;
outLinkAuthData?: unknown;
},
ctx: z.RefinementCtx
......@@ -130,6 +155,7 @@ export const refineOptionalChatTargetInput = (
data: {
appId?: unknown;
skillId?: unknown;
sourceType?: unknown;
outLinkAuthData?: unknown;
},
ctx: z.RefinementCtx
......@@ -150,6 +176,7 @@ const refineChatAuthTargetInput = (
) => {
const hasAppTarget = !!data.appId;
const hasSkillTarget = !!data.skillId;
const isChatAgentHelperTarget = data.sourceType === ChatSourceTypeEnum.chatAgentHelper;
const nestedAuthData = getNestedOutLinkAuthData(data);
const shareId = nestedAuthData.shareId;
const outLinkUid = nestedAuthData.outLinkUid;
......@@ -165,6 +192,27 @@ const refineChatAuthTargetInput = (
});
}
if ([hasAppTarget, hasSkillTarget, hasCompleteShareAuth].filter(Boolean).length > 1) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'appId, skillId and share auth cannot be provided at the same time'
});
}
if (isChatAgentHelperTarget && !hasAppTarget) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'sourceType=chatAgentHelper requires appId'
});
}
if (isChatAgentHelperTarget && hasSkillTarget) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'sourceType=chatAgentHelper cannot be used with skillId'
});
}
if (hasShareAuth && !hasCompleteShareAuth) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
......@@ -201,12 +249,18 @@ const refineChatAuthTargetInput = (
* raw schema,避免文档暴露内部 `sourceType/sourceId`。
*/
export const transformChatTargetInput = <T extends ChatTargetInput>(data: T) => {
const { appId, skillId, ...rest } = data;
const { appId, skillId, sourceType: rawSourceType, ...rest } = data;
const sourceId = (appId || skillId) as string;
return {
...rest,
sourceType: appId ? ChatSourceTypeEnum.app : ChatSourceTypeEnum.skillEdit,
sourceType: (() => {
if (skillId) return ChatSourceTypeEnum.skillEdit;
if (rawSourceType === ChatSourceTypeEnum.chatAgentHelper) {
return ChatSourceTypeEnum.chatAgentHelper;
}
return ChatSourceTypeEnum.app;
})(),
sourceId
};
};
......@@ -218,7 +272,7 @@ export const transformChatTargetInput = <T extends ChatTargetInput>(data: T) =>
* 外链上下文统一收敛到 `outLinkAuthData`,再由 `authChatTargetCrud` 解析真实 sourceId。
*/
export const transformChatAuthTargetInput = <T extends ChatTargetInput>(data: T) => {
const { appId, skillId, ...rest } = data;
const { appId, skillId, sourceType: rawSourceType, ...rest } = data;
delete rest.outLinkAuthData;
const sourceId = (appId || skillId) as string | undefined;
const normalizedOutLinkAuthData = getNormalizedOutLinkAuthData(data);
......@@ -238,7 +292,13 @@ export const transformChatAuthTargetInput = <T extends ChatTargetInput>(data: T)
return {
...rest,
...(normalizedOutLinkAuthData ? { outLinkAuthData: normalizedOutLinkAuthData } : {}),
sourceType: appId ? ChatSourceTypeEnum.app : ChatSourceTypeEnum.skillEdit,
sourceType: (() => {
if (skillId) return ChatSourceTypeEnum.skillEdit;
if (rawSourceType === ChatSourceTypeEnum.chatAgentHelper) {
return ChatSourceTypeEnum.chatAgentHelper;
}
return ChatSourceTypeEnum.app;
})(),
sourceId: sourceId!
};
};
......@@ -250,7 +310,7 @@ export const transformChatAuthTargetInput = <T extends ChatTargetInput>(data: T)
* `sourceType/sourceId`。
*/
export const transformOptionalChatTargetInput = <T extends ChatTargetInput>(data: T) => {
const { appId, skillId, ...rest } = data;
const { appId, skillId, sourceType: rawSourceType, ...rest } = data;
delete rest.outLinkAuthData;
const sourceId = (appId || skillId) as string | undefined;
const normalizedOutLinkAuthData = getNormalizedOutLinkAuthData(data);
......@@ -272,7 +332,13 @@ export const transformOptionalChatTargetInput = <T extends ChatTargetInput>(data
...(normalizedOutLinkAuthData ? { outLinkAuthData: normalizedOutLinkAuthData } : {}),
...(appId || skillId
? {
sourceType: appId ? ChatSourceTypeEnum.app : ChatSourceTypeEnum.skillEdit,
sourceType: (() => {
if (skillId) return ChatSourceTypeEnum.skillEdit;
if (rawSourceType === ChatSourceTypeEnum.chatAgentHelper) {
return ChatSourceTypeEnum.chatAgentHelper;
}
return ChatSourceTypeEnum.app;
})(),
sourceId: sourceId!
}
: {
......
export {
AuxiliaryGenerationChatFileSchema as ChatAgentHelperChatFileSchema,
ChatAgentHelperCompletionsParamsSchema,
type AuxiliaryGenerationChatFileType as ChatAgentHelperChatFileType,
type ChatAgentHelperCompletionsParamsType
} from '../../../../core/ai/auxiliaryGeneration/type';
import z from 'zod';
import type { OpenAPIPath } from '../../../type';
import { DevApiTagsMap } from '../../../tag';
import { ChatAgentHelperCompletionsParamsSchema } from './api';
export const ChatAgentHelperPath: OpenAPIPath = {
'/proApi/core/chat/chatAgentHelper/completions': {
post: {
summary: 'Chat Agent 辅助生成对话接口',
description: 'Chat Agent 辅助生成对话接口',
tags: [DevApiTagsMap.chatAgentHelper],
requestBody: {
content: {
'application/json': {
schema: ChatAgentHelperCompletionsParamsSchema
}
}
},
responses: {
200: {
description: '成功返回流式处理结果',
content: {
'application/stream+json': {
schema: z.any()
}
}
}
}
}
}
};
import { z } from 'zod';
import { PaginationResponseSchema, PaginationSchema } from '../../../api';
import { ChatFileTypeEnum } from '../../../../core/chat/constants';
import {
HelperBotChatItemSiteSchema,
HelperBotTypeEnum,
HelperBotTypeEnumSchema
} from '../../../../core/chat/helperBot/type';
import { topAgentParamsSchema } from '../../../../core/chat/helperBot/topAgent/type';
export const HelperBotCompletionsParamsSchema = z.object({
chatId: z.string(),
chatItemId: z.string(),
query: z.string(),
files: z.array(
z.object({
type: z.enum(ChatFileTypeEnum),
key: z.string(),
url: z.string().optional(),
name: z.string()
})
),
metadata: z.object({
type: z.literal(HelperBotTypeEnum.topAgent),
data: topAgentParamsSchema
})
});
export type HelperBotCompletionsParamsType = z.infer<typeof HelperBotCompletionsParamsSchema>;
// 分页获取记录
export const GetHelperBotChatRecordsParamsSchema = PaginationSchema.extend({
type: HelperBotTypeEnumSchema,
chatId: z.string()
});
export type GetHelperBotChatRecordsParamsType = z.infer<typeof GetHelperBotChatRecordsParamsSchema>;
export const GetHelperBotChatRecordsResponseSchema = PaginationResponseSchema(
HelperBotChatItemSiteSchema
);
export type GetHelperBotChatRecordsResponseType = z.infer<
typeof GetHelperBotChatRecordsResponseSchema
>;
// 删除单组对话
export const DeleteHelperBotChatParamsSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string(),
chatItemId: z.string()
});
export type DeleteHelperBotChatParamsType = z.infer<typeof DeleteHelperBotChatParamsSchema>;
// 获取文件上传签名
export const GetHelperBotFilePresignParamsSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string(),
filename: z.string()
});
export type GetHelperBotFilePresignParamsType = z.infer<typeof GetHelperBotFilePresignParamsSchema>;
// 获取文件预览链接
export const GetHelperBotFilePreviewParamsSchema = z.object({
key: z.string().min(1),
mode: z.enum(['proxy', 'presigned']).optional()
});
export type GetHelperBotFilePreviewParamsType = z.infer<typeof GetHelperBotFilePreviewParamsSchema>;
export const GetHelperBotFilePreviewResponseSchema = z.string();
import z from 'zod';
import type { OpenAPIPath } from '../../../type';
import { DevApiTagsMap } from '../../../tag';
import {
HelperBotCompletionsParamsSchema,
DeleteHelperBotChatParamsSchema,
GetHelperBotFilePresignParamsSchema,
GetHelperBotFilePreviewParamsSchema,
GetHelperBotFilePreviewResponseSchema,
GetHelperBotChatRecordsParamsSchema,
GetHelperBotChatRecordsResponseSchema
} from './api';
import { CreatePostPresignedUrlResponseSchema } from '../../../../common/file/s3/type';
export const HelperBotPath: OpenAPIPath = {
'/proApi/core/chat/helperBot/completions': {
post: {
summary: '辅助生成统一对话接口',
description: '辅助生成统一对话接口',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: HelperBotCompletionsParamsSchema
}
}
},
responses: {
200: {
description: '成功返回流式处理结果',
content: {
'application/stream+json': {
schema: z.any()
}
}
}
}
}
},
'/core/chat/helperBot/getRecords': {
get: {
summary: '分页获取记录',
description: '分页获取记录',
tags: [DevApiTagsMap.helperBot],
requestParams: {
query: GetHelperBotChatRecordsParamsSchema
},
responses: {
200: {
description: '成功返回记录列表',
content: {
'application/json': {
schema: GetHelperBotChatRecordsResponseSchema
}
}
}
}
}
},
'/core/chat/helperBot/deleteRecord': {
delete: {
summary: '删除单组对话',
description: '删除单组对话',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: DeleteHelperBotChatParamsSchema
}
}
},
responses: {
200: {
description: '成功删除记录',
content: {
'application/json': {
schema: z.any()
}
}
}
}
}
},
'/core/chat/helperBot/getFilePresign': {
post: {
summary: '获取辅助助手文件上传签名',
description: '获取辅助助手文件上传签名',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: GetHelperBotFilePresignParamsSchema
}
}
},
responses: {
200: {
description: '成功返回文件上传签名',
content: {
'application/json': {
schema: CreatePostPresignedUrlResponseSchema
}
}
}
}
}
},
'/core/chat/helperBot/getFilePreviewUrl': {
post: {
summary: '获取辅助助手文件预览链接',
description: '获取辅助助手文件预览链接',
tags: [DevApiTagsMap.helperBot],
requestBody: {
content: {
'application/json': {
schema: GetHelperBotFilePreviewParamsSchema
}
}
},
responses: {
200: {
description: '成功返回文件预览链接',
content: {
'application/json': {
schema: GetHelperBotFilePreviewResponseSchema
}
}
}
}
}
}
};
......@@ -6,7 +6,7 @@ import { ChatHistoryPath } from './history/index';
import { GetRecentlyUsedAppsResponseSchema } from './api';
import { DevApiTagsMap } from '../../tag';
import { ChatControllerPath } from './controler';
import { HelperBotPath } from './helperBot';
import { ChatAgentHelperPath } from './chatAgentHelper';
import { ChatInputGuidePath } from './inputGuide/index';
import { OutLinkChatPath } from './outLink/index';
import { ChatRecordPath } from './record/index';
......@@ -20,7 +20,7 @@ export const ChatPath: OpenAPIPath = {
...ChatFavouriteAppPath,
...ChatHistoryPath,
...ChatControllerPath,
...HelperBotPath,
...ChatAgentHelperPath,
...ChatInputGuidePath,
...OutLinkChatPath,
...ChatRecordPath,
......
......@@ -101,6 +101,6 @@ export const openAPITagGroups = [
},
{
name: '系统接口',
tags: [DevApiTagsMap.helperBot]
tags: [DevApiTagsMap.chatAgentHelper]
}
];
......@@ -58,7 +58,7 @@ export const DevApiTagsMap = {
/* 通用-基础功能 */
apiKey: 'API Key 管理',
customDomain: '自定义域名',
helperBot: '辅助助手',
chatAgentHelper: 'Chat Agent 辅助生成',
/* 辅助-团队管理 */
teamManage: '团队管理',
......
import { describe, expect, it } from 'vitest';
import { helperChats2GPTMessages } from '@fastgpt/global/core/chat/helperBot/adaptor';
import { ChatRoleEnum, ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/constants';
import type { HelperBotChatItemType } from '@fastgpt/global/core/chat/helperBot/type';
describe('helperChats2GPTMessages', () => {
it('should convert system message', () => {
const messages = [
{
obj: ChatRoleEnum.System,
value: [{ text: { content: 'You are a helpful assistant' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.System);
expect(result[0].content).toBe('You are a helpful assistant');
});
it('should skip system message with empty content', () => {
const messages = [
{
obj: ChatRoleEnum.System,
value: [{ text: { content: '' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(0);
});
it('should convert human message with text', () => {
const messages = [
{
obj: ChatRoleEnum.Human,
value: [{ text: { content: 'Hello' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.User);
expect(result[0].content).toBe('Hello');
});
it('should convert human message with image', () => {
const messages = [
{
obj: ChatRoleEnum.Human,
value: [
{
file: {
type: ChatFileTypeEnum.image,
name: 'test.png',
url: 'http://example.com/test.png',
key: 'test-key'
}
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.User);
expect(Array.isArray(result[0].content)).toBe(true);
const content = result[0].content as any[];
expect(content[0].type).toBe('image_url');
expect(content[0].image_url.url).toBe('http://example.com/test.png');
});
it('should convert human message with file', () => {
const messages = [
{
obj: ChatRoleEnum.Human,
value: [
{
file: {
type: ChatFileTypeEnum.file,
name: 'document.pdf',
url: 'http://example.com/document.pdf',
key: 'doc-key'
}
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
const content = result[0].content as any[];
expect(content[0].type).toBe('file_url');
expect(content[0].name).toBe('document.pdf');
});
it('should convert AI message with text', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'Hello, how can I help?' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(result[0].content).toBe('Hello, how can I help?');
});
it('should convert merged AI reasoning and text value with visible text', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [
{
reasoning: { content: 'Hidden thinking' },
hideReason: true,
text: { content: 'Visible answer' }
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(result[0].content).toBe('Visible answer');
});
it('should concat multiple AI text values', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'Part 1' } }, { text: { content: ' Part 2' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].content).toBe('Part 1 Part 2');
});
it('should add empty assistant message when AI has no content', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: []
}
] as unknown as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(result[0].content).toBe('');
});
it('should handle mixed message types', () => {
const messages = [
{
obj: ChatRoleEnum.System,
value: [{ text: { content: 'System prompt' } }]
},
{
obj: ChatRoleEnum.Human,
value: [{ text: { content: 'User question' } }]
},
{
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'AI response' } }]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(3);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.System);
expect(result[1].role).toBe(ChatCompletionRequestMessageRoleEnum.User);
expect(result[2].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
});
it('should handle collectionForm in AI message', () => {
const messages = [
{
obj: ChatRoleEnum.AI,
value: [
{
collectionForm: {
type: 'userInput',
params: {
description: 'Please fill the form',
inputForm: [
{ label: 'Name', type: 'input', key: 'name', required: true, value: '' }
]
}
}
}
]
}
] as HelperBotChatItemType[];
const result = helperChats2GPTMessages({ messages });
expect(result).toHaveLength(1);
expect(result[0].role).toBe(ChatCompletionRequestMessageRoleEnum.Assistant);
expect(typeof result[0].content).toBe('string');
expect(result[0].content).toContain('Name');
});
});
import { describe, expect, it } from 'vitest';
import { topAgentParamsSchema } from '@fastgpt/global/core/chat/helperBot/topAgent/type';
describe('topAgentParamsSchema', () => {
it('should validate empty object', () => {
const result = topAgentParamsSchema.safeParse({});
expect(result.success).toBe(true);
});
it('should validate full params', () => {
const result = topAgentParamsSchema.safeParse({
role: 'assistant',
taskObject: 'Help user with coding',
systemPrompt: 'You are a helpful assistant',
selectedTools: ['tool1', 'tool2'],
selectedDatasets: ['dataset1'],
selectedAgentSkills: [
{
skillId: 'skill1',
name: 'Research Skill',
description: 'Research workflow',
isDeleted: false
}
],
fileUpload: true
});
expect(result.success).toBe(true);
});
it('should validate partial params', () => {
const result = topAgentParamsSchema.safeParse({
role: 'assistant',
fileUpload: false
});
expect(result.success).toBe(true);
});
it('should allow null values for optional fields', () => {
const result = topAgentParamsSchema.safeParse({
role: null,
taskObject: null,
systemPrompt: null,
selectedTools: null,
selectedDatasets: null,
selectedAgentSkills: null,
fileUpload: null
});
expect(result.success).toBe(true);
});
it('should validate selectedTools as string array', () => {
const result = topAgentParamsSchema.safeParse({
selectedTools: ['tool1', 'tool2', 'tool3']
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.selectedTools).toHaveLength(3);
}
});
it('should validate selectedDatasets as string array', () => {
const result = topAgentParamsSchema.safeParse({
selectedDatasets: ['dataset1', 'dataset2']
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.selectedDatasets).toHaveLength(2);
}
});
it('should validate selectedAgentSkills as selected skill item array', () => {
const result = topAgentParamsSchema.safeParse({
selectedAgentSkills: [
{
skillId: 'skill1',
name: 'Research Skill',
description: 'Research workflow'
}
]
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.selectedAgentSkills).toHaveLength(1);
expect(result.data.selectedAgentSkills?.[0]?.isDeleted).toBe(false);
}
});
it('should reject invalid selectedTools type', () => {
const result = topAgentParamsSchema.safeParse({
selectedTools: 'not-an-array'
});
expect(result.success).toBe(false);
});
it('should reject invalid selectedAgentSkills type', () => {
const result = topAgentParamsSchema.safeParse({
selectedAgentSkills: ['skill1']
});
expect(result.success).toBe(false);
});
it('should reject invalid fileUpload type', () => {
const result = topAgentParamsSchema.safeParse({
fileUpload: 'not-a-boolean'
});
expect(result.success).toBe(false);
});
});
import { describe, expect, it } from 'vitest';
import {
HelperBotTypeEnum,
HelperBotTypeEnumSchema,
HelperBotChatSchema,
HelperBotChatItemSchema,
HelperBotChatItemSiteSchema,
AIChatItemValueItemSchema
} from '@fastgpt/global/core/chat/helperBot/type';
describe('HelperBotTypeEnum', () => {
it('should have correct enum values', () => {
expect(HelperBotTypeEnum.topAgent).toBe('topAgent');
});
it('should have all expected types', () => {
const types = Object.values(HelperBotTypeEnum);
expect(types).toHaveLength(1);
expect(types).toContain('topAgent');
});
});
describe('HelperBotTypeEnumSchema', () => {
it('should validate valid enum values', () => {
const result = HelperBotTypeEnumSchema.safeParse('topAgent');
expect(result.success).toBe(true);
});
it('should reject invalid enum values', () => {
const result = HelperBotTypeEnumSchema.safeParse('invalid');
expect(result.success).toBe(false);
});
});
describe('AIChatItemValueItemSchema', () => {
it('should validate text content', () => {
const result = AIChatItemValueItemSchema.safeParse({
text: { content: 'Hello' }
});
expect(result.success).toBe(true);
});
it('should validate reasoning content', () => {
const result = AIChatItemValueItemSchema.safeParse({
reasoning: { content: 'Let me think...' }
});
expect(result.success).toBe(true);
});
it('should validate merged text and hidden reasoning content', () => {
const result = AIChatItemValueItemSchema.safeParse({
reasoning: { content: 'Hidden thinking' },
hideReason: true,
text: { content: 'Visible answer' }
});
expect(result.success).toBe(true);
});
it('should reject content value without text or reasoning', () => {
const result = AIChatItemValueItemSchema.safeParse({
hideReason: true
});
expect(result.success).toBe(false);
});
it('should validate planHint', () => {
const result = AIChatItemValueItemSchema.safeParse({
planHint: { type: 'generation' }
});
expect(result.success).toBe(true);
});
it('should reject invalid planHint type', () => {
const result = AIChatItemValueItemSchema.safeParse({
planHint: { type: 'invalid' }
});
expect(result.success).toBe(false);
});
});
......@@ -59,6 +59,21 @@ describe('openapi/core/chat target schema', () => {
expect('skillId' in result).toBe(false);
});
it('transforms raw appId with chatAgentHelper sourceType to internal chatAgentHelper source', () => {
const result = StopV2ChatSchema.parse({
appId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
chatId: 'chat-1'
});
expect(result).toMatchObject({
sourceType: ChatSourceTypeEnum.chatAgentHelper,
sourceId: appId,
chatId: 'chat-1'
});
expect('appId' in result).toBe(false);
});
it('transforms question guide skillId to internal skillEdit source with debug model config', () => {
const result = CreateQuestionGuideV2BodySchema.parse({
skillId,
......@@ -81,7 +96,7 @@ describe('openapi/core/chat target schema', () => {
expect('skillId' in result).toBe(false);
});
it('rejects required target when appId and skillId are both provided', () => {
it('rejects required target when multiple explicit targets are provided', () => {
const payload = {
appId,
skillId,
......@@ -90,6 +105,13 @@ describe('openapi/core/chat target schema', () => {
expect(() => MarkChatReadBodySchema.parse(payload)).toThrow();
expect(() =>
StopV2ChatSchema.parse({
skillId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
chatId: 'chat-1'
})
).toThrow();
expect(() =>
GetPaginationRecordsBodySchema.parse({
...payload,
pageSize: 10,
......
import type { NextApiResponse } from 'next';
import { responseWrite } from './index';
export type SseStreamWriter = (params: { event?: string; data: string }) => void;
export type SseStreamResumeMirror = {
enqueueRaw?: (chunk: string) => Promise<void> | void;
flush?: () => Promise<void>;
shrinkTTLAfterComplete?: () => Promise<void>;
};
type CreateSseStreamContextParams = {
res?: NextApiResponse;
stream?: boolean;
streamResumeMirror?: SseStreamResumeMirror;
heartbeat?: {
intervalMs?: number;
write: (writer: SseStreamWriter) => void;
};
onCleanup?: () => void;
onError?: () => void;
};
const isResponseClosed = (res: NextApiResponse) =>
!!(res.closed || res.writableEnded || res.destroyed);
/**
* 创建通用 SSE 协议上下文。
*
* 该 helper 只处理 header、原始 chunk 写入、resume mirror、心跳和连接清理;
* 不理解 workflow、chat 或 auxiliary generation 的事件语义。
*/
export const createSseStreamContext = ({
res,
stream = true,
streamResumeMirror,
heartbeat,
onCleanup,
onError
}: CreateSseStreamContextParams) => {
const write: SseStreamWriter = ({ event, data }) => {
if (!stream) return;
const raw = `${event ? `event: ${event}\n` : ''}data: ${data}\n\n`;
void streamResumeMirror?.enqueueRaw?.(raw);
if (!res || isResponseClosed(res)) return;
responseWrite({ res, event, data });
};
if (!stream || !res) {
return {
write,
cleanup: () => undefined,
async flushResume() {
await streamResumeMirror?.flush?.();
await streamResumeMirror?.shrinkTTLAfterComplete?.();
}
};
}
if (!res.headersSent) {
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Type', 'text/event-stream;charset=utf-8');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('X-Accel-Buffering', 'no');
res.setHeader('Cache-Control', 'no-cache, no-transform');
}
let cleaned = false;
const streamCheckTimer = heartbeat
? setInterval(() => {
heartbeat.write(write);
}, heartbeat.intervalMs ?? 10000)
: undefined;
const cleanup = () => {
if (cleaned) return;
cleaned = true;
if (streamCheckTimer) {
clearInterval(streamCheckTimer);
}
onCleanup?.();
};
res.once('finish', cleanup);
res.once('close', cleanup);
res.on('error', () => {
cleanup();
onError?.();
res.end();
});
return {
write,
cleanup,
async flushResume() {
await streamResumeMirror?.flush?.();
await streamResumeMirror?.shrinkTTLAfterComplete?.();
}
};
};
......@@ -26,14 +26,7 @@ export const UploadConstraintsInputSchema = z.object({
});
export type UploadConstraintsInput = z.infer<typeof UploadConstraintsInputSchema>;
export const S3SourcesSchema = z.enum([
'avatar',
'chat',
'dataset',
'temp',
'rawText',
'helperBot'
]);
export const S3SourcesSchema = z.enum(['avatar', 'chat', 'dataset', 'temp', 'rawText']);
export const S3Sources = S3SourcesSchema.enum;
export type S3SourceType = z.infer<typeof S3SourcesSchema>;
......
import type { S3HelperBotSource } from './index';
declare global {
var helperBotBucket: S3HelperBotSource;
}
export {};
import { parseFileExtensionFromUrl } from '@fastgpt/global/common/string/tools';
import { S3PrivateBucket } from '../../buckets/private';
import { S3Sources } from '../../contracts/type';
import {
type CheckHelperBotFileKeys,
type DelChatFileByPrefixParams,
DelChatFileByPrefixSchema,
HelperBotFileUploadSchema
} from './type';
import { differenceInHours } from 'date-fns';
import { S3Buckets } from '../../config/constants';
import path from 'path';
import { getFileS3Key } from '../../utils';
import { parseHelperBotFileS3Key } from './key';
export class S3HelperBotSource extends S3PrivateBucket {
private static instance: S3HelperBotSource;
constructor() {
super();
}
static getInstance() {
return (this.instance ??= new S3HelperBotSource());
}
static parseFileUrl(url: string | URL) {
try {
const parseUrl = new URL(url);
const pathname = decodeURIComponent(parseUrl.pathname);
// 非 S3 key
if (!pathname.startsWith(`/${S3Buckets.private}/${S3Sources.helperBot}/`)) {
return {
filename: '',
extension: '',
imageParsePrefix: ''
};
}
const filename = pathname.split('/').pop() || 'file';
const extension = path.extname(filename);
return {
filename,
extension: extension.replace('.', ''),
imageParsePrefix: `${pathname.replace(`/${S3Buckets.private}/`, '').replace(extension, '')}-parsed`
};
} catch (error) {
return {
filename: '',
extension: '',
imageParsePrefix: ''
};
}
}
parseKey(key: string) {
return parseHelperBotFileS3Key(key);
}
async createGetFileURL(params: {
key: string;
expiredHours?: number;
external: boolean;
mode?: 'proxy' | 'presigned';
}) {
const { key, expiredHours = 1, external = false, mode } = params; // 默认一个小时
if (external) {
return await this.createExternalUrl({ key, expiredHours, mode });
}
return await this.createPreviewUrl({ key, expiredHours });
}
async createUploadFileURL(params: CheckHelperBotFileKeys) {
const { type, chatId, userId, filename, expiredTime } = HelperBotFileUploadSchema.parse(params);
const { fileKey } = getFileS3Key.helperBot({ type, chatId, userId, filename });
return await this.createPresignedPutUrl(
{ rawKey: fileKey, filename },
{ expiredHours: expiredTime ? differenceInHours(expiredTime, new Date()) : 1 }
);
}
deleteFilesByPrefix(params: DelChatFileByPrefixParams) {
const { type, chatId, userId } = DelChatFileByPrefixSchema.parse(params);
const prefix = [S3Sources.helperBot, type, userId, chatId].filter(Boolean).join('/');
return this.addDeleteJob({ prefix });
}
deleteFileByKey(key: string) {
return this.addDeleteJob({ key });
}
}
export function getS3HelperBotSource() {
if (global.helperBotBucket) {
return global.helperBotBucket;
}
global.helperBotBucket = new S3HelperBotSource();
return global.helperBotBucket;
}
import type { HelperBotTypeEnumType } from '@fastgpt/global/core/chat/helperBot/type';
import { HelperBotTypeEnumSchema } from '@fastgpt/global/core/chat/helperBot/type';
import { isS3ObjectKey } from '../../utils';
/**
* 解析 HelperBot 文件 key。
*
* HelperBot 文件 key 的第一段是固定 source,后续才是 type/user/chat 维度。
*/
export function parseHelperBotFileS3Key(key: string): {
type: HelperBotTypeEnumType;
userId: string;
chatId: string;
filename: string;
} | null {
if (!isS3ObjectKey(key, 'helperBot')) return null;
const [, type, userId, chatId, ...filenameParts] = key.split('/');
const filename = filenameParts.join('/');
const parsedType = HelperBotTypeEnumSchema.safeParse(type);
if (!parsedType.success || !userId || !chatId || !filename) return null;
return {
type: parsedType.data,
userId,
chatId,
filename
};
}
/**
* 判断 HelperBot 文件 key 是否属于当前用户。
*/
export function isAuthorizedHelperBotFileS3Key({ key, userId }: { key: string; userId: string }) {
const parsedKey = parseHelperBotFileS3Key(key);
return !!parsedKey && String(parsedKey.userId) === String(userId);
}
import z from 'zod';
import { HelperBotTypeEnumSchema } from '@fastgpt/global/core/chat/helperBot/type';
export const HelperBotFileUploadSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string().nonempty(),
userId: z.string().nonempty(),
filename: z.string().nonempty(),
expiredTime: z.coerce.date().optional()
});
export type CheckHelperBotFileKeys = z.infer<typeof HelperBotFileUploadSchema>;
export const DelChatFileByPrefixSchema = z.object({
type: HelperBotTypeEnumSchema,
chatId: z.string().nonempty().optional(),
userId: z.string().nonempty().optional()
});
export type DelChatFileByPrefixParams = z.infer<typeof DelChatFileByPrefixSchema>;
......@@ -10,7 +10,6 @@ import { S3PublicBucket } from './buckets/public';
import { getNanoid } from '@fastgpt/global/common/string/tools';
import path from 'node:path';
import type { ParsedFileContentS3KeyParams } from './sources/dataset/type';
import type { HelperBotTypeEnumType } from '@fastgpt/global/core/chat/helperBot/type';
// S3文件名最大长度配置
export const S3_FILENAME_MAX_LENGTH = 50;
......@@ -234,25 +233,6 @@ export const getFileS3Key = {
};
},
helperBot: ({
type,
chatId,
userId,
filename
}: {
type: HelperBotTypeEnumType;
chatId: string;
userId: string;
filename: string;
}) => {
const { formatedFilename, extension } = getFormatedFilename(filename);
const basePrefix = [S3Sources.helperBot, type, userId, chatId].filter(Boolean).join('/');
return {
fileKey: [basePrefix, `${formatedFilename}${extension ? `.${extension}` : ''}`].join('/'),
fileParsedPrefix: [basePrefix, `${formatedFilename}-parsed`].join('/')
};
},
// 上传数据集的文件的解析结果的图片的 Key
dataset: (params: ParsedFileContentS3KeyParams) => {
const { datasetId, filename } = params;
......
import type { ChatCompletionMessageParam } from '@fastgpt/global/core/ai/llm/type';
import { runUnifiedAgentLoop, createUpdatePlanTool } from '../llm/agentLoop';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import type { AuxiliaryGenerationStreamWriter } from './stream';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import { createChatCompletionDeltaResponse } from '@fastgpt/global/core/ai/llm/utils';
type RunAuxiliaryGenerationAgentLoopParams = {
teamId: string;
model: string;
systemPrompt: string;
messages: ChatCompletionMessageParam[];
useVision?: boolean;
useAudio?: boolean;
useVideo?: boolean;
streamWriter?: AuxiliaryGenerationStreamWriter;
checkIsStopping?: () => boolean;
usageSink?: (usages: ChatNodeUsageType[]) => void;
};
/**
* 运行辅助生成的无工具 Agent Loop。
*
* 该入口只封装统一 agent loop 的模型循环和计划维护,不注册 runtime tools,
* 因此不会隐式获得 workflow、Skill 或虚拟机执行能力。
*/
export async function runAuxiliaryGenerationAgentLoop({
teamId,
model,
systemPrompt,
messages,
useVision,
useAudio,
useVideo,
streamWriter,
checkIsStopping,
usageSink
}: RunAuxiliaryGenerationAgentLoopParams) {
const result = await runUnifiedAgentLoop({
runtime: {
teamId,
model,
stream: true,
useVision,
useAudio,
useVideo,
toolCatalog: {
runtimeTools: [],
updatePlanTool: createUpdatePlanTool()
},
executeTool: async () => {
throw new Error('Auxiliary generation does not support runtime tools');
},
checkIsStopping,
emitEvent: (event) => {
if (event.type === 'reasoning_delta') {
streamWriter?.({
event: AuxiliaryGenerationEventEnum.answer,
data: createChatCompletionDeltaResponse({ reasoningContent: event.text })
});
}
},
usageSink
},
input: {
systemPrompt,
messages
}
});
return {
status: result.status,
answerText: result.answerText ?? '',
reasoningText: result.reasoningText
};
}
export * from './type';
export * from './stream';
export * from './usage';
export * from './agentLoop';
export * from './service';
export * from './stop';
import { createAuxiliaryGenerationStream } from './stream';
import { createAuxiliaryGenerationUsage } from './usage';
import { clearAuxiliaryGenerationStop, shouldAuxiliaryGenerationStop } from './stop';
import type { AuxiliaryGenerationRunParams, AuxiliaryGenerationRunResult } from './type';
/**
* 执行一次辅助生成。
*
* 该函数负责鉴权之后的通用生命周期:余额校验、用量记录、SSE 初始化、
* stop 标记、processor 调用和结束事件。业务差异由调用方传入的 processor 承担。
*/
export const runAuxiliaryGeneration = async <T>({
req,
res,
teamId,
tmbId,
userId,
isRoot,
lang,
appName,
sourceType,
sourceId,
chatId,
query,
files,
data,
histories,
usageSource,
processor,
maxFiles,
customPdfParse,
onStreamContextReady
}: AuxiliaryGenerationRunParams<T>): Promise<
AuxiliaryGenerationRunResult & {
streamContext: Awaited<ReturnType<typeof createAuxiliaryGenerationStream>>;
}
> => {
let stopping = false;
const startedAt = Date.now();
const streamContext = await createAuxiliaryGenerationStream({
req,
res,
teamId,
sourceType,
sourceId,
chatId
});
onStreamContextReady?.(streamContext);
const usageContext = await createAuxiliaryGenerationUsage({
teamId,
tmbId,
appName,
sourceType,
sourceId,
usageSource
});
await clearAuxiliaryGenerationStop({ sourceType, sourceId, chatId });
res.once('close', () => {
stopping = true;
});
const stopCheckTimer = setInterval(async () => {
if (stopping) return;
stopping = await shouldAuxiliaryGenerationStop({ sourceType, sourceId, chatId });
}, 100);
try {
const result = await processor({
query,
files,
data,
histories,
requestOrigin: req.headers.origin,
streamWriter: streamContext.write,
checkIsStopping: () => stopping,
usageSink: usageContext.pushUsage,
maxFiles,
customPdfParse,
user: {
teamId,
tmbId,
userId,
isRoot,
lang
}
});
streamContext.writeDone();
return {
...result,
durationSeconds: +((Date.now() - startedAt) / 1000).toFixed(2),
streamContext
};
} finally {
clearInterval(stopCheckTimer);
await clearAuxiliaryGenerationStop({ sourceType, sourceId, chatId });
}
};
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { getGlobalRedisConnection } from '../../../common/redis';
const RUNTIME_STOP_KEY_PREFIX = 'agent_runtime_stopping';
type AuxiliaryGenerationStopParams = {
sourceType: ChatSourceTypeEnum;
sourceId: string;
chatId: string;
};
const getAuxiliaryGenerationStopKey = ({
sourceType,
sourceId,
chatId
}: AuxiliaryGenerationStopParams) =>
`${RUNTIME_STOP_KEY_PREFIX}:${sourceType}:${sourceId}:${chatId}`;
/**
* 读取 `/v2/chat/stop` 写入的运行态停止标记。
*
* 该 key 目前由 workflow status 模块写入;辅助生成不依赖 workflow 执行器,
* 但必须识别同一个停止信号,保证 ChatBox 的停止按钮对 ChatAgentHelper 生效。
*/
export const shouldAuxiliaryGenerationStop = async (params: AuxiliaryGenerationStopParams) => {
const redis = getGlobalRedisConnection();
return redis
.get(getAuxiliaryGenerationStopKey(params))
.then((res) => !!res)
.catch(() => false);
};
/**
* 清理运行态停止标记,让 `/v2/chat/stop` 的等待逻辑能判断生成已经结束。
*/
export const clearAuxiliaryGenerationStop = async (params: AuxiliaryGenerationStopParams) => {
const redis = getGlobalRedisConnection();
await redis.del(getAuxiliaryGenerationStopKey(params)).catch(() => undefined);
};
import type { NextApiRequest, NextApiResponse } from 'next';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { STREAM_RESUME_REQUEST_HEADER } from '@fastgpt/global/core/chat/constants';
import { getSseErrorResponse } from '../../../common/response';
import { createSseStreamContext } from '../../../common/response/sse';
import { clearCookie } from '../../../support/permission/auth/common';
import { getStreamResumeMirror } from '../../chat/resume';
import { createChatCompletionDeltaResponse } from '@fastgpt/global/core/ai/llm/utils';
export type AuxiliaryGenerationStreamWriter = (params: {
event?: `${AuxiliaryGenerationEventEnum}` | string;
data: string | object;
}) => void;
type CreateAuxiliaryGenerationStreamParams = {
req: NextApiRequest;
res: NextApiResponse;
teamId: string;
sourceType: ChatSourceTypeEnum;
sourceId: string;
chatId: string;
};
export type AuxiliaryGenerationStreamContext = {
write: AuxiliaryGenerationStreamWriter;
writeError: (error: unknown) => void;
writeDone: () => void;
flushResume: () => Promise<void>;
};
/**
* 初始化辅助生成 SSE 响应。
*
* 这里只处理通用 SSE 协议、心跳和 chat stream resume mirror,不引入 workflow writer。
* 调用方需要显式写入 answer/interactive/config 等业务事件。
*/
export const createAuxiliaryGenerationStream = async ({
req,
res,
teamId,
sourceType,
sourceId,
chatId
}: CreateAuxiliaryGenerationStreamParams): Promise<AuxiliaryGenerationStreamContext> => {
const mirror = await getStreamResumeMirror({
resumeRequestHeaderValue: req.headers?.[STREAM_RESUME_REQUEST_HEADER],
teamId,
sourceType,
sourceId,
chatId
});
const sseContext = createSseStreamContext({
res,
streamResumeMirror: mirror,
heartbeat: {
write: (writer) => {
writer({
event: AuxiliaryGenerationEventEnum.answer,
data: JSON.stringify(createChatCompletionDeltaResponse({ text: '' }))
});
}
}
});
const write: AuxiliaryGenerationStreamWriter = ({ event, data }) => {
const payload = typeof data === 'string' ? data : JSON.stringify(data);
sseContext.write({ event, data: payload });
};
return {
write,
writeError(error) {
const { data, shouldClearCookie } = getSseErrorResponse(error);
if (shouldClearCookie) {
clearCookie(res);
}
write({
event: AuxiliaryGenerationEventEnum.error,
data
});
},
writeDone() {
write({
event: AuxiliaryGenerationEventEnum.answer,
data: createChatCompletionDeltaResponse({
text: null,
finishReason: 'stop'
})
});
write({
event: AuxiliaryGenerationEventEnum.answer,
data: '[DONE]'
});
},
async flushResume() {
await sseContext.flushResume();
}
};
};
import type { NextApiRequest, NextApiResponse } from 'next';
import type { localeType } from '@fastgpt/global/common/i18n/type';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { AIChatItemValueItemType, ChatItemDBSchemaType } from '@fastgpt/global/core/chat/type';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
import type { AuxiliaryGenerationChatFileType } from '@fastgpt/global/core/ai/auxiliaryGeneration/type';
import type { AuxiliaryGenerationStreamContext, AuxiliaryGenerationStreamWriter } from './stream';
export type AuxiliaryGenerationUser = {
teamId: string;
tmbId: string;
userId: string;
isRoot: boolean;
lang: localeType;
};
export type AuxiliaryGenerationProcessorParams<T = unknown> = {
query: string;
files: AuxiliaryGenerationChatFileType[];
data: T;
histories: ChatItemDBSchemaType[];
streamWriter?: AuxiliaryGenerationStreamWriter;
requestOrigin?: string;
maxFiles?: number;
customPdfParse?: boolean;
checkIsStopping?: () => boolean;
usageSink?: (usages: ChatNodeUsageType[]) => void;
user: AuxiliaryGenerationUser;
};
export type AuxiliaryGenerationProcessorResponse = {
aiResponse: AIChatItemValueItemType[];
usage: {
model: string;
inputTokens: number;
outputTokens: number;
};
};
export type AuxiliaryGenerationRunParams<T = unknown> = {
req: NextApiRequest;
res: NextApiResponse;
teamId: string;
tmbId: string;
userId: string;
isRoot: boolean;
lang: localeType;
appName: string;
sourceType: ChatSourceTypeEnum;
sourceId: string;
chatId: string;
query: string;
files: AuxiliaryGenerationChatFileType[];
data: T;
histories: ChatItemDBSchemaType[];
usageSource: UsageSourceEnum;
processor: (
params: AuxiliaryGenerationProcessorParams<T>
) => Promise<AuxiliaryGenerationProcessorResponse>;
maxFiles?: number;
customPdfParse?: boolean;
/** SSE 创建后立即暴露给路由层,用于失败时写 error 和 flush resume。 */
onStreamContextReady?: (streamContext: AuxiliaryGenerationStreamContext) => void;
};
export type AuxiliaryGenerationRunResult = AuxiliaryGenerationProcessorResponse & {
durationSeconds: number;
};
import { checkTeamAIPoints } from '../../../support/permission/teamLimit';
import { createChatUsageRecord, pushChatItemUsage } from '../../../support/wallet/usage/controller';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
type CreateAuxiliaryGenerationUsageParams = {
teamId: string;
tmbId: string;
appName: string;
sourceType: ChatSourceTypeEnum;
sourceId: string;
usageSource: UsageSourceEnum;
};
/**
* 为辅助生成建立统一的扣费上下文。
*
* 余额校验在生成前执行;后续 processor 只需把各模型/工具用量推入 `pushUsage`。
*/
export const createAuxiliaryGenerationUsage = async ({
teamId,
tmbId,
appName,
sourceType,
sourceId,
usageSource
}: CreateAuxiliaryGenerationUsageParams) => {
await checkTeamAIPoints(teamId);
const usageAppId = (() => {
if ([ChatSourceTypeEnum.app, ChatSourceTypeEnum.chatAgentHelper].includes(sourceType)) {
return sourceId;
}
})();
const usageSkillId = (() => {
if (sourceType === ChatSourceTypeEnum.skillEdit) return sourceId;
})();
const usageId = await createChatUsageRecord({
appName,
appId: usageAppId,
skillId: usageSkillId,
teamId,
tmbId,
source: usageSource
});
return {
usageId,
pushUsage(usages: ChatNodeUsageType[]) {
pushChatItemUsage({
teamId,
usageId,
nodeUsages: usages
});
}
};
};
......@@ -23,7 +23,6 @@ import { refreshVersionKey } from '../../../common/cache';
import { SystemCacheKeyEnum } from '../../../common/cache/type';
import { getLogger, LogCategories } from '../../../common/logger';
import { getRuntimeResolvedPriceTiers } from '@fastgpt/global/core/ai/pricing';
import { serviceEnv } from '../../../env';
export const loadSystemModels = async (init = false, language = 'en') => {
if (!init && global.systemModelList) return;
......@@ -80,9 +79,6 @@ export const loadSystemModels = async (init = false, language = 'en') => {
if (model.isDefaultChatTitleModel) {
_systemDefaultModel.chatTitleLLM = model;
}
if (model.model === serviceEnv.HELPER_BOT_MODEL) {
_systemDefaultModel.helperBotLLM = model;
}
} else if (model.type === ModelTypeEnum.embedding) {
_embeddingModelMap.set(model.model, model);
_embeddingModelMap.set(model.name, model);
......@@ -191,10 +187,6 @@ export const loadSystemModels = async (init = false, language = 'en') => {
(item) => item.vision
);
}
if (!_systemDefaultModel.helperBotLLM) {
// HELPER_BOT_MODEL 未配置或未命中已启用模型时,回退到系统默认 LLM。
_systemDefaultModel.helperBotLLM = _systemDefaultModel.llm;
}
if (!_systemDefaultModel.embedding) {
_systemDefaultModel.embedding = Array.from(_embeddingModelMap.values())[0];
}
......
......@@ -33,9 +33,6 @@ export const getVlmModel = (model?: string) => {
export const getDefaultChatTitleModel = () => global?.systemDefaultModel.chatTitleLLM;
export const getDefaultHelperBotModel = (): LLMModelItemType =>
global?.systemDefaultModel.helperBotLLM || getDefaultLLMModel();
export const getDefaultEmbeddingModel = () => global?.systemDefaultModel.embedding!;
export const getEmbeddingModel = (model?: string | EmbeddingModelItemType) => {
if (!model) return getDefaultEmbeddingModel();
......
......@@ -93,6 +93,9 @@ const buildSandboxResourcesToArchiveQuery = (params: {
const buildSandboxResourceSourceQuery = ({ sourceType, sourceId }: SandboxSourceParams) => {
if (sourceType === ChatSourceTypeEnum.app) return { sourceType, sourceId };
if (sourceType === ChatSourceTypeEnum.skillEdit) return { sourceType, sourceId };
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
throw new Error('ChatAgentHelper source does not support sandbox resources');
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported sandbox source type: ${exhaustiveCheck}`);
......
......@@ -96,6 +96,10 @@ export function buildSandboxClientQueryFromChatSource({
};
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
throw new Error('ChatAgentHelper source does not support sandbox runtime');
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
}
......@@ -27,6 +27,10 @@ export const getRunningSandboxId = ({
return getEditDebugSandboxId(sourceId);
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
throw new Error('ChatAgentHelper source does not support sandbox id generation');
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
};
......@@ -34,6 +34,7 @@ import { getDefaultLLMModel } from '../../model';
import { getRunningSkillEditSandbox } from '../../sandbox/interface/skillEdit';
import { dispatchWorkFlow } from '../../../workflow/dispatch';
import { WORKFLOW_MAX_RUN_TIMES } from '../../../workflow/constants';
import type { AppFileSelectConfigType } from '@fastgpt/global/core/app/type/config.schema';
import { getChatItems } from '../../../chat/controller';
import {
failChatRound,
......@@ -51,6 +52,16 @@ import { buildDebugRuntimeNodes } from './runtime';
import type { AgentSandboxPrepareAction } from '../../../workflow/dispatch/ai/agent/sub/sandbox';
const logger = getLogger(LogCategories.MODULE.AGENT_SKILLS);
const skillDebugFileSelectConfig: AppFileSelectConfigType = {
maxFiles: 10,
canSelectFile: true,
canSelectImg: true,
customPdfParse: false,
canSelectVideo: true,
canSelectAudio: true,
canSelectCustomFileExtension: false,
customFileExtensionList: []
};
/**
* 处理 Skill 调试对话的共享主流程。
......@@ -206,7 +217,9 @@ export async function handleSkillDebugChat(
variables: {},
query: removeEmptyUserInput(userQuestion.value),
lastInteractive: interactive,
chatConfig: {},
chatConfig: {
fileSelectConfig: skillDebugFileSelectConfig
},
histories: newHistories,
stream: true,
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
......
......@@ -102,6 +102,13 @@ export function buildDebugRuntimeNodes(
value: systemPrompt
},
{
key: NodeInputKeyEnum.aiChatVision,
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.boolean,
label: '',
value: true
},
{
key: NodeInputKeyEnum.editSkillId,
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.string,
......
......@@ -43,7 +43,7 @@ const mergeMongoAndQuery = (...queries: Record<string, unknown>[]) => {
/**
* 查询当前成员可读的 Agent Skill 列表。
*
* 这里集中维护 Skill 列表 API 和 HelperBot TopAgent 共用的权限过滤规则:
* 这里集中维护 Skill 列表 API 和 ChatAgentHelper 共用的权限过滤规则:
* owner 可读团队内资源;普通成员按成员、用户组、组织授权计算,并支持目录继承权限。
*/
export const listReadableAgentSkills = async ({
......
......@@ -30,7 +30,6 @@ export type SystemDefaultModelType = {
datasetTextLLM?: LLMModelItemType;
datasetImageLLM?: LLMModelItemType;
chatTitleLLM?: LLMModelItemType;
helperBotLLM?: LLMModelItemType;
[ModelTypeEnum.embedding]?: EmbeddingModelItemType;
[ModelTypeEnum.tts]?: TTSModelType;
......
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { connectionMongo, getMongoModel } from '../../../common/mongo';
const { Schema } = connectionMongo;
import { helperBotChatItemCollectionName } from './constants';
import { HelperBotTypeEnum } from '@fastgpt/global/core/chat/helperBot/type';
import type { HelperBotChatItemType } from '@fastgpt/global/core/chat/helperBot/type';
const HelperBotChatItemSchema = new Schema({
userId: {
type: String,
require: true
},
chatId: {
type: String,
require: true
},
dataId: {
type: String,
require: true
},
createTime: {
type: Date,
default: () => new Date()
},
obj: {
type: String,
require: true,
enum: Object.values(ChatRoleEnum)
},
value: {
type: Array,
require: true
},
memories: Object
});
HelperBotChatItemSchema.index({ userId: 1, chatId: 1, dataId: 1, obj: 1 }, { unique: true });
export const MongoHelperBotChatItem = getMongoModel<HelperBotChatItemType>(
helperBotChatItemCollectionName,
HelperBotChatItemSchema
);
import { connectionMongo, getMongoModel } from '../../../common/mongo';
const { Schema } = connectionMongo;
import { helperBotChatCollectionName } from './constants';
import { HelperBotTypeEnum } from '@fastgpt/global/core/chat/helperBot/type';
import type { HelperBotChatType } from '../../../../global/core/chat/helperBot/type';
const HelperBotChatSchema = new Schema({
type: {
type: String,
required: true,
enum: Object.values(HelperBotTypeEnum)
},
userId: {
type: String,
require: true
},
chatId: {
type: String,
require: true
},
createTime: {
type: Date,
default: () => new Date()
},
updateTime: {
type: Date,
default: () => new Date()
},
metadata: Object
});
HelperBotChatSchema.index({ type: 1, userId: 1, chatId: 1 }, { unique: true });
export const MongoHelperBotChat = getMongoModel<HelperBotChatType>(
helperBotChatCollectionName,
HelperBotChatSchema
);
export const helperBotChatCollectionName = 'helper_bot_chats';
export const helperBotChatItemCollectionName = 'helper_bot_chat_items';
import type {
AIChatItemValueItemType,
HelperBotTypeEnum
} from '@fastgpt/global/core/chat/helperBot/type';
import type { HelperBotCompletionsParamsType } from '@fastgpt/global/openapi/core/chat/helperBot/api';
import { MongoHelperBotChat } from './chatSchema';
import type { UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { MongoHelperBotChatItem } from './chatItemSchema';
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { mongoSessionRun } from '../../../common/mongo/sessionRun';
export const pushChatRecords = async ({
type,
userId,
chatId,
chatItemId,
query,
files,
aiResponse,
memories,
metadata
}: {
type: HelperBotTypeEnum;
userId: string;
chatId: string;
chatItemId: string;
query: string;
files: HelperBotCompletionsParamsType['files'];
aiResponse: AIChatItemValueItemType[];
memories?: Record<string, any>;
metadata?: Record<string, any>;
}) => {
const chat = await MongoHelperBotChat.findOne(
{
type,
userId,
chatId
},
'_id metadata'
).lean();
const metadataUpdate = {
...chat?.metadata,
...metadata
};
const userValue: UserChatItemValueItemType[] = [
...files.map((file) => ({
file: {
type: file.type,
name: file.name,
url: '',
key: file.key || ''
}
})),
...(query
? [
{
text: {
content: query
}
}
]
: [])
];
await mongoSessionRun(async (session) => {
await MongoHelperBotChatItem.create(
[
{
userId,
chatId,
dataId: chatItemId,
obj: ChatRoleEnum.Human,
value: userValue
},
{
userId,
chatId,
dataId: chatItemId,
obj: ChatRoleEnum.AI,
value: aiResponse,
memories
}
],
{ session, ordered: true }
);
await MongoHelperBotChat.updateOne(
{
type,
userId,
chatId
},
{
updateTime: new Date(),
metadata: metadataUpdate
},
{
session
}
);
});
};
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { parseUrlToFileType } from './context';
import { getS3RawTextSource } from '../../../common/s3/sources/rawText';
import { isInternalAddress, PRIVATE_URL_TEXT } from '../../../common/system/utils';
import { pickOutboundAxios } from '../../../common/api/axios';
import { S3Buckets } from '../../../common/s3/config/constants';
import { S3Sources } from '../../../common/s3/contracts/type';
import { audioFileType, imageFileType, videoFileType } from '@fastgpt/global/common/file/constants';
import { ChatFileTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import type {
ChatItemMiniType,
UserChatItemFileItemType,
UserChatItemValueItemType
} from '@fastgpt/global/core/chat/type';
import { getS3RawTextSource } from '../../common/s3/sources/rawText';
import { isInternalAddress, PRIVATE_URL_TEXT } from '../../common/system/utils';
import { pickOutboundAxios } from '../../common/api/axios';
import { S3Buckets } from '../../common/s3/config/constants';
import { S3Sources } from '../../common/s3/contracts/type';
import {
detectFileEncoding,
parseContentDispositionFilename
} from '@fastgpt/global/common/file/tools';
import path from 'path';
import { getFileS3Key } from '../../../common/s3/utils';
import { S3ChatSource } from '../../../common/s3/sources/chat';
import { readFileContentByBuffer } from '../../../common/file/read/utils';
import { getFileS3Key } from '../../common/s3/utils';
import { S3ChatSource } from '../../common/s3/sources/chat';
import { readFileContentByBuffer } from '../../common/file/read/utils';
import { addDays } from 'date-fns';
import { replaceS3KeyToPreviewUrl } from '../../dataset/utils';
import { replaceS3KeyToPreviewUrl } from '../dataset/utils';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { getUserFilesPrompt, injectUserQueryPrompt } from '../../ai/llm/prompt';
import { getUserFilesPrompt, injectUserQueryPrompt } from '../ai/llm/prompt';
import { getAxiosHeaderValue } from '@fastgpt/global/common/axios/utils';
type GetFileProps = {
......@@ -29,6 +33,98 @@ type GetFileProps = {
usageId?: string;
};
/**
* 将 URL 解析成 ChatBox 文件结构。
*
* `urlTypeMap` 用于 workflow 运行态传入显式文件类型;普通聊天/辅助生成场景则按文件名后缀推断。
*/
export const parseUrlToChatFileType = ({
url,
urlTypeMap = {}
}: {
url: string;
urlTypeMap?: Record<string, ChatFileTypeEnum>;
}): UserChatItemFileItemType | undefined => {
if (typeof url !== 'string') return;
if (url.startsWith('data:')) {
const matches = url.match(/^data:([^;]+);base64,/);
if (!matches) return;
const mimeType = matches[1].toLowerCase();
if (!mimeType.startsWith('image/')) return;
const extension = mimeType.split('/')[1];
return {
type: ChatFileTypeEnum.image,
name: `image.${extension}`,
url
};
}
try {
const parseUrl = new URL(url, 'http://localhost:3000');
const filename = (() => {
if (url.startsWith('chat/')) {
const basename = path.basename(url);
return basename.includes('.') ? basename : '';
}
const fromParam = parseUrl.searchParams.get('filename');
if (fromParam) return fromParam;
const basename = path.basename(parseUrl.pathname);
return basename.includes('.') ? basename : '';
})();
const type = urlTypeMap[url];
if (type) {
return {
type,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
const extension = filename?.split('.').pop()?.toLowerCase() || '';
if (extension && imageFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.image,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && audioFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.audio,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && videoFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.video,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
return {
type: ChatFileTypeEnum.file,
name: filename ? decodeURIComponent(filename) : url,
url
};
} catch {
return {
type: ChatFileTypeEnum.file,
name: url,
url
};
}
};
export const formatUserQueryWithFiles = async ({
userQuery,
parseFileFn
......@@ -77,6 +173,55 @@ export const formatUserQueryWithFiles = async ({
};
/**
* 在发送给 LLM 前把 Human 消息里的普通文件解析为文本上下文。
*
* 历史 Human 消息是否解析由调用方控制;未解析时会移除历史文件项,避免旧文件继续作为模型文件输入。
*/
export const rewriteChatMessagesWithFileContext = async ({
messages,
parseHistoryFiles,
parseFileFn
}: {
messages: ChatItemMiniType[];
parseHistoryFiles: boolean;
parseFileFn: (urls: string[]) => Promise<
{
id?: string;
name: string;
url: string;
sandboxPath?: string;
content?: string;
}[]
>;
}) => {
return Promise.all(
messages.map(async (message, index): Promise<ChatItemMiniType> => {
if (message.obj !== ChatRoleEnum.Human) {
return message;
}
const isCurrentUserMessage = index === messages.length - 1;
if (!isCurrentUserMessage && !parseHistoryFiles) {
return {
...message,
value: message.value.filter((item) => !item.file)
};
}
const query = await formatUserQueryWithFiles({
userQuery: message.value,
parseFileFn
});
return {
...message,
value: query
};
})
);
};
/**
* 格式化文件 URL,移除请求头部分,只保留文件 URL
*/
export const normalizeReadableFileUrl = ({
......@@ -96,7 +241,7 @@ export const normalizeReadableFileUrl = ({
return '';
}
if (parseUrlToFileType(normalizedUrl)?.type !== ChatFileTypeEnum.file) {
if (parseUrlToChatFileType({ url: normalizedUrl })?.type !== ChatFileTypeEnum.file) {
return '';
}
......
......@@ -69,7 +69,14 @@ const beforeProcess = (props: Props) => {
const isFileValueWithKey = (file: unknown): file is { key: string } =>
!!file && typeof file === 'object' && 'key' in file && typeof file.key === 'string' && !!file.key;
const afterProcess = async ({
/**
* 对话内容落库成功后,移除已持久化文件的 S3 TTL 记录。
*
* ChatBox 上传文件时会先写入临时 TTL,避免用户只上传不发送时文件长期残留。
* 当包含这些 file key 的消息、工作流表单值或文件变量已经进入对话记录后,
* 这里统一删除 TTL,表示这些文件跟随对话生命周期持久化。
*/
export const persistChatFiles = async ({
contents,
variables,
variableList,
......@@ -363,7 +370,7 @@ export const finalizeChatRound = async (props: Props) => {
}
);
await afterProcess({
await persistChatFiles({
contents: processedContent,
variables,
variableList,
......@@ -584,7 +591,8 @@ export const pushChatRecords = async (props: Props) => {
shareId,
outLinkUid,
metadata: metadataUpdate,
updateTime: new Date()
updateTime: new Date(),
chatGenerateStatus: ChatGenerateStatusEnum.done
},
$setOnInsert: {
createTime: new Date()
......@@ -598,7 +606,7 @@ export const pushChatRecords = async (props: Props) => {
}
);
await afterProcess({
await persistChatFiles({
contents: processedContent,
variables,
variableList,
......@@ -890,7 +898,7 @@ export const updateInteractiveChat = async ({
}
);
await afterProcess({
await persistChatFiles({
contents: [userContent, aiContent],
variables,
variableList,
......
......@@ -22,8 +22,8 @@ export function buildChatSourceWriteFields({ sourceType, sourceId }: ChatSourceP
/**
* 构造 chat 三表 source-aware 查询条件。
*
* App 查询默认兼容缺失 `sourceType` 的历史数据;Skill Edit 必须精确匹配
* `sourceType=skillEdit`。
* App 查询默认兼容缺失 `sourceType` 的历史数据;非 App 来源必须精确匹配
* `sourceType`,避免不同资源类型复用同一物理 `appId` 字段时串记录。
*/
export function buildChatSourceQuery({ sourceType, sourceId }: ChatSourceParams) {
if (sourceType === ChatSourceTypeEnum.app) {
......@@ -33,10 +33,13 @@ export function buildChatSourceQuery({ sourceType, sourceId }: ChatSourceParams)
};
}
if (sourceType === ChatSourceTypeEnum.skillEdit) {
if (
sourceType === ChatSourceTypeEnum.skillEdit ||
sourceType === ChatSourceTypeEnum.chatAgentHelper
) {
return {
appId: sourceId,
sourceType: ChatSourceTypeEnum.skillEdit
sourceType
};
}
......
......@@ -119,11 +119,12 @@ export function parseAgentInputFiles({
.map(({ file, url }, index) => {
const parsedFile = parseUrlToFileType(url);
if (!parsedFile) return;
const type = file.type && file.type !== ChatFileTypeEnum.file ? file.type : parsedFile.type;
return {
id: `${prefixId}-${index}`,
name: getSafeSandboxInputFilename(file.name || parsedFile.name || url, index, usedNames),
type: parsedFile.type,
type,
url: parsedFile.url
};
})
......@@ -293,6 +294,15 @@ ${currentTime ? `当前时间: ${currentTime}` : ''}
${currentWorkingDirectory ? `当前 sandbox 工作目录: ${currentWorkingDirectory}` : ''}`;
};
const getAgentMultimodalChatFiles = (files: AgentInputFile[]) =>
files
.filter((file) => file.type !== ChatFileTypeEnum.file)
.map(({ name, type, url }) => ({
name,
type: type as ChatFileTypeEnum,
url
}));
const buildSandboxFileWriteBoundaryPrompt = ({
currentWorkingDirectory
}: {
......@@ -437,6 +447,7 @@ export const useUserContext = async ({
return {
...message,
value: runtimePrompt2ChatsValue({
files: getAgentMultimodalChatFiles(formatFiles),
text: buildAgentUserReminderInput({
query: text,
filesInfo: formatFiles
......@@ -479,6 +490,7 @@ export const useUserContext = async ({
const currentUserMessage: ChatItemMiniType = {
...currentMessage,
value: runtimePrompt2ChatsValue({
files: getAgentMultimodalChatFiles(currentInputFiles),
// 当前 Human 才注入完整 reminder:sandbox、skill、文件、知识库、当前时间和原始问题。
text: buildAgentUserReminderInput({
query: currentUserInput,
......
......@@ -9,6 +9,7 @@ import type {
ChatHistoryItemResType,
ChatItemMiniType
} from '@fastgpt/global/core/chat/type';
import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { chats2GPTMessages } from '@fastgpt/global/core/chat/adapt';
import { getSystemToolInfo } from '@fastgpt/global/core/workflow/node/agent/constants';
import { SANDBOX_SYSTEM_PROMPT } from '@fastgpt/global/core/ai/sandbox/constants';
......@@ -177,6 +178,7 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
effectiveUseAgentSandbox && useAgentSandbox && global.feConfigs?.show_agent_sandbox
? sandboxEntrypoint
: undefined;
const skipSandboxInputFiles = runningAppInfo.sourceType === ChatSourceTypeEnum.skillEdit;
// 初始化对话框输入的文件
const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList);
......@@ -229,7 +231,7 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
selectedSkills,
editSkillId,
prepareActions: agentSandboxPrepareActions,
currentFiles: userContext.currentFiles
currentFiles: skipSandboxInputFiles ? [] : userContext.currentFiles
});
// 获取请求上下文
const { chatHistories, queryInput, filesMap, fileUrlMap } = userContext;
......
......@@ -2,7 +2,7 @@ import { getErrText } from '@fastgpt/global/common/error/utils';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { i18nT } from '@fastgpt/global/common/i18n/utils';
import type { DispatchSubAppResponse } from '../../type';
import { getFileContentByUrl } from '../../../../../utils/file';
import { getFileContentByUrl } from '../../../../../../chat/fileContext';
type FileReadParams = {
files: { id: string; url: string }[];
......
......@@ -284,11 +284,10 @@ export const getExecuteTool = ({
};
}
const ids = toolParams.data.ids;
const files = ids.map((id) => ({
id,
url: filesMap[id]
}));
const files = ids.flatMap((id) => {
const url = filesMap[id];
return url ? [{ id, url }] : [];
});
const result = await dispatchFileRead({
files,
teamId: runningUserInfo.teamId,
......
......@@ -8,7 +8,7 @@ import {
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import type { LLMModelItemType } from '@fastgpt/global/core/ai/model.schema';
import type { ChatDispatchProps } from '@fastgpt/global/core/workflow/runtime/type';
import { parseFileContentFromUrls } from '../../../utils/file';
import { parseFileContentFromUrls } from '../../../../chat/fileContext';
import { rewriteChatMessagesWithFiles } from './fileContext';
/**
......
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import type { FlowNodeInputItemType } from '@fastgpt/global/core/workflow/type/io';
import { parseUrlToFileType } from '../../../utils/context';
import { formatUserQueryWithFiles } from '../../../utils/file';
import { rewriteChatMessagesWithFileContext } from '../../../../chat/fileContext';
import type { ChatMessageFileParser } from './type';
/**
......@@ -44,31 +43,11 @@ export const rewriteChatMessagesWithFiles = async ({
parseHistoryFiles: boolean;
parseFileFn: ChatMessageFileParser;
}) => {
return Promise.all(
messages.map(async (message, index): Promise<ChatItemMiniType> => {
if (message.obj !== ChatRoleEnum.Human) {
return message;
}
const isCurrentUserMessage = index === messages.length - 1;
if (!isCurrentUserMessage && !parseHistoryFiles) {
return {
...message,
value: message.value.filter((item) => !item.file)
};
}
const query = await formatUserQueryWithFiles({
userQuery: message.value,
parseFileFn
});
return {
...message,
value: query
};
})
);
return rewriteChatMessagesWithFileContext({
messages,
parseHistoryFiles,
parseFileFn
});
};
/**
......
......@@ -5,7 +5,7 @@ import {
} from '@fastgpt/global/core/chat/adapt';
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants';
import { formatUserQueryWithFiles, parseFileInfoFromUrls } from '../../../../utils/file';
import { formatUserQueryWithFiles, parseFileInfoFromUrls } from '../../../../../chat/fileContext';
import { parseUrlToFileType } from '../../../../utils/context';
import type { DispatchToolModuleProps, FileInputType } from '../type';
......
import type { ChatCompletionTool } from '@fastgpt/global/core/ai/llm/type';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { getFileContentByUrl } from '../../../../utils/file';
import { getFileContentByUrl } from '../../../../../chat/fileContext';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { getLogger } from '@fastgpt-sdk/otel/logger';
import { LogCategories } from '../../../../../../common/logger';
......
......@@ -34,8 +34,12 @@ import { dispatchIfElse } from './tools/runIfElse';
import { dispatchUpdateVariable } from './tools/runUpdateVar';
import { dispatchTextEditor } from './tools/textEditor';
import { dispatchRunAgent } from './ai/agent';
import { dispatchInternalRuntimeNode, internalRuntimeNodeType } from './internal/runtimeNode';
export const callbackMap: Record<FlowNodeTypeEnum, (...args: any[]) => unknown> = {
export const callbackMap: Record<
FlowNodeTypeEnum | typeof internalRuntimeNodeType,
(...args: any[]) => unknown
> = {
[FlowNodeTypeEnum.workflowStart]: dispatchWorkflowStart,
// Child
......@@ -74,6 +78,7 @@ export const callbackMap: Record<FlowNodeTypeEnum, (...args: any[]) => unknown>
[FlowNodeTypeEnum.nestedEnd]: dispatchLoopEnd,
[FlowNodeTypeEnum.formInput]: dispatchFormInput,
[FlowNodeTypeEnum.tool]: dispatchRunTool,
[internalRuntimeNodeType]: dispatchInternalRuntimeNode,
// none
[FlowNodeTypeEnum.systemConfig]: dispatchSystemConfig,
......
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
export const internalRuntimeNodeRunnerKey = '__internalRuntimeNodeRunner';
export const internalRuntimeNodeType = '__internalRuntimeNode';
export type InternalRuntimeNodeRunnerProps = {
usagePush: (usages: ChatNodeUsageType[]) => void;
checkIsStopping: () => boolean;
};
export type InternalRuntimeNodeRunner = (
props: InternalRuntimeNodeRunnerProps
) => Promise<Record<string, any>> | Record<string, any>;
/**
* 执行只存在于服务端内存里的 runtime 节点。
*
* 这类节点不来自用户工作流模板,也不会持久化到应用配置;调用方用它把受控业务逻辑接入
* workflow runtime,从而复用 checkTeamAIPoints、usage record、nodeResponse writer 等通用能力。
*/
export const dispatchInternalRuntimeNode = async (props: Record<string, any>) => {
const runner = props.params?.[internalRuntimeNodeRunnerKey];
if (typeof runner !== 'function') {
return {
error: {
[NodeOutputKeyEnum.errorText]: 'Internal runtime node runner is missing'
},
[DispatchNodeResponseKeyEnum.nodeResponse]: {
error: 'Internal runtime node runner is missing'
}
};
}
return runner(props);
};
......@@ -6,7 +6,7 @@ import { type DispatchNodeResultType } from '@fastgpt/global/core/workflow/runti
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { type ChatItemMiniType } from '@fastgpt/global/core/chat/type';
import { getNodeErrResponse } from '../utils';
import { parseFileContentFromUrls } from '../../utils/file';
import { parseFileContentFromUrls } from '../../../chat/fileContext';
import { sliceStrStartEnd } from '@fastgpt/global/common/string/tools';
type Props = ModuleDispatchProps<{
......
import { audioFileType, imageFileType, videoFileType } from '@fastgpt/global/common/file/constants';
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import type { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import { parseUrlToChatFileType } from '../../chat/fileContext';
import { AsyncLocalStorage } from 'async_hooks';
import path from 'path';
import type { MCPClient } from '../../app/mcp';
type ContextType = {
......@@ -34,92 +32,9 @@ export const updateWorkflowContextVal = (val: Partial<ContextType>) => {
}
};
// Url => user upload file type
export const parseUrlToFileType = (url: string): UserChatItemFileItemType | undefined => {
if (typeof url !== 'string') return;
// Handle base64 image
if (url.startsWith('data:')) {
const matches = url.match(/^data:([^;]+);base64,/);
if (!matches) return;
const mimeType = matches[1].toLowerCase();
if (!mimeType.startsWith('image/')) return;
const extension = mimeType.split('/')[1];
return {
type: ChatFileTypeEnum.image,
name: `image.${extension}`,
url
};
}
try {
const parseUrl = new URL(url, 'http://localhost:3000');
// Get filename from URL
const filename = (() => {
// Here is a S3 Object Key
if (url.startsWith('chat/')) {
const basename = path.basename(url);
// Return empty if no extension
return basename.includes('.') ? basename : '';
}
const fromParam = parseUrl.searchParams.get('filename');
if (fromParam) {
return fromParam;
}
const basename = path.basename(parseUrl.pathname);
// Return empty if no extension
return basename.includes('.') ? basename : '';
})();
const context = getWorkflowContext();
const type = context?.queryUrlTypeMap[url];
if (type) {
return {
type,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
const extension = filename?.split('.').pop()?.toLowerCase() || '';
if (extension && imageFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.image,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && audioFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.audio,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
if (extension && videoFileType.includes(extension)) {
return {
type: ChatFileTypeEnum.video,
name: filename ? decodeURIComponent(filename) : url,
url
};
}
// If it's a document type, return as file, otherwise treat as image
return {
type: ChatFileTypeEnum.file,
name: filename ? decodeURIComponent(filename) : url,
url
};
} catch {
return {
type: ChatFileTypeEnum.file,
name: url,
url
};
}
};
/** 结合 workflow 运行态 URL 类型映射,将 URL 解析成 ChatBox 文件结构。 */
export const parseUrlToFileType = (url: string) =>
parseUrlToChatFileType({
url,
urlTypeMap: getWorkflowContext()?.queryUrlTypeMap
});
......@@ -5,6 +5,7 @@ import { STREAM_RESUME_REQUEST_HEADER } from '@fastgpt/global/core/chat/constant
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { textAdaptGptResponse } from '@fastgpt/global/core/workflow/runtime/utils';
import { createSseStreamContext } from '../../../common/response/sse';
import { getStreamResumeMirror } from '../../chat/resume';
import { getWorkflowResponseWrite } from '../dispatch/utils';
......@@ -99,42 +100,30 @@ export const initWorkflowSseResponse = ({
return;
}
if (!res.headersSent) {
res.setHeader('Connection', 'keep-alive');
res.setHeader('Content-Type', 'text/event-stream;charset=utf-8');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('X-Accel-Buffering', 'no');
res.setHeader('Cache-Control', 'no-cache, no-transform');
}
let cleaned = false;
const cleanup = () => {
if (cleaned) return;
cleaned = true;
clearInterval(streamCheckTimer);
delete workflowRes[workflowSseResponseControllerKey];
};
res.once('finish', cleanup);
res.once('close', cleanup);
res.on('error', () => {
cleanup();
onError?.();
res.end();
const sseContext = createSseStreamContext({
res,
stream,
onError,
onCleanup: () => {
delete workflowRes[workflowSseResponseControllerKey];
},
// 10s 发送一次空 answer,沿用统一 SSE writer,避免浏览器或代理认为长连接已断开。
heartbeat: {
write: () => {
responseWrite?.({
event: SseResponseEventEnum.answer,
data: textAdaptGptResponse({
text: ''
})
});
}
}
});
// 10s 发送一次空 answer,沿用统一 SSE writer,避免浏览器或代理认为长连接已断开。
const streamCheckTimer = setInterval(() => {
responseWrite?.({
event: SseResponseEventEnum.answer,
data: textAdaptGptResponse({
text: ''
})
});
}, 10000);
const controller: WorkflowSseResponseController = {
cleanup
cleanup: () => {
sseContext.cleanup();
}
};
workflowRes[workflowSseResponseControllerKey] = controller;
};
......
......@@ -244,10 +244,6 @@ export const serviceEnv = createEnv({
AGENT_ENGINE: z.enum(['default', 'pi']).default('default').meta({
description: 'Agent 引擎选择:default(unified agent loop)| pi(pi-agent-core 引擎)'
}),
HELPER_BOT_MODEL: z
.string()
.optional()
.meta({ description: '辅助生成模型(暂时只能指定一个,需保证系统中已激活该模型)' }),
SKIP_FILE_TYPE_CHECK: BoolSchema.default(false).meta({
description: '是否跳过文件类型检查'
}),
......
......@@ -175,6 +175,8 @@ export const checkTeamDatasetSyncPermission = async (teamId: string) => {
};
export const checkTeamSandboxPermission = async (teamId: string) => {
if (!global.subPlans?.standard) return;
const { standard } = await getTeamStandPlan({
teamId
});
......
......@@ -160,6 +160,7 @@ export const initTeamFreePlan = async ({
// 获取团队标准套餐
export const getTeamStandPlan = async ({ teamId }: { teamId: string }) => {
const standardPlans = global.subPlans?.standard;
const plans = await MongoTeamSub.find(
{
teamId,
......@@ -172,11 +173,10 @@ export const getTeamStandPlan = async ({ teamId }: { teamId: string }) => {
).lean();
sortStandPlans(plans);
const standardPlans = global.subPlans?.standard;
const standard = plans[0];
const standardConstants =
standard.currentSubLevel && standardPlans
standard?.currentSubLevel && standardPlans
? standardPlans[
standard.currentSubLevel === StandardSubLevelEnum.custom
? StandardSubLevelEnum.advanced
......@@ -185,9 +185,8 @@ export const getTeamStandPlan = async ({ teamId }: { teamId: string }) => {
: undefined;
return {
[SubTypeEnum.standard]: standardConstants
? buildStandardPlan(standard, standardConstants)
: undefined
[SubTypeEnum.standard]:
standard && standardConstants ? buildStandardPlan(standard, standardConstants) : undefined
};
};
......
......@@ -7,10 +7,6 @@ import {
isAuthorizedDatasetFileS3Key,
parseDatasetFileS3Key
} from '@fastgpt/service/common/s3/sources/dataset/key';
import {
isAuthorizedHelperBotFileS3Key,
parseHelperBotFileS3Key
} from '@fastgpt/service/common/s3/sources/helperbot/key';
import { isAuthorizedTempFileS3Key } from '@fastgpt/service/common/s3/sources/temp/key';
describe('authorized S3 object key helpers', () => {
......@@ -125,20 +121,6 @@ describe('authorized S3 object key helpers', () => {
expect(parseDatasetFileS3Key('dataset/dataset-1')).toBeNull();
});
it('parses and authorizes helper bot file keys by userId', () => {
const key = 'helperBot/topAgent/user-1/chat-1/demo.pdf';
expect(parseHelperBotFileS3Key(key)).toEqual({
type: 'topAgent',
userId: 'user-1',
chatId: 'chat-1',
filename: 'demo.pdf'
});
expect(isAuthorizedHelperBotFileS3Key({ key, userId: 'user-1' })).toBe(true);
expect(isAuthorizedHelperBotFileS3Key({ key, userId: 'user-2' })).toBe(false);
expect(parseHelperBotFileS3Key('helperBot/unknown/user-1/chat-1/demo.pdf')).toBeNull();
});
it('authorizes temp file keys by exact team path segment', () => {
expect(isAuthorizedTempFileS3Key({ key: 'temp/team-1/demo.pdf', teamId: 'team-1' })).toBe(true);
expect(isAuthorizedTempFileS3Key({ key: 'temp/team-11/demo.pdf', teamId: 'team-1' })).toBe(
......
......@@ -13,6 +13,8 @@ import { MongoChatItem } from '@fastgpt/service/core/chat/chatItemSchema';
import { MongoChat } from '@fastgpt/service/core/chat/chatSchema';
import { MongoAppChatLog } from '@fastgpt/service/core/app/logs/chatLogsSchema';
import { MongoChatItemResponse } from '@fastgpt/service/core/chat/chatItemResponseSchema';
import { MongoS3TTL } from '@fastgpt/service/common/s3/models/ttl';
import { S3Buckets } from '@fastgpt/service/common/s3/config/constants';
import {
ChatFileTypeEnum,
ChatGenerateStatusEnum,
......@@ -606,6 +608,103 @@ describe('pushChatRecords', () => {
expect(aiItem?.responseData).toBeUndefined();
});
it('should persist skill edit uploaded files by removing S3 TTL after finalization', async () => {
const responseChatItemId = 'skill-edit-file-finalize';
const fileKey = `chat/${ChatSourceTypeEnum.skillEdit}/${testAppId}/${testTmbId}/skill-chat-id/spec.md`;
const props = createMockProps(
{
chatId: 'skill-chat-id',
sourceType: ChatSourceTypeEnum.skillEdit,
sourceId: testAppId,
userContent: {
dataId: responseChatItemId,
obj: ChatRoleEnum.Human,
value: [
{
file: {
type: ChatFileTypeEnum.file,
name: 'spec.md',
url: 'https://example.com/temp-preview',
key: fileKey
}
}
]
},
aiContent: {
dataId: responseChatItemId,
obj: ChatRoleEnum.AI,
value: [
{
text: {
content: 'Skill debug answer'
}
}
]
}
},
{ appId: testAppId, teamId: testTeamId, tmbId: testTmbId }
);
await MongoS3TTL.create({
bucketName: S3Buckets.private,
minioKey: fileKey,
expiredTime: new Date(Date.now() + 60 * 60 * 1000)
});
await MongoChat.create({
appId: testAppId,
chatId: props.chatId,
teamId: testTeamId,
tmbId: testTmbId,
sourceType: ChatSourceTypeEnum.skillEdit,
source: props.source,
chatGenerateStatus: ChatGenerateStatusEnum.generating,
hasBeenRead: false
});
await MongoChatItem.create([
{
teamId: testTeamId,
tmbId: testTmbId,
sourceType: ChatSourceTypeEnum.skillEdit,
appId: testAppId,
chatId: props.chatId,
dataId: responseChatItemId,
obj: ChatRoleEnum.Human,
value: []
},
{
teamId: testTeamId,
tmbId: testTmbId,
sourceType: ChatSourceTypeEnum.skillEdit,
appId: testAppId,
chatId: props.chatId,
dataId: responseChatItemId,
obj: ChatRoleEnum.AI,
value: []
}
]);
await finalizeChatRound(props);
const ttl = await MongoS3TTL.findOne({
bucketName: S3Buckets.private,
minioKey: fileKey
}).lean();
expect(ttl).toBeNull();
const humanItem = await MongoChatItem.findOne({
sourceType: ChatSourceTypeEnum.skillEdit,
appId: testAppId,
chatId: props.chatId,
obj: ChatRoleEnum.Human
}).lean();
expect(humanItem?.value[0].file).toEqual({
type: ChatFileTypeEnum.file,
name: 'spec.md',
url: '',
key: fileKey
});
});
it('should mark prepared round as error and keep ai placeholder', async () => {
const responseChatItemId = 'prepared-ai-error';
const props = createMockProps({}, { appId: testAppId, teamId: testTeamId, tmbId: testTmbId });
......
......@@ -20,6 +20,7 @@ vi.mock('@fastgpt/global/common/time/timezone', () => ({
getSystemTime: vi.fn(() => '2026-05-14 10:00:00 Thursday')
}));
vi.mock('@fastgpt/service/core/dataset/schema', () => ({
DatasetCollectionName: 'datasets',
MongoDataset: {
find: vi.fn(() => ({
lean: vi.fn(async () => [])
......@@ -211,6 +212,28 @@ describe('parseAgentInputFiles', () => {
]);
expect(files.every((file) => !file.name.includes('/'))).toBe(true);
});
it('keeps explicit image type when preview url has no file extension', () => {
const files = parseAgentInputFiles({
files: [
{
name: '20260629-233136_wT851P.jpg',
type: ChatFileTypeEnum.image,
url: 'https://fastgpt.example.com/api/core/chat/file/preview?token=abc'
}
],
prefixId: 'current',
maxFiles: 10
});
expect(files).toEqual([
expect.objectContaining({
name: '20260629-233136_wT851P.jpg',
type: ChatFileTypeEnum.image,
url: 'https://fastgpt.example.com/api/core/chat/file/preview?token=abc'
})
]);
});
});
describe('buildAgentUserReminderInput', () => {
......@@ -412,6 +435,7 @@ describe('useUserContext', () => {
{
queryUrlTypeMap: {
'/old.pdf': ChatFileTypeEnum.file,
'/old.png': ChatFileTypeEnum.image,
'/current.pdf': ChatFileTypeEnum.file,
'/current.png': ChatFileTypeEnum.image
},
......@@ -421,7 +445,10 @@ describe('useUserContext', () => {
const history = createHumanMessage({
dataId: 'history_1',
text: '历史问题',
files: [{ name: 'old.pdf', url: '/old.pdf' }]
files: [
{ name: 'old.pdf', url: '/old.pdf' },
{ name: 'old.png', url: '/old.png', type: ChatFileTypeEnum.image }
]
});
const result = await getUserContextMessagesForTest({
history: 6,
......@@ -452,19 +479,36 @@ describe('useUserContext', () => {
});
expect(result.fileUrlMap).toEqual({
'history_1-0': '/old.pdf',
'history_1-1': '/old.png',
'current_chat_item-0': '/current.pdf',
'current_chat_item-1': '/current.png'
});
const { text: historyText } = chatValue2RuntimePrompt(result.rewrittenHistories[0].value);
const { text: historyText, files: historyFiles } = chatValue2RuntimePrompt(
result.rewrittenHistories[0].value
);
const { text: currentText, files: currentFiles } = chatValue2RuntimePrompt(
result.currentUserMessage.value
);
expect(historyFiles).toEqual([
{
name: 'old.png',
type: ChatFileTypeEnum.image,
url: '/old.png'
}
]);
expect(historyText).toContain('<id>history_1-0</id>');
expect(historyText).toContain('<id>history_1-1</id>');
expect(historyText).not.toContain('当前 sandbox 工作目录');
expect(historyText).not.toContain('当前时间');
expect(currentFiles).toEqual([]);
expect(currentFiles).toEqual([
{
name: 'current.png',
type: ChatFileTypeEnum.image,
url: '/current.png'
}
]);
expect(currentText).toContain('## 背景信息');
expect(currentText).toContain('当前 sandbox 工作目录: /workspace');
expect(currentText).toContain('<id>current_chat_item-0</id>');
......@@ -724,6 +768,47 @@ describe('useUserContext', () => {
);
});
it('keeps uploaded image as multimodal content when preview url has no extension', async () => {
await runWithContextAsync(
{
queryUrlTypeMap: {},
mcpClientMemory: {}
},
async () => {
const previewUrl = 'https://fastgpt.example.com/api/core/chat/file/preview?token=abc';
const result = await getUserContextMessagesForTest({
history: 0,
histories: [],
currentUserInput: '看一下这张图',
currentDataId: 'current_chat_item',
currentQuery: runtimePrompt2ChatsValue({
text: '前端原始问题',
files: [
{
name: '20260629-233136_wT851P.jpg',
url: previewUrl,
type: ChatFileTypeEnum.image
}
]
}),
tmbId: 'tmb_1',
timezone: 'Asia/Shanghai',
maxFiles: 20
});
const { files, text } = chatValue2RuntimePrompt(result.currentUserMessage.value);
expect(files).toEqual([
expect.objectContaining({
type: ChatFileTypeEnum.image,
name: '20260629-233136_wT851P.jpg',
url: previewUrl
})
]);
expect(text).toContain('<type>image</type>');
}
);
});
it('uses message index when historical human has no human dataId', async () => {
await runWithContextAsync(
{
......
......@@ -509,6 +509,48 @@ describe('dispatchRunAgent user context', () => {
expect(loopInput.messages.at(-1)?.content).toContain('<path>./SKILL.md</path>');
});
it('does not inject current files into sandbox for skill edit preview', async () => {
const { dispatchRunAgent } = await import('@fastgpt/service/core/workflow/dispatch/ai/agent');
const props = createProps();
props.runningAppInfo.sourceType = ChatSourceTypeEnum.skillEdit;
props.runningAppInfo.sourceId = 'edit_skill_1';
props.params.editSkillId = 'edit_skill_1';
ensureAgentSandboxRuntimeMock.mockResolvedValueOnce({
sandboxClient: {
provider: {
writeFiles: vi.fn()
},
exec: sandboxClientExecMock,
getSandboxId: () => 'sandbox_prepared'
},
currentWorkingDirectory: '/workspace',
skillInfos: []
});
let result: any;
runWithContext(
{
queryUrlTypeMap: {
'/current.pdf': ChatFileTypeEnum.file
},
mcpClientMemory: {}
},
() => {
result = dispatchRunAgent(props);
}
);
await result;
expect(ensureAgentSandboxRuntimeMock).toHaveBeenCalledWith(
expect.objectContaining({
sourceType: ChatSourceTypeEnum.skillEdit,
sourceId: 'edit_skill_1',
editSkillId: 'edit_skill_1',
currentFiles: []
})
);
});
it('returns the final answer as assistant response', async () => {
const { dispatchRunAgent } = await import('@fastgpt/service/core/workflow/dispatch/ai/agent');
......
......@@ -23,7 +23,6 @@ const {
ensureAgentSandboxRuntimeMock,
streamAgentSandboxInitStatusMock,
sandboxClientExecMock,
axiosGetMock,
getAgentRuntimeToolsMock
} = vi.hoisted(() => ({
agentPromptMock: vi.fn(),
......@@ -37,7 +36,6 @@ const {
ensureAgentSandboxRuntimeMock: vi.fn(),
streamAgentSandboxInitStatusMock: vi.fn(),
sandboxClientExecMock: vi.fn(),
axiosGetMock: vi.fn(),
getAgentRuntimeToolsMock: vi.fn(async () => [])
}));
......@@ -87,18 +85,6 @@ vi.mock('@fastgpt/service/core/workflow/dispatch/ai/agent/sub/sandbox', async (i
};
});
vi.mock('@fastgpt/service/common/api/axios', async (importOriginal) => {
const original = await importOriginal<typeof import('@fastgpt/service/common/api/axios')>();
const mockClient = {
get: axiosGetMock
};
return {
...original,
pickOutboundAxios: () => mockClient
};
});
vi.mock('@fastgpt/service/core/dataset/schema', async (importOriginal) => {
const original = await importOriginal<typeof import('@fastgpt/service/core/dataset/schema')>();
return {
......@@ -256,9 +242,6 @@ describe('dispatchPiAgent user context', () => {
stdout: '/workspace\n',
stderr: ''
});
axiosGetMock.mockResolvedValue({
data: new ArrayBuffer(1)
});
ensureAgentSandboxRuntimeMock.mockResolvedValue({
sandboxClient: {
provider: {
......
......@@ -5,8 +5,8 @@ const { getFileContentByUrlMock } = vi.hoisted(() => ({
getFileContentByUrlMock: vi.fn()
}));
vi.mock('@fastgpt/service/core/workflow/utils/file', async (importOriginal) => {
const mod = await importOriginal<typeof import('@fastgpt/service/core/workflow/utils/file')>();
vi.mock('@fastgpt/service/core/chat/fileContext', async (importOriginal) => {
const mod = await importOriginal<typeof import('@fastgpt/service/core/chat/fileContext')>();
return {
...mod,
getFileContentByUrl: getFileContentByUrlMock
......
......@@ -30,21 +30,22 @@ vi.mock('@fastgpt/service/core/app/mcp', () => ({
}))
}));
vi.mock('@fastgpt/service/common/logger', () => ({
LogCategories: {
MODULE: {
APP: {
TOOL: 'tool'
},
AI: {
LLM: 'llm'
}
}
},
getLogger: vi.fn(() => ({
error: vi.fn()
}))
}));
vi.mock('@fastgpt/service/common/logger', async () => {
const actual = await vi.importActual<typeof import('@fastgpt/service/common/logger')>(
'@fastgpt/service/common/logger'
);
return {
...actual,
getLogger: vi.fn(() => ({
log: vi.fn(),
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn()
}))
};
});
vi.mock('@fastgpt/service/common/middle/tracks/utils', () => ({
pushTrack: {
......
......@@ -163,6 +163,59 @@ describe('Agent read_files tool protocol', () => {
);
});
it('ignores read_files ids that are not registered document files', async () => {
const executeTool = getExecuteTool({
checkIsStopping: vi.fn(),
chatConfig: {},
runningUserInfo: {
teamId: 'team_1',
tmbId: 'tmb_1'
},
runningAppInfo: {
id: 'app_1'
},
chatId: 'chat_1',
uid: 'user_1',
variableState: {} as any,
externalProvider: {
openaiAccount: undefined
} as any,
lang: 'zh-CN',
requestOrigin: '',
mode: 'chat',
timezone: 'Asia/Shanghai',
retainDatasetCite: false,
maxRunTimes: 10,
workflowDispatchDeep: 0,
params: {
model: 'gpt-4'
},
stream: false,
getSubAppInfo: () => ({
name: '文件解析',
avatar: '',
toolDescription: ''
}),
getSubApp: () => undefined,
completionTools: [readFileTool],
filesMap: {}
} as any);
const result = await executeTool({
callId: 'call_read_files',
toolId: SubAppIds.readFiles,
args: '{"ids":["current-0"]}'
});
expect(dispatchFileReadMock).toHaveBeenCalledTimes(1);
expect(dispatchFileReadMock).toHaveBeenCalledWith(
expect.objectContaining({
files: []
})
);
expect(result.response).toBe('file content');
});
it('replaces agent file ids before dispatching user tools', async () => {
dispatchToolMock.mockResolvedValue({
response: 'tool response',
......
......@@ -10,7 +10,7 @@ const { formatUserQueryWithFilesMock, parseFileInfoFromUrlsMock, parseUrlToFileT
parseUrlToFileTypeMock: vi.fn()
}));
vi.mock('@fastgpt/service/core/workflow/utils/file', () => ({
vi.mock('@fastgpt/service/core/chat/fileContext', () => ({
formatUserQueryWithFiles: formatUserQueryWithFilesMock,
parseFileInfoFromUrls: parseFileInfoFromUrlsMock
}));
......
......@@ -5,7 +5,7 @@ const { getFileContentByUrlMock } = vi.hoisted(() => ({
getFileContentByUrlMock: vi.fn()
}));
vi.mock('@fastgpt/service/core/workflow/utils/file', () => ({
vi.mock('@fastgpt/service/core/chat/fileContext', () => ({
getFileContentByUrl: getFileContentByUrlMock
}));
......
......@@ -23,6 +23,7 @@ vi.mock('@fastgpt/service/core/dataset/search', () => ({
}));
vi.mock('@fastgpt/service/core/dataset/schema', () => ({
DatasetCollectionName: 'datasets',
MongoDataset: {
findById: findDatasetByIdMock
}
......
......@@ -6,7 +6,7 @@ import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runti
const mockparseFileContentFromUrls = vi.hoisted(() => vi.fn());
vi.mock('@fastgpt/service/core/workflow/utils/file', () => ({
vi.mock('@fastgpt/service/core/chat/fileContext', () => ({
parseFileContentFromUrls: mockparseFileContentFromUrls
}));
......
......@@ -84,7 +84,7 @@ import {
parseFileInfoFromUrls,
normalizeReadableFileUrl,
formatUserQueryWithFiles
} from '@fastgpt/service/core/workflow/utils/file';
} from '@fastgpt/service/core/chat/fileContext';
const createHumanMessage = (value: UserChatItemValueItemType[]): ChatItemMiniType => ({
obj: ChatRoleEnum.Human,
......
......@@ -836,9 +836,22 @@ describe('checkTeamDatasetSyncPermission', () => {
describe('checkTeamSandboxPermission', () => {
beforeEach(() => {
vi.clearAllMocks();
delete (global as any).subPlans;
});
it('当 global.subPlans.standard 不存在时直接返回', async () => {
const getTeamStandPlanSpy = vi.spyOn(walletUtils, 'getTeamStandPlan');
await expect(checkTeamSandboxPermission(mockTeamId)).resolves.toBeUndefined();
expect(getTeamStandPlanSpy).not.toHaveBeenCalled();
});
it('当 enableSandbox 为 false 时抛出错误', async () => {
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.basic]: {}
}
};
const mockStandard = {
standard: {
enableSandbox: false
......@@ -852,6 +865,11 @@ describe('checkTeamSandboxPermission', () => {
});
it('当 standard 不存在时不抛出错误', async () => {
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.basic]: {}
}
};
const mockStandard = {
standard: undefined
};
......@@ -861,6 +879,11 @@ describe('checkTeamSandboxPermission', () => {
});
it('当 enableSandbox 为 true 时正常通过', async () => {
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.basic]: {}
}
};
const mockStandard = {
standard: {
enableSandbox: true
......
......@@ -678,6 +678,16 @@ describe('getTeamStandPlan', () => {
delete (global as any).subPlans;
});
it('未开启标准套餐配置且没有套餐记录时返回 undefined', async () => {
vi.spyOn(MongoTeamSub, 'find').mockReturnValue({
lean: vi.fn().mockResolvedValue([])
} as any);
const result = await getTeamStandPlan({ teamId: mockTeamId });
expect(result[SubTypeEnum.standard]).toBeUndefined();
});
it('返回团队标准套餐', async () => {
const teamId = mockTeamId;
const mockPlan = {
......@@ -722,6 +732,32 @@ describe('getTeamStandPlan', () => {
expect(result[SubTypeEnum.standard]).toBeDefined();
expect(result[SubTypeEnum.standard]?.name).toBe('Basic Plan');
});
it('开启标准套餐配置但没有套餐记录时返回 undefined', async () => {
vi.spyOn(MongoTeamSub, 'find').mockReturnValue({
lean: vi.fn().mockResolvedValue([])
} as any);
(global as any).subPlans = {
standard: {
[StandardSubLevelEnum.free]: {
name: 'Free Plan',
price: 0,
totalPoints: 100,
maxTeamMember: 1,
maxAppAmount: 3,
maxDatasetAmount: 1,
maxDatasetSize: 10,
chatHistoryStoreDuration: 7,
enableSandbox: true
}
}
};
const result = await getTeamStandPlan({ teamId: mockTeamId });
expect(result[SubTypeEnum.standard]).toBeUndefined();
});
});
describe('getTeamPlanStatus', () => {
......
Subproject commit cb39de11f8364bc2496ee04804379b3347f322cf
Subproject commit dd14c92c8816a91dc85611cb3d749b3fac5f4bed
......@@ -188,8 +188,6 @@ OPENAPI_KEY_MAX_COUNT=100
# Agent 引擎选择:default(Plan+Step 编排)| pi(pi-agent-core 引擎)
AGENT_ENGINE=default
# 辅助生成模型(暂时只能指定一个,需保证系统中已激活该模型)
HELPER_BOT_MODEL=qwen-max
# 对话标题生成模型(不填则使用默认 LLM 模型)
CHAT_TITLE_MODEL=
SKIP_FILE_TYPE_CHECK=false
......
......@@ -21,7 +21,7 @@ const BotShowRouter: { [key: string]: boolean } = {
'/account/info': true
};
const HelperBot = () => {
const SupportBot = () => {
const router = useRouter();
const { i18n } = useTranslation();
const [open, setOpen] = useToggle(true);
......@@ -47,12 +47,6 @@ const HelperBot = () => {
const botIframeUrl = feConfigs?.botIframeUrl;
useEffect(() => {
if (showChat) {
setIsLoading(true);
}
}, [showChat]);
useEffect(() => {
const handleMessage = async (event: MessageEvent) => {
if (event.data?.type === 'workorderRequest') {
if (!hasTicketAccess) {
......@@ -197,6 +191,9 @@ const HelperBot = () => {
}}
onClick={() => {
if (open) {
if (!showChat) {
setIsLoading(true);
}
setShowChat.toggle();
} else {
setOpen.set(true);
......@@ -208,4 +205,4 @@ const HelperBot = () => {
);
};
export default HelperBot;
export default SupportBot;
......@@ -15,7 +15,7 @@ import { useDebounceEffect, useMount } from 'ahooks';
import { useTranslation } from 'next-i18next';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { useCheckCoupon } from './hooks/checkCoupon';
import HelperBot from './HelperBot';
import SupportBot from './SupportBot';
const Navbar = dynamic(() => import('./navbar'));
const NavbarPhone = dynamic(() => import('./navbarPhone'));
......@@ -213,7 +213,7 @@ const Layout = ({ children }: { children: JSX.Element }) => {
<ImportantInform informs={importantInforms} refetch={refetchUnRead} />
)}
<ResetExpiredPswModal />
<HelperBot />
<SupportBot />
</>
)}
<EnterpriseAuthNoticeModal key={`${router.pathname}-${userInfo?.team?.teamId ?? ''}`} />
......
import React, { type ReactNode, useEffect, useImperativeHandle, useMemo } from 'react';
import { useContextSelector } from 'use-context-selector';
import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { createChatAgentHelperAppConfig } from '@fastgpt/global/core/ai/auxiliaryGeneration/chatAgentHelper';
import { ChatAgentHelperTypeEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import { ChatTypeEnum } from '../ChatContainer/ChatBox/constants';
import ChatBox from '../ChatContainer/ChatBox';
import ChatItemContextProvider, { ChatItemContext } from '@/web/core/chat/context/chatItemContext';
import ChatRecordContextProvider from '@/web/core/chat/context/chatRecordContext';
import { streamFetch } from '@/web/common/api/fetch';
import type { StartChatFnProps } from '../ChatContainer/type';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import { getChatSourceKey, type ChatSourceTarget } from '@/web/core/chat/utils';
import { useChatStore } from '@/web/core/chat/context/useChatStore';
import { useRequest } from '@fastgpt/web/hooks/useRequest';
import { getInitChatInfo } from '@/web/core/chat/api';
import { useUserStore } from '@/web/support/user/useUserStore';
import { batchDeleteChatHistories } from '@/web/core/chat/history/api';
import type {
ChatAgentConfigFormDataType,
ChatAgentHelperMetadataType
} from '@fastgpt/global/core/ai/auxiliaryGeneration/type';
export type ChatAgentHelperRefType = {
restartChat: () => Promise<void>;
};
export type ChatAgentHelperProps = {
InputLeftComponent?: ReactNode;
ChatBoxRef: React.ForwardedRef<ChatAgentHelperRefType>;
appId: string;
} & {
type: typeof ChatAgentHelperTypeEnum.chatAgent;
metadata: ChatAgentHelperMetadataType;
onApply: (e: ChatAgentConfigFormDataType) => void;
};
const ChatAgentHelperChatBox = ({
type,
appId,
metadata,
onApply,
ChatBoxRef,
InputLeftComponent,
chatId,
onRestart,
sourceTarget
}: ChatAgentHelperProps & {
chatId: string;
onRestart: () => void;
sourceTarget: ChatSourceTarget;
}) => {
const sourceKey = useMemo(() => getChatSourceKey(sourceTarget), [sourceTarget]);
const setChatBoxData = useContextSelector(ChatItemContext, (v) => v.setChatBoxData);
const clearChatRecords = useContextSelector(ChatItemContext, (v) => v.clearChatRecords);
useEffect(() => {
setChatBoxData((prev) => {
const isSameChat = prev.sourceKey === sourceKey && prev.chatId === chatId;
return {
...prev,
sourceKey,
appId,
chatId,
chatGenerateStatus: isSameChat ? prev.chatGenerateStatus : undefined,
hasBeenRead: isSameChat ? prev.hasBeenRead : undefined,
app: createChatAgentHelperAppConfig()
};
});
}, [appId, chatId, setChatBoxData, sourceKey]);
useRequest(
async () => {
if (!appId || !chatId) return;
const res = await getInitChatInfo({
appId,
chatId,
sourceType: ChatSourceTypeEnum.chatAgentHelper
});
setChatBoxData((prev) => ({
...prev,
sourceKey,
appId,
chatId: res.chatId || chatId,
title: res.title,
chatGenerateStatus: res.chatGenerateStatus,
hasBeenRead: res.hasBeenRead
}));
},
{
manual: false,
refreshDeps: [appId, chatId, sourceKey]
}
);
const onStartChat = async ({
messages,
responseChatItemId,
interactive,
controller,
generatingMessage
}: StartChatFnProps) => {
if (!responseChatItemId) {
throw new Error('ChatAgentHelper response chat item id is empty');
}
const { responseText } = await streamFetch({
url: '/api/proApi/core/chat/chatAgentHelper/completions',
data: {
chatId,
responseChatItemId,
appId,
messages,
interactive,
metadata: {
type,
data: metadata
}
},
onMessage: (event) => {
if (
event.event === AuxiliaryGenerationEventEnum.chatAgentConfig &&
event.formData &&
type === ChatAgentHelperTypeEnum.chatAgent
) {
onApply(event.formData);
}
generatingMessage(event);
},
abortCtrl: controller
});
return { responseText };
};
useImperativeHandle(ChatBoxRef, () => ({
async restartChat() {
await batchDeleteChatHistories({
appId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
chatIds: [chatId]
});
clearChatRecords();
onRestart();
}
}));
return (
<ChatBox
isReady
sourceTarget={sourceTarget}
chatId={chatId}
chatType={ChatTypeEnum.test}
features={{
markRead: false,
mark: false,
voice: false,
tts: false,
inputGuide: false,
sandbox: false,
autoResume: true,
quickReplies: false,
disableFooterHoverTranslate: true
}}
InputLeftComponent={InputLeftComponent}
onStartChat={onStartChat}
/>
);
};
const ChatAgentHelper = (props: ChatAgentHelperProps) => {
const tmbId = useUserStore((state) => state.userInfo?.team?.tmbId ?? '');
const sourceTarget = useMemo<ChatSourceTarget>(
() => ({
sourceType: ChatSourceTypeEnum.chatAgentHelper,
sourceId: props.appId
}),
[props.appId]
);
const chatIdCacheKey = useMemo(() => {
if (!tmbId) return '';
return `${getChatSourceKey(sourceTarget)}:${tmbId}`;
}, [sourceTarget, tmbId]);
const chatId = useChatStore((state) => state.sourceChatIdMap[chatIdCacheKey] || '');
const ensureSourceChatId = useChatStore((state) => state.ensureSourceChatId);
const setSourceChatId = useChatStore((state) => state.setSourceChatId);
const chatRecordProviderParams = useMemo(
() => ({
chatId,
appId: props.appId,
sourceType: ChatSourceTypeEnum.chatAgentHelper as const
}),
[chatId, props.appId]
);
useEffect(() => {
if (chatIdCacheKey && !chatId) {
ensureSourceChatId(chatIdCacheKey);
}
}, [chatId, chatIdCacheKey, ensureSourceChatId]);
const onRestart = () => {
setSourceChatId(chatIdCacheKey);
};
return (
<ChatItemContextProvider
showRouteToDatasetDetail={false}
canDownloadSource={false}
isShowCite={false}
isShowFullText={false}
showRunningStatus={true}
showSkillReferences={false}
showWholeResponse={false}
showPoints={false}
showAvatar={true}
showSandboxAction={false}
>
{chatId && (
<ChatRecordContextProvider params={chatRecordProviderParams}>
<ChatAgentHelperChatBox
{...props}
chatId={chatId}
onRestart={onRestart}
sourceTarget={sourceTarget}
/>
</ChatRecordContextProvider>
)}
</ChatItemContextProvider>
);
};
export default React.memo(ChatAgentHelper);
......@@ -176,7 +176,8 @@ const ChatInput = ({
onSendMessage({
text: val.trim(),
files: fileList,
interactive: lastInteractive
interactive: lastInteractive,
clearInput: true
});
replaceFiles([]);
},
......@@ -579,7 +580,8 @@ const ChatInput = ({
onSendMessage({
text: text.trim(),
files: fileList,
autoTTSResponse
autoTTSResponse,
clearInput: true
});
replaceFiles([]);
}}
......
......@@ -11,13 +11,9 @@ import { useContextSelector } from 'use-context-selector';
import { ChatBoxContext } from '../../Provider';
import { ChatItemContext } from '@/web/core/chat/context/chatItemContext';
import AIChatLoading from '../AIChatLoading';
import { hasAiProcessingContent } from './utils';
import { hasAiProcessingContent, shouldShowNoOutputTip } from './utils';
import { useTranslation } from 'next-i18next';
import {
ChatGenerateStatusEnum,
ChatRoleEnum,
ChatStatusEnum
} from '@fastgpt/global/core/chat/constants';
import { ChatGenerateStatusEnum, ChatStatusEnum } from '@fastgpt/global/core/chat/constants';
const ResponseTags = dynamic(() => import('../ResponseTags'));
const WholeResponseModal = dynamic(() => import('../../../../components/WholeResponseModal'));
......@@ -85,15 +81,16 @@ const AIChatBubble = ({
!chat.errorMsg &&
!chat.errorText &&
!hasValidContent;
const showNoOutputTip =
chat.obj === ChatRoleEnum.AI &&
chat.status === ChatStatusEnum.finish &&
isLastValueGroup &&
!isChatting &&
!shouldWaitCurrentChatStatus &&
!chat.errorMsg &&
!chat.errorText &&
!hasValidContent;
const showNoOutputTip = shouldShowNoOutputTip({
obj: chat.obj,
status: chat.status,
isLastValueGroup,
isLastChild,
isChatting,
shouldWaitCurrentChatStatus,
hasError: !!chat.errorMsg || !!chat.errorText,
hasValidContent
});
const placeholderText = showLoadingPlaceholder
? t('chat:chat_loading_content')
: showNoOutputTip
......
import type { AIChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import { ChatRoleEnum, ChatStatusEnum } from '@fastgpt/global/core/chat/constants';
export const hasAiAnswerContent = (item: AIChatItemValueItemType) =>
Boolean(item.text?.content?.trim());
......@@ -11,11 +12,11 @@ export const hasAiProcessingContent = (item: AIChatItemValueItemType) => {
const tools = item.tools || (item.tool ? [item.tool] : undefined);
return Boolean(
(item.reasoning?.content && !item.hideReason) ||
(item.agentPlanUpdate?.reasoningText && !item.hideReason) ||
tools?.length ||
item.skills?.length ||
item.plan ||
item.planStatus?.status === 'generating'
(item.agentPlanUpdate?.reasoningText && !item.hideReason) ||
tools?.length ||
item.skills?.length ||
item.plan ||
item.planStatus?.status === 'generating'
);
};
......@@ -25,17 +26,15 @@ export const hasAiFoldableProcessingContent = (item: AIChatItemValueItemType) =>
const tools = item.tools || (item.tool ? [item.tool] : undefined);
return Boolean(
(item.reasoning?.content && !item.hideReason) ||
(item.agentPlanUpdate?.reasoningText && !item.hideReason) ||
tools?.length
(item.agentPlanUpdate?.reasoningText && !item.hideReason) ||
tools?.length
);
};
export const hasAiStandaloneProcessingContent = (item: AIChatItemValueItemType) => {
if (item.hideInUI) return false;
return Boolean(
item.skills?.length || item.plan || item.planStatus?.status === 'generating'
);
return Boolean(item.skills?.length || item.plan || item.planStatus?.status === 'generating');
};
export const shouldFilterAiValue = (item: AIChatItemValueItemType) => {
......@@ -56,3 +55,36 @@ export const shouldFilterAiValue = (item: AIChatItemValueItemType) => {
item.contextCheckpoint
);
};
/**
* 判断 AI 气泡是否需要展示“应用无输出内容”兜底提示。
*
* `isChatting` 是 ChatBox 级别的全局状态,历史消息在下一轮生成时也会收到 true。
* 因此只能在当前气泡自身是最后一条且正在生成时隐藏提示,避免历史空输出提示被后续发送误隐藏。
*/
export const shouldShowNoOutputTip = ({
obj,
status,
isLastValueGroup,
isLastChild,
isChatting,
shouldWaitCurrentChatStatus,
hasError,
hasValidContent
}: {
obj: `${ChatRoleEnum}`;
status: `${ChatStatusEnum}`;
isLastValueGroup: boolean;
isLastChild: boolean;
isChatting: boolean;
shouldWaitCurrentChatStatus: boolean;
hasError: boolean;
hasValidContent: boolean;
}) =>
obj === ChatRoleEnum.AI &&
status === ChatStatusEnum.finish &&
isLastValueGroup &&
!(isLastChild && isChatting) &&
!shouldWaitCurrentChatStatus &&
!hasError &&
!hasValidContent;
......@@ -22,6 +22,7 @@ import {
mergeNodeResponseDataByIdAndParent
} from '@fastgpt/global/core/chat/utils/mergeNode';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import { TeamErrEnum } from '@fastgpt/global/common/error/code/team';
import { useSystemStore } from '@/web/common/system/useSystemStore';
import { ChatItemContext } from '@/web/core/chat/context/chatItemContext';
......@@ -208,6 +209,21 @@ export const useChatGenerate = ({
responseData: appendNodeResponseByParent(item.responseData, nodeResponse)
};
}
if (event === AuxiliaryGenerationEventEnum.status && status) {
if (status === 'finish') {
return {
...item,
status: ChatStatusEnum.loading,
moduleName: name || item.moduleName
};
}
return {
...item,
status,
moduleName: name || item.moduleName
};
}
if (event === SseResponseEventEnum.flowNodeStatus && status) {
return {
...item,
......@@ -577,7 +593,7 @@ export const useChatGenerate = ({
interactive,
autoTTSResponse = false,
hideInUI = false,
clearInput = true
clearInput = false
}) => {
variablesForm.handleSubmit(
async ({ variables = {} }) => {
......@@ -721,6 +737,7 @@ export const useChatGenerate = ({
const { responseText } = await onStartChat({
messages,
responseChatItemId: responseChatId,
interactive,
controller: abortSignal,
generatingMessage: (e) => generatingMessage({ ...e, autoTTSResponse }),
variables: requestVariables
......
......@@ -25,7 +25,7 @@ const uniqueDataIds = (dataIds: Array<string | undefined>) =>
* - `retryInput` 会删除目标 human 消息及其后续记录,再用旧输入重新发送。
* - `editInput` 会删除目标 human 消息及其后续记录,再用编辑后的文本重新发送。
* - `delOneMessage` 会删除一条 human 消息,并顺带删除紧随其后的 AI 回复。
* - `onDelMessage` 是 hook 内部的删除通道,统一使用标准 `delChatRecordById` API,
* - `onDelMessages` 是 hook 内部的删除通道,统一使用标准 `delChatRecordById` API,
* 并自动带上当前 chat target、chatId 和外链鉴权信息。
*
* 设计边界:
......
......@@ -274,12 +274,48 @@ export const useChatResume = ({
}
if (resumeUnavailable) {
resumeFinalStatus = ChatGenerateStatusEnum.generating;
upsertResumeAiPlaceholder(
responseChatId,
getResumeUnavailablePlaceholderText(),
ChatStatusEnum.loading
);
resumeFinalStatus = ChatGenerateStatusEnum.done;
const resumeUnavailablePlaceholderText = getResumeUnavailablePlaceholderText();
setChatRecords((state) => {
const currentLastItem = state[state.length - 1];
if (
currentLastItem?.dataId !== responseChatId ||
currentLastItem.obj !== ChatRoleEnum.AI
) {
return state;
}
const next = state.map((item, index) => {
if (index !== state.length - 1) return item;
return {
...item,
status: ChatStatusEnum.finish,
time: new Date(),
responseData: mergeNodeResponseDataByIdAndParent(item.responseData || [])
};
});
const updatedLastItem = next[next.length - 1];
const hasOnlyResumeUnavailablePlaceholder =
!hasReceivedResumeOutput &&
updatedLastItem?.dataId === responseChatId &&
updatedLastItem.value.length === 1 &&
updatedLastItem.value[0]?.text?.content === resumeUnavailablePlaceholderText &&
!updatedLastItem.responseData?.length;
if (
updatedLastItem?.dataId === responseChatId &&
(!hasMeaningfulAiOutput(updatedLastItem as ChatSiteItemType) ||
hasOnlyResumeUnavailablePlaceholder) &&
!responseText
) {
return next.slice(0, -1);
}
return next;
});
scrollToBottom('auto');
return;
}
......
......@@ -191,7 +191,7 @@ export const useFileUpload = (props: UseFileUploadOptions) => {
// Get Upload Post Presigned URL
const { url, key, headers, maxSize, previewUrl } = await getUploadChatFilePresignedUrl({
filename: copyFile.rawFile.name,
filename: copyFile.rawFile!.name,
...chatAuthTarget,
chatId,
fileSelectConfig
......@@ -236,6 +236,8 @@ export const useFileUpload = (props: UseFileUploadOptions) => {
fileSelectConfig,
removeFiles,
replaceFiles,
sourceTarget.sourceId,
sourceTarget.sourceType,
t,
toast,
updateFiles
......
......@@ -513,8 +513,7 @@ const ChatBox = ({
sendPromptWithDisabledGuard({
text: trimmedText,
interactive: lastInteractive,
clearInput: false
interactive: lastInteractive
});
});
......
......@@ -36,7 +36,7 @@ export type ChatBoxInputType = {
files?: UserInputFileItemType[];
interactive?: WorkflowInteractiveResponseType;
hideInUI?: boolean;
/** 发送后是否清空输入框;快捷回复等不占用输入框内容的发送场景可设为 false。 */
/** 发送后是否清空输入框;默认不清空,只有主输入框确认发送等占用输入框内容的场景显式设为 true。 */
clearInput?: boolean;
};
......
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import type {
AIChatItemValueItemType,
ChatHistoryItemResType
......@@ -26,10 +27,11 @@ import type { ChatSiteItemType } from '../type';
*
* 该函数必须和 `generatingMessage` 支持的可见 SSE 事件保持同步。
*/
export const shouldCreateResumeAiPlaceholder = (event: SseResponseEventEnum) => {
return [
export const shouldCreateResumeAiPlaceholder = (event: string) => {
return new Set<string>([
SseResponseEventEnum.flowNodeResponse,
SseResponseEventEnum.flowNodeStatus,
AuxiliaryGenerationEventEnum.status,
SseResponseEventEnum.answer,
SseResponseEventEnum.fastAnswer,
SseResponseEventEnum.toolCall,
......@@ -39,7 +41,7 @@ export const shouldCreateResumeAiPlaceholder = (event: SseResponseEventEnum) =>
SseResponseEventEnum.plan,
SseResponseEventEnum.planStatus,
SseResponseEventEnum.workflowDuration
].includes(event);
]).has(event);
};
/**
......
......@@ -7,15 +7,13 @@ import type {
SkillModuleResponseItemType
} from '@fastgpt/global/core/chat/type';
import type { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import type {
UserInputInteractive,
WorkflowInteractiveResponseType
} from '@fastgpt/global/core/workflow/template/system/interactive/type';
import type { TopAgentFormDataType } from '@fastgpt/global/core/chat/helperBot/topAgent/type';
import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workflow/template/system/interactive/type';
import type { ChatAgentConfigFormDataType } from '@fastgpt/global/core/ai/auxiliaryGeneration/type';
import type { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import type { AgentPlanStatusType, AgentPlanType } from '@fastgpt/global/core/ai/agent/type';
export type generatingMessageProps = {
event: SseResponseEventEnum;
event: SseResponseEventEnum | AuxiliaryGenerationEventEnum;
responseValueId?: string;
text?: string;
......@@ -37,14 +35,13 @@ export type generatingMessageProps = {
sandboxStatus?: SandboxStatusItemType;
skill?: SkillModuleResponseItemType;
// HelperBot
collectionForm?: UserInputInteractive;
formData?: TopAgentFormDataType;
formData?: ChatAgentConfigFormDataType;
};
export type StartChatFnProps = {
messages: ChatCompletionMessageParam[];
responseChatItemId?: string;
interactive?: WorkflowInteractiveResponseType;
controller: AbortController;
variables: Record<string, any>;
generatingMessage: (e: generatingMessageProps) => void;
......
import type { FlexProps } from '@chakra-ui/react';
import { Box, Flex, Textarea, useBoolean } from '@chakra-ui/react';
import React, { useCallback, useMemo } from 'react';
import { useTranslation } from 'next-i18next';
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
import MyIcon from '@fastgpt/web/components/common/Icon';
import { type ChatBoxInputFormType } from '../ChatContainer/ChatBox/type';
import { ChatInputDefaultHeight, textareaMinH } from '../ChatContainer/ChatBox/constants';
import { useFieldArray, type UseFormReturn } from 'react-hook-form';
import { useContextSelector } from 'use-context-selector';
import { useSystem } from '@fastgpt/web/hooks/useSystem';
import { documentFileType } from '@fastgpt/global/common/file/constants';
import FilePreview from '../ChatContainer/components/FilePreview';
import { useFileUpload } from './hooks/useFileUpload';
import ComplianceTip from '@/components/common/ComplianceTip/index';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { HelperBotContext } from './context';
import type { onSendMessageFnType } from './type';
const fileTypeFilter = (file: File) => {
return (
file.type.includes('image') ||
documentFileType.split(',').some((type) => file.name.endsWith(type.trim()))
);
};
const ChatInput = ({
chatId,
onSendMessage,
onStop,
TextareaDom,
chatForm,
isChatting
}: {
chatId: string;
onSendMessage: onSendMessageFnType;
onStop: () => void;
TextareaDom: React.MutableRefObject<HTMLTextAreaElement | null>;
chatForm: UseFormReturn<ChatBoxInputFormType>;
isChatting: boolean;
}) => {
const { t } = useTranslation();
const { toast } = useToast();
const { isPc } = useSystem();
const { setValue, watch, control } = chatForm;
const inputValue = watch('input');
const type = useContextSelector(HelperBotContext, (v) => v.type);
const fileSelectConfig = useContextSelector(HelperBotContext, (v) => v.fileSelectConfig);
const [focusing, { on: onFocus, off: offFocus }] = useBoolean();
const fileCtrl = useFieldArray({
control,
name: 'files'
});
const {
File,
onOpenSelectFile,
fileList,
onSelectFile,
selectFileIcon,
selectFileLabel,
showSelectFile,
showSelectImg,
showSelectVideo,
showSelectAudio,
showSelectCustomFileExtension,
removeFiles,
replaceFiles,
hasFileUploading
} = useFileUpload({
fileSelectConfig,
fileCtrl,
type,
chatId
});
const havInput = !!inputValue || fileList.length > 0;
const canSendMessage = havInput && !hasFileUploading;
const canUploadFile =
showSelectFile ||
showSelectImg ||
showSelectVideo ||
showSelectAudio ||
showSelectCustomFileExtension;
const isDefaultInputHeight = !inputValue && fileList.length === 0;
/* on send */
const handleSend = useCallback(
async (val?: string) => {
if (!canSendMessage) return;
const textareaValue = val || TextareaDom.current?.value || '';
onSendMessage({
query: textareaValue.trim(),
files: fileList
});
replaceFiles([]);
},
[TextareaDom, canSendMessage, fileList, onSendMessage, replaceFiles]
);
const RenderTextarea = useMemo(
() => (
<Flex direction={'column'} mt={fileList.length > 0 ? 1 : 0}>
{/* Textarea */}
<Flex w={'100%'}>
{/* Prompt Container */}
<Textarea
ref={TextareaDom}
py={0}
mx={0}
px={0}
border={'none'}
borderRadius={0}
appearance={'none'}
_focusVisible={{
border: 'none'
}}
placeholder={
isPc ? t('common:core.chat.Type a message') : t('chat:input_placeholder_phone')
}
resize={'none'}
rows={1}
height={textareaMinH}
lineHeight={textareaMinH}
maxHeight={[24, 32]}
minH={textareaMinH}
mb={0}
maxLength={-1}
overflowY={'hidden'}
overflowX={'hidden'}
whiteSpace={'pre-wrap'}
wordBreak={'break-word'}
boxShadow={'none !important'}
color={'myGray.900'}
fontWeight={400}
fontSize={'16px'}
letterSpacing={'0.5px'}
w={'100%'}
_placeholder={{
color: 'myGray.400',
fontSize: 'inherit',
lineHeight: 'inherit',
letterSpacing: 'inherit'
}}
value={inputValue}
onChange={(e) => {
const textarea = e.target;
textarea.style.height = textareaMinH;
const maxHeight = 128;
const newHeight = Math.min(textarea.scrollHeight, maxHeight);
textarea.style.height = `${newHeight}px`;
// Only show scrollbar when content exceeds max height
if (textarea.scrollHeight > maxHeight) {
textarea.style.overflowY = 'auto';
} else {
textarea.style.overflowY = 'hidden';
}
setValue('input', textarea.value);
}}
onKeyDown={(e) => {
// enter send.(pc or iframe && enter and unPress shift)
const isEnter = e.key === 'Enter';
const textarea = e.currentTarget;
if (isEnter && (e.ctrlKey || e.altKey)) {
// Add a new line
const index = textarea.selectionStart;
const val = textarea.value;
textarea.value = `${val.slice(0, index)}\n${val.slice(index)}`;
textarea.selectionStart = index + 1;
textarea.selectionEnd = index + 1;
textarea.style.height = textareaMinH;
textarea.style.height = `${textarea.scrollHeight}px`;
return;
}
// Select all content
if (e.key === 'a' && e.ctrlKey) {
textarea.select();
}
if ((isPc || window !== parent) && e.keyCode === 13 && !e.shiftKey) {
handleSend(textarea.value);
e.preventDefault();
}
}}
onPaste={(e) => {
const clipboardData = e.clipboardData;
if (clipboardData && canUploadFile) {
const items = clipboardData.items;
const files = Array.from(items)
.map((item) => (item.kind === 'file' ? item.getAsFile() : undefined))
.filter((file) => {
return file && fileTypeFilter(file);
}) as File[];
onSelectFile({ files });
if (files.length > 0) {
e.preventDefault();
e.stopPropagation();
}
}
}}
onFocus={onFocus}
onBlur={offFocus}
/>
</Flex>
</Flex>
),
[
fileList.length,
TextareaDom,
isPc,
t,
inputValue,
onFocus,
offFocus,
setValue,
handleSend,
canUploadFile,
onSelectFile
]
);
const RenderButtonGroup = useMemo(() => {
const iconSize = {
w: '20px',
h: '20px'
};
return (
<Flex
alignItems={'flex-start'}
justifyContent={'space-between'}
w={'100%'}
mt={0}
h={9}
gap={[0, 1]}
>
<Flex alignItems={'center'} gap={2} flex={'1 1 0'} minW={0} w={0} />
{/* 右侧按钮组 */}
<Flex alignItems={'center'} gap={[0, 1]}>
{/* Attachment Group */}
<Flex alignItems={'center'} h={9}>
{/* file selector button */}
{canUploadFile && (
<Flex
alignItems={'center'}
justifyContent={'center'}
w={9}
h={9}
p={[1, 2]}
borderRadius={'sm'}
cursor={'pointer'}
_hover={{ bg: 'rgba(0, 0, 0, 0.04)' }}
onClick={(e) => {
e.stopPropagation();
onOpenSelectFile();
}}
>
<MyTooltip label={selectFileLabel}>
<MyIcon name={selectFileIcon as any} {...iconSize} color={'myGray.500'} />
</MyTooltip>
<File onSelect={(files) => onSelectFile({ files })} />
</Flex>
)}
</Flex>
{/* Divider Container */}
{canUploadFile && (
<Flex alignItems={'center'} justifyContent={'center'} w={2} h={4} mr={2}>
<Box w={'2px'} h={5} bg={'myGray.200'} />
</Flex>
)}
{/* Send Button Container */}
<Flex alignItems={'center'} w={9} h={9} borderRadius={'lg'}>
<Flex
alignItems={'center'}
justifyContent={'center'}
w={9}
h={9}
p={[1, 2]}
bg={
isChatting ? 'primary.50' : canSendMessage ? 'primary.500' : 'rgba(17, 24, 36, 0.1)'
}
borderRadius={['md', 'lg']}
cursor={isChatting ? 'pointer' : canSendMessage ? 'pointer' : 'not-allowed'}
onClick={(e) => {
e.stopPropagation();
if (isChatting) {
return onStop();
}
return handleSend();
}}
>
{isChatting ? (
<MyIcon {...iconSize} name={'stop'} color={'primary.600'} />
) : (
<MyTooltip label={t('common:core.chat.Send Message')}>
<MyIcon name={'core/chat/sendFill'} {...iconSize} color={'white'} />
</MyTooltip>
)}
</Flex>
</Flex>
</Flex>
</Flex>
);
}, [
canUploadFile,
selectFileLabel,
selectFileIcon,
File,
isChatting,
canSendMessage,
t,
onOpenSelectFile,
onSelectFile,
handleSend,
onStop
]);
const activeStyles: FlexProps = {
boxShadow: '0px 5px 20px -4px rgba(19, 51, 107, 0.13)',
border: '1px solid',
borderColor: 'myGray.250'
};
return (
<Box
w={'100%'}
maxW={['100%', '780px']}
mx={'auto'}
pb={['calc(16px + env(safe-area-inset-bottom))', 4]}
onDragOver={(e) => e.preventDefault()}
onDrop={(e) => {
e.preventDefault();
if (!canUploadFile) return;
const files = Array.from(e.dataTransfer.files);
const droppedFiles = files.filter((file) => fileTypeFilter(file));
if (droppedFiles.length > 0) {
onSelectFile({ files: droppedFiles });
}
const invalidFileName = files
.filter((file) => !fileTypeFilter(file))
.map((file) => file.name)
.join(', ');
if (invalidFileName) {
toast({
status: 'warning',
title: t('chat:unsupported_file_type'),
description: invalidFileName
});
}
}}
>
{/* Real Chat Input */}
<Flex
direction={'column'}
h={isDefaultInputHeight ? ChatInputDefaultHeight : undefined}
minH={ChatInputDefaultHeight}
p={4}
mb={0}
position={'relative'}
borderRadius={['xl', 'xxl']}
bg={'white'}
overflow={'display'}
{...(focusing
? activeStyles
: {
_hover: activeStyles,
border: '1px solid',
borderColor: 'myGray.200',
boxShadow: `0px 5px 16px -4px rgba(19, 51, 107, 0.08)`
})}
onClick={() => TextareaDom?.current?.focus()}
>
<Box flex={1}>
{/* file preview */}
<Box>
<FilePreview fileList={fileList} removeFiles={removeFiles} pt={0} />
</Box>
{RenderTextarea}
</Box>
<Box>{RenderButtonGroup}</Box>
</Flex>
<ComplianceTip type={'chat'} pt={4} pb={0} />
</Box>
);
};
export default React.memo(ChatInput);
import { POST, GET, DELETE } from '@/web/common/api/request';
import type {
GetHelperBotChatRecordsParamsType,
DeleteHelperBotChatParamsType,
GetHelperBotChatRecordsResponseType,
GetHelperBotFilePresignParamsType
} from '@fastgpt/global/openapi/core/chat/helperBot/api';
import type { CreatePostPresignedUrlResponseType } from '@fastgpt/global/common/file/s3/type';
export const getHelperBotChatRecords = (data: GetHelperBotChatRecordsParamsType) =>
GET<GetHelperBotChatRecordsResponseType>('/core/chat/helperBot/getRecords', data);
export const deleteHelperBotChatRecord = (data: DeleteHelperBotChatParamsType) =>
DELETE('/core/chat/helperBot/deleteRecord', data);
export const getHelperBotFilePresign = (data: GetHelperBotFilePresignParamsType) =>
POST<CreatePostPresignedUrlResponseType>('/core/chat/helperBot/getFilePresign', data);
import React, { useMemo } from 'react';
import type { HelperBotChatItemSiteType } from '@fastgpt/global/core/chat/helperBot/type';
import {
Box,
Accordion,
AccordionButton,
AccordionIcon,
AccordionItem,
AccordionPanel,
Flex,
HStack,
Button
} from '@chakra-ui/react';
import { useTranslation } from 'next-i18next';
import MyIcon from '@fastgpt/web/components/common/Icon';
import Markdown from '@/components/Markdown';
import type { UserInputInteractive } from '@fastgpt/global/core/workflow/template/system/interactive/type';
import { Controller, useForm } from 'react-hook-form';
import { nodeInputTypeToInputType } from '@/components/core/app/formRender/utils';
import FormLabel from '@fastgpt/web/components/common/MyBox/FormLabel';
import InputRender from '@/components/core/app/formRender';
import ChatAvatar from '@/components/core/chat/ChatContainer/ChatBox/components/ChatAvatar';
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
const accordionButtonStyle = {
w: 'auto',
bg: 'white',
borderRadius: 'md',
borderWidth: '1px',
borderColor: 'myGray.200',
boxShadow: '1',
pl: 3,
pr: 2.5,
_hover: {
bg: 'auto'
}
};
const RenderResoningContent = React.memo(function RenderResoningContent({
content,
isChatting,
isLastResponseValue
}: {
content: string;
isChatting: boolean;
isLastResponseValue: boolean;
}) {
const { t } = useTranslation();
const showAnimation = isChatting && isLastResponseValue;
return (
<Accordion allowToggle defaultIndex={isLastResponseValue ? 0 : undefined}>
<AccordionItem borderTop={'none'} borderBottom={'none'}>
<AccordionButton {...accordionButtonStyle} py={1}>
<HStack mr={2} spacing={1}>
<MyIcon name={'core/chat/think'} w={'0.85rem'} />
<Box fontSize={'sm'}>{t('chat:ai_reasoning')}</Box>
</HStack>
{showAnimation && <MyIcon name={'common/loading'} w={'0.85rem'} />}
<AccordionIcon color={'myGray.600'} ml={5} />
</AccordionButton>
<AccordionPanel
py={0}
pr={0}
pl={3}
mt={2}
borderLeft={'2px solid'}
borderColor={'myGray.300'}
color={'myGray.500'}
>
<Markdown source={content} showAnimation={showAnimation} />
</AccordionPanel>
</AccordionItem>
</Accordion>
);
});
const RenderText = React.memo(function RenderText({
showAnimation,
text
}: {
showAnimation: boolean;
text: string;
}) {
const source = useMemo(() => {
if (!text) return '';
// Remove quote references if not showing response detail
return text;
}, [text]);
return <Markdown source={source} showAnimation={showAnimation} />;
});
const RenderCollectionForm = React.memo(function RenderCollectionForm({
canSubmit,
collectionForm,
onSubmit,
showDescription = true
}: {
canSubmit: boolean;
collectionForm: UserInputInteractive;
onSubmit: (formData: string) => void;
showDescription?: boolean;
}) {
const { t } = useTranslation();
const { control, handleSubmit } = useForm();
const submitted = collectionForm.params.submitted;
return (
<Box>
{showDescription && <Box mb={3}>{collectionForm.params.description}</Box>}
<Flex flexDirection={'column'} gap={3}>
{collectionForm.params.inputForm.map((input) => {
const inputType = nodeInputTypeToInputType([input.type]);
return (
<Controller
key={input.key}
control={control}
name={input.key}
render={({ field: { onChange, value }, fieldState: { error } }) => {
return (
<Box>
<FormLabel whiteSpace={'pre-wrap'} mb={0.5}>
{input.label}
</FormLabel>
<InputRender
{...input}
inputType={inputType}
value={value}
onChange={onChange}
isDisabled={submitted}
isInvalid={!!error}
isRichText={false}
/>
</Box>
);
}}
/>
);
})}
</Flex>
{!submitted && canSubmit && (
<Flex justifyContent={'flex-end'} mt={4}>
<Button
size={'sm'}
onClick={handleSubmit((data) => {
// 需要把 label 作为 key
const dataByLabel = Object.fromEntries(
collectionForm.params.inputForm.map((input) => [input.label, data[input.key]])
);
onSubmit(JSON.stringify(dataByLabel));
})}
>
{t('common:Submit')}
</Button>
</Flex>
)}
</Box>
);
});
const AIItem = ({
chat,
isChatting,
isLastChild,
onSubmitCollectionForm
}: {
chat: HelperBotChatItemSiteType;
isChatting: boolean;
isLastChild: boolean;
onSubmitCollectionForm: (formData: string) => void;
}) => {
const { t } = useTranslation();
const firstValue = chat.value[0];
const isWaitingForResponse =
chat.value.length === 1 &&
!(
('text' in firstValue && firstValue.text?.content) ||
('reasoning' in firstValue && firstValue.reasoning && !firstValue.hideReason)
);
const lastCollectionFormIndex = chat.value.findLastIndex(
(value) => 'collectionForm' in value && value.collectionForm
);
return (
<Box
_hover={{
'& .controler': {
display: 'flex'
}
}}
>
<Flex alignItems={'flex-start'} justifyContent={'flex-start'} gap={2} w={'100%'}>
<ChatAvatar type={ChatRoleEnum.AI} src={'/imgs/bot.svg'} />
<Box
px={4}
py={3}
borderRadius={'sm'}
display="inline-block"
maxW={['calc(80% - 25px)', 'calc(80% - 40px)']}
color={'myGray.900'}
bg={'myGray.100'}
>
{isWaitingForResponse ? (
<RenderText showAnimation={true} text={t('chat:chat.waiting_for_response')} />
) : (
<>
{chat.value.map((value, i) => {
if ('planHint' in value) {
return (
<RenderText key={i} showAnimation={false} text={t('chat:plan_check_tip')} />
);
}
if ('collectionForm' in value && value.collectionForm) {
return (
<RenderCollectionForm
key={i}
canSubmit={isLastChild && i === lastCollectionFormIndex}
collectionForm={value.collectionForm}
onSubmit={onSubmitCollectionForm}
/>
);
}
return (
<React.Fragment key={i}>
{'reasoning' in value && value.reasoning && !value.hideReason && (
<RenderResoningContent
isChatting={isChatting}
isLastResponseValue={isLastChild}
content={value.reasoning.content}
/>
)}
{'text' in value && value.text && (
<RenderText
showAnimation={isChatting && isLastChild}
text={value.text.content}
/>
)}
</React.Fragment>
);
})}
</>
)}
</Box>
</Flex>
{/* Controller */}
<Flex h={'26px'} mt={1}>
{/* <Flex className="controler" display={['flex', 'none']} alignItems={'center'} gap={1}>
<MyTooltip label={t('common:Copy')}>
<MyIconButton
icon="copy"
color={'myGray.500'}
onClick={() => {
const text = chat.value
.map((value) => {
if ('text' in value) {
return value.text || '';
}
return '';
})
.join('');
return copyData(text ?? '');
}}
/>
</MyTooltip>
<MyTooltip label={t('common:Delete')}>
<MyIconButton
icon="delete"
color={'myGray.500'}
hoverColor={'red.600'}
hoverBg={'red.50'}
// onClick={() => copyData(text ?? '')}
/>
</MyTooltip>
</Flex> */}
</Flex>
</Box>
);
};
export default AIItem;
import React from 'react';
import { formatChatValue2InputType } from '../../ChatContainer/ChatBox/utils/chatValue';
import { Box, Flex } from '@chakra-ui/react';
import FileBlock from '../../ChatContainer/ChatBox/components/FilesBox';
import type { UserChatItemType } from '@fastgpt/global/core/chat/type';
import ChatAvatar from '@/components/core/chat/ChatContainer/ChatBox/components/ChatAvatar';
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { useUserStore } from '@/web/support/user/useUserStore';
import { getWebReqUrl } from '@fastgpt/web/common/system/utils';
const HumanItem = ({ chat }: { chat: UserChatItemType }) => {
const { text, files = [] } = formatChatValue2InputType(chat.value);
const userInfo = useUserStore((state) => state.userInfo);
const humanAvatar = userInfo?.avatar || getWebReqUrl('/imgs/botClosed.svg');
// TODO: delete chatitem
return (
<Flex
direction={'column'}
alignItems={'flex-end'}
_hover={{
'& .controler': {
display: 'flex'
}
}}
>
<Flex alignItems={'flex-start'} justifyContent={'flex-end'} gap={2} w={'100%'}>
<Box
px={4}
py={3}
borderRadius={'sm'}
display="inline-block"
maxW={['calc(80% - 25px)', 'calc(80% - 40px)']}
color={'myGray.900'}
bg={'primary.100'}
order={0}
>
<Flex flexDirection={'column'} gap={4}>
{files.length > 0 && <FileBlock files={files} />}
{text && (
<Box whiteSpace={'pre-wrap'} wordBreak={'break-word'}>
{text}
</Box>
)}
</Flex>
</Box>
<ChatAvatar type={ChatRoleEnum.Human} src={humanAvatar} />
</Flex>
{/* Controller */}
<Flex h={'26px'} mt={1}>
{/* <Flex className="controler" display={['flex', 'none']} alignItems={'center'} gap={1}>
<MyTooltip label={t('common:Copy')}>
<MyIconButton icon="copy" color={'myGray.500'} onClick={() => copyData(text ?? '')} />
</MyTooltip>
<MyTooltip label={t('common:Delete')}>
<MyIconButton
icon="delete"
color={'myGray.500'}
hoverColor={'red.600'}
hoverBg={'red.50'}
onClick={() => copyData(text ?? '')}
/>
</MyTooltip>
</Flex> */}
</Flex>
</Flex>
);
};
export default HumanItem;
import { useMemoEnhance } from '@fastgpt/web/hooks/useMemoEnhance';
import React, { type ReactNode } from 'react';
import { createContext } from 'use-context-selector';
import { HelperBotTypeEnum } from '@fastgpt/global/core/chat/helperBot/type';
import type {
TopAgentFormDataType,
TopAgentParamsType
} from '@fastgpt/global/core/chat/helperBot/topAgent/type';
import { type AppFileSelectConfigType } from '@fastgpt/global/core/app/type/config.schema';
export type HelperBotRefType = {
restartChat: () => void;
};
export type HelperBotProps = {
emptyDom?: ReactNode;
fileSelectConfig?: AppFileSelectConfigType;
ChatBoxRef: React.ForwardedRef<HelperBotRefType>;
} & {
type: typeof HelperBotTypeEnum.topAgent;
metadata: TopAgentParamsType;
onApply: (e: TopAgentFormDataType) => void;
};
type HelperBotContextType = HelperBotProps & {};
export const HelperBotContext = createContext<HelperBotContextType>({
type: HelperBotTypeEnum.topAgent,
ChatBoxRef: null,
metadata: {
role: '',
taskObject: '',
selectedTools: [],
selectedDatasets: [],
selectedAgentSkills: [],
fileUpload: false,
enableSandbox: false
},
onApply: function (e): void {
throw new Error('Function not implemented.');
}
});
const HelperBotContextProvider = ({
children,
...params
}: { children: ReactNode } & HelperBotProps) => {
const contextValue: HelperBotContextType = useMemoEnhance(() => params, [params]);
return <HelperBotContext.Provider value={contextValue}>{children}</HelperBotContext.Provider>;
};
export default HelperBotContextProvider;
import { useCallback, useMemo } from 'react';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { useTranslation } from 'next-i18next';
import { useSelectFile } from '@/web/common/file/hooks/useSelectFile';
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import { getNanoid } from '@fastgpt/global/common/string/tools';
import { getFileIcon } from '@fastgpt/global/common/file/icon';
import { formatFileSize } from '@fastgpt/global/common/file/tools';
import { clone } from 'lodash';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { type UseFieldArrayReturn } from 'react-hook-form';
import type { ChatBoxInputFormType, UserInputFileItemType } from '../../ChatContainer/ChatBox/type';
import { type AppFileSelectConfigType } from '@fastgpt/global/core/app/type/config.schema';
import { useSystemStore } from '@/web/common/system/useSystemStore';
import { getUploadFileType } from '@fastgpt/global/core/app/constants';
import type { HelperBotTypeEnumType } from '@fastgpt/global/core/chat/helperBot/type';
import { getHelperBotFilePresign } from '../api';
import { useRequest } from '@fastgpt/web/hooks/useRequest';
import { putFileToS3 } from '@fastgpt/web/common/file/utils';
import { getUploadChatFileType } from '../../ChatContainer/ChatBox/utils/file';
type UseFileUploadOptions = {
fileSelectConfig?: AppFileSelectConfigType;
fileCtrl: UseFieldArrayReturn<ChatBoxInputFormType, 'files', 'id'>;
type: HelperBotTypeEnumType;
chatId: string;
};
export const useFileUpload = (props: UseFileUploadOptions) => {
const { fileSelectConfig, fileCtrl, type, chatId } = props;
const { toast } = useToast();
const { t } = useTranslation();
const { feConfigs } = useSystemStore();
const {
update: updateFiles,
remove: removeFiles,
fields: fileList,
replace: replaceFiles,
append: appendFiles
} = fileCtrl;
const hasFileUploading = fileList.some((item) => !item.url);
const showSelectFile = fileSelectConfig?.canSelectFile;
const showSelectImg = fileSelectConfig?.canSelectImg;
const showSelectVideo = fileSelectConfig?.canSelectVideo;
const showSelectAudio = fileSelectConfig?.canSelectAudio;
const showSelectCustomFileExtension = fileSelectConfig?.canSelectCustomFileExtension;
const canUploadFile =
showSelectFile ||
showSelectImg ||
showSelectVideo ||
showSelectAudio ||
showSelectCustomFileExtension;
const maxSelectFiles = fileSelectConfig?.maxFiles ?? 10;
const maxSize = (feConfigs?.uploadFileMaxSize || 1024) * 1024 * 1024; // nkb
const canSelectFileAmount = maxSelectFiles - fileList.length;
const { icon: selectFileIcon, label: selectFileLabel } = useMemo(() => {
if (canUploadFile) {
return {
icon: 'core/chat/fileSelect',
label: t('chat:select_file')
};
}
return {};
}, [canUploadFile, t]);
const fileType = useMemo(() => {
return getUploadFileType({
canSelectFile: showSelectFile,
canSelectImg: showSelectImg,
canSelectVideo: showSelectVideo,
canSelectAudio: showSelectAudio,
canSelectCustomFileExtension: showSelectCustomFileExtension,
customFileExtensionList: fileSelectConfig?.customFileExtensionList
});
}, [
fileSelectConfig?.customFileExtensionList,
showSelectAudio,
showSelectCustomFileExtension,
showSelectFile,
showSelectImg,
showSelectVideo
]);
const { File, onOpen: onOpenSelectFile } = useSelectFile({
fileType,
multiple: true,
maxCount: canSelectFileAmount
});
const onSelectFile = useCallback(
async ({ files }: { files: File[] }) => {
if (!files || files.length === 0) {
return [];
}
// Filter max files
if (files.length > maxSelectFiles) {
files = files.slice(0, maxSelectFiles);
toast({
status: 'warning',
title: t('chat:file_amount_over', { max: maxSelectFiles })
});
}
// Filter files by max size
const filterFilesByMaxSize = files.filter((file) => file.size <= maxSize);
if (filterFilesByMaxSize.length < files.length) {
toast({
status: 'warning',
title: t('file:some_file_size_exceeds_limit', { maxSize: formatFileSize(maxSize) })
});
}
// Convert files to UserInputFileItemType
const loadFiles = await Promise.all(
filterFilesByMaxSize.map(
(file) =>
new Promise<UserInputFileItemType>((resolve, reject) => {
const chatFileType = getUploadChatFileType(file);
if (chatFileType === ChatFileTypeEnum.image) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
const item: UserInputFileItemType = {
id: getNanoid(6),
rawFile: file,
type: chatFileType,
name: file.name,
icon: reader.result as string,
status: 0
};
resolve(item);
};
reader.onerror = () => {
reject(reader.error);
};
} else {
resolve({
id: getNanoid(6),
rawFile: file,
type: chatFileType,
name: file.name,
icon: getFileIcon(file.name),
status: 0
});
}
})
)
);
appendFiles(loadFiles);
return loadFiles;
},
[maxSelectFiles, appendFiles, toast, t, maxSize]
);
const uploadFiles = useCallback(async () => {
const filterFiles = fileList.filter((item) => item.status === 0);
if (filterFiles.length === 0) return;
replaceFiles(fileList.map((item) => ({ ...item, status: 1 })));
const errorFileIndex: number[] = [];
await Promise.allSettled(
filterFiles.map(async (file) => {
const copyFile = clone(file);
copyFile.status = 1;
if (!copyFile.rawFile) return;
try {
const fileIndex = fileList.findIndex((item) => item.id === file.id)!;
// Get Upload Post Presigned URL
const { url, key, headers, maxSize, previewUrl } = await getHelperBotFilePresign({
type,
chatId,
filename: copyFile.rawFile.name
});
// Upload File to S3
await putFileToS3({
url,
file: copyFile.rawFile,
headers,
onUploadProgress: (e) => {
if (!e.total) return;
const percent = Math.round((e.loaded / e.total) * 100);
copyFile.process = percent;
updateFiles(fileIndex, copyFile);
},
t,
maxSize
});
// Update file url and key
copyFile.url = previewUrl;
copyFile.key = key;
updateFiles(fileIndex, copyFile);
} catch (error) {
errorFileIndex.push(fileList.findIndex((item) => item.id === file.id)!);
toast({
status: 'warning',
title: t(
getErrText(error, t('common:error.upload_file_error_filename', { name: file.name }))
)
});
}
})
);
removeFiles(errorFileIndex);
}, [chatId, fileList, removeFiles, replaceFiles, t, toast, type, updateFiles]);
const sortFileList = useMemo(() => {
// Sort: Document/audio/video, image
const sortResult = clone(fileList).sort((a, b) => {
return Number(a.type === ChatFileTypeEnum.image) - Number(b.type === ChatFileTypeEnum.image);
});
return sortResult;
}, [fileList]);
// Upload files
useRequest(uploadFiles, {
manual: false,
errorToast: t('common:upload_file_error'),
refreshDeps: [fileList, type, chatId]
});
return {
File,
onOpenSelectFile,
fileList: sortFileList,
onSelectFile,
uploadFiles,
selectFileIcon,
selectFileLabel,
showSelectFile,
showSelectImg,
showSelectVideo,
showSelectAudio,
showSelectCustomFileExtension,
removeFiles,
replaceFiles,
hasFileUploading
};
};
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
import HelperBotContextProvider, { type HelperBotProps } from './context';
import type { AIChatItemValueItemType } from '@fastgpt/global/core/chat/helperBot/type';
import { getNanoid } from '@fastgpt/global/common/string/tools';
import MyBox from '@fastgpt/web/components/common/MyBox';
import { ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { useScrollPagination } from '@fastgpt/web/hooks/useScrollPagination';
import { Box } from '@chakra-ui/react';
import HumanItem from './components/HumanItem';
import AIItem from './components/AIItem';
import { useMemoEnhance } from '@fastgpt/web/hooks/useMemoEnhance';
import { getHelperBotChatRecords } from './api';
import type {
GetHelperBotChatRecordsParamsType,
GetHelperBotChatRecordsResponseType
} from '@fastgpt/global/openapi/core/chat/helperBot/api';
import ChatInput from './Chatinput';
import type { ChatBoxInputFormType } from '../ChatContainer/ChatBox/type';
import { useForm } from 'react-hook-form';
import { useToast } from '@fastgpt/web/hooks/useToast';
import { useTranslation } from 'next-i18next';
import { useMemoizedFn } from 'ahooks';
import {
HelperBotTypeEnum,
type HelperBotChatItemSiteType
} from '@fastgpt/global/core/chat/helperBot/type';
import type { onSendMessageParamsType } from './type';
import { ChatInputWrapperStyle, textareaMinH } from '../ChatContainer/ChatBox/constants';
import { streamFetch } from '@/web/common/api/fetch';
import type { generatingMessageProps } from '../ChatContainer/type';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { useChatScroll } from '../ChatContainer/ChatBox/hooks/useChatScroll';
import ScrollToBottomButton from '../ChatContainer/ChatBox/components/ScrollToBottomButton';
const ChatBox = ({ type, metadata, onApply, ChatBoxRef, ...props }: HelperBotProps) => {
const { toast } = useToast();
const { t } = useTranslation();
const { ScrollContainerRef, scrollToBottom, generatingScroll, isScrollToBottomButtonVisible } =
useChatScroll();
// Messages 管理
const [chatId, setChatId] = useState<string>(getNanoid(12));
const [isChatting, setIsChatting] = useState(false);
const chatForm = useForm<ChatBoxInputFormType>({
defaultValues: {
input: '',
files: [],
chatStarted: false,
variables: {}
}
});
const { setValue } = chatForm;
const requestParams = useMemoEnhance(() => {
return {
chatId,
type
};
}, []);
const {
data: chatRecords,
setData: setChatRecords,
ScrollData
} = useScrollPagination(
async (
data: GetHelperBotChatRecordsParamsType
): Promise<GetHelperBotChatRecordsResponseType> => {
const res = await getHelperBotChatRecords(data);
// First load scroll to bottom
if (Number(data.offset) === 0) {
scrollToBottom('auto');
}
return {
...res,
list: res.list
};
},
{
pageSize: 10,
refreshDeps: [requestParams],
params: requestParams,
scrollLoadType: 'top',
showErrorToast: false
}
);
const chatController = useRef(new AbortController());
const abortRequest = useMemoizedFn((signal: string = 'stop') => {
chatController.current?.abort(signal);
});
const resetInputVal = useMemoizedFn(({ query = '', files = [] }: onSendMessageParamsType) => {
if (!TextareaDom.current) return;
setValue('files', files);
setValue('input', query);
sessionStorage.removeItem(`chatInput_${chatId}`);
setTimeout(() => {
/* 回到最小高度 */
if (TextareaDom.current) {
TextareaDom.current.style.height =
query === '' ? textareaMinH : `${TextareaDom.current.scrollHeight}px`;
}
}, 100);
});
// Text area
const TextareaDom = useRef<HTMLTextAreaElement>(null);
// Message request
const generatingMessage = useMemoizedFn(
({ event, text = '', reasoningText, collectionForm, formData }: generatingMessageProps) => {
setChatRecords((state) =>
state.map((item, index) => {
if (index !== state.length - 1) return item;
if (item.obj !== ChatRoleEnum.AI) return item;
const updateIndex = item.value.length - 1;
const updateValue: AIChatItemValueItemType = item.value[updateIndex];
// Special event: form data
if (event === SseResponseEventEnum.collectionForm && collectionForm) {
return {
...item,
value: item.value.concat({
collectionForm
})
};
}
if (event === SseResponseEventEnum.plan) {
return {
...item,
value: item.value.concat({
planHint: {
type: 'generation'
}
})
};
}
if (
event === SseResponseEventEnum.topAgentConfig &&
formData &&
type === HelperBotTypeEnum.topAgent
) {
onApply(formData);
return item;
}
if (event === SseResponseEventEnum.answer || event === SseResponseEventEnum.fastAnswer) {
const replaceUpdateValue = (nextValue: AIChatItemValueItemType) => ({
...item,
value: [
...item.value.slice(0, updateIndex),
nextValue,
...item.value.slice(updateIndex + 1)
]
});
if (reasoningText) {
if ('reasoning' in updateValue && updateValue.reasoning) {
return replaceUpdateValue({
...updateValue,
reasoning: {
...updateValue.reasoning,
content: updateValue.reasoning.content + reasoningText
}
});
} else if ('text' in updateValue && updateValue.text && !updateValue.text.content) {
return replaceUpdateValue({
...updateValue,
reasoning: {
content: reasoningText
}
});
} else {
const val: AIChatItemValueItemType = {
reasoning: {
content: reasoningText
}
};
return {
...item,
value: [...item.value, val]
};
}
}
if (text) {
if ('text' in updateValue && updateValue.text) {
return replaceUpdateValue({
...updateValue,
text: {
...updateValue.text,
content: updateValue.text.content + text
}
});
} else if ('reasoning' in updateValue && updateValue.reasoning) {
return replaceUpdateValue({
...updateValue,
text: {
content: text
}
});
} else {
const newValue: AIChatItemValueItemType = {
text: {
content: text
}
};
return {
...item,
value: item.value.concat(newValue)
};
}
}
}
return item;
})
);
generatingScroll(false);
}
);
const handleSendMessage = useMemoizedFn(
async ({ query = '', collectionFormData }: onSendMessageParamsType) => {
// Init check
if (isChatting) {
return toast({
title: t('chat:is_chatting'),
status: 'warning'
});
}
abortRequest();
query = query.trim();
const mergeQuery = query || collectionFormData;
if (!mergeQuery) {
toast({
title: t('chat:content_empty'),
status: 'warning'
});
return;
}
const chatItemDataId = getNanoid(24);
const newChatList: HelperBotChatItemSiteType[] = [
...chatRecords,
// 用户消息
{
_id: getNanoid(24),
createTime: new Date(),
dataId: chatItemDataId,
obj: ChatRoleEnum.Human,
value: [
{
text: {
content: mergeQuery
}
}
]
},
// AI 消息 - 空白,用于接收流式输出
{
_id: getNanoid(24),
createTime: new Date(),
dataId: chatItemDataId,
obj: ChatRoleEnum.AI,
value: [
{
text: {
content: ''
}
}
]
}
];
setChatRecords(newChatList);
resetInputVal({});
scrollToBottom();
setIsChatting(true);
try {
const abortSignal = new AbortController();
chatController.current = abortSignal;
const response = await streamFetch({
url: '/api/proApi/core/chat/helperBot/completions',
data: {
chatId,
chatItemId: chatItemDataId,
query: mergeQuery,
files: chatForm.getValues('files').map((item) => ({
type: item.type,
key: item.key,
// url: item.url,
name: item.name
})),
metadata: {
type: type,
data: metadata
}
},
onMessage: generatingMessage,
abortCtrl: abortSignal
});
} catch (error) {
setChatRecords((state) =>
state.map((item, index) => {
if (index !== state.length - 1 || item.obj !== ChatRoleEnum.AI) return item;
return {
...item,
value: [
...item.value,
{
text: {
content: `Error: ${getErrText(error)}`
}
}
]
};
})
);
}
setIsChatting(false);
}
);
useImperativeHandle(ChatBoxRef, () => ({
restartChat() {
abortRequest();
setChatRecords([]);
setChatId(getNanoid(12));
}
}));
useEffect(() => {
return () => {
abortRequest();
};
}, []);
return (
<MyBox display={'flex'} flexDirection={'column'} h={'100%'} position={'relative'}>
<ScrollData
ScrollContainerRef={ScrollContainerRef}
flex={'1 0 0'}
h={0}
w={'100%'}
overflow={'overlay'}
px={[4, 0]}
pb={3}
>
{chatRecords.map((item, index) => (
<Box
key={item._id}
px={[3, 5]}
w={'100%'}
maxW={['auto', 'min(1000px, 100%)']}
mx="auto"
_notLast={{
mb: 2
}}
>
{item.obj === ChatRoleEnum.Human && <HumanItem chat={item} />}
{item.obj === ChatRoleEnum.AI && (
<AIItem
chat={item}
isChatting={isChatting}
isLastChild={index === chatRecords.length - 1}
onSubmitCollectionForm={(data) => handleSendMessage({ collectionFormData: data })}
/>
)}
</Box>
))}
</ScrollData>
<Box {...ChatInputWrapperStyle}>
<Box position="relative">
<ScrollToBottomButton
isVisible={isScrollToBottomButtonVisible}
onClick={() => scrollToBottom('smooth')}
/>
<ChatInput
TextareaDom={TextareaDom}
chatId={chatId}
chatForm={chatForm}
isChatting={isChatting}
onSendMessage={handleSendMessage}
onStop={() => {}}
/>
</Box>
</Box>
</MyBox>
);
};
const index = (props: HelperBotProps) => {
return (
<HelperBotContextProvider {...props}>
<ChatBox {...props} />
</HelperBotContextProvider>
);
};
export default index;
import type { UserInputFileItemType } from '../ChatContainer/ChatBox/type';
export type onSendMessageParamsType = {
query?: string;
collectionFormData?: string;
files?: UserInputFileItemType[];
};
export type onSendMessageFnType = (e: onSendMessageParamsType) => Promise<any>;
......@@ -74,35 +74,21 @@ export const ChoiceCollapseToggleButton = React.memo(function ChoiceCollapseTogg
display={'inline-flex'}
alignItems={'center'}
alignSelf={'flex-start'}
position={'relative'}
mt={mt}
mx={'8px'}
px={0}
h={'16px'}
p={0}
minW={0}
border={0}
bg={'transparent'}
color={'primary.600'}
cursor={'pointer'}
fontSize={'11px'}
fontWeight={500}
lineHeight={'16px'}
_before={{
content: '""',
position: 'absolute',
inset: '-6px -8px',
borderRadius: '6px',
bg: 'transparent',
transition: 'background-color 150ms ease'
}}
_hover={{
_before: {
bg: 'myGray.100'
}
cursor: 'pointer'
}}
onClick={onToggle}
>
<Box as="span" position={'relative'} zIndex={1}>
<Box as="span">
{isOptionsExpanded
? t('chat:interactive.user_select.collapse_options')
: t('chat:interactive.user_select.expand_options')}
......
......@@ -151,10 +151,23 @@ export const FormInputComponent = React.memo(function FormInputComponent({
render={({ field: { onChange, value }, fieldState: { error } }) => {
return (
<FormControl isInvalid={!!error}>
<Flex alignItems={'center'} mb={1}>
{input.required && <Box color={'red.500'}>*</Box>}
<FormLabel>{input.label}</FormLabel>
{input.description && <QuestionTip ml={1} label={input.description} />}
<Flex alignItems={'flex-start'} mb={1} minW={0}>
{input.required && (
<Box color={'red.500'} flexShrink={0}>
*
</Box>
)}
<FormLabel
minW={0}
flexShrink={1}
whiteSpace={'pre-wrap'}
wordBreak={'break-word'}
>
{input.label}
</FormLabel>
{input.description && (
<QuestionTip flexShrink={0} ml={1} mt={'2px'} label={input.description} />
)}
</Flex>
<InputRender
{...input}
......
......@@ -4,25 +4,24 @@ import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import MyTooltip from '@fastgpt/web/components/common/MyTooltip';
import MyIcon from '@fastgpt/web/components/common/Icon';
import { useSafeState } from 'ahooks';
import { useLocalStorageState, useSafeState } from 'ahooks';
import type { AppFormEditFormType } from '@fastgpt/global/core/app/formEdit/type';
import { useContextSelector } from 'use-context-selector';
import { AppContext } from '../../context';
import { useChatTest } from '../../useChatTest';
import ChatItemContextProvider, { ChatItemContext } from '@/web/core/chat/context/chatItemContext';
import ChatRecordContextProvider from '@/web/core/chat/context/chatRecordContext';
import { useChatStore } from '@/web/core/chat/context/useChatStore';
import { AgentChatTestTabEnum, useChatStore } from '@/web/core/chat/context/useChatStore';
import MyBox from '@fastgpt/web/components/common/MyBox';
import { cardStyles } from '../../constants';
import ChatQuoteList from '@/pageComponents/chat/ChatQuoteList';
import { ChatTypeEnum } from '@/components/core/chat/ChatContainer/ChatBox/constants';
import type { Form2WorkflowFnType } from '../FormComponent/type';
import FillRowTabs from '@fastgpt/web/components/common/Tabs/FillRowTabs';
import HelperBot from '@/components/core/chat/HelperBot';
import type { HelperBotRefType } from '@/components/core/chat/HelperBot/context';
import { HelperBotTypeEnum } from '@fastgpt/global/core/chat/helperBot/type';
import { loadGeneratedTools } from './utils';
import { checkAgentSkillSandboxUnavailable } from './utils';
import ChatAgentHelper from '@/components/core/chat/ChatAgentHelper';
import type { ChatAgentHelperRefType } from '@/components/core/chat/ChatAgentHelper';
import { ChatAgentHelperTypeEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import { checkAgentSkillSandboxUnavailable, loadGeneratedTools } from './utils';
import { systemSubInfo } from '@fastgpt/global/core/workflow/node/agent/constants';
import { useSandboxEditor, useSandboxStatus } from '@/pageComponents/chat/SandboxEditor/hook';
import { useSystemStore } from '@/web/common/system/useSystemStore';
......@@ -30,6 +29,8 @@ import { useUserStore } from '@/web/support/user/useUserStore';
import { useToast } from '@fastgpt/web/hooks/useToast';
import ChatVariableButton from '@/pageComponents/chat/ChatWindow/ChatVariableButton';
import ProModal from '@/components/ProTip/ProModal';
import ChatAIModelSelector from '@/pageComponents/chat/ChatWindow/ChatAIModelSelector';
import { getErrText } from '@fastgpt/global/common/error/utils';
type Props = {
appForm: AppFormEditFormType;
......@@ -40,8 +41,8 @@ type Props = {
const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props) => {
const { t } = useTranslation();
const { toast } = useToast();
const { chatId } = useChatStore();
const { feConfigs } = useSystemStore();
const { chatId, agentChatTestTab, setAgentChatTestTab } = useChatStore();
const { feConfigs, llmModelList, defaultModels } = useSystemStore();
const { teamPlanStatus } = useUserStore();
const enableSandbox = !teamPlanStatus?.standard || !!teamPlanStatus?.standard?.enableSandbox;
const showSandbox = feConfigs.show_agent_sandbox;
......@@ -51,9 +52,17 @@ const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props
enableSandbox
});
const [activeTab, setActiveTab] = useSafeState<'helper' | 'chat_debug'>('chat_debug');
const canUseHelper = !!feConfigs?.isPlus;
const activeTab = canUseHelper ? agentChatTestTab : AgentChatTestTabEnum.chatDebug;
const [hasRenderedHelper, setHasRenderedHelper] = useSafeState(false);
const [proModalOpen, setProModalOpen] = useSafeState(false);
const HelperBotRef = useRef<HelperBotRefType>(null);
const [helperSelectedModel, setHelperSelectedModel] = useLocalStorageState<string>(
'chat_agent_helper_model',
{
defaultValue: defaultModels.llm?.model
}
);
const ChatAgentHelperRef = useRef<ChatAgentHelperRefType>(null);
const { appDetail } = useContextSelector(AppContext, (v) => v);
const datasetCiteData = useContextSelector(ChatItemContext, (v) => v.datasetCiteData);
......@@ -64,6 +73,48 @@ const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props
edges: appDetail.edges || []
});
const modelSelectList = useMemo(
() => llmModelList.map((item) => ({ label: item.name, value: item.model })),
[llmModelList]
);
const helperModel = useMemo(() => {
const modelSet = new Set(llmModelList.map((item) => item.model));
const defaultModel = defaultModels.llm?.model || llmModelList[0]?.model || '';
if (helperSelectedModel && modelSet.has(helperSelectedModel)) {
return helperSelectedModel;
}
if (defaultModel && modelSet.has(defaultModel)) {
return defaultModel;
}
return llmModelList[0]?.model || '';
}, [defaultModels.llm?.model, helperSelectedModel, llmModelList]);
const onChangeHelperModel = useCallback(
(model: string) => {
setHelperSelectedModel(model);
},
[setHelperSelectedModel]
);
const HelperModelSelectorInput = useMemo(
() => (
<Box w={'fit-content'} maxW={'300px'} flex={'0 1 auto'} minW={0}>
<ChatAIModelSelector
h={'36px'}
w={'fit-content'}
maxW={'300px'}
boxShadow={'none'}
size={'sm'}
bg={'myGray.50'}
rounded={'10px'}
value={helperModel}
list={modelSelectList}
onChange={onChangeHelperModel}
/>
</Box>
),
[helperModel, modelSelectList, onChangeHelperModel]
);
// Sandbox: Status Hook 负责网络同步,UI Hook 负责弹窗渲染
const { SandboxEntryIcon } = useSandboxStatus({
appId: appDetail._id,
......@@ -84,17 +135,32 @@ const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props
}, [datasetCiteData, setRenderEdit]);
useEffect(() => {
setActiveTab('chat_debug');
setCiteModalData(undefined);
setRenderEdit(true);
}, [appDetail._id, chatId, setActiveTab, setCiteModalData, setRenderEdit]);
}, [appDetail._id, chatId, setCiteModalData, setRenderEdit]);
const updateActiveTab = useCallback(
(value: AgentChatTestTabEnum) => {
if (value === AgentChatTestTabEnum.helper) {
setHasRenderedHelper(true);
}
setAgentChatTestTab(value);
},
[setAgentChatTestTab, setHasRenderedHelper]
);
useEffect(() => {
if (activeTab === AgentChatTestTabEnum.helper) {
setHasRenderedHelper(true);
}
}, [activeTab, setHasRenderedHelper]);
const { ChatContainer, restartChat } = useChatTest({
...workflowData,
chatConfig: appForm.chatConfig,
isReady: !isAgentSkillSandboxUnavailable
});
const onRestartChat = useCallback(() => {
const onRestartChat = useCallback(async () => {
if (isAgentSkillSandboxUnavailable) {
toast({
status: 'warning',
......@@ -102,15 +168,22 @@ const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props
});
return;
}
if (activeTab === 'helper') {
HelperBotRef.current?.restartChat();
if (activeTab === AgentChatTestTabEnum.helper) {
try {
await ChatAgentHelperRef.current?.restartChat();
} catch (error) {
toast({
status: 'warning',
title: getErrText(error, t('common:core.chat.error.Chat error'))
});
}
} else {
restartChat();
}
}, [activeTab, isAgentSkillSandboxUnavailable, restartChat, t, toast]);
// 构建 TopAgent metadata,从 appForm 中提取配置
const topAgentMetadata = useMemo(
// 构建 ChatAgentHelper metadata,从 appForm 中提取配置。
const chatAgentHelperMetadata = useMemo(
() => ({
systemPrompt: appForm.aiSettings.systemPrompt,
selectedTools: appForm.selectedTools.map((tool) => tool.id),
......@@ -119,13 +192,10 @@ const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props
fileUpload: appForm.chatConfig.fileSelectConfig?.canSelectFile || false,
enableSandbox: appForm.aiSettings.useAgentSandbox || false,
modelConfig: {
model: appForm.aiSettings.model,
temperature: appForm.aiSettings.temperature,
maxToken: appForm.aiSettings.maxToken,
stream: true
model: helperModel
}
}),
[appForm]
[appForm, helperModel]
);
return (
......@@ -143,32 +213,34 @@ const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props
boxShadow={'3'}
>
<Flex px={[2, 5]} pb={2} alignItems={'center'}>
<FillRowTabs<'helper' | 'chat_debug'>
<FillRowTabs<AgentChatTestTabEnum>
py={1}
list={[
{
label: t('app:helper_bot'),
value: 'helper'
value: AgentChatTestTabEnum.helper
},
{
label: t('app:chat_debug'),
value: 'chat_debug'
value: AgentChatTestTabEnum.chatDebug
}
]}
value={activeTab}
onChange={(value) => {
if (value === 'helper' && !feConfigs?.isPlus) {
if (value === AgentChatTestTabEnum.helper && !canUseHelper) {
setProModalOpen(true);
return;
}
setActiveTab(value);
updateActiveTab(value);
}}
/>
<Box flex={1} />
<Flex alignItems={'center'} gap={2}>
<SandboxEntryIcon size={'smSquare'} onOpen={onOpenSandboxModal} />
{activeTab === 'chat_debug' && <ChatVariableButton chatType={ChatTypeEnum.test} />}
{activeTab === AgentChatTestTabEnum.chatDebug && (
<ChatVariableButton chatType={ChatTypeEnum.test} />
)}
<MyTooltip label={t('common:core.chat.Restart')}>
<IconButton
className="chat"
......@@ -185,70 +257,71 @@ const ChatTest = ({ appForm, setAppForm, setRenderEdit, form2WorkflowFn }: Props
</MyTooltip>
</Flex>
</Flex>
<Box flex={1}>
{activeTab === 'helper' && (
<HelperBot
ChatBoxRef={HelperBotRef}
type={HelperBotTypeEnum.topAgent}
metadata={topAgentMetadata}
onApply={async (formData) => {
const fileUploadEnabled = !!formData.fileUploadEnabled;
const enableSandboxEnabled = !!formData.enableSandboxEnabled;
<Box flex={1} minH={0}>
{hasRenderedHelper && (
<Box h={'100%'} display={activeTab === AgentChatTestTabEnum.helper ? 'block' : 'none'}>
<ChatAgentHelper
ChatBoxRef={ChatAgentHelperRef}
appId={appDetail._id}
type={ChatAgentHelperTypeEnum.chatAgent}
metadata={chatAgentHelperMetadata}
InputLeftComponent={HelperModelSelectorInput}
onApply={async (formData) => {
// Filter internal tools
const filteredToolIds = (formData.tools || []).filter(
(toolId) => !(toolId in systemSubInfo)
);
// Filter internal tools
const filteredToolIds = (formData.tools || []).filter(
(toolId) => !(toolId in systemSubInfo)
);
const newTools = await loadGeneratedTools({
newToolIds: filteredToolIds,
existsTools: appForm.selectedTools,
fileSelectConfig: appForm.chatConfig.fileSelectConfig
});
const newTools = await loadGeneratedTools({
newToolIds: filteredToolIds,
existsTools: appForm.selectedTools,
fileSelectConfig: appForm.chatConfig.fileSelectConfig
});
setAppForm((prev) => {
const fileUploadEnabled = !!formData.fileUploadEnabled;
const enableSandboxEnabled = !!formData.enableSandboxEnabled;
setAppForm((prev) => {
const newForm: AppFormEditFormType = {
...prev,
selectedTools: [...newTools],
selectedAgentSkills: formData.selectedAgentSkills || [],
dataset:
formData.datasets && formData.datasets.length > 0
? {
...prev.dataset,
datasets: formData.datasets
}
: prev.dataset,
aiSettings: {
...prev.aiSettings,
systemPrompt: formData.systemPrompt || prev.aiSettings.systemPrompt,
useAgentSandbox:
enableSandboxEnabled || (formData.selectedAgentSkills?.length || 0) > 0
},
chatConfig: {
...prev.chatConfig,
fileSelectConfig: fileUploadEnabled
? {
...prev.chatConfig.fileSelectConfig,
canSelectFile: true
}
: {
maxFiles: undefined,
canSelectFile: false,
customPdfParse: false,
canSelectImg: false,
canSelectVideo: false,
canSelectAudio: false,
canSelectCustomFileExtension: false,
customFileExtensionList: []
}
}
};
return newForm;
});
}}
/>
const newForm: AppFormEditFormType = {
...prev,
selectedTools: [...newTools],
selectedAgentSkills: formData.selectedAgentSkills || [],
dataset: {
...prev.dataset,
datasets: formData.datasets ?? []
},
aiSettings: {
...prev.aiSettings,
systemPrompt: formData.systemPrompt || prev.aiSettings.systemPrompt,
useAgentSandbox:
enableSandboxEnabled || (formData.selectedAgentSkills?.length || 0) > 0
},
chatConfig: {
...prev.chatConfig,
fileSelectConfig: fileUploadEnabled
? {
...prev.chatConfig.fileSelectConfig,
canSelectFile: true
}
: {
maxFiles: undefined,
canSelectFile: false,
customPdfParse: false,
canSelectImg: false,
canSelectVideo: false,
canSelectAudio: false,
canSelectCustomFileExtension: false,
customFileExtensionList: []
}
}
};
return newForm;
});
}}
/>
</Box>
)}
{activeTab === 'chat_debug' && <ChatContainer />}
{activeTab === AgentChatTestTabEnum.chatDebug && <ChatContainer />}
</Box>
</MyBox>
{datasetCiteData && (
......
......@@ -431,12 +431,12 @@ export const getEmptyAgentConfig = (t: any) => {
export const loadGeneratedTools = async ({
newToolIds,
existsTools = [],
topAgentSelectedTools = [],
generatedSelectedTools = [],
fileSelectConfig
}: {
newToolIds: string[]; // 新的,完整的 toolId
existsTools?: SelectedToolItemType[];
topAgentSelectedTools?: SelectedToolItemType[];
generatedSelectedTools?: SelectedToolItemType[];
fileSelectConfig?: AppFileSelectConfigType;
}): Promise<SelectedToolItemType[]> => {
const results = (
......@@ -465,12 +465,12 @@ export const loadGeneratedTools = async ({
return;
}
const topTool = topAgentSelectedTools.find((item) => item.pluginId === toolId);
if (topTool) {
const generatedTool = generatedSelectedTools.find((item) => item.pluginId === toolId);
if (generatedTool) {
tool.inputs.forEach((input) => {
const topInput = topTool.inputs.find((topIn) => topIn.key === input.key);
if (topInput) {
input.value = topInput.value;
const generatedInput = generatedTool.inputs.find((topIn) => topIn.key === input.key);
if (generatedInput) {
input.value = generatedInput.value;
}
});
}
......
......@@ -22,7 +22,7 @@ import { isDebugToolSource } from '@fastgpt/global/core/app/tool/utils';
import DebugToolTag from '@fastgpt/web/components/core/plugin/tool/DebugToolTag';
const ToolSelect = ({
topAgentSelectedTools,
generatedSelectedTools,
selectedModel,
selectedTools = [],
fileSelectConfig = {},
......@@ -30,7 +30,7 @@ const ToolSelect = ({
onUpdateTool,
onRemoveTool
}: {
topAgentSelectedTools?: SelectedToolItemType[];
generatedSelectedTools?: SelectedToolItemType[];
selectedModel: LLMModelItemType;
selectedTools?: SelectedToolItemType[];
fileSelectConfig?: AppFileSelectConfigType;
......@@ -176,7 +176,7 @@ const ToolSelect = ({
{isOpenToolsSelect && (
<ToolSelectModal
topAgentSelectedTools={topAgentSelectedTools}
generatedSelectedTools={generatedSelectedTools}
selectedTools={selectedTools}
fileSelectConfig={fileSelectConfig}
selectedModel={selectedModel}
......
......@@ -48,7 +48,7 @@ import { isDebugToolSource } from '@fastgpt/global/core/app/tool/utils';
import DebugToolTag from '@fastgpt/web/components/core/plugin/tool/DebugToolTag';
type Props = {
topAgentSelectedTools?: SelectedToolItemType[];
generatedSelectedTools?: SelectedToolItemType[];
selectedTools: FlowNodeTemplateType[];
fileSelectConfig: AppFormEditFormType['chatConfig']['fileSelectConfig'];
selectedModel: LLMModelItemType;
......@@ -271,7 +271,7 @@ const ToolSelectModal = ({ onClose, ...props }: Props & { onClose: () => void })
export default React.memo(ToolSelectModal);
const RenderList = React.memo(function RenderList({
topAgentSelectedTools = [],
generatedSelectedTools = [],
templates,
type,
parentId,
......@@ -331,12 +331,14 @@ const RenderList = React.memo(function RenderList({
}
// 添加与 top 相同工具的配置
const topTool = topAgentSelectedTools.find((tool) => tool.pluginId === res.pluginId);
if (topTool) {
const generatedTool = generatedSelectedTools.find((tool) => tool.pluginId === res.pluginId);
if (generatedTool) {
res.inputs.forEach((input) => {
const topInput = topTool.inputs.find((topInput) => topInput.key === input.key);
if (topInput) {
input.value = topInput.value;
const generatedInput = generatedTool.inputs.find(
(generatedInput) => generatedInput.key === input.key
);
if (generatedInput) {
input.value = generatedInput.value;
}
});
}
......
......@@ -25,11 +25,11 @@ import { getInitChatInfo } from '@/web/core/chat/api';
const fileSelectConfig: AppFileSelectConfigType = {
maxFiles: 10,
canSelectFile: false,
canSelectImg: false,
canSelectFile: true,
canSelectImg: true,
customPdfParse: false,
canSelectVideo: false,
canSelectAudio: false,
canSelectVideo: true,
canSelectAudio: true,
canSelectCustomFileExtension: false,
customFileExtensionList: []
};
......
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import {
DeleteHelperBotChatParamsSchema,
type DeleteHelperBotChatParamsType
} from '@fastgpt/global/openapi/core/chat/helperBot/api';
import { authHelperBotChatCrud } from '@/service/support/permission/auth/chat';
import { MongoHelperBotChatItem } from '@fastgpt/service/core/chat/HelperBot/chatItemSchema';
import { parseApiInput } from '@fastgpt/service/common/zod/requestParseError';
export type deleteRecordQuery = DeleteHelperBotChatParamsType;
export type deleteRecordBody = Record<string, never>;
export type deleteRecordResponse = Record<string, never>;
async function handler(
req: ApiRequestProps<deleteRecordBody, deleteRecordQuery>
): Promise<deleteRecordResponse> {
const { type, chatId, chatItemId } = parseApiInput({
req,
querySchema: DeleteHelperBotChatParamsSchema
}).query;
const { userId } = await authHelperBotChatCrud({
type,
chatId,
req,
authToken: true
});
await MongoHelperBotChatItem.deleteMany({ userId, chatId, chatItemId });
return {};
}
export default NextAPI(handler);
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import {
GetHelperBotFilePresignParamsSchema,
type GetHelperBotFilePresignParamsType
} from '@fastgpt/global/openapi/core/chat/helperBot/api';
import type { CreatePostPresignedUrlResponseType } from '@fastgpt/global/common/file/s3/type';
import { authHelperBotChatCrud } from '@/service/support/permission/auth/chat';
import { getS3HelperBotSource } from '../../../../../../../../packages/service/common/s3/sources/helperbot/index';
import { authFrequencyLimit } from '@fastgpt/service/common/system/frequencyLimit/utils';
import { addSeconds } from 'date-fns';
import { parseApiInput } from '@fastgpt/service/common/zod/requestParseError';
export type getFilePresignQuery = Record<string, never>;
export type getFilePresignBody = GetHelperBotFilePresignParamsType;
export type getFilePresignResponse = CreatePostPresignedUrlResponseType;
const authUploadLimit = (tmbId: string) => {
if (!global.feConfigs.uploadFileMaxAmount) return;
return authFrequencyLimit({
eventId: `${tmbId}-uploadfile`,
maxAmount: global.feConfigs.uploadFileMaxAmount * 2,
expiredTime: addSeconds(new Date(), 30) // 30s
});
};
async function handler(
req: ApiRequestProps<getFilePresignBody, getFilePresignQuery>
): Promise<getFilePresignResponse> {
const { type, chatId, filename } = parseApiInput({
req,
bodySchema: GetHelperBotFilePresignParamsSchema
}).body;
const { userId } = await authHelperBotChatCrud({
type,
chatId,
req,
authToken: true
});
await authUploadLimit(userId);
return await getS3HelperBotSource().createUploadFileURL({
type,
chatId,
userId,
filename
});
}
export default NextAPI(handler);
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import {
GetHelperBotFilePreviewParamsSchema,
type GetHelperBotFilePreviewParamsType
} from '@fastgpt/global/openapi/core/chat/helperBot/api';
import { authCert } from '@fastgpt/service/support/permission/auth/common';
import { getS3HelperBotSource } from '@fastgpt/service/common/s3/sources/helperbot';
import { ChatErrEnum } from '@fastgpt/global/common/error/code/chat';
import { isAuthorizedHelperBotFileS3Key } from '@fastgpt/service/common/s3/sources/helperbot/key';
import { parseApiInput } from '@fastgpt/service/common/zod/requestParseError';
async function handler(req: ApiRequestProps<GetHelperBotFilePreviewParamsType>): Promise<string> {
const { key, mode } = parseApiInput({
req,
bodySchema: GetHelperBotFilePreviewParamsSchema
}).body;
const { userId } = await authCert({
req,
authToken: true
});
if (!isAuthorizedHelperBotFileS3Key({ key, userId })) {
return Promise.reject(ChatErrEnum.unAuthChat);
}
return (await getS3HelperBotSource().createGetFileURL({ key, external: true, mode })).url;
}
export default NextAPI(handler);
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import {
GetHelperBotChatRecordsParamsSchema,
type GetHelperBotChatRecordsParamsType,
type GetHelperBotChatRecordsResponseType
} from '@fastgpt/global/openapi/core/chat/helperBot/api';
import { authHelperBotChatCrud } from '@/service/support/permission/auth/chat';
import { MongoHelperBotChatItem } from '../../../../../../../../packages/service/core/chat/HelperBot/chatItemSchema';
import { parsePaginationRequest } from '@fastgpt/service/common/api/pagination';
import { addPreviewUrlToChatItems } from '@fastgpt/service/core/chat/utils';
import type { ChatItemMiniType } from '@fastgpt/global/core/chat/type';
import { parseApiInput } from '@fastgpt/service/common/zod/requestParseError';
export type getRecordsQuery = GetHelperBotChatRecordsParamsType;
export type getRecordsBody = Record<string, never>;
export type getRecordsResponse = GetHelperBotChatRecordsResponseType;
async function handler(
req: ApiRequestProps<getRecordsBody, getRecordsQuery>
): Promise<getRecordsResponse> {
const { type, chatId } = parseApiInput({
req,
querySchema: GetHelperBotChatRecordsParamsSchema
}).query;
const { userId } = await authHelperBotChatCrud({
type,
chatId,
req,
authToken: true
});
const { offset } = parsePaginationRequest(req);
const [histories, total] = await Promise.all([
MongoHelperBotChatItem.find({ userId, chatId }).sort({ _id: -1 }).skip(offset).limit(20).lean(),
MongoHelperBotChatItem.countDocuments({ userId, chatId })
]);
histories.reverse();
await addPreviewUrlToChatItems(histories as unknown as ChatItemMiniType[], 'chatFlow');
return {
total,
list: histories
};
}
export default NextAPI(handler);
......@@ -17,6 +17,10 @@ const getBatchDeletePermission = (sourceType: ChatSourceTypeEnum) => {
return WritePermissionVal;
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
return AppReadChatLogPerVal;
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
};
......
......@@ -23,6 +23,7 @@ import { buildChatSourceQuery } from '@fastgpt/service/core/chat/source';
import { authSkill } from '@fastgpt/service/support/permission/skill/auth';
import { AppTypeEnum } from '@fastgpt/global/core/app/constants';
import { buildChatTargetResponse } from '@fastgpt/global/openapi/core/chat/api';
import { createChatAgentHelperAppConfig } from '@fastgpt/global/core/ai/auxiliaryGeneration/chatAgentHelper';
async function handler(req: NextApiRequest): Promise<InitChatResponseType> {
const { sourceType, sourceId, chatId } = parseApiInput({
......@@ -88,6 +89,51 @@ async function handler(req: NextApiRequest): Promise<InitChatResponseType> {
});
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
const [{ teamId, tmbId, permission }, chat] = await Promise.all([
authApp({
req,
authToken: true,
authApiKey: true,
appId: sourceId,
per: ReadPermissionVal
}),
chatId
? MongoChat.findOne({ ...buildChatSourceQuery({ sourceType, sourceId }), chatId })
: undefined
]);
if (chat) {
if (String(chat.teamId) !== String(teamId)) {
return Promise.reject(ChatErrEnum.unAuthChat);
}
if (!permission.hasReadChatLogPer && String(chat.tmbId) !== String(tmbId)) {
return Promise.reject(ChatErrEnum.unAuthChat);
}
}
const chatGenerateStatus = chat?.chatGenerateStatus ?? ChatGenerateStatusEnum.done;
if (chat?.hasBeenRead === false && chatGenerateStatus !== ChatGenerateStatusEnum.generating) {
await MongoChat.updateOne(
{ ...buildChatSourceQuery({ sourceType, sourceId }), chatId },
{ $set: { hasBeenRead: true } }
);
chat.hasBeenRead = true;
}
return InitChatResponseSchema.parse({
chatId,
...buildChatTargetResponse({ sourceType, sourceId }),
title: chat?.title || '',
userAvatar: undefined,
variables: {},
chatGenerateStatus,
hasBeenRead: chat?.hasBeenRead ?? true,
app: createChatAgentHelperAppConfig()
});
}
if (sourceType !== ChatSourceTypeEnum.app) {
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
......
......@@ -9,9 +9,6 @@ import { authSkill } from '@fastgpt/service/support/permission/skill/auth';
import { MongoChatItem } from '@fastgpt/service/core/chat/chatItemSchema';
import { DatasetErrEnum } from '@fastgpt/global/common/error/code/dataset';
import { ChatRoleEnum, ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { HelperBotTypeEnum } from '@fastgpt/global/core/chat/helperBot/type';
import { MongoHelperBotChat } from '@fastgpt/service/core/chat/HelperBot/chatSchema';
import { authCert } from '@fastgpt/service/support/permission/auth/common';
import {
buildChatSourceAggregateMatch,
buildChatSourceQuery
......@@ -200,6 +197,7 @@ export type ChatTargetAuthParams = AuthModeType & {
type AuthChatTargetCrudResult = {
appId?: string;
userId?: string;
sourceType: ChatSourceTypeEnum;
sourceId: string;
teamId: string;
......@@ -283,6 +281,50 @@ export async function authChatTargetCrud({
};
}
if (sourceType === ChatSourceTypeEnum.chatAgentHelper) {
if (!sourceId) return Promise.reject(ChatErrEnum.unAuthChat);
const authRes = await authApp({
...props,
appId: sourceId,
per
});
if (!authRes.userId) return Promise.reject(ChatErrEnum.unAuthChat);
const chat =
(chatId
? await MongoChat.findOne({
...buildChatSourceQuery({ sourceType, sourceId }),
chatId
}).lean()
: undefined) ?? undefined;
if (chat) {
if (String(chat.teamId) !== String(authRes.teamId)) {
return Promise.reject(ChatErrEnum.unAuthChat);
}
if (!authRes.permission.hasReadChatLogPer && String(chat.tmbId) !== String(authRes.tmbId)) {
return Promise.reject(ChatErrEnum.unAuthChat);
}
}
return {
userId: authRes.userId,
teamId: authRes.teamId,
tmbId: authRes.tmbId,
uid: chat?.tmbId ? String(chat.tmbId) : authRes.tmbId,
chat,
showCite: false,
showRunningStatus: false,
showSkillReferences: false,
showFullText: false,
canDownloadSource: false,
sourceType,
sourceId,
authType: authRes.authType
};
}
const exhaustiveCheck: never = sourceType;
throw new Error(`Unsupported chat source type: ${exhaustiveCheck}`);
}
......@@ -336,18 +378,3 @@ export const authCollectionInChat = async ({
}
return Promise.reject(DatasetErrEnum.unAuthDatasetFile);
};
export const authHelperBotChatCrud = async ({
type,
chatId,
...props
}: AuthModeType & {
type: `${HelperBotTypeEnum}`;
chatId: string;
}) => {
const { userId, teamId, tmbId } = await authCert(props);
const chat = await MongoHelperBotChat.findOne({ type, userId, chatId }).lean();
return { chat, userId, teamId, tmbId };
};
......@@ -5,43 +5,6 @@ import { i18nT } from '@fastgpt/global/common/i18n/utils';
import { getDefaultSTTModel } from '@fastgpt/service/core/ai/model';
import type { UsageItemType } from '@fastgpt/global/support/wallet/usage/type';
export const pushHelperBotUsage = ({
teamId,
tmbId,
model,
inputTokens,
outputTokens
}: {
teamId: string;
tmbId: string;
model: string;
inputTokens: number;
outputTokens: number;
}) => {
const { totalPoints, modelName } = formatModelChars2Points({
model,
inputTokens,
outputTokens
});
createUsage({
teamId,
tmbId,
appName: i18nT('account_usage:helper_bot'),
totalPoints,
source: UsageSourceEnum.fastgpt,
list: [
{
moduleName: i18nT('account_usage:helper_bot'),
amount: totalPoints,
model: modelName,
inputTokens,
outputTokens
}
]
});
};
export const pushGenerateVectorUsage = ({
usageId,
teamId,
......
......@@ -7,6 +7,7 @@ import {
StreamResumeUnavailableReasonEnum
} from '@fastgpt/global/core/workflow/runtime/constants';
import {
ChatSourceTypeEnum,
STREAM_RESUME_REQUEST_HEADER,
STREAM_RESUME_REQUEST_HEADER_ENABLED
} from '@fastgpt/global/core/chat/constants';
......@@ -26,8 +27,8 @@ import type {
ToolModuleResponseItemType,
SkillModuleResponseItemType
} from '@fastgpt/global/core/chat/type';
import type { TopAgentFormDataType } from '@fastgpt/global/core/chat/helperBot/topAgent/type';
import type { UserInputInteractive } from '@fastgpt/global/core/workflow/template/system/interactive/type';
import type { ChatAgentConfigFormDataType } from '@fastgpt/global/core/ai/auxiliaryGeneration/type';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import type { AgentPlanStatusType, AgentPlanType } from '@fastgpt/global/core/ai/agent/type';
import type { StreamNoNeedToBeResumeType } from '@fastgpt/global/openapi/core/ai/api';
......@@ -60,6 +61,7 @@ const shouldSendStreamResumeHeader = (url: string) =>
'/api/v2/chat/completions',
'/api/proApi/core/chat/chatHome',
'/api/core/chat/chatTest',
'/api/proApi/core/chat/chatAgentHelper/completions',
'/api/core/ai/skill/debugChat',
'/api/proApi/core/ai/skill/debugChat'
]).has(url);
......@@ -86,12 +88,8 @@ type ResponseQueueItemType = CommonResponseType &
tool: ToolModuleResponseItemType;
}
| {
event: SseResponseEventEnum.collectionForm;
collectionForm: UserInputInteractive;
}
| {
event: SseResponseEventEnum.topAgentConfig;
data: TopAgentFormDataType;
event: AuxiliaryGenerationEventEnum.chatAgentConfig;
data: ChatAgentConfigFormDataType;
}
| {
event: SseResponseEventEnum.plan;
......@@ -205,13 +203,13 @@ function handleEventSourceData(params: HandleEventSourceDataParams) {
break;
}
case SseResponseEventEnum.collectionForm: {
enqueue({ responseValueId, event, collectionForm: obj });
case AuxiliaryGenerationEventEnum.chatAgentConfig: {
onmessage({ event, formData: obj });
break;
}
case SseResponseEventEnum.topAgentConfig: {
onmessage({ event, formData: obj });
case AuxiliaryGenerationEventEnum.status: {
onmessage({ event, ...obj });
break;
}
......@@ -301,7 +299,7 @@ function $ssefetch(params: SSEFetchParams) {
return resolve({ responseText, title });
};
const isAnswerEvent = (event: SseResponseEventEnum) => {
const isAnswerEvent = (event: string) => {
return event === SseResponseEventEnum.answer || event === SseResponseEventEnum.fastAnswer;
};
......@@ -460,7 +458,7 @@ function $resumefetch({ url, onmessage, onResumeUnavailable, controller }: Resum
} satisfies ResumeStreamErrorType);
};
const isAnswerEvent = (event: SseResponseEventEnum) => {
const isAnswerEvent = (event: string) => {
return event === SseResponseEventEnum.answer || event === SseResponseEventEnum.fastAnswer;
};
......@@ -671,6 +669,9 @@ export async function streamResumeFetch(params: StreamResumeFetchParams) {
query.set('skillId', params.skillId);
} else {
query.set('appId', params.appId!);
if (params.sourceType === ChatSourceTypeEnum.chatAgentHelper) {
query.set('sourceType', ChatSourceTypeEnum.chatAgentHelper);
}
}
const url = `/api/core/chat/resume?${query}`;
......
......@@ -7,16 +7,13 @@ import { getChatRecords } from '../record/api';
import { ChatStatusEnum } from '@fastgpt/global/core/chat/constants';
import { type BoxProps } from '@chakra-ui/react';
import { useMemoEnhance } from '@fastgpt/web/hooks/useMemoEnhance';
import type {
GetRecordsV2BodyType,
GetRecordsV2ResponseType
} from '@fastgpt/global/openapi/core/chat/record/api';
import type { GetRecordsV2ResponseType } from '@fastgpt/global/openapi/core/chat/record/api';
import { hasChatAuthTargetInput, type ChatAuthTargetInput } from '../utils';
type ChatRecordProviderParams = Omit<GetRecordsV2BodyType, 'pageSize' | 'outLinkAuthData'> &
ChatAuthTargetInput & {
pageSize?: number | string;
};
type ChatRecordProviderParams = ChatAuthTargetInput & {
chatId?: string;
pageSize?: number | string;
} & Record<string, unknown>;
type ChatRecordContextType = {
isLoadingRecords: boolean;
......@@ -86,7 +83,7 @@ const ChatRecordContextProvider = ({
async (
data: LinkedPaginationProps<ChatRecordProviderParams>
): Promise<LinkedListResponse<ChatSiteItemType>> => {
if (!hasChatAuthTargetInput(data)) {
if (!fetchFn && !hasChatAuthTargetInput(data)) {
return {
list: [],
hasMorePrev: false,
......
......@@ -4,6 +4,11 @@ import { type OutLinkChatAuthProps } from '@fastgpt/global/support/permission/ch
import { ChatSourceEnum } from '@fastgpt/global/core/chat/constants';
import { ChatSidebarPaneEnum } from '@/pageComponents/chat/constants';
export enum AgentChatTestTabEnum {
helper = 'helper',
chatDebug = 'chat_debug'
}
type State = {
source?: `${ChatSourceEnum}`;
setSource: (e: `${ChatSourceEnum}`) => any;
......@@ -18,10 +23,18 @@ type State = {
setChatId: (e?: string) => any;
/** 每个应用最近一次打开的 chatId,用于切换应用时恢复会话 */
appChatIdMap: Record<string, string>;
/** 非主会话按标准 sourceKey 隔离的 chatId,用于同一页面承载多个 ChatBox。 */
sourceChatIdMap: Record<string, string>;
ensureSourceChatId: (sourceKey: string) => string;
setSourceChatId: (sourceKey: string, chatId?: string) => string;
lastPane: ChatSidebarPaneEnum;
setLastPane: (e: ChatSidebarPaneEnum) => any;
/** Agent V2 详情页右侧测试面板当前 tab,仅在当前浏览器 tab 内保持。 */
agentChatTestTab: AgentChatTestTabEnum;
setAgentChatTestTab: (e: AgentChatTestTabEnum) => any;
outLinkAuthData: OutLinkChatAuthProps;
setOutLinkAuthData: (e: OutLinkChatAuthProps) => any;
......@@ -54,7 +67,7 @@ const getAppChatIdCacheKey = ({
const createCustomStorage = () => {
// source/chatId/appId 跟当前 tab 绑定,放 sessionStorage;其余跨 tab 共享字段放 localStorage
const sessionKeys = ['source', 'chatId', 'appId'];
const sessionKeys = ['source', 'chatId', 'appId', 'agentChatTestTab'];
return {
getItem: (name: string) => {
......@@ -126,9 +139,7 @@ export const useChatStore = create<State>()(
appId: state.appId,
outLinkAuthData: state.outLinkAuthData
});
const restoredAppChatId = nextCacheKey
? state.appChatIdMap[nextCacheKey]
: undefined;
const restoredAppChatId = nextCacheKey ? state.appChatIdMap[nextCacheKey] : undefined;
// 分享会话的恢复必须依赖 shareId + outLinkUid,不能只靠 lastChatId 的 source 前缀。
const lastChatPrefix = `${e}-`;
const restoredLastChatId =
......@@ -174,6 +185,26 @@ export const useChatStore = create<State>()(
lastChatId: '',
chatId: '',
appChatIdMap: {},
sourceChatIdMap: {},
ensureSourceChatId(sourceKey) {
let resolvedChatId = '';
set((state) => {
if (!sourceKey) return;
resolvedChatId = state.sourceChatIdMap[sourceKey] || getNanoid(24);
state.sourceChatIdMap[sourceKey] = resolvedChatId;
});
return resolvedChatId;
},
setSourceChatId(sourceKey, chatId) {
const resolvedChatId = chatId || getNanoid(24);
set((state) => {
if (!sourceKey) return;
state.sourceChatIdMap[sourceKey] = resolvedChatId;
});
return resolvedChatId;
},
setChatId(e) {
const id = e || getNanoid(24);
set((state) => {
......@@ -201,6 +232,12 @@ export const useChatStore = create<State>()(
state.lastPane = e;
});
},
agentChatTestTab: AgentChatTestTabEnum.chatDebug,
setAgentChatTestTab(e) {
set((state) => {
state.agentChatTestTab = e;
});
},
outLinkAuthData: {},
setOutLinkAuthData(e) {
set((state) => {
......@@ -236,7 +273,9 @@ export const useChatStore = create<State>()(
state.chatId = '';
state.lastChatId = '';
state.appChatIdMap = {};
state.sourceChatIdMap = {};
state.lastPane = ChatSidebarPaneEnum.HOME;
state.agentChatTestTab = AgentChatTestTabEnum.chatDebug;
state.outLinkAuthData = {};
});
}
......@@ -251,7 +290,9 @@ export const useChatStore = create<State>()(
lastChatId: state.lastChatId,
lastChatAppId: state.lastChatAppId,
lastPane: state.lastPane,
appChatIdMap: state.appChatIdMap
agentChatTestTab: state.agentChatTestTab,
appChatIdMap: state.appChatIdMap,
sourceChatIdMap: state.sourceChatIdMap
})
}
)
......@@ -270,7 +311,7 @@ const createStorageListener = (store: any) => {
const currentState = store.getState();
// 只同步 localStorage 中的数据(非 session 数据)
const sessionKeys = ['source', 'chatId', 'appId'];
const sessionKeys = ['source', 'chatId', 'appId', 'agentChatTestTab'];
const updatedState: Partial<State> = {};
let hasChanges = false;
......
......@@ -7,11 +7,17 @@ import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { useMemo } from 'react';
import type { OutLinkChatAuthProps } from '@fastgpt/global/support/permission/chat';
type OptionalChatTargetInput = Partial<Record<'appId' | 'skillId', string>>;
type OptionalChatTargetInput = Partial<Record<'appId' | 'skillId', string>> & {
sourceType?: ChatSourceTypeEnum.app | ChatSourceTypeEnum.chatAgentHelper;
};
type OptionalChatSourceInput = {
sourceType?: ChatSourceTypeEnum;
sourceId?: string;
};
type ChatAuthQueryInputSource = Partial<Record<'appId' | 'skillId', string>> &
OptionalChatSourceInput & {
outLinkAuthData?: OutLinkChatAuthProps;
};
export type ChatAuthTargetInput = OptionalChatTargetInput & {
outLinkAuthData?: OutLinkChatAuthProps;
};
......@@ -20,7 +26,10 @@ export type ChatAuthQueryTargetInput = OptionalChatTargetInput & {
};
export type ChatSourceTarget = {
sourceType: ChatSourceTypeEnum.app | ChatSourceTypeEnum.skillEdit;
sourceType:
| ChatSourceTypeEnum.app
| ChatSourceTypeEnum.skillEdit
| ChatSourceTypeEnum.chatAgentHelper;
sourceId: string;
};
......@@ -39,7 +48,10 @@ export const toChatSourceTarget = (target: ChatTargetInputType): ChatSourceTarge
}
return {
sourceType: ChatSourceTypeEnum.app,
sourceType:
target.sourceType === ChatSourceTypeEnum.chatAgentHelper
? ChatSourceTypeEnum.chatAgentHelper
: ChatSourceTypeEnum.app,
sourceId: target.appId!
};
};
......@@ -54,6 +66,9 @@ export const toChatApiTarget = (target: ChatSourceTarget): ChatTargetInputType =
if (target.sourceType === ChatSourceTypeEnum.skillEdit) {
return { skillId: target.sourceId };
}
if (target.sourceType === ChatSourceTypeEnum.chatAgentHelper) {
return { appId: target.sourceId, sourceType: ChatSourceTypeEnum.chatAgentHelper };
}
return { appId: target.sourceId };
};
......@@ -77,6 +92,10 @@ export const toChatAuthApiTarget = ({
return { outLinkAuthData };
}
if (sourceTarget.sourceType === ChatSourceTypeEnum.chatAgentHelper) {
return toChatApiTarget(sourceTarget);
}
return toChatApiTarget(sourceTarget);
};
......@@ -98,7 +117,9 @@ export const getChatAuthTargetInput = (target: ChatAuthTargetInput): ChatAuthTar
}
if (target.appId) {
return { appId: target.appId };
return target.sourceType === ChatSourceTypeEnum.chatAgentHelper
? { appId: target.appId, sourceType: ChatSourceTypeEnum.chatAgentHelper }
: { appId: target.appId };
}
return {};
......@@ -125,21 +146,32 @@ export const toChatAuthQueryTarget = (target: ChatAuthTargetInput): ChatAuthQuer
}
if (chatAuthTarget.appId) {
return { appId: chatAuthTarget.appId };
return chatAuthTarget.sourceType === ChatSourceTypeEnum.chatAgentHelper
? { appId: chatAuthTarget.appId, sourceType: ChatSourceTypeEnum.chatAgentHelper }
: { appId: chatAuthTarget.appId };
}
return {};
};
/** 保留 chat API 其它 query 字段,只把鉴权 target 转成 query 传输形态。 */
export const toChatAuthQueryInput = <T extends ChatAuthTargetInput & OptionalChatSourceInput>(
export const toChatAuthQueryInput = <T extends ChatAuthQueryInputSource>(
data: T
): Omit<T, 'appId' | 'skillId' | 'outLinkAuthData'> & ChatAuthQueryTargetInput => {
const { appId, skillId, outLinkAuthData, ...rest } = data;
): Omit<T, 'appId' | 'skillId' | 'sourceType' | 'outLinkAuthData'> & ChatAuthQueryTargetInput => {
const { appId, skillId, sourceType, outLinkAuthData, ...rest } = data;
return {
...rest,
...toChatAuthQueryTarget({ appId, skillId, outLinkAuthData })
...(!appId && !skillId && sourceType ? { sourceType } : {}),
...toChatAuthQueryTarget({
appId,
skillId,
sourceType:
sourceType === ChatSourceTypeEnum.chatAgentHelper
? ChatSourceTypeEnum.chatAgentHelper
: undefined,
outLinkAuthData
})
};
};
......@@ -183,7 +215,9 @@ export const getChatTargetInput = (target: ChatTargetInputType): ChatTargetInput
return { skillId: target.skillId };
}
return { appId: target.appId! };
return target.sourceType === ChatSourceTypeEnum.chatAgentHelper
? { appId: target.appId!, sourceType: ChatSourceTypeEnum.chatAgentHelper }
: { appId: target.appId! };
};
export const hasChatTargetInput = (target?: { appId?: unknown; skillId?: unknown } | null) =>
......@@ -197,6 +231,7 @@ export const hasChatAuthTargetInput = (
} & {
appId?: unknown;
skillId?: unknown;
sourceType?: unknown;
})
| null
) => {
......
......@@ -202,6 +202,19 @@ describe('buildDebugRuntimeNodes', () => {
expect(promptInput!.valueType).toBe(WorkflowIOValueTypeEnum.string);
});
it('should enable vision preview for uploaded images', () => {
const { runtimeNodes } = buildDebugRuntimeNodes(SKILL_ID, MODEL, SYSTEM_PROMPT);
const agentNode = runtimeNodes[1];
const visionInput = agentNode.inputs.find((i) => i.key === NodeInputKeyEnum.aiChatVision);
expect(visionInput).toMatchObject({
renderTypeList: [FlowNodeInputTypeEnum.hidden],
valueType: WorkflowIOValueTypeEnum.boolean,
value: true
});
});
it('editSkillId input should contain exactly the given skillId', () => {
const { runtimeNodes } = buildDebugRuntimeNodes(SKILL_ID, MODEL, SYSTEM_PROMPT);
const agentNode = runtimeNodes[1];
......@@ -548,6 +561,13 @@ describe('debugChat handler — parameter validation', () => {
expect.objectContaining({
chatId: 'prepared-debug-chat-id',
responseChatItemId: 'prepared-debug-response-id',
chatConfig: {
fileSelectConfig: expect.objectContaining({
canSelectFile: true,
canSelectImg: true,
maxFiles: 10
})
},
agentSandboxPrepareActions: undefined
})
);
......
......@@ -243,6 +243,73 @@ describe('batchDelete api test', () => {
).toBe(0);
});
it('should batch delete chatAgentHelper records from generic chat tables', async () => {
const helperChatId = getNanoid();
await Promise.all([
MongoChat.create({
teamId: testUser.teamId,
tmbId: testUser.tmbId,
userId: testUser.userId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId,
chatId: helperChatId,
source: ChatSourceEnum.test
}),
MongoChatItem.create({
teamId: testUser.teamId,
tmbId: testUser.tmbId,
userId: testUser.userId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId,
chatId: helperChatId,
dataId: getNanoid(),
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'helper response' } }]
}),
MongoChatItemResponse.create({
teamId: testUser.teamId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId,
chatId: helperChatId,
chatItemDataId: getNanoid(),
data: { nodeId: 'helper-node' }
})
]);
const res = await Call<ChatBatchDeleteBodyType, EmptyQuery>(handler, {
auth: testUser,
body: {
appId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
chatIds: [helperChatId]
}
});
expect(res.code).toBe(200);
expect(mocks.deleteAppChatRuntimeSandboxes).not.toHaveBeenCalled();
expect(
await MongoChat.countDocuments({
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId,
chatId: helperChatId
})
).toBe(0);
expect(
await MongoChatItem.countDocuments({
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId,
chatId: helperChatId
})
).toBe(0);
expect(
await MongoChatItemResponse.countDocuments({
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId,
chatId: helperChatId
})
).toBe(0);
});
it('should delete single chat', async () => {
const deleteIds = [chatIds[0]];
......
import handler from '@/pages/api/core/chat/record/getRecords_v2';
import { AgentSkillSourceEnum } from '@fastgpt/global/core/ai/skill/constants';
import { AppTypeEnum } from '@fastgpt/global/core/app/constants';
import {
ChatRoleEnum,
ChatSourceEnum,
......@@ -11,6 +12,7 @@ import type {
GetRecordsV2ResponseType
} from '@fastgpt/global/openapi/core/chat/record/api';
import { MongoAgentSkills } from '@fastgpt/service/core/ai/skill/model/schema';
import { MongoApp } from '@fastgpt/service/core/app/schema';
import { MongoChat } from '@fastgpt/service/core/chat/chatSchema';
import { MongoChatItem } from '@fastgpt/service/core/chat/chatItemSchema';
import { getUser } from '@test/datas/users';
......@@ -149,4 +151,63 @@ describe('getRecords_v2 skill edit target', () => {
expect(res.data.list.length).toBeLessThanOrEqual(3);
expect(res.data.hasMorePrev).toBe(true);
});
it('should read chatAgentHelper records through generic records api', async () => {
const app = await MongoApp.create({
name: 'ChatAgentHelper Records App',
type: AppTypeEnum.simple,
teamId: testUser.teamId,
tmbId: testUser.tmbId,
modules: []
});
const helperChatId = getNanoid();
const [, humanItem, aiItem] = await Promise.all([
MongoChat.create({
teamId: testUser.teamId,
tmbId: testUser.tmbId,
userId: testUser.userId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId: String(app._id),
chatId: helperChatId,
source: ChatSourceEnum.test
}),
MongoChatItem.create({
teamId: testUser.teamId,
tmbId: testUser.tmbId,
userId: testUser.userId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId: String(app._id),
chatId: helperChatId,
dataId: 'helper-round-1',
obj: ChatRoleEnum.Human,
value: [{ text: { content: 'build an app' } }]
}),
MongoChatItem.create({
teamId: testUser.teamId,
tmbId: testUser.tmbId,
userId: testUser.userId,
sourceType: ChatSourceTypeEnum.chatAgentHelper,
appId: String(app._id),
chatId: helperChatId,
dataId: 'helper-round-1',
obj: ChatRoleEnum.AI,
value: [{ text: { content: 'agent draft' } }]
})
]);
const res = await Call<GetRecordsV2BodyType, any, GetRecordsV2ResponseType>(handler, {
auth: testUser,
body: {
appId: String(app._id),
sourceType: ChatSourceTypeEnum.chatAgentHelper,
chatId: helperChatId,
pageSize: 10
}
});
expect(res.code).toBe(200);
expect(res.data.total).toBe(2);
expect(res.data.list.map((item) => item.id)).toEqual([humanItem.dataId, aiItem.dataId]);
expect(res.data.list.map((item) => item.dataId)).toEqual(['helper-round-1', 'helper-round-1']);
});
});
import { describe, expect, it } from 'vitest';
import { ChatRoleEnum, ChatStatusEnum } from '@fastgpt/global/core/chat/constants';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { AuxiliaryGenerationEventEnum } from '@fastgpt/global/core/ai/auxiliaryGeneration/constants';
import {
hasMeaningfulAiOutput,
mergeResumeCompletedChatRecords,
......@@ -41,6 +42,7 @@ describe('shouldCreateResumeAiPlaceholder', () => {
expect(shouldCreateResumeAiPlaceholder(SseResponseEventEnum.plan)).toBe(true);
expect(shouldCreateResumeAiPlaceholder(SseResponseEventEnum.planStatus)).toBe(true);
expect(shouldCreateResumeAiPlaceholder(SseResponseEventEnum.workflowDuration)).toBe(true);
expect(shouldCreateResumeAiPlaceholder(AuxiliaryGenerationEventEnum.status)).toBe(true);
});
it('returns false for stream control events that do not create chat content', () => {
......
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { EventNameEnum, eventBus } from '@/web/common/utils/eventbus';
import { onSendPrompt } from '@/components/core/chat/components/AIResponseBox/utils';
describe('AIResponseBox utils', () => {
beforeEach(() => {
eventBus.off(EventNameEnum.sendQuestion);
});
it('does not request clearing chat input for interactive send event', () => {
const handler = vi.fn();
eventBus.on(EventNameEnum.sendQuestion, handler);
onSendPrompt('A');
expect(handler).toHaveBeenCalledWith({
text: 'A',
focus: true
});
});
});
import { ApiRequestInputParseError } from '@fastgpt/service/common/zod/requestParseError';
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
authHelperBotChatCrud: vi.fn(),
deleteMany: vi.fn()
}));
vi.mock('@/service/middleware/entry', () => ({
NextAPI: (handler: unknown) => handler
}));
vi.mock('@/service/support/permission/auth/chat', () => ({
authHelperBotChatCrud: mocks.authHelperBotChatCrud
}));
vi.mock('@fastgpt/service/core/chat/HelperBot/chatItemSchema', () => ({
MongoHelperBotChatItem: {
deleteMany: mocks.deleteMany
}
}));
import handler from '@/pages/api/core/chat/helperBot/deleteRecord';
const deleteHandler = handler as unknown as (
req: ApiRequestProps
) => Promise<Record<string, never>>;
const callHandler = (query: Record<string, unknown>) =>
deleteHandler({
query
} as ApiRequestProps);
describe('helperBot/deleteRecord', () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.authHelperBotChatCrud.mockResolvedValue({
chat: { chatId: 'chat-1' },
userId: 'user-1'
});
mocks.deleteMany.mockResolvedValue({ deletedCount: 1 });
});
it('validates query before deleting records', async () => {
await expect(
callHandler({
type: 'topAgent',
chatId: 'chat-1',
chatItemId: 'item-1'
})
).resolves.toEqual({});
expect(mocks.authHelperBotChatCrud).toHaveBeenCalledWith({
type: 'topAgent',
chatId: 'chat-1',
req: expect.any(Object),
authToken: true
});
expect(mocks.deleteMany).toHaveBeenCalledWith({
userId: 'user-1',
chatId: 'chat-1',
chatItemId: 'item-1'
});
});
it('rejects invalid query before auth or delete', async () => {
await expect(callHandler({ type: 'topAgent', chatId: 'chat-1' })).rejects.toBeInstanceOf(
ApiRequestInputParseError
);
expect(mocks.authHelperBotChatCrud).not.toHaveBeenCalled();
expect(mocks.deleteMany).not.toHaveBeenCalled();
});
});
import { ApiRequestInputParseError } from '@fastgpt/service/common/zod/requestParseError';
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
authHelperBotChatCrud: vi.fn(),
authFrequencyLimit: vi.fn(),
createUploadFileURL: vi.fn()
}));
vi.mock('@/service/middleware/entry', () => ({
NextAPI: (handler: unknown) => handler
}));
vi.mock('@/service/support/permission/auth/chat', () => ({
authHelperBotChatCrud: mocks.authHelperBotChatCrud
}));
vi.mock('@fastgpt/service/common/system/frequencyLimit/utils', () => ({
authFrequencyLimit: mocks.authFrequencyLimit
}));
vi.mock('@fastgpt/service/common/s3/sources/helperbot/index', () => ({
getS3HelperBotSource: () => ({
createUploadFileURL: mocks.createUploadFileURL
})
}));
import handler from '@/pages/api/core/chat/helperBot/getFilePresign';
const presignHandler = handler as unknown as (req: ApiRequestProps) => Promise<unknown>;
const callHandler = (body: Record<string, unknown>) =>
presignHandler({
body
} as ApiRequestProps);
describe('helperBot/getFilePresign', () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.authHelperBotChatCrud.mockResolvedValue({
userId: 'user-1'
});
mocks.createUploadFileURL.mockResolvedValue({
url: 'https://example.com/upload',
key: 'helperBot/topAgent/user-1/chat-1/demo.pdf',
headers: {},
previewUrl: 'https://example.com/preview'
});
});
it('validates body before creating upload URL', async () => {
await expect(
callHandler({
type: 'topAgent',
chatId: 'chat-1',
filename: 'demo.pdf'
})
).resolves.toEqual({
url: 'https://example.com/upload',
key: 'helperBot/topAgent/user-1/chat-1/demo.pdf',
headers: {},
previewUrl: 'https://example.com/preview'
});
expect(mocks.authHelperBotChatCrud).toHaveBeenCalledWith({
type: 'topAgent',
chatId: 'chat-1',
req: expect.any(Object),
authToken: true
});
expect(mocks.createUploadFileURL).toHaveBeenCalledWith({
type: 'topAgent',
chatId: 'chat-1',
userId: 'user-1',
filename: 'demo.pdf'
});
});
it('rejects invalid body before auth or signing', async () => {
await expect(callHandler({ type: 'topAgent', chatId: 'chat-1' })).rejects.toBeInstanceOf(
ApiRequestInputParseError
);
expect(mocks.authHelperBotChatCrud).not.toHaveBeenCalled();
expect(mocks.authFrequencyLimit).not.toHaveBeenCalled();
expect(mocks.createUploadFileURL).not.toHaveBeenCalled();
});
});
import { ChatErrEnum } from '@fastgpt/global/common/error/code/chat';
import { ApiRequestInputParseError } from '@fastgpt/service/common/zod/requestParseError';
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
authCert: vi.fn(),
createGetFileURL: vi.fn()
}));
vi.mock('@/service/middleware/entry', () => ({
NextAPI: (handler: unknown) => handler
}));
vi.mock('@fastgpt/service/support/permission/auth/common', () => ({
authCert: mocks.authCert
}));
vi.mock('@fastgpt/service/common/s3/sources/helperbot', () => ({
getS3HelperBotSource: () => ({
createGetFileURL: mocks.createGetFileURL
})
}));
import handler from '@/pages/api/core/chat/helperBot/getFilePreviewUrl';
const previewHandler = handler as unknown as (req: ApiRequestProps) => Promise<string>;
const callHandler = (body: Record<string, unknown>) =>
previewHandler({
body
} as ApiRequestProps);
describe('getFilePreviewUrl', () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.authCert.mockResolvedValue({
userId: 'user-1'
});
mocks.createGetFileURL.mockResolvedValue({
url: 'https://example.com/helper-bot-file'
});
});
it('signs a helper bot file only when the key belongs to the current user', async () => {
await expect(
callHandler({
key: 'helperBot/topAgent/user-1/chat-1/demo.pdf'
})
).resolves.toBe('https://example.com/helper-bot-file');
expect(mocks.createGetFileURL).toHaveBeenCalledWith({
key: 'helperBot/topAgent/user-1/chat-1/demo.pdf',
external: true,
mode: undefined
});
});
it('rejects a helper bot file key from another user', async () => {
await expect(
callHandler({
key: 'helperBot/topAgent/user-2/chat-1/demo.pdf'
})
).rejects.toBe(ChatErrEnum.unAuthChat);
expect(mocks.createGetFileURL).not.toHaveBeenCalled();
});
it('rejects a malformed helper bot file key before signing', async () => {
await expect(
callHandler({
key: 'topAgent/chat-1/user-1/demo.pdf'
})
).rejects.toBe(ChatErrEnum.unAuthChat);
expect(mocks.createGetFileURL).not.toHaveBeenCalled();
});
it('rejects invalid request body before auth or signing', async () => {
await expect(callHandler({})).rejects.toBeInstanceOf(ApiRequestInputParseError);
expect(mocks.authCert).not.toHaveBeenCalled();
expect(mocks.createGetFileURL).not.toHaveBeenCalled();
});
});
......@@ -34,6 +34,7 @@ describe('useChatStore', () => {
lastChatId: '',
chatId: '',
appChatIdMap: {},
sourceChatIdMap: {},
lastPane: undefined,
outLinkAuthData: {}
});
......@@ -84,6 +85,27 @@ describe('useChatStore', () => {
expect(useChatStore.getState().chatId).toBe('chat-b');
});
it('should manage sourceKey chatId independently from the main chatId', () => {
const store = useChatStore.getState();
store.setSource(ChatSourceEnum.online);
store.setAppId('app-a');
store.setChatId('debug-chat-id');
store.setSourceChatId('chatAgentHelper:app-a', 'helper-chat-id');
const newState = useChatStore.getState();
expect(newState.chatId).toBe('debug-chat-id');
expect(newState.sourceChatIdMap['chatAgentHelper:app-a']).toBe('helper-chat-id');
expect(useChatStore.getState().ensureSourceChatId('chatAgentHelper:app-a')).toBe(
'helper-chat-id'
);
expect(store.ensureSourceChatId('chatAgentHelper:app-b')).toBe('test-generated-id');
expect(useChatStore.getState().ensureSourceChatId('chatAgentHelper:app-b')).toBe(
'test-generated-id'
);
});
it('should namespace app chat cache by source', () => {
const store = useChatStore.getState();
......@@ -308,6 +330,9 @@ describe('useChatStore', () => {
lastChatAppId: 'last-app-id',
chatId: 'chat-id',
lastChatId: `${ChatSourceEnum.share}-chat-id`,
sourceChatIdMap: {
'chatAgentHelper:app-a': 'helper-chat-id'
},
lastPane: ChatSidebarPaneEnum.RECENTLY_USED_APPS,
outLinkAuthData: {
shareId: 'share-id',
......@@ -324,6 +349,7 @@ describe('useChatStore', () => {
chatId: '',
lastChatId: '',
appChatIdMap: {},
sourceChatIdMap: {},
lastPane: ChatSidebarPaneEnum.HOME,
outLinkAuthData: {}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment