@@ -25,6 +25,7 @@ import {
2525import { useLoadBalancedEnvironment } from "../hooks/useLoadBalancedEnvironment" ;
2626import { useScratchProject } from "../hooks/useScratchProject" ;
2727import { useStableCallback } from "../hooks/useStableCallback" ;
28+ import { useProjectScriptEditing } from "./chat/useProjectScriptEditing" ;
2829import { useThreadTerminalActions } from "./terminal/useThreadTerminalActions" ;
2930import { usePendingThreadRequests } from "./chat/usePendingThreadRequests" ;
3031import { isScratchProject } from "@t3tools/client-runtime/state/projects" ;
@@ -50,7 +51,6 @@ import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
5051import * as Schema from "effect/Schema" ;
5152import {
5253 AuthOrchestrationOperateScope ,
53- AuthSettingsWriteScope ,
5454 AuthSourceControlWriteScope ,
5555 AuthPreviewOperateScope ,
5656 AuthTerminalReadScope ,
@@ -60,7 +60,6 @@ import {
6060 CommandId ,
6161 DEFAULT_MODEL ,
6262 isProviderNativeSubagentThread ,
63- EnvironmentAuthorizationError ,
6463 type EnvironmentId ,
6564 type MessageId ,
6665 type ModelSelection ,
@@ -73,7 +72,6 @@ import {
7372 type ThreadId ,
7473 type RunId ,
7574 type RuntimeRequestId ,
76- type KeybindingCommand ,
7775 PROVIDER_SEND_TURN_MAX_ATTACHMENTS ,
7876 ProviderInteractionMode ,
7977 ProviderDriverKind ,
@@ -167,7 +165,6 @@ import {
167165 squashAtomCommandFailure ,
168166 type AtomCommandResult ,
169167} from "@t3tools/client-runtime/state/runtime" ;
170- import * as Cause from "effect/Cause" ;
171168import { AsyncResult } from "effect/reactivity" ;
172169import { isElectron } from "../env" ;
173170import { readLocalApi } from "../localApi" ;
@@ -292,18 +289,7 @@ import {
292289import { cn , randomUUID } from "~/lib/utils" ;
293290import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar" ;
294291import { stackedThreadToast , toastManager } from "./ui/toast" ;
295- import {
296- decodeProjectScriptKeybindingRule ,
297- keybindingValueForCommand ,
298- } from "~/lib/projectScriptKeybindings" ;
299- import { type NewProjectScriptInput } from "./ProjectScriptsControl" ;
300- import {
301- buildProjectScript ,
302- commandForProjectScript ,
303- nextProjectScriptId ,
304- projectScriptIdFromCommand ,
305- releaseClaimedRoles ,
306- } from "~/projectScripts" ;
292+ import { projectScriptIdFromCommand } from "~/projectScripts" ;
307293import { newDraftId , newMessageId , newThreadId } from "~/lib/utils" ;
308294import { registerFaviconProjectForThread } from "~/browserFaviconStore" ;
309295import { getProviderModelCapabilities } from "../providerModels" ;
@@ -1101,15 +1087,6 @@ export default function ChatView(props: ChatViewProps) {
11011087 currentRouteThreadKeyRef . current = null ;
11021088 } ;
11031089 } , [ routeThreadKey ] ) ;
1104- const updateProjectScriptSettings = useAtomCommand ( serverEnvironment . updateSettings , {
1105- reportFailure : false ,
1106- } ) ;
1107- const upsertKeybinding = useAtomCommand ( serverEnvironment . upsertKeybinding , {
1108- reportFailure : false ,
1109- } ) ;
1110- const removeKeybinding = useAtomCommand ( serverEnvironment . removeKeybinding , {
1111- reportFailure : false ,
1112- } ) ;
11131090 const createThread = useOrchestrationCommand ( threadEnvironment . create , { reportFailure : false } ) ;
11141091 const deleteThread = useOrchestrationCommand ( threadEnvironment . delete , { reportFailure : false } ) ;
11151092 const updateThreadMetadata = useOrchestrationCommand ( threadEnvironment . updateMetadata , {
@@ -3797,223 +3774,16 @@ export default function ChatView(props: ChatViewProps) {
37973774 } ;
37983775 const runShellCommandStable = useStableCallback ( runShellCommand ) ;
37993776
3800- const supportsProjectSettingsOverrides =
3801- environmentById . get ( environmentId ) ?. serverConfig ?. environment . capabilities
3802- . projectSettingsOverrides === true ;
3803- const persistProjectScripts = async ( input : {
3804- projectId : ProjectId ;
3805- projectCwd : string ;
3806- previousScripts : ReadonlyArray < ProjectScript > ;
3807- nextScripts : ReadonlyArray < ProjectScript > ;
3808- keybinding : NewProjectScriptInput [ "keybinding" ] ;
3809- keybindingCommand : KeybindingCommand | null ;
3810- } ) : Promise < AtomCommandResult < void , unknown > > => {
3811- const previousKeybinding = keybindingValueForCommand (
3812- appAtomRegistry . get ( serverEnvironment . configValueAtom ( environmentId ) ) ?. keybindings ?? [ ] ,
3813- input . keybindingCommand ,
3814- ) ;
3815- const isDeletingScript = ! input . nextScripts . some (
3816- ( script ) => commandForProjectScript ( script . id ) === input . keybindingCommand ,
3817- ) ;
3818- const changesKeybinding =
3819- input . keybinding !== undefined &&
3820- ( input . keybinding ?. trim ( ) || null ) !== previousKeybinding &&
3821- ( ! isDeletingScript || readEnvironmentScope ( environmentId , AuthSettingsWriteScope ) ) ;
3822- if ( changesKeybinding && ! readEnvironmentScope ( environmentId , AuthSettingsWriteScope ) ) {
3823- return AsyncResult . failure (
3824- Cause . fail (
3825- new EnvironmentAuthorizationError ( {
3826- requiredScope : AuthSettingsWriteScope ,
3827- message : "This connection cannot change keyboard shortcuts." ,
3828- } ) ,
3829- ) ,
3830- ) ;
3831- }
3832- const keybindingRule = changesKeybinding
3833- ? decodeProjectScriptKeybindingRule ( {
3834- keybinding : input . keybinding ,
3835- command : input . keybindingCommand ,
3836- } )
3837- : null ;
3838- const updateResult = mapAtomCommandResult (
3839- await updateProjectScriptSettings ( {
3840- environmentId,
3841- input : {
3842- patch : supportsProjectSettingsOverrides
3843- ? {
3844- projectSettingsOverrides : {
3845- [ input . projectId ] : {
3846- ...settings . projectSettingsOverrides [ input . projectId ] ,
3847- defaultProjectScripts : input . nextScripts ,
3848- } ,
3849- } ,
3850- }
3851- : {
3852- projectScriptOverrides : {
3853- [ input . projectId ] : input . nextScripts ,
3854- } ,
3855- } ,
3856- } ,
3857- } ) ,
3858- ( ) => undefined ,
3859- ) ;
3860- if ( updateResult . _tag === "Failure" ) {
3861- return updateResult ;
3862- }
3863-
3864- if ( ! changesKeybinding ) return updateResult ;
3865- if ( ! readEnvironmentScope ( environmentId , AuthSettingsWriteScope ) ) {
3866- return AsyncResult . failure (
3867- Cause . fail (
3868- new EnvironmentAuthorizationError ( {
3869- requiredScope : AuthSettingsWriteScope ,
3870- message : isDeletingScript
3871- ? "The script was deleted, but its keyboard shortcut could not be removed because permission changed."
3872- : "The script was saved, but this connection can no longer change keyboard shortcuts." ,
3873- } ) ,
3874- ) ,
3875- ) ;
3876- }
3877-
3878- const scriptId = input . keybindingCommand
3879- ? projectScriptIdFromCommand ( input . keybindingCommand )
3880- : null ;
3881- if ( ! keybindingRule && ! input . previousScripts . some ( ( script ) => script . id === scriptId ) ) {
3882- return updateResult ;
3883- }
3884- const retainedElsewhere =
3885- ! input . nextScripts . some ( ( script ) => script . id === scriptId ) &&
3886- ( settings . defaultProjectScripts . some ( ( script ) => script . id === scriptId ) ||
3887- Object . entries ( settings . projectSettingsOverrides ) . some (
3888- ( [ projectId , entry ] ) =>
3889- projectId !== input . projectId &&
3890- entry . defaultProjectScripts ?. some ( ( script ) => script . id === scriptId ) ,
3891- ) ||
3892- allProjects . some (
3893- ( other ) =>
3894- other . environmentId === environmentId &&
3895- other . id !== input . projectId &&
3896- resolveProjectScripts ( settings , other ) . some ( ( script ) => script . id === scriptId ) ,
3897- ) ) ;
3898- if ( ! keybindingRule && retainedElsewhere ) return updateResult ;
3899-
3900- const previousRules = (
3901- environmentById . get ( environmentId ) ?. serverConfig ?. keybindings ?? [ ]
3902- ) . flatMap ( ( binding ) => {
3903- if ( binding . command !== input . keybindingCommand || binding . whenAst ) return [ ] ;
3904- const previous = decodeProjectScriptKeybindingRule ( {
3905- keybinding : keybindingValueForCommand ( [ binding ] , input . keybindingCommand ) ,
3906- command : input . keybindingCommand ,
3907- } ) ;
3908- return previous ? [ previous ] : [ ] ;
3909- } ) ;
3910- const previous = previousRules . at ( - 1 ) ;
3911- for ( const rule of keybindingRule ? previousRules . slice ( 0 , - 1 ) : previousRules ) {
3912- const result = await removeKeybinding ( { environmentId, input : rule } ) ;
3913- if ( result . _tag === "Failure" ) return mapAtomCommandResult ( result , ( ) => undefined ) ;
3914- }
3915- return keybindingRule
3916- ? mapAtomCommandResult (
3917- await upsertKeybinding ( {
3918- environmentId,
3919- input :
3920- previous && previous . key !== keybindingRule . key
3921- ? { ...keybindingRule , replace : previous }
3922- : keybindingRule ,
3923- } ) ,
3924- ( ) => undefined ,
3925- )
3926- : updateResult ;
3927- } ;
3928- const saveProjectScript = async (
3929- input : NewProjectScriptInput ,
3930- ) : Promise < AtomCommandResult < void , unknown > > => {
3931- if ( ! activeProject ) {
3932- return AsyncResult . success ( undefined ) ;
3933- }
3934- const nextId = nextProjectScriptId (
3935- input . name ,
3936- activeProjectScripts . map ( ( script ) => script . id ) ,
3937- ) ;
3938- const nextScript = buildProjectScript ( nextId , input ) ;
3939- const nextScripts = [
3940- ...activeProjectScripts . map ( ( script ) => releaseClaimedRoles ( script , input ) ) ,
3941- nextScript ,
3942- ] ;
3943-
3944- return persistProjectScripts ( {
3945- projectId : activeProject . id ,
3946- projectCwd : activeProject . workspaceRoot ,
3947- previousScripts : activeProjectScripts ,
3948- nextScripts,
3949- keybinding : input . keybinding ,
3950- keybindingCommand : commandForProjectScript ( nextId ) ,
3951- } ) ;
3952- } ;
3953- const saveProjectScriptStable = useStableCallback ( saveProjectScript ) ;
3954- const updateProjectScript = async (
3955- scriptId : string ,
3956- input : NewProjectScriptInput ,
3957- ) : Promise < AtomCommandResult < void , unknown > > => {
3958- if ( ! activeProject ) {
3959- return AsyncResult . success ( undefined ) ;
3960- }
3961- const existingScript = activeProjectScripts . find ( ( script ) => script . id === scriptId ) ;
3962- if ( ! existingScript ) {
3963- return AsyncResult . failure ( Cause . fail ( new Error ( "Script not found." ) ) ) ;
3964- }
3965-
3966- const updatedScript = buildProjectScript ( existingScript . id , input ) ;
3967- const nextScripts = activeProjectScripts . map ( ( script ) =>
3968- script . id === scriptId ? updatedScript : releaseClaimedRoles ( script , input ) ,
3969- ) ;
3970-
3971- return persistProjectScripts ( {
3972- projectId : activeProject . id ,
3973- projectCwd : activeProject . workspaceRoot ,
3974- previousScripts : activeProjectScripts ,
3975- nextScripts,
3976- keybinding : input . keybinding ,
3977- keybindingCommand : commandForProjectScript ( scriptId ) ,
3978- } ) ;
3979- } ;
3980- const updateProjectScriptStable = useStableCallback ( updateProjectScript ) ;
3981- const deleteProjectScript = async (
3982- scriptId : string ,
3983- ) : Promise < AtomCommandResult < void , unknown > > => {
3984- if ( ! activeProject ) {
3985- return AsyncResult . success ( undefined ) ;
3986- }
3987- const nextScripts = activeProjectScripts . filter ( ( script ) => script . id !== scriptId ) ;
3988-
3989- const deletedName = activeProjectScripts . find ( ( s ) => s . id === scriptId ) ?. name ;
3990-
3991- const result = await persistProjectScripts ( {
3992- projectId : activeProject . id ,
3993- projectCwd : activeProject . workspaceRoot ,
3994- previousScripts : activeProjectScripts ,
3995- nextScripts,
3996- keybinding : null ,
3997- keybindingCommand : commandForProjectScript ( scriptId ) ,
3998- } ) ;
3999- if ( result . _tag === "Success" ) {
4000- toastManager . add ( {
4001- type : "success" ,
4002- title : `Deleted action "${ deletedName ?? "Unknown" } "` ,
4003- } ) ;
4004- } else if ( ! isAtomCommandInterrupted ( result ) ) {
4005- const error = squashAtomCommandFailure ( result ) ;
4006- toastManager . add (
4007- stackedThreadToast ( {
4008- type : "error" ,
4009- title : "Could not delete action" ,
4010- description : error instanceof Error ? error . message : "An unexpected error occurred." ,
4011- } ) ,
4012- ) ;
4013- }
4014- return result ;
4015- } ;
4016- const deleteProjectScriptStable = useStableCallback ( deleteProjectScript ) ;
3777+ const {
3778+ saveProjectScript : saveProjectScriptStable ,
3779+ updateProjectScript : updateProjectScriptStable ,
3780+ deleteProjectScript : deleteProjectScriptStable ,
3781+ } = useProjectScriptEditing ( {
3782+ environmentId,
3783+ project : activeProject ,
3784+ scripts : activeProjectScripts ,
3785+ settings,
3786+ } ) ;
40173787
40183788 const handleRuntimeModeChange = ( mode : RuntimeMode ) => {
40193789 if ( mode === runtimeMode ) return ;
0 commit comments