feat: require validation receipts for h5 code runs

This commit is contained in:
Your Name
2026-07-02 11:52:40 +08:00
parent 80b762053b
commit c0f922b5f8
6 changed files with 185 additions and 21 deletions
+43 -2
View File
@@ -58,7 +58,7 @@ import type {
} from '../types';
import { CHAT_IMAGE_UPLOAD_MAX_INPUT_BYTES } from '../utils/imageUpload';
import { normalizeConversationMessages, normalizeUserMessageForApi } from '../utils/message';
import type { AgentRunCreateOptions } from '../utils/agentRunMode';
import type { AgentRunCreateOptions, AgentRunValidation } from '../utils/agentRunMode';
const API = '/api';
const DEFAULT_API_TIMEOUT_MS = 20_000;
@@ -79,6 +79,46 @@ export type AgentRun = {
completedAt: number | null;
};
function appendAgentRunValidationInstruction(message: Message, instruction?: string | null): Message {
const normalizedInstruction = String(instruction ?? '').trim();
if (!normalizedInstruction) return message;
return {
...message,
content: [
...message.content,
{ type: 'text', text: normalizedInstruction },
],
};
}
function withAgentRunValidationMetadata(
message: Message,
validation?: AgentRunValidation | null,
): Message {
if (!validation) return message;
const metadata = message.metadata as Message['metadata'] & {
memindRun?: Record<string, unknown>;
};
return {
...message,
metadata: {
...message.metadata,
memindRun: {
...(metadata.memindRun && typeof metadata.memindRun === 'object' ? metadata.memindRun : {}),
validation,
},
} as Message['metadata'],
};
}
function prepareAgentRunUserMessage(message: Message, options: AgentRunCreateOptions): Message {
const normalized = normalizeUserMessageForApi(message);
return appendAgentRunValidationInstruction(
withAgentRunValidationMetadata(normalized, options.validation),
options.toolMode === 'code' ? options.validationInstruction : null,
);
}
export class ApiError extends Error {
readonly status: number;
readonly code?: string;
@@ -2250,6 +2290,7 @@ export async function createAgentRun(
userMessage: Message,
options: AgentRunCreateOptions = {},
): Promise<AgentRun> {
const userMessagePayload = prepareAgentRunUserMessage(userMessage, options);
const result = await apiFetch<{ run: AgentRun }>(
AGENT_RUNS_PATH,
{
@@ -2257,7 +2298,7 @@ export async function createAgentRun(
body: JSON.stringify({
session_id: sessionId,
request_id: requestId,
user_message: normalizeUserMessageForApi(userMessage),
user_message: userMessagePayload,
...(options.toolMode ? { tool_mode: options.toolMode } : {}),
...(options.taskType ? { task_type: options.taskType } : {}),
}),