feat: require validation receipts for h5 code runs
This commit is contained in:
@@ -1,6 +1,18 @@
|
||||
export type AgentRunCreateOptions = {
|
||||
toolMode?: 'chat' | 'code';
|
||||
taskType?: string | null;
|
||||
validation?: AgentRunValidation | null;
|
||||
validationInstruction?: string | null;
|
||||
};
|
||||
|
||||
export type AgentRunValidationFile = {
|
||||
path: string;
|
||||
contains?: string;
|
||||
};
|
||||
|
||||
export type AgentRunValidation = {
|
||||
expectedFile?: AgentRunValidationFile | string;
|
||||
expectedFiles?: Array<AgentRunValidationFile | string>;
|
||||
};
|
||||
|
||||
function envFlag(value: unknown): boolean {
|
||||
@@ -38,6 +50,36 @@ const CODE_TASK_PATTERNS = [
|
||||
/(代码|仓库|项目|文件|组件|接口).{0,12}(修改|修复|重构|调试|实现|新增|编写|更新)/,
|
||||
];
|
||||
|
||||
function sanitizeRequestIdForPath(requestId: string): string {
|
||||
const normalized = String(requestId ?? '').trim().replace(/[^a-zA-Z0-9._-]/g, '-');
|
||||
return normalized || 'unknown-request';
|
||||
}
|
||||
|
||||
export function buildAgentRunValidationReceipt(requestId: string): {
|
||||
validation: AgentRunValidation;
|
||||
instruction: string;
|
||||
} {
|
||||
const normalizedRequestId = String(requestId ?? '').trim();
|
||||
const safeRequestId = sanitizeRequestIdForPath(normalizedRequestId);
|
||||
const receiptPath = `.memind/agent-runs/${safeRequestId}.json`;
|
||||
const marker = normalizedRequestId || safeRequestId;
|
||||
return {
|
||||
validation: {
|
||||
expectedFile: {
|
||||
path: receiptPath,
|
||||
contains: marker,
|
||||
},
|
||||
},
|
||||
instruction: [
|
||||
'',
|
||||
'[Memind code-run validation]',
|
||||
`Before finishing, create or update ${receiptPath}.`,
|
||||
`The file must be valid JSON and include this requestId value: ${marker}.`,
|
||||
'Do not skip this receipt even if the user task itself is complete.',
|
||||
].join('\n'),
|
||||
};
|
||||
}
|
||||
|
||||
export function resolveAgentRunOptions(
|
||||
text: string,
|
||||
{
|
||||
@@ -45,19 +87,24 @@ export function resolveAgentRunOptions(
|
||||
forceCode = false,
|
||||
allowAutodetect = agentCodeRunsAutodetectEnabled,
|
||||
userId = null,
|
||||
requestId = null,
|
||||
}: {
|
||||
taskType?: string;
|
||||
forceCode?: boolean;
|
||||
allowAutodetect?: boolean;
|
||||
userId?: string | null;
|
||||
requestId?: string | null;
|
||||
} = {},
|
||||
): AgentRunCreateOptions {
|
||||
if (!agentCodeRunsEnabledForUser(userId)) return {};
|
||||
const normalizedText = String(text ?? '').trim();
|
||||
const shouldUseCode = forceCode || (allowAutodetect && CODE_TASK_PATTERNS.some((pattern) => pattern.test(normalizedText)));
|
||||
if (!shouldUseCode) return {};
|
||||
const receipt = buildAgentRunValidationReceipt(requestId ?? crypto.randomUUID());
|
||||
return {
|
||||
toolMode: 'code',
|
||||
taskType,
|
||||
validation: receipt.validation,
|
||||
validationInstruction: receipt.instruction,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user