@restregular/dsh-memory 0.19.2 → 0.21.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 +32 -0
- package/README.md +19 -11
- package/lib/client.js +5292 -5229
- package/lib/inject/apply.js +34 -4
- package/lib/inject/policy.js +2 -0
- package/lib/inject/session-state-store.js +3 -1
- package/lib/inject/state.js +51 -3
- package/lib/panel.js +17 -2
- package/lib/provider/apply.js +9 -4
- package/lib/provider/index.js +178 -43
- package/lib/tools/apply.js +45 -5
- package/lib/tools/index.js +396 -67
- package/lib/tools/render.js +76 -6
- package/package.json +1 -1
package/lib/tools/render.js
CHANGED
|
@@ -4,13 +4,26 @@
|
|
|
4
4
|
*
|
|
5
5
|
* 0.16.0 起 memory_search 只给“印象”(id / 作用域 / 类型 / 标签 / 短摘要),
|
|
6
6
|
* 正文走 memory_get;memory_get 的 fields 决定渲染哪些元数据。
|
|
7
|
+
*
|
|
8
|
+
* 0.20.0:范围外命中被挡住时,把**真实原因**(用户拒绝 / 审批超时 / 无法审批)
|
|
9
|
+
* 写进渲染文本。绝对限制下工具根本不会带 withheld(视为不存在),所以这里
|
|
10
|
+
* 不需要也不应该提"被挡住"。
|
|
7
11
|
*/
|
|
8
|
-
|
|
12
|
+
|
|
9
13
|
|
|
10
14
|
/** 把 scope 键渲染成展示文案(宿主能查到会话标题时用它,否则用短 id)。 */
|
|
11
15
|
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
/** 有内容没返回时的原因(absolute 模式不会走到这里)。 */
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 上下文去重的提示语(0.21.0)。memory_search / memory_list 里已经出现过的条目
|
|
22
|
+
* 只回 id、不再回摘要/正文;这条提示告诉模型为什么少了东西、以及怎么取全文。
|
|
23
|
+
*/
|
|
24
|
+
export const DEDUPE_NOTE = '本次请求部分记忆摘要因上下文去重机制被省略,若要获取完整内容请使用 memory_get'
|
|
25
|
+
|
|
26
|
+
export const shortId = (id ) => id.slice(0, 8)
|
|
14
27
|
|
|
15
28
|
const day = (value ) =>
|
|
16
29
|
typeof value === 'number' && Number.isFinite(value) ? new Date(value).toISOString().slice(0, 10) : ''
|
|
@@ -18,14 +31,67 @@ const day = (value ) =>
|
|
|
18
31
|
const joinList = (value ) =>
|
|
19
32
|
Array.isArray(value) ? value.map(String).filter((v) => v !== '').join(', ') : ''
|
|
20
33
|
|
|
34
|
+
/**
|
|
35
|
+
* 范围外内容为什么没返回——给模型看的真实原因。
|
|
36
|
+
* 空字符串 = 没有需要说明的(没有 withheld,或 absolute 模式下不告知)。
|
|
37
|
+
*/
|
|
38
|
+
export function renderWithheldNote(withheld ) {
|
|
39
|
+
if (withheld === undefined || withheld.count <= 0) return ''
|
|
40
|
+
const count = withheld.count
|
|
41
|
+
switch (withheld.reason) {
|
|
42
|
+
case 'rejected':
|
|
43
|
+
return `另有 ${count} 条记忆在本会话的可见域外,用户拒绝了本次访问(限制等级:普通限制),未返回。如确实需要,请让用户调整「记忆范围」后重试。`
|
|
44
|
+
case 'timeout':
|
|
45
|
+
return `另有 ${count} 条记忆在本会话的可见域外,审批在 5 分钟内没有得到答复,已按拒绝处理,未返回。如确实需要,请让用户调整「记忆范围」后重试。`
|
|
46
|
+
case 'unavailable':
|
|
47
|
+
return `另有 ${count} 条记忆在本会话的可见域外,当前环境无法发起审批,未返回。`
|
|
48
|
+
case 'partial':
|
|
49
|
+
return `另有 ${count} 条记忆在本会话的可见域外,用户在本次审批中只允许了其余条目(限制等级:普通限制),未返回。如确实需要,请让用户调整「记忆范围」后重试。`
|
|
50
|
+
default:
|
|
51
|
+
return ''
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
21
55
|
/** 「[得分] id (作用域/类型 · 标签) 摘要」;没有任何一条命中时给明确空结果。 */
|
|
22
56
|
export function renderSearchResult(value , label ) {
|
|
23
|
-
|
|
24
|
-
|
|
57
|
+
const note = renderWithheldNote(value.withheld)
|
|
58
|
+
if (value.records.length === 0) {
|
|
59
|
+
if (note === '') return '没有找到相关记忆。'
|
|
60
|
+
return `没有找到可见范围内的相关记忆。\n${note}`
|
|
61
|
+
}
|
|
62
|
+
// 已出现过的条目(上下文去重)只回 id:不带作用域/类型/标签/摘要。
|
|
63
|
+
let omitted = 0
|
|
64
|
+
const body = value.records.map((hit) => {
|
|
65
|
+
if (hit.deduped === true) {
|
|
66
|
+
omitted += 1
|
|
67
|
+
return shortId(hit.id)
|
|
68
|
+
}
|
|
25
69
|
const tags = joinList(hit.tags)
|
|
26
70
|
const meta = `${label(hit.scope)}/${hit.kind}${tags === '' ? '' : ` · ${tags}`}`
|
|
27
71
|
return `[${hit.score.toFixed(3)}] ${shortId(hit.id)} (${meta}) ${hit.summary}`
|
|
28
72
|
}).join('\n')
|
|
73
|
+
const tail = [omitted > 0 ? DEDUPE_NOTE : '', note].filter((part) => part !== '').join('\n\n')
|
|
74
|
+
return tail === '' ? body : `${body}\n\n${tail}`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* memory_list 的渲染:每条「(作用域/类型) 正文」;上下文去重过的条目只给 id。
|
|
79
|
+
* `scopeStats` 的尾部摘要由 apply.ts 追加(那里能查会话标题)。
|
|
80
|
+
*/
|
|
81
|
+
export function renderListResult(value , label ) {
|
|
82
|
+
let omitted = 0
|
|
83
|
+
const body = value.records.length === 0
|
|
84
|
+
? '该作用域还没有记忆。'
|
|
85
|
+
: value.records.map((record) => {
|
|
86
|
+
if (record.deduped === true) {
|
|
87
|
+
omitted += 1
|
|
88
|
+
return shortId(record.id)
|
|
89
|
+
}
|
|
90
|
+
return `(${label(record.scope)}/${record.kind}) ${record.content}`
|
|
91
|
+
}).join('\n')
|
|
92
|
+
const note = renderWithheldNote(value.withheld)
|
|
93
|
+
const tail = [omitted > 0 ? DEDUPE_NOTE : '', note].filter((part) => part !== '').join('\n\n')
|
|
94
|
+
return tail === '' ? body : `${body}\n\n${tail}`
|
|
29
95
|
}
|
|
30
96
|
|
|
31
97
|
/**
|
|
@@ -74,8 +140,12 @@ export function renderGetResult(value , label ) {
|
|
|
74
140
|
return text === '' ? head : `${head}\n${text}`
|
|
75
141
|
}).join('\n\n')
|
|
76
142
|
const missing = value.missing.length > 0 ? `未找到:${value.missing.join(', ')}` : ''
|
|
77
|
-
|
|
78
|
-
|
|
143
|
+
// 被可见域挡住的那几条:absolute 模式下 provider 不会给 withheld(视为不存在),
|
|
144
|
+
// 其余情况把真实原因(拒绝 / 超时 / 无法审批)告诉模型。
|
|
145
|
+
const note = renderWithheldNote(value.withheld)
|
|
146
|
+
const tail = [missing, note].filter((part) => part !== '').join('\n\n')
|
|
147
|
+
if (body === '') return tail === '' ? '没有找到记忆。' : tail
|
|
148
|
+
return tail === '' ? body : `${body}\n\n${tail}`
|
|
79
149
|
}
|
|
80
150
|
|
|
81
151
|
|