44 isAtomCommandInterrupted ,
55 squashAtomCommandFailure ,
66} from "@t3tools/client-runtime/state/runtime" ;
7- import type { EnvironmentId } from "@t3tools/contracts" ;
7+ import { type EnvironmentId , TRIAGE_PROMPT_FILE_NAME } from "@t3tools/contracts" ;
88
99import { stackedThreadToast , toastManager } from "~/components/ui/toast" ;
1010import { useComposerDraftStore } from "~/composerDraftStore" ;
@@ -16,53 +16,49 @@ import { writeTextToClipboard } from "./useCopyToClipboard";
1616import { useNewThreadHandler } from "./useHandleNewThread" ;
1717import { useScratchProject } from "./useScratchProject" ;
1818
19- const CLI_COMMAND = "npx t3 triage" ;
19+ export const TRIAGE_CLI_COMMAND = "npx t3 triage" ;
2020
21- const copyCliCommandAction = {
22- children : "Copy command" ,
23- onClick : ( ) => void writeTextToClipboard ( CLI_COMMAND , "command" ) ,
24- } ;
21+ const copyTriageCliCommand = ( ) => writeTextToClipboard ( TRIAGE_CLI_COMMAND , "command" ) ;
22+
23+ /** A draft is a triage draft while its message still links the playbook file. */
24+ export const isTriagePrompt = ( prompt : string ) => prompt . includes ( `[ ${ TRIAGE_PROMPT_FILE_NAME } ](` ) ;
2525
2626/**
27- * In-app `t3 triage`: the server writes the session files and clones the
28- * matching source, then a No project draft opens with the prompt filled in.
29- * The user picks the provider in the composer and describes the problem.
27+ * In-app `t3 triage`: the server writes the session files and starts cloning
28+ * the matching source, then a No project draft opens with the playbook linked.
29+ * The draft hero explains the rest; the user picks a provider and describes
30+ * the problem.
3031 */
3132export function useStartTriage ( ) {
3233 const { scratchWorkspaceRootFor } = useScratchProject ( ) ;
3334 const handleNewThread = useNewThreadHandler ( ) ;
3435 const prepare = useAtomCommand ( serverEnvironment . prepareTriage , { reportFailure : false } ) ;
3536 const openScratch = useAtomCommand ( projectEnvironment . openScratch , { reportFailure : false } ) ;
3637
37- return async ( environmentId : EnvironmentId ) => {
38- const toastId = toastManager . add ( { type : "loading" , title : "Preparing triage…" } ) ;
39- const fail = ( description : string ) =>
40- toastManager . update (
41- toastId ,
42- stackedThreadToast ( {
43- type : "error" ,
44- title : "Could not start triage" ,
45- description : `${ description } Run ${ CLI_COMMAND } in a terminal instead.` ,
46- timeout : 0 ,
47- actionProps : copyCliCommandAction ,
48- } ) ,
49- ) ;
38+ // With no draft to show, a toast is the only place to point at the CLI.
39+ const fail = ( description : string ) =>
40+ toastManager . add (
41+ stackedThreadToast ( {
42+ type : "error" ,
43+ title : "Could not start triage" ,
44+ description : `${ description } Run ${ TRIAGE_CLI_COMMAND } in a terminal instead.` ,
45+ timeout : 0 ,
46+ actionProps : { children : "Copy command" , onClick : ( ) => void copyTriageCliCommand ( ) } ,
47+ } ) ,
48+ ) ;
49+ const failWith = (
50+ result : Extract < AtomCommandResult < unknown , unknown > , { readonly _tag : "Failure" } > ,
51+ ) => {
52+ if ( isAtomCommandInterrupted ( result ) ) return ;
53+ const error = squashAtomCommandFailure ( result ) ;
54+ fail ( error instanceof Error ? error . message : "An error occurred." ) ;
55+ } ;
5056
57+ return async ( environmentId : EnvironmentId ) => {
5158 if ( scratchWorkspaceRootFor ( environmentId ) === null ) {
5259 fail ( "This environment cannot start threads without a project." ) ;
5360 return ;
5461 }
55- const failWith = (
56- result : Extract < AtomCommandResult < unknown , unknown > , { readonly _tag : "Failure" } > ,
57- ) => {
58- if ( isAtomCommandInterrupted ( result ) ) {
59- toastManager . close ( toastId ) ;
60- return ;
61- }
62- const error = squashAtomCommandFailure ( result ) ;
63- fail ( error instanceof Error ? error . message : "An error occurred." ) ;
64- } ;
65-
6662 const prepared = await prepare ( {
6763 environmentId,
6864 input : { client : currentClientMetadata ( ) } ,
@@ -78,12 +74,5 @@ export function useStartTriage() {
7874 return ;
7975 }
8076 useComposerDraftStore . getState ( ) . setPrompt ( opened . draftId , prepared . value . prompt ) ;
81- toastManager . update ( toastId , {
82- type : "info" ,
83- title : "Describe what went wrong, then send" ,
84- description : `Pick any provider. If triage does not work here, run ${ CLI_COMMAND } in a terminal.` ,
85- timeout : 10_000 ,
86- actionProps : copyCliCommandAction ,
87- } ) ;
8877 } ;
8978}
0 commit comments