-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from route06inc/infer-flow
Indicate the Final Node
- Loading branch information
Showing
6 changed files
with
217 additions
and
11 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
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
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
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
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,49 @@ | ||
import type { GiselleNode, GiselleNodeId } from "../../giselle-node/types"; | ||
|
||
const v2NodeActionTypes = { | ||
updateNode: "v2.updateNode", | ||
} as const; | ||
type V2NodeActionType = | ||
(typeof v2NodeActionTypes)[keyof typeof v2NodeActionTypes]; | ||
export function isV2NodeAction(action: unknown): action is V2NodeAction { | ||
return Object.values(v2NodeActionTypes).includes( | ||
(action as V2NodeAction).type, | ||
); | ||
} | ||
interface UpdateNodeAction { | ||
type: Extract<V2NodeActionType, "v2.updateNode">; | ||
input: UpdateNodeInput; | ||
} | ||
interface UpdateNodeInput { | ||
id: GiselleNodeId; | ||
isFinal?: boolean; | ||
} | ||
export function updateNode({ | ||
input, | ||
}: { input: UpdateNodeInput }): UpdateNodeAction { | ||
return { | ||
type: v2NodeActionTypes.updateNode, | ||
input, | ||
}; | ||
} | ||
|
||
export type V2NodeAction = UpdateNodeAction; | ||
|
||
export function v2NodeReducer( | ||
nodes: GiselleNode[], | ||
action: V2NodeAction, | ||
): GiselleNode[] { | ||
switch (action.type) { | ||
case v2NodeActionTypes.updateNode: | ||
return nodes.map((node) => { | ||
if (node.id === action.input.id) { | ||
return { | ||
...node, | ||
isFinal: action.input.isFinal ?? false, | ||
}; | ||
} | ||
return node; | ||
}); | ||
} | ||
return nodes; | ||
} |
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,56 @@ | ||
import { giselleNodeCategories } from "@/app/(playground)/p/[agentId]/beta-proto/giselle-node/types"; | ||
import { agents, db } from "@/drizzle"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
function chunkArray<T>(array: T[], chunkSize: number): T[][] { | ||
const chunks: T[][] = []; | ||
for (let i = 0; i < array.length; i += chunkSize) { | ||
chunks.push(array.slice(i, i + chunkSize)); | ||
} | ||
return chunks; | ||
} | ||
|
||
console.log("Add 'isFinal' to the all of the nodes..."); | ||
const listOfAgents = await db.select().from(agents); | ||
console.log(`Updating ${listOfAgents.length} agents...`); | ||
|
||
const agentChunks = chunkArray(listOfAgents, 10); | ||
|
||
for (let i = 0; i < agentChunks.length; i++) { | ||
const chunk = agentChunks[i]; | ||
console.log( | ||
` ├ Processing chunk ${i + 1}/${agentChunks.length} (${chunk.length} agents)`, | ||
); | ||
|
||
await Promise.all( | ||
chunk.map(async (agent) => { | ||
const instructionNodes = agent.graphv2.nodes.filter( | ||
(node) => node.category === giselleNodeCategories.instruction, | ||
); | ||
const actionNodes = agent.graphv2.nodes.filter( | ||
(node) => node.category === giselleNodeCategories.action, | ||
); | ||
await db | ||
.update(agents) | ||
.set({ | ||
graphv2: { | ||
...agent.graphv2, | ||
nodes: [ | ||
...instructionNodes.map((node) => ({ | ||
...node, | ||
isFinal: false, | ||
})), | ||
...actionNodes.map((node, index) => ({ | ||
...node, | ||
isFinal: index === actionNodes.length - 1, | ||
})), | ||
], | ||
}, | ||
}) | ||
.where(eq(agents.id, agent.id)); | ||
}), | ||
); | ||
|
||
console.log(` │ └ Completed chunk ${i + 1}/${i + 1}`); | ||
} | ||
console.log("All agents have been updated!"); |