Commit 146ade2d by Archer Committed by GitHub

feat: add workflow file context (#7346)

* feat: add workflow file context

* refactor: remove unused outbound axios selector

* fix: register workflow file inputs

* fix: authorize workflow file cache reads

* refactor: unify external workflow file reads

* fix: isolate child workflow file context

* fix: align SSE user errors and compliance copy

* fix: tighten workflow file handling and SSE errors

* test: cover workflow file context edge cases

* fix: enforce child workflow file boundaries

* fix: preserve workflow form input files

* fix: address workflow file context regressions

* docs: clarify FE_DOMAIN origin

* docs: simplify FE_DOMAIN guidance

* docs: refine FE_DOMAIN Chinese description

* test: align workflow file metadata expectation

* refactor(workflow): pass file urls to read_files

* refactor(workflow): unify read file execution

* refactor(workflow): unify AI file message handling

* fix: preserve pro chat file context export

* refactor(workflow): remove obsolete file context adapters

* submodule

* refactor(workflow): finalize file URL handling

* fix(workflow): enforce plugin file input limits

* fix(workflow): enforce file input limits

* test(workflow): complete app schema mock

* fix(workflow): isolate file context inputs

* fix(workflow): isolate interactive edge state

* fix(workflow): harden file reading

* chore: update pro workflow file handling

* fix(workflow): unify file upload quotas

* refactor(workflow): clarify file limit resolution
parent 8ac1f812
...@@ -161,7 +161,7 @@ services: ...@@ -161,7 +161,7 @@ services:
<<: [*x-share-db-config, *x-vec-config, *x-log-config, *x-no-proxy-config] <<: [*x-share-db-config, *x-vec-config, *x-log-config, *x-no-proxy-config]
HOSTNAME: 0.0.0.0 HOSTNAME: 0.0.0.0
# ==================== 基础配置 ==================== # ==================== 基础配置 ====================
# 前端外部可访问的地址,用于自动补全文件资源路径。例如 https:fastgpt.cn,不能填 localhost。这个值可以不填,不填则发给模型的图片会是一个相对路径,而不是全路径,模型可能伪造Host。 # 必填。客户端访问 FastGPT 时使用的地址,由协议、主机和可选端口组成,用于补全文件资源路径。例如 https://fastgpt.cn;本地开发可使用 http://localhost:3000。
FE_DOMAIN: FE_DOMAIN:
# root key(最高权限) # root key(最高权限)
ROOT_KEY: *x-system-key ROOT_KEY: *x-system-key
......
...@@ -303,7 +303,7 @@ services: ...@@ -303,7 +303,7 @@ services:
*x-service-env-config, *x-service-env-config,
*x-app-env-config *x-app-env-config
] ]
# 前端外部可访问的地址,用于自动补全文件资源路径。例如 https:fastgpt.cn,不能填 localhost。这个值可以不填,不填则发给模型的图片会是一个相对路径,而不是全路径,模型可能伪造Host。 # 必填。客户端访问 FastGPT 时使用的地址,由协议、主机和可选端口组成,用于补全文件资源路径。例如 https://fastgpt.cn;本地开发可使用 http://localhost:3000。
FE_DOMAIN: FE_DOMAIN:
# 文件域名(也指向 FastGPT 服务);如需更高安全性可独立分配域名,避免高危文件读取到主域名内容 # 文件域名(也指向 FastGPT 服务);如需更高安全性可独立分配域名,避免高危文件读取到主域名内容
FILE_DOMAIN: FILE_DOMAIN:
......
...@@ -44,7 +44,7 @@ These variables are mainly validated by `packages/service/env.ts` and apply to ` ...@@ -44,7 +44,7 @@ These variables are mainly validated by `packages/service/env.ts` and apply to `
| `OPENAI_BASE_URL` | `https://api.openai.com/v1` | Default OpenAI-compatible model endpoint when AI Proxy is not configured. | | `OPENAI_BASE_URL` | `https://api.openai.com/v1` | Default OpenAI-compatible model endpoint when AI Proxy is not configured. |
| `CHAT_API_KEY` | Empty | Default OpenAI-compatible model API key when AI Proxy token is not configured. | | `CHAT_API_KEY` | Empty | Default OpenAI-compatible model API key when AI Proxy token is not configured. |
| `CRM_API_URL` | Empty | Lead attribution CRM API base URL (including `/api/v1`). Empty disables identity reporting. | | `CRM_API_URL` | Empty | Lead attribution CRM API base URL (including `/api/v1`). Empty disables identity reporting. |
| `CRM_API_KEY` | Empty | CRM admin API key used to bind a FastGPT user to `visitor_id` after registration or login. | | `CRM_API_KEY` | Empty | CRM admin API key used to bind a FastGPT user to `visitor_id` after registration or login. |
| `MARKETPLACE_URL` | `https://v2.marketplace.fastgpt.cn` | Plugin marketplace API URL. | | `MARKETPLACE_URL` | `https://v2.marketplace.fastgpt.cn` | Plugin marketplace API URL. |
| `FEISHU_BASE_URL` | `https://open.feishu.cn` | Lark Open Platform URL. Use your private Lark domain when self-hosting Lark. | | `FEISHU_BASE_URL` | `https://open.feishu.cn` | Lark Open Platform URL. Use your private Lark domain when self-hosting Lark. |
| `DINGTALK_BASE_URL` | `https://api.dingtalk.com` | DingTalk new API base URL. | | `DINGTALK_BASE_URL` | `https://api.dingtalk.com` | DingTalk new API base URL. |
...@@ -150,16 +150,16 @@ These variables are mainly validated by `packages/service/env.ts` and apply to ` ...@@ -150,16 +150,16 @@ These variables are mainly validated by `packages/service/env.ts` and apply to `
### Domains, Frontend, and Runtime ### Domains, Frontend, and Runtime
| Variable | Default | Description | | Variable | Default | Description |
| ------------------------- | --------------------------- | -------------------------------------------------------------------------------------------------------------- | | ------------------------- | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `FE_DOMAIN` | Empty | External FastGPT frontend URL. Used to complete file and image URLs. Do not set this to `localhost`. | | `FE_DOMAIN` | Required | The origin clients use to access FastGPT, including the scheme, host, and optional port. It completes file and image URLs. Local development can use `http://localhost:3000`. |
| `FILE_DOMAIN` | Empty | File access domain. It usually points to FastGPT, but a separate domain can isolate file risk. | | `FILE_DOMAIN` | Empty | File access domain. It usually points to FastGPT, but a separate domain can isolate file risk. |
| `NEXT_PUBLIC_BASE_URL` | Empty | Next.js sub-path deployment prefix, such as `/fastgpt`. It must be fixed when building the image. | | `NEXT_PUBLIC_BASE_URL` | Empty | Next.js sub-path deployment prefix, such as `/fastgpt`. It must be fixed when building the image. |
| `HOSTNAME` | `localhost` | Service host used for internal URLs and SSRF local-address detection. Containers commonly set it to `0.0.0.0`. | | `HOSTNAME` | `localhost` | Service host used for internal URLs and SSRF local-address detection. Containers commonly set it to `0.0.0.0`. |
| `PORT` | `3000` | Next.js listening port. Also used for local-address detection. | | `PORT` | `3000` | Next.js listening port. Also used for local-address detection. |
| `NODE_ENV` | Empty | Standard Node/Next.js runtime environment. Production images set it to `production`. | | `NODE_ENV` | Empty | Standard Node/Next.js runtime environment. Production images set it to `production`. |
| `NEXT_TELEMETRY_DISABLED` | `1` | Disables Next.js Telemetry in production images. | | `NEXT_TELEMETRY_DISABLED` | `1` | Disables Next.js Telemetry in production images. |
| `NODE_OPTIONS` | `--max-old-space-size=4096` | Node options used during production image builds to increase the build memory limit. | | `NODE_OPTIONS` | `--max-old-space-size=4096` | Node options used during production image builds to increase the build memory limit. |
### Security ### Security
......
...@@ -43,8 +43,8 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说 ...@@ -43,8 +43,8 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
| `AIPROXY_API_TOKEN` | 空 | 调用 AI Proxy 使用的认证 Token。 | | `AIPROXY_API_TOKEN` | 空 | 调用 AI Proxy 使用的认证 Token。 |
| `OPENAI_BASE_URL` | `https://api.openai.com/v1` | 未配置 AI Proxy 时,兼容 OpenAI 协议的默认模型接口地址。 | | `OPENAI_BASE_URL` | `https://api.openai.com/v1` | 未配置 AI Proxy 时,兼容 OpenAI 协议的默认模型接口地址。 |
| `CHAT_API_KEY` | 空 | 未配置 AI Proxy Token 时,兼容 OpenAI 协议的默认模型 API Key。 | | `CHAT_API_KEY` | 空 | 未配置 AI Proxy Token 时,兼容 OpenAI 协议的默认模型 API Key。 |
| `CRM_API_URL` | 空 | 官网访客归因 CRM 的 API 基础地址(包含 `/api/v1`);为空时不进行身份上报。 | | `CRM_API_URL` | 空 | 官网访客归因 CRM 的 API 基础地址(包含 `/api/v1`);为空时不进行身份上报。 |
| `CRM_API_KEY` | 空 | CRM 管理 API Key,用于注册或登录成功后按 `visitor_id` 绑定 FastGPT 用户。 | | `CRM_API_KEY` | 空 | CRM 管理 API Key,用于注册或登录成功后按 `visitor_id` 绑定 FastGPT 用户。 |
| `MARKETPLACE_URL` | `https://v2.marketplace.fastgpt.cn` | 插件市场接口地址。 | | `MARKETPLACE_URL` | `https://v2.marketplace.fastgpt.cn` | 插件市场接口地址。 |
| `FEISHU_BASE_URL` | `https://open.feishu.cn` | 飞书开放平台地址,私有化飞书可改为对应域名。 | | `FEISHU_BASE_URL` | `https://open.feishu.cn` | 飞书开放平台地址,私有化飞书可改为对应域名。 |
| `DINGTALK_BASE_URL` | `https://api.dingtalk.com` | 钉钉新版 API 基础地址。 | | `DINGTALK_BASE_URL` | `https://api.dingtalk.com` | 钉钉新版 API 基础地址。 |
...@@ -150,16 +150,16 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说 ...@@ -150,16 +150,16 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
### 域名、前端与运行时 ### 域名、前端与运行时
| 变量 | 默认值 | 说明 | | 变量 | 默认值 | 说明 |
| ------------------------- | --------------------------- | -------------------------------------------------------------------------------- | | ------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `FE_DOMAIN` | 空 | FastGPT 前端外部访问地址,用于补全文件、图片等资源路径;不要配置为 `localhost`。 | | `FE_DOMAIN` | 必填 | 客户端访问 FastGPT 时使用的地址(由协议、主机和可选端口组成),用于补全文件、图片等资源路径;本地开发可使用 `http://localhost:3000`。 |
| `FILE_DOMAIN` | 空 | 文件访问域名,通常也指向 FastGPT 服务;可独立域名隔离文件风险。 | | `FILE_DOMAIN` | 空 | 文件访问域名,通常也指向 FastGPT 服务;可独立域名隔离文件风险。 |
| `NEXT_PUBLIC_BASE_URL` | 空 | Next.js 子路径部署前缀,例如 `/fastgpt`;需要在构建镜像时确定。 | | `NEXT_PUBLIC_BASE_URL` | 空 | Next.js 子路径部署前缀,例如 `/fastgpt`;需要在构建镜像时确定。 |
| `HOSTNAME` | `localhost` | 服务本机 Host,用于内部 URL 与 SSRF 本地地址识别;容器中常设为 `0.0.0.0`。 | | `HOSTNAME` | `localhost` | 服务本机 Host,用于内部 URL 与 SSRF 本地地址识别;容器中常设为 `0.0.0.0`。 |
| `PORT` | `3000` | Next.js 服务监听端口,也用于本地地址识别。 | | `PORT` | `3000` | Next.js 服务监听端口,也用于本地地址识别。 |
| `NODE_ENV` | 空 | 标准 Node/Next.js 运行环境变量,生产镜像中为 `production`。 | | `NODE_ENV` | 空 | 标准 Node/Next.js 运行环境变量,生产镜像中为 `production`。 |
| `NEXT_TELEMETRY_DISABLED` | `1` | 生产镜像中关闭 Next.js Telemetry。 | | `NEXT_TELEMETRY_DISABLED` | `1` | 生产镜像中关闭 Next.js Telemetry。 |
| `NODE_OPTIONS` | `--max-old-space-size=4096` | 生产镜像构建阶段使用的 Node.js 启动参数,用于提高构建内存上限。 | | `NODE_OPTIONS` | `--max-old-space-size=4096` | 生产镜像构建阶段使用的 Node.js 启动参数,用于提高构建内存上限。 |
### 安全配置 ### 安全配置
......
...@@ -3,10 +3,24 @@ title: 'V4.15.4 (In Progress)' ...@@ -3,10 +3,24 @@ title: 'V4.15.4 (In Progress)'
description: 'FastGPT V4.15.4 Release Notes' description: 'FastGPT V4.15.4 Release Notes'
--- ---
## 📦 Upgrade Guide
### Configure the required FE_DOMAIN
FastGPT services now validate `FE_DOMAIN` at startup. Set it to the origin clients use to access
FastGPT, including the scheme, host, and optional port. Use the public client-facing origin in
production; local development can use `http://localhost:3000`.
```bash
FE_DOMAIN=https://fastgpt.example.com
```
## 🚀 New Features ## 🚀 New Features
## ⚙️ Improvements ## ⚙️ Improvements
1. Added Workflow file context management to reduce duplicate URL signing and address potential security issues.
## 🐛 Fixes ## 🐛 Fixes
1. Fixed an issue where Chatbox displayed system tool errors during streaming responses. 1. Fixed an issue where Chatbox displayed system tool errors during streaming responses.
......
...@@ -3,10 +3,22 @@ title: 'V4.15.4(进行中)' ...@@ -3,10 +3,22 @@ title: 'V4.15.4(进行中)'
description: 'FastGPT V4.15.4 更新说明' description: 'FastGPT V4.15.4 更新说明'
--- ---
## 📦 升级指南
### 配置必填的 FE_DOMAIN
FastGPT 服务启动时会校验 `FE_DOMAIN`。请将它配置为客户端访问 FastGPT 时使用的地址,该地址由协议、主机和可选端口组成。公网部署应填写客户端实际使用的公网访问地址;本地开发可使用 `http://localhost:3000`。
```bash
FE_DOMAIN=https://fastgpt.example.com
```
## 🚀 新增内容 ## 🚀 新增内容
## ⚙️ 优化 ## ⚙️ 优化
1. 工作流文件上下文管理,减少重复签发以及避免潜在安全问题。
## 🐛 修复 ## 🐛 修复
1. chatbox 流输出时候,不应该展示系统工具的错误。 1. chatbox 流输出时候,不应该展示系统工具的错误。
......
...@@ -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-07-02T11:54:55+08:00", "content/plugin/system-tool-development.en.mdx": "2026-07-02T11:54:55+08:00",
"content/plugin/system-tool-development.mdx": "2026-07-02T11:54:55+08:00", "content/plugin/system-tool-development.mdx": "2026-07-02T11:54:55+08:00",
"content/self-host/config/env.en.mdx": "2026-07-20T17:52:36+08:00", "content/self-host/config/env.en.mdx": "2026-07-22T11:47:48+08:00",
"content/self-host/config/env.mdx": "2026-07-20T17:52:36+08:00", "content/self-host/config/env.mdx": "2026-07-22T11:47:48+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",
"content/self-host/config/model/intro.mdx": "2026-06-04T16:10:15+08:00", "content/self-host/config/model/intro.mdx": "2026-06-04T16:10:15+08:00",
"content/self-host/config/model/minimax.en.mdx": "2026-06-03T10:40:17+08:00", "content/self-host/config/model/minimax.en.mdx": "2026-06-03T10:40:17+08:00",
...@@ -324,8 +324,8 @@ ...@@ -324,8 +324,8 @@
"content/self-host/upgrading/4-15/4152.mdx": "2026-07-18T12:51:59+08:00", "content/self-host/upgrading/4-15/4152.mdx": "2026-07-18T12:51:59+08:00",
"content/self-host/upgrading/4-15/4153.en.mdx": "2026-07-18T12:51:59+08:00", "content/self-host/upgrading/4-15/4153.en.mdx": "2026-07-18T12:51:59+08:00",
"content/self-host/upgrading/4-15/4153.mdx": "2026-07-18T12:51:59+08:00", "content/self-host/upgrading/4-15/4153.mdx": "2026-07-18T12:51:59+08:00",
"content/self-host/upgrading/4-15/4154.en.mdx": "2026-07-21T13:14:50+08:00", "content/self-host/upgrading/4-15/4154.en.mdx": "2026-07-22T11:49:20+08:00",
"content/self-host/upgrading/4-15/4154.mdx": "2026-07-21T13:14:50+08:00", "content/self-host/upgrading/4-15/4154.mdx": "2026-07-22T11:49:20+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",
......
...@@ -5,16 +5,3 @@ export const SandboxImageConfigSchema = z.object({ ...@@ -5,16 +5,3 @@ export const SandboxImageConfigSchema = z.object({
tag: z.string().optional() tag: z.string().optional()
}); });
export type SandboxImageConfigType = z.infer<typeof SandboxImageConfigSchema>; export type SandboxImageConfigType = z.infer<typeof SandboxImageConfigSchema>;
/**
* Sandbox 生成文件的服务端持久化引用。
*
* url 只用于在历史读取时定位并替换旧签名链接;key 才是文件的稳定身份。
* 该结构不会作为工具响应发送给模型或通过 SSE 暴露给前端。
*/
export const SandboxFileRefSchema = z.object({
key: z.string(),
filename: z.string(),
url: z.string()
});
export type SandboxFileRef = z.infer<typeof SandboxFileRefSchema>;
...@@ -20,7 +20,6 @@ import { ...@@ -20,7 +20,6 @@ import {
AgentPlanStatusSchema AgentPlanStatusSchema
} from '../ai/agent/type'; } from '../ai/agent/type';
import { ObjectIdSchema } from '../../common/type/mongo'; import { ObjectIdSchema } from '../../common/type/mongo';
import { SandboxFileRefSchema } from '../ai/sandbox/type';
export const ChatHistoryItemResSchema = DispatchNodeResponseSchema.extend({ export const ChatHistoryItemResSchema = DispatchNodeResponseSchema.extend({
nodeId: z.string(), nodeId: z.string(),
...@@ -41,8 +40,7 @@ export const ToolModuleResponseItemSchema = z.object({ ...@@ -41,8 +40,7 @@ export const ToolModuleResponseItemSchema = z.object({
toolAvatar: z.string(), toolAvatar: z.string(),
params: z.string(), params: z.string(),
response: z.string().nullish(), response: z.string().nullish(),
functionName: z.string(), functionName: z.string()
fileRefs: z.array(SandboxFileRefSchema).optional()
}); });
export type ToolModuleResponseItemType = z.infer<typeof ToolModuleResponseItemSchema>; export type ToolModuleResponseItemType = z.infer<typeof ToolModuleResponseItemSchema>;
......
const normalizeFileAmount = (amount: number) => Math.max(0, Math.floor(amount));
/** 用户文件数量配额:团队套餐已配置时使用团队值,否则回退到系统值。 */
export const getUserFileAmountLimit = ({
teamMaxFileAmount,
systemMaxFileAmount
}: {
teamMaxFileAmount?: number;
systemMaxFileAmount: number;
}) => normalizeFileAmount(teamMaxFileAmount ?? systemMaxFileAmount);
/** 模块文件数量上限取模块配额和用户配额的较小值;模块未配置时使用用户配额。 */
export const getFileAmountLimit = ({
teamMaxFileAmount,
systemMaxFileAmount,
moduleMaxFileAmount,
defaultModuleMaxFileAmount
}: {
teamMaxFileAmount?: number;
systemMaxFileAmount: number;
moduleMaxFileAmount?: number;
defaultModuleMaxFileAmount?: number;
}) => {
const userLimit = getUserFileAmountLimit({ teamMaxFileAmount, systemMaxFileAmount });
return getModuleFileAmountLimit({
userMaxFileAmount: userLimit,
moduleMaxFileAmount,
defaultModuleMaxFileAmount
});
};
/** 已解析用户配额的执行链中,仅计算模块的有效文件数量上限。 */
export const getModuleFileAmountLimit = ({
userMaxFileAmount,
moduleMaxFileAmount,
defaultModuleMaxFileAmount
}: {
userMaxFileAmount: number;
moduleMaxFileAmount?: number;
defaultModuleMaxFileAmount?: number;
}) => {
const userLimit = normalizeFileAmount(userMaxFileAmount);
const configuredModuleLimit = moduleMaxFileAmount ?? defaultModuleMaxFileAmount;
if (configuredModuleLimit === undefined) return userLimit;
return Math.min(userLimit, normalizeFileAmount(configuredModuleLimit));
};
/** 用户单文件大小配额,输入单位为 MB,输出单位为字节。 */
export const getFileSizeLimitBytes = ({
teamMaxFileSize,
systemMaxFileSize
}: {
teamMaxFileSize?: number;
systemMaxFileSize: number;
}) => Math.max(0, teamMaxFileSize ?? systemMaxFileSize) * 1024 * 1024;
...@@ -211,7 +211,7 @@ export const storeEdges2RuntimeEdges = ( ...@@ -211,7 +211,7 @@ export const storeEdges2RuntimeEdges = (
if (lastInteractive) { if (lastInteractive) {
const memoryEdges = lastInteractive.memoryEdges || []; const memoryEdges = lastInteractive.memoryEdges || [];
if (memoryEdges && memoryEdges.length > 0) { if (memoryEdges && memoryEdges.length > 0) {
return memoryEdges; return memoryEdges.map((edge) => ({ ...edge }));
} }
} }
......
import { describe, expect, it } from 'vitest';
import {
getFileAmountLimit,
getFileSizeLimitBytes,
getModuleFileAmountLimit,
getUserFileAmountLimit
} from '../../../core/workflow/fileLimit';
describe('workflow file limits', () => {
it('uses the team amount when configured and otherwise falls back to the system amount', () => {
expect(getUserFileAmountLimit({ teamMaxFileAmount: 8, systemMaxFileAmount: 10 })).toBe(8);
expect(getUserFileAmountLimit({ systemMaxFileAmount: 10 })).toBe(10);
expect(getUserFileAmountLimit({ teamMaxFileAmount: 0, systemMaxFileAmount: 10 })).toBe(0);
});
it('caps an explicit module amount by the user amount', () => {
expect(
getFileAmountLimit({
teamMaxFileAmount: 8,
systemMaxFileAmount: 10,
moduleMaxFileAmount: 3
})
).toBe(3);
expect(
getFileAmountLimit({
teamMaxFileAmount: 3,
systemMaxFileAmount: 10,
moduleMaxFileAmount: 8
})
).toBe(3);
});
it('uses the user amount when a query has no explicit module amount', () => {
expect(getFileAmountLimit({ teamMaxFileAmount: 8, systemMaxFileAmount: 10 })).toBe(8);
});
it('supports a module default for legacy Form, Plugin and variable inputs', () => {
expect(
getFileAmountLimit({
systemMaxFileAmount: 3,
defaultModuleMaxFileAmount: 5
})
).toBe(3);
expect(
getModuleFileAmountLimit({
userMaxFileAmount: 3,
defaultModuleMaxFileAmount: 5
})
).toBe(3);
});
it('uses the team size when configured and returns bytes', () => {
expect(getFileSizeLimitBytes({ teamMaxFileSize: 2, systemMaxFileSize: 10 })).toBe(
2 * 1024 * 1024
);
expect(getFileSizeLimitBytes({ systemMaxFileSize: 10 })).toBe(10 * 1024 * 1024);
expect(getFileSizeLimitBytes({ teamMaxFileSize: 0, systemMaxFileSize: 10 })).toBe(0);
});
});
...@@ -887,7 +887,12 @@ describe('storeEdges2RuntimeEdges', () => { ...@@ -887,7 +887,12 @@ describe('storeEdges2RuntimeEdges', () => {
} as WorkflowInteractiveResponseType; } as WorkflowInteractiveResponseType;
const result = storeEdges2RuntimeEdges(edges, lastInteractive); const result = storeEdges2RuntimeEdges(edges, lastInteractive);
expect(result).toBe(memoryEdges); expect(result).toEqual(memoryEdges);
expect(result).not.toBe(memoryEdges);
expect(result[0]).not.toBe(memoryEdges[0]);
result[0].status = 'waiting';
expect(memoryEdges[0].status).toBe('active');
}); });
it('should return converted edges when lastInteractive has empty memoryEdges', () => { it('should return converted edges when lastInteractive has empty memoryEdges', () => {
......
...@@ -12,8 +12,6 @@ import { ProxyAgent, type ProxyAgentOptions } from 'proxy-agent'; ...@@ -12,8 +12,6 @@ import { ProxyAgent, type ProxyAgentOptions } from 'proxy-agent';
import { getProxyForUrl } from 'proxy-from-env'; import { getProxyForUrl } from 'proxy-from-env';
import { isDevEnv } from '@fastgpt/global/common/system/constants'; import { isDevEnv } from '@fastgpt/global/common/system/constants';
import { isInternalAddress, isInternalResolvedIP, PRIVATE_URL_TEXT } from '../system/utils'; import { isInternalAddress, isInternalResolvedIP, PRIVATE_URL_TEXT } from '../system/utils';
import { isAbsoluteUrl } from '../security/network';
import { SERVICE_LOCAL_HOST } from '../system/tools';
/** /**
* 给 shared axios 实例添加 SSRF 防护。 * 给 shared axios 实例添加 SSRF 防护。
...@@ -412,32 +410,3 @@ export function createProxyAxios(config?: AxiosRequestConfig, ssrfCheck = true) ...@@ -412,32 +410,3 @@ export function createProxyAxios(config?: AxiosRequestConfig, ssrfCheck = true)
/** @see https://github.com/axios/axios/issues/4531 */ /** @see https://github.com/axios/axios/issues/4531 */
export const axios = createProxyAxios(); export const axios = createProxyAxios();
export const axiosWithoutSSRF = createProxyAxios(undefined, false); export const axiosWithoutSSRF = createProxyAxios(undefined, false);
/**
* 内部相对路径请求专用的 axios 实例:
* - baseURL 固定为本机 NextJS API
* - 不带 SSRF 拦截器(本机调用必然解析到 localhost,装拦截会把所有合法请求拦死)
* - 不复用 safe axios 的 ProxyAgent,保证内部回环不会被外部代理转走
*
* 仅在 url 是相对路径时使用;绝对 URL 必须走 safe `axios`。
*/
const internalAxios: AxiosInstance = _.create({
baseURL: `http://${SERVICE_LOCAL_HOST}`
});
/**
* 根据 URL 类型自动选择合适的 axios 实例,避免每个调用点重复
* `isAbsoluteUrl ? safe : raw` 三元。
*
* - 绝对 URL(`http(s)://...` 或 `//...`)→ safe `axios`(SSRF 拦截,拒绝内网/metadata)
* - 相对路径(`/api/...` 等)→ `internalAxios`(本机 baseURL,可信内部 API)
*
* 用法:
* ```ts
* const client = pickOutboundAxios(url);
* const res = await client.get(url, { responseType: 'arraybuffer' });
* ```
*/
export const pickOutboundAxios = (url: string): AxiosInstance => {
return isAbsoluteUrl(url) ? axios : internalAxios;
};
import { getAxiosHeaderValue } from '@fastgpt/global/common/axios/utils';
import { UserError } from '@fastgpt/global/common/error/utils';
import { Readable } from 'node:stream';
import { axios } from '../../api/axios';
import { getFileMaxSize } from '../utils';
import { readStreamToBuffer } from '../../s3/utils';
const EXTERNAL_FILE_DOWNLOAD_TIMEOUT_MS = 180_000;
/**
* 使用统一的 SSRF-safe Axios 下载绝对 HTTP(S) 文件,并执行响应头和流式双重大小限制。
*/
export const readExternalFileBuffer = async ({
url,
maxFileSize = getFileMaxSize()
}: {
url: string;
maxFileSize?: number;
}) => {
if (!/^https?:\/\//i.test(url)) {
throw new UserError('External file URL must be an absolute HTTP(S) URL');
}
const parsedUrl = new URL(url);
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
throw new UserError('External file URL must use HTTP(S)');
}
const response = await axios.get<Readable>(url, {
responseType: 'stream',
timeout: EXTERNAL_FILE_DOWNLOAD_TIMEOUT_MS,
maxContentLength: maxFileSize
});
const contentLength = Number(getAxiosHeaderValue(response.headers['content-length']) || 0);
if (contentLength > maxFileSize) {
if (response.data instanceof Readable) response.data.destroy();
throw new UserError(`File exceeds maximum allowed size (${maxFileSize} bytes)`);
}
const responseStream =
response.data instanceof Readable
? response.data
: Readable.from([Buffer.from(response.data as unknown as ArrayBuffer)]);
return {
buffer: await readStreamToBuffer({
stream: responseStream,
maxBytes: maxFileSize,
exceededMessage: `File exceeds maximum allowed size (${maxFileSize} bytes)`
}),
contentType: getAxiosHeaderValue(response.headers['content-type']),
contentDisposition: getAxiosHeaderValue(response.headers['content-disposition'])
};
};
...@@ -210,42 +210,20 @@ export const getSseErrorResponse = ( ...@@ -210,42 +210,20 @@ export const getSseErrorResponse = (
shouldClearCookie: boolean; shouldClearCookie: boolean;
} => { } => {
const errResponseKey = typeof error === 'string' ? error : error?.message; const errResponseKey = typeof error === 'string' ? error : error?.message;
const processedError = processError({ error });
// Specified error
if (ERROR_RESPONSE[errResponseKey]) { if (ERROR_RESPONSE[errResponseKey]) {
// login is expired
if (errResponseKey === ERROR_ENUM.unAuthorization) {
return {
event: SseResponseEventEnum.error,
data: JSON.stringify(ERROR_RESPONSE[errResponseKey]),
shouldClearCookie: true
};
}
return { return {
event: SseResponseEventEnum.error, event: SseResponseEventEnum.error,
data: JSON.stringify(ERROR_RESPONSE[errResponseKey]), data: JSON.stringify(ERROR_RESPONSE[errResponseKey]),
shouldClearCookie: false shouldClearCookie: processedError.shouldClearCookie
}; };
} }
let msg = error?.response?.statusText || error?.message || '请求错误';
if (typeof error === 'string') {
msg = error;
} else if (proxyError[error?.code]) {
msg = '网络连接异常';
} else if (error?.response?.data?.error?.message) {
msg = error?.response?.data?.error?.message;
} else if (error?.error?.message) {
msg = `${error?.error?.code} ${error?.error?.message}`;
}
logger.error('SSE error', { message: msg, error });
return { return {
event: SseResponseEventEnum.error, event: SseResponseEventEnum.error,
data: JSON.stringify({ message: replaceSensitiveText(msg) }), data: JSON.stringify({ message: processedError.message }),
shouldClearCookie: false shouldClearCookie: processedError.shouldClearCookie
}; };
}; };
......
...@@ -6,7 +6,6 @@ import type { ...@@ -6,7 +6,6 @@ import type {
import type { AgentPlanType } from '@fastgpt/global/core/ai/agent/type'; import type { AgentPlanType } from '@fastgpt/global/core/ai/agent/type';
import type { AgentAskPayload } from './systemTool/ask'; import type { AgentAskPayload } from './systemTool/ask';
import type { AgentLoopUsage } from './usage'; import type { AgentLoopUsage } from './usage';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
export type AgentLoopToolResponseCompress = { export type AgentLoopToolResponseCompress = {
response: string; response: string;
...@@ -85,7 +84,6 @@ export type AgentLoopEvent = ...@@ -85,7 +84,6 @@ export type AgentLoopEvent =
errorMessage?: string; errorMessage?: string;
seconds: number; seconds: number;
usages?: AgentLoopUsage[]; usages?: AgentLoopUsage[];
fileRefs?: SandboxFileRef[];
toolResponseCompress?: AgentLoopToolResponseCompress; toolResponseCompress?: AgentLoopToolResponseCompress;
/** executeTool 返回的 opaque metadata,agent-loop 不解释其业务结构。 */ /** executeTool 返回的 opaque metadata,agent-loop 不解释其业务结构。 */
metadata?: unknown; metadata?: unknown;
......
...@@ -4,10 +4,14 @@ import z from 'zod'; ...@@ -4,10 +4,14 @@ import z from 'zod';
export const READ_FILES_TOOL_NAME = 'read_files'; export const READ_FILES_TOOL_NAME = 'read_files';
export const ReadFilesToolParamsSchema = z.object({ export const ReadFilesToolParamsSchema = z.object({
ids: z.array(z.string()) urls: z.array(z.string())
}); });
export const createReadFilesTool = (): ChatCompletionTool => ({ export const createReadFilesTool = ({
maxFileAmount
}: {
maxFileAmount: number;
}): ChatCompletionTool => ({
type: 'function', type: 'function',
function: { function: {
name: READ_FILES_TOOL_NAME, name: READ_FILES_TOOL_NAME,
...@@ -15,15 +19,16 @@ export const createReadFilesTool = (): ChatCompletionTool => ({ ...@@ -15,15 +19,16 @@ export const createReadFilesTool = (): ChatCompletionTool => ({
parameters: { parameters: {
type: 'object', type: 'object',
properties: { properties: {
ids: { urls: {
type: 'array', type: 'array',
maxItems: maxFileAmount,
items: { items: {
type: 'string' type: 'string'
}, },
description: 'File IDs' description: 'Absolute HTTP(S) file URLs'
} }
}, },
required: ['ids'] required: ['urls']
} }
} }
}); });
......
...@@ -6,7 +6,6 @@ import type { ...@@ -6,7 +6,6 @@ import type {
import type { SandboxClient } from '../../../sandbox/interface/runtime'; import type { SandboxClient } from '../../../sandbox/interface/runtime';
import type { AgentLoopDatasetSearchExecutor } from './systemTool/datasetSearch'; import type { AgentLoopDatasetSearchExecutor } from './systemTool/datasetSearch';
import type { AgentLoopUsage } from './usage'; import type { AgentLoopUsage } from './usage';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
export type AgentLoopToolCatalog = { export type AgentLoopToolCatalog = {
runtimeTools: ChatCompletionTool[]; runtimeTools: ChatCompletionTool[];
...@@ -31,7 +30,6 @@ export type AgentLoopToolExecutionResult<TChildrenResponse = unknown> = { ...@@ -31,7 +30,6 @@ export type AgentLoopToolExecutionResult<TChildrenResponse = unknown> = {
stop?: boolean; stop?: boolean;
skipResponseCompress?: boolean; skipResponseCompress?: boolean;
errorMessage?: string; errorMessage?: string;
fileRefs?: SandboxFileRef[];
/** 由调用方透传并在 agent-loop 外部解释的工具运行元数据。 */ /** 由调用方透传并在 agent-loop 外部解释的工具运行元数据。 */
metadata?: unknown; metadata?: unknown;
}; };
...@@ -60,6 +58,7 @@ export type AgentLoopSystemTools = { ...@@ -60,6 +58,7 @@ export type AgentLoopSystemTools = {
}; };
readFile?: { readFile?: {
enabled: boolean; enabled: boolean;
maxFileAmount: number;
execute: AgentLoopReadFileExecutor; execute: AgentLoopReadFileExecutor;
}; };
datasetSearch?: { datasetSearch?: {
......
...@@ -96,7 +96,13 @@ export const runFastAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -96,7 +96,13 @@ export const runFastAgentLoop = async <TChildrenResponse = unknown>({
...(runtime.systemTools?.sandbox?.enabled && runtime.systemTools.sandbox.client ...(runtime.systemTools?.sandbox?.enabled && runtime.systemTools.sandbox.client
? { sandboxTools: createAgentLoopSandboxTools() } ? { sandboxTools: createAgentLoopSandboxTools() }
: {}), : {}),
...(runtime.systemTools?.readFile?.enabled ? { readFileTool: createReadFilesTool() } : {}), ...(runtime.systemTools?.readFile?.enabled
? {
readFileTool: createReadFilesTool({
maxFileAmount: runtime.systemTools.readFile.maxFileAmount
})
}
: {}),
...(runtime.systemTools?.datasetSearch?.enabled ...(runtime.systemTools?.datasetSearch?.enabled
? { datasetSearchTool: createDatasetSearchTool() } ? { datasetSearchTool: createDatasetSearchTool() }
: {}) : {})
......
...@@ -23,7 +23,6 @@ import { getErrText } from '@fastgpt/global/common/error/utils'; ...@@ -23,7 +23,6 @@ import { getErrText } from '@fastgpt/global/common/error/utils';
import { batchRun } from '@fastgpt/global/common/system/utils'; import { batchRun } from '@fastgpt/global/common/system/utils';
import { normalizeToolResponseContent } from '@fastgpt/global/core/ai/llm/utils'; import { normalizeToolResponseContent } from '@fastgpt/global/core/ai/llm/utils';
import type { AgentPlanType } from '@fastgpt/global/core/ai/agent/type'; import type { AgentPlanType } from '@fastgpt/global/core/ai/agent/type';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
type RunAgentCallProps<TChildrenResponse = unknown> = { type RunAgentCallProps<TChildrenResponse = unknown> = {
maxRunAgentTimes: number; maxRunAgentTimes: number;
...@@ -88,7 +87,6 @@ type RunAgentCallProps<TChildrenResponse = unknown> = { ...@@ -88,7 +87,6 @@ type RunAgentCallProps<TChildrenResponse = unknown> = {
errorMessage?: string; errorMessage?: string;
seconds: number; seconds: number;
usages?: AgentLoopUsage[]; usages?: AgentLoopUsage[];
fileRefs?: SandboxFileRef[];
metadata?: unknown; metadata?: unknown;
toolResponseCompress?: { toolResponseCompress?: {
response: string; response: string;
...@@ -292,7 +290,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -292,7 +290,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
interactive, interactive,
stop, stop,
errorMessage, errorMessage,
fileRefs,
metadata metadata
} = await (async () => { } = await (async () => {
try { try {
...@@ -320,7 +317,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -320,7 +317,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
errorMessage: interactiveToolErrorMessage || errorMessage, errorMessage: interactiveToolErrorMessage || errorMessage,
seconds: +((Date.now() - toolStartTime) / 1000).toFixed(2), seconds: +((Date.now() - toolStartTime) / 1000).toFixed(2),
usages: toolUsages, usages: toolUsages,
fileRefs,
metadata metadata
}); });
...@@ -530,7 +526,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -530,7 +526,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
stop: stopLoop, stop: stopLoop,
skipResponseCompress, skipResponseCompress,
errorMessage, errorMessage,
fileRefs,
metadata metadata
} = await (async () => { } = await (async () => {
try { try {
...@@ -594,7 +589,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -594,7 +589,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
: {}), : {}),
seconds: +((Date.now() - toolStartTime) / 1000).toFixed(2), seconds: +((Date.now() - toolStartTime) / 1000).toFixed(2),
usages: [...toolUsages, ...(toolResponseCompress ? [toolResponseCompress.usage] : [])], usages: [...toolUsages, ...(toolResponseCompress ? [toolResponseCompress.usage] : [])],
fileRefs,
toolResponseCompress, toolResponseCompress,
metadata metadata
}); });
......
...@@ -353,7 +353,6 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({ ...@@ -353,7 +353,6 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({
seconds, seconds,
errorMessage, errorMessage,
usages, usages,
fileRefs,
toolResponseCompress, toolResponseCompress,
metadata metadata
}) => { }) => {
...@@ -369,7 +368,6 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({ ...@@ -369,7 +368,6 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({
seconds, seconds,
errorMessage, errorMessage,
usages, usages,
fileRefs,
toolResponseCompress, toolResponseCompress,
metadata metadata
}); });
...@@ -460,8 +458,7 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({ ...@@ -460,8 +458,7 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({
}); });
return createToolResponse(sandboxResult.response, { return createToolResponse(sandboxResult.response, {
skipResponseCompress: true, skipResponseCompress: true,
errorMessage: sandboxResult.success ? undefined : sandboxResult.response, errorMessage: sandboxResult.success ? undefined : sandboxResult.response
fileRefs: sandboxResult.fileRefs
}); });
} }
......
...@@ -305,7 +305,6 @@ export const runPiAgentLoop = async <TChildrenResponse = unknown>({ ...@@ -305,7 +305,6 @@ export const runPiAgentLoop = async <TChildrenResponse = unknown>({
assistantMessages: result.assistantMessages, assistantMessages: result.assistantMessages,
usages: result.usages, usages: result.usages,
errorMessage: result.errorMessage, errorMessage: result.errorMessage,
fileRefs: result.fileRefs,
metadata: result.metadata, metadata: result.metadata,
seconds: +((Date.now() - startedAt) / 1000).toFixed(2) seconds: +((Date.now() - startedAt) / 1000).toFixed(2)
}); });
......
...@@ -33,7 +33,6 @@ import { ...@@ -33,7 +33,6 @@ import {
import { runSandboxTools } from '../../../../../sandbox/interface/toolCall'; import { runSandboxTools } from '../../../../../sandbox/interface/toolCall';
import { createToolCall, normalizeToolArgs, stringifyJson } from '../message'; import { createToolCall, normalizeToolArgs, stringifyJson } from '../message';
import { getPiAgentRuntimeTools } from './catalog'; import { getPiAgentRuntimeTools } from './catalog';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
type PlanOperationEvent = Extract<AgentLoopEvent, { type: 'plan_operation' }>; type PlanOperationEvent = Extract<AgentLoopEvent, { type: 'plan_operation' }>;
...@@ -105,7 +104,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({ ...@@ -105,7 +104,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
interactive?: TChildrenResponse; interactive?: TChildrenResponse;
stop?: boolean; stop?: boolean;
errorMessage?: string; errorMessage?: string;
fileRefs?: SandboxFileRef[];
metadata?: unknown; metadata?: unknown;
}>; }>;
}) => { }) => {
...@@ -133,7 +131,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({ ...@@ -133,7 +131,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
assistantMessages, assistantMessages,
usages, usages,
errorMessage: result.errorMessage, errorMessage: result.errorMessage,
fileRefs: result.fileRefs,
metadata: result.metadata, metadata: result.metadata,
seconds: +((Date.now() - startedAt) / 1000).toFixed(2) seconds: +((Date.now() - startedAt) / 1000).toFixed(2)
}); });
...@@ -296,7 +293,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({ ...@@ -296,7 +293,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
response: sandboxResult.response, response: sandboxResult.response,
assistantMessages: [], assistantMessages: [],
usages: [], usages: [],
fileRefs: sandboxResult.fileRefs,
errorMessage: sandboxResult.success ? undefined : sandboxResult.response errorMessage: sandboxResult.success ? undefined : sandboxResult.response
}; };
} }
...@@ -308,7 +304,9 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({ ...@@ -308,7 +304,9 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
} }
if (runtime.systemTools?.readFile?.enabled) { if (runtime.systemTools?.readFile?.enabled) {
const readFileTool = createReadFilesTool(); const readFileTool = createReadFilesTool({
maxFileAmount: runtime.systemTools.readFile.maxFileAmount
});
tools.push({ tools.push({
name: readFileTool.function.name, name: readFileTool.function.name,
label: readFileTool.function.name, label: readFileTool.function.name,
......
...@@ -14,7 +14,9 @@ const getPiAgentSystemTools = <TChildrenResponse = unknown>( ...@@ -14,7 +14,9 @@ const getPiAgentSystemTools = <TChildrenResponse = unknown>(
...(runtime.systemTools?.sandbox?.enabled && runtime.systemTools.sandbox.client ...(runtime.systemTools?.sandbox?.enabled && runtime.systemTools.sandbox.client
? createAgentLoopSandboxTools() ? createAgentLoopSandboxTools()
: []), : []),
...(runtime.systemTools?.readFile?.enabled ? [createReadFilesTool()] : []), ...(runtime.systemTools?.readFile?.enabled
? [createReadFilesTool({ maxFileAmount: runtime.systemTools.readFile.maxFileAmount })]
: []),
...(runtime.systemTools?.datasetSearch?.enabled ? [createDatasetSearchTool()] : []) ...(runtime.systemTools?.datasetSearch?.enabled ? [createDatasetSearchTool()] : [])
]; ];
......
...@@ -4,7 +4,7 @@ import { READ_FILES_TOOL_NAME } from '../agentLoop/interface'; ...@@ -4,7 +4,7 @@ import { READ_FILES_TOOL_NAME } from '../agentLoop/interface';
* 将本轮上传文件整理为文本上下文,包含文件名、沙盒路径和可选文件内容。 * 将本轮上传文件整理为文本上下文,包含文件名、沙盒路径和可选文件内容。
*/ */
export const getUserFilesPrompt = ( export const getUserFilesPrompt = (
files: { id?: string; name: string; url: string; sandboxPath?: string; content?: string }[] = [] files: { name: string; url: string; sandboxPath?: string; content?: string }[] = []
) => { ) => {
if (files.length === 0) return ''; if (files.length === 0) return '';
return `## 对话文件 return `## 对话文件
...@@ -15,7 +15,6 @@ export const getUserFilesPrompt = ( ...@@ -15,7 +15,6 @@ export const getUserFilesPrompt = (
${files ${files
.map((file) => .map((file) =>
`<file> `<file>
${file.id ? `<id>${file.id}</id>` : ''}
<name>${file.name}</name> <name>${file.name}</name>
<url>${file.url}</url> <url>${file.url}</url>
${file.sandboxPath ? `<sandboxPath>${file.sandboxPath}</sandboxPath>` : ''} ${file.sandboxPath ? `<sandboxPath>${file.sandboxPath}</sandboxPath>` : ''}
......
...@@ -5,15 +5,17 @@ ...@@ -5,15 +5,17 @@
*/ */
import type { FileWriteEntry, ISandbox } from '@fastgpt-sdk/sandbox-adapter'; import type { FileWriteEntry, ISandbox } from '@fastgpt-sdk/sandbox-adapter';
import mime from 'mime'; import mime from 'mime';
import { pickOutboundAxios } from '../../../../common/api/axios';
import type { SandboxClient } from './runtime/client'; import type { SandboxClient } from './runtime/client';
import { getSandboxRuntimeProfile } from '../infrastructure/provider/runtimeProfile'; import { getSandboxRuntimeProfile } from '../infrastructure/provider/runtimeProfile';
import { readExternalFileBuffer } from '../../../../common/file/read/external';
export type SandboxUrlFile = { export type SandboxUrlFile = {
path: string; path: string;
url: string; url: string;
}; };
export type SandboxInputFileReader = (url: string) => Promise<Buffer>;
export type SandboxFileContent = { export type SandboxFileContent = {
content: Buffer; content: Buffer;
contentType: string; contentType: string;
...@@ -39,25 +41,30 @@ const isWithinSandboxWorkspace = (path: string, workDirectory: string) => { ...@@ -39,25 +41,30 @@ const isWithinSandboxWorkspace = (path: string, workDirectory: string) => {
return path === workspace || path.startsWith(`${workspace}/`); return path === workspace || path.startsWith(`${workspace}/`);
}; };
/** 读取准备写入 Sandbox 的远程文件,禁止相对 URL 回环访问本机 API。 */
export const readSandboxUrlFile = async (url: string) => {
return (await readExternalFileBuffer({ url })).buffer;
};
/** /**
* 将远程 URL 文件写入已存在的 sandbox 实例。 * 将远程 URL 文件写入已存在的 sandbox 实例。
* *
* 这里不负责 sandbox 生命周期,只统一处理下载和 writeFiles。 * 这里不负责 sandbox 生命周期,只统一处理下载和 writeFiles。
*/ */
export async function writeUrlFilesToSandbox(sandbox: ISandbox, files: SandboxUrlFile[]) { export async function writeUrlFilesToSandbox(
sandbox: ISandbox,
files: SandboxUrlFile[],
readInputFile: SandboxInputFileReader = readSandboxUrlFile
) {
const writeFileTasks: Promise<FileWriteEntry>[] = []; const writeFileTasks: Promise<FileWriteEntry>[] = [];
for (const { path, url } of files) { for (const { path, url } of files) {
if (!path) continue; if (!path) continue;
writeFileTasks.push( writeFileTasks.push(
pickOutboundAxios(url) readInputFile(url).then((data) => ({
.get<ArrayBuffer>(url, { path,
responseType: 'arraybuffer' data
}) }))
.then((response) => ({
path,
data: response.data
}))
); );
} }
......
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
*/ */
import type { FileWriteEntry, ISandbox } from '@fastgpt-sdk/sandbox-adapter'; import type { FileWriteEntry, ISandbox } from '@fastgpt-sdk/sandbox-adapter';
import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants'; import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants';
import { pickOutboundAxios } from '../../../../../common/api/axios';
import { getSafeSandboxInputFilename } from '../../utils'; import { getSafeSandboxInputFilename } from '../../utils';
import { readSandboxUrlFile, type SandboxInputFileReader } from '../file';
export type { SandboxInputFileReader } from '../file';
export type SandboxInputFile = { export type SandboxInputFile = {
name: string; name: string;
...@@ -41,7 +43,11 @@ export const readSandboxPwd = async (sandboxClient: SandboxCommandClient) => { ...@@ -41,7 +43,11 @@ export const readSandboxPwd = async (sandboxClient: SandboxCommandClient) => {
* 路径规则和通用 toolcall 保持一致:用户文件直接写入 user_files/<文件名>。 * 路径规则和通用 toolcall 保持一致:用户文件直接写入 user_files/<文件名>。
* 这里直接消费 currentFiles,避免先构造中间 sandbox file 结构再二次遍历。 * 这里直接消费 currentFiles,避免先构造中间 sandbox file 结构再二次遍历。
*/ */
export const injectInputFilesToSandbox = async (sandbox: ISandbox, files: SandboxInputFile[]) => { export const injectInputFilesToSandbox = async (
sandbox: ISandbox,
files: SandboxInputFile[],
readInputFile: SandboxInputFileReader = readSandboxUrlFile
) => {
const writeFileTasks: Promise<FileWriteEntry>[] = []; const writeFileTasks: Promise<FileWriteEntry>[] = [];
const usedNames = new Map<string, number>(); const usedNames = new Map<string, number>();
...@@ -49,14 +55,10 @@ export const injectInputFilesToSandbox = async (sandbox: ISandbox, files: Sandbo ...@@ -49,14 +55,10 @@ export const injectInputFilesToSandbox = async (sandbox: ISandbox, files: Sandbo
const filename = getSafeSandboxInputFilename(file.name, index, usedNames); const filename = getSafeSandboxInputFilename(file.name, index, usedNames);
const path = `${SANDBOX_USER_FILES_PATH}${filename}`; const path = `${SANDBOX_USER_FILES_PATH}${filename}`;
writeFileTasks.push( writeFileTasks.push(
pickOutboundAxios(file.url) readInputFile(file.url).then((data) => ({
.get<ArrayBuffer>(file.url, { path,
responseType: 'arraybuffer' data
}) }))
.then((response) => ({
path,
data: response.data
}))
); );
} }
......
...@@ -9,7 +9,8 @@ import { ...@@ -9,7 +9,8 @@ import {
injectInputFilesToSandbox, injectInputFilesToSandbox,
readSandboxPwd, readSandboxPwd,
type SandboxCommandClient, type SandboxCommandClient,
type SandboxInputFile type SandboxInputFile,
type SandboxInputFileReader
} from './files'; } from './files';
import { prepareSandboxRuntimeMirrors } from './mirrors'; import { prepareSandboxRuntimeMirrors } from './mirrors';
...@@ -101,10 +102,11 @@ export const emptyWorkDirectory = ...@@ -101,10 +102,11 @@ export const emptyWorkDirectory =
/** 将本轮用户输入文件写入 sandbox 的 user_files 目录。 */ /** 将本轮用户输入文件写入 sandbox 的 user_files 目录。 */
export const injectCurrentInputFiles = export const injectCurrentInputFiles =
<Context extends SandboxPrepareContext>( <Context extends SandboxPrepareContext>(
currentFiles: SandboxInputFile[] currentFiles: SandboxInputFile[],
readInputFile?: SandboxInputFileReader
): SandboxPrepareStep<Context> => ): SandboxPrepareStep<Context> =>
async (context) => { async (context) => {
await injectInputFilesToSandbox(context.sandbox, currentFiles); await injectInputFilesToSandbox(context.sandbox, currentFiles, readInputFile);
return context; return context;
}; };
......
...@@ -47,17 +47,11 @@ export const sandboxGetFileUrlTool = defineTool({ ...@@ -47,17 +47,11 @@ export const sandboxGetFileUrlTool = defineTool({
external: true external: true
}); });
return { return { fileUrl, filename };
responseFile: { fileUrl, filename },
fileRef: { key, filename, url: fileUrl }
};
}) })
); );
return { return { response: JSON.stringify(result) };
response: JSON.stringify(result.map((item) => item.responseFile)),
fileRefs: result.map((item) => item.fileRef)
};
} }
}); });
......
...@@ -17,12 +17,11 @@ import { toolMap as shellToolMap } from './shell.tool'; ...@@ -17,12 +17,11 @@ import { toolMap as shellToolMap } from './shell.tool';
import { toolMap as writeFileToolMap } from './writeFile.tool'; import { toolMap as writeFileToolMap } from './writeFile.tool';
import { getSandboxClient, type SandboxClient } from '../runtime/client'; import { getSandboxClient, type SandboxClient } from '../runtime/client';
import { parseJsonArgs } from '../../../utils'; import { parseJsonArgs } from '../../../utils';
import { writeUrlFilesToSandbox } from '../file'; import { writeUrlFilesToSandbox, type SandboxInputFileReader } from '../file';
import { getSandboxRuntimeProfile } from '../../infrastructure/provider/runtimeProfile'; import { getSandboxRuntimeProfile } from '../../infrastructure/provider/runtimeProfile';
import { preparePackageMirrors, prepareSandbox } from '../runtime/prepare'; import { preparePackageMirrors, prepareSandbox } from '../runtime/prepare';
import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants'; import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { getRunningSandboxId } from '../../utils/id'; import { getRunningSandboxId } from '../../utils/id';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
const ToolMap = { const ToolMap = {
...editFileToolMap, ...editFileToolMap,
...@@ -39,7 +38,6 @@ export type SandboxToolCallResult = { ...@@ -39,7 +38,6 @@ export type SandboxToolCallResult = {
success: boolean; success: boolean;
input: Record<string, any>; input: Record<string, any>;
response: string; response: string;
fileRefs?: SandboxFileRef[];
durationSeconds: number; durationSeconds: number;
}; };
...@@ -91,7 +89,6 @@ export const runSandboxTools = async ({ ...@@ -91,7 +89,6 @@ export const runSandboxTools = async ({
success: true, success: true,
input: parsedArgs.data, input: parsedArgs.data,
response: result.response, response: result.response,
...(result.fileRefs?.length ? { fileRefs: result.fileRefs } : {}),
durationSeconds: getDuration() durationSeconds: getDuration()
}; };
}; };
...@@ -107,13 +104,15 @@ export const prepareSandboxToolRuntime = async ({ ...@@ -107,13 +104,15 @@ export const prepareSandboxToolRuntime = async ({
sourceId, sourceId,
userId, userId,
chatId, chatId,
files files,
readInputFile
}: { }: {
sourceType: ChatSourceTypeEnum; sourceType: ChatSourceTypeEnum;
sourceId: string; sourceId: string;
userId: string; userId: string;
chatId: string; chatId: string;
files: { path: string; url: string }[]; files: { path: string; url: string }[];
readInputFile?: SandboxInputFileReader;
}) => { }) => {
const sandboxId = getRunningSandboxId({ const sandboxId = getRunningSandboxId({
sourceType, sourceType,
...@@ -142,7 +141,7 @@ export const prepareSandboxToolRuntime = async ({ ...@@ -142,7 +141,7 @@ export const prepareSandboxToolRuntime = async ({
}, },
preparePackageMirrors() preparePackageMirrors()
); );
await writeUrlFilesToSandbox(instance.provider, files); await writeUrlFilesToSandbox(instance.provider, files, readInputFile);
return instance; return instance;
}; };
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
*/ */
import type { z } from 'zod'; import type { z } from 'zod';
import type { SandboxClient } from '../runtime/client'; import type { SandboxClient } from '../runtime/client';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
type ToolExecuteContext<P> = { type ToolExecuteContext<P> = {
sandboxInstance: SandboxClient; sandboxInstance: SandboxClient;
...@@ -19,9 +18,7 @@ type ToolExecuteContext<P> = { ...@@ -19,9 +18,7 @@ type ToolExecuteContext<P> = {
*/ */
export type ToolDefinition<S extends z.ZodTypeAny = z.ZodTypeAny> = { export type ToolDefinition<S extends z.ZodTypeAny = z.ZodTypeAny> = {
zodSchema: S; zodSchema: S;
execute: ( execute: (ctx: ToolExecuteContext<z.infer<S>>) => Promise<{ response: string }>;
ctx: ToolExecuteContext<z.infer<S>>
) => Promise<{ response: string; fileRefs?: SandboxFileRef[] }>;
}; };
/** /**
......
...@@ -8,7 +8,12 @@ export { ...@@ -8,7 +8,12 @@ export {
addDirectoryToArchive, addDirectoryToArchive,
getSandboxFileContent, getSandboxFileContent,
isSandboxPathDirectory, isSandboxPathDirectory,
readSandboxUrlFile,
resolveSandboxWorkspacePath, resolveSandboxWorkspacePath,
writeUrlFilesToSandbox writeUrlFilesToSandbox
} from '../application/file'; } from '../application/file';
export type { SandboxFileContent, SandboxUrlFile } from '../application/file'; export type {
SandboxFileContent,
SandboxInputFileReader,
SandboxUrlFile
} from '../application/file';
...@@ -31,6 +31,7 @@ import { formatModelChars2Points } from '../../../../support/wallet/usage/utils' ...@@ -31,6 +31,7 @@ import { formatModelChars2Points } from '../../../../support/wallet/usage/utils'
import { getDefaultLLMModel } from '../../model'; import { getDefaultLLMModel } from '../../model';
import { getRunningSkillEditSandbox } from '../../sandbox/interface/skillEdit'; import { getRunningSkillEditSandbox } from '../../sandbox/interface/skillEdit';
import { dispatchWorkFlow } from '../../../workflow/dispatch'; import { dispatchWorkFlow } from '../../../workflow/dispatch';
import { prepareWorkflowFileQuery } from '../../../workflow/utils/fileLimits';
import { WORKFLOW_MAX_RUN_TIMES } from '../../../workflow/constants'; import { WORKFLOW_MAX_RUN_TIMES } from '../../../workflow/constants';
import type { AppFileSelectConfigType } from '@fastgpt/global/core/app/type/config.schema'; import type { AppFileSelectConfigType } from '@fastgpt/global/core/app/type/config.schema';
import { getChatItems } from '../../../chat/controller'; import { getChatItems } from '../../../chat/controller';
...@@ -146,13 +147,29 @@ export async function handleSkillDebugChat( ...@@ -146,13 +147,29 @@ export async function handleSkillDebugChat(
const newHistories = concatHistories(histories, chatMessages); const newHistories = concatHistories(histories, chatMessages);
const interactive = getLastInteractiveValue(newHistories); const interactive = getLastInteractiveValue(newHistories);
const chatConfig = {
fileSelectConfig: skillDebugFileSelectConfig
};
const {
query: workflowQuery,
maxFileAmount,
maxBytesPerFile
} = await prepareWorkflowFileQuery({
teamId,
chatConfig,
query: userQuestion.value
});
const workflowUserQuestion: UserChatItemType = {
...userQuestion,
value: workflowQuery
};
const preparedRound = await preChatRound({ const preparedRound = await preChatRound({
...chatSource, ...chatSource,
chatId, chatId,
teamId, teamId,
tmbId, tmbId,
source: ChatSourceEnum.test, source: ChatSourceEnum.test,
userContent: userQuestion, userContent: workflowUserQuestion,
responseChatItemId: responseChatItemIdFromBody, responseChatItemId: responseChatItemIdFromBody,
interactive interactive
}); });
...@@ -213,11 +230,11 @@ export async function handleSkillDebugChat( ...@@ -213,11 +230,11 @@ export async function handleSkillDebugChat(
runtimeNodes, runtimeNodes,
runtimeEdges, runtimeEdges,
variables: {}, variables: {},
query: removeEmptyUserInput(userQuestion.value), query: removeEmptyUserInput(workflowQuery),
maxFileAmount,
maxBytesPerFile,
lastInteractive: interactive, lastInteractive: interactive,
chatConfig: { chatConfig,
fileSelectConfig: skillDebugFileSelectConfig
},
histories: newHistories, histories: newHistories,
stream: true, stream: true,
maxRunTimes: WORKFLOW_MAX_RUN_TIMES, maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
...@@ -280,7 +297,7 @@ export async function handleSkillDebugChat( ...@@ -280,7 +297,7 @@ export async function handleSkillDebugChat(
appChatConfig: {}, appChatConfig: {},
variables: {}, variables: {},
source: ChatSourceEnum.test, source: ChatSourceEnum.test,
userContent: userQuestion, userContent: workflowUserQuestion,
aiContent: aiResponse, aiContent: aiResponse,
durationSeconds, durationSeconds,
nodeResponseSummary, nodeResponseSummary,
...@@ -336,7 +353,6 @@ export async function handleSkillDebugChat( ...@@ -336,7 +353,6 @@ export async function handleSkillDebugChat(
} }
} }
logger.error('Skill debug chat error', { error: err, skillId });
if (streamResponseContext) { if (streamResponseContext) {
streamResponseContext.writeStreamError(err); streamResponseContext.writeStreamError(err);
} else { } else {
......
...@@ -4,10 +4,8 @@ import type { ...@@ -4,10 +4,8 @@ import type {
} from '@fastgpt/global/core/ai/llm/type'; } from '@fastgpt/global/core/ai/llm/type';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants'; import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { UserError } from '@fastgpt/global/common/error/utils'; import { UserError } from '@fastgpt/global/common/error/utils';
import { getFileMaxSize } from '../../common/file/utils';
import { isValidImageContentType } from '../../common/file/image/utils'; import { isValidImageContentType } from '../../common/file/image/utils';
import { getS3ChatSource } from '../../common/s3/sources/chat'; import { getS3ChatSource } from '../../common/s3/sources/chat';
import { serviceEnv } from '../../env';
type NormalizeCompletionMessagesProps = { type NormalizeCompletionMessagesProps = {
messages: ChatCompletionMessageParam[]; messages: ChatCompletionMessageParam[];
...@@ -15,6 +13,8 @@ type NormalizeCompletionMessagesProps = { ...@@ -15,6 +13,8 @@ type NormalizeCompletionMessagesProps = {
sourceId: string; sourceId: string;
chatId: string; chatId: string;
uid: string; uid: string;
maxFileAmount: number;
maxBytesPerFile: number;
}; };
/** /**
...@@ -48,7 +48,13 @@ const imageExtensionMap: Record<string, string> = { ...@@ -48,7 +48,13 @@ const imageExtensionMap: Record<string, string> = {
/** /**
* 解析图片 Data URL,并在解码前校验格式和大小,避免超大 base64 产生额外内存峰值。 * 解析图片 Data URL,并在解码前校验格式和大小,避免超大 base64 产生额外内存峰值。
*/ */
const parseImageDataUrl = (dataUrl: string) => { const parseImageDataUrl = ({
dataUrl,
maxBytesPerFile
}: {
dataUrl: string;
maxBytesPerFile: number;
}) => {
const match = dataUrl.match(/^data:([^;,]+);base64,([A-Za-z0-9+/=\r\n]+)$/i); const match = dataUrl.match(/^data:([^;,]+);base64,([A-Za-z0-9+/=\r\n]+)$/i);
if (!match) { if (!match) {
throw new UserError('Invalid image base64 data URL'); throw new UserError('Invalid image base64 data URL');
...@@ -62,9 +68,10 @@ const parseImageDataUrl = (dataUrl: string) => { ...@@ -62,9 +68,10 @@ const parseImageDataUrl = (dataUrl: string) => {
const base64 = match[2].replace(/[\r\n]/g, ''); const base64 = match[2].replace(/[\r\n]/g, '');
const paddingLength = base64.endsWith('==') ? 2 : base64.endsWith('=') ? 1 : 0; const paddingLength = base64.endsWith('==') ? 2 : base64.endsWith('=') ? 1 : 0;
const decodedSize = Math.floor((base64.length * 3) / 4) - paddingLength; const decodedSize = Math.floor((base64.length * 3) / 4) - paddingLength;
const maxSize = getFileMaxSize(); if (decodedSize <= 0 || decodedSize > maxBytesPerFile) {
if (decodedSize <= 0 || decodedSize > maxSize) { throw new UserError(
throw new UserError(`Image size exceeds limit: ${decodedSize} bytes, maximum ${maxSize} bytes`); `Image size exceeds limit: ${decodedSize} bytes, maximum ${maxBytesPerFile} bytes`
);
} }
const body = Buffer.from(base64, 'base64'); const body = Buffer.from(base64, 'base64');
...@@ -85,21 +92,43 @@ const parseImageDataUrl = (dataUrl: string) => { ...@@ -85,21 +92,43 @@ const parseImageDataUrl = (dataUrl: string) => {
/** /**
* 规范化 completions 消息中的 base64 图片。 * 规范化 completions 消息中的 base64 图片。
* *
* 当模型允许直接使用 URL 时,先把 Data URL 上传到当前会话的私有 S3 路径,再用临时预览 * Data URL 始终先上传到当前会话的私有 S3 路径,再用临时预览 URL 覆写运行态消息并附带
* URL 覆写运行态消息,并附带稳定 key。运行结束后聊天持久化只保存 key,不保存 base64 或 * 稳定 key。模型层根据 MULTIPLE_DATA_TO_BASE64 决定是否重新读取并转换为 base64。
* 过期签名 URL。同一请求中重复出现的 Data URL 只上传一次。 * 上传前统一校验数量和大小,避免部分文件已上传后才发现请求超限;相同 Data URL 只上传一次。
*/ */
export const normalizeCompletionMessages = async ({ export const normalizeCompletionMessages = async ({
messages, messages,
sourceType, sourceType,
sourceId, sourceId,
chatId, chatId,
uid uid,
maxFileAmount,
maxBytesPerFile
}: NormalizeCompletionMessagesProps): Promise<ChatCompletionMessageParam[]> => { }: NormalizeCompletionMessagesProps): Promise<ChatCompletionMessageParam[]> => {
if (serviceEnv.MULTIPLE_DATA_TO_BASE64) return messages;
const chatS3 = getS3ChatSource(); const chatS3 = getS3ChatSource();
const uploadCache = new Map<string, Promise<{ key: string; url: string }>>(); const uploadCache = new Map<string, Promise<{ key: string; url: string }>>();
const parsedImageCache = new Map<string, ReturnType<typeof parseImageDataUrl>>();
const base64ImageParts = messages.flatMap((message) => {
if (message.role !== 'user' || !Array.isArray(message.content)) return [];
return message.content.filter(
(part): part is Extract<ChatCompletionContentPart, { type: 'image_url' }> =>
part.type === 'image_url' && /^data:image\//i.test(part.image_url.url)
);
});
const fileLimit = Math.max(0, Math.floor(maxFileAmount));
if (base64ImageParts.length > fileLimit) {
throw new UserError(
`Image amount exceeds limit: ${base64ImageParts.length}, maximum ${fileLimit}`
);
}
base64ImageParts.forEach((part) => {
const dataUrl = part.image_url.url;
if (parsedImageCache.has(dataUrl)) return;
parsedImageCache.set(dataUrl, parseImageDataUrl({ dataUrl, maxBytesPerFile }));
});
const normalizeImagePart = async ( const normalizeImagePart = async (
part: Extract<ChatCompletionContentPart, { type: 'image_url' }> part: Extract<ChatCompletionContentPart, { type: 'image_url' }>
...@@ -112,7 +141,9 @@ export const normalizeCompletionMessages = async ({ ...@@ -112,7 +141,9 @@ export const normalizeCompletionMessages = async ({
if (cached) return cached; if (cached) return cached;
const upload = (async () => { const upload = (async () => {
const { body, contentType, filename } = parseImageDataUrl(dataUrl); const parsedImage = parsedImageCache.get(dataUrl);
if (!parsedImage) throw new Error('Base64 image was not validated before upload');
const { body, contentType, filename } = parsedImage;
const { key, accessUrl } = await chatS3.uploadChatFile({ const { key, accessUrl } = await chatS3.uploadChatFile({
sourceType, sourceType,
sourceId, sourceId,
......
...@@ -42,7 +42,6 @@ import { ...@@ -42,7 +42,6 @@ import {
stripUserContentFileUrls stripUserContentFileUrls
} from './utils/prepare'; } from './utils/prepare';
import { buildChatSourceQuery, buildChatSourceWriteFields, type ChatSourceParams } from './source'; import { buildChatSourceQuery, buildChatSourceWriteFields, type ChatSourceParams } from './source';
import { isChatFileS3KeyForChat } from '../../common/s3/sources/chat/key';
const logger = getLogger(LogCategories.MODULE.CHAT); const logger = getLogger(LogCategories.MODULE.CHAT);
...@@ -86,12 +85,8 @@ export const persistChatFiles = async ({ ...@@ -86,12 +85,8 @@ export const persistChatFiles = async ({
contents, contents,
variables, variables,
variableList, variableList,
sourceType,
sourceId,
chatId,
session session
}: ChatSourceParams & { }: {
chatId: string;
contents: (UserChatItemType | AIChatItemType)[]; contents: (UserChatItemType | AIChatItemType)[];
variables?: Record<string, any>; variables?: Record<string, any>;
variableList?: VariableItemType[]; variableList?: VariableItemType[];
...@@ -110,26 +105,7 @@ export const persistChatFiles = async ({ ...@@ -110,26 +105,7 @@ export const persistChatFiles = async ({
keys.push(valueItem.file.key); keys.push(valueItem.file.key);
} }
// 2. Sandbox 生成文件只在对话落库时转为正式资产。 // 2. query 是特殊格式的(工作流工具 + 表单输入)
if ('tools' in valueItem && valueItem.tools) {
valueItem.tools.forEach((tool) => {
tool.fileRefs?.forEach((fileRef) => {
if (
!isChatFileS3KeyForChat({
key: fileRef.key,
sourceType,
sourceId,
chatId
})
) {
throw new Error('Sandbox file reference does not belong to the current chat');
}
keys.push(fileRef.key);
});
});
}
// 3. query 是特殊格式的(工作流工具 + 表单输入)
if ('text' in valueItem && valueItem.text?.content) { if ('text' in valueItem && valueItem.text?.content) {
try { try {
const parsed = JSON.parse(valueItem.text.content); const parsed = JSON.parse(valueItem.text.content);
...@@ -404,8 +380,6 @@ export const finalizeChatRound = async (props: Props) => { ...@@ -404,8 +380,6 @@ export const finalizeChatRound = async (props: Props) => {
contents: processedContent, contents: processedContent,
variables, variables,
variableList, variableList,
...chatSource,
chatId,
session session
}); });
...@@ -642,8 +616,6 @@ export const pushChatRecords = async (props: Props) => { ...@@ -642,8 +616,6 @@ export const pushChatRecords = async (props: Props) => {
contents: processedContent, contents: processedContent,
variables, variables,
variableList, variableList,
...chatSource,
chatId,
session session
}); });
...@@ -1041,8 +1013,6 @@ export const updateInteractiveChat = async ({ ...@@ -1041,8 +1013,6 @@ export const updateInteractiveChat = async ({
contents: [userContent, aiContent], contents: [userContent, aiContent],
variables, variables,
variableList, variableList,
...chatSource,
chatId,
session session
}); });
}); });
......
...@@ -31,143 +31,126 @@ const hasChildrenResponse = ( ...@@ -31,143 +31,126 @@ const hasChildrenResponse = (
'childrenResponse' in interactive.params && 'childrenResponse' in interactive.params &&
!!interactive.params.childrenResponse; !!interactive.params.childrenResponse;
/** 刷新历史消息中的服务端文件 URL,返回新 histories,不修改传入对象。 */
export const addPreviewUrlToChatItems = async ( export const addPreviewUrlToChatItems = async (
histories: ChatItemMiniType[], histories: ChatItemMiniType[],
type: 'chatFlow' | 'workflowTool' type: 'chatFlow' | 'workflowTool',
getPreviewUrl: (key: string) => Promise<string | undefined> = createChatFilePreviewUrlGetter()
) => { ) => {
const getPreviewUrl = createChatFilePreviewUrlGetter(); async function addPreviewUrlToFileValue(file: ChatFileValueWithPreview) {
const sandboxUrlReplacements = new Map<string, string>(); if (!file.key) return { ...file };
async function refreshSandboxToolFiles() {
await Promise.all(
histories.map(async (item) => {
if (item.obj !== ChatRoleEnum.AI) return;
await Promise.all(
item.value.flatMap((value) =>
(value.tools ?? []).map(async (tool) => {
if (!tool.fileRefs?.length) return;
const replacements = await Promise.all(
tool.fileRefs.map(async (fileRef) => ({
oldUrl: fileRef.url,
newUrl: await getPreviewUrl(fileRef.key)
}))
);
replacements.forEach(({ oldUrl, newUrl }) => {
sandboxUrlReplacements.set(oldUrl, newUrl);
if (tool.response) {
tool.response = tool.response.replaceAll(oldUrl, newUrl);
}
});
// fileRefs 只用于服务端持久化,不能进入历史接口或下一轮模型上下文。
delete tool.fileRefs;
})
)
);
})
);
if (sandboxUrlReplacements.size === 0) return;
histories.forEach((item) => {
if (item.obj !== ChatRoleEnum.AI) return;
item.value.forEach((value) => { const previewUrl = await getPreviewUrl(file.key);
if (!value.text?.content) return; if (previewUrl) {
return { ...file, url: previewUrl };
}
let content = value.text.content; return;
sandboxUrlReplacements.forEach((newUrl, oldUrl) => {
content = content.replaceAll(oldUrl, newUrl);
});
value.text.content = content;
});
});
} }
async function addPreviewUrlToFileValue(files: ChatFileValueWithPreview[]) { async function addPreviewUrlToValue(value: RuntimeValue) {
await Promise.all( if (!Array.isArray(value)) return value;
files.map(async (file) => {
if (!file || typeof file !== 'object') return;
if (!file.key) return;
file.url = await getPreviewUrl(file.key); const files = await Promise.all(
}) value.map((file) =>
file && typeof file === 'object'
? addPreviewUrlToFileValue(file as ChatFileValueWithPreview)
: file
)
); );
return files.filter((file) => file !== undefined);
} }
async function addToInteractive(interactive: WorkflowInteractiveResponseType) { async function addToInteractive(
interactive: WorkflowInteractiveResponseType
): Promise<WorkflowInteractiveResponseType> {
let params = interactive.params ? { ...interactive.params } : interactive.params;
if (interactive.type === 'userInput' && Array.isArray(interactive.params?.inputForm)) { if (interactive.type === 'userInput' && Array.isArray(interactive.params?.inputForm)) {
await Promise.all( params = {
interactive.params.inputForm.map(async (input) => { ...params,
if (input.type === FlowNodeInputTypeEnum.fileSelect) { inputForm: await Promise.all(
const files = formatFileValueList(input.value); interactive.params.inputForm.map(async (input) => ({
await addPreviewUrlToFileValue(files); ...input,
} value:
}) input.type === FlowNodeInputTypeEnum.fileSelect
); ? await addPreviewUrlToValue(input.value)
: input.value
}))
)
};
} }
if (hasChildrenResponse(interactive)) { if (hasChildrenResponse(interactive)) {
await addToInteractive(interactive.params.childrenResponse); params = {
...params,
childrenResponse: await addToInteractive(interactive.params.childrenResponse)
};
} }
return { ...interactive, params } as WorkflowInteractiveResponseType;
} }
async function addToChatflow(item: ChatItemMiniType) { async function addToChatflow(item: ChatItemMiniType): Promise<ChatItemMiniType> {
await Promise.all( return {
item.value.map(async (value) => { ...item,
if ('file' in value && value.file && value.file.key) { value: await Promise.all(
await addPreviewUrlToFileValue([value.file]); item.value.map(async (value) => ({
} ...value,
...('file' in value && value.file
if ('interactive' in value && value.interactive) { ? { file: await addPreviewUrlToFileValue(value.file) }
await addToInteractive(value.interactive); : {}),
} ...('interactive' in value && value.interactive
}) ? { interactive: await addToInteractive(value.interactive) }
); : {})
}))
)
} as ChatItemMiniType;
} }
async function addToWorkflowTool(item: ChatItemMiniType) { async function addToWorkflowTool(item: ChatItemMiniType): Promise<ChatItemMiniType> {
if (item.obj !== ChatRoleEnum.Human || !Array.isArray(item.value)) return; if (item.obj !== ChatRoleEnum.Human || !Array.isArray(item.value)) {
return { ...item, value: [...item.value] } as ChatItemMiniType;
}
await Promise.all( return {
item.value.map(async (value, index) => { ...item,
if (!('text' in value)) return; value: await Promise.all(
const inputValueString = value.text?.content || ''; item.value.map(async (value) => {
const parsedInputValue = JSON.parse(inputValueString) as FlowNodeInputItemType[]; if (!('text' in value)) return { ...value };
const inputValueString = value.text?.content || '';
const parsedInputValue = JSON.parse(inputValueString) as FlowNodeInputItemType[];
const nextInputValue = await Promise.all(
parsedInputValue.map(async (input) => {
if (!input.renderTypeList?.includes(FlowNodeInputTypeEnum.fileSelect)) {
return { ...input };
}
return {
...input,
value: await addPreviewUrlToValue(input.value)
};
})
);
await Promise.all( return {
parsedInputValue.map(async (input) => { ...value,
if (!input.renderTypeList?.includes(FlowNodeInputTypeEnum.fileSelect)) { text: {
return; ...value.text,
content: JSON.stringify(nextInputValue)
} }
const files = formatFileValueList(input.value); };
await addPreviewUrlToFileValue(files); })
}) )
); } as ChatItemMiniType;
item.value[index].text = {
...value.text,
content: JSON.stringify(parsedInputValue)
};
})
);
} }
// 先刷新工具产出文件,确保后续历史上下文不会继续引用过期链接。 return Promise.all(
await refreshSandboxToolFiles();
// Presign file urls
await Promise.all(
histories.map(async (item) => { histories.map(async (item) => {
if (type === 'chatFlow') { if (type === 'chatFlow') {
await addToChatflow(item); return addToChatflow(item);
} else if (type === 'workflowTool') {
await addToWorkflowTool(item);
} }
return addToWorkflowTool(item);
}) })
); );
}; };
......
...@@ -13,12 +13,13 @@ import { ...@@ -13,12 +13,13 @@ import {
import type { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants'; import type { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { getHistories } from '../utils'; import { getHistories } from '../utils';
import { WorkflowVariableState } from '../utils/variables'; import { getWorkflowFileVariableInputs, WorkflowVariableState } from '../utils/variables';
import { chatValue2RuntimePrompt, runtimePrompt2ChatsValue } from '@fastgpt/global/core/chat/adapt'; import { chatValue2RuntimePrompt, runtimePrompt2ChatsValue } from '@fastgpt/global/core/chat/adapt';
import type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime'; import type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime';
import { authAppByTmbId } from '../../../../support/permission/app/auth'; import { authAppByTmbId } from '../../../../support/permission/app/auth';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant'; import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
import { getUserChatInfo } from '../../../../support/user/team/utils'; import { getUserChatInfo } from '../../../../support/user/team/utils';
import { runWithDerivedWorkflowFileContext } from '../../utils/context';
type Props = ModuleDispatchProps<{ type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.userChatInput]: string; [NodeInputKeyEnum.userChatInput]: string;
...@@ -64,40 +65,59 @@ export const dispatchAppRequest = async (props: Props): Promise<Response> => { ...@@ -64,40 +65,59 @@ export const dispatchAppRequest = async (props: Props): Promise<Response> => {
isChildApp: true isChildApp: true
}; };
const { externalProvider } = await getUserChatInfo(appData.tmbId); const { externalProvider } = await getUserChatInfo(appData.tmbId);
const childVariableState = await WorkflowVariableState.create({ const childInputVariables = variableState.toStoreRecord();
timezone: props.timezone, const childQuery = runtimePrompt2ChatsValue({
runningAppInfo: childRunningAppInfo, files,
uid: props.uid, text: userChatInput
chatId: props.chatId,
responseChatItemId: props.responseChatItemId,
histories: chatHistories,
variablesConfig: appData.chatConfig?.variables,
inputVariables: variableState.toStoreRecord(),
externalVariables: externalProvider?.externalWorkflowVariables,
sourceVariableState: variableState
}); });
let filteredChildHistories = chatHistories;
let filteredChildQuery = childQuery;
const { assistantResponses, system_memories, runtimeNodeResponseSummary } = await runWorkflow({ const { assistantResponses, system_memories, runtimeNodeResponseSummary } =
...props, await runWithDerivedWorkflowFileContext({
runningAppInfo: childRunningAppInfo, query: childQuery,
runtimeNodes: storeNodes2RuntimeNodes( histories: chatHistories,
appData.modules, files: getWorkflowFileVariableInputs({
getWorkflowEntryNodeIds(appData.modules) variablesConfig: appData.chatConfig?.variables,
), inputVariables: childInputVariables
runtimeEdges: storeEdges2RuntimeEdges(appData.edges), }),
variableState: childVariableState, fn: async ({ resolveInputFile, query: filteredQuery, histories: filteredHistories }) => {
chatConfig: appData.chatConfig, filteredChildHistories = filteredHistories;
histories: chatHistories, filteredChildQuery = filteredQuery;
query: runtimePrompt2ChatsValue({ const childVariableState = await WorkflowVariableState.create({
files, timezone: props.timezone,
text: userChatInput runningAppInfo: childRunningAppInfo,
}) uid: props.uid,
}); chatId: props.chatId,
responseChatItemId: props.responseChatItemId,
histories: filteredHistories,
variablesConfig: appData.chatConfig?.variables,
inputVariables: childInputVariables,
externalVariables: externalProvider?.externalWorkflowVariables,
sourceVariableState: variableState,
resolveInputFile
});
return runWorkflow({
...props,
runningAppInfo: childRunningAppInfo,
runtimeNodes: storeNodes2RuntimeNodes(
appData.modules,
getWorkflowEntryNodeIds(appData.modules)
),
runtimeEdges: storeEdges2RuntimeEdges(appData.edges),
variableState: childVariableState,
chatConfig: appData.chatConfig,
histories: filteredHistories,
query: filteredQuery
});
}
});
const completeMessages = chatHistories.concat([ const completeMessages = filteredChildHistories.concat([
{ {
obj: ChatRoleEnum.Human, obj: ChatRoleEnum.Human,
value: query value: filteredChildQuery
}, },
{ {
obj: ChatRoleEnum.AI, obj: ChatRoleEnum.AI,
......
...@@ -45,6 +45,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => { ...@@ -45,6 +45,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => {
lastInteractive?.type === 'loopInteractive' ? lastInteractive?.params : undefined; lastInteractive?.type === 'loopInteractive' ? lastInteractive?.params : undefined;
let lastIndex = interactiveData?.currentIndex; let lastIndex = interactiveData?.currentIndex;
// 已知问题:旧 Loop 恢复时会继续写入历史 loopResult;为保持旧流程行为暂不复制。
const outputValueArr = interactiveData ? interactiveData.loopResult : []; const outputValueArr = interactiveData ? interactiveData.loopResult : [];
const assistantResponses: AIChatItemValueItemType[] = []; const assistantResponses: AIChatItemValueItemType[] = [];
const customFeedbacks: string[] = []; const customFeedbacks: string[] = [];
...@@ -67,6 +68,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => { ...@@ -67,6 +68,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => {
// Init entry // Init entry
if (isInteractiveResponseIndex) { if (isInteractiveResponseIndex) {
// 已知问题:旧 Loop 会直接修改并传递父级 runtimeNodes;该弃用节点暂时保留兼容行为。
runtimeNodes.forEach((node) => { runtimeNodes.forEach((node) => {
if (interactiveData?.childrenResponse?.entryNodeIds.includes(node.nodeId)) { if (interactiveData?.childrenResponse?.entryNodeIds.includes(node.nodeId)) {
node.isEntry = true; node.isEntry = true;
......
...@@ -22,7 +22,6 @@ type WorkflowAgentLoopRuntimeContext = ToolDispatchContext & { ...@@ -22,7 +22,6 @@ type WorkflowAgentLoopRuntimeContext = ToolDispatchContext & {
nodeId: string; nodeId: string;
flowNodeType: FlowNodeTypeEnum; flowNodeType: FlowNodeTypeEnum;
}; };
filesMap: UseUserContextResult['filesMap'];
currentFiles: UseUserContextResult['currentFiles']; currentFiles: UseUserContextResult['currentFiles'];
sandboxClient?: SandboxClient; sandboxClient?: SandboxClient;
}; };
...@@ -144,6 +143,7 @@ export const createWorkflowAgentLoopRuntime = ({ ...@@ -144,6 +143,7 @@ export const createWorkflowAgentLoopRuntime = ({
readFile: toolProvider.readFileExecutor readFile: toolProvider.readFileExecutor
? { ? {
enabled: true, enabled: true,
maxFileAmount: toolProvider.readFileMaxFileAmount,
execute: toolProvider.readFileExecutor execute: toolProvider.readFileExecutor
} }
: undefined : undefined
......
...@@ -25,6 +25,7 @@ import { ...@@ -25,6 +25,7 @@ import {
type AgentSandboxPrepareAction type AgentSandboxPrepareAction
} from './sub/sandbox'; } from './sub/sandbox';
import type { RuntimeNodeResponseSummary } from '../../type'; import type { RuntimeNodeResponseSummary } from '../../type';
import { getWorkflowFileMaxAmount } from '../../../utils/context';
import { createAgentNodeResponseCollector } from './nodeResponseCollector'; import { createAgentNodeResponseCollector } from './nodeResponseCollector';
import { createAgentSandboxPermissionDeniedError } from '../../../../ai/sandbox/interface/runtime'; import { createAgentSandboxPermissionDeniedError } from '../../../../ai/sandbox/interface/runtime';
import { replaceAgentPromptToolReferences } from './adapter/prompt'; import { replaceAgentPromptToolReferences } from './adapter/prompt';
...@@ -105,7 +106,6 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise ...@@ -105,7 +106,6 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
lang, lang,
histories, histories,
query, query,
requestOrigin,
chatConfig, chatConfig,
lastInteractive, lastInteractive,
runningAppInfo, runningAppInfo,
...@@ -154,10 +154,8 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise ...@@ -154,10 +154,8 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
// 初始化对话框输入的文件 // 初始化对话框输入的文件
const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList); const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList);
const fileLinks = const parseHistoryFiles = !!fileUrlInput?.value?.length;
fileUrlInput && fileUrlInput.value && fileUrlInput.value.length > 0 const fileLinks = parseHistoryFiles ? props.params.fileUrlList : undefined;
? props.params.fileUrlList
: undefined;
try { try {
if (hasSandboxRuntimeDependency && !global.feConfigs?.show_agent_sandbox) { if (hasSandboxRuntimeDependency && !global.feConfigs?.show_agent_sandbox) {
...@@ -171,12 +169,12 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise ...@@ -171,12 +169,12 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
currentUserInput: userChatInput, currentUserInput: userChatInput,
currentQuery: query, currentQuery: query,
currentDataId: responseChatItemId, currentDataId: responseChatItemId,
parseHistoryFiles,
selectedDataset: datasetParams?.datasets, selectedDataset: datasetParams?.datasets,
authTmbId: datasetParams?.authTmbId, authTmbId: datasetParams?.authTmbId,
tmbId: runningUserInfo.tmbId, tmbId: runningUserInfo.tmbId,
timezone, timezone,
requestOrigin, maxFileAmount: getWorkflowFileMaxAmount()
maxFiles: chatConfig?.fileSelectConfig?.maxFiles || 20
}); });
if (effectiveUseAgentSandbox) { if (effectiveUseAgentSandbox) {
...@@ -206,7 +204,7 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise ...@@ -206,7 +204,7 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
currentFiles: skipSandboxInputFiles ? [] : userContext.currentFiles currentFiles: skipSandboxInputFiles ? [] : userContext.currentFiles
}); });
// 获取请求上下文 // 获取请求上下文
const { chatHistories, queryInput, fileUrlMap, filesMap } = userContext; const { chatHistories, queryInput } = userContext;
const { rewrittenHistories, currentUserMessage } = userContext.getCurrentMessages({ const { rewrittenHistories, currentUserMessage } = userContext.getCurrentMessages({
skillInfos, skillInfos,
currentWorkingDirectory currentWorkingDirectory
...@@ -253,8 +251,6 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise ...@@ -253,8 +251,6 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
getSubAppInfo, getSubAppInfo,
getSubApp, getSubApp,
completionTools: agentCompletionTools, completionTools: agentCompletionTools,
fileUrlMap,
filesMap,
currentFiles: userContext.currentFiles, currentFiles: userContext.currentFiles,
sandboxClient, sandboxClient,
streamResponseFn: workflowStreamResponse streamResponseFn: workflowStreamResponse
......
import { getErrText } from '@fastgpt/global/common/error/utils';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { i18nT } from '@fastgpt/global/common/i18n/utils';
import type { DispatchSubAppResponse } from '../../type';
import { getFileContentByUrl } from '../../../../../../chat/fileContext';
type FileReadParams = {
files: { id: string; name?: string; url: string }[];
teamId: string;
tmbId: string;
customPdfParse?: boolean;
usageId?: string;
};
/**
* 使用聊天文件的统一读取链路解析 Agent 文件,并保留用户上传时的文件名用于响应展示。
*/
export const dispatchFileRead = async ({
files,
teamId,
tmbId,
customPdfParse,
usageId
}: FileReadParams): Promise<DispatchSubAppResponse> => {
try {
const readFilesResult = await Promise.all(
files.map(async ({ id, url, name: inputName }) => {
try {
const { name, content } = await getFileContentByUrl({
url,
teamId,
tmbId,
customPdfParse,
usageId
});
return {
id,
name: inputName ?? name,
content
};
} catch (error) {
return {
id,
name: inputName ?? '',
content: getErrText(error, 'Load file error')
};
}
})
);
return {
response: JSON.stringify(readFilesResult),
usages: [],
nodeResponse: {
moduleType: FlowNodeTypeEnum.readFiles,
moduleName: i18nT('chat:read_file'),
readFiles: files.map((file, index) => ({
name: readFilesResult[index]!.name,
url: file.url
}))
}
};
} catch (error) {
return {
response: `Failed to read file: ${getErrText(error)}`,
usages: []
};
}
};
...@@ -22,6 +22,7 @@ import { ...@@ -22,6 +22,7 @@ import {
type SandboxPrepareStep type SandboxPrepareStep
} from '../../../../../../ai/sandbox/interface/runtime'; } from '../../../../../../ai/sandbox/interface/runtime';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants'; import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { readWorkflowFileBuffer } from '../../../../../utils/context';
export type AgentSandboxPrepareContext = SandboxPrepareContext & { export type AgentSandboxPrepareContext = SandboxPrepareContext & {
sandboxClient: SandboxClient; sandboxClient: SandboxClient;
...@@ -89,7 +90,7 @@ export async function ensureAgentSandboxRuntime({ ...@@ -89,7 +90,7 @@ export async function ensureAgentSandboxRuntime({
chatId, chatId,
teamId teamId
}); });
const readInputFile = (url: string) => readWorkflowFileBuffer({ url });
const preparedContext = await withAgentSandboxInitLease({ const preparedContext = await withAgentSandboxInitLease({
sandboxId: sandboxContext.sandboxClient.getSandboxId(), sandboxId: sandboxContext.sandboxClient.getSandboxId(),
fn: () => { fn: () => {
...@@ -105,7 +106,7 @@ export async function ensureAgentSandboxRuntime({ ...@@ -105,7 +106,7 @@ export async function ensureAgentSandboxRuntime({
? prepareSandbox( ? prepareSandbox(
context, context,
preparePackageMirrors(), preparePackageMirrors(),
injectCurrentInputFiles(currentFiles), injectCurrentInputFiles(currentFiles, readInputFile),
...prepareActions, ...prepareActions,
readCurrentWorkingDirectory(), readCurrentWorkingDirectory(),
scanEditDebugSkillInfos() scanEditDebugSkillInfos()
...@@ -114,7 +115,7 @@ export async function ensureAgentSandboxRuntime({ ...@@ -114,7 +115,7 @@ export async function ensureAgentSandboxRuntime({
context, context,
preparePackageMirrors(), preparePackageMirrors(),
injectSelectedSkillFiles({ teamId, tmbId, skillIds, selectedSkills }), injectSelectedSkillFiles({ teamId, tmbId, skillIds, selectedSkills }),
injectCurrentInputFiles(currentFiles), injectCurrentInputFiles(currentFiles, readInputFile),
...prepareActions, ...prepareActions,
readCurrentWorkingDirectory(), readCurrentWorkingDirectory(),
runSandboxEntrypoint({ sandboxEntrypoint }), runSandboxEntrypoint({ sandboxEntrypoint }),
......
...@@ -94,28 +94,9 @@ export type ToolDispatchContext = Pick< ...@@ -94,28 +94,9 @@ export type ToolDispatchContext = Pick<
getSubAppInfo: GetSubAppInfoFnType; getSubAppInfo: GetSubAppInfoFnType;
getSubApp: (id: string) => SubAppRuntimeType | undefined; getSubApp: (id: string) => SubAppRuntimeType | undefined;
completionTools: ChatCompletionTool[]; completionTools: ChatCompletionTool[];
fileUrlMap?: Record<string, string>;
streamResponseFn?: (args: WorkflowResponseItemType) => void | undefined; streamResponseFn?: (args: WorkflowResponseItemType) => void | undefined;
}; };
/** 将工具参数中完整匹配的 Agent 文件 id 递归替换为真实 URL。 */
export const replaceAgentFileIdsWithUrls = <T>(value: T, fileUrlMap: Record<string, string>): T => {
if (!value || Object.keys(fileUrlMap).length === 0) return value;
const replaceValue = (input: unknown): unknown => {
if (typeof input === 'string') return fileUrlMap[input] || input;
if (Array.isArray(input)) return input.map(replaceValue);
if (input && typeof input === 'object') {
return Object.fromEntries(
Object.entries(input).map(([key, item]) => [key, replaceValue(item)])
);
}
return input;
};
return replaceValue(value) as T;
};
const filterAgentWorkflowRuntimeParams = (params: Record<string, any>) => { const filterAgentWorkflowRuntimeParams = (params: Record<string, any>) => {
const runtimeParams = { ...params }; const runtimeParams = { ...params };
delete runtimeParams[NodeInputKeyEnum.forbidStream]; delete runtimeParams[NodeInputKeyEnum.forbidStream];
...@@ -156,7 +137,6 @@ export const getAgentDatasetParams = ( ...@@ -156,7 +137,6 @@ export const getAgentDatasetParams = (
export const getExecuteTool = ({ export const getExecuteTool = ({
getSubAppInfo, getSubAppInfo,
getSubApp, getSubApp,
fileUrlMap = {},
checkIsStopping, checkIsStopping,
runningUserInfo, runningUserInfo,
runningAppInfo, runningAppInfo,
...@@ -223,13 +203,10 @@ export const getExecuteTool = ({ ...@@ -223,13 +203,10 @@ export const getExecuteTool = ({
response: 'Params is not object' response: 'Params is not object'
}; };
} }
const requestParams = replaceAgentFileIdsWithUrls( const requestParams = {
{ ...tool.params,
...tool.params, ...toolCallParams
...toolCallParams };
},
fileUrlMap
);
if (tool.type === 'tool') { if (tool.type === 'tool') {
const { response, usages, nodeResponse } = await dispatchTool({ const { response, usages, nodeResponse } = await dispatchTool({
......
...@@ -3,15 +3,15 @@ import { SubAppIds } from '@fastgpt/global/core/workflow/node/agent/constants'; ...@@ -3,15 +3,15 @@ import { SubAppIds } from '@fastgpt/global/core/workflow/node/agent/constants';
import type { AgentLoopDatasetSearchExecutor } from '../../../../../ai/llm/agentLoop/interface'; import type { AgentLoopDatasetSearchExecutor } from '../../../../../ai/llm/agentLoop/interface';
import type { AgentLoopCoreToolRunResult } from '../../agentLoopCore/interface'; import type { AgentLoopCoreToolRunResult } from '../../agentLoopCore/interface';
import { import {
buildAgentLoopCoreSystemToolFileUrl,
createAgentLoopCoreReadFileExecutor, createAgentLoopCoreReadFileExecutor,
normalizeAgentLoopCoreDatasetSearchResult normalizeAgentLoopCoreDatasetSearchResult
} from '../../agentLoopCore/interface'; } from '../../agentLoopCore/interface';
import { dispatchAgentDatasetSearch } from '../sub/dataset'; import { dispatchAgentDatasetSearch } from '../sub/dataset';
import { dispatchFileRead } from '../sub/file'; import { dispatchWorkflowReadFiles } from '../../readFiles';
import { getAgentDatasetParams, getExecuteTool } from '../sub/utils'; import { getAgentDatasetParams, getExecuteTool } from '../sub/utils';
import type { WorkflowAgentToolProvider, WorkflowAgentToolProviderContext } from './type'; import type { WorkflowAgentToolProvider, WorkflowAgentToolProviderContext } from './type';
import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workflow/template/system/interactive/type'; import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workflow/template/system/interactive/type';
import { getWorkflowFileMaxAmount } from '../../../../utils/context';
/** /**
* 创建 Workflow Agent 节点的工具 provider。 * 创建 Workflow Agent 节点的工具 provider。
...@@ -58,25 +58,17 @@ export const createWorkflowAgentToolProvider = ({ ...@@ -58,25 +58,17 @@ export const createWorkflowAgentToolProvider = ({
}); });
} }
: undefined; : undefined;
const readFileMaxFileAmount = getWorkflowFileMaxAmount();
const readFileExecutor = createAgentLoopCoreReadFileExecutor({ const readFileExecutor = createAgentLoopCoreReadFileExecutor({
enabled: Object.keys(context.filesMap).length > 0, enabled: true,
resolveFiles: (ids) =>
ids.map((id) => {
const file = context.filesMap[id];
return {
id,
...(file?.name ? { name: file.name } : {}),
url: file?.url || ''
};
}),
execute: async ({ files }) => execute: async ({ files }) =>
dispatchFileRead({ dispatchWorkflowReadFiles({
files, files,
teamId: context.runningUserInfo.teamId, teamId: context.runningUserInfo.teamId,
tmbId: context.runningUserInfo.tmbId, tmbId: context.runningUserInfo.tmbId,
customPdfParse: context.chatConfig?.fileSelectConfig?.customPdfParse, customPdfParse: context.chatConfig?.fileSelectConfig?.customPdfParse,
usageId: context.usageId usageId: context.usageId,
maxFileAmount: readFileMaxFileAmount
}) })
}); });
...@@ -114,12 +106,8 @@ export const createWorkflowAgentToolProvider = ({ ...@@ -114,12 +106,8 @@ export const createWorkflowAgentToolProvider = ({
}); });
}, },
readFileExecutor, readFileExecutor,
readFileMaxFileAmount,
datasetSearchExecutor, datasetSearchExecutor,
currentInputFiles: context.currentFiles.map((file) => currentInputFiles: context.currentFiles.map((file) => file.url)
buildAgentLoopCoreSystemToolFileUrl({
url: file.url,
requestOrigin: context.requestOrigin
})
)
}; };
}; };
...@@ -7,7 +7,6 @@ import type { SubAppRuntimeType } from '../type'; ...@@ -7,7 +7,6 @@ import type { SubAppRuntimeType } from '../type';
import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workflow/template/system/interactive/type'; import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workflow/template/system/interactive/type';
export type WorkflowAgentToolProviderContext = ToolDispatchContext & { export type WorkflowAgentToolProviderContext = ToolDispatchContext & {
filesMap: UseUserContextResult['filesMap'];
currentFiles: UseUserContextResult['currentFiles']; currentFiles: UseUserContextResult['currentFiles'];
}; };
...@@ -16,6 +15,7 @@ export type WorkflowAgentToolProvider = AgentLoopCoreToolProvider< ...@@ -16,6 +15,7 @@ export type WorkflowAgentToolProvider = AgentLoopCoreToolProvider<
WorkflowInteractiveResponseType WorkflowInteractiveResponseType
> & { > & {
readFileExecutor?: AgentLoopReadFileExecutor; readFileExecutor?: AgentLoopReadFileExecutor;
readFileMaxFileAmount: number;
datasetSearchExecutor?: AgentLoopDatasetSearchExecutor; datasetSearchExecutor?: AgentLoopDatasetSearchExecutor;
currentInputFiles: string[]; currentInputFiles: string[];
}; };
...@@ -356,14 +356,12 @@ export const createAgentLoopCoreAssistantEventCollector = ({ ...@@ -356,14 +356,12 @@ export const createAgentLoopCoreAssistantEventCollector = ({
toolAvatar: toolInfo?.avatar || '', toolAvatar: toolInfo?.avatar || '',
functionName, functionName,
params: event.call.function.arguments ?? '', params: event.call.function.arguments ?? '',
response: event.response, response: event.response
...(event.fileRefs?.length ? { fileRefs: event.fileRefs } : {})
}); });
} else { } else {
updateToolResponse(event.call.id, (tool) => ({ updateToolResponse(event.call.id, (tool) => ({
...tool, ...tool,
response: appendUniqueDelta(tool.response, event.response), response: appendUniqueDelta(tool.response, event.response)
...(event.fileRefs?.length ? { fileRefs: event.fileRefs } : {})
})); }));
} }
......
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import { chatValue2RuntimePrompt, runtimePrompt2ChatsValue } from '@fastgpt/global/core/chat/adapt';
import type { ChatItemMiniType } from '@fastgpt/global/core/chat/type';
import {
buildAgentLoopCoreUserReminderInput,
type AgentLoopCoreUserReminderContext
} from './reminder';
export type AgentLoopCoreInputFile = {
name: string;
type: ChatFileTypeEnum;
url: string;
sandboxPath?: string;
};
type RewriteAgentLoopCoreUserMessageWithFilesParams = {
message: ChatItemMiniType;
files: AgentLoopCoreInputFile[];
query?: string;
reminderContext?: AgentLoopCoreUserReminderContext;
};
/**
* 使用 AgentLoop 文件语义改写 Human 消息。
*
* 文档只进入 reminder,供 read_files 按 URL 读取;图片、音频和视频继续作为独立消息项
* 传给模型。文件 URL 的鉴权、归一化和去重由外层业务适配器完成。
*/
export const rewriteAgentLoopCoreUserMessageWithFiles = ({
message,
files,
query: inputQuery,
reminderContext
}: RewriteAgentLoopCoreUserMessageWithFilesParams): ChatItemMiniType => {
if (files.length === 0 && !reminderContext && inputQuery === undefined) return message;
const { text: messageQuery = '' } = chatValue2RuntimePrompt(message.value);
return {
...message,
value: runtimePrompt2ChatsValue({
files: files
.filter((file) => file.type !== ChatFileTypeEnum.file)
.map(({ name, type, url }) => ({ name, type, url })),
text: buildAgentLoopCoreUserReminderInput({
query: inputQuery ?? messageQuery,
filesInfo: files,
...reminderContext
})
})
};
};
export * from './input'; export * from './input';
export * from './files';
export * from './messages'; export * from './messages';
export * from './prompt'; export * from './prompt';
export * from './reminder'; export * from './reminder';
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { SelectedDatasetType } from '@fastgpt/global/core/workflow/type/io'; import type { SelectedDatasetType } from '@fastgpt/global/core/workflow/type/io';
import type { DeployedSkillInfo } from '../../../../../../ai/sandbox/interface/runtime'; import type { DeployedSkillInfo } from '../../../../../../ai/sandbox/interface/runtime';
import { READ_FILES_TOOL_NAME } from '../../../../../../ai/llm/agentLoop/interface'; import { READ_FILES_TOOL_NAME } from '../../../../../../ai/llm/agentLoop/interface';
import { SANDBOX_READ_FILE_TOOL_NAME } from '@fastgpt/global/core/ai/sandbox/tools'; import { SANDBOX_READ_FILE_TOOL_NAME } from '@fastgpt/global/core/ai/sandbox/tools';
import type { AgentLoopCoreInputFile } from './files';
export type AgentLoopCoreInputFile = {
id: string;
name: string;
type: ChatFileTypeEnum;
url: string;
};
export type AgentLoopCoreSelectedDatasetInput = Pick<SelectedDatasetType, 'datasetId'> & export type AgentLoopCoreSelectedDatasetInput = Pick<SelectedDatasetType, 'datasetId'> &
Partial<Omit<SelectedDatasetType, 'datasetId'>>; Partial<Omit<SelectedDatasetType, 'datasetId'>>;
...@@ -27,9 +20,6 @@ const escapeXml = (value: string) => ...@@ -27,9 +20,6 @@ const escapeXml = (value: string) =>
.replace(/"/g, '&quot;') .replace(/"/g, '&quot;')
.replace(/'/g, '&apos;'); .replace(/'/g, '&apos;');
export const filterAgentLoopCoreDocumentFiles = (files: AgentLoopCoreInputFile[] = []) =>
files.filter((file) => file.type === ChatFileTypeEnum.file);
/** /**
* 生成当前输入文件的模型可见提示。 * 生成当前输入文件的模型可见提示。
* 文档可通过 read_files 读取;其他多模态文件同时保留 URL,供模型或工具引用。 * 文档可通过 read_files 读取;其他多模态文件同时保留 URL,供模型或工具引用。
...@@ -45,10 +35,10 @@ export const buildAgentLoopCoreInputFilesPrompt = (files: AgentLoopCoreInputFile ...@@ -45,10 +35,10 @@ export const buildAgentLoopCoreInputFilesPrompt = (files: AgentLoopCoreInputFile
${files ${files
.map( .map(
(file) => `<file> (file) => `<file>
<id>${escapeXml(file.id)}</id>
<name>${escapeXml(file.name)}</name> <name>${escapeXml(file.name)}</name>
<type>${escapeXml(file.type)}</type> <type>${escapeXml(file.type)}</type>
<url>${escapeXml(file.url)}</url> <url>${escapeXml(file.url)}</url>
${file.sandboxPath ? `<sandboxPath>${escapeXml(file.sandboxPath)}</sandboxPath>` : ''}
</file>` </file>`
) )
.join('\n')}`; .join('\n')}`;
...@@ -134,6 +124,13 @@ ${currentWorkingDirectory ? `当前 sandbox 工作目录: ${currentWorkingDirect ...@@ -134,6 +124,13 @@ ${currentWorkingDirectory ? `当前 sandbox 工作目录: ${currentWorkingDirect
* 生成当前用户消息内的动态上下文提醒。 * 生成当前用户消息内的动态上下文提醒。
* 该内容是 user message 的一部分,不是 system role prompt;历史消息只应注入稳定文件片段。 * 该内容是 user message 的一部分,不是 system role prompt;历史消息只应注入稳定文件片段。
*/ */
export type AgentLoopCoreUserReminderContext = {
skillInfos?: DeployedSkillInfo[];
selectedDataset?: AgentLoopCoreSelectedDatasetContext[];
currentTime?: string;
currentWorkingDirectory?: string;
};
export const buildAgentLoopCoreUserReminderInput = ({ export const buildAgentLoopCoreUserReminderInput = ({
query = '', query = '',
skillInfos, skillInfos,
...@@ -141,13 +138,9 @@ export const buildAgentLoopCoreUserReminderInput = ({ ...@@ -141,13 +138,9 @@ export const buildAgentLoopCoreUserReminderInput = ({
selectedDataset, selectedDataset,
currentTime, currentTime,
currentWorkingDirectory currentWorkingDirectory
}: { }: AgentLoopCoreUserReminderContext & {
query?: string; query?: string;
skillInfos?: DeployedSkillInfo[];
filesInfo?: AgentLoopCoreInputFile[]; filesInfo?: AgentLoopCoreInputFile[];
selectedDataset?: AgentLoopCoreSelectedDatasetContext[];
currentTime?: string;
currentWorkingDirectory?: string;
}) => { }) => {
const reminder = [ const reminder = [
buildAgentLoopCoreSkillsPrompt(skillInfos), buildAgentLoopCoreSkillsPrompt(skillInfos),
......
...@@ -18,7 +18,7 @@ export type AgentLoopCoreSystemToolNodeResponseInput = Omit< ...@@ -18,7 +18,7 @@ export type AgentLoopCoreSystemToolNodeResponseInput = Omit<
export type AgentLoopCoreParsedReadFileCall = export type AgentLoopCoreParsedReadFileCall =
| { | {
success: true; success: true;
ids: string[]; urls: string[];
} }
| { | {
success: false; success: false;
...@@ -27,14 +27,12 @@ export type AgentLoopCoreParsedReadFileCall = ...@@ -27,14 +27,12 @@ export type AgentLoopCoreParsedReadFileCall =
}; };
export type AgentLoopCoreReadFileItem = { export type AgentLoopCoreReadFileItem = {
id: string;
name?: string; name?: string;
url: string; url: string;
}; };
export type CreateAgentLoopCoreReadFileExecutorParams = { export type CreateAgentLoopCoreReadFileExecutorParams = {
enabled: boolean; enabled: boolean;
resolveFiles: (ids: string[]) => AgentLoopCoreReadFileItem[];
execute: (params: { callId: string; files: AgentLoopCoreReadFileItem[] }) => Promise<{ execute: (params: { callId: string; files: AgentLoopCoreReadFileItem[] }) => Promise<{
response: string; response: string;
usages?: ChatNodeUsageType[]; usages?: ChatNodeUsageType[];
...@@ -74,19 +72,18 @@ export const parseAgentLoopCoreReadFileCall = ( ...@@ -74,19 +72,18 @@ export const parseAgentLoopCoreReadFileCall = (
return { return {
success: true, success: true,
ids: toolParams.data.ids urls: toolParams.data.urls
}; };
}; };
/** /**
* 创建 read_files system tool 执行器。 * 创建 read_files system tool 执行器。
* *
* core 统一处理 LLM 参数校验、文件 id 到文件列表的结构转换入口、usage 默认值和 * core 统一处理 LLM URL 参数校验、文件列表结构转换、usage 默认值和
* nodeResponse 运行字段补齐;真实文件来源和解析服务仍由 Workflow Agent/ToolCall 外壳注入。 * nodeResponse 运行字段补齐;真实文件来源和解析服务仍由 Workflow Agent/ToolCall 外壳注入。
*/ */
export const createAgentLoopCoreReadFileExecutor = ({ export const createAgentLoopCoreReadFileExecutor = ({
enabled, enabled,
resolveFiles,
execute execute
}: CreateAgentLoopCoreReadFileExecutorParams): AgentLoopReadFileExecutor | undefined => { }: CreateAgentLoopCoreReadFileExecutorParams): AgentLoopReadFileExecutor | undefined => {
if (!enabled) return undefined; if (!enabled) return undefined;
...@@ -98,7 +95,7 @@ export const createAgentLoopCoreReadFileExecutor = ({ ...@@ -98,7 +95,7 @@ export const createAgentLoopCoreReadFileExecutor = ({
const startTime = Date.now(); const startTime = Date.now();
const result = await execute({ const result = await execute({
callId: call.id, callId: call.id,
files: resolveFiles(toolParams.ids) files: toolParams.urls.map((url) => ({ url }))
}); });
const usages = result.usages ?? []; const usages = result.usages ?? [];
......
...@@ -9,6 +9,7 @@ export type CreateAgentLoopCoreSystemToolsParams = { ...@@ -9,6 +9,7 @@ export type CreateAgentLoopCoreSystemToolsParams = {
sandboxClient?: SandboxClient; sandboxClient?: SandboxClient;
readFile?: { readFile?: {
enabled: boolean; enabled: boolean;
maxFileAmount: number;
execute: AgentLoopReadFileExecutor; execute: AgentLoopReadFileExecutor;
}; };
datasetSearch?: { datasetSearch?: {
...@@ -49,6 +50,7 @@ export const createAgentLoopCoreSystemTools = ({ ...@@ -49,6 +50,7 @@ export const createAgentLoopCoreSystemTools = ({
? { ? {
readFile: { readFile: {
enabled: true, enabled: true,
maxFileAmount: readFile.maxFileAmount,
execute: readFile.execute execute: readFile.execute
} }
} }
......
...@@ -81,8 +81,8 @@ export const formatAgentLoopCoreToolResponse = (toolResponses: any) => { ...@@ -81,8 +81,8 @@ export const formatAgentLoopCoreToolResponse = (toolResponses: any) => {
}; };
/** /**
* 这些 runtime edge/node 是为一次 tool workflow 派生出的副本。 * 初始化阶段直接在传入的 runtime edge/node 上标记入口状态。
* 初始化阶段直接在副本上标记入口状态,避免后续调度还要维护额外的 entry 集合。 * 调用方需要根据自身的状态隔离要求决定是否先创建副本。
*/ */
export const initAgentLoopCoreWorkflowToolEdges = ( export const initAgentLoopCoreWorkflowToolEdges = (
edges: RuntimeEdgeItemType[], edges: RuntimeEdgeItemType[],
...@@ -265,6 +265,9 @@ export const createAgentLoopCoreWorkflowSystemToolExecutor = <TChildrenResponse ...@@ -265,6 +265,9 @@ export const createAgentLoopCoreWorkflowSystemToolExecutor = <TChildrenResponse
* *
* ToolCall 和未来的简化 Agent 都可以把“某个 runtime node 作为工具入口运行”的能力交给这里。 * ToolCall 和未来的简化 Agent 都可以把“某个 runtime node 作为工具入口运行”的能力交给这里。
* 节点外壳只负责提供实际 runWorkflow 函数、展示信息和缓存/流式回调。 * 节点外壳只负责提供实际 runWorkflow 函数、展示信息和缓存/流式回调。
*
* 已知问题:普通 Workflow Tool 的首次执行和交互恢复都会复用并修改父流程的 runtime graph。
* 当前保留原有状态传递行为,后续需要通过子流程快照和显式同步完成隔离。
*/ */
export const createAgentLoopCoreWorkflowToolRunner = <TChildrenResponse = unknown>({ export const createAgentLoopCoreWorkflowToolRunner = <TChildrenResponse = unknown>({
runtimeNodes, runtimeNodes,
......
...@@ -10,7 +10,6 @@ import type { ...@@ -10,7 +10,6 @@ import type {
AgentLoopToolExecutionResult AgentLoopToolExecutionResult
} from '../../../../../ai/llm/agentLoop/interface'; } from '../../../../../ai/llm/agentLoop/interface';
import type { AgentLoopCoreSystemToolInfo } from './toolInfo'; import type { AgentLoopCoreSystemToolInfo } from './toolInfo';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
export type AgentLoopCoreUserToolInfo<TRaw = unknown> = { export type AgentLoopCoreUserToolInfo<TRaw = unknown> = {
type: 'user'; type: 'user';
...@@ -35,7 +34,6 @@ export type AgentLoopCoreToolRunResult<TChildrenResponse = unknown> = { ...@@ -35,7 +34,6 @@ export type AgentLoopCoreToolRunResult<TChildrenResponse = unknown> = {
interactive?: TChildrenResponse; interactive?: TChildrenResponse;
stop?: boolean; stop?: boolean;
errorMessage?: string; errorMessage?: string;
fileRefs?: SandboxFileRef[];
nodeResponse?: ChatHistoryItemResType; nodeResponse?: ChatHistoryItemResType;
}; };
...@@ -63,7 +61,6 @@ export const normalizeAgentLoopCoreToolRunResult = <TChildrenResponse = unknown> ...@@ -63,7 +61,6 @@ export const normalizeAgentLoopCoreToolRunResult = <TChildrenResponse = unknown>
interactive, interactive,
stop = false, stop = false,
errorMessage, errorMessage,
fileRefs,
nodeResponse nodeResponse
}: AgentLoopCoreToolRunResult<TChildrenResponse>): AgentLoopToolExecutionResult<TChildrenResponse> => { }: AgentLoopCoreToolRunResult<TChildrenResponse>): AgentLoopToolExecutionResult<TChildrenResponse> => {
return { return {
...@@ -73,7 +70,6 @@ export const normalizeAgentLoopCoreToolRunResult = <TChildrenResponse = unknown> ...@@ -73,7 +70,6 @@ export const normalizeAgentLoopCoreToolRunResult = <TChildrenResponse = unknown>
interactive, interactive,
stop, stop,
errorMessage, errorMessage,
fileRefs,
metadata: nodeResponse metadata: nodeResponse
}; };
}; };
...@@ -5,12 +5,16 @@ export { ...@@ -5,12 +5,16 @@ export {
} from '../application/run'; } from '../application/run';
export { buildAgentLoopCoreInput } from '../application/context/input'; export { buildAgentLoopCoreInput } from '../application/context/input';
export {
rewriteAgentLoopCoreUserMessageWithFiles,
type AgentLoopCoreInputFile
} from '../application/context/files';
export { readAgentLoopCoreActivePlan } from '../application/context/activePlan'; export { readAgentLoopCoreActivePlan } from '../application/context/activePlan';
export { buildAgentLoopCoreRequestMessages } from '../application/context/messages'; export { buildAgentLoopCoreRequestMessages } from '../application/context/messages';
export { buildAgentLoopCoreSystemPrompt } from '../application/context/prompt'; export { buildAgentLoopCoreSystemPrompt } from '../application/context/prompt';
export { export {
buildAgentLoopCoreUserReminderInput, buildAgentLoopCoreUserReminderInput,
type AgentLoopCoreInputFile, type AgentLoopCoreUserReminderContext,
type AgentLoopCoreSelectedDatasetContext, type AgentLoopCoreSelectedDatasetContext,
type AgentLoopCoreSelectedDatasetInput type AgentLoopCoreSelectedDatasetInput
} from '../application/context/reminder'; } from '../application/context/reminder';
......
...@@ -8,8 +8,11 @@ import { ...@@ -8,8 +8,11 @@ import {
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type'; import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import type { LLMModelItemType } from '@fastgpt/global/core/ai/model.schema'; import type { LLMModelItemType } from '@fastgpt/global/core/ai/model.schema';
import type { ChatDispatchProps } from '../../../types/runtime'; import type { ChatDispatchProps } from '../../../types/runtime';
import { parseFileContentFromUrls } from '../../../../chat/fileContext'; import {
import { rewriteChatMessagesWithFiles } from './fileContext'; parseFileContentFromUrls,
rewriteChatMessagesWithFileContext
} from '../../../../chat/fileContext';
import { getWorkflowFileContext } from '../../../utils/context';
/** /**
* 组装系统提示、历史、当前输入和文件上下文,并按模型上下文限制裁剪为 LLM 消息。 * 组装系统提示、历史、当前输入和文件上下文,并按模型上下文限制裁剪为 LLM 消息。
...@@ -23,8 +26,7 @@ export const getChatMessages = async ({ ...@@ -23,8 +26,7 @@ export const getChatMessages = async ({
userChatInput, userChatInput,
userFiles, userFiles,
parseHistoryFiles, parseHistoryFiles,
requestOrigin, maxFileAmount,
maxFiles,
customPdfParse, customPdfParse,
usageId, usageId,
runningUserInfo runningUserInfo
...@@ -40,8 +42,7 @@ export const getChatMessages = async ({ ...@@ -40,8 +42,7 @@ export const getChatMessages = async ({
userFiles: UserChatItemFileItemType[]; userFiles: UserChatItemFileItemType[];
parseHistoryFiles: boolean; parseHistoryFiles: boolean;
requestOrigin?: string; maxFileAmount: number;
maxFiles: number;
customPdfParse?: boolean; customPdfParse?: boolean;
usageId?: string; usageId?: string;
runningUserInfo: ChatDispatchProps['runningUserInfo']; runningUserInfo: ChatDispatchProps['runningUserInfo'];
...@@ -66,18 +67,20 @@ export const getChatMessages = async ({ ...@@ -66,18 +67,20 @@ export const getChatMessages = async ({
} }
]; ];
const messages = await rewriteChatMessagesWithFiles({ const messages = await rewriteChatMessagesWithFileContext({
messages: rawUserMessages, messages: rawUserMessages,
parseHistoryFiles, parseHistoryFiles,
maxFiles: maxFileAmount,
parseFileFn: async (urls) => { parseFileFn: async (urls) => {
const files = await parseFileContentFromUrls({ const files = await parseFileContentFromUrls({
urls, urls,
requestOrigin, // 每条消息已经按 Workflow 上限截取,这里统一解析去重后的文件集合。
maxFiles, maxFiles: urls.length,
teamId: runningUserInfo.teamId, teamId: runningUserInfo.teamId,
tmbId: runningUserInfo.tmbId, tmbId: runningUserInfo.tmbId,
customPdfParse, customPdfParse,
usageId usageId,
fileContext: getWorkflowFileContext()
}); });
return files.map((file) => ({ return files.map((file) => ({
......
...@@ -14,6 +14,7 @@ import { getChatMessages } from './chatMessages'; ...@@ -14,6 +14,7 @@ import { getChatMessages } from './chatMessages';
import { getDatasetCiteData } from './datasetCite'; import { getDatasetCiteData } from './datasetCite';
import { getAIChatFileContextConfig, getInputFiles } from './fileContext'; import { getAIChatFileContextConfig, getInputFiles } from './fileContext';
import type { ChatProps, ChatResponse } from './type'; import type { ChatProps, ChatResponse } from './type';
import { getWorkflowFileMaxAmount } from '../../../utils/context';
/** /**
* 调度 AI Chat 节点:整理引用、历史和文件上下文,调用 LLM,并写入节点响应与用量。 * 调度 AI Chat 节点:整理引用、历史和文件上下文,调用 LLM,并写入节点响应与用量。
...@@ -117,8 +118,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp ...@@ -117,8 +118,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
userChatInput, userChatInput,
userFiles, userFiles,
parseHistoryFiles: fileContextConfig.parseHistoryFiles, parseHistoryFiles: fileContextConfig.parseHistoryFiles,
requestOrigin, maxFileAmount: getWorkflowFileMaxAmount(),
maxFiles: chatConfig?.fileSelectConfig?.maxFiles || 20,
customPdfParse: chatConfig?.fileSelectConfig?.customPdfParse, customPdfParse: chatConfig?.fileSelectConfig?.customPdfParse,
usageId, usageId,
runningUserInfo runningUserInfo
......
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type'; import type { UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants'; import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import type { FlowNodeInputItemType } from '@fastgpt/global/core/workflow/type/io'; import type { FlowNodeInputItemType } from '@fastgpt/global/core/workflow/type/io';
import { parseUrlToFileType } from '../../../utils/context'; import { parseUrlToFileType } from '../../../utils/context';
import { rewriteChatMessagesWithFileContext } from '../../../../chat/fileContext';
import type { ChatMessageFileParser } from './type';
/** /**
* 根据 AI Chat 节点的文件链接输入计算文件上下文开关。 * 根据 AI Chat 节点的文件链接输入计算文件上下文开关。
...@@ -28,29 +26,6 @@ export const getAIChatFileContextConfig = ({ ...@@ -28,29 +26,6 @@ export const getAIChatFileContextConfig = ({
}; };
/** /**
* 在发送给 LLM 前把 Human 消息里的普通文件解析为文本。
*
* 当前轮 Human 消息始终允许根据已归一化的 `fileLinks` 解析;历史 Human 消息只有在
* 节点显式绑定文件链接输入时才解析。未绑定时会移除历史 Human 的 file item,避免后续
* 适配层继续从历史中生成 `file_url` / `image_url`。
*/
export const rewriteChatMessagesWithFiles = async ({
messages,
parseHistoryFiles,
parseFileFn
}: {
messages: ChatItemMiniType[];
parseHistoryFiles: boolean;
parseFileFn: ChatMessageFileParser;
}) => {
return rewriteChatMessagesWithFileContext({
messages,
parseHistoryFiles,
parseFileFn
});
};
/**
* 把节点输入的文件链接转换成聊天消息文件结构;无法识别的链接会被过滤。 * 把节点输入的文件链接转换成聊天消息文件结构;无法识别的链接会被过滤。
*/ */
export const getInputFiles = ({ fileLinks = [] }: { fileLinks?: string[] }) => { export const getInputFiles = ({ fileLinks = [] }: { fileLinks?: string[] }) => {
......
export { dispatchChatCompletion } from './dispatchChatCompletion'; export { dispatchChatCompletion } from './dispatchChatCompletion';
export { export { getAIChatFileContextConfig, getInputFiles } from './fileContext';
getAIChatFileContextConfig, export type { ChatProps, ChatResponse } from './type';
getInputFiles,
rewriteChatMessagesWithFiles
} from './fileContext';
export type { ChatMessageFileParser, ChatProps, ChatResponse } from './type';
...@@ -26,13 +26,3 @@ export type ChatResponse = DispatchNodeResultType< ...@@ -26,13 +26,3 @@ export type ChatResponse = DispatchNodeResultType<
[NodeOutputKeyEnum.errorText]: string; [NodeOutputKeyEnum.errorText]: string;
} }
>; >;
export type ChatMessageFileParser = (urls: string[]) => Promise<
{
id?: string;
name: string;
url: string;
sandboxPath?: string;
content?: string;
}[]
>;
import { ChatFileTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { getSafeSandboxInputFilename } from '../../../ai/sandbox/interface/runtime';
import { getWorkflowFileContext, parseUrlToFileType } from '../../utils/context';
import { isAbsoluteHttpUrl } from '../../utils/fileContext';
import {
rewriteAgentLoopCoreUserMessageWithFiles,
type AgentLoopCoreInputFile,
type AgentLoopCoreUserReminderContext
} from './agentLoopCore/interface';
type ParseWorkflowAIInputFilesParams = {
files: UserChatItemFileItemType[];
maxFileAmount: number;
};
type BuildWorkflowAICurrentInputFilesParams = {
currentFiles?: string[];
currentQuery?: ChatItemMiniType['value'];
maxFileAmount: number;
};
type RewriteWorkflowAIUserMessageWithFilesParams = {
message: ChatItemMiniType;
maxFileAmount: number;
files?: AgentLoopCoreInputFile[];
query?: string;
reminderContext?: AgentLoopCoreUserReminderContext;
transformFiles?: (files: AgentLoopCoreInputFile[]) => AgentLoopCoreInputFile[];
};
type RewriteWorkflowAIHistoryMessageWithFilesParams = Pick<
RewriteWorkflowAIUserMessageWithFilesParams,
'message' | 'maxFileAmount' | 'transformFiles'
> & {
parseHistoryFiles: boolean;
};
/**
* 将 Workflow AI 输入文件转换成可直接交给模型的 URL 文件描述。
*
* 已登记文件使用 WorkflowFileContext 提供的 modelUrl 和 identity;未登记外链仅接受绝对
* HTTP(S) URL。返回结果按 identity 去重、按请求上限截断,并统一清洗文件名。
*/
export const parseWorkflowAIInputFiles = ({
files,
maxFileAmount
}: ParseWorkflowAIInputFilesParams): AgentLoopCoreInputFile[] => {
const workflowFileContext = getWorkflowFileContext();
const normalizedFiles = files
.map((file) => ({
file,
ref: workflowFileContext?.resolve(file.url)
}))
.map(({ file, ref }) => {
const url = ref?.modelUrl ?? file.url;
if (!isAbsoluteHttpUrl(url)) return;
return {
file,
url,
identity: workflowFileContext?.getIdentity(file.url) ?? url
};
})
.filter(Boolean) as { file: UserChatItemFileItemType; url: string; identity: string }[];
const uniqueFiles = Array.from(
normalizedFiles
.reduce((map, item) => {
if (!map.has(item.identity)) {
map.set(item.identity, item);
}
return map;
}, new Map<string, (typeof normalizedFiles)[number]>())
.values()
);
const usedNames = new Map<string, number>();
return uniqueFiles
.slice(0, maxFileAmount)
.map(({ file, url }, index) => {
const parsedFile = parseUrlToFileType(url);
if (!parsedFile) return;
const type = file.type && file.type !== ChatFileTypeEnum.file ? file.type : parsedFile.type;
return {
name: getSafeSandboxInputFilename(file.name || parsedFile.name || url, index, usedNames),
type,
url: parsedFile.url
};
})
.filter(Boolean) as AgentLoopCoreInputFile[];
};
/** 合并 query 文件和节点文件输入,并使用统一的 Workflow Context 规则生成本轮文件列表。 */
export const buildWorkflowAICurrentInputFiles = ({
currentFiles = [],
currentQuery,
maxFileAmount
}: BuildWorkflowAICurrentInputFilesParams): AgentLoopCoreInputFile[] => {
const { files: queryFiles = [] } = currentQuery
? chatValue2RuntimePrompt(currentQuery)
: { files: [] };
const inputUrls = [...queryFiles.map((file) => file.url), ...currentFiles].filter(
(url, index, list) => url && list.indexOf(url) === index
);
const queryFilesByUrl = new Map(queryFiles.map((file) => [file.url, file]));
return parseWorkflowAIInputFiles({
files: inputUrls.map((url) => queryFilesByUrl.get(url) || { type: ChatFileTypeEnum.file, url }),
maxFileAmount
});
};
/**
* Agent 与 ToolCall 共用的 Workflow 文件 query 改写入口。
*
* 外层先完成 Context URL 解析和可选 Sandbox 路径补充,再交给纯 AgentLoop 消息逻辑完成
* 文档 reminder 与多模态消息分流。
*/
export const rewriteWorkflowAIUserMessageWithFiles = ({
message,
maxFileAmount,
files: inputFiles,
query,
reminderContext,
transformFiles
}: RewriteWorkflowAIUserMessageWithFilesParams) => {
const { files: messageFiles = [] } = chatValue2RuntimePrompt(message.value);
const parsedFiles =
inputFiles ?? parseWorkflowAIInputFiles({ files: messageFiles, maxFileAmount });
const files = transformFiles?.(parsedFiles) ?? parsedFiles;
return {
files,
message: rewriteAgentLoopCoreUserMessageWithFiles({
message,
files,
query,
reminderContext
})
};
};
/**
* 按节点文件输入绑定状态改写历史消息。
*
* 未启用历史文件时移除 Human 消息中的全部文件项;启用时复用当前消息的文件归一化、
* 去重和文档/多模态分流规则。非 Human 消息保持原样。
*/
export const rewriteWorkflowAIHistoryMessageWithFiles = ({
message,
maxFileAmount,
parseHistoryFiles,
transformFiles
}: RewriteWorkflowAIHistoryMessageWithFilesParams) => {
if (message.obj !== ChatRoleEnum.Human) {
return { message, files: [] };
}
if (!parseHistoryFiles) {
return {
message: {
...message,
value: message.value.filter((item) => !item.file)
},
files: []
};
}
return rewriteWorkflowAIUserMessageWithFiles({
message,
maxFileAmount,
transformFiles
});
};
import { getErrText } from '@fastgpt/global/common/error/utils';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { i18nT } from '@fastgpt/global/common/i18n/utils';
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { batchRun } from '@fastgpt/global/common/system/utils';
import { getFileContentByUrl } from '../../../chat/fileContext';
import { getWorkflowFileContext } from '../../utils/context';
import { AgentNodeResponseDisplay } from './agentLoopCore/interface';
export type WorkflowReadFileItem = {
name?: string;
url: string;
};
type DispatchWorkflowReadFilesParams = {
files: WorkflowReadFileItem[];
teamId: string;
tmbId: string;
customPdfParse?: boolean;
usageId?: string;
maxFileAmount: number;
};
/**
* 通过 Workflow 文件上下文批量解析模型提交的 URL。
*
* Context 命中的 URL 读取已授权私有对象;其余绝对 URL 作为运行期动态外链读取,
* 不应用上传文件域名白名单,但仍保留 HTTP(S)、SSRF、重定向和文件大小限制。
* 每次调用只读取前 maxFileAmount 个文件,并以固定 5 并发执行,避免模型参数放大资源占用。
* 单文件失败不会中断同批其他文件,ToolCall 和 Agent 共用相同 JSON 与节点响应语义。
*/
export const dispatchWorkflowReadFiles = async ({
files,
teamId,
tmbId,
customPdfParse,
usageId,
maxFileAmount
}: DispatchWorkflowReadFilesParams) => {
const usages: ChatNodeUsageType[] = [];
const readFilesResult = await batchRun(
files.slice(0, maxFileAmount),
async ({ url, name: inputName }) => {
try {
const { name, content } = await getFileContentByUrl({
url,
teamId,
tmbId,
customPdfParse,
usageId,
fileContext: getWorkflowFileContext(),
validateExternalUrlDomain: false
});
return {
url,
name: inputName ?? name,
content
};
} catch (error) {
return {
url,
name: inputName ?? url,
content: '',
error: getErrText(error, 'Load file error')
};
}
},
5
);
const toolResponse = readFilesResult.map((item) =>
'error' in item
? { name: item.name, content: item.content, error: item.error }
: { name: item.name, content: item.content }
);
return {
response: JSON.stringify(toolResponse),
usages,
nodeResponse: {
moduleType: FlowNodeTypeEnum.readFiles,
moduleName: i18nT('chat:read_file'),
moduleLogo: AgentNodeResponseDisplay.readFile.moduleLogo,
readFiles: readFilesResult.map(({ name, url }) => ({ name, url }))
}
};
};
...@@ -5,8 +5,11 @@ import { ...@@ -5,8 +5,11 @@ import {
} from '@fastgpt/global/core/chat/adapt'; } from '@fastgpt/global/core/chat/adapt';
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type'; import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants'; import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants';
import { formatUserQueryWithFiles, parseFileInfoFromUrls } from '../../../../../chat/fileContext'; import { getWorkflowFileMaxAmount, parseUrlToFileType } from '../../../../utils/context';
import { parseUrlToFileType } from '../../../../utils/context'; import {
rewriteWorkflowAIHistoryMessageWithFiles,
rewriteWorkflowAIUserMessageWithFiles
} from '../../fileContext';
import type { DispatchToolModuleProps, FileInputType } from '../type'; import type { DispatchToolModuleProps, FileInputType } from '../type';
const getUserFilesFromLinks = ({ fileLinks = [] }: { fileLinks?: string[] }) => { const getUserFilesFromLinks = ({ fileLinks = [] }: { fileLinks?: string[] }) => {
...@@ -22,11 +25,10 @@ export const useToolMessages = async ({ ...@@ -22,11 +25,10 @@ export const useToolMessages = async ({
responseChatItemId, responseChatItemId,
userChatInput, userChatInput,
fileLinks, fileLinks,
parseHistoryFiles,
lastInteractive, lastInteractive,
isEntry, isEntry,
chatConfig, chatConfig,
requestOrigin,
runningUserInfo,
useSandbox useSandbox
}: { }: {
defaultSystemPrompt?: string; defaultSystemPrompt?: string;
...@@ -35,95 +37,66 @@ export const useToolMessages = async ({ ...@@ -35,95 +37,66 @@ export const useToolMessages = async ({
responseChatItemId?: string; responseChatItemId?: string;
userChatInput: string; userChatInput: string;
fileLinks?: string[]; fileLinks?: string[];
parseHistoryFiles: boolean;
lastInteractive: DispatchToolModuleProps['lastInteractive']; lastInteractive: DispatchToolModuleProps['lastInteractive'];
isEntry?: boolean; isEntry?: boolean;
chatConfig: DispatchToolModuleProps['chatConfig']; chatConfig: DispatchToolModuleProps['chatConfig'];
requestOrigin?: string;
runningUserInfo: DispatchToolModuleProps['runningUserInfo'];
useSandbox: boolean; useSandbox: boolean;
}) => { }) => {
const allFiles = new Map<string, FileInputType>();
const currentInputFiles: FileInputType[] = []; const currentInputFiles: FileInputType[] = [];
const userFiles = getUserFilesFromLinks({ fileLinks }); const userFiles = getUserFilesFromLinks({ fileLinks });
const concatenateSystemPrompt = [defaultSystemPrompt, systemPrompt] const concatenateSystemPrompt = [defaultSystemPrompt, systemPrompt]
.filter(Boolean) .filter(Boolean)
.join('\n\n-----\n\n'); .join('\n\n-----\n\n');
const isInteractiveResume = !!lastInteractive && !!isEntry; const isInteractiveResume = !!lastInteractive && !!isEntry;
const value: ChatItemMiniType[] = [ const historyMessages: ChatItemMiniType[] = [
...getSystemPrompt_ChatItemType(concatenateSystemPrompt), ...getSystemPrompt_ChatItemType(concatenateSystemPrompt),
...chatHistories, ...chatHistories
// child interactive 的用户输入由子 workflow 消费,不作为父模型的新 user message。
// 历史中的上一条 AI tool_call 必须保留,provider 依赖它恢复原 call 名称和参数。
...(!isInteractiveResume
? [
{
dataId: responseChatItemId,
obj: ChatRoleEnum.Human,
value: runtimePrompt2ChatsValue({
text: userChatInput,
files: userFiles
})
} as ChatItemMiniType
]
: [])
]; ];
const runtimeMessages = value; const maxFileAmount = getWorkflowFileMaxAmount();
const maxFiles = chatConfig?.fileSelectConfig?.maxFiles || 20;
/** /**
* 文件链接会被替换成供 LLM 调用 read_file 的 id。 * 文件链接会作为 model URL 暴露给 LLM,供 read_files 和其他工具直接使用。
* 当前轮输入文件额外记录下来,用于 sandbox 场景先上传到隔离目录。 * 当前轮输入文件额外记录下来,用于 sandbox 场景先上传到隔离目录。
*/ */
const messages = await Promise.all( const transformFiles = useSandbox
runtimeMessages.map(async (message, index): Promise<ChatItemMiniType> => { ? (files: FileInputType[]) =>
if (message.obj !== ChatRoleEnum.Human) { files.map((file) => ({
return message; ...file,
} sandboxPath: `${SANDBOX_USER_FILES_PATH}${file.name}`
}))
/** : undefined;
* 重写 human query,把文件 URL 替换成 read_file 可识别的文件 id。 const messages = historyMessages.map(
*/ (message) =>
const prefixId = message.dataId || `${index}`; rewriteWorkflowAIHistoryMessageWithFiles({
const query = await formatUserQueryWithFiles({ message,
userQuery: message.value, maxFileAmount,
parseFileFn: async (urls) => { parseHistoryFiles,
const files = await parseFileInfoFromUrls({ transformFiles
urls, }).message
requestOrigin,
maxFiles,
teamId: runningUserInfo.teamId
}).then((res) =>
res
.filter((item) => item.success)
.map((item, index) => ({
id: `${prefixId}-${index}`,
name: item.name,
url: item.url,
sandboxPath: useSandbox ? `${SANDBOX_USER_FILES_PATH}${item.name}` : undefined
}))
);
files.forEach((file) => {
allFiles.set(file.id, file);
});
if (index === runtimeMessages.length - 1) {
currentInputFiles.push(...files);
}
return files;
}
});
return {
...message,
value: query
};
})
); );
// child interactive 的用户输入由子 workflow 消费,不作为父模型的新 user message。
// 历史中的上一条 AI tool_call 必须保留,provider 依赖它恢复原 call 名称和参数。
if (!isInteractiveResume) {
const { message, files } = rewriteWorkflowAIUserMessageWithFiles({
message: {
dataId: responseChatItemId,
obj: ChatRoleEnum.Human,
value: runtimePrompt2ChatsValue({
text: userChatInput,
files: userFiles
})
},
maxFileAmount,
transformFiles
});
messages.push(message);
currentInputFiles.push(...files);
}
return { return {
messages, messages,
allFiles,
currentInputFiles currentInputFiles
}; };
}; };
...@@ -10,6 +10,7 @@ import { useToolNodeList } from './hooks/useToolNodeList'; ...@@ -10,6 +10,7 @@ import { useToolNodeList } from './hooks/useToolNodeList';
import { useToolMessages } from './hooks/useToolMessages'; import { useToolMessages } from './hooks/useToolMessages';
import { checkTeamSandboxPermission } from '../../../../../support/permission/teamLimit'; import { checkTeamSandboxPermission } from '../../../../../support/permission/teamLimit';
import { prepareSandboxToolRuntime } from '../../../../ai/sandbox/interface/toolCall'; import { prepareSandboxToolRuntime } from '../../../../ai/sandbox/interface/toolCall';
import { readWorkflowFileBuffer } from '../../../utils/context';
import { import {
createAgentSandboxPermissionDeniedError, createAgentSandboxPermissionDeniedError,
getRunningSandboxId, getRunningSandboxId,
...@@ -36,7 +37,6 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise< ...@@ -36,7 +37,6 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
runtimeNodes, runtimeNodes,
runtimeEdges, runtimeEdges,
histories, histories,
requestOrigin,
chatConfig, chatConfig,
lastInteractive, lastInteractive,
runningUserInfo, runningUserInfo,
...@@ -75,10 +75,8 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise< ...@@ -75,10 +75,8 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
const useVideo = aiChatVideo && toolModel.video; const useVideo = aiChatVideo && toolModel.video;
const chatHistories = getAgentLoopHistories(history, histories); const chatHistories = getAgentLoopHistories(history, histories);
const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList); const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList);
const fileLinks = const parseHistoryFiles = !!fileUrlInput?.value?.length;
!fileUrlInput || !fileUrlInput.value || fileUrlInput.value.length === 0 const fileLinks = parseHistoryFiles ? rawFileLinks : undefined;
? undefined
: rawFileLinks;
props.params.aiChatVision = aiChatVision && toolModel.vision; props.params.aiChatVision = aiChatVision && toolModel.vision;
props.params.aiChatAudio = useAudio; props.params.aiChatAudio = useAudio;
...@@ -97,18 +95,17 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise< ...@@ -97,18 +95,17 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
// 交互恢复入口会由子工具继续接管,父 ToolCall 节点本轮不再作为入口节点。 // 交互恢复入口会由子工具继续接管,父 ToolCall 节点本轮不再作为入口节点。
props.node.isEntry = false; props.node.isEntry = false;
const { messages, allFiles, currentInputFiles } = await useToolMessages({ const { messages, currentInputFiles } = await useToolMessages({
defaultSystemPrompt: toolModel.defaultSystemChatPrompt, defaultSystemPrompt: toolModel.defaultSystemChatPrompt,
systemPrompt, systemPrompt,
chatHistories, chatHistories,
responseChatItemId, responseChatItemId,
userChatInput, userChatInput,
fileLinks, fileLinks,
parseHistoryFiles,
lastInteractive, lastInteractive,
isEntry, isEntry,
chatConfig, chatConfig,
requestOrigin,
runningUserInfo,
useSandbox useSandbox
}); });
...@@ -127,6 +124,7 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise< ...@@ -127,6 +124,7 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
sourceId: props.runningAppInfo.sourceId, sourceId: props.runningAppInfo.sourceId,
userId: props.uid, userId: props.uid,
chatId: props.chatId, chatId: props.chatId,
readInputFile: (url) => readWorkflowFileBuffer({ url }),
files: currentInputFiles.map((file) => ({ files: currentInputFiles.map((file) => ({
path: file.sandboxPath!, path: file.sandboxPath!,
url: file.url url: file.url
...@@ -173,7 +171,6 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise< ...@@ -173,7 +171,6 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
return runToolCall({ return runToolCall({
...props, ...props,
allFiles,
currentInputFiles, currentInputFiles,
sandboxClient, sandboxClient,
runtimeNodes, runtimeNodes,
......
...@@ -33,8 +33,7 @@ type ResponseType = { ...@@ -33,8 +33,7 @@ type ResponseType = {
}; };
export const runToolCall = async (props: DispatchToolModuleProps): Promise<ResponseType> => { export const runToolCall = async (props: DispatchToolModuleProps): Promise<ResponseType> => {
const { messages, toolNodes, toolModel, childrenInteractiveParams, allFiles, ...workflowProps } = const { messages, toolNodes, toolModel, childrenInteractiveParams, ...workflowProps } = props;
props;
const { const {
checkIsStopping, checkIsStopping,
runtimeNodes, runtimeNodes,
...@@ -85,8 +84,6 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo ...@@ -85,8 +84,6 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo
workflowProps, workflowProps,
runtimeNodes, runtimeNodes,
runtimeEdges, runtimeEdges,
allFiles,
fileUrlList,
cacheToolFlowResponse: runtimeEnvironment.cacheToolFlowResponse cacheToolFlowResponse: runtimeEnvironment.cacheToolFlowResponse
}); });
getProviderToolInfo = toolProvider.getToolInfo; getProviderToolInfo = toolProvider.getToolInfo;
...@@ -134,6 +131,7 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo ...@@ -134,6 +131,7 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo
readFile: toolProvider.readFileExecutor readFile: toolProvider.readFileExecutor
? { ? {
enabled: true, enabled: true,
maxFileAmount: toolProvider.readFileMaxFileAmount,
execute: toolProvider.readFileExecutor execute: toolProvider.readFileExecutor
} }
: undefined, : undefined,
......
...@@ -13,7 +13,8 @@ import { ...@@ -13,7 +13,8 @@ import {
import { runWorkflow } from '../../../index'; import { runWorkflow } from '../../../index';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant'; import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { dispatchReadFileTool, getToolCallFileUrl } from '../tools/file'; import { dispatchWorkflowReadFiles } from '../../readFiles';
import { getWorkflowFileMaxAmount } from '../../../../utils/context';
type CacheToolFlowResponse = (args: { type CacheToolFlowResponse = (args: {
callId: string; callId: string;
...@@ -35,8 +36,6 @@ export const createToolCallToolProvider = async ({ ...@@ -35,8 +36,6 @@ export const createToolCallToolProvider = async ({
workflowProps, workflowProps,
runtimeNodes, runtimeNodes,
runtimeEdges, runtimeEdges,
allFiles,
fileUrlList,
cacheToolFlowResponse cacheToolFlowResponse
}: { }: {
messages: DispatchToolModuleProps['messages']; messages: DispatchToolModuleProps['messages'];
...@@ -45,17 +44,10 @@ export const createToolCallToolProvider = async ({ ...@@ -45,17 +44,10 @@ export const createToolCallToolProvider = async ({
lang: DispatchToolModuleProps['lang']; lang: DispatchToolModuleProps['lang'];
workflowProps: Omit< workflowProps: Omit<
DispatchToolModuleProps, DispatchToolModuleProps,
| 'messages' 'messages' | 'toolNodes' | 'toolModel' | 'childrenInteractiveParams' | 'currentInputFiles'
| 'toolNodes'
| 'toolModel'
| 'childrenInteractiveParams'
| 'allFiles'
| 'currentInputFiles'
>; >;
runtimeNodes: DispatchToolModuleProps['runtimeNodes']; runtimeNodes: DispatchToolModuleProps['runtimeNodes'];
runtimeEdges: DispatchToolModuleProps['runtimeEdges']; runtimeEdges: DispatchToolModuleProps['runtimeEdges'];
allFiles: DispatchToolModuleProps['allFiles'];
fileUrlList?: string[];
cacheToolFlowResponse: CacheToolFlowResponse; cacheToolFlowResponse: CacheToolFlowResponse;
}): Promise<ToolCallToolProvider> => { }): Promise<ToolCallToolProvider> => {
const { finalMessages, tools, getToolInfo } = await useToolCatalog({ const { finalMessages, tools, getToolInfo } = await useToolCatalog({
...@@ -94,38 +86,18 @@ export const createToolCallToolProvider = async ({ ...@@ -94,38 +86,18 @@ export const createToolCallToolProvider = async ({
runWorkflowTool, runWorkflowTool,
cacheToolFlowResponse cacheToolFlowResponse
}); });
const readFileMaxFileAmount = getWorkflowFileMaxAmount();
const readFileExecutor = createAgentLoopCoreReadFileExecutor({ const readFileExecutor = createAgentLoopCoreReadFileExecutor({
enabled: allFiles.size > 0, enabled: true,
resolveFiles: (ids) => execute: async ({ files }) =>
ids.map((id) => { dispatchWorkflowReadFiles({
const file = allFiles.get(id);
return {
id,
...(file?.name ? { name: file.name } : {}),
url: getToolCallFileUrl({
id,
allFiles,
fileUrlList
})
};
}),
execute: async ({ callId, files }) => {
const result = await dispatchReadFileTool({
files, files,
toolCallId: callId,
teamId: workflowProps.runningUserInfo.teamId, teamId: workflowProps.runningUserInfo.teamId,
tmbId: workflowProps.runningUserInfo.tmbId, tmbId: workflowProps.runningUserInfo.tmbId,
customPdfParse: workflowProps.chatConfig?.fileSelectConfig?.customPdfParse, customPdfParse: workflowProps.chatConfig?.fileSelectConfig?.customPdfParse,
usageId: workflowProps.usageId usageId: workflowProps.usageId,
}); maxFileAmount: readFileMaxFileAmount
})
return {
response: result.response,
usages: result.usages,
nodeResponse: result.flowResponse.flowResponses[0]
};
}
}); });
return { return {
...@@ -137,6 +109,7 @@ export const createToolCallToolProvider = async ({ ...@@ -137,6 +109,7 @@ export const createToolCallToolProvider = async ({
params: AgentLoopInteractiveToolExecuteParams<WorkflowInteractiveResponseType> params: AgentLoopInteractiveToolExecuteParams<WorkflowInteractiveResponseType>
) => runInteractiveTool(params), ) => runInteractiveTool(params),
readFileExecutor, readFileExecutor,
readFileMaxFileAmount,
datasetSearchExecutor: datasetSearchExecutor:
datasetSearchNodeIds.length > 0 datasetSearchNodeIds.length > 0
? createAgentLoopCoreWorkflowSystemToolExecutor({ ? createAgentLoopCoreWorkflowSystemToolExecutor({
......
...@@ -13,5 +13,6 @@ export type ToolCallToolProvider = AgentLoopCoreToolProvider< ...@@ -13,5 +13,6 @@ export type ToolCallToolProvider = AgentLoopCoreToolProvider<
finalMessages: ChatCompletionMessageParam[]; finalMessages: ChatCompletionMessageParam[];
getToolInfo: (name: string) => ToolInfo | undefined; getToolInfo: (name: string) => ToolInfo | undefined;
readFileExecutor?: AgentLoopReadFileExecutor; readFileExecutor?: AgentLoopReadFileExecutor;
readFileMaxFileAmount: number;
datasetSearchExecutor?: AgentLoopDatasetSearchExecutor; datasetSearchExecutor?: AgentLoopDatasetSearchExecutor;
}; };
import type { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { getFileContentByUrl } from '../../../../../chat/fileContext';
import { getErrText } from '@fastgpt/global/common/error/utils';
import { getLogger } from '@fastgpt-sdk/otel/logger';
import { LogCategories } from '../../../../../../common/logger';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { i18nT } from '@fastgpt/global/common/i18n/utils';
import {
AgentNodeResponseDisplay,
type AgentLoopCoreToolRunFlowResponse
} from '../../agentLoopCore/interface';
import type { FileInputType } from '../type';
const logger = getLogger(LogCategories.MODULE.AI.TOOL_CALL);
export const ReadFileToolDisplay = {
name: {
'zh-CN': '文件解析',
en: 'File parse',
'zh-Hant': '文件解析'
},
avatar: AgentNodeResponseDisplay.readFile.moduleLogo
};
export const getToolCallFileUrl = ({
id,
allFiles,
fileUrlList
}: {
id: string;
allFiles: Map<string, FileInputType>;
fileUrlList?: string[];
}) => {
const file = allFiles.get(id);
if (file?.url) return file.url;
const index = Number(id);
if (Array.isArray(fileUrlList) && Number.isInteger(index) && index >= 0) {
return fileUrlList[index] || '';
}
return '';
};
type FileReadParams = {
files: { id: string; name?: string; url: string }[];
toolCallId: string;
teamId: string;
tmbId: string;
customPdfParse?: boolean;
usageId?: string;
};
export const dispatchReadFileTool = async ({
files,
toolCallId,
teamId,
tmbId,
customPdfParse,
usageId
}: FileReadParams) => {
const startTime = Date.now();
const usages: ChatNodeUsageType[] = [];
const getFlowResponse = (
nodeResponse: Record<string, any> = {}
): AgentLoopCoreToolRunFlowResponse => ({
flowResponses: [
{
...nodeResponse,
moduleType: FlowNodeTypeEnum.readFiles,
moduleName: i18nT('chat:read_file'),
moduleLogo: ReadFileToolDisplay.avatar,
id: toolCallId,
nodeId: toolCallId,
runningTime: +((Date.now() - startTime) / 1000).toFixed(2),
totalPoints: usages.reduce((sum, item) => sum + item.totalPoints, 0)
}
],
flowUsages: usages,
runTimes: 0
});
try {
const readFilesResult = await Promise.all(
files.map(async ({ url, id, name: inputName }) => {
try {
const { name, content } = await getFileContentByUrl({
url,
teamId,
tmbId,
customPdfParse,
usageId
});
return {
id,
name: inputName || name,
content
};
} catch (error) {
return {
id,
name: inputName || url,
content: getErrText(error, 'Load file error')
};
}
})
);
// Stringify the result
const response = readFilesResult
.map(
(file) => `<file>
<id>${file.id}</id>
<name>${file.name}</name>
<content>${file.content}</content>
</file>`
)
.join('\n');
return {
response,
usages,
flowResponse: getFlowResponse({
readFiles: readFilesResult.map((file) => ({
name: file.name,
url: files.find((item) => item.id === file.id)?.url || ''
}))
})
};
} catch (error) {
logger.error('[File Read] Compression failed, using original content', { error });
const response = `Failed to read file: ${getErrText(error)}`;
const nodeResponse = {
errorText: response
};
return {
response,
usages,
flowResponse: getFlowResponse(nodeResponse)
};
}
};
...@@ -9,6 +9,7 @@ import type { JSONSchemaInputType } from '@fastgpt/global/core/app/jsonschema'; ...@@ -9,6 +9,7 @@ import type { JSONSchemaInputType } from '@fastgpt/global/core/app/jsonschema';
import type { ReasoningEffort } from '@fastgpt/global/core/ai/llm/type'; import type { ReasoningEffort } from '@fastgpt/global/core/ai/llm/type';
import type { AgentLoopChildrenInteractiveParams } from '../../../../ai/llm/agentLoop/interface'; import type { AgentLoopChildrenInteractiveParams } from '../../../../ai/llm/agentLoop/interface';
import type { SandboxClient } from '../../../../ai/sandbox/interface/runtime'; import type { SandboxClient } from '../../../../ai/sandbox/interface/runtime';
import type { AgentLoopCoreInputFile } from '../agentLoopCore/interface';
export type DispatchToolModuleProps = ModuleDispatchProps<{ export type DispatchToolModuleProps = ModuleDispatchProps<{
[NodeInputKeyEnum.history]?: ChatItemMiniType[]; [NodeInputKeyEnum.history]?: ChatItemMiniType[];
...@@ -37,7 +38,6 @@ export type DispatchToolModuleProps = ModuleDispatchProps<{ ...@@ -37,7 +38,6 @@ export type DispatchToolModuleProps = ModuleDispatchProps<{
toolNodes: ToolNodeItemType[]; toolNodes: ToolNodeItemType[];
toolModel: LLMModelItemType; toolModel: LLMModelItemType;
childrenInteractiveParams?: AgentLoopChildrenInteractiveParams<WorkflowInteractiveResponseType>; childrenInteractiveParams?: AgentLoopChildrenInteractiveParams<WorkflowInteractiveResponseType>;
allFiles: Map<string, FileInputType>;
currentInputFiles: FileInputType[]; currentInputFiles: FileInputType[];
sandboxClient?: SandboxClient; sandboxClient?: SandboxClient;
}; };
...@@ -54,9 +54,4 @@ export type ToolNodeItemType = { ...@@ -54,9 +54,4 @@ export type ToolNodeItemType = {
toolParams: RuntimeNodeItemType['inputs']; toolParams: RuntimeNodeItemType['inputs'];
}; };
export type FileInputType = { export type FileInputType = AgentLoopCoreInputFile;
id: string;
name: string;
url: string;
sandboxPath?: string;
};
...@@ -13,13 +13,13 @@ import type { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants'; ...@@ -13,13 +13,13 @@ import type { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants'; import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { getNodeErrResponse, getHistories } from '../utils'; import { getNodeErrResponse, getHistories } from '../utils';
import { WorkflowVariableState } from '../utils/variables'; import { getWorkflowFileVariableInputs, WorkflowVariableState } from '../utils/variables';
import { chatValue2RuntimePrompt, runtimePrompt2ChatsValue } from '@fastgpt/global/core/chat/adapt'; import { chatValue2RuntimePrompt, runtimePrompt2ChatsValue } from '@fastgpt/global/core/chat/adapt';
import type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime'; import type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime';
import { authAppByTmbId } from '../../../../support/permission/app/auth'; import { authAppByTmbId } from '../../../../support/permission/app/auth';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant'; import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
import { getAppVersionById } from '../../../app/version/controller'; import { getAppVersionById } from '../../../app/version/controller';
import { parseUrlToFileType } from '../../utils/context'; import { parseUrlToFileType, runWithDerivedWorkflowFileContext } from '../../utils/context';
import { getUserChatInfo } from '../../../../support/user/team/utils'; import { getUserChatInfo } from '../../../../support/user/team/utils';
import { getRunningUserInfoByTmbId } from '../../../../support/user/team/utils'; import { getRunningUserInfoByTmbId } from '../../../../support/user/team/utils';
import { getRuntimeNodeResponseSummary } from '../utils'; import { getRuntimeNodeResponseSummary } from '../utils';
...@@ -95,29 +95,6 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => { ...@@ -95,29 +95,6 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {
const chatHistories = getHistories(history, histories); const chatHistories = getHistories(history, histories);
// Rewrite children app variables
const { externalProvider } = await getUserChatInfo(appData.tmbId);
const childRunningAppInfo = {
sourceType: ChatSourceTypeEnum.app,
sourceId: String(appData._id),
teamId: appData.teamId,
tmbId: appData.tmbId,
name: appData.name,
isChildApp: true
};
const childrenVariableState = await WorkflowVariableState.create({
timezone: props.timezone,
runningAppInfo: childRunningAppInfo,
chatId: props.chatId,
responseChatItemId: props.responseChatItemId,
histories: chatHistories,
uid: props.uid,
variablesConfig: chatConfig.variables,
inputVariables: childrenAppVariables,
externalVariables: externalProvider?.externalWorkflowVariables,
sourceVariableState: variableState
});
const childrenInteractive = const childrenInteractive =
lastInteractive?.type === 'childrenInteractive' lastInteractive?.type === 'childrenInteractive'
? lastInteractive.params.childrenResponse ? lastInteractive.params.childrenResponse
...@@ -135,6 +112,19 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => { ...@@ -135,6 +112,19 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {
? query ? query
: runtimePrompt2ChatsValue({ files: userInputFiles, text: userChatInput }); : runtimePrompt2ChatsValue({ files: userInputFiles, text: userChatInput });
// Rewrite children app variables
const { externalProvider } = await getUserChatInfo(appData.tmbId);
const childRunningAppInfo = {
sourceType: ChatSourceTypeEnum.app,
sourceId: String(appData._id),
teamId: appData.teamId,
tmbId: appData.tmbId,
name: appData.name,
isChildApp: true
};
let filteredChildHistories = chatHistories;
let filteredChildQuery = theQuery;
const { const {
flowUsages, flowUsages,
assistantResponses, assistantResponses,
...@@ -143,37 +133,63 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => { ...@@ -143,37 +133,63 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {
system_memories, system_memories,
customFeedbacks, customFeedbacks,
runtimeNodeResponseSummary runtimeNodeResponseSummary
} = await runWorkflow({ } = await runWithDerivedWorkflowFileContext({
...props,
lastInteractive: childrenInteractive,
// Rewrite stream mode
...(system_forbid_stream
? {
stream: false,
workflowStreamResponse: undefined
}
: {}),
runningAppInfo: {
sourceType: ChatSourceTypeEnum.app,
sourceId: String(appData._id),
name: appData.name,
teamId: String(appData.teamId),
tmbId: String(appData.tmbId),
isChildApp: true
},
runningUserInfo: await getRunningUserInfoByTmbId(appData.tmbId),
runtimeNodes,
runtimeEdges,
histories: chatHistories,
variableState: childrenVariableState,
query: theQuery, query: theQuery,
chatConfig histories: chatHistories,
files: getWorkflowFileVariableInputs({
variablesConfig: chatConfig.variables,
inputVariables: childrenAppVariables
}),
fn: async ({ resolveInputFile, query: childQuery, histories: childHistories }) => {
filteredChildHistories = childHistories;
filteredChildQuery = childQuery;
const childrenVariableState = await WorkflowVariableState.create({
timezone: props.timezone,
runningAppInfo: childRunningAppInfo,
chatId: props.chatId,
responseChatItemId: props.responseChatItemId,
histories: childHistories,
uid: props.uid,
variablesConfig: chatConfig.variables,
inputVariables: childrenAppVariables,
externalVariables: externalProvider?.externalWorkflowVariables,
sourceVariableState: variableState,
resolveInputFile
});
return runWorkflow({
...props,
lastInteractive: childrenInteractive,
// Rewrite stream mode
...(system_forbid_stream
? {
stream: false,
workflowStreamResponse: undefined
}
: {}),
runningAppInfo: {
sourceType: ChatSourceTypeEnum.app,
sourceId: String(appData._id),
name: appData.name,
teamId: String(appData.teamId),
tmbId: String(appData.tmbId),
isChildApp: true
},
runningUserInfo: await getRunningUserInfoByTmbId(appData.tmbId),
runtimeNodes,
runtimeEdges,
histories: childHistories,
variableState: childrenVariableState,
query: childQuery,
chatConfig
});
}
}); });
const completeMessages = chatHistories.concat([ const completeMessages = filteredChildHistories.concat([
{ {
obj: ChatRoleEnum.Human, obj: ChatRoleEnum.Human,
value: query value: filteredChildQuery
}, },
{ {
obj: ChatRoleEnum.AI, obj: ChatRoleEnum.AI,
......
...@@ -49,16 +49,14 @@ import { checkTeamAIPoints } from '../../../support/permission/teamLimit'; ...@@ -49,16 +49,14 @@ import { checkTeamAIPoints } from '../../../support/permission/teamLimit';
import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants'; import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
import { createChatUsageRecord, pushChatItemUsage } from '../../../support/wallet/usage/controller'; import { createChatUsageRecord, pushChatItemUsage } from '../../../support/wallet/usage/controller';
import type { RequireOnlyOne } from '@fastgpt/global/common/type/utils'; import type { RequireOnlyOne } from '@fastgpt/global/common/type/utils';
import { createChatFilePreviewUrlGetter } from '../../../common/s3/sources/chat';
import { addPreviewUrlToChatItems } from '../../chat/utils'; import { addPreviewUrlToChatItems } from '../../chat/utils';
import { TeamErrEnum } from '@fastgpt/global/common/error/code/team'; import { TeamErrEnum } from '@fastgpt/global/common/error/code/team';
import { i18nT } from '@fastgpt/global/common/i18n/utils'; import { i18nT } from '@fastgpt/global/common/i18n/utils';
import { validateFileUrlDomain } from '../../../common/security/fileUrlValidator';
import { classifyEdgesByDFS, findSCCs, isNodeInCycle, getEdgeType } from '../utils/tarjan'; import { classifyEdgesByDFS, findSCCs, isNodeInCycle, getEdgeType } from '../utils/tarjan';
import { observeWorkflowRun, observeWorkflowStep } from '../metrics'; import { observeWorkflowRun, observeWorkflowStep } from '../metrics';
import { withActiveSpan } from '../../../common/tracing'; import { withActiveSpan } from '../../../common/tracing';
import { delAgentRuntimeStopSign, shouldWorkflowStop } from './workflowStatus'; import { delAgentRuntimeStopSign, shouldWorkflowStop } from './workflowStatus';
import { buildQueryUrlFileMap, buildQueryUrlTypeMap, runWithContext } from '../utils/context'; import { runWithContext } from '../utils/context';
import { createClientAbortTracker } from './utils/clientAbort'; import { createClientAbortTracker } from './utils/clientAbort';
import type { IncomingMessage } from 'node:http'; import type { IncomingMessage } from 'node:http';
import { getNodeResponseChildResponseCount } from '../../chat/nodeResponseStorage'; import { getNodeResponseChildResponseCount } from '../../chat/nodeResponseStorage';
...@@ -77,6 +75,7 @@ import { ...@@ -77,6 +75,7 @@ import {
import { getWorkflowNodeRunParams } from './utils/runtime'; import { getWorkflowNodeRunParams } from './utils/runtime';
import type { AgentSandboxPrepareAction } from './ai/agent/sub/sandbox'; import type { AgentSandboxPrepareAction } from './ai/agent/sub/sandbox';
import { getWorkflowSource } from './utils/source'; import { getWorkflowSource } from './utils/source';
import { prepareWorkflowFileContext } from '../utils/fileContext';
const logger = getLogger(LogCategories.MODULE.WORKFLOW.DISPATCH); const logger = getLogger(LogCategories.MODULE.WORKFLOW.DISPATCH);
...@@ -97,6 +96,10 @@ type Props = Omit< ...@@ -97,6 +96,10 @@ type Props = Omit<
defaultSkipNodeQueue?: WorkflowDebugResponse['skipNodeQueue']; defaultSkipNodeQueue?: WorkflowDebugResponse['skipNodeQueue'];
nodeResponseWriteConfig: WorkflowNodeResponseWriteConfig; nodeResponseWriteConfig: WorkflowNodeResponseWriteConfig;
agentSandboxPrepareActions?: AgentSandboxPrepareAction[]; agentSandboxPrepareActions?: AgentSandboxPrepareAction[];
/** 已在持久化前计算完成的用户级 Workflow 文件数量上限。 */
maxFileAmount: number;
/** 已按团队配置优先、系统配置兜底计算完成的单文件读取上限。 */
maxBytesPerFile: number;
}; };
type NodeResponseType = DispatchNodeResultType<{ type NodeResponseType = DispatchNodeResultType<{
[key: string]: any; [key: string]: any;
...@@ -134,7 +137,6 @@ export async function dispatchWorkFlow({ ...@@ -134,7 +137,6 @@ export async function dispatchWorkFlow({
apiVersion apiVersion
} = data; } = data;
const responseChatItemId = data.responseChatItemId; const responseChatItemId = data.responseChatItemId;
if (stream && res && !isWorkflowSseResponseInitialized(res)) { if (stream && res && !isWorkflowSseResponseInitialized(res)) {
// HTTP SSE 响应必须由调用入口提前初始化,dispatch 只执行 workflow,不隐式管理响应协议。 // HTTP SSE 响应必须由调用入口提前初始化,dispatch 只执行 workflow,不隐式管理响应协议。
return Promise.reject( return Promise.reject(
...@@ -143,26 +145,38 @@ export async function dispatchWorkFlow({ ...@@ -143,26 +145,38 @@ export async function dispatchWorkFlow({
} }
const chatSource = getWorkflowSource(runningAppInfo); const chatSource = getWorkflowSource(runningAppInfo);
// Check url valid
const invalidInput = query.some((item) => {
if ('file' in item && item.file?.url) {
if (!validateFileUrlDomain(item.file.url)) {
return true;
}
}
});
if (invalidInput) {
logger.info('Workflow run blocked due to invalid file url');
return Promise.reject(new UserError('Invalid file url'));
}
/* Init function */ /* Init function */
// Check point // Check point
await checkTeamAIPoints(runningUserInfo.teamId); await checkTeamAIPoints(runningUserInfo.teamId);
const getPreviewUrl = createChatFilePreviewUrlGetter(); const {
fileContext,
fileRegistrar,
getPreviewUrl,
query: runtimeQuery,
histories: preparedHistories
} = await prepareWorkflowFileContext({
query,
histories,
scope: {
sourceType: runningAppInfo.sourceType,
sourceId: runningAppInfo.sourceId,
uid: data.uid,
chatId
},
maxFileAmount: data.maxFileAmount,
maxBytesPerFile: data.maxBytesPerFile
});
const getHistoryPreviewUrl = async (key: string) => {
try {
return await getPreviewUrl(key);
} catch (error) {
if (!(error instanceof UserError)) throw error;
logger.warn('Skip unavailable workflow history file', { key, message: error.message });
}
};
const [{ timezone, externalProvider }, newUsageId] = await Promise.all([ const [{ timezone, externalProvider }, newUsageId, runtimeHistories] = await Promise.all([
getUserChatInfo(runningUserInfo.tmbId), getUserChatInfo(runningUserInfo.tmbId),
(() => { (() => {
if (lastInteractive?.usageId) { if (lastInteractive?.usageId) {
...@@ -186,13 +200,8 @@ export async function dispatchWorkFlow({ ...@@ -186,13 +200,8 @@ export async function dispatchWorkFlow({
} }
return usageId; return usageId;
})(), })(),
// Add preview url to chat items // 复用 Context 的鉴权和请求级签名缓存,刷新 history 里其它服务端文件引用。
addPreviewUrlToChatItems(histories, 'chatFlow'), addPreviewUrlToChatItems(preparedHistories, 'chatFlow', getHistoryPreviewUrl),
// Add preview url to query
...query.map(async (item) => {
if (!item.file?.key) return;
item.file.url = await getPreviewUrl(item.file.key);
}),
// Remove stopping sign // Remove stopping sign
delAgentRuntimeStopSign({ delAgentRuntimeStopSign({
...chatSource, ...chatSource,
...@@ -203,20 +212,25 @@ export async function dispatchWorkFlow({ ...@@ -203,20 +212,25 @@ export async function dispatchWorkFlow({
const clientAbortTracker = const clientAbortTracker =
apiVersion === 'v1' ? createClientAbortTracker({ req: data.req, res }) : undefined; apiVersion === 'v1' ? createClientAbortTracker({ req: data.req, res }) : undefined;
// 私有文件已恢复为无后缀短链;进入节点调度前保留类型和原始文件名等媒体元数据。
const queryUrlTypeMap = buildQueryUrlTypeMap(query);
const queryUrlFileMap = buildQueryUrlFileMap(query);
const variableState = await WorkflowVariableState.create({ const variableState = await WorkflowVariableState.create({
timezone, timezone,
runningAppInfo, runningAppInfo,
uid: data.uid, uid: data.uid,
chatId, chatId,
responseChatItemId, responseChatItemId,
histories, histories: runtimeHistories,
variablesConfig: data.chatConfig?.variables, variablesConfig: data.chatConfig?.variables,
inputVariables: data.variables, inputVariables: data.variables,
externalVariables: externalProvider.externalWorkflowVariables externalVariables: externalProvider.externalWorkflowVariables,
maxFileAmount: data.maxFileAmount,
resolveInputFile: async (file) => {
const ref = await fileRegistrar.registerInputFile({
file,
source: 'variable'
});
if (!ref) throw new UserError('Invalid workflow variable file');
return ref.modelUrl;
}
}); });
// Stop sign(没有 apiVersion,说明不会有暂停) // Stop sign(没有 apiVersion,说明不会有暂停)
...@@ -262,9 +276,9 @@ export async function dispatchWorkFlow({ ...@@ -262,9 +276,9 @@ export async function dispatchWorkFlow({
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
runWithContext( runWithContext(
{ {
queryUrlTypeMap, mcpClientMemory: {},
queryUrlFileMap, fileContext,
mcpClientMemory: {} fileRegistrar
}, },
(ctx) => { (ctx) => {
runWorkflow({ runWorkflow({
...@@ -272,8 +286,8 @@ export async function dispatchWorkFlow({ ...@@ -272,8 +286,8 @@ export async function dispatchWorkFlow({
responseChatItemId, responseChatItemId,
nodeResponseSink, nodeResponseSink,
checkIsStopping, checkIsStopping,
query, query: runtimeQuery,
histories, histories: runtimeHistories,
timezone, timezone,
externalProvider, externalProvider,
variableState, variableState,
......
...@@ -8,8 +8,16 @@ import { FlowNodeInputTypeEnum } from '@fastgpt/global/core/workflow/node/consta ...@@ -8,8 +8,16 @@ import { FlowNodeInputTypeEnum } from '@fastgpt/global/core/workflow/node/consta
import { anyValueDecrypt } from '../../../../common/secret/utils'; import { anyValueDecrypt } from '../../../../common/secret/utils';
import { getLogger, LogCategories } from '../../../../common/logger'; import { getLogger, LogCategories } from '../../../../common/logger';
import { createChatFilePreviewUrlGetter } from '../../../../common/s3/sources/chat'; import { createChatFilePreviewUrlGetter } from '../../../../common/s3/sources/chat';
import {
normalizeChatFileStoreValues,
type ChatFileValueInput
} from '../../../chat/fileStoreValue';
import { getWorkflowFileContext, getWorkflowFileRegistrar } from '../../utils/context';
import type { WorkflowFileRegistrar } from '../../utils/fileContext';
import { getModuleFileAmountLimit } from '@fastgpt/global/core/workflow/fileLimit';
const logger = getLogger(LogCategories.MODULE.WORKFLOW.INTERACTIVE); const logger = getLogger(LogCategories.MODULE.WORKFLOW.INTERACTIVE);
const DEFAULT_FORM_FILE_INPUT_MAX_FILES = 5;
type Props = ModuleDispatchProps<{ type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.description]: string; [NodeInputKeyEnum.description]: string;
...@@ -24,14 +32,38 @@ type FormInputResponse = DispatchNodeResultType<{ ...@@ -24,14 +32,38 @@ type FormInputResponse = DispatchNodeResultType<{
const isFileSelectObject = (value: unknown): value is { key?: unknown; url?: unknown } => const isFileSelectObject = (value: unknown): value is { key?: unknown; url?: unknown } =>
!!value && typeof value === 'object' && !Array.isArray(value); !!value && typeof value === 'object' && !Array.isArray(value);
const formatFileSelectRuntimeValue = async ( const formatFileSelectRuntimeValue = async ({
value: unknown, value,
getPreviewUrl: (key: string) => Promise<string> maxFiles,
) => { getPreviewUrl,
fileRegistrar
}: {
value: unknown;
maxFiles?: number;
getPreviewUrl: (key: string) => Promise<string>;
fileRegistrar?: WorkflowFileRegistrar;
}) => {
if (!Array.isArray(value)) return value; if (!Array.isArray(value)) return value;
const urls = await Promise.all( const urls = await Promise.all(
value.map(async (file) => { value.slice(0, Math.max(maxFiles ?? DEFAULT_FORM_FILE_INPUT_MAX_FILES, 0)).map(async (file) => {
if (fileRegistrar) {
const storeValue = normalizeChatFileStoreValues([
typeof file === 'string'
? { url: file }
: isFileSelectObject(file)
? (file as ChatFileValueInput)
: {}
])[0];
if (!storeValue) return;
const ref = await fileRegistrar.registerInputFile({
file: storeValue,
source: 'interactive'
});
return ref?.modelUrl;
}
if (typeof file === 'string') return file; if (typeof file === 'string') return file;
if (!isFileSelectObject(file)) return; if (!isFileSelectObject(file)) return;
...@@ -84,6 +116,9 @@ export const dispatchFormInput = async (props: Props): Promise<FormInputResponse ...@@ -84,6 +116,9 @@ export const dispatchFormInput = async (props: Props): Promise<FormInputResponse
})(); })();
const getPreviewUrl = createChatFilePreviewUrlGetter({ expiredHours: 1 }); const getPreviewUrl = createChatFilePreviewUrlGetter({ expiredHours: 1 });
const userMaxFileAmount =
getWorkflowFileContext()?.limits.maxFileAmount ?? DEFAULT_FORM_FILE_INPUT_MAX_FILES;
const fileRegistrar = getWorkflowFileRegistrar();
const inputConfigMap = new Map(userInputForms.map((form) => [form.key, form])); const inputConfigMap = new Map(userInputForms.map((form) => [form.key, form]));
const userInputEntries = await Promise.all( const userInputEntries = await Promise.all(
Object.entries(rawUserInputVal).map(async ([key, value]) => { Object.entries(rawUserInputVal).map(async ([key, value]) => {
...@@ -93,7 +128,19 @@ export const dispatchFormInput = async (props: Props): Promise<FormInputResponse ...@@ -93,7 +128,19 @@ export const dispatchFormInput = async (props: Props): Promise<FormInputResponse
return [key, anyValueDecrypt(value)] as const; return [key, anyValueDecrypt(value)] as const;
} }
if (inputConfig?.type === FlowNodeInputTypeEnum.fileSelect) { if (inputConfig?.type === FlowNodeInputTypeEnum.fileSelect) {
return [key, await formatFileSelectRuntimeValue(value, getPreviewUrl)] as const; return [
key,
await formatFileSelectRuntimeValue({
value,
maxFiles: getModuleFileAmountLimit({
userMaxFileAmount,
moduleMaxFileAmount: inputConfig.maxFiles,
defaultModuleMaxFileAmount: DEFAULT_FORM_FILE_INPUT_MAX_FILES
}),
getPreviewUrl,
fileRegistrar
})
] as const;
} }
return [key, value] as const; return [key, value] as const;
}) })
......
...@@ -31,9 +31,14 @@ import type { AppToolRuntimeType } from '@fastgpt/global/core/app/tool/type'; ...@@ -31,9 +31,14 @@ import type { AppToolRuntimeType } from '@fastgpt/global/core/app/tool/type';
import { anyValueDecrypt } from '../../../../common/secret/utils'; import { anyValueDecrypt } from '../../../../common/secret/utils';
import { getAppVersionById } from '../../../app/version/controller'; import { getAppVersionById } from '../../../app/version/controller';
import { parseI18nString } from '@fastgpt/global/common/i18n/utils'; import { parseI18nString } from '@fastgpt/global/common/i18n/utils';
import { WorkflowVariableState } from '../utils/variables'; import {
getWorkflowFileInputsFromValue,
getWorkflowFileVariableInputs,
WorkflowVariableState
} from '../utils/variables';
import { SystemToolRepo } from '../../../app/tool/systemTool/systemTool.repo'; import { SystemToolRepo } from '../../../app/tool/systemTool/systemTool.repo';
import { getRuntimeNodeResponseSummary } from '../utils'; import { getRuntimeNodeResponseSummary } from '../utils';
import { runWithDerivedWorkflowFileContext } from '../../utils/context';
type RunPluginProps = ModuleDispatchProps<{ type RunPluginProps = ModuleDispatchProps<{
[NodeInputKeyEnum.forbidStream]?: boolean; [NodeInputKeyEnum.forbidStream]?: boolean;
...@@ -149,17 +154,18 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi ...@@ -149,17 +154,18 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
associatedPluginId: systemTool.associatedPluginId associatedPluginId: systemTool.associatedPluginId
}; };
} }
const childWorkflowTool = workflowTool;
const outputFilterMap = const outputFilterMap =
workflowTool.nodes childWorkflowTool.nodes
.find((node) => node.flowNodeType === FlowNodeTypeEnum.pluginOutput) .find((node) => node.flowNodeType === FlowNodeTypeEnum.pluginOutput)
?.inputs.reduce<Record<string, boolean>>((acc, cur) => { ?.inputs.reduce<Record<string, boolean>>((acc, cur) => {
acc[cur.key] = cur.isToolOutput === false ? false : true; acc[cur.key] = cur.isToolOutput === false ? false : true;
return acc; return acc;
}, {}) ?? {}; }, {}) ?? {};
const runtimeNodes = storeNodes2RuntimeNodes( const runtimeNodes = storeNodes2RuntimeNodes(
workflowTool.nodes, childWorkflowTool.nodes,
getWorkflowEntryNodeIds(workflowTool.nodes) getWorkflowEntryNodeIds(childWorkflowTool.nodes)
).map((node) => { ).map((node) => {
// Update workflowTool input value // Update workflowTool input value
if (node.flowNodeType === FlowNodeTypeEnum.pluginInput) { if (node.flowNodeType === FlowNodeTypeEnum.pluginInput) {
...@@ -194,26 +200,29 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi ...@@ -194,26 +200,29 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
const { externalProvider } = await getUserChatInfo(runningAppInfo.tmbId); const { externalProvider } = await getUserChatInfo(runningAppInfo.tmbId);
const childRunningAppInfo = { const childRunningAppInfo = {
sourceType: ChatSourceTypeEnum.app, sourceType: ChatSourceTypeEnum.app,
sourceId: String(workflowTool.id), sourceId: String(childWorkflowTool.id),
name: workflowTool.name, name: childWorkflowTool.name,
teamId: workflowTool.teamId || runningAppInfo.teamId, teamId: childWorkflowTool.teamId || runningAppInfo.teamId,
tmbId: workflowTool.tmbId || runningAppInfo.tmbId, tmbId: childWorkflowTool.tmbId || runningAppInfo.tmbId,
isChildApp: true isChildApp: true
}; };
const childVariableState = await WorkflowVariableState.create({ const childFileInputs = [
timezone: props.timezone, ...files,
runningAppInfo: childRunningAppInfo, ...getWorkflowFileVariableInputs({
uid: props.uid, variablesConfig: childWorkflowTool.chatConfig?.variables,
chatId: props.chatId, inputVariables: {}
responseChatItemId: props.responseChatItemId, }),
histories: props.histories, ...childWorkflowTool.nodes.flatMap((node) =>
variablesConfig: workflowTool.chatConfig?.variables ?? [], node.flowNodeType === FlowNodeTypeEnum.pluginInput
inputVariables: {}, ? node.inputs.flatMap((input) =>
externalVariables: externalProvider?.externalWorkflowVariables, input.renderTypeList.includes(FlowNodeInputTypeEnum.fileSelect)
sourceVariableState: props.variableState ? getWorkflowFileInputsFromValue(data[input.key] ?? input.value, input.maxFiles)
}); : []
const runtimeVariables = childVariableState.toRuntimeRecord(); )
const shouldStoreChildNodeResponses = !workflowTool.associatedPluginId; : []
)
];
const shouldStoreChildNodeResponses = !childWorkflowTool.associatedPluginId;
const { const {
flowUsages, flowUsages,
assistantResponses, assistantResponses,
...@@ -221,35 +230,72 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi ...@@ -221,35 +230,72 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
system_memories, system_memories,
runtimeNodeResponseSummary, runtimeNodeResponseSummary,
[DispatchNodeResponseKeyEnum.customFeedbacks]: customFeedbacks [DispatchNodeResponseKeyEnum.customFeedbacks]: customFeedbacks
} = await runWorkflow({ } = await runWithDerivedWorkflowFileContext({
...props, histories: props.histories,
// 系统级 workflow tool 只保留工具节点自身的响应,不展开保存其内部 workflow 详情。 files: childFileInputs,
...(shouldStoreChildNodeResponses ? {} : { nodeResponseSink: undefined }), fn: async ({ resolveInputFile, histories: childHistories, filterFiles }) => {
// Rewrite stream mode const childRuntimeNodes = runtimeNodes.map((node) =>
...(system_forbid_stream node.flowNodeType === FlowNodeTypeEnum.pluginInput
? { ? {
stream: false, ...node,
workflowStreamResponse: undefined inputs: node.inputs.map((input) => ({
} ...input,
: {}), value:
runningAppInfo: { input.renderTypeList.includes(FlowNodeInputTypeEnum.fileSelect) &&
sourceType: ChatSourceTypeEnum.app, Array.isArray(input.value)
sourceId: String(workflowTool.id), ? filterFiles(input.value)
name: workflowTool.name, : input.value
// 如果系统插件有 teamId 和 tmbId,则使用系统插件的 teamId 和 tmbId(管理员指定了插件作为系统插件) }))
teamId: workflowTool.teamId || runningAppInfo.teamId, }
tmbId: workflowTool.tmbId || runningAppInfo.tmbId, : node
isChildApp: true );
}, const childVariableState = await WorkflowVariableState.create({
variableState: childVariableState, timezone: props.timezone,
query: serverGetWorkflowToolRunUserQuery({ runningAppInfo: childRunningAppInfo,
pluginInputs: getWorkflowToolInputsFromStoreNodes(workflowTool.nodes), uid: props.uid,
variables: runtimeVariables, chatId: props.chatId,
files responseChatItemId: props.responseChatItemId,
}).value, histories: childHistories,
chatConfig: workflowTool.chatConfig ?? {}, variablesConfig: childWorkflowTool.chatConfig?.variables ?? [],
runtimeNodes, inputVariables: {},
runtimeEdges: storeEdges2RuntimeEdges(workflowTool.edges) externalVariables: externalProvider?.externalWorkflowVariables,
sourceVariableState: props.variableState,
resolveInputFile
});
const runtimeVariables = childVariableState.toRuntimeRecord();
return runWorkflow({
...props,
// 系统级 workflow tool 只保留工具节点自身的响应,不展开保存其内部 workflow 详情。
...(shouldStoreChildNodeResponses ? {} : { nodeResponseSink: undefined }),
// Rewrite stream mode
...(system_forbid_stream
? {
stream: false,
workflowStreamResponse: undefined
}
: {}),
runningAppInfo: {
sourceType: ChatSourceTypeEnum.app,
sourceId: String(childWorkflowTool.id),
name: childWorkflowTool.name,
// 如果系统插件有 teamId 和 tmbId,则使用系统插件的 teamId 和 tmbId(管理员指定了插件作为系统插件)
teamId: childWorkflowTool.teamId || runningAppInfo.teamId,
tmbId: childWorkflowTool.tmbId || runningAppInfo.tmbId,
isChildApp: true
},
variableState: childVariableState,
histories: childHistories,
query: serverGetWorkflowToolRunUserQuery({
pluginInputs: getWorkflowToolInputsFromStoreNodes(childWorkflowTool.nodes),
variables: runtimeVariables,
files: filterFiles(files)
}).value,
chatConfig: childWorkflowTool.chatConfig ?? {},
runtimeNodes: childRuntimeNodes,
runtimeEdges: storeEdges2RuntimeEdges(childWorkflowTool.edges)
});
}
}); });
const runtimeSummary = getRuntimeNodeResponseSummary({ const runtimeSummary = getRuntimeNodeResponseSummary({
runtimeNodeResponseSummary runtimeNodeResponseSummary
......
import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt'; import { chatValue2RuntimePrompt } from '@fastgpt/global/core/chat/adapt';
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants'; import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { FlowNodeInputTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime'; import type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime';
import { createChatFilePreviewUrlGetter } from '../../../../common/s3/sources/chat'; import { UserError } from '@fastgpt/global/common/error/utils';
import {
normalizeChatFileStoreValue,
normalizeChatFileStoreValues
} from '../../../chat/fileStoreValue';
import { getWorkflowFileContext, getWorkflowFileRegistrar } from '../../utils/context';
import { getModuleFileAmountLimit } from '@fastgpt/global/core/workflow/fileLimit';
const DEFAULT_PLUGIN_FILE_INPUT_MAX_FILES = 5;
export type PluginInputProps = ModuleDispatchProps<{ export type PluginInputProps = ModuleDispatchProps<{
[key: string]: any; [key: string]: any;
...@@ -13,46 +21,64 @@ export type PluginInputResponse = DispatchNodeResultType<{ ...@@ -13,46 +21,64 @@ export type PluginInputResponse = DispatchNodeResultType<{
[key: string]: any; [key: string]: any;
}>; }>;
type PluginFileItem = { /** 将 Plugin Input 的 fileSelect 值登记到当前 Workflow 文件上下文并输出模型 URL。 */
type: ChatFileTypeEnum;
key?: string;
url?: string;
};
const isPluginFileItem = (item: unknown): item is PluginFileItem =>
!!item &&
typeof item === 'object' &&
((item as PluginFileItem).type === ChatFileTypeEnum.file ||
(item as PluginFileItem).type === ChatFileTypeEnum.image);
export const dispatchPluginInput = async ( export const dispatchPluginInput = async (
props: PluginInputProps props: PluginInputProps
): Promise<PluginInputResponse> => { ): Promise<PluginInputResponse> => {
const { params, query } = props; const { params, query, node } = props;
const { files } = chatValue2RuntimePrompt(query); const { files } = chatValue2RuntimePrompt(query);
const getPreviewUrl = createChatFilePreviewUrlGetter({ expiredHours: 1 }); const fileContext = getWorkflowFileContext();
const fileRegistrar = getWorkflowFileRegistrar();
const userMaxFileAmount =
fileContext?.limits.maxFileAmount ?? DEFAULT_PLUGIN_FILE_INPUT_MAX_FILES;
const fileInputMaxFiles = new Map(
node.inputs
.filter((input) => input.renderTypeList.includes(FlowNodeInputTypeEnum.fileSelect))
.map(
(input) =>
[
input.key,
getModuleFileAmountLimit({
userMaxFileAmount,
moduleMaxFileAmount: input.maxFiles,
defaultModuleMaxFileAmount: DEFAULT_PLUGIN_FILE_INPUT_MAX_FILES
})
] as const
)
);
/* /*
对 params 中文件类型数据进行处理 对 params 中文件类型数据进行处理
* 插件单独运行时,这里会是一个特殊的数组 * 插件单独运行时,这里会是文件对象数组
* 插件调用的话,这个参数是一个 string[] 不会进行处理 * 插件调用时,这个参数可能已经被转换成 string[]
* 硬性要求:API 单独调用插件时,要避免这种特殊类型冲突
TODO: 需要 filter max files
*/ */
await Promise.all( await Promise.all(
Object.keys(params).map(async (key) => { Object.keys(params).map(async (key) => {
const val = params[key]; const val = params[key];
if (Array.isArray(val) && val.every(isPluginFileItem)) { const maxFiles = fileInputMaxFiles.get(key);
// 为文件对象重新签发 URL(如果有 key 但没有 url) if (maxFiles !== undefined && Array.isArray(val)) {
await Promise.all( const fileUrls = await Promise.all(
val.map(async (fileItem) => { val.slice(0, maxFiles).map(async (fileItem) => {
if (fileItem.key && !fileItem.url) { const storeValue =
fileItem.url = await getPreviewUrl(fileItem.key); typeof fileItem === 'string'
} ? normalizeChatFileStoreValue({ url: fileItem })
: normalizeChatFileStoreValues([fileItem])[0];
if (!storeValue) throw new UserError('Invalid workflow plugin file');
const existingRef = fileContext?.resolveInputFile(storeValue);
if (existingRef) return existingRef.modelUrl;
if (!fileRegistrar) throw new UserError('Workflow file context is unavailable');
const ref = await fileRegistrar.registerInputFile({
file: storeValue,
source: 'plugin'
});
return ref?.modelUrl;
}) })
); );
params[key] = val.map((item) => item.url); params[key] = fileUrls.filter(
(url): url is string => typeof url === 'string' && Boolean(url)
);
} }
}) })
); );
......
...@@ -8,13 +8,19 @@ import { type ChatItemMiniType } from '@fastgpt/global/core/chat/type'; ...@@ -8,13 +8,19 @@ import { type ChatItemMiniType } from '@fastgpt/global/core/chat/type';
import { getNodeErrResponse } from '../utils'; import { getNodeErrResponse } from '../utils';
import { parseFileContentFromUrls } from '../../../chat/fileContext'; import { parseFileContentFromUrls } from '../../../chat/fileContext';
import { sliceStrStartEnd } from '@fastgpt/global/common/string/tools'; import { sliceStrStartEnd } from '@fastgpt/global/common/string/tools';
import { getWorkflowFileContext, getWorkflowFileMaxAmount } from '../../utils/context';
type Props = ModuleDispatchProps<{ type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.fileUrlList]: string[]; [NodeInputKeyEnum.fileUrlList]: string[];
}>; }>;
type Response = DispatchNodeResultType<{ type Response = DispatchNodeResultType<{
[NodeOutputKeyEnum.text]: string; [NodeOutputKeyEnum.text]: string;
[NodeOutputKeyEnum.rawResponse]: { filename: string; url: string; text: string }[]; [NodeOutputKeyEnum.rawResponse]: {
filename: string;
url: string;
text: string;
error?: string;
}[];
}>; }>;
/** /**
...@@ -33,7 +39,6 @@ export const buildReadFilesOutputText = ( ...@@ -33,7 +39,6 @@ export const buildReadFilesOutputText = (
export const dispatchReadFiles = async (props: Props): Promise<Response> => { export const dispatchReadFiles = async (props: Props): Promise<Response> => {
const { const {
requestOrigin,
runningUserInfo: { teamId, tmbId }, runningUserInfo: { teamId, tmbId },
histories, histories,
chatConfig, chatConfig,
...@@ -41,7 +46,7 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => { ...@@ -41,7 +46,7 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => {
params: { fileUrlList = [] }, params: { fileUrlList = [] },
usageId usageId
} = props; } = props;
const maxFiles = chatConfig?.fileSelectConfig?.maxFiles || 20; const maxFileAmount = getWorkflowFileMaxAmount();
const customPdfParse = chatConfig?.fileSelectConfig?.customPdfParse || false; const customPdfParse = chatConfig?.fileSelectConfig?.customPdfParse || false;
// Get files from histories // Get files from histories
...@@ -51,12 +56,12 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => { ...@@ -51,12 +56,12 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => {
const readFilesResult = await parseFileContentFromUrls({ const readFilesResult = await parseFileContentFromUrls({
// Concat fileUrlList and filesFromHistories; remove not supported files // Concat fileUrlList and filesFromHistories; remove not supported files
urls: [...fileUrlList, ...filesFromHistories], urls: [...fileUrlList, ...filesFromHistories],
requestOrigin, maxFiles: maxFileAmount,
maxFiles,
teamId, teamId,
tmbId, tmbId,
customPdfParse, customPdfParse,
usageId usageId,
fileContext: getWorkflowFileContext()
}); });
const files = readFilesResult.map((item, index) => ({ const files = readFilesResult.map((item, index) => ({
id: `${index}`, id: `${index}`,
...@@ -76,7 +81,8 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => { ...@@ -76,7 +81,8 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => {
[NodeOutputKeyEnum.rawResponse]: readFilesResult.map((item) => ({ [NodeOutputKeyEnum.rawResponse]: readFilesResult.map((item) => ({
filename: item.name, filename: item.name,
url: item.url, url: item.url,
text: item.content text: item.success ? item.content : '',
...(item.success ? {} : { error: item.content })
})) }))
}, },
[DispatchNodeResponseKeyEnum.nodeResponse]: { [DispatchNodeResponseKeyEnum.nodeResponse]: {
......
...@@ -4,6 +4,7 @@ import { VariableInputEnum } from '@fastgpt/global/core/workflow/constants'; ...@@ -4,6 +4,7 @@ import { VariableInputEnum } from '@fastgpt/global/core/workflow/constants';
import type { ChatDispatchProps, WorkflowVariableStateLike } from '../../types/runtime'; import type { ChatDispatchProps, WorkflowVariableStateLike } from '../../types/runtime';
import { valueTypeFormat } from '@fastgpt/global/core/workflow/runtime/utils'; import { valueTypeFormat } from '@fastgpt/global/core/workflow/runtime/utils';
import { getSystemTime } from '@fastgpt/global/common/time/timezone'; import { getSystemTime } from '@fastgpt/global/common/time/timezone';
import { UserError } from '@fastgpt/global/common/error/utils';
import { encryptSecret } from '../../../../common/secret/aes256gcm'; import { encryptSecret } from '../../../../common/secret/aes256gcm';
import { anyValueDecrypt } from '../../../../common/secret/utils'; import { anyValueDecrypt } from '../../../../common/secret/utils';
import { createChatFilePreviewUrlGetter } from '../../../../common/s3/sources/chat'; import { createChatFilePreviewUrlGetter } from '../../../../common/s3/sources/chat';
...@@ -13,6 +14,15 @@ import { ...@@ -13,6 +14,15 @@ import {
assertChatFileRuntimeValue, assertChatFileRuntimeValue,
normalizeChatFileStoreValue normalizeChatFileStoreValue
} from '../../../chat/fileStoreValue'; } from '../../../chat/fileStoreValue';
import { getWorkflowFileContext } from '../../utils/context';
import { isAbsoluteHttpUrl } from '../../utils/fileContext';
import { getModuleFileAmountLimit } from '@fastgpt/global/core/workflow/fileLimit';
const DEFAULT_VARIABLE_FILE_INPUT_MAX_FILES = 5;
type WorkflowVariableRuntimeConfig = VariableItemType & {
maxFiles: number;
};
/** /**
* 工作流全局变量状态管理器。 * 工作流全局变量状态管理器。
...@@ -33,7 +43,7 @@ import { ...@@ -33,7 +43,7 @@ import {
*/ */
export type WorkflowVariableStateItem = { export type WorkflowVariableStateItem = {
key: string; key: string;
config?: VariableItemType; config?: WorkflowVariableRuntimeConfig;
storeValue: unknown; storeValue: unknown;
runtimeValue: unknown; runtimeValue: unknown;
runtimeOnly?: boolean; runtimeOnly?: boolean;
...@@ -57,6 +67,10 @@ export type WorkflowVariableStateCreateProps = { ...@@ -57,6 +67,10 @@ export type WorkflowVariableStateCreateProps = {
inputVariables?: Record<string, unknown>; inputVariables?: Record<string, unknown>;
// 源变量状态,用于复制变量状态 // 源变量状态,用于复制变量状态
sourceVariableState?: WorkflowVariableStateLike; sourceVariableState?: WorkflowVariableStateLike;
// 根 Workflow 创建状态时尚未进入 AsyncLocalStorage,需要显式传入请求级文件数量限制。
maxFileAmount?: number;
// 只在入口初始化全局文件变量时登记可信文件,节点运行时 set 不得扩展文件 Context。
resolveInputFile?: (file: ChatFileStoreValue) => Promise<string | undefined>;
}; };
/** 根据变量配置从入参中取值,兼容 API label 入参与前端 key 入参。 */ /** 根据变量配置从入参中取值,兼容 API label 入参与前端 key 入参。 */
...@@ -72,18 +86,60 @@ const getVariableInputValue = ({ ...@@ -72,18 +86,60 @@ const getVariableInputValue = ({
return item.defaultValue; return item.defaultValue;
}; };
/** 从已知文件字段的运行值中提取文件输入。 */
export const getWorkflowFileInputsFromValue = (
value: unknown,
moduleMaxFileAmount?: number,
userMaxFileAmount = getWorkflowFileContext()?.limits.maxFileAmount ??
DEFAULT_VARIABLE_FILE_INPUT_MAX_FILES
) =>
Array.isArray(value)
? assertChatFileRuntimeValue(value as ChatFileRuntimeValueItem[]).slice(
0,
getModuleFileAmountLimit({
userMaxFileAmount,
moduleMaxFileAmount,
defaultModuleMaxFileAmount: DEFAULT_VARIABLE_FILE_INPUT_MAX_FILES
})
)
: [];
/** 收集 Child 全局文件变量的实际输入,只用于派生请求级文件 Context。 */
export const getWorkflowFileVariableInputs = ({
variablesConfig = [],
inputVariables = {},
maxFileAmount = getWorkflowFileContext()?.limits.maxFileAmount
}: Pick<
WorkflowVariableStateCreateProps,
'variablesConfig' | 'inputVariables' | 'maxFileAmount'
>) =>
variablesConfig.flatMap((item) => {
if (item.type !== VariableInputEnum.file) return [];
const value = getVariableInputValue({ variables: inputVariables, item });
return getWorkflowFileInputsFromValue(value, item.maxFiles, maxFileAmount);
});
/** 将文件存储值转换为运行时 URL,并记录 URL 到 store metadata 的映射。 */ /** 将文件存储值转换为运行时 URL,并记录 URL 到 store metadata 的映射。 */
const fileStoreValuesToRuntimeUrls = async ({ const fileStoreValuesToRuntimeUrls = async ({
files, files,
fileMetaMap, fileMetaMap,
resolveInputFile,
getPreviewUrl = createChatFilePreviewUrlGetter() getPreviewUrl = createChatFilePreviewUrlGetter()
}: { }: {
files: ChatFileStoreValue[]; files: ChatFileStoreValue[];
fileMetaMap: Map<string, ChatFileStoreValue>; fileMetaMap: Map<string, ChatFileStoreValue>;
resolveInputFile?: (file: ChatFileStoreValue) => Promise<string | undefined>;
getPreviewUrl?: (key: string) => Promise<string>; getPreviewUrl?: (key: string) => Promise<string>;
}) => { }) => {
const urls = await Promise.all( const urls = await Promise.all(
files.map(async (file) => { files.map(async (file) => {
if (resolveInputFile) {
const url = await resolveInputFile(file);
if (!url) return;
fileMetaMap.set(url, file);
return url;
}
if ('key' in file) { if ('key' in file) {
const url = await getPreviewUrl(file.key); const url = await getPreviewUrl(file.key);
fileMetaMap.set(url, file); fileMetaMap.set(url, file);
...@@ -121,13 +177,24 @@ export class WorkflowVariableState implements WorkflowVariableStateLike { ...@@ -121,13 +177,24 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
inputVariables = {}, inputVariables = {},
externalVariables = {}, externalVariables = {},
runtimeOnlyVariables = {}, runtimeOnlyVariables = {},
sourceVariableState sourceVariableState,
maxFileAmount = getWorkflowFileContext()?.limits.maxFileAmount ??
DEFAULT_VARIABLE_FILE_INPUT_MAX_FILES,
resolveInputFile
}: WorkflowVariableStateCreateProps) { }: WorkflowVariableStateCreateProps) {
const state = new WorkflowVariableState(new Map(), new Map(), sourceVariableState); const state = new WorkflowVariableState(new Map(), new Map(), sourceVariableState);
for (const item of variablesConfig) { for (const item of variablesConfig) {
const value = getVariableInputValue({ variables: inputVariables, item }); const config: WorkflowVariableRuntimeConfig = {
await state.initConfiguredVariable(item, value); ...item,
maxFiles: getModuleFileAmountLimit({
userMaxFileAmount: maxFileAmount,
moduleMaxFileAmount: item.maxFiles,
defaultModuleMaxFileAmount: DEFAULT_VARIABLE_FILE_INPUT_MAX_FILES
})
};
const value = getVariableInputValue({ variables: inputVariables, item: config });
await state.initConfiguredVariable(config, value, resolveInputFile);
} }
state.setRuntimeOnlyVariables(externalVariables); state.setRuntimeOnlyVariables(externalVariables);
...@@ -177,12 +244,25 @@ export class WorkflowVariableState implements WorkflowVariableStateLike { ...@@ -177,12 +244,25 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
} }
if (config?.type === VariableInputEnum.file) { if (config?.type === VariableInputEnum.file) {
const val = value as ChatFileRuntimeValue; if (!Array.isArray(value)) throw new UserError('File variable value must be an array');
const storeValue = this.runtimeFileValueToStoreValue(assertChatFileRuntimeValue(val)); const files = value.slice(0, config.maxFiles).map((url) => {
const runtimeValue = await fileStoreValuesToRuntimeUrls({ if (!isAbsoluteHttpUrl(url)) {
files: storeValue, throw new UserError('File variable updates only accept absolute HTTP(S) URLs');
fileMetaMap: this.fileMetaMap }
const storeValue =
this.getFileStoreValueByRuntimeUrl(url) ?? normalizeChatFileStoreValue({ url });
if (!storeValue) {
throw new UserError('Invalid external file URL');
}
return { runtimeUrl: url, storeValue };
});
files.forEach(({ runtimeUrl, storeValue }) => {
this.fileMetaMap.set(runtimeUrl, storeValue);
}); });
const storeValue = files.map((file) => file.storeValue);
const runtimeValue = files.map((file) => file.runtimeUrl);
this.state.set(key, { this.state.set(key, {
key, key,
config, config,
...@@ -251,16 +331,24 @@ export class WorkflowVariableState implements WorkflowVariableStateLike { ...@@ -251,16 +331,24 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
} }
/** 根据变量配置初始化单个用户变量,并完成特殊类型的 store/runtime 转换。 */ /** 根据变量配置初始化单个用户变量,并完成特殊类型的 store/runtime 转换。 */
private async initConfiguredVariable(config: VariableItemType, value: unknown) { private async initConfiguredVariable(
config: WorkflowVariableRuntimeConfig,
value: unknown,
resolveInputFile?: (file: ChatFileStoreValue) => Promise<string | undefined>
) {
if (config.type === VariableInputEnum.file) { if (config.type === VariableInputEnum.file) {
const storeValue = Array.isArray(value) const storeValue = Array.isArray(value)
? this.runtimeFileValueToStoreValue( ? this.runtimeFileValueToStoreValue(
assertChatFileRuntimeValue(value as ChatFileRuntimeValueItem[]) assertChatFileRuntimeValue(value as ChatFileRuntimeValueItem[]).slice(
0,
config.maxFiles
)
) )
: []; : [];
const runtimeValue = await fileStoreValuesToRuntimeUrls({ const runtimeValue = await fileStoreValuesToRuntimeUrls({
files: storeValue, files: storeValue,
fileMetaMap: this.fileMetaMap fileMetaMap: this.fileMetaMap,
resolveInputFile
}); });
this.state.set(config.key, { this.state.set(config.key, {
key: config.key, key: config.key,
...@@ -312,7 +400,7 @@ export class WorkflowVariableState implements WorkflowVariableStateLike { ...@@ -312,7 +400,7 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
}); });
} }
/** 将 file runtime 值转回 store 值,优先复用已知 URL 对应的原始 metadata。 */ /** 初始化文件变量时恢复原始 store metadata。 */
private runtimeFileValueToStoreValue(value: ChatFileRuntimeValue): ChatFileStoreValue[] { private runtimeFileValueToStoreValue(value: ChatFileRuntimeValue): ChatFileStoreValue[] {
return value return value
.map((item) => { .map((item) => {
......
...@@ -60,6 +60,8 @@ export type ChatDispatchProps = { ...@@ -60,6 +60,8 @@ export type ChatDispatchProps = {
checkIsStopping: () => boolean; checkIsStopping: () => boolean;
lang?: localeType; lang?: localeType;
requestOrigin?: string; requestOrigin?: string;
/** Workflow 服务端读取单个输入文件的最大字节数;缺省使用系统文件大小限制。 */
maxBytesPerFile?: number;
mode: 'test' | 'chat' | 'debug'; mode: 'test' | 'chat' | 'debug';
timezone: string; timezone: string;
externalProvider: ExternalProviderType; externalProvider: ExternalProviderType;
......
import type { AppSchemaType } from '@fastgpt/global/core/app/type';
import type { UserChatItemValueItemType } from '@fastgpt/global/core/chat/type';
import {
getFileAmountLimit,
getFileSizeLimitBytes,
getModuleFileAmountLimit,
getUserFileAmountLimit
} from '@fastgpt/global/core/workflow/fileLimit';
import { getTeamPlanStatus } from '../../../support/wallet/sub/utils';
export type WorkflowFileLimits = {
maxFileAmount: number;
maxBytesPerFile: number;
};
/** 分别计算用户可用的 Workflow 文件额度与应用 query 上传额度。 */
export const getWorkflowFileAmountLimits = ({
teamMaxFileAmount,
systemMaxFileAmount,
queryMaxFileAmount
}: {
teamMaxFileAmount?: number;
systemMaxFileAmount: number;
queryMaxFileAmount?: number;
}) => {
const maxFileAmount = getUserFileAmountLimit({ teamMaxFileAmount, systemMaxFileAmount });
return {
maxFileAmount,
queryMaxFileAmount: getFileAmountLimit({
teamMaxFileAmount,
systemMaxFileAmount,
moduleMaxFileAmount: queryMaxFileAmount
})
};
};
/** 获取当前团队的 Workflow 文件数量和单文件大小上限。 */
export const getWorkflowFileLimits = async ({
teamId
}: {
teamId: string;
}): Promise<WorkflowFileLimits> => {
const planStatus = await getTeamPlanStatus({ teamId });
return {
maxFileAmount: getUserFileAmountLimit({
teamMaxFileAmount: planStatus.standard?.maxUploadFileCount,
systemMaxFileAmount: global.feConfigs.uploadFileMaxAmount
}),
maxBytesPerFile: getFileSizeLimitBytes({
teamMaxFileSize: planStatus.standard?.maxUploadFileSize,
systemMaxFileSize: global.feConfigs.uploadFileMaxSize
})
};
};
/** 按上传上限静默过滤根 query 文件,保留非文件输入及原始顺序。 */
export const filterWorkflowQueryFiles = ({
query,
maxFileAmount
}: {
query: UserChatItemValueItemType[];
maxFileAmount: number;
}): UserChatItemValueItemType[] => {
const fileLimit = Math.max(0, Math.floor(maxFileAmount));
let fileAmount = 0;
return query.filter((item) => {
if (!item.file) return true;
if (fileAmount >= fileLimit) return false;
fileAmount += 1;
return true;
});
};
/**
* 在聊天持久化和 Workflow dispatch 之前统一准备根 query 与请求级文件额度。
* 返回新数组,调用方应将同一份 query 同时用于 preChatRound 和 dispatch。
*/
export const prepareWorkflowFileQuery = async ({
teamId,
chatConfig,
query,
limits
}: {
teamId: string;
chatConfig?: AppSchemaType['chatConfig'];
query: UserChatItemValueItemType[];
limits?: WorkflowFileLimits;
}) => {
const workflowFileLimits = limits ?? (await getWorkflowFileLimits({ teamId }));
const queryMaxFileAmount = getModuleFileAmountLimit({
userMaxFileAmount: workflowFileLimits.maxFileAmount,
moduleMaxFileAmount: chatConfig?.fileSelectConfig?.maxFiles
});
return {
query: filterWorkflowQueryFiles({ query, maxFileAmount: queryMaxFileAmount }),
maxFileAmount: workflowFileLimits.maxFileAmount,
maxBytesPerFile: workflowFileLimits.maxBytesPerFile
};
};
...@@ -222,9 +222,9 @@ export const serviceEnv = createEnv({ ...@@ -222,9 +222,9 @@ export const serviceEnv = createEnv({
TRACING_OTEL_SAMPLE_RATIO: NumSchema.min(0).max(1).optional(), TRACING_OTEL_SAMPLE_RATIO: NumSchema.min(0).max(1).optional(),
// ==================== 域名与前端 ==================== // ==================== 域名与前端 ====================
FE_DOMAIN: UrlSchema.optional().meta({ FE_DOMAIN: UrlSchema.meta({
description: description:
'前端外部可访问的地址,用于自动补全文件资源路径。例如 https:fastgpt.cn,不能填 localhost。这个值可以不填,不填则发给模型的图片会是一个相对路径,而不是全路径,模型可能伪造Host。' '客户端访问 FastGPT 时使用的地址(由协议、主机和可选端口组成),用于补全文件资源路径。例如 https://fastgpt.cn;本地开发可使用 http://localhost:3000。'
}), }),
FILE_DOMAIN: UrlSchema.optional().meta({ FILE_DOMAIN: UrlSchema.optional().meta({
description: description:
......
...@@ -22,6 +22,7 @@ import { ...@@ -22,6 +22,7 @@ import {
import { preChatRound, type PreChatRoundResult } from '../../../core/chat/utils/prepare'; import { preChatRound, type PreChatRoundResult } from '../../../core/chat/utils/prepare';
import { updateChatGenerateStatus } from '../../../core/chat/chatGenerateStatus'; import { updateChatGenerateStatus } from '../../../core/chat/chatGenerateStatus';
import { dispatchWorkFlow } from '../../../core/workflow/dispatch'; import { dispatchWorkFlow } from '../../../core/workflow/dispatch';
import { prepareWorkflowFileQuery } from '../../../core/workflow/utils/fileLimits';
import { getRunningUserInfoByTmbId } from '../../../support/user/team/utils'; import { getRunningUserInfoByTmbId } from '../../../support/user/team/utils';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants'; import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { authOutLinkLimit } from './auth'; import { authOutLinkLimit } from './auth';
...@@ -229,10 +230,19 @@ export async function outlinkInvokeChat<T extends OutlinkAppType>({ ...@@ -229,10 +230,19 @@ export async function outlinkInvokeChat<T extends OutlinkAppType>({
// Merge global variables from database // Merge global variables from database
const variables = chatDetail?.variables ?? {}; const variables = chatDetail?.variables ?? {};
const {
query: workflowQuery,
maxFileAmount,
maxBytesPerFile
} = await prepareWorkflowFileQuery({
teamId: String(outLinkConfig.teamId),
chatConfig,
query
});
const userContent: UserChatItemType & { dataId?: string } = { const userContent: UserChatItemType & { dataId?: string } = {
dataId: messageId, dataId: messageId,
obj: ChatRoleEnum.Human, obj: ChatRoleEnum.Human,
value: query value: workflowQuery
}; };
const preparedRound = await preChatRound({ const preparedRound = await preChatRound({
...chatSource, ...chatSource,
...@@ -273,7 +283,9 @@ export async function outlinkInvokeChat<T extends OutlinkAppType>({ ...@@ -273,7 +283,9 @@ export async function outlinkInvokeChat<T extends OutlinkAppType>({
responseChatItemId: preparedRound.responseChatItemId, responseChatItemId: preparedRound.responseChatItemId,
variables, variables,
histories, histories,
query: query, query: workflowQuery,
maxFileAmount,
maxBytesPerFile,
chatConfig, chatConfig,
stream: enableStreaming, stream: enableStreaming,
workflowStreamResponse, workflowStreamResponse,
......
...@@ -545,33 +545,4 @@ describe('axios.ts', () => { ...@@ -545,33 +545,4 @@ describe('axios.ts', () => {
expect(adapter).not.toHaveBeenCalled(); expect(adapter).not.toHaveBeenCalled();
}); });
}); });
describe('pickOutboundAxios', () => {
it.each([
'http://example.com',
'https://example.com/path',
'http://169.254.169.254/latest/meta-data/',
'//attacker.example/probe' // protocol-relative 也按绝对处理
])('绝对 URL %j 返回 safe axios 实例', async (url) => {
const { axios, pickOutboundAxios } = await import('../../../common/api/axios');
expect(pickOutboundAxios(url)).toBe(axios);
});
it.each(['/api/foo', 'api/foo', '/support/outLink/feishu/abc'])(
'相对路径 %j 返回内部 axios(baseURL 固定到本机)',
async (url) => {
const { axios, pickOutboundAxios } = await import('../../../common/api/axios');
const client = pickOutboundAxios(url);
expect(client).not.toBe(axios);
expect(client.defaults.baseURL).toMatch(/^http:\/\//);
}
);
it('多次调用同一类型的 URL,内部 client 应被复用(避免每次新建实例)', async () => {
const { pickOutboundAxios } = await import('../../../common/api/axios');
const a = pickOutboundAxios('/api/a');
const b = pickOutboundAxios('/api/b');
expect(a).toBe(b);
});
});
}); });
...@@ -385,12 +385,12 @@ describe('getImageBase64', () => { ...@@ -385,12 +385,12 @@ describe('getImageBase64', () => {
describe('addEndpointToImageUrl', () => { describe('addEndpointToImageUrl', () => {
beforeEach(() => { beforeEach(() => {
vi.stubEnv('FE_DOMAIN', undefined); vi.stubEnv('FE_DOMAIN', 'https://example.com');
vi.stubEnv('NEXT_PUBLIC_BASE_URL', undefined); vi.stubEnv('NEXT_PUBLIC_BASE_URL', undefined);
}); });
afterEach(() => { afterEach(() => {
vi.stubEnv('FE_DOMAIN', undefined); vi.stubEnv('FE_DOMAIN', 'https://example.com');
vi.stubEnv('NEXT_PUBLIC_BASE_URL', undefined); vi.stubEnv('NEXT_PUBLIC_BASE_URL', undefined);
}); });
...@@ -399,12 +399,6 @@ describe('addEndpointToImageUrl', () => { ...@@ -399,12 +399,6 @@ describe('addEndpointToImageUrl', () => {
return addEndpointToImageUrl; return addEndpointToImageUrl;
}; };
it('should return text unchanged when FE_DOMAIN is not set', async () => {
const addEndpointToImageUrl = await loadAddEndpointToImageUrl();
const text = '/api/system/img/abc123.png';
expect(addEndpointToImageUrl(text)).toBe(text);
});
it('should prepend FE_DOMAIN to matching image URLs without subRoute', async () => { it('should prepend FE_DOMAIN to matching image URLs without subRoute', async () => {
vi.stubEnv('FE_DOMAIN', 'https://example.com'); vi.stubEnv('FE_DOMAIN', 'https://example.com');
const addEndpointToImageUrl = await loadAddEndpointToImageUrl(); const addEndpointToImageUrl = await loadAddEndpointToImageUrl();
......
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import { z } from 'zod'; import { z } from 'zod';
import { ApiRequestInputParseError } from '../../../common/zod/requestParseError'; import { ApiRequestInputParseError } from '../../../common/zod/requestParseError';
import { UserError } from '@fastgpt/global/common/error/utils';
import { ERROR_ENUM, ERROR_RESPONSE } from '@fastgpt/global/common/error/errorCode';
vi.unmock('@fastgpt/service/common/response'); vi.unmock('@fastgpt/service/common/response');
...@@ -18,7 +20,7 @@ vi.mock('@fastgpt/service/common/logger', () => ({ ...@@ -18,7 +20,7 @@ vi.mock('@fastgpt/service/common/logger', () => ({
} }
})); }));
const { processError } = await import('../../../common/response'); const { getSseErrorResponse, processError } = await import('../../../common/response');
function buildZodError() { function buildZodError() {
try { try {
...@@ -82,3 +84,47 @@ describe('processError zod logging', () => { ...@@ -82,3 +84,47 @@ describe('processError zod logging', () => {
expect(logger.info).not.toHaveBeenCalled(); expect(logger.info).not.toHaveBeenCalled();
}); });
}); });
describe('getSseErrorResponse logging', () => {
beforeEach(() => {
logger.info.mockClear();
logger.error.mockClear();
});
it('keeps UserError out of error-level otel logs', () => {
const error = new UserError('Invalid stream input');
const response = getSseErrorResponse(error);
expect(JSON.parse(response.data)).toEqual({ message: 'Invalid stream input' });
expect(logger.info).toHaveBeenCalledWith('Request error', {
url: undefined,
message: 'Invalid stream input'
});
expect(logger.error).not.toHaveBeenCalled();
});
it('keeps unexpected SSE failures at error level', () => {
const error = new Error('Unexpected stream failure');
const response = getSseErrorResponse(error);
expect(JSON.parse(response.data)).toEqual({ message: 'Unexpected stream failure' });
expect(logger.error).toHaveBeenCalledWith('System unexpected error', {
url: undefined,
message: 'Unexpected stream failure',
error
});
expect(logger.info).not.toHaveBeenCalled();
});
it('keeps specified error responses unchanged for UserError', () => {
const error = new UserError(ERROR_ENUM.unAuthorization);
const response = getSseErrorResponse(error);
expect(JSON.parse(response.data)).toEqual(ERROR_RESPONSE[ERROR_ENUM.unAuthorization]);
expect(response.shouldClearCookie).toBe(true);
expect(logger.error).not.toHaveBeenCalled();
});
});
...@@ -21,7 +21,7 @@ const loadAccessLinkModules = async ( ...@@ -21,7 +21,7 @@ const loadAccessLinkModules = async (
vi.stubEnv('FILE_TOKEN_KEY', strongFileTokenKey); vi.stubEnv('FILE_TOKEN_KEY', strongFileTokenKey);
vi.stubEnv('FILE_DOMAIN', env.fileDomain ?? 'https://files.example.com/'); vi.stubEnv('FILE_DOMAIN', env.fileDomain ?? 'https://files.example.com/');
vi.stubEnv('FILE_DOWNLOAD_PUBLIC_URL_PREFIX', env.fileDownloadPublicUrlPrefix); vi.stubEnv('FILE_DOWNLOAD_PUBLIC_URL_PREFIX', env.fileDownloadPublicUrlPrefix);
vi.stubEnv('FE_DOMAIN', env.feDomain); vi.stubEnv('FE_DOMAIN', env.feDomain ?? 'https://app.example.com');
vi.stubEnv('NEXT_PUBLIC_BASE_URL', env.nextPublicBaseUrl ?? '/fastgpt'); vi.stubEnv('NEXT_PUBLIC_BASE_URL', env.nextPublicBaseUrl ?? '/fastgpt');
const [accessLink, downloadAliasSchema, uploadSessionSchema] = await Promise.all([ const [accessLink, downloadAliasSchema, uploadSessionSchema] = await Promise.all([
......
...@@ -17,7 +17,7 @@ describe('fileUrlValidator', () => { ...@@ -17,7 +17,7 @@ describe('fileUrlValidator', () => {
vi.stubEnv('STORAGE_S3_ENDPOINT', undefined); vi.stubEnv('STORAGE_S3_ENDPOINT', undefined);
vi.stubEnv('STORAGE_EXTERNAL_ENDPOINT', undefined); vi.stubEnv('STORAGE_EXTERNAL_ENDPOINT', undefined);
vi.stubEnv('STORAGE_S3_CDN_ENDPOINT', undefined); vi.stubEnv('STORAGE_S3_CDN_ENDPOINT', undefined);
vi.stubEnv('FE_DOMAIN', undefined); vi.stubEnv('FE_DOMAIN', 'https://fastgpt.example.com');
vi.stubEnv('PRO_URL', undefined); vi.stubEnv('PRO_URL', undefined);
}); });
......
...@@ -265,7 +265,7 @@ describe('runFastAgentLoop', () => { ...@@ -265,7 +265,7 @@ describe('runFastAgentLoop', () => {
id: 'read_file_call', id: 'read_file_call',
name: 'read_files', name: 'read_files',
args: { args: {
ids: ['file_1'] urls: ['https://files.example.com/file_1']
} }
}), }),
text({ text({
...@@ -287,6 +287,7 @@ describe('runFastAgentLoop', () => { ...@@ -287,6 +287,7 @@ describe('runFastAgentLoop', () => {
systemTools: { systemTools: {
readFile: { readFile: {
enabled: true, enabled: true,
maxFileAmount: 20,
execute: executeReadFile execute: executeReadFile
} }
}, },
...@@ -342,7 +343,7 @@ describe('runFastAgentLoop', () => { ...@@ -342,7 +343,7 @@ describe('runFastAgentLoop', () => {
id: 'runtime_read_file_call', id: 'runtime_read_file_call',
name: 'read_files', name: 'read_files',
args: { args: {
ids: ['file_1'] urls: ['https://files.example.com/file_1']
} }
}), }),
text({ text({
......
...@@ -713,6 +713,7 @@ describe('runPiAgentLoop', () => { ...@@ -713,6 +713,7 @@ describe('runPiAgentLoop', () => {
ask: { enabled: true }, ask: { enabled: true },
readFile: { readFile: {
enabled: true, enabled: true,
maxFileAmount: 20,
execute: vi.fn() execute: vi.fn()
}, },
datasetSearch: { datasetSearch: {
......
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Readable } from 'node:stream';
const axiosMock = vi.hoisted(() => ({ const axiosMock = vi.hoisted(() => ({
get: vi.fn() get: vi.fn()
})); }));
vi.mock('@fastgpt/service/common/api/axios', () => ({ vi.mock('@fastgpt/service/common/api/axios', async (importOriginal) => {
pickOutboundAxios: vi.fn(() => axiosMock) const mod = await importOriginal<typeof import('@fastgpt/service/common/api/axios')>();
})); return {
...mod,
axios: axiosMock
};
});
import { writeUrlFilesToSandbox } from '@fastgpt/service/core/ai/sandbox/application/file'; import { writeUrlFilesToSandbox } from '@fastgpt/service/core/ai/sandbox/application/file';
...@@ -29,9 +34,11 @@ describe('sandbox file application', () => { ...@@ -29,9 +34,11 @@ describe('sandbox file application', () => {
}); });
it('downloads url files and writes them to sandbox', async () => { it('downloads url files and writes them to sandbox', async () => {
const first = new ArrayBuffer(1); const first = Buffer.from('a');
const second = new ArrayBuffer(2); const second = Buffer.from('bc');
axiosMock.get.mockResolvedValueOnce({ data: first }).mockResolvedValueOnce({ data: second }); axiosMock.get
.mockResolvedValueOnce({ data: Readable.from([first]), headers: {} })
.mockResolvedValueOnce({ data: Readable.from([second]), headers: {} });
const sandbox = { const sandbox = {
writeFiles: vi.fn(async () => undefined) writeFiles: vi.fn(async () => undefined)
}; };
...@@ -41,15 +48,45 @@ describe('sandbox file application', () => { ...@@ -41,15 +48,45 @@ describe('sandbox file application', () => {
{ path: '/workspace/b.txt', url: 'https://example.com/b.txt' } { path: '/workspace/b.txt', url: 'https://example.com/b.txt' }
]); ]);
expect(axiosMock.get).toHaveBeenCalledWith('https://example.com/a.txt', { expect(axiosMock.get).toHaveBeenCalledWith(
responseType: 'arraybuffer' 'https://example.com/a.txt',
}); expect.objectContaining({ responseType: 'stream' })
expect(axiosMock.get).toHaveBeenCalledWith('https://example.com/b.txt', { );
responseType: 'arraybuffer' expect(axiosMock.get).toHaveBeenCalledWith(
}); 'https://example.com/b.txt',
expect.objectContaining({ responseType: 'stream' })
);
expect(sandbox.writeFiles).toHaveBeenCalledWith([ expect(sandbox.writeFiles).toHaveBeenCalledWith([
{ path: '/workspace/a.txt', data: first }, { path: '/workspace/a.txt', data: first },
{ path: '/workspace/b.txt', data: second } { path: '/workspace/b.txt', data: second }
]); ]);
}); });
it('uses the injected reader for workflow context files', async () => {
const sandbox = { writeFiles: vi.fn(async () => undefined) };
const readInputFile = vi.fn().mockResolvedValue(Buffer.from('private'));
await writeUrlFilesToSandbox(
sandbox as any,
[{ path: '/workspace/private.pdf', url: 'https://files.example.com/signed' }],
readInputFile
);
expect(readInputFile).toHaveBeenCalledWith('https://files.example.com/signed');
expect(axiosMock.get).not.toHaveBeenCalled();
expect(sandbox.writeFiles).toHaveBeenCalledWith([
{ path: '/workspace/private.pdf', data: Buffer.from('private') }
]);
});
it('rejects relative input URLs instead of calling an internal axios', async () => {
const sandbox = { writeFiles: vi.fn() };
await expect(
writeUrlFilesToSandbox(sandbox as any, [
{ path: '/workspace/a.txt', url: '/api/system/file/d/token' }
])
).rejects.toThrow('absolute HTTP(S)');
expect(axiosMock.get).not.toHaveBeenCalled();
});
}); });
This diff is collapsed. Click to expand it.
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