Commit 60c62b7a by Archer Committed by GitHub

Fix test (#7179)

* perf: title llm config

* fix: test

* feat: teamId in reacord llm

* perf: merge messages

* fix: repair pr action failures

* test: update service ci fixtures

* fix: watch input
parent aa1d879b
...@@ -169,9 +169,28 @@ jobs: ...@@ -169,9 +169,28 @@ jobs:
- name: Create manifest list and push - name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests working-directory: ${{ runner.temp }}/digests
run: | run: |
retry_imagetools_create() {
local tag="$1"
local sources
sources="$(printf 'ghcr.io/${{ github.repository_owner }}/${{ matrix.sub_routes.repo }}@sha256:%s ' *)"
for attempt in 1 2 3 4; do
if docker buildx imagetools create -t "$tag" $sources; then
return 0
fi
if [ "$attempt" -eq 4 ]; then
echo "::error::Failed to push manifest ${tag} after 3 retries."
return 1
fi
echo "::warning::Failed to push manifest ${tag}; retrying in $((attempt * 15)) seconds (${attempt}/3)."
sleep $((attempt * 15))
done
}
TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}")" TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}")"
for TAG in $TAGS; do for TAG in $TAGS; do
docker buildx imagetools create -t $TAG \ retry_imagetools_create "$TAG"
$(printf 'ghcr.io/${{ github.repository_owner }}/${{ matrix.sub_routes.repo }}@sha256:%s ' *)
sleep 5 sleep 5
done done
...@@ -141,10 +141,29 @@ jobs: ...@@ -141,10 +141,29 @@ jobs:
- name: Create manifest list and push - name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests working-directory: ${{ runner.temp }}/digests
run: | run: |
retry_imagetools_create() {
local tag="$1"
local sources
sources="$(printf 'ghcr.io/${{ github.repository_owner }}/fastgpt-agent-sandbox@sha256:%s ' *)"
for attempt in 1 2 3 4; do
if docker buildx imagetools create -t "$tag" $sources; then
return 0
fi
if [ "$attempt" -eq 4 ]; then
echo "::error::Failed to push manifest ${tag} after 3 retries."
return 1
fi
echo "::warning::Failed to push manifest ${tag}; retrying in $((attempt * 15)) seconds (${attempt}/3)."
sleep $((attempt * 15))
done
}
TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}\n${Docker_Hub_Tag}\n${Docker_Hub_Latest}")" TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}\n${Docker_Hub_Tag}\n${Docker_Hub_Latest}")"
for TAG in $TAGS; do for TAG in $TAGS; do
docker buildx imagetools create -t $TAG \ retry_imagetools_create "$TAG"
$(printf 'ghcr.io/${{ github.repository_owner }}/fastgpt-agent-sandbox@sha256:%s ' *)
sleep 5 sleep 5
done done
...@@ -266,9 +285,28 @@ jobs: ...@@ -266,9 +285,28 @@ jobs:
- name: Create manifest list and push - name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests working-directory: ${{ runner.temp }}/digests
run: | run: |
retry_imagetools_create() {
local tag="$1"
local sources
sources="$(printf 'ghcr.io/${{ github.repository_owner }}/fastgpt-agent-volume-manager@sha256:%s ' *)"
for attempt in 1 2 3 4; do
if docker buildx imagetools create -t "$tag" $sources; then
return 0
fi
if [ "$attempt" -eq 4 ]; then
echo "::error::Failed to push manifest ${tag} after 3 retries."
return 1
fi
echo "::warning::Failed to push manifest ${tag}; retrying in $((attempt * 15)) seconds (${attempt}/3)."
sleep $((attempt * 15))
done
}
TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}\n${Docker_Hub_Tag}\n${Docker_Hub_Latest}")" TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}\n${Docker_Hub_Tag}\n${Docker_Hub_Latest}")"
for TAG in $TAGS; do for TAG in $TAGS; do
docker buildx imagetools create -t $TAG \ retry_imagetools_create "$TAG"
$(printf 'ghcr.io/${{ github.repository_owner }}/fastgpt-agent-volume-manager@sha256:%s ' *)
sleep 5 sleep 5
done done
...@@ -135,9 +135,28 @@ jobs: ...@@ -135,9 +135,28 @@ jobs:
- name: Create manifest list and push - name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests working-directory: ${{ runner.temp }}/digests
run: | run: |
retry_imagetools_create() {
local tag="$1"
local sources
sources="$(printf 'ghcr.io/${{ github.repository_owner }}/fastgpt-code-sandbox@sha256:%s ' *)"
for attempt in 1 2 3 4; do
if docker buildx imagetools create -t "$tag" $sources; then
return 0
fi
if [ "$attempt" -eq 4 ]; then
echo "::error::Failed to push manifest ${tag} after 3 retries."
return 1
fi
echo "::warning::Failed to push manifest ${tag}; retrying in $((attempt * 15)) seconds (${attempt}/3)."
sleep $((attempt * 15))
done
}
TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}")" TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}")"
for TAG in $TAGS; do for TAG in $TAGS; do
docker buildx imagetools create -t $TAG \ retry_imagetools_create "$TAG"
$(printf 'ghcr.io/${{ github.repository_owner }}/fastgpt-code-sandbox@sha256:%s ' *)
sleep 5 sleep 5
done done
...@@ -163,9 +163,28 @@ jobs: ...@@ -163,9 +163,28 @@ jobs:
- name: Create manifest list and push - name: Create manifest list and push
working-directory: ${{ runner.temp }}/digests working-directory: ${{ runner.temp }}/digests
run: | run: |
retry_imagetools_create() {
local tag="$1"
local sources
sources="$(printf 'ghcr.io/${{ github.repository_owner }}/${{ matrix.sub_routes.repo }}@sha256:%s ' *)"
for attempt in 1 2 3 4; do
if docker buildx imagetools create -t "$tag" $sources; then
return 0
fi
if [ "$attempt" -eq 4 ]; then
echo "::error::Failed to push manifest ${tag} after 3 retries."
return 1
fi
echo "::warning::Failed to push manifest ${tag}; retrying in $((attempt * 15)) seconds (${attempt}/3)."
sleep $((attempt * 15))
done
}
TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}\n${Docker_Hub_Tag}\n${Docker_Hub_Latest}")" TAGS="$(echo -e "${Git_Tag}\n${Git_Latest}\n${Ali_Tag}\n${Ali_Latest}\n${Docker_Hub_Tag}\n${Docker_Hub_Latest}")"
for TAG in $TAGS; do for TAG in $TAGS; do
docker buildx imagetools create -t $TAG \ retry_imagetools_create "$TAG"
$(printf 'ghcr.io/${{ github.repository_owner }}/${{ matrix.sub_routes.repo }}@sha256:%s ' *)
sleep 5 sleep 5
done done
...@@ -20,16 +20,16 @@ These variables are mainly validated by `packages/service/env.ts` and apply to ` ...@@ -20,16 +20,16 @@ These variables are mainly validated by `packages/service/env.ts` and apply to `
### Basics and Secrets ### Basics and Secrets
| Variable | Default | Description | | Variable | Default | Description |
| ------------------- | ------------------- | ---------------------------------------------------------------------------------------------------------- | | --------------------- | ------------------- | ---------------------------------------------------------------------------------------------------------- |
| `DB_MAX_LINK` | `5` | Maximum connection pool size for MongoDB, PG, OceanBase, openGauss, and other databases. | | `DB_MAX_LINK` | `5` | Maximum connection pool size for MongoDB, PG, OceanBase, openGauss, and other databases. |
| `SYNC_INDEX` | `true` | Whether MongoDB indexes are synchronized at startup. | | `SYNC_INDEX` | `true` | Whether MongoDB indexes are synchronized at startup. |
| `TOKEN_KEY` | `fastgpt_token_key` | Signing secret for user sessions and business tokens. Must be at least 6 characters. | | `TOKEN_KEY` | `fastgpt_token_key` | Signing secret for user sessions and business tokens. Must be at least 6 characters. |
| `FILE_TOKEN_KEY` | None, **required** | Secret for file read and file authorization flows. Must be at least 6 characters. | | `FILE_TOKEN_KEY` | None, **required** | Secret for file read and file authorization flows. Must be at least 6 characters. |
| `AES256_SECRET_KEY` | None, **required** | Secret used by AES encryption and decryption. Must be at least 6 characters. | | `AES256_SECRET_KEY` | None, **required** | Secret used by AES encryption and decryption. Must be at least 6 characters. |
| `INVOKE_TOKEN_SECRET` | None, **required** | JWT secret for Invoke reverse calls. Must be at least 32 characters. | | `INVOKE_TOKEN_SECRET` | None, **required** | JWT secret for Invoke reverse calls. Must be at least 32 characters. |
| `ROOT_KEY` | `fastgpt_root_key` | Root API key or internal highest-privilege secret. Must be at least 6 characters. | | `ROOT_KEY` | `fastgpt_root_key` | Root API key or internal highest-privilege secret. Must be at least 6 characters. |
| `PRO_URL` | Empty | Commercial service URL. When set, App can call Pro APIs, and the domain is allowed by file URL validation. | | `PRO_URL` | Empty | Commercial service URL. When set, App can call Pro APIs, and the domain is allowed by file URL validation. |
### Service URLs and Integrations ### Service URLs and Integrations
...@@ -182,7 +182,6 @@ These variables are mainly validated by `packages/service/env.ts` and apply to ` ...@@ -182,7 +182,6 @@ These variables are mainly validated by `packages/service/env.ts` and apply to `
| -------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | -------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AGENT_ENGINE` | `default` | Agent engine. Supported values are `default` and `pi`. | | `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. | | `HELPER_BOT_MODEL` | Empty | Helper generation model. The model must be enabled in the system. |
| `CHAT_TITLE_MODEL` | Empty | Chat title generation model. If unset or not matched to an enabled model, the system falls back to the default LLM. |
| `SKIP_FILE_TYPE_CHECK` | `false` | Whether upload file type checks are skipped. | | `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`. | | `WECHAT_CHANNEL_CONCURRENCY` | `1000` | WeChat channel poll worker concurrency. Minimum value is `10`. |
| `PARSE_FILE_WORKERS` | `10` | Resident file parsing worker count. | | `PARSE_FILE_WORKERS` | `10` | Resident file parsing worker count. |
......
...@@ -20,16 +20,16 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说 ...@@ -20,16 +20,16 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
### 基础与密钥 ### 基础与密钥
| 变量 | 默认值 | 说明 | | 变量 | 默认值 | 说明 |
| ------------------- | ------------------- | ------------------------------------------------------------------------------ | | --------------------- | ------------------- | ------------------------------------------------------------------------------ |
| `DB_MAX_LINK` | `5` | MongoDB、PG、OceanBase、openGauss 等数据库连接池最大连接数。 | | `DB_MAX_LINK` | `5` | MongoDB、PG、OceanBase、openGauss 等数据库连接池最大连接数。 |
| `SYNC_INDEX` | `true` | 启动时是否同步 MongoDB 索引。 | | `SYNC_INDEX` | `true` | 启动时是否同步 MongoDB 索引。 |
| `TOKEN_KEY` | `fastgpt_token_key` | 用户登录态、业务 Token 等签名密钥,长度至少 6 位。 | | `TOKEN_KEY` | `fastgpt_token_key` | 用户登录态、业务 Token 等签名密钥,长度至少 6 位。 |
| `FILE_TOKEN_KEY` | 无,**必填** | 文件读取、文件鉴权相关密钥,长度至少 6 位。 | | `FILE_TOKEN_KEY` | 无,**必填** | 文件读取、文件鉴权相关密钥,长度至少 6 位。 |
| `AES256_SECRET_KEY` | 无,**必填** | AES 加解密密钥,长度至少 6 位。 | | `AES256_SECRET_KEY` | 无,**必填** | AES 加解密密钥,长度至少 6 位。 |
| `INVOKE_TOKEN_SECRET` | 无,**必填** | Invoke 反向调用 JWT 密钥,长度至少 32 位。 | | `INVOKE_TOKEN_SECRET` | 无,**必填** | Invoke 反向调用 JWT 密钥,长度至少 32 位。 |
| `ROOT_KEY` | `fastgpt_root_key` | Root API Key/内部最高权限密钥,长度至少 6 位。 | | `ROOT_KEY` | `fastgpt_root_key` | Root API Key/内部最高权限密钥,长度至少 6 位。 |
| `PRO_URL` | 空 | 商业版服务地址,配置后 App 可调用 Pro API,也会作为文件 URL 安全校验允许域名。 | | `PRO_URL` | 空 | 商业版服务地址,配置后 App 可调用 Pro API,也会作为文件 URL 安全校验允许域名。 |
### 服务地址与集成 ### 服务地址与集成
...@@ -182,7 +182,6 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说 ...@@ -182,7 +182,6 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
| -------------------------------------- | --------- | ---------------------------------------------------------------------------------------------- | | -------------------------------------- | --------- | ---------------------------------------------------------------------------------------------- |
| `AGENT_ENGINE` | `default` | Agent 引擎,可选 `default` 或 `pi`。 | | `AGENT_ENGINE` | `default` | Agent 引擎,可选 `default` 或 `pi`。 |
| `HELPER_BOT_MODEL` | 空 | 辅助生成模型,需保证系统中已启用对应模型。 | | `HELPER_BOT_MODEL` | 空 | 辅助生成模型,需保证系统中已启用对应模型。 |
| `CHAT_TITLE_MODEL` | 空 | 对话标题生成模型;不配置或未命中已启用模型时,回退到系统默认 LLM。 |
| `SKIP_FILE_TYPE_CHECK` | `false` | 是否跳过上传文件类型检查。 | | `SKIP_FILE_TYPE_CHECK` | `false` | 是否跳过上传文件类型检查。 |
| `WECHAT_CHANNEL_CONCURRENCY` | `1000` | 微信渠道 poll worker 并发数,最小 `10`。 | | `WECHAT_CHANNEL_CONCURRENCY` | `1000` | 微信渠道 poll worker 并发数,最小 `10`。 |
| `PARSE_FILE_WORKERS` | `10` | 文件解析 worker 常驻线程数。 | | `PARSE_FILE_WORKERS` | `10` | 文件解析 worker 常驻线程数。 |
......
...@@ -39,8 +39,6 @@ SYNC_INDEX=true ...@@ -39,8 +39,6 @@ SYNC_INDEX=true
TRUSTED_PROXY_ENABLE=false TRUSTED_PROXY_ENABLE=false
# Trusted reverse proxy IP/CIDR list, separated by commas or whitespace. Only effective when TRUSTED_PROXY_ENABLE=true # Trusted reverse proxy IP/CIDR list, separated by commas or whitespace. Only effective when TRUSTED_PROXY_ENABLE=true
TRUSTED_PROXY_IPS= TRUSTED_PROXY_IPS=
# Model used to automatically generate chat titles
CHAT_TITLE_MODEL=deepseek-v4-flash
# Maximum number of characters for synchronous system string processing, in M. Range: 1-100 # Maximum number of characters for synchronous system string processing, in M. Range: 1-100
SYSTEM_MAX_STRING_LENGTH_M=100 SYSTEM_MAX_STRING_LENGTH_M=100
# Maximum folder depth. Default: 4. Range: 2-20 # Maximum folder depth. Default: 4. Range: 2-20
...@@ -108,6 +106,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \ ...@@ -108,6 +106,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \
1. API Key behavior has changed. FastGPT no longer distinguishes between app keys and system keys; only system keys are kept. For OpenAI SDK compatibility, pass the token as `apikey-appId`. Existing API keys remain compatible and continue to work. For details, see the [FastGPT API documentation](../../../openapi/intro). 1. API Key behavior has changed. FastGPT no longer distinguishes between app keys and system keys; only system keys are kept. For OpenAI SDK compatibility, pass the token as `apikey-appId`. Existing API keys remain compatible and continue to work. For details, see the [FastGPT API documentation](../../../openapi/intro).
2. Some APIs now enforce stricter data format validation. If you see a `zod parse error`, please submit an issue. It may be caused by legacy data or custom data structures that do not match the declared schema. 2. Some APIs now enforce stricter data format validation. If you see a `zod parse error`, please submit an issue. It may be caused by legacy data or custom data structures that do not match the declared schema.
3. LLM request traces now enforce team isolation. `llm_request_records` stores `teamId`, `GET /api/core/ai/record/getRecord` queries by `{ requestId, teamId }`, and the unique index changes to `{ teamId, requestId }`. Trace records written before this upgrade do not contain `teamId` and can no longer be queried; the UI will treat them as expired. Export relevant logs or keep original request details before upgrading if you need to investigate historical calls. If your self-hosted deployment has `SYNC_INDEX` disabled, run an index sync after upgrading so the old `requestId_1` unique index is removed.
## 🚀 New Features ## 🚀 New Features
...@@ -167,6 +166,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \ ...@@ -167,6 +166,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \
30. Improved the login page UI. 30. Improved the login page UI.
31. Deduplicated site sync rate-limit error prompts. 31. Deduplicated site sync rate-limit error prompts.
32. Image processing workers now support configuring whether images are converted to base64 before being sent to the model. 32. Image processing workers now support configuring whether images are converted to base64 before being sent to the model.
33. LLM request traces now use team-isolated queries to prevent request IDs from exposing request bodies, retrieved Knowledge Base chunks, and model responses across teams.
## 🐛 Bug Fixes ## 🐛 Bug Fixes
......
...@@ -12,8 +12,6 @@ description: 'FastGPT V4.15.0 更新说明' ...@@ -12,8 +12,6 @@ description: 'FastGPT V4.15.0 更新说明'
`fastgpt-app` 和 `fastgpt-pro` 本次升级需关注下面新增或变更语义的配置。同时需检查是否都包含 `AES256_SECRET_KEY` 且相等。 `fastgpt-app` 和 `fastgpt-pro` 本次升级需关注下面新增或变更语义的配置。同时需检查是否都包含 `AES256_SECRET_KEY` 且相等。
```dotenv ```dotenv
# 自动生成对话标题使用的模型
CHAT_TITLE_MODEL=deepseek-v4-flash
# 文件解析 worker 并发数 # 文件解析 worker 并发数
PARSE_FILE_WORKERS=10 PARSE_FILE_WORKERS=10
# 文件解析超时时间,单位秒 # 文件解析超时时间,单位秒
...@@ -97,6 +95,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \ ...@@ -97,6 +95,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \
1. ApiKey 功能调整,不再区分应用 key 和系统 key,只保留系统 key,如需兼容 openai sdk 用法,可使用 `apikey-appId` 的方式传递 Token。已有的 apikey 保持兼容,不影响使用。具体和查阅 [FastGPT API 文档说明](../../../openapi/intro) 1. ApiKey 功能调整,不再区分应用 key 和系统 key,只保留系统 key,如需兼容 openai sdk 用法,可使用 `apikey-appId` 的方式传递 Token。已有的 apikey 保持兼容,不影响使用。具体和查阅 [FastGPT API 文档说明](../../../openapi/intro)
2. 部分 API 增加了更为严格的数据格式校验,如过遇到报错: `zod parse error` 可提交 issue 反馈,可能因一些旧数据或者自定义数据结构与声明不一致。 2. 部分 API 增加了更为严格的数据格式校验,如过遇到报错: `zod parse error` 可提交 issue 反馈,可能因一些旧数据或者自定义数据结构与声明不一致。
3. LLM 请求追踪记录新增团队隔离:`llm_request_records` 写入 `teamId`,`GET /api/core/ai/record/getRecord` 按 `{ requestId, teamId }` 查询,唯一索引调整为 `{ teamId, requestId }`。升级前没有 `teamId` 的旧追踪记录将无法继续查询,页面会提示追踪记录已过期;如需排查历史调用详情,请在升级前导出相关日志或保留原始请求信息。自托管环境如关闭了 `SYNC_INDEX`,升级后需要执行一次索引同步,确保旧的 `requestId_1` 唯一索引被移除。
## 🚀 新增内容 ## 🚀 新增内容
...@@ -156,6 +155,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \ ...@@ -156,6 +155,7 @@ curl --location --request POST 'https://{{host}}/api/admin/initSandboxArchive' \
29. 登录页 UI 优化。 29. 登录页 UI 优化。
30. 站点同步限流错误提示去重。 30. 站点同步限流错误提示去重。
31. 应用/知识库增加虚拟列表渲染,优化大列表加载性能。 31. 应用/知识库增加虚拟列表渲染,优化大列表加载性能。
32. LLM 请求追踪记录增加团队隔离,避免 `requestId` 被跨团队用于读取请求体、知识库召回片段和模型响应。
## 🐛 修复 ## 🐛 修复
......
---
title: 'V4.15.0-beta6'
description: 'FastGPT V4.15.0-beta6 Release Notes'
---
## 📦 Upgrade Guide
### 1. Update the default model configuration
The chat title generation model is no longer configured through the `CHAT_TITLE_MODEL` environment variable. After upgrading, select the Chat Title Model in Model Configuration > Default Model Configuration. This setting can be left unset. When unset, FastGPT does not call a model to generate the title and uses a truncated user question instead.
If you previously configured `CHAT_TITLE_MODEL`, remove it from the `fastgpt` and `fastgpt-pro` environment variables, then select the corresponding model in the UI.
### 2. Add team isolation to LLM request traces
LLM request traces (`llm_request_records`) now include a `teamId` field. `GET /api/core/ai/record/getRecord` queries records by `{ requestId, teamId }` for the current team, preventing a `requestId` from being used to read another team's request body, retrieved Knowledge Base chunks, or model response.
The unique index on `llm_request_records` has also changed from the single `requestId` field to the compound unique index `{ teamId: 1, requestId: 1 }`. If your self-hosted deployment has `SYNC_INDEX` disabled, run an index sync after upgrading so the old `requestId_1` unique index is removed.
Risk: trace records written before this upgrade do not have `teamId`, so they can no longer be queried by `requestId` after the upgrade. The UI will treat them as expired. These records already have a TTL and are intended only for temporary debugging. Export the relevant logs or keep the original request details before upgrading if you need to investigate historical calls.
## ⚙️ Improvements
1. Chat title generation now uses the system default model configuration, making it easier to switch at runtime and manage consistently.
2. LLM request traces are now queried with team isolation, and the unique index is now `{ teamId, requestId }` to prevent request IDs from exposing sensitive traces across teams.
---
title: 'V4.15.0-beta6'
description: 'FastGPT V4.15.0-beta6 更新说明'
---
## 📦 升级指南
### 1. 修改默认模型配置
对话标题生成模型不再通过环境变量 `CHAT_TITLE_MODEL` 配置,升级后可在「模型配置」的「默认模型配置」中选择「对话标题模型」。该配置可以不设置,不设置时不会调用模型生成标题,仅使用用户问题截断作为标题。
如此前配置过 `CHAT_TITLE_MODEL`,升级后可从 `fastgpt` 和 `fastgpt-pro` 的环境变量中移除,并在页面中重新选择对应模型。
### 2. LLM 请求追踪记录增加团队隔离
LLM 请求追踪记录(`llm_request_records`)新增 `teamId` 字段,`GET /api/core/ai/record/getRecord` 会按当前登录团队查询 `{ requestId, teamId }`,避免仅凭 `requestId` 读取其他团队的请求体、知识库召回片段和模型响应。
同时,`llm_request_records` 的唯一索引从单字段 `requestId` 调整为复合唯一索引 `{ teamId: 1, requestId: 1 }`。如自托管环境关闭了 `SYNC_INDEX`,升级后需要执行一次索引同步,确保旧的 `requestId_1` 唯一索引被移除。
风险点:升级前已写入的旧追踪记录没有 `teamId`,升级后将无法再通过 `requestId` 查询,页面会按追踪记录已过期处理。该记录本身有 TTL,仅用于临时排查模型调用详情;如需排查历史问题,请在升级前导出相关日志或保留原始请求信息。
## ⚙️ 优化
1. 对话标题生成模型改为使用系统默认模型配置管理,便于运行时切换和统一维护。
2. LLM 请求追踪记录按团队隔离查询,唯一索引调整为 `{ teamId, requestId }`,避免 `requestId` 被其他团队复用读取敏感 trace。
{ {
"title": "4.15.x", "title": "4.15.x",
"description": "", "description": "",
"pages": ["41500", "41505", "41504", "41503", "41502", "41501"] "pages": ["41500", "41506", "41505", "41504", "41503", "41502", "41501"]
} }
{ {
"title": "4.15.x", "title": "4.15.x",
"description": "", "description": "",
"pages": ["41500", "41505", "41504", "41503", "41502", "41501"] "pages": ["41500", "41506", "41505", "41504", "41503", "41502", "41501"]
} }
...@@ -152,6 +152,7 @@ description: FastGPT Toc ...@@ -152,6 +152,7 @@ description: FastGPT Toc
- [/en/self-host/upgrading/4-15/41503](/en/self-host/upgrading/4-15/41503) - [/en/self-host/upgrading/4-15/41503](/en/self-host/upgrading/4-15/41503)
- [/en/self-host/upgrading/4-15/41504](/en/self-host/upgrading/4-15/41504) - [/en/self-host/upgrading/4-15/41504](/en/self-host/upgrading/4-15/41504)
- [/en/self-host/upgrading/4-15/41505](/en/self-host/upgrading/4-15/41505) - [/en/self-host/upgrading/4-15/41505](/en/self-host/upgrading/4-15/41505)
- [/en/self-host/upgrading/4-15/41506](/en/self-host/upgrading/4-15/41506)
- [/en/self-host/upgrading/outdated/40](/en/self-host/upgrading/outdated/40) - [/en/self-host/upgrading/outdated/40](/en/self-host/upgrading/outdated/40)
- [/en/self-host/upgrading/outdated/41](/en/self-host/upgrading/outdated/41) - [/en/self-host/upgrading/outdated/41](/en/self-host/upgrading/outdated/41)
- [/en/self-host/upgrading/outdated/4100](/en/self-host/upgrading/outdated/4100) - [/en/self-host/upgrading/outdated/4100](/en/self-host/upgrading/outdated/4100)
......
...@@ -155,6 +155,7 @@ description: FastGPT 文档目录 ...@@ -155,6 +155,7 @@ description: FastGPT 文档目录
- [/self-host/upgrading/4-15/41503](/self-host/upgrading/4-15/41503) - [/self-host/upgrading/4-15/41503](/self-host/upgrading/4-15/41503)
- [/self-host/upgrading/4-15/41504](/self-host/upgrading/4-15/41504) - [/self-host/upgrading/4-15/41504](/self-host/upgrading/4-15/41504)
- [/self-host/upgrading/4-15/41505](/self-host/upgrading/4-15/41505) - [/self-host/upgrading/4-15/41505](/self-host/upgrading/4-15/41505)
- [/self-host/upgrading/4-15/41506](/self-host/upgrading/4-15/41506)
- [/self-host/upgrading/outdated/40](/self-host/upgrading/outdated/40) - [/self-host/upgrading/outdated/40](/self-host/upgrading/outdated/40)
- [/self-host/upgrading/outdated/41](/self-host/upgrading/outdated/41) - [/self-host/upgrading/outdated/41](/self-host/upgrading/outdated/41)
- [/self-host/upgrading/outdated/4100](/self-host/upgrading/outdated/4100) - [/self-host/upgrading/outdated/4100](/self-host/upgrading/outdated/4100)
......
...@@ -167,8 +167,8 @@ ...@@ -167,8 +167,8 @@
"content/plugin/model-presets.mdx": "2026-06-04T16:10:15+08:00", "content/plugin/model-presets.mdx": "2026-06-04T16:10:15+08:00",
"content/plugin/system-tool-development.en.mdx": "2026-06-09T16:03:58+08:00", "content/plugin/system-tool-development.en.mdx": "2026-06-09T16:03:58+08:00",
"content/plugin/system-tool-development.mdx": "2026-06-09T16:03:58+08:00", "content/plugin/system-tool-development.mdx": "2026-06-09T16:03:58+08:00",
"content/self-host/config/env.en.mdx": "2026-06-23T21:09:39+08:00", "content/self-host/config/env.en.mdx": "2026-06-25T11:22:10+08:00",
"content/self-host/config/env.mdx": "2026-06-23T21:09:39+08:00", "content/self-host/config/env.mdx": "2026-06-25T11:22:10+08:00",
"content/self-host/config/json.en.mdx": "2026-06-22T11:01:59+08:00", "content/self-host/config/json.en.mdx": "2026-06-22T11:01:59+08:00",
"content/self-host/config/json.mdx": "2026-06-22T11:01:59+08:00", "content/self-host/config/json.mdx": "2026-06-22T11:01:59+08:00",
"content/self-host/config/model/intro.en.mdx": "2026-06-04T16:10:15+08:00", "content/self-host/config/model/intro.en.mdx": "2026-06-04T16:10:15+08:00",
...@@ -271,6 +271,8 @@ ...@@ -271,6 +271,8 @@
"content/self-host/upgrading/4-14/41424.mdx": "2026-06-10T17:33:23+08:00", "content/self-host/upgrading/4-14/41424.mdx": "2026-06-10T17:33:23+08:00",
"content/self-host/upgrading/4-14/41425.en.mdx": "2026-06-22T12:20:51+08:00", "content/self-host/upgrading/4-14/41425.en.mdx": "2026-06-22T12:20:51+08:00",
"content/self-host/upgrading/4-14/41425.mdx": "2026-06-22T12:20:51+08:00", "content/self-host/upgrading/4-14/41425.mdx": "2026-06-22T12:20:51+08:00",
"content/self-host/upgrading/4-14/41426.en.mdx": "2026-06-24T23:05:29+08:00",
"content/self-host/upgrading/4-14/41426.mdx": "2026-06-24T23:05:29+08:00",
"content/self-host/upgrading/4-14/4143.en.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/4-14/4143.en.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/4-14/4143.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/4-14/4143.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/4-14/4144.en.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/4-14/4144.en.mdx": "2026-04-26T21:08:47+08:00",
...@@ -289,8 +291,8 @@ ...@@ -289,8 +291,8 @@
"content/self-host/upgrading/4-14/41481.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/4-14/41481.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/4-14/4149.en.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/4-14/4149.en.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/4-14/4149.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/4-14/4149.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/4-15/41500.en.mdx": "2026-06-23T13:54:06+08:00", "content/self-host/upgrading/4-15/41500.en.mdx": "2026-06-25T11:22:10+08:00",
"content/self-host/upgrading/4-15/41500.mdx": "2026-06-23T13:54:06+08:00", "content/self-host/upgrading/4-15/41500.mdx": "2026-06-25T11:22:10+08:00",
"content/self-host/upgrading/4-15/41501.mdx": "2026-06-23T21:09:39+08:00", "content/self-host/upgrading/4-15/41501.mdx": "2026-06-23T21:09:39+08:00",
"content/self-host/upgrading/4-15/41502.en.mdx": "2026-05-25T11:21:30+08:00", "content/self-host/upgrading/4-15/41502.en.mdx": "2026-05-25T11:21:30+08:00",
"content/self-host/upgrading/4-15/41502.mdx": "2026-06-23T13:54:06+08:00", "content/self-host/upgrading/4-15/41502.mdx": "2026-06-23T13:54:06+08:00",
...@@ -300,6 +302,8 @@ ...@@ -300,6 +302,8 @@
"content/self-host/upgrading/4-15/41504.mdx": "2026-06-15T23:34:43+08:00", "content/self-host/upgrading/4-15/41504.mdx": "2026-06-15T23:34:43+08:00",
"content/self-host/upgrading/4-15/41505.en.mdx": "2026-06-23T13:54:06+08:00", "content/self-host/upgrading/4-15/41505.en.mdx": "2026-06-23T13:54:06+08:00",
"content/self-host/upgrading/4-15/41505.mdx": "2026-06-24T13:53:05+08:00", "content/self-host/upgrading/4-15/41505.mdx": "2026-06-24T13:53:05+08:00",
"content/self-host/upgrading/4-15/41506.en.mdx": "2026-06-25T11:22:10+08:00",
"content/self-host/upgrading/4-15/41506.mdx": "2026-06-25T11:22:10+08:00",
"content/self-host/upgrading/outdated/40.en.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/outdated/40.en.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/outdated/40.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/outdated/40.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/outdated/41.en.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/outdated/41.en.mdx": "2026-04-26T21:08:47+08:00",
...@@ -440,6 +444,6 @@ ...@@ -440,6 +444,6 @@
"content/self-host/upgrading/outdated/499.mdx": "2026-05-07T15:06:40+08:00", "content/self-host/upgrading/outdated/499.mdx": "2026-05-07T15:06:40+08:00",
"content/self-host/upgrading/upgrade-intruction.en.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/upgrade-intruction.en.mdx": "2026-04-26T21:08:47+08:00",
"content/self-host/upgrading/upgrade-intruction.mdx": "2026-04-26T21:08:47+08:00", "content/self-host/upgrading/upgrade-intruction.mdx": "2026-04-26T21:08:47+08:00",
"content/toc.en.mdx": "2026-06-24T18:16:41+08:00", "content/toc.en.mdx": "2026-06-25T11:22:10+08:00",
"content/toc.mdx": "2026-06-24T18:16:41+08:00" "content/toc.mdx": "2026-06-25T11:22:10+08:00"
} }
\ No newline at end of file
...@@ -50,7 +50,6 @@ const BaseModelItemSchema = z.object({ ...@@ -50,7 +50,6 @@ const BaseModelItemSchema = z.object({
// Test mode: when enabled, classify/extract/tool call/evaluation scenarios are disabled // Test mode: when enabled, classify/extract/tool call/evaluation scenarios are disabled
testMode: z.boolean().optional() // test mode flag testMode: z.boolean().optional() // test mode flag
}); });
type BaseModelItemType = z.infer<typeof BaseModelItemSchema>;
export const LLMModelItemSchema = PriceTypeSchema.extend(BaseModelItemSchema.shape).extend({ export const LLMModelItemSchema = PriceTypeSchema.extend(BaseModelItemSchema.shape).extend({
type: z.literal(ModelTypeEnum.llm), type: z.literal(ModelTypeEnum.llm),
...@@ -81,6 +80,7 @@ export const LLMModelItemSchema = PriceTypeSchema.extend(BaseModelItemSchema.sha ...@@ -81,6 +80,7 @@ export const LLMModelItemSchema = PriceTypeSchema.extend(BaseModelItemSchema.sha
// LLM // LLM
isDefaultDatasetTextModel: z.boolean().optional(), isDefaultDatasetTextModel: z.boolean().optional(),
isDefaultDatasetImageModel: z.boolean().optional(), isDefaultDatasetImageModel: z.boolean().optional(),
isDefaultChatTitleModel: z.boolean().optional(),
isDefaultHelperBotModel: z.boolean().optional(), isDefaultHelperBotModel: z.boolean().optional(),
/** @deprecated */ /** @deprecated */
......
...@@ -16,6 +16,10 @@ export type GetLLMRequestRecordParamsType = z.infer<typeof GetLLMRequestRecordPa ...@@ -16,6 +16,10 @@ export type GetLLMRequestRecordParamsType = z.infer<typeof GetLLMRequestRecordPa
// Response // Response
export const LLMRequestRecordSchema = z.object({ export const LLMRequestRecordSchema = z.object({
_id: ObjectIdSchema, _id: ObjectIdSchema,
teamId: ObjectIdSchema.meta({
example: '60f6b3b3b3b3b3b3b3b3b3b3',
description: '所属团队 ID'
}),
requestId: z.string().meta({ requestId: z.string().meta({
example: 'V1StGXR8_Z5jdHi6B-myT', example: 'V1StGXR8_Z5jdHi6B-myT',
description: '请求追踪 ID' description: '请求追踪 ID'
......
...@@ -77,6 +77,9 @@ export const loadSystemModels = async (init = false, language = 'en') => { ...@@ -77,6 +77,9 @@ export const loadSystemModels = async (init = false, language = 'en') => {
if (model.isDefaultDatasetImageModel) { if (model.isDefaultDatasetImageModel) {
_systemDefaultModel.datasetImageLLM = model; _systemDefaultModel.datasetImageLLM = model;
} }
if (model.isDefaultChatTitleModel) {
_systemDefaultModel.chatTitleLLM = model;
}
if (model.model === serviceEnv.HELPER_BOT_MODEL) { if (model.model === serviceEnv.HELPER_BOT_MODEL) {
_systemDefaultModel.helperBotLLM = model; _systemDefaultModel.helperBotLLM = model;
} }
...@@ -270,7 +273,7 @@ export const watchSystemModelUpdate = () => { ...@@ -270,7 +273,7 @@ export const watchSystemModelUpdate = () => {
await loadSystemModels(true); await loadSystemModels(true);
// All node reaload buffer // All node reaload buffer
await reloadFastGPTConfigBuffer(); await reloadFastGPTConfigBuffer();
} catch (error) {} } catch {}
}, 500) }, 500)
); );
}; };
......
...@@ -13,11 +13,13 @@ const logger = getLogger(LogCategories.MODULE.AI.FUNCTIONS); ...@@ -13,11 +13,13 @@ const logger = getLogger(LogCategories.MODULE.AI.FUNCTIONS);
export async function createQuestionGuide({ export async function createQuestionGuide({
messages, messages,
model, model,
customPrompt customPrompt,
teamId
}: { }: {
messages: ChatCompletionMessageParam[]; messages: ChatCompletionMessageParam[];
model: string; model: string;
customPrompt?: string; customPrompt?: string;
teamId: string;
}): Promise<{ }): Promise<{
result: string[]; result: string[];
inputTokens: number; inputTokens: number;
...@@ -36,6 +38,8 @@ export async function createQuestionGuide({ ...@@ -36,6 +38,8 @@ export async function createQuestionGuide({
answerText: answer, answerText: answer,
usage: { inputTokens, outputTokens } usage: { inputTokens, outputTokens }
} = await createLLMResponse({ } = await createLLMResponse({
teamId,
saveLLMResponseRecord: false,
body: { body: {
model, model,
messages: concatMessages, messages: concatMessages,
......
...@@ -112,6 +112,7 @@ export const queryExtension = async ({ ...@@ -112,6 +112,7 @@ export const queryExtension = async ({
llmModel, llmModel,
embeddingModel, embeddingModel,
userKey, userKey,
teamId,
generateCount = 10 // 生成优化问题集的数量,默认为10个 generateCount = 10 // 生成优化问题集的数量,默认为10个
}: { }: {
chatBg?: string; chatBg?: string;
...@@ -120,6 +121,7 @@ export const queryExtension = async ({ ...@@ -120,6 +121,7 @@ export const queryExtension = async ({
llmModel: string; llmModel: string;
embeddingModel: string; embeddingModel: string;
userKey?: OpenaiAccountType; userKey?: OpenaiAccountType;
teamId: string;
generateCount?: number; generateCount?: number;
}): Promise<{ }): Promise<{
rawQuery: string; rawQuery: string;
...@@ -178,6 +180,7 @@ export const queryExtension = async ({ ...@@ -178,6 +180,7 @@ export const queryExtension = async ({
usage: { inputTokens, outputTokens, usedUserOpenAIKey } usage: { inputTokens, outputTokens, usedUserOpenAIKey }
} = await createLLMResponse({ } = await createLLMResponse({
userKey, userKey,
teamId,
body: { body: {
stream: true, stream: true,
model: modelData.model, model: modelData.model,
......
...@@ -38,6 +38,7 @@ type RunAgentCallProps<TChildrenResponse = unknown> = { ...@@ -38,6 +38,7 @@ type RunAgentCallProps<TChildrenResponse = unknown> = {
usagePush: (usages: ChatNodeUsageType[]) => void; usagePush: (usages: ChatNodeUsageType[]) => void;
isAborted: CreateLLMResponseProps['isAborted']; isAborted: CreateLLMResponseProps['isAborted'];
userKey?: CreateLLMResponseProps['userKey']; userKey?: CreateLLMResponseProps['userKey'];
teamId: string;
childrenInteractiveParams?: AgentLoopChildrenInteractiveParams<TChildrenResponse>; childrenInteractiveParams?: AgentLoopChildrenInteractiveParams<TChildrenResponse>;
// LLM 压缩后回调 // LLM 压缩后回调
...@@ -55,6 +56,7 @@ type RunAgentCallProps<TChildrenResponse = unknown> = { ...@@ -55,6 +56,7 @@ type RunAgentCallProps<TChildrenResponse = unknown> = {
onRunTool: (e: { onRunTool: (e: {
call: ChatCompletionMessageToolCall; call: ChatCompletionMessageToolCall;
messages: ChatCompletionMessageParam[]; messages: ChatCompletionMessageParam[];
assistantMessage?: ChatCompletionMessageParam;
}) => Promise<AgentLoopToolExecutionResult<TChildrenResponse>>; }) => Promise<AgentLoopToolExecutionResult<TChildrenResponse>>;
// 模型准备以无工具调用结束时的本地停止检查。 // 模型准备以无工具调用结束时的本地停止检查。
// 返回 feedbackMessage 时会把消息追加回同一个 loop,而不是让外层重新开一层循环。 // 返回 feedbackMessage 时会把消息追加回同一个 loop,而不是让外层重新开一层循环。
...@@ -129,7 +131,8 @@ export const onCompressContext = async ({ ...@@ -129,7 +131,8 @@ export const onCompressContext = async ({
modelData, modelData,
reasoningEffort, reasoningEffort,
tools, tools,
userKey userKey,
teamId
}: { }: {
isAborted: RunAgentCallProps['isAborted']; isAborted: RunAgentCallProps['isAborted'];
messageTokens?: number; messageTokens?: number;
...@@ -138,6 +141,7 @@ export const onCompressContext = async ({ ...@@ -138,6 +141,7 @@ export const onCompressContext = async ({
reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort']; reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort'];
tools?: ChatCompletionTool[]; tools?: ChatCompletionTool[];
userKey: RunAgentCallProps['userKey']; userKey: RunAgentCallProps['userKey'];
teamId: RunAgentCallProps['teamId'];
}) => { }) => {
const compressStartTime = Date.now(); const compressStartTime = Date.now();
const result = await compressRequestMessages({ const result = await compressRequestMessages({
...@@ -147,7 +151,8 @@ export const onCompressContext = async ({ ...@@ -147,7 +151,8 @@ export const onCompressContext = async ({
model: modelData, model: modelData,
reasoningEffort, reasoningEffort,
tools, tools,
userKey userKey,
teamId
}); });
if (result.usage || result.contextCheckpoint || result.messages !== requestMessages) { if (result.usage || result.contextCheckpoint || result.messages !== requestMessages) {
return { return {
...@@ -184,6 +189,7 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -184,6 +189,7 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
body: { model, messages, max_tokens, ...body }, body: { model, messages, max_tokens, ...body },
userKey, userKey,
teamId,
usagePush, usagePush,
isAborted, isAborted,
...@@ -322,7 +328,8 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -322,7 +328,8 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
modelData, modelData,
reasoningEffort: body.reasoning_effort, reasoningEffort: body.reasoning_effort,
tools: body.tools, tools: body.tools,
userKey userKey,
teamId
}); });
if (compressResult) { if (compressResult) {
requestMessagesTokenCount = compressResult.messageTokens ?? requestMessagesTokenCount; requestMessagesTokenCount = compressResult.messageTokens ?? requestMessagesTokenCount;
...@@ -378,6 +385,7 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -378,6 +385,7 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
parallel_tool_calls: body.parallel_tool_calls ?? true parallel_tool_calls: body.parallel_tool_calls ?? true
}, },
userKey, userKey,
teamId,
isAborted, isAborted,
onReasoning, onReasoning,
onStreaming, onStreaming,
...@@ -477,7 +485,8 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -477,7 +485,8 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
try { try {
return await onRunTool({ return await onRunTool({
call: tool, call: tool,
messages: cloneRequestMessages messages: cloneRequestMessages,
assistantMessage: llmAssistantMessage
}); });
} catch (error) { } catch (error) {
toolErrorMessage = `Tool error: ${getErrText(error)}`; toolErrorMessage = `Tool error: ${getErrText(error)}`;
...@@ -506,7 +515,8 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -506,7 +515,8 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
response, response,
model: modelData, model: modelData,
reasoningEffort: body.reasoning_effort, reasoningEffort: body.reasoning_effort,
userKey userKey,
teamId
}); });
const { compressed: compressed_context, usage: compressionUsage } = compressionResult; const { compressed: compressed_context, usage: compressionUsage } = compressionResult;
const normalizedCompressedContext = normalizeToolResponseContent(compressed_context); const normalizedCompressedContext = normalizeToolResponseContent(compressed_context);
......
...@@ -100,6 +100,7 @@ export type AgentLoopToolExecutionResult<TChildrenResponse = unknown> = { ...@@ -100,6 +100,7 @@ export type AgentLoopToolExecutionResult<TChildrenResponse = unknown> = {
}; };
export type AgentLoopRuntime = { export type AgentLoopRuntime = {
teamId: string;
model: string; model: string;
reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort']; reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort'];
userKey?: CreateLLMResponseProps['userKey']; userKey?: CreateLLMResponseProps['userKey'];
...@@ -116,6 +117,7 @@ export type AgentLoopRuntime = { ...@@ -116,6 +117,7 @@ export type AgentLoopRuntime = {
executeTool: (e: { executeTool: (e: {
call: ChatCompletionMessageToolCall; call: ChatCompletionMessageToolCall;
messages: ChatCompletionMessageParam[]; messages: ChatCompletionMessageParam[];
assistantMessage?: ChatCompletionMessageParam;
}) => Promise<AgentLoopToolExecutionResult>; }) => Promise<AgentLoopToolExecutionResult>;
emitEvent?: (event: AgentLoopEvent) => void; emitEvent?: (event: AgentLoopEvent) => void;
usageSink?: (usages: ChatNodeUsageType[]) => void; usageSink?: (usages: ChatNodeUsageType[]) => void;
......
...@@ -85,28 +85,45 @@ const buildInitialMessages = ({ ...@@ -85,28 +85,45 @@ const buildInitialMessages = ({
const buildAskPendingContext = ({ const buildAskPendingContext = ({
messages, messages,
call, call,
assistantMessage,
activePlan, activePlan,
requirePlan, requirePlan,
runtimeToolCalledSinceLastPlanUpdate runtimeToolCalledSinceLastPlanUpdate
}: { }: {
messages: ChatCompletionMessageParam[]; messages: ChatCompletionMessageParam[];
call: ChatCompletionMessageToolCall; call: ChatCompletionMessageToolCall;
assistantMessage?: ChatCompletionMessageParam;
activePlan?: AgentPlanType; activePlan?: AgentPlanType;
requirePlan?: boolean; requirePlan?: boolean;
runtimeToolCalledSinceLastPlanUpdate?: boolean; runtimeToolCalledSinceLastPlanUpdate?: boolean;
}): PendingMainContext => ({ }): PendingMainContext => {
messages: [ const assistantFields =
...messages, assistantMessage?.role === ChatCompletionRequestMessageRoleEnum.Assistant
{ ? {
role: ChatCompletionRequestMessageRoleEnum.Assistant, ...(('content' in assistantMessage && assistantMessage.content
tool_calls: [call] ? { content: assistantMessage.content }
} : {}) as Pick<typeof assistantMessage, 'content'>),
], ...(assistantMessage.reasoning_content
askToolCallId: call.id, ? { reasoning_content: assistantMessage.reasoning_content }
activePlan, : {})
requirePlan, }
runtimeToolCalledSinceLastPlanUpdate : {};
});
return {
messages: [
...messages,
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
...assistantFields,
tool_calls: [call]
}
],
askToolCallId: call.id,
activePlan,
requirePlan,
runtimeToolCalledSinceLastPlanUpdate
};
};
/** /**
* 单主 Agent Loop。 * 单主 Agent Loop。
...@@ -189,6 +206,7 @@ export const runUnifiedAgentLoop = async ({ ...@@ -189,6 +206,7 @@ export const runUnifiedAgentLoop = async ({
}), }),
parallel_tool_calls: true parallel_tool_calls: true
}, },
teamId: runtime.teamId,
userKey: runtime.userKey, userKey: runtime.userKey,
usagePush: (usages) => runtime.usageSink?.(usages), usagePush: (usages) => runtime.usageSink?.(usages),
isAborted: runtime.checkIsStopping, isAborted: runtime.checkIsStopping,
...@@ -258,7 +276,7 @@ export const runUnifiedAgentLoop = async ({ ...@@ -258,7 +276,7 @@ export const runUnifiedAgentLoop = async ({
callId: call.id, callId: call.id,
argsDelta argsDelta
}), }),
onRunTool: async ({ call, messages }) => { onRunTool: async ({ call, messages, assistantMessage }) => {
// 先特殊处理系统级别工具 // 先特殊处理系统级别工具
if (call.function.name === askToolName) { if (call.function.name === askToolName) {
const parsed = parsePlanAskToolCall(call); const parsed = parsePlanAskToolCall(call);
...@@ -271,6 +289,7 @@ export const runUnifiedAgentLoop = async ({ ...@@ -271,6 +289,7 @@ export const runUnifiedAgentLoop = async ({
context: buildAskPendingContext({ context: buildAskPendingContext({
messages, messages,
call, call,
assistantMessage,
activePlan, activePlan,
requirePlan, requirePlan,
runtimeToolCalledSinceLastPlanUpdate runtimeToolCalledSinceLastPlanUpdate
......
...@@ -514,7 +514,8 @@ export const compressRequestMessages = async ({ ...@@ -514,7 +514,8 @@ export const compressRequestMessages = async ({
model, model,
reasoningEffort, reasoningEffort,
tools, tools,
userKey userKey,
teamId
}: { }: {
checkIsStopping?: CreateLLMResponseProps['isAborted']; checkIsStopping?: CreateLLMResponseProps['isAborted'];
messageTokens?: number; messageTokens?: number;
...@@ -523,6 +524,7 @@ export const compressRequestMessages = async ({ ...@@ -523,6 +524,7 @@ export const compressRequestMessages = async ({
reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort']; reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort'];
tools?: ChatCompletionTool[]; tools?: ChatCompletionTool[];
userKey?: OpenaiAccountType; userKey?: OpenaiAccountType;
teamId: string;
}): Promise<{ }): Promise<{
messages: ChatCompletionMessageParam[]; messages: ChatCompletionMessageParam[];
messageTokens?: number; messageTokens?: number;
...@@ -600,6 +602,7 @@ export const compressRequestMessages = async ({ ...@@ -600,6 +602,7 @@ export const compressRequestMessages = async ({
throwError: false, throwError: false,
isAborted: checkIsStopping, isAborted: checkIsStopping,
userKey, userKey,
teamId,
body: { body: {
stream: true, stream: true,
model, model,
...@@ -747,7 +750,8 @@ export const compressLargeContent = async ({ ...@@ -747,7 +750,8 @@ export const compressLargeContent = async ({
compressedTokenLimit, compressedTokenLimit,
moduleName = i18nT('account_usage:llm_compress_text'), moduleName = i18nT('account_usage:llm_compress_text'),
reasoningEffort, reasoningEffort,
userKey userKey,
teamId
}: { }: {
content: string; content: string;
model: LLMModelItemType; model: LLMModelItemType;
...@@ -755,6 +759,7 @@ export const compressLargeContent = async ({ ...@@ -755,6 +759,7 @@ export const compressLargeContent = async ({
moduleName?: string; moduleName?: string;
reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort']; reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort'];
userKey?: OpenaiAccountType; userKey?: OpenaiAccountType;
teamId: string;
}): Promise<{ }): Promise<{
compressed: string; compressed: string;
usage?: ChatNodeUsageType; usage?: ChatNodeUsageType;
...@@ -804,6 +809,8 @@ export const compressLargeContent = async ({ ...@@ -804,6 +809,8 @@ export const compressLargeContent = async ({
const { answerText, usage, requestId } = await createLLMResponse({ const { answerText, usage, requestId } = await createLLMResponse({
throwError: false, throwError: false,
userKey, userKey,
teamId,
saveLLMResponseRecord: false,
body: { body: {
model, model,
messages: [ messages: [
...@@ -1081,12 +1088,14 @@ export const compressToolResponse = async ({ ...@@ -1081,12 +1088,14 @@ export const compressToolResponse = async ({
response, response,
model, model,
reasoningEffort, reasoningEffort,
userKey userKey,
teamId
}: { }: {
response: string; response: string;
model: LLMModelItemType; model: LLMModelItemType;
reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort']; reasoningEffort?: CreateLLMResponseProps['body']['reasoning_effort'];
userKey?: OpenaiAccountType; userKey?: OpenaiAccountType;
teamId: string;
}): Promise<{ }): Promise<{
compressed: string; compressed: string;
usage?: ChatNodeUsageType; usage?: ChatNodeUsageType;
...@@ -1131,7 +1140,8 @@ export const compressToolResponse = async ({ ...@@ -1131,7 +1140,8 @@ export const compressToolResponse = async ({
compressedTokenLimit: llmCompressedTokenLimit, compressedTokenLimit: llmCompressedTokenLimit,
moduleName: i18nT('account_usage:tool_response_compress'), moduleName: i18nT('account_usage:tool_response_compress'),
reasoningEffort, reasoningEffort,
userKey userKey,
teamId
}); });
const compressedTokens = await countPromptTokens(result.compressed); const compressedTokens = await countPromptTokens(result.compressed);
......
...@@ -7,6 +7,7 @@ import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/co ...@@ -7,6 +7,7 @@ import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/co
import { getLogger, LogCategories } from '../../../../common/logger'; import { getLogger, LogCategories } from '../../../../common/logger';
import { countGptMessagesTokens } from '../../../../common/string/tiktoken/index'; import { countGptMessagesTokens } from '../../../../common/string/tiktoken/index';
import { i18nT } from '@fastgpt/global/common/i18n/utils'; import { i18nT } from '@fastgpt/global/common/i18n/utils';
import { mergeAssistantFieldMessages } from '@fastgpt/global/core/chat/adapt';
import { getLLMModel } from '../../model'; import { getLLMModel } from '../../model';
import { promptToolCallMessageRewrite } from '../promptCall'; import { promptToolCallMessageRewrite } from '../promptCall';
import { loadRequestMessages } from '../utils'; import { loadRequestMessages } from '../utils';
...@@ -45,7 +46,8 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -45,7 +46,8 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
custonHeaders, custonHeaders,
userKey, userKey,
maxContinuations = 1, maxContinuations = 1,
saveLLMResponseRecord = true saveLLMResponseRecord = true,
teamId
} = args; } = args;
const { messages, useVision, useAudio, useVideo, extractFiles, tools, toolCallMode } = body; const { messages, useVision, useAudio, useVideo, extractFiles, tools, toolCallMode } = body;
const model = getLLMModel(body.model); const model = getLLMModel(body.model);
...@@ -72,11 +74,15 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -72,11 +74,15 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
messages: rewriteMessages messages: rewriteMessages
}); });
const accumulator = useLLMResponseAccumulator(); const accumulator = useLLMResponseAccumulator();
let currentMessages = [...requestBody.messages]; const initialRequestMessages = mergeAssistantFieldMessages(
requestBody.messages as ChatCompletionMessageParam[]
) as ChatCompletionMessageParam[];
let currentMessages: ChatCompletionMessageParam[] = [...initialRequestMessages];
let continuationCount = 0; let continuationCount = 0;
let aiRequestMeta: AIApiRequestMeta = { let aiRequestMeta: AIApiRequestMeta = {
usedUserOpenAIKey: false usedUserOpenAIKey: false
}; };
let hasSavedLLMRequestRecord = false;
try { try {
// 自带“继续”的循环:finish_reason=length 时追加已生成内容后继续请求。 // 自带“继续”的循环:finish_reason=length 时追加已生成内容后继续请求。
...@@ -88,7 +94,7 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -88,7 +94,7 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
} = await createChatCompletion({ } = await createChatCompletion({
body: { body: {
...requestBody, ...requestBody,
messages: currentMessages messages: currentMessages as typeof requestBody.messages
}, },
modelData, modelData,
userKey, userKey,
...@@ -102,7 +108,6 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -102,7 +108,6 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
aiRequestMeta = requestMeta; aiRequestMeta = requestMeta;
// 连续输出补偿请求可能多次进入循环,但本次调用对外只认第一次响应形态。 // 连续输出补偿请求可能多次进入循环,但本次调用对外只认第一次响应形态。
accumulator.setFirstResponseType(currentIsStreamResponse); accumulator.setFirstResponseType(currentIsStreamResponse);
const parsedResponse = await (async () => { const parsedResponse = await (async () => {
if (currentIsStreamResponse) { if (currentIsStreamResponse) {
return createStreamResponse({ return createStreamResponse({
...@@ -129,9 +134,11 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -129,9 +134,11 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
if (accumulator.shouldContinue()) { if (accumulator.shouldContinue()) {
// 继续输出时,用原始 requestBody.messages 做基础,避免把 rewrite 前 messages 混回上下文。 // 继续输出时,用原始 requestBody.messages 做基础,避免把 rewrite 前 messages 混回上下文。
currentMessages = accumulator.buildContinuationMessages({ currentMessages = mergeAssistantFieldMessages(
baseMessages: requestBody.messages as ChatCompletionMessageParam[] accumulator.buildContinuationMessages({
}) as typeof currentMessages; baseMessages: requestBody.messages as ChatCompletionMessageParam[]
})
) as ChatCompletionMessageParam[];
logger.debug(`Continue LLM response due to length limit`, { logger.debug(`Continue LLM response due to length limit`, {
continuationCount, continuationCount,
...@@ -210,9 +217,10 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -210,9 +217,10 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
(finish_reason === 'stop' || !finish_reason || isEmptyToolCallsFinish); (finish_reason === 'stop' || !finish_reason || isEmptyToolCallsFinish);
const responseEmptyTip = isNotResponse ? getEmptyResponseTip() : undefined; const responseEmptyTip = isNotResponse ? getEmptyResponseTip() : undefined;
if (saveLLMResponseRecord) { if (saveLLMResponseRecord && teamId) {
// 保存详情只记录实际请求与模型响应,不把用于计费分支的 usedUserOpenAIKey 写入详情。 // 保存详情只记录实际请求与模型响应,不把用于计费分支的 usedUserOpenAIKey 写入详情。
persistLLMResponseRecord({ persistLLMResponseRecord({
teamId,
requestId, requestId,
requestBody: requestBody as ChatCompletionCreateParams, requestBody: requestBody as ChatCompletionCreateParams,
answerText, answerText,
...@@ -224,6 +232,7 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -224,6 +232,7 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
outputTokens, outputTokens,
error: error ?? responseEmptyTip error: error ?? responseEmptyTip
}); });
hasSavedLLMRequestRecord = true;
} }
if (error && throwError) { if (error && throwError) {
...@@ -246,14 +255,16 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -246,14 +255,16 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
rawUsage: usage, rawUsage: usage,
requestId, requestId,
requestMessages, requestMessages: initialRequestMessages,
assistantMessage, assistantMessage,
completeMessages: [...requestMessages, assistantMessage] completeMessages: [...initialRequestMessages, assistantMessage]
}; };
} catch (error) { } catch (error) {
// createChatCompletion 抛错或解析阶段抛错都会落到这里,保证 requestId 对应的错误详情被保存。 // createChatCompletion 抛错或解析阶段抛错都会落到这里,保证 requestId 对应的错误详情被保存。
if (saveLLMResponseRecord) { // 如果已拿到模型响应并保存过包含 error 的详情,throwError 再抛出时不重复写同一个 requestId。
if (saveLLMResponseRecord && teamId && !hasSavedLLMRequestRecord) {
persistLLMErrorRecord({ persistLLMErrorRecord({
teamId,
requestId, requestId,
requestBody: requestBody as ChatCompletionCreateParams, requestBody: requestBody as ChatCompletionCreateParams,
error error
...@@ -276,8 +287,8 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>( ...@@ -276,8 +287,8 @@ export const createLLMResponse = async <T extends ChatCompletionCreateParams>(
outputTokens: 0, outputTokens: 0,
usedUserOpenAIKey: false usedUserOpenAIKey: false
}, },
requestMessages: requestBody.messages as ChatCompletionMessageParam[], requestMessages: initialRequestMessages,
completeMessages: [...requestBody.messages] as ChatCompletionMessageParam[] completeMessages: [...initialRequestMessages]
}; };
} }
}; };
...@@ -95,18 +95,11 @@ export const useLLMResponseAccumulator = () => { ...@@ -95,18 +95,11 @@ export const useLLMResponseAccumulator = () => {
}): ChatCompletionMessageParam[] { }): ChatCompletionMessageParam[] {
return [ return [
...baseMessages, ...baseMessages,
...(toolCalls
? [
{
role: ChatCompletionRequestMessageRoleEnum.Assistant as 'assistant',
tool_calls: toolCalls
}
]
: []),
{ {
role: ChatCompletionRequestMessageRoleEnum.Assistant as 'assistant', role: ChatCompletionRequestMessageRoleEnum.Assistant as 'assistant',
...(answerText && { content: answerText }), ...(answerText && { content: answerText }),
...(reasoningText && { reasoning_content: reasoningText }) ...(reasoningText && { reasoning_content: reasoningText }),
...(toolCalls?.length && { tool_calls: toolCalls })
}, },
{ {
role: ChatCompletionRequestMessageRoleEnum.User as 'user', role: ChatCompletionRequestMessageRoleEnum.User as 'user',
......
...@@ -15,6 +15,7 @@ import type { LLMAccumulatedUsage } from './types'; ...@@ -15,6 +15,7 @@ import type { LLMAccumulatedUsage } from './types';
* 不写入详情,避免把计费分支信息混入模型响应快照。 * 不写入详情,避免把计费分支信息混入模型响应快照。
*/ */
export const saveLLMResponseRecord = ({ export const saveLLMResponseRecord = ({
teamId,
requestId, requestId,
requestBody, requestBody,
answerText, answerText,
...@@ -26,6 +27,7 @@ export const saveLLMResponseRecord = ({ ...@@ -26,6 +27,7 @@ export const saveLLMResponseRecord = ({
outputTokens, outputTokens,
error error
}: { }: {
teamId: string;
requestId: string; requestId: string;
requestBody: ChatCompletionCreateParams; requestBody: ChatCompletionCreateParams;
answerText: string; answerText: string;
...@@ -38,6 +40,7 @@ export const saveLLMResponseRecord = ({ ...@@ -38,6 +40,7 @@ export const saveLLMResponseRecord = ({
error?: any; error?: any;
}) => { }) => {
void saveLLMRequestRecord({ void saveLLMRequestRecord({
teamId,
requestId, requestId,
body: requestBody, body: requestBody,
response: { response: {
...@@ -68,15 +71,18 @@ export const saveLLMResponseRecord = ({ ...@@ -68,15 +71,18 @@ export const saveLLMResponseRecord = ({
* 这里不记录 usage:失败请求不应参与用量累加,且底层错误通常没有可信 token 信息。 * 这里不记录 usage:失败请求不应参与用量累加,且底层错误通常没有可信 token 信息。
*/ */
export const saveLLMErrorRecord = ({ export const saveLLMErrorRecord = ({
teamId,
requestId, requestId,
requestBody, requestBody,
error error
}: { }: {
teamId: string;
requestId: string; requestId: string;
requestBody: ChatCompletionCreateParams; requestBody: ChatCompletionCreateParams;
error: any; error: any;
}) => { }) => {
void saveLLMRequestRecord({ void saveLLMRequestRecord({
teamId,
requestId, requestId,
body: requestBody, body: requestBody,
response: { response: {
......
...@@ -68,6 +68,8 @@ export type CreateLLMResponseProps< ...@@ -68,6 +68,8 @@ export type CreateLLMResponseProps<
maxContinuations?: number; maxContinuations?: number;
// 是否保存 LLM 请求响应详情。内部辅助调用可关闭,避免污染用户可见的请求记录。 // 是否保存 LLM 请求响应详情。内部辅助调用可关闭,避免污染用户可见的请求记录。
saveLLMResponseRecord?: boolean; saveLLMResponseRecord?: boolean;
// 请求详情会通过 requestId 暴露给前端查询,必须绑定团队后才能落库。
teamId: string;
} & ResponseEvents; } & ResponseEvents;
export type LLMResponse = { export type LLMResponse = {
......
...@@ -31,6 +31,8 @@ export const getVlmModel = (model?: string) => { ...@@ -31,6 +31,8 @@ export const getVlmModel = (model?: string) => {
return list.find((item) => item.model === model || item.name === model) || list[0]; return list.find((item) => item.model === model || item.name === model) || list[0];
}; };
export const getDefaultChatTitleModel = () => global?.systemDefaultModel.chatTitleLLM;
export const getDefaultHelperBotModel = (): LLMModelItemType => export const getDefaultHelperBotModel = (): LLMModelItemType =>
global?.systemDefaultModel.helperBotLLM || getDefaultLLMModel(); global?.systemDefaultModel.helperBotLLM || getDefaultLLMModel();
......
...@@ -69,12 +69,14 @@ export const sanitizeLLMRequestRecordPayload = <T>(payload: T, key?: string): T ...@@ -69,12 +69,14 @@ export const sanitizeLLMRequestRecordPayload = <T>(payload: T, key?: string): T
* @param params - 包含 requestId, body, response 的对象 * @param params - 包含 requestId, body, response 的对象
*/ */
export const saveLLMRequestRecord = async (params: { export const saveLLMRequestRecord = async (params: {
teamId: string;
requestId: string; requestId: string;
body: any; body: any;
response: any; response: any;
}) => { }) => {
try { try {
await MongoLLMRequestRecord.create({ await MongoLLMRequestRecord.create({
teamId: params.teamId,
requestId: params.requestId, requestId: params.requestId,
body: sanitizeLLMRequestRecordPayload(params.body), body: sanitizeLLMRequestRecordPayload(params.body),
response: sanitizeLLMRequestRecordPayload(params.response) response: sanitizeLLMRequestRecordPayload(params.response)
...@@ -91,23 +93,28 @@ export const saveLLMRequestRecord = async (params: { ...@@ -91,23 +93,28 @@ export const saveLLMRequestRecord = async (params: {
/** /**
* 根据 requestId 查询追踪记录 * 根据 requestId 查询追踪记录
* @param requestId - 请求ID * @param requestId - 请求ID
* @param teamId - 调用方团队 ID,用于隔离不同团队的请求详情
* @returns LLM 请求追踪记录 * @returns LLM 请求追踪记录
*/ */
export const getLLMRequestRecord = async ( export const getLLMRequestRecord = async (
requestId: string requestId: string,
teamId: string
): Promise<LLMRequestRecordSchemaType | null> => { ): Promise<LLMRequestRecordSchemaType | null> => {
return await MongoLLMRequestRecord.findOne({ requestId }).lean(); return await MongoLLMRequestRecord.findOne({ requestId, teamId }).lean();
}; };
/** /**
* 批量查询追踪记录 * 批量查询追踪记录
* @param requestIds - 请求ID数组 * @param requestIds - 请求ID数组
* @param teamId - 调用方团队 ID,用于隔离不同团队的请求详情
* @returns LLM 请求追踪记录列表 * @returns LLM 请求追踪记录列表
*/ */
export const getLLMRequestRecords = async ( export const getLLMRequestRecords = async (
requestIds: string[] requestIds: string[],
teamId: string
): Promise<LLMRequestRecordSchemaType[]> => { ): Promise<LLMRequestRecordSchemaType[]> => {
return MongoLLMRequestRecord.find({ return MongoLLMRequestRecord.find({
requestId: { $in: requestIds } requestId: { $in: requestIds },
teamId
}); });
}; };
...@@ -7,10 +7,13 @@ export const LLMRequestRecordCollectionName = 'llm_request_records'; ...@@ -7,10 +7,13 @@ export const LLMRequestRecordCollectionName = 'llm_request_records';
const expiredHours = serviceEnv.LLM_REQUEST_TRACKING_RETENTION_HOURS; const expiredHours = serviceEnv.LLM_REQUEST_TRACKING_RETENTION_HOURS;
const LLMRequestRecordSchema = new Schema({ const LLMRequestRecordSchema = new Schema({
teamId: {
type: Schema.Types.ObjectId,
required: true
},
requestId: { requestId: {
type: String, type: String,
required: true, required: true
unique: true
}, },
body: { body: {
type: Schema.Types.Mixed, type: Schema.Types.Mixed,
...@@ -27,6 +30,8 @@ const LLMRequestRecordSchema = new Schema({ ...@@ -27,6 +30,8 @@ const LLMRequestRecordSchema = new Schema({
} }
}); });
LLMRequestRecordSchema.index({ teamId: 1, requestId: 1 }, { unique: true });
export const MongoLLMRequestRecord = getMongoLogModel<LLMRequestRecordSchemaType>( export const MongoLLMRequestRecord = getMongoLogModel<LLMRequestRecordSchemaType>(
LLMRequestRecordCollectionName, LLMRequestRecordCollectionName,
LLMRequestRecordSchema LLMRequestRecordSchema
......
...@@ -161,6 +161,7 @@ export async function completePendingSkillCreation(data: AgentSkillCreateJobData ...@@ -161,6 +161,7 @@ export async function completePendingSkillCreation(data: AgentSkillCreateJobData
// 有用户需求时走模型辅助生成;否则只创建一个最小 SKILL.md 模板。 // 有用户需求时走模型辅助生成;否则只创建一个最小 SKILL.md 模板。
const model = getSkillCreationLLMModel(); const model = getSkillCreationLLMModel();
const [generatedSkillMd, usage] = await generateSkillMd({ const [generatedSkillMd, usage] = await generateSkillMd({
teamId,
name, name,
description, description,
requirements: requirements.trim(), requirements: requirements.trim(),
......
...@@ -4,6 +4,7 @@ import { sliceJsonStr } from '@fastgpt/global/common/string/tools'; ...@@ -4,6 +4,7 @@ import { sliceJsonStr } from '@fastgpt/global/common/string/tools';
import json5 from 'json5'; import json5 from 'json5';
export type GenerateSkillParam = { export type GenerateSkillParam = {
teamId: string;
name: string; name: string;
description: string; description: string;
requirements: string; requirements: string;
...@@ -210,7 +211,8 @@ export async function getSkillGuidance({ ...@@ -210,7 +211,8 @@ export async function getSkillGuidance({
name, name,
description, description,
requirements, requirements,
model model,
teamId
}: GenerateSkillParam): Promise<{ }: GenerateSkillParam): Promise<{
guidance: SkillGuidance; guidance: SkillGuidance;
usage: SkillMdGenerationUsage; usage: SkillMdGenerationUsage;
...@@ -231,6 +233,8 @@ export async function getSkillGuidance({ ...@@ -231,6 +233,8 @@ export async function getSkillGuidance({
]; ];
const { answerText, usage } = await createLLMResponse({ const { answerText, usage } = await createLLMResponse({
teamId,
saveLLMResponseRecord: false,
body: { body: {
model, model,
messages, messages,
...@@ -275,7 +279,8 @@ export async function generateSkillMd( ...@@ -275,7 +279,8 @@ export async function generateSkillMd(
name: params.name, name: params.name,
description: params.description, description: params.description,
requirements: params.requirements, requirements: params.requirements,
model model,
teamId: params.teamId
}); });
const messages: ChatCompletionMessageParam[] = [ const messages: ChatCompletionMessageParam[] = [
...@@ -295,6 +300,8 @@ export async function generateSkillMd( ...@@ -295,6 +300,8 @@ export async function generateSkillMd(
]; ];
const { answerText, usage: generateUsage } = await createLLMResponse({ const { answerText, usage: generateUsage } = await createLLMResponse({
teamId: params.teamId,
saveLLMResponseRecord: false,
body: { body: {
model, model,
messages, messages,
......
...@@ -8,7 +8,6 @@ import type { ...@@ -8,7 +8,6 @@ import type {
} from '@fastgpt/global/core/ai/model.schema'; } from '@fastgpt/global/core/ai/model.schema';
import type { import type {
I18nStringStrictType, I18nStringStrictType,
AIProxyChannelsType,
AiproxyMapProviderItemType AiproxyMapProviderItemType
} from '@fastgpt/global/sdk/fastgpt-plugin'; } from '@fastgpt/global/sdk/fastgpt-plugin';
import type { langType, ModelProviderItemType } from '@fastgpt/global/core/ai/provider'; import type { langType, ModelProviderItemType } from '@fastgpt/global/core/ai/provider';
...@@ -30,6 +29,7 @@ export type SystemDefaultModelType = { ...@@ -30,6 +29,7 @@ export type SystemDefaultModelType = {
[ModelTypeEnum.llm]?: LLMModelItemType; [ModelTypeEnum.llm]?: LLMModelItemType;
datasetTextLLM?: LLMModelItemType; datasetTextLLM?: LLMModelItemType;
datasetImageLLM?: LLMModelItemType; datasetImageLLM?: LLMModelItemType;
chatTitleLLM?: LLMModelItemType;
helperBotLLM?: LLMModelItemType; helperBotLLM?: LLMModelItemType;
[ModelTypeEnum.embedding]?: EmbeddingModelItemType; [ModelTypeEnum.embedding]?: EmbeddingModelItemType;
......
...@@ -2,10 +2,9 @@ import type { UserChatItemType } from '@fastgpt/global/core/chat/type'; ...@@ -2,10 +2,9 @@ import type { UserChatItemType } from '@fastgpt/global/core/chat/type';
import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/constants'; import { ChatCompletionRequestMessageRoleEnum } from '@fastgpt/global/core/ai/constants';
import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt'; import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { serviceEnv } from '../../env';
import { getLogger, LogCategories } from '../../common/logger'; import { getLogger, LogCategories } from '../../common/logger';
import { createLLMResponse } from '../ai/llm/request'; import { createLLMResponse } from '../ai/llm/request';
import { getLLMModel } from '../ai/model'; import { getDefaultChatTitleModel } from '../ai/model';
import { MongoChat } from './chatSchema'; import { MongoChat } from './chatSchema';
const logger = getLogger(LogCategories.MODULE.CHAT); const logger = getLogger(LogCategories.MODULE.CHAT);
...@@ -83,8 +82,14 @@ export const getFallbackChatTitleFromUserContent = ( ...@@ -83,8 +82,14 @@ export const getFallbackChatTitleFromUserContent = (
return questionText.slice(0, FALLBACK_CHAT_TITLE_MAX_LENGTH); return questionText.slice(0, FALLBACK_CHAT_TITLE_MAX_LENGTH);
}; };
const generateChatTitleFromQuestion = async (question: string): Promise<string | undefined> => { const generateChatTitleFromQuestion = async ({
const titleModel = getLLMModel(serviceEnv.CHAT_TITLE_MODEL); question,
teamId
}: {
question: string;
teamId: string;
}): Promise<string | undefined> => {
const titleModel = getDefaultChatTitleModel();
if (!titleModel?.model) return question.slice(0, FALLBACK_CHAT_TITLE_MAX_LENGTH); if (!titleModel?.model) return question.slice(0, FALLBACK_CHAT_TITLE_MAX_LENGTH);
const questionForTitle = question.slice(0, CHAT_TITLE_QUESTION_MAX_LENGTH); const questionForTitle = question.slice(0, CHAT_TITLE_QUESTION_MAX_LENGTH);
const userPrompt = `Generate a title for the following source text. Do not answer it. const userPrompt = `Generate a title for the following source text. Do not answer it.
...@@ -98,6 +103,7 @@ Return only the title.`; ...@@ -98,6 +103,7 @@ Return only the title.`;
let answerText = ''; let answerText = '';
try { try {
const response = await createLLMResponse({ const response = await createLLMResponse({
teamId,
throwError: false, throwError: false,
saveLLMResponseRecord: false, saveLLMResponseRecord: false,
body: { body: {
...@@ -166,12 +172,14 @@ export type GeneratedChatTitleResult = { ...@@ -166,12 +172,14 @@ export type GeneratedChatTitleResult = {
export const syncGeneratedChatTitleFromUserContent = async ({ export const syncGeneratedChatTitleFromUserContent = async ({
appId, appId,
chatId, chatId,
teamId,
userContent, userContent,
shouldGenerateTitle = true, shouldGenerateTitle = true,
fixedTitle fixedTitle
}: { }: {
appId: string; appId: string;
chatId: string; chatId: string;
teamId: string;
userContent: UserChatItemType; userContent: UserChatItemType;
shouldGenerateTitle?: boolean; shouldGenerateTitle?: boolean;
fixedTitle?: string; fixedTitle?: string;
...@@ -183,7 +191,8 @@ export const syncGeneratedChatTitleFromUserContent = async ({ ...@@ -183,7 +191,8 @@ export const syncGeneratedChatTitleFromUserContent = async ({
if (!questionText && !fixedTitle) return; if (!questionText && !fixedTitle) return;
const nextTitle = const nextTitle =
normalizeFixedChatTitle(fixedTitle) || (await generateChatTitleFromQuestion(questionText)); normalizeFixedChatTitle(fixedTitle) ||
(await generateChatTitleFromQuestion({ question: questionText, teamId }));
if (!nextTitle) return; if (!nextTitle) return;
const customTitleCondition = { const customTitleCondition = {
...@@ -230,6 +239,7 @@ export const syncGeneratedChatTitleFromUserContent = async ({ ...@@ -230,6 +239,7 @@ export const syncGeneratedChatTitleFromUserContent = async ({
export const scheduleGeneratedChatTitleFromUserContent = (params: { export const scheduleGeneratedChatTitleFromUserContent = (params: {
appId: string; appId: string;
chatId: string; chatId: string;
teamId: string;
userContent: UserChatItemType; userContent: UserChatItemType;
shouldGenerateTitle?: boolean; shouldGenerateTitle?: boolean;
fixedTitle?: string; fixedTitle?: string;
......
...@@ -295,6 +295,7 @@ export const preChatRound = async (params: PreChatRoundParams): Promise<PreChatR ...@@ -295,6 +295,7 @@ export const preChatRound = async (params: PreChatRoundParams): Promise<PreChatR
const titleGeneration = syncGeneratedChatTitleFromUserContent({ const titleGeneration = syncGeneratedChatTitleFromUserContent({
appId: params.appId, appId: params.appId,
chatId, chatId,
teamId: params.teamId,
userContent: params.userContent, userContent: params.userContent,
shouldGenerateTitle: preparedChatRound.shouldGenerateTitle, shouldGenerateTitle: preparedChatRound.shouldGenerateTitle,
fixedTitle: params.fixedTitle fixedTitle: params.fixedTitle
......
...@@ -33,11 +33,13 @@ const emptyImageCaptionQueries = (): ImageCaptionQueries => ({ ...@@ -33,11 +33,13 @@ const emptyImageCaptionQueries = (): ImageCaptionQueries => ({
export const getImageCaptionQueries = async ({ export const getImageCaptionQueries = async ({
vlmModel, vlmModel,
imageQueries, imageQueries,
userKey userKey,
teamId
}: { }: {
vlmModel?: string; vlmModel?: string;
imageQueries: string[]; imageQueries: string[];
userKey?: OpenaiAccountType; userKey?: OpenaiAccountType;
teamId: string;
}): Promise<ImageCaptionQueries> => { }): Promise<ImageCaptionQueries> => {
if (!vlmModel || imageQueries.length === 0) { if (!vlmModel || imageQueries.length === 0) {
return emptyImageCaptionQueries(); return emptyImageCaptionQueries();
...@@ -58,6 +60,8 @@ export const getImageCaptionQueries = async ({ ...@@ -58,6 +60,8 @@ export const getImageCaptionQueries = async ({
usage: { inputTokens, outputTokens, usedUserOpenAIKey } usage: { inputTokens, outputTokens, usedUserOpenAIKey }
} = await createLLMResponse({ } = await createLLMResponse({
userKey, userKey,
teamId,
saveLLMResponseRecord: false,
body: { body: {
model: vlmModelData.model, model: vlmModelData.model,
stream: true, stream: true,
......
...@@ -64,7 +64,8 @@ export async function searchDatasetData( ...@@ -64,7 +64,8 @@ export async function searchDatasetData(
const imageCaptionQueries = await getImageCaptionQueries({ const imageCaptionQueries = await getImageCaptionQueries({
vlmModel, vlmModel,
imageQueries, imageQueries,
userKey userKey,
teamId
}); });
// caption 结果需要回传给工作流计费与响应详情;没有生成出有效描述时不输出该段。 // caption 结果需要回传给工作流计费与响应详情;没有生成出有效描述时不输出该段。
......
...@@ -34,7 +34,8 @@ export const defaultSearchDatasetData = async ({ ...@@ -34,7 +34,8 @@ export const defaultSearchDatasetData = async ({
embeddingModel: props.model, embeddingModel: props.model,
extensionBg: datasetSearchExtensionBg, extensionBg: datasetSearchExtensionBg,
histories: props.histories, histories: props.histories,
userKey userKey,
teamId: props.teamId
}) })
: { : {
searchQueries: [], searchQueries: [],
......
...@@ -71,6 +71,7 @@ export const datasetSearchQueryExtension = async ({ ...@@ -71,6 +71,7 @@ export const datasetSearchQueryExtension = async ({
llmModel, llmModel,
embeddingModel, embeddingModel,
userKey, userKey,
teamId,
extensionBg = '', extensionBg = '',
histories = [] histories = []
}: { }: {
...@@ -78,6 +79,7 @@ export const datasetSearchQueryExtension = async ({ ...@@ -78,6 +79,7 @@ export const datasetSearchQueryExtension = async ({
llmModel?: string; llmModel?: string;
embeddingModel?: string; embeddingModel?: string;
userKey?: OpenaiAccountType; userKey?: OpenaiAccountType;
teamId: string;
extensionBg?: string; extensionBg?: string;
histories?: ChatItemMiniType[]; histories?: ChatItemMiniType[];
}) => { }) => {
...@@ -115,7 +117,8 @@ export const datasetSearchQueryExtension = async ({ ...@@ -115,7 +117,8 @@ export const datasetSearchQueryExtension = async ({
histories, histories,
llmModel, llmModel,
embeddingModel, embeddingModel,
userKey userKey,
teamId
}); });
if (result.extensionQueries?.length === 0) return; if (result.extensionQueries?.length === 0) return;
return result; return result;
......
...@@ -153,6 +153,7 @@ export const createWorkflowAgentLoopRuntime = ({ ...@@ -153,6 +153,7 @@ export const createWorkflowAgentLoopRuntime = ({
return { return {
artifacts, artifacts,
runtime: { runtime: {
teamId: context.runningUserInfo.teamId,
model: context.params.model, model: context.params.model,
batchToolSize: 5, batchToolSize: 5,
reasoningEffort: context.params.aiChatReasoningEffort, reasoningEffort: context.params.aiChatReasoningEffort,
......
...@@ -293,6 +293,7 @@ export const createPiAgentWorkflowRuntime = ({ ...@@ -293,6 +293,7 @@ export const createPiAgentWorkflowRuntime = ({
let answerText = ''; let answerText = '';
let reasoningText = ''; let reasoningText = '';
const usedUserOpenAIKey = !!props.externalProvider.openaiAccount?.key; const usedUserOpenAIKey = !!props.externalProvider.openaiAccount?.key;
const teamId = props.runningUserInfo.teamId;
const appendChildNodeResponse = (nodeResponse: ChatHistoryItemResType) => { const appendChildNodeResponse = (nodeResponse: ChatHistoryItemResType) => {
appendNodeResponse(nodeResponse); appendNodeResponse(nodeResponse);
...@@ -306,6 +307,7 @@ export const createPiAgentWorkflowRuntime = ({ ...@@ -306,6 +307,7 @@ export const createPiAgentWorkflowRuntime = ({
response: Record<string, unknown>; response: Record<string, unknown>;
}) => { }) => {
void saveLLMRequestRecordFn({ void saveLLMRequestRecordFn({
teamId,
requestId: request.requestId, requestId: request.requestId,
body: request.body, body: request.body,
response response
......
...@@ -65,12 +65,14 @@ const selectRelevantChunksByLLM = async ({ ...@@ -65,12 +65,14 @@ const selectRelevantChunksByLLM = async ({
query, query,
chunks, chunks,
model, model,
userKey userKey,
teamId
}: { }: {
query: string; query: string;
chunks: SearchDataResponseItemType[]; chunks: SearchDataResponseItemType[];
model: string; model: string;
userKey?: OpenaiAccountType; userKey?: OpenaiAccountType;
teamId: string;
}): Promise< }): Promise<
| { | {
ids: string[]; ids: string[];
...@@ -120,6 +122,7 @@ ${chunkSummaries} ...@@ -120,6 +122,7 @@ ${chunkSummaries}
const llmStartTime = Date.now(); const llmStartTime = Date.now();
const response = await createLLMResponse({ const response = await createLLMResponse({
userKey, userKey,
teamId,
body: { body: {
model, model,
messages: [{ role: 'user', content: prompt }], messages: [{ role: 'user', content: prompt }],
...@@ -321,7 +324,8 @@ export const dispatchAgentDatasetSearch = async ({ ...@@ -321,7 +324,8 @@ export const dispatchAgentDatasetSearch = async ({
query, query,
chunks: searchRes, chunks: searchRes,
model: llmModel, model: llmModel,
userKey userKey,
teamId
}); });
if (pickResults) { if (pickResults) {
if (pickResults.ids.length > 0) { if (pickResults.ids.length > 0) {
......
...@@ -15,6 +15,7 @@ type ModelAgentConfig = { ...@@ -15,6 +15,7 @@ type ModelAgentConfig = {
}; };
type DispatchModelAgentProps = ModelAgentConfig & { type DispatchModelAgentProps = ModelAgentConfig & {
teamId: string;
systemPrompt: string; systemPrompt: string;
task: string; task: string;
onReasoning: ResponseEvents['onReasoning']; onReasoning: ResponseEvents['onReasoning'];
...@@ -32,6 +33,7 @@ type DispatchModelAgentResponse = { ...@@ -32,6 +33,7 @@ type DispatchModelAgentResponse = {
* 该函数不参与 agent loop,只负责按给定 systemPrompt/task 调一次 LLM 并返回文本和 usage。 * 该函数不参与 agent loop,只负责按给定 systemPrompt/task 调一次 LLM 并返回文本和 usage。
*/ */
export async function dispatchModelAgent({ export async function dispatchModelAgent({
teamId,
model, model,
temperature, temperature,
top_p, top_p,
...@@ -67,6 +69,7 @@ export async function dispatchModelAgent({ ...@@ -67,6 +69,7 @@ export async function dispatchModelAgent({
top_p, top_p,
stream stream
}, },
teamId,
onReasoning, onReasoning,
onStreaming onStreaming
}); });
......
...@@ -208,6 +208,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp ...@@ -208,6 +208,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
requestOrigin requestOrigin
}, },
userKey: externalProvider.openaiAccount, userKey: externalProvider.openaiAccount,
teamId: runningUserInfo.teamId,
isAborted: checkIsStopping, isAborted: checkIsStopping,
onReasoning({ text }) { onReasoning({ text }) {
if (!aiChatReasoning) return; if (!aiChatReasoning) return;
......
...@@ -105,6 +105,7 @@ export const dispatchClassifyQuestion = async (props: Props): Promise<CQResponse ...@@ -105,6 +105,7 @@ export const dispatchClassifyQuestion = async (props: Props): Promise<CQResponse
const completions = async ({ const completions = async ({
cqModel, cqModel,
externalProvider, externalProvider,
runningUserInfo,
histories, histories,
lastMemory, lastMemory,
params: { agents, systemPrompt = '', userChatInput } params: { agents, systemPrompt = '', userChatInput }
...@@ -148,7 +149,8 @@ const completions = async ({ ...@@ -148,7 +149,8 @@ const completions = async ({
messages: chats2GPTMessages({ messages, reserveId: false, reserveReason: false }), messages: chats2GPTMessages({ messages, reserveId: false, reserveReason: false }),
stream: true stream: true
}, },
userKey: externalProvider.openaiAccount userKey: externalProvider.openaiAccount,
teamId: runningUserInfo.teamId
}); });
// console.log(JSON.stringify(chats2GPTMessages({ messages, reserveId: false }), null, 2)); // console.log(JSON.stringify(chats2GPTMessages({ messages, reserveId: false }), null, 2));
......
...@@ -205,7 +205,8 @@ const completions = async (props: ActionProps) => { ...@@ -205,7 +205,8 @@ const completions = async (props: ActionProps) => {
messages: chats2GPTMessages({ messages, reserveId: false, reserveReason: false }), messages: chats2GPTMessages({ messages, reserveId: false, reserveReason: false }),
stream: true stream: true
}, },
userKey: externalProvider.openaiAccount userKey: externalProvider.openaiAccount,
teamId: props.runningUserInfo.teamId
}); });
// parse response // parse response
......
...@@ -151,6 +151,7 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo ...@@ -151,6 +151,7 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo
}, },
childrenInteractiveParams, childrenInteractiveParams,
userKey: externalProvider.openaiAccount, userKey: externalProvider.openaiAccount,
teamId: workflowProps.runningUserInfo.teamId,
isAborted: checkIsStopping, isAborted: checkIsStopping,
usagePush, usagePush,
/** /**
......
...@@ -24,6 +24,7 @@ export const dispatchQueryExtension = async ({ ...@@ -24,6 +24,7 @@ export const dispatchQueryExtension = async ({
histories, histories,
node, node,
usagePush, usagePush,
runningUserInfo,
params: { model, systemPrompt, history, userChatInput } params: { model, systemPrompt, history, userChatInput }
}: Props): Promise<Response> => { }: Props): Promise<Response> => {
if (!userChatInput) { if (!userChatInput) {
...@@ -46,7 +47,8 @@ export const dispatchQueryExtension = async ({ ...@@ -46,7 +47,8 @@ export const dispatchQueryExtension = async ({
query: userChatInput, query: userChatInput,
histories: chatHistories, histories: chatHistories,
llmModel: queryExtensionModel.model, llmModel: queryExtensionModel.model,
embeddingModel: embeddingModel.model embeddingModel: embeddingModel.model,
teamId: runningUserInfo.teamId
}); });
extensionQueries.unshift(userChatInput); extensionQueries.unshift(userChatInput);
......
...@@ -227,9 +227,6 @@ export const serviceEnv = createEnv({ ...@@ -227,9 +227,6 @@ export const serviceEnv = createEnv({
.string() .string()
.optional() .optional()
.meta({ description: '辅助生成模型(暂时只能指定一个,需保证系统中已激活该模型)' }), .meta({ description: '辅助生成模型(暂时只能指定一个,需保证系统中已激活该模型)' }),
CHAT_TITLE_MODEL: z.string().optional().meta({
description: '对话标题生成模型;不配置或未命中已启用模型时,回退到系统默认 LLM'
}),
SKIP_FILE_TYPE_CHECK: BoolSchema.default(false).meta({ SKIP_FILE_TYPE_CHECK: BoolSchema.default(false).meta({
description: '是否跳过文件类型检查' description: '是否跳过文件类型检查'
}), }),
...@@ -315,9 +312,7 @@ export const serviceEnv = createEnv({ ...@@ -315,9 +312,7 @@ export const serviceEnv = createEnv({
YUQUE_DATASET_BASE_URL: UrlSchema.default('https://www.yuque.com'), YUQUE_DATASET_BASE_URL: UrlSchema.default('https://www.yuque.com'),
// Invoke 反向调用相关。该密钥用于签发/校验插件反向调用 JWT,必须显式配置,避免未配置时落到公开默认值。 // Invoke 反向调用相关。该密钥用于签发/校验插件反向调用 JWT,必须显式配置,避免未配置时落到公开默认值。
INVOKE_TOKEN_SECRET: z INVOKE_TOKEN_SECRET: z.string().min(32, 'INVOKE_TOKEN_SECRET must be at least 32 characters')
.string()
.min(32, 'INVOKE_TOKEN_SECRET must be at least 32 characters')
}, },
emptyStringAsUndefined: true, emptyStringAsUndefined: true,
runtimeEnv: getRuntimeEnv(), runtimeEnv: getRuntimeEnv(),
......
...@@ -35,7 +35,8 @@ describe('createQuestionGuide', () => { ...@@ -35,7 +35,8 @@ describe('createQuestionGuide', () => {
await createQuestionGuide({ await createQuestionGuide({
messages: [], messages: [],
model: 'deepseek-r1' model: 'deepseek-r1',
teamId: 'team_1'
}); });
expect(createLLMResponseMock.mock.calls[0][0].body).toMatchObject({ expect(createLLMResponseMock.mock.calls[0][0].body).toMatchObject({
...@@ -52,7 +53,8 @@ describe('createQuestionGuide', () => { ...@@ -52,7 +53,8 @@ describe('createQuestionGuide', () => {
await createQuestionGuide({ await createQuestionGuide({
messages: [], messages: [],
model: 'gpt-4o' model: 'gpt-4o',
teamId: 'team_1'
}); });
expect(createLLMResponseMock.mock.calls[0][0].body).not.toHaveProperty('reasoning_effort'); expect(createLLMResponseMock.mock.calls[0][0].body).not.toHaveProperty('reasoning_effort');
......
...@@ -56,7 +56,8 @@ describe('queryExtension', () => { ...@@ -56,7 +56,8 @@ describe('queryExtension', () => {
query: 'original query', query: 'original query',
histories: [], histories: [],
llmModel: 'gpt-query', llmModel: 'gpt-query',
embeddingModel: 'embedding-query' embeddingModel: 'embedding-query',
teamId: 'team_1'
}); });
expect(result).toEqual({ expect(result).toEqual({
...@@ -88,7 +89,8 @@ describe('queryExtension', () => { ...@@ -88,7 +89,8 @@ describe('queryExtension', () => {
query: 'original query', query: 'original query',
histories: [], histories: [],
llmModel: 'gpt-query', llmModel: 'gpt-query',
embeddingModel: 'embedding-query' embeddingModel: 'embedding-query',
teamId: 'team_1'
}); });
expect(lazyGreedyQuerySelectionMock).toHaveBeenCalledWith({ expect(lazyGreedyQuerySelectionMock).toHaveBeenCalledWith({
...@@ -114,7 +116,8 @@ describe('queryExtension', () => { ...@@ -114,7 +116,8 @@ describe('queryExtension', () => {
query: 'original query', query: 'original query',
histories: [], histories: [],
llmModel: 'gpt-query', llmModel: 'gpt-query',
embeddingModel: 'embedding-query' embeddingModel: 'embedding-query',
teamId: 'team_1'
}); });
expect(lazyGreedyQuerySelectionMock).not.toHaveBeenCalled(); expect(lazyGreedyQuerySelectionMock).not.toHaveBeenCalled();
...@@ -149,6 +152,7 @@ describe('queryExtension', () => { ...@@ -149,6 +152,7 @@ describe('queryExtension', () => {
histories: [], histories: [],
llmModel: 'gpt-query', llmModel: 'gpt-query',
embeddingModel: 'embedding-query', embeddingModel: 'embedding-query',
teamId: 'team_1',
generateCount: 4 generateCount: 4
}); });
......
...@@ -33,14 +33,17 @@ const stringifyArgs = (args: string | Record<string, unknown>) => ...@@ -33,14 +33,17 @@ const stringifyArgs = (args: string | Record<string, unknown>) =>
export const toolCall = ({ export const toolCall = ({
id, id,
name, name,
args = {} args = {},
reasoning
}: { }: {
id?: string; id?: string;
name: string; name: string;
args?: string | Record<string, unknown>; args?: string | Record<string, unknown>;
reasoning?: string;
}): LLMQueueItem => ({ }): LLMQueueItem => ({
requestId: `req_${id || name}`, requestId: `req_${id || name}`,
finishReason: 'tool_calls', finishReason: 'tool_calls',
reasoningText: reasoning || '',
toolCalls: [ toolCalls: [
{ {
id: id || `call_${name}`, id: id || `call_${name}`,
......
...@@ -418,6 +418,19 @@ describe('runAgentLoop with mocked createLLMResponse', () => { ...@@ -418,6 +418,19 @@ describe('runAgentLoop with mocked createLLMResponse', () => {
expect(createLLMResponseMock).toHaveBeenCalledTimes(2); expect(createLLMResponseMock).toHaveBeenCalledTimes(2);
expect(onRunTool).toHaveBeenCalledWith({ expect(onRunTool).toHaveBeenCalledWith({
assistantMessage: {
role: 'assistant',
tool_calls: [
{
id: 'call_search',
type: 'function',
function: {
name: 'search',
arguments: '{"q":"FastGPT"}'
}
}
]
},
call: { call: {
id: 'call_search', id: 'call_search',
type: 'function', type: 'function',
......
...@@ -704,6 +704,7 @@ describe('runUnifiedAgentLoop', () => { ...@@ -704,6 +704,7 @@ describe('runUnifiedAgentLoop', () => {
toolCall({ toolCall({
id: 'call_ask', id: 'call_ask',
name: 'ask_agent', name: 'ask_agent',
reasoning: 'Need to ask before planning can continue.',
args: { args: {
reason: 'Need private repository path', reason: 'Need private repository path',
blockerType: 'missing_required_input', blockerType: 'missing_required_input',
...@@ -739,6 +740,7 @@ describe('runUnifiedAgentLoop', () => { ...@@ -739,6 +740,7 @@ describe('runUnifiedAgentLoop', () => {
expect(result.pendingMainContext?.askToolCallId).toBe('call_ask'); expect(result.pendingMainContext?.askToolCallId).toBe('call_ask');
expect(result.pendingMainContext?.messages.at(-1)).toEqual({ expect(result.pendingMainContext?.messages.at(-1)).toEqual({
role: 'assistant', role: 'assistant',
reasoning_content: 'Need to ask before planning can continue.',
tool_calls: [ tool_calls: [
{ {
id: 'call_ask', id: 'call_ask',
......
...@@ -58,12 +58,29 @@ vi.mock('@fastgpt/service/common/logger', () => ({ ...@@ -58,12 +58,29 @@ vi.mock('@fastgpt/service/common/logger', () => ({
})); }));
import { import {
compressLargeContent, compressLargeContent as rawCompressLargeContent,
compressRequestMessages, compressRequestMessages as rawCompressRequestMessages,
compressToolResponse compressToolResponse as rawCompressToolResponse
} from '@fastgpt/service/core/ai/llm/compress'; } from '@fastgpt/service/core/ai/llm/compress';
import { extractExactAnchors } from '@fastgpt/service/core/ai/llm/compress/prompt'; import { extractExactAnchors } from '@fastgpt/service/core/ai/llm/compress/prompt';
const testTeamId = 'team_1';
const compressLargeContent: typeof rawCompressLargeContent = ((args: any) =>
rawCompressLargeContent({
teamId: testTeamId,
...args
})) as typeof rawCompressLargeContent;
const compressRequestMessages: typeof rawCompressRequestMessages = ((args: any) =>
rawCompressRequestMessages({
teamId: testTeamId,
...args
})) as typeof rawCompressRequestMessages;
const compressToolResponse: typeof rawCompressToolResponse = ((args: any) =>
rawCompressToolResponse({
teamId: testTeamId,
...args
})) as typeof rawCompressToolResponse;
const model: LLMModelItemType = { const model: LLMModelItemType = {
type: ModelTypeEnum.llm, type: ModelTypeEnum.llm,
provider: 'openai', provider: 'openai',
......
...@@ -51,6 +51,9 @@ vi.mock('@fastgpt/service/core/ai/record/controller', () => ({ ...@@ -51,6 +51,9 @@ vi.mock('@fastgpt/service/core/ai/record/controller', () => ({
vi.mock('@fastgpt/global/core/ai/llm/utils', () => ({ vi.mock('@fastgpt/global/core/ai/llm/utils', () => ({
removeDatasetCiteText: vi.fn((text: string) => text), removeDatasetCiteText: vi.fn((text: string) => text),
normalizeToolResponseContent: vi.fn((content: unknown) =>
typeof content === 'string' ? content : JSON.stringify(content)
),
getLLMSupportParams: vi.fn(() => ({ getLLMSupportParams: vi.fn(() => ({
vision: false, vision: false,
temperature: true, temperature: true,
...@@ -79,7 +82,7 @@ import { getLLMSupportParams } from '@fastgpt/global/core/ai/llm/utils'; ...@@ -79,7 +82,7 @@ import { getLLMSupportParams } from '@fastgpt/global/core/ai/llm/utils';
import { promptToolCallMessageRewrite } from '@fastgpt/service/core/ai/llm/promptCall'; import { promptToolCallMessageRewrite } from '@fastgpt/service/core/ai/llm/promptCall';
import { saveLLMRequestRecord } from '@fastgpt/service/core/ai/record/controller'; import { saveLLMRequestRecord } from '@fastgpt/service/core/ai/record/controller';
import { createLLMResponse } from '@fastgpt/service/core/ai/llm/request/createLLMResponse'; import { createLLMResponse as rawCreateLLMResponse } from '@fastgpt/service/core/ai/llm/request/createLLMResponse';
const mockGetAIApi = vi.mocked(getAIApi); const mockGetAIApi = vi.mocked(getAIApi);
const mockGetLLMModel = vi.mocked(getLLMModel); const mockGetLLMModel = vi.mocked(getLLMModel);
...@@ -91,6 +94,12 @@ const mockGetLLMSupportParams = vi.mocked(getLLMSupportParams); ...@@ -91,6 +94,12 @@ const mockGetLLMSupportParams = vi.mocked(getLLMSupportParams);
const mockPromptToolCallMessageRewrite = vi.mocked(promptToolCallMessageRewrite); const mockPromptToolCallMessageRewrite = vi.mocked(promptToolCallMessageRewrite);
const mockSaveLLMRequestRecord = vi.mocked(saveLLMRequestRecord); const mockSaveLLMRequestRecord = vi.mocked(saveLLMRequestRecord);
const createLLMResponse: typeof rawCreateLLMResponse = ((args: any) =>
rawCreateLLMResponse({
teamId: 'team_1',
...args
})) as typeof rawCreateLLMResponse;
const createMockAIApiResult = ( const createMockAIApiResult = (
ai: any, ai: any,
requestMeta = { requestMeta = {
...@@ -362,6 +371,95 @@ describe('createLLMResponse', () => { ...@@ -362,6 +371,95 @@ describe('createLLMResponse', () => {
}); });
describe('Non-stream text output', () => { describe('Non-stream text output', () => {
it('should normalize split assistant fields before sending request', async () => {
mockGetLLMModel.mockReturnValue(createMockModelData({ reasoning: true }));
const mockResponse = {
choices: [
{
message: {
role: 'assistant',
content: 'ok'
},
finish_reason: 'stop'
}
],
usage: {
prompt_tokens: 10,
completion_tokens: 2,
total_tokens: 12
}
};
const createMock = vi.fn().mockResolvedValue(mockResponse);
mockGetAIApi.mockReturnValue(
createMockAIApiResult({
chat: {
completions: {
create: createMock
}
}
})
);
const toolCall: ChatCompletionMessageToolCall = {
id: 'call_update_plan',
type: 'function',
function: {
name: 'update_plan',
arguments: '{"updates":[]}'
}
};
await createLLMResponse({
body: {
model: 'gpt-4',
messages: [
{ role: ChatCompletionRequestMessageRoleEnum.User, content: 'make a plan' },
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
reasoning_content: 'Need to create a plan.'
},
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
tool_calls: [toolCall]
},
{
role: ChatCompletionRequestMessageRoleEnum.Tool,
tool_call_id: 'call_update_plan',
content: 'Created active plan.'
}
],
tools: [
{
type: 'function',
function: {
name: 'update_plan',
description: 'Update plan',
parameters: {
type: 'object',
properties: {}
}
}
}
],
stream: false
}
});
expect(createMock.mock.calls[0][0].messages).toEqual([
{ role: ChatCompletionRequestMessageRoleEnum.User, content: 'make a plan' },
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
reasoning_content: 'Need to create a plan.',
tool_calls: [toolCall]
},
{
role: ChatCompletionRequestMessageRoleEnum.Tool,
tool_call_id: 'call_update_plan',
content: 'Created active plan.'
}
]);
});
it('should handle simple non-stream text response', async () => { it('should handle simple non-stream text response', async () => {
const mockResponse = { const mockResponse = {
choices: [ choices: [
...@@ -1560,6 +1658,65 @@ describe('createLLMResponse', () => { ...@@ -1560,6 +1658,65 @@ describe('createLLMResponse', () => {
expect(result.usage.outputTokens).toBe(2); expect(result.usage.outputTokens).toBe(2);
}); });
it('should not save duplicate error record when response error is rethrown', async () => {
const mockResponse = {
choices: [
{
message: {
role: 'assistant',
content: ''
},
finish_reason: 'stop'
}
],
error: new Error('Some error'),
usage: {
prompt_tokens: 10,
completion_tokens: 2,
total_tokens: 12
}
};
const mockAI = {
chat: {
completions: {
create: vi.fn().mockResolvedValue(mockResponse)
}
}
};
mockGetAIApi.mockReturnValue(createMockAIApiResult(mockAI));
const messages: ChatCompletionMessageParam[] = [
{ role: ChatCompletionRequestMessageRoleEnum.User, content: 'Hello' }
];
await expect(
createLLMResponse({
throwError: true,
body: {
model: 'gpt-4',
messages,
stream: false
}
})
).rejects.toThrow('Some error');
expect(mockSaveLLMRequestRecord).toHaveBeenCalledTimes(1);
expect(mockSaveLLMRequestRecord).toHaveBeenCalledWith(
expect.objectContaining({
requestId: 'test-request-id',
response: expect.objectContaining({
finish_reason: 'error',
usage: {
inputTokens: 10,
outputTokens: 2
},
error: expect.any(Error)
})
})
);
});
it('should keep usage when stream is interrupted with partial response', async () => { it('should keep usage when stream is interrupted with partial response', async () => {
const errorChunks = [ const errorChunks = [
{ {
...@@ -1967,5 +2124,56 @@ describe('createLLMResponse', () => { ...@@ -1967,5 +2124,56 @@ describe('createLLMResponse', () => {
expect(result.requestMessages).toEqual(rewritten); expect(result.requestMessages).toEqual(rewritten);
expect(result.completeMessages).toEqual(rewritten); expect(result.completeMessages).toEqual(rewritten);
}); });
it('should return normalized requestMessages on API error', async () => {
mockGetLLMModel.mockReturnValue(createMockModelData({ reasoning: true }));
const mockAI = {
chat: {
completions: {
create: vi.fn().mockRejectedValue(new Error('boom'))
}
}
};
mockGetAIApi.mockReturnValue(createMockAIApiResult(mockAI));
const toolCall: ChatCompletionMessageToolCall = {
id: 'call_search',
type: 'function',
function: {
name: 'search',
arguments: '{"q":"x"}'
}
};
const result = await createLLMResponse({
throwError: false,
body: {
model: 'gpt-4',
messages: [
{ role: ChatCompletionRequestMessageRoleEnum.User, content: 'q' },
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
reasoning_content: 'Need search.'
},
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
tool_calls: [toolCall]
}
],
stream: false
}
});
expect(result.error).toBeDefined();
expect(result.requestMessages).toEqual([
{ role: ChatCompletionRequestMessageRoleEnum.User, content: 'q' },
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
reasoning_content: 'Need search.',
tool_calls: [toolCall]
}
]);
expect(result.completeMessages).toEqual(result.requestMessages);
});
}); });
}); });
...@@ -79,12 +79,9 @@ describe('useLLMResponseAccumulator', () => { ...@@ -79,12 +79,9 @@ describe('useLLMResponseAccumulator', () => {
...baseMessages, ...baseMessages,
{ {
role: ChatCompletionRequestMessageRoleEnum.Assistant, role: ChatCompletionRequestMessageRoleEnum.Assistant,
tool_calls: [createToolCall('call_1', 'search', '{"q":"x"}')]
},
{
role: ChatCompletionRequestMessageRoleEnum.Assistant,
content: 'partial', content: 'partial',
reasoning_content: 'reasoning' reasoning_content: 'reasoning',
tool_calls: [createToolCall('call_1', 'search', '{"q":"x"}')]
}, },
{ {
role: ChatCompletionRequestMessageRoleEnum.User, role: ChatCompletionRequestMessageRoleEnum.User,
......
import { describe, expect, it } from 'vitest'; import { beforeEach, describe, expect, it } from 'vitest';
import { sanitizeLLMRequestRecordPayload } from '@fastgpt/service/core/ai/record/controller'; import {
getLLMRequestRecord,
getLLMRequestRecords,
sanitizeLLMRequestRecordPayload,
saveLLMRequestRecord
} from '@fastgpt/service/core/ai/record/controller';
import { MongoLLMRequestRecord } from '@fastgpt/service/core/ai/record/schema';
const createBase64 = (length = 320) => 'a'.repeat(length); const createBase64 = (length = 320) => 'a'.repeat(length);
beforeEach(async () => {
await MongoLLMRequestRecord.deleteMany({});
});
describe('sanitizeLLMRequestRecordPayload', () => { describe('sanitizeLLMRequestRecordPayload', () => {
it('redacts base64 data urls without changing surrounding request structure', () => { it('redacts base64 data urls without changing surrounding request structure', () => {
const payload = { const payload = {
...@@ -73,3 +83,59 @@ describe('sanitizeLLMRequestRecordPayload', () => { ...@@ -73,3 +83,59 @@ describe('sanitizeLLMRequestRecordPayload', () => {
}); });
}); });
}); });
describe('LLM request record team isolation', () => {
it('saves records with teamId and only reads them from the same team', async () => {
await saveLLMRequestRecord({
teamId: '507f1f77bcf86cd799439011',
requestId: 'req_team_a',
body: { prompt: 'team a prompt' },
response: { answerText: 'team a answer' }
});
await expect(getLLMRequestRecord('req_team_a', '507f1f77bcf86cd799439012')).resolves.toBeNull();
const record = await getLLMRequestRecord('req_team_a', '507f1f77bcf86cd799439011');
expect(record).toMatchObject({
requestId: 'req_team_a',
body: { prompt: 'team a prompt' },
response: { answerText: 'team a answer' }
});
expect(String(record?.teamId)).toBe('507f1f77bcf86cd799439011');
});
it('scopes batch record reads by teamId', async () => {
await saveLLMRequestRecord({
teamId: '507f1f77bcf86cd799439011',
requestId: 'same_request_id',
body: { prompt: 'team a prompt' },
response: { answerText: 'team a answer' }
});
await saveLLMRequestRecord({
teamId: '507f1f77bcf86cd799439012',
requestId: 'same_request_id',
body: { prompt: 'team b prompt' },
response: { answerText: 'team b answer' }
});
const records = await getLLMRequestRecords(['same_request_id'], '507f1f77bcf86cd799439011');
expect(records).toHaveLength(1);
expect(records[0]).toMatchObject({
requestId: 'same_request_id',
body: { prompt: 'team a prompt' },
response: { answerText: 'team a answer' }
});
expect(String(records[0]?.teamId)).toBe('507f1f77bcf86cd799439011');
const teamBRecord = await getLLMRequestRecord('same_request_id', '507f1f77bcf86cd799439012');
expect(teamBRecord).toMatchObject({
requestId: 'same_request_id',
body: { prompt: 'team b prompt' },
response: { answerText: 'team b answer' }
});
expect(String(teamBRecord?.teamId)).toBe('507f1f77bcf86cd799439012');
});
});
...@@ -139,6 +139,7 @@ version: "1.0.0" ...@@ -139,6 +139,7 @@ version: "1.0.0"
} as any); } as any);
const result = await getSkillGuidance({ const result = await getSkillGuidance({
teamId: 'team_1',
name: 'summarize-doc', name: 'summarize-doc',
description: 'Summarize documents', description: 'Summarize documents',
requirements: 'Create a skill that summarizes documents into 200 words max', requirements: 'Create a skill that summarizes documents into 200 words max',
...@@ -160,6 +161,7 @@ version: "1.0.0" ...@@ -160,6 +161,7 @@ version: "1.0.0"
} as any); } as any);
const result = await getSkillGuidance({ const result = await getSkillGuidance({
teamId: 'team_1',
name: 'test-skill', name: 'test-skill',
description: 'My description', description: 'My description',
requirements: 'Some requirements', requirements: 'Some requirements',
...@@ -177,6 +179,7 @@ version: "1.0.0" ...@@ -177,6 +179,7 @@ version: "1.0.0"
} as any); } as any);
const result = await getSkillGuidance({ const result = await getSkillGuidance({
teamId: 'team_1',
name: 'fallback-skill', name: 'fallback-skill',
description: '', description: '',
requirements: 'Some requirements', requirements: 'Some requirements',
...@@ -193,6 +196,7 @@ version: "1.0.0" ...@@ -193,6 +196,7 @@ version: "1.0.0"
} as any); } as any);
const result = await getSkillGuidance({ const result = await getSkillGuidance({
teamId: 'team_1',
name: 'test-skill', name: 'test-skill',
description: 'Test description', description: 'Test description',
requirements: 'Test requirements', requirements: 'Test requirements',
...@@ -212,6 +216,7 @@ version: "1.0.0" ...@@ -212,6 +216,7 @@ version: "1.0.0"
} as any); } as any);
const result = await getSkillGuidance({ const result = await getSkillGuidance({
teamId: 'team_1',
name: 'skill', name: 'skill',
description: 'desc', description: 'desc',
requirements: 'reqs', requirements: 'reqs',
...@@ -296,6 +301,7 @@ version: "1.0.0" ...@@ -296,6 +301,7 @@ version: "1.0.0"
} as any); } as any);
const [content, usage] = await generateSkillMd({ const [content, usage] = await generateSkillMd({
teamId: 'team_1',
name: 'code-review', name: 'code-review',
description: 'Review code', description: 'Review code',
requirements: 'Analyze code quality and suggest improvements', requirements: 'Analyze code quality and suggest improvements',
...@@ -320,6 +326,7 @@ version: "1.0.0" ...@@ -320,6 +326,7 @@ version: "1.0.0"
} as any); } as any);
await generateSkillMd({ await generateSkillMd({
teamId: 'team_1',
name: 'test-skill', name: 'test-skill',
description: 'Test description', description: 'Test description',
requirements: 'Test requirements', requirements: 'Test requirements',
...@@ -347,6 +354,7 @@ version: "1.0.0" ...@@ -347,6 +354,7 @@ version: "1.0.0"
} as any); } as any);
await generateSkillMd({ await generateSkillMd({
teamId: 'team_1',
name: 'chinese-skill', name: 'chinese-skill',
description: '生成中文技能', description: '生成中文技能',
requirements: '请生成一个中文技能说明', requirements: '请生成一个中文技能说明',
...@@ -379,6 +387,7 @@ version: "1.0.0" ...@@ -379,6 +387,7 @@ version: "1.0.0"
} as any); } as any);
await generateSkillMd({ await generateSkillMd({
teamId: 'team_1',
name: 'test', name: 'test',
description: '', description: '',
requirements: 'reqs', requirements: 'reqs',
...@@ -418,6 +427,7 @@ version: "1.0.0" ...@@ -418,6 +427,7 @@ version: "1.0.0"
} as any); } as any);
await generateSkillMd({ await generateSkillMd({
teamId: 'team_1',
name: 'test', name: 'test',
description: '', description: '',
requirements: 'reqs', requirements: 'reqs',
...@@ -454,6 +464,7 @@ version: "1.0.0" ...@@ -454,6 +464,7 @@ version: "1.0.0"
} as any); } as any);
await generateSkillMd({ await generateSkillMd({
teamId: 'team_1',
name: 'test', name: 'test',
description: '', description: '',
requirements: 'reqs', requirements: 'reqs',
...@@ -475,6 +486,7 @@ version: "1.0.0" ...@@ -475,6 +486,7 @@ version: "1.0.0"
} as any); } as any);
await generateSkillMd({ await generateSkillMd({
teamId: 'team_1',
name: 'test', name: 'test',
description: '', description: '',
requirements: 'reqs', requirements: 'reqs',
......
...@@ -5,17 +5,21 @@ import { ...@@ -5,17 +5,21 @@ import {
ChatSourceEnum ChatSourceEnum
} from '@fastgpt/global/core/chat/constants'; } from '@fastgpt/global/core/chat/constants';
import { MongoChat } from '@fastgpt/service/core/chat/chatSchema'; import { MongoChat } from '@fastgpt/service/core/chat/chatSchema';
import { syncGeneratedChatTitleFromUserContent } from '@fastgpt/service/core/chat/title'; import {
createGeneratedChatTitleSender,
syncGeneratedChatTitleFromUserContent
} from '@fastgpt/service/core/chat/title';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
const createLLMResponseMock = vi.hoisted(() => vi.fn()); const createLLMResponseMock = vi.hoisted(() => vi.fn());
const getLLMModelMock = vi.hoisted(() => vi.fn()); const getDefaultChatTitleModelMock = vi.hoisted(() => vi.fn());
vi.mock('@fastgpt/service/core/ai/llm/request', () => ({ vi.mock('@fastgpt/service/core/ai/llm/request', () => ({
createLLMResponse: createLLMResponseMock createLLMResponse: createLLMResponseMock
})); }));
vi.mock('@fastgpt/service/core/ai/model', () => ({ vi.mock('@fastgpt/service/core/ai/model', () => ({
getLLMModel: getLLMModelMock getDefaultChatTitleModel: getDefaultChatTitleModelMock
})); }));
const base = { const base = {
...@@ -38,7 +42,7 @@ const createChat = (override: Record<string, unknown> = {}) => ...@@ -38,7 +42,7 @@ const createChat = (override: Record<string, unknown> = {}) =>
describe('syncGeneratedChatTitleFromUserContent', () => { describe('syncGeneratedChatTitleFromUserContent', () => {
beforeEach(async () => { beforeEach(async () => {
vi.clearAllMocks(); vi.clearAllMocks();
getLLMModelMock.mockReturnValue({ getDefaultChatTitleModelMock.mockReturnValue({
model: 'gpt-title', model: 'gpt-title',
reasoning: true reasoning: true
}); });
...@@ -76,6 +80,7 @@ describe('syncGeneratedChatTitleFromUserContent', () => { ...@@ -76,6 +80,7 @@ describe('syncGeneratedChatTitleFromUserContent', () => {
}); });
expect(createLLMResponseMock).toHaveBeenCalledWith( expect(createLLMResponseMock).toHaveBeenCalledWith(
expect.objectContaining({ expect.objectContaining({
teamId: base.teamId,
throwError: false, throwError: false,
saveLLMResponseRecord: false, saveLLMResponseRecord: false,
body: expect.objectContaining({ body: expect.objectContaining({
...@@ -122,7 +127,7 @@ describe('syncGeneratedChatTitleFromUserContent', () => { ...@@ -122,7 +127,7 @@ describe('syncGeneratedChatTitleFromUserContent', () => {
}); });
it('uses local question text fallback when title model is unavailable', async () => { it('uses local question text fallback when title model is unavailable', async () => {
getLLMModelMock.mockReturnValue(undefined); getDefaultChatTitleModelMock.mockReturnValue(undefined);
await createChat(); await createChat();
const result = await syncGeneratedChatTitleFromUserContent({ const result = await syncGeneratedChatTitleFromUserContent({
...@@ -413,3 +418,63 @@ describe('syncGeneratedChatTitleFromUserContent', () => { ...@@ -413,3 +418,63 @@ describe('syncGeneratedChatTitleFromUserContent', () => {
expect(createLLMResponseMock).toHaveBeenCalled(); expect(createLLMResponseMock).toHaveBeenCalled();
}); });
}); });
describe('createGeneratedChatTitleSender', () => {
it('writes a stream title as soon as generation resolves', async () => {
const writeChatTitle = vi.fn();
const titleSender = createGeneratedChatTitleSender({
titleGeneration: Promise.resolve({
title: 'Generated Title',
updated: true
}),
stream: true,
detail: true,
writeChatTitle
});
const title = await titleSender.send();
expect(title).toBe('Generated Title');
expect(writeChatTitle).toHaveBeenCalledWith({
event: SseResponseEventEnum.chatTitle,
data: {
title: 'Generated Title'
}
});
});
it('reuses the in-flight title send promise without writing duplicate title events', async () => {
const writeChatTitle = vi.fn();
let resolveTitle:
| ((value: { title: string; updated: boolean } | undefined) => void)
| undefined;
const titleGeneration = new Promise<{ title: string; updated: boolean } | undefined>(
(resolve) => {
resolveTitle = resolve;
}
);
const titleSender = createGeneratedChatTitleSender({
titleGeneration,
stream: true,
detail: true,
writeChatTitle
});
const firstTitle = titleSender.send();
const secondTitle = titleSender.send();
resolveTitle?.({
title: 'Generated Title',
updated: true
});
await expect(firstTitle).resolves.toBe('Generated Title');
await expect(secondTitle).resolves.toBe('Generated Title');
expect(writeChatTitle).toHaveBeenCalledTimes(1);
expect(writeChatTitle).toHaveBeenCalledWith({
event: SseResponseEventEnum.chatTitle,
data: {
title: 'Generated Title'
}
});
});
});
...@@ -22,6 +22,9 @@ const createContext = (overrides = {}) => ...@@ -22,6 +22,9 @@ const createContext = (overrides = {}) =>
openaiAccount: { key: 'user-key' } openaiAccount: { key: 'user-key' }
}, },
stream: true, stream: true,
runningUserInfo: {
teamId: 'team_1'
},
node: { node: {
nodeId: 'agent_node', nodeId: 'agent_node',
flowNodeType: FlowNodeTypeEnum.agent flowNodeType: FlowNodeTypeEnum.agent
......
...@@ -17,6 +17,9 @@ const createProps = (overrides = {}) => ...@@ -17,6 +17,9 @@ const createProps = (overrides = {}) =>
externalProvider: { externalProvider: {
openaiAccount: { key: 'user-key' } openaiAccount: { key: 'user-key' }
}, },
runningUserInfo: {
teamId: 'team_1'
},
...overrides ...overrides
}) as any; }) as any;
......
...@@ -517,6 +517,7 @@ describe('dispatchPiAgent user context', () => { ...@@ -517,6 +517,7 @@ describe('dispatchPiAgent user context', () => {
sandbox: expect.any(Object), sandbox: expect.any(Object),
skillIds: [], skillIds: [],
teamId: 'team_1', teamId: 'team_1',
tmbId: 'tmb_1',
workDirectory: getSandboxRuntimeProfile().workDirectory workDirectory: getSandboxRuntimeProfile().workDirectory
}); });
expect(agentConstructorArgs[0].initialState.systemPrompt).not.toContain('pwd: /workspace'); expect(agentConstructorArgs[0].initialState.systemPrompt).not.toContain('pwd: /workspace');
......
...@@ -46,6 +46,9 @@ const createProps = () => ...@@ -46,6 +46,9 @@ const createProps = () =>
} }
], ],
externalProvider: {}, externalProvider: {},
runningUserInfo: {
teamId: 'team_1'
},
usagePush: vi.fn(), usagePush: vi.fn(),
params: { params: {
content: '张三来自杭州', content: '张三来自杭州',
......
...@@ -4,7 +4,6 @@ const validInvokeTokenSecret = 'fastgpt_test_invoke_token_secret_32'; ...@@ -4,7 +4,6 @@ const validInvokeTokenSecret = 'fastgpt_test_invoke_token_secret_32';
const originalEnv = { const originalEnv = {
SYSTEM_MAX_STRING_LENGTH_M: process.env.SYSTEM_MAX_STRING_LENGTH_M, SYSTEM_MAX_STRING_LENGTH_M: process.env.SYSTEM_MAX_STRING_LENGTH_M,
CHAT_TITLE_MODEL: process.env.CHAT_TITLE_MODEL,
FILE_TOKEN_KEY: process.env.FILE_TOKEN_KEY, FILE_TOKEN_KEY: process.env.FILE_TOKEN_KEY,
AES256_SECRET_KEY: process.env.AES256_SECRET_KEY, AES256_SECRET_KEY: process.env.AES256_SECRET_KEY,
INVOKE_TOKEN_SECRET: process.env.INVOKE_TOKEN_SECRET, INVOKE_TOKEN_SECRET: process.env.INVOKE_TOKEN_SECRET,
...@@ -21,7 +20,6 @@ const importServiceEnv = async () => { ...@@ -21,7 +20,6 @@ const importServiceEnv = async () => {
describe('serviceEnv', () => { describe('serviceEnv', () => {
afterEach(() => { afterEach(() => {
vi.stubEnv('SYSTEM_MAX_STRING_LENGTH_M', originalEnv.SYSTEM_MAX_STRING_LENGTH_M); vi.stubEnv('SYSTEM_MAX_STRING_LENGTH_M', originalEnv.SYSTEM_MAX_STRING_LENGTH_M);
vi.stubEnv('CHAT_TITLE_MODEL', originalEnv.CHAT_TITLE_MODEL);
vi.stubEnv('FILE_TOKEN_KEY', originalEnv.FILE_TOKEN_KEY); vi.stubEnv('FILE_TOKEN_KEY', originalEnv.FILE_TOKEN_KEY);
vi.stubEnv('AES256_SECRET_KEY', originalEnv.AES256_SECRET_KEY); vi.stubEnv('AES256_SECRET_KEY', originalEnv.AES256_SECRET_KEY);
vi.stubEnv('INVOKE_TOKEN_SECRET', originalEnv.INVOKE_TOKEN_SECRET); vi.stubEnv('INVOKE_TOKEN_SECRET', originalEnv.INVOKE_TOKEN_SECRET);
...@@ -66,19 +64,6 @@ describe('serviceEnv', () => { ...@@ -66,19 +64,6 @@ describe('serviceEnv', () => {
await expect(importServiceEnv()).rejects.toThrow('Invalid environment variables'); await expect(importServiceEnv()).rejects.toThrow('Invalid environment variables');
}); });
it('validates chat title generation env during service env init', async () => {
vi.stubEnv('FILE_TOKEN_KEY', 'filetokenkey');
vi.stubEnv('AES256_SECRET_KEY', 'fastgptsecret');
vi.stubEnv('INVOKE_TOKEN_SECRET', validInvokeTokenSecret);
vi.stubEnv('CHAT_TITLE_MODEL', 'title-model');
await expect(importServiceEnv()).resolves.toMatchObject({
serviceEnv: {
CHAT_TITLE_MODEL: 'title-model'
}
});
});
it('requires INVOKE_TOKEN_SECRET during service env init', async () => { it('requires INVOKE_TOKEN_SECRET during service env init', async () => {
vi.stubEnv('FILE_TOKEN_KEY', 'filetokenkey'); vi.stubEnv('FILE_TOKEN_KEY', 'filetokenkey');
vi.stubEnv('AES256_SECRET_KEY', 'fastgptsecret'); vi.stubEnv('AES256_SECRET_KEY', 'fastgptsecret');
......
...@@ -83,6 +83,9 @@ ...@@ -83,6 +83,9 @@
"view_table": "Table", "view_table": "Table",
"vlm_model": "Vlm", "vlm_model": "Vlm",
"vlm_model_tip": "Automatically labels images in documents and generates text descriptions to assist text retrieval", "vlm_model_tip": "Automatically labels images in documents and generates text descriptions to assist text retrieval",
"chat_title_model": "Chat title model",
"chat_title_model_tip": "Used to automatically generate chat titles. If unset, no model is called and the user question is truncated as the title.",
"not_set_chat_title_model": "Not set",
"volunme_of_failed_calls": "Error amount", "volunme_of_failed_calls": "Error amount",
"video": "Video recognition", "video": "Video recognition",
"video_tip": "Turn this on if the model supports video recognition.", "video_tip": "Turn this on if the model supports video recognition.",
......
...@@ -83,6 +83,9 @@ ...@@ -83,6 +83,9 @@
"view_table": "表格", "view_table": "表格",
"vlm_model": "图片理解模型", "vlm_model": "图片理解模型",
"vlm_model_tip": "自动标注文档里的图片并生成文本描述,辅助文本检索", "vlm_model_tip": "自动标注文档里的图片并生成文本描述,辅助文本检索",
"chat_title_model": "对话标题模型",
"chat_title_model_tip": "用于自动生成对话标题;不设置时不调用模型,仅使用用户问题截断作为标题",
"not_set_chat_title_model": "不设置",
"volunme_of_failed_calls": "调用失败量", "volunme_of_failed_calls": "调用失败量",
"video": "支持视频识别", "video": "支持视频识别",
"video_tip": "如果模型支持视频识别,则打开该开关。", "video_tip": "如果模型支持视频识别,则打开该开关。",
......
...@@ -83,6 +83,9 @@ ...@@ -83,6 +83,9 @@
"view_table": "表格", "view_table": "表格",
"vlm_model": "圖片理解模型", "vlm_model": "圖片理解模型",
"vlm_model_tip": "自動標註文件裡的圖片並生成文字描述,輔助文字檢索", "vlm_model_tip": "自動標註文件裡的圖片並生成文字描述,輔助文字檢索",
"chat_title_model": "對話標題模型",
"chat_title_model_tip": "用於自動生成對話標題;不設定時不調用模型,僅使用使用者問題截斷作為標題",
"not_set_chat_title_model": "不設定",
"volunme_of_failed_calls": "調用失敗量", "volunme_of_failed_calls": "調用失敗量",
"video": "支援視頻識別", "video": "支援視頻識別",
"video_tip": "如果模型支援視頻識別,則開啟該開關。", "video_tip": "如果模型支援視頻識別,則開啟該開關。",
......
Subproject commit 9770addfa4f270f507921328e2c2cfa9d446b381 Subproject commit 1d3699fa547f04ed2e30df5e6c459fd989bb7872
...@@ -18,6 +18,8 @@ type Props = SelectProps & { ...@@ -18,6 +18,8 @@ type Props = SelectProps & {
disableTip?: string; disableTip?: string;
noOfLines?: ResponsiveValue<number>; noOfLines?: ResponsiveValue<number>;
cacheModel?: boolean; cacheModel?: boolean;
canBeUnset?: boolean;
unsetLabel?: string;
}; };
const modelAvatarSizeMap = { const modelAvatarSizeMap = {
...@@ -41,6 +43,22 @@ const isMultimodalEmbeddingModel = (model?: SystemModelItemType) => { ...@@ -41,6 +43,22 @@ const isMultimodalEmbeddingModel = (model?: SystemModelItemType) => {
return model?.type === ModelTypeEnum.embedding && !!model.vision; return model?.type === ModelTypeEnum.embedding && !!model.vision;
}; };
const UNSET_MODEL_VALUE = '';
const UnsetOptionLabel = React.memo(function UnsetOptionLabel({
label
}: {
label: string | React.ReactNode;
}) {
return (
<Flex alignItems={'center'} py={1} w={'100%'} minW={0}>
<Box noOfLines={1} flex={'1 1 0'} minW={0} overflow={'hidden'}>
{label}
</Box>
</Flex>
);
});
const SelectorActiveModelTags = React.memo(function SelectorActiveModelTags({ const SelectorActiveModelTags = React.memo(function SelectorActiveModelTags({
model model
}: { }: {
...@@ -104,6 +122,8 @@ const OneRowSelector = ({ ...@@ -104,6 +122,8 @@ const OneRowSelector = ({
disableTip, disableTip,
noOfLines, noOfLines,
cacheModel = true, cacheModel = true,
canBeUnset = false,
unsetLabel,
...props ...props
}: Props) => { }: Props) => {
const { t } = useTranslation(['common', 'account']); const { t } = useTranslation(['common', 'account']);
...@@ -146,9 +166,10 @@ const OneRowSelector = ({ ...@@ -146,9 +166,10 @@ const OneRowSelector = ({
() => allModels.find((model) => model.model === props.value), () => allModels.find((model) => model.model === props.value),
[allModels, props.value] [allModels, props.value]
); );
const selectedUnsetLabel = canBeUnset && props.value === UNSET_MODEL_VALUE;
const avatarList = useMemo(() => { const avatarList = useMemo(() => {
return list const modelOptions = list
.map((item) => { .map((item) => {
const modelData = allModels.find((model) => model.model === item.value); const modelData = allModels.find((model) => model.model === item.value);
if (!modelData) return; if (!modelData) return;
...@@ -182,7 +203,27 @@ const OneRowSelector = ({ ...@@ -182,7 +203,27 @@ const OneRowSelector = ({
value: any; value: any;
label: React.JSX.Element; label: React.JSX.Element;
}[]; }[];
}, [allModels, list, getModelProvider, avatarSize, noOfLines, myModels]);
if (!canBeUnset) return modelOptions;
return [
{
value: UNSET_MODEL_VALUE,
label: <UnsetOptionLabel label={unsetLabel ?? t('common:not_model_config')} />
},
...modelOptions
];
}, [
allModels,
list,
getModelProvider,
avatarSize,
noOfLines,
myModels,
canBeUnset,
unsetLabel,
t
]);
return ( return (
<Box <Box
...@@ -199,7 +240,9 @@ const OneRowSelector = ({ ...@@ -199,7 +240,9 @@ const OneRowSelector = ({
isDisabled={!!disableTip} isDisabled={!!disableTip}
list={avatarList} list={avatarList}
valueLabel={ valueLabel={
selectedModelData ? ( selectedUnsetLabel ? (
<UnsetOptionLabel label={unsetLabel ?? t('common:not_model_config')} />
) : selectedModelData ? (
<Flex alignItems={'center'} py={1} minW={0} overflow={'hidden'}> <Flex alignItems={'center'} py={1} minW={0} overflow={'hidden'}>
<Avatar <Avatar
borderRadius={'0'} borderRadius={'0'}
...@@ -237,6 +280,8 @@ const MultipleRowSelector = ({ ...@@ -237,6 +280,8 @@ const MultipleRowSelector = ({
disableTip, disableTip,
placeholder, placeholder,
noOfLines, noOfLines,
canBeUnset = false,
unsetLabel,
...props ...props
}: Props) => { }: Props) => {
const { t, i18n } = useTranslation(['common', 'account']); const { t, i18n } = useTranslation(['common', 'account']);
...@@ -283,8 +328,13 @@ const MultipleRowSelector = ({ ...@@ -283,8 +328,13 @@ const MultipleRowSelector = ({
[modelList, props.value] [modelList, props.value]
); );
const value = useMemo( const value = useMemo(
() => (selectedModelData ? [selectedModelData.provider, selectedModelData.model] : []), () =>
[selectedModelData] selectedModelData
? [selectedModelData.provider, selectedModelData.model]
: canBeUnset && props.value === UNSET_MODEL_VALUE
? [UNSET_MODEL_VALUE]
: [],
[canBeUnset, props.value, selectedModelData]
); );
const selectorList = useMemo(() => { const selectorList = useMemo(() => {
...@@ -309,6 +359,14 @@ const MultipleRowSelector = ({ ...@@ -309,6 +359,14 @@ const MultipleRowSelector = ({
children: [] children: []
})); }));
if (canBeUnset) {
renderList.unshift({
label: <UnsetOptionLabel label={unsetLabel ?? t('common:not_model_config')} />,
value: UNSET_MODEL_VALUE,
children: []
});
}
for (const item of list) { for (const item of list) {
const modelData = modelList.find((model) => model?.model === item.value); const modelData = modelList.find((model) => model?.model === item.value);
if (!modelData) continue; if (!modelData) continue;
...@@ -330,18 +388,23 @@ const MultipleRowSelector = ({ ...@@ -330,18 +388,23 @@ const MultipleRowSelector = ({
}); });
} }
return renderList.filter((item) => item.children.length > 0); return renderList.filter(
}, [getModelProviders, i18n.language, avatarSize, list, modelList]); (item) => item.value === UNSET_MODEL_VALUE || item.children.length > 0
);
}, [getModelProviders, i18n.language, avatarSize, canBeUnset, unsetLabel, t, list, modelList]);
const onSelect = useCallback( const onSelect = useCallback(
(e: string[]) => { (e: (string | undefined)[]) => {
return onChange?.(e[1]); return onChange?.(e[0] === UNSET_MODEL_VALUE ? UNSET_MODEL_VALUE : e[1]);
}, },
[onChange] [onChange]
); );
const SelectedLabel = useMemo(() => { const SelectedLabel = useMemo(() => {
if (loading) return <>{t('common:model_loading')}</>; if (loading) return <>{t('common:model_loading')}</>;
if (canBeUnset && props.value === UNSET_MODEL_VALUE) {
return <UnsetOptionLabel label={unsetLabel ?? t('common:not_model_config')} />;
}
if (!props.value) return <>{t('common:not_model_config')}</>; if (!props.value) return <>{t('common:not_model_config')}</>;
if (!selectedModelData) return <>{t('common:not_model_config')}</>; if (!selectedModelData) return <>{t('common:not_model_config')}</>;
...@@ -364,7 +427,17 @@ const MultipleRowSelector = ({ ...@@ -364,7 +427,17 @@ const MultipleRowSelector = ({
/> />
</Flex> </Flex>
); );
}, [loading, props.value, t, selectedModelData, getModelProvider, avatarSize, noOfLines]); }, [
loading,
canBeUnset,
props.value,
unsetLabel,
t,
selectedModelData,
getModelProvider,
avatarSize,
noOfLines
]);
return ( return (
<Box <Box
......
...@@ -12,7 +12,7 @@ import { ...@@ -12,7 +12,7 @@ import {
type StopChatFnResult type StopChatFnResult
} from '../type'; } from '../type';
import { ChatInputDefaultHeight, ChatTypeEnum, textareaMinH } from '../constants'; import { ChatInputDefaultHeight, ChatTypeEnum, textareaMinH } from '../constants';
import { useFieldArray, type UseFormReturn } from 'react-hook-form'; import { useFieldArray, useWatch, type UseFormReturn } from 'react-hook-form';
import { ChatBoxContext } from '../Provider'; import { ChatBoxContext } from '../Provider';
import dynamic from 'next/dynamic'; import dynamic from 'next/dynamic';
import { useContextSelector } from 'use-context-selector'; import { useContextSelector } from 'use-context-selector';
...@@ -42,7 +42,6 @@ const fileTypeFilter = (file: File) => { ...@@ -42,7 +42,6 @@ const fileTypeFilter = (file: File) => {
type ChatInputProps = BoxProps & { type ChatInputProps = BoxProps & {
lastInteractive?: WorkflowInteractiveResponseType; lastInteractive?: WorkflowInteractiveResponseType;
onSendMessage: SendPromptFnType; onSendMessage: SendPromptFnType;
onStop: () => void;
onStopChat: () => Promise<StopChatFnResult>; onStopChat: () => Promise<StopChatFnResult>;
onStopSettled?: (status: ChatGenerateStatusEnum, completed: boolean) => void; onStopSettled?: (status: ChatGenerateStatusEnum, completed: boolean) => void;
disableSend?: boolean; disableSend?: boolean;
...@@ -54,7 +53,6 @@ type ChatInputProps = BoxProps & { ...@@ -54,7 +53,6 @@ type ChatInputProps = BoxProps & {
const ChatInput = ({ const ChatInput = ({
lastInteractive, lastInteractive,
onSendMessage, onSendMessage,
onStop,
onStopChat, onStopChat,
onStopSettled, onStopSettled,
disableSend, disableSend,
...@@ -68,8 +66,11 @@ const ChatInput = ({ ...@@ -68,8 +66,11 @@ const ChatInput = ({
const { isPc } = useSystem(); const { isPc } = useSystem();
const VoiceInputRef = useRef<VoiceInputComponentRef>(null); const VoiceInputRef = useRef<VoiceInputComponentRef>(null);
const { setValue, watch, control } = chatForm; const { setValue, control } = chatForm;
const inputValue = watch('input'); const inputValue = useWatch({
control,
name: 'input'
});
const [focusing, { on: onFocus, off: offFocus }] = useBoolean(); const [focusing, { on: onFocus, off: offFocus }] = useBoolean();
...@@ -179,8 +180,6 @@ const ChatInput = ({ ...@@ -179,8 +180,6 @@ const ChatInput = ({
} }
} catch { } catch {
onStopSettled?.(ChatGenerateStatusEnum.generating, false); onStopSettled?.(ChatGenerateStatusEnum.generating, false);
} finally {
onStop();
} }
}); });
...@@ -242,7 +241,10 @@ const ChatInput = ({ ...@@ -242,7 +241,10 @@ const ChatInput = ({
textarea.style.overflowY = 'hidden'; textarea.style.overflowY = 'hidden';
} }
setValue('input', textarea.value); setValue('input', textarea.value, {
shouldDirty: true,
shouldTouch: true
});
}} }}
onKeyDown={(e) => { onKeyDown={(e) => {
// enter send.(pc or iframe && enter and unPress shift) // enter send.(pc or iframe && enter and unPress shift)
...@@ -542,7 +544,10 @@ const ChatInput = ({ ...@@ -542,7 +544,10 @@ const ChatInput = ({
appId={appId} appId={appId}
text={inputValue} text={inputValue}
onSelect={(e) => { onSelect={(e) => {
setValue('input', e); setValue('input', e, {
shouldDirty: true,
shouldTouch: true
});
}} }}
onSend={(e) => { onSend={(e) => {
handleSend(e); handleSend(e);
......
...@@ -90,10 +90,10 @@ const isAbortByLeave = (reason: unknown) => { ...@@ -90,10 +90,10 @@ const isAbortByLeave = (reason: unknown) => {
* 这个 hook 承接原 `ChatBox/index.tsx` 中最核心的运行时逻辑: * 这个 hook 承接原 `ChatBox/index.tsx` 中最核心的运行时逻辑:
* - `generatingMessage`:按 SSE event 增量更新最后一条 AI 消息。 * - `generatingMessage`:按 SSE event 增量更新最后一条 AI 消息。
* - `sendPrompt`:校验输入、创建 human/AI placeholder、调用 `onStartChat`、处理完成和失败。 * - `sendPrompt`:校验输入、创建 human/AI placeholder、调用 `onStartChat`、处理完成和失败。
* - `abortRequest`:统一中断 chat、question guide、plugin 和 resume 请求。 * - `abortRequest`:统一中断 chat、question guide、plugin 和 resume 请求,仅用于页面切换、重开会话等前端生命周期清理。
* *
* 输入约定: * 输入约定:
* - refs 仍由 ChatBox 持有,保证停止按钮、页面切换、恢复生成共用同一组 controller。 * - refs 仍由 ChatBox 持有,保证页面切换、恢复生成等生命周期清理共用同一组 controller。
* - `resetInputVal`、`createQuestionGuide`、`scrollToBottom`、`generatingScroll` 都来自前面 PR * - `resetInputVal`、`createQuestionGuide`、`scrollToBottom`、`generatingScroll` 都来自前面 PR
* 已抽出的基础 hook,`useChatGenerate` 只编排它们,不重新实现输入或滚动细节。 * 已抽出的基础 hook,`useChatGenerate` 只编排它们,不重新实现输入或滚动细节。
* - `syncSidebarChatGenerateStatus` 负责侧边栏历史状态,本 hook 只在普通发送开始/完成/失败时调用它。 * - `syncSidebarChatGenerateStatus` 负责侧边栏历史状态,本 hook 只在普通发送开始/完成/失败时调用它。
......
...@@ -599,7 +599,6 @@ const ChatBox = ({ ...@@ -599,7 +599,6 @@ const ChatBox = ({
) : ( ) : (
<ChatInput <ChatInput
onSendMessage={sendPromptWithDisabledGuard} onSendMessage={sendPromptWithDisabledGuard}
onStop={() => abortRequest('stop')}
onStopChat={requestStopChat} onStopChat={requestStopChat}
onStopSettled={handleStopSettled} onStopSettled={handleStopSettled}
disableSend={isRoundPending || (!isReady && !disabledSendTip)} disableSend={isRoundPending || (!isReady && !disabledSendTip)}
...@@ -667,7 +666,6 @@ const ChatBox = ({ ...@@ -667,7 +666,6 @@ const ChatBox = ({
<ChatInput <ChatInput
onSendMessage={sendPromptWithDisabledGuard} onSendMessage={sendPromptWithDisabledGuard}
lastInteractive={lastInteractive} lastInteractive={lastInteractive}
onStop={() => abortRequest('stop')}
onStopChat={requestStopChat} onStopChat={requestStopChat}
onStopSettled={handleStopSettled} onStopSettled={handleStopSettled}
disableSend={isRoundPending} disableSend={isRoundPending}
......
...@@ -282,6 +282,7 @@ const ModelTable = ({ Tab }: { Tab: React.ReactNode }) => { ...@@ -282,6 +282,7 @@ const ModelTable = ({ Tab }: { Tab: React.ReactNode }) => {
isDefault: false, isDefault: false,
isDefaultDatasetTextModel: false, isDefaultDatasetTextModel: false,
isDefaultDatasetImageModel: false, isDefaultDatasetImageModel: false,
isDefaultChatTitleModel: false,
type, type,
...(type === ModelTypeEnum.llm ...(type === ModelTypeEnum.llm
? { ? {
...@@ -727,6 +728,30 @@ const DefaultModelModal = ({ ...@@ -727,6 +728,30 @@ const DefaultModelModal = ({
/> />
</Box> </Box>
</Box> </Box>
<Box>
<Flex mt={4} {...labelStyles} alignItems={'center'}>
<Box mr={0.5}>{t('account_model:chat_title_model')}</Box>
<QuestionTip label={t('account_model:chat_title_model_tip')} />
</Flex>
<Box flex={1}>
<AIModelSelector
bg="myGray.50"
value={defaultData.chatTitleLLM?.model || ''}
canBeUnset
unsetLabel={t('account_model:not_set_chat_title_model')}
list={llmModelList.map((item) => ({
value: item.model,
label: item.name
}))}
onChange={(e) => {
setDefaultData((state) => ({
...state,
chatTitleLLM: llmModelList.find((item) => item.model === e)
}));
}}
/>
</Box>
</Box>
</ModalBody> </ModalBody>
<ModalFooter> <ModalFooter>
<Button variant={'whiteBase'} mr={4} onClick={onClose}> <Button variant={'whiteBase'} mr={4} onClick={onClose}>
...@@ -742,7 +767,8 @@ const DefaultModelModal = ({ ...@@ -742,7 +767,8 @@ const DefaultModelModal = ({
[ModelTypeEnum.stt]: defaultData.stt?.model, [ModelTypeEnum.stt]: defaultData.stt?.model,
[ModelTypeEnum.rerank]: defaultData.rerank?.model, [ModelTypeEnum.rerank]: defaultData.rerank?.model,
datasetTextLLM: defaultData.datasetTextLLM?.model, datasetTextLLM: defaultData.datasetTextLLM?.model,
datasetImageLLM: defaultData.datasetImageLLM?.model datasetImageLLM: defaultData.datasetImageLLM?.model,
chatTitleLLM: defaultData.chatTitleLLM?.model
}) })
} }
> >
......
...@@ -39,7 +39,8 @@ async function handler( ...@@ -39,7 +39,8 @@ async function handler(
const { result, inputTokens, outputTokens } = await createQuestionGuide({ const { result, inputTokens, outputTokens } = await createQuestionGuide({
messages: messages as ChatCompletionMessageParam[], messages: messages as ChatCompletionMessageParam[],
model: qgModel.model model: qgModel.model,
teamId
}); });
pushQuestionGuideUsage({ pushQuestionGuideUsage({
......
...@@ -56,7 +56,8 @@ async function handler(req: ApiRequestProps<CreateQuestionGuideParams>, res: Nex ...@@ -56,7 +56,8 @@ async function handler(req: ApiRequestProps<CreateQuestionGuideParams>, res: Nex
const { result, inputTokens, outputTokens } = await createQuestionGuide({ const { result, inputTokens, outputTokens } = await createQuestionGuide({
messages, messages,
model: qgModel, model: qgModel,
customPrompt: questionGuide?.customPrompt customPrompt: questionGuide?.customPrompt,
teamId
}); });
pushQuestionGuideUsage({ pushQuestionGuideUsage({
......
...@@ -26,7 +26,7 @@ export type testBody = Record<string, never>; ...@@ -26,7 +26,7 @@ export type testBody = Record<string, never>;
export type testResponse = any; export type testResponse = any;
async function handler(req: ApiRequestProps<testBody, testQuery>): Promise<testResponse> { async function handler(req: ApiRequestProps<testBody, testQuery>): Promise<testResponse> {
await authSystemAdmin({ req }); const { teamId } = await authSystemAdmin({ req });
const { model, channelId } = req.query; const { model, channelId } = req.query;
const modelData = findModelFromAlldata(model); const modelData = findModelFromAlldata(model);
...@@ -46,7 +46,7 @@ async function handler(req: ApiRequestProps<testBody, testQuery>): Promise<testR ...@@ -46,7 +46,7 @@ async function handler(req: ApiRequestProps<testBody, testQuery>): Promise<testR
logger.debug(`Test model`, modelData); logger.debug(`Test model`, modelData);
if (modelData.type === 'llm') { if (modelData.type === 'llm') {
return testLLMModel(modelData, headers); return testLLMModel(modelData, headers, teamId);
} }
if (modelData.type === 'embedding') { if (modelData.type === 'embedding') {
return testEmbeddingModel(modelData, headers); return testEmbeddingModel(modelData, headers);
...@@ -66,8 +66,14 @@ async function handler(req: ApiRequestProps<testBody, testQuery>): Promise<testR ...@@ -66,8 +66,14 @@ async function handler(req: ApiRequestProps<testBody, testQuery>): Promise<testR
export default NextAPI(handler); export default NextAPI(handler);
const testLLMModel = async (model: LLMModelItemType, headers: Record<string, string>) => { const testLLMModel = async (
model: LLMModelItemType,
headers: Record<string, string>,
teamId: string
) => {
const { answerText } = await createLLMResponse({ const { answerText } = await createLLMResponse({
teamId,
saveLLMResponseRecord: false,
body: { body: {
model, // 传递实体 model 进去,保障底层不会去拿内存里的实体。 model, // 传递实体 model 进去,保障底层不会去拿内存里的实体。
messages: [{ role: 'user', content: 'hi' }], messages: [{ role: 'user', content: 'hi' }],
......
import type { ApiRequestProps, ApiResponseType } from '@fastgpt/service/type/next'; import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry'; import { NextAPI } from '@/service/middleware/entry';
import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun'; import { mongoSessionRun } from '@fastgpt/service/common/mongo/sessionRun';
import { MongoSystemModel } from '@fastgpt/service/core/ai/config/schema'; import { MongoSystemModel } from '@fastgpt/service/core/ai/config/schema';
import { updatedReloadSystemModel } from '@fastgpt/service/core/ai/config/utils'; import { updatedReloadSystemModel } from '@fastgpt/service/core/ai/config/utils';
import type { ModelTypeEnum } from '@fastgpt/global/core/ai/constants'; import { ModelTypeEnum } from '@fastgpt/global/core/ai/constants';
import { authSystemAdmin } from '@fastgpt/service/support/permission/user/auth'; import { authSystemAdmin } from '@fastgpt/service/support/permission/user/auth';
import { parseApiInput } from '@fastgpt/service/common/zod/requestParseError';
import { z } from 'zod';
export type updateDefaultQuery = {}; export type updateDefaultQuery = Record<string, never>;
export type updateDefaultBody = { const UpdateDefaultBodySchema = z.object({
[ModelTypeEnum.llm]?: string; [ModelTypeEnum.llm]: z.string().optional(),
[ModelTypeEnum.embedding]?: string; [ModelTypeEnum.embedding]: z.string().optional(),
[ModelTypeEnum.tts]?: string; [ModelTypeEnum.tts]: z.string().optional(),
[ModelTypeEnum.stt]?: string; [ModelTypeEnum.stt]: z.string().optional(),
[ModelTypeEnum.rerank]?: string; [ModelTypeEnum.rerank]: z.string().optional(),
datasetTextLLM?: string; datasetTextLLM: z.string().optional(),
datasetImageLLM?: string; datasetImageLLM: z.string().optional(),
}; chatTitleLLM: z.string().optional()
});
export type updateDefaultResponse = {}; const UpdateDefaultResponseSchema = z.object({});
export type updateDefaultBody = z.infer<typeof UpdateDefaultBodySchema>;
export type updateDefaultResponse = z.infer<typeof UpdateDefaultResponseSchema>;
async function handler( async function handler(
req: ApiRequestProps<updateDefaultBody, updateDefaultQuery>, req: ApiRequestProps<updateDefaultBody, updateDefaultQuery>
res: ApiResponseType<any>
): Promise<updateDefaultResponse> { ): Promise<updateDefaultResponse> {
await authSystemAdmin({ req }); await authSystemAdmin({ req });
const { llm, embedding, tts, stt, rerank, datasetTextLLM, datasetImageLLM } = req.body; const { llm, embedding, tts, stt, rerank, datasetTextLLM, datasetImageLLM, chatTitleLLM } =
parseApiInput({ req, bodySchema: UpdateDefaultBodySchema }).body;
const defaultModelNames = [llm, embedding, tts, stt, rerank].filter(
(model): model is string => !!model
);
const datasetTextModelName = datasetTextLLM || null;
const datasetImageModelName = datasetImageLLM || null;
const chatTitleModelName = chatTitleLLM || null;
await mongoSessionRun(async (session) => { await mongoSessionRun(async (session) => {
// Remove all default flags // 用 pipeline update 一次性重算所有默认标记,避免多次 updateOne 造成额外数据库往返。
await MongoSystemModel.updateMany( await MongoSystemModel.updateMany(
{}, {},
{ [
$unset: { {
'metadata.isDefault': 1, $set: {
'metadata.isDefaultDatasetTextModel': 1, 'metadata.isDefault': {
'metadata.isDefaultDatasetImageModel': 1 $cond: [{ $in: ['$model', defaultModelNames] }, true, '$$REMOVE']
},
'metadata.isDefaultDatasetTextModel': {
$cond: [{ $eq: ['$model', datasetTextModelName] }, true, '$$REMOVE']
},
'metadata.isDefaultDatasetImageModel': {
$cond: [{ $eq: ['$model', datasetImageModelName] }, true, '$$REMOVE']
},
'metadata.isDefaultChatTitleModel': {
$cond: [{ $eq: ['$model', chatTitleModelName] }, true, '$$REMOVE']
}
}
} }
}, ],
{ session } { session }
); );
if (llm) {
await MongoSystemModel.updateOne(
{ model: llm },
{ $set: { 'metadata.isDefault': true } },
{ session }
);
}
if (datasetTextLLM) {
await MongoSystemModel.updateOne(
{ model: datasetTextLLM },
{ $set: { 'metadata.isDefaultDatasetTextModel': true } },
{ session }
);
}
if (datasetImageLLM) {
await MongoSystemModel.updateOne(
{ model: datasetImageLLM },
{ $set: { 'metadata.isDefaultDatasetImageModel': true } },
{ session }
);
}
if (embedding) {
await MongoSystemModel.updateOne(
{ model: embedding },
{ $set: { 'metadata.isDefault': true } },
{ session }
);
}
if (tts) {
await MongoSystemModel.updateOne(
{ model: tts },
{ $set: { 'metadata.isDefault': true } },
{ session }
);
}
if (stt) {
await MongoSystemModel.updateOne(
{ model: stt },
{ $set: { 'metadata.isDefault': true } },
{ session }
);
}
if (rerank) {
await MongoSystemModel.updateOne(
{ model: rerank },
{ $set: { 'metadata.isDefault': true } },
{ session }
);
}
}); });
await updatedReloadSystemModel(); await updatedReloadSystemModel();
return {}; return UpdateDefaultResponseSchema.parse({});
} }
export default NextAPI(handler); export default NextAPI(handler);
...@@ -100,6 +100,8 @@ async function handler(req: ApiRequestProps<OptimizePromptBody>, res: ApiRespons ...@@ -100,6 +100,8 @@ async function handler(req: ApiRequestProps<OptimizePromptBody>, res: ApiRespons
const { const {
usage: { inputTokens, outputTokens } usage: { inputTokens, outputTokens }
} = await createLLMResponse({ } = await createLLMResponse({
teamId,
saveLLMResponseRecord: false,
body: { body: {
model, model,
messages, messages,
......
...@@ -21,7 +21,7 @@ export type GetRecordBody = Record<string, never>; ...@@ -21,7 +21,7 @@ export type GetRecordBody = Record<string, never>;
export type GetRecordResponse = LLMRequestRecordSchemaType; export type GetRecordResponse = LLMRequestRecordSchemaType;
async function handler(req: ApiRequestProps): Promise<GetRecordResponse | undefined> { async function handler(req: ApiRequestProps): Promise<GetRecordResponse | undefined> {
const { tmbId } = await authCert({ req, authToken: true }); const { teamId, tmbId } = await authCert({ req, authToken: true });
await authFrequencyLimit({ await authFrequencyLimit({
eventId: `${tmbId}-getrecords`, eventId: `${tmbId}-getrecords`,
...@@ -36,7 +36,7 @@ async function handler(req: ApiRequestProps): Promise<GetRecordResponse | undefi ...@@ -36,7 +36,7 @@ async function handler(req: ApiRequestProps): Promise<GetRecordResponse | undefi
querySchema: GetLLMRequestRecordParamsSchema querySchema: GetLLMRequestRecordParamsSchema
}).query; }).query;
const record = await getLLMRequestRecord(requestId); const record = await getLLMRequestRecord(requestId, teamId);
if (!record) { if (!record) {
return Promise.reject(i18nT('common:error.llm_track_expired')); return Promise.reject(i18nT('common:error.llm_track_expired'));
......
...@@ -401,16 +401,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { ...@@ -401,16 +401,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
} }
}); });
// Send finish signals
streamResponseContext.responseWrite({
event: SseResponseEventEnum.answer,
data: textAdaptGptResponse({ text: null, finish_reason: 'stop' })
});
streamResponseContext.responseWrite({
event: SseResponseEventEnum.answer,
data: '[DONE]'
});
// Save chat records (using skillId as virtual appId) // Save chat records (using skillId as virtual appId)
const aiResponse: AIChatItemType & { dataId?: string } = { const aiResponse: AIChatItemType & { dataId?: string } = {
dataId: finalResponseChatItemId, dataId: finalResponseChatItemId,
...@@ -456,6 +446,16 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { ...@@ -456,6 +446,16 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}); });
} }
// 落库完成后再发送结束信号,避免前端先退出 chatting 而服务端仍停留在 generating。
streamResponseContext.responseWrite({
event: SseResponseEventEnum.answer,
data: textAdaptGptResponse({ text: null, finish_reason: 'stop' })
});
streamResponseContext.responseWrite({
event: SseResponseEventEnum.answer,
data: '[DONE]'
});
await streamResponseContext.flushResume(); await streamResponseContext.flushResume();
} catch (err: any) { } catch (err: any) {
const { preparedRound } = roundState; const { preparedRound } = roundState;
......
...@@ -110,6 +110,8 @@ async function handler(req: ApiRequestProps<OptimizeCodeBody>, res: ApiResponseT ...@@ -110,6 +110,8 @@ async function handler(req: ApiRequestProps<OptimizeCodeBody>, res: ApiResponseT
]; ];
const llmResponse = await createLLMResponse({ const llmResponse = await createLLMResponse({
teamId,
saveLLMResponseRecord: false,
body: { body: {
model, model,
messages, messages,
......
...@@ -311,6 +311,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { ...@@ -311,6 +311,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
detail, detail,
writeChatTitle: workflowResponseWrite writeChatTitle: workflowResponseWrite
}); });
if (stream) {
void titleSender.send();
}
/* start flow controller */ /* start flow controller */
const { const {
......
...@@ -318,6 +318,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) { ...@@ -318,6 +318,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
detail, detail,
writeChatTitle: (payload) => streamResponseContext?.responseWrite(payload) writeChatTitle: (payload) => streamResponseContext?.responseWrite(payload)
}); });
if (stream) {
void titleSender.send();
}
/* start flow controller */ /* start flow controller */
const { const {
......
...@@ -141,6 +141,8 @@ export async function generateQA(): Promise<any> { ...@@ -141,6 +141,8 @@ export async function generateQA(): Promise<any> {
answerText: answer, answerText: answer,
usage: { inputTokens, outputTokens } usage: { inputTokens, outputTokens }
} = await createLLMResponse({ } = await createLLMResponse({
teamId: data.teamId,
saveLLMResponseRecord: false,
body: { body: {
model: modelData.model, model: modelData.model,
messages, messages,
......
...@@ -499,5 +499,13 @@ describe('debugChat handler — parameter validation', () => { ...@@ -499,5 +499,13 @@ describe('debugChat handler — parameter validation', () => {
}) })
}) })
); );
const doneWriteIndex = debugChatMocks.responseWrite.mock.calls.findIndex(
([payload]) => payload.data === '[DONE]'
);
expect(doneWriteIndex).toBeGreaterThanOrEqual(0);
expect(debugChatMocks.finalizeChatRound.mock.invocationCallOrder[0]).toBeLessThan(
debugChatMocks.responseWrite.mock.invocationCallOrder[doneWriteIndex]
);
}); });
}); });
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