mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-14 09:27:58 -05:00
* fix(billing): should allow restoring subscription (#1728) * fix(already-cancelled-sub): UI should allow restoring subscription * restore functionality fixed * fix * improvement(api-keys): move to workspace level * remove migration to prep merge * remove two more unused cols * prep staging merge * add migration back --------- Co-authored-by: Waleed <walif6@gmail.com> Co-authored-by: Siddharth Ganesan <33737564+Sg312@users.noreply.github.com>
165 lines
4.4 KiB
TypeScript
165 lines
4.4 KiB
TypeScript
import { SimStudioClient, SimStudioError } from '../src/index'
|
|
|
|
// Example 1: Basic workflow execution
|
|
async function basicExample() {
|
|
const client = new SimStudioClient({
|
|
apiKey: process.env.SIM_API_KEY!,
|
|
baseUrl: 'https://sim.ai',
|
|
})
|
|
|
|
try {
|
|
// Execute a workflow without input
|
|
const result = await client.executeWorkflow('your-workflow-id')
|
|
|
|
if (result.success) {
|
|
console.log('✅ Workflow executed successfully!')
|
|
console.log('Output:', result.output)
|
|
console.log('Duration:', result.metadata?.duration, 'ms')
|
|
} else {
|
|
console.log('❌ Workflow failed:', result.error)
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof SimStudioError) {
|
|
console.error('SDK Error:', error.message, 'Code:', error.code)
|
|
} else {
|
|
console.error('Unexpected error:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Example 2: Workflow execution with input data
|
|
async function withInputExample() {
|
|
const client = new SimStudioClient({
|
|
apiKey: process.env.SIM_API_KEY!,
|
|
})
|
|
|
|
try {
|
|
const result = await client.executeWorkflow('your-workflow-id', {
|
|
input: {
|
|
message: 'Hello from SDK!',
|
|
userId: '12345',
|
|
data: {
|
|
type: 'analysis',
|
|
parameters: {
|
|
includeMetadata: true,
|
|
format: 'json',
|
|
},
|
|
},
|
|
},
|
|
timeout: 60000, // 60 seconds
|
|
})
|
|
|
|
if (result.success) {
|
|
console.log('✅ Workflow executed successfully!')
|
|
console.log('Output:', result.output)
|
|
if (result.metadata?.duration) {
|
|
console.log('Duration:', result.metadata.duration, 'ms')
|
|
}
|
|
} else {
|
|
console.log('❌ Workflow failed:', result.error)
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof SimStudioError) {
|
|
console.error('SDK Error:', error.message, 'Code:', error.code)
|
|
} else {
|
|
console.error('Unexpected error:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Example 3: Workflow validation and status checking
|
|
async function statusExample() {
|
|
const client = new SimStudioClient({
|
|
apiKey: process.env.SIM_API_KEY!,
|
|
})
|
|
|
|
try {
|
|
// Check if workflow is ready
|
|
const isReady = await client.validateWorkflow('your-workflow-id')
|
|
console.log('Workflow ready:', isReady)
|
|
|
|
// Get detailed status
|
|
const status = await client.getWorkflowStatus('your-workflow-id')
|
|
console.log('Status:', {
|
|
deployed: status.isDeployed,
|
|
needsRedeployment: status.needsRedeployment,
|
|
deployedAt: status.deployedAt,
|
|
})
|
|
|
|
if (status.isDeployed) {
|
|
// Execute the workflow
|
|
const result = await client.executeWorkflow('your-workflow-id')
|
|
|
|
if (result.success) {
|
|
console.log('✅ Workflow executed successfully!')
|
|
console.log('Output:', result.output)
|
|
} else {
|
|
console.log('❌ Workflow failed:', result.error)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof SimStudioError) {
|
|
console.error('SDK Error:', error.message, 'Code:', error.code)
|
|
} else {
|
|
console.error('Unexpected error:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Example 4: Workflow execution with streaming
|
|
async function streamingExample() {
|
|
const client = new SimStudioClient({
|
|
apiKey: process.env.SIM_API_KEY!,
|
|
})
|
|
|
|
try {
|
|
const result = await client.executeWorkflow('your-workflow-id', {
|
|
input: {
|
|
message: 'Count to five',
|
|
},
|
|
stream: true,
|
|
selectedOutputs: ['agent1.content'], // Use blockName.attribute format
|
|
timeout: 60000,
|
|
})
|
|
|
|
if (result.success) {
|
|
console.log('✅ Workflow executed successfully!')
|
|
console.log('Output:', result.output)
|
|
console.log('Duration:', result.metadata?.duration, 'ms')
|
|
} else {
|
|
console.log('❌ Workflow failed:', result.error)
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof SimStudioError) {
|
|
console.error('SDK Error:', error.message, 'Code:', error.code)
|
|
} else {
|
|
console.error('Unexpected error:', error)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run examples
|
|
if (require.main === module) {
|
|
async function runExamples() {
|
|
console.log('🚀 Running Sim SDK Examples\n')
|
|
|
|
try {
|
|
await basicExample()
|
|
console.log('\n✅ Basic example completed')
|
|
|
|
await withInputExample()
|
|
console.log('\n✅ Input example completed')
|
|
|
|
await statusExample()
|
|
console.log('\n✅ Status example completed')
|
|
|
|
await streamingExample()
|
|
console.log('\n✅ Streaming example completed')
|
|
} catch (error) {
|
|
console.error('Error running examples:', error)
|
|
}
|
|
}
|
|
|
|
runExamples()
|
|
}
|