@unieai/rabi-session-log-export 0.0.0-stage → 0.2.2-rc.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/client.js +2 -0
- package/lib/index.js +980 -0
- package/lib/types/archive.d.ts +132 -0
- package/lib/types/client/CopyToast.d.ts +21 -0
- package/lib/types/client/Dialog.d.ts +22 -0
- package/lib/types/client/controller.d.ts +69 -0
- package/lib/types/client/copy-controller.d.ts +76 -0
- package/lib/types/client/index.d.ts +24 -0
- package/lib/types/client/locales.d.ts +126 -0
- package/lib/types/index.d.ts +24 -0
- package/lib/types/markdown.d.ts +22 -0
- package/lib/types/routes.d.ts +14 -0
- package/locale/de.json +6 -0
- package/locale/en.json +5 -0
- package/locale/es.json +6 -0
- package/locale/fr.json +6 -0
- package/locale/ja.json +6 -0
- package/locale/pt.json +6 -0
- package/locale/ru.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +57 -4
- package/src/archive.ts +706 -0
- package/src/client/CopyToast.tsx +40 -0
- package/src/client/Dialog.tsx +55 -0
- package/src/client/controller.ts +139 -0
- package/src/client/copy-controller.ts +201 -0
- package/src/client/index.ts +105 -0
- package/src/client/locales.ts +149 -0
- package/src/index.ts +234 -0
- package/src/markdown.ts +367 -0
- package/src/routes.ts +17 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import type { ObservableSnapshot } from '@unieai/rabi-client-store'
|
|
3
|
+
import { IconWarningOutlineRegular, Toast } from '@unieai/rabi-client-ui-primitives'
|
|
4
|
+
import type { InjectFace, PropsLocale, PropsRuntime } from '@unieai/rabi-client-ui-slots'
|
|
5
|
+
import type {} from '@unieai/rabi-client-ui-layout/client'
|
|
6
|
+
import type { SessionMarkdownCopyState } from './copy-controller.ts'
|
|
7
|
+
import { NS } from './locales.ts'
|
|
8
|
+
|
|
9
|
+
/** Copy state and dismissal bound into the root overlay entry. */
|
|
10
|
+
export interface SessionMarkdownCopyToastInjected {
|
|
11
|
+
hooks: { sessionMarkdownCopy: ObservableSnapshot<SessionMarkdownCopyState> }
|
|
12
|
+
dismiss: () => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Props of the root overlay copy notice. */
|
|
16
|
+
export type SessionMarkdownCopyToastProps = PropsRuntime<'shell.overlay'>
|
|
17
|
+
& PropsLocale<typeof NS>
|
|
18
|
+
& InjectFace<SessionMarkdownCopyToastInjected>
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Show the settled copy outcome outside the Session Header and menu lifetime.
|
|
22
|
+
* @param props - copy notice hook, dismissal action, and localized copy.
|
|
23
|
+
* @returns the success or failure toast, or null while no notice shows.
|
|
24
|
+
*/
|
|
25
|
+
export function SessionMarkdownCopyToast({
|
|
26
|
+
useSessionMarkdownCopy, dismiss, t,
|
|
27
|
+
}: SessionMarkdownCopyToastProps): ReactNode {
|
|
28
|
+
const notice = useSessionMarkdownCopy(state => state.notice)
|
|
29
|
+
if (notice === null) return null
|
|
30
|
+
return notice.kind === 'success'
|
|
31
|
+
? <Toast key={notice.seq} text={t('toast.copied')} tone="success" onDone={dismiss} />
|
|
32
|
+
: (
|
|
33
|
+
<Toast
|
|
34
|
+
key={notice.seq}
|
|
35
|
+
text={t(notice.kind === 'preparationFailed' ? 'toast.preparationFailed' : 'toast.clipboardFailed')}
|
|
36
|
+
icon={<IconWarningOutlineRegular />}
|
|
37
|
+
onDone={dismiss}
|
|
38
|
+
/>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Root overlay showing one Session export at a time in initiation order. */
|
|
2
|
+
import type { ObservableSnapshot } from '@unieai/rabi-client-store'
|
|
3
|
+
import type { SessionId } from '@unieai/rabi-session/types'
|
|
4
|
+
import { Button, Modal } from '@unieai/rabi-client-ui-primitives'
|
|
5
|
+
import type { InjectFace, PropsLocale, PropsRuntime } from '@unieai/rabi-client-ui-slots'
|
|
6
|
+
import type {} from '@unieai/rabi-client-ui-layout/client'
|
|
7
|
+
import type { SessionLogDownloadState } from './controller.ts'
|
|
8
|
+
import { NS } from './locales.ts'
|
|
9
|
+
|
|
10
|
+
/** Download state and dismissal bound into the root overlay entry. */
|
|
11
|
+
export interface SessionLogDownloadDialogInjected {
|
|
12
|
+
hooks: { sessionLogDownload: ObservableSnapshot<SessionLogDownloadState> }
|
|
13
|
+
dismiss: (sessionId: SessionId) => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Root runtime, localized copy, and injected download state and actions. */
|
|
17
|
+
export type SessionLogDownloadDialogProps =
|
|
18
|
+
PropsRuntime<'shell.overlay'>
|
|
19
|
+
& PropsLocale<typeof NS>
|
|
20
|
+
& InjectFace<SessionLogDownloadDialogInjected>
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Show the earliest open export independently of the displayed conversation.
|
|
24
|
+
* @param props - root runtime, bound controller state, dismissal, and localized copy.
|
|
25
|
+
* @returns one modal, or null when all exports are dismissed.
|
|
26
|
+
*/
|
|
27
|
+
export function SessionLogDownloadDialog({
|
|
28
|
+
useSessionLogDownload, dismiss, t,
|
|
29
|
+
}: SessionLogDownloadDialogProps) {
|
|
30
|
+
const bySession = useSessionLogDownload(state => state.bySession)
|
|
31
|
+
const pending = Object.entries(bySession)
|
|
32
|
+
.filter((pair): pair is [string, NonNullable<typeof pair[1]>] => pair[1]?.open === true)
|
|
33
|
+
.sort((a, b) => a[1].order - b[1].order)[0]
|
|
34
|
+
if (pending === undefined) return null
|
|
35
|
+
const [id, entry] = pending
|
|
36
|
+
const sessionId = id as SessionId
|
|
37
|
+
const { status } = entry
|
|
38
|
+
const title = status === 'downloading'
|
|
39
|
+
? t('dialog.preparingTitle')
|
|
40
|
+
: status === 'success' ? t('dialog.successTitle') : t('dialog.errorTitle')
|
|
41
|
+
const description = status === 'downloading'
|
|
42
|
+
? t('dialog.preparingDescription')
|
|
43
|
+
: status === 'success' ? t('dialog.successDescription') : entry.error || t('dialog.commandFailed')
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<Modal
|
|
47
|
+
open
|
|
48
|
+
onClose={() => { dismiss(sessionId) }}
|
|
49
|
+
title={title}
|
|
50
|
+
description={`${t('dialog.session', { sessionId })} ${description}`}
|
|
51
|
+
closeLabel={t('dialog.close')}
|
|
52
|
+
footer={<Button variant="primary" onClick={() => { dismiss(sessionId) }}>{t('dialog.close')}</Button>}
|
|
53
|
+
/>
|
|
54
|
+
)
|
|
55
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/** Browser download operations and FIFO modal state shared by menu actions and `/export`. */
|
|
2
|
+
|
|
3
|
+
import { createSnapshotStore, type SnapshotStore } from '@unieai/rabi-client-store'
|
|
4
|
+
import type { SessionId } from '@unieai/rabi-session/types'
|
|
5
|
+
import { SESSION_LOG_EXPORT_ROUTE } from '../routes.ts'
|
|
6
|
+
|
|
7
|
+
/** Download phases presented by the shared modal. */
|
|
8
|
+
export type SessionLogDownloadStatus = 'downloading' | 'success' | 'error'
|
|
9
|
+
|
|
10
|
+
/** One Session's current download-dialog state. */
|
|
11
|
+
export interface SessionLogDownloadEntry {
|
|
12
|
+
/** Initiation order of this operation; repeated in-flight requests keep it. */
|
|
13
|
+
readonly order: number
|
|
14
|
+
readonly open: boolean
|
|
15
|
+
readonly status: SessionLogDownloadStatus
|
|
16
|
+
readonly error: string | null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Authoritative download states; open entries display in initiation order across Sessions. */
|
|
20
|
+
export interface SessionLogDownloadState {
|
|
21
|
+
bySession: Record<string, SessionLogDownloadEntry | undefined>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** HTTP carrier for the export route. */
|
|
25
|
+
type Fetch = (input: string | URL, init?: RequestInit) => Promise<Response>
|
|
26
|
+
type Save = (url: string, filename: string) => void
|
|
27
|
+
|
|
28
|
+
const INITIAL: SessionLogDownloadState = { bySession: {} }
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Collapse an untrusted Session id into the filename convention owned by the host endpoint.
|
|
32
|
+
* @param sessionId - Session whose archive is downloaded.
|
|
33
|
+
* @returns one safe browser download filename.
|
|
34
|
+
*/
|
|
35
|
+
export function sessionLogZipFilename(sessionId: SessionId): string {
|
|
36
|
+
return `rabi-session-${String(sessionId).replace(/[^A-Za-z0-9_-]/g, '_')}.zip`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Hand a Host download route to the browser download manager, which resolves it
|
|
41
|
+
* against the document's own base.
|
|
42
|
+
* @param url - document-relative Host download route.
|
|
43
|
+
* @param filename - browser download filename.
|
|
44
|
+
*/
|
|
45
|
+
export function downloadUrl(url: string, filename: string): void {
|
|
46
|
+
const anchor = document.createElement('a')
|
|
47
|
+
anchor.href = url
|
|
48
|
+
anchor.download = filename
|
|
49
|
+
anchor.click()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function messageOf(error: unknown): string {
|
|
53
|
+
return error instanceof Error ? error.message : String(error)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Owns one in-flight browser download per Session and publishes modal state. */
|
|
57
|
+
export class SessionLogDownloadController {
|
|
58
|
+
/** Stable snapshot source for the root modal and menu availability. */
|
|
59
|
+
readonly store: SnapshotStore<SessionLogDownloadState> = createSnapshotStore(INITIAL)
|
|
60
|
+
|
|
61
|
+
private readonly active = new Map<SessionId, { readonly abort: AbortController; readonly done: Promise<void> }>()
|
|
62
|
+
private disposed = false
|
|
63
|
+
private nextOrder = 0
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @param fetcher - HTTP carrier used to read the host-streamed ZIP.
|
|
67
|
+
* @param save - browser save operation.
|
|
68
|
+
*/
|
|
69
|
+
constructor(
|
|
70
|
+
private readonly fetcher: Fetch = (input, init) => fetch(input, init),
|
|
71
|
+
private readonly save: Save = downloadUrl,
|
|
72
|
+
) {}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Download one Session tree; concurrent gestures for the same Session share one operation.
|
|
76
|
+
* Different Sessions run concurrently; each new operation joins the modal FIFO.
|
|
77
|
+
* @param sessionId - root Session whose ZIP includes descendants and attachments.
|
|
78
|
+
* @returns after the browser save starts, an error state is published, or a late post-disposal request is ignored.
|
|
79
|
+
*/
|
|
80
|
+
download(sessionId: SessionId): Promise<void> {
|
|
81
|
+
const existing = this.active.get(sessionId)
|
|
82
|
+
if (existing !== undefined) return existing.done
|
|
83
|
+
if (this.disposed) return Promise.resolve()
|
|
84
|
+
const abort = new AbortController()
|
|
85
|
+
const done = this.run(sessionId, abort.signal, this.nextOrder++).finally(() => {
|
|
86
|
+
this.active.delete(sessionId)
|
|
87
|
+
})
|
|
88
|
+
this.active.set(sessionId, { abort, done })
|
|
89
|
+
return done
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Close one Session's dialog without cancelling an in-flight browser download.
|
|
94
|
+
* @param sessionId - Session whose modal closes.
|
|
95
|
+
*/
|
|
96
|
+
dismiss(sessionId: SessionId): void {
|
|
97
|
+
const current = this.store.getSnapshot().bySession[String(sessionId)]
|
|
98
|
+
if (current === undefined || !current.open) return
|
|
99
|
+
this.publish(sessionId, { ...current, open: false })
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Abort active fetches and reach quiescence.
|
|
104
|
+
* @returns after every active operation settles.
|
|
105
|
+
*/
|
|
106
|
+
async dispose(): Promise<void> {
|
|
107
|
+
this.disposed = true
|
|
108
|
+
const active = [...this.active.values()]
|
|
109
|
+
for (const operation of active) operation.abort.abort()
|
|
110
|
+
await Promise.allSettled(active.map(operation => operation.done))
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private async run(sessionId: SessionId, signal: AbortSignal, order: number): Promise<void> {
|
|
114
|
+
this.publish(sessionId, { order, open: true, status: 'downloading', error: null })
|
|
115
|
+
try {
|
|
116
|
+
const query = new URLSearchParams({ sessionId, includeDescendants: 'true' })
|
|
117
|
+
const route = `${SESSION_LOG_EXPORT_ROUTE}?${query.toString()}`
|
|
118
|
+
const response = await this.fetcher(route, { method: 'HEAD', signal })
|
|
119
|
+
if (!response.ok) {
|
|
120
|
+
const detail = await response.text().catch(() => '')
|
|
121
|
+
throw new Error(`Export failed: HTTP ${response.status}${detail === '' ? '' : ` ${detail}`}`)
|
|
122
|
+
}
|
|
123
|
+
if (signal.aborted) return
|
|
124
|
+
this.save(route, sessionLogZipFilename(sessionId))
|
|
125
|
+
const open = this.store.getSnapshot().bySession[String(sessionId)]?.open ?? true
|
|
126
|
+
this.publish(sessionId, { order, open, status: 'success', error: null })
|
|
127
|
+
} catch (error: unknown) {
|
|
128
|
+
if (signal.aborted) return
|
|
129
|
+
const open = this.store.getSnapshot().bySession[String(sessionId)]?.open ?? true
|
|
130
|
+
this.publish(sessionId, { order, open, status: 'error', error: messageOf(error) })
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private publish(sessionId: SessionId, entry: SessionLogDownloadEntry): void {
|
|
135
|
+
this.store.update((state) => {
|
|
136
|
+
state.bySession = { ...state.bySession, [String(sessionId)]: entry }
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser controller for the Session Header's "Copy Session as Markdown" action. One
|
|
3
|
+
* plugin instance owns one pending copy: the clipboard is a browser-wide
|
|
4
|
+
* destination, so a second request joins the pending operation instead of
|
|
5
|
+
* racing it.
|
|
6
|
+
*
|
|
7
|
+
* On hosts with `ClipboardItem` and `navigator.clipboard.write`, the controller
|
|
8
|
+
* starts that write synchronously from the click with a promised text Blob, so
|
|
9
|
+
* the browser keeps the user activation while the Host prepares the
|
|
10
|
+
* transcript. Other hosts fall back to `writeClipboard` after preparation. A
|
|
11
|
+
* disposal aborts the read and prevents later text from reaching the Blob, but
|
|
12
|
+
* an accepted operating-system clipboard write cannot be rolled back.
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { createSnapshotStore, type SnapshotStore } from '@unieai/rabi-client-store'
|
|
17
|
+
import type { SessionId } from '@unieai/rabi-session/types'
|
|
18
|
+
import { writeClipboard } from '@unieai/rabi-client-ui-primitives'
|
|
19
|
+
import { SESSION_MARKDOWN_EXPORT_ROUTE } from '../routes.ts'
|
|
20
|
+
|
|
21
|
+
/** Whether the latest copy attempt succeeded, prepared no transcript, or could not write. */
|
|
22
|
+
export type SessionMarkdownCopyNoticeKind = 'success' | 'preparationFailed' | 'clipboardFailed'
|
|
23
|
+
|
|
24
|
+
/** One settled copy notice; `seq` keys the toast so a repeat copy restarts it. */
|
|
25
|
+
export interface SessionMarkdownCopyNotice {
|
|
26
|
+
readonly kind: SessionMarkdownCopyNoticeKind
|
|
27
|
+
readonly seq: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Copy state shared by every Header row and the root overlay toast. */
|
|
31
|
+
export interface SessionMarkdownCopyState {
|
|
32
|
+
/** Whether this plugin instance has a copy operation still pending. */
|
|
33
|
+
copying: boolean
|
|
34
|
+
/** Latest settled copy notice, or null while none shows. */
|
|
35
|
+
notice: SessionMarkdownCopyNotice | null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Browser clipboard operations used by one copy attempt. */
|
|
39
|
+
export interface SessionMarkdownClipboard {
|
|
40
|
+
/** Whether a deferred write can start inside the click's activation window. */
|
|
41
|
+
supportsDeferredWrite(): boolean
|
|
42
|
+
/** Start one deferred text write; the browser observes the data promise as it settles. */
|
|
43
|
+
writeDeferred(data: Promise<Blob>): Promise<void>
|
|
44
|
+
/** Write prepared text on hosts without the deferred API; false means the browser rejected it. */
|
|
45
|
+
writeText(text: string): Promise<boolean>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** HTTP carrier for the Markdown route. */
|
|
49
|
+
type Fetch = (input: string | URL, init?: RequestInit) => Promise<Response>
|
|
50
|
+
|
|
51
|
+
const INITIAL: SessionMarkdownCopyState = { copying: false, notice: null }
|
|
52
|
+
|
|
53
|
+
const browserClipboard: SessionMarkdownClipboard = {
|
|
54
|
+
supportsDeferredWrite() {
|
|
55
|
+
const clipboard: unknown = Reflect.get(navigator, 'clipboard')
|
|
56
|
+
return typeof globalThis.ClipboardItem === 'function'
|
|
57
|
+
&& typeof clipboard === 'object' && clipboard !== null
|
|
58
|
+
&& typeof Reflect.get(clipboard, 'write') === 'function'
|
|
59
|
+
},
|
|
60
|
+
writeDeferred(data) {
|
|
61
|
+
return navigator.clipboard.write([new ClipboardItem({ 'text/plain': data })])
|
|
62
|
+
},
|
|
63
|
+
writeText: writeClipboard,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function startWrite(start: () => Promise<void>): Promise<void> {
|
|
67
|
+
try {
|
|
68
|
+
return start()
|
|
69
|
+
} catch (error) {
|
|
70
|
+
return Promise.reject(new Error('clipboard write could not start', { cause: error }))
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Owns the one pending Markdown copy for this plugin instance. */
|
|
75
|
+
export class SessionMarkdownCopyController {
|
|
76
|
+
/** uSES-safe state source shared by every Header row and the overlay toast. */
|
|
77
|
+
readonly store: SnapshotStore<SessionMarkdownCopyState> = createSnapshotStore(INITIAL)
|
|
78
|
+
|
|
79
|
+
private pending: { readonly abort: AbortController; readonly done: Promise<void> } | undefined
|
|
80
|
+
private disposed = false
|
|
81
|
+
private noticeSeq = 0
|
|
82
|
+
|
|
83
|
+
constructor(
|
|
84
|
+
private readonly fetcher: Fetch = (input, init) => fetch(input, init),
|
|
85
|
+
private readonly clipboard: SessionMarkdownClipboard = browserClipboard,
|
|
86
|
+
) {}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Copy one Session's complete transcript. A request received while a copy is
|
|
90
|
+
* pending joins that operation and keeps its originally captured Session.
|
|
91
|
+
* @param sessionId - Session captured by the click.
|
|
92
|
+
* @returns after the clipboard work settles, a failure notice is published, or a late post-disposal request is ignored.
|
|
93
|
+
*/
|
|
94
|
+
copy(sessionId: SessionId): Promise<void> {
|
|
95
|
+
if (this.disposed) return Promise.resolve()
|
|
96
|
+
if (this.pending !== undefined) return this.pending.done
|
|
97
|
+
const abort = new AbortController()
|
|
98
|
+
const done = this.run(sessionId, abort.signal).finally(() => {
|
|
99
|
+
this.pending = undefined
|
|
100
|
+
})
|
|
101
|
+
this.pending = { abort, done }
|
|
102
|
+
return done
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Clear the shown toast without affecting a pending copy. */
|
|
106
|
+
dismiss(): void {
|
|
107
|
+
if (this.store.getSnapshot().notice === null) return
|
|
108
|
+
this.store.update((state) => { state.notice = null })
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Abort a pending read, suppress a late clipboard write or notice, and reach
|
|
113
|
+
* quiescence. A write already accepted by the operating system cannot be
|
|
114
|
+
* undone.
|
|
115
|
+
* @returns after the pending operation and any admitted clipboard work settle.
|
|
116
|
+
*/
|
|
117
|
+
async dispose(): Promise<void> {
|
|
118
|
+
this.disposed = true
|
|
119
|
+
const pending = this.pending
|
|
120
|
+
pending?.abort.abort()
|
|
121
|
+
await pending?.done
|
|
122
|
+
this.store.set(INITIAL)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private run(sessionId: SessionId, signal: AbortSignal): Promise<void> {
|
|
126
|
+
this.store.update((state) => { state.copying = true })
|
|
127
|
+
const preparation = this.prepare(sessionId, signal)
|
|
128
|
+
if (this.clipboard.supportsDeferredWrite()) {
|
|
129
|
+
const data = preparation.then((markdown) => {
|
|
130
|
+
if (this.isCancelled() || signal.aborted) throw new Error('Session Markdown copy cancelled')
|
|
131
|
+
return new Blob([markdown], { type: 'text/plain' })
|
|
132
|
+
})
|
|
133
|
+
const write = startWrite(() => this.clipboard.writeDeferred(data))
|
|
134
|
+
return this.settleDeferred(preparation, data, write)
|
|
135
|
+
}
|
|
136
|
+
return this.settleFallback(preparation)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private async prepare(sessionId: SessionId, signal: AbortSignal): Promise<string> {
|
|
140
|
+
const query = new URLSearchParams({ sessionId })
|
|
141
|
+
const route = `${SESSION_MARKDOWN_EXPORT_ROUTE}?${query.toString()}`
|
|
142
|
+
const response = await this.fetcher(route, { signal })
|
|
143
|
+
if (!response.ok) throw new Error(`Session Markdown request failed: HTTP ${String(response.status)}`)
|
|
144
|
+
return response.text()
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private async settleDeferred(
|
|
148
|
+
preparation: Promise<string>,
|
|
149
|
+
data: Promise<Blob>,
|
|
150
|
+
write: Promise<void>,
|
|
151
|
+
): Promise<void> {
|
|
152
|
+
try {
|
|
153
|
+
const [prepared, blob, written] = await Promise.allSettled([preparation, data, write])
|
|
154
|
+
if (this.isCancelled()) return
|
|
155
|
+
if (prepared.status === 'rejected' || blob.status === 'rejected') {
|
|
156
|
+
this.publishNotice('preparationFailed')
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
if (written.status === 'rejected') {
|
|
160
|
+
this.publishNotice('clipboardFailed')
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
this.publishNotice('success')
|
|
164
|
+
} finally {
|
|
165
|
+
this.store.update((state) => { state.copying = false })
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private async settleFallback(preparation: Promise<string>): Promise<void> {
|
|
170
|
+
try {
|
|
171
|
+
let markdown: string
|
|
172
|
+
try {
|
|
173
|
+
markdown = await preparation
|
|
174
|
+
} catch {
|
|
175
|
+
if (!this.isCancelled()) this.publishNotice('preparationFailed')
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
if (this.isCancelled()) return
|
|
179
|
+
let accepted = false
|
|
180
|
+
try {
|
|
181
|
+
accepted = await this.clipboard.writeText(markdown)
|
|
182
|
+
} catch {
|
|
183
|
+
accepted = false
|
|
184
|
+
}
|
|
185
|
+
if (this.isCancelled()) return
|
|
186
|
+
this.publishNotice(accepted ? 'success' : 'clipboardFailed')
|
|
187
|
+
} finally {
|
|
188
|
+
this.store.update((state) => { state.copying = false })
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private isCancelled(): boolean {
|
|
193
|
+
return this.disposed
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private publishNotice(kind: SessionMarkdownCopyNoticeKind): void {
|
|
197
|
+
this.store.update((state) => {
|
|
198
|
+
state.notice = { kind, seq: ++this.noticeSeq }
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/** Browser plugin owning ZIP download and Markdown copy state, plus feedback controls. */
|
|
2
|
+
|
|
3
|
+
import type { Context as ClientContext } from '@unieai/cordis'
|
|
4
|
+
import type { SessionId } from '@unieai/rabi-session/types'
|
|
5
|
+
import type {} from '@unieai/rabi-client-locale/client'
|
|
6
|
+
import type {} from '@unieai/rabi-client-ui-commands/client'
|
|
7
|
+
import type {} from '@unieai/rabi-client-ui-renderer/client'
|
|
8
|
+
import type {} from '@unieai/rabi-client-ui-message-feedback/client'
|
|
9
|
+
import { createSnapshotStore } from '@unieai/rabi-client-store'
|
|
10
|
+
import type {} from '@unieai/rabi-client-ui-context-menu/client'
|
|
11
|
+
import type {} from '@unieai/rabi-client-ui-conversation/client'
|
|
12
|
+
import { IconCopyOutlineRegular, IconDownloadOutlineRegular, IconPaperPlaneOutlineRegular } from '@unieai/rabi-client-ui-primitives'
|
|
13
|
+
import { SessionLogDownloadController } from './controller.ts'
|
|
14
|
+
import { SessionMarkdownCopyController } from './copy-controller.ts'
|
|
15
|
+
import { SessionMarkdownCopyToast, type SessionMarkdownCopyToastInjected } from './CopyToast.tsx'
|
|
16
|
+
import { SessionLogDownloadDialog, type SessionLogDownloadDialogInjected } from './Dialog.tsx'
|
|
17
|
+
import { en, ja, es, fr, de, ru, pt, NS, zh, type SessionLogDownloadKey } from './locales.ts'
|
|
18
|
+
|
|
19
|
+
declare module '@unieai/cordis' {
|
|
20
|
+
interface Context {
|
|
21
|
+
sessionLogDownload: SessionLogDownloadController
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare module '@unieai/rabi-client-ui-slots' {
|
|
26
|
+
interface LocaleNamespaceMap {
|
|
27
|
+
'session-log-download': SessionLogDownloadKey
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type { SessionLogDownloadEntry, SessionLogDownloadState } from './controller.ts'
|
|
32
|
+
|
|
33
|
+
export const inject = ['slots', 'locale', 'contextMenus']
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Provide the download controller and mount its modal, the Markdown copy
|
|
37
|
+
* controller and its root toast, and the shared Session menu actions.
|
|
38
|
+
* @param ctx - browser context carrying slots and locale services.
|
|
39
|
+
*/
|
|
40
|
+
export function apply(ctx: ClientContext): void {
|
|
41
|
+
const controller = new SessionLogDownloadController()
|
|
42
|
+
const copyController = new SessionMarkdownCopyController()
|
|
43
|
+
ctx.provide('sessionLogDownload', controller)
|
|
44
|
+
ctx.effect(() => async () => { await controller.dispose() }, 'session-log-download: browser download lifecycle')
|
|
45
|
+
ctx.effect(() => async () => { await copyController.dispose() }, 'session-log-download: browser Markdown copy lifecycle')
|
|
46
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en, ja, es, fr, de, ru, pt }), 'session-log-download: browser dictionaries')
|
|
47
|
+
const feedbackAvailable = createSnapshotStore(false)
|
|
48
|
+
ctx.inject(['feedbackUi'], (scope: ClientContext) => {
|
|
49
|
+
scope.effect(() => {
|
|
50
|
+
feedbackAvailable.set(true)
|
|
51
|
+
return () => { feedbackAvailable.set(false) }
|
|
52
|
+
}, 'session-log-download: feedback availability')
|
|
53
|
+
})
|
|
54
|
+
const t = ctx.locale.bind(NS)
|
|
55
|
+
ctx.slots.inject('contextMenu.icon', function* () {
|
|
56
|
+
for (const [key, Icon] of Object.entries({
|
|
57
|
+
'session-log-download.copy': IconCopyOutlineRegular,
|
|
58
|
+
'session-log-download.download': IconDownloadOutlineRegular,
|
|
59
|
+
'session-log-download.feedback': IconPaperPlaneOutlineRegular,
|
|
60
|
+
})) yield ctx.slots.register({ name: 'contextMenu.icon', key }, Icon)
|
|
61
|
+
})
|
|
62
|
+
ctx.effect(() => ctx.contextMenus.register({
|
|
63
|
+
id: 'session-log-download.export', target: 'sessionHeader', order: 100,
|
|
64
|
+
observe: () => [controller.store, copyController.store, ctx.locale],
|
|
65
|
+
resolve: ({ sessionId }) => [
|
|
66
|
+
{ id: 'copy', label: t('menu.copyMarkdown'), icon: { key: 'session-log-download.copy' },
|
|
67
|
+
disabled: copyController.store.getSnapshot().copying, run: () => copyController.copy(sessionId) },
|
|
68
|
+
{ id: 'download', label: t('menu.download'), icon: { key: 'session-log-download.download' },
|
|
69
|
+
disabled: controller.store.getSnapshot().bySession[sessionId]?.status === 'downloading',
|
|
70
|
+
run: () => controller.download(sessionId) },
|
|
71
|
+
],
|
|
72
|
+
}), 'session-log-download: Header exports')
|
|
73
|
+
ctx.effect(() => ctx.contextMenus.register({
|
|
74
|
+
id: 'session-log-download.feedback', target: 'sessionHeader', order: 600,
|
|
75
|
+
observe: () => [feedbackAvailable, ctx.locale],
|
|
76
|
+
resolve: ({ sessionId }) => feedbackAvailable.getSnapshot() ? [{
|
|
77
|
+
id: 'feedback', label: t('menu.feedback'), icon: { key: 'session-log-download.feedback' }, separatorBefore: true,
|
|
78
|
+
// The feedback plugin can unload between menu preparation and selection.
|
|
79
|
+
run: () => { ctx.get('feedbackUi')?.openSession(sessionId) },
|
|
80
|
+
}] : [],
|
|
81
|
+
}), 'session-log-download: Header feedback')
|
|
82
|
+
ctx.on('command/executed', (sessionId, commandName, result) => {
|
|
83
|
+
if (commandName === 'export' && result.kind === 'success') void controller.download(sessionId)
|
|
84
|
+
})
|
|
85
|
+
ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
|
86
|
+
name: 'shell.overlay',
|
|
87
|
+
id: 'session-log-download.copy-toast',
|
|
88
|
+
locale: NS,
|
|
89
|
+
inject: (): SessionMarkdownCopyToastInjected => ({
|
|
90
|
+
hooks: { sessionMarkdownCopy: copyController.store },
|
|
91
|
+
dismiss: () => { copyController.dismiss() },
|
|
92
|
+
}),
|
|
93
|
+
}, SessionMarkdownCopyToast))
|
|
94
|
+
ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
|
95
|
+
name: 'shell.overlay',
|
|
96
|
+
id: 'session-log-download',
|
|
97
|
+
locale: NS,
|
|
98
|
+
inject: (): SessionLogDownloadDialogInjected => ({
|
|
99
|
+
hooks: { sessionLogDownload: controller.store },
|
|
100
|
+
dismiss: (sessionId: SessionId) => { controller.dismiss(sessionId) },
|
|
101
|
+
}),
|
|
102
|
+
}, SessionLogDownloadDialog))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type { SessionLogDownloadDialogInjected, SessionLogDownloadDialogProps } from './Dialog.tsx'
|