Skip to content

The agent that grows with you

fix(desktop): preserve code punctuation in URL references

Denis H2c7a3ed927a5
diff --git a/apps/desktop/src/app/chat/composer/rich-editor.ts b/apps/desktop/src/app/chat/composer/rich-editor.ts
index b4f96e4259c..16969cb01ec 100644
--- a/apps/desktop/src/app/chat/composer/rich-editor.ts
+++ b/apps/desktop/src/app/chat/composer/rich-editor.ts
@@ -15,7 +15,7 @@ import {
type SlashChipKind,
slashIconElement
} from '@/components/assistant-ui/directive-text'
-import { referenceKind, referenceRe } from '@/components/assistant-ui/reference-kinds'
+import { referenceKind, referenceRe, unwrapReferenceValue } from '@/components/assistant-ui/reference-kinds'
import { slashCommandMatches, type SlashCommandScanOptions } from './slash-refs'
@@ -106,11 +106,7 @@ export function escapeHtml(value: string) {
}
export function unquoteRef(raw: string) {
- const head = raw[0]
- const tail = raw[raw.length - 1]
- const quoted = (head === '`' && tail === '`') || (head === '"' && tail === '"') || (head === "'" && tail === "'")
-
- return quoted ? raw.slice(1, -1) : raw.replace(/[,.;!?]+$/, '')
+ return unwrapReferenceValue(raw)
}
/** Always-quote variant of formatRefValue — chips need a fence even for safe values. */
diff --git a/apps/desktop/src/components/assistant-ui/reference-kinds.ts b/apps/desktop/src/components/assistant-ui/reference-kinds.ts
index 1986fbcaf6b..f30f86a696f 100644
--- a/apps/desktop/src/components/assistant-ui/reference-kinds.ts
+++ b/apps/desktop/src/components/assistant-ui/reference-kinds.ts
@@ -194,8 +194,10 @@ export function splitLeadingAttachmentRefs(text: string): { refs: string[]; text
}
return refs.length ? { refs, text: lines.join('\n').trim() } : { refs, text }
-/** Remove optional wire-format quotes and terminal prose punctuation from a reference value. */
-export function unwrapReferenceValue(raw: string): string {
+}
+
+/** Remove optional wire-format quotes while preserving every character in the value. */
+export function unquoteReferenceValue(raw: string): string {
if (raw.length < 2) {
return raw
}
@@ -203,9 +205,12 @@ export function unwrapReferenceValue(raw: string): string {
const head = raw[0]
const tail = raw[raw.length - 1]
- if ((head === '`' && tail === '`') || (head === '"' && tail === '"') || (head === "'" && tail === "'")) {
- return raw.slice(1, -1)
- }
+ return (head === '`' && tail === '`') || (head === '"' && tail === '"') || (head === "'" && tail === "'")
+ ? raw.slice(1, -1)
+ : raw
+}
- return raw.replace(/[,.;!?]+$/, '')
+/** Remove optional wire-format quotes and terminal prose punctuation from a reference value. */
+export function unwrapReferenceValue(raw: string): string {
+ return unquoteReferenceValue(raw).replace(/[,.;!?]+$/, '')
}
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.test.ts b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
index 5cbcff93470..19938b52705 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.test.ts
+++ b/apps/desktop/src/components/chat/shiki-highlighter.test.ts
@@ -40,13 +40,22 @@ describe('copyableCodeText', () => {
)
})
- it('unwraps each supported URL quote style without changing other references', () => {
- expect(copyableCodeText(`first @url:'https://one.example'\nsecond @url:"https://two.example" @file:\`src/app.ts\``)).toBe(
- 'first https://one.example\nsecond https://two.example @file:`src/app.ts`'
+ it('unwraps bare URL reference directives without losing code punctuation', () => {
+ expect(copyableCodeText('curl @url:https://example.com/image.png; echo done')).toBe(
+ 'curl https://example.com/image.png; echo done'
)
})
- it('leaves text that is not a wire reference unchanged', () => {
- expect(copyableCodeText("const directive = '@url:'")).toBe("const directive = '@url:'")
+ it('unwraps each supported URL quote style without changing other references', () => {
+ const code = `first @url:'https://one.example'
+second @url:"https://two.example" @file:\`src/app.ts\``
+
+ expect(copyableCodeText(code)).toBe('first https://one.example\nsecond https://two.example @file:`src/app.ts`')
+ })
+
+ it('leaves invalid URL directives and other reference kinds unchanged', () => {
+ const code = '@url:ftp://example.com @url:example.com @image:https://example.com/image.png'
+
+ expect(copyableCodeText(code)).toBe(code)
})
})
diff --git a/apps/desktop/src/components/chat/shiki-highlighter.tsx b/apps/desktop/src/components/chat/shiki-highlighter.tsx
index 28682ba159d..48248f7c5dc 100644
--- a/apps/desktop/src/components/chat/shiki-highlighter.tsx
+++ b/apps/desktop/src/components/chat/shiki-highlighter.tsx
@@ -3,7 +3,7 @@
import type { SyntaxHighlighterProps } from '@assistant-ui/react-streamdown'
import { type FC, lazy, Suspense, useMemo } from 'react'
-import { referenceRe, unwrapReferenceValue } from '@/components/assistant-ui/reference-kinds'
+import { referenceRe, unquoteReferenceValue } from '@/components/assistant-ui/reference-kinds'
import { CodeCard, CodeCardBody } from '@/components/chat/code-card'
import { ExpandableBlock } from '@/components/chat/expandable-block'
// Theme constants live in shiki-config (dependency-free) so the lazy shiki
@@ -131,7 +131,7 @@ const URL_REFERENCE_VALUE_RE = /^https?:\/\/\S+$/i
/** Convert URL references back to literal URLs for code-block clipboard text. */
export function copyableCodeText(code: string): string {
return code.replace(referenceRe(), (directive, kind: string, value: string) => {
- const url = unwrapReferenceValue(value)
+ const url = unquoteReferenceValue(value)
return kind === 'url' && URL_REFERENCE_VALUE_RE.test(url) ? url : directive
})
@@ -167,7 +167,7 @@ export const SyntaxHighlighter: FC<HermesSyntaxHighlighterProps> = ({
iconClassName="size-3"
label={t.assistant.tool.copyCode}
showLabel={false}
- text={copyableCodeText(content)}
+ text={() => copyableCodeText(content)}
/>
<CodeCardBody className="[&_pre]:px-3 [&_pre]:py-2.5">
<ExpandableBlock>