Skip to content

Commit fa97edb

Browse files
committed
fix(files): render embedded workspace images in markdown
1 parent cf84e05 commit fa97edb

4 files changed

Lines changed: 93 additions & 6 deletions

File tree

apps/sim/app/api/files/view/[id]/route.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { fileViewContract } from '@/lib/api/contracts/storage-transfer'
55
import { parseRequest } from '@/lib/api/server'
66
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
77
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
8-
import { USE_BLOB_STORAGE } from '@/lib/uploads/config'
8+
import { type StorageContext, USE_BLOB_STORAGE } from '@/lib/uploads/config'
99
import { getFileMetadataById } from '@/lib/uploads/server/metadata'
1010
import { verifyFileAccess } from '@/app/api/files/authorization'
1111

@@ -29,16 +29,44 @@ export const GET = withRouteHandler(
2929
return NextResponse.json({ error: 'Not found' }, { status: 404 })
3030
}
3131

32-
const hasAccess = await verifyFileAccess(record.key, authResult.userId)
32+
// Authorize before disclosing anything about the file. Pass the record's own context so access is
33+
// resolved from the DB row (workspace and mothership both map to workspace membership) rather than
34+
// re-inferred from the key, and deny with 404 so a caller without access cannot distinguish a
35+
// file's existence or context from a missing id.
36+
const hasAccess = await verifyFileAccess(
37+
record.key,
38+
authResult.userId,
39+
undefined,
40+
record.context as StorageContext | 'general'
41+
)
3342
if (!hasAccess) {
3443
logger.warn('Unauthorized file view attempt', { id, userId: authResult.userId })
35-
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
44+
return NextResponse.json({ error: 'Not found' }, { status: 404 })
45+
}
46+
47+
// Only workspace-scoped files are embeddable/viewable here. Other contexts (e.g. chat-scoped
48+
// `mothership` uploads) are not durable workspace artifacts; now that the caller is known to have
49+
// access, reject with a legible 422 so the embed fails cleanly and the file agent can self-correct.
50+
if (record.context !== 'workspace') {
51+
logger.warn('Rejected non-workspace file view', { id, context: record.context })
52+
return NextResponse.json(
53+
{
54+
error: `File ${id} has context "${record.context}" and is not embeddable. Only workspace files can be viewed via /api/files/view. Save it into the workspace and reference the workspace copy.`,
55+
},
56+
{ status: 422 }
57+
)
3658
}
3759

3860
const storagePrefix = USE_BLOB_STORAGE ? 'blob' : 's3'
3961
const servePath = `/api/files/serve/${storagePrefix}/${encodeURIComponent(record.key)}`
4062
logger.info('Redirecting file view to serve path', { id, servePath })
4163

42-
return NextResponse.redirect(new URL(servePath, request.url), { status: 302 })
64+
// Emit a relative Location so the browser resolves it against the public origin it requested.
65+
// `NextResponse.redirect(new URL(servePath, request.url))` bakes in the host from `request.url`,
66+
// which behind the load balancer is the internal pod host (e.g. ip-10-0-x.ec2.internal:3000) —
67+
// unreachable from the browser, so embedded <img src="/api/files/view/<id>"> loads fail. A
68+
// relative Location resolves against the original public URL (matching how the Files tab fetches
69+
// serve URLs directly).
70+
return new NextResponse(null, { status: 302, headers: { Location: servePath } })
4371
}
4472
)

apps/sim/lib/copilot/tools/server/files/edit-content.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { isE2BDocEnabled } from '@/lib/core/config/env-flags'
99
import { updateWorkspaceFileContent } from '@/lib/uploads/contexts/workspace/workspace-file-manager'
1010
import { getE2BDocFormat } from './doc-compile'
11+
import { buildEmbeddedImageRefWarning } from './embedded-image-refs'
1112
import { consumeLatestFileIntent } from './file-intent-store'
1213
import { compileDocForWrite, getDocumentFormatInfo, inferContentType } from './workspace-file'
1314

@@ -250,9 +251,13 @@ export const editContentServerTool: BaseServerTool<EditContentArgs, EditContentR
250251
userId: context.userId,
251252
})
252253

254+
// Flag any `/api/files/view/<id>` embeds the model just authored that won't render/export
255+
// (non-workspace or missing), so it can self-correct on the next step.
256+
const embedWarning = await buildEmbeddedImageRefWarning(content, workspaceId)
257+
253258
return {
254259
success: true,
255-
message: `File "${fileRecord.name}" ${verb} successfully (${fileBuffer.length} bytes)`,
260+
message: `File "${fileRecord.name}" ${verb} successfully (${fileBuffer.length} bytes)${embedWarning}`,
256261
data: {
257262
id: intent.fileId,
258263
name: fileRecord.name,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { getFileMetadataById } from '@/lib/uploads/server/metadata'
2+
3+
/** The canonical embed form the file agent writes for workspace images: `/api/files/view/<fileId>`. */
4+
const VIEW_EMBED_RE = /\/api\/files\/view\/([A-Za-z0-9_-]+)/g
5+
6+
/**
7+
* Returns the ids of `/api/files/view/<id>` image embeds in `content` that will not render or survive a
8+
* workspace export. An embed is valid only when its id resolves to a workspace file in this same
9+
* workspace — the only thing the view route serves and an export can bundle. Every other case (missing,
10+
* archived, a different workspace, or a non-`workspace` upload such as a chat-scoped `mothership` file)
11+
* is flagged by id alone, without disclosing the referenced file's real context or owning workspace, so
12+
* the result can't be used to probe files outside this workspace. Best-effort and never throws, so a
13+
* content write is never blocked by this validation.
14+
*/
15+
export async function findUnembeddableImageRefs(
16+
content: string,
17+
workspaceId: string
18+
): Promise<string[]> {
19+
const ids = new Set<string>()
20+
for (const match of content.matchAll(VIEW_EMBED_RE)) ids.add(match[1])
21+
if (ids.size === 0) return []
22+
23+
const checked = await Promise.all(
24+
[...ids].map(async (id): Promise<string | null> => {
25+
try {
26+
const record = await getFileMetadataById(id)
27+
const embeddable = record?.context === 'workspace' && record.workspaceId === workspaceId
28+
return embeddable ? null : id
29+
} catch {
30+
return null
31+
}
32+
})
33+
)
34+
35+
return checked.filter((id): id is string => id !== null)
36+
}
37+
38+
/**
39+
* Builds an actionable suffix appended to a successful file-write tool result so the model can
40+
* self-correct: only workspace files in this workspace embed, so any other reference must be re-saved
41+
* into the workspace and re-referenced by the workspace file's id. Empty when there is nothing to flag.
42+
*/
43+
export async function buildEmbeddedImageRefWarning(
44+
content: string,
45+
workspaceId: string
46+
): Promise<string> {
47+
const ids = await findUnembeddableImageRefs(content, workspaceId)
48+
if (ids.length === 0) return ''
49+
const list = ids.map((id) => `/api/files/view/${id}`).join('; ')
50+
return ` Warning: embedded image(s) will not render or export because they are not workspace files in this workspace — ${list}. Save each image as a workspace file (under files/) and reference it via /api/files/view/<workspace-file-id>.`
51+
}

apps/sim/lib/copilot/tools/server/files/workspace-file.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
getE2BDocFormat,
3535
PPTXGENJS_SOURCE_MIME,
3636
} from './doc-compile'
37+
import { buildEmbeddedImageRefWarning } from './embedded-image-refs'
3738
import { storeFileIntent } from './file-intent-store'
3839

3940
const logger = createLogger('WorkspaceFileServerTool')
@@ -398,9 +399,11 @@ export const workspaceFileServerTool: BaseServerTool<WorkspaceFileArgs, Workspac
398399
userId: context.userId,
399400
})
400401

402+
const embedWarning = await buildEmbeddedImageRefWarning(content, workspaceId)
403+
401404
return {
402405
success: true,
403-
message: `File "${fileName}" created successfully (${fileBuffer.length} bytes)`,
406+
message: `File "${fileName}" created successfully (${fileBuffer.length} bytes)${embedWarning}`,
404407
data: {
405408
id: result.id,
406409
name: result.name,

0 commit comments

Comments
 (0)