@whiteplusms/dsh-redis 0.0.0-stage → 0.1.3
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 +184 -2
- package/README.zh.md +185 -0
- package/cordis.patch.yml +4 -0
- package/icon.svg +1 -0
- package/lib/client/index.d.ts +4 -0
- package/lib/client.js +7684 -0
- package/lib/connection-errors.d.ts +66 -0
- package/lib/connection-errors.js +21 -0
- package/lib/domain.d.ts +451 -0
- package/lib/domain.js +20 -0
- package/lib/host/ai-answer.d.ts +7 -0
- package/lib/host/ai-answer.js +132 -0
- package/lib/host/ai-budget.d.ts +43 -0
- package/lib/host/ai-budget.js +78 -0
- package/lib/host/ai-connection-tools.d.ts +8 -0
- package/lib/host/ai-connection-tools.js +49 -0
- package/lib/host/ai-hash-filter.d.ts +13 -0
- package/lib/host/ai-hash-filter.js +97 -0
- package/lib/host/ai-hash-pages.d.ts +15 -0
- package/lib/host/ai-hash-pages.js +42 -0
- package/lib/host/ai-key-pages.d.ts +14 -0
- package/lib/host/ai-key-pages.js +27 -0
- package/lib/host/ai-locate-tool.d.ts +6 -0
- package/lib/host/ai-locate-tool.js +234 -0
- package/lib/host/ai-match-tools.d.ts +7 -0
- package/lib/host/ai-match-tools.js +112 -0
- package/lib/host/ai-page-coordinator.d.ts +21 -0
- package/lib/host/ai-page-coordinator.js +81 -0
- package/lib/host/ai-page-store.d.ts +40 -0
- package/lib/host/ai-page-store.js +104 -0
- package/lib/host/ai-profile-tools.d.ts +7 -0
- package/lib/host/ai-profile-tools.js +44 -0
- package/lib/host/ai-query-scope.d.ts +42 -0
- package/lib/host/ai-query-scope.js +104 -0
- package/lib/host/ai-reader.d.ts +219 -0
- package/lib/host/ai-reader.js +1221 -0
- package/lib/host/ai-report-samples.d.ts +26 -0
- package/lib/host/ai-report-samples.js +77 -0
- package/lib/host/ai-report-tools.d.ts +11 -0
- package/lib/host/ai-report-tools.js +70 -0
- package/lib/host/ai-set-pages.d.ts +15 -0
- package/lib/host/ai-set-pages.js +40 -0
- package/lib/host/ai-stream-pages.d.ts +15 -0
- package/lib/host/ai-stream-pages.js +42 -0
- package/lib/host/ai-tools.d.ts +4 -0
- package/lib/host/ai-tools.js +106 -0
- package/lib/host/ai-ui-tools.d.ts +10 -0
- package/lib/host/ai-ui-tools.js +72 -0
- package/lib/host/ai-value.d.ts +12 -0
- package/lib/host/ai-value.js +15 -0
- package/lib/host/ai-write-tools.d.ts +5 -0
- package/lib/host/ai-write-tools.js +101 -0
- package/lib/host/ai-writer.d.ts +25 -0
- package/lib/host/ai-writer.js +77 -0
- package/lib/host/ai-zset-pages.d.ts +15 -0
- package/lib/host/ai-zset-pages.js +40 -0
- package/lib/host/byte-identity.d.ts +3 -0
- package/lib/host/byte-identity.js +12 -0
- package/lib/host/client-list.d.ts +13 -0
- package/lib/host/client-list.js +35 -0
- package/lib/host/connection-service.d.ts +71 -0
- package/lib/host/connection-service.js +468 -0
- package/lib/host/database-reader.d.ts +10 -0
- package/lib/host/database-reader.js +62 -0
- package/lib/host/hash-reader.d.ts +12 -0
- package/lib/host/hash-reader.js +91 -0
- package/lib/host/key-reader.d.ts +20 -0
- package/lib/host/key-reader.js +65 -0
- package/lib/host/list-reader.d.ts +8 -0
- package/lib/host/list-reader.js +61 -0
- package/lib/host/match-preset-store.d.ts +36 -0
- package/lib/host/match-preset-store.js +201 -0
- package/lib/host/profile-snapshots.d.ts +32 -0
- package/lib/host/profile-snapshots.js +187 -0
- package/lib/host/profile-store.d.ts +15 -0
- package/lib/host/profile-store.js +120 -0
- package/lib/host/read-context.d.ts +23 -0
- package/lib/host/read-context.js +41 -0
- package/lib/host/redis-transport.d.ts +34 -0
- package/lib/host/redis-transport.js +262 -0
- package/lib/host/redis-write-transaction.d.ts +28 -0
- package/lib/host/redis-write-transaction.js +180 -0
- package/lib/host/resp.d.ts +38 -0
- package/lib/host/resp.js +174 -0
- package/lib/host/scan-cursors.d.ts +25 -0
- package/lib/host/scan-cursors.js +59 -0
- package/lib/host/selected-reader.d.ts +22 -0
- package/lib/host/selected-reader.js +84 -0
- package/lib/host/set-reader.d.ts +12 -0
- package/lib/host/set-reader.js +85 -0
- package/lib/host/stream-groups.d.ts +17 -0
- package/lib/host/stream-groups.js +42 -0
- package/lib/host/stream-reader.d.ts +12 -0
- package/lib/host/stream-reader.js +105 -0
- package/lib/host/string-reader.d.ts +9 -0
- package/lib/host/string-reader.js +81 -0
- package/lib/host/ui-action-channel.d.ts +4 -0
- package/lib/host/ui-action-channel.js +182 -0
- package/lib/host/ui-action-coordinator.d.ts +28 -0
- package/lib/host/ui-action-coordinator.js +188 -0
- package/lib/host/value-prefix.d.ts +5 -0
- package/lib/host/value-prefix.js +11 -0
- package/lib/host/write-bytes.d.ts +3 -0
- package/lib/host/write-bytes.js +9 -0
- package/lib/host/write-history.d.ts +18 -0
- package/lib/host/write-history.js +79 -0
- package/lib/host/write-proposals.d.ts +46 -0
- package/lib/host/write-proposals.js +132 -0
- package/lib/host/zset-reader.d.ts +12 -0
- package/lib/host/zset-reader.js +103 -0
- package/lib/host/zset-values.d.ts +6 -0
- package/lib/host/zset-values.js +26 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +5 -0
- package/lib/redis-answer.d.ts +32 -0
- package/lib/redis-answer.js +215 -0
- package/lib/redis-databases.d.ts +3 -0
- package/lib/redis-databases.js +21 -0
- package/lib/redis-read-tools.d.ts +2 -0
- package/lib/redis-read-tools.js +7 -0
- package/lib/redis-result.d.ts +4 -0
- package/lib/redis-result.js +18 -0
- package/lib/redis-score.d.ts +2 -0
- package/lib/redis-score.js +6 -0
- package/lib/remote-service.d.ts +69 -0
- package/lib/remote-service.js +187 -0
- package/lib/runtime.d.ts +3 -0
- package/lib/runtime.js +29 -0
- package/lib/stream-id.d.ts +7 -0
- package/lib/stream-id.js +19 -0
- package/lib/typert.host.d.ts +13 -0
- package/lib/typert.host.js +7 -0
- package/lib/typert.remote-client.d.ts +98 -0
- package/lib/typert.remote-client.js +3 -0
- package/lib/typert.shared.d.ts +62 -0
- package/lib/typert.shared.js +735 -0
- package/lib/ui-action-selection.d.ts +3 -0
- package/lib/ui-action-selection.js +12 -0
- package/licenses/THIRD_PARTY_NOTICES.txt +9 -0
- package/licenses/deepseek-harness-MIT.txt +21 -0
- package/licenses/dsh-database-MIT.txt +21 -0
- package/licenses/dsh-git-graph-MIT.txt +21 -0
- package/licenses/lucide-react-LICENSE.txt +43 -0
- package/licenses/phosphor-icons-MIT.txt +21 -0
- package/package.json +132 -3
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
import { MAX_KEY_BYTES, MAX_SCAN_KEYS } from "../domain.js";
|
|
3
|
+
import { isStreamId } from "../stream-id.js";
|
|
4
|
+
import { connectionErrorMessages, keyStateMessages } from "../connection-errors.js";
|
|
5
|
+
import { hashFieldReadRequestSchema, listReadRequestSchema, setMemberReadRequestSchema, stringReadRequestSchema } from "../typert.shared.js";
|
|
6
|
+
import { ConnectionFailure } from "./connection-service.js";
|
|
7
|
+
import { byteIdentity } from "./byte-identity.js";
|
|
8
|
+
/** Reject lone UTF-16 surrogates instead of silently encoding replacement bytes. */
|
|
9
|
+
function textIdentity(value) {
|
|
10
|
+
const bytes = Buffer.from(value, 'utf8');
|
|
11
|
+
if (bytes.toString('utf8') !== value)
|
|
12
|
+
throw new Error('文本包含无法无损编码的字符,请使用规范 Base64 身份。');
|
|
13
|
+
return bytes.toString('base64');
|
|
14
|
+
}
|
|
15
|
+
/** Locate only an explicitly supplied key in the current trusted session. */
|
|
16
|
+
export function registerRedisLocateTool(ctx, reader, actions) {
|
|
17
|
+
ctx.tools.register(defineTool({
|
|
18
|
+
name: 'redis_viewer_locate_stream_field',
|
|
19
|
+
description: '仅在用户明确要求定位当前 Stream Key 的消息字段对时使用。id 是完整消息 ID,fieldIndex 是整条消息中从 0 开始的字段对位置,而非同名字段出现次数。按 ID 精确 XRANGE 只读采样,不推进消费组;确认字段身份后界面再采样,消息可能发生变化。只有界面确认后返回 applied。',
|
|
20
|
+
parameters: { id: { type: 'string', required: true }, fieldIndex: { type: 'integer', required: true } },
|
|
21
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
22
|
+
timeoutMs: 15000,
|
|
23
|
+
async execute(args, exec) {
|
|
24
|
+
exec.signal.throwIfAborted();
|
|
25
|
+
if (exec.agent === undefined)
|
|
26
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
27
|
+
if (Object.keys(args).some(key => key !== 'id' && key !== 'fieldIndex'))
|
|
28
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
29
|
+
if (!isStreamId(args.id) || args.id === '0-0' || !Number.isSafeInteger(args.fieldIndex) || args.fieldIndex < 0 || args.fieldIndex >= MAX_SCAN_KEYS)
|
|
30
|
+
throw new Error('消息 ID 或字段位置无效。');
|
|
31
|
+
const session = exec.agent.session.id;
|
|
32
|
+
return reader.withBudget(session, exec.signal, async () => {
|
|
33
|
+
const prepared = await reader.prepareStreamField(session, args.id, args.fieldIndex, exec.signal).catch((error) => {
|
|
34
|
+
if (error instanceof ConnectionFailure && error.code === 'stale')
|
|
35
|
+
return null;
|
|
36
|
+
throw error;
|
|
37
|
+
});
|
|
38
|
+
if (prepared === null)
|
|
39
|
+
return { outcome: 'stale', message: '连接或界面选择已变化,请重试。' };
|
|
40
|
+
const sample = prepared.sample;
|
|
41
|
+
if (sample.state !== 'value') {
|
|
42
|
+
const message = sample.error !== null ? connectionErrorMessages[sample.error].zh : sample.state === 'missing' ? keyStateMessages.missing.zh : sample.state === 'selection-missing' ? '采样时该消息或字段位置不存在。' : '采样时 Key 类型或消息已变化。';
|
|
43
|
+
return { outcome: sample.state, error: sample.error, sampledAt: sample.sampledAt, message };
|
|
44
|
+
}
|
|
45
|
+
const field = sample.message?.fields[args.fieldIndex]?.field;
|
|
46
|
+
if (field === undefined)
|
|
47
|
+
throw new Error('采样消息缺少目标字段。');
|
|
48
|
+
const outcome = await actions.dispatch(session, { kind: 'selectStreamField', keyBase64: prepared.keyBase64, id: args.id, fieldIndex: args.fieldIndex, fieldBase64: field.base64 }, prepared.target, prepared.revision, prepared.currentKey, exec.signal);
|
|
49
|
+
return { outcome, id: args.id, fieldIndex: args.fieldIndex, field: { label: field.label, byteLength: field.byteLength }, sampledAt: sample.sampledAt, message: outcome === 'applied' ? '已确认当前 Redis 面板定位该消息字段;消息可能在两次采样间变化。' : outcome === 'unknown' ? '界面动作已进入准备阶段,但最终结果未知,请检查当前面板。' : '未能确认消息字段定位,请检查当前面板后重试。' };
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
}));
|
|
53
|
+
ctx.tools.register(defineTool({
|
|
54
|
+
name: 'redis_viewer_locate_zset_member',
|
|
55
|
+
description: '仅在用户明确要求定位 ZSet 成员时使用。目标是当前面板已选中的 ZSet Key;member 文本或 memberBase64 规范二进制身份二选一,空成员合法。使用 ZSCORE 精确只读采样,不扫描;返回的 score 和 sampledAt 属于工具预采样,面板另行采样,分数和时间可能不同;只有界面确认后返回 applied。',
|
|
56
|
+
parameters: { member: { type: 'string' }, memberBase64: { type: 'string' } },
|
|
57
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
58
|
+
timeoutMs: 15000,
|
|
59
|
+
async execute(args, exec) {
|
|
60
|
+
exec.signal.throwIfAborted();
|
|
61
|
+
if (exec.agent === undefined)
|
|
62
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
63
|
+
if (Object.keys(args).some(key => key !== 'member' && key !== 'memberBase64'))
|
|
64
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
65
|
+
if ((args.member === undefined) === (args.memberBase64 === undefined))
|
|
66
|
+
throw new Error('必须提供 member 或 memberBase64 中的一个。');
|
|
67
|
+
if (args.member !== undefined && Buffer.byteLength(args.member, 'utf8') > MAX_KEY_BYTES)
|
|
68
|
+
throw new Error('成员超过允许的字节数。');
|
|
69
|
+
const session = exec.agent.session.id;
|
|
70
|
+
return reader.withBudget(session, exec.signal, async () => {
|
|
71
|
+
const request = setMemberReadRequestSchema.parse({ target: reader.currentTarget(session), keyBase64: '', memberBase64: args.member === undefined ? args.memberBase64 : textIdentity(args.member) });
|
|
72
|
+
const prepared = await reader.prepareZSetMember(session, request.memberBase64, exec.signal).catch((error) => {
|
|
73
|
+
if (error instanceof ConnectionFailure && error.code === 'stale')
|
|
74
|
+
return null;
|
|
75
|
+
throw error;
|
|
76
|
+
});
|
|
77
|
+
if (prepared === null)
|
|
78
|
+
return { outcome: 'stale', message: '连接或界面选择已变化,请重试。' };
|
|
79
|
+
const sample = prepared.sample;
|
|
80
|
+
if (sample.state !== 'value') {
|
|
81
|
+
const message = sample.error !== null ? connectionErrorMessages[sample.error].zh : sample.state === 'missing' ? keyStateMessages.missing.zh : sample.state === 'selection-missing' ? '采样时该 ZSet 成员不存在。' : '采样时 Key 类型已变化,不能定位 ZSet 成员。';
|
|
82
|
+
return { outcome: sample.state, error: sample.error, sampledAt: sample.sampledAt, message };
|
|
83
|
+
}
|
|
84
|
+
const member = byteIdentity(Buffer.from(request.memberBase64, 'base64'));
|
|
85
|
+
const outcome = await actions.dispatch(session, { kind: 'selectZSetMember', keyBase64: prepared.keyBase64, member }, prepared.target, prepared.revision, prepared.currentKey, exec.signal);
|
|
86
|
+
return { outcome, member: { label: member.label, byteLength: member.byteLength }, score: sample.score, sampledAt: sample.sampledAt, sampleNote: 'score 和 sampledAt 属于工具预采样;面板单独再次采样,分数和时间可能不同。', message: outcome === 'applied' ? '已确认当前 Redis 面板定位该成员。' : outcome === 'unknown' ? '界面动作已进入准备阶段,但最终结果未知,请检查当前面板。' : '未能确认成员定位,请检查当前面板后重试。' };
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
}));
|
|
90
|
+
ctx.tools.register(defineTool({
|
|
91
|
+
name: 'redis_viewer_locate_set_member',
|
|
92
|
+
description: '仅在用户明确要求定位 Set 成员时使用。目标是当前面板已选中的 Set Key,不跨 Key;member 为文本或 memberBase64 为规范二进制身份,二者只能提供一个,空成员合法。使用精确 SISMEMBER 采样,不扫描、不修改数据;Key 和成员组合过大时请求可能超限并明确报告 oversize;只有界面确认后才返回 applied,成员存在性仅代表采样时结果。',
|
|
93
|
+
parameters: { member: { type: 'string' }, memberBase64: { type: 'string' } },
|
|
94
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
95
|
+
timeoutMs: 15000,
|
|
96
|
+
async execute(args, exec) {
|
|
97
|
+
exec.signal.throwIfAborted();
|
|
98
|
+
if (exec.agent === undefined)
|
|
99
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
100
|
+
if (Object.keys(args).some(key => key !== 'member' && key !== 'memberBase64'))
|
|
101
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
102
|
+
if ((args.member === undefined) === (args.memberBase64 === undefined))
|
|
103
|
+
throw new Error('必须提供 member 或 memberBase64 中的一个。');
|
|
104
|
+
if (args.member !== undefined && Buffer.byteLength(args.member, 'utf8') > MAX_KEY_BYTES)
|
|
105
|
+
throw new Error('成员超过允许的字节数。');
|
|
106
|
+
const session = exec.agent.session.id;
|
|
107
|
+
return reader.withBudget(session, exec.signal, async () => {
|
|
108
|
+
const request = setMemberReadRequestSchema.parse({ target: reader.currentTarget(session), keyBase64: '', memberBase64: args.member === undefined ? args.memberBase64 : textIdentity(args.member) });
|
|
109
|
+
const prepared = await reader.prepareSetMember(session, request.memberBase64, exec.signal).catch((error) => {
|
|
110
|
+
if (error instanceof ConnectionFailure && error.code === 'stale')
|
|
111
|
+
return null;
|
|
112
|
+
throw error;
|
|
113
|
+
});
|
|
114
|
+
if (prepared === null)
|
|
115
|
+
return { outcome: 'stale', message: '连接或界面选择已变化,请重试。' };
|
|
116
|
+
const sample = prepared.sample;
|
|
117
|
+
if (sample.state !== 'value') {
|
|
118
|
+
const message = sample.error !== null ? connectionErrorMessages[sample.error].zh : sample.state === 'missing' ? keyStateMessages.missing.zh : sample.state === 'selection-missing' ? '采样时该 Set 成员不存在。' : '采样时 Key 类型已变化,不能定位 Set 成员。';
|
|
119
|
+
return { outcome: sample.state, error: sample.error, sampledAt: sample.sampledAt, message };
|
|
120
|
+
}
|
|
121
|
+
const member = byteIdentity(Buffer.from(request.memberBase64, 'base64'));
|
|
122
|
+
const outcome = await actions.dispatch(session, { kind: 'selectSetMember', keyBase64: prepared.keyBase64, member }, prepared.target, prepared.revision, prepared.currentKey, exec.signal);
|
|
123
|
+
return { outcome, member: { label: member.label, byteLength: member.byteLength }, sampledAt: sample.sampledAt, message: outcome === 'applied' ? '已确认当前 Redis 面板定位该成员,存在性仅代表采样时结果。' : outcome === 'unknown' ? '界面动作已进入准备阶段,但最终结果未知,请检查当前面板。' : '未能确认成员定位,请检查当前面板后重试。' };
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
}));
|
|
127
|
+
ctx.tools.register(defineTool({
|
|
128
|
+
name: 'redis_viewer_locate_key',
|
|
129
|
+
description: '仅在用户明确要求定位 Key 时使用:在当前对话已连接的 Redis 和唯一可见面板中定位指定 Key。key 为文本,keyBase64 为精确二进制身份,两者只能提供一个;空 Key 合法。只做一次 TYPE 定点检查,不扫描全库、不修改数据。仅界面确认后返回 applied;不存在、权限不足或界面变化分别报告。极长 Key 即使定位成功,后续状态或值读取可能因完整身份超过单次结果预算而被拒绝。',
|
|
130
|
+
parameters: { key: { type: 'string' }, keyBase64: { type: 'string' } },
|
|
131
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
132
|
+
timeoutMs: 15000,
|
|
133
|
+
async execute(args, exec) {
|
|
134
|
+
exec.signal.throwIfAborted();
|
|
135
|
+
if (exec.agent === undefined)
|
|
136
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
137
|
+
if (Object.keys(args).some(key => key !== 'key' && key !== 'keyBase64'))
|
|
138
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
139
|
+
if ((args.key === undefined) === (args.keyBase64 === undefined))
|
|
140
|
+
throw new Error('必须提供 key 或 keyBase64 中的一个。');
|
|
141
|
+
if (args.key !== undefined && Buffer.byteLength(args.key, 'utf8') > MAX_KEY_BYTES)
|
|
142
|
+
throw new Error('Key 超过允许的字节数。');
|
|
143
|
+
const session = exec.agent.session.id;
|
|
144
|
+
return reader.withBudget(session, exec.signal, async () => {
|
|
145
|
+
const target = reader.currentTarget(session), revision = actions.selectionRevision(session);
|
|
146
|
+
if (revision === null)
|
|
147
|
+
return { outcome: 'stale', message: '请在当前对话打开唯一 Redis 面板并等待选择同步。' };
|
|
148
|
+
const request = stringReadRequestSchema.parse({ target, keyBase64: args.key === undefined ? args.keyBase64 : textIdentity(args.key), offset: '0', count: 1 });
|
|
149
|
+
const validate = () => { if (actions.selectionRevision(session) !== revision)
|
|
150
|
+
throw new ConnectionFailure('stale'); };
|
|
151
|
+
const probe = await reader.probeKey(session, target, request.keyBase64, exec.signal, validate);
|
|
152
|
+
exec.signal.throwIfAborted();
|
|
153
|
+
if (probe.state === 'missing')
|
|
154
|
+
return { outcome: 'missing', sampledAt: probe.sampledAt, message: keyStateMessages.missing.zh };
|
|
155
|
+
if (probe.state === 'failed')
|
|
156
|
+
return { outcome: 'failed', error: probe.error, sampledAt: probe.sampledAt, message: probe.error === null ? 'Key 检查失败。' : connectionErrorMessages[probe.error].zh };
|
|
157
|
+
if (actions.selectionRevision(session) !== revision || !reader.isCurrentTarget(session, target))
|
|
158
|
+
return { outcome: 'stale', message: '连接或界面选择已变化,请重试。' };
|
|
159
|
+
const key = byteIdentity(Buffer.from(request.keyBase64, 'base64'));
|
|
160
|
+
const outcome = await actions.dispatch(session, { kind: 'selectKey', key }, target, revision, () => reader.isCurrentTarget(session, target), exec.signal);
|
|
161
|
+
return { outcome, key: { label: key.label, byteLength: key.byteLength }, sampledAt: probe.sampledAt, message: outcome === 'applied' ? '已确认当前 Redis 面板定位该 Key。' : outcome === 'unknown' ? '界面动作已进入准备阶段,但最终结果未知,请检查当前面板。' : '未能确认界面定位,请检查当前面板后重试。' };
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
}));
|
|
165
|
+
ctx.tools.register(defineTool({
|
|
166
|
+
name: 'redis_viewer_locate_list_index',
|
|
167
|
+
description: '仅在用户明确要求定位 List 下标时使用。目标是当前面板已选中的 List Key;index 是非负十进制整数文本,零为第一个位置,最大为 Redis 有符号 64 位整数。只读取该位置,不扫描、不修改数据;并发插入或删除会改变下标对应的元素,位置仅代表采样时结果,不能声称业务元素身份不变。只有界面确认后才返回 applied。',
|
|
168
|
+
parameters: { index: { type: 'string', required: true } },
|
|
169
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
170
|
+
timeoutMs: 15000,
|
|
171
|
+
async execute(args, exec) {
|
|
172
|
+
exec.signal.throwIfAborted();
|
|
173
|
+
if (exec.agent === undefined)
|
|
174
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
175
|
+
if (Object.keys(args).some(key => key !== 'index'))
|
|
176
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
177
|
+
const session = exec.agent.session.id;
|
|
178
|
+
return reader.withBudget(session, exec.signal, async () => {
|
|
179
|
+
const request = listReadRequestSchema.parse({ target: reader.currentTarget(session), keyBase64: '', offset: args.index, count: 1 });
|
|
180
|
+
const prepared = await reader.prepareListIndex(session, request.offset, exec.signal).catch((error) => {
|
|
181
|
+
if (error instanceof ConnectionFailure && error.code === 'stale')
|
|
182
|
+
return null;
|
|
183
|
+
throw error;
|
|
184
|
+
});
|
|
185
|
+
if (prepared === null)
|
|
186
|
+
return { outcome: 'stale', message: '连接或界面选择已变化,请重试。' };
|
|
187
|
+
const sample = prepared.sample;
|
|
188
|
+
if (sample.state !== 'value') {
|
|
189
|
+
const message = sample.error !== null ? connectionErrorMessages[sample.error].zh : sample.state === 'missing' ? keyStateMessages.missing.zh : sample.state === 'selection-missing' ? '采样时该 List 下标不存在。' : '采样时 Key 类型或 List 内容已变化,请重试。';
|
|
190
|
+
return { outcome: sample.state, error: sample.error, sampledAt: sample.sampledAt, message };
|
|
191
|
+
}
|
|
192
|
+
const outcome = await actions.dispatch(session, { kind: 'selectListIndex', keyBase64: prepared.keyBase64, index: request.offset }, prepared.target, prepared.revision, prepared.currentKey, exec.signal);
|
|
193
|
+
return { outcome, index: request.offset, sampledAt: sample.sampledAt, message: outcome === 'applied' ? '已确认当前 Redis 面板定位该下标;List 并发变化时该位置可能指向其他元素。' : outcome === 'unknown' ? '界面动作已进入准备阶段,但最终结果未知,请检查当前面板。' : '未能确认下标定位,请检查当前面板后重试。' };
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
}));
|
|
197
|
+
ctx.tools.register(defineTool({
|
|
198
|
+
name: 'redis_viewer_locate_hash_field',
|
|
199
|
+
description: '仅在用户明确要求定位 Hash 字段时使用。目标是当前面板已选中的 Hash Key,不跨 Key;field 为文本或 fieldBase64 为规范二进制身份,二者只能提供一个,空字段合法。使用精确 HGET 采样,不扫描、不修改数据;Key 和字段组合过大时请求可能超限并明确报告 oversize;只有界面确认后才返回 applied。',
|
|
200
|
+
parameters: { field: { type: 'string' }, fieldBase64: { type: 'string' } },
|
|
201
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
202
|
+
timeoutMs: 15000,
|
|
203
|
+
async execute(args, exec) {
|
|
204
|
+
exec.signal.throwIfAborted();
|
|
205
|
+
if (exec.agent === undefined)
|
|
206
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
207
|
+
if (Object.keys(args).some(key => key !== 'field' && key !== 'fieldBase64'))
|
|
208
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
209
|
+
if ((args.field === undefined) === (args.fieldBase64 === undefined))
|
|
210
|
+
throw new Error('必须提供 field 或 fieldBase64 中的一个。');
|
|
211
|
+
if (args.field !== undefined && Buffer.byteLength(args.field, 'utf8') > MAX_KEY_BYTES)
|
|
212
|
+
throw new Error('字段超过允许的字节数。');
|
|
213
|
+
const session = exec.agent.session.id;
|
|
214
|
+
return reader.withBudget(session, exec.signal, async () => {
|
|
215
|
+
const request = hashFieldReadRequestSchema.parse({ target: reader.currentTarget(session), keyBase64: '', fieldBase64: args.field === undefined ? args.fieldBase64 : textIdentity(args.field) });
|
|
216
|
+
const prepared = await reader.prepareHashField(session, request.fieldBase64, exec.signal).catch((error) => {
|
|
217
|
+
if (error instanceof ConnectionFailure && error.code === 'stale')
|
|
218
|
+
return null;
|
|
219
|
+
throw error;
|
|
220
|
+
});
|
|
221
|
+
if (prepared === null)
|
|
222
|
+
return { outcome: 'stale', message: '连接或界面选择已变化,请重试。' };
|
|
223
|
+
const sample = prepared.sample;
|
|
224
|
+
if (sample.state !== 'value') {
|
|
225
|
+
const message = sample.error !== null ? connectionErrorMessages[sample.error].zh : sample.state === 'missing' ? keyStateMessages.missing.zh : sample.state === 'selection-missing' ? '采样时该 Hash 字段不存在。' : '采样时 Key 的类型已变化,不能定位 Hash 字段。';
|
|
226
|
+
return { outcome: sample.state, error: sample.error, sampledAt: sample.sampledAt, message };
|
|
227
|
+
}
|
|
228
|
+
const field = byteIdentity(Buffer.from(request.fieldBase64, 'base64'));
|
|
229
|
+
const outcome = await actions.dispatch(session, { kind: 'selectHashField', keyBase64: prepared.keyBase64, field }, prepared.target, prepared.revision, prepared.currentKey, exec.signal);
|
|
230
|
+
return { outcome, field: { label: field.label, byteLength: field.byteLength }, sampledAt: sample.sampledAt, message: outcome === 'applied' ? '已确认当前 Redis 面板定位该字段。' : outcome === 'unknown' ? '界面动作已进入准备阶段,但最终结果未知,请检查当前面板。' : '未能确认字段定位,请检查当前面板后重试。' };
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { RedisAiReader } from './ai-reader.js';
|
|
3
|
+
import { type RedisMatchPresets } from './match-preset-store.js';
|
|
4
|
+
import type { UiAction } from '../domain.js';
|
|
5
|
+
import type { UiActionCoordinator } from './ui-action-coordinator.js';
|
|
6
|
+
/** The DSH profile owns these immutable names and patterns, not Redis. */
|
|
7
|
+
export declare function registerRedisMatchTools(ctx: Context, reader: RedisAiReader, presets: RedisMatchPresets, actions: UiActionCoordinator<UiAction>): void;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
import { MatchPresetError } from "./match-preset-store.js";
|
|
3
|
+
import { scanKeysRequestSchema } from "../typert.shared.js";
|
|
4
|
+
import { parseQueryArgs, registerQueryScope } from "./ai-query-scope.js";
|
|
5
|
+
const applyName = 'redis_viewer_apply_match_preset';
|
|
6
|
+
const applyParameters = { id: { type: 'string', required: true } };
|
|
7
|
+
/** The DSH profile owns these immutable names and patterns, not Redis. */
|
|
8
|
+
export function registerRedisMatchTools(ctx, reader, presets, actions) {
|
|
9
|
+
const approvedPreset = registerQueryScope(ctx, reader, name => name === applyName, async (exec, sessionId, signal) => {
|
|
10
|
+
const { id } = parseQueryArgs(applyParameters, exec.arguments);
|
|
11
|
+
const target = reader.currentTarget(sessionId), revision = actions.selectionRevision(sessionId);
|
|
12
|
+
if (revision === null)
|
|
13
|
+
return { target, conditions: null, data: { outcome: 'stale' } };
|
|
14
|
+
let preset;
|
|
15
|
+
try {
|
|
16
|
+
preset = await presets.get(id, signal);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
if (error instanceof MatchPresetError && error.code === 'cancelled')
|
|
20
|
+
throw error;
|
|
21
|
+
throw new Error('无法读取该筛选预设,请检查本地资料。');
|
|
22
|
+
}
|
|
23
|
+
if (preset === null)
|
|
24
|
+
return { target, conditions: null, data: { outcome: 'missing' } };
|
|
25
|
+
const pattern = scanKeysRequestSchema.parse({ target, pattern: preset.pattern, cursor: null }).pattern;
|
|
26
|
+
return { target, conditions: { match: pattern, preset: preset.name }, sidebar: true, data: { outcome: 'ready', target, revision, preset, pattern } };
|
|
27
|
+
});
|
|
28
|
+
ctx.tools.register(defineTool({
|
|
29
|
+
name: 'redis_viewer_match_presets',
|
|
30
|
+
description: '仅在用户明确要求查看已保存的 Redis MATCH 筛选预设时调用。返回名称、ID 和条件,每页最多 10 条;条件按用户保存原样返回,可能包含敏感文本。每页重新读取,分页不是快照;不连接 Redis。',
|
|
31
|
+
parameters: { cursor: { type: 'string', description: '续页使用上次返回的 nextCursor;首次省略。' } },
|
|
32
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
33
|
+
async execute(args, exec) {
|
|
34
|
+
exec.signal.throwIfAborted();
|
|
35
|
+
if (exec.agent === undefined)
|
|
36
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
37
|
+
if (Object.keys(args).some(key => key !== 'cursor'))
|
|
38
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
39
|
+
const cursor = args.cursor ?? null;
|
|
40
|
+
if (cursor !== null && (!/^(?:0|[1-9]\d{0,2})$/u.test(cursor) || Number(cursor) >= 256))
|
|
41
|
+
throw new Error('筛选预设游标无效。');
|
|
42
|
+
return reader.withBudget(exec.agent.session.id, exec.signal, async () => {
|
|
43
|
+
try {
|
|
44
|
+
const page = await presets.list(cursor, exec.signal);
|
|
45
|
+
return { ...page, presets: page.presets.map(preset => ({ ...preset })) };
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error instanceof MatchPresetError && error.code === 'cancelled')
|
|
49
|
+
throw error;
|
|
50
|
+
throw new Error('无法读取筛选预设,请检查本地资料。');
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
}));
|
|
55
|
+
ctx.tools.register(defineTool({
|
|
56
|
+
name: applyName,
|
|
57
|
+
description: '仅在用户明确要求将唯一确定的已保存 MATCH 预设应用到当前 Redis 面板时调用。先列出预设并提供 id;范围确认通过后会清除当前 Key 选择并启动该条件的有界扫描。只有界面确认筛选已设置才返回 applied,扫描可能仍在进行或不完整;取消或回执丢失后检查面板,不能假定操作没有发生。',
|
|
58
|
+
parameters: applyParameters, output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] }, timeoutMs: 15000,
|
|
59
|
+
async execute(_args, exec) {
|
|
60
|
+
const prepared = approvedPreset(exec);
|
|
61
|
+
if (exec.agent === undefined)
|
|
62
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
63
|
+
const session = exec.agent.session.id;
|
|
64
|
+
return reader.withBudget(session, exec.signal, async () => {
|
|
65
|
+
if (prepared.outcome === 'stale')
|
|
66
|
+
return { outcome: 'stale', message: '请打开唯一 Redis 面板并等待选择同步。' };
|
|
67
|
+
if (prepared.outcome === 'missing')
|
|
68
|
+
return { outcome: 'missing', message: '该筛选预设不存在。' };
|
|
69
|
+
const { target, revision, preset, pattern } = prepared;
|
|
70
|
+
if (!reader.isCurrentTarget(session, target) || actions.selectionRevision(session) !== revision)
|
|
71
|
+
return { outcome: 'stale', message: '连接或界面选择已变化,请检查面板。' };
|
|
72
|
+
const result = await actions.dispatch(session, { kind: 'applyMatch', target, pattern }, null, revision, () => reader.isCurrentTarget(session, target), exec.signal);
|
|
73
|
+
const outcome = result === 'applied' && actions.selectionRevision(session) !== revision + 1 ? 'stale' : result;
|
|
74
|
+
return { outcome, preset: { id: preset.id, name: preset.name, pattern }, message: outcome === 'applied' ? '已确认设置筛选并启动扫描;扫描进度和结果完整性请查看面板。' : '未能确认筛选设置,请检查当前面板。' };
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
}));
|
|
78
|
+
ctx.tools.register(defineTool({
|
|
79
|
+
name: 'redis_viewer_save_match_preset',
|
|
80
|
+
description: '仅在用户明确要求保存筛选预设且提供名称和完整 Redis MATCH 条件时调用。保存到当前 DSH 本地 profile,不连接或修改 Redis;名称冲突不覆盖。不可猜测当前界面条件;中断后保存结果可能未知,应重新列出核对。',
|
|
81
|
+
parameters: { name: { type: 'string', required: true }, pattern: { type: 'string', required: true } },
|
|
82
|
+
output: { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
83
|
+
async execute(args, exec) {
|
|
84
|
+
exec.signal.throwIfAborted();
|
|
85
|
+
if (exec.agent === undefined)
|
|
86
|
+
throw new Error('Redis 工具调用缺少有效的 DSH 对话身份。');
|
|
87
|
+
if (Object.keys(args).some(key => key !== 'name' && key !== 'pattern'))
|
|
88
|
+
throw new Error('Redis 工具包含不支持的参数。');
|
|
89
|
+
let started = false;
|
|
90
|
+
try {
|
|
91
|
+
return await reader.withBudget(exec.agent.session.id, exec.signal, async () => {
|
|
92
|
+
started = true;
|
|
93
|
+
try {
|
|
94
|
+
return { outcome: 'saved', preset: { ...await presets.save({ name: args.name, pattern: args.pattern }, exec.signal) } };
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (error instanceof MatchPresetError && (error.code === 'invalid' || error.code === 'conflict' || error.code === 'full'))
|
|
98
|
+
return { outcome: error.code };
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
// Once save starts, even a lost response or interrupted budget commit
|
|
105
|
+
// cannot prove that the immutable filesystem link was not created.
|
|
106
|
+
if (started)
|
|
107
|
+
throw new Error('保存结果未知,请重新列出筛选预设核对后再操作。');
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PreparedAiPage } from './ai-page-store.js';
|
|
2
|
+
interface Slot {
|
|
3
|
+
disposeSession(sessionId: string): void;
|
|
4
|
+
dispose(): void;
|
|
5
|
+
}
|
|
6
|
+
/** A candidate first page must commit before it replaces an existing cursor. */
|
|
7
|
+
export declare class AiPageCoordinator<T extends Slot> {
|
|
8
|
+
private readonly slots;
|
|
9
|
+
private readonly firstPage;
|
|
10
|
+
private readonly sessions;
|
|
11
|
+
private revision;
|
|
12
|
+
constructor(slots: readonly [T, T], firstPage?: {
|
|
13
|
+
readonly replacePending?: true;
|
|
14
|
+
readonly retainUndelivered?: true;
|
|
15
|
+
});
|
|
16
|
+
snapshot(sessionId: string): bigint;
|
|
17
|
+
prepare(sessionId: string, cursor: string | null, snapshot: bigint, fetch: (slot: T) => Promise<PreparedAiPage>): Promise<PreparedAiPage>;
|
|
18
|
+
disposeSession(sessionId: string): void;
|
|
19
|
+
dispose(): void;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ConnectionFailure } from "./connection-service.js";
|
|
2
|
+
/** A candidate first page must commit before it replaces an existing cursor. */
|
|
3
|
+
export class AiPageCoordinator {
|
|
4
|
+
slots;
|
|
5
|
+
firstPage;
|
|
6
|
+
sessions = new Map();
|
|
7
|
+
revision = 0n;
|
|
8
|
+
constructor(slots, firstPage = {}) {
|
|
9
|
+
this.slots = slots;
|
|
10
|
+
this.firstPage = firstPage;
|
|
11
|
+
}
|
|
12
|
+
snapshot(sessionId) { return this.sessions.get(sessionId)?.revision ?? 0n; }
|
|
13
|
+
async prepare(sessionId, cursor, snapshot, fetch) {
|
|
14
|
+
let state = this.sessions.get(sessionId);
|
|
15
|
+
if (state === undefined) {
|
|
16
|
+
if (cursor !== null || snapshot !== 0n)
|
|
17
|
+
throw new ConnectionFailure('stale');
|
|
18
|
+
state = { revision: 0n, current: null, busy: null };
|
|
19
|
+
this.sessions.set(sessionId, state);
|
|
20
|
+
}
|
|
21
|
+
if (cursor === null && state.revision !== snapshot)
|
|
22
|
+
throw new ConnectionFailure('stale');
|
|
23
|
+
if (state.busy !== null && (!this.firstPage.replacePending || cursor !== null || !state.busy.first))
|
|
24
|
+
throw new Error('该类型的读取正在执行,请等待后重试。');
|
|
25
|
+
if (cursor !== null && state.current === null)
|
|
26
|
+
throw new ConnectionFailure('stale');
|
|
27
|
+
const index = cursor === null ? (state.current === 0 ? 1 : 0) : state.current === 1 ? 1 : 0;
|
|
28
|
+
const owner = { first: cursor === null }, current = state, slot = this.slots[index];
|
|
29
|
+
// A newer first page supersedes the in-flight candidate, leaving the last
|
|
30
|
+
// delivered slot intact until the new result is accepted.
|
|
31
|
+
if (current.busy !== null)
|
|
32
|
+
slot.disposeSession(sessionId);
|
|
33
|
+
current.busy = owner;
|
|
34
|
+
const active = () => this.sessions.get(sessionId) === current && current.busy === owner;
|
|
35
|
+
const release = (discard) => {
|
|
36
|
+
if (!active())
|
|
37
|
+
return;
|
|
38
|
+
if (discard && cursor === null)
|
|
39
|
+
slot.disposeSession(sessionId);
|
|
40
|
+
current.busy = null;
|
|
41
|
+
if (current.current === null && current.revision === 0n)
|
|
42
|
+
this.sessions.delete(sessionId);
|
|
43
|
+
};
|
|
44
|
+
try {
|
|
45
|
+
const page = await fetch(slot);
|
|
46
|
+
let finished = false;
|
|
47
|
+
return { result: page.result, isPage: page.isPage,
|
|
48
|
+
commit: () => {
|
|
49
|
+
if (finished || !active())
|
|
50
|
+
throw new ConnectionFailure('stale');
|
|
51
|
+
page.commit();
|
|
52
|
+
if (cursor === null && page.isPage) {
|
|
53
|
+
const previous = current.current;
|
|
54
|
+
current.current = index;
|
|
55
|
+
if (previous !== null)
|
|
56
|
+
this.slots[previous].disposeSession(sessionId);
|
|
57
|
+
}
|
|
58
|
+
if (page.isPage)
|
|
59
|
+
current.revision = ++this.revision;
|
|
60
|
+
finished = true;
|
|
61
|
+
release(!page.isPage);
|
|
62
|
+
},
|
|
63
|
+
rollback: () => {
|
|
64
|
+
if (finished)
|
|
65
|
+
return;
|
|
66
|
+
finished = true;
|
|
67
|
+
page.rollback();
|
|
68
|
+
release(!this.firstPage.retainUndelivered);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
release(!this.firstPage.retainUndelivered);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
disposeSession(sessionId) { this.sessions.delete(sessionId); for (const slot of this.slots)
|
|
78
|
+
slot.disposeSession(sessionId); }
|
|
79
|
+
dispose() { this.sessions.clear(); for (const slot of this.slots)
|
|
80
|
+
slot.dispose(); }
|
|
81
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ConnectionErrorCode } from '../domain.js';
|
|
3
|
+
type AiJson = z.infer<ReturnType<typeof z.json>>;
|
|
4
|
+
interface Page {
|
|
5
|
+
readonly state: string;
|
|
6
|
+
readonly error: ConnectionErrorCode | null;
|
|
7
|
+
readonly cursor: string | null;
|
|
8
|
+
}
|
|
9
|
+
export interface PreparedAiResult {
|
|
10
|
+
readonly result: AiJson;
|
|
11
|
+
commit(): void;
|
|
12
|
+
rollback(): void;
|
|
13
|
+
}
|
|
14
|
+
export interface PreparedAiPage extends PreparedAiResult {
|
|
15
|
+
readonly isPage: boolean;
|
|
16
|
+
}
|
|
17
|
+
interface Request<P extends Page, Row> {
|
|
18
|
+
readonly sessionId: string;
|
|
19
|
+
readonly binding: string;
|
|
20
|
+
readonly selection: object | null;
|
|
21
|
+
readonly cursor: string | null;
|
|
22
|
+
readonly signal: AbortSignal;
|
|
23
|
+
readonly maxRows?: number;
|
|
24
|
+
readonly fetch: (cursor: string | null) => Promise<P>;
|
|
25
|
+
readonly rows: (page: P) => readonly Row[];
|
|
26
|
+
readonly output: (page: P, rows: readonly Row[], cursor: string | null, first: boolean) => unknown;
|
|
27
|
+
readonly validate: (error: ConnectionErrorCode | null) => void;
|
|
28
|
+
readonly discard: () => void;
|
|
29
|
+
readonly oversize: string;
|
|
30
|
+
}
|
|
31
|
+
/** One bounded page per session and reader; delivery commits only with budget accounting. */
|
|
32
|
+
export declare class AiPageStore<P extends Page, Row> {
|
|
33
|
+
private readonly prefix;
|
|
34
|
+
private readonly sessions;
|
|
35
|
+
constructor(prefix: string);
|
|
36
|
+
disposeSession(sessionId: string): void;
|
|
37
|
+
dispose(): void;
|
|
38
|
+
prepare(request: Request<P, Row>): Promise<PreparedAiPage>;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { ConnectionFailure } from "./connection-service.js";
|
|
4
|
+
import { MAX_AI_RESULT_BYTES } from "./ai-budget.js";
|
|
5
|
+
/** One bounded page per session and reader; delivery commits only with budget accounting. */
|
|
6
|
+
export class AiPageStore {
|
|
7
|
+
prefix;
|
|
8
|
+
sessions = new Map();
|
|
9
|
+
constructor(prefix) {
|
|
10
|
+
this.prefix = prefix;
|
|
11
|
+
}
|
|
12
|
+
disposeSession(sessionId) { this.sessions.delete(sessionId); }
|
|
13
|
+
dispose() { this.sessions.clear(); }
|
|
14
|
+
async prepare(request) {
|
|
15
|
+
const { sessionId, binding, selection, cursor, signal } = request;
|
|
16
|
+
signal.throwIfAborted();
|
|
17
|
+
request.validate(null);
|
|
18
|
+
let state = this.sessions.get(sessionId);
|
|
19
|
+
if (cursor === null) {
|
|
20
|
+
// Retry a successfully fetched first page that never reached commit.
|
|
21
|
+
if (state === undefined || state.binding !== binding || state.selection !== selection || state.delivered || state.busy || state.page === null) {
|
|
22
|
+
state = { binding, selection, token: null, busy: false, page: null, offset: 0, nextCursor: null, delivered: false };
|
|
23
|
+
this.sessions.set(sessionId, state);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (state === undefined || state.binding !== binding || state.selection !== selection || state.token !== cursor || state.busy)
|
|
27
|
+
throw new ConnectionFailure('stale');
|
|
28
|
+
const current = state;
|
|
29
|
+
current.busy = true;
|
|
30
|
+
const active = () => { if (this.sessions.get(sessionId) !== current)
|
|
31
|
+
throw new ConnectionFailure('stale'); };
|
|
32
|
+
try {
|
|
33
|
+
if (current.page === null) {
|
|
34
|
+
const page = await request.fetch(current.nextCursor);
|
|
35
|
+
active();
|
|
36
|
+
// A successful lower-level page already advanced its token. Retain it
|
|
37
|
+
// before observing late cancellation so the input token can be retried.
|
|
38
|
+
const isPage = page.state === 'page';
|
|
39
|
+
if (!isPage)
|
|
40
|
+
signal.throwIfAborted();
|
|
41
|
+
request.validate(page.error);
|
|
42
|
+
current.page = page;
|
|
43
|
+
current.offset = 0;
|
|
44
|
+
current.nextCursor = page.cursor;
|
|
45
|
+
signal.throwIfAborted();
|
|
46
|
+
}
|
|
47
|
+
const page = current.page, rows = request.rows(page);
|
|
48
|
+
const token = this.prefix + randomUUID(), remaining = rows.length - current.offset;
|
|
49
|
+
const first = !current.delivered && current.offset === 0;
|
|
50
|
+
const output = (count, next) => request.output(page, rows.slice(current.offset, current.offset + count), next, first);
|
|
51
|
+
// Measure all enclosing data, not only the row list; a real token's length
|
|
52
|
+
// is included even if Redis's page contains no matching rows.
|
|
53
|
+
const fits = (count) => {
|
|
54
|
+
const next = count < remaining || current.nextCursor !== null ? token : null;
|
|
55
|
+
return Buffer.byteLength(JSON.stringify(output(count, next))) <= MAX_AI_RESULT_BYTES;
|
|
56
|
+
};
|
|
57
|
+
let low = 0, high = Math.min(remaining, request.maxRows ?? remaining);
|
|
58
|
+
while (low < high) {
|
|
59
|
+
const middle = Math.ceil((low + high) / 2);
|
|
60
|
+
if (fits(middle))
|
|
61
|
+
low = middle;
|
|
62
|
+
else
|
|
63
|
+
high = middle - 1;
|
|
64
|
+
}
|
|
65
|
+
if (!fits(low) || (remaining > 0 && low === 0)) {
|
|
66
|
+
if (this.sessions.get(sessionId) === current) {
|
|
67
|
+
this.sessions.delete(sessionId);
|
|
68
|
+
request.discard();
|
|
69
|
+
}
|
|
70
|
+
throw new Error(request.oversize);
|
|
71
|
+
}
|
|
72
|
+
const end = current.offset + low, next = end < rows.length || current.nextCursor !== null ? token : null;
|
|
73
|
+
const result = z.json().parse(output(low, next));
|
|
74
|
+
let finished = false;
|
|
75
|
+
return { result, isPage: page.state === 'page',
|
|
76
|
+
commit: () => {
|
|
77
|
+
if (finished)
|
|
78
|
+
throw new ConnectionFailure('stale');
|
|
79
|
+
signal.throwIfAborted();
|
|
80
|
+
active();
|
|
81
|
+
request.validate(page.error);
|
|
82
|
+
finished = true;
|
|
83
|
+
current.delivered = true;
|
|
84
|
+
current.busy = false;
|
|
85
|
+
current.offset = end;
|
|
86
|
+
current.token = next;
|
|
87
|
+
if (end === rows.length) {
|
|
88
|
+
current.page = null;
|
|
89
|
+
current.offset = 0;
|
|
90
|
+
}
|
|
91
|
+
if (next === null)
|
|
92
|
+
this.sessions.delete(sessionId);
|
|
93
|
+
},
|
|
94
|
+
rollback: () => { if (!finished && this.sessions.get(sessionId) === current)
|
|
95
|
+
current.busy = false; },
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (this.sessions.get(sessionId) === current)
|
|
100
|
+
current.busy = false;
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import type { Agent } from '@deepseek-ai/dsh-agent';
|
|
3
|
+
import { type ProfileCollection } from '../domain.js';
|
|
4
|
+
import type { RedisAiReader } from './ai-reader.js';
|
|
5
|
+
import type { RedisProfileSnapshots } from './profile-snapshots.js';
|
|
6
|
+
/** Profile names are shared by the DSH profile; connection settings stay local. */
|
|
7
|
+
export declare function registerRedisProfileTools(ctx: Context, reader: RedisAiReader, snapshots: RedisProfileSnapshots, list: (agent: Agent, signal: AbortSignal) => Promise<ProfileCollection>): void;
|