@restregular/dsh-memory 0.34.0 → 0.35.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/CHANGELOG.md +25 -0
- package/README.md +37 -2
- package/lib/client.js +381 -15
- package/lib/index.js +6 -1
- package/lib/inject/apply.js +43 -12
- package/lib/panel.js +1717 -1616
- package/lib/provider/apply.js +54 -0
- package/lib/provider/approval-audit.js +1 -1
- package/lib/provider/embed-install.js +132 -9
- package/lib/provider/hooks.js +597 -0
- package/lib/provider/index.js +292 -3
- package/lib/skills.js +150 -0
- package/lib/tools/apply.js +6 -1
- package/lib/tools/index.js +436 -1
- package/lib/tools/render.js +78 -1
- package/package.json +2 -1
- package/skills/memory-hook-scripts/SKILL.md +142 -0
- package/skills/memory-hook-scripts/references/examples.md +159 -0
package/lib/provider/apply.js
CHANGED
|
@@ -380,6 +380,60 @@ export class CordisMemoryService extends Service {
|
|
|
380
380
|
return this.impl.injectSettings()
|
|
381
381
|
}
|
|
382
382
|
|
|
383
|
+
// ---------------------------------------------------------------- 记忆钩子(0.35.0)
|
|
384
|
+
/**
|
|
385
|
+
* 钩子能力必须在这里**逐个转发**:面板与工具拿到的都是本包装实例,
|
|
386
|
+
* 少转一个方法就是 undefined(0.32.0 的归档/屏蔽就整体这样失效过,
|
|
387
|
+
* 而单测直接 new LocalMemoryService 完全看不见)。
|
|
388
|
+
*/
|
|
389
|
+
hookSettings() {
|
|
390
|
+
return this.impl.hookSettings()
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
updateHookSettings(patch ) {
|
|
394
|
+
return this.impl.updateHookSettings(patch)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
listHooks() {
|
|
398
|
+
return this.impl.listHooks()
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
getHook(id ) {
|
|
402
|
+
return this.impl.getHook(id)
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
saveHook(input ) {
|
|
406
|
+
return this.impl.saveHook(input)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
deleteHook(id ) {
|
|
410
|
+
return this.impl.deleteHook(id)
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
approveHook(id ) {
|
|
414
|
+
return this.impl.approveHook(id)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
hookPendingApproval(id ) {
|
|
418
|
+
return this.impl.hookPendingApproval(id)
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
requestHookApproval(
|
|
422
|
+
agent ,
|
|
423
|
+
id ,
|
|
424
|
+
options ,
|
|
425
|
+
) {
|
|
426
|
+
return this.impl.requestHookApproval(agent, id, options)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
runHooks(phase , input ) {
|
|
430
|
+
return this.impl.runHooks(phase, input)
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
parseForInject(records , sessionId ) {
|
|
434
|
+
return this.impl.parseForInject(records, sessionId)
|
|
435
|
+
}
|
|
436
|
+
|
|
383
437
|
/** 面板改设置:落盘后广播,注入半边下一步就能按新设置跑。 */
|
|
384
438
|
updateInjectSettings(patch ) {
|
|
385
439
|
const next = this.impl.updateInjectSettings(patch)
|
|
@@ -53,6 +53,20 @@ export const LOG_FILE_NAME = 'llama-server.log'
|
|
|
53
53
|
const RELEASES_API = 'https://api.github.com/repos/ggml-org/llama.cpp/releases?per_page=20'
|
|
54
54
|
/** GitHub 要求带 UA;不带会被 403。 */
|
|
55
55
|
const USER_AGENT = 'dsh-memory-local-installer'
|
|
56
|
+
/**
|
|
57
|
+
* 认证用的环境变量,按序取第一个非空的(GitHub 官方是 GITHUB_TOKEN,gh CLI 是 GH_TOKEN)。
|
|
58
|
+
* 有 token 时额度 5000 次/小时;没有就是匿名 60 次/小时,而且是**按出口 IP 算**,
|
|
59
|
+
* 公司/校园网、CI 里几乎必然被别的进程吃掉 —— issue #5 就是撞在这一条上。
|
|
60
|
+
*/
|
|
61
|
+
const TOKEN_ENV_VARS = ['GITHUB_TOKEN', 'GH_TOKEN']
|
|
62
|
+
/**
|
|
63
|
+
* 发行版列表的落盘缓存(放在 `<root>/llamacpp/` 里:这个目录本来就是安装器自己的,
|
|
64
|
+
* 卸载时会一起清掉)。作用有两个:
|
|
65
|
+
* ① 一次安装内 release 列表要查两次(主包 + CUDA 伴生包),不该发两个请求;
|
|
66
|
+
* ② 匿名额度被用完时还能拿上一次的结果把安装走完 —— 限流不该让用户卡住。
|
|
67
|
+
*/
|
|
68
|
+
const RELEASES_CACHE_FILE = '.releases-cache.json'
|
|
69
|
+
const RELEASES_CACHE_TTL_MS = 15 * 60_000
|
|
56
70
|
|
|
57
71
|
/**
|
|
58
72
|
* 模型下载源,按顺序回退(同一份 Q4_K_M,仓库都是 gpustack/bge-m3-GGUF)。
|
|
@@ -150,6 +164,11 @@ export const MODEL_SIZE_BYTES = 437_778_496
|
|
|
150
164
|
|
|
151
165
|
|
|
152
166
|
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
153
172
|
|
|
154
173
|
|
|
155
174
|
/** zip 中央目录里一条记录(只保留解压需要的字段)。 */
|
|
@@ -704,6 +723,33 @@ function urlHost(url ) {
|
|
|
704
723
|
}
|
|
705
724
|
}
|
|
706
725
|
|
|
726
|
+
/**
|
|
727
|
+
* 限流错误的完整文案:说清「是谁的额度用完了、什么时候恢复、怎么一次解决」。
|
|
728
|
+
*
|
|
729
|
+
* 旧文案只有一句「未认证请求每小时 60 次,请稍后重试」,实测(issue #5)把人卡死
|
|
730
|
+
* 在这里:用户不知道要等多久,也不知道配个 token 就能绕过 —— 而匿名额度是按出口
|
|
731
|
+
* IP 算的,共用网络下重试几乎永远不会成功。
|
|
732
|
+
*/
|
|
733
|
+
function rateLimitMessage(res , authenticated ) {
|
|
734
|
+
const reset = Number.parseInt(res.headers.get('x-ratelimit-reset') ?? '', 10)
|
|
735
|
+
const when = Number.isFinite(reset) && reset > 0 ? `,额度将在 ${formatEpoch(reset)} 恢复` : ''
|
|
736
|
+
if (authenticated) {
|
|
737
|
+
return `GitHub API 限流(已用 token,额度 5000 次/小时)${when};请等待恢复后重试`
|
|
738
|
+
}
|
|
739
|
+
const retryAfter = Number.parseInt(res.headers.get('retry-after') ?? '', 10)
|
|
740
|
+
const after = Number.isFinite(retryAfter) && retryAfter > 0 ? `,建议 ${retryAfter} 秒后再试` : ''
|
|
741
|
+
return `GitHub API 限流(未认证请求按出口 IP 每小时 60 次,很可能已被同网络的其他程序用完)${when}${after};`
|
|
742
|
+
+ '解决办法:设置环境变量 GITHUB_TOKEN(或 GH_TOKEN)为任意 GitHub token 后重试,额度升到 5000 次/小时'
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/** 把 x-ratelimit-reset 的 epoch 秒写成本地时间(用户要看的是「几点」。 */
|
|
746
|
+
function formatEpoch(seconds ) {
|
|
747
|
+
const date = new Date(seconds * 1000)
|
|
748
|
+
if (Number.isNaN(date.getTime())) return '稍后'
|
|
749
|
+
const pad = (n ) => String(n).padStart(2, '0')
|
|
750
|
+
return `${pad(date.getHours())}:${pad(date.getMinutes())}`
|
|
751
|
+
}
|
|
752
|
+
|
|
707
753
|
/** 等一个背压 drain;顺手把流错误也变成 reject,免得卡死。 */
|
|
708
754
|
function waitDrain(stream ) {
|
|
709
755
|
return new Promise((resolve, reject) => {
|
|
@@ -751,6 +797,8 @@ export class VectorInstaller {
|
|
|
751
797
|
controller = null
|
|
752
798
|
/** 当前正在写的 .part 与它的流(取消时要立刻销毁,否则 fd 悬着)。 */
|
|
753
799
|
sink = null
|
|
800
|
+
/** 本轮安装的发行版列表:主包与 CUDA 伴生包共用,避免同一轮发两次相同请求。 */
|
|
801
|
+
releasesCache = null
|
|
754
802
|
|
|
755
803
|
constructor(opts ) {
|
|
756
804
|
this.opts = opts
|
|
@@ -826,6 +874,8 @@ export class VectorInstaller {
|
|
|
826
874
|
...(this.opts.modelSources ? { sources: this.opts.modelSources } : {}),
|
|
827
875
|
}
|
|
828
876
|
const flavor = this.opts.flavor ?? 'cpu'
|
|
877
|
+
// 上一轮(例如用户换过风味后重装)的结果不能带进本轮:release 列表是易失数据。
|
|
878
|
+
this.releasesCache = null
|
|
829
879
|
const paths = installPaths(root, model.file)
|
|
830
880
|
try {
|
|
831
881
|
this.setPhase(gen, 'preparing', `准备安装目录 ${root}`)
|
|
@@ -935,8 +985,7 @@ export class VectorInstaller {
|
|
|
935
985
|
// prerelease:true,而 latest 只认正式版,现在返回的是一个与本项目无关的
|
|
936
986
|
// v0.6.0(资产只有 nightly-tag.txt)——照它匹配必然失败。正确做法是拉倒序
|
|
937
987
|
// 列表,取第一个含匹配资产的 release(prerelease 与否无关)。
|
|
938
|
-
const releases = await this.
|
|
939
|
-
if (!Array.isArray(releases)) throw new Error('llama.cpp 发行版接口返回的数据格式异常')
|
|
988
|
+
const releases = await this.releases(gen)
|
|
940
989
|
const wanted = flavor === 'metal' ? 'cpu' : flavor
|
|
941
990
|
const hit = findAssetInReleases(releases, pattern)
|
|
942
991
|
if (hit !== null) return hit
|
|
@@ -961,8 +1010,7 @@ export class VectorInstaller {
|
|
|
961
1010
|
) {
|
|
962
1011
|
const pattern = runtimeCompanionPattern(platform, arch, flavor)
|
|
963
1012
|
if (pattern === null) return null
|
|
964
|
-
const releases = await this.
|
|
965
|
-
if (!Array.isArray(releases)) return null
|
|
1013
|
+
const releases = await this.releases(gen)
|
|
966
1014
|
const hit = findAssetInReleases(releases, pattern)
|
|
967
1015
|
if (hit === null) {
|
|
968
1016
|
this.report(gen, { message: `没有找到 CUDA 运行库(匹配 ${String(pattern)}):llama-server 可能起不来,可改用 Vulkan 或 CPU 运行时` })
|
|
@@ -1074,23 +1122,49 @@ export class VectorInstaller {
|
|
|
1074
1122
|
return received
|
|
1075
1123
|
}
|
|
1076
1124
|
|
|
1077
|
-
/**
|
|
1125
|
+
/**
|
|
1126
|
+
* 发一个带 UA 的请求;网络层错误翻译成人话,但 AbortError 原样上抛。
|
|
1127
|
+
*
|
|
1128
|
+
* 只有 api.github.com 才带 Authorization:别的下载源(hf-mirror / modelscope)
|
|
1129
|
+
* 收到一个 GitHub token 既没用又等于把凭据送给第三方。
|
|
1130
|
+
*/
|
|
1078
1131
|
async request(url , accept ) {
|
|
1079
1132
|
const impl = this.opts.fetchImpl ?? fetch
|
|
1080
1133
|
const signal = this.requestSignal()
|
|
1134
|
+
const headers = { accept, 'user-agent': USER_AGENT }
|
|
1135
|
+
if (urlHost(url) === 'api.github.com') {
|
|
1136
|
+
const token = this.token()
|
|
1137
|
+
if (token !== null) headers.authorization = `Bearer ${token}`
|
|
1138
|
+
}
|
|
1081
1139
|
try {
|
|
1082
|
-
return await impl(url, { headers
|
|
1140
|
+
return await impl(url, { headers, signal, redirect: 'follow' })
|
|
1083
1141
|
} catch (error) {
|
|
1084
1142
|
if (isAbortError(error)) throw error
|
|
1085
1143
|
throw new Error(`网络请求失败(${urlHost(url)}):${describeError(error)}`)
|
|
1086
1144
|
}
|
|
1087
1145
|
}
|
|
1088
1146
|
|
|
1147
|
+
/** 本次安装用的 GitHub token:显式注入优先(给了就只认它,测试可传 '' 强制匿名),其次环境变量。 */
|
|
1148
|
+
token() {
|
|
1149
|
+
if (this.opts.token !== undefined) {
|
|
1150
|
+
const explicit = this.opts.token.trim()
|
|
1151
|
+
return explicit === '' ? null : explicit
|
|
1152
|
+
}
|
|
1153
|
+
for (const name of TOKEN_ENV_VARS) {
|
|
1154
|
+
const value = process.env[name]?.trim()
|
|
1155
|
+
if (value !== undefined && value !== '') return value
|
|
1156
|
+
}
|
|
1157
|
+
return null
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1089
1160
|
async fetchJson(url , what ) {
|
|
1090
1161
|
const res = await this.request(url, 'application/vnd.github+json')
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1162
|
+
// 判定「是不是限流」只看响应头,不看状态码:403 既可能是额度耗尽,也可能是
|
|
1163
|
+
// 代理/区域策略拒绝、secondary rate limit、甚至 token 权限不足。旧版一律报
|
|
1164
|
+
// 「未认证请求每小时 60 次」,让拿着 token 的用户照着这句话重试一辈子。
|
|
1165
|
+
const remaining = res.headers.get('x-ratelimit-remaining')
|
|
1166
|
+
const limited = res.status === 429 || (res.status === 403 && (remaining === '0' || res.headers.get('retry-after') !== null))
|
|
1167
|
+
if (limited) throw new Error(rateLimitMessage(res, this.token() !== null))
|
|
1094
1168
|
if (!res.ok) throw new Error(`${what}失败:HTTP ${res.status}(${url})`)
|
|
1095
1169
|
try {
|
|
1096
1170
|
return await res.json()
|
|
@@ -1099,6 +1173,55 @@ export class VectorInstaller {
|
|
|
1099
1173
|
}
|
|
1100
1174
|
}
|
|
1101
1175
|
|
|
1176
|
+
/**
|
|
1177
|
+
* 查发行版列表,带两层优化(都是 issue #5 的直接推论):
|
|
1178
|
+
* ① 一次安装内只查一次 —— 主包与 CUDA 伴生包共用同一份结果;
|
|
1179
|
+
* ② 查询失败时退回磁盘上的近期缓存;查到了也顺手写一份。
|
|
1180
|
+
*
|
|
1181
|
+
* 缓存只是**降级通道**:它绝不覆盖一份成功的实时结果,过期(15 分钟)或读不出来
|
|
1182
|
+
* 就当没有。命中缓存时会在进度消息里说明,用户不会以为自己拿到了最新构建。
|
|
1183
|
+
*/
|
|
1184
|
+
async releases(gen ) {
|
|
1185
|
+
if (this.releasesCache !== null) return this.releasesCache
|
|
1186
|
+
const cachePath = join(installPaths(this.opts.root).dir, RELEASES_CACHE_FILE)
|
|
1187
|
+
try {
|
|
1188
|
+
const list = await this.fetchJson(RELEASES_API, '查询 llama.cpp 发行版')
|
|
1189
|
+
if (!Array.isArray(list)) throw new Error('llama.cpp 发行版接口返回的数据格式异常')
|
|
1190
|
+
this.releasesCache = list
|
|
1191
|
+
this.writeReleasesCache(cachePath, list)
|
|
1192
|
+
return list
|
|
1193
|
+
} catch (error) {
|
|
1194
|
+
const cached = this.readReleasesCache(cachePath)
|
|
1195
|
+
if (cached === null) throw error
|
|
1196
|
+
this.releasesCache = cached
|
|
1197
|
+
this.report(gen, {
|
|
1198
|
+
message: `查询 llama.cpp 发行版失败(${describeError(error)}),改用 ${Math.round((Date.now() - cached.at) / 60_000)} 分钟前的缓存结果继续安装`,
|
|
1199
|
+
})
|
|
1200
|
+
return cached.releases
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
writeReleasesCache(path , releases ) {
|
|
1205
|
+
try {
|
|
1206
|
+
ensureDir(dirname(path), '创建运行时目录')
|
|
1207
|
+
writeFileSync(path, JSON.stringify({ at: Date.now(), releases }))
|
|
1208
|
+
} catch {
|
|
1209
|
+
// 缓存写不进去不影响安装(只影响下次限流时能不能兜底)。
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
readReleasesCache(path ) {
|
|
1214
|
+
try {
|
|
1215
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'))
|
|
1216
|
+
if (typeof raw.at !== 'number' || !Array.isArray(raw.releases)) return null
|
|
1217
|
+
if (raw.releases.length === 0) return null
|
|
1218
|
+
if (!Number.isFinite(raw.at) || Date.now() - raw.at > RELEASES_CACHE_TTL_MS) return null
|
|
1219
|
+
return { at: raw.at, releases: raw.releases }
|
|
1220
|
+
} catch {
|
|
1221
|
+
return null
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1102
1225
|
/** 自带 controller + 调用方 signal 的合并信号。 */
|
|
1103
1226
|
requestSignal() {
|
|
1104
1227
|
const external = this.opts.signal
|