Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { useAtomValue } from "@effect/atom-react";
import { type ComposerPathSearchTarget } from "@akeru/client-runtime/state/threads";
import {
createThreadSearchResultsAtomFamily,
makeThreadSearchKey,
type EnvironmentThreadSearchMatch,
} from "@akeru/client-runtime/state/thread-search";
import type {
EnvironmentId,
OrchestrationThread,
ProjectEntryKind,
ThreadId,
} from "@akeru/contracts";
import * as Option from "effect/Option";
import { Atom } from "effect/unstable/reactivity";
import { useEffect, useMemo, useState } from "react";
import { orchestrationEnvironment } from "./orchestration";
import { projectEnvironment } from "./projects";
import { useEnvironmentQuery } from "./query";
import { useEnvironmentThread } from "./threads";
const PROJECT_PATH_SEARCH_DEBOUNCE_MS = 120;
const COMPOSER_PATH_SEARCH_LIMIT = 80;
const THREAD_SEARCH_DEBOUNCE_MS = 200;
const EMPTY_THREAD_SEARCH_MATCHES: ReadonlyArray<EnvironmentThreadSearchMatch> = Object.freeze([]);
const EMPTY_THREAD_SEARCH_ATOM = Atom.make({
matches: EMPTY_THREAD_SEARCH_MATCHES,
isLoading: false,
}).pipe(Atom.withLabel("web:thread-search:empty"));
const threadSearchResultsAtom = createThreadSearchResultsAtomFamily({
getSearchAtom: (environmentId, query) =>
orchestrationEnvironment.threadSearch({
environmentId,
input: { query },
}),
labelPrefix: "web:thread-search",
});
export interface ThreadDetailView {
readonly data: OrchestrationThread | null;
readonly error: string | null;
readonly isPending: boolean;
readonly isDeleted: boolean;
}
export function useDebouncedValue<A>(value: A, delayMs: number): A {
const [debounced, setDebounced] = useState(value);
useEffect(() => {
const timer = window.setTimeout(() => {
setDebounced(value);
}, delayMs);
return () => {
window.clearTimeout(timer);
};
}, [delayMs, value]);
return debounced;
}
export function useThreadSearch(environmentIds: ReadonlyArray<EnvironmentId>, query: string) {
const normalizedQuery = query.trim();
const debouncedQuery = useDebouncedValue(normalizedQuery, THREAD_SEARCH_DEBOUNCE_MS);
const canSearch = environmentIds.length > 0 && normalizedQuery.length >= 2;
const settledQuery = canSearch && normalizedQuery === debouncedQuery ? debouncedQuery : null;
const searchKey = useMemo(
() => (settledQuery === null ? null : makeThreadSearchKey(environmentIds, settledQuery)),
[environmentIds, settledQuery],
);
const result = useAtomValue(
searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearchResultsAtom(searchKey),
);
const isDebouncing = canSearch && normalizedQuery !== debouncedQuery;
return {
matches: isDebouncing ? EMPTY_THREAD_SEARCH_MATCHES : result.matches,
isPending: canSearch && (isDebouncing || result.isLoading),
};
}
export function useThreadDetail(
environmentId: EnvironmentId | null,
threadId: ThreadId | null,
): ThreadDetailView {
const state = useEnvironmentThread(environmentId, threadId);
return {
data: Option.getOrNull(state.data),
error: Option.getOrNull(state.error),
isPending: state.status === "synchronizing",
isDeleted: state.status === "deleted",
};
}
type ProjectPathSearchTarget = ComposerPathSearchTarget & {
readonly kind?: ProjectEntryKind | undefined;
readonly imageOnly?: boolean | undefined;
};
export function areProjectPathSearchTargetsEqual(
left: ProjectPathSearchTarget,
right: ProjectPathSearchTarget,
): boolean {
return (
left.environmentId === right.environmentId &&
left.cwd === right.cwd &&
left.query === right.query &&
left.kind === right.kind &&
left.imageOnly === right.imageOnly
);
}
export function useProjectPathSearch(
target: ProjectPathSearchTarget,
limit: number,
options?: { readonly allowEmptyQuery?: boolean },
) {
const allowEmptyQuery = options?.allowEmptyQuery === true;
const normalizedTarget = useMemo(
() => ({
environmentId: target.environmentId,
cwd: target.cwd,
query: target.query == null ? null : target.query.trim(),
kind: target.kind,
imageOnly: target.imageOnly,
}),
[target.cwd, target.environmentId, target.imageOnly, target.kind, target.query],
);
const debouncedTarget = useDebouncedValue(normalizedTarget, PROJECT_PATH_SEARCH_DEBOUNCE_MS);
const result = useEnvironmentQuery(
debouncedTarget.environmentId !== null &&
debouncedTarget.cwd !== null &&
debouncedTarget.query !== null &&
(allowEmptyQuery || debouncedTarget.query.length > 0)
? projectEnvironment.searchEntries({
environmentId: debouncedTarget.environmentId,
input: {
cwd: debouncedTarget.cwd,
query: debouncedTarget.query,
limit,
...(debouncedTarget.kind ? { kind: debouncedTarget.kind } : {}),
...(debouncedTarget.imageOnly ? { imageOnly: true } : {}),
},
})
: null,
);
return {
entries: result.data?.entries ?? [],
error: result.error,
isPending:
!areProjectPathSearchTargetsEqual(normalizedTarget, debouncedTarget) || result.isPending,
searchedQuery: debouncedTarget.query ?? "",
refresh: result.refresh,
};
}
export function useComposerPathSearch(target: ComposerPathSearchTarget) {
return useProjectPathSearch(target, COMPOSER_PATH_SEARCH_LIMIT);
}