@nelsonlongxiang/dsh-prompt-templates 0.2.9 → 0.4.4
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/README.md +10 -11
- package/README_zh.md +10 -11
- package/cordis.patch.yml +4 -17
- package/images/image-01.png +0 -0
- package/lib/client/Panel.js +154 -23
- package/lib/client/api.js +19 -0
- package/lib/client/index.js +11 -1
- package/lib/client/locales.js +12 -0
- package/lib/client/service.js +52 -3
- package/lib/client.js +423 -67
- package/lib/client.js.map +1 -1
- package/lib/index.js +57 -47
- package/lib/store.js +275 -0
- package/lib/types/client/api.d.ts +14 -1
- package/lib/types/client/locales.d.ts +12 -0
- package/lib/types/client/service.d.ts +19 -3
- package/lib/types/client/slots.d.ts +19 -4
- package/lib/types/index.d.ts +13 -19
- package/lib/types/store.d.ts +57 -0
- package/lib/types/types.d.ts +20 -3
- package/lib/types.js +2 -2
- package/package.json +8 -15
- package/python/README.md +0 -26
- package/python/pyproject.toml +0 -33
- package/python/src/dsh_prompt_templates/__init__.py +0 -22
- package/python/src/dsh_prompt_templates/cli.py +0 -216
- package/python/src/dsh_prompt_templates/db.py +0 -125
- package/python/src/dsh_prompt_templates/models.py +0 -59
- package/python/src/dsh_prompt_templates/server.py +0 -170
- package/python/src/dsh_prompt_templates/store.py +0 -156
- package/python/uv.lock +0 -520
- package/scripts/bootstrap.mjs +0 -38
package/README.md
CHANGED
|
@@ -4,7 +4,9 @@ English | [中文](README_zh.md)
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@nelsonlongxiang/dsh-prompt-templates)
|
|
6
6
|
|
|
7
|
-
Quick prompt templates for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness): global and per-session templates, a right-side browser panel, and
|
|
7
|
+
Quick prompt templates for [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness): global and per-session templates, a right-side browser panel, and pure-TS SQLite persistence via `node:sqlite` — one self-contained plugin, installable with `dsh plugin`.
|
|
8
|
+
|
|
9
|
+

|
|
8
10
|
|
|
9
11
|
## Install
|
|
10
12
|
|
|
@@ -14,7 +16,7 @@ dsh plugin --profile web add @nelsonlongxiang/dsh-prompt-templates
|
|
|
14
16
|
|
|
15
17
|
Restart `dsh web`, then look for the templates button in the composer tool row.
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
No Python toolchain required — since 0.3.0 the host half persists templates through `node:sqlite` (Node ≥ 24) directly inside the host process, reading and writing the same `$DSH_HOME/ext/prompt-templates/db.sqlite3` the former Python child owned (zero data migration).
|
|
18
20
|
|
|
19
21
|
## What you get
|
|
20
22
|
|
|
@@ -22,35 +24,32 @@ Restart `dsh web`, then look for the templates button in the composer tool row.
|
|
|
22
24
|
- **Insert or send** — one click appends the template into the draft; send-now dispatches immediately; edit, delete, and make-global all live in the row
|
|
23
25
|
- **Search & scroll** — filter the list as you type, reliable scrolling for long collections
|
|
24
26
|
- **Drag to reposition, double-click to reset** — the panel remembers where you put it
|
|
25
|
-
- **Durable by design** — templates persist in SQLite through a
|
|
27
|
+
- **Durable by design** — templates persist in SQLite through a pure-TS store (`node:sqlite`) owned by the host plugin and closed with its lifecycle
|
|
26
28
|
|
|
27
29
|
## How it works
|
|
28
30
|
|
|
29
31
|
```text
|
|
30
32
|
src/ TypeScript plugin (host half + browser half)
|
|
31
|
-
index.ts Host:
|
|
33
|
+
index.ts Host: owns the TS store, exposes the HTTP routes
|
|
34
|
+
store.ts Pure-TS template store over node:sqlite
|
|
32
35
|
client/ Browser: panel UI registered into shell.overlay and
|
|
33
36
|
conversation.input.right
|
|
34
|
-
python/ Python backend package (template domain only)
|
|
35
|
-
src/dsh_prompt_templates/
|
|
36
|
-
pyproject.toml models, store, JSON-RPC server, CLI
|
|
37
37
|
cordis.patch.yml Bundle patch: mounts the plugin row
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
The
|
|
40
|
+
The store is pure TS inside the host half; no child process is spawned.
|
|
41
41
|
|
|
42
42
|
## Security
|
|
43
43
|
|
|
44
44
|
- Template content reaches a model request only as ordinary user text the user chose to insert — no automatic injection into any prompt
|
|
45
|
-
- The panel talks to the
|
|
45
|
+
- The panel talks to the host store over the Host's plugin routes only; no extra listener, no outbound network
|
|
46
46
|
|
|
47
47
|
## Development
|
|
48
48
|
|
|
49
49
|
```sh
|
|
50
50
|
pnpm install
|
|
51
|
-
pnpm verify # typecheck
|
|
51
|
+
pnpm verify # typecheck
|
|
52
52
|
pnpm build # tsc host + tsc client + tsdown browser bundle
|
|
53
|
-
node scripts/bootstrap.mjs # uv sync the package-local venv
|
|
54
53
|
```
|
|
55
54
|
|
|
56
55
|
## Known limitations
|
package/README_zh.md
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@nelsonlongxiang/dsh-prompt-templates)
|
|
6
6
|
|
|
7
|
-
[DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness)
|
|
7
|
+
[DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) 快捷提示词模板:全局与会话级模板、右侧浏览器面板、纯 TS 的 `node:sqlite` 持久化——一个自包含插件,`dsh plugin` 即装。
|
|
8
|
+
|
|
9
|
+

|
|
8
10
|
|
|
9
11
|
## 安装
|
|
10
12
|
|
|
@@ -14,7 +16,7 @@ dsh plugin --profile web add @nelsonlongxiang/dsh-prompt-templates
|
|
|
14
16
|
|
|
15
17
|
重启 `dsh web`,在输入框工具行找到模板按钮。
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
无需 Python 工具链——0.3.0 起 host 面直接通过 `node:sqlite`(Node ≥ 24)在宿主进程内持久化,读写原来 Python 子进程持有的同一个 `$DSH_HOME/ext/prompt-templates/db.sqlite3`(数据零迁移)。
|
|
18
20
|
|
|
19
21
|
## 你能得到什么
|
|
20
22
|
|
|
@@ -22,35 +24,32 @@ dsh plugin --profile web add @nelsonlongxiang/dsh-prompt-templates
|
|
|
22
24
|
- **插入或直发**——一键追加进草稿;send-now 立即发送;编辑、删除、转全局都在行内
|
|
23
25
|
- **搜索与滚动**——边输入边过滤,长列表滚动可靠
|
|
24
26
|
- **拖拽定位、双击复位**——面板记住你放的位置
|
|
25
|
-
-
|
|
27
|
+
- **设计即持久**——模板经纯 TS 存储(`node:sqlite`)落 SQLite,由 host 插件持有并随其生命周期关闭
|
|
26
28
|
|
|
27
29
|
## 工作原理
|
|
28
30
|
|
|
29
31
|
```text
|
|
30
32
|
src/ TypeScript 插件(host 面 + 浏览器面)
|
|
31
|
-
index.ts Host
|
|
33
|
+
index.ts Host:持有 TS 存储,暴露 HTTP 路由
|
|
34
|
+
store.ts 纯 TS 模板存储(node:sqlite)
|
|
32
35
|
client/ 浏览器:面板 UI 注册进 shell.overlay 与
|
|
33
36
|
conversation.input.right
|
|
34
|
-
python/ Python 后端包(仅模板领域)
|
|
35
|
-
src/dsh_prompt_templates/
|
|
36
|
-
pyproject.toml models、store、JSON-RPC server、CLI
|
|
37
37
|
cordis.patch.yml Bundle patch:挂插件行
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
存储是 host 面内的纯 TS 实现;不拉起任何子进程。
|
|
41
41
|
|
|
42
42
|
## 安全
|
|
43
43
|
|
|
44
44
|
- 模板内容只有经用户主动插入才作为普通用户文本进入模型请求——绝不自动注入任何提示词
|
|
45
|
-
- 面板仅经 Host 的插件路由访问
|
|
45
|
+
- 面板仅经 Host 的插件路由访问 host 存储;无额外监听、无外联网络
|
|
46
46
|
|
|
47
47
|
## 开发
|
|
48
48
|
|
|
49
49
|
```sh
|
|
50
50
|
pnpm install
|
|
51
|
-
pnpm verify # typecheck
|
|
51
|
+
pnpm verify # typecheck
|
|
52
52
|
pnpm build # tsc host + tsc client + tsdown 浏览器 bundle
|
|
53
|
-
node scripts/bootstrap.mjs # uv sync 包内 venv
|
|
54
53
|
```
|
|
55
54
|
|
|
56
55
|
## 已知限制
|
package/cordis.patch.yml
CHANGED
|
@@ -8,29 +8,16 @@
|
|
|
8
8
|
# profile and reconciles it into the profile's `dsh.profile.bundles` layer
|
|
9
9
|
# list.
|
|
10
10
|
#
|
|
11
|
-
# The
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# `process` and `baseUrl` (the profile directory, whose node_modules holds
|
|
16
|
-
# the installed package) — `require`/`__dirname` are not available there.
|
|
11
|
+
# The host half is pure TS: templates persist through `node:sqlite`
|
|
12
|
+
# directly inside the host process (no Python child, no pythonBridge
|
|
13
|
+
# service dependency). The patch JS sandbox exposes `process`; the default
|
|
14
|
+
# database path already resolves under $DSH_HOME, so only overrides need it.
|
|
17
15
|
- insert:
|
|
18
|
-
# Python backend child. The shared `python-bridge` service is provided by
|
|
19
|
-
# the host profile (typically via @nelsonlongxiang/dsh-native-teams or by
|
|
20
|
-
# explicitly bundling @nelsonlongxiang/dsh-python-bridge); this patch no
|
|
21
|
-
# longer declares it to avoid duplicate loader entry ids when multiple
|
|
22
|
-
# Python-backed plugins are composed.
|
|
23
16
|
- id: prompt-templates
|
|
24
17
|
# Node-resolvable package name — must stay in sync with package.json
|
|
25
18
|
# `name`. Quoted because `@` is a reserved YAML indicator.
|
|
26
19
|
name: '@nelsonlongxiang/dsh-prompt-templates'
|
|
27
20
|
config:
|
|
28
|
-
# The Python backend child: uv-managed package venv running the
|
|
29
|
-
# extracted template backend's serve command.
|
|
30
|
-
command: !!js >
|
|
31
|
-
process.platform === 'win32'
|
|
32
|
-
? ['uv', 'run', '--project', process.getBuiltinModule('node:url').fileURLToPath(new URL('node_modules/@nelsonlongxiang/dsh-prompt-templates/python', baseUrl)), 'python', '-m', 'dsh_prompt_templates.cli', 'serve']
|
|
33
|
-
: ['uv', 'run', '--project', process.getBuiltinModule('node:url').fileURLToPath(new URL('node_modules/@nelsonlongxiang/dsh-prompt-templates/python', baseUrl)), 'python', '-m', 'dsh_prompt_templates.cli', 'serve']
|
|
34
21
|
# Explicit SQLite database path so persistence lands under
|
|
35
22
|
# $DSH_HOME/ext/prompt-templates/ regardless of cwd or environment drift.
|
|
36
23
|
dbPath: !!js >
|
|
Binary file
|
package/lib/client/Panel.js
CHANGED
|
@@ -7,16 +7,17 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
7
7
|
* visibility; the current session arrives through the standard kit.
|
|
8
8
|
*/
|
|
9
9
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
10
|
-
import { IconEditOutline16, IconGlobeOutline14, IconPlusOutline16, IconSendOutline16, IconTrashOutline16 } from '@deepseek-ai/dsh-client-ui-primitives';
|
|
10
|
+
import { IconEditOutline16, IconGlobeOutline14, IconPlusOutline16, IconQueueOutline14, IconSendOutline16, IconTrashOutline16 } from '@deepseek-ai/dsh-client-ui-primitives';
|
|
11
11
|
import css from './Panel.module.css';
|
|
12
|
-
function TemplateRow({ template, onInsert, onSend, onEdit, onDelete, onMakeGlobal, t }) {
|
|
13
|
-
return (_jsxs("div", { className: css.row, "data-prompt-template": true, children: [_jsxs("button", { type: "button", className: css.insert, onClick: () => { onInsert(template.content); }, title: t('panel.insert'), children: [_jsx("span", { className: css.name, children: template.name }), _jsx("span", { className: css.preview, children: template.content })] }), _jsx("button", { type: "button", className: css.sendBtn, onClick: () => { onSend(template.content); }, "aria-label": t('panel.sendNow'), title: t('panel.sendNow'), children: _jsx(IconSendOutline16, { size: 12 }) }), onMakeGlobal !== undefined && (_jsx("button", { type: "button", className: css.globalBtn, onClick: () => { onMakeGlobal(template.id); }, "aria-label": t('panel.makeGlobal'), title: t('panel.makeGlobal'), children: _jsx(IconGlobeOutline14, { size: 12 }) })), _jsx("button", { type: "button", className: css.editBtn, onClick: () => { onEdit(template.id); }, "aria-label": t('panel.edit'), title: t('panel.edit'), children: _jsx(IconEditOutline16, { size: 12 }) }), _jsx("button", { type: "button", className: css.deleteBtn, onClick: () => { onDelete(template.id); }, "aria-label": t('panel.delete'), children: _jsx(IconTrashOutline16, { size: 12 }) })] }));
|
|
12
|
+
function TemplateRow({ template, onInsert, onSend, onInterject, onEdit, onDelete, onMakeGlobal, onDragStart, onDropOn, t }) {
|
|
13
|
+
return (_jsxs("div", { className: css.row, "data-prompt-template": true, onMouseDown: (e) => { e.preventDefault(); }, draggable: true, onDragStart: () => { onDragStart(template.id); }, onDragOver: (e) => { e.preventDefault(); }, onDrop: (e) => { e.preventDefault(); e.stopPropagation(); onDropOn(template.id); }, children: [_jsxs("button", { type: "button", className: css.insert, onClick: () => { onInsert(template.content); }, title: t('panel.insert'), children: [_jsx("span", { className: css.name, children: template.name }), _jsx("span", { className: css.preview, children: template.content })] }), _jsx("button", { type: "button", className: css.interjectBtn, onClick: () => { onInterject(template.content); }, "aria-label": t('panel.interject'), title: t('panel.interject'), children: _jsx(IconQueueOutline14, { size: 12 }) }), _jsx("button", { type: "button", className: css.sendBtn, onClick: () => { onSend(template.content); }, "aria-label": t('panel.sendNow'), title: t('panel.sendNow'), children: _jsx(IconSendOutline16, { size: 12 }) }), onMakeGlobal !== undefined && (_jsx("button", { type: "button", className: css.globalBtn, onClick: () => { onMakeGlobal(template.id); }, "aria-label": t('panel.makeGlobal'), title: t('panel.makeGlobal'), children: _jsx(IconGlobeOutline14, { size: 12 }) })), _jsx("button", { type: "button", className: css.editBtn, onClick: () => { onEdit(template.id); }, "aria-label": t('panel.edit'), title: t('panel.edit'), children: _jsx(IconEditOutline16, { size: 12 }) }), _jsx("button", { type: "button", className: css.deleteBtn, onClick: () => { onDelete(template.id); }, "aria-label": t('panel.delete'), children: _jsx(IconTrashOutline16, { size: 12 }) })] }));
|
|
14
14
|
}
|
|
15
|
-
/** The template inline form: creation (with scope
|
|
16
|
-
function TemplateForm({ initial, allowScope, sessionId, t, submitLabel, onSubmit, onDone }) {
|
|
15
|
+
/** The template inline form: creation (with scope + category pickers) and in-place edit. */
|
|
16
|
+
function TemplateForm({ initial, allowScope, sessionId, categories, t, submitLabel, onSubmit, onDone, onCancel }) {
|
|
17
17
|
const [name, setName] = useState(initial?.name ?? '');
|
|
18
18
|
const [content, setContent] = useState(initial?.content ?? '');
|
|
19
19
|
const [scope, setScope] = useState('global');
|
|
20
|
+
const [category, setCategory] = useState(null);
|
|
20
21
|
const [busy, setBusy] = useState(false);
|
|
21
22
|
const [error, setError] = useState(null);
|
|
22
23
|
const submit = useCallback(async () => {
|
|
@@ -30,34 +31,64 @@ function TemplateForm({ initial, allowScope, sessionId, t, submitLabel, onSubmit
|
|
|
30
31
|
}
|
|
31
32
|
setBusy(true);
|
|
32
33
|
setError(null);
|
|
33
|
-
const ok = await onSubmit(trimmedName, trimmedContent, scope);
|
|
34
|
+
const ok = await onSubmit(trimmedName, trimmedContent, scope, category);
|
|
34
35
|
setBusy(false);
|
|
35
36
|
if (!ok) {
|
|
36
37
|
setError(t('panel.error'));
|
|
37
38
|
return;
|
|
38
39
|
}
|
|
39
40
|
onDone();
|
|
40
|
-
}, [name, content, scope, allowScope, sessionId, onSubmit, t, onDone]);
|
|
41
|
-
|
|
41
|
+
}, [name, content, scope, category, allowScope, sessionId, onSubmit, t, onDone]);
|
|
42
|
+
// Categories selectable for the CURRENT scope pick (matching partition).
|
|
43
|
+
const scopeCategories = categories.filter((cat) => cat.scope === scope && (cat.scope === 'global' || cat.session_id === sessionId));
|
|
44
|
+
return (_jsxs("div", { className: css.addForm, "data-prompt-add": true, children: [_jsx("input", { className: css.nameInput, type: "text", placeholder: t('panel.addName'), value: name, onChange: (e) => { setName(e.target.value); }, "aria-label": t('panel.addName') }), _jsx("textarea", { className: css.contentInput, placeholder: t('panel.addContent'), value: content, onChange: (e) => { setContent(e.target.value); }, rows: 3, "aria-label": t('panel.addContent') }), _jsxs("div", { className: css.addRow, children: [allowScope && (_jsx("label", { className: css.scopeLabel, children: _jsxs("select", { value: scope, onChange: (e) => { setScope(e.target.value); setCategory(null); }, "aria-label": "scope", children: [_jsx("option", { value: "global", children: t('panel.global') }), _jsx("option", { value: "session", disabled: sessionId === null, children: t('panel.session') })] }) })), _jsx("label", { className: css.scopeLabel, children: _jsxs("select", { value: category ?? '', onChange: (e) => { setCategory(e.target.value === '' ? null : e.target.value); }, "aria-label": t('panel.category'), children: [_jsx("option", { value: "", children: t('panel.categoryNone') }), scopeCategories.map((cat) => _jsx("option", { value: cat.name, children: cat.name }, `${cat.scope}:${cat.name}`))] }) }), _jsxs("div", { className: css.addActions, children: [error !== null && _jsx("span", { className: css.error, role: "alert", children: error }), onCancel !== undefined && (_jsx("button", { type: "button", className: css.cancelBtn, onClick: onCancel, disabled: busy, children: t('panel.addCancel') })), _jsx("button", { type: "button", className: css.saveBtn, onClick: () => void submit(), disabled: busy || name.trim() === '' || content.trim() === '', children: submitLabel })] })] })] }));
|
|
45
|
+
}
|
|
46
|
+
/** Panel width used for viewport clamping (mirrors the CSS `.panel` width). */
|
|
47
|
+
const PANEL_WIDTH = 320;
|
|
48
|
+
/**
|
|
49
|
+
* Clamp a restored panel position into the current viewport so a placement
|
|
50
|
+
* saved on a wider screen (or a monitor that is no longer attached) can never
|
|
51
|
+
* render the panel unreachable off-screen. Null passes through untouched.
|
|
52
|
+
* @param position - the persisted placement, if any.
|
|
53
|
+
* @returns the clamped placement.
|
|
54
|
+
*/
|
|
55
|
+
function clampToViewport(position) {
|
|
56
|
+
if (position === null)
|
|
57
|
+
return null;
|
|
58
|
+
const maxX = Math.max(0, window.innerWidth - PANEL_WIDTH);
|
|
59
|
+
const maxY = Math.max(0, window.innerHeight - 40);
|
|
60
|
+
return {
|
|
61
|
+
x: Math.min(Math.max(position.x, 0), maxX),
|
|
62
|
+
y: Math.min(Math.max(position.y, 0), maxY),
|
|
63
|
+
};
|
|
42
64
|
}
|
|
43
65
|
/**
|
|
44
66
|
* The prompt-templates panel body.
|
|
45
67
|
* @param props - runtime kit, injected face members, store handle, and locale.
|
|
46
68
|
*/
|
|
47
69
|
export function PromptPanel(props) {
|
|
48
|
-
const { useStore, useSessions, refresh, create, update, remove, makeGlobal, insert, send, panelPosition, savePanelPosition, resetPanelPosition, actions, t, } = props;
|
|
70
|
+
const { useStore, useSessions, refresh, refreshCategories, createCategory, removeCategory, create, update, remove, makeGlobal, insert, send, interject, panelPosition, savePanelPosition, resetPanelPosition, actions, t, } = props;
|
|
49
71
|
const open = useStore(s => s.open);
|
|
50
72
|
const currentSession = useSessions(s => s.current);
|
|
51
73
|
const [templates, setTemplates] = useState([]);
|
|
74
|
+
const [categories, setCategories] = useState([]);
|
|
52
75
|
const [showAdd, setShowAdd] = useState(false);
|
|
53
76
|
// Live search filter over name + content, case-insensitive.
|
|
54
77
|
const [query, setQuery] = useState('');
|
|
55
78
|
// In-place editing: the id whose row swaps to the edit form, null = none.
|
|
56
79
|
const [editingId, setEditingId] = useState(null);
|
|
80
|
+
// Active tab: 'global' / 'session' (the implicit default tabs) or
|
|
81
|
+
// `cat:<scope>:<name>` for one user-created category tab.
|
|
82
|
+
const [activeTab, setActiveTab] = useState('global');
|
|
83
|
+
const [showCatForm, setShowCatForm] = useState(false);
|
|
84
|
+
// Row drag-sort: the id currently being dragged (HTML5 DnD).
|
|
85
|
+
const dragRowRef = useRef(null);
|
|
57
86
|
// Drag placement: seeded from the persisted position, live-updated while
|
|
58
87
|
// dragging, committed to the settings document on release. A null with no
|
|
59
|
-
// drag in flight keeps the CSS right-anchored default.
|
|
60
|
-
|
|
88
|
+
// drag in flight keeps the CSS right-anchored default. The restored value
|
|
89
|
+
// is clamped into the current viewport: a position saved on a wider screen
|
|
90
|
+
// or a second monitor would otherwise render the panel off-screen forever.
|
|
91
|
+
const persisted = clampToViewport(panelPosition());
|
|
61
92
|
const [dragPos, setDragPos] = useState(null);
|
|
62
93
|
const dragRef = useRef(null);
|
|
63
94
|
const panelRef = useRef(null);
|
|
@@ -66,10 +97,15 @@ export function PromptPanel(props) {
|
|
|
66
97
|
const items = await refresh();
|
|
67
98
|
setTemplates(items);
|
|
68
99
|
}, [refresh]);
|
|
100
|
+
const loadCategories = useCallback(async () => {
|
|
101
|
+
setCategories(await refreshCategories());
|
|
102
|
+
}, [refreshCategories]);
|
|
69
103
|
useEffect(() => {
|
|
70
|
-
if (open)
|
|
104
|
+
if (open) {
|
|
71
105
|
void load();
|
|
72
|
-
|
|
106
|
+
void loadCategories();
|
|
107
|
+
}
|
|
108
|
+
}, [open, load, loadCategories]);
|
|
73
109
|
const handleInsert = useCallback((content) => {
|
|
74
110
|
if (sessionId === null)
|
|
75
111
|
return;
|
|
@@ -80,6 +116,11 @@ export function PromptPanel(props) {
|
|
|
80
116
|
return;
|
|
81
117
|
send(sessionId, content);
|
|
82
118
|
}, [send, sessionId]);
|
|
119
|
+
const handleInterject = useCallback((content) => {
|
|
120
|
+
if (sessionId === null)
|
|
121
|
+
return;
|
|
122
|
+
interject(sessionId, content);
|
|
123
|
+
}, [interject, sessionId]);
|
|
83
124
|
const handleRemove = useCallback(async (id) => {
|
|
84
125
|
const result = await remove(id);
|
|
85
126
|
if (result.ok)
|
|
@@ -137,30 +178,120 @@ export function PromptPanel(props) {
|
|
|
137
178
|
};
|
|
138
179
|
if (!open)
|
|
139
180
|
return null;
|
|
140
|
-
// The full set is loaded;
|
|
141
|
-
//
|
|
142
|
-
// query narrows both groups by name + content, case-insensitive.
|
|
181
|
+
// The full set is loaded; filtering happens here. The search query
|
|
182
|
+
// narrows by name + content, case-insensitive.
|
|
143
183
|
const needle = query.trim().toLowerCase();
|
|
144
184
|
const matches = (row) => needle === ''
|
|
145
185
|
|| row.name.toLowerCase().includes(needle)
|
|
146
186
|
|| row.content.toLowerCase().includes(needle);
|
|
147
|
-
|
|
187
|
+
// Visible user-category tabs: global ones always, session ones only for
|
|
188
|
+
// the current session.
|
|
189
|
+
const visibleCategories = categories.filter((cat) => cat.scope === 'global' || (cat.scope === 'session' && cat.session_id === sessionId));
|
|
190
|
+
const activeCategory = activeTab.startsWith('cat:')
|
|
191
|
+
? visibleCategories.find((cat) => `cat:${cat.scope}:${cat.name}` === activeTab)
|
|
192
|
+
: undefined;
|
|
193
|
+
// A deleted (or no-longer-visible) category tab falls back to 全局.
|
|
194
|
+
const effectiveTab = activeTab.startsWith('cat:') && activeCategory === undefined ? 'global' : activeTab;
|
|
195
|
+
// The active tab decides the visible partition; rows sort by position.
|
|
196
|
+
const tabRows = templates.filter((row) => {
|
|
197
|
+
if (!matches(row))
|
|
198
|
+
return false;
|
|
199
|
+
if (effectiveTab === 'global')
|
|
200
|
+
return row.scope === 'global' && row.category === null;
|
|
201
|
+
if (effectiveTab === 'session')
|
|
202
|
+
return row.scope === 'session' && row.session_id === sessionId && row.category === null;
|
|
203
|
+
return activeCategory !== undefined
|
|
204
|
+
&& row.category === activeCategory.name
|
|
205
|
+
&& row.scope === activeCategory.scope
|
|
206
|
+
&& (activeCategory.scope === 'global' || row.session_id === sessionId);
|
|
207
|
+
});
|
|
208
|
+
// Drop one dragged row onto another: reorder within the tab by writing the
|
|
209
|
+
// full position sequence back (idempotent and partition-local).
|
|
210
|
+
const handleRowDrop = (targetId) => {
|
|
211
|
+
const draggedId = dragRowRef.current;
|
|
212
|
+
dragRowRef.current = null;
|
|
213
|
+
if (draggedId === null || draggedId === targetId)
|
|
214
|
+
return;
|
|
215
|
+
const ids = tabRows.map((row) => row.id);
|
|
216
|
+
const from = ids.indexOf(draggedId);
|
|
217
|
+
const to = ids.indexOf(targetId);
|
|
218
|
+
if (from === -1 || to === -1)
|
|
219
|
+
return;
|
|
220
|
+
ids.splice(to, 0, ...ids.splice(from, 1));
|
|
221
|
+
void (async () => {
|
|
222
|
+
await Promise.all(ids.map((id, index) => update(id, { position: index })));
|
|
223
|
+
void load();
|
|
224
|
+
})();
|
|
225
|
+
};
|
|
148
226
|
/** One list row: the edit form while being edited, the rendered row otherwise. */
|
|
149
227
|
const renderTemplateRow = (row) => (editingId === row.id
|
|
150
|
-
? (_jsx(TemplateForm, { initial: { name: row.name, content: row.content }, allowScope: false, sessionId: sessionId, t: t, submitLabel: t('panel.addSave'), onSubmit: async (name, content) => (await update(row.id, { name, content })).ok, onDone: () => { setEditingId(null); void load(); } }, row.id))
|
|
151
|
-
: (_jsx(TemplateRow, { template: row, onInsert: (c) => { handleInsert(c); }, onSend: (c) => { handleSend(c); }, onEdit: (id) => { setEditingId(id); }, onDelete: (id) => { void handleRemove(id); }, onMakeGlobal: row.scope === 'session' ? (id) => { void handleMakeGlobal(id); } : undefined, t: t }, row.id)));
|
|
152
|
-
const sessionRows = templates.filter(row => row.scope === 'session' && row.session_id === sessionId && matches(row));
|
|
228
|
+
? (_jsx(TemplateForm, { initial: { name: row.name, content: row.content }, allowScope: false, sessionId: sessionId, categories: visibleCategories, t: t, submitLabel: t('panel.addSave'), onSubmit: async (name, content, _scope, category) => (await update(row.id, { name, content, category: row.scope === 'global' ? category : category })).ok, onDone: () => { setEditingId(null); void load(); }, onCancel: () => { setEditingId(null); } }, row.id))
|
|
229
|
+
: (_jsx(TemplateRow, { template: row, onInsert: (c) => { handleInsert(c); }, onSend: (c) => { handleSend(c); }, onInterject: (c) => { handleInterject(c); }, onEdit: (id) => { setEditingId(id); }, onDelete: (id) => { void handleRemove(id); }, onMakeGlobal: row.scope === 'session' ? (id) => { void handleMakeGlobal(id); } : undefined, onDragStart: (id) => { dragRowRef.current = id; }, onDropOn: (id) => { handleRowDrop(id); }, t: t }, row.id)));
|
|
153
230
|
const placement = dragPos ?? persisted;
|
|
231
|
+
// With bottom:auto the fixed panel's height is content-driven, so a long
|
|
232
|
+
// list would stretch past the viewport with no internal scroll. Cap the
|
|
233
|
+
// height at (viewport - top - margin); the flex column + body's
|
|
234
|
+
// min-height/overflow then keeps the list scrolling inside the panel.
|
|
154
235
|
const placementStyle = placement === null
|
|
155
236
|
? undefined
|
|
156
|
-
: {
|
|
157
|
-
|
|
237
|
+
: {
|
|
238
|
+
left: placement.x,
|
|
239
|
+
top: placement.y,
|
|
240
|
+
right: 'auto',
|
|
241
|
+
bottom: 'auto',
|
|
242
|
+
maxHeight: `calc(100vh - ${placement.y}px - 16px)`,
|
|
243
|
+
};
|
|
244
|
+
return (_jsxs("div", { className: css.panel, "data-prompt-panel": true, ref: panelRef, style: placementStyle, children: [_jsxs("div", { className: css.header, title: t('panel.dragHint'), onPointerDown: onHeaderPointerDown, onPointerMove: onHeaderPointerMove, onPointerUp: onHeaderPointerUp, onDoubleClick: onHeaderDoubleClick, children: [_jsx("span", { className: css.title, children: t('panel.title') }), _jsx("button", { type: "button", className: css.closeBtn, onClick: () => { actions.close(); }, "aria-label": t('panel.close'), children: "\u00D7" })] }), _jsx("div", { className: css.searchBar, children: _jsx("input", { type: "search", className: css.searchInput, value: query, onChange: (e) => { setQuery(e.target.value); }, placeholder: t('panel.search'), "aria-label": t('panel.search') }) }), _jsxs("div", { className: css.tabsBar, children: [_jsx("button", { type: "button", className: effectiveTab === 'global' ? `${css.tab} ${css.tabActive}` : css.tab, onClick: () => { setActiveTab('global'); }, children: t('panel.global') }), _jsx("button", { type: "button", className: effectiveTab === 'session' ? `${css.tab} ${css.tabActive}` : css.tab, onClick: () => { setActiveTab('session'); }, disabled: sessionId === null, children: t('panel.session') }), visibleCategories.map((cat) => {
|
|
245
|
+
const key = `cat:${cat.scope}:${cat.name}`;
|
|
246
|
+
return (_jsxs("span", { className: effectiveTab === key ? `${css.tab} ${css.tabActive}` : css.tab, children: [_jsx("button", { type: "button", onClick: () => { setActiveTab(key); }, children: cat.name }), _jsx("button", { type: "button", className: css.tabDelete, "aria-label": t('panel.deleteCategory'), title: t('panel.deleteCategory'), onClick: () => {
|
|
247
|
+
void (async () => {
|
|
248
|
+
const result = await removeCategory(cat.name, cat.scope, cat.session_id);
|
|
249
|
+
if (result.ok) {
|
|
250
|
+
if (effectiveTab === key)
|
|
251
|
+
setActiveTab('global');
|
|
252
|
+
void loadCategories();
|
|
253
|
+
void load();
|
|
254
|
+
}
|
|
255
|
+
})();
|
|
256
|
+
}, children: "\u00D7" })] }, key));
|
|
257
|
+
}), _jsx("button", { type: "button", className: css.tabAdd, "aria-label": t('panel.newCategory'), title: t('panel.newCategory'), onClick: () => { setShowCatForm(v => !v); }, children: _jsx(IconPlusOutline16, { size: 10 }) })] }), showCatForm && (_jsx(CategoryForm, { sessionId: sessionId, createCategory: createCategory, t: t, onDone: () => { setShowCatForm(false); void loadCategories(); } })), _jsxs("div", { className: css.body, children: [tabRows.length === 0 && !showAdd && !showCatForm && (_jsx("div", { className: css.empty, children: needle !== '' ? t('panel.noMatch') : t('panel.empty') })), tabRows.length > 0 && (_jsx("div", { className: css.group, onDragOver: (e) => { e.preventDefault(); }, children: tabRows.map(row => renderTemplateRow(row)) })), showAdd && (_jsx(TemplateForm, { sessionId: sessionId, categories: visibleCategories, t: t, allowScope: true, submitLabel: t('panel.addSave'), onSubmit: async (name, content, scope, category) => {
|
|
158
258
|
const result = await create({
|
|
159
259
|
name,
|
|
160
260
|
content,
|
|
161
261
|
scope,
|
|
162
262
|
session_id: scope === 'session' ? sessionId : null,
|
|
263
|
+
category,
|
|
163
264
|
});
|
|
164
265
|
return result.ok;
|
|
165
|
-
}, onDone: () => { setShowAdd(false); void load(); } }))] }), _jsx("div", { className: css.footer, children: _jsxs("button", { type: "button", className: css.addBtn, onClick: () => { setShowAdd(v => !v); }, children: [_jsx(IconPlusOutline16, { size: 12 }), " ", t('panel.add')] }) })] }));
|
|
266
|
+
}, onDone: () => { setShowAdd(false); void load(); }, onCancel: () => { setShowAdd(false); } }))] }), _jsx("div", { className: css.footer, children: _jsxs("button", { type: "button", className: css.addBtn, onClick: () => { setShowAdd(v => !v); }, children: [_jsx(IconPlusOutline16, { size: 12 }), " ", t('panel.add')] }) })] }));
|
|
267
|
+
}
|
|
268
|
+
/** Inline form creating one category tab: name + scope (global, or the current session). */
|
|
269
|
+
function CategoryForm({ sessionId, createCategory, t, onDone }) {
|
|
270
|
+
const [name, setName] = useState('');
|
|
271
|
+
const [scope, setScope] = useState('global');
|
|
272
|
+
const [busy, setBusy] = useState(false);
|
|
273
|
+
const [error, setError] = useState(null);
|
|
274
|
+
const submit = async () => {
|
|
275
|
+
const trimmed = name.trim();
|
|
276
|
+
if (trimmed === '')
|
|
277
|
+
return;
|
|
278
|
+
if (scope === 'session' && sessionId === null) {
|
|
279
|
+
setError(t('panel.error'));
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
setBusy(true);
|
|
283
|
+
setError(null);
|
|
284
|
+
const result = await createCategory({
|
|
285
|
+
name: trimmed,
|
|
286
|
+
scope,
|
|
287
|
+
session_id: scope === 'session' ? sessionId : null,
|
|
288
|
+
});
|
|
289
|
+
setBusy(false);
|
|
290
|
+
if (!result.ok) {
|
|
291
|
+
setError(t('panel.error'));
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
onDone();
|
|
295
|
+
};
|
|
296
|
+
return (_jsxs("div", { className: css.addForm, "data-prompt-category-add": true, children: [_jsx("input", { className: css.nameInput, type: "text", placeholder: t('panel.categoryName'), value: name, onChange: (e) => { setName(e.target.value); }, "aria-label": t('panel.categoryName') }), _jsxs("div", { className: css.addRow, children: [_jsx("label", { className: css.scopeLabel, children: _jsxs("select", { value: scope, onChange: (e) => { setScope(e.target.value); }, "aria-label": "scope", children: [_jsx("option", { value: "global", children: t('panel.global') }), _jsx("option", { value: "session", disabled: sessionId === null, children: t('panel.session') })] }) }), _jsxs("div", { className: css.addActions, children: [error !== null && _jsx("span", { className: css.error, role: "alert", children: error }), _jsx("button", { type: "button", className: css.saveBtn, onClick: () => void submit(), disabled: busy || name.trim() === '', children: t('panel.addSave') })] })] })] }));
|
|
166
297
|
}
|
package/lib/client/api.js
CHANGED
|
@@ -13,6 +13,25 @@ export async function listTemplates() {
|
|
|
13
13
|
const body = await response.json();
|
|
14
14
|
return [...body.items ?? []];
|
|
15
15
|
}
|
|
16
|
+
/** Load every user category tab (global + all sessions'; the panel filters). */
|
|
17
|
+
export async function listCategories() {
|
|
18
|
+
const response = await fetch(`${BASE}/categories`, { cache: 'no-store' });
|
|
19
|
+
if (!response.ok)
|
|
20
|
+
return [];
|
|
21
|
+
const body = await response.json();
|
|
22
|
+
return [...body.items ?? []];
|
|
23
|
+
}
|
|
24
|
+
/** Create one category tab. */
|
|
25
|
+
export async function createCategory(request) {
|
|
26
|
+
return mutate('POST', '/categories', request);
|
|
27
|
+
}
|
|
28
|
+
/** Delete one category tab (its templates fall back to the default tab). */
|
|
29
|
+
export async function deleteCategory(name, scope, sessionId) {
|
|
30
|
+
const params = new URLSearchParams({ scope });
|
|
31
|
+
if (sessionId !== null)
|
|
32
|
+
params.set('session_id', sessionId);
|
|
33
|
+
return mutate('DELETE', `/categories/${encodeURIComponent(name)}?${params.toString()}`);
|
|
34
|
+
}
|
|
16
35
|
/** Create one template. */
|
|
17
36
|
export async function createTemplate(request) {
|
|
18
37
|
return mutate('POST', '/templates', request);
|
package/lib/client/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { listTemplates, createTemplate, updateTemplate, deleteTemplate, makeTemplateGlobal, } from "./api.js";
|
|
1
|
+
import { listTemplates, listCategories, createCategory, deleteCategory, createTemplate, updateTemplate, deleteTemplate, makeTemplateGlobal, } from "./api.js";
|
|
2
2
|
import { PromptPanel } from "./Panel.js";
|
|
3
3
|
import { PanelButton } from "./PanelButton.js";
|
|
4
4
|
import { createPromptPanelStore } from "./store.js";
|
|
@@ -29,6 +29,13 @@ export function apply(ctx) {
|
|
|
29
29
|
// the global rows (their session_id is NULL and never matches).
|
|
30
30
|
const panelFace = {
|
|
31
31
|
refresh: listTemplates,
|
|
32
|
+
refreshCategories: listCategories,
|
|
33
|
+
createCategory: (request) => {
|
|
34
|
+
return createCategory(request);
|
|
35
|
+
},
|
|
36
|
+
removeCategory: (name, scope, sessionId) => {
|
|
37
|
+
return deleteCategory(name, scope, sessionId);
|
|
38
|
+
},
|
|
32
39
|
create: (request) => {
|
|
33
40
|
return createTemplate(request);
|
|
34
41
|
},
|
|
@@ -47,6 +54,9 @@ export function apply(ctx) {
|
|
|
47
54
|
send: (sessionId, content) => {
|
|
48
55
|
controller.send(sessionId, content);
|
|
49
56
|
},
|
|
57
|
+
interject: (sessionId, content) => {
|
|
58
|
+
controller.interject(sessionId, content);
|
|
59
|
+
},
|
|
50
60
|
panelPosition: () => {
|
|
51
61
|
const snapshot = uiSettings.getSnapshot();
|
|
52
62
|
if (snapshot.status !== 'ready')
|
package/lib/client/locales.js
CHANGED
|
@@ -17,6 +17,12 @@ export const zh = {
|
|
|
17
17
|
'panel.addSave': '保存',
|
|
18
18
|
'panel.addCancel': '取消',
|
|
19
19
|
'panel.insert': '插入',
|
|
20
|
+
'panel.interject': '插话打断',
|
|
21
|
+
'panel.newCategory': '新增分类',
|
|
22
|
+
'panel.categoryName': '分类名称',
|
|
23
|
+
'panel.categoryNone': '不设分类',
|
|
24
|
+
'panel.category': '分类',
|
|
25
|
+
'panel.deleteCategory': '删除分类',
|
|
20
26
|
'panel.delete': '删除',
|
|
21
27
|
'panel.edit': '编辑',
|
|
22
28
|
'panel.makeGlobal': '设为全局',
|
|
@@ -42,6 +48,12 @@ export const en = {
|
|
|
42
48
|
'panel.addSave': 'Save',
|
|
43
49
|
'panel.addCancel': 'Cancel',
|
|
44
50
|
'panel.insert': 'Insert',
|
|
51
|
+
'panel.interject': 'Interject (steer)',
|
|
52
|
+
'panel.newCategory': 'New category',
|
|
53
|
+
'panel.categoryName': 'Category name',
|
|
54
|
+
'panel.categoryNone': 'No category',
|
|
55
|
+
'panel.category': 'Category',
|
|
56
|
+
'panel.deleteCategory': 'Delete category',
|
|
45
57
|
'panel.delete': 'Delete',
|
|
46
58
|
'panel.edit': 'Edit',
|
|
47
59
|
'panel.makeGlobal': 'Make global',
|
package/lib/client/service.js
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locate the composer textarea for one draft. The input machine publishes
|
|
3
|
+
* no caret, so the caret can only be read from the DOM: of every textarea
|
|
4
|
+
* on the page, the one whose value equals the draft snapshot is the
|
|
5
|
+
* composer (the panel's own form fields never hold the draft). Falls back
|
|
6
|
+
* to null when no match exists (detached DOM, session switched mid-click).
|
|
7
|
+
*/
|
|
8
|
+
function composerTextarea(draft) {
|
|
9
|
+
if (typeof document === 'undefined')
|
|
10
|
+
return null;
|
|
11
|
+
for (const el of Array.from(document.querySelectorAll('textarea'))) {
|
|
12
|
+
if (el.value === draft)
|
|
13
|
+
return el;
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
1
17
|
/**
|
|
2
18
|
* Cross-entry panel face (ctx.promptPanel).
|
|
3
19
|
*/
|
|
@@ -45,8 +61,13 @@ export class PromptPanelController {
|
|
|
45
61
|
this.#actions.openOnce();
|
|
46
62
|
}
|
|
47
63
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
64
|
+
* Insert one template's content into a session's composer draft at the
|
|
65
|
+
* live caret position (selection, when a range is selected, is replaced).
|
|
66
|
+
* The caret lives only in the DOM textarea — the input machine publishes
|
|
67
|
+
* no caret — so the composer textarea is located by matching its value
|
|
68
|
+
* against the draft snapshot; when no textarea is found the content falls
|
|
69
|
+
* back to an end-append (empty draft takes it verbatim, non-empty joins
|
|
70
|
+
* with a newline).
|
|
50
71
|
* @param sessionId - target session.
|
|
51
72
|
* @param content - template content.
|
|
52
73
|
*/
|
|
@@ -55,7 +76,35 @@ export class PromptPanelController {
|
|
|
55
76
|
if (input === undefined)
|
|
56
77
|
return;
|
|
57
78
|
const draft = input.state.getSnapshot().draft;
|
|
58
|
-
|
|
79
|
+
const textarea = composerTextarea(draft);
|
|
80
|
+
if (textarea === null) {
|
|
81
|
+
input.setDraft(draft === '' ? content : `${draft}\n${content}`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const start = textarea.selectionStart ?? draft.length;
|
|
85
|
+
const end = textarea.selectionEnd ?? start;
|
|
86
|
+
const next = `${draft.slice(0, start)}${content}${draft.slice(end)}`;
|
|
87
|
+
const caret = start + content.length;
|
|
88
|
+
input.setDraft(next);
|
|
89
|
+
// Return focus and land the caret after the inserted text; the panel
|
|
90
|
+
// rows preventDefault on mousedown, so the textarea never lost focus.
|
|
91
|
+
textarea.focus();
|
|
92
|
+
textarea.setSelectionRange(caret, caret);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Interject one template: steer it into the running agent turn. With
|
|
96
|
+
* steer mode the content interrupts the current turn at its next step
|
|
97
|
+
* (the same delivery as Cmd/Ctrl+Enter插话发送), rather than queuing
|
|
98
|
+
* after it. The user's own draft is left untouched.
|
|
99
|
+
* @param sessionId - target session.
|
|
100
|
+
* @param content - template content.
|
|
101
|
+
*/
|
|
102
|
+
interject(sessionId, content) {
|
|
103
|
+
const input = this.#inputOf(sessionId);
|
|
104
|
+
if (input === undefined)
|
|
105
|
+
return;
|
|
106
|
+
input.setDraft(content);
|
|
107
|
+
input.submit('steer');
|
|
59
108
|
}
|
|
60
109
|
/**
|
|
61
110
|
* Fill the draft with one template's content and submit it immediately —
|