Skip to content

Commit

Permalink
H-2206, H-2242: Support entities with multiple types in frontend (#5312)
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranMn authored Nov 1, 2024
1 parent 9f8fc71 commit 8cb1cb2
Show file tree
Hide file tree
Showing 253 changed files with 3,550 additions and 2,212 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ blocks/**/.env
**/dist
**/build
**/target
**/pkg

# vscode config
**/.vscode/settings.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export const inferEntitiesFromContentAction: FlowActionActivity = async ({

return {
localEntityId: localIdToEntityId[proposal.entityId]!,
entityTypeId: entityTypeId as VersionedUrl,
entityTypeIds: [entityTypeId as VersionedUrl],
claims: {
isObjectOf: [],
isSubjectOf: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export const persistEntitiesAction: FlowActionActivity = async ({ inputs }) => {
*/
const entitiesWithDependenciesSortedLast = proposedEntities.toSorted(
(a, b) => {
const isAFileEntity = fileEntityTypeIds.includes(a.entityTypeId);
const isBFileEntity = fileEntityTypeIds.includes(b.entityTypeId);
const isAFileEntity = a.entityTypeIds.some((entityTypeId) =>
fileEntityTypeIds.includes(entityTypeId),
);
const isBFileEntity = b.entityTypeIds.some((entityTypeId) =>
fileEntityTypeIds.includes(entityTypeId),
);
if (isAFileEntity && !isBFileEntity) {
return -1;
} else if (isBFileEntity && !isAFileEntity) {
Expand Down Expand Up @@ -75,7 +79,7 @@ export const persistEntitiesAction: FlowActionActivity = async ({ inputs }) => {
for (const unresolvedEntity of entitiesWithDependenciesSortedLast) {
const {
claims,
entityTypeId,
entityTypeIds,
localEntityId,
properties,
provenance,
Expand All @@ -86,7 +90,7 @@ export const persistEntitiesAction: FlowActionActivity = async ({ inputs }) => {

const entityWithResolvedLinks: ProposedEntityWithResolvedLinks = {
claims,
entityTypeId,
entityTypeIds,
localEntityId,
properties,
propertyMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const persistEntityAction: FlowActionActivity = async ({ inputs }) => {
const createEditionAsDraft = draft ?? false;

const {
entityTypeId,
entityTypeIds,
localEntityId,
claims,
properties,
Expand All @@ -80,7 +80,7 @@ export const persistEntityAction: FlowActionActivity = async ({ inputs }) => {
CreateEntityParameters,
"relationships" | "ownedById" | "draft" | "linkData"
> & { linkData: Entity["linkData"] } = {
entityTypeId,
entityTypeIds,
properties: mergePropertyObjectAndMetadata(properties, propertyMetadata),
linkData,
provenance,
Expand Down Expand Up @@ -114,7 +114,9 @@ export const persistEntityAction: FlowActionActivity = async ({ inputs }) => {
* by looking up the entity type's parents in the graph, rather than
* relying on a hardcoded value.
*/
const isFileEntity = fileEntityTypeIds.includes(entityTypeId);
const isFileEntity = entityTypeIds.some((entityTypeId) =>
fileEntityTypeIds.includes(entityTypeId),
);

const fileUrl = isFileEntity
? (properties as Partial<FileProperties>)[
Expand All @@ -134,7 +136,7 @@ export const persistEntityAction: FlowActionActivity = async ({ inputs }) => {
url: fileUrl,
propertyMetadata,
provenance,
entityTypeId,
entityTypeIds,
});

if (getFileEntityFromUrlStatus.status !== "ok") {
Expand Down Expand Up @@ -305,12 +307,15 @@ export const persistEntityAction: FlowActionActivity = async ({ inputs }) => {
includeDrafts: draft,
});

const entityTypeId =
`https://hash.ai/@hash/types/entity-type/${linkType}/v/1` as const;

return LinkEntity.create<T extends "has-subject" ? HasSubject : HasObject>(
graphApiClient,
{ actorId: webBotActorId },
{
draft,
entityTypeId: `https://hash.ai/@hash/types/entity-type/${linkType}/v/1`,
entityTypeIds: [entityTypeId],
ownedById: webId,
provenance: {
...claim.metadata.provenance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const persistFlowActivity = async (
await Entity.create<FlowRun>(graphApiClient, userAuthentication, {
ownedById: webId,
entityUuid: flowRunId,
entityTypeId: systemEntityTypes.flowRun.entityTypeId,
entityTypeIds: [systemEntityTypes.flowRun.entityTypeId],
properties: flowRunProperties,
provenance,
draft: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ const parseAndResolveCoordinatorInputs = async (params: {
graphApiClient,
entityTypeIds: [
...entityTypeIds!,
...(existingEntities?.map(({ metadata }) => metadata.entityTypeId) ?? []),
...(existingEntities?.flatMap(({ metadata }) => metadata.entityTypeIds) ??
[]),
].filter((entityTypeId, index, all) => all.indexOf(entityTypeId) === index),
actorId: userAuthentication.actorId,
});
Expand Down Expand Up @@ -502,7 +503,7 @@ export const runCoordinatingAgent: FlowActionActivity<{
*/
propertyMetadata: { value: {} },
provenance: fileEditionProvenance,
entityTypeId,
entityTypeIds: [entityTypeId],
localEntityId: entityIdFromComponents(
webId,
generateUuid() as EntityUuid,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ export const generateProgressReport = (params: {
.map((proposedEntity) =>
simplifyProposedEntityForLlmConsumption({
proposedEntity,
entityType:
allDereferencedEntityTypesById[proposedEntity.entityTypeId]!,
entityTypes: proposedEntity.entityTypeIds.map(
(entityTypeId) => allDereferencedEntityTypesById[entityTypeId]!,
),
}),
)
.join("\n")}
Expand All @@ -69,8 +70,9 @@ export const generateProgressReport = (params: {
.map((proposedLink) =>
simplifyProposedEntityForLlmConsumption({
proposedEntity: proposedLink,
entityType:
allDereferencedEntityTypesById[proposedLink.entityTypeId]!,
entityTypes: proposedLink.entityTypeIds.map(
(entityTypeId) => allDereferencedEntityTypesById[entityTypeId]!,
),
}),
)
.join("\n")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export const processCompleteToolCall = ({

const missingEntityTypes = input.entityTypes.filter(
({ $id }) =>
!submittedEntities.some(({ entityTypeId }) => entityTypeId === $id),
!submittedEntities.some(({ entityTypeIds }) =>
entityTypeIds.includes($id),
),
);

if (missingEntityTypes.length > 0) {
Expand All @@ -99,7 +101,9 @@ export const processCompleteToolCall = ({

const missingLinkEntityTypes = input.linkEntityTypes?.filter(
({ $id }) =>
!submittedEntities.some(({ entityTypeId }) => entityTypeId === $id),
!submittedEntities.some(({ entityTypeIds }) =>
entityTypeIds.includes($id),
),
);

if (missingLinkEntityTypes && missingLinkEntityTypes.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { simplifyEntity } from "../../../shared/simplify-entity.js";

export type ExistingEntitySummary = {
entityId: EntityId;
entityTypeId: VersionedUrl;
entityTypeIds: [VersionedUrl, ...VersionedUrl[]];
name: string;
summary: string;
};
Expand Down Expand Up @@ -142,7 +142,7 @@ export const summarizeExistingEntities = async (params: {

validEntitySummaries.push({
entityId: existingEntity.metadata.recordId.entityId,
entityTypeId: existingEntity.metadata.entityTypeId,
entityTypeIds: existingEntity.metadata.entityTypeIds,
name,
summary,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ const generateUserMessage = (
<Entities>
Here is the information about entities you have already gathered:
${JSON.stringify(
entitySummaries.map(({ localId, name, summary, entityTypeId }) => {
entitySummaries.map(({ localId, name, summary, entityTypeIds }) => {
const claimsAboutEntity = claimsGathered.filter(
(claim) => claim.subjectEntityLocalId === localId,
);
return {
name,
summary,
entityType: entityTypeId,
entityTypes: entityTypeIds,
claims: JSON.stringify(
claimsAboutEntity.map(simplifyClaimForLlmConsumption),
),
Expand Down
Loading

0 comments on commit 8cb1cb2

Please sign in to comment.