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:
<<: [*x-share-db-config, *x-vec-config, *x-log-config, *x-no-proxy-config]
HOSTNAME: 0.0.0.0
# ==================== 基础配置 ====================
# 前端外部可访问的地址,用于自动补全文件资源路径。例如 https:fastgpt.cn,不能填 localhost。这个值可以不填,不填则发给模型的图片会是一个相对路径,而不是全路径,模型可能伪造Host。
# 必填。客户端访问 FastGPT 时使用的地址,由协议、主机和可选端口组成,用于补全文件资源路径。例如 https://fastgpt.cn;本地开发可使用 http://localhost:3000。
FE_DOMAIN:
# root key(最高权限)
ROOT_KEY: *x-system-key
......
......@@ -303,7 +303,7 @@ services:
*x-service-env-config,
*x-app-env-config
]
# 前端外部可访问的地址,用于自动补全文件资源路径。例如 https:fastgpt.cn,不能填 localhost。这个值可以不填,不填则发给模型的图片会是一个相对路径,而不是全路径,模型可能伪造Host。
# 必填。客户端访问 FastGPT 时使用的地址,由协议、主机和可选端口组成,用于补全文件资源路径。例如 https://fastgpt.cn;本地开发可使用 http://localhost:3000。
FE_DOMAIN:
# 文件域名(也指向 FastGPT 服务);如需更高安全性可独立分配域名,避免高危文件读取到主域名内容
FILE_DOMAIN:
......
......@@ -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. |
| `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_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. |
| `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. |
......@@ -150,16 +150,16 @@ These variables are mainly validated by `packages/service/env.ts` and apply to `
### Domains, Frontend, and Runtime
| Variable | Default | Description |
| ------------------------- | --------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `FE_DOMAIN` | Empty | External FastGPT frontend URL. Used to complete file and image URLs. Do not set this to `localhost`. |
| `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. |
| `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. |
| `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. |
| `NODE_OPTIONS` | `--max-old-space-size=4096` | Node options used during production image builds to increase the build memory limit. |
| Variable | Default | Description |
| ------------------------- | --------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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. |
| `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`. |
| `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`. |
| `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. |
### Security
......
......@@ -43,8 +43,8 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
| `AIPROXY_API_TOKEN` | 空 | 调用 AI Proxy 使用的认证 Token。 |
| `OPENAI_BASE_URL` | `https://api.openai.com/v1` | 未配置 AI Proxy 时,兼容 OpenAI 协议的默认模型接口地址。 |
| `CHAT_API_KEY` | 空 | 未配置 AI Proxy Token 时,兼容 OpenAI 协议的默认模型 API Key。 |
| `CRM_API_URL` | 空 | 官网访客归因 CRM 的 API 基础地址(包含 `/api/v1`);为空时不进行身份上报。 |
| `CRM_API_KEY` | 空 | CRM 管理 API Key,用于注册或登录成功后按 `visitor_id` 绑定 FastGPT 用户。 |
| `CRM_API_URL` | 空 | 官网访客归因 CRM 的 API 基础地址(包含 `/api/v1`);为空时不进行身份上报。 |
| `CRM_API_KEY` | 空 | CRM 管理 API Key,用于注册或登录成功后按 `visitor_id` 绑定 FastGPT 用户。 |
| `MARKETPLACE_URL` | `https://v2.marketplace.fastgpt.cn` | 插件市场接口地址。 |
| `FEISHU_BASE_URL` | `https://open.feishu.cn` | 飞书开放平台地址,私有化飞书可改为对应域名。 |
| `DINGTALK_BASE_URL` | `https://api.dingtalk.com` | 钉钉新版 API 基础地址。 |
......@@ -150,16 +150,16 @@ description: projects/app、projects/code-sandbox 与 pro/admin 环境变量说
### 域名、前端与运行时
| 变量 | 默认值 | 说明 |
| ------------------------- | --------------------------- | -------------------------------------------------------------------------------- |
| `FE_DOMAIN` | 空 | FastGPT 前端外部访问地址,用于补全文件、图片等资源路径;不要配置为 `localhost`。 |
| `FILE_DOMAIN` | 空 | 文件访问域名,通常也指向 FastGPT 服务;可独立域名隔离文件风险。 |
| `NEXT_PUBLIC_BASE_URL` | 空 | Next.js 子路径部署前缀,例如 `/fastgpt`;需要在构建镜像时确定。 |
| `HOSTNAME` | `localhost` | 服务本机 Host,用于内部 URL 与 SSRF 本地地址识别;容器中常设为 `0.0.0.0`。 |
| `PORT` | `3000` | Next.js 服务监听端口,也用于本地地址识别。 |
| `NODE_ENV` | 空 | 标准 Node/Next.js 运行环境变量,生产镜像中为 `production`。 |
| `NEXT_TELEMETRY_DISABLED` | `1` | 生产镜像中关闭 Next.js Telemetry。 |
| `NODE_OPTIONS` | `--max-old-space-size=4096` | 生产镜像构建阶段使用的 Node.js 启动参数,用于提高构建内存上限。 |
| 变量 | 默认值 | 说明 |
| ------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| `FE_DOMAIN` | 必填 | 客户端访问 FastGPT 时使用的地址(由协议、主机和可选端口组成),用于补全文件、图片等资源路径;本地开发可使用 `http://localhost:3000`。 |
| `FILE_DOMAIN` | 空 | 文件访问域名,通常也指向 FastGPT 服务;可独立域名隔离文件风险。 |
| `NEXT_PUBLIC_BASE_URL` | 空 | Next.js 子路径部署前缀,例如 `/fastgpt`;需要在构建镜像时确定。 |
| `HOSTNAME` | `localhost` | 服务本机 Host,用于内部 URL 与 SSRF 本地地址识别;容器中常设为 `0.0.0.0`。 |
| `PORT` | `3000` | Next.js 服务监听端口,也用于本地地址识别。 |
| `NODE_ENV` | 空 | 标准 Node/Next.js 运行环境变量,生产镜像中为 `production`。 |
| `NEXT_TELEMETRY_DISABLED` | `1` | 生产镜像中关闭 Next.js Telemetry。 |
| `NODE_OPTIONS` | `--max-old-space-size=4096` | 生产镜像构建阶段使用的 Node.js 启动参数,用于提高构建内存上限。 |
### 安全配置
......
......@@ -3,10 +3,24 @@ title: 'V4.15.4 (In Progress)'
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
## ⚙️ Improvements
1. Added Workflow file context management to reduce duplicate URL signing and address potential security issues.
## 🐛 Fixes
1. Fixed an issue where Chatbox displayed system tool errors during streaming responses.
......
......@@ -3,10 +3,22 @@ title: '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 流输出时候,不应该展示系统工具的错误。
......
......@@ -167,8 +167,8 @@
"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.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.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-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.mdx": "2026-06-04T16:10:15+08:00",
"content/self-host/config/model/minimax.en.mdx": "2026-06-03T10:40:17+08:00",
......@@ -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/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/4154.en.mdx": "2026-07-21T13:14:50+08:00",
"content/self-host/upgrading/4-15/4154.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-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.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({
tag: z.string().optional()
});
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 {
AgentPlanStatusSchema
} from '../ai/agent/type';
import { ObjectIdSchema } from '../../common/type/mongo';
import { SandboxFileRefSchema } from '../ai/sandbox/type';
export const ChatHistoryItemResSchema = DispatchNodeResponseSchema.extend({
nodeId: z.string(),
......@@ -41,8 +40,7 @@ export const ToolModuleResponseItemSchema = z.object({
toolAvatar: z.string(),
params: z.string(),
response: z.string().nullish(),
functionName: z.string(),
fileRefs: z.array(SandboxFileRefSchema).optional()
functionName: z.string()
});
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 = (
if (lastInteractive) {
const memoryEdges = lastInteractive.memoryEdges || [];
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', () => {
} as WorkflowInteractiveResponseType;
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', () => {
......
......@@ -12,8 +12,6 @@ import { ProxyAgent, type ProxyAgentOptions } from 'proxy-agent';
import { getProxyForUrl } from 'proxy-from-env';
import { isDevEnv } from '@fastgpt/global/common/system/constants';
import { isInternalAddress, isInternalResolvedIP, PRIVATE_URL_TEXT } from '../system/utils';
import { isAbsoluteUrl } from '../security/network';
import { SERVICE_LOCAL_HOST } from '../system/tools';
/**
* 给 shared axios 实例添加 SSRF 防护。
......@@ -412,32 +410,3 @@ export function createProxyAxios(config?: AxiosRequestConfig, ssrfCheck = true)
/** @see https://github.com/axios/axios/issues/4531 */
export const axios = createProxyAxios();
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 = (
shouldClearCookie: boolean;
} => {
const errResponseKey = typeof error === 'string' ? error : error?.message;
const processedError = processError({ error });
// Specified error
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 {
event: SseResponseEventEnum.error,
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 {
event: SseResponseEventEnum.error,
data: JSON.stringify({ message: replaceSensitiveText(msg) }),
shouldClearCookie: false
data: JSON.stringify({ message: processedError.message }),
shouldClearCookie: processedError.shouldClearCookie
};
};
......
......@@ -6,7 +6,6 @@ import type {
import type { AgentPlanType } from '@fastgpt/global/core/ai/agent/type';
import type { AgentAskPayload } from './systemTool/ask';
import type { AgentLoopUsage } from './usage';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
export type AgentLoopToolResponseCompress = {
response: string;
......@@ -85,7 +84,6 @@ export type AgentLoopEvent =
errorMessage?: string;
seconds: number;
usages?: AgentLoopUsage[];
fileRefs?: SandboxFileRef[];
toolResponseCompress?: AgentLoopToolResponseCompress;
/** executeTool 返回的 opaque metadata,agent-loop 不解释其业务结构。 */
metadata?: unknown;
......
......@@ -4,10 +4,14 @@ import z from 'zod';
export const READ_FILES_TOOL_NAME = 'read_files';
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',
function: {
name: READ_FILES_TOOL_NAME,
......@@ -15,15 +19,16 @@ export const createReadFilesTool = (): ChatCompletionTool => ({
parameters: {
type: 'object',
properties: {
ids: {
urls: {
type: 'array',
maxItems: maxFileAmount,
items: {
type: 'string'
},
description: 'File IDs'
description: 'Absolute HTTP(S) file URLs'
}
},
required: ['ids']
required: ['urls']
}
}
});
......
......@@ -6,7 +6,6 @@ import type {
import type { SandboxClient } from '../../../sandbox/interface/runtime';
import type { AgentLoopDatasetSearchExecutor } from './systemTool/datasetSearch';
import type { AgentLoopUsage } from './usage';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
export type AgentLoopToolCatalog = {
runtimeTools: ChatCompletionTool[];
......@@ -31,7 +30,6 @@ export type AgentLoopToolExecutionResult<TChildrenResponse = unknown> = {
stop?: boolean;
skipResponseCompress?: boolean;
errorMessage?: string;
fileRefs?: SandboxFileRef[];
/** 由调用方透传并在 agent-loop 外部解释的工具运行元数据。 */
metadata?: unknown;
};
......@@ -60,6 +58,7 @@ export type AgentLoopSystemTools = {
};
readFile?: {
enabled: boolean;
maxFileAmount: number;
execute: AgentLoopReadFileExecutor;
};
datasetSearch?: {
......
......@@ -96,7 +96,13 @@ export const runFastAgentLoop = async <TChildrenResponse = unknown>({
...(runtime.systemTools?.sandbox?.enabled && runtime.systemTools.sandbox.client
? { sandboxTools: createAgentLoopSandboxTools() }
: {}),
...(runtime.systemTools?.readFile?.enabled ? { readFileTool: createReadFilesTool() } : {}),
...(runtime.systemTools?.readFile?.enabled
? {
readFileTool: createReadFilesTool({
maxFileAmount: runtime.systemTools.readFile.maxFileAmount
})
}
: {}),
...(runtime.systemTools?.datasetSearch?.enabled
? { datasetSearchTool: createDatasetSearchTool() }
: {})
......
......@@ -23,7 +23,6 @@ import { getErrText } from '@fastgpt/global/common/error/utils';
import { batchRun } from '@fastgpt/global/common/system/utils';
import { normalizeToolResponseContent } from '@fastgpt/global/core/ai/llm/utils';
import type { AgentPlanType } from '@fastgpt/global/core/ai/agent/type';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
type RunAgentCallProps<TChildrenResponse = unknown> = {
maxRunAgentTimes: number;
......@@ -88,7 +87,6 @@ type RunAgentCallProps<TChildrenResponse = unknown> = {
errorMessage?: string;
seconds: number;
usages?: AgentLoopUsage[];
fileRefs?: SandboxFileRef[];
metadata?: unknown;
toolResponseCompress?: {
response: string;
......@@ -292,7 +290,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
interactive,
stop,
errorMessage,
fileRefs,
metadata
} = await (async () => {
try {
......@@ -320,7 +317,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
errorMessage: interactiveToolErrorMessage || errorMessage,
seconds: +((Date.now() - toolStartTime) / 1000).toFixed(2),
usages: toolUsages,
fileRefs,
metadata
});
......@@ -530,7 +526,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
stop: stopLoop,
skipResponseCompress,
errorMessage,
fileRefs,
metadata
} = await (async () => {
try {
......@@ -594,7 +589,6 @@ export const runAgentLoop = async <TChildrenResponse = unknown>({
: {}),
seconds: +((Date.now() - toolStartTime) / 1000).toFixed(2),
usages: [...toolUsages, ...(toolResponseCompress ? [toolResponseCompress.usage] : [])],
fileRefs,
toolResponseCompress,
metadata
});
......
......@@ -353,7 +353,6 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({
seconds,
errorMessage,
usages,
fileRefs,
toolResponseCompress,
metadata
}) => {
......@@ -369,7 +368,6 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({
seconds,
errorMessage,
usages,
fileRefs,
toolResponseCompress,
metadata
});
......@@ -460,8 +458,7 @@ export const runFastAgentMainLoop = async <TChildrenResponse = unknown>({
});
return createToolResponse(sandboxResult.response, {
skipResponseCompress: true,
errorMessage: sandboxResult.success ? undefined : sandboxResult.response,
fileRefs: sandboxResult.fileRefs
errorMessage: sandboxResult.success ? undefined : sandboxResult.response
});
}
......
......@@ -305,7 +305,6 @@ export const runPiAgentLoop = async <TChildrenResponse = unknown>({
assistantMessages: result.assistantMessages,
usages: result.usages,
errorMessage: result.errorMessage,
fileRefs: result.fileRefs,
metadata: result.metadata,
seconds: +((Date.now() - startedAt) / 1000).toFixed(2)
});
......
......@@ -33,7 +33,6 @@ import {
import { runSandboxTools } from '../../../../../sandbox/interface/toolCall';
import { createToolCall, normalizeToolArgs, stringifyJson } from '../message';
import { getPiAgentRuntimeTools } from './catalog';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
type PlanOperationEvent = Extract<AgentLoopEvent, { type: 'plan_operation' }>;
......@@ -105,7 +104,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
interactive?: TChildrenResponse;
stop?: boolean;
errorMessage?: string;
fileRefs?: SandboxFileRef[];
metadata?: unknown;
}>;
}) => {
......@@ -133,7 +131,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
assistantMessages,
usages,
errorMessage: result.errorMessage,
fileRefs: result.fileRefs,
metadata: result.metadata,
seconds: +((Date.now() - startedAt) / 1000).toFixed(2)
});
......@@ -296,7 +293,6 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
response: sandboxResult.response,
assistantMessages: [],
usages: [],
fileRefs: sandboxResult.fileRefs,
errorMessage: sandboxResult.success ? undefined : sandboxResult.response
};
}
......@@ -308,7 +304,9 @@ export const buildPiAgentTools = async <TChildrenResponse = unknown>({
}
if (runtime.systemTools?.readFile?.enabled) {
const readFileTool = createReadFilesTool();
const readFileTool = createReadFilesTool({
maxFileAmount: runtime.systemTools.readFile.maxFileAmount
});
tools.push({
name: readFileTool.function.name,
label: readFileTool.function.name,
......
......@@ -14,7 +14,9 @@ const getPiAgentSystemTools = <TChildrenResponse = unknown>(
...(runtime.systemTools?.sandbox?.enabled && runtime.systemTools.sandbox.client
? createAgentLoopSandboxTools()
: []),
...(runtime.systemTools?.readFile?.enabled ? [createReadFilesTool()] : []),
...(runtime.systemTools?.readFile?.enabled
? [createReadFilesTool({ maxFileAmount: runtime.systemTools.readFile.maxFileAmount })]
: []),
...(runtime.systemTools?.datasetSearch?.enabled ? [createDatasetSearchTool()] : [])
];
......
......@@ -4,7 +4,7 @@ import { READ_FILES_TOOL_NAME } from '../agentLoop/interface';
* 将本轮上传文件整理为文本上下文,包含文件名、沙盒路径和可选文件内容。
*/
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 '';
return `## 对话文件
......@@ -15,7 +15,6 @@ export const getUserFilesPrompt = (
${files
.map((file) =>
`<file>
${file.id ? `<id>${file.id}</id>` : ''}
<name>${file.name}</name>
<url>${file.url}</url>
${file.sandboxPath ? `<sandboxPath>${file.sandboxPath}</sandboxPath>` : ''}
......
......@@ -5,15 +5,17 @@
*/
import type { FileWriteEntry, ISandbox } from '@fastgpt-sdk/sandbox-adapter';
import mime from 'mime';
import { pickOutboundAxios } from '../../../../common/api/axios';
import type { SandboxClient } from './runtime/client';
import { getSandboxRuntimeProfile } from '../infrastructure/provider/runtimeProfile';
import { readExternalFileBuffer } from '../../../../common/file/read/external';
export type SandboxUrlFile = {
path: string;
url: string;
};
export type SandboxInputFileReader = (url: string) => Promise<Buffer>;
export type SandboxFileContent = {
content: Buffer;
contentType: string;
......@@ -39,25 +41,30 @@ const isWithinSandboxWorkspace = (path: string, workDirectory: string) => {
return path === workspace || path.startsWith(`${workspace}/`);
};
/** 读取准备写入 Sandbox 的远程文件,禁止相对 URL 回环访问本机 API。 */
export const readSandboxUrlFile = async (url: string) => {
return (await readExternalFileBuffer({ url })).buffer;
};
/**
* 将远程 URL 文件写入已存在的 sandbox 实例。
*
* 这里不负责 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>[] = [];
for (const { path, url } of files) {
if (!path) continue;
writeFileTasks.push(
pickOutboundAxios(url)
.get<ArrayBuffer>(url, {
responseType: 'arraybuffer'
})
.then((response) => ({
path,
data: response.data
}))
readInputFile(url).then((data) => ({
path,
data
}))
);
}
......
......@@ -5,8 +5,10 @@
*/
import type { FileWriteEntry, ISandbox } from '@fastgpt-sdk/sandbox-adapter';
import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants';
import { pickOutboundAxios } from '../../../../../common/api/axios';
import { getSafeSandboxInputFilename } from '../../utils';
import { readSandboxUrlFile, type SandboxInputFileReader } from '../file';
export type { SandboxInputFileReader } from '../file';
export type SandboxInputFile = {
name: string;
......@@ -41,7 +43,11 @@ export const readSandboxPwd = async (sandboxClient: SandboxCommandClient) => {
* 路径规则和通用 toolcall 保持一致:用户文件直接写入 user_files/<文件名>。
* 这里直接消费 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 usedNames = new Map<string, number>();
......@@ -49,14 +55,10 @@ export const injectInputFilesToSandbox = async (sandbox: ISandbox, files: Sandbo
const filename = getSafeSandboxInputFilename(file.name, index, usedNames);
const path = `${SANDBOX_USER_FILES_PATH}${filename}`;
writeFileTasks.push(
pickOutboundAxios(file.url)
.get<ArrayBuffer>(file.url, {
responseType: 'arraybuffer'
})
.then((response) => ({
path,
data: response.data
}))
readInputFile(file.url).then((data) => ({
path,
data
}))
);
}
......
......@@ -9,7 +9,8 @@ import {
injectInputFilesToSandbox,
readSandboxPwd,
type SandboxCommandClient,
type SandboxInputFile
type SandboxInputFile,
type SandboxInputFileReader
} from './files';
import { prepareSandboxRuntimeMirrors } from './mirrors';
......@@ -101,10 +102,11 @@ export const emptyWorkDirectory =
/** 将本轮用户输入文件写入 sandbox 的 user_files 目录。 */
export const injectCurrentInputFiles =
<Context extends SandboxPrepareContext>(
currentFiles: SandboxInputFile[]
currentFiles: SandboxInputFile[],
readInputFile?: SandboxInputFileReader
): SandboxPrepareStep<Context> =>
async (context) => {
await injectInputFilesToSandbox(context.sandbox, currentFiles);
await injectInputFilesToSandbox(context.sandbox, currentFiles, readInputFile);
return context;
};
......
......@@ -47,17 +47,11 @@ export const sandboxGetFileUrlTool = defineTool({
external: true
});
return {
responseFile: { fileUrl, filename },
fileRef: { key, filename, url: fileUrl }
};
return { fileUrl, filename };
})
);
return {
response: JSON.stringify(result.map((item) => item.responseFile)),
fileRefs: result.map((item) => item.fileRef)
};
return { response: JSON.stringify(result) };
}
});
......
......@@ -17,12 +17,11 @@ import { toolMap as shellToolMap } from './shell.tool';
import { toolMap as writeFileToolMap } from './writeFile.tool';
import { getSandboxClient, type SandboxClient } from '../runtime/client';
import { parseJsonArgs } from '../../../utils';
import { writeUrlFilesToSandbox } from '../file';
import { writeUrlFilesToSandbox, type SandboxInputFileReader } from '../file';
import { getSandboxRuntimeProfile } from '../../infrastructure/provider/runtimeProfile';
import { preparePackageMirrors, prepareSandbox } from '../runtime/prepare';
import { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { getRunningSandboxId } from '../../utils/id';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
const ToolMap = {
...editFileToolMap,
......@@ -39,7 +38,6 @@ export type SandboxToolCallResult = {
success: boolean;
input: Record<string, any>;
response: string;
fileRefs?: SandboxFileRef[];
durationSeconds: number;
};
......@@ -91,7 +89,6 @@ export const runSandboxTools = async ({
success: true,
input: parsedArgs.data,
response: result.response,
...(result.fileRefs?.length ? { fileRefs: result.fileRefs } : {}),
durationSeconds: getDuration()
};
};
......@@ -107,13 +104,15 @@ export const prepareSandboxToolRuntime = async ({
sourceId,
userId,
chatId,
files
files,
readInputFile
}: {
sourceType: ChatSourceTypeEnum;
sourceId: string;
userId: string;
chatId: string;
files: { path: string; url: string }[];
readInputFile?: SandboxInputFileReader;
}) => {
const sandboxId = getRunningSandboxId({
sourceType,
......@@ -142,7 +141,7 @@ export const prepareSandboxToolRuntime = async ({
},
preparePackageMirrors()
);
await writeUrlFilesToSandbox(instance.provider, files);
await writeUrlFilesToSandbox(instance.provider, files, readInputFile);
return instance;
};
......
......@@ -5,7 +5,6 @@
*/
import type { z } from 'zod';
import type { SandboxClient } from '../runtime/client';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
type ToolExecuteContext<P> = {
sandboxInstance: SandboxClient;
......@@ -19,9 +18,7 @@ type ToolExecuteContext<P> = {
*/
export type ToolDefinition<S extends z.ZodTypeAny = z.ZodTypeAny> = {
zodSchema: S;
execute: (
ctx: ToolExecuteContext<z.infer<S>>
) => Promise<{ response: string; fileRefs?: SandboxFileRef[] }>;
execute: (ctx: ToolExecuteContext<z.infer<S>>) => Promise<{ response: string }>;
};
/**
......
......@@ -8,7 +8,12 @@ export {
addDirectoryToArchive,
getSandboxFileContent,
isSandboxPathDirectory,
readSandboxUrlFile,
resolveSandboxWorkspacePath,
writeUrlFilesToSandbox
} 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'
import { getDefaultLLMModel } from '../../model';
import { getRunningSkillEditSandbox } from '../../sandbox/interface/skillEdit';
import { dispatchWorkFlow } from '../../../workflow/dispatch';
import { prepareWorkflowFileQuery } from '../../../workflow/utils/fileLimits';
import { WORKFLOW_MAX_RUN_TIMES } from '../../../workflow/constants';
import type { AppFileSelectConfigType } from '@fastgpt/global/core/app/type/config.schema';
import { getChatItems } from '../../../chat/controller';
......@@ -146,13 +147,29 @@ export async function handleSkillDebugChat(
const newHistories = concatHistories(histories, chatMessages);
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({
...chatSource,
chatId,
teamId,
tmbId,
source: ChatSourceEnum.test,
userContent: userQuestion,
userContent: workflowUserQuestion,
responseChatItemId: responseChatItemIdFromBody,
interactive
});
......@@ -213,11 +230,11 @@ export async function handleSkillDebugChat(
runtimeNodes,
runtimeEdges,
variables: {},
query: removeEmptyUserInput(userQuestion.value),
query: removeEmptyUserInput(workflowQuery),
maxFileAmount,
maxBytesPerFile,
lastInteractive: interactive,
chatConfig: {
fileSelectConfig: skillDebugFileSelectConfig
},
chatConfig,
histories: newHistories,
stream: true,
maxRunTimes: WORKFLOW_MAX_RUN_TIMES,
......@@ -280,7 +297,7 @@ export async function handleSkillDebugChat(
appChatConfig: {},
variables: {},
source: ChatSourceEnum.test,
userContent: userQuestion,
userContent: workflowUserQuestion,
aiContent: aiResponse,
durationSeconds,
nodeResponseSummary,
......@@ -336,7 +353,6 @@ export async function handleSkillDebugChat(
}
}
logger.error('Skill debug chat error', { error: err, skillId });
if (streamResponseContext) {
streamResponseContext.writeStreamError(err);
} else {
......
......@@ -4,10 +4,8 @@ import type {
} from '@fastgpt/global/core/ai/llm/type';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { UserError } from '@fastgpt/global/common/error/utils';
import { getFileMaxSize } from '../../common/file/utils';
import { isValidImageContentType } from '../../common/file/image/utils';
import { getS3ChatSource } from '../../common/s3/sources/chat';
import { serviceEnv } from '../../env';
type NormalizeCompletionMessagesProps = {
messages: ChatCompletionMessageParam[];
......@@ -15,6 +13,8 @@ type NormalizeCompletionMessagesProps = {
sourceId: string;
chatId: string;
uid: string;
maxFileAmount: number;
maxBytesPerFile: number;
};
/**
......@@ -48,7 +48,13 @@ const imageExtensionMap: Record<string, string> = {
/**
* 解析图片 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);
if (!match) {
throw new UserError('Invalid image base64 data URL');
......@@ -62,9 +68,10 @@ const parseImageDataUrl = (dataUrl: string) => {
const base64 = match[2].replace(/[\r\n]/g, '');
const paddingLength = base64.endsWith('==') ? 2 : base64.endsWith('=') ? 1 : 0;
const decodedSize = Math.floor((base64.length * 3) / 4) - paddingLength;
const maxSize = getFileMaxSize();
if (decodedSize <= 0 || decodedSize > maxSize) {
throw new UserError(`Image size exceeds limit: ${decodedSize} bytes, maximum ${maxSize} bytes`);
if (decodedSize <= 0 || decodedSize > maxBytesPerFile) {
throw new UserError(
`Image size exceeds limit: ${decodedSize} bytes, maximum ${maxBytesPerFile} bytes`
);
}
const body = Buffer.from(base64, 'base64');
......@@ -85,21 +92,43 @@ const parseImageDataUrl = (dataUrl: string) => {
/**
* 规范化 completions 消息中的 base64 图片。
*
* 当模型允许直接使用 URL 时,先把 Data URL 上传到当前会话的私有 S3 路径,再用临时预览
* URL 覆写运行态消息,并附带稳定 key。运行结束后聊天持久化只保存 key,不保存 base64 或
* 过期签名 URL。同一请求中重复出现的 Data URL 只上传一次。
* Data URL 始终先上传到当前会话的私有 S3 路径,再用临时预览 URL 覆写运行态消息并附带
* 稳定 key。模型层根据 MULTIPLE_DATA_TO_BASE64 决定是否重新读取并转换为 base64。
* 上传前统一校验数量和大小,避免部分文件已上传后才发现请求超限;相同 Data URL 只上传一次。
*/
export const normalizeCompletionMessages = async ({
messages,
sourceType,
sourceId,
chatId,
uid
uid,
maxFileAmount,
maxBytesPerFile
}: NormalizeCompletionMessagesProps): Promise<ChatCompletionMessageParam[]> => {
if (serviceEnv.MULTIPLE_DATA_TO_BASE64) return messages;
const chatS3 = getS3ChatSource();
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 (
part: Extract<ChatCompletionContentPart, { type: 'image_url' }>
......@@ -112,7 +141,9 @@ export const normalizeCompletionMessages = async ({
if (cached) return cached;
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({
sourceType,
sourceId,
......
......@@ -42,7 +42,6 @@ import {
stripUserContentFileUrls
} from './utils/prepare';
import { buildChatSourceQuery, buildChatSourceWriteFields, type ChatSourceParams } from './source';
import { isChatFileS3KeyForChat } from '../../common/s3/sources/chat/key';
const logger = getLogger(LogCategories.MODULE.CHAT);
......@@ -86,12 +85,8 @@ export const persistChatFiles = async ({
contents,
variables,
variableList,
sourceType,
sourceId,
chatId,
session
}: ChatSourceParams & {
chatId: string;
}: {
contents: (UserChatItemType | AIChatItemType)[];
variables?: Record<string, any>;
variableList?: VariableItemType[];
......@@ -110,26 +105,7 @@ export const persistChatFiles = async ({
keys.push(valueItem.file.key);
}
// 2. Sandbox 生成文件只在对话落库时转为正式资产。
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 是特殊格式的(工作流工具 + 表单输入)
// 2. query 是特殊格式的(工作流工具 + 表单输入)
if ('text' in valueItem && valueItem.text?.content) {
try {
const parsed = JSON.parse(valueItem.text.content);
......@@ -404,8 +380,6 @@ export const finalizeChatRound = async (props: Props) => {
contents: processedContent,
variables,
variableList,
...chatSource,
chatId,
session
});
......@@ -642,8 +616,6 @@ export const pushChatRecords = async (props: Props) => {
contents: processedContent,
variables,
variableList,
...chatSource,
chatId,
session
});
......@@ -1041,8 +1013,6 @@ export const updateInteractiveChat = async ({
contents: [userContent, aiContent],
variables,
variableList,
...chatSource,
chatId,
session
});
});
......
......@@ -31,143 +31,126 @@ const hasChildrenResponse = (
'childrenResponse' in interactive.params &&
!!interactive.params.childrenResponse;
/** 刷新历史消息中的服务端文件 URL,返回新 histories,不修改传入对象。 */
export const addPreviewUrlToChatItems = async (
histories: ChatItemMiniType[],
type: 'chatFlow' | 'workflowTool'
type: 'chatFlow' | 'workflowTool',
getPreviewUrl: (key: string) => Promise<string | undefined> = createChatFilePreviewUrlGetter()
) => {
const getPreviewUrl = createChatFilePreviewUrlGetter();
const sandboxUrlReplacements = new Map<string, string>();
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;
async function addPreviewUrlToFileValue(file: ChatFileValueWithPreview) {
if (!file.key) return { ...file };
item.value.forEach((value) => {
if (!value.text?.content) return;
const previewUrl = await getPreviewUrl(file.key);
if (previewUrl) {
return { ...file, url: previewUrl };
}
let content = value.text.content;
sandboxUrlReplacements.forEach((newUrl, oldUrl) => {
content = content.replaceAll(oldUrl, newUrl);
});
value.text.content = content;
});
});
return;
}
async function addPreviewUrlToFileValue(files: ChatFileValueWithPreview[]) {
await Promise.all(
files.map(async (file) => {
if (!file || typeof file !== 'object') return;
if (!file.key) return;
async function addPreviewUrlToValue(value: RuntimeValue) {
if (!Array.isArray(value)) return value;
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)) {
await Promise.all(
interactive.params.inputForm.map(async (input) => {
if (input.type === FlowNodeInputTypeEnum.fileSelect) {
const files = formatFileValueList(input.value);
await addPreviewUrlToFileValue(files);
}
})
);
params = {
...params,
inputForm: await Promise.all(
interactive.params.inputForm.map(async (input) => ({
...input,
value:
input.type === FlowNodeInputTypeEnum.fileSelect
? await addPreviewUrlToValue(input.value)
: input.value
}))
)
};
}
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) {
await Promise.all(
item.value.map(async (value) => {
if ('file' in value && value.file && value.file.key) {
await addPreviewUrlToFileValue([value.file]);
}
if ('interactive' in value && value.interactive) {
await addToInteractive(value.interactive);
}
})
);
async function addToChatflow(item: ChatItemMiniType): Promise<ChatItemMiniType> {
return {
...item,
value: await Promise.all(
item.value.map(async (value) => ({
...value,
...('file' in value && value.file
? { file: await addPreviewUrlToFileValue(value.file) }
: {}),
...('interactive' in value && value.interactive
? { interactive: await addToInteractive(value.interactive) }
: {})
}))
)
} as ChatItemMiniType;
}
async function addToWorkflowTool(item: ChatItemMiniType) {
if (item.obj !== ChatRoleEnum.Human || !Array.isArray(item.value)) return;
async function addToWorkflowTool(item: ChatItemMiniType): Promise<ChatItemMiniType> {
if (item.obj !== ChatRoleEnum.Human || !Array.isArray(item.value)) {
return { ...item, value: [...item.value] } as ChatItemMiniType;
}
await Promise.all(
item.value.map(async (value, index) => {
if (!('text' in value)) return;
const inputValueString = value.text?.content || '';
const parsedInputValue = JSON.parse(inputValueString) as FlowNodeInputItemType[];
return {
...item,
value: await Promise.all(
item.value.map(async (value) => {
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(
parsedInputValue.map(async (input) => {
if (!input.renderTypeList?.includes(FlowNodeInputTypeEnum.fileSelect)) {
return;
return {
...value,
text: {
...value.text,
content: JSON.stringify(nextInputValue)
}
const files = formatFileValueList(input.value);
await addPreviewUrlToFileValue(files);
})
);
item.value[index].text = {
...value.text,
content: JSON.stringify(parsedInputValue)
};
})
);
};
})
)
} as ChatItemMiniType;
}
// 先刷新工具产出文件,确保后续历史上下文不会继续引用过期链接。
await refreshSandboxToolFiles();
// Presign file urls
await Promise.all(
return Promise.all(
histories.map(async (item) => {
if (type === 'chatFlow') {
await addToChatflow(item);
} else if (type === 'workflowTool') {
await addToWorkflowTool(item);
return addToChatflow(item);
}
return addToWorkflowTool(item);
})
);
};
......
......@@ -13,12 +13,13 @@ import {
import type { NodeInputKeyEnum, NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
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 type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime';
import { authAppByTmbId } from '../../../../support/permission/app/auth';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
import { getUserChatInfo } from '../../../../support/user/team/utils';
import { runWithDerivedWorkflowFileContext } from '../../utils/context';
type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.userChatInput]: string;
......@@ -64,40 +65,59 @@ export const dispatchAppRequest = async (props: Props): Promise<Response> => {
isChildApp: true
};
const { externalProvider } = await getUserChatInfo(appData.tmbId);
const childVariableState = await WorkflowVariableState.create({
timezone: props.timezone,
runningAppInfo: childRunningAppInfo,
uid: props.uid,
chatId: props.chatId,
responseChatItemId: props.responseChatItemId,
histories: chatHistories,
variablesConfig: appData.chatConfig?.variables,
inputVariables: variableState.toStoreRecord(),
externalVariables: externalProvider?.externalWorkflowVariables,
sourceVariableState: variableState
const childInputVariables = variableState.toStoreRecord();
const childQuery = runtimePrompt2ChatsValue({
files,
text: userChatInput
});
let filteredChildHistories = chatHistories;
let filteredChildQuery = childQuery;
const { assistantResponses, system_memories, runtimeNodeResponseSummary } = await runWorkflow({
...props,
runningAppInfo: childRunningAppInfo,
runtimeNodes: storeNodes2RuntimeNodes(
appData.modules,
getWorkflowEntryNodeIds(appData.modules)
),
runtimeEdges: storeEdges2RuntimeEdges(appData.edges),
variableState: childVariableState,
chatConfig: appData.chatConfig,
histories: chatHistories,
query: runtimePrompt2ChatsValue({
files,
text: userChatInput
})
});
const { assistantResponses, system_memories, runtimeNodeResponseSummary } =
await runWithDerivedWorkflowFileContext({
query: childQuery,
histories: chatHistories,
files: getWorkflowFileVariableInputs({
variablesConfig: appData.chatConfig?.variables,
inputVariables: childInputVariables
}),
fn: async ({ resolveInputFile, query: filteredQuery, histories: filteredHistories }) => {
filteredChildHistories = filteredHistories;
filteredChildQuery = filteredQuery;
const childVariableState = await WorkflowVariableState.create({
timezone: props.timezone,
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,
value: query
value: filteredChildQuery
},
{
obj: ChatRoleEnum.AI,
......
......@@ -45,6 +45,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => {
lastInteractive?.type === 'loopInteractive' ? lastInteractive?.params : undefined;
let lastIndex = interactiveData?.currentIndex;
// 已知问题:旧 Loop 恢复时会继续写入历史 loopResult;为保持旧流程行为暂不复制。
const outputValueArr = interactiveData ? interactiveData.loopResult : [];
const assistantResponses: AIChatItemValueItemType[] = [];
const customFeedbacks: string[] = [];
......@@ -67,6 +68,7 @@ export const dispatchLoop = async (props: Props): Promise<Response> => {
// Init entry
if (isInteractiveResponseIndex) {
// 已知问题:旧 Loop 会直接修改并传递父级 runtimeNodes;该弃用节点暂时保留兼容行为。
runtimeNodes.forEach((node) => {
if (interactiveData?.childrenResponse?.entryNodeIds.includes(node.nodeId)) {
node.isEntry = true;
......
......@@ -22,7 +22,6 @@ type WorkflowAgentLoopRuntimeContext = ToolDispatchContext & {
nodeId: string;
flowNodeType: FlowNodeTypeEnum;
};
filesMap: UseUserContextResult['filesMap'];
currentFiles: UseUserContextResult['currentFiles'];
sandboxClient?: SandboxClient;
};
......@@ -144,6 +143,7 @@ export const createWorkflowAgentLoopRuntime = ({
readFile: toolProvider.readFileExecutor
? {
enabled: true,
maxFileAmount: toolProvider.readFileMaxFileAmount,
execute: toolProvider.readFileExecutor
}
: undefined
......
......@@ -25,6 +25,7 @@ import {
type AgentSandboxPrepareAction
} from './sub/sandbox';
import type { RuntimeNodeResponseSummary } from '../../type';
import { getWorkflowFileMaxAmount } from '../../../utils/context';
import { createAgentNodeResponseCollector } from './nodeResponseCollector';
import { createAgentSandboxPermissionDeniedError } from '../../../../ai/sandbox/interface/runtime';
import { replaceAgentPromptToolReferences } from './adapter/prompt';
......@@ -105,7 +106,6 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
lang,
histories,
query,
requestOrigin,
chatConfig,
lastInteractive,
runningAppInfo,
......@@ -154,10 +154,8 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
// 初始化对话框输入的文件
const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList);
const fileLinks =
fileUrlInput && fileUrlInput.value && fileUrlInput.value.length > 0
? props.params.fileUrlList
: undefined;
const parseHistoryFiles = !!fileUrlInput?.value?.length;
const fileLinks = parseHistoryFiles ? props.params.fileUrlList : undefined;
try {
if (hasSandboxRuntimeDependency && !global.feConfigs?.show_agent_sandbox) {
......@@ -171,12 +169,12 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
currentUserInput: userChatInput,
currentQuery: query,
currentDataId: responseChatItemId,
parseHistoryFiles,
selectedDataset: datasetParams?.datasets,
authTmbId: datasetParams?.authTmbId,
tmbId: runningUserInfo.tmbId,
timezone,
requestOrigin,
maxFiles: chatConfig?.fileSelectConfig?.maxFiles || 20
maxFileAmount: getWorkflowFileMaxAmount()
});
if (effectiveUseAgentSandbox) {
......@@ -206,7 +204,7 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
currentFiles: skipSandboxInputFiles ? [] : userContext.currentFiles
});
// 获取请求上下文
const { chatHistories, queryInput, fileUrlMap, filesMap } = userContext;
const { chatHistories, queryInput } = userContext;
const { rewrittenHistories, currentUserMessage } = userContext.getCurrentMessages({
skillInfos,
currentWorkingDirectory
......@@ -253,8 +251,6 @@ export const dispatchRunAgent = async (props: DispatchAgentModuleProps): Promise
getSubAppInfo,
getSubApp,
completionTools: agentCompletionTools,
fileUrlMap,
filesMap,
currentFiles: userContext.currentFiles,
sandboxClient,
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 {
type SandboxPrepareStep
} from '../../../../../../ai/sandbox/interface/runtime';
import type { ChatSourceTypeEnum } from '@fastgpt/global/core/chat/constants';
import { readWorkflowFileBuffer } from '../../../../../utils/context';
export type AgentSandboxPrepareContext = SandboxPrepareContext & {
sandboxClient: SandboxClient;
......@@ -89,7 +90,7 @@ export async function ensureAgentSandboxRuntime({
chatId,
teamId
});
const readInputFile = (url: string) => readWorkflowFileBuffer({ url });
const preparedContext = await withAgentSandboxInitLease({
sandboxId: sandboxContext.sandboxClient.getSandboxId(),
fn: () => {
......@@ -105,7 +106,7 @@ export async function ensureAgentSandboxRuntime({
? prepareSandbox(
context,
preparePackageMirrors(),
injectCurrentInputFiles(currentFiles),
injectCurrentInputFiles(currentFiles, readInputFile),
...prepareActions,
readCurrentWorkingDirectory(),
scanEditDebugSkillInfos()
......@@ -114,7 +115,7 @@ export async function ensureAgentSandboxRuntime({
context,
preparePackageMirrors(),
injectSelectedSkillFiles({ teamId, tmbId, skillIds, selectedSkills }),
injectCurrentInputFiles(currentFiles),
injectCurrentInputFiles(currentFiles, readInputFile),
...prepareActions,
readCurrentWorkingDirectory(),
runSandboxEntrypoint({ sandboxEntrypoint }),
......
......@@ -94,28 +94,9 @@ export type ToolDispatchContext = Pick<
getSubAppInfo: GetSubAppInfoFnType;
getSubApp: (id: string) => SubAppRuntimeType | undefined;
completionTools: ChatCompletionTool[];
fileUrlMap?: Record<string, string>;
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 runtimeParams = { ...params };
delete runtimeParams[NodeInputKeyEnum.forbidStream];
......@@ -156,7 +137,6 @@ export const getAgentDatasetParams = (
export const getExecuteTool = ({
getSubAppInfo,
getSubApp,
fileUrlMap = {},
checkIsStopping,
runningUserInfo,
runningAppInfo,
......@@ -223,13 +203,10 @@ export const getExecuteTool = ({
response: 'Params is not object'
};
}
const requestParams = replaceAgentFileIdsWithUrls(
{
...tool.params,
...toolCallParams
},
fileUrlMap
);
const requestParams = {
...tool.params,
...toolCallParams
};
if (tool.type === 'tool') {
const { response, usages, nodeResponse } = await dispatchTool({
......
......@@ -3,15 +3,15 @@ import { SubAppIds } from '@fastgpt/global/core/workflow/node/agent/constants';
import type { AgentLoopDatasetSearchExecutor } from '../../../../../ai/llm/agentLoop/interface';
import type { AgentLoopCoreToolRunResult } from '../../agentLoopCore/interface';
import {
buildAgentLoopCoreSystemToolFileUrl,
createAgentLoopCoreReadFileExecutor,
normalizeAgentLoopCoreDatasetSearchResult
} from '../../agentLoopCore/interface';
import { dispatchAgentDatasetSearch } from '../sub/dataset';
import { dispatchFileRead } from '../sub/file';
import { dispatchWorkflowReadFiles } from '../../readFiles';
import { getAgentDatasetParams, getExecuteTool } from '../sub/utils';
import type { WorkflowAgentToolProvider, WorkflowAgentToolProviderContext } from './type';
import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workflow/template/system/interactive/type';
import { getWorkflowFileMaxAmount } from '../../../../utils/context';
/**
* 创建 Workflow Agent 节点的工具 provider。
......@@ -58,25 +58,17 @@ export const createWorkflowAgentToolProvider = ({
});
}
: undefined;
const readFileMaxFileAmount = getWorkflowFileMaxAmount();
const readFileExecutor = createAgentLoopCoreReadFileExecutor({
enabled: Object.keys(context.filesMap).length > 0,
resolveFiles: (ids) =>
ids.map((id) => {
const file = context.filesMap[id];
return {
id,
...(file?.name ? { name: file.name } : {}),
url: file?.url || ''
};
}),
enabled: true,
execute: async ({ files }) =>
dispatchFileRead({
dispatchWorkflowReadFiles({
files,
teamId: context.runningUserInfo.teamId,
tmbId: context.runningUserInfo.tmbId,
customPdfParse: context.chatConfig?.fileSelectConfig?.customPdfParse,
usageId: context.usageId
usageId: context.usageId,
maxFileAmount: readFileMaxFileAmount
})
});
......@@ -114,12 +106,8 @@ export const createWorkflowAgentToolProvider = ({
});
},
readFileExecutor,
readFileMaxFileAmount,
datasetSearchExecutor,
currentInputFiles: context.currentFiles.map((file) =>
buildAgentLoopCoreSystemToolFileUrl({
url: file.url,
requestOrigin: context.requestOrigin
})
)
currentInputFiles: context.currentFiles.map((file) => file.url)
};
};
......@@ -7,7 +7,6 @@ import type { SubAppRuntimeType } from '../type';
import type { WorkflowInteractiveResponseType } from '@fastgpt/global/core/workflow/template/system/interactive/type';
export type WorkflowAgentToolProviderContext = ToolDispatchContext & {
filesMap: UseUserContextResult['filesMap'];
currentFiles: UseUserContextResult['currentFiles'];
};
......@@ -16,6 +15,7 @@ export type WorkflowAgentToolProvider = AgentLoopCoreToolProvider<
WorkflowInteractiveResponseType
> & {
readFileExecutor?: AgentLoopReadFileExecutor;
readFileMaxFileAmount: number;
datasetSearchExecutor?: AgentLoopDatasetSearchExecutor;
currentInputFiles: string[];
};
......@@ -356,14 +356,12 @@ export const createAgentLoopCoreAssistantEventCollector = ({
toolAvatar: toolInfo?.avatar || '',
functionName,
params: event.call.function.arguments ?? '',
response: event.response,
...(event.fileRefs?.length ? { fileRefs: event.fileRefs } : {})
response: event.response
});
} else {
updateToolResponse(event.call.id, (tool) => ({
...tool,
response: appendUniqueDelta(tool.response, event.response),
...(event.fileRefs?.length ? { fileRefs: event.fileRefs } : {})
response: appendUniqueDelta(tool.response, event.response)
}));
}
......
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 './files';
export * from './messages';
export * from './prompt';
export * from './reminder';
import { ChatFileTypeEnum } from '@fastgpt/global/core/chat/constants';
import type { SelectedDatasetType } from '@fastgpt/global/core/workflow/type/io';
import type { DeployedSkillInfo } from '../../../../../../ai/sandbox/interface/runtime';
import { READ_FILES_TOOL_NAME } from '../../../../../../ai/llm/agentLoop/interface';
import { SANDBOX_READ_FILE_TOOL_NAME } from '@fastgpt/global/core/ai/sandbox/tools';
export type AgentLoopCoreInputFile = {
id: string;
name: string;
type: ChatFileTypeEnum;
url: string;
};
import type { AgentLoopCoreInputFile } from './files';
export type AgentLoopCoreSelectedDatasetInput = Pick<SelectedDatasetType, 'datasetId'> &
Partial<Omit<SelectedDatasetType, 'datasetId'>>;
......@@ -27,9 +20,6 @@ const escapeXml = (value: string) =>
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
export const filterAgentLoopCoreDocumentFiles = (files: AgentLoopCoreInputFile[] = []) =>
files.filter((file) => file.type === ChatFileTypeEnum.file);
/**
* 生成当前输入文件的模型可见提示。
* 文档可通过 read_files 读取;其他多模态文件同时保留 URL,供模型或工具引用。
......@@ -45,10 +35,10 @@ export const buildAgentLoopCoreInputFilesPrompt = (files: AgentLoopCoreInputFile
${files
.map(
(file) => `<file>
<id>${escapeXml(file.id)}</id>
<name>${escapeXml(file.name)}</name>
<type>${escapeXml(file.type)}</type>
<url>${escapeXml(file.url)}</url>
${file.sandboxPath ? `<sandboxPath>${escapeXml(file.sandboxPath)}</sandboxPath>` : ''}
</file>`
)
.join('\n')}`;
......@@ -134,6 +124,13 @@ ${currentWorkingDirectory ? `当前 sandbox 工作目录: ${currentWorkingDirect
* 生成当前用户消息内的动态上下文提醒。
* 该内容是 user message 的一部分,不是 system role prompt;历史消息只应注入稳定文件片段。
*/
export type AgentLoopCoreUserReminderContext = {
skillInfos?: DeployedSkillInfo[];
selectedDataset?: AgentLoopCoreSelectedDatasetContext[];
currentTime?: string;
currentWorkingDirectory?: string;
};
export const buildAgentLoopCoreUserReminderInput = ({
query = '',
skillInfos,
......@@ -141,13 +138,9 @@ export const buildAgentLoopCoreUserReminderInput = ({
selectedDataset,
currentTime,
currentWorkingDirectory
}: {
}: AgentLoopCoreUserReminderContext & {
query?: string;
skillInfos?: DeployedSkillInfo[];
filesInfo?: AgentLoopCoreInputFile[];
selectedDataset?: AgentLoopCoreSelectedDatasetContext[];
currentTime?: string;
currentWorkingDirectory?: string;
}) => {
const reminder = [
buildAgentLoopCoreSkillsPrompt(skillInfos),
......
......@@ -18,7 +18,7 @@ export type AgentLoopCoreSystemToolNodeResponseInput = Omit<
export type AgentLoopCoreParsedReadFileCall =
| {
success: true;
ids: string[];
urls: string[];
}
| {
success: false;
......@@ -27,14 +27,12 @@ export type AgentLoopCoreParsedReadFileCall =
};
export type AgentLoopCoreReadFileItem = {
id: string;
name?: string;
url: string;
};
export type CreateAgentLoopCoreReadFileExecutorParams = {
enabled: boolean;
resolveFiles: (ids: string[]) => AgentLoopCoreReadFileItem[];
execute: (params: { callId: string; files: AgentLoopCoreReadFileItem[] }) => Promise<{
response: string;
usages?: ChatNodeUsageType[];
......@@ -74,19 +72,18 @@ export const parseAgentLoopCoreReadFileCall = (
return {
success: true,
ids: toolParams.data.ids
urls: toolParams.data.urls
};
};
/**
* 创建 read_files system tool 执行器。
*
* core 统一处理 LLM 参数校验、文件 id 到文件列表的结构转换入口、usage 默认值和
* core 统一处理 LLM URL 参数校验、文件列表结构转换、usage 默认值和
* nodeResponse 运行字段补齐;真实文件来源和解析服务仍由 Workflow Agent/ToolCall 外壳注入。
*/
export const createAgentLoopCoreReadFileExecutor = ({
enabled,
resolveFiles,
execute
}: CreateAgentLoopCoreReadFileExecutorParams): AgentLoopReadFileExecutor | undefined => {
if (!enabled) return undefined;
......@@ -98,7 +95,7 @@ export const createAgentLoopCoreReadFileExecutor = ({
const startTime = Date.now();
const result = await execute({
callId: call.id,
files: resolveFiles(toolParams.ids)
files: toolParams.urls.map((url) => ({ url }))
});
const usages = result.usages ?? [];
......
......@@ -9,6 +9,7 @@ export type CreateAgentLoopCoreSystemToolsParams = {
sandboxClient?: SandboxClient;
readFile?: {
enabled: boolean;
maxFileAmount: number;
execute: AgentLoopReadFileExecutor;
};
datasetSearch?: {
......@@ -49,6 +50,7 @@ export const createAgentLoopCoreSystemTools = ({
? {
readFile: {
enabled: true,
maxFileAmount: readFile.maxFileAmount,
execute: readFile.execute
}
}
......
......@@ -81,8 +81,8 @@ export const formatAgentLoopCoreToolResponse = (toolResponses: any) => {
};
/**
* 这些 runtime edge/node 是为一次 tool workflow 派生出的副本。
* 初始化阶段直接在副本上标记入口状态,避免后续调度还要维护额外的 entry 集合。
* 初始化阶段直接在传入的 runtime edge/node 上标记入口状态。
* 调用方需要根据自身的状态隔离要求决定是否先创建副本。
*/
export const initAgentLoopCoreWorkflowToolEdges = (
edges: RuntimeEdgeItemType[],
......@@ -265,6 +265,9 @@ export const createAgentLoopCoreWorkflowSystemToolExecutor = <TChildrenResponse
*
* ToolCall 和未来的简化 Agent 都可以把“某个 runtime node 作为工具入口运行”的能力交给这里。
* 节点外壳只负责提供实际 runWorkflow 函数、展示信息和缓存/流式回调。
*
* 已知问题:普通 Workflow Tool 的首次执行和交互恢复都会复用并修改父流程的 runtime graph。
* 当前保留原有状态传递行为,后续需要通过子流程快照和显式同步完成隔离。
*/
export const createAgentLoopCoreWorkflowToolRunner = <TChildrenResponse = unknown>({
runtimeNodes,
......
......@@ -10,7 +10,6 @@ import type {
AgentLoopToolExecutionResult
} from '../../../../../ai/llm/agentLoop/interface';
import type { AgentLoopCoreSystemToolInfo } from './toolInfo';
import type { SandboxFileRef } from '@fastgpt/global/core/ai/sandbox/type';
export type AgentLoopCoreUserToolInfo<TRaw = unknown> = {
type: 'user';
......@@ -35,7 +34,6 @@ export type AgentLoopCoreToolRunResult<TChildrenResponse = unknown> = {
interactive?: TChildrenResponse;
stop?: boolean;
errorMessage?: string;
fileRefs?: SandboxFileRef[];
nodeResponse?: ChatHistoryItemResType;
};
......@@ -63,7 +61,6 @@ export const normalizeAgentLoopCoreToolRunResult = <TChildrenResponse = unknown>
interactive,
stop = false,
errorMessage,
fileRefs,
nodeResponse
}: AgentLoopCoreToolRunResult<TChildrenResponse>): AgentLoopToolExecutionResult<TChildrenResponse> => {
return {
......@@ -73,7 +70,6 @@ export const normalizeAgentLoopCoreToolRunResult = <TChildrenResponse = unknown>
interactive,
stop,
errorMessage,
fileRefs,
metadata: nodeResponse
};
};
......@@ -5,12 +5,16 @@ export {
} from '../application/run';
export { buildAgentLoopCoreInput } from '../application/context/input';
export {
rewriteAgentLoopCoreUserMessageWithFiles,
type AgentLoopCoreInputFile
} from '../application/context/files';
export { readAgentLoopCoreActivePlan } from '../application/context/activePlan';
export { buildAgentLoopCoreRequestMessages } from '../application/context/messages';
export { buildAgentLoopCoreSystemPrompt } from '../application/context/prompt';
export {
buildAgentLoopCoreUserReminderInput,
type AgentLoopCoreInputFile,
type AgentLoopCoreUserReminderContext,
type AgentLoopCoreSelectedDatasetContext,
type AgentLoopCoreSelectedDatasetInput
} from '../application/context/reminder';
......
......@@ -8,8 +8,11 @@ import {
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import type { LLMModelItemType } from '@fastgpt/global/core/ai/model.schema';
import type { ChatDispatchProps } from '../../../types/runtime';
import { parseFileContentFromUrls } from '../../../../chat/fileContext';
import { rewriteChatMessagesWithFiles } from './fileContext';
import {
parseFileContentFromUrls,
rewriteChatMessagesWithFileContext
} from '../../../../chat/fileContext';
import { getWorkflowFileContext } from '../../../utils/context';
/**
* 组装系统提示、历史、当前输入和文件上下文,并按模型上下文限制裁剪为 LLM 消息。
......@@ -23,8 +26,7 @@ export const getChatMessages = async ({
userChatInput,
userFiles,
parseHistoryFiles,
requestOrigin,
maxFiles,
maxFileAmount,
customPdfParse,
usageId,
runningUserInfo
......@@ -40,8 +42,7 @@ export const getChatMessages = async ({
userFiles: UserChatItemFileItemType[];
parseHistoryFiles: boolean;
requestOrigin?: string;
maxFiles: number;
maxFileAmount: number;
customPdfParse?: boolean;
usageId?: string;
runningUserInfo: ChatDispatchProps['runningUserInfo'];
......@@ -66,18 +67,20 @@ export const getChatMessages = async ({
}
];
const messages = await rewriteChatMessagesWithFiles({
const messages = await rewriteChatMessagesWithFileContext({
messages: rawUserMessages,
parseHistoryFiles,
maxFiles: maxFileAmount,
parseFileFn: async (urls) => {
const files = await parseFileContentFromUrls({
urls,
requestOrigin,
maxFiles,
// 每条消息已经按 Workflow 上限截取,这里统一解析去重后的文件集合。
maxFiles: urls.length,
teamId: runningUserInfo.teamId,
tmbId: runningUserInfo.tmbId,
customPdfParse,
usageId
usageId,
fileContext: getWorkflowFileContext()
});
return files.map((file) => ({
......
......@@ -14,6 +14,7 @@ import { getChatMessages } from './chatMessages';
import { getDatasetCiteData } from './datasetCite';
import { getAIChatFileContextConfig, getInputFiles } from './fileContext';
import type { ChatProps, ChatResponse } from './type';
import { getWorkflowFileMaxAmount } from '../../../utils/context';
/**
* 调度 AI Chat 节点:整理引用、历史和文件上下文,调用 LLM,并写入节点响应与用量。
......@@ -117,8 +118,7 @@ export const dispatchChatCompletion = async (props: ChatProps): Promise<ChatResp
userChatInput,
userFiles,
parseHistoryFiles: fileContextConfig.parseHistoryFiles,
requestOrigin,
maxFiles: chatConfig?.fileSelectConfig?.maxFiles || 20,
maxFileAmount: getWorkflowFileMaxAmount(),
customPdfParse: chatConfig?.fileSelectConfig?.customPdfParse,
usageId,
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 type { FlowNodeInputItemType } from '@fastgpt/global/core/workflow/type/io';
import { parseUrlToFileType } from '../../../utils/context';
import { rewriteChatMessagesWithFileContext } from '../../../../chat/fileContext';
import type { ChatMessageFileParser } from './type';
/**
* 根据 AI Chat 节点的文件链接输入计算文件上下文开关。
......@@ -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 { dispatchChatCompletion } from './dispatchChatCompletion';
export {
getAIChatFileContextConfig,
getInputFiles,
rewriteChatMessagesWithFiles
} from './fileContext';
export type { ChatMessageFileParser, ChatProps, ChatResponse } from './type';
export { getAIChatFileContextConfig, getInputFiles } from './fileContext';
export type { ChatProps, ChatResponse } from './type';
......@@ -26,13 +26,3 @@ export type ChatResponse = DispatchNodeResultType<
[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 {
} from '@fastgpt/global/core/chat/adapt';
import type { ChatItemMiniType, UserChatItemFileItemType } from '@fastgpt/global/core/chat/type';
import { SANDBOX_USER_FILES_PATH } from '@fastgpt/global/core/ai/sandbox/constants';
import { formatUserQueryWithFiles, parseFileInfoFromUrls } from '../../../../../chat/fileContext';
import { parseUrlToFileType } from '../../../../utils/context';
import { getWorkflowFileMaxAmount, parseUrlToFileType } from '../../../../utils/context';
import {
rewriteWorkflowAIHistoryMessageWithFiles,
rewriteWorkflowAIUserMessageWithFiles
} from '../../fileContext';
import type { DispatchToolModuleProps, FileInputType } from '../type';
const getUserFilesFromLinks = ({ fileLinks = [] }: { fileLinks?: string[] }) => {
......@@ -22,11 +25,10 @@ export const useToolMessages = async ({
responseChatItemId,
userChatInput,
fileLinks,
parseHistoryFiles,
lastInteractive,
isEntry,
chatConfig,
requestOrigin,
runningUserInfo,
useSandbox
}: {
defaultSystemPrompt?: string;
......@@ -35,95 +37,66 @@ export const useToolMessages = async ({
responseChatItemId?: string;
userChatInput: string;
fileLinks?: string[];
parseHistoryFiles: boolean;
lastInteractive: DispatchToolModuleProps['lastInteractive'];
isEntry?: boolean;
chatConfig: DispatchToolModuleProps['chatConfig'];
requestOrigin?: string;
runningUserInfo: DispatchToolModuleProps['runningUserInfo'];
useSandbox: boolean;
}) => {
const allFiles = new Map<string, FileInputType>();
const currentInputFiles: FileInputType[] = [];
const userFiles = getUserFilesFromLinks({ fileLinks });
const concatenateSystemPrompt = [defaultSystemPrompt, systemPrompt]
.filter(Boolean)
.join('\n\n-----\n\n');
const isInteractiveResume = !!lastInteractive && !!isEntry;
const value: ChatItemMiniType[] = [
const historyMessages: ChatItemMiniType[] = [
...getSystemPrompt_ChatItemType(concatenateSystemPrompt),
...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
]
: [])
...chatHistories
];
const runtimeMessages = value;
const maxFiles = chatConfig?.fileSelectConfig?.maxFiles || 20;
const maxFileAmount = getWorkflowFileMaxAmount();
/**
* 文件链接会被替换成供 LLM 调用 read_file 的 id。
* 文件链接会作为 model URL 暴露给 LLM,供 read_files 和其他工具直接使用。
* 当前轮输入文件额外记录下来,用于 sandbox 场景先上传到隔离目录。
*/
const messages = await Promise.all(
runtimeMessages.map(async (message, index): Promise<ChatItemMiniType> => {
if (message.obj !== ChatRoleEnum.Human) {
return message;
}
/**
* 重写 human query,把文件 URL 替换成 read_file 可识别的文件 id。
*/
const prefixId = message.dataId || `${index}`;
const query = await formatUserQueryWithFiles({
userQuery: message.value,
parseFileFn: async (urls) => {
const files = await parseFileInfoFromUrls({
urls,
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
};
})
const transformFiles = useSandbox
? (files: FileInputType[]) =>
files.map((file) => ({
...file,
sandboxPath: `${SANDBOX_USER_FILES_PATH}${file.name}`
}))
: undefined;
const messages = historyMessages.map(
(message) =>
rewriteWorkflowAIHistoryMessageWithFiles({
message,
maxFileAmount,
parseHistoryFiles,
transformFiles
}).message
);
// 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 {
messages,
allFiles,
currentInputFiles
};
};
......@@ -10,6 +10,7 @@ import { useToolNodeList } from './hooks/useToolNodeList';
import { useToolMessages } from './hooks/useToolMessages';
import { checkTeamSandboxPermission } from '../../../../../support/permission/teamLimit';
import { prepareSandboxToolRuntime } from '../../../../ai/sandbox/interface/toolCall';
import { readWorkflowFileBuffer } from '../../../utils/context';
import {
createAgentSandboxPermissionDeniedError,
getRunningSandboxId,
......@@ -36,7 +37,6 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
runtimeNodes,
runtimeEdges,
histories,
requestOrigin,
chatConfig,
lastInteractive,
runningUserInfo,
......@@ -75,10 +75,8 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
const useVideo = aiChatVideo && toolModel.video;
const chatHistories = getAgentLoopHistories(history, histories);
const fileUrlInput = inputs.find((item) => item.key === NodeInputKeyEnum.fileUrlList);
const fileLinks =
!fileUrlInput || !fileUrlInput.value || fileUrlInput.value.length === 0
? undefined
: rawFileLinks;
const parseHistoryFiles = !!fileUrlInput?.value?.length;
const fileLinks = parseHistoryFiles ? rawFileLinks : undefined;
props.params.aiChatVision = aiChatVision && toolModel.vision;
props.params.aiChatAudio = useAudio;
......@@ -97,18 +95,17 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
// 交互恢复入口会由子工具继续接管,父 ToolCall 节点本轮不再作为入口节点。
props.node.isEntry = false;
const { messages, allFiles, currentInputFiles } = await useToolMessages({
const { messages, currentInputFiles } = await useToolMessages({
defaultSystemPrompt: toolModel.defaultSystemChatPrompt,
systemPrompt,
chatHistories,
responseChatItemId,
userChatInput,
fileLinks,
parseHistoryFiles,
lastInteractive,
isEntry,
chatConfig,
requestOrigin,
runningUserInfo,
useSandbox
});
......@@ -127,6 +124,7 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
sourceId: props.runningAppInfo.sourceId,
userId: props.uid,
chatId: props.chatId,
readInputFile: (url) => readWorkflowFileBuffer({ url }),
files: currentInputFiles.map((file) => ({
path: file.sandboxPath!,
url: file.url
......@@ -173,7 +171,6 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
return runToolCall({
...props,
allFiles,
currentInputFiles,
sandboxClient,
runtimeNodes,
......
......@@ -33,8 +33,7 @@ type ResponseType = {
};
export const runToolCall = async (props: DispatchToolModuleProps): Promise<ResponseType> => {
const { messages, toolNodes, toolModel, childrenInteractiveParams, allFiles, ...workflowProps } =
props;
const { messages, toolNodes, toolModel, childrenInteractiveParams, ...workflowProps } = props;
const {
checkIsStopping,
runtimeNodes,
......@@ -85,8 +84,6 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo
workflowProps,
runtimeNodes,
runtimeEdges,
allFiles,
fileUrlList,
cacheToolFlowResponse: runtimeEnvironment.cacheToolFlowResponse
});
getProviderToolInfo = toolProvider.getToolInfo;
......@@ -134,6 +131,7 @@ export const runToolCall = async (props: DispatchToolModuleProps): Promise<Respo
readFile: toolProvider.readFileExecutor
? {
enabled: true,
maxFileAmount: toolProvider.readFileMaxFileAmount,
execute: toolProvider.readFileExecutor
}
: undefined,
......
......@@ -13,7 +13,8 @@ import {
import { runWorkflow } from '../../../index';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
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: {
callId: string;
......@@ -35,8 +36,6 @@ export const createToolCallToolProvider = async ({
workflowProps,
runtimeNodes,
runtimeEdges,
allFiles,
fileUrlList,
cacheToolFlowResponse
}: {
messages: DispatchToolModuleProps['messages'];
......@@ -45,17 +44,10 @@ export const createToolCallToolProvider = async ({
lang: DispatchToolModuleProps['lang'];
workflowProps: Omit<
DispatchToolModuleProps,
| 'messages'
| 'toolNodes'
| 'toolModel'
| 'childrenInteractiveParams'
| 'allFiles'
| 'currentInputFiles'
'messages' | 'toolNodes' | 'toolModel' | 'childrenInteractiveParams' | 'currentInputFiles'
>;
runtimeNodes: DispatchToolModuleProps['runtimeNodes'];
runtimeEdges: DispatchToolModuleProps['runtimeEdges'];
allFiles: DispatchToolModuleProps['allFiles'];
fileUrlList?: string[];
cacheToolFlowResponse: CacheToolFlowResponse;
}): Promise<ToolCallToolProvider> => {
const { finalMessages, tools, getToolInfo } = await useToolCatalog({
......@@ -94,38 +86,18 @@ export const createToolCallToolProvider = async ({
runWorkflowTool,
cacheToolFlowResponse
});
const readFileMaxFileAmount = getWorkflowFileMaxAmount();
const readFileExecutor = createAgentLoopCoreReadFileExecutor({
enabled: allFiles.size > 0,
resolveFiles: (ids) =>
ids.map((id) => {
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({
enabled: true,
execute: async ({ files }) =>
dispatchWorkflowReadFiles({
files,
toolCallId: callId,
teamId: workflowProps.runningUserInfo.teamId,
tmbId: workflowProps.runningUserInfo.tmbId,
customPdfParse: workflowProps.chatConfig?.fileSelectConfig?.customPdfParse,
usageId: workflowProps.usageId
});
return {
response: result.response,
usages: result.usages,
nodeResponse: result.flowResponse.flowResponses[0]
};
}
usageId: workflowProps.usageId,
maxFileAmount: readFileMaxFileAmount
})
});
return {
......@@ -137,6 +109,7 @@ export const createToolCallToolProvider = async ({
params: AgentLoopInteractiveToolExecuteParams<WorkflowInteractiveResponseType>
) => runInteractiveTool(params),
readFileExecutor,
readFileMaxFileAmount,
datasetSearchExecutor:
datasetSearchNodeIds.length > 0
? createAgentLoopCoreWorkflowSystemToolExecutor({
......
......@@ -13,5 +13,6 @@ export type ToolCallToolProvider = AgentLoopCoreToolProvider<
finalMessages: ChatCompletionMessageParam[];
getToolInfo: (name: string) => ToolInfo | undefined;
readFileExecutor?: AgentLoopReadFileExecutor;
readFileMaxFileAmount: number;
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';
import type { ReasoningEffort } from '@fastgpt/global/core/ai/llm/type';
import type { AgentLoopChildrenInteractiveParams } from '../../../../ai/llm/agentLoop/interface';
import type { SandboxClient } from '../../../../ai/sandbox/interface/runtime';
import type { AgentLoopCoreInputFile } from '../agentLoopCore/interface';
export type DispatchToolModuleProps = ModuleDispatchProps<{
[NodeInputKeyEnum.history]?: ChatItemMiniType[];
......@@ -37,7 +38,6 @@ export type DispatchToolModuleProps = ModuleDispatchProps<{
toolNodes: ToolNodeItemType[];
toolModel: LLMModelItemType;
childrenInteractiveParams?: AgentLoopChildrenInteractiveParams<WorkflowInteractiveResponseType>;
allFiles: Map<string, FileInputType>;
currentInputFiles: FileInputType[];
sandboxClient?: SandboxClient;
};
......@@ -54,9 +54,4 @@ export type ToolNodeItemType = {
toolParams: RuntimeNodeItemType['inputs'];
};
export type FileInputType = {
id: string;
name: string;
url: string;
sandboxPath?: string;
};
export type FileInputType = AgentLoopCoreInputFile;
......@@ -13,13 +13,13 @@ import type { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
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 type { DispatchNodeResultType, ModuleDispatchProps } from '../../types/runtime';
import { authAppByTmbId } from '../../../../support/permission/app/auth';
import { ReadPermissionVal } from '@fastgpt/global/support/permission/constant';
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 { getRunningUserInfoByTmbId } from '../../../../support/user/team/utils';
import { getRuntimeNodeResponseSummary } from '../utils';
......@@ -95,29 +95,6 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {
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 =
lastInteractive?.type === 'childrenInteractive'
? lastInteractive.params.childrenResponse
......@@ -135,6 +112,19 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {
? query
: 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 {
flowUsages,
assistantResponses,
......@@ -143,37 +133,63 @@ export const dispatchRunAppNode = async (props: Props): Promise<Response> => {
system_memories,
customFeedbacks,
runtimeNodeResponseSummary
} = await 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: chatHistories,
variableState: childrenVariableState,
} = await runWithDerivedWorkflowFileContext({
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,
value: query
value: filteredChildQuery
},
{
obj: ChatRoleEnum.AI,
......
......@@ -49,16 +49,14 @@ import { checkTeamAIPoints } from '../../../support/permission/teamLimit';
import type { UsageSourceEnum } from '@fastgpt/global/support/wallet/usage/constants';
import { createChatUsageRecord, pushChatItemUsage } from '../../../support/wallet/usage/controller';
import type { RequireOnlyOne } from '@fastgpt/global/common/type/utils';
import { createChatFilePreviewUrlGetter } from '../../../common/s3/sources/chat';
import { addPreviewUrlToChatItems } from '../../chat/utils';
import { TeamErrEnum } from '@fastgpt/global/common/error/code/team';
import { i18nT } from '@fastgpt/global/common/i18n/utils';
import { validateFileUrlDomain } from '../../../common/security/fileUrlValidator';
import { classifyEdgesByDFS, findSCCs, isNodeInCycle, getEdgeType } from '../utils/tarjan';
import { observeWorkflowRun, observeWorkflowStep } from '../metrics';
import { withActiveSpan } from '../../../common/tracing';
import { delAgentRuntimeStopSign, shouldWorkflowStop } from './workflowStatus';
import { buildQueryUrlFileMap, buildQueryUrlTypeMap, runWithContext } from '../utils/context';
import { runWithContext } from '../utils/context';
import { createClientAbortTracker } from './utils/clientAbort';
import type { IncomingMessage } from 'node:http';
import { getNodeResponseChildResponseCount } from '../../chat/nodeResponseStorage';
......@@ -77,6 +75,7 @@ import {
import { getWorkflowNodeRunParams } from './utils/runtime';
import type { AgentSandboxPrepareAction } from './ai/agent/sub/sandbox';
import { getWorkflowSource } from './utils/source';
import { prepareWorkflowFileContext } from '../utils/fileContext';
const logger = getLogger(LogCategories.MODULE.WORKFLOW.DISPATCH);
......@@ -97,6 +96,10 @@ type Props = Omit<
defaultSkipNodeQueue?: WorkflowDebugResponse['skipNodeQueue'];
nodeResponseWriteConfig: WorkflowNodeResponseWriteConfig;
agentSandboxPrepareActions?: AgentSandboxPrepareAction[];
/** 已在持久化前计算完成的用户级 Workflow 文件数量上限。 */
maxFileAmount: number;
/** 已按团队配置优先、系统配置兜底计算完成的单文件读取上限。 */
maxBytesPerFile: number;
};
type NodeResponseType = DispatchNodeResultType<{
[key: string]: any;
......@@ -134,7 +137,6 @@ export async function dispatchWorkFlow({
apiVersion
} = data;
const responseChatItemId = data.responseChatItemId;
if (stream && res && !isWorkflowSseResponseInitialized(res)) {
// HTTP SSE 响应必须由调用入口提前初始化,dispatch 只执行 workflow,不隐式管理响应协议。
return Promise.reject(
......@@ -143,26 +145,38 @@ export async function dispatchWorkFlow({
}
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 */
// Check point
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),
(() => {
if (lastInteractive?.usageId) {
......@@ -186,13 +200,8 @@ export async function dispatchWorkFlow({
}
return usageId;
})(),
// Add preview url to chat items
addPreviewUrlToChatItems(histories, 'chatFlow'),
// Add preview url to query
...query.map(async (item) => {
if (!item.file?.key) return;
item.file.url = await getPreviewUrl(item.file.key);
}),
// 复用 Context 的鉴权和请求级签名缓存,刷新 history 里其它服务端文件引用。
addPreviewUrlToChatItems(preparedHistories, 'chatFlow', getHistoryPreviewUrl),
// Remove stopping sign
delAgentRuntimeStopSign({
...chatSource,
......@@ -203,20 +212,25 @@ export async function dispatchWorkFlow({
const clientAbortTracker =
apiVersion === 'v1' ? createClientAbortTracker({ req: data.req, res }) : undefined;
// 私有文件已恢复为无后缀短链;进入节点调度前保留类型和原始文件名等媒体元数据。
const queryUrlTypeMap = buildQueryUrlTypeMap(query);
const queryUrlFileMap = buildQueryUrlFileMap(query);
const variableState = await WorkflowVariableState.create({
timezone,
runningAppInfo,
uid: data.uid,
chatId,
responseChatItemId,
histories,
histories: runtimeHistories,
variablesConfig: data.chatConfig?.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,说明不会有暂停)
......@@ -262,9 +276,9 @@ export async function dispatchWorkFlow({
return new Promise((resolve, reject) => {
runWithContext(
{
queryUrlTypeMap,
queryUrlFileMap,
mcpClientMemory: {}
mcpClientMemory: {},
fileContext,
fileRegistrar
},
(ctx) => {
runWorkflow({
......@@ -272,8 +286,8 @@ export async function dispatchWorkFlow({
responseChatItemId,
nodeResponseSink,
checkIsStopping,
query,
histories,
query: runtimeQuery,
histories: runtimeHistories,
timezone,
externalProvider,
variableState,
......
......@@ -8,8 +8,16 @@ import { FlowNodeInputTypeEnum } from '@fastgpt/global/core/workflow/node/consta
import { anyValueDecrypt } from '../../../../common/secret/utils';
import { getLogger, LogCategories } from '../../../../common/logger';
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 DEFAULT_FORM_FILE_INPUT_MAX_FILES = 5;
type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.description]: string;
......@@ -24,14 +32,38 @@ type FormInputResponse = DispatchNodeResultType<{
const isFileSelectObject = (value: unknown): value is { key?: unknown; url?: unknown } =>
!!value && typeof value === 'object' && !Array.isArray(value);
const formatFileSelectRuntimeValue = async (
value: unknown,
getPreviewUrl: (key: string) => Promise<string>
) => {
const formatFileSelectRuntimeValue = async ({
value,
maxFiles,
getPreviewUrl,
fileRegistrar
}: {
value: unknown;
maxFiles?: number;
getPreviewUrl: (key: string) => Promise<string>;
fileRegistrar?: WorkflowFileRegistrar;
}) => {
if (!Array.isArray(value)) return value;
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 (!isFileSelectObject(file)) return;
......@@ -84,6 +116,9 @@ export const dispatchFormInput = async (props: Props): Promise<FormInputResponse
})();
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 userInputEntries = await Promise.all(
Object.entries(rawUserInputVal).map(async ([key, value]) => {
......@@ -93,7 +128,19 @@ export const dispatchFormInput = async (props: Props): Promise<FormInputResponse
return [key, anyValueDecrypt(value)] as const;
}
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;
})
......
......@@ -31,9 +31,14 @@ import type { AppToolRuntimeType } from '@fastgpt/global/core/app/tool/type';
import { anyValueDecrypt } from '../../../../common/secret/utils';
import { getAppVersionById } from '../../../app/version/controller';
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 { getRuntimeNodeResponseSummary } from '../utils';
import { runWithDerivedWorkflowFileContext } from '../../utils/context';
type RunPluginProps = ModuleDispatchProps<{
[NodeInputKeyEnum.forbidStream]?: boolean;
......@@ -149,17 +154,18 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
associatedPluginId: systemTool.associatedPluginId
};
}
const childWorkflowTool = workflowTool;
const outputFilterMap =
workflowTool.nodes
childWorkflowTool.nodes
.find((node) => node.flowNodeType === FlowNodeTypeEnum.pluginOutput)
?.inputs.reduce<Record<string, boolean>>((acc, cur) => {
acc[cur.key] = cur.isToolOutput === false ? false : true;
return acc;
}, {}) ?? {};
const runtimeNodes = storeNodes2RuntimeNodes(
workflowTool.nodes,
getWorkflowEntryNodeIds(workflowTool.nodes)
childWorkflowTool.nodes,
getWorkflowEntryNodeIds(childWorkflowTool.nodes)
).map((node) => {
// Update workflowTool input value
if (node.flowNodeType === FlowNodeTypeEnum.pluginInput) {
......@@ -194,26 +200,29 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
const { externalProvider } = await getUserChatInfo(runningAppInfo.tmbId);
const childRunningAppInfo = {
sourceType: ChatSourceTypeEnum.app,
sourceId: String(workflowTool.id),
name: workflowTool.name,
teamId: workflowTool.teamId || runningAppInfo.teamId,
tmbId: workflowTool.tmbId || runningAppInfo.tmbId,
sourceId: String(childWorkflowTool.id),
name: childWorkflowTool.name,
teamId: childWorkflowTool.teamId || runningAppInfo.teamId,
tmbId: childWorkflowTool.tmbId || runningAppInfo.tmbId,
isChildApp: true
};
const childVariableState = await WorkflowVariableState.create({
timezone: props.timezone,
runningAppInfo: childRunningAppInfo,
uid: props.uid,
chatId: props.chatId,
responseChatItemId: props.responseChatItemId,
histories: props.histories,
variablesConfig: workflowTool.chatConfig?.variables ?? [],
inputVariables: {},
externalVariables: externalProvider?.externalWorkflowVariables,
sourceVariableState: props.variableState
});
const runtimeVariables = childVariableState.toRuntimeRecord();
const shouldStoreChildNodeResponses = !workflowTool.associatedPluginId;
const childFileInputs = [
...files,
...getWorkflowFileVariableInputs({
variablesConfig: childWorkflowTool.chatConfig?.variables,
inputVariables: {}
}),
...childWorkflowTool.nodes.flatMap((node) =>
node.flowNodeType === FlowNodeTypeEnum.pluginInput
? node.inputs.flatMap((input) =>
input.renderTypeList.includes(FlowNodeInputTypeEnum.fileSelect)
? getWorkflowFileInputsFromValue(data[input.key] ?? input.value, input.maxFiles)
: []
)
: []
)
];
const shouldStoreChildNodeResponses = !childWorkflowTool.associatedPluginId;
const {
flowUsages,
assistantResponses,
......@@ -221,35 +230,72 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
system_memories,
runtimeNodeResponseSummary,
[DispatchNodeResponseKeyEnum.customFeedbacks]: customFeedbacks
} = await runWorkflow({
...props,
// 系统级 workflow tool 只保留工具节点自身的响应,不展开保存其内部 workflow 详情。
...(shouldStoreChildNodeResponses ? {} : { nodeResponseSink: undefined }),
// Rewrite stream mode
...(system_forbid_stream
? {
stream: false,
workflowStreamResponse: undefined
}
: {}),
runningAppInfo: {
sourceType: ChatSourceTypeEnum.app,
sourceId: String(workflowTool.id),
name: workflowTool.name,
// 如果系统插件有 teamId 和 tmbId,则使用系统插件的 teamId 和 tmbId(管理员指定了插件作为系统插件)
teamId: workflowTool.teamId || runningAppInfo.teamId,
tmbId: workflowTool.tmbId || runningAppInfo.tmbId,
isChildApp: true
},
variableState: childVariableState,
query: serverGetWorkflowToolRunUserQuery({
pluginInputs: getWorkflowToolInputsFromStoreNodes(workflowTool.nodes),
variables: runtimeVariables,
files
}).value,
chatConfig: workflowTool.chatConfig ?? {},
runtimeNodes,
runtimeEdges: storeEdges2RuntimeEdges(workflowTool.edges)
} = await runWithDerivedWorkflowFileContext({
histories: props.histories,
files: childFileInputs,
fn: async ({ resolveInputFile, histories: childHistories, filterFiles }) => {
const childRuntimeNodes = runtimeNodes.map((node) =>
node.flowNodeType === FlowNodeTypeEnum.pluginInput
? {
...node,
inputs: node.inputs.map((input) => ({
...input,
value:
input.renderTypeList.includes(FlowNodeInputTypeEnum.fileSelect) &&
Array.isArray(input.value)
? filterFiles(input.value)
: input.value
}))
}
: node
);
const childVariableState = await WorkflowVariableState.create({
timezone: props.timezone,
runningAppInfo: childRunningAppInfo,
uid: props.uid,
chatId: props.chatId,
responseChatItemId: props.responseChatItemId,
histories: childHistories,
variablesConfig: childWorkflowTool.chatConfig?.variables ?? [],
inputVariables: {},
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({
runtimeNodeResponseSummary
......
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 { FlowNodeInputTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
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<{
[key: string]: any;
......@@ -13,46 +21,64 @@ export type PluginInputResponse = DispatchNodeResultType<{
[key: string]: any;
}>;
type PluginFileItem = {
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);
/** 将 Plugin Input 的 fileSelect 值登记到当前 Workflow 文件上下文并输出模型 URL。 */
export const dispatchPluginInput = async (
props: PluginInputProps
): Promise<PluginInputResponse> => {
const { params, query } = props;
const { params, query, node } = props;
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 中文件类型数据进行处理
* 插件单独运行时,这里会是一个特殊的数组
* 插件调用的话,这个参数是一个 string[] 不会进行处理
* 硬性要求:API 单独调用插件时,要避免这种特殊类型冲突
TODO: 需要 filter max files
* 插件单独运行时,这里会是文件对象数组
* 插件调用时,这个参数可能已经被转换成 string[]
*/
await Promise.all(
Object.keys(params).map(async (key) => {
const val = params[key];
if (Array.isArray(val) && val.every(isPluginFileItem)) {
// 为文件对象重新签发 URL(如果有 key 但没有 url)
await Promise.all(
val.map(async (fileItem) => {
if (fileItem.key && !fileItem.url) {
fileItem.url = await getPreviewUrl(fileItem.key);
}
const maxFiles = fileInputMaxFiles.get(key);
if (maxFiles !== undefined && Array.isArray(val)) {
const fileUrls = await Promise.all(
val.slice(0, maxFiles).map(async (fileItem) => {
const storeValue =
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';
import { getNodeErrResponse } from '../utils';
import { parseFileContentFromUrls } from '../../../chat/fileContext';
import { sliceStrStartEnd } from '@fastgpt/global/common/string/tools';
import { getWorkflowFileContext, getWorkflowFileMaxAmount } from '../../utils/context';
type Props = ModuleDispatchProps<{
[NodeInputKeyEnum.fileUrlList]: string[];
}>;
type Response = DispatchNodeResultType<{
[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 = (
export const dispatchReadFiles = async (props: Props): Promise<Response> => {
const {
requestOrigin,
runningUserInfo: { teamId, tmbId },
histories,
chatConfig,
......@@ -41,7 +46,7 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => {
params: { fileUrlList = [] },
usageId
} = props;
const maxFiles = chatConfig?.fileSelectConfig?.maxFiles || 20;
const maxFileAmount = getWorkflowFileMaxAmount();
const customPdfParse = chatConfig?.fileSelectConfig?.customPdfParse || false;
// Get files from histories
......@@ -51,12 +56,12 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => {
const readFilesResult = await parseFileContentFromUrls({
// Concat fileUrlList and filesFromHistories; remove not supported files
urls: [...fileUrlList, ...filesFromHistories],
requestOrigin,
maxFiles,
maxFiles: maxFileAmount,
teamId,
tmbId,
customPdfParse,
usageId
usageId,
fileContext: getWorkflowFileContext()
});
const files = readFilesResult.map((item, index) => ({
id: `${index}`,
......@@ -76,7 +81,8 @@ export const dispatchReadFiles = async (props: Props): Promise<Response> => {
[NodeOutputKeyEnum.rawResponse]: readFilesResult.map((item) => ({
filename: item.name,
url: item.url,
text: item.content
text: item.success ? item.content : '',
...(item.success ? {} : { error: item.content })
}))
},
[DispatchNodeResponseKeyEnum.nodeResponse]: {
......
......@@ -4,6 +4,7 @@ import { VariableInputEnum } from '@fastgpt/global/core/workflow/constants';
import type { ChatDispatchProps, WorkflowVariableStateLike } from '../../types/runtime';
import { valueTypeFormat } from '@fastgpt/global/core/workflow/runtime/utils';
import { getSystemTime } from '@fastgpt/global/common/time/timezone';
import { UserError } from '@fastgpt/global/common/error/utils';
import { encryptSecret } from '../../../../common/secret/aes256gcm';
import { anyValueDecrypt } from '../../../../common/secret/utils';
import { createChatFilePreviewUrlGetter } from '../../../../common/s3/sources/chat';
......@@ -13,6 +14,15 @@ import {
assertChatFileRuntimeValue,
normalizeChatFileStoreValue
} 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 {
*/
export type WorkflowVariableStateItem = {
key: string;
config?: VariableItemType;
config?: WorkflowVariableRuntimeConfig;
storeValue: unknown;
runtimeValue: unknown;
runtimeOnly?: boolean;
......@@ -57,6 +67,10 @@ export type WorkflowVariableStateCreateProps = {
inputVariables?: Record<string, unknown>;
// 源变量状态,用于复制变量状态
sourceVariableState?: WorkflowVariableStateLike;
// 根 Workflow 创建状态时尚未进入 AsyncLocalStorage,需要显式传入请求级文件数量限制。
maxFileAmount?: number;
// 只在入口初始化全局文件变量时登记可信文件,节点运行时 set 不得扩展文件 Context。
resolveInputFile?: (file: ChatFileStoreValue) => Promise<string | undefined>;
};
/** 根据变量配置从入参中取值,兼容 API label 入参与前端 key 入参。 */
......@@ -72,18 +86,60 @@ const getVariableInputValue = ({
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 的映射。 */
const fileStoreValuesToRuntimeUrls = async ({
files,
fileMetaMap,
resolveInputFile,
getPreviewUrl = createChatFilePreviewUrlGetter()
}: {
files: ChatFileStoreValue[];
fileMetaMap: Map<string, ChatFileStoreValue>;
resolveInputFile?: (file: ChatFileStoreValue) => Promise<string | undefined>;
getPreviewUrl?: (key: string) => Promise<string>;
}) => {
const urls = await Promise.all(
files.map(async (file) => {
if (resolveInputFile) {
const url = await resolveInputFile(file);
if (!url) return;
fileMetaMap.set(url, file);
return url;
}
if ('key' in file) {
const url = await getPreviewUrl(file.key);
fileMetaMap.set(url, file);
......@@ -121,13 +177,24 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
inputVariables = {},
externalVariables = {},
runtimeOnlyVariables = {},
sourceVariableState
sourceVariableState,
maxFileAmount = getWorkflowFileContext()?.limits.maxFileAmount ??
DEFAULT_VARIABLE_FILE_INPUT_MAX_FILES,
resolveInputFile
}: WorkflowVariableStateCreateProps) {
const state = new WorkflowVariableState(new Map(), new Map(), sourceVariableState);
for (const item of variablesConfig) {
const value = getVariableInputValue({ variables: inputVariables, item });
await state.initConfiguredVariable(item, value);
const config: WorkflowVariableRuntimeConfig = {
...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);
......@@ -177,12 +244,25 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
}
if (config?.type === VariableInputEnum.file) {
const val = value as ChatFileRuntimeValue;
const storeValue = this.runtimeFileValueToStoreValue(assertChatFileRuntimeValue(val));
const runtimeValue = await fileStoreValuesToRuntimeUrls({
files: storeValue,
fileMetaMap: this.fileMetaMap
if (!Array.isArray(value)) throw new UserError('File variable value must be an array');
const files = value.slice(0, config.maxFiles).map((url) => {
if (!isAbsoluteHttpUrl(url)) {
throw new UserError('File variable updates only accept absolute HTTP(S) URLs');
}
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, {
key,
config,
......@@ -251,16 +331,24 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
}
/** 根据变量配置初始化单个用户变量,并完成特殊类型的 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) {
const storeValue = Array.isArray(value)
? this.runtimeFileValueToStoreValue(
assertChatFileRuntimeValue(value as ChatFileRuntimeValueItem[])
assertChatFileRuntimeValue(value as ChatFileRuntimeValueItem[]).slice(
0,
config.maxFiles
)
)
: [];
const runtimeValue = await fileStoreValuesToRuntimeUrls({
files: storeValue,
fileMetaMap: this.fileMetaMap
fileMetaMap: this.fileMetaMap,
resolveInputFile
});
this.state.set(config.key, {
key: config.key,
......@@ -312,7 +400,7 @@ export class WorkflowVariableState implements WorkflowVariableStateLike {
});
}
/** 将 file runtime 值转回 store 值,优先复用已知 URL 对应的原始 metadata。 */
/** 初始化文件变量时恢复原始 store metadata。 */
private runtimeFileValueToStoreValue(value: ChatFileRuntimeValue): ChatFileStoreValue[] {
return value
.map((item) => {
......
......@@ -60,6 +60,8 @@ export type ChatDispatchProps = {
checkIsStopping: () => boolean;
lang?: localeType;
requestOrigin?: string;
/** Workflow 服务端读取单个输入文件的最大字节数;缺省使用系统文件大小限制。 */
maxBytesPerFile?: number;
mode: 'test' | 'chat' | 'debug';
timezone: string;
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({
TRACING_OTEL_SAMPLE_RATIO: NumSchema.min(0).max(1).optional(),
// ==================== 域名与前端 ====================
FE_DOMAIN: UrlSchema.optional().meta({
FE_DOMAIN: UrlSchema.meta({
description:
'前端外部可访问的地址,用于自动补全文件资源路径。例如 https:fastgpt.cn,不能填 localhost。这个值可以不填,不填则发给模型的图片会是一个相对路径,而不是全路径,模型可能伪造Host。'
'客户端访问 FastGPT 时使用的地址(由协议、主机和可选端口组成),用于补全文件资源路径。例如 https://fastgpt.cn;本地开发可使用 http://localhost:3000。'
}),
FILE_DOMAIN: UrlSchema.optional().meta({
description:
......
......@@ -22,6 +22,7 @@ import {
import { preChatRound, type PreChatRoundResult } from '../../../core/chat/utils/prepare';
import { updateChatGenerateStatus } from '../../../core/chat/chatGenerateStatus';
import { dispatchWorkFlow } from '../../../core/workflow/dispatch';
import { prepareWorkflowFileQuery } from '../../../core/workflow/utils/fileLimits';
import { getRunningUserInfoByTmbId } from '../../../support/user/team/utils';
import { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { authOutLinkLimit } from './auth';
......@@ -229,10 +230,19 @@ export async function outlinkInvokeChat<T extends OutlinkAppType>({
// Merge global variables from database
const variables = chatDetail?.variables ?? {};
const {
query: workflowQuery,
maxFileAmount,
maxBytesPerFile
} = await prepareWorkflowFileQuery({
teamId: String(outLinkConfig.teamId),
chatConfig,
query
});
const userContent: UserChatItemType & { dataId?: string } = {
dataId: messageId,
obj: ChatRoleEnum.Human,
value: query
value: workflowQuery
};
const preparedRound = await preChatRound({
...chatSource,
......@@ -273,7 +283,9 @@ export async function outlinkInvokeChat<T extends OutlinkAppType>({
responseChatItemId: preparedRound.responseChatItemId,
variables,
histories,
query: query,
query: workflowQuery,
maxFileAmount,
maxBytesPerFile,
chatConfig,
stream: enableStreaming,
workflowStreamResponse,
......
......@@ -545,33 +545,4 @@ describe('axios.ts', () => {
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', () => {
describe('addEndpointToImageUrl', () => {
beforeEach(() => {
vi.stubEnv('FE_DOMAIN', undefined);
vi.stubEnv('FE_DOMAIN', 'https://example.com');
vi.stubEnv('NEXT_PUBLIC_BASE_URL', undefined);
});
afterEach(() => {
vi.stubEnv('FE_DOMAIN', undefined);
vi.stubEnv('FE_DOMAIN', 'https://example.com');
vi.stubEnv('NEXT_PUBLIC_BASE_URL', undefined);
});
......@@ -399,12 +399,6 @@ describe('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 () => {
vi.stubEnv('FE_DOMAIN', 'https://example.com');
const addEndpointToImageUrl = await loadAddEndpointToImageUrl();
......
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { z } from 'zod';
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');
......@@ -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() {
try {
......@@ -82,3 +84,47 @@ describe('processError zod logging', () => {
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 (
vi.stubEnv('FILE_TOKEN_KEY', strongFileTokenKey);
vi.stubEnv('FILE_DOMAIN', env.fileDomain ?? 'https://files.example.com/');
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');
const [accessLink, downloadAliasSchema, uploadSessionSchema] = await Promise.all([
......
......@@ -17,7 +17,7 @@ describe('fileUrlValidator', () => {
vi.stubEnv('STORAGE_S3_ENDPOINT', undefined);
vi.stubEnv('STORAGE_EXTERNAL_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);
});
......
......@@ -265,7 +265,7 @@ describe('runFastAgentLoop', () => {
id: 'read_file_call',
name: 'read_files',
args: {
ids: ['file_1']
urls: ['https://files.example.com/file_1']
}
}),
text({
......@@ -287,6 +287,7 @@ describe('runFastAgentLoop', () => {
systemTools: {
readFile: {
enabled: true,
maxFileAmount: 20,
execute: executeReadFile
}
},
......@@ -342,7 +343,7 @@ describe('runFastAgentLoop', () => {
id: 'runtime_read_file_call',
name: 'read_files',
args: {
ids: ['file_1']
urls: ['https://files.example.com/file_1']
}
}),
text({
......
......@@ -713,6 +713,7 @@ describe('runPiAgentLoop', () => {
ask: { enabled: true },
readFile: {
enabled: true,
maxFileAmount: 20,
execute: vi.fn()
},
datasetSearch: {
......
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { Readable } from 'node:stream';
const axiosMock = vi.hoisted(() => ({
get: vi.fn()
}));
vi.mock('@fastgpt/service/common/api/axios', () => ({
pickOutboundAxios: vi.fn(() => axiosMock)
}));
vi.mock('@fastgpt/service/common/api/axios', async (importOriginal) => {
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';
......@@ -29,9 +34,11 @@ describe('sandbox file application', () => {
});
it('downloads url files and writes them to sandbox', async () => {
const first = new ArrayBuffer(1);
const second = new ArrayBuffer(2);
axiosMock.get.mockResolvedValueOnce({ data: first }).mockResolvedValueOnce({ data: second });
const first = Buffer.from('a');
const second = Buffer.from('bc');
axiosMock.get
.mockResolvedValueOnce({ data: Readable.from([first]), headers: {} })
.mockResolvedValueOnce({ data: Readable.from([second]), headers: {} });
const sandbox = {
writeFiles: vi.fn(async () => undefined)
};
......@@ -41,15 +48,45 @@ describe('sandbox file application', () => {
{ path: '/workspace/b.txt', url: 'https://example.com/b.txt' }
]);
expect(axiosMock.get).toHaveBeenCalledWith('https://example.com/a.txt', {
responseType: 'arraybuffer'
});
expect(axiosMock.get).toHaveBeenCalledWith('https://example.com/b.txt', {
responseType: 'arraybuffer'
});
expect(axiosMock.get).toHaveBeenCalledWith(
'https://example.com/a.txt',
expect.objectContaining({ responseType: 'stream' })
);
expect(axiosMock.get).toHaveBeenCalledWith(
'https://example.com/b.txt',
expect.objectContaining({ responseType: 'stream' })
);
expect(sandbox.writeFiles).toHaveBeenCalledWith([
{ path: '/workspace/a.txt', data: first },
{ 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