mirror of
https://github.com/getpaseo/paseo.git
synced 2026-07-29 12:01:31 +00:00
319 lines
8.6 KiB
TypeScript
319 lines
8.6 KiB
TypeScript
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
import type { UserComposerAttachment } from "@/attachments/types";
|
|
import type { DraftAgentControlsProps } from "@/composer/agent-controls";
|
|
import type { DraftCommandConfig } from "@/hooks/use-agent-commands-query";
|
|
import {
|
|
useAgentFormState,
|
|
type CreateAgentInitialValues,
|
|
type UseAgentFormStateResult,
|
|
} from "@/hooks/use-agent-form-state";
|
|
import { useDraftAgentFeatures } from "@/hooks/use-draft-agent-features";
|
|
import {
|
|
areAttachmentsEqual,
|
|
buildDraftAgentControls,
|
|
hasDraftContent,
|
|
resolveDraftKey,
|
|
type DraftKeyInput,
|
|
} from "@/composer/draft/input-draft-core";
|
|
import {
|
|
buildDraftCommandConfig,
|
|
resolveEffectiveComposerModelId,
|
|
resolveEffectiveComposerThinkingOptionId,
|
|
type ProviderSelectionState,
|
|
} from "@/provider-selection/provider-selection";
|
|
import { useDraftStore } from "@/stores/draft-store";
|
|
|
|
type AttachmentUpdater =
|
|
| UserComposerAttachment[]
|
|
| ((prev: UserComposerAttachment[]) => UserComposerAttachment[]);
|
|
|
|
interface AgentInputDraftComposerOptions {
|
|
initialServerId: string | null;
|
|
initialValues?: CreateAgentInitialValues;
|
|
initialFeatureValues?: Record<string, unknown>;
|
|
isVisible?: boolean;
|
|
onlineServerIds?: string[];
|
|
lockedWorkingDir?: string;
|
|
}
|
|
|
|
interface UseAgentInputDraftInput {
|
|
draftKey: DraftKeyInput;
|
|
composer?: AgentInputDraftComposerOptions;
|
|
}
|
|
|
|
type DraftComposerState = UseAgentFormStateResult & {
|
|
workingDir: string;
|
|
effectiveModelId: string;
|
|
effectiveThinkingOptionId: string;
|
|
featureValues: Record<string, unknown> | undefined;
|
|
agentControls: DraftAgentControlsProps;
|
|
commandDraftConfig: DraftCommandConfig | undefined;
|
|
};
|
|
|
|
export interface AgentInputDraft {
|
|
text: string;
|
|
setText: (text: string) => void;
|
|
attachments: UserComposerAttachment[];
|
|
setAttachments: (updater: AttachmentUpdater) => void;
|
|
clear: (lifecycle: "sent" | "abandoned") => void;
|
|
isHydrated: boolean;
|
|
composerState: DraftComposerState | null;
|
|
}
|
|
|
|
export function useAgentInputDraft(input: UseAgentInputDraftInput): AgentInputDraft {
|
|
const composerOptions = input.composer ?? null;
|
|
const formState = useAgentFormState({
|
|
initialServerId: composerOptions?.initialServerId ?? null,
|
|
initialValues: composerOptions?.initialValues,
|
|
isVisible: composerOptions?.isVisible ?? false,
|
|
isCreateFlow: true,
|
|
onlineServerIds: composerOptions?.onlineServerIds ?? [],
|
|
});
|
|
const draftKey = useMemo(
|
|
() =>
|
|
resolveDraftKey({
|
|
draftKey: input.draftKey,
|
|
selectedServerId: formState.selectedServerId,
|
|
}),
|
|
[formState.selectedServerId, input.draftKey],
|
|
);
|
|
const [text, setText] = useState("");
|
|
const [attachments, setAttachmentsState] = useState<UserComposerAttachment[]>([]);
|
|
const [isHydrated, setIsHydrated] = useState(false);
|
|
const draftGenerationRef = useRef(0);
|
|
const hydratedGenerationRef = useRef(0);
|
|
|
|
const setAttachments = useCallback((updater: AttachmentUpdater) => {
|
|
setAttachmentsState((previousAttachments) => {
|
|
if (typeof updater === "function") {
|
|
return updater(previousAttachments);
|
|
}
|
|
return updater;
|
|
});
|
|
}, []);
|
|
|
|
const clear = useCallback(
|
|
(lifecycle: "sent" | "abandoned") => {
|
|
const store = useDraftStore.getState();
|
|
store.clearDraftInput({ draftKey, lifecycle });
|
|
|
|
const generation = store.beginDraftGeneration(draftKey);
|
|
draftGenerationRef.current = generation;
|
|
hydratedGenerationRef.current = generation;
|
|
|
|
setText("");
|
|
setAttachmentsState([]);
|
|
setIsHydrated(true);
|
|
},
|
|
[draftKey],
|
|
);
|
|
|
|
useEffect(() => {
|
|
const store = useDraftStore.getState();
|
|
const generation = store.beginDraftGeneration(draftKey);
|
|
draftGenerationRef.current = generation;
|
|
hydratedGenerationRef.current = 0;
|
|
|
|
setText("");
|
|
setAttachmentsState([]);
|
|
setIsHydrated(false);
|
|
|
|
let cancelled = false;
|
|
|
|
void (async () => {
|
|
const draft = await store.hydrateDraftInput({
|
|
draftKey,
|
|
});
|
|
if (cancelled) {
|
|
return;
|
|
}
|
|
if (!useDraftStore.getState().isDraftGenerationCurrent({ draftKey, generation })) {
|
|
return;
|
|
}
|
|
|
|
if (draft) {
|
|
setText(draft.text);
|
|
setAttachmentsState(draft.attachments);
|
|
}
|
|
|
|
hydratedGenerationRef.current = generation;
|
|
setIsHydrated(true);
|
|
})();
|
|
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [draftKey]);
|
|
|
|
useEffect(() => {
|
|
const currentGeneration = draftGenerationRef.current;
|
|
if (currentGeneration <= 0) {
|
|
return;
|
|
}
|
|
|
|
const store = useDraftStore.getState();
|
|
const isCurrentGeneration = store.isDraftGenerationCurrent({
|
|
draftKey,
|
|
generation: currentGeneration,
|
|
});
|
|
if (!isCurrentGeneration) {
|
|
return;
|
|
}
|
|
if (hydratedGenerationRef.current !== currentGeneration) {
|
|
return;
|
|
}
|
|
|
|
const existing = store.getDraftInput(draftKey);
|
|
const isSameDraft =
|
|
existing !== undefined &&
|
|
existing.text === text &&
|
|
areAttachmentsEqual({
|
|
left: existing.attachments,
|
|
right: attachments,
|
|
});
|
|
if (isSameDraft) {
|
|
return;
|
|
}
|
|
|
|
if (!hasDraftContent({ text, attachments })) {
|
|
if (existing) {
|
|
store.clearDraftInput({ draftKey, lifecycle: "abandoned" });
|
|
}
|
|
return;
|
|
}
|
|
|
|
store.saveDraftInput({
|
|
draftKey,
|
|
draft: {
|
|
text,
|
|
attachments,
|
|
},
|
|
});
|
|
}, [attachments, draftKey, text]);
|
|
|
|
const lockedWorkingDir = composerOptions?.lockedWorkingDir?.trim() ?? "";
|
|
useEffect(() => {
|
|
if (!composerOptions || !lockedWorkingDir) {
|
|
return;
|
|
}
|
|
if (formState.workingDir.trim() === lockedWorkingDir) {
|
|
return;
|
|
}
|
|
formState.setWorkingDir(lockedWorkingDir);
|
|
}, [composerOptions, formState, lockedWorkingDir]);
|
|
|
|
const providerSelection = useMemo<ProviderSelectionState>(
|
|
() => ({
|
|
provider: formState.selectedProvider,
|
|
modelId: formState.selectedModel,
|
|
modeId: formState.selectedMode,
|
|
thinkingOptionId: formState.selectedThinkingOptionId,
|
|
availableModels: formState.availableModels,
|
|
modeOptions: formState.modeOptions,
|
|
}),
|
|
[
|
|
formState.availableModels,
|
|
formState.modeOptions,
|
|
formState.selectedMode,
|
|
formState.selectedModel,
|
|
formState.selectedProvider,
|
|
formState.selectedThinkingOptionId,
|
|
],
|
|
);
|
|
|
|
const effectiveModelId = useMemo(
|
|
() => resolveEffectiveComposerModelId(providerSelection),
|
|
[providerSelection],
|
|
);
|
|
|
|
const effectiveThinkingOptionId = useMemo(
|
|
() => resolveEffectiveComposerThinkingOptionId(providerSelection, effectiveModelId),
|
|
[effectiveModelId, providerSelection],
|
|
);
|
|
|
|
const workingDir = lockedWorkingDir || formState.workingDir;
|
|
const {
|
|
features: draftFeatures,
|
|
featureValues: draftFeatureValues,
|
|
setFeatureValue: setDraftFeatureValue,
|
|
} = useDraftAgentFeatures({
|
|
serverId: formState.selectedServerId,
|
|
provider: formState.selectedProvider,
|
|
cwd: workingDir,
|
|
modeId: formState.selectedMode,
|
|
modelId: effectiveModelId,
|
|
thinkingOptionId: effectiveThinkingOptionId,
|
|
initialFeatureValues: composerOptions?.initialFeatureValues,
|
|
});
|
|
|
|
const commandDraftConfig = useMemo(
|
|
() =>
|
|
composerOptions
|
|
? buildDraftCommandConfig({
|
|
selection: providerSelection,
|
|
cwd: workingDir,
|
|
effectiveModelId,
|
|
effectiveThinkingOptionId,
|
|
featureValues: draftFeatureValues,
|
|
})
|
|
: undefined,
|
|
[
|
|
composerOptions,
|
|
effectiveModelId,
|
|
effectiveThinkingOptionId,
|
|
draftFeatureValues,
|
|
providerSelection,
|
|
workingDir,
|
|
],
|
|
);
|
|
|
|
const composerState = useMemo<DraftComposerState | null>(() => {
|
|
if (!composerOptions) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
...formState,
|
|
workingDir,
|
|
effectiveModelId,
|
|
effectiveThinkingOptionId,
|
|
featureValues: draftFeatureValues,
|
|
agentControls: buildDraftAgentControls({
|
|
formState,
|
|
features: draftFeatures,
|
|
onSetFeature: setDraftFeatureValue,
|
|
}),
|
|
commandDraftConfig,
|
|
};
|
|
}, [
|
|
commandDraftConfig,
|
|
composerOptions,
|
|
effectiveModelId,
|
|
effectiveThinkingOptionId,
|
|
draftFeatures,
|
|
draftFeatureValues,
|
|
formState,
|
|
setDraftFeatureValue,
|
|
workingDir,
|
|
]);
|
|
|
|
return {
|
|
text,
|
|
setText,
|
|
attachments,
|
|
setAttachments,
|
|
clear,
|
|
isHydrated,
|
|
composerState,
|
|
};
|
|
}
|
|
|
|
export const __private__ = {
|
|
resolveDraftKey,
|
|
resolveEffectiveComposerModelId,
|
|
resolveEffectiveComposerThinkingOptionId,
|
|
buildDraftCommandConfig,
|
|
buildDraftComposerCommandConfig: buildDraftCommandConfig,
|
|
buildDraftAgentControls,
|
|
};
|