@xlcongi/dsh-plugin-kanban 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/cordis.patch.yml +3 -0
- package/index.js +304 -0
- package/lib/client.js +167 -0
- package/lib/format.js +62 -0
- package/package.json +49 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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,40 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @xlcongi/dsh-plugin-kanban
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**自选股盯盘看板** DeepSeek Harness 插件:在对话里打开看板、查看当日与浮动盈亏、手填或导入持仓。
|
|
4
|
+
|
|
5
|
+
> 非投资建议。只读公开行情,**不下单**。不做券商登录 / OCR。
|
|
6
|
+
|
|
7
|
+
## 能做什么
|
|
8
|
+
|
|
9
|
+
| 工具 | 作用 |
|
|
10
|
+
|------|------|
|
|
11
|
+
| `kanban_open` | 打开盯盘看板(浏览器地址) |
|
|
12
|
+
| `kanban_summary` | 总资产 / 现金 / 当日与浮动盈亏 / 持仓摘要 |
|
|
13
|
+
| `kanban_holdings_get` | 读取当前持仓配置 |
|
|
14
|
+
| `kanban_holdings_set` | 手填保存持仓(未传现金时保留原现金,不会误清零) |
|
|
15
|
+
| `kanban_holdings_import` | 粘贴 CSV、JSON 或交割明细导入 |
|
|
16
|
+
| `kanban_refresh` | 全量刷新行情与报告 |
|
|
17
|
+
|
|
18
|
+
## 安装
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
dsh plugin --profile desktop add @xlcongi/dsh-plugin-kanban
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
或:
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
dsh plugin --profile desktop add xlcongi/dsh-plugin-kanban
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
安装后重启 DSH。首次使用请设置环境变量 `KANBAN_HOME` 指向看板程序目录(内含 `app.py`),或确认看板已在 `http://127.0.0.1:5000` 运行。
|
|
31
|
+
|
|
32
|
+
## 使用示例
|
|
33
|
+
|
|
34
|
+
- 「打开盯盘看板」
|
|
35
|
+
- 「现在持仓盈亏怎样」
|
|
36
|
+
- 「导入这段持仓 CSV」
|
|
37
|
+
|
|
38
|
+
## 说明
|
|
39
|
+
|
|
40
|
+
插件通过本机看板服务展示页面与计算盈亏;数据留在本机,不上传券商账户。
|
package/cordis.patch.yml
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSH 自选股盯盘看板插件
|
|
3
|
+
* 工具:kanban_open / kanban_summary / kanban_holdings_get /
|
|
4
|
+
* kanban_holdings_set / kanban_holdings_import / kanban_refresh
|
|
5
|
+
* 对接本地 Flask :5000;只读公开行情,不下单。非投资建议。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
9
|
+
import { api, baseUrl, ensureBoard, resolveKanbanHome } from './lib/client.js'
|
|
10
|
+
import { formatHoldingsCard, formatSummaryCard } from './lib/format.js'
|
|
11
|
+
|
|
12
|
+
export const name = '@xlcongi/dsh-plugin-kanban'
|
|
13
|
+
export const inject = ['tools']
|
|
14
|
+
|
|
15
|
+
function textCard(text) {
|
|
16
|
+
return [{ type: 'text', text }]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** cash 未传时保留现有值,禁止隐式写成 0。 */
|
|
20
|
+
async function resolveCashOrPreserve(argsCash) {
|
|
21
|
+
if (argsCash != null && argsCash !== '') {
|
|
22
|
+
const cash = Number(argsCash)
|
|
23
|
+
if (!Number.isFinite(cash) || cash < 0) {
|
|
24
|
+
throw new Error('cash 必须是非负数字')
|
|
25
|
+
}
|
|
26
|
+
return { cash, preserved: false }
|
|
27
|
+
}
|
|
28
|
+
const cur = await api('/api/holdings', { timeoutMs: 15000 })
|
|
29
|
+
const cash = Number(cur.cash)
|
|
30
|
+
if (!Number.isFinite(cash)) {
|
|
31
|
+
throw new Error('无法读取现有现金,请显式传入 cash')
|
|
32
|
+
}
|
|
33
|
+
return { cash, preserved: true }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function apply(ctx) {
|
|
37
|
+
ctx.effect(() =>
|
|
38
|
+
ctx.tools.register(
|
|
39
|
+
defineTool({
|
|
40
|
+
name: 'kanban_open',
|
|
41
|
+
description:
|
|
42
|
+
'打开/启动本地自选股盯盘看板(默认 http://127.0.0.1:5000)。未运行时尝试 py -3 app.py。需本机 kanban 源码或 KANBAN_HOME。非投资建议。',
|
|
43
|
+
parameters: {
|
|
44
|
+
start: {
|
|
45
|
+
type: 'boolean',
|
|
46
|
+
description: '未运行时是否自动启动,默认 true',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
output: {
|
|
50
|
+
schema: { type: 'object', additionalProperties: true },
|
|
51
|
+
render: (_args, value) => textCard(value.card),
|
|
52
|
+
},
|
|
53
|
+
async execute(args) {
|
|
54
|
+
const start = args.start !== false
|
|
55
|
+
const st = await ensureBoard({ start })
|
|
56
|
+
const card = st.ok
|
|
57
|
+
? [
|
|
58
|
+
`【看板】已就绪 ${st.url}`,
|
|
59
|
+
st.started ? `本次已自动启动(pid=${st.pid || '-'})` : '进程已在运行',
|
|
60
|
+
st.home ? `源码目录 ${st.home}` : '未解析到 KANBAN_HOME(仅 HTTP)',
|
|
61
|
+
'浏览器打开上述地址即可盯盘。非投资建议。',
|
|
62
|
+
].join('\n')
|
|
63
|
+
: [
|
|
64
|
+
`【看板】未能连接 ${st.url}`,
|
|
65
|
+
st.error || '',
|
|
66
|
+
`请设置 KANBAN_HOME 后手动:cd kanban && py -3 app.py`,
|
|
67
|
+
`当前解析 home=${st.home || '(空)'}`,
|
|
68
|
+
].join('\n')
|
|
69
|
+
return { ...st, card, 免责: '非投资建议' }
|
|
70
|
+
},
|
|
71
|
+
}),
|
|
72
|
+
),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
ctx.effect(() =>
|
|
76
|
+
ctx.tools.register(
|
|
77
|
+
defineTool({
|
|
78
|
+
name: 'kanban_summary',
|
|
79
|
+
description:
|
|
80
|
+
'拉取自选股盯盘看板实时汇总:总资产/市值/现金、当日与浮动盈亏、持仓明细摘要。默认 live=1。非投资建议。',
|
|
81
|
+
parameters: {
|
|
82
|
+
live: {
|
|
83
|
+
type: 'boolean',
|
|
84
|
+
description: '是否实时拉行情重算,默认 true',
|
|
85
|
+
},
|
|
86
|
+
ensure: {
|
|
87
|
+
type: 'boolean',
|
|
88
|
+
description: '未运行时是否先启动看板,默认 true',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
output: {
|
|
92
|
+
schema: { type: 'object', additionalProperties: true },
|
|
93
|
+
render: (_args, value) => textCard(value.card),
|
|
94
|
+
},
|
|
95
|
+
async execute(args) {
|
|
96
|
+
if (args.ensure !== false) {
|
|
97
|
+
const st = await ensureBoard({ start: true })
|
|
98
|
+
if (!st.ok) throw new Error(st.error || `看板未就绪 ${baseUrl()}`)
|
|
99
|
+
}
|
|
100
|
+
const live = args.live !== false
|
|
101
|
+
const report = await api(`/api/report?live=${live ? '1' : '0'}`, {
|
|
102
|
+
timeoutMs: live ? 45000 : 15000,
|
|
103
|
+
})
|
|
104
|
+
const card = formatSummaryCard(report)
|
|
105
|
+
return {
|
|
106
|
+
updateTime: report.updateTime,
|
|
107
|
+
tradeDate: report.tradeDate,
|
|
108
|
+
summary: report.summary,
|
|
109
|
+
positionCount: (report.positions || []).length,
|
|
110
|
+
positions: (report.positions || []).map((p) => ({
|
|
111
|
+
symbol: p.symbol,
|
|
112
|
+
name: p.name,
|
|
113
|
+
price: p.price,
|
|
114
|
+
dayPnL: p.dayPnL,
|
|
115
|
+
dayPct: p.dayPct,
|
|
116
|
+
floatPnL: p.floatPnL,
|
|
117
|
+
mv: p.mv,
|
|
118
|
+
})),
|
|
119
|
+
url: baseUrl(),
|
|
120
|
+
card,
|
|
121
|
+
免责: '非投资建议',
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
}),
|
|
125
|
+
),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
ctx.effect(() =>
|
|
129
|
+
ctx.tools.register(
|
|
130
|
+
defineTool({
|
|
131
|
+
name: 'kanban_holdings_get',
|
|
132
|
+
description: '读取看板当前持仓配置(现金 + positions)。非投资建议。',
|
|
133
|
+
parameters: {
|
|
134
|
+
ensure: {
|
|
135
|
+
type: 'boolean',
|
|
136
|
+
description: '未运行时是否先启动看板,默认 true',
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
output: {
|
|
140
|
+
schema: { type: 'object', additionalProperties: true },
|
|
141
|
+
render: (_args, value) => textCard(value.card),
|
|
142
|
+
},
|
|
143
|
+
async execute(args) {
|
|
144
|
+
if (args.ensure !== false) {
|
|
145
|
+
const st = await ensureBoard({ start: true })
|
|
146
|
+
if (!st.ok) throw new Error(st.error || `看板未就绪 ${baseUrl()}`)
|
|
147
|
+
}
|
|
148
|
+
const holdings = await api('/api/holdings')
|
|
149
|
+
return {
|
|
150
|
+
holdings,
|
|
151
|
+
card: formatHoldingsCard(holdings),
|
|
152
|
+
url: baseUrl(),
|
|
153
|
+
免责: '非投资建议',
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
}),
|
|
157
|
+
),
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
ctx.effect(() =>
|
|
161
|
+
ctx.tools.register(
|
|
162
|
+
defineTool({
|
|
163
|
+
name: 'kanban_holdings_set',
|
|
164
|
+
description:
|
|
165
|
+
'手填保存持仓:positions 必填;cash 可选——省略则先读现有现金再保存,禁止隐式清零。保存后看板重算。非投资建议。',
|
|
166
|
+
parameters: {
|
|
167
|
+
cash: {
|
|
168
|
+
type: 'number',
|
|
169
|
+
description: '可用现金;省略则保留看板现有现金(不会写成 0)',
|
|
170
|
+
},
|
|
171
|
+
positions: {
|
|
172
|
+
type: 'array',
|
|
173
|
+
description: '持仓列表,每项含 symbol/shares/cost,可选 name、bucket',
|
|
174
|
+
items: { type: 'object', additionalProperties: true },
|
|
175
|
+
},
|
|
176
|
+
positions_json: {
|
|
177
|
+
type: 'string',
|
|
178
|
+
description: '若不便传数组,可传 positions 的 JSON 字符串',
|
|
179
|
+
},
|
|
180
|
+
note: { type: 'string', description: '备注(可选)' },
|
|
181
|
+
},
|
|
182
|
+
output: {
|
|
183
|
+
schema: { type: 'object', additionalProperties: true },
|
|
184
|
+
render: (_args, value) => textCard(value.card),
|
|
185
|
+
},
|
|
186
|
+
async execute(args) {
|
|
187
|
+
const st = await ensureBoard({ start: true })
|
|
188
|
+
if (!st.ok) throw new Error(st.error || `看板未就绪 ${baseUrl()}`)
|
|
189
|
+
|
|
190
|
+
let positions = args.positions
|
|
191
|
+
if ((!positions || !positions.length) && args.positions_json) {
|
|
192
|
+
positions = JSON.parse(args.positions_json)
|
|
193
|
+
}
|
|
194
|
+
if (!Array.isArray(positions)) {
|
|
195
|
+
throw new Error('positions 必须是数组,或提供 positions_json')
|
|
196
|
+
}
|
|
197
|
+
const { cash, preserved } = await resolveCashOrPreserve(args.cash)
|
|
198
|
+
const body = {
|
|
199
|
+
cash,
|
|
200
|
+
positions,
|
|
201
|
+
note: args.note || (preserved ? 'DSH 插件手填保存(保留原现金)' : 'DSH 插件手填保存'),
|
|
202
|
+
}
|
|
203
|
+
const res = await api('/api/holdings', { method: 'POST', body, timeoutMs: 60000 })
|
|
204
|
+
const holdings = res.holdings || body
|
|
205
|
+
return {
|
|
206
|
+
ok: !!res.ok,
|
|
207
|
+
updateTime: res.updateTime,
|
|
208
|
+
cashPreserved: preserved,
|
|
209
|
+
holdings,
|
|
210
|
+
card: [
|
|
211
|
+
`【持仓已保存】${res.updateTime || ''}${preserved ? '|现金已保留现有值' : ''}`,
|
|
212
|
+
formatHoldingsCard(holdings),
|
|
213
|
+
].join('\n'),
|
|
214
|
+
免责: '非投资建议',
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
),
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
ctx.effect(() =>
|
|
222
|
+
ctx.tools.register(
|
|
223
|
+
defineTool({
|
|
224
|
+
name: 'kanban_holdings_import',
|
|
225
|
+
description:
|
|
226
|
+
'粘贴导入持仓:holdings_csv / holdings_json / settlement_csv(交割明细汇总)。merge=true 合并。非投资建议。',
|
|
227
|
+
parameters: {
|
|
228
|
+
text: { type: 'string', required: true, description: 'CSV 或 JSON 全文' },
|
|
229
|
+
mode: {
|
|
230
|
+
type: 'string',
|
|
231
|
+
description: 'holdings_csv | holdings_json | settlement_csv,默认 holdings_csv',
|
|
232
|
+
},
|
|
233
|
+
merge: { type: 'boolean', description: '是否与现有持仓合并,默认 false' },
|
|
234
|
+
cash: { type: 'number', description: '覆盖现金(可选)' },
|
|
235
|
+
},
|
|
236
|
+
output: {
|
|
237
|
+
schema: { type: 'object', additionalProperties: true },
|
|
238
|
+
render: (_args, value) => textCard(value.card),
|
|
239
|
+
},
|
|
240
|
+
async execute(args) {
|
|
241
|
+
const st = await ensureBoard({ start: true })
|
|
242
|
+
if (!st.ok) throw new Error(st.error || `看板未就绪 ${baseUrl()}`)
|
|
243
|
+
const body = {
|
|
244
|
+
text: args.text,
|
|
245
|
+
mode: args.mode || 'holdings_csv',
|
|
246
|
+
merge: !!args.merge,
|
|
247
|
+
}
|
|
248
|
+
if (args.cash != null) body.cash = Number(args.cash)
|
|
249
|
+
const res = await api('/api/holdings/import', {
|
|
250
|
+
method: 'POST',
|
|
251
|
+
body,
|
|
252
|
+
timeoutMs: 90000,
|
|
253
|
+
})
|
|
254
|
+
const holdings = res.holdings || {}
|
|
255
|
+
const warn = (res.warnings || []).map((w) => `[提示] ${w}`)
|
|
256
|
+
return {
|
|
257
|
+
ok: !!res.ok,
|
|
258
|
+
mode: res.mode,
|
|
259
|
+
imported: res.imported,
|
|
260
|
+
warnings: res.warnings || [],
|
|
261
|
+
updateTime: res.updateTime,
|
|
262
|
+
holdings,
|
|
263
|
+
card: [
|
|
264
|
+
`【持仓导入】mode=${res.mode} 解析 ${res.imported} 行|${res.updateTime || ''}`,
|
|
265
|
+
...warn,
|
|
266
|
+
formatHoldingsCard(holdings),
|
|
267
|
+
].join('\n'),
|
|
268
|
+
免责: '非投资建议',
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
}),
|
|
272
|
+
),
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
ctx.effect(() =>
|
|
276
|
+
ctx.tools.register(
|
|
277
|
+
defineTool({
|
|
278
|
+
name: 'kanban_refresh',
|
|
279
|
+
description:
|
|
280
|
+
'触发看板全量刷新(写盘、网格 tick、K 线缓存)。日常盯盘用 kanban_summary 即可。非投资建议。',
|
|
281
|
+
parameters: {},
|
|
282
|
+
output: {
|
|
283
|
+
schema: { type: 'object', additionalProperties: true },
|
|
284
|
+
render: (_args, value) => textCard(value.card),
|
|
285
|
+
},
|
|
286
|
+
async execute() {
|
|
287
|
+
const st = await ensureBoard({ start: true })
|
|
288
|
+
if (!st.ok) throw new Error(st.error || `看板未就绪 ${baseUrl()}`)
|
|
289
|
+
const res = await api('/api/refresh', { timeoutMs: 120000 })
|
|
290
|
+
const home = resolveKanbanHome()
|
|
291
|
+
return {
|
|
292
|
+
...res,
|
|
293
|
+
url: baseUrl(),
|
|
294
|
+
home,
|
|
295
|
+
card: res.ok
|
|
296
|
+
? `【刷新完成】${res.updateTime || ''}\n看板 ${baseUrl()}\n非投资建议。`
|
|
297
|
+
: `【刷新失败】${res.error || 'unknown'}\n非投资建议。`,
|
|
298
|
+
免责: '非投资建议',
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
}),
|
|
302
|
+
),
|
|
303
|
+
)
|
|
304
|
+
}
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地自选股盯盘看板 HTTP 客户端 + 可选拉起 Flask。
|
|
3
|
+
* 默认 http://127.0.0.1:5000;看板源码目录由 KANBAN_HOME 指定。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { spawn } from 'node:child_process'
|
|
7
|
+
import fs from 'node:fs'
|
|
8
|
+
import net from 'node:net'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import { fileURLToPath } from 'node:url'
|
|
11
|
+
|
|
12
|
+
const DEFAULT_PORT = 5000
|
|
13
|
+
const DEFAULT_HOST = '127.0.0.1'
|
|
14
|
+
|
|
15
|
+
const LIB_DIR = path.dirname(fileURLToPath(import.meta.url))
|
|
16
|
+
const PACKAGE_ROOT = path.resolve(LIB_DIR, '..')
|
|
17
|
+
|
|
18
|
+
/** 解析看板 Python 工程目录(含 app.py) */
|
|
19
|
+
export function resolveKanbanHome() {
|
|
20
|
+
const env = (process.env.KANBAN_HOME || '').trim()
|
|
21
|
+
if (env && fs.existsSync(path.join(env, 'app.py'))) return path.resolve(env)
|
|
22
|
+
|
|
23
|
+
const rootEnv = (process.env.SMART_QUANT_ROOT || '').trim()
|
|
24
|
+
if (rootEnv) {
|
|
25
|
+
const p = path.join(rootEnv, 'kanban')
|
|
26
|
+
if (fs.existsSync(path.join(p, 'app.py'))) return path.resolve(p)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 本仓库:dsh-plugin-kanban 与 kanban 同级(本地开发约定,非个人绝对路径)
|
|
30
|
+
const sibling = path.resolve(PACKAGE_ROOT, '..', 'kanban')
|
|
31
|
+
if (fs.existsSync(path.join(sibling, 'app.py'))) return sibling
|
|
32
|
+
|
|
33
|
+
return ''
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function baseUrl() {
|
|
37
|
+
const fromEnv = (process.env.KANBAN_URL || '').trim().replace(/\/$/, '')
|
|
38
|
+
if (fromEnv) return fromEnv
|
|
39
|
+
const host = (process.env.KANBAN_HOST || DEFAULT_HOST).trim() || DEFAULT_HOST
|
|
40
|
+
const port = Number(process.env.KANBAN_PORT || DEFAULT_PORT) || DEFAULT_PORT
|
|
41
|
+
return `http://${host}:${port}`
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function sleep(ms) {
|
|
45
|
+
return new Promise((r) => setTimeout(r, ms))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function tcpOpen(host, port, timeoutMs = 800) {
|
|
49
|
+
return new Promise((resolve) => {
|
|
50
|
+
const sock = net.connect({ host, port }, () => {
|
|
51
|
+
sock.end()
|
|
52
|
+
resolve(true)
|
|
53
|
+
})
|
|
54
|
+
sock.on('error', () => resolve(false))
|
|
55
|
+
sock.setTimeout(timeoutMs, () => {
|
|
56
|
+
sock.destroy()
|
|
57
|
+
resolve(false)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function parseHostPort(url) {
|
|
63
|
+
try {
|
|
64
|
+
const u = new URL(url)
|
|
65
|
+
return { host: u.hostname || DEFAULT_HOST, port: Number(u.port) || DEFAULT_PORT }
|
|
66
|
+
} catch {
|
|
67
|
+
return { host: DEFAULT_HOST, port: DEFAULT_PORT }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function api(pathname, { method = 'GET', body, timeoutMs = 20000 } = {}) {
|
|
72
|
+
const url = `${baseUrl()}${pathname.startsWith('/') ? pathname : `/${pathname}`}`
|
|
73
|
+
const ctrl = new AbortController()
|
|
74
|
+
const t = setTimeout(() => ctrl.abort(), timeoutMs)
|
|
75
|
+
try {
|
|
76
|
+
const init = {
|
|
77
|
+
method,
|
|
78
|
+
signal: ctrl.signal,
|
|
79
|
+
headers: { Accept: 'application/json' },
|
|
80
|
+
}
|
|
81
|
+
if (body !== undefined) {
|
|
82
|
+
init.headers['Content-Type'] = 'application/json; charset=utf-8'
|
|
83
|
+
init.body = typeof body === 'string' ? body : JSON.stringify(body)
|
|
84
|
+
}
|
|
85
|
+
const res = await fetch(url, init)
|
|
86
|
+
const text = await res.text()
|
|
87
|
+
let data
|
|
88
|
+
try {
|
|
89
|
+
data = text ? JSON.parse(text) : {}
|
|
90
|
+
} catch {
|
|
91
|
+
data = { raw: text }
|
|
92
|
+
}
|
|
93
|
+
if (!res.ok) {
|
|
94
|
+
const msg = data.error || data.message || `HTTP ${res.status}`
|
|
95
|
+
const err = new Error(msg)
|
|
96
|
+
err.status = res.status
|
|
97
|
+
err.data = data
|
|
98
|
+
throw err
|
|
99
|
+
}
|
|
100
|
+
return data
|
|
101
|
+
} finally {
|
|
102
|
+
clearTimeout(t)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function spawnBoard(home) {
|
|
107
|
+
if (!home) {
|
|
108
|
+
throw new Error(
|
|
109
|
+
'未找到看板源码。请设置环境变量 KANBAN_HOME 指向含 app.py 的看板目录',
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
const isWin = process.platform === 'win32'
|
|
113
|
+
const cmd = isWin ? 'py' : 'python3'
|
|
114
|
+
const args = isWin ? ['-3', 'app.py'] : ['app.py']
|
|
115
|
+
const child = spawn(cmd, args, {
|
|
116
|
+
cwd: home,
|
|
117
|
+
detached: true,
|
|
118
|
+
stdio: 'ignore',
|
|
119
|
+
windowsHide: true,
|
|
120
|
+
env: {
|
|
121
|
+
...process.env,
|
|
122
|
+
PYTHONIOENCODING: 'utf-8',
|
|
123
|
+
PYTHONUTF8: '1',
|
|
124
|
+
},
|
|
125
|
+
})
|
|
126
|
+
child.unref()
|
|
127
|
+
return child.pid
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** 探测并必要时拉起看板;返回 { url, home, started, pid? } */
|
|
131
|
+
export async function ensureBoard({ start = true } = {}) {
|
|
132
|
+
const url = baseUrl()
|
|
133
|
+
const { host, port } = parseHostPort(url)
|
|
134
|
+
const home = resolveKanbanHome()
|
|
135
|
+
const up = await tcpOpen(host, port)
|
|
136
|
+
if (up) {
|
|
137
|
+
try {
|
|
138
|
+
await api('/api/holdings', { timeoutMs: 5000 })
|
|
139
|
+
return { url, home, started: false, ok: true }
|
|
140
|
+
} catch {
|
|
141
|
+
// 端口占用但不是看板
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (!start) {
|
|
145
|
+
return { url, home, started: false, ok: false, error: `看板未响应 ${url}` }
|
|
146
|
+
}
|
|
147
|
+
const pid = await spawnBoard(home)
|
|
148
|
+
for (let i = 0; i < 30; i++) {
|
|
149
|
+
await sleep(500)
|
|
150
|
+
if (await tcpOpen(host, port)) {
|
|
151
|
+
try {
|
|
152
|
+
await api('/api/holdings', { timeoutMs: 8000 })
|
|
153
|
+
return { url, home, started: true, pid, ok: true }
|
|
154
|
+
} catch {
|
|
155
|
+
/* keep waiting */
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
url,
|
|
161
|
+
home,
|
|
162
|
+
started: true,
|
|
163
|
+
pid,
|
|
164
|
+
ok: false,
|
|
165
|
+
error: `已尝试启动(pid=${pid})但仍无法连接 ${url},请本机手动运行 py -3 app.py`,
|
|
166
|
+
}
|
|
167
|
+
}
|
package/lib/format.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** 文本卡片:A 股红涨绿跌语义仅用于展示,非投资建议。 */
|
|
2
|
+
|
|
3
|
+
function n(v, digits = 2) {
|
|
4
|
+
const x = Number(v)
|
|
5
|
+
if (!Number.isFinite(x)) return '-'
|
|
6
|
+
return x.toLocaleString('zh-CN', {
|
|
7
|
+
minimumFractionDigits: digits,
|
|
8
|
+
maximumFractionDigits: digits,
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function pct(v) {
|
|
13
|
+
const x = Number(v)
|
|
14
|
+
if (!Number.isFinite(x)) return '-'
|
|
15
|
+
const s = x >= 0 ? '+' : ''
|
|
16
|
+
return `${s}${x.toFixed(2)}%`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function money(v) {
|
|
20
|
+
const x = Number(v)
|
|
21
|
+
if (!Number.isFinite(x)) return '-'
|
|
22
|
+
const s = x > 0 ? '+' : ''
|
|
23
|
+
return `${s}${n(x)}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function formatSummaryCard(report) {
|
|
27
|
+
const s = report.summary || {}
|
|
28
|
+
const lines = [
|
|
29
|
+
`【自选股盯盘】${report.updateTime || ''} 交易日 ${report.tradeDate || '-'}`,
|
|
30
|
+
`总资产 ${n(s.totalAsset)}|市值 ${n(s.totalMarketValue)}|现金 ${n(s.cash)}`,
|
|
31
|
+
`当日盈亏 ${money(s.totalDayPnL)}(${pct(s.totalDayPct)})|浮动盈亏 ${money(s.totalFloatPnL)}(${pct(s.totalFloatPct)})`,
|
|
32
|
+
]
|
|
33
|
+
if (report.error) lines.push(`注意:${report.error}`)
|
|
34
|
+
const pos = Array.isArray(report.positions) ? report.positions : []
|
|
35
|
+
const top = [...pos]
|
|
36
|
+
.sort((a, b) => Math.abs(Number(b.dayPnL) || 0) - Math.abs(Number(a.dayPnL) || 0))
|
|
37
|
+
.slice(0, 12)
|
|
38
|
+
for (const p of top) {
|
|
39
|
+
lines.push(
|
|
40
|
+
`${p.symbol} ${p.name || ''} 现价 ${n(p.price, 3)} 当日 ${money(p.dayPnL)}(${pct(p.dayPct)}) 浮动 ${money(p.floatPnL)} 市值 ${n(p.mv)}`,
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
if (pos.length > top.length) lines.push(`…其余 ${pos.length - top.length} 只已省略`)
|
|
44
|
+
lines.push(report.disclaimer || '非投资建议。只读公开行情,不下单。')
|
|
45
|
+
return lines.join('\n')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function formatHoldingsCard(payload) {
|
|
49
|
+
const positions = payload.positions || []
|
|
50
|
+
const lines = [
|
|
51
|
+
`【持仓配置】现金 ${n(payload.cash)}|${positions.length} 只|来源 ${payload.source || '-'}|${payload.updated_at || payload.updateTime || ''}`,
|
|
52
|
+
]
|
|
53
|
+
for (const p of positions.slice(0, 30)) {
|
|
54
|
+
lines.push(
|
|
55
|
+
`${p.symbol} ${p.name || ''} ${p.shares}股 成本 ${n(p.cost, 3)}${p.bucket ? ` [${p.bucket}]` : ''}`,
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
if (positions.length > 30) lines.push(`…其余 ${positions.length - 30} 只已省略`)
|
|
59
|
+
if (payload.note) lines.push(`备注:${payload.note}`)
|
|
60
|
+
lines.push('非投资建议。')
|
|
61
|
+
return lines.join('\n')
|
|
62
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xlcongi/dsh-plugin-kanban",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "自选股盯盘看板:在对话里打开看板、查看盈亏、录入或导入持仓。只读公开行情,不下单。非投资建议。",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"files": [
|
|
11
|
+
"index.js",
|
|
12
|
+
"lib",
|
|
13
|
+
"cordis.patch.yml",
|
|
14
|
+
"README.md",
|
|
15
|
+
"LICENSE"
|
|
16
|
+
],
|
|
17
|
+
"keywords": [
|
|
18
|
+
"dsh-plugin",
|
|
19
|
+
"deepseek-harness",
|
|
20
|
+
"a-share",
|
|
21
|
+
"watchlist",
|
|
22
|
+
"holdings",
|
|
23
|
+
"kanban"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "xlcongi <xlcongi@foxmail.com>",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/xlcongi/dsh-plugin-kanban.git"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/xlcongi/dsh-plugin-kanban#readme",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/xlcongi/dsh-plugin-kanban/issues"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"dsh": {
|
|
39
|
+
"bundle": {
|
|
40
|
+
"patch": "./cordis.patch.yml"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@deepseek-ai/dsh-tools": "*"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"@deepseek-ai/dsh-tools": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|