Skip to content

Commit 0929aeb

Browse files
committed
Add copy-to-clipboard functionality for assistant message parts
Implements feature requested in issue anomalyco#5641. Users can now click on: - Text parts (code snippets, markdown content) - Tool parts (command outputs, file contents) Clicking copies the content to clipboard and shows a success toast. Text selection (click+drag) continues to work as before - if text is selected, the copy action is skipped to preserve existing behavior. Changes: - Extended session context with copyToClipboard helper function - Added onMouseUp handlers to TextPart and ToolPart components - Helper checks for text selection, copies to clipboard, and shows toast - Tool parts only copy when completed with output available
1 parent c23ea2a commit 0929aeb

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

  • packages/opencode/src/cli/cmd/tui/routes/session

‎packages/opencode/src/cli/cmd/tui/routes/session/index.tsx‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const context = createContext<{
8787
showDetails: () => boolean
8888
diffWrapMode: () => "word" | "none"
8989
sync: ReturnType<typeof useSync>
90+
copyToClipboard: (text: string) => Promise<boolean>
9091
}>()
9192

9293
function use() {
@@ -817,6 +818,16 @@ export function Session() {
817818
// snap to bottom when session changes
818819
createEffect(on(() => route.sessionID, toBottom))
819820

821+
async function copyToClipboard(text: string): Promise<boolean> {
822+
if (renderer.getSelection()?.getSelectedText()) return false
823+
await Clipboard.copy(text)
824+
toast.show({
825+
message: "Copied to clipboard",
826+
variant: "success",
827+
})
828+
return true
829+
}
830+
820831
return (
821832
<context.Provider
822833
value={{
@@ -830,6 +841,7 @@ export function Session() {
830841
showDetails,
831842
diffWrapMode,
832843
sync,
844+
copyToClipboard,
833845
}}
834846
>
835847
<box flexDirection="row">
@@ -1193,7 +1205,13 @@ function TextPart(props: { last: boolean; part: TextPart; message: AssistantMess
11931205
const { theme, syntax } = useTheme()
11941206
return (
11951207
<Show when={props.part.text.trim()}>
1196-
<box id={"text-" + props.part.id} paddingLeft={3} marginTop={1} flexShrink={0}>
1208+
<box
1209+
id={"text-" + props.part.id}
1210+
paddingLeft={3}
1211+
marginTop={1}
1212+
flexShrink={0}
1213+
onMouseUp={() => ctx.copyToClipboard(props.part.text.trim())}
1214+
>
11971215
<code
11981216
filetype="markdown"
11991217
drawUnstyledText={false}
@@ -1212,9 +1230,18 @@ function TextPart(props: { last: boolean; part: TextPart; message: AssistantMess
12121230

12131231
function ToolPart(props: { last: boolean; part: ToolPart; message: AssistantMessage }) {
12141232
const { theme } = useTheme()
1215-
const { showDetails } = use()
1233+
const ctx = use()
1234+
const { showDetails } = ctx
12161235
const sync = useSync()
12171236
const [margin, setMargin] = createSignal(0)
1237+
1238+
function getToolOutput(): string | undefined {
1239+
if (props.part.state.status === "completed") {
1240+
return props.part.state.output
1241+
}
1242+
return undefined
1243+
}
1244+
12181245
const component = createMemo(() => {
12191246
// Hide tool if showDetails is false and tool completed successfully
12201247
// But always show if there's an error or permission is required
@@ -1257,6 +1284,10 @@ function ToolPart(props: { last: boolean; part: ToolPart; message: AssistantMess
12571284
<box
12581285
marginTop={margin()}
12591286
{...style}
1287+
onMouseUp={() => {
1288+
const output = getToolOutput()
1289+
if (output) ctx.copyToClipboard(output)
1290+
}}
12601291
renderBefore={function () {
12611292
const el = this as BoxRenderable
12621293
const parent = el.parent

0 commit comments

Comments
 (0)