Skip to content

Commit

Permalink
refactor(graph): Extract source index parsing logic to utility function
Browse files Browse the repository at this point in the history
Move the source index extraction logic from the generateText action into a
dedicated utility function extractSourceIndexesFromNode for better code
organization and reusability.
  • Loading branch information
toyamarinyon committed Oct 30, 2024
1 parent 8bf2c81 commit 654feba
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 78 deletions.
78 changes: 3 additions & 75 deletions app/(playground)/p/[agentId]/beta-proto/graph/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
giselleNodeToGiselleNodeArtifactElement,
} from "../giselle-node/utils";
import type { SourceIndex } from "../source/types";
import { extractSourceIndexesFromNode } from "../source/utils";
import type { TextContent, TextContentId } from "../text-content/types";
import { generateWebSearchStream } from "../web-search/server-action";
import {
Expand Down Expand Up @@ -463,88 +464,15 @@ export const generateText =
throw new Error("Instruction node not found");
}

const sourceIndexes: SourceIndex[] = [];
if (Array.isArray(instructionNode.properties.sources)) {
for (const source of instructionNode.properties.sources) {
if (
typeof source !== "object" ||
source === null ||
typeof source.id !== "string" ||
typeof source.object !== "string"
) {
continue;
}
if (source.object === "textContent") {
sourceIndexes.push({
id: source.id as TextContentId,
object: "textContent",
title: source.title as string,
content: source.content as string,
});
} else if (source.object === "artifact.reference") {
const artifact = state.graph.artifacts.find(
(artifact) => artifact.id === source.id,
);
if (artifact !== undefined) {
sourceIndexes.push({
object: "artifact.reference",
id: artifact.id,
});
}
} else if (source.object === "file") {
if (
typeof source.status === "string" &&
source.status === fileStatuses.processed &&
typeof source.structuredDataBlobUrl === "string" &&
typeof source.name === "string"
) {
const structuredData = await fetch(
source.structuredDataBlobUrl,
).then((res) => res.text());
sourceIndexes.push({
object: "file",
id: source.id,
blobUrl: source.blobUrl,
structuredDataBlobUrl: source.structuredDataBlobUrl,
name: source.name,
status: "processed",
});
}
} else if (source.object === "webSearch") {
if (
typeof source.status === "string" &&
source.status === webSearchStatus.completed &&
Array.isArray(source.items)
) {
sourceIndexes.push({
object: "webSearch",
id: source.id,
name: source.name,
items: source.items,
generatorNode: source.generatorNode,
status: "completed",
});
await Promise.all(
(source.items as WebSearchItemReference[]).map(async (item) => {
if (item.status === webSearchItemStatus.completed) {
const webSearchData = await fetch(item.contentBlobUrl).then(
(res) => res.text(),
);
}
}),
);
}
}
}
}

const node = state.graph.nodes.find(
(node) => node.id === args.textGeneratorNode.id,
);
if (node === undefined) {
/** @todo error handling */
throw new Error("Node not found");
}

const sourceIndexes = extractSourceIndexesFromNode(node);
switch (instructionConnector.targetNodeArcheType) {
case giselleNodeArchetypes.textGenerator: {
const { object } = await generateArtifactStream({
Expand Down
87 changes: 84 additions & 3 deletions app/(playground)/p/[agentId]/beta-proto/source/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { db } from "@/drizzle";
import { type StructuredData, fileStatuses } from "../files/types";
import type { ArtifactReference } from "../artifact/types";
import {
type GiselleFile,
type StructuredData,
fileStatuses,
} from "../files/types";
import type { GiselleNode } from "../giselle-node/types";
import type { TextContent, TextContentId } from "../text-content/types";
import type { AgentId } from "../types";
import { type WebSearchItem, webSearchItemStatus } from "../web-search/types";
import type { Source, SourceIndex, WebSearch } from "./types";
import {
type WebSearch,
type WebSearchItem,
webSearchItemStatus,
webSearchStatus,
} from "../web-search/types";
import type { Source, SourceIndex } from "./types";

interface SourceIndexesToSourcesInput {
agentId: AgentId;
Expand Down Expand Up @@ -112,3 +124,72 @@ ${sources
)
.join("\n")}`;
}

export function extractSourceIndexesFromNode(node: GiselleNode): SourceIndex[] {
if (!Array.isArray(node.properties.sources)) {
return [];
}
return node.properties.sources
.map((source) => {
if (
typeof source !== "object" ||
source === null ||
typeof source.id !== "string" ||
typeof source.object !== "string"
) {
return null;
}

if (source.object === "textContent") {
return {
id: source.id as TextContentId,
object: "textContent",
title: source.title as string,
content: source.content as string,
} satisfies TextContent;
}
if (source.object === "artifact.reference") {
return {
object: "artifact.reference",
id: source.id,
} satisfies ArtifactReference;
}
if (source.object === "file") {
if (
typeof source.status !== "string" ||
source.status !== fileStatuses.processed ||
typeof source.structuredDataBlobUrl !== "string" ||
typeof source.name !== "string"
) {
return null;
}
return {
object: "file",
id: source.id,
blobUrl: source.blobUrl,
structuredDataBlobUrl: source.structuredDataBlobUrl,
name: source.name,
status: "processed",
} satisfies GiselleFile;
}
if (source.object === "webSearch") {
if (
typeof source.status !== "string" ||
source.status !== webSearchStatus.completed ||
Array.isArray(source.items)
) {
return null;
}
return {
object: "webSearch",
id: source.id,
name: source.name,
items: source.items,
generatorNode: source.generatorNode,
status: webSearchStatus.completed,
} satisfies WebSearch;
}
return null;
})
.filter((sourceIndex) => sourceIndex !== null);
}

0 comments on commit 654feba

Please sign in to comment.