|
1 |
| -import { Workflow } from "@/src/components/Workflow"; |
2 |
| -import { WorkflowContext } from "@/src/components/Workflow"; |
3 |
| -import { createWorkflowOutput } from "@/src/hooks/useWorkflowOutput"; |
| 1 | +import * as gsx from "@/index"; |
4 | 2 |
|
5 |
| -import { BlogWritingWorkflow } from "./blog/BlogWritingWorkflow"; |
6 |
| -import { TweetWritingWorkflow } from "./tweet/TweetWritingWorkflow"; |
7 |
| - |
8 |
| -async function runParallelWorkflow() { |
9 |
| - const title = "Programmatic Secrets with ESC"; |
10 |
| - const prompt = "Write an article..."; |
11 |
| - |
12 |
| - const [blogPost, setBlogPost] = createWorkflowOutput(""); |
13 |
| - const [tweet, setTweet] = createWorkflowOutput(""); |
14 |
| - |
15 |
| - const workflow = ( |
16 |
| - <Workflow> |
17 |
| - <BlogWritingWorkflow |
18 |
| - title={title} |
19 |
| - prompt={prompt} |
20 |
| - setOutput={setBlogPost} |
21 |
| - /> |
22 |
| - <TweetWritingWorkflow content={blogPost} setOutput={setTweet} /> |
23 |
| - </Workflow> |
24 |
| - ); |
25 |
| - |
26 |
| - const context = new WorkflowContext(workflow); |
27 |
| - await context.execute(); |
| 3 | +interface LLMResearchBrainstormProps { |
| 4 | + prompt: string; |
| 5 | +} |
| 6 | +type LLMResearchBrainstormOutput = string[]; |
| 7 | +const LLMResearchBrainstorm = gsx.Component< |
| 8 | + LLMResearchBrainstormProps, |
| 9 | + LLMResearchBrainstormOutput |
| 10 | +>(async ({ prompt }) => { |
| 11 | + console.log("🔍 Starting research for:", prompt); |
| 12 | + const topics = await Promise.resolve(["topic 1", "topic 2", "topic 3"]); |
| 13 | + return topics; |
| 14 | +}); |
28 | 15 |
|
29 |
| - console.log("\n=== Parallel Workflow Results ==="); |
30 |
| - console.log("Blog Post:", await blogPost); |
31 |
| - console.log("Tweet:", await tweet); |
| 16 | +interface LLMResearchProps { |
| 17 | + topic: string; |
32 | 18 | }
|
| 19 | +type LLMResearchOutput = string; |
| 20 | +const LLMResearch = gsx.Component<LLMResearchProps, LLMResearchOutput>( |
| 21 | + async ({ topic }) => { |
| 22 | + console.log("📚 Researching topic:", topic); |
| 23 | + return await Promise.resolve(`research results for ${topic}`); |
| 24 | + }, |
| 25 | +); |
33 | 26 |
|
34 |
| -async function runNestedWorkflow() { |
35 |
| - const title = "Programmatic Secrets with ESC"; |
36 |
| - const prompt = "Write an article..."; |
| 27 | +interface LLMWriterProps { |
| 28 | + research: string; |
| 29 | + prompt: string; |
| 30 | +} |
| 31 | +type LLMWriterOutput = string; |
| 32 | +const LLMWriter = gsx.Component<LLMWriterProps, LLMWriterOutput>( |
| 33 | + async ({ research, prompt }) => { |
| 34 | + console.log("✍️ Writing draft based on research"); |
| 35 | + return await Promise.resolve( |
| 36 | + `**draft\n${research}\n${prompt}\n**end draft`, |
| 37 | + ); |
| 38 | + }, |
| 39 | +); |
37 | 40 |
|
38 |
| - let blogPost = ""; |
39 |
| - let tweet = ""; |
| 41 | +interface LLMEditorProps { |
| 42 | + draft: string; |
| 43 | +} |
| 44 | +type LLMEditorOutput = string; |
| 45 | +const LLMEditor = gsx.Component<LLMEditorProps, LLMEditorOutput>( |
| 46 | + async ({ draft }) => { |
| 47 | + console.log("✨ Polishing final draft"); |
| 48 | + return await Promise.resolve(`edited result: ${draft}`); |
| 49 | + }, |
| 50 | +); |
40 | 51 |
|
41 |
| - const workflow = ( |
42 |
| - <Workflow> |
43 |
| - <BlogWritingWorkflow |
44 |
| - title={ |
45 |
| - new Promise(resolve => { |
46 |
| - resolve(title); |
47 |
| - }) |
48 |
| - } |
49 |
| - prompt={prompt} |
50 |
| - > |
51 |
| - {blogPostResult => ( |
52 |
| - <TweetWritingWorkflow content={blogPostResult}> |
53 |
| - {tweetResult => { |
54 |
| - blogPost = blogPostResult; |
55 |
| - tweet = tweetResult; |
56 |
| - return null; |
57 |
| - }} |
58 |
| - </TweetWritingWorkflow> |
59 |
| - )} |
60 |
| - </BlogWritingWorkflow> |
61 |
| - </Workflow> |
62 |
| - ); |
| 52 | +interface WebResearcherProps { |
| 53 | + prompt: string; |
| 54 | +} |
| 55 | +type WebResearcherOutput = string[]; |
| 56 | +const WebResearcher = gsx.Component<WebResearcherProps, WebResearcherOutput>( |
| 57 | + async ({ prompt }) => { |
| 58 | + console.log("🌐 Researching web for:", prompt); |
| 59 | + const results = await Promise.resolve([ |
| 60 | + "web result 1", |
| 61 | + "web result 2", |
| 62 | + "web result 3", |
| 63 | + ]); |
| 64 | + return results; |
| 65 | + }, |
| 66 | +); |
63 | 67 |
|
64 |
| - const context = new WorkflowContext(workflow); |
65 |
| - await context.execute(); |
| 68 | +type ParallelResearchOutput = [string[], string[]]; |
| 69 | +interface ParallelResearchComponentProps { |
| 70 | + prompt: string; |
| 71 | +} |
| 72 | +const ParallelResearch = gsx.Component< |
| 73 | + ParallelResearchComponentProps, |
| 74 | + ParallelResearchOutput |
| 75 | +>(({ prompt }) => ( |
| 76 | + <> |
| 77 | + <LLMResearchBrainstorm prompt={prompt}> |
| 78 | + {topics => topics.map(topic => <LLMResearch topic={topic} />)} |
| 79 | + </LLMResearchBrainstorm> |
| 80 | + <WebResearcher prompt={prompt} /> |
| 81 | + </> |
| 82 | +)); |
66 | 83 |
|
67 |
| - console.log("\n=== Nested Workflow Results ==="); |
68 |
| - console.log("Blog Post:", blogPost); |
69 |
| - console.log("Tweet:", tweet); |
| 84 | +interface BlogWritingWorkflowProps { |
| 85 | + prompt: string; |
70 | 86 | }
|
| 87 | +type BlogWritingWorkflowOutput = string; |
| 88 | +const BlogWritingWorkflow = gsx.Component< |
| 89 | + BlogWritingWorkflowProps, |
| 90 | + BlogWritingWorkflowOutput |
| 91 | +>(async ({ prompt }) => ( |
| 92 | + <ParallelResearch prompt={prompt}> |
| 93 | + {([catalogResearch, webResearch]) => { |
| 94 | + console.log("🧠 Research:", { catalogResearch, webResearch }); |
| 95 | + return ( |
| 96 | + <LLMWriter |
| 97 | + research={[catalogResearch.join("\n"), webResearch.join("\n")].join( |
| 98 | + "\n\n", |
| 99 | + )} |
| 100 | + prompt={prompt} |
| 101 | + > |
| 102 | + {draft => <LLMEditor draft={draft} />} |
| 103 | + </LLMWriter> |
| 104 | + ); |
| 105 | + }} |
| 106 | + </ParallelResearch> |
| 107 | +)); |
71 | 108 |
|
72 | 109 | async function main() {
|
73 |
| - try { |
74 |
| - await runParallelWorkflow(); |
75 |
| - await runNestedWorkflow(); |
76 |
| - } catch (error) { |
77 |
| - console.error("Workflow execution failed:", error); |
78 |
| - process.exit(1); |
79 |
| - } |
| 110 | + console.log("🚀 Starting blog writing workflow"); |
| 111 | + |
| 112 | + // Use the gensx function to execute the workflow and annotate with the output type. |
| 113 | + const result = await gsx.execute<string>( |
| 114 | + <BlogWritingWorkflow prompt="Write a blog post about the future of AI" />, |
| 115 | + ); |
| 116 | + console.log("✅ Final result:", { result }); |
80 | 117 | }
|
81 | 118 |
|
82 |
| -main().catch((error: unknown) => { |
83 |
| - console.error("Unhandled error:", error); |
84 |
| - process.exit(1); |
85 |
| -}); |
| 119 | +await main(); |
0 commit comments