Skip to content

Commit eb5d019

Browse files
authored
feat/codegen: added initial outline for sdk, added api keys table & view (#126)
* feat[sdk]: added scaffolding for sdk pkg * feat[sdk]: simplified usage of tools for agent block * fix[sdk]: added api key directly into AgentOptions * fix[sdk]: simplified the conditional block by removing the code input since we don't need it * feat[sdk]: added account-level api keys & db table to manage them
1 parent ed5f8a9 commit eb5d019

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+10263
-7
lines changed

packages/sdk/README.md

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# Sim Studio SDK
2+
3+
The Sim Studio SDK allows developers to create, manage, and deploy workflows programmatically.
4+
5+
## Installation
6+
7+
```bash
8+
npm install @sim-studio/sdk
9+
```
10+
11+
## Usage
12+
13+
### Creating a Simple Workflow
14+
15+
```typescript
16+
import { SimStudio, AgentBlock, FunctionBlock } from '@sim-studio/sdk';
17+
18+
// Initialize the SDK
19+
const simStudio = new SimStudio({
20+
apiKey: 'your-api-key',
21+
});
22+
23+
// Create a workflow
24+
const workflow = simStudio.createWorkflow(
25+
'Simple Workflow',
26+
'A simple workflow that uses an agent to generate content'
27+
);
28+
29+
// Create an agent block
30+
const agentBlock = new AgentBlock({
31+
model: 'claude-3-7-sonnet',
32+
prompt: 'Write a blog post about <input.topic>',
33+
systemPrompt: 'You are a professional content writer.',
34+
});
35+
36+
// Create a function block to format the content
37+
const formatterBlock = new FunctionBlock({
38+
code: `
39+
function formatContent(input) {
40+
return {
41+
title: "Blog: " + input.topic,
42+
content: input.content,
43+
wordCount: input.content.split(' ').length
44+
};
45+
}
46+
`
47+
});
48+
49+
// Add blocks to the workflow
50+
workflow.addBlock(agentBlock);
51+
workflow.addBlock(formatterBlock);
52+
53+
// Connect blocks
54+
const starterBlock = workflow.getStarterBlock();
55+
workflow.connect(starterBlock.id, agentBlock.id);
56+
workflow.connect(agentBlock.id, formatterBlock.id);
57+
58+
// Build and save the workflow
59+
const builtWorkflow = workflow.build();
60+
simStudio.saveWorkflow(builtWorkflow)
61+
.then(savedWorkflow => {
62+
console.log(`Workflow saved with ID: ${savedWorkflow.id}`);
63+
});
64+
```
65+
66+
### Using Tools with Agents
67+
68+
Agent blocks can use tools for enhanced capabilities. You can simply reference built-in tools by ID or define custom tools:
69+
70+
```typescript
71+
import { SimStudio, AgentBlock } from '@sim-studio/sdk';
72+
73+
// Initialize the SDK
74+
const simStudio = new SimStudio({
75+
apiKey: 'your-api-key',
76+
});
77+
78+
// Create a workflow
79+
const workflow = simStudio.createWorkflow('Research Assistant');
80+
81+
// Create an agent block
82+
const researchAgent = new AgentBlock({
83+
model: 'claude-3-opus',
84+
prompt: 'Research the topic "{{input.topic}}" and provide a comprehensive summary.',
85+
systemPrompt: 'You are a research assistant.',
86+
tools: [] // Will be populated with tool references
87+
});
88+
89+
// Reference built-in tools by their IDs
90+
// The system will automatically transform these references into tool definitions
91+
researchAgent.data.toolReferences = ['tavily_search', 'serper_search'];
92+
93+
// Configure tool settings with required parameters
94+
researchAgent.data.toolSettings = {
95+
tavily_search: {
96+
apiKey: 'your-tavily-api-key'
97+
},
98+
serper_search: {
99+
apiKey: 'your-serper-api-key'
100+
}
101+
};
102+
103+
// For custom tools, you can define them directly
104+
const customTool = {
105+
name: 'findCompany',
106+
description: 'Find information about a company by name',
107+
parameters: {
108+
type: 'object',
109+
properties: {
110+
companyName: {
111+
type: 'string',
112+
description: 'The name of the company'
113+
}
114+
},
115+
required: ['companyName']
116+
}
117+
};
118+
119+
// Add the custom tool to the agent
120+
researchAgent.data.tools.push(customTool);
121+
122+
// Add to workflow
123+
workflow.addBlock(researchAgent);
124+
125+
// Connect to starter
126+
const starterBlock = workflow.getStarterBlock();
127+
workflow.connect(starterBlock.id, researchAgent.id);
128+
129+
// Build and save
130+
const builtWorkflow = workflow.build();
131+
```
132+
133+
### Creating Conditional Workflows
134+
135+
You can create workflows with conditional branching using the ConditionBlock:
136+
137+
```typescript
138+
import { SimStudio, AgentBlock, ConditionBlock } from '@sim-studio/sdk';
139+
140+
// Create a condition block
141+
const conditionBlock = new ConditionBlock({
142+
conditions: [
143+
{ expression: 'input.score > 0.8', id: 'high' },
144+
{ expression: 'input.score > 0.5', id: 'medium' },
145+
{ expression: 'true', id: 'low' }
146+
]
147+
});
148+
149+
// Connect condition outcomes to different blocks
150+
workflow.connect(
151+
conditionBlock.id,
152+
highQualityBlock.id,
153+
{ sourceHandle: 'condition-high' }
154+
);
155+
156+
workflow.connect(
157+
conditionBlock.id,
158+
mediumQualityBlock.id,
159+
{ sourceHandle: 'condition-medium' }
160+
);
161+
162+
workflow.connect(
163+
conditionBlock.id,
164+
lowQualityBlock.id,
165+
{ sourceHandle: 'condition-low' }
166+
);
167+
```
168+
169+
## API Reference
170+
171+
### SimStudio
172+
173+
- `createWorkflow(name, description?)`: Creates a new workflow builder
174+
- `saveWorkflow(workflow)`: Saves a workflow and returns the saved workflow with ID
175+
- `deployWorkflow(workflowId, options?)`: Deploys a workflow
176+
- `executeWorkflow(workflowId, input)`: Executes a workflow with the given input
177+
178+
### WorkflowBuilder
179+
180+
- `addBlock(block)`: Adds a block to the workflow
181+
- `connect(sourceId, targetId, options?)`: Connects two blocks
182+
- `getStarterBlock()`: Gets the starter block
183+
- `build()`: Builds the workflow
184+
185+
### Block Types
186+
187+
- `AgentBlock`: Agent that can generate content using LLMs
188+
- `FunctionBlock`: Executes JavaScript code
189+
- `ConditionBlock`: Routes based on conditions
190+
- `RouterBlock`: Routes based on agent decisions
191+
- `ApiBlock`: Makes HTTP requests
192+
- `EvaluatorBlock`: Evaluates content quality
193+
194+
## Examples
195+
196+
See the [examples](./examples) directory for more examples of using the SDK.
197+
198+
## Features
199+
200+
- Create and manage workflows programmatically
201+
- Build complex workflows with various block types
202+
- Execute workflows and retrieve results
203+
- Deploy workflows as API endpoints
204+
- Schedule workflows for automatic execution
205+
206+
## Block Types
207+
208+
The SDK supports all Sim Studio block types:
209+
210+
- **Agent**: LLM-powered operations
211+
- **Function**: Custom JavaScript code execution
212+
- **Condition**: Branching based on logical expressions
213+
- **Router**: Dynamic path selection
214+
- **API**: HTTP requests
215+
- **Evaluator**: LLM-based output assessment
216+
217+
## Documentation
218+
219+
For detailed documentation, visit [docs.simstudio.dev](https://docs.simstudio.dev)
220+
221+
## License
222+
223+
MIT
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { SimStudio, AgentBlock, FunctionBlock } from '../src'
2+
3+
/**
4+
* Creates a simple workflow with Sim Studio SDK
5+
*/
6+
async function createBasicWorkflow() {
7+
const simStudio = new SimStudio({
8+
apiKey: 'your-api-key',
9+
})
10+
11+
// Create a new workflow with a name and description
12+
const workflowBuilder = simStudio.createWorkflow(
13+
'Content Creation Workflow',
14+
'Generate and optimize content for blog posts'
15+
)
16+
17+
// Add blocks
18+
const contentGeneratorBlock = new AgentBlock({
19+
model: 'claude-3-sonnet',
20+
prompt: 'Write a detailed blog post about {{input.topic}}',
21+
systemPrompt: 'You are a professional content writer with expertise in creating engaging blog posts.',
22+
temperature: 0.7,
23+
apiKey: 'your-agent-api-key'
24+
}).setName('Content Generator')
25+
26+
const formatterBlock = new FunctionBlock({
27+
code: `
28+
function formatContent(input) {
29+
// Simple formatting function
30+
const title = input.topic.toUpperCase()
31+
const content = input.content
32+
const wordCount = content.split(' ').length
33+
34+
return {
35+
title,
36+
content,
37+
wordCount,
38+
timestamp: new Date().toISOString()
39+
}
40+
}
41+
`,
42+
}).setName('Formatter')
43+
44+
const seoOptimizerBlock = new AgentBlock({
45+
model: 'claude-3-haiku',
46+
prompt: 'Optimize this content for SEO: {{input.content}}',
47+
systemPrompt: 'You are an SEO expert. Add relevant keywords, improve readability, and optimize for search engines.',
48+
temperature: 0.4,
49+
apiKey: 'your-agent-api-key'
50+
}).setName('SEO Optimizer')
51+
52+
// Add blocks to workflow
53+
workflowBuilder
54+
.addBlock(contentGeneratorBlock)
55+
.addBlock(formatterBlock)
56+
.addBlock(seoOptimizerBlock)
57+
58+
// Connect blocks
59+
const starterBlock = workflowBuilder.getStarterBlock()
60+
workflowBuilder
61+
.connect(starterBlock.id, contentGeneratorBlock.id)
62+
.connect(contentGeneratorBlock.id, formatterBlock.id)
63+
.connect(formatterBlock.id, seoOptimizerBlock.id)
64+
65+
// Build the workflow
66+
const workflow = workflowBuilder.build()
67+
68+
// Execute the workflow
69+
try {
70+
const workflowId = '123456' // In a real scenario, this would be the ID returned from saveWorkflow
71+
const result = await simStudio.executeWorkflow(workflowId, {
72+
topic: 'The benefits of AI for content creation'
73+
})
74+
75+
} catch (error) {
76+
console.error('Error executing workflow:', error)
77+
}
78+
79+
return workflow
80+
}
81+
82+
// Run the example
83+
if (require.main === module) {
84+
createBasicWorkflow().catch(console.error)
85+
}
86+
87+
export default createBasicWorkflow

0 commit comments

Comments
 (0)