Skip to content

Commit

Permalink
fix: plugin cost (#3533)
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu authored Jan 6, 2025
1 parent 72ed72e commit bb669ca
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/service/common/middle/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import NextCors from 'nextjs-cors';

export async function withNextCors(req: NextApiRequest, res: NextApiResponse) {
const methods = ['GET', 'eHEAD', 'PUT', 'PATCH', 'POST', 'DELETE'];

const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',');
const origin = req.headers.origin;

await NextCors(req, res, {
methods,
origin: origin,
origin: allowedOrigins || origin,
optionsSuccessStatus: 200
});
}
25 changes: 16 additions & 9 deletions packages/service/core/app/plugin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { ChatNodeUsageType } from '@fastgpt/global/support/wallet/bill/type';
import { PluginRuntimeType } from '@fastgpt/global/core/plugin/type';
import { splitCombinePluginId } from './controller';
import { PluginSourceEnum } from '@fastgpt/global/core/plugin/constants';

/*
Plugin points calculation:
1. Return 0 if error
2. Add configured points if commercial plugin
3. Add sum of child nodes points
1. 商业版插件:
- 有错误:返回 0
- 无错误:返回 配置的点数 + 子节点点数
2. 其他插件:
- 返回 子节点点数
*/
export const computedPluginUsage = async ({
plugin,
Expand All @@ -16,13 +20,16 @@ export const computedPluginUsage = async ({
childrenUsage: ChatNodeUsageType[];
error?: boolean;
}) => {
if (error) {
return 0;
}
const { source } = await splitCombinePluginId(plugin.id);
const childrenUsages = childrenUsage.reduce((sum, item) => sum + (item.totalPoints || 0), 0);

if (source !== PluginSourceEnum.personal) {
if (error) return 0;

const childrenIUsages = childrenUsage.reduce((sum, item) => sum + (item.totalPoints || 0), 0);
const pluginCurrentCose = plugin.currentCost ?? 0;

const pluginCurrentCose = plugin.currentCost ?? 0;
return plugin.hasTokenFee ? pluginCurrentCose + childrenUsages : pluginCurrentCose;
}

return plugin.hasTokenFee ? pluginCurrentCose + childrenIUsages : pluginCurrentCose;
return childrenUsages;
};
2 changes: 1 addition & 1 deletion packages/web/i18n/en/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"publish_success": "Publish Successful",
"question_guide_tip": "After the conversation, 3 guiding questions will be generated for you.",
"saved_success": "Saved successfully! \nTo use this version externally, click Save and Publish",
"search_app": "Search Application",
"search_app": "Search apps",
"setting_app": "Workflow",
"setting_plugin": "Workflow",
"simple_tool_tips": "This plugin contains special inputs and is not currently supported for invocation by simple applications.",
Expand Down
9 changes: 8 additions & 1 deletion projects/app/src/pages/api/common/file/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getDownloadStream, getFileById } from '@fastgpt/service/common/file/gri
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
import { stream2Encoding } from '@fastgpt/service/common/file/gridfs/utils';

// Abandoned, use: file/read/[filename].ts
export default async function handler(req: NextApiRequest, res: NextApiResponse<any>) {
try {
await connectToDatabase();
Expand Down Expand Up @@ -37,9 +38,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
return stream2Encoding(fileStream);
})();

const extension = file.filename.split('.').pop() || '';
const disposition = ['html', 'htm'].includes(extension) ? 'attachment' : 'inline';

res.setHeader('Content-Type', `${file.contentType}; charset=${encoding}`);
res.setHeader('Cache-Control', 'public, max-age=31536000');
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(file.filename)}"`);
res.setHeader(
'Content-Disposition',
`${disposition}; filename="${encodeURIComponent(file.filename)}"`
);
res.setHeader('Content-Length', file.length);

stream.pipe(res);
Expand Down
8 changes: 7 additions & 1 deletion projects/app/src/pages/api/common/file/read/[filename].ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
return stream2Encoding(fileStream);
})();

const extension = file.filename.split('.').pop() || '';
const disposition = ['html', 'htm'].includes(extension) ? 'attachment' : 'inline';

res.setHeader('Content-Type', `${file.contentType}; charset=${encoding}`);
res.setHeader('Cache-Control', 'public, max-age=31536000');
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(filename)}"`);
res.setHeader(
'Content-Disposition',
`${disposition}; filename="${encodeURIComponent(filename)}"`
);
res.setHeader('Content-Length', file.length);

stream.pipe(res);
Expand Down

0 comments on commit bb669ca

Please sign in to comment.