fix: name draft-only items by their draft path in chat mentions and answers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem Lemouel
2026-10-28 03:54:32 -07:00
co-authored by Claude Opus 5.5
parent c858cd1d14
commit 9e2c8772f0
13 changed files with 102 additions and 20 deletions
@@ -147,7 +147,13 @@
{/snippet}
{#snippet actions()}
<span class="hidden md:inline-flex">
<AskAiRowButton kind="app" path={app.path} summary={app.summary} rawApp={!!app.raw_app} />
<AskAiRowButton
kind="app"
path={app.path}
displayPath={app.draft_only ? (app as any).draft_path : undefined}
summary={app.summary}
rawApp={!!app.raw_app}
/>
{#if showEditButton && app.canWrite}
<div>
<Button
@@ -9,11 +9,20 @@
let {
kind,
path,
displayPath,
summary,
rawApp
}: { kind: BulkKind; path: string; summary?: string; rawApp?: boolean } = $props()
}: {
kind: BulkKind
path: string
displayPath?: string
summary?: string
rawApp?: boolean
} = $props()
let element = $derived(itemContextElement({ kind, path, summary: summary || undefined, rawApp }))
let element = $derived(
itemContextElement({ kind, path, displayPath, summary: summary || undefined, rawApp })
)
</script>
{#if element && homeComposerContext.available}
@@ -178,7 +178,12 @@
{/snippet}
{#snippet actions()}
<span class="hidden md:inline-flex">
<AskAiRowButton kind="flow" path={flow.path} summary={flow.summary} />
<AskAiRowButton
kind="flow"
path={flow.path}
displayPath={flow.draft_only ? flow.draft_path : undefined}
summary={flow.summary}
/>
{#if !hideForOperator && showEditButton && flow.canWrite && !flow.archived}
<div>
<Button
@@ -234,7 +234,12 @@
{#snippet actions()}
<span class="hidden md:inline-flex">
<AskAiRowButton kind="script" path={script.path} summary={script.summary} />
<AskAiRowButton
kind="script"
path={script.path}
displayPath={script.draft_only ? script.draft_path : undefined}
summary={script.summary}
/>
{#if $roleCapabilities.code}
{#if showEditButton}
{#if script.use_codebase}
@@ -301,7 +301,7 @@ Content is materialized at message-prep time by `AIChatManager` — see PR #9216
const element: WorkspaceScriptElement & { deletable: boolean } = {
type: 'workspace_script',
path: d.path,
title: d.path,
title: workspaceItemDisplayPath(d),
summary: d.summary,
deletable: true
}
@@ -310,7 +310,7 @@ Content is materialized at message-prep time by `AIChatManager` — see PR #9216
const element: WorkspaceFlowElement & { deletable: boolean } = {
type: 'workspace_flow',
path: d.path,
title: d.path,
title: workspaceItemDisplayPath(d),
summary: d.summary,
deletable: true
}
@@ -320,7 +320,7 @@ Content is materialized at message-prep time by `AIChatManager` — see PR #9216
const element: WorkspaceAppElement & { deletable: boolean } = {
type: 'workspace_app',
path: d.path,
title: d.path,
title: workspaceItemDisplayPath(d),
summary: d.summary,
deletable: true
}
@@ -64,7 +64,7 @@ at the workspace root because the parent menu item already names the scope.
return {
type: 'workspace_script',
path: item.path,
title: item.path,
title: workspaceItemDisplayPath(item),
summary: item.summary,
deletable: true
} satisfies WorkspaceScriptElement & { deletable: boolean }
@@ -73,7 +73,7 @@ at the workspace root because the parent menu item already names the scope.
return {
type: 'workspace_flow',
path: item.path,
title: item.path,
title: workspaceItemDisplayPath(item),
summary: item.summary,
deletable: true
} satisfies WorkspaceFlowElement & { deletable: boolean }
@@ -82,7 +82,7 @@ at the workspace root because the parent menu item already names the scope.
return {
type: 'workspace_app',
path: item.path,
title: item.path,
title: workspaceItemDisplayPath(item),
summary: item.summary,
deletable: true
} satisfies WorkspaceAppElement & { deletable: boolean }
@@ -8092,6 +8092,15 @@ describe('prepareGlobalUserMessage', () => {
expect(message.content).not.toContain('Dashboard raw app')
})
it('ties a draft mentioned by its draft path to its storage path', () => {
const message = prepareGlobalUserMessage('Fix @u/me/report', [
{ type: 'workspace_flow', path: 'u/me/draft_1234', title: 'u/me/report' }
])
expect(message.content).toContain(
'- type: flow, path: u/me/draft_1234, mentioned as: u/me/report\n'
)
})
it('lists attached files as id references without their content', () => {
const message = prepareGlobalUserMessage('Summarize', [], {
files: [
@@ -186,6 +186,7 @@ import { appFileEditorLang } from '../appFileEditorLang'
import { searchDocsTool, readDocsPageTool } from '../docs/core'
import { createDbSchemaTool } from '../script/core'
import type { ContextElement } from '../context'
import { mentionedAs } from '../mention'
import { getDatatableTools } from '../datatableTools'
import { getDatatableMigrationTools } from '../datatableMigrationTools'
import { getDucklakeTools } from '../ducklakeTools'
@@ -6040,7 +6041,9 @@ function createVariableToDraftState(
// `null` clears and omitted keeps the base's. The draft stores a cleared expiry as
// `undefined`, since a stored draft goes through a JSON round-trip.
value_valid_until:
args.value_valid_until === null ? undefined : (args.value_valid_until ?? base?.value_valid_until)
args.value_valid_until === null
? undefined
: (args.value_valid_until ?? base?.value_valid_until)
}
}
@@ -10086,7 +10089,7 @@ export function prepareGlobalUserMessage(
: context.type === 'workspace_flow'
? 'flow'
: 'raw_app'
content += `- type: ${itemType}, path: ${context.path}\n`
content += `- type: ${itemType}, path: ${context.path}${mentionedAs(context)}\n`
}
content += '\n'
}
@@ -30,6 +30,13 @@ export function formatMention(name: string): string {
return BARE_SAFE.test(name) ? `@${name}` : `@[${name.replace(/[\\\]]/g, '\\$&')}]`
}
/** A draft-only item is mentioned by its draft path while `path` stays its generated
* `draft_<uuid>` storage path, which is what tools resolve; the prompt names both so
* the model ties the `@` token in the message to the item. */
export function mentionedAs(item: { path: string; title: string }): string {
return item.title && item.title !== item.path ? `, mentioned as: ${item.title}` : ''
}
function isWordChar(char: string | undefined): boolean {
return char !== undefined && /[\p{L}\p{N}\p{M}_]/u.test(char)
}
@@ -39,6 +39,7 @@ import type { PasteAttachment } from './pasteTokens'
import { dataUrlToImagePart, type AttachedImage } from './imageUtils'
import type { AttachedTextFile } from './textFileUtils'
import type { CodePieceElement, ContextElement, FlowModuleCodePieceElement } from './context'
import { mentionedAs } from './mention'
import { workspaceStore } from '$lib/stores'
import type { ExtendedOpenFlow } from '$lib/components/flows/types'
import { findModuleInFlow, findModuleInModules } from '$lib/components/flows/flowTree'
@@ -444,17 +445,17 @@ export function buildContextString(selectedContext: ContextElement[]): string {
if (!workspaceItemsContext) {
workspaceItemsContext = 'SELECTED WORKSPACE ITEMS:\n'
}
workspaceItemsContext += `- type: script, path: ${context.path}\n`
workspaceItemsContext += `- type: script, path: ${context.path}${mentionedAs(context)}\n`
} else if (context.type === 'workspace_flow') {
if (!workspaceItemsContext) {
workspaceItemsContext = 'SELECTED WORKSPACE ITEMS:\n'
}
workspaceItemsContext += `- type: flow, path: ${context.path}\n`
workspaceItemsContext += `- type: flow, path: ${context.path}${mentionedAs(context)}\n`
} else if (context.type === 'workspace_app') {
if (!workspaceItemsContext) {
workspaceItemsContext = 'SELECTED WORKSPACE ITEMS:\n'
}
workspaceItemsContext += `- type: raw_app, path: ${context.path}\n`
workspaceItemsContext += `- type: raw_app, path: ${context.path}${mentionedAs(context)}\n`
}
}
@@ -56,6 +56,9 @@ export interface WorkspaceItemEntry {
/** Scripts, flows and apps that exist only as the viewer's draft: no deployed version,
* so only their editor can open them. */
draftOnly?: boolean
/** The path a draft-only item is named by when it is stored at a generated
* `draft_<uuid>` path. Display only: `path` stays what links and actions address. */
draftPath?: string
}
export type WorkspaceItemTargetKind = 'script' | 'flow'
@@ -209,7 +212,8 @@ class WorkspaceItemRegistry {
targetKind:
typeof it.is_flow === 'boolean' ? (it.is_flow ? 'flow' : 'script') : undefined,
rawApp: kind === 'app' ? it.raw_app === true : undefined,
draftOnly: it.draft_only === true || undefined
draftOnly: it.draft_only === true || undefined,
draftPath: (it.draft_only && it.draft_path) || undefined
})
}
}
@@ -340,7 +344,7 @@ function buildPathLinkNode(
data: {
hProperties
},
children: [{ type: 'text', value: displayPath }]
children: [{ type: 'text', value: entry.draftPath ?? displayPath }]
}
}
@@ -49,7 +49,12 @@ describe('workspaceItemRegistry', () => {
])
await workspaceItemRegistry.ensureLoaded('ws-draft')
const entry = workspaceItemRegistry.resolve('ws-draft', 'u/me/report')
expect(entry).toMatchObject({ kind: 'flow', path: 'u/me/draft_1234', draftOnly: true })
expect(entry).toMatchObject({
kind: 'flow',
path: 'u/me/draft_1234',
draftOnly: true,
draftPath: 'u/me/report'
})
expect(itemHref(entry!, 'ws-draft')).toBe('/flows/edit/u/me/draft_1234?workspace=ws-draft')
})
@@ -328,6 +333,25 @@ describe('remarkWindmillPaths (mdast)', () => {
expect(byPath['f/ops/dashboard']['data-wm-raw-app']).toBeUndefined()
})
it('names a draft stored at a synthetic path by its draft path', () => {
const processor = unified()
.use(remarkParse)
.use(
remarkWindmillPaths({
resolve: (p) =>
p === 'u/me/draft_1234'
? { kind: 'flow', path: p, draftOnly: true, draftPath: 'u/me/report' }
: undefined
})
)
const tree = processor.runSync(processor.parse('Saved u/me/draft_1234.')) as MdastRoot
const [link] = findLinks(tree)
expect((link.children[0] as Text).value).toBe('u/me/report')
expect((link.data?.hProperties as Record<string, string>)['data-wm-path']).toBe(
'u/me/draft_1234'
)
})
it('leaves unknown paths as plain text', () => {
const processor = buildProcessor()
const tree = processor.runSync(
@@ -10,6 +10,9 @@ type Attach = (element: ContextElement) => Promise<void>
export function itemContextElement(item: {
kind: BulkKind
path: string
/** The path a draft-only item is named by, shown as its mention instead of the
* generated `draft_<uuid>` storage path. */
displayPath?: string
summary?: string
rawApp?: boolean
}): ContextElement | undefined {
@@ -22,7 +25,13 @@ export function itemContextElement(item: {
? 'workspace_app'
: undefined
if (!type) return undefined
return { type, path: item.path, title: item.path, summary: item.summary, deletable: true }
return {
type,
path: item.path,
title: item.displayPath || item.path,
summary: item.summary,
deletable: true
}
}
let attach = $state<Attach | undefined>(undefined)