拆分客户/员工工作台,落地登录关怀、待办统计、公告双栏布局;同步接口映射 AI、H5 订单与相关页面优化。 Co-authored-by: Cursor <cursoragent@cursor.com>
145 lines
3.9 KiB
TypeScript
145 lines
3.9 KiB
TypeScript
import { chatAi, getAiSkill, listAiSkills } from '@/api/ai'
|
||
|
||
export interface SkillCard {
|
||
id: string
|
||
name: string
|
||
description: string
|
||
skillPath: string
|
||
}
|
||
|
||
export interface ScriptAiContext {
|
||
scriptType: 'prefix' | 'postfix'
|
||
interfaceName?: string
|
||
interfaceId?: number
|
||
currentScript?: string
|
||
requirement: string
|
||
}
|
||
|
||
export interface ScriptAiGenerateOptions {
|
||
providerKey: string
|
||
model?: string
|
||
}
|
||
|
||
const CODE_BLOCK_RE = /```(?:csharp|cs)?\s*([\s\S]*?)```/i
|
||
const ITFX_SCENE = 'itfxPrefixScript'
|
||
const ITFX_CHAT_SCENE = 'itfx_prefix_script'
|
||
|
||
export async function loadSkillManifest(): Promise<SkillCard[]> {
|
||
try {
|
||
const items = await listAiSkills({
|
||
scene: ITFX_SCENE,
|
||
includeContent: false,
|
||
enabledOnly: true,
|
||
})
|
||
if (items.length) {
|
||
return items.map((s) => ({
|
||
id: s.skillKey,
|
||
name: s.name,
|
||
description: s.description || '',
|
||
skillPath: s.skillKey,
|
||
}))
|
||
}
|
||
} catch {
|
||
/* fall through to static manifest */
|
||
}
|
||
|
||
const resp = await fetch('/skills/manifest.json')
|
||
if (!resp.ok) {
|
||
throw new Error('加载技能卡列表失败')
|
||
}
|
||
return resp.json()
|
||
}
|
||
|
||
export async function loadSkillContent(skillPath: string): Promise<string> {
|
||
// DB skillKey(无 /)或静态路径(/skills/...)
|
||
if (skillPath && !skillPath.includes('/')) {
|
||
try {
|
||
const skill = await getAiSkill({ skillKey: skillPath })
|
||
if (skill?.content?.trim()) return skill.content
|
||
} catch {
|
||
/* fall through */
|
||
}
|
||
}
|
||
|
||
const resp = await fetch(skillPath.startsWith('/') ? skillPath : `/skills/${skillPath}`)
|
||
if (!resp.ok) {
|
||
throw new Error(`加载技能文件失败: ${skillPath}`)
|
||
}
|
||
return resp.text()
|
||
}
|
||
|
||
export function extractCSharpCode(content: string): string {
|
||
const match = content.match(CODE_BLOCK_RE)
|
||
if (match?.[1]) {
|
||
return match[1].trim()
|
||
}
|
||
return content.trim()
|
||
}
|
||
|
||
function buildUserPrompt(ctx: ScriptAiContext): string {
|
||
const scriptLabel = ctx.scriptType === 'prefix' ? '前置脚本 PrefixScript' : '后置脚本 PostfixScript'
|
||
const lines = [
|
||
`请为接口通 InterfaceMap 生成 ${scriptLabel}。`,
|
||
ctx.interfaceName ? `下游接口:${ctx.interfaceName}` : '',
|
||
ctx.interfaceId ? `接口 ID:${ctx.interfaceId}` : '',
|
||
'',
|
||
'## 用户需求',
|
||
ctx.requirement.trim(),
|
||
]
|
||
|
||
if (ctx.currentScript?.trim()) {
|
||
lines.push('', '## 当前脚本(可在其基础上修改)', '```csharp', ctx.currentScript.trim(), '```')
|
||
}
|
||
|
||
lines.push(
|
||
'',
|
||
'## 输出要求',
|
||
'- 只输出一个 ```csharp 代码块',
|
||
'- 代码为 Natasha 可执行的方法体,参数名必须是 obj',
|
||
'- 严格按用户描述的签名算法与文档规则实现,不要默认套用某一服务商',
|
||
'- 禁止 using / class / namespace',
|
||
'- 类型使用全限定名',
|
||
)
|
||
|
||
return lines.filter((line) => line !== '').join('\n')
|
||
}
|
||
|
||
export async function generateItfxScript(
|
||
skillContent: string,
|
||
ctx: ScriptAiContext,
|
||
options: ScriptAiGenerateOptions,
|
||
): Promise<string> {
|
||
if (!options.providerKey?.trim()) {
|
||
throw new Error('请选择 AI 提供商')
|
||
}
|
||
|
||
const scriptRole =
|
||
ctx.scriptType === 'prefix'
|
||
? '前置脚本在 HTTP 请求发出前执行,用于签名、鉴权等。'
|
||
: '后置脚本在收到 HTTP 响应后执行,用于解析、转换响应数据。'
|
||
|
||
const systemPrompt = [
|
||
'你是接口通(itfx / InterfaceForward)Natasha C# 脚本生成助手。',
|
||
scriptRole,
|
||
'严格遵循下方 Skill 规范生成脚本。',
|
||
'',
|
||
skillContent,
|
||
].join('\n')
|
||
|
||
const res = await chatAi({
|
||
providerKey: options.providerKey.trim(),
|
||
model: options.model?.trim() || undefined,
|
||
scene: ITFX_CHAT_SCENE,
|
||
messages: [
|
||
{ role: 'system', content: systemPrompt },
|
||
{ role: 'user', content: buildUserPrompt(ctx) },
|
||
],
|
||
})
|
||
|
||
const content = String(res?.content ?? '').trim()
|
||
if (!content) {
|
||
throw new Error('AI 未返回内容')
|
||
}
|
||
return extractCSharpCode(content)
|
||
}
|