Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/codex-claw/src/components/export-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCallback, useState } from 'react'
import { HugeiconsIcon } from '@hugeicons/react'
import { Download01Icon } from '@hugeicons/core-free-icons'

import type { ExportFormat } from '@/hooks/use-export'
import {
MenuContent,
MenuItem,
Expand All @@ -19,8 +20,6 @@ import {
import { buttonVariants } from '@/components/ui/button'
import { cn } from '@/lib/utils'

type ExportFormat = 'markdown' | 'json' | 'text'

type ExportMenuProps = {
onExport: (format: ExportFormat) => void
disabled?: boolean
Expand All @@ -30,6 +29,9 @@ const formats: Array<{ format: ExportFormat; label: string; ext: string }> = [
{ format: 'markdown', label: 'Markdown', ext: '.md' },
{ format: 'json', label: 'JSON', ext: '.json' },
{ format: 'text', label: 'Plain Text', ext: '.txt' },
{ format: 'bundle', label: 'Redacted Bundle', ext: '.md' },
{ format: 'issue-draft', label: 'Issue Draft', ext: '.md' },
{ format: 'pr-draft', label: 'PR Draft', ext: '.md' },
]

export function ExportMenu({ onExport, disabled }: ExportMenuProps) {
Expand Down
49 changes: 48 additions & 1 deletion apps/codex-claw/src/hooks/use-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { chatQueryKeys } from '../screens/chat/chat-queries'
import { getMessageTimestamp, textFromMessage } from '../screens/chat/utils'
import type { GatewayMessage, HistoryResponse } from '../screens/chat/types'

type ExportFormat = 'markdown' | 'json' | 'text'
export type ExportFormat =
| 'markdown'
| 'json'
| 'text'
| 'bundle'
| 'issue-draft'
| 'pr-draft'

type UseExportInput = {
currentFriendlyId: string
Expand All @@ -22,6 +28,15 @@ export function useExport({

const exportConversation = useCallback(
function exportConversation(format: ExportFormat) {
if (isServerExport(format)) {
exportServerHandoff({
currentFriendlyId,
currentSessionKey,
format,
})
return
}

const historyKey = chatQueryKeys.history(
currentFriendlyId,
currentSessionKey || currentFriendlyId,
Expand Down Expand Up @@ -69,6 +84,38 @@ export function useExport({
return { exportConversation }
}

function isServerExport(format: ExportFormat) {
return (
format === 'bundle' ||
format === 'issue-draft' ||
format === 'pr-draft'
)
}

function handoffKind(format: ExportFormat) {
if (format === 'issue-draft') return 'issue'
if (format === 'pr-draft') return 'pr'
return 'bundle'
}

function exportServerHandoff(input: {
currentFriendlyId: string
currentSessionKey: string
format: ExportFormat
}) {
if (typeof window === 'undefined') return
const params = new URLSearchParams()
params.set('kind', handoffKind(input.format))
params.set('download', '1')
if (input.currentSessionKey) {
params.set('sessionKey', input.currentSessionKey)
}
if (input.currentFriendlyId) {
params.set('friendlyId', input.currentFriendlyId)
}
window.location.assign('/api/session-bundle?' + params.toString())
}

function formatTimestamp(message: GatewayMessage): string {
const ts = getMessageTimestamp(message)
return new Date(ts).toLocaleString('en-GB', {
Expand Down
21 changes: 21 additions & 0 deletions apps/codex-claw/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Route as ApiWorkspacesRouteImport } from './routes/api/workspaces'
import { Route as ApiTasksRouteImport } from './routes/api/tasks'
import { Route as ApiStreamRouteImport } from './routes/api/stream'
import { Route as ApiSessionsRouteImport } from './routes/api/sessions'
import { Route as ApiSessionBundleRouteImport } from './routes/api/session-bundle'
import { Route as ApiSendRouteImport } from './routes/api/send'
import { Route as ApiRunEventsRouteImport } from './routes/api/run-events'
import { Route as ApiRepoContextRouteImport } from './routes/api/repo-context'
Expand Down Expand Up @@ -68,6 +69,11 @@ const ApiSessionsRoute = ApiSessionsRouteImport.update({
path: '/api/sessions',
getParentRoute: () => rootRouteImport,
} as any)
const ApiSessionBundleRoute = ApiSessionBundleRouteImport.update({
id: '/api/session-bundle',
path: '/api/session-bundle',
getParentRoute: () => rootRouteImport,
} as any)
const ApiSendRoute = ApiSendRouteImport.update({
id: '/api/send',
path: '/api/send',
Expand Down Expand Up @@ -133,6 +139,7 @@ export interface FileRoutesByFullPath {
'/api/repo-context': typeof ApiRepoContextRoute
'/api/run-events': typeof ApiRunEventsRoute
'/api/send': typeof ApiSendRoute
'/api/session-bundle': typeof ApiSessionBundleRoute
'/api/sessions': typeof ApiSessionsRoute
'/api/stream': typeof ApiStreamRoute
'/api/tasks': typeof ApiTasksRoute
Expand All @@ -153,6 +160,7 @@ export interface FileRoutesByTo {
'/api/repo-context': typeof ApiRepoContextRoute
'/api/run-events': typeof ApiRunEventsRoute
'/api/send': typeof ApiSendRoute
'/api/session-bundle': typeof ApiSessionBundleRoute
'/api/sessions': typeof ApiSessionsRoute
'/api/stream': typeof ApiStreamRoute
'/api/tasks': typeof ApiTasksRoute
Expand All @@ -174,6 +182,7 @@ export interface FileRoutesById {
'/api/repo-context': typeof ApiRepoContextRoute
'/api/run-events': typeof ApiRunEventsRoute
'/api/send': typeof ApiSendRoute
'/api/session-bundle': typeof ApiSessionBundleRoute
'/api/sessions': typeof ApiSessionsRoute
'/api/stream': typeof ApiStreamRoute
'/api/tasks': typeof ApiTasksRoute
Expand All @@ -196,6 +205,7 @@ export interface FileRouteTypes {
| '/api/repo-context'
| '/api/run-events'
| '/api/send'
| '/api/session-bundle'
| '/api/sessions'
| '/api/stream'
| '/api/tasks'
Expand All @@ -216,6 +226,7 @@ export interface FileRouteTypes {
| '/api/repo-context'
| '/api/run-events'
| '/api/send'
| '/api/session-bundle'
| '/api/sessions'
| '/api/stream'
| '/api/tasks'
Expand All @@ -236,6 +247,7 @@ export interface FileRouteTypes {
| '/api/repo-context'
| '/api/run-events'
| '/api/send'
| '/api/session-bundle'
| '/api/sessions'
| '/api/stream'
| '/api/tasks'
Expand All @@ -257,6 +269,7 @@ export interface RootRouteChildren {
ApiRepoContextRoute: typeof ApiRepoContextRoute
ApiRunEventsRoute: typeof ApiRunEventsRoute
ApiSendRoute: typeof ApiSendRoute
ApiSessionBundleRoute: typeof ApiSessionBundleRoute
ApiSessionsRoute: typeof ApiSessionsRoute
ApiStreamRoute: typeof ApiStreamRoute
ApiTasksRoute: typeof ApiTasksRoute
Expand Down Expand Up @@ -322,6 +335,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof ApiSessionsRouteImport
parentRoute: typeof rootRouteImport
}
'/api/session-bundle': {
id: '/api/session-bundle'
path: '/api/session-bundle'
fullPath: '/api/session-bundle'
preLoaderRoute: typeof ApiSessionBundleRouteImport
parentRoute: typeof rootRouteImport
}
'/api/send': {
id: '/api/send'
path: '/api/send'
Expand Down Expand Up @@ -409,6 +429,7 @@ const rootRouteChildren: RootRouteChildren = {
ApiRepoContextRoute: ApiRepoContextRoute,
ApiRunEventsRoute: ApiRunEventsRoute,
ApiSendRoute: ApiSendRoute,
ApiSessionBundleRoute: ApiSessionBundleRoute,
ApiSessionsRoute: ApiSessionsRoute,
ApiStreamRoute: ApiStreamRoute,
ApiTasksRoute: ApiTasksRoute,
Expand Down
48 changes: 48 additions & 0 deletions apps/codex-claw/src/routes/api/session-bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createFileRoute } from '@tanstack/react-router'
import { json } from '@tanstack/react-start'
import {
getSessionHandoffExport,
isSessionHandoffKind,
} from '../../server/session-bundle'

function downloadName(filename: string) {
return filename.replace(/[^a-zA-Z0-9._-]/g, '-')
}

export const Route = createFileRoute('/api/session-bundle')({
server: {
handlers: {
GET: ({ request }) => {
try {
const url = new URL(request.url)
const rawKind = url.searchParams.get('kind') ?? 'bundle'
const kind = isSessionHandoffKind(rawKind) ? rawKind : 'bundle'
const payload = getSessionHandoffExport({
kind,
sessionKey: url.searchParams.get('sessionKey') ?? undefined,
friendlyId: url.searchParams.get('friendlyId') ?? undefined,
})

if (url.searchParams.get('download') === '1') {
return new Response(payload.markdown, {
headers: {
'content-type': 'text/markdown; charset=utf-8',
'content-disposition':
'attachment; filename="' + downloadName(payload.filename) + '"',
},
})
}

return json(payload)
} catch (err) {
return json(
{
error: err instanceof Error ? err.message : String(err),
},
{ status: 404 },
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The catch block maps all errors to 404, which hides real server errors and returns incorrect HTTP semantics.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/codex-claw/src/routes/api/session-bundle.ts, line 42:

<comment>The catch block maps all errors to 404, which hides real server errors and returns incorrect HTTP semantics.</comment>

<file context>
@@ -0,0 +1,48 @@
+            {
+              error: err instanceof Error ? err.message : String(err),
+            },
+            { status: 404 },
+          )
+        }
</file context>

)
}
},
},
},
})
3 changes: 1 addition & 2 deletions apps/codex-claw/src/screens/chat/components/chat-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import {
Settings01Icon,
} from '@hugeicons/core-free-icons'
import { ContextMeter } from './context-meter'
import type { ExportFormat } from '@/hooks/use-export'
import { Button } from '@/components/ui/button'
import { ExportMenu } from '@/components/export-menu'

type ExportFormat = 'markdown' | 'json' | 'text'

type ChatHeaderProps = {
activeTitle: string
wrapperRef?: React.Ref<HTMLDivElement>
Expand Down
Loading
Loading