@kir_sleeping/dsh-relay 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 sleeping
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.md CHANGED
@@ -1,3 +1,151 @@
1
- # Temporary Holding Version
1
+ # Relay
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 把本机任何应用接到同一个 agent 上。
4
+
5
+ 应用不需要内置 AI、不需要自己管会话、不需要理解 agent 的内部结构——像调一个普通 HTTP API 一样把任务交出去,拿回结果。所有调用共享同一份上下文,所以 agent 在邮件应用里知道你在便签里写过什么。
6
+
7
+ ```
8
+ 应用 A ─┐
9
+ 应用 B ─┼─→ Relay ─→ DSH agent ─→ 同一个会话
10
+ 应用 C ─┘
11
+ ```
12
+
13
+ ---
14
+
15
+ ## 它解决什么
16
+
17
+ 现在的 AI 是每个应用各配一个,彼此失忆:你在每个里面都要重新解释一遍自己。Relay 把这件事反过来——**agent 在应用外面,应用来调它**。
18
+
19
+ 由此得到三件单应用做不到的事:
20
+
21
+ - **上下文统一**:谁说过的话都在一条线上,不用重复交代。
22
+ - **应用不需要内置 AI**:接进来只改一个请求,模型、工具、权限、压缩都不用自己实现。
23
+ - **应用可以把数据交出去**:不是让用户复制粘贴,而是把结构化数据连同任务一起发过去。
24
+
25
+ ---
26
+
27
+ ## 能力
28
+
29
+ | | |
30
+ |---|---|
31
+ | 入口 | `POST /x/relay/submit`,可配置前缀,挂在 DSH 现有端口上 |
32
+ | 交付 | 同步等 / 稍后取 / SSE 流式 |
33
+ | 鉴权 | 凭证 + 0–3 级授权,首次接入需用户在系统原生确认框里点「允许」 |
34
+ | 会话 | 默认 `main`,全部落在独立的 Relay 工作区里 |
35
+ | 区分发起方 | `trigger: user \| app`——用户点的,还是应用自己发的 |
36
+
37
+ ---
38
+
39
+ ## 安装
40
+
41
+ 当前以本地链接方式安装(开发形态):
42
+
43
+ ```bash
44
+ # 在 DSH 里
45
+ plugin_manager install_bundle /path/to/plugins/relay
46
+ ```
47
+
48
+ 或命令行:
49
+
50
+ ```bash
51
+ dsh plugin add link:/path/to/plugins/relay
52
+ ```
53
+
54
+ > 发布到 registry 之后:
55
+
56
+ ```bash
57
+ dsh plugin add @kir_sleeping/dsh-relay
58
+ ```
59
+
60
+ `link:` 会把绝对路径写进 profile,只适合本机开发。
61
+
62
+ 安装后插件自动加载,并在 `$DSH_HOME/relay/workspace` 下建一个名为「外部任务」的会话。
63
+
64
+ ---
65
+
66
+ ## 快速开始
67
+
68
+ ```bash
69
+ # 1. 申请凭证。这一步会在主机上弹系统确认框,用户点「允许」才继续
70
+ curl -s -X POST http://127.0.0.1:3080/x/relay/fingerprint \
71
+ -H 'Content-Type: application/json' -d '{"source":"my-app"}'
72
+ # → {"source":"my-app","fingerprint":"…","level":1}
73
+
74
+ # 2. 提交
75
+ curl -s -X POST http://127.0.0.1:3080/x/relay/submit \
76
+ -H 'Content-Type: application/json' \
77
+ -d '{"content":"请只回复 OK","source":"my-app","fingerprint":"…","trigger":"user","wait":true}'
78
+
79
+ # 3. 或者流式
80
+ curl -N -X POST http://127.0.0.1:3080/x/relay/submit \
81
+ -H 'Content-Type: application/json' \
82
+ -d '{"content":"介绍一下这个项目","source":"my-app","fingerprint":"…","trigger":"user","stream":true}'
83
+ ```
84
+
85
+ 完整的接入说明见 [开发者文档](../../relay-guide.md),可运行的示例见 [relay-demo.html](../../relay-demo.html)。
86
+
87
+ ---
88
+
89
+ ## 配置
90
+
91
+ 在 profile 的 `cordis.patch.yml` 里那一行插件的 `config` 中设置,全部可选:
92
+
93
+ | 键 | 默认 | 说明 |
94
+ |---|---|---|
95
+ | `pathPrefix` | `/x/relay` | 路由前缀 |
96
+ | `defaultSession` | `main` | 默认外部会话名 |
97
+ | `workspacePath` | `$DSH_HOME/relay/workspace` | 外部会话所属工作区目录 |
98
+ | `workspaceTitle` | `Relay` | 工作区显示名 |
99
+ | `queueLimitMs` | `600000` | 排队多久未开始就作废 |
100
+ | `maxWaitMs` | `3600000` | 单次等待上限 |
101
+ | `resultTtlMs` | `86400000` | 结果在内存里保留多久(过期后从会话日志重建) |
102
+ | `authTimeoutMs` | `60000` | 确认框等待上限 |
103
+
104
+ > `workspacePath` 改了会让所有既有会话变成"不在工作区里"(会话的目录在创建时定死),不要随便改。
105
+
106
+ ---
107
+
108
+ ## 安全模型
109
+
110
+ **应用不是可信的,用户才是。**
111
+
112
+ - 首次接入必须由用户在系统原生确认框里点「允许」,才拿到凭证。默认级别 1(每条消息都要确认)。
113
+ - 级别在「设置 → 应用权限」里随时可改、可撤销,不需要应用配合。撤销立即生效。
114
+ - **`trigger` 表示谁发起的,`approved` 表示这次调用被允许发生。两者独立。** 后台发起的消息永远不会被当成用户说的话,即使用户在确认框里点过允许——放行不等于署名。
115
+ - 外部入口只能触达 Relay 工作区里的会话,碰不到用户的私人会话。
116
+ - 服务只监听 `127.0.0.1`。不要用反向代理把它暴露出去。
117
+
118
+ ---
119
+
120
+ ## 状态
121
+
122
+ 已实现并实测:受理 / 三种交付 / 事件关联 / 结果重建 / 排队作废 / 凭证与分级 / 原生确认框 / 流式。
123
+
124
+ 已知限制:
125
+
126
+ - 没有桌面时无法弹确认框(远程访问、无人值守),需要确认的操作会返回 `ERR_AUTH_UNAVAILABLE`;级别 3 的已授权来源不受影响。
127
+ - 原生确认框的**右下角小窗**只在 Windows / WSL 上有;macOS 与 Linux 仍是居中对话框。
128
+ - 无幂等、无取消、无跨机器调用。
129
+ - `trigger` 是应用自报的,Relay 无法验证"真的是用户点的"——级别 3 下只能如实转述。
130
+
131
+ ---
132
+
133
+ ## 文档
134
+
135
+ | 文件 | 内容 |
136
+ |---|---|
137
+ | [relay-guide.md](../../relay-guide.md) | 接入方文档:接口、字段、错误码、示例 |
138
+ | [relay-demo.html](../../relay-demo.html) | 可运行的单文件示例 |
139
+ | [relay-reference.md](../../relay-reference.md) | 维护者参考:内部结构、不变式、扩展点 |
140
+ | [relay-plan.md](../../relay-plan.md) | 设计:契约与取舍 |
141
+ | [relay-auth-plan.md](../../relay-auth-plan.md) | 鉴权设计 |
142
+
143
+ ---
144
+
145
+ ## 文档位置
146
+
147
+ 本 README 随包分发;其余文档(接入指南、示例、设计稿)在仓库根目录,不在 npm 包里。
148
+
149
+ ## License
150
+
151
+ MIT。
package/client.js ADDED
@@ -0,0 +1,144 @@
1
+ /**
2
+ * Relay 的浏览器半:设置面板里的「应用权限」页。
3
+ *
4
+ * 只做展示和操作,手里没有凭证不参与授权判断——放松权限的操作会由 Host
5
+ * 弹一次系统确认框,所以这里的请求被伪造也提不了权。
6
+ */
7
+
8
+ window.__ModuleLoader__.load({
9
+ id: '@kir_sleeping/dsh-relay',
10
+ factory(require) {
11
+ const React = require('react')
12
+ const h = React.createElement
13
+
14
+ const BASE = '/x/relay'
15
+ const LEVELS = [
16
+ { value: 0, label: '禁止' },
17
+ { value: 1, label: '每条确认' },
18
+ { value: 2, label: '信任已确认标记' },
19
+ { value: 3, label: '完全信任' },
20
+ ]
21
+
22
+ const styles = {
23
+ intro: { opacity: 0.75, lineHeight: 1.6, margin: '0 0 16px' },
24
+ table: { width: '100%', borderCollapse: 'collapse', fontSize: 13 },
25
+ th: { textAlign: 'left', fontWeight: 600, padding: '6px 8px', borderBottom: '1px solid currentColor', opacity: 0.6 },
26
+ td: { padding: '8px', borderBottom: '1px solid currentColor', verticalAlign: 'middle' },
27
+ actions: { display: 'flex', gap: 6, flexWrap: 'wrap' },
28
+ button: {
29
+ font: 'inherit', fontSize: 12, padding: '3px 8px', cursor: 'pointer',
30
+ color: 'inherit', background: 'transparent', border: '1px solid currentColor',
31
+ borderRadius: 6, opacity: 0.85,
32
+ },
33
+ select: {
34
+ font: 'inherit', fontSize: 12, padding: '3px 6px', color: 'inherit',
35
+ background: 'transparent', border: '1px solid currentColor', borderRadius: 6,
36
+ },
37
+ error: {
38
+ margin: '0 0 12px', padding: '8px 10px', borderRadius: 6,
39
+ border: '1px solid currentColor', color: 'inherit', fontSize: 12, opacity: 0.9,
40
+ },
41
+ empty: { opacity: 0.6, padding: '12px 0' },
42
+ }
43
+
44
+ async function request(method, path, body) {
45
+ const init = { method }
46
+ if (body !== undefined) {
47
+ init.headers = { 'Content-Type': 'application/json' }
48
+ init.body = JSON.stringify(body)
49
+ }
50
+ const response = await fetch(BASE + path, init)
51
+ let payload = {}
52
+ try {
53
+ payload = await response.json()
54
+ } catch {
55
+ payload = {}
56
+ }
57
+ if (!response.ok) {
58
+ const error = new Error(`${payload.code || `HTTP ${response.status}`}:${payload.message || ''}`)
59
+ throw error
60
+ }
61
+ return payload
62
+ }
63
+
64
+ function RelaySettings() {
65
+ const [rows, setRows] = React.useState([])
66
+ const [error, setError] = React.useState('')
67
+ const [busy, setBusy] = React.useState(false)
68
+
69
+ const load = React.useCallback(async () => {
70
+ try {
71
+ const payload = await request('GET', '/sources')
72
+ setRows(Array.isArray(payload.sources) ? payload.sources : [])
73
+ } catch (failure) {
74
+ setError(String(failure.message || failure))
75
+ }
76
+ }, [])
77
+
78
+ React.useEffect(() => {
79
+ load()
80
+ }, [load])
81
+
82
+ const run = React.useCallback(async (path, body) => {
83
+ setBusy(true)
84
+ setError('')
85
+ try {
86
+ await request('POST', path, body)
87
+ } catch (failure) {
88
+ setError(String(failure.message || failure))
89
+ } finally {
90
+ setBusy(false)
91
+ load()
92
+ }
93
+ }, [load])
94
+
95
+ const rows_ = rows.map((row) => h('tr', { key: row.source },
96
+ h('td', { style: styles.td }, row.source),
97
+ h('td', { style: styles.td },
98
+ h('select', {
99
+ style: styles.select,
100
+ value: String(row.level),
101
+ disabled: busy || !row.hasFingerprint,
102
+ onChange: (event) => {
103
+ const level = Number(event.currentTarget.value)
104
+ if (level !== row.level) run('/sources/level', { source: row.source, level })
105
+ },
106
+ }, LEVELS.map((option) => h('option', { key: option.value, value: String(option.value) }, option.label)))),
107
+ h('td', { style: styles.td }, row.hasFingerprint ? '已持有' : '—'),
108
+ h('td', { style: styles.td },
109
+ h('div', { style: styles.actions },
110
+ row.hasFingerprint
111
+ ? h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/reset', { source: row.source }) }, '重置凭证')
112
+ : h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/clear', { source: row.source }) }, '解除禁止'),
113
+ h('button', { style: styles.button, disabled: busy, onClick: () => run('/sources/delete', { source: row.source }) }, '删除')))))
114
+
115
+ return h('div', null,
116
+ h('p', { style: styles.intro },
117
+ '这里的每个应用都能把任务交给同一个 agent,并共享「外部任务」会话的上下文。',
118
+ '「完全信任」意味着它可以在不询问你的情况下让 agent 执行任何操作;',
119
+ '提高权限会弹出一次系统确认框。'),
120
+ error ? h('p', { style: styles.error }, error) : null,
121
+ rows.length === 0
122
+ ? h('p', { style: styles.empty }, '还没有外部应用接入。应用第一次调用时会弹出确认框。')
123
+ : h('table', { style: styles.table },
124
+ h('thead', null, h('tr', null,
125
+ h('th', { style: styles.th }, '应用'),
126
+ h('th', { style: styles.th }, '级别'),
127
+ h('th', { style: styles.th }, '凭证'),
128
+ h('th', { style: styles.th }, '操作'))),
129
+ h('tbody', null, rows_)))
130
+ }
131
+
132
+ return {
133
+ inject: ['slots'],
134
+ apply(ctx) {
135
+ ctx.slots.inject('settings.section', () => ctx.slots.register({
136
+ name: 'settings.section',
137
+ id: 'relay',
138
+ order: 25,
139
+ label: '应用权限',
140
+ }, RelaySettings))
141
+ },
142
+ }
143
+ },
144
+ })
@@ -0,0 +1,4 @@
1
+ - insert:
2
+ - id: relay
3
+ name: '@kir_sleeping/dsh-relay'
4
+ config: {}
package/icon.svg ADDED
@@ -0,0 +1,7 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Relay">
2
+ <rect x="4" y="4" width="56" height="56" rx="14" fill="#1f2937"/>
3
+ <path d="M14 32h20" stroke="#60a5fa" stroke-width="4" stroke-linecap="round"/>
4
+ <path d="M28 22l10 10-10 10" fill="none" stroke="#60a5fa" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
5
+ <rect x="40" y="20" width="12" height="24" rx="3" fill="none" stroke="#93c5fd" stroke-width="3"/>
6
+ <path d="M43 26h6M43 32h6M43 38h6" stroke="#93c5fd" stroke-width="2.5" stroke-linecap="round"/>
7
+ </svg>