dsh-lamplit 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/NOTICE.md +21 -0
- package/README.md +162 -2
- package/client.js +4972 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +14 -0
- package/index.js +61 -0
- package/lib/ai.js +88 -0
- package/lib/api.js +749 -0
- package/lib/epub.js +209 -0
- package/lib/http.js +93 -0
- package/lib/markdown.js +233 -0
- package/lib/notes.js +155 -0
- package/lib/pdf-text.js +1814 -0
- package/lib/pdf.js +62 -0
- package/lib/providers/fanqie.js +208 -0
- package/lib/providers/weread.js +144 -0
- package/lib/store.js +506 -0
- package/lib/text-util.js +172 -0
- package/lib/txt.js +24 -0
- package/lib/vendor/weread/LICENSE +27 -0
- package/lib/vendor/weread/crypto.js +495 -0
- package/lib/vendor/weread/qr-login.js +160 -0
- package/lib/vendor/weread/qrcode-generator.cjs +2297 -0
- package/lib/vendor/weread/reader.js +469 -0
- package/lib/vendor/weread/web.js +187 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +68 -4
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="灯下 Lamplit">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="glow" x1="0" y1="0" x2="0" y2="1">
|
|
4
|
+
<stop offset="0" stop-color="#f2c987" stop-opacity="0.55"/>
|
|
5
|
+
<stop offset="1" stop-color="#f2c987" stop-opacity="0"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect x="0" y="0" width="64" height="64" rx="14" fill="#16120f"/>
|
|
9
|
+
<path d="M32 10v7" stroke="#e8c07a" stroke-width="2" stroke-linecap="round"/>
|
|
10
|
+
<path d="M21 17h22l-4 8H25z" fill="#e8c07a"/>
|
|
11
|
+
<path d="M26 27h12l7 17H19z" fill="url(#glow)"/>
|
|
12
|
+
<path d="M19 45c4.4-2.4 8.8-2.4 13 0 4.2-2.4 8.6-2.4 13 0v7c-4.4-2.4-8.8-2.4-13 0-4.2-2.4-8.6-2.4-13 0z" fill="#f4e8d2"/>
|
|
13
|
+
<path d="M32 45v7" stroke="#c9603c" stroke-width="1.6" stroke-linecap="round"/>
|
|
14
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 灯下 Lamplit — Host half.
|
|
3
|
+
*
|
|
4
|
+
* 自带一个小型 HTTP 面(`/lamplit/api/*`),Client 用同源 fetch 读写:
|
|
5
|
+
* 书架、导入(PDF / Markdown / TXT / EPUB)、阅读进度、笔记、划词 AI,
|
|
6
|
+
* 以及两个内容来源(微信读书、番茄小说)的登录与同步。
|
|
7
|
+
*
|
|
8
|
+
* 数据落在 `~/.dsh/lamplit/`,不出本机。
|
|
9
|
+
*
|
|
10
|
+
* 用 handler 路由而不是 resource 路由:resource 只接 GET/HEAD,
|
|
11
|
+
* 而这套接口要 POST/DELETE。
|
|
12
|
+
*/
|
|
13
|
+
import { createApi } from './lib/api.js'
|
|
14
|
+
|
|
15
|
+
export const name = 'lamplit'
|
|
16
|
+
|
|
17
|
+
/** `webServer` 是唯一的硬依赖:没有它就无从服务 Client。 */
|
|
18
|
+
export const inject = ['webServer']
|
|
19
|
+
|
|
20
|
+
function toRequest(req, body) {
|
|
21
|
+
const headers = new Headers()
|
|
22
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
23
|
+
if (typeof value === 'string') headers.set(key, value)
|
|
24
|
+
else if (Array.isArray(value)) headers.set(key, value.join(', '))
|
|
25
|
+
}
|
|
26
|
+
const method = (req.method || 'GET').toUpperCase()
|
|
27
|
+
const url = `http://127.0.0.1${req.url || '/'}`
|
|
28
|
+
const init = { method, headers }
|
|
29
|
+
if (method !== 'GET' && method !== 'HEAD' && body.byteLength > 0) init.body = body
|
|
30
|
+
return new Request(url, init)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function apply(ctx, config) {
|
|
34
|
+
const api = createApi({ ctx, config: config ?? {} })
|
|
35
|
+
|
|
36
|
+
ctx.effect(() =>
|
|
37
|
+
ctx.webServer.register({
|
|
38
|
+
kind: 'prefix',
|
|
39
|
+
path: '/lamplit',
|
|
40
|
+
handler: async (req, res) => {
|
|
41
|
+
try {
|
|
42
|
+
const chunks = []
|
|
43
|
+
for await (const chunk of req) chunks.push(chunk)
|
|
44
|
+
const response = await api.handle(toRequest(req, Buffer.concat(chunks)))
|
|
45
|
+
res.statusCode = response.status
|
|
46
|
+
response.headers.forEach((value, key) => res.setHeader(key, value))
|
|
47
|
+
res.end(Buffer.from(await response.arrayBuffer()))
|
|
48
|
+
} catch (error) {
|
|
49
|
+
ctx.logger?.warn?.('[lamplit] 请求失败:%s', error?.stack ?? String(error))
|
|
50
|
+
if (!res.headersSent) {
|
|
51
|
+
res.statusCode = 500
|
|
52
|
+
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
53
|
+
}
|
|
54
|
+
res.end(JSON.stringify({ ok: false, code: 'INTERNAL', error: error?.message ?? String(error) }))
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
ctx.logger?.info?.('[lamplit] serving /lamplit (data: %s)', api.storageDir)
|
|
61
|
+
}
|
package/lib/ai.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 划词 AI:把选中的一段文字交给当前默认模型,回一段解释或背景补充。
|
|
3
|
+
*
|
|
4
|
+
* 走宿主已有的 `llm` 服务(有适配器、有计费、有重试),不自己造 HTTP 客户端,
|
|
5
|
+
* 所以「用哪个模型」跟着用户在 Harness 里选的默认模型走。
|
|
6
|
+
*/
|
|
7
|
+
import { ProviderError } from './http.js'
|
|
8
|
+
|
|
9
|
+
const SYSTEM = [
|
|
10
|
+
'你是「灯下」阅读器里的读书助手。读者正读一本书并选中了一段文字。',
|
|
11
|
+
'用中文回答,直接、具体、有信息量;不要复述原文,不要客套,不要加「这段话主要讲了」这种壳。',
|
|
12
|
+
'不确定的地方就说「不确定」,不要编造出处。',
|
|
13
|
+
].join('\n')
|
|
14
|
+
|
|
15
|
+
function instruction(mode) {
|
|
16
|
+
if (mode === 'expand') {
|
|
17
|
+
return '请补充这段的背景:典故或出处、相关人物与事件、它可能引用的文本、以及值得延伸阅读的方向。300 字以内。'
|
|
18
|
+
}
|
|
19
|
+
return '请解释这段:它在说什么、关键概念指什么、有没有隐含前提或言外之意。300 字以内。'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createAi({ ctx }) {
|
|
23
|
+
function selection() {
|
|
24
|
+
const service = ctx.get('agentDefaultModel')
|
|
25
|
+
const current = service?.currentSelection?.()
|
|
26
|
+
if (current === null || typeof current !== 'object' || !current.provider || !current.model) {
|
|
27
|
+
throw new ProviderError('AI', '还没有可用的默认模型:先在 Harness 里选一个模型。')
|
|
28
|
+
}
|
|
29
|
+
return current
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
async ask(input) {
|
|
34
|
+
const llm = ctx.get('llm')
|
|
35
|
+
if (llm === undefined) throw new ProviderError('AI', '当前 profile 没有启用模型服务。')
|
|
36
|
+
const text = String(input.text ?? '').trim()
|
|
37
|
+
if (text === '') throw new ProviderError('BADREQ', '没有选中任何文字。')
|
|
38
|
+
const clipped = text.slice(0, 4000)
|
|
39
|
+
const mode = input.mode === 'expand' ? 'expand' : 'explain'
|
|
40
|
+
const { provider, model } = selection()
|
|
41
|
+
|
|
42
|
+
const context = [input.bookTitle, input.chapterTitle].filter((item) => typeof item === 'string' && item.trim() !== '')
|
|
43
|
+
const prompt = [
|
|
44
|
+
context.length > 0 ? `书名:${context[0]}` : '',
|
|
45
|
+
context.length > 1 ? `章节:${context[1]}` : '',
|
|
46
|
+
'',
|
|
47
|
+
'原文:',
|
|
48
|
+
clipped,
|
|
49
|
+
'',
|
|
50
|
+
instruction(mode),
|
|
51
|
+
]
|
|
52
|
+
.filter((line, index) => index > 2 || line !== '')
|
|
53
|
+
.join('\n')
|
|
54
|
+
|
|
55
|
+
const messages = [{ role: 'user', content: [{ type: 'text', text: prompt }] }]
|
|
56
|
+
const controller = new AbortController()
|
|
57
|
+
const timer = setTimeout(() => controller.abort(), 120_000)
|
|
58
|
+
|
|
59
|
+
let answer = ''
|
|
60
|
+
let failure = null
|
|
61
|
+
try {
|
|
62
|
+
const stream = llm.stream({
|
|
63
|
+
provider,
|
|
64
|
+
model,
|
|
65
|
+
system: SYSTEM,
|
|
66
|
+
messages,
|
|
67
|
+
maxTokens: 1200,
|
|
68
|
+
temperature: 0.4,
|
|
69
|
+
signal: controller.signal,
|
|
70
|
+
})
|
|
71
|
+
for await (const chunk of stream) {
|
|
72
|
+
if (chunk?.type === 'text-delta' && typeof chunk.text === 'string') answer += chunk.text
|
|
73
|
+
if (chunk?.type === 'finish' && chunk.reason?.kind === 'error') {
|
|
74
|
+
failure = chunk.reason.failure?.message ?? '模型调用失败。'
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} catch (error) {
|
|
78
|
+
failure = error?.message ?? String(error)
|
|
79
|
+
} finally {
|
|
80
|
+
clearTimeout(timer)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (failure !== null && answer.trim() === '') throw new ProviderError('AI', failure)
|
|
84
|
+
if (answer.trim() === '') throw new ProviderError('AI', '模型没有返回内容。')
|
|
85
|
+
return { answer: answer.trim(), provider, model }
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
}
|