@zhangxij/dsh-positions 0.1.2 → 0.3.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/README.md +30 -12
- package/client.js +952 -130
- package/docs/screenshots/01-overview.jpg +0 -0
- package/docs/screenshots/02-entry.jpg +0 -0
- package/docs/screenshots/03-fees.jpg +0 -0
- package/docs/screenshots/04-calc.jpg +0 -0
- package/index.js +389 -3
- package/package.json +1 -1
- package/screenshots.json +3 -3
- package/seed.js +14 -0
- package/docs/screenshots/02-calc.jpg +0 -0
- package/docs/screenshots/03-modal.jpg +0 -0
- package/docs/screenshots/04-groups.jpg +0 -0
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/index.js
CHANGED
|
@@ -11,14 +11,15 @@
|
|
|
11
11
|
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
12
12
|
import { homedir } from 'node:os'
|
|
13
13
|
import { dirname, join, resolve } from 'node:path'
|
|
14
|
+
import { inflateRawSync } from 'node:zlib'
|
|
14
15
|
import { fileURLToPath } from 'node:url'
|
|
15
16
|
import seed from './seed.js'
|
|
16
17
|
|
|
17
18
|
const ROUTE = '/positions'
|
|
18
|
-
const MAX_BODY_BYTES =
|
|
19
|
+
const MAX_BODY_BYTES = 8 * 1024 * 1024
|
|
19
20
|
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
20
21
|
|
|
21
|
-
export const inject = ['webServer']
|
|
22
|
+
export const inject = ['webServer', 'llm', 'agentDefaultModel']
|
|
22
23
|
|
|
23
24
|
/* ------------------------------------------------------------------ 数据目录 */
|
|
24
25
|
|
|
@@ -47,7 +48,7 @@ function normalizeRecord(raw) {
|
|
|
47
48
|
if (!isObject(raw)) return null
|
|
48
49
|
const date = str(raw.date)
|
|
49
50
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) return null
|
|
50
|
-
const side = raw.side === '卖出' ? '卖出' : raw.side === '买入' ? '买入' : null
|
|
51
|
+
const side = raw.side === '卖出' ? '卖出' : raw.side === '买入' ? '买入' : raw.side === '分红' ? '分红' : null
|
|
51
52
|
const price = num(raw.price)
|
|
52
53
|
const qty = num(raw.qty)
|
|
53
54
|
if (!side || price <= 0 || qty <= 0) return null
|
|
@@ -66,16 +67,84 @@ function normalizeStock(raw) {
|
|
|
66
67
|
group: str(raw.group).trim(),
|
|
67
68
|
price: Math.max(0, num(raw.price)),
|
|
68
69
|
dayPnl: num(raw.dayPnl),
|
|
70
|
+
archived: raw.archived === true,
|
|
71
|
+
note: str(raw.note).slice(0, 500),
|
|
72
|
+
fields: isObject(raw.fields)
|
|
73
|
+
? Object.keys(raw.fields).reduce((acc, k) => { acc[k] = str(raw.fields[k]).slice(0, 200); return acc }, {})
|
|
74
|
+
: {},
|
|
69
75
|
records: Array.isArray(raw.records) ? raw.records.map(normalizeRecord).filter(Boolean) : []
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
|
|
79
|
+
const DEFAULT_COLUMNS = [
|
|
80
|
+
{ key: 'stock', label: '股票', builtin: true },
|
|
81
|
+
{ key: 'date', label: '日期', builtin: true },
|
|
82
|
+
{ key: 'side', label: '方向', builtin: true },
|
|
83
|
+
{ key: 'price', label: '价格', builtin: true },
|
|
84
|
+
{ key: 'qty', label: '数量', builtin: true },
|
|
85
|
+
{ key: 'fee', label: '费用', builtin: true },
|
|
86
|
+
{ key: 'net', label: '当次含费用投入', builtin: true },
|
|
87
|
+
{ key: 'cumQty', label: '累计数量', builtin: true },
|
|
88
|
+
{ key: 'cumInvest', label: '累计投入', builtin: true },
|
|
89
|
+
{ key: 'cost', label: '累计成本', builtin: true }
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
function normalizeColumn(raw) {
|
|
93
|
+
if (!isObject(raw)) return null
|
|
94
|
+
const key = str(raw.key).trim()
|
|
95
|
+
const label = str(raw.label).trim()
|
|
96
|
+
if (!key || !label) return null
|
|
97
|
+
return { key, label: label.slice(0, 20), builtin: raw.builtin === true }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function clampRate(value, fallback) {
|
|
101
|
+
const n = num(value, fallback)
|
|
102
|
+
return n >= 0 && n <= 0.05 ? n : fallback
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function normalizeRule(raw) {
|
|
106
|
+
if (!isObject(raw)) return null
|
|
107
|
+
const name = str(raw.name).trim()
|
|
108
|
+
if (!name) return null
|
|
109
|
+
return {
|
|
110
|
+
id: str(raw.id).trim() || 'rule-' + name,
|
|
111
|
+
name,
|
|
112
|
+
commissionRate: clampRate(raw.commissionRate, 0.00025),
|
|
113
|
+
minCommission: Math.max(0, num(raw.minCommission, 5)),
|
|
114
|
+
stampDutyRate: clampRate(raw.stampDutyRate, 0.0005),
|
|
115
|
+
transferFeeRate: clampRate(raw.transferFeeRate, 0.00001),
|
|
116
|
+
otherFixed: Math.max(0, num(raw.otherFixed, 0)),
|
|
117
|
+
note: str(raw.note).slice(0, 200),
|
|
118
|
+
image: str(raw.image).slice(0, 400000)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
73
122
|
function normalizeState(raw) {
|
|
74
123
|
const state = isObject(raw) ? raw : {}
|
|
75
124
|
const stocks = Array.isArray(state.stocks) ? state.stocks.map(normalizeStock).filter(Boolean) : []
|
|
125
|
+
let rules = Array.isArray(state.feeRules) ? state.feeRules.map(normalizeRule).filter(Boolean) : []
|
|
126
|
+
if (rules.length === 0) rules = seed.feeRules.map(normalizeRule).filter(Boolean)
|
|
127
|
+
const activeFeeRule = rules.some((r) => r.id === state.activeFeeRule) ? state.activeFeeRule : rules[0].id
|
|
128
|
+
let columns = Array.isArray(state.columns) ? state.columns.map(normalizeColumn).filter(Boolean) : []
|
|
129
|
+
if (columns.length === 0) columns = DEFAULT_COLUMNS.map((c) => Object.assign({}, c))
|
|
130
|
+
if (!columns.some((c) => c.key === 'note')) {
|
|
131
|
+
const costIndex = columns.findIndex((c) => c.key === 'cost')
|
|
132
|
+
const noteColumn = { key: 'note', label: '备注', builtin: true }
|
|
133
|
+
if (costIndex >= 0) columns.splice(costIndex + 1, 0, noteColumn)
|
|
134
|
+
else columns.push(noteColumn)
|
|
135
|
+
}
|
|
76
136
|
return {
|
|
77
137
|
version: 1,
|
|
138
|
+
columns,
|
|
139
|
+
feeRules: rules,
|
|
140
|
+
activeFeeRule,
|
|
78
141
|
feeRate: num(state.feeRate) > 0 ? num(state.feeRate) : 0.0003,
|
|
142
|
+
vision: {
|
|
143
|
+
enabled: state.vision?.enabled === true,
|
|
144
|
+
baseUrl: str(state.vision?.baseUrl).slice(0, 300),
|
|
145
|
+
apiKey: str(state.vision?.apiKey).slice(0, 300),
|
|
146
|
+
model: str(state.vision?.model).slice(0, 100)
|
|
147
|
+
},
|
|
79
148
|
tags: Array.isArray(state.tags) ? state.tags.map((t) => str(t).trim()).filter(Boolean) : seed.tags.slice(),
|
|
80
149
|
activeTags: Array.isArray(state.activeTags) ? state.activeTags.map((t) => str(t).trim()).filter(Boolean) : [],
|
|
81
150
|
calc: isObject(state.calc) ? state.calc : {},
|
|
@@ -83,6 +152,82 @@ function normalizeState(raw) {
|
|
|
83
152
|
}
|
|
84
153
|
}
|
|
85
154
|
|
|
155
|
+
/* ------------------------------------------------------------------ xlsx */
|
|
156
|
+
|
|
157
|
+
function unzipEntries(buf) {
|
|
158
|
+
let eocd = -1
|
|
159
|
+
for (let i = buf.length - 22; i >= Math.max(0, buf.length - 65557); i--) {
|
|
160
|
+
if (buf.readUInt32LE(i) === 0x06054b50) { eocd = i; break }
|
|
161
|
+
}
|
|
162
|
+
if (eocd < 0) throw bad('不是有效的 xlsx 文件')
|
|
163
|
+
const count = buf.readUInt16LE(eocd + 10)
|
|
164
|
+
let off = buf.readUInt32LE(eocd + 16)
|
|
165
|
+
const files = {}
|
|
166
|
+
for (let i = 0; i < count; i++) {
|
|
167
|
+
if (off + 46 > buf.length || buf.readUInt32LE(off) !== 0x02014b50) break
|
|
168
|
+
const method = buf.readUInt16LE(off + 10)
|
|
169
|
+
const csize = buf.readUInt32LE(off + 20)
|
|
170
|
+
const nameLen = buf.readUInt16LE(off + 28)
|
|
171
|
+
const extraLen = buf.readUInt16LE(off + 30)
|
|
172
|
+
const commentLen = buf.readUInt16LE(off + 32)
|
|
173
|
+
const localOff = buf.readUInt32LE(off + 42)
|
|
174
|
+
const name = buf.toString('utf8', off + 46, off + 46 + nameLen)
|
|
175
|
+
const lNameLen = buf.readUInt16LE(localOff + 26)
|
|
176
|
+
const lExtraLen = buf.readUInt16LE(localOff + 28)
|
|
177
|
+
const dataStart = localOff + 30 + lNameLen + lExtraLen
|
|
178
|
+
const raw = buf.subarray(dataStart, dataStart + csize)
|
|
179
|
+
files[name] = method === 0 ? raw : inflateRawSync(raw)
|
|
180
|
+
off += 46 + nameLen + extraLen + commentLen
|
|
181
|
+
}
|
|
182
|
+
return files
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function xmlText(xml) {
|
|
186
|
+
return String(xml)
|
|
187
|
+
.replace(/<\/?t[^>]*>/g, '')
|
|
188
|
+
.replace(/<[^>]+>/g, '')
|
|
189
|
+
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'")
|
|
190
|
+
.replace(/&#(\d+);/g, (_, d) => String.fromCodePoint(Number(d)))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function colIndex(ref) {
|
|
194
|
+
const letters = (String(ref).match(/^[A-Z]+/) || ['A'])[0]
|
|
195
|
+
let n = 0
|
|
196
|
+
for (const ch of letters) n = n * 26 + (ch.charCodeAt(0) - 64)
|
|
197
|
+
return n - 1
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function parseXlsx(buf) {
|
|
201
|
+
const files = unzipEntries(buf)
|
|
202
|
+
const sheetName = Object.keys(files).find((n) => /^xl\/worksheets\/sheet1\.xml$/.test(n)) || Object.keys(files).find((n) => /^xl\/worksheets\/.*\.xml$/.test(n))
|
|
203
|
+
if (!sheetName) throw bad('xlsx 里找不到工作表')
|
|
204
|
+
const sharedXml = files['xl/sharedStrings.xml'] ? files['xl/sharedStrings.xml'].toString('utf8') : ''
|
|
205
|
+
const shared = []
|
|
206
|
+
for (const m of sharedXml.matchAll(/<si>([\s\S]*?)<\/si>/g)) shared.push(xmlText(m[1]))
|
|
207
|
+
|
|
208
|
+
const sheetXml = files[sheetName].toString('utf8')
|
|
209
|
+
const lines = []
|
|
210
|
+
for (const rowMatch of sheetXml.matchAll(/<row[^>]*>([\s\S]*?)<\/row>/g)) {
|
|
211
|
+
const cells = []
|
|
212
|
+
for (const c of rowMatch[1].matchAll(/<c\s+([^>]*?)(\/>|>([\s\S]*?)<\/c>)/g)) {
|
|
213
|
+
const attrs = c[1]
|
|
214
|
+
const body = c[3] || ''
|
|
215
|
+
const type = (attrs.match(/\bt="([^"]+)"/) || [])[1] || ''
|
|
216
|
+
const ref = (attrs.match(/\br="([^"]+)"/) || [])[1] || ''
|
|
217
|
+
const rawValue = (body.match(/<v>([\s\S]*?)<\/v>/) || [])[1]
|
|
218
|
+
let value = ''
|
|
219
|
+
if (type === 's') value = shared[Number(rawValue)] ?? ''
|
|
220
|
+
else if (type === 'inlineStr') value = xmlText((body.match(/<is>([\s\S]*?)<\/is>/) || [])[1] || '')
|
|
221
|
+
else if (rawValue !== undefined) value = xmlText(rawValue)
|
|
222
|
+
const index = ref ? colIndex(ref) : cells.length
|
|
223
|
+
cells[index] = String(value).trim()
|
|
224
|
+
}
|
|
225
|
+
const line = cells.map((v) => v ?? '').join(',').replace(/,+$/, '')
|
|
226
|
+
if (line.trim()) lines.push(line)
|
|
227
|
+
}
|
|
228
|
+
return lines
|
|
229
|
+
}
|
|
230
|
+
|
|
86
231
|
/* -------------------------------------------------------------------- 动作 */
|
|
87
232
|
|
|
88
233
|
function findStock(state, name) {
|
|
@@ -105,6 +250,104 @@ const actions = {
|
|
|
105
250
|
return state
|
|
106
251
|
},
|
|
107
252
|
|
|
253
|
+
addColumn(state, payload) {
|
|
254
|
+
const label = str(payload?.label).trim()
|
|
255
|
+
if (!label) throw bad('列名不能为空')
|
|
256
|
+
if (state.columns.filter((c) => !c.builtin).length >= 10) throw bad('自定义列最多 10 个')
|
|
257
|
+
state.columns.push({ key: 'c' + Date.now(), label: label.slice(0, 20), builtin: false })
|
|
258
|
+
return state
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
deleteColumn(state, payload) {
|
|
262
|
+
const key = str(payload?.key).trim()
|
|
263
|
+
const column = state.columns.find((c) => c.key === key)
|
|
264
|
+
if (!column) throw bad('找不到这列')
|
|
265
|
+
if (column.builtin) throw bad('内置列不能删除')
|
|
266
|
+
state.columns = state.columns.filter((c) => c.key !== key)
|
|
267
|
+
state.stocks.forEach((s) => { if (s.fields) delete s.fields[key] })
|
|
268
|
+
return state
|
|
269
|
+
},
|
|
270
|
+
|
|
271
|
+
moveColumn(state, payload) {
|
|
272
|
+
const key = str(payload?.key).trim()
|
|
273
|
+
const dir = num(payload?.dir, 0) > 0 ? 1 : -1
|
|
274
|
+
const index = state.columns.findIndex((c) => c.key === key)
|
|
275
|
+
const next = index + dir
|
|
276
|
+
if (index < 0 || next < 0 || next >= state.columns.length) return state
|
|
277
|
+
const list = state.columns.slice()
|
|
278
|
+
const tmp = list[index]
|
|
279
|
+
list[index] = list[next]
|
|
280
|
+
list[next] = tmp
|
|
281
|
+
state.columns = list
|
|
282
|
+
return state
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
setVision(state, payload) {
|
|
286
|
+
state.vision = {
|
|
287
|
+
enabled: payload?.enabled === true,
|
|
288
|
+
baseUrl: str(payload?.baseUrl).slice(0, 300),
|
|
289
|
+
apiKey: str(payload?.apiKey).slice(0, 300),
|
|
290
|
+
model: str(payload?.model).slice(0, 100)
|
|
291
|
+
}
|
|
292
|
+
return state
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
setNote(state, payload) {
|
|
296
|
+
const stock = findStock(state, str(payload?.name).trim())
|
|
297
|
+
if (!stock) throw bad('找不到这只股票')
|
|
298
|
+
stock.note = str(payload?.note).slice(0, 500)
|
|
299
|
+
return state
|
|
300
|
+
},
|
|
301
|
+
|
|
302
|
+
setField(state, payload) {
|
|
303
|
+
const stock = findStock(state, str(payload?.name).trim())
|
|
304
|
+
const key = str(payload?.key).trim()
|
|
305
|
+
if (!stock) throw bad('找不到这只股票')
|
|
306
|
+
if (!state.columns.some((c) => c.key === key && !c.builtin)) throw bad('找不到这列')
|
|
307
|
+
stock.fields = stock.fields || {}
|
|
308
|
+
stock.fields[key] = str(payload?.value).slice(0, 200)
|
|
309
|
+
return state
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
archiveStock(state, payload) {
|
|
313
|
+
const stock = findStock(state, str(payload?.name).trim())
|
|
314
|
+
if (!stock) throw bad('找不到这只股票')
|
|
315
|
+
stock.archived = true
|
|
316
|
+
return state
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
unarchiveStock(state, payload) {
|
|
320
|
+
const stock = findStock(state, str(payload?.name).trim())
|
|
321
|
+
if (!stock) throw bad('找不到这只股票')
|
|
322
|
+
stock.archived = false
|
|
323
|
+
return state
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
saveFeeRule(state, payload) {
|
|
327
|
+
const rule = normalizeRule(payload?.rule)
|
|
328
|
+
if (!rule) throw bad('费率规则不完整')
|
|
329
|
+
const index = state.feeRules.findIndex((r) => r.id === rule.id)
|
|
330
|
+
if (index >= 0) state.feeRules[index] = rule
|
|
331
|
+
else state.feeRules.push(rule)
|
|
332
|
+
if (!state.feeRules.some((r) => r.id === state.activeFeeRule)) state.activeFeeRule = rule.id
|
|
333
|
+
return state
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
deleteFeeRule(state, payload) {
|
|
337
|
+
const id = str(payload?.id).trim()
|
|
338
|
+
if (state.feeRules.length <= 1) throw bad('至少保留一条费率规则')
|
|
339
|
+
state.feeRules = state.feeRules.filter((r) => r.id !== id)
|
|
340
|
+
if (!state.feeRules.some((r) => r.id === state.activeFeeRule)) state.activeFeeRule = state.feeRules[0].id
|
|
341
|
+
return state
|
|
342
|
+
},
|
|
343
|
+
|
|
344
|
+
setActiveFeeRule(state, payload) {
|
|
345
|
+
const id = str(payload?.id).trim()
|
|
346
|
+
if (!state.feeRules.some((r) => r.id === id)) throw bad('找不到这条费率规则')
|
|
347
|
+
state.activeFeeRule = id
|
|
348
|
+
return state
|
|
349
|
+
},
|
|
350
|
+
|
|
108
351
|
deleteStock(state, payload) {
|
|
109
352
|
const name = str(payload?.name).trim()
|
|
110
353
|
state.stocks = state.stocks.filter((s) => s.name !== name)
|
|
@@ -200,6 +443,51 @@ const actions = {
|
|
|
200
443
|
}
|
|
201
444
|
}
|
|
202
445
|
|
|
446
|
+
async function recognizeViaHarness(ctx, dataUrl, prompt) {
|
|
447
|
+
const llm = ctx.llm
|
|
448
|
+
if (!llm || typeof llm.stream !== 'function') return { ok: false, error: '当前 Harness 没有可用的模型服务' }
|
|
449
|
+
const sel = ctx.agentDefaultModel && typeof ctx.agentDefaultModel.currentSelection === 'function'
|
|
450
|
+
? ctx.agentDefaultModel.currentSelection()
|
|
451
|
+
: null
|
|
452
|
+
if (!sel || !sel.provider || !sel.model) return { ok: false, error: '当前会话没有默认模型' }
|
|
453
|
+
const m = /^data:(image\/[a-zA-Z0-9.+-]+);base64,([\s\S]+)$/.exec(dataUrl)
|
|
454
|
+
if (!m) return { ok: false, error: '图片格式不对' }
|
|
455
|
+
const messages = [{
|
|
456
|
+
role: 'user',
|
|
457
|
+
source: { kind: 'user' },
|
|
458
|
+
content: [
|
|
459
|
+
{ type: 'text', text: prompt },
|
|
460
|
+
{ type: 'image', attachment: { mediaType: m[1], data: m[2] } }
|
|
461
|
+
]
|
|
462
|
+
}]
|
|
463
|
+
const result = await llm.stream({
|
|
464
|
+
provider: sel.provider,
|
|
465
|
+
model: sel.model,
|
|
466
|
+
sessionId: 'dsh-positions',
|
|
467
|
+
temperature: 0,
|
|
468
|
+
messages
|
|
469
|
+
})
|
|
470
|
+
let text = ''
|
|
471
|
+
if (typeof result === 'string') text = result
|
|
472
|
+
else if (result && typeof result.text === 'string') text = result.text
|
|
473
|
+
else if (result && typeof result[Symbol.asyncIterator] === 'function') {
|
|
474
|
+
const seen = []
|
|
475
|
+
for await (const chunk of result) {
|
|
476
|
+
if (typeof chunk === 'string') text += chunk
|
|
477
|
+
else if (chunk) {
|
|
478
|
+
if (typeof chunk.text === 'string') text += chunk.text
|
|
479
|
+
else if (typeof chunk.delta === 'string' && String(chunk.type || '').includes('text')) text += chunk.delta
|
|
480
|
+
if (seen.length < 8) seen.push(String(chunk.type || (chunk.constructor && chunk.constructor.name) || '?'))
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (!text) text = 'DEBUG_CHUNKS:' + seen.join(',')
|
|
484
|
+
} else if (result && result.content) {
|
|
485
|
+
const parts = Array.isArray(result.content) ? result.content : [result.content]
|
|
486
|
+
text = parts.map((part) => (typeof part === 'string' ? part : (part && part.text) || '')).join('')
|
|
487
|
+
}
|
|
488
|
+
return { ok: true, text }
|
|
489
|
+
}
|
|
490
|
+
|
|
203
491
|
function bad(message) {
|
|
204
492
|
const error = new Error(message)
|
|
205
493
|
error.status = 400
|
|
@@ -295,6 +583,104 @@ export function apply(ctx, config) {
|
|
|
295
583
|
return
|
|
296
584
|
}
|
|
297
585
|
|
|
586
|
+
if (req.method === 'POST' && suffix === '/parse-xlsx') {
|
|
587
|
+
const body = await readJson(req)
|
|
588
|
+
const data = str(body?.data)
|
|
589
|
+
if (!data) { send(res, 400, { ok: false, error: '缺少文件内容' }); return }
|
|
590
|
+
const buf = Buffer.from(data, 'base64')
|
|
591
|
+
if (buf.length > 8 * 1024 * 1024) { send(res, 413, { ok: false, error: '文件过大(上限 8MB)' }); return }
|
|
592
|
+
const lines = parseXlsx(buf)
|
|
593
|
+
send(res, 200, { ok: true, lines })
|
|
594
|
+
return
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
if (req.method === 'POST' && suffix === '/recognize') {
|
|
598
|
+
const body = await readJson(req)
|
|
599
|
+
const vision = state.vision || {}
|
|
600
|
+
if (vision.enabled !== true) {
|
|
601
|
+
send(res, 400, { ok: false, error: '截图识别需要联网,请先打开「联网识别」开关' })
|
|
602
|
+
return
|
|
603
|
+
}
|
|
604
|
+
const image = str(body?.image)
|
|
605
|
+
if (!image.startsWith('data:image/')) { send(res, 400, { ok: false, error: '图片格式不对' }); return }
|
|
606
|
+
if (image.length > 6 * 1024 * 1024) { send(res, 413, { ok: false, error: '图片过大(上限 6MB)' }); return }
|
|
607
|
+
|
|
608
|
+
const prompt = [
|
|
609
|
+
'你是股票成交记录提取器。从图片中提取所有成交记录,输出严格 JSON 数组,不要任何解释。',
|
|
610
|
+
'每项字段:date(YYYY-MM-DD)、side(买入/卖出/分红)、price(数字)、qty(数字)、name(股票名称,没有就空字符串)。',
|
|
611
|
+
'如果图片里没有日期,date 用今天。无法识别的行直接跳过。'
|
|
612
|
+
].join('\n')
|
|
613
|
+
|
|
614
|
+
let rows = []
|
|
615
|
+
let source = 'harness'
|
|
616
|
+
let harnessError = ''
|
|
617
|
+
let harnessText = ''
|
|
618
|
+
try {
|
|
619
|
+
const viaHarness = await recognizeViaHarness(ctx, image, prompt)
|
|
620
|
+
if (viaHarness.ok) {
|
|
621
|
+
harnessText = String(viaHarness.text || '')
|
|
622
|
+
const match = harnessText.match(/\[[\s\S]*\]/)
|
|
623
|
+
if (match) {
|
|
624
|
+
try { rows = JSON.parse(match[0]) } catch { rows = [] }
|
|
625
|
+
}
|
|
626
|
+
if (!Array.isArray(rows)) rows = []
|
|
627
|
+
} else {
|
|
628
|
+
harnessError = viaHarness.error || 'Harness 模型不可用'
|
|
629
|
+
}
|
|
630
|
+
} catch (error) {
|
|
631
|
+
harnessError = error?.message || String(error)
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (rows.length === 0 && vision.apiKey && vision.baseUrl && vision.model) {
|
|
635
|
+
const endpoint = vision.baseUrl.replace(/\/+$/, '') + '/chat/completions'
|
|
636
|
+
const response = await fetch(endpoint, {
|
|
637
|
+
method: 'POST',
|
|
638
|
+
headers: { 'content-type': 'application/json', authorization: 'Bearer ' + vision.apiKey },
|
|
639
|
+
body: JSON.stringify({
|
|
640
|
+
model: vision.model,
|
|
641
|
+
temperature: 0,
|
|
642
|
+
messages: [
|
|
643
|
+
{ role: 'system', content: prompt },
|
|
644
|
+
{ role: 'user', content: [
|
|
645
|
+
{ type: 'text', text: '提取这些成交记录' },
|
|
646
|
+
{ type: 'image_url', image_url: { url: image } }
|
|
647
|
+
] }
|
|
648
|
+
]
|
|
649
|
+
})
|
|
650
|
+
})
|
|
651
|
+
const text = await response.text()
|
|
652
|
+
if (response.ok) {
|
|
653
|
+
try {
|
|
654
|
+
const json = JSON.parse(text)
|
|
655
|
+
const content = json.choices?.[0]?.message?.content ?? ''
|
|
656
|
+
const match = String(content).match(/\[[\s\S]*\]/)
|
|
657
|
+
if (match) rows = JSON.parse(match[0])
|
|
658
|
+
if (!Array.isArray(rows)) rows = []
|
|
659
|
+
source = 'endpoint'
|
|
660
|
+
} catch {
|
|
661
|
+
send(res, 502, { ok: false, error: '接口返回的不是 JSON' })
|
|
662
|
+
return
|
|
663
|
+
}
|
|
664
|
+
} else {
|
|
665
|
+
send(res, 502, { ok: false, error: '视觉接口返回 ' + response.status + ':' + text.slice(0, 160) })
|
|
666
|
+
return
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
if (rows.length === 0) {
|
|
671
|
+
send(res, 200, {
|
|
672
|
+
ok: false,
|
|
673
|
+
error: harnessError
|
|
674
|
+
? '识别失败:' + harnessError + '(可在「视觉接口设置」里指定一个支持图片的模型)'
|
|
675
|
+
: '没有识别出成交记录,换一张更清晰的截图试试',
|
|
676
|
+
raw: harnessText.slice(0, 400)
|
|
677
|
+
})
|
|
678
|
+
return
|
|
679
|
+
}
|
|
680
|
+
send(res, 200, { ok: true, source, rows: rows.slice(0, 200) })
|
|
681
|
+
return
|
|
682
|
+
}
|
|
683
|
+
|
|
298
684
|
if (req.method === 'POST' && suffix === '/action') {
|
|
299
685
|
const body = await readJson(req)
|
|
300
686
|
const type = str(body?.type)
|
package/package.json
CHANGED
package/screenshots.json
CHANGED
package/seed.js
CHANGED
|
@@ -6,6 +6,20 @@ export default {
|
|
|
6
6
|
version: 1,
|
|
7
7
|
updatedAt: '2027-03-15',
|
|
8
8
|
feeRate: 0.0003,
|
|
9
|
+
feeRules: [
|
|
10
|
+
{
|
|
11
|
+
id: 'eastmoney',
|
|
12
|
+
name: '东方财富(默认)',
|
|
13
|
+
commissionRate: 0.00025,
|
|
14
|
+
minCommission: 5,
|
|
15
|
+
stampDutyRate: 0.0005,
|
|
16
|
+
transferFeeRate: 0.00001,
|
|
17
|
+
otherFixed: 0,
|
|
18
|
+
note: '佣金万 2.5、最低 5 元;卖出印花税 0.05%;过户费 0.001%。可在「费率」里按你的券商改。',
|
|
19
|
+
image: ''
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
activeFeeRule: 'eastmoney',
|
|
9
23
|
tags: ['量比/换手/振幅', '偏态', '趋势4项', '股东数', '全球流动性'],
|
|
10
24
|
activeTags: [],
|
|
11
25
|
calc: {},
|
|
Binary file
|
|
Binary file
|
|
Binary file
|