Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(cowork): open only documents and media from Open, show anything else in its folder
James Damf0ea8bddddd7
diff --git a/web-app/src/containers/CoworkArtifactCard.tsx b/web-app/src/containers/CoworkArtifactCard.tsxindex 19db3922f..4f902b2b5 100644--- a/web-app/src/containers/CoworkArtifactCard.tsx+++ b/web-app/src/containers/CoworkArtifactCard.tsx@@ -9,7 +9,11 @@ import {import { Card } from '@/components/ui/card'import { useServiceHub } from '@/hooks/useServiceHub'import { useTranslation } from '@/i18n/react-i18next-compat'-import { previewKindFor, resolveInRoots } from '@/lib/coworkPreview'+import {+ openOrReveal,+ previewKindFor,+ resolveInRoots,+} from '@/lib/coworkPreview'import { ARTIFACT_ICON, type CoworkArtifact } from '@/lib/coworkArtifacts'import { cn } from '@/lib/utils'@@ -124,7 +128,7 @@ export function CoworkArtifactCard({<div className="flex shrink-0 items-center overflow-hidden rounded-md border"><buttontype="button"- onClick={() => void serviceHub.opener().openPath(abs)}+ onClick={() => void openOrReveal(serviceHub.opener(), abs)}title={t('common:artifactOpenExternal')}className="flex items-center gap-1.5 px-2.5 py-1.5 text-xs font-medium hover:bg-accent">@@ -150,7 +154,7 @@ export function CoworkArtifactCard({</DropdownMenuItem><DropdownMenuSeparator /><DropdownMenuItem- onClick={() => void serviceHub.opener().openPath(abs)}+ onClick={() => void openOrReveal(serviceHub.opener(), abs)}><SquareArrowOutUpRight size={14} />{t('common:artifactOpenExternal')}diff --git a/web-app/src/containers/CoworkPreviewPanel.tsx b/web-app/src/containers/CoworkPreviewPanel.tsxindex c86d3e908..d01e2aa1d 100644--- a/web-app/src/containers/CoworkPreviewPanel.tsx+++ b/web-app/src/containers/CoworkPreviewPanel.tsx@@ -29,6 +29,7 @@ import {basenameOf,externalRefs,isAssetKind,+ openOrReveal,previewKindFor,resolveInRoots,unresolvedRefs,@@ -204,7 +205,7 @@ export function CoworkPreviewPanel({ roots, path, onClose }: Props) {{iconButton(t('common:artifactOpenExternal'),SquareArrowOutUpRight,- () => void serviceHub.opener().openPath(abs)+ () => void openOrReveal(serviceHub.opener(), abs))}{iconButton(t('common:artifactShowInFolder'),diff --git a/web-app/src/lib/__tests__/coworkPreview.test.ts b/web-app/src/lib/__tests__/coworkPreview.test.tsindex 4bccb4766..caa9c2fc9 100644--- a/web-app/src/lib/__tests__/coworkPreview.test.ts+++ b/web-app/src/lib/__tests__/coworkPreview.test.ts@@ -1,4 +1,4 @@-import { describe, it, expect } from 'vitest'+import { describe, it, expect, vi } from 'vitest'import {previewKindFor,extensionOf,@@ -9,6 +9,8 @@ import {previewUrlFor,resolveInRoot,resolveInRoots,+ opensInDefaultApp,+ openOrReveal,} from '@/lib/coworkPreview'describe('previewKindFor', () => {@@ -188,3 +190,67 @@ describe('resolveInRoots', () => {expect(resolveInRoots([null, null], 'a.md')).toBeNull()})})++describe('opensInDefaultApp', () => {+ it('opens documents, media and text', () => {+ for (const p of [+ '/w/report.pdf',+ '/w/chart.PNG',+ 'C:\\w\\notes.md',+ '/w/data.csv',+ '/w/page.html',+ '/w/deck.pptx',+ '/w/clip.mp4',+ '/w/song.mp3',+ '/w/logo.svg',+ ]) {+ expect(opensInDefaultApp(p), p).toBe(true)+ }+ })++ it('never opens anything the OS would run', () => {+ for (const p of [+ '/w/run.command',+ '/w/Thing.app',+ '/w/link.lnk',+ '/w/x.desktop',+ '/w/setup.exe',+ '/w/a.bat',+ '/w/a.cmd',+ '/w/a.ps1',+ '/w/a.sh',+ '/w/a.terminal',+ '/w/a.scpt',+ '/w/a.jar',+ '/w/a.js',+ '/w/a.py',+ '/w/a.vbs',+ '/w/report.pdf.command',+ '/w/noext',+ '/w/.bashrc',+ ]) {+ expect(opensInDefaultApp(p), p).toBe(false)+ }+ })+})++describe('openOrReveal', () => {+ const opener = () => ({+ openPath: vi.fn().mockResolvedValue(undefined),+ revealItemInDir: vi.fn().mockResolvedValue(undefined),+ })++ it('opens an allowed type', async () => {+ const o = opener()+ await openOrReveal(o, '/w/report.pdf')+ expect(o.openPath).toHaveBeenCalledWith('/w/report.pdf')+ expect(o.revealItemInDir).not.toHaveBeenCalled()+ })++ it('reveals anything else instead of opening it', async () => {+ const o = opener()+ await openOrReveal(o, '/w/run.command')+ expect(o.revealItemInDir).toHaveBeenCalledWith('/w/run.command')+ expect(o.openPath).not.toHaveBeenCalled()+ })+})diff --git a/web-app/src/lib/coworkPreview.ts b/web-app/src/lib/coworkPreview.tsindex 22cd6b930..803e7f9b6 100644--- a/web-app/src/lib/coworkPreview.ts+++ b/web-app/src/lib/coworkPreview.ts@@ -91,6 +91,40 @@ export function isAssetKind(kind: PreviewKind): boolean {return kind === 'image' || kind === 'video' || kind === 'audio'}+/**+ * Types "Open" may hand to the OS default app: documents, media and plain text.+ * The file was written by the agent, so anything the OS would run (`.command`,+ * `.app`, `.exe`, `.bat`, `.ps1`, `.sh`, `.js`, `.py`, `.lnk`, `.desktop`,+ * `.jar`, ...) is never opened, only shown in its folder.+ */+const OPENS_IN_DEFAULT_APP = new Set([+ 'pdf',+ 'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'svg',+ 'txt', 'md', 'markdown', 'csv', 'tsv', 'json', 'html', 'htm', 'rtf',+ 'docx', 'xlsx', 'pptx', 'odt', 'ods', 'odp',+ 'mp3', 'wav', 'ogg', 'flac', 'm4a', 'aac', 'opus',+ 'mp4', 'webm', 'mov', 'mkv', 'avi', 'm4v',+])++/** Whether "Open" may hand `path` to its default app; see `openOrReveal`. */+export function opensInDefaultApp(path: string): boolean {+ return OPENS_IN_DEFAULT_APP.has(extensionOf(path))+}++/** "Open" for an agent-written file: the default app for a document or media+ * type, the file manager (file selected) for anything else. */+export function openOrReveal(+ opener: {+ openPath(path: string): Promise<void>+ revealItemInDir(path: string): Promise<void>+ },+ abs: string+): Promise<void> {+ return opensInDefaultApp(abs)+ ? opener.openPath(abs)+ : opener.revealItemInDir(abs)+}+export function basenameOf(path: string): string {return path.split(/[/\\]/).pop() || path}