FastGPT/packages/service/core/chat/utils.ts
Archer 58000324e2
feature: V4.14.3 (#5970)
* feat(marketplace): update plugin/ download count statistic (#5957)

* feat: download count

* feat: update ui

* fix: ui

* chore: update sdk verison

* chore: update .env.template

* chore: adjust

* chore: remove console.log

* chore: adjust

* Update projects/marketplace/src/pages/index.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update projects/marketplace/src/pages/index.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update projects/app/src/pages/config/tool/marketplace.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: update refresh; feat: marketplace download count per hour

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* download

* marketplace code

* fix: ui (#5963)

* feat: support dataset and files as global variables (#5961)

* json & dataset

* file

* fix file var

* fix

* fix init

* remove

* perf: file vars

* fix: file uploading errors (#5969)

* fix: file uploading errors

* fix build

* perf: fileselector ux

* feat: integrate S3 for dataset with compatibility (#5941)

* fix: text split

* remove test

* feat: integrate S3 for dataset with compatibility

* fix: delay s3 files delete timing

* fix: remove imageKeys

* fix: remove parsed images' TTL

* fix: improve codes by pr comments

---------

Co-authored-by: archer <545436317@qq.com>

* remove log

* perf: request limit

* chore: s3 migration script (#5971)

* test

* perf: s3 code

* fix: migration script (#5972)

* perf: s3 move object

* wip: fix s3 bugs (#5976)

* fix: incorrect replace origin logic (#5978)

* fix: add downloadURL (#5980)

* perf: file variable ttl & quick create dataset with temp s3 bucket (#5973)

* perf: file variable ttl & quick create dataset with temp s3 bucket

* fix

* plugin & form input variables (#5979)

* plugin & form input variables

* fix

* docs: 4143.mdx (#5981)

* doc: update 4143.mdx (#5982)

* fix form input file ttl (#5983)

* trans file type (#5986)

* trans file type

* fix

* fix: S3 script early return (#5985)

* fix: S3 script typeof

* fix: truncate large filename to fit S3 name

* perf(permission): add a schema verification for resource permission, tmbId, groupId, orgId should be set at least one of them (#5987)

* fix: version & typo (#5988)

* fix-v4.14.3 (#5991)

* fix: empty alt make replace JWT failed & incorrect image dataset preview url (#5989)

* fix: empty alt make replace JWT failed & incorrect image dataset preview url

* fix: s3 files recovery script

* fix: incorrect chat external url parsing (#5993)

---------

Co-authored-by: Finley Ge <32237950+FinleyGe@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: heheer <heheer@sealos.io>
Co-authored-by: Roy <whoeverimf5@gmail.com>
2025-11-26 20:47:28 +08:00

106 lines
3.2 KiB
TypeScript

import { ChatItemValueTypeEnum, ChatRoleEnum } from '@fastgpt/global/core/chat/constants';
import type { ChatItemType } from '@fastgpt/global/core/chat/type';
import { getS3ChatSource } from '../../common/s3/sources/chat';
import type { FlowNodeInputItemType } from '@fastgpt/global/core/workflow/type/io';
import { FlowNodeInputTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import type { VariableItemType } from '@fastgpt/global/core/app/type';
import { VariableInputEnum } from '@fastgpt/global/core/workflow/constants';
import { clone, cloneDeep } from 'lodash';
export const addPreviewUrlToChatItems = async (
histories: ChatItemType[],
type: 'chatFlow' | 'workflowTool'
) => {
async function addToChatflow(item: ChatItemType) {
for await (const value of item.value) {
if (value.type === ChatItemValueTypeEnum.file && value.file && value.file.key) {
value.file.url = await s3ChatSource.createGetChatFileURL({
key: value.file.key,
external: true
});
}
}
}
async function addToWorkflowTool(item: ChatItemType) {
if (item.obj !== ChatRoleEnum.Human || !Array.isArray(item.value)) return;
for (let j = 0; j < item.value.length; j++) {
const value = item.value[j];
if (value.type !== ChatItemValueTypeEnum.text) continue;
const inputValueString = value.text?.content || '';
const parsedInputValue = JSON.parse(inputValueString) as FlowNodeInputItemType[];
for (const input of parsedInputValue) {
if (
input.renderTypeList[0] !== FlowNodeInputTypeEnum.fileSelect ||
!Array.isArray(input.value)
)
continue;
for (const file of input.value) {
if (!file.key) continue;
const url = await getS3ChatSource().createGetChatFileURL({
key: file.key,
external: true
});
file.url = url;
}
}
item.value[j].text = {
...value.text,
content: JSON.stringify(parsedInputValue)
};
}
}
// Presign file urls
const s3ChatSource = getS3ChatSource();
for await (const item of histories) {
if (type === 'chatFlow') {
await addToChatflow(item);
} else if (type === 'workflowTool') {
await addToWorkflowTool(item);
}
}
};
// Presign variables file urls
export const presignVariablesFileUrls = async ({
variables,
variableConfig
}: {
variables?: Record<string, any>;
variableConfig?: VariableItemType[];
}) => {
if (!variables || !variableConfig) return variables;
const cloneVars = cloneDeep(variables);
await Promise.all(
variableConfig.map(async (item) => {
if (item.type === VariableInputEnum.file) {
const val = cloneVars[item.key];
if (Array.isArray(val)) {
cloneVars[item.key] = await Promise.all(
val.map(async (item) => {
if (!item.key) return item;
const url = await getS3ChatSource().createGetChatFileURL({
key: item.key,
external: true
});
return {
...item,
url
};
})
).then((urls) => urls.filter(Boolean));
}
}
})
);
return cloneVars;
};