mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
Added translate block, added ability to add default value during block definition
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
import { TranslateIcon } from '@/components/icons'
|
||||
import { BlockConfig } from '../types'
|
||||
import { ChatResponse } from '@/tools/openai/chat'
|
||||
|
||||
const MODEL_TOOLS = {
|
||||
'gpt-4o': 'openai.chat',
|
||||
'o1': 'openai.chat',
|
||||
'o1-mini': 'openai.chat',
|
||||
'claude-3-5-sonnet-20241022': 'anthropic.chat',
|
||||
'gemini-pro': 'google.chat',
|
||||
} as const
|
||||
|
||||
const getTranslationPrompt = (targetLanguage: string) => `You are a highly skilled translator. Your task is to translate the given text into ${targetLanguage || 'English'} while:
|
||||
1. Preserving the original meaning and nuance
|
||||
2. Maintaining appropriate formality levels
|
||||
3. Adapting idioms and cultural references appropriately
|
||||
4. Preserving formatting and special characters
|
||||
5. Handling technical terms accurately
|
||||
|
||||
Only return the translated text without any explanations or notes. The translation should be natural and fluent in ${targetLanguage || 'English'}.`
|
||||
|
||||
export const TranslateBlock: BlockConfig<ChatResponse> = {
|
||||
type: 'translate',
|
||||
toolbar: {
|
||||
title: 'Translate',
|
||||
description: 'Translate text to any language',
|
||||
bgColor: '#FF4B4B',
|
||||
icon: TranslateIcon,
|
||||
category: 'basic',
|
||||
},
|
||||
tools: {
|
||||
access: ['openai.chat', 'anthropic.chat', 'google.chat'],
|
||||
config: {
|
||||
tool: (params: Record<string, any>) => {
|
||||
const model = params.model || 'gpt-4o'
|
||||
|
||||
if (!model) {
|
||||
throw new Error('No model selected')
|
||||
}
|
||||
|
||||
const tool = MODEL_TOOLS[model as keyof typeof MODEL_TOOLS]
|
||||
|
||||
if (!tool) {
|
||||
throw new Error(`Invalid model selected: ${model}`)
|
||||
}
|
||||
|
||||
return tool
|
||||
}
|
||||
}
|
||||
},
|
||||
workflow: {
|
||||
inputs: {
|
||||
context: { type: 'string', required: true },
|
||||
targetLanguage: { type: 'string', required: true },
|
||||
apiKey: { type: 'string', required: true },
|
||||
systemPrompt: { type: 'string', required: true }
|
||||
},
|
||||
outputs: {
|
||||
response: {
|
||||
type: {
|
||||
content: 'string',
|
||||
model: 'string',
|
||||
tokens: 'any'
|
||||
}
|
||||
}
|
||||
},
|
||||
subBlocks: [
|
||||
{
|
||||
id: 'context',
|
||||
title: 'Text to Translate',
|
||||
type: 'long-input',
|
||||
layout: 'full',
|
||||
placeholder: 'Enter the text you want to translate'
|
||||
},
|
||||
{
|
||||
id: 'targetLanguage',
|
||||
title: 'Translate To',
|
||||
type: 'short-input',
|
||||
layout: 'full',
|
||||
placeholder: 'Enter language (e.g. Spanish, French, etc.)'
|
||||
},
|
||||
{
|
||||
id: 'model',
|
||||
title: 'Model',
|
||||
type: 'dropdown',
|
||||
layout: 'half',
|
||||
options: Object.keys(MODEL_TOOLS)
|
||||
},
|
||||
{
|
||||
id: 'apiKey',
|
||||
title: "API Key",
|
||||
type: "short-input",
|
||||
layout: "full",
|
||||
placeholder: "Enter your API key",
|
||||
password: true,
|
||||
connectionDroppable: false
|
||||
},
|
||||
{
|
||||
id: 'systemPrompt',
|
||||
title: 'System Prompt',
|
||||
type: 'code',
|
||||
layout: 'full',
|
||||
hidden: true,
|
||||
value: (params: Record<string, any>) => {
|
||||
return getTranslationPrompt(params.targetLanguage || 'English')
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
+5
-2
@@ -7,6 +7,7 @@ import { FunctionBlock } from './blocks/function'
|
||||
import { CrewAIVisionBlock } from './blocks/crewai'
|
||||
import { FirecrawlScrapeBlock } from './blocks/firecrawl'
|
||||
import { JinaBlock } from './blocks/jina'
|
||||
import { TranslateBlock } from './blocks/translate'
|
||||
|
||||
// Export blocks for ease of use
|
||||
export {
|
||||
@@ -15,7 +16,8 @@ export {
|
||||
FunctionBlock,
|
||||
CrewAIVisionBlock,
|
||||
FirecrawlScrapeBlock,
|
||||
JinaBlock
|
||||
JinaBlock,
|
||||
TranslateBlock
|
||||
}
|
||||
|
||||
// Registry of all block configurations
|
||||
@@ -25,7 +27,8 @@ const blocks: Record<string, BlockConfig> = {
|
||||
function: FunctionBlock,
|
||||
crewai_vision: CrewAIVisionBlock,
|
||||
firecrawl_scrape: FirecrawlScrapeBlock,
|
||||
jina_reader: JinaBlock
|
||||
jina_reader: JinaBlock,
|
||||
translate: TranslateBlock
|
||||
}
|
||||
|
||||
// Build a reverse mapping of tools to block types
|
||||
|
||||
@@ -46,6 +46,8 @@ export interface SubBlockConfig {
|
||||
placeholder?: string
|
||||
password?: boolean
|
||||
connectionDroppable?: boolean
|
||||
hidden?: boolean
|
||||
value?: (params: Record<string, any>) => string
|
||||
}
|
||||
|
||||
export interface BlockConfig<T extends ToolResponse = ToolResponse> {
|
||||
|
||||
Reference in New Issue
Block a user