mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-27 01:11:40 +00:00
87 lines
2.3 KiB
TypeScript
87 lines
2.3 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
|
|
|
import type { CompletionItem } from '../app/interfaces.js'
|
|
import type { GatewayClient } from '../gatewayClient.js'
|
|
import type { CompletionResponse } from '../gatewayTypes.js'
|
|
import { asRpcResult } from '../lib/rpc.js'
|
|
|
|
const TAB_PATH_RE = /((?:["']?(?:[A-Za-z]:[\\/]|\.{1,2}\/|~\/|\/|@|[^"'`\s]+\/))[^\s]*)$/
|
|
|
|
export function useCompletion(input: string, blocked: boolean, gw: GatewayClient) {
|
|
const [completions, setCompletions] = useState<CompletionItem[]>([])
|
|
const [compIdx, setCompIdx] = useState(0)
|
|
const [compReplace, setCompReplace] = useState(0)
|
|
const ref = useRef('')
|
|
|
|
useEffect(() => {
|
|
const clear = () => {
|
|
setCompletions(prev => (prev.length ? [] : prev))
|
|
setCompIdx(prev => (prev ? 0 : prev))
|
|
setCompReplace(prev => (prev ? 0 : prev))
|
|
}
|
|
|
|
if (blocked) {
|
|
ref.current = ''
|
|
clear()
|
|
|
|
return
|
|
}
|
|
|
|
if (input === ref.current) {
|
|
return
|
|
}
|
|
|
|
ref.current = input
|
|
|
|
const isSlash = input.startsWith('/')
|
|
const pathWord = !isSlash ? (input.match(TAB_PATH_RE)?.[1] ?? null) : null
|
|
|
|
if (!isSlash && !pathWord) {
|
|
clear()
|
|
|
|
return
|
|
}
|
|
|
|
const t = setTimeout(() => {
|
|
if (ref.current !== input) {
|
|
return
|
|
}
|
|
|
|
const req = isSlash
|
|
? gw.request<CompletionResponse>('complete.slash', { text: input })
|
|
: gw.request<CompletionResponse>('complete.path', { word: pathWord })
|
|
|
|
req
|
|
.then(raw => {
|
|
const r = asRpcResult<CompletionResponse>(raw)
|
|
|
|
if (ref.current !== input) {
|
|
return
|
|
}
|
|
|
|
setCompletions(r?.items ?? [])
|
|
setCompIdx(0)
|
|
setCompReplace(isSlash ? (r?.replace_from ?? 1) : input.length - (pathWord?.length ?? 0))
|
|
})
|
|
.catch((e: unknown) => {
|
|
if (ref.current !== input) {
|
|
return
|
|
}
|
|
|
|
setCompletions([
|
|
{
|
|
text: '',
|
|
display: 'completion unavailable',
|
|
meta: e instanceof Error && e.message ? e.message : 'unavailable'
|
|
}
|
|
])
|
|
setCompIdx(0)
|
|
setCompReplace(isSlash ? 1 : input.length - (pathWord?.length ?? 0))
|
|
})
|
|
}, 60)
|
|
|
|
return () => clearTimeout(t)
|
|
}, [blocked, gw, input])
|
|
|
|
return { completions, compIdx, setCompIdx, compReplace }
|
|
}
|