Skip to content

Commit c80827f

Browse files
authored
feat(agent): added workflow, kb, and function as a tool for agent block, fix keyboard nav in tool input (#2107)
* feat(agent): added workflow, kb, and function as a tool for agent block * fix keyboard nav and keyboard selection in tool-inp * ack PR comments * remove custom tool changes * fixed kb tools for agent * cleanup
1 parent a5b7897 commit c80827f

File tree

16 files changed

+1180
-616
lines changed

16 files changed

+1180
-616
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/components/tool-command/tool-command.tsx

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type React from 'react'
2-
import {
1+
import React, {
32
createContext,
43
type ReactNode,
54
useCallback,
@@ -19,6 +18,7 @@ type CommandContextType = {
1918
registerItem: (id: string) => void
2019
unregisterItem: (id: string) => void
2120
selectItem: (id: string) => void
21+
handleKeyDown: (e: React.KeyboardEvent) => void
2222
}
2323

2424
const CommandContext = createContext<CommandContextType | undefined>(undefined)
@@ -31,6 +31,11 @@ const useCommandContext = () => {
3131
return context
3232
}
3333

34+
export const useCommandKeyDown = () => {
35+
const context = useContext(CommandContext)
36+
return context?.handleKeyDown
37+
}
38+
3439
interface CommandProps {
3540
children: ReactNode
3641
className?: string
@@ -71,7 +76,6 @@ export function Command({
7176
const [items, setItems] = useState<string[]>([])
7277
const [filteredItems, setFilteredItems] = useState<string[]>([])
7378

74-
// Use external searchQuery if provided, otherwise use internal state
7579
const searchQuery = externalSearchQuery ?? internalSearchQuery
7680

7781
const registerItem = useCallback((id: string) => {
@@ -101,19 +105,27 @@ export function Command({
101105
return
102106
}
103107

104-
const filtered = items
105-
.map((item) => {
106-
const score = filter ? filter(item, searchQuery) : defaultFilter(item, searchQuery)
107-
return { item, score }
108-
})
109-
.filter((item) => item.score > 0)
110-
.sort((a, b) => b.score - a.score)
111-
.map((item) => item.item)
108+
const filtered = items.filter((item) => {
109+
const score = filter ? filter(item, searchQuery) : defaultFilter(item, searchQuery)
110+
return score > 0
111+
})
112112

113113
setFilteredItems(filtered)
114114
setActiveIndex(filtered.length > 0 ? 0 : -1)
115115
}, [searchQuery, items, filter])
116116

117+
useEffect(() => {
118+
if (activeIndex >= 0 && filteredItems[activeIndex]) {
119+
const activeElement = document.getElementById(filteredItems[activeIndex])
120+
if (activeElement) {
121+
activeElement.scrollIntoView({
122+
behavior: 'smooth',
123+
block: 'nearest',
124+
})
125+
}
126+
}
127+
}, [activeIndex, filteredItems])
128+
117129
const defaultFilter = useCallback((value: string, search: string): number => {
118130
const normalizedValue = value.toLowerCase()
119131
const normalizedSearch = search.toLowerCase()
@@ -158,15 +170,22 @@ export function Command({
158170
registerItem,
159171
unregisterItem,
160172
selectItem,
173+
handleKeyDown,
161174
}),
162-
[searchQuery, activeIndex, filteredItems, registerItem, unregisterItem, selectItem]
175+
[
176+
searchQuery,
177+
activeIndex,
178+
filteredItems,
179+
registerItem,
180+
unregisterItem,
181+
selectItem,
182+
handleKeyDown,
183+
]
163184
)
164185

165186
return (
166187
<CommandContext.Provider value={contextValue}>
167-
<div className={cn('flex w-full flex-col', className)} onKeyDown={handleKeyDown}>
168-
{children}
169-
</div>
188+
<div className={cn('flex w-full flex-col', className)}>{children}</div>
170189
</CommandContext.Provider>
171190
)
172191
}

0 commit comments

Comments
 (0)