dsh-session-attachments 0.0.0-stage → 0.1.1
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/CHANGELOG.md +32 -0
- package/LICENSE +21 -0
- package/README.en.md +91 -0
- package/README.md +90 -2
- package/client.js +614 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +8 -0
- package/index.js +328 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +63 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
本文件记录每个版本的变化。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
4
|
+
|
|
5
|
+
## [Unreleased]
|
|
6
|
+
|
|
7
|
+
## [0.1.1] - 2026-12-02
|
|
8
|
+
|
|
9
|
+
### 安全
|
|
10
|
+
|
|
11
|
+
- 只把被动内容(图片、音视频、PDF、纯文本)交给 DSH 预览器;HTML、SVG、脚本等主动内容不再在应用内渲染,改为经 `.download` 接口以附件形式下载,文件名保留原样。
|
|
12
|
+
- 下载响应固定 `application/octet-stream` 并带 `X-Content-Type-Options: nosniff`,避免浏览器按内容嗅探成可执行页面。
|
|
13
|
+
|
|
14
|
+
### 修复
|
|
15
|
+
|
|
16
|
+
- 附件库判断会话是否可跳转时的兜底:会话列表快照缺少 `ids` 字段时改用 `items` 推导,避免整库被误判、跳转按钮全部停用。
|
|
17
|
+
- 「会话已不可用」提示补充刷新指引;已归档会话不再误报为不可用(列表为空时放行)。
|
|
18
|
+
|
|
19
|
+
## [0.1.0] - 2026-11-28
|
|
20
|
+
|
|
21
|
+
首个公开版本。
|
|
22
|
+
|
|
23
|
+
### 新增
|
|
24
|
+
|
|
25
|
+
- 右侧面板「附件」页:列出当前会话上传的图片(网格)和文件(列表),标出大小、时间、第几轮。
|
|
26
|
+
- 左侧栏「附件库」:按天分组列出所有会话的附件,支持搜索、类型筛选、排序。
|
|
27
|
+
- 预览:把附件克隆进工作区 `.attachments/`,用 DSH 自带预览器打开;macOS APFS 上不额外占空间。
|
|
28
|
+
- 「跳回原会话」:打开原会话,并在底部提示附件在第几轮。
|
|
29
|
+
- 插件出错时显示错误信息,不再整页空白。
|
|
30
|
+
- 中文、英文界面。
|
|
31
|
+
|
|
32
|
+
[0.1.0]: https://github.com/huoxingyue/dsh-session-attachments/releases/tag/v0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 v_zhangyue65
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Session attachments dsh-session-attachments
|
|
2
|
+
|
|
3
|
+
[简体中文](README.md) | English
|
|
4
|
+
|
|
5
|
+
Find every screenshot and file you have sent in DeepSeek Harness, in one click.
|
|
6
|
+
|
|
7
|
+
## Sound familiar?
|
|
8
|
+
|
|
9
|
+
- You sent the AI a screenshot last week and want to look at it again, but the only way is scrolling up through old chats.
|
|
10
|
+
- You uploaded a dozen images in one session and want to check which ones.
|
|
11
|
+
- You remember sending a file, but not which session it was in.
|
|
12
|
+
- The image in the message is too small to read.
|
|
13
|
+
|
|
14
|
+
## What you get
|
|
15
|
+
|
|
16
|
+
| You want to | Do this |
|
|
17
|
+
|---|---|
|
|
18
|
+
| Find an image or file you sent before | Click **Attachments** in the left sidebar, browse by date or search |
|
|
19
|
+
| See what this session has | Open **Attachments** in the session's right panel |
|
|
20
|
+
| See an image in detail | Click it to open it full size |
|
|
21
|
+
| Go back to where you sent it | Click **Go to session**; a note tells you which turn it is in |
|
|
22
|
+
|
|
23
|
+
No setup. Install, reload the page, and it works.
|
|
24
|
+
|
|
25
|
+
## How to use it
|
|
26
|
+
|
|
27
|
+
### 1. Find past attachments: the Attachments page
|
|
28
|
+
|
|
29
|
+
1. In the left sidebar of the main window, click **Attachments** (below Artifacts).
|
|
30
|
+
2. Attachments from every session are listed by day, newest first.
|
|
31
|
+
3. To find one fast, type a file name or session title in the search box, or click **Images** or **Documents** to see one type.
|
|
32
|
+
4. Each row has two buttons on the right:
|
|
33
|
+
- **Preview** opens the session and shows the file in the right panel.
|
|
34
|
+
- **Go to session** opens the session, and a note at the bottom tells you which turn the file is in. Use the session's turn navigator to get there.
|
|
35
|
+
|
|
36
|
+
### 2. See this session's attachments: the Attachments tab
|
|
37
|
+
|
|
38
|
+
1. Open a session and expand the right panel.
|
|
39
|
+
2. Click **+** to open a new tab, then choose **Attachments**.
|
|
40
|
+
3. Images are shown as thumbnails on top, files as a list below, each marked with its turn.
|
|
41
|
+
4. Click any item to open it in the right panel.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
1. In the left sidebar, click **Plugins**, then **Install a third-party plugin**.
|
|
46
|
+
2. Paste `dsh-session-attachments` into **Package name or address**, or the full URL `https://github.com/huoxingyue/dsh-session-attachments`
|
|
47
|
+
3. Confirm, then reload the page (`Cmd + R`).
|
|
48
|
+
|
|
49
|
+
Requires DeepSeek Harness 0.2.2 or later.
|
|
50
|
+
|
|
51
|
+
## FAQ
|
|
52
|
+
|
|
53
|
+
### 1. Does it upload my files anywhere?
|
|
54
|
+
|
|
55
|
+
No. The plugin runs only on your computer. It does not go online and does not send your attachments to the AI.
|
|
56
|
+
|
|
57
|
+
### 2. Does it use extra disk space?
|
|
58
|
+
|
|
59
|
+
Barely, on a Mac.
|
|
60
|
+
|
|
61
|
+
When you preview a file, the plugin puts it in a `.attachments` folder in the current workspace. On a Mac this does not copy the file's contents, so it takes almost no space. On other systems it makes a copy.
|
|
62
|
+
|
|
63
|
+
### 3. Why doesn't Go to session stop right at the image?
|
|
64
|
+
|
|
65
|
+
DSH does not yet let plugins control where a session scrolls. So the plugin tells you the turn number, and one click on the turn navigator takes you there.
|
|
66
|
+
|
|
67
|
+
### 4. Are files the AI created listed?
|
|
68
|
+
|
|
69
|
+
No. Only images and files you uploaded yourself.
|
|
70
|
+
|
|
71
|
+
### 5. What about sessions I deleted?
|
|
72
|
+
|
|
73
|
+
Their attachments are still listed, but you cannot preview them or go back to the session.
|
|
74
|
+
|
|
75
|
+
### 6. Why do HTML and SVG files download instead of opening?
|
|
76
|
+
|
|
77
|
+
Both formats can carry scripts, and rendering them inside the app would let someone else's code run with your app's privileges. Only passive media — images, audio, video, PDF and plain text — is previewed; everything else is downloaded.
|
|
78
|
+
|
|
79
|
+
### 7. How do I uninstall it?
|
|
80
|
+
|
|
81
|
+
Left sidebar: **Plugins** → **Installed** → **Session attachments** → **Uninstall**.
|
|
82
|
+
|
|
83
|
+
Your uploaded attachments are not affected. The `.attachments` folders in your workspaces stay; delete them by hand if you don't need them.
|
|
84
|
+
|
|
85
|
+
## Feedback
|
|
86
|
+
|
|
87
|
+
Found a problem or have an idea? Open an [issue](https://github.com/huoxingyue/dsh-session-attachments/issues). Release notes are in the [CHANGELOG](CHANGELOG.md).
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
[MIT](LICENSE) © 2026 huoxingyue. A personal project, not affiliated with DeepSeek.
|
package/README.md
CHANGED
|
@@ -1,3 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 会话附件 dsh-session-attachments
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
简体中文 | [English](README.en.md)
|
|
4
|
+
|
|
5
|
+
在 DeepSeek Harness 里,一键找回你发过的所有截图和文件。
|
|
6
|
+
|
|
7
|
+
## 你可能遇到过
|
|
8
|
+
|
|
9
|
+
- 上周发给 AI 的一张截图,现在想再看一眼,只能一页页往上翻聊天记录。
|
|
10
|
+
- 一个会话里来回传了十几张图,想核对自己到底传过哪些。
|
|
11
|
+
- 记得发过某个文件,却想不起是在哪个会话里发的。
|
|
12
|
+
- 消息里的图片太小,看不清细节。
|
|
13
|
+
|
|
14
|
+
## 装上之后
|
|
15
|
+
|
|
16
|
+
| 你想做的事 | 怎么做 |
|
|
17
|
+
|---|---|
|
|
18
|
+
| 找回以前发过的图或文件 | 左侧栏点「附件库」,按日期找,或直接搜索 |
|
|
19
|
+
| 看当前会话传过什么 | 在会话右侧面板打开「附件」 |
|
|
20
|
+
| 看清图片细节 | 点一下附件,大图打开 |
|
|
21
|
+
| 回到当时发图的会话 | 点「跳回原会话」,会提示这张图在第几轮 |
|
|
22
|
+
|
|
23
|
+
不用设置,装好刷新页面就能用。
|
|
24
|
+
|
|
25
|
+
## 怎么用
|
|
26
|
+
|
|
27
|
+
### 1. 找以前发过的附件:附件库
|
|
28
|
+
|
|
29
|
+
1. 在主界面左侧栏,点「附件库」(在「作品」下面)。
|
|
30
|
+
2. 所有会话的附件按天列出,最新的在最上面。
|
|
31
|
+
3. 想快速找到:在顶部搜索框输入文件名或会话标题,或点「图片」「文档」只看一类。
|
|
32
|
+
4. 找到后,每一行右边有两个按钮:
|
|
33
|
+
- 「预览」:打开原会话,并在右侧面板显示这个附件。
|
|
34
|
+
- 「跳回原会话」:打开原会话,底部提示附件在第几轮,用会话里的轮次导航就能翻到。
|
|
35
|
+
|
|
36
|
+
### 2. 看当前会话的附件:附件页
|
|
37
|
+
|
|
38
|
+
1. 进入一个会话,展开右侧面板。
|
|
39
|
+
2. 点「+」新建标签页,在列出的工具里选「附件」。
|
|
40
|
+
3. 上面是图片缩略图,下面是文件列表,每项都标了第几轮上传。
|
|
41
|
+
4. 点任意一项,就在右侧面板里打开查看。
|
|
42
|
+
|
|
43
|
+
## 安装
|
|
44
|
+
|
|
45
|
+
1. 在左侧栏点「插件」,再点「安装第三方插件」。
|
|
46
|
+
2. 在「包名或地址」里粘贴 `dsh-session-attachments`,也可以用完整地址 `https://github.com/huoxingyue/dsh-session-attachments`。
|
|
47
|
+
3. 确认安装,完成后刷新页面(`Cmd + R`)。
|
|
48
|
+
|
|
49
|
+
需要 DeepSeek Harness 0.2.2 或更新版本。
|
|
50
|
+
|
|
51
|
+
## 常见问题
|
|
52
|
+
|
|
53
|
+
### 1. 会把我的文件传到网上吗?
|
|
54
|
+
|
|
55
|
+
不会。插件只在你的电脑上运行,不联网,也不会把附件发给 AI。
|
|
56
|
+
|
|
57
|
+
### 2. 会多占硬盘吗?
|
|
58
|
+
|
|
59
|
+
在 Mac 上基本不会。
|
|
60
|
+
|
|
61
|
+
点开预览时,插件会在当前工作区里放一个 `.attachments` 文件夹。Mac 上它不复制文件内容,几乎不占空间;其他系统上会复制一份。
|
|
62
|
+
|
|
63
|
+
### 3. 为什么跳回会话后,不能直接停在那张图上?
|
|
64
|
+
|
|
65
|
+
DSH 目前不允许插件控制会话滚动到哪里。所以插件会告诉你图在第几轮,你用轮次导航点一下就到了。
|
|
66
|
+
|
|
67
|
+
### 4. AI 生成的文件会显示吗?
|
|
68
|
+
|
|
69
|
+
不会。这里只列你自己上传的图片和文件。
|
|
70
|
+
|
|
71
|
+
### 5. 删了的会话,附件还在吗?
|
|
72
|
+
|
|
73
|
+
还会列出来,但不能预览,也不能跳回。
|
|
74
|
+
|
|
75
|
+
### 6. HTML、SVG 文件为什么点了是下载?
|
|
76
|
+
|
|
77
|
+
这两类文件能带脚本,在应用里直接打开等于让别人的代码在你界面里跑,所以只允许下载后用外部程序打开。图片、音视频、PDF、纯文本仍然可以直接预览。
|
|
78
|
+
|
|
79
|
+
### 7. 怎么卸载?
|
|
80
|
+
|
|
81
|
+
左侧栏「插件」→「已安装」→ 找到「会话附件」→「卸载」。
|
|
82
|
+
|
|
83
|
+
你原来上传的附件不受影响。各工作区里的 `.attachments` 文件夹不会自动删除,不需要的话可以手动删掉。
|
|
84
|
+
|
|
85
|
+
## 反馈
|
|
86
|
+
|
|
87
|
+
遇到问题或有想法,欢迎提 [Issue](https://github.com/huoxingyue/dsh-session-attachments/issues)。更新记录见 [CHANGELOG](CHANGELOG.md)。
|
|
88
|
+
|
|
89
|
+
## 许可
|
|
90
|
+
|
|
91
|
+
[MIT](LICENSE) © 2026 huoxingyue。本插件为个人作品,与 DeepSeek 官方无关。
|
package/client.js
ADDED
|
@@ -0,0 +1,614 @@
|
|
|
1
|
+
// dsh-session-attachments: Client half. A right-panel tab lists this session's
|
|
2
|
+
// uploads; a left-sidebar page lists uploads across every session.
|
|
3
|
+
window.__ModuleLoader__.load({
|
|
4
|
+
id: 'dsh-session-attachments',
|
|
5
|
+
factory(require) {
|
|
6
|
+
const React = require('react')
|
|
7
|
+
const h = React.createElement
|
|
8
|
+
const { useState, useEffect, useMemo, useCallback, useSyncExternalStore } = React
|
|
9
|
+
|
|
10
|
+
const PLUGIN = 'dsh-session-attachments'
|
|
11
|
+
const NS = 'dsh-session-attachments'
|
|
12
|
+
const KIND = 'session-attachments'
|
|
13
|
+
const PANEL_ID = 'session-attachments.library'
|
|
14
|
+
const LIST = 'api/session-attachments.list'
|
|
15
|
+
const RAW = 'api/session-attachments.raw'
|
|
16
|
+
const OPEN = 'api/session-attachments.open'
|
|
17
|
+
const DOWNLOAD = 'api/session-attachments.download'
|
|
18
|
+
const POLL_MS = 5000
|
|
19
|
+
|
|
20
|
+
const zh = {
|
|
21
|
+
'tab.title': '附件',
|
|
22
|
+
'guide.title': '附件',
|
|
23
|
+
'guide.description': '本会话上传过的图片和文件',
|
|
24
|
+
'panel': '附件库',
|
|
25
|
+
'library.title': '附件库',
|
|
26
|
+
'images': '图片',
|
|
27
|
+
'files': '文件',
|
|
28
|
+
'empty': '这个会话还没有上传过附件',
|
|
29
|
+
'empty.hint': '在输入框里粘贴或拖入文件,就会出现在这里',
|
|
30
|
+
'library.empty': '还没有任何会话上传过附件',
|
|
31
|
+
'library.none': '没有符合条件的附件',
|
|
32
|
+
'loading': '正在读取…',
|
|
33
|
+
'error': '读取失败:{message}',
|
|
34
|
+
'turn': '第 {n} 轮',
|
|
35
|
+
'gone': '原文件已不在',
|
|
36
|
+
'preview': '预览',
|
|
37
|
+
'download': '下载',
|
|
38
|
+
'goto': '跳回原会话',
|
|
39
|
+
'refresh': '刷新',
|
|
40
|
+
'search': '搜索文件名或会话',
|
|
41
|
+
'filter.all': '全部',
|
|
42
|
+
'filter.image': '图片',
|
|
43
|
+
'filter.doc': '文档',
|
|
44
|
+
'filter.other': '其他',
|
|
45
|
+
'sort.time': '最近上传',
|
|
46
|
+
'sort.session': '按会话',
|
|
47
|
+
'sort.size': '按大小',
|
|
48
|
+
'col.name': '文件名',
|
|
49
|
+
'col.session': '所在会话',
|
|
50
|
+
'col.time': '上传时间',
|
|
51
|
+
'col.size': '大小',
|
|
52
|
+
'col.actions': '操作',
|
|
53
|
+
'today': '今天',
|
|
54
|
+
'yesterday': '昨天',
|
|
55
|
+
'untitled': '未命名会话',
|
|
56
|
+
'session.gone': '会话已不可用',
|
|
57
|
+
'session.gone.hint': '会话已不可用,刷新页面可恢复',
|
|
58
|
+
'open.failed': '打开失败:{message}',
|
|
59
|
+
'open.unsafe': '{name} 可以在应用外打开,已改为下载',
|
|
60
|
+
'footer': '点开时附件以克隆方式放到工作区 .attachments/ 下,不额外占用磁盘',
|
|
61
|
+
'count': '{n} 个',
|
|
62
|
+
'jump.notice': '已打开「{title}」,这张图在第 {n} 轮',
|
|
63
|
+
'jump.hint': '点会话右侧的轮次导航即可跳过去',
|
|
64
|
+
'close': '关闭',
|
|
65
|
+
}
|
|
66
|
+
const en = {
|
|
67
|
+
'tab.title': 'Attachments',
|
|
68
|
+
'guide.title': 'Attachments',
|
|
69
|
+
'guide.description': 'Images and files uploaded in this session',
|
|
70
|
+
'panel': 'Attachments',
|
|
71
|
+
'library.title': 'Attachments',
|
|
72
|
+
'images': 'Images',
|
|
73
|
+
'files': 'Files',
|
|
74
|
+
'empty': 'No attachments in this session yet',
|
|
75
|
+
'empty.hint': 'Paste or drop a file into the composer and it shows up here',
|
|
76
|
+
'library.empty': 'No session has attachments yet',
|
|
77
|
+
'library.none': 'No attachments match',
|
|
78
|
+
'loading': 'Loading…',
|
|
79
|
+
'error': 'Could not load: {message}',
|
|
80
|
+
'turn': 'Turn {n}',
|
|
81
|
+
'gone': 'The stored file is gone',
|
|
82
|
+
'preview': 'Preview',
|
|
83
|
+
'download': 'Download',
|
|
84
|
+
'goto': 'Go to session',
|
|
85
|
+
'refresh': 'Refresh',
|
|
86
|
+
'search': 'Search file or session',
|
|
87
|
+
'filter.all': 'All',
|
|
88
|
+
'filter.image': 'Images',
|
|
89
|
+
'filter.doc': 'Documents',
|
|
90
|
+
'filter.other': 'Other',
|
|
91
|
+
'sort.time': 'Newest',
|
|
92
|
+
'sort.session': 'By session',
|
|
93
|
+
'sort.size': 'By size',
|
|
94
|
+
'col.name': 'Name',
|
|
95
|
+
'col.session': 'Session',
|
|
96
|
+
'col.time': 'Uploaded',
|
|
97
|
+
'col.size': 'Size',
|
|
98
|
+
'col.actions': 'Actions',
|
|
99
|
+
'today': 'Today',
|
|
100
|
+
'yesterday': 'Yesterday',
|
|
101
|
+
'untitled': 'Untitled session',
|
|
102
|
+
'session.gone': 'Session unavailable',
|
|
103
|
+
'session.gone.hint': 'Session unavailable — refresh the page to restore it',
|
|
104
|
+
'open.failed': 'Could not open: {message}',
|
|
105
|
+
'open.unsafe': '{name} is active content, so it was downloaded instead',
|
|
106
|
+
'footer': 'Opening clones the attachment into .attachments/ in the workspace without using extra disk space',
|
|
107
|
+
'count': '{n}',
|
|
108
|
+
'jump.notice': 'Opened “{title}”. This file is in turn {n}',
|
|
109
|
+
'jump.hint': 'Use the turn navigator on the right of the chat to jump there',
|
|
110
|
+
'close': 'Close',
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const DOC_EXT = /\.(pdf|docx?|xlsx?|pptx?|csv|tsv|txt|md|json|html?|rtf|pages|numbers|key)$/i
|
|
114
|
+
|
|
115
|
+
function category(item) {
|
|
116
|
+
if (item.kind === 'image' || /^image\//.test(item.mediaType || '')) return 'image'
|
|
117
|
+
return DOC_EXT.test(item.name) ? 'doc' : 'other'
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function formatSize(bytes) {
|
|
121
|
+
if (!bytes) return '—'
|
|
122
|
+
if (bytes < 1024) return `${bytes} B`
|
|
123
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(bytes < 10240 ? 1 : 0)} KB`
|
|
124
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MB`
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
128
|
+
const clock = (time) => { const d = new Date(time); return `${pad(d.getHours())}:${pad(d.getMinutes())}` }
|
|
129
|
+
const dayKey = (time) => { const d = new Date(time); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}` }
|
|
130
|
+
|
|
131
|
+
function dayLabel(time, t) {
|
|
132
|
+
const key = dayKey(time)
|
|
133
|
+
const now = Date.now()
|
|
134
|
+
if (key === dayKey(now)) return t('today')
|
|
135
|
+
if (key === dayKey(now - 86400000)) return t('yesterday')
|
|
136
|
+
const d = new Date(time)
|
|
137
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const rawUrl = (sessionId, key) => `${RAW}?${new URLSearchParams({ session: sessionId, key })}`
|
|
141
|
+
|
|
142
|
+
/** Session-scoped file address, as DSH's own file tree builds it. */
|
|
143
|
+
function fileAddress(sessionId, path) {
|
|
144
|
+
const enc = (s) => encodeURIComponent(s).replace(/%3A/gi, ':')
|
|
145
|
+
const rel = path.replace(/^\.\//, '')
|
|
146
|
+
return `dsh-resource://file/session/${enc(sessionId)}/${rel.split('/').map(enc).join('/')}`
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function getJson(url, init) {
|
|
150
|
+
const response = await fetch(url, { cache: 'no-store', ...init })
|
|
151
|
+
const body = await response.json().catch(() => ({}))
|
|
152
|
+
if (!response.ok) throw new Error(body.error || `HTTP ${response.status}`)
|
|
153
|
+
return body
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Ask the Host to clone the attachment into the workspace; resolve to the file address. */
|
|
157
|
+
async function cloneAddress(sessionId, key) {
|
|
158
|
+
const body = await getJson(`${OPEN}?${new URLSearchParams({ session: sessionId, key })}`, { method: 'POST' })
|
|
159
|
+
return fileAddress(sessionId, body.path)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// HTML, SVG and other active content must not be rendered from the app origin:
|
|
163
|
+
// a preview runs its scripts with the app's own privileges. Those files are
|
|
164
|
+
// downloaded instead; only passive media is handed to the previewer.
|
|
165
|
+
const PREVIEWABLE = new Set([
|
|
166
|
+
'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'avif', 'ico', 'apng',
|
|
167
|
+
'mp4', 'webm', 'mov', 'm4v', 'mp3', 'wav', 'm4a', 'ogg', 'oga', 'flac',
|
|
168
|
+
'pdf', 'txt', 'md', 'markdown', 'log', 'csv', 'tsv', 'json', 'yaml', 'yml',
|
|
169
|
+
])
|
|
170
|
+
|
|
171
|
+
function isPreviewSafe(item) {
|
|
172
|
+
const type = String(item?.mediaType || '').toLowerCase()
|
|
173
|
+
if (type === 'image/svg+xml') return false
|
|
174
|
+
if (type.startsWith('image/') || type.startsWith('video/') || type.startsWith('audio/')) return true
|
|
175
|
+
const name = String(item?.name || '')
|
|
176
|
+
const dot = name.lastIndexOf('.')
|
|
177
|
+
if (dot < 0) return false
|
|
178
|
+
return PREVIEWABLE.has(name.slice(dot + 1).toLowerCase())
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Save the original bytes through the Host instead of rendering them here. */
|
|
182
|
+
function downloadItem(sessionId, item) {
|
|
183
|
+
const query = new URLSearchParams({ session: item.sessionId || sessionId || '', key: item.key })
|
|
184
|
+
const anchor = document.createElement('a')
|
|
185
|
+
anchor.href = `${DOWNLOAD}?${query}`
|
|
186
|
+
anchor.download = item.name || 'attachment'
|
|
187
|
+
anchor.rel = 'noopener'
|
|
188
|
+
document.body.appendChild(anchor)
|
|
189
|
+
anchor.click()
|
|
190
|
+
anchor.remove()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function useList(sessionId, poll) {
|
|
194
|
+
const [state, setState] = useState({ status: 'loading', data: null, error: '' })
|
|
195
|
+
const load = useCallback(() => {
|
|
196
|
+
const query = sessionId ? `?${new URLSearchParams({ session: sessionId })}` : ''
|
|
197
|
+
getJson(LIST + query)
|
|
198
|
+
.then((data) => setState({ status: 'ready', data, error: '' }))
|
|
199
|
+
.catch((error) => setState((s) => ({ status: s.data ? 'ready' : 'error', data: s.data, error: error.message })))
|
|
200
|
+
}, [sessionId])
|
|
201
|
+
useEffect(() => {
|
|
202
|
+
setState({ status: 'loading', data: null, error: '' })
|
|
203
|
+
load()
|
|
204
|
+
if (!poll) return undefined
|
|
205
|
+
const timer = setInterval(() => { if (document.visibilityState === 'visible') load() }, POLL_MS)
|
|
206
|
+
return () => clearInterval(timer)
|
|
207
|
+
}, [load, poll])
|
|
208
|
+
return [state, load]
|
|
209
|
+
}
|
|
210
|
+
const ICONS = {
|
|
211
|
+
image: 'M4 4.5A1.5 1.5 0 0 1 5.5 3h9A1.5 1.5 0 0 1 16 4.5v11a1.5 1.5 0 0 1-1.5 1.5h-9A1.5 1.5 0 0 1 4 15.5v-11ZM4 13l3.5-3.5 3 3 2-2L16 14M12.5 7.5h.01',
|
|
212
|
+
doc: 'M6 2.75h5.2l3.8 3.8v9.2A1.5 1.5 0 0 1 13.5 17.25h-7.5A1.5 1.5 0 0 1 4.5 15.75v-11.5A1.5 1.5 0 0 1 6 2.75ZM11 3v3.75h3.75M7.5 10.5h5M7.5 13.5h3.5',
|
|
213
|
+
other: 'M6 2.75h5.2l3.8 3.8v9.2A1.5 1.5 0 0 1 13.5 17.25h-7.5A1.5 1.5 0 0 1 4.5 15.75v-11.5A1.5 1.5 0 0 1 6 2.75ZM11 3v3.75h3.75',
|
|
214
|
+
clip: 'M15.5 9.5l-5.8 5.8a3.5 3.5 0 0 1-5-5l6.4-6.4a2.3 2.3 0 0 1 3.3 3.3l-6.3 6.3a1.1 1.1 0 0 1-1.6-1.6l5.6-5.6',
|
|
215
|
+
search: 'M9 15.5a6.5 6.5 0 1 0 0-13 6.5 6.5 0 0 0 0 13ZM17.5 17.5l-3.9-3.9',
|
|
216
|
+
refresh: 'M16 10a6 6 0 1 1-1.8-4.3M16 3.5v3h-3',
|
|
217
|
+
eye: 'M2.5 10s2.7-5.5 7.5-5.5 7.5 5.5 7.5 5.5-2.7 5.5-7.5 5.5S2.5 10 2.5 10ZM10 12.25a2.25 2.25 0 1 0 0-4.5 2.25 2.25 0 0 0 0 4.5Z',
|
|
218
|
+
jump: 'M11 3.5h5.5V9M16.5 3.5 9 11M14 12v3.5a1 1 0 0 1-1 1H4.5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1H8',
|
|
219
|
+
close: 'M5.5 5.5l9 9M14.5 5.5l-9 9',
|
|
220
|
+
download: 'M10 3v9.5M6 9l4 4 4-4M4 16.5h12',
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function Icon({ name, size = 16 }) {
|
|
224
|
+
return h('svg', { width: size, height: size, viewBox: '0 0 20 20', fill: 'none', 'aria-hidden': true, className: 'dsa-icon' },
|
|
225
|
+
h('path', { d: ICONS[name], stroke: 'currentColor', strokeWidth: 1.5, strokeLinecap: 'round', strokeLinejoin: 'round' }))
|
|
226
|
+
}
|
|
227
|
+
const ClipIcon = ({ size = 16 }) => h(Icon, { name: 'clip', size })
|
|
228
|
+
|
|
229
|
+
function ToolButton({ icon, label, onClick, disabled }) {
|
|
230
|
+
return h('button', { type: 'button', className: 'dsa-tool', title: label, 'aria-label': label, onClick, disabled }, h(Icon, { name: icon }))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const CSS = `
|
|
234
|
+
.dsa-tab{height:100%;min-height:0;display:flex;flex-direction:column;color:var(--dsw-alias-label-primary);background:var(--dsw-alias-bg-base);font-size:13px;line-height:1.5}
|
|
235
|
+
.dsa-tab-scroll{flex:1;min-height:0;overflow:auto;padding:4px 12px 16px}
|
|
236
|
+
.dsa-head{display:flex;align-items:center;justify-content:space-between;gap:8px;padding:8px 12px 4px}
|
|
237
|
+
.dsa-head-title{font-size:12px;color:var(--dsw-alias-label-tertiary)}
|
|
238
|
+
.dsa-section{margin-top:12px}
|
|
239
|
+
.dsa-section-title{display:flex;align-items:center;gap:6px;margin:0 0 8px;font-size:12px;font-weight:500;color:var(--dsw-alias-label-secondary)}
|
|
240
|
+
.dsa-section-title span{color:var(--dsw-alias-label-tertiary);font-weight:400}
|
|
241
|
+
.dsa-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(88px,1fr));gap:8px}
|
|
242
|
+
.dsa-thumb{position:relative;aspect-ratio:1;border:.5px solid var(--dsw-alias-border-l2);border-radius:8px;overflow:hidden;background:var(--dsw-alias-bg-skeleton,transparent);cursor:pointer;padding:0}
|
|
243
|
+
.dsa-thumb img{width:100%;height:100%;object-fit:cover;display:block}
|
|
244
|
+
.dsa-thumb:hover{border-color:var(--dsw-alias-state-business-primary)}
|
|
245
|
+
.dsa-thumb-turn{position:absolute;left:4px;bottom:4px;padding:0 5px;border-radius:4px;background:rgba(0,0,0,.55);color:#fff;font-size:11px;line-height:18px}
|
|
246
|
+
.dsa-row{display:flex;align-items:center;gap:10px;padding:6px 8px;border-radius:8px;cursor:pointer;min-width:0}
|
|
247
|
+
.dsa-row:hover{background:var(--dsw-alias-interactive-bg-hover)}
|
|
248
|
+
.dsa-row[data-gone=true]{opacity:.5;cursor:default}
|
|
249
|
+
.dsa-file-icon{flex:none;width:32px;height:32px;border-radius:6px;display:flex;align-items:center;justify-content:center;background:var(--dsw-alias-interactive-bg-hover);color:var(--dsw-alias-label-secondary)}
|
|
250
|
+
.dsa-row-main{flex:1;min-width:0}
|
|
251
|
+
.dsa-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--dsw-alias-label-primary)}
|
|
252
|
+
.dsa-meta{font-size:12px;color:var(--dsw-alias-label-tertiary);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
|
253
|
+
.dsa-tool{flex:none;display:inline-flex;align-items:center;justify-content:center;width:28px;height:28px;padding:0;border:0;border-radius:6px;background:none;color:var(--dsw-alias-label-secondary);cursor:pointer}
|
|
254
|
+
.dsa-tool:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover);color:var(--dsw-alias-label-primary)}
|
|
255
|
+
.dsa-tool:disabled{opacity:.4;cursor:default}
|
|
256
|
+
.dsa-empty{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:6px;padding:48px 16px;text-align:center;color:var(--dsw-alias-label-tertiary)}
|
|
257
|
+
.dsa-empty strong{font-weight:500;color:var(--dsw-alias-label-secondary)}
|
|
258
|
+
.dsa-error{margin:8px 12px;padding:8px 10px;border-radius:8px;color:var(--dsw-alias-state-error-primary);background:var(--dsw-alias-interactive-bg-hover);font-size:12px}
|
|
259
|
+
.dsa-footer{padding:8px 12px;border-top:.5px solid var(--dsw-alias-border-l2);font-size:11px;color:var(--dsw-alias-label-caption,var(--dsw-alias-label-tertiary))}
|
|
260
|
+
.dsa-icon{display:block;flex:none}
|
|
261
|
+
.dsa-crash{padding:24px;color:var(--dsw-alias-label-primary);font-size:13px}.dsa-crash pre{white-space:pre-wrap;font-size:12px;color:var(--dsw-alias-label-secondary)}
|
|
262
|
+
.dsa-page{width:100%;height:100%;min-height:0;display:flex;flex-direction:column;overflow:hidden;color:var(--dsw-alias-label-primary);background:var(--dsw-alias-bg-base);font-size:14px;line-height:1.6}
|
|
263
|
+
.dsa-page-scroll{flex:1;min-height:0;overflow:auto;scrollbar-gutter:stable}
|
|
264
|
+
.dsa-page-content{max-width:1120px;margin:0 auto;padding:0 clamp(24px,4vw,48px) 56px}
|
|
265
|
+
.dsa-page-heading{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:12px 16px;padding-top:28px;margin-bottom:16px}
|
|
266
|
+
[data-platform=darwin] .dsa-page-heading{padding-top:calc(28px + var(--dsh-frame-top-clearance,0px))}
|
|
267
|
+
.dsa-page-heading h1{margin:0;font-size:20px;font-weight:500;line-height:28px}
|
|
268
|
+
.dsa-tools{display:flex;align-items:center;gap:8px;margin-left:auto}
|
|
269
|
+
.dsa-search{display:flex;align-items:center;gap:6px;width:min(260px,50vw);height:32px;padding:0 10px;border:.5px solid var(--dsw-alias-border-l2);border-radius:8px;color:var(--dsw-alias-label-tertiary)}
|
|
270
|
+
.dsa-search:focus-within{border-color:var(--dsw-alias-state-business-primary)}
|
|
271
|
+
.dsa-search input{flex:1;min-width:0;height:100%;border:0;padding:0;background:none;color:var(--dsw-alias-label-primary);font:inherit;font-size:13px;outline:none}
|
|
272
|
+
.dsa-bar{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:8px;margin-bottom:12px}
|
|
273
|
+
.dsa-chips{display:flex;gap:6px}
|
|
274
|
+
.dsa-chip{height:28px;padding:0 12px;border:.5px solid var(--dsw-alias-border-l2);border-radius:14px;background:none;color:var(--dsw-alias-label-secondary);font:inherit;font-size:13px;cursor:pointer}
|
|
275
|
+
.dsa-chip[aria-pressed=true]{border-color:var(--dsw-alias-state-business-primary);color:var(--dsw-alias-state-business-primary)}
|
|
276
|
+
.dsa-select{height:28px;padding:0 8px;border:.5px solid var(--dsw-alias-border-l2);border-radius:8px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-secondary);font:inherit;font-size:13px}
|
|
277
|
+
.dsa-table{width:100%;border-collapse:collapse;table-layout:fixed}
|
|
278
|
+
.dsa-table th{text-align:left;font-size:12px;font-weight:400;color:var(--dsw-alias-label-tertiary);padding:6px 8px;border-bottom:.5px solid var(--dsw-alias-border-l2)}
|
|
279
|
+
.dsa-table td{padding:6px 8px;border-bottom:.5px solid var(--dsw-alias-border-l1,var(--dsw-alias-border-l2));font-size:13px;vertical-align:middle;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
280
|
+
.dsa-table tbody tr:hover{background:var(--dsw-alias-interactive-bg-hover)}
|
|
281
|
+
.dsa-day td{padding-top:18px;font-size:12px;font-weight:500;color:var(--dsw-alias-label-secondary);border-bottom:0}
|
|
282
|
+
.dsa-day:hover{background:none!important}
|
|
283
|
+
.dsa-cell-name{display:flex;align-items:center;gap:10px;min-width:0}
|
|
284
|
+
.dsa-mini{flex:none;width:32px;height:32px;border-radius:6px;object-fit:cover;border:.5px solid var(--dsw-alias-border-l2)}
|
|
285
|
+
.dsa-link{border:0;padding:0;background:none;color:var(--dsw-alias-label-secondary);font:inherit;font-size:13px;cursor:pointer;max-width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
286
|
+
.dsa-link:hover:not(:disabled){color:var(--dsw-alias-state-business-primary)}
|
|
287
|
+
.dsa-link:disabled{cursor:default;color:var(--dsw-alias-label-tertiary)}
|
|
288
|
+
.dsa-actions{display:flex;gap:2px}
|
|
289
|
+
@keyframes dsa-in{from{opacity:0;transform:translate(-50%,8px)}to{opacity:1;transform:translate(-50%,0)}}
|
|
290
|
+
.dsa-notice{position:fixed;left:50%;bottom:96px;transform:translateX(-50%);z-index:1000;display:flex;align-items:flex-start;gap:10px;max-width:min(480px,calc(100vw - 32px));padding:10px 12px 10px 14px;border-radius:10px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);border:.5px solid var(--dsw-alias-border-l2);box-shadow:0 6px 24px rgba(0,0,0,.14);font-size:13px;line-height:20px;animation:dsa-in .18s ease-out}
|
|
291
|
+
.dsa-notice-main{min-width:0}
|
|
292
|
+
.dsa-notice-main strong{display:block;font-weight:500;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
|
|
293
|
+
.dsa-notice-main span{color:var(--dsw-alias-label-secondary);font-size:12px}
|
|
294
|
+
.dsa-notice .dsa-tool{flex:none;width:24px;height:24px}
|
|
295
|
+
.dsa-toast{position:absolute;left:50%;bottom:24px;transform:translateX(-50%);padding:8px 14px;border-radius:8px;background:var(--dsw-alias-label-primary);color:var(--dsw-alias-bg-base);font-size:13px}
|
|
296
|
+
`
|
|
297
|
+
/** Use the slot's bound translator when given; fall back to our own tables. */
|
|
298
|
+
function translator(t) {
|
|
299
|
+
return (key, vars) => {
|
|
300
|
+
let text = typeof t === 'function' ? t(key, vars) : undefined
|
|
301
|
+
if (typeof text !== 'string' || text === key) text = (navigator.language || '').startsWith('zh') ? zh[key] ?? key : en[key] ?? key
|
|
302
|
+
if (vars) text = text.replace(/\{(\w+)\}/g, (m, k) => (k in vars ? String(vars[k]) : m))
|
|
303
|
+
return text
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ---------- Right-panel tab: this session ----------
|
|
308
|
+
|
|
309
|
+
function AttachmentsTab(props) {
|
|
310
|
+
const t = translator(props.t)
|
|
311
|
+
const sessionId = props.sessionId
|
|
312
|
+
const info = typeof props.useTabInfo === 'function' ? props.useTabInfo() : null
|
|
313
|
+
const [state, reload] = useList(sessionId, true)
|
|
314
|
+
const [error, setError] = useState('')
|
|
315
|
+
const items = state.data?.items ?? []
|
|
316
|
+
const images = items.filter((i) => category(i) === 'image')
|
|
317
|
+
const files = items.filter((i) => category(i) !== 'image')
|
|
318
|
+
|
|
319
|
+
const open = useCallback(async (item) => {
|
|
320
|
+
if (!item.available) return
|
|
321
|
+
setError('')
|
|
322
|
+
if (!isPreviewSafe(item)) { downloadItem(sessionId, item); return }
|
|
323
|
+
try {
|
|
324
|
+
const address = await cloneAddress(sessionId, item.key)
|
|
325
|
+
const tab = info?.tab
|
|
326
|
+
const panel = info?.panel
|
|
327
|
+
if (tab?.actions?.openResource) {
|
|
328
|
+
if (panel?.isFloating || panel?.id === undefined) tab.actions.openResource(address)
|
|
329
|
+
else tab.actions.openResource(address, { paneId: panel.id })
|
|
330
|
+
}
|
|
331
|
+
} catch (e) {
|
|
332
|
+
setError(t('open.failed', { message: e.message }))
|
|
333
|
+
}
|
|
334
|
+
}, [sessionId, info])
|
|
335
|
+
|
|
336
|
+
let body
|
|
337
|
+
if (state.status === 'loading') body = h('div', { className: 'dsa-empty' }, t('loading'))
|
|
338
|
+
else if (state.status === 'error') body = h('div', { className: 'dsa-error' }, t('error', { message: state.error }))
|
|
339
|
+
else if (items.length === 0) {
|
|
340
|
+
body = h('div', { className: 'dsa-empty' }, h(Icon, { name: 'clip', size: 28 }), h('strong', null, t('empty')), h('div', null, t('empty.hint')))
|
|
341
|
+
} else {
|
|
342
|
+
body = h(React.Fragment, null,
|
|
343
|
+
images.length > 0 && h('section', { className: 'dsa-section' },
|
|
344
|
+
h('h3', { className: 'dsa-section-title' }, t('images'), h('span', null, images.length)),
|
|
345
|
+
h('div', { className: 'dsa-grid' }, images.map((item) => h('button', {
|
|
346
|
+
key: item.key, type: 'button', className: 'dsa-thumb', title: `${item.name} · ${formatSize(item.bytes)} · ${clock(item.time)}`,
|
|
347
|
+
onClick: () => open(item), disabled: !item.available,
|
|
348
|
+
},
|
|
349
|
+
item.available ? h('img', { src: rawUrl(sessionId, item.key), alt: item.name, loading: 'lazy' }) : h('div', { className: 'dsa-empty' }, t('gone')),
|
|
350
|
+
h('span', { className: 'dsa-thumb-turn' }, t('turn', { n: item.turn || 1 }))))),
|
|
351
|
+
),
|
|
352
|
+
files.length > 0 && h('section', { className: 'dsa-section' },
|
|
353
|
+
h('h3', { className: 'dsa-section-title' }, t('files'), h('span', null, files.length)),
|
|
354
|
+
files.map((item) => h('div', {
|
|
355
|
+
key: item.key, className: 'dsa-row', 'data-gone': !item.available, role: 'button', tabIndex: 0,
|
|
356
|
+
onClick: () => open(item), onKeyDown: (e) => { if (e.key === 'Enter') open(item) },
|
|
357
|
+
title: item.available ? item.name : t('gone'),
|
|
358
|
+
},
|
|
359
|
+
h('div', { className: 'dsa-file-icon' }, h(Icon, { name: category(item) === 'doc' ? 'doc' : 'other', size: 18 })),
|
|
360
|
+
h('div', { className: 'dsa-row-main' },
|
|
361
|
+
h('div', { className: 'dsa-name' }, item.name),
|
|
362
|
+
h('div', { className: 'dsa-meta' }, `${formatSize(item.bytes)} · ${clock(item.time)} · ${t('turn', { n: item.turn || 1 })}`)),
|
|
363
|
+
))),
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
return h('div', { className: 'dsa-tab', 'data-testid': 'session-attachments-tab' },
|
|
368
|
+
h('div', { className: 'dsa-head' },
|
|
369
|
+
h('span', { className: 'dsa-head-title' }, items.length ? t('count', { n: items.length }) : ''),
|
|
370
|
+
h(ToolButton, { icon: 'refresh', label: t('refresh'), onClick: reload })),
|
|
371
|
+
error && h('div', { className: 'dsa-error' }, error),
|
|
372
|
+
h('div', { className: 'dsa-tab-scroll' }, body),
|
|
373
|
+
items.length > 0 && h('div', { className: 'dsa-footer' }, t('footer')))
|
|
374
|
+
}
|
|
375
|
+
// ---------- Left-sidebar page: every session ----------
|
|
376
|
+
|
|
377
|
+
function listedSessions(sessions) {
|
|
378
|
+
if (Array.isArray(sessions?.ids)) return sessions.ids
|
|
379
|
+
return (sessions?.items ?? []).map((item) => item.sessionId)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function sessionReachable(id, sessions, workspaces) {
|
|
383
|
+
const listed = listedSessions(sessions)
|
|
384
|
+
if (listed.length === 0) return true
|
|
385
|
+
if (workspaces?.archivedSessionIds?.includes(id)) return false
|
|
386
|
+
return listed.includes(id)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function LibraryPage(props) {
|
|
390
|
+
const t = translator(props.t)
|
|
391
|
+
// Fall back when the slot did not pass the injected props, so the page still renders.
|
|
392
|
+
const sessionsStore = storeOf(props.sessionsStore)
|
|
393
|
+
const workspacesStore = storeOf(props.workspacesStore)
|
|
394
|
+
const goToSession = props.goToSession ?? (() => {})
|
|
395
|
+
const previewIn = props.previewIn ?? (() => {})
|
|
396
|
+
const sessions = useSyncExternalStore(sessionsStore.subscribe, sessionsStore.getSnapshot)
|
|
397
|
+
const workspaces = useSyncExternalStore(workspacesStore.subscribe, workspacesStore.getSnapshot)
|
|
398
|
+
const [state, reload] = useList('', false)
|
|
399
|
+
const [query, setQuery] = useState('')
|
|
400
|
+
const [filter, setFilter] = useState('all')
|
|
401
|
+
const [sort, setSort] = useState('time')
|
|
402
|
+
const [toast, setToast] = useState('')
|
|
403
|
+
|
|
404
|
+
useEffect(() => {
|
|
405
|
+
if (!toast) return undefined
|
|
406
|
+
const timer = setTimeout(() => setToast(''), 3000)
|
|
407
|
+
return () => clearTimeout(timer)
|
|
408
|
+
}, [toast])
|
|
409
|
+
|
|
410
|
+
const titles = useMemo(() => {
|
|
411
|
+
const map = new Map()
|
|
412
|
+
for (const s of state.data?.sessions ?? []) map.set(s.id, s.title || t('untitled'))
|
|
413
|
+
return map
|
|
414
|
+
}, [state.data])
|
|
415
|
+
|
|
416
|
+
const rows = useMemo(() => {
|
|
417
|
+
const q = query.trim().toLowerCase()
|
|
418
|
+
let list = (state.data?.items ?? []).filter((item) => {
|
|
419
|
+
if (filter !== 'all' && category(item) !== filter) return false
|
|
420
|
+
if (!q) return true
|
|
421
|
+
return item.name.toLowerCase().includes(q) || (titles.get(item.sessionId) ?? '').toLowerCase().includes(q)
|
|
422
|
+
})
|
|
423
|
+
if (sort === 'size') list = [...list].sort((a, b) => b.bytes - a.bytes)
|
|
424
|
+
else if (sort === 'session') list = [...list].sort((a, b) => (titles.get(a.sessionId) ?? '').localeCompare(titles.get(b.sessionId) ?? '') || b.time - a.time)
|
|
425
|
+
return list
|
|
426
|
+
}, [state.data, query, filter, sort, titles])
|
|
427
|
+
|
|
428
|
+
const go = (item) => {
|
|
429
|
+
if (!sessionReachable(item.sessionId, sessions, workspaces)) return setToast(t('session.gone.hint'))
|
|
430
|
+
goToSession(item.sessionId, { turn: item.turn || 1, title: titles.get(item.sessionId) ?? t('untitled') })
|
|
431
|
+
}
|
|
432
|
+
const preview = async (item) => {
|
|
433
|
+
if (!item.available) return
|
|
434
|
+
if (!sessionReachable(item.sessionId, sessions, workspaces)) return setToast(t('session.gone.hint'))
|
|
435
|
+
if (!isPreviewSafe(item)) { downloadItem(item.sessionId, item); return setToast(t('open.unsafe', { name: item.name })) }
|
|
436
|
+
try {
|
|
437
|
+
const address = await cloneAddress(item.sessionId, item.key)
|
|
438
|
+
previewIn(item.sessionId, address)
|
|
439
|
+
} catch (e) {
|
|
440
|
+
setToast(t('open.failed', { message: e.message }))
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const tableRows = []
|
|
445
|
+
let lastGroup = ''
|
|
446
|
+
for (const item of rows) {
|
|
447
|
+
const group = sort === 'time' ? dayLabel(item.time, t) : sort === 'session' ? (titles.get(item.sessionId) ?? '') : ''
|
|
448
|
+
if (group && group !== lastGroup) {
|
|
449
|
+
tableRows.push(h('tr', { key: `g:${group}:${item.key}`, className: 'dsa-day' }, h('td', { colSpan: 5 }, group)))
|
|
450
|
+
lastGroup = group
|
|
451
|
+
}
|
|
452
|
+
const reachable = sessionReachable(item.sessionId, sessions, workspaces)
|
|
453
|
+
tableRows.push(h('tr', { key: `${item.sessionId}:${item.key}`, onDoubleClick: () => preview(item) },
|
|
454
|
+
h('td', null, h('div', { className: 'dsa-cell-name' },
|
|
455
|
+
category(item) === 'image' && item.available
|
|
456
|
+
? h('img', { className: 'dsa-mini', src: rawUrl(item.sessionId, item.key), alt: '', loading: 'lazy' })
|
|
457
|
+
: h('div', { className: 'dsa-file-icon' }, h(Icon, { name: category(item) === 'image' ? 'image' : category(item) === 'doc' ? 'doc' : 'other', size: 18 })),
|
|
458
|
+
h('span', { className: 'dsa-name', title: item.available ? item.name : t('gone') }, item.name))),
|
|
459
|
+
h('td', null, h('button', { type: 'button', className: 'dsa-link', disabled: !reachable, title: reachable ? t('goto') : t('session.gone.hint'), onClick: () => go(item) }, titles.get(item.sessionId) ?? t('untitled'))),
|
|
460
|
+
h('td', null, `${dayKey(item.time).slice(5)} ${clock(item.time)}`),
|
|
461
|
+
h('td', null, formatSize(item.bytes)),
|
|
462
|
+
h('td', null, h('div', { className: 'dsa-actions' },
|
|
463
|
+
h(ToolButton, {
|
|
464
|
+
icon: isPreviewSafe(item) ? 'eye' : 'download',
|
|
465
|
+
label: isPreviewSafe(item) ? t('preview') : t('download'),
|
|
466
|
+
onClick: () => preview(item),
|
|
467
|
+
disabled: !item.available || !reachable,
|
|
468
|
+
}),
|
|
469
|
+
h(ToolButton, { icon: 'jump', label: t('goto'), onClick: () => go(item), disabled: !reachable })))))
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
let body
|
|
473
|
+
if (state.status === 'loading') body = h('div', { className: 'dsa-empty' }, t('loading'))
|
|
474
|
+
else if (state.status === 'error') body = h('div', { className: 'dsa-error' }, t('error', { message: state.error }))
|
|
475
|
+
else if ((state.data?.items ?? []).length === 0) body = h('div', { className: 'dsa-empty' }, h(Icon, { name: 'clip', size: 28 }), h('strong', null, t('library.empty')))
|
|
476
|
+
else if (rows.length === 0) body = h('div', { className: 'dsa-empty' }, t('library.none'))
|
|
477
|
+
else {
|
|
478
|
+
body = h('table', { className: 'dsa-table' },
|
|
479
|
+
h('colgroup', null, h('col', null), h('col', { style: { width: '28%' } }), h('col', { style: { width: '120px' } }), h('col', { style: { width: '84px' } }), h('col', { style: { width: '72px' } })),
|
|
480
|
+
h('thead', null, h('tr', null, ['col.name', 'col.session', 'col.time', 'col.size', 'col.actions'].map((k) => h('th', { key: k }, t(k))))),
|
|
481
|
+
h('tbody', null, tableRows))
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return h('section', { className: 'dsa-page', 'aria-label': t('library.title'), 'data-testid': 'session-attachments-library', style: { position: 'relative' } },
|
|
485
|
+
h('div', { className: 'dsa-page-scroll' }, h('div', { className: 'dsa-page-content' },
|
|
486
|
+
h('div', { className: 'dsa-page-heading' },
|
|
487
|
+
h('h1', null, t('library.title')),
|
|
488
|
+
h('div', { className: 'dsa-tools' },
|
|
489
|
+
h('label', { className: 'dsa-search' }, h(Icon, { name: 'search', size: 14 }),
|
|
490
|
+
h('input', { value: query, placeholder: t('search'), 'aria-label': t('search'), onChange: (e) => setQuery(e.target.value) })),
|
|
491
|
+
h(ToolButton, { icon: 'refresh', label: t('refresh'), onClick: reload }))),
|
|
492
|
+
h('div', { className: 'dsa-bar' },
|
|
493
|
+
h('div', { className: 'dsa-chips' }, ['all', 'image', 'doc', 'other'].map((k) => h('button', {
|
|
494
|
+
key: k, type: 'button', className: 'dsa-chip', 'aria-pressed': filter === k, onClick: () => setFilter(k),
|
|
495
|
+
}, t(`filter.${k}`)))),
|
|
496
|
+
h('select', { className: 'dsa-select', value: sort, 'aria-label': t('sort.time'), onChange: (e) => setSort(e.target.value) },
|
|
497
|
+
['time', 'session', 'size'].map((k) => h('option', { key: k, value: k }, t(`sort.${k}`))))),
|
|
498
|
+
body)),
|
|
499
|
+
toast && h('div', { className: 'dsa-toast', role: 'status' }, toast))
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// ---------- Jump notice: survives the switch from the library to the session ----------
|
|
503
|
+
|
|
504
|
+
function createNoticeStore() {
|
|
505
|
+
let value = null
|
|
506
|
+
const listeners = new Set()
|
|
507
|
+
return {
|
|
508
|
+
getSnapshot: () => value,
|
|
509
|
+
subscribe: (listener) => { listeners.add(listener); return () => listeners.delete(listener) },
|
|
510
|
+
set: (next) => { value = next; for (const listener of listeners) listener() },
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function JumpNotice(props) {
|
|
515
|
+
const t = translator(props.t)
|
|
516
|
+
const store = props.noticeStore ?? EMPTY
|
|
517
|
+
const notice = useSyncExternalStore(store.subscribe, store.getSnapshot)
|
|
518
|
+
useEffect(() => {
|
|
519
|
+
if (!notice) return undefined
|
|
520
|
+
const timer = setTimeout(() => store.set?.(null), 8000)
|
|
521
|
+
return () => clearTimeout(timer)
|
|
522
|
+
}, [notice])
|
|
523
|
+
if (!notice) return null
|
|
524
|
+
return h('div', { key: notice.seq, className: 'dsa-notice', role: 'status' },
|
|
525
|
+
h(Icon, { name: 'clip', size: 16 }),
|
|
526
|
+
h('div', { className: 'dsa-notice-main' },
|
|
527
|
+
h('strong', null, t('jump.notice', { title: notice.title, n: notice.turn })),
|
|
528
|
+
h('span', null, t('jump.hint'))),
|
|
529
|
+
h(ToolButton, { icon: 'close', label: t('close'), onClick: () => store.set?.(null) }))
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// Show a crash inside the plugin instead of a blank page.
|
|
533
|
+
class Boundary extends React.Component {
|
|
534
|
+
constructor(props) { super(props); this.state = { error: null } }
|
|
535
|
+
static getDerivedStateFromError(error) { return { error } }
|
|
536
|
+
componentDidCatch(error, info) { console.error(`[${PLUGIN}]`, error, info?.componentStack) }
|
|
537
|
+
render() {
|
|
538
|
+
const { error } = this.state
|
|
539
|
+
if (!error) return this.props.children
|
|
540
|
+
return h('div', { className: 'dsa-crash', role: 'alert' },
|
|
541
|
+
h('strong', null, `${PLUGIN} 出错 / crashed`),
|
|
542
|
+
h('pre', null, String(error?.stack || error?.message || error).split('\n').slice(0, 8).join('\n')))
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
const guarded = (Comp) => function Guarded(props) { return h(Boundary, null, h(Comp, props)) }
|
|
546
|
+
|
|
547
|
+
// ---------- Registration ----------
|
|
548
|
+
|
|
549
|
+
const EMPTY = { getSnapshot: () => undefined, subscribe: () => () => {} }
|
|
550
|
+
// Bind the methods: useSyncExternalStore calls them detached from their object.
|
|
551
|
+
const storeOf = (value) => (typeof value?.getSnapshot === 'function' && typeof value?.subscribe === 'function'
|
|
552
|
+
? { getSnapshot: () => value.getSnapshot(), subscribe: (listener) => value.subscribe(listener) }
|
|
553
|
+
: EMPTY)
|
|
554
|
+
|
|
555
|
+
return {
|
|
556
|
+
inject: ['slots', 'locale'],
|
|
557
|
+
// Exposed for the tests: which items may be rendered by the previewer.
|
|
558
|
+
internals: { isPreviewSafe, PREVIEWABLE },
|
|
559
|
+
apply(ctx) {
|
|
560
|
+
ctx.effect(() => {
|
|
561
|
+
const tag = document.createElement('style')
|
|
562
|
+
tag.dataset.plugin = PLUGIN
|
|
563
|
+
tag.textContent = CSS
|
|
564
|
+
document.head.appendChild(tag)
|
|
565
|
+
return () => tag.remove()
|
|
566
|
+
}, `${PLUGIN}: styles`)
|
|
567
|
+
ctx.effect(() => ctx.locale.register(NS, { zh, en }), `${PLUGIN}: dictionaries`)
|
|
568
|
+
const bound = ctx.locale.bind?.(NS)
|
|
569
|
+
const t = translator(bound)
|
|
570
|
+
|
|
571
|
+
const tabs = ctx.get('sidebarRightTabs')
|
|
572
|
+
if (tabs?.register) {
|
|
573
|
+
ctx.effect(() => tabs.register({
|
|
574
|
+
id: PLUGIN,
|
|
575
|
+
kind: KIND,
|
|
576
|
+
keepMounted: false,
|
|
577
|
+
title: () => t('tab.title'),
|
|
578
|
+
guide: [{ id: 'session', order: 40, title: () => t('guide.title'), description: () => t('guide.description'), icon: ClipIcon }],
|
|
579
|
+
}), `${PLUGIN}: tab type`)
|
|
580
|
+
ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register(
|
|
581
|
+
{ name: 'sidebar.right.pane.tab', key: PLUGIN, locale: NS }, guarded(AttachmentsTab)))
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
const previewIn = (sessionId, address) => {
|
|
585
|
+
const sidebar = ctx.get('sidebarRight')
|
|
586
|
+
if (typeof sidebar?.openResourceIn === 'function') sidebar.openResourceIn(sessionId, address)
|
|
587
|
+
}
|
|
588
|
+
const notices = createNoticeStore()
|
|
589
|
+
let noticeSeq = 0
|
|
590
|
+
const goToSession = (sessionId, hint) => {
|
|
591
|
+
ctx.get('uiWorkspace')?.openSession?.(sessionId)
|
|
592
|
+
if (hint) notices.set({ ...hint, sessionId, seq: ++noticeSeq })
|
|
593
|
+
}
|
|
594
|
+
ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
|
595
|
+
name: 'shell.overlay', id: `${PLUGIN}.notice`, locale: NS,
|
|
596
|
+
inject: () => ({ noticeStore: notices }),
|
|
597
|
+
}, guarded(JumpNotice)))
|
|
598
|
+
|
|
599
|
+
ctx.slots.inject('main', () => ctx.slots.register({
|
|
600
|
+
name: 'main', key: PANEL_ID, locale: NS,
|
|
601
|
+
inject: () => ({
|
|
602
|
+
sessionsStore: storeOf(ctx.get('sessions')?.list),
|
|
603
|
+
workspacesStore: storeOf(ctx.get('workspaces')?.list),
|
|
604
|
+
goToSession,
|
|
605
|
+
previewIn: (sessionId, address) => { goToSession(sessionId); previewIn(sessionId, address) },
|
|
606
|
+
}),
|
|
607
|
+
}, guarded(LibraryPage)))
|
|
608
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
609
|
+
name: 'sidebar.panellist', id: PANEL_ID, order: 25, locale: NS, label: () => t('panel'),
|
|
610
|
+
}, ClipIcon))
|
|
611
|
+
},
|
|
612
|
+
}
|
|
613
|
+
},
|
|
614
|
+
})
|
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64">
|
|
2
|
+
<rect width="64" height="64" rx="14" fill="#3964FE"/>
|
|
3
|
+
<rect x="17" y="12" width="30" height="40" rx="5" fill="#FFFFFF" opacity="0.95"/>
|
|
4
|
+
<rect x="22" y="20" width="20" height="13" rx="2.5" fill="#C9D5FF"/>
|
|
5
|
+
<path d="M24 31l5-5 4 4 3-3 5 5" stroke="#3964FE" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
|
|
6
|
+
<rect x="22" y="38" width="20" height="3" rx="1.5" fill="#C9D5FF"/>
|
|
7
|
+
<rect x="22" y="44" width="13" height="3" rx="1.5" fill="#C9D5FF"/>
|
|
8
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// dsh-session-attachments: Host half. It reads session logs to list what users
|
|
2
|
+
// uploaded, serves attachment bytes for thumbnails, and clones an attachment into
|
|
3
|
+
// the session's workspace so DSH's own previewer can open it.
|
|
4
|
+
import { constants, existsSync, mkdirSync, readdirSync, readFileSync, statSync, copyFileSync } from 'node:fs'
|
|
5
|
+
import { execFileSync } from 'node:child_process'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import { basename, extname, join, relative, isAbsolute } from 'node:path'
|
|
8
|
+
import { zstdDecompressSync } from 'node:zlib'
|
|
9
|
+
|
|
10
|
+
export const name = 'dsh-session-attachments'
|
|
11
|
+
|
|
12
|
+
const PLUGIN = 'dsh-session-attachments'
|
|
13
|
+
const LIST_PATH = '/api/session-attachments.list'
|
|
14
|
+
const RAW_PATH = '/api/session-attachments.raw'
|
|
15
|
+
const OPEN_PATH = '/api/session-attachments.open'
|
|
16
|
+
const DOWNLOAD_PATH = '/api/session-attachments.download'
|
|
17
|
+
const CLONE_DIR = '.attachments'
|
|
18
|
+
const ZSTD_MAGIC = 0xfd2fb528
|
|
19
|
+
const SHA = /^[0-9a-f]{64}$/
|
|
20
|
+
const SESSION_ID = /^[A-Za-z0-9._-]{1,200}$/
|
|
21
|
+
|
|
22
|
+
const dshHome = () => process.env.DSH_HOME || join(homedir(), '.dsh')
|
|
23
|
+
const attachmentRoot = () => join(dshHome(), 'attachments', 'v1')
|
|
24
|
+
const sessionsRoot = () => join(dshHome(), 'sessions')
|
|
25
|
+
|
|
26
|
+
/** Split a multi-frame zstd log into frames (same layout DSH writes). */
|
|
27
|
+
function zstdFrames(buffer) {
|
|
28
|
+
const frames = []
|
|
29
|
+
let offset = 0
|
|
30
|
+
while (offset + 4 <= buffer.length) {
|
|
31
|
+
const start = offset
|
|
32
|
+
if (buffer.readUInt32LE(offset) !== ZSTD_MAGIC) break
|
|
33
|
+
offset += 4
|
|
34
|
+
if (offset >= buffer.length) break
|
|
35
|
+
const d = buffer.readUInt8(offset)
|
|
36
|
+
offset += 1
|
|
37
|
+
const flag = d >>> 6
|
|
38
|
+
const single = (d & 0x20) !== 0
|
|
39
|
+
const checksum = (d & 0x04) !== 0
|
|
40
|
+
const dict = (d & 3) === 3 ? 4 : d & 3
|
|
41
|
+
const size = flag === 0 ? (single ? 1 : 0) : 1 << flag
|
|
42
|
+
offset += (single ? 0 : 1) + dict + size
|
|
43
|
+
let complete = false
|
|
44
|
+
while (offset + 3 <= buffer.length) {
|
|
45
|
+
const header = buffer.readUIntLE(offset, 3)
|
|
46
|
+
offset += 3
|
|
47
|
+
const type = (header >>> 1) & 3
|
|
48
|
+
offset += type === 1 ? 1 : header >>> 3
|
|
49
|
+
if (offset > buffer.length) break
|
|
50
|
+
if (header & 1) { complete = true; break }
|
|
51
|
+
}
|
|
52
|
+
if (!complete) break
|
|
53
|
+
if (checksum) offset += 4
|
|
54
|
+
if (offset > buffer.length) break
|
|
55
|
+
frames.push(buffer.subarray(start, offset))
|
|
56
|
+
}
|
|
57
|
+
return frames
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function readLog(file) {
|
|
61
|
+
const raw = readFileSync(file)
|
|
62
|
+
let text = ''
|
|
63
|
+
for (const frame of zstdFrames(raw)) {
|
|
64
|
+
try { text += zstdDecompressSync(frame).toString('utf8') } catch { break }
|
|
65
|
+
}
|
|
66
|
+
return text
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const WANTED = /"type":\s*"(session|session\/title|turn\/start|user\/message)"/
|
|
70
|
+
|
|
71
|
+
/** Pull the session's header, title and uploaded attachments out of its log. */
|
|
72
|
+
function parseSession(file) {
|
|
73
|
+
const out = { id: '', cwd: '', createdAt: 0, title: '', turns: 0, items: [] }
|
|
74
|
+
let turn = 0
|
|
75
|
+
for (const line of readLog(file).split('\n')) {
|
|
76
|
+
if (!line || !WANTED.test(line.slice(0, 60))) continue
|
|
77
|
+
let event
|
|
78
|
+
try { event = JSON.parse(line) } catch { continue }
|
|
79
|
+
const data = event.data ?? {}
|
|
80
|
+
if (event.type === 'session') {
|
|
81
|
+
out.id = event.id ?? ''
|
|
82
|
+
out.cwd = event.cwd ?? ''
|
|
83
|
+
out.createdAt = Date.parse(event.createdAt ?? '') || event.time || 0
|
|
84
|
+
} else if (event.type === 'session/title') {
|
|
85
|
+
if (typeof data.title === 'string') out.title = data.title
|
|
86
|
+
} else if (event.type === 'turn/start') {
|
|
87
|
+
turn = typeof data.turn === 'number' ? data.turn : turn + 1
|
|
88
|
+
out.turns = Math.max(out.turns, turn)
|
|
89
|
+
} else if (event.type === 'user/message' && Array.isArray(data.content)) {
|
|
90
|
+
data.content.forEach((block, index) => {
|
|
91
|
+
const a = block?.attachment
|
|
92
|
+
if ((block?.type !== 'image' && block?.type !== 'file') || !a) return
|
|
93
|
+
const sha = String(a.attachmentId ?? '').replace(/^sha256:/, '')
|
|
94
|
+
if (!SHA.test(sha)) return
|
|
95
|
+
out.items.push({
|
|
96
|
+
key: `${event.seq}:${index}`,
|
|
97
|
+
sha,
|
|
98
|
+
kind: block.type,
|
|
99
|
+
name: typeof a.name === 'string' && a.name ? basename(a.name) : (block.type === 'image' ? 'image' : 'file'),
|
|
100
|
+
mediaType: a.mediaType ?? '',
|
|
101
|
+
bytes: a.bytes ?? 0,
|
|
102
|
+
width: a.width,
|
|
103
|
+
height: a.height,
|
|
104
|
+
time: event.time ?? 0,
|
|
105
|
+
seq: event.seq ?? 0,
|
|
106
|
+
turn,
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return out
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const cache = new Map()
|
|
115
|
+
|
|
116
|
+
/** Every session log on disk, parsed (cached by size and mtime). */
|
|
117
|
+
function allSessions() {
|
|
118
|
+
const root = sessionsRoot()
|
|
119
|
+
const result = []
|
|
120
|
+
const seen = new Set()
|
|
121
|
+
let groups = []
|
|
122
|
+
try { groups = readdirSync(root, { withFileTypes: true }) } catch { return result }
|
|
123
|
+
for (const group of groups) {
|
|
124
|
+
if (!group.isDirectory()) continue
|
|
125
|
+
let dirs = []
|
|
126
|
+
try { dirs = readdirSync(join(root, group.name), { withFileTypes: true }) } catch { continue }
|
|
127
|
+
for (const dir of dirs) {
|
|
128
|
+
if (!dir.isDirectory()) continue
|
|
129
|
+
const file = join(root, group.name, dir.name, 'session.v4.jsonl.zstd')
|
|
130
|
+
let stat
|
|
131
|
+
try { stat = statSync(file) } catch { continue }
|
|
132
|
+
seen.add(file)
|
|
133
|
+
const hit = cache.get(file)
|
|
134
|
+
if (hit && hit.size === stat.size && hit.mtimeMs === stat.mtimeMs) { result.push(hit.data); continue }
|
|
135
|
+
let data
|
|
136
|
+
try { data = parseSession(file) } catch { continue }
|
|
137
|
+
if (!data.id) data.id = dir.name
|
|
138
|
+
cache.set(file, { size: stat.size, mtimeMs: stat.mtimeMs, data })
|
|
139
|
+
result.push(data)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const key of cache.keys()) if (!seen.has(key)) cache.delete(key)
|
|
143
|
+
return result
|
|
144
|
+
}
|
|
145
|
+
const EXT = {
|
|
146
|
+
'image/webp': '.webp', 'image/png': '.png', 'image/jpeg': '.jpg', 'image/gif': '.gif',
|
|
147
|
+
'image/svg+xml': '.svg', 'image/heic': '.heic', 'image/avif': '.avif', 'image/bmp': '.bmp',
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Where the attachment store keeps these bytes. */
|
|
151
|
+
function storedPath(item) {
|
|
152
|
+
const root = attachmentRoot()
|
|
153
|
+
if (item.kind === 'image') return join(root, 'objects', item.sha.slice(0, 2), item.sha)
|
|
154
|
+
const named = join(root, 'files', item.sha.slice(0, 2), item.sha, item.name)
|
|
155
|
+
if (existsSync(named)) return named
|
|
156
|
+
return join(root, 'file-objects', item.sha.slice(0, 2), item.sha)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The file name to show and clone under: images get the extension of their real bytes. */
|
|
160
|
+
function displayName(item) {
|
|
161
|
+
if (item.kind !== 'image') return item.name
|
|
162
|
+
const ext = EXT[item.mediaType]
|
|
163
|
+
if (!ext) return item.name
|
|
164
|
+
const current = extname(item.name).toLowerCase()
|
|
165
|
+
if (current === ext || (ext === '.jpg' && current === '.jpeg')) return item.name
|
|
166
|
+
const stem = current ? item.name.slice(0, -current.length) : item.name
|
|
167
|
+
return stem + ext
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Clone the bytes (APFS copy-on-write via `cp -c` on macOS), falling back to a
|
|
172
|
+
* plain copy only when cloning is unavailable. Node's COPYFILE_FICLONE_FORCE is
|
|
173
|
+
* ENOSYS on macOS, so `cp -c` is the reliable way to get a real clone there.
|
|
174
|
+
*/
|
|
175
|
+
function placeFile(source, target) {
|
|
176
|
+
if (process.platform === 'darwin') {
|
|
177
|
+
try { execFileSync('/bin/cp', ['-c', source, target], { stdio: 'ignore' }); return 'clone' } catch {}
|
|
178
|
+
} else {
|
|
179
|
+
try { copyFileSync(source, target, constants.COPYFILE_FICLONE_FORCE); return 'clone' } catch {}
|
|
180
|
+
}
|
|
181
|
+
copyFileSync(source, target)
|
|
182
|
+
return 'copy'
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function publicItem(item) {
|
|
186
|
+
return { ...item, name: displayName(item), available: existsSync(storedPath(item)) }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function sessionSummary(s) {
|
|
190
|
+
return { id: s.id, cwd: s.cwd, title: s.title, createdAt: s.createdAt, turns: s.turns, count: s.items.length }
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function findItem(sessionId, key) {
|
|
194
|
+
const session = allSessions().find((s) => s.id === sessionId)
|
|
195
|
+
const item = session?.items.find((i) => i.key === key)
|
|
196
|
+
return session && item ? { session, item } : null
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const json = (body, status = 200) => Response.json(body, { status, headers: { 'cache-control': 'no-store' } })
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Active content (HTML, SVG, scripts) must never be rendered from this origin, so
|
|
203
|
+
* those files are served as downloads with a neutral content type.
|
|
204
|
+
*/
|
|
205
|
+
function contentDisposition(name) {
|
|
206
|
+
const fallback = name.replace(/[^\x20-\x7e]/g, '_').replace(/["\\]/g, '_').slice(0, 150) || 'download'
|
|
207
|
+
const encoded = encodeURIComponent(name).replace(/'/g, '%27')
|
|
208
|
+
return `attachment; filename="${fallback}"; filename*=UTF-8''${encoded}`
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function registerRoutes(routes) {
|
|
212
|
+
routes.register({
|
|
213
|
+
path: LIST_PATH,
|
|
214
|
+
methods: ['GET'],
|
|
215
|
+
requestBody: 'buffered',
|
|
216
|
+
fetch: async (request) => {
|
|
217
|
+
const sessionId = new URL(request.url).searchParams.get('session') ?? ''
|
|
218
|
+
const sessions = allSessions()
|
|
219
|
+
if (sessionId) {
|
|
220
|
+
const session = sessions.find((s) => s.id === sessionId)
|
|
221
|
+
if (!session) return json({ session: null, items: [] })
|
|
222
|
+
return json({ session: sessionSummary(session), items: session.items.map(publicItem) })
|
|
223
|
+
}
|
|
224
|
+
const items = []
|
|
225
|
+
for (const s of sessions) for (const item of s.items) items.push({ ...publicItem(item), sessionId: s.id })
|
|
226
|
+
items.sort((a, b) => b.time - a.time)
|
|
227
|
+
return json({ sessions: sessions.filter((s) => s.items.length > 0).map(sessionSummary), items })
|
|
228
|
+
},
|
|
229
|
+
})
|
|
230
|
+
routes.register({
|
|
231
|
+
path: RAW_PATH,
|
|
232
|
+
methods: ['GET'],
|
|
233
|
+
requestBody: 'buffered',
|
|
234
|
+
fetch: async (request) => {
|
|
235
|
+
const params = new URL(request.url).searchParams
|
|
236
|
+
const sessionId = params.get('session') ?? ''
|
|
237
|
+
if (!SESSION_ID.test(sessionId)) return json({ error: 'bad session' }, 400)
|
|
238
|
+
const found = findItem(sessionId, params.get('key') ?? '')
|
|
239
|
+
if (!found) return json({ error: 'not found' }, 404)
|
|
240
|
+
const path = storedPath(found.item)
|
|
241
|
+
if (!existsSync(path)) return json({ error: 'gone' }, 404)
|
|
242
|
+
const type = found.item.mediaType || 'application/octet-stream'
|
|
243
|
+
return new Response(readFileSync(path), {
|
|
244
|
+
headers: { 'content-type': type, 'cache-control': 'private, max-age=86400', 'x-content-type-options': 'nosniff' },
|
|
245
|
+
})
|
|
246
|
+
},
|
|
247
|
+
})
|
|
248
|
+
routes.register({
|
|
249
|
+
path: OPEN_PATH,
|
|
250
|
+
methods: ['POST'],
|
|
251
|
+
requestBody: 'buffered',
|
|
252
|
+
fetch: async (request) => {
|
|
253
|
+
const params = new URL(request.url).searchParams
|
|
254
|
+
const sessionId = params.get('session') ?? ''
|
|
255
|
+
if (!SESSION_ID.test(sessionId)) return json({ error: 'bad session' }, 400)
|
|
256
|
+
const found = findItem(sessionId, params.get('key') ?? '')
|
|
257
|
+
if (!found) return json({ error: 'not found' }, 404)
|
|
258
|
+
const { session, item } = found
|
|
259
|
+
if (!session.cwd || !isAbsolute(session.cwd) || !existsSync(session.cwd)) return json({ error: 'workspace missing' }, 409)
|
|
260
|
+
const source = storedPath(item)
|
|
261
|
+
if (!existsSync(source)) return json({ error: 'gone' }, 404)
|
|
262
|
+
const dir = join(session.cwd, CLONE_DIR, sessionId, item.sha.slice(0, 8))
|
|
263
|
+
const target = join(dir, displayName(item))
|
|
264
|
+
if (relative(session.cwd, target).startsWith('..')) return json({ error: 'bad name' }, 400)
|
|
265
|
+
let how = 'existing'
|
|
266
|
+
if (!existsSync(target)) {
|
|
267
|
+
mkdirSync(dir, { recursive: true })
|
|
268
|
+
how = placeFile(source, target)
|
|
269
|
+
}
|
|
270
|
+
return json({ path: target, relative: relative(session.cwd, target), how })
|
|
271
|
+
},
|
|
272
|
+
})
|
|
273
|
+
routes.register({
|
|
274
|
+
path: DOWNLOAD_PATH,
|
|
275
|
+
methods: ['GET'],
|
|
276
|
+
requestBody: 'buffered',
|
|
277
|
+
fetch: async (request) => {
|
|
278
|
+
const params = new URL(request.url).searchParams
|
|
279
|
+
const sessionId = params.get('session') ?? ''
|
|
280
|
+
if (!SESSION_ID.test(sessionId)) return json({ error: 'bad session' }, 400)
|
|
281
|
+
const found = findItem(sessionId, params.get('key') ?? '')
|
|
282
|
+
if (!found) return json({ error: 'not found' }, 404)
|
|
283
|
+
const path = storedPath(found.item)
|
|
284
|
+
if (!existsSync(path)) return json({ error: 'gone' }, 404)
|
|
285
|
+
return new Response(readFileSync(path), {
|
|
286
|
+
headers: {
|
|
287
|
+
'content-type': 'application/octet-stream',
|
|
288
|
+
'content-disposition': contentDisposition(displayName(found.item)),
|
|
289
|
+
'x-content-type-options': 'nosniff',
|
|
290
|
+
'cache-control': 'no-store',
|
|
291
|
+
},
|
|
292
|
+
})
|
|
293
|
+
},
|
|
294
|
+
})
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const ROUTES_RETRY_MS = 100
|
|
298
|
+
const ROUTES_TRIES = 100
|
|
299
|
+
|
|
300
|
+
/** Register the routes, retrying while an older copy of this plugin still holds them. */
|
|
301
|
+
function registerWhenFree(sub, warn) {
|
|
302
|
+
let timer
|
|
303
|
+
let gone = false
|
|
304
|
+
sub.effect?.(() => () => { gone = true; clearTimeout(timer) }, `${PLUGIN}: routes`)
|
|
305
|
+
const attempt = (left) => {
|
|
306
|
+
if (gone) return
|
|
307
|
+
const got = []
|
|
308
|
+
try {
|
|
309
|
+
registerRoutes({ register: (route) => { const undo = sub.connection.fetch.register(route); got.push(undo); return undo } })
|
|
310
|
+
sub.effect?.(() => () => { for (const undo of got.reverse()) if (typeof undo === 'function') undo() }, `${PLUGIN}: route cleanup`)
|
|
311
|
+
} catch (error) {
|
|
312
|
+
for (const undo of got.reverse()) if (typeof undo === 'function') undo()
|
|
313
|
+
const message = error?.message ?? String(error)
|
|
314
|
+
if (left > 1 && /already registered/i.test(message)) timer = setTimeout(() => attempt(left - 1), ROUTES_RETRY_MS)
|
|
315
|
+
else warn(`routes: ${message}`)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
attempt(ROUTES_TRIES)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function apply(ctx) {
|
|
322
|
+
ctx.inject?.(['connection'], (sub) => {
|
|
323
|
+
registerWhenFree(sub, (message) => ctx.logger?.warn?.(`${PLUGIN}: ${message}`))
|
|
324
|
+
})
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Exposed for the test script.
|
|
328
|
+
export const internal = { parseSession, allSessions, storedPath, displayName, zstdFrames, contentDisposition }
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Session attachments",
|
|
4
|
+
"description": "The Attachments tab in the right panel lists the images and files uploaded in this session. The Attachments page in the left sidebar lists uploads from every session by day, with search, filters, and sorting. Open one in the built-in previewer, or go back to its session with a note on which turn it is in."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-attachments",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "DeepSeek Harness plugin: see and preview the files and images you uploaded, per session and across sessions.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "huoxingyue",
|
|
7
|
+
"homepage": "https://github.com/huoxingyue/dsh-session-attachments#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/huoxingyue/dsh-session-attachments.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/huoxingyue/dsh-session-attachments/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "index.js",
|
|
17
|
+
"keywords": [
|
|
18
|
+
"dsh",
|
|
19
|
+
"deepseek-harness",
|
|
20
|
+
"dsh-plugin",
|
|
21
|
+
"attachments"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./index.js",
|
|
25
|
+
"./client": "./client.js",
|
|
26
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
27
|
+
"./locale/*.json": "./locale/*.json",
|
|
28
|
+
"./icon": "./icon.svg",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"index.js",
|
|
33
|
+
"client.js",
|
|
34
|
+
"cordis.patch.yml",
|
|
35
|
+
"locale/*.json",
|
|
36
|
+
"icon.svg",
|
|
37
|
+
"README.md",
|
|
38
|
+
"README.en.md",
|
|
39
|
+
"CHANGELOG.md",
|
|
40
|
+
"LICENSE"
|
|
41
|
+
],
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=24",
|
|
44
|
+
"dsh": ">=0.2.2-0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"dsh": {
|
|
50
|
+
"bundle": {
|
|
51
|
+
"patch": "./cordis.patch.yml"
|
|
52
|
+
},
|
|
53
|
+
"client": {
|
|
54
|
+
"platform": "web",
|
|
55
|
+
"immediately": true,
|
|
56
|
+
"inject": [
|
|
57
|
+
"@deepseek-ai/dsh-client-locale",
|
|
58
|
+
"@deepseek-ai/dsh-client-ui-layout",
|
|
59
|
+
"@deepseek-ai/dsh-client-ui-sidebar",
|
|
60
|
+
"@deepseek-ai/dsh-client-ui-sidebar-right",
|
|
61
|
+
"@deepseek-ai/dsh-client-ui-workspace"
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|