mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-12 07:24:55 -05:00
Compare commits
2 Commits
sim-609-to
...
fix/s3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
512668040d | ||
|
|
bc9b4a26ad |
@@ -1,189 +0,0 @@
|
|||||||
'use client'
|
|
||||||
|
|
||||||
import type React from 'react'
|
|
||||||
import { useRef, useState } from 'react'
|
|
||||||
import { ArrowLeftRight, ArrowUp } from 'lucide-react'
|
|
||||||
import { Button, Input, Label, Tooltip } from '@/components/emcn'
|
|
||||||
import { cn } from '@/lib/core/utils/cn'
|
|
||||||
import type { WandControlHandlers } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/sub-block'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Props for a generic parameter with label component
|
|
||||||
*/
|
|
||||||
export interface ParameterWithLabelProps {
|
|
||||||
paramId: string
|
|
||||||
title: string
|
|
||||||
isRequired: boolean
|
|
||||||
visibility: string
|
|
||||||
wandConfig?: {
|
|
||||||
enabled: boolean
|
|
||||||
prompt?: string
|
|
||||||
placeholder?: string
|
|
||||||
}
|
|
||||||
canonicalToggle?: {
|
|
||||||
mode: 'basic' | 'advanced'
|
|
||||||
disabled?: boolean
|
|
||||||
onToggle?: () => void
|
|
||||||
}
|
|
||||||
disabled: boolean
|
|
||||||
isPreview: boolean
|
|
||||||
children: (wandControlRef: React.MutableRefObject<WandControlHandlers | null>) => React.ReactNode
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generic wrapper component for parameters that manages wand state and renders label + input
|
|
||||||
*/
|
|
||||||
export function ParameterWithLabel({
|
|
||||||
paramId,
|
|
||||||
title,
|
|
||||||
isRequired,
|
|
||||||
visibility,
|
|
||||||
wandConfig,
|
|
||||||
canonicalToggle,
|
|
||||||
disabled,
|
|
||||||
isPreview,
|
|
||||||
children,
|
|
||||||
}: ParameterWithLabelProps) {
|
|
||||||
const [isSearchActive, setIsSearchActive] = useState(false)
|
|
||||||
const [searchQuery, setSearchQuery] = useState('')
|
|
||||||
const searchInputRef = useRef<HTMLInputElement>(null)
|
|
||||||
const wandControlRef = useRef<WandControlHandlers | null>(null)
|
|
||||||
|
|
||||||
const isWandEnabled = wandConfig?.enabled ?? false
|
|
||||||
const showWand = isWandEnabled && !isPreview && !disabled
|
|
||||||
|
|
||||||
const handleSearchClick = (): void => {
|
|
||||||
setIsSearchActive(true)
|
|
||||||
setTimeout(() => {
|
|
||||||
searchInputRef.current?.focus()
|
|
||||||
}, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleSearchBlur = (): void => {
|
|
||||||
if (!searchQuery.trim() && !wandControlRef.current?.isWandStreaming) {
|
|
||||||
setIsSearchActive(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleSearchChange = (value: string): void => {
|
|
||||||
setSearchQuery(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleSearchSubmit = (): void => {
|
|
||||||
if (searchQuery.trim() && wandControlRef.current) {
|
|
||||||
wandControlRef.current.onWandTrigger(searchQuery)
|
|
||||||
setSearchQuery('')
|
|
||||||
setIsSearchActive(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleSearchCancel = (): void => {
|
|
||||||
setSearchQuery('')
|
|
||||||
setIsSearchActive(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
const isStreaming = wandControlRef.current?.isWandStreaming ?? false
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div key={paramId} className='relative min-w-0 space-y-[6px]'>
|
|
||||||
<div className='flex items-center justify-between gap-[6px] pl-[2px]'>
|
|
||||||
<Label className='flex items-center gap-[6px] whitespace-nowrap font-medium text-[13px] text-[var(--text-primary)]'>
|
|
||||||
{title}
|
|
||||||
{isRequired && visibility === 'user-only' && <span className='ml-0.5'>*</span>}
|
|
||||||
{visibility !== 'user-only' && (
|
|
||||||
<span className='ml-[6px] text-[12px] text-[var(--text-tertiary)]'>(optional)</span>
|
|
||||||
)}
|
|
||||||
</Label>
|
|
||||||
<div className='flex min-w-0 flex-1 items-center justify-end gap-[6px]'>
|
|
||||||
{showWand &&
|
|
||||||
(!isSearchActive ? (
|
|
||||||
<Button
|
|
||||||
variant='active'
|
|
||||||
className='-my-1 h-5 px-2 py-0 text-[11px]'
|
|
||||||
onClick={handleSearchClick}
|
|
||||||
>
|
|
||||||
Generate
|
|
||||||
</Button>
|
|
||||||
) : (
|
|
||||||
<div className='-my-1 flex min-w-[120px] max-w-[280px] flex-1 items-center gap-[4px]'>
|
|
||||||
<Input
|
|
||||||
ref={searchInputRef}
|
|
||||||
value={isStreaming ? 'Generating...' : searchQuery}
|
|
||||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
|
|
||||||
handleSearchChange(e.target.value)
|
|
||||||
}
|
|
||||||
onBlur={(e: React.FocusEvent<HTMLInputElement>) => {
|
|
||||||
const relatedTarget = e.relatedTarget as HTMLElement | null
|
|
||||||
if (relatedTarget?.closest('button')) return
|
|
||||||
handleSearchBlur()
|
|
||||||
}}
|
|
||||||
onKeyDown={(e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
||||||
if (e.key === 'Enter' && searchQuery.trim() && !isStreaming) {
|
|
||||||
handleSearchSubmit()
|
|
||||||
} else if (e.key === 'Escape') {
|
|
||||||
handleSearchCancel()
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
disabled={isStreaming}
|
|
||||||
className={cn(
|
|
||||||
'h-5 min-w-[80px] flex-1 text-[11px]',
|
|
||||||
isStreaming && 'text-muted-foreground'
|
|
||||||
)}
|
|
||||||
placeholder='Generate with AI...'
|
|
||||||
/>
|
|
||||||
<Button
|
|
||||||
variant='tertiary'
|
|
||||||
disabled={!searchQuery.trim() || isStreaming}
|
|
||||||
onMouseDown={(e: React.MouseEvent) => {
|
|
||||||
e.preventDefault()
|
|
||||||
e.stopPropagation()
|
|
||||||
}}
|
|
||||||
onClick={(e: React.MouseEvent) => {
|
|
||||||
e.stopPropagation()
|
|
||||||
handleSearchSubmit()
|
|
||||||
}}
|
|
||||||
className='h-[20px] w-[20px] flex-shrink-0 p-0'
|
|
||||||
>
|
|
||||||
<ArrowUp className='h-[12px] w-[12px]' />
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
{canonicalToggle && !isPreview && (
|
|
||||||
<Tooltip.Root>
|
|
||||||
<Tooltip.Trigger asChild>
|
|
||||||
<button
|
|
||||||
type='button'
|
|
||||||
className='flex h-[12px] w-[12px] flex-shrink-0 items-center justify-center bg-transparent p-0 disabled:cursor-not-allowed disabled:opacity-50'
|
|
||||||
onClick={canonicalToggle.onToggle}
|
|
||||||
disabled={canonicalToggle.disabled || disabled}
|
|
||||||
aria-label={
|
|
||||||
canonicalToggle.mode === 'advanced'
|
|
||||||
? 'Switch to selector'
|
|
||||||
: 'Switch to manual ID'
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<ArrowLeftRight
|
|
||||||
className={cn(
|
|
||||||
'!h-[12px] !w-[12px]',
|
|
||||||
canonicalToggle.mode === 'advanced'
|
|
||||||
? 'text-[var(--text-primary)]'
|
|
||||||
: 'text-[var(--text-secondary)]'
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
</Tooltip.Trigger>
|
|
||||||
<Tooltip.Content side='top'>
|
|
||||||
<p>
|
|
||||||
{canonicalToggle.mode === 'advanced'
|
|
||||||
? 'Switch to selector'
|
|
||||||
: 'Switch to manual ID'}
|
|
||||||
</p>
|
|
||||||
</Tooltip.Content>
|
|
||||||
</Tooltip.Root>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div className='relative w-full min-w-0'>{children(wandControlRef)}</div>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
'use client'
|
|
||||||
|
|
||||||
import { useEffect, useMemo, useRef } from 'react'
|
|
||||||
import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value'
|
|
||||||
import { SubBlock } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/sub-block'
|
|
||||||
import type { SubBlockConfig as BlockSubBlockConfig } from '@/blocks/types'
|
|
||||||
|
|
||||||
interface ToolSubBlockRendererProps {
|
|
||||||
blockId: string
|
|
||||||
subBlockId: string
|
|
||||||
toolIndex: number
|
|
||||||
subBlock: BlockSubBlockConfig
|
|
||||||
effectiveParamId: string
|
|
||||||
toolParams: Record<string, string> | undefined
|
|
||||||
onParamChange: (toolIndex: number, paramId: string, value: string) => void
|
|
||||||
disabled: boolean
|
|
||||||
canonicalToggle?: {
|
|
||||||
mode: 'basic' | 'advanced'
|
|
||||||
disabled?: boolean
|
|
||||||
onToggle?: () => void
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bridges the subblock store with StoredTool.params via a synthetic store key,
|
|
||||||
* then delegates all rendering to SubBlock for full parity.
|
|
||||||
*
|
|
||||||
* Two effects handle bidirectional sync:
|
|
||||||
* - tool.params → store (external changes)
|
|
||||||
* - store → tool.params (user interaction)
|
|
||||||
*/
|
|
||||||
export function ToolSubBlockRenderer({
|
|
||||||
blockId,
|
|
||||||
subBlockId,
|
|
||||||
toolIndex,
|
|
||||||
subBlock,
|
|
||||||
effectiveParamId,
|
|
||||||
toolParams,
|
|
||||||
onParamChange,
|
|
||||||
disabled,
|
|
||||||
canonicalToggle,
|
|
||||||
}: ToolSubBlockRendererProps) {
|
|
||||||
const syntheticId = `${subBlockId}-tool-${toolIndex}-${effectiveParamId}`
|
|
||||||
const [storeValue, setStoreValue] = useSubBlockValue(blockId, syntheticId)
|
|
||||||
|
|
||||||
const toolParamValue = toolParams?.[effectiveParamId] ?? ''
|
|
||||||
|
|
||||||
/** Tracks the last value we pushed to the store from tool.params to avoid echo loops */
|
|
||||||
const lastPushedToStoreRef = useRef<string | null>(null)
|
|
||||||
/** Tracks the last value we synced back to tool.params from the store */
|
|
||||||
const lastPushedToParamsRef = useRef<string | null>(null)
|
|
||||||
|
|
||||||
// Sync tool.params → store: push when the prop value changes (including first mount)
|
|
||||||
useEffect(() => {
|
|
||||||
if (!toolParamValue && lastPushedToStoreRef.current === null) {
|
|
||||||
// Skip initializing the store with an empty value on first mount —
|
|
||||||
// let the SubBlock component use its own default.
|
|
||||||
lastPushedToStoreRef.current = toolParamValue
|
|
||||||
lastPushedToParamsRef.current = toolParamValue
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (toolParamValue !== lastPushedToStoreRef.current) {
|
|
||||||
lastPushedToStoreRef.current = toolParamValue
|
|
||||||
lastPushedToParamsRef.current = toolParamValue
|
|
||||||
setStoreValue(toolParamValue)
|
|
||||||
}
|
|
||||||
}, [toolParamValue, setStoreValue])
|
|
||||||
|
|
||||||
// Sync store → tool.params: push when the user changes the value via SubBlock
|
|
||||||
useEffect(() => {
|
|
||||||
if (storeValue == null) return
|
|
||||||
const stringValue = typeof storeValue === 'string' ? storeValue : JSON.stringify(storeValue)
|
|
||||||
if (stringValue !== lastPushedToParamsRef.current) {
|
|
||||||
lastPushedToParamsRef.current = stringValue
|
|
||||||
lastPushedToStoreRef.current = stringValue
|
|
||||||
onParamChange(toolIndex, effectiveParamId, stringValue)
|
|
||||||
}
|
|
||||||
}, [storeValue, toolIndex, effectiveParamId, onParamChange])
|
|
||||||
|
|
||||||
// Determine if the parameter is optional for the user (LLM can fill it)
|
|
||||||
const visibility = subBlock.paramVisibility ?? 'user-or-llm'
|
|
||||||
const isOptionalForUser = visibility !== 'user-only'
|
|
||||||
|
|
||||||
const labelSuffix = useMemo(
|
|
||||||
() =>
|
|
||||||
isOptionalForUser ? (
|
|
||||||
<span className='ml-[6px] text-[12px] text-[var(--text-tertiary)]'>(optional)</span>
|
|
||||||
) : null,
|
|
||||||
[isOptionalForUser]
|
|
||||||
)
|
|
||||||
|
|
||||||
// Suppress SubBlock's "*" required indicator for optional-for-user params
|
|
||||||
const config = {
|
|
||||||
...subBlock,
|
|
||||||
id: syntheticId,
|
|
||||||
...(isOptionalForUser && { required: false }),
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<SubBlock
|
|
||||||
blockId={blockId}
|
|
||||||
config={config}
|
|
||||||
isPreview={false}
|
|
||||||
disabled={disabled}
|
|
||||||
canonicalToggle={canonicalToggle}
|
|
||||||
labelSuffix={labelSuffix}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -2,12 +2,37 @@
|
|||||||
* @vitest-environment node
|
* @vitest-environment node
|
||||||
*/
|
*/
|
||||||
import { describe, expect, it } from 'vitest'
|
import { describe, expect, it } from 'vitest'
|
||||||
import type { StoredTool } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/types'
|
|
||||||
import {
|
interface StoredTool {
|
||||||
isCustomToolAlreadySelected,
|
type: string
|
||||||
isMcpToolAlreadySelected,
|
title?: string
|
||||||
isWorkflowAlreadySelected,
|
toolId?: string
|
||||||
} from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/utils'
|
params?: Record<string, string>
|
||||||
|
customToolId?: string
|
||||||
|
schema?: any
|
||||||
|
code?: string
|
||||||
|
operation?: string
|
||||||
|
usageControl?: 'auto' | 'force' | 'none'
|
||||||
|
}
|
||||||
|
|
||||||
|
const isMcpToolAlreadySelected = (selectedTools: StoredTool[], mcpToolId: string): boolean => {
|
||||||
|
return selectedTools.some((tool) => tool.type === 'mcp' && tool.toolId === mcpToolId)
|
||||||
|
}
|
||||||
|
|
||||||
|
const isCustomToolAlreadySelected = (
|
||||||
|
selectedTools: StoredTool[],
|
||||||
|
customToolId: string
|
||||||
|
): boolean => {
|
||||||
|
return selectedTools.some(
|
||||||
|
(tool) => tool.type === 'custom-tool' && tool.customToolId === customToolId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const isWorkflowAlreadySelected = (selectedTools: StoredTool[], workflowId: string): boolean => {
|
||||||
|
return selectedTools.some(
|
||||||
|
(tool) => tool.type === 'workflow_input' && tool.params?.workflowId === workflowId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
describe('isMcpToolAlreadySelected', () => {
|
describe('isMcpToolAlreadySelected', () => {
|
||||||
describe('basic functionality', () => {
|
describe('basic functionality', () => {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,31 +0,0 @@
|
|||||||
/**
|
|
||||||
* Represents a tool selected and configured in the workflow
|
|
||||||
*
|
|
||||||
* @remarks
|
|
||||||
* For custom tools (new format), we only store: type, customToolId, usageControl, isExpanded.
|
|
||||||
* Everything else (title, schema, code) is loaded dynamically from the database.
|
|
||||||
* Legacy custom tools with inline schema/code are still supported for backwards compatibility.
|
|
||||||
*/
|
|
||||||
export interface StoredTool {
|
|
||||||
/** Block type identifier */
|
|
||||||
type: string
|
|
||||||
/** Display title for the tool (optional for new custom tool format) */
|
|
||||||
title?: string
|
|
||||||
/** Direct tool ID for execution (optional for new custom tool format) */
|
|
||||||
toolId?: string
|
|
||||||
/** Parameter values configured by the user (optional for new custom tool format) */
|
|
||||||
params?: Record<string, string>
|
|
||||||
/** Whether the tool details are expanded in UI */
|
|
||||||
isExpanded?: boolean
|
|
||||||
/** Database ID for custom tools (new format - reference only) */
|
|
||||||
customToolId?: string
|
|
||||||
/** Tool schema for custom tools (legacy format - inline JSON schema) */
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
schema?: Record<string, any>
|
|
||||||
/** Implementation code for custom tools (legacy format - inline) */
|
|
||||||
code?: string
|
|
||||||
/** Selected operation for multi-operation tools */
|
|
||||||
operation?: string
|
|
||||||
/** Tool usage control mode for LLM */
|
|
||||||
usageControl?: 'auto' | 'force' | 'none'
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import type { StoredTool } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/types'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if an MCP tool is already selected.
|
|
||||||
*/
|
|
||||||
export function isMcpToolAlreadySelected(selectedTools: StoredTool[], mcpToolId: string): boolean {
|
|
||||||
return selectedTools.some((tool) => tool.type === 'mcp' && tool.toolId === mcpToolId)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a custom tool is already selected.
|
|
||||||
*/
|
|
||||||
export function isCustomToolAlreadySelected(
|
|
||||||
selectedTools: StoredTool[],
|
|
||||||
customToolId: string
|
|
||||||
): boolean {
|
|
||||||
return selectedTools.some(
|
|
||||||
(tool) => tool.type === 'custom-tool' && tool.customToolId === customToolId
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a workflow is already selected.
|
|
||||||
*/
|
|
||||||
export function isWorkflowAlreadySelected(
|
|
||||||
selectedTools: StoredTool[],
|
|
||||||
workflowId: string
|
|
||||||
): boolean {
|
|
||||||
return selectedTools.some(
|
|
||||||
(tool) => tool.type === 'workflow_input' && tool.params?.workflowId === workflowId
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -76,7 +76,6 @@ interface SubBlockProps {
|
|||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
onToggle?: () => void
|
onToggle?: () => void
|
||||||
}
|
}
|
||||||
labelSuffix?: React.ReactNode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -203,8 +202,7 @@ const renderLabel = (
|
|||||||
showCopyButton: boolean
|
showCopyButton: boolean
|
||||||
copied: boolean
|
copied: boolean
|
||||||
onCopy: () => void
|
onCopy: () => void
|
||||||
},
|
}
|
||||||
labelSuffix?: React.ReactNode
|
|
||||||
): JSX.Element | null => {
|
): JSX.Element | null => {
|
||||||
if (config.type === 'switch') return null
|
if (config.type === 'switch') return null
|
||||||
if (!config.title) return null
|
if (!config.title) return null
|
||||||
@@ -220,7 +218,6 @@ const renderLabel = (
|
|||||||
<Label className='flex items-center gap-[6px] whitespace-nowrap'>
|
<Label className='flex items-center gap-[6px] whitespace-nowrap'>
|
||||||
{config.title}
|
{config.title}
|
||||||
{required && <span className='ml-0.5'>*</span>}
|
{required && <span className='ml-0.5'>*</span>}
|
||||||
{labelSuffix}
|
|
||||||
{config.type === 'code' &&
|
{config.type === 'code' &&
|
||||||
config.language === 'json' &&
|
config.language === 'json' &&
|
||||||
!isValidJson &&
|
!isValidJson &&
|
||||||
@@ -388,8 +385,7 @@ const arePropsEqual = (prevProps: SubBlockProps, nextProps: SubBlockProps): bool
|
|||||||
prevProps.disabled === nextProps.disabled &&
|
prevProps.disabled === nextProps.disabled &&
|
||||||
prevProps.fieldDiffStatus === nextProps.fieldDiffStatus &&
|
prevProps.fieldDiffStatus === nextProps.fieldDiffStatus &&
|
||||||
prevProps.allowExpandInPreview === nextProps.allowExpandInPreview &&
|
prevProps.allowExpandInPreview === nextProps.allowExpandInPreview &&
|
||||||
canonicalToggleEqual &&
|
canonicalToggleEqual
|
||||||
prevProps.labelSuffix === nextProps.labelSuffix
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -419,7 +415,6 @@ function SubBlockComponent({
|
|||||||
fieldDiffStatus,
|
fieldDiffStatus,
|
||||||
allowExpandInPreview,
|
allowExpandInPreview,
|
||||||
canonicalToggle,
|
canonicalToggle,
|
||||||
labelSuffix,
|
|
||||||
}: SubBlockProps): JSX.Element {
|
}: SubBlockProps): JSX.Element {
|
||||||
const [isValidJson, setIsValidJson] = useState(true)
|
const [isValidJson, setIsValidJson] = useState(true)
|
||||||
const [isSearchActive, setIsSearchActive] = useState(false)
|
const [isSearchActive, setIsSearchActive] = useState(false)
|
||||||
@@ -1064,8 +1059,7 @@ function SubBlockComponent({
|
|||||||
showCopyButton: Boolean(config.showCopyButton && config.useWebhookUrl),
|
showCopyButton: Boolean(config.showCopyButton && config.useWebhookUrl),
|
||||||
copied,
|
copied,
|
||||||
onCopy: handleCopy,
|
onCopy: handleCopy,
|
||||||
},
|
}
|
||||||
labelSuffix
|
|
||||||
)}
|
)}
|
||||||
{renderInput()}
|
{renderInput()}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -58,6 +58,16 @@ export const S3Block: BlockConfig<S3Response> = {
|
|||||||
},
|
},
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: 'getObjectRegion',
|
||||||
|
title: 'AWS Region',
|
||||||
|
type: 'short-input',
|
||||||
|
placeholder: 'Used when S3 URL does not include region',
|
||||||
|
condition: {
|
||||||
|
field: 'operation',
|
||||||
|
value: ['get_object'],
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
id: 'bucketName',
|
id: 'bucketName',
|
||||||
title: 'Bucket Name',
|
title: 'Bucket Name',
|
||||||
@@ -291,34 +301,11 @@ export const S3Block: BlockConfig<S3Response> = {
|
|||||||
if (!params.s3Uri) {
|
if (!params.s3Uri) {
|
||||||
throw new Error('S3 Object URL is required')
|
throw new Error('S3 Object URL is required')
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
// Parse S3 URI for get_object
|
accessKeyId: params.accessKeyId,
|
||||||
try {
|
secretAccessKey: params.secretAccessKey,
|
||||||
const url = new URL(params.s3Uri)
|
region: params.getObjectRegion || params.region,
|
||||||
const hostname = url.hostname
|
s3Uri: params.s3Uri,
|
||||||
const bucketName = hostname.split('.')[0]
|
|
||||||
const regionMatch = hostname.match(/s3[.-]([^.]+)\.amazonaws\.com/)
|
|
||||||
const region = regionMatch ? regionMatch[1] : params.region
|
|
||||||
const objectKey = url.pathname.startsWith('/')
|
|
||||||
? url.pathname.substring(1)
|
|
||||||
: url.pathname
|
|
||||||
|
|
||||||
if (!bucketName || !objectKey) {
|
|
||||||
throw new Error('Could not parse S3 URL')
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
accessKeyId: params.accessKeyId,
|
|
||||||
secretAccessKey: params.secretAccessKey,
|
|
||||||
region,
|
|
||||||
bucketName,
|
|
||||||
objectKey,
|
|
||||||
s3Uri: params.s3Uri,
|
|
||||||
}
|
|
||||||
} catch (_error) {
|
|
||||||
throw new Error(
|
|
||||||
'Invalid S3 Object URL format. Expected: https://bucket-name.s3.region.amazonaws.com/path/to/file'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,6 +388,7 @@ export const S3Block: BlockConfig<S3Response> = {
|
|||||||
acl: { type: 'string', description: 'Access control list' },
|
acl: { type: 'string', description: 'Access control list' },
|
||||||
// Download inputs
|
// Download inputs
|
||||||
s3Uri: { type: 'string', description: 'S3 object URL' },
|
s3Uri: { type: 'string', description: 'S3 object URL' },
|
||||||
|
getObjectRegion: { type: 'string', description: 'Optional AWS region override for downloads' },
|
||||||
// List inputs
|
// List inputs
|
||||||
prefix: { type: 'string', description: 'Prefix filter' },
|
prefix: { type: 'string', description: 'Prefix filter' },
|
||||||
maxKeys: { type: 'number', description: 'Maximum results' },
|
maxKeys: { type: 'number', description: 'Maximum results' },
|
||||||
|
|||||||
@@ -196,8 +196,6 @@ export interface SubBlockConfig {
|
|||||||
type: SubBlockType
|
type: SubBlockType
|
||||||
mode?: 'basic' | 'advanced' | 'both' | 'trigger' // Default is 'both' if not specified. 'trigger' means only shown in trigger mode
|
mode?: 'basic' | 'advanced' | 'both' | 'trigger' // Default is 'both' if not specified. 'trigger' means only shown in trigger mode
|
||||||
canonicalParamId?: string
|
canonicalParamId?: string
|
||||||
/** Controls parameter visibility in agent/tool-input context */
|
|
||||||
paramVisibility?: 'user-or-llm' | 'user-only' | 'llm-only' | 'hidden'
|
|
||||||
required?:
|
required?:
|
||||||
| boolean
|
| boolean
|
||||||
| {
|
| {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
buildCanonicalIndex,
|
buildCanonicalIndex,
|
||||||
type CanonicalIndex,
|
type CanonicalIndex,
|
||||||
type CanonicalModeOverrides,
|
|
||||||
evaluateSubBlockCondition,
|
evaluateSubBlockCondition,
|
||||||
getCanonicalValues,
|
getCanonicalValues,
|
||||||
isCanonicalPair,
|
isCanonicalPair,
|
||||||
@@ -13,10 +12,7 @@ import type { SubBlockConfig as BlockSubBlockConfig } from '@/blocks/types'
|
|||||||
export {
|
export {
|
||||||
buildCanonicalIndex,
|
buildCanonicalIndex,
|
||||||
type CanonicalIndex,
|
type CanonicalIndex,
|
||||||
type CanonicalModeOverrides,
|
|
||||||
evaluateSubBlockCondition,
|
evaluateSubBlockCondition,
|
||||||
isCanonicalPair,
|
|
||||||
resolveCanonicalMode,
|
|
||||||
type SubBlockCondition,
|
type SubBlockCondition,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,13 @@
|
|||||||
import { createLogger } from '@sim/logger'
|
import { createLogger } from '@sim/logger'
|
||||||
import { extractInputFieldsFromBlocks } from '@/lib/workflows/input-format'
|
import { extractInputFieldsFromBlocks } from '@/lib/workflows/input-format'
|
||||||
import {
|
import {
|
||||||
buildCanonicalIndex,
|
|
||||||
type CanonicalModeOverrides,
|
|
||||||
evaluateSubBlockCondition,
|
evaluateSubBlockCondition,
|
||||||
isCanonicalPair,
|
|
||||||
resolveCanonicalMode,
|
|
||||||
type SubBlockCondition,
|
type SubBlockCondition,
|
||||||
} from '@/lib/workflows/subblocks/visibility'
|
} from '@/lib/workflows/subblocks/visibility'
|
||||||
import type { SubBlockConfig as BlockSubBlockConfig, GenerationType } from '@/blocks/types'
|
import type { SubBlockConfig as BlockSubBlockConfig } from '@/blocks/types'
|
||||||
import { safeAssign } from '@/tools/safe-assign'
|
import { safeAssign } from '@/tools/safe-assign'
|
||||||
import { isEmptyTagValue } from '@/tools/shared/tags'
|
import { isEmptyTagValue } from '@/tools/shared/tags'
|
||||||
import type { OAuthConfig, ParameterVisibility, ToolConfig } from '@/tools/types'
|
import type { ParameterVisibility, ToolConfig } from '@/tools/types'
|
||||||
import { getTool } from '@/tools/utils'
|
import { getTool } from '@/tools/utils'
|
||||||
|
|
||||||
const logger = createLogger('ToolsParams')
|
const logger = createLogger('ToolsParams')
|
||||||
@@ -68,14 +64,6 @@ export interface UIComponentConfig {
|
|||||||
mode?: 'basic' | 'advanced' | 'both' | 'trigger'
|
mode?: 'basic' | 'advanced' | 'both' | 'trigger'
|
||||||
/** The actual subblock ID this config was derived from */
|
/** The actual subblock ID this config was derived from */
|
||||||
actualSubBlockId?: string
|
actualSubBlockId?: string
|
||||||
/** Wand configuration for AI assistance */
|
|
||||||
wandConfig?: {
|
|
||||||
enabled: boolean
|
|
||||||
prompt: string
|
|
||||||
generationType?: GenerationType
|
|
||||||
placeholder?: string
|
|
||||||
maintainHistory?: boolean
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SubBlockConfig {
|
export interface SubBlockConfig {
|
||||||
@@ -339,7 +327,6 @@ export function getToolParametersConfig(
|
|||||||
canonicalParamId: subBlock.canonicalParamId,
|
canonicalParamId: subBlock.canonicalParamId,
|
||||||
mode: subBlock.mode,
|
mode: subBlock.mode,
|
||||||
actualSubBlockId: subBlock.id,
|
actualSubBlockId: subBlock.id,
|
||||||
wandConfig: subBlock.wandConfig,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -825,200 +812,3 @@ export function formatParameterLabel(paramId: string): string {
|
|||||||
// Simple case - just capitalize first letter
|
// Simple case - just capitalize first letter
|
||||||
return paramId.charAt(0).toUpperCase() + paramId.slice(1)
|
return paramId.charAt(0).toUpperCase() + paramId.slice(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* SubBlock IDs that are "structural" — they control tool routing or auth,
|
|
||||||
* not user-facing parameters. These are excluded from tool-input rendering
|
|
||||||
* unless they have an explicit paramVisibility set.
|
|
||||||
*/
|
|
||||||
const STRUCTURAL_SUBBLOCK_IDS = new Set(['operation', 'authMethod', 'destinationType'])
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SubBlock types that represent auth/credential inputs handled separately
|
|
||||||
* by the tool-input OAuth credential selector.
|
|
||||||
*/
|
|
||||||
const AUTH_SUBBLOCK_TYPES = new Set(['oauth-input'])
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SubBlock types that should never appear in tool-input context.
|
|
||||||
*/
|
|
||||||
const EXCLUDED_SUBBLOCK_TYPES = new Set([
|
|
||||||
'tool-input',
|
|
||||||
'skill-input',
|
|
||||||
'condition-input',
|
|
||||||
'eval-input',
|
|
||||||
'webhook-config',
|
|
||||||
'schedule-info',
|
|
||||||
'trigger-save',
|
|
||||||
'input-format',
|
|
||||||
'response-format',
|
|
||||||
'mcp-server-selector',
|
|
||||||
'mcp-tool-selector',
|
|
||||||
'mcp-dynamic-args',
|
|
||||||
'input-mapping',
|
|
||||||
'variables-input',
|
|
||||||
'messages-input',
|
|
||||||
'router-input',
|
|
||||||
'text',
|
|
||||||
])
|
|
||||||
|
|
||||||
export interface SubBlocksForToolInput {
|
|
||||||
toolConfig: ToolConfig
|
|
||||||
subBlocks: BlockSubBlockConfig[]
|
|
||||||
oauthConfig?: OAuthConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns filtered SubBlockConfig[] for rendering in tool-input context.
|
|
||||||
* Uses subblock definitions as the primary source of UI metadata,
|
|
||||||
* getting all features (wandConfig, rich conditions, dependsOn, etc.) for free.
|
|
||||||
*
|
|
||||||
* For blocks without paramVisibility annotations, falls back to inferring
|
|
||||||
* visibility from the tool's param definitions.
|
|
||||||
*/
|
|
||||||
export function getSubBlocksForToolInput(
|
|
||||||
toolId: string,
|
|
||||||
blockType: string,
|
|
||||||
currentValues?: Record<string, unknown>,
|
|
||||||
canonicalModeOverrides?: CanonicalModeOverrides
|
|
||||||
): SubBlocksForToolInput | null {
|
|
||||||
try {
|
|
||||||
const toolConfig = getTool(toolId)
|
|
||||||
if (!toolConfig) {
|
|
||||||
logger.warn(`Tool not found: ${toolId}`)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const blockConfigs = getBlockConfigurations()
|
|
||||||
const blockConfig = blockConfigs[blockType]
|
|
||||||
if (!blockConfig?.subBlocks?.length) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
const allSubBlocks = blockConfig.subBlocks as BlockSubBlockConfig[]
|
|
||||||
const canonicalIndex = buildCanonicalIndex(allSubBlocks)
|
|
||||||
|
|
||||||
// Build values for condition evaluation
|
|
||||||
const values = currentValues || {}
|
|
||||||
const valuesWithOperation = { ...values }
|
|
||||||
if (valuesWithOperation.operation === undefined) {
|
|
||||||
const parts = toolId.split('_')
|
|
||||||
valuesWithOperation.operation =
|
|
||||||
parts.length >= 3 ? parts.slice(2).join('_') : parts[parts.length - 1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build a map of tool param IDs to their resolved visibility
|
|
||||||
const toolParamVisibility: Record<string, ParameterVisibility> = {}
|
|
||||||
for (const [paramId, param] of Object.entries(toolConfig.params || {})) {
|
|
||||||
toolParamVisibility[paramId] =
|
|
||||||
param.visibility ?? (param.required ? 'user-or-llm' : 'user-only')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Track which canonical groups we've already included (to avoid duplicates)
|
|
||||||
const includedCanonicalIds = new Set<string>()
|
|
||||||
|
|
||||||
const filtered: BlockSubBlockConfig[] = []
|
|
||||||
|
|
||||||
for (const sb of allSubBlocks) {
|
|
||||||
// Skip excluded types
|
|
||||||
if (EXCLUDED_SUBBLOCK_TYPES.has(sb.type)) continue
|
|
||||||
|
|
||||||
// Skip trigger-mode-only subblocks
|
|
||||||
if (sb.mode === 'trigger') continue
|
|
||||||
|
|
||||||
// Determine the effective param ID (canonical or subblock id)
|
|
||||||
const effectiveParamId = sb.canonicalParamId || sb.id
|
|
||||||
|
|
||||||
// Resolve paramVisibility: explicit > inferred from tool params > skip
|
|
||||||
let visibility = sb.paramVisibility
|
|
||||||
if (!visibility) {
|
|
||||||
// Infer from structural checks
|
|
||||||
if (STRUCTURAL_SUBBLOCK_IDS.has(sb.id)) {
|
|
||||||
visibility = 'hidden'
|
|
||||||
} else if (AUTH_SUBBLOCK_TYPES.has(sb.type)) {
|
|
||||||
visibility = 'hidden'
|
|
||||||
} else if (
|
|
||||||
sb.password &&
|
|
||||||
(sb.id === 'botToken' || sb.id === 'accessToken' || sb.id === 'apiKey')
|
|
||||||
) {
|
|
||||||
// Auth tokens without explicit paramVisibility are hidden
|
|
||||||
// (they're handled by the OAuth credential selector or structurally)
|
|
||||||
// But only if they don't have a matching tool param
|
|
||||||
if (!(sb.id in toolParamVisibility)) {
|
|
||||||
visibility = 'hidden'
|
|
||||||
} else {
|
|
||||||
visibility = toolParamVisibility[sb.id] || 'user-or-llm'
|
|
||||||
}
|
|
||||||
} else if (effectiveParamId in toolParamVisibility) {
|
|
||||||
// Fallback: infer from tool param visibility
|
|
||||||
visibility = toolParamVisibility[effectiveParamId]
|
|
||||||
} else if (sb.id in toolParamVisibility) {
|
|
||||||
visibility = toolParamVisibility[sb.id]
|
|
||||||
} else if (sb.canonicalParamId) {
|
|
||||||
// SubBlock has a canonicalParamId that doesn't directly match a tool param.
|
|
||||||
// This means the block's params() function transforms it before sending to the tool
|
|
||||||
// (e.g. listFolderId → folderId). These are user-facing inputs, default to user-or-llm.
|
|
||||||
visibility = 'user-or-llm'
|
|
||||||
} else {
|
|
||||||
// SubBlock has no corresponding tool param — skip it
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter by visibility: exclude hidden and llm-only
|
|
||||||
if (visibility === 'hidden' || visibility === 'llm-only') continue
|
|
||||||
|
|
||||||
// Evaluate condition against current values
|
|
||||||
if (sb.condition) {
|
|
||||||
const conditionMet = evaluateSubBlockCondition(
|
|
||||||
sb.condition as SubBlockCondition,
|
|
||||||
valuesWithOperation
|
|
||||||
)
|
|
||||||
if (!conditionMet) continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle canonical pairs: only include the active mode variant
|
|
||||||
const canonicalId = canonicalIndex.canonicalIdBySubBlockId[sb.id]
|
|
||||||
if (canonicalId) {
|
|
||||||
const group = canonicalIndex.groupsById[canonicalId]
|
|
||||||
if (group && isCanonicalPair(group)) {
|
|
||||||
if (includedCanonicalIds.has(canonicalId)) continue
|
|
||||||
includedCanonicalIds.add(canonicalId)
|
|
||||||
|
|
||||||
// Determine active mode
|
|
||||||
const mode = resolveCanonicalMode(group, valuesWithOperation, canonicalModeOverrides)
|
|
||||||
if (mode === 'advanced') {
|
|
||||||
// Find the advanced variant
|
|
||||||
const advancedSb = allSubBlocks.find((s) => group.advancedIds.includes(s.id))
|
|
||||||
if (advancedSb) {
|
|
||||||
filtered.push({ ...advancedSb, paramVisibility: visibility })
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Include basic variant (current sb if it's the basic one)
|
|
||||||
if (group.basicId === sb.id) {
|
|
||||||
filtered.push({ ...sb, paramVisibility: visibility })
|
|
||||||
} else {
|
|
||||||
const basicSb = allSubBlocks.find((s) => s.id === group.basicId)
|
|
||||||
if (basicSb) {
|
|
||||||
filtered.push({ ...basicSb, paramVisibility: visibility })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Non-canonical, non-hidden, condition-passing subblock
|
|
||||||
filtered.push({ ...sb, paramVisibility: visibility })
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
toolConfig,
|
|
||||||
subBlocks: filtered,
|
|
||||||
oauthConfig: toolConfig.oauth,
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
logger.error('Error getting subblocks for tool input:', error)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -26,6 +26,13 @@ export const s3GetObjectTool: ToolConfig = {
|
|||||||
visibility: 'user-only',
|
visibility: 'user-only',
|
||||||
description: 'Your AWS Secret Access Key',
|
description: 'Your AWS Secret Access Key',
|
||||||
},
|
},
|
||||||
|
region: {
|
||||||
|
type: 'string',
|
||||||
|
required: false,
|
||||||
|
visibility: 'user-only',
|
||||||
|
description:
|
||||||
|
'Optional region override when URL does not include region (e.g., us-east-1, eu-west-1)',
|
||||||
|
},
|
||||||
s3Uri: {
|
s3Uri: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
required: true,
|
required: true,
|
||||||
@@ -37,7 +44,7 @@ export const s3GetObjectTool: ToolConfig = {
|
|||||||
request: {
|
request: {
|
||||||
url: (params) => {
|
url: (params) => {
|
||||||
try {
|
try {
|
||||||
const { bucketName, region, objectKey } = parseS3Uri(params.s3Uri)
|
const { bucketName, region, objectKey } = parseS3Uri(params.s3Uri, params.region)
|
||||||
|
|
||||||
params.bucketName = bucketName
|
params.bucketName = bucketName
|
||||||
params.region = region
|
params.region = region
|
||||||
@@ -46,7 +53,7 @@ export const s3GetObjectTool: ToolConfig = {
|
|||||||
return `https://${bucketName}.s3.${region}.amazonaws.com/${encodeS3PathComponent(objectKey)}`
|
return `https://${bucketName}.s3.${region}.amazonaws.com/${encodeS3PathComponent(objectKey)}`
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Invalid S3 Object URL format. Expected format: https://bucket-name.s3.region.amazonaws.com/path/to/file'
|
'Invalid S3 Object URL. Use a valid S3 URL and optionally provide region if the URL omits it.'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -55,7 +62,7 @@ export const s3GetObjectTool: ToolConfig = {
|
|||||||
try {
|
try {
|
||||||
// Parse S3 URI if not already parsed
|
// Parse S3 URI if not already parsed
|
||||||
if (!params.bucketName || !params.region || !params.objectKey) {
|
if (!params.bucketName || !params.region || !params.objectKey) {
|
||||||
const { bucketName, region, objectKey } = parseS3Uri(params.s3Uri)
|
const { bucketName, region, objectKey } = parseS3Uri(params.s3Uri, params.region)
|
||||||
params.bucketName = bucketName
|
params.bucketName = bucketName
|
||||||
params.region = region
|
params.region = region
|
||||||
params.objectKey = objectKey
|
params.objectKey = objectKey
|
||||||
@@ -102,7 +109,7 @@ export const s3GetObjectTool: ToolConfig = {
|
|||||||
transformResponse: async (response: Response, params) => {
|
transformResponse: async (response: Response, params) => {
|
||||||
// Parse S3 URI if not already parsed
|
// Parse S3 URI if not already parsed
|
||||||
if (!params.bucketName || !params.region || !params.objectKey) {
|
if (!params.bucketName || !params.region || !params.objectKey) {
|
||||||
const { bucketName, region, objectKey } = parseS3Uri(params.s3Uri)
|
const { bucketName, region, objectKey } = parseS3Uri(params.s3Uri, params.region)
|
||||||
params.bucketName = bucketName
|
params.bucketName = bucketName
|
||||||
params.region = region
|
params.region = region
|
||||||
params.objectKey = objectKey
|
params.objectKey = objectKey
|
||||||
|
|||||||
@@ -20,7 +20,10 @@ export function getSignatureKey(
|
|||||||
return kSigning
|
return kSigning
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseS3Uri(s3Uri: string): {
|
export function parseS3Uri(
|
||||||
|
s3Uri: string,
|
||||||
|
fallbackRegion?: string
|
||||||
|
): {
|
||||||
bucketName: string
|
bucketName: string
|
||||||
region: string
|
region: string
|
||||||
objectKey: string
|
objectKey: string
|
||||||
@@ -28,10 +31,55 @@ export function parseS3Uri(s3Uri: string): {
|
|||||||
try {
|
try {
|
||||||
const url = new URL(s3Uri)
|
const url = new URL(s3Uri)
|
||||||
const hostname = url.hostname
|
const hostname = url.hostname
|
||||||
const bucketName = hostname.split('.')[0]
|
const normalizedPath = url.pathname.startsWith('/') ? url.pathname.slice(1) : url.pathname
|
||||||
const regionMatch = hostname.match(/s3[.-]([^.]+)\.amazonaws\.com/)
|
|
||||||
const region = regionMatch ? regionMatch[1] : 'us-east-1'
|
const virtualHostedDualstackMatch = hostname.match(
|
||||||
const objectKey = url.pathname.startsWith('/') ? url.pathname.substring(1) : url.pathname
|
/^(.+)\.s3\.dualstack\.([^.]+)\.amazonaws\.com(?:\.cn)?$/
|
||||||
|
)
|
||||||
|
const virtualHostedRegionalMatch = hostname.match(
|
||||||
|
/^(.+)\.s3[.-]([^.]+)\.amazonaws\.com(?:\.cn)?$/
|
||||||
|
)
|
||||||
|
const virtualHostedGlobalMatch = hostname.match(/^(.+)\.s3\.amazonaws\.com(?:\.cn)?$/)
|
||||||
|
|
||||||
|
const pathStyleDualstackMatch = hostname.match(
|
||||||
|
/^s3\.dualstack\.([^.]+)\.amazonaws\.com(?:\.cn)?$/
|
||||||
|
)
|
||||||
|
const pathStyleRegionalMatch = hostname.match(/^s3[.-]([^.]+)\.amazonaws\.com(?:\.cn)?$/)
|
||||||
|
const pathStyleGlobalMatch = hostname.match(/^s3\.amazonaws\.com(?:\.cn)?$/)
|
||||||
|
|
||||||
|
const isPathStyleHost = Boolean(
|
||||||
|
pathStyleDualstackMatch || pathStyleRegionalMatch || pathStyleGlobalMatch
|
||||||
|
)
|
||||||
|
|
||||||
|
const firstSlashIndex = normalizedPath.indexOf('/')
|
||||||
|
const pathStyleBucketName =
|
||||||
|
firstSlashIndex === -1 ? normalizedPath : normalizedPath.slice(0, firstSlashIndex)
|
||||||
|
const pathStyleObjectKey =
|
||||||
|
firstSlashIndex === -1 ? '' : normalizedPath.slice(firstSlashIndex + 1)
|
||||||
|
|
||||||
|
const bucketName = isPathStyleHost
|
||||||
|
? pathStyleBucketName
|
||||||
|
: (virtualHostedDualstackMatch?.[1] ??
|
||||||
|
virtualHostedRegionalMatch?.[1] ??
|
||||||
|
virtualHostedGlobalMatch?.[1] ??
|
||||||
|
'')
|
||||||
|
|
||||||
|
const rawObjectKey = isPathStyleHost ? pathStyleObjectKey : normalizedPath
|
||||||
|
const objectKey = (() => {
|
||||||
|
try {
|
||||||
|
return decodeURIComponent(rawObjectKey)
|
||||||
|
} catch {
|
||||||
|
return rawObjectKey
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
|
const normalizedFallbackRegion = fallbackRegion?.trim()
|
||||||
|
const regionFromHost =
|
||||||
|
virtualHostedDualstackMatch?.[2] ??
|
||||||
|
virtualHostedRegionalMatch?.[2] ??
|
||||||
|
pathStyleDualstackMatch?.[1] ??
|
||||||
|
pathStyleRegionalMatch?.[1]
|
||||||
|
const region = regionFromHost || normalizedFallbackRegion || 'us-east-1'
|
||||||
|
|
||||||
if (!bucketName || !objectKey) {
|
if (!bucketName || !objectKey) {
|
||||||
throw new Error('Invalid S3 URI format')
|
throw new Error('Invalid S3 URI format')
|
||||||
@@ -40,7 +88,7 @@ export function parseS3Uri(s3Uri: string): {
|
|||||||
return { bucketName, region, objectKey }
|
return { bucketName, region, objectKey }
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Invalid S3 Object URL format. Expected format: https://bucket-name.s3.region.amazonaws.com/path/to/file'
|
'Invalid S3 Object URL format. Expected S3 virtual-hosted or path-style URL with object key.'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user