-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fccd2cc
commit b9c1044
Showing
4 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
app/(playground)/p/[agentId]/beta-proto/graph/v2/composition/remove-connector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { setConnectors } from "../../../connector/actions"; | ||
import type { ConnectorId } from "../../../connector/types"; | ||
import { giselleNodeArchetypes } from "../../../giselle-node/blueprints"; | ||
import { giselleNodeCategories } from "../../../giselle-node/types"; | ||
import type { CompositeAction } from "../../context"; | ||
import { type Source, removeSource } from "./remove-source"; | ||
|
||
type RemoveConnectorInput = { | ||
connectorId: ConnectorId; | ||
}; | ||
|
||
export function removeConnector({ | ||
input, | ||
}: { input: RemoveConnectorInput }): CompositeAction { | ||
return (dispatch, getState) => { | ||
const removeConnector = getState().graph.connectors.find( | ||
(connector) => connector.id === input.connectorId, | ||
); | ||
if (removeConnector === undefined) { | ||
throw new Error(`Connector not found: ${input.connectorId}`); | ||
} | ||
switch (removeConnector.targetNodeCategory) { | ||
case giselleNodeCategories.action: { | ||
switch (removeConnector.sourceNodeCategory) { | ||
case giselleNodeCategories.action: { | ||
// Execute this process when a connector | ||
// from an action node like TextGenerator | ||
// or WebSearch to another action node is removed. | ||
// There is a Prompt node connected to the target | ||
// action node that instructed the connection, | ||
// hence remove it. | ||
const relevantInstructionConnector = | ||
getState().graph.connectors.find( | ||
(connector) => | ||
connector.target === removeConnector.target && | ||
connector.sourceNodeCategory === | ||
giselleNodeCategories.instruction, | ||
); | ||
if (relevantInstructionConnector === undefined) { | ||
throw new Error( | ||
`Instruction connector not found: ${removeConnector.target}`, | ||
); | ||
} | ||
let source: Source | undefined; | ||
if ( | ||
removeConnector.sourceNodeArcheType === | ||
giselleNodeArchetypes.textGenerator | ||
) { | ||
const removeArtifact = getState().graph.artifacts.find( | ||
(artifact) => | ||
artifact.generatorNode.id === removeConnector.source, | ||
); | ||
if (removeArtifact !== undefined) { | ||
source = { | ||
object: "artifact.reference", | ||
id: removeArtifact.id, | ||
}; | ||
} | ||
} else if ( | ||
removeConnector.sourceNodeArcheType === | ||
giselleNodeArchetypes.webSearch | ||
) { | ||
const webSearch = getState().graph.webSearches.find( | ||
(webSearch) => | ||
webSearch.generatorNode.id === removeConnector.source, | ||
); | ||
if (webSearch !== undefined) { | ||
source = webSearch; | ||
} | ||
} | ||
if (source === undefined) { | ||
throw new Error(`Source not found: ${removeConnector.source}`); | ||
} | ||
|
||
dispatch( | ||
removeSource({ | ||
input: { | ||
nodeId: relevantInstructionConnector.source, | ||
source, | ||
}, | ||
}), | ||
); | ||
break; | ||
} | ||
case giselleNodeCategories.instruction: | ||
dispatch( | ||
setConnectors({ | ||
input: { | ||
connectors: getState().graph.connectors.filter( | ||
(connector) => connector.id !== removeConnector.id, | ||
), | ||
}, | ||
}), | ||
); | ||
break; | ||
} | ||
break; | ||
} | ||
default: | ||
throw new Error("Unexpected target node category detected"); | ||
} | ||
if (removeConnector.targetNodeCategory === giselleNodeCategories.action) { | ||
if (removeConnector.sourceNodeCategory === giselleNodeCategories.action) { | ||
} | ||
} | ||
}; | ||
} |
98 changes: 98 additions & 0 deletions
98
app/(playground)/p/[agentId]/beta-proto/graph/v2/composition/remove-source.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import type { ArtifactReference } from "../../../artifact/types"; | ||
import { setConnectors } from "../../../connector/actions"; | ||
import type { GiselleNode, GiselleNodeId } from "../../../giselle-node/types"; | ||
import type { TextContentReference } from "../../../text-content/types"; | ||
import type { WebSearch } from "../../../web-search/types"; | ||
import { removeParameterFromNode, updateNodeProperty } from "../../actions"; | ||
import type { CompositeAction } from "../../context"; | ||
|
||
export type Source = ArtifactReference | TextContentReference | WebSearch; | ||
type RemoveSourceInput = { | ||
source: Source; | ||
nodeId: GiselleNodeId; | ||
}; | ||
|
||
export function removeSource({ | ||
input, | ||
}: { input: RemoveSourceInput }): CompositeAction { | ||
return (dispatch, getState) => { | ||
const node = getState().graph.nodes.find( | ||
(node) => node.id === input.nodeId, | ||
); | ||
if (node === undefined) { | ||
throw new Error(`Node not found: ${input.nodeId}`); | ||
} | ||
|
||
// Remove the source from the sourceNode property | ||
const currentSources = node.properties.sources ?? []; | ||
if (!Array.isArray(currentSources)) { | ||
throw new Error(`${node.id}'s sources property is not an array`); | ||
} | ||
dispatch( | ||
updateNodeProperty({ | ||
node: { | ||
id: input.nodeId, | ||
property: { | ||
key: "sources", | ||
value: currentSources.filter( | ||
(currentSource) => | ||
typeof currentSource === "object" && | ||
currentSource !== null && | ||
typeof currentSource.id === "string" && | ||
currentSource.id !== input.source.id, | ||
), | ||
}, | ||
}, | ||
}), | ||
); | ||
|
||
// Remove the source from the targetNode parameter | ||
let sourceCreatorNodeId: GiselleNodeId | undefined; | ||
if (input.source.object === "artifact.reference") { | ||
sourceCreatorNodeId = getState().graph.artifacts.find( | ||
(artifact) => artifact.id === input.source.id, | ||
)?.generatorNode?.id; | ||
} else if (input.source.object === "webSearch") { | ||
sourceCreatorNodeId = getState().graph.webSearches.find( | ||
(webSearch) => webSearch.id === input.source.id, | ||
)?.generatorNode?.id; | ||
} | ||
if (sourceCreatorNodeId === undefined) { | ||
throw new Error(`Source creator node not found: ${input.source.id}`); | ||
} | ||
const relevantConnectors = getState().graph.connectors.filter( | ||
(connector) => connector.source === input.nodeId, | ||
); | ||
for (const relevantConnector of relevantConnectors) { | ||
const sourceConnector = getState().graph.connectors.find( | ||
(connector) => | ||
connector.source === sourceCreatorNodeId && | ||
connector.target === relevantConnector.target, | ||
); | ||
if (sourceConnector === undefined) { | ||
throw new Error( | ||
`Source connector not found: ${sourceCreatorNodeId} -> ${relevantConnector.target}`, | ||
); | ||
} | ||
dispatch( | ||
removeParameterFromNode({ | ||
node: { | ||
id: relevantConnector.target, | ||
}, | ||
parameter: { | ||
key: sourceConnector.targetHandle, | ||
}, | ||
}), | ||
); | ||
dispatch( | ||
setConnectors({ | ||
input: { | ||
connectors: getState().graph.connectors.filter( | ||
(connector) => connector.id !== sourceConnector.id, | ||
), | ||
}, | ||
}), | ||
); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters