2026-04-15 03:06:29 +08:00
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
|
|
|
|
import {
|
|
|
|
|
type CommandEnvelope,
|
|
|
|
|
type BridgeContext,
|
|
|
|
|
} from "@/lib/documents/bridge";
|
2026-04-15 20:01:12 +08:00
|
|
|
import {
|
|
|
|
|
recordBridgeCommandFailureArtifacts,
|
2026-04-26 19:35:52 +08:00
|
|
|
recordRustBridgeCommandArtifacts,
|
2026-04-15 20:01:12 +08:00
|
|
|
} from "@/lib/documents/bridge-log";
|
2026-04-15 03:06:29 +08:00
|
|
|
import type { DocumentSavePayload } from "@/lib/documents/save-contract";
|
|
|
|
|
import { DocumentBridgeError } from "@/lib/documents/bridge";
|
2026-04-15 20:01:12 +08:00
|
|
|
import {
|
|
|
|
|
executeRustBridgeMutationTransport,
|
|
|
|
|
resolveRustBridgeCommandPlan,
|
|
|
|
|
} from "@/lib/documents/rust-runtime";
|
2026-04-15 03:06:29 +08:00
|
|
|
|
|
|
|
|
export type DocumentSaveExecutionResult = {
|
|
|
|
|
requestId: string;
|
|
|
|
|
traceId: string;
|
|
|
|
|
commandId: string;
|
|
|
|
|
commandName: string;
|
|
|
|
|
revision: number | null;
|
|
|
|
|
conflictDetectionKey: string | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function executeSaveBridgeCommand(input: {
|
|
|
|
|
context: BridgeContext;
|
|
|
|
|
envelope: CommandEnvelope<DocumentSavePayload>;
|
|
|
|
|
}): Promise<DocumentSaveExecutionResult> {
|
|
|
|
|
const { client } = await getAuthedConvexClient();
|
2026-04-15 20:01:12 +08:00
|
|
|
const plan = await resolveRustBridgeCommandPlan({
|
2026-04-15 03:06:29 +08:00
|
|
|
context: input.context,
|
|
|
|
|
envelope: input.envelope,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let mutationResult;
|
|
|
|
|
try {
|
2026-04-15 20:01:12 +08:00
|
|
|
mutationResult = await executeRustBridgeMutationTransport<{
|
|
|
|
|
revision?: number | null;
|
|
|
|
|
conflict_detection_key?: string | null;
|
|
|
|
|
}>({
|
2026-04-15 03:06:29 +08:00
|
|
|
client,
|
2026-04-15 20:01:12 +08:00
|
|
|
plan,
|
2026-04-15 03:06:29 +08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
2026-04-15 20:01:12 +08:00
|
|
|
await recordBridgeCommandFailureArtifacts({
|
|
|
|
|
client,
|
|
|
|
|
context: input.context,
|
|
|
|
|
envelope: input.envelope,
|
|
|
|
|
error,
|
|
|
|
|
});
|
2026-04-15 03:06:29 +08:00
|
|
|
if (error instanceof Error && /正文(内容已变更|冲突检测失败)/.test(error.message)) {
|
|
|
|
|
throw new DocumentBridgeError(error.message, 409, "REJECTED", {
|
|
|
|
|
reason: "content_conflict",
|
|
|
|
|
revision: input.envelope.payload.revision,
|
|
|
|
|
conflictDetectionKey: input.envelope.payload.conflictDetectionKey,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2026-04-26 19:35:52 +08:00
|
|
|
await recordRustBridgeCommandArtifacts({
|
|
|
|
|
client,
|
2026-04-15 03:06:29 +08:00
|
|
|
context: input.context,
|
|
|
|
|
envelope: input.envelope,
|
2026-04-26 19:35:52 +08:00
|
|
|
plan,
|
|
|
|
|
result: mutationResult,
|
2026-04-15 03:06:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
requestId: input.context.requestId,
|
|
|
|
|
traceId: input.context.traceId,
|
|
|
|
|
commandId: input.envelope.commandId,
|
|
|
|
|
commandName: input.envelope.name,
|
|
|
|
|
revision:
|
|
|
|
|
typeof mutationResult?.revision === "number" && Number.isInteger(mutationResult.revision)
|
|
|
|
|
? mutationResult.revision
|
|
|
|
|
: null,
|
|
|
|
|
conflictDetectionKey:
|
|
|
|
|
typeof mutationResult?.conflict_detection_key === "string" &&
|
|
|
|
|
mutationResult.conflict_detection_key.trim()
|
|
|
|
|
? mutationResult.conflict_detection_key
|
|
|
|
|
: null,
|
|
|
|
|
};
|
|
|
|
|
}
|