feat: complete mindspace conversation packages
This commit is contained in:
@@ -16,6 +16,9 @@ export const CONVERSATION_ARTIFACT_KINDS = Object.freeze([
|
||||
'pdf',
|
||||
]);
|
||||
|
||||
const MYSQL_SIGNED_INT_MAX = 2147483647;
|
||||
const MYSQL_SIGNED_INT_MIN = -2147483648;
|
||||
|
||||
function requiredString(value, field) {
|
||||
const normalized = String(value ?? '').trim();
|
||||
if (!normalized) {
|
||||
@@ -38,6 +41,18 @@ function optionalNumber(value) {
|
||||
return Number.isFinite(number) ? number : null;
|
||||
}
|
||||
|
||||
function safeSortOrder(value) {
|
||||
const number = Number(value);
|
||||
if (!Number.isFinite(number)) return 0;
|
||||
let normalized = Math.trunc(number);
|
||||
if (normalized > MYSQL_SIGNED_INT_MAX && Math.abs(normalized) >= 1_000_000_000_000) {
|
||||
normalized = Math.trunc(normalized / 1000);
|
||||
}
|
||||
if (normalized > MYSQL_SIGNED_INT_MAX) return MYSQL_SIGNED_INT_MAX;
|
||||
if (normalized < MYSQL_SIGNED_INT_MIN) return MYSQL_SIGNED_INT_MIN;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function safeStorageSegment(value, field) {
|
||||
const normalized = requiredString(value, field);
|
||||
const encoded = encodeURIComponent(normalized);
|
||||
@@ -117,7 +132,7 @@ export function createConversationArtifact(input) {
|
||||
sizeBytes: optionalNumber(input?.sizeBytes),
|
||||
storageKey: optionalString(input?.storageKey),
|
||||
canonicalUrl: optionalString(input?.canonicalUrl),
|
||||
sortOrder: Number.isFinite(Number(input?.sortOrder)) ? Number(input.sortOrder) : 0,
|
||||
sortOrder: safeSortOrder(input?.sortOrder),
|
||||
createdAt: Number.isFinite(Number(input?.createdAt)) ? Number(input.createdAt) : now,
|
||||
};
|
||||
}
|
||||
@@ -163,6 +178,27 @@ function groupedManifestEntries(artifacts, keyField, idField) {
|
||||
return [...groups.values()].sort((a, b) => a.createdAt - b.createdAt || a[idField].localeCompare(b[idField]));
|
||||
}
|
||||
|
||||
function provenanceManifestEntries(artifacts, keyField, idField) {
|
||||
const groups = new Map();
|
||||
for (const artifact of artifacts) {
|
||||
const key = artifact[keyField];
|
||||
if (!key) continue;
|
||||
const existing = groups.get(key) ?? {
|
||||
[idField]: key,
|
||||
artifactIds: [],
|
||||
kinds: [],
|
||||
createdAt: artifact.createdAt,
|
||||
updatedAt: artifact.createdAt,
|
||||
};
|
||||
existing.artifactIds.push(artifact.id);
|
||||
if (!existing.kinds.includes(artifact.artifactKind)) existing.kinds.push(artifact.artifactKind);
|
||||
existing.createdAt = Math.min(existing.createdAt, artifact.createdAt);
|
||||
existing.updatedAt = Math.max(existing.updatedAt, artifact.createdAt);
|
||||
groups.set(key, existing);
|
||||
}
|
||||
return [...groups.values()].sort((a, b) => a.createdAt - b.createdAt || a[idField].localeCompare(b[idField]));
|
||||
}
|
||||
|
||||
export function buildConversationPackageManifest({ packageRecord, artifacts = [] }) {
|
||||
const record = createConversationPackageRecord(packageRecord);
|
||||
const normalizedArtifacts = artifacts
|
||||
@@ -184,6 +220,8 @@ export function buildConversationPackageManifest({ packageRecord, artifacts = []
|
||||
artifacts: normalizedArtifacts.map(artifactManifestEntry),
|
||||
pages: groupedManifestEntries(normalizedArtifacts, 'pageId', 'pageId'),
|
||||
publications: groupedManifestEntries(normalizedArtifacts, 'publicationId', 'publicationId'),
|
||||
messages: provenanceManifestEntries(normalizedArtifacts, 'messageId', 'messageId'),
|
||||
agentRuns: provenanceManifestEntries(normalizedArtifacts, 'agentRunId', 'agentRunId'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -191,5 +229,7 @@ export const mindspaceConversationPackageInternals = {
|
||||
requiredString,
|
||||
optionalNumber,
|
||||
optionalString,
|
||||
provenanceManifestEntries,
|
||||
safeStorageSegment,
|
||||
safeSortOrder,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user