@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,101 @@
|
|
|
1
|
+
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { MAX_REDIS_WRITE_BYTES } from "../domain.js";
|
|
4
|
+
import { databaseTargetSchema } from "../typert.shared.js";
|
|
5
|
+
import { decodeWriteBytes, MAX_WRITE_BASE64 } from "./write-bytes.js";
|
|
6
|
+
const nullableString = { oneOf: [{ type: 'string' }, { type: 'null' }] };
|
|
7
|
+
const proposalParameter = { type: 'object', required: true, additionalProperties: false, properties: {
|
|
8
|
+
id: { type: 'string', required: true }, expiresAt: { type: 'string', required: true },
|
|
9
|
+
target: { type: 'object', required: true, additionalProperties: false, properties: { id: { type: 'string', required: true }, generation: { type: 'integer', required: true }, db: { type: 'integer', required: true } } },
|
|
10
|
+
host: { type: 'string', required: true }, port: { type: 'integer', required: true }, type: { type: 'string', enum: ['string', 'hash'], required: true },
|
|
11
|
+
keyTtl: { type: 'string', enum: ['none'], required: true }, fieldTtl: { ...nullableString, required: true },
|
|
12
|
+
keyBase64: { type: 'string', required: true }, fieldBase64: { ...nullableString, required: true },
|
|
13
|
+
beforeBase64: { type: 'string', required: true }, afterBase64: { type: 'string', required: true },
|
|
14
|
+
} };
|
|
15
|
+
const proposalSchema = z.object({
|
|
16
|
+
id: z.string().uuid(), expiresAt: z.string().max(24),
|
|
17
|
+
target: z.unknown().transform(value => databaseTargetSchema.parse(value)),
|
|
18
|
+
host: z.string().min(1).max(1024), port: z.number().int().min(1).max(65535), type: z.enum(['string', 'hash']),
|
|
19
|
+
keyTtl: z.literal('none'), fieldTtl: z.literal('none').nullable(), keyBase64: z.string().max(MAX_WRITE_BASE64), fieldBase64: z.string().max(MAX_WRITE_BASE64).nullable(),
|
|
20
|
+
beforeBase64: z.string().max(MAX_WRITE_BASE64), afterBase64: z.string().max(MAX_WRITE_BASE64),
|
|
21
|
+
}).strict();
|
|
22
|
+
const output = { schema: { type: 'json' }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] };
|
|
23
|
+
const wireRecord = (record) => ({ ...record, target: { ...record.target }, identity: { ...record.identity } });
|
|
24
|
+
function identity(text, encoded) {
|
|
25
|
+
if ((text === undefined) === (encoded === undefined))
|
|
26
|
+
throw new Error('文本和 Base64 必须且只能指定一种。');
|
|
27
|
+
if (text !== undefined) {
|
|
28
|
+
if (text.length > MAX_REDIS_WRITE_BYTES)
|
|
29
|
+
throw new Error('文本无法无损编码或超过限制。');
|
|
30
|
+
const bytes = Buffer.from(text, 'utf8');
|
|
31
|
+
if (bytes.length > MAX_REDIS_WRITE_BYTES || bytes.toString('utf8') !== text)
|
|
32
|
+
throw new Error('文本无法无损编码或超过限制。');
|
|
33
|
+
return bytes.toString('base64');
|
|
34
|
+
}
|
|
35
|
+
const value = encoded ?? '';
|
|
36
|
+
if (decodeWriteBytes(value) === null)
|
|
37
|
+
throw new Error('Base64 不规范或超过限制。');
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
/** Use committed native Session events, never a model-supplied session or turn. */
|
|
41
|
+
export function registerRedisWriteTools(ctx, reader, writer) {
|
|
42
|
+
ctx.on('session/event', (session, event) => {
|
|
43
|
+
if (event.type === 'turn/end')
|
|
44
|
+
writer.endTurn(session.id, String(event.data.turn));
|
|
45
|
+
});
|
|
46
|
+
const owner = (exec) => {
|
|
47
|
+
exec.signal.throwIfAborted();
|
|
48
|
+
const session = exec.agent?.session;
|
|
49
|
+
if (session === undefined)
|
|
50
|
+
throw new Error('Redis 写入缺少有效的 DSH 对话身份。');
|
|
51
|
+
const { openTurnStartSeq, lastTurn } = ctx.agents.turnBoundaryOf(session);
|
|
52
|
+
if (openTurnStartSeq === null)
|
|
53
|
+
throw new Error('Redis 写入需要当前已开始的 DSH 轮次。');
|
|
54
|
+
return { session: session.id, turn: String(lastTurn), current: () => {
|
|
55
|
+
const boundary = ctx.agents.turnBoundaryOf(session);
|
|
56
|
+
return boundary.openTurnStartSeq === openTurnStartSeq && boundary.lastTurn === lastTurn;
|
|
57
|
+
} };
|
|
58
|
+
};
|
|
59
|
+
ctx.tools.register(defineTool({
|
|
60
|
+
name: 'redis_viewer_prepare_write',
|
|
61
|
+
description: '仅在用户明确要求修改当前连接的已有 String 或已有 Hash 字段时使用。先在 Redis 面板由用户启用 AI 写入;指定精确 Key、Hash 字段和完整新值,各项文本或规范 Base64 二选一。仅采样不写入,返回含连接、DB、旧值、新值和过期时间的完整 proposal;值超过 4096 字节、有 TTL、目标缺失或采样冲突会拒绝。必须将 proposal 原样交给执行工具,不能自行改写。',
|
|
62
|
+
parameters: { type: { type: 'string', enum: ['string', 'hash'], required: true }, key: { type: 'string' }, keyBase64: { type: 'string' }, field: { type: 'string' }, fieldBase64: { type: 'string' }, value: { type: 'string' }, valueBase64: { type: 'string' } }, output, timeoutMs: 15000,
|
|
63
|
+
async execute(args, exec) {
|
|
64
|
+
if (Object.keys(args).some(key => !['type', 'key', 'keyBase64', 'field', 'fieldBase64', 'value', 'valueBase64'].includes(key)))
|
|
65
|
+
throw new Error('Redis 写入提案包含不支持的参数。');
|
|
66
|
+
const context = owner(exec);
|
|
67
|
+
const keyBase64 = identity(args.key, args.keyBase64), afterBase64 = identity(args.value, args.valueBase64);
|
|
68
|
+
const fieldBase64 = args.type === 'hash' ? identity(args.field, args.fieldBase64) : null;
|
|
69
|
+
if (args.type === 'string' && (args.field !== undefined || args.fieldBase64 !== undefined))
|
|
70
|
+
throw new Error('String 目标不能指定 Hash 字段。');
|
|
71
|
+
const proposal = await writer.prepare(context.session, reader.currentTarget(context.session), context.turn, context.current, { type: args.type, keyBase64, fieldBase64, afterBase64 }, exec.signal);
|
|
72
|
+
return { proposal: { ...proposal, target: { ...proposal.target } }, note: '前态经 WATCH 验证;不代表已经写入。执行时还会再次检查。' };
|
|
73
|
+
},
|
|
74
|
+
}));
|
|
75
|
+
ctx.tools.register(defineTool({
|
|
76
|
+
name: 'redis_viewer_execute_write',
|
|
77
|
+
description: '仅执行本轮刚生成且未过期的完整 proposal;必须原样复制全部字段,不能改变目标或数值。该工具遵守 DSH 当前工具权限策略。结果区分未执行、冲突、明确拒绝、已写入并核对、回读未确认、不一致及结果未知;未知时不要自动重试。',
|
|
78
|
+
parameters: { proposal: proposalParameter }, output, timeoutMs: 15000,
|
|
79
|
+
async execute(args, exec) {
|
|
80
|
+
if (Object.keys(args).some(key => key !== 'proposal'))
|
|
81
|
+
throw new Error('Redis 写入执行包含不支持的参数。');
|
|
82
|
+
const context = owner(exec);
|
|
83
|
+
return wireRecord(await writer.execute(context.session, context.turn, context.current, proposalSchema.parse(args.proposal), exec.signal));
|
|
84
|
+
},
|
|
85
|
+
}));
|
|
86
|
+
ctx.tools.register(defineTool({
|
|
87
|
+
name: 'redis_viewer_write_status',
|
|
88
|
+
description: '按操作 ID 查看本会话内仍保留的 Redis 写入状态;记录不可用不代表成功或失败。结果未知时不要自动重试,应先检查目标当前状态。',
|
|
89
|
+
parameters: { id: { type: 'string', required: true } }, output,
|
|
90
|
+
async execute(args, exec) {
|
|
91
|
+
if (Object.keys(args).some(key => key !== 'id') || args.id.length > 36)
|
|
92
|
+
throw new Error('Redis 操作 ID 无效。');
|
|
93
|
+
const session = exec.agent?.session;
|
|
94
|
+
if (session === undefined)
|
|
95
|
+
throw new Error('Redis 写入缺少有效的 DSH 对话身份。');
|
|
96
|
+
exec.signal.throwIfAborted();
|
|
97
|
+
const record = writer.status(session.id, args.id);
|
|
98
|
+
return record === null ? { id: args.id, state: 'unavailable', note: '记录不存在或已过期,不能推断写入结果。' } : wireRecord(record);
|
|
99
|
+
},
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { DatabaseTarget } from '../domain.js';
|
|
2
|
+
import { RedisConnections } from './connection-service.js';
|
|
3
|
+
import type { RedisWriteRecord } from '../domain.js';
|
|
4
|
+
import { type RedisWriteProposal } from './write-proposals.js';
|
|
5
|
+
export interface RedisWriteInput {
|
|
6
|
+
readonly type: 'string' | 'hash';
|
|
7
|
+
readonly keyBase64: string;
|
|
8
|
+
readonly fieldBase64: string | null;
|
|
9
|
+
readonly afterBase64: string;
|
|
10
|
+
}
|
|
11
|
+
/** Host-only coordinator; callers supply the trusted live DSH turn check. */
|
|
12
|
+
export declare class RedisAiWriter {
|
|
13
|
+
private readonly connections;
|
|
14
|
+
private readonly proposals;
|
|
15
|
+
private readonly history;
|
|
16
|
+
constructor(connections: RedisConnections);
|
|
17
|
+
enable(sessionId: string, target: DatabaseTarget): void;
|
|
18
|
+
disable(sessionId: string): void;
|
|
19
|
+
isEnabled(sessionId: string, target: DatabaseTarget): boolean;
|
|
20
|
+
endTurn(sessionId: string, turnId: string): void;
|
|
21
|
+
status(sessionId: string, id: string): RedisWriteRecord | null;
|
|
22
|
+
prepare(sessionId: string, target: DatabaseTarget, turnId: string, currentTurn: () => boolean, input: RedisWriteInput, signal: AbortSignal): Promise<RedisWriteProposal>;
|
|
23
|
+
execute(sessionId: string, turnId: string, currentTurn: () => boolean, requested: RedisWriteProposal, signal: AbortSignal): Promise<RedisWriteRecord>;
|
|
24
|
+
dispose(): void;
|
|
25
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { decodeWriteBytes } from "./write-bytes.js";
|
|
2
|
+
import { RedisConnections } from "./connection-service.js";
|
|
3
|
+
import { RedisWriteHistory } from "./write-history.js";
|
|
4
|
+
import { RedisWriteProposals } from "./write-proposals.js";
|
|
5
|
+
function bytes(value) {
|
|
6
|
+
const result = decodeWriteBytes(value);
|
|
7
|
+
if (result === null)
|
|
8
|
+
throw new Error('Redis 写入数据不是规范 Base64 或超过限制。');
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
/** Host-only coordinator; callers supply the trusted live DSH turn check. */
|
|
12
|
+
export class RedisAiWriter {
|
|
13
|
+
connections;
|
|
14
|
+
proposals = new RedisWriteProposals();
|
|
15
|
+
history = new RedisWriteHistory();
|
|
16
|
+
constructor(connections) {
|
|
17
|
+
this.connections = connections;
|
|
18
|
+
}
|
|
19
|
+
enable(sessionId, target) {
|
|
20
|
+
this.connections.assertTarget(sessionId, target);
|
|
21
|
+
this.proposals.enable(sessionId, target);
|
|
22
|
+
}
|
|
23
|
+
disable(sessionId) { this.proposals.disable(sessionId); }
|
|
24
|
+
isEnabled(sessionId, target) {
|
|
25
|
+
try {
|
|
26
|
+
this.connections.assertTarget(sessionId, target);
|
|
27
|
+
return this.proposals.isEnabled(sessionId, target);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
endTurn(sessionId, turnId) { this.proposals.endTurn(sessionId, turnId); }
|
|
34
|
+
status(sessionId, id) { return this.history.get(sessionId, id); }
|
|
35
|
+
async prepare(sessionId, target, turnId, currentTurn, input, signal) {
|
|
36
|
+
const key = bytes(input.keyBase64), after = bytes(input.afterBase64);
|
|
37
|
+
if (input.type !== 'string' && input.type !== 'hash' || (input.type === 'string' ? input.fieldBase64 !== null : input.fieldBase64 === null))
|
|
38
|
+
throw new Error('Redis 写入目标类型无效。');
|
|
39
|
+
const field = input.fieldBase64 === null ? null : bytes(input.fieldBase64);
|
|
40
|
+
if (!currentTurn() || !this.isEnabled(sessionId, target))
|
|
41
|
+
throw new Error('Redis 写入未启用或对话已变化。');
|
|
42
|
+
const token = this.proposals.beginTurn(sessionId, turnId);
|
|
43
|
+
const validate = () => {
|
|
44
|
+
if (!currentTurn() || !this.proposals.isCurrent(sessionId, turnId, token, target))
|
|
45
|
+
throw new Error('Redis 写入对话或启用状态已变化。');
|
|
46
|
+
};
|
|
47
|
+
const sample = field === null ? { type: 'string', key } : { type: 'hash', key, field };
|
|
48
|
+
const before = await this.connections.sampleExisting(sessionId, target, sample, signal, validate);
|
|
49
|
+
validate();
|
|
50
|
+
const view = this.connections.status(sessionId);
|
|
51
|
+
if (view.target === null)
|
|
52
|
+
throw new Error('Redis 连接不可用。');
|
|
53
|
+
return this.proposals.issue(sessionId, turnId, token, { target, host: view.target.host, port: view.target.port, type: input.type, keyTtl: 'none', fieldTtl: field === null ? null : 'none', keyBase64: key.toString('base64'), fieldBase64: field?.toString('base64') ?? null, beforeBase64: before.toString('base64'), afterBase64: after.toString('base64') });
|
|
54
|
+
}
|
|
55
|
+
async execute(sessionId, turnId, currentTurn, requested, signal) {
|
|
56
|
+
if (!currentTurn() || !this.isEnabled(sessionId, requested.target))
|
|
57
|
+
throw new Error('Redis 写入未启用或对话已变化。');
|
|
58
|
+
const token = this.proposals.beginTurn(sessionId, turnId);
|
|
59
|
+
const consumed = this.proposals.consume(sessionId, turnId, requested);
|
|
60
|
+
if (consumed === null)
|
|
61
|
+
throw new Error('Redis 写入提案已变化、过期或已使用。');
|
|
62
|
+
const proposal = consumed.proposal;
|
|
63
|
+
const record = this.history.start(sessionId, proposal.id, proposal.target, { type: proposal.type, keyBase64: proposal.keyBase64, fieldBase64: proposal.fieldBase64 });
|
|
64
|
+
const key = bytes(proposal.keyBase64), before = bytes(proposal.beforeBase64), after = bytes(proposal.afterBase64);
|
|
65
|
+
const request = proposal.type === 'string' ? { type: 'string', key, before, after } : { type: 'hash', key, field: bytes(proposal.fieldBase64), before, after };
|
|
66
|
+
const validate = () => { if (!currentTurn() || !this.proposals.isCurrent(sessionId, turnId, token, proposal.target) || !consumed.isCurrent())
|
|
67
|
+
throw new Error('Redis 写入提案已过期或对话已变化。'); };
|
|
68
|
+
let sampledAt;
|
|
69
|
+
const result = await this.connections.replaceExisting(sessionId, proposal.target, request, signal, validate, record.submitted, value => { sampledAt = value; });
|
|
70
|
+
record.finish(result, sampledAt);
|
|
71
|
+
const stored = this.history.get(sessionId, proposal.id);
|
|
72
|
+
if (stored === null)
|
|
73
|
+
throw new Error('Redis 操作记录不可用,请检查目标当前状态。');
|
|
74
|
+
return stored;
|
|
75
|
+
}
|
|
76
|
+
dispose() { this.proposals.dispose(); this.history.dispose(); }
|
|
77
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DatabaseTarget } from '../domain.js';
|
|
2
|
+
import type { RedisConnections } from './connection-service.js';
|
|
3
|
+
import type { RedisZSets } from './zset-reader.js';
|
|
4
|
+
import { type PreparedAiPage } from './ai-page-store.js';
|
|
5
|
+
export declare const isAiZSetCursor: (cursor: string | null) => boolean;
|
|
6
|
+
/** ZSCAN retains full member bytes and original score text; it never assigns ranks. */
|
|
7
|
+
export declare class RedisAiZSetPages {
|
|
8
|
+
private readonly connections;
|
|
9
|
+
private readonly zsets;
|
|
10
|
+
private readonly pages;
|
|
11
|
+
constructor(connections: RedisConnections, zsets: RedisZSets);
|
|
12
|
+
disposeSession(sessionId: string): void;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
prepare(sessionId: string, target: DatabaseTarget, keyBase64: string, pattern: string, cursor: string | null, selected: object | null, assertSelection: () => void, signal: AbortSignal): Promise<PreparedAiPage>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { connectionErrorMessages, keyStateMessages } from "../connection-errors.js";
|
|
2
|
+
import { byteIdentity } from "./byte-identity.js";
|
|
3
|
+
import { AiPageStore } from "./ai-page-store.js";
|
|
4
|
+
export const isAiZSetCursor = (cursor) => cursor?.startsWith('z:') === true;
|
|
5
|
+
/** ZSCAN retains full member bytes and original score text; it never assigns ranks. */
|
|
6
|
+
export class RedisAiZSetPages {
|
|
7
|
+
connections;
|
|
8
|
+
zsets;
|
|
9
|
+
pages = new AiPageStore('z:');
|
|
10
|
+
constructor(connections, zsets) {
|
|
11
|
+
this.connections = connections;
|
|
12
|
+
this.zsets = zsets;
|
|
13
|
+
}
|
|
14
|
+
disposeSession(sessionId) { this.pages.disposeSession(sessionId); this.zsets.disposeSession(sessionId); }
|
|
15
|
+
dispose() { this.pages.dispose(); this.zsets.dispose(); }
|
|
16
|
+
prepare(sessionId, target, keyBase64, pattern, cursor, selected, assertSelection, signal) {
|
|
17
|
+
signal.throwIfAborted();
|
|
18
|
+
this.connections.assertTarget(sessionId, target);
|
|
19
|
+
assertSelection();
|
|
20
|
+
const identity = byteIdentity(Buffer.from(keyBase64, 'base64'));
|
|
21
|
+
return this.pages.prepare({
|
|
22
|
+
sessionId, binding: JSON.stringify([target.id, target.generation, target.db, keyBase64, 'scan', pattern, selected !== null]), selection: selected, cursor, signal,
|
|
23
|
+
fetch: async (next) => {
|
|
24
|
+
const read = await this.zsets.read(sessionId, { target, keyBase64, mode: 'scan', pattern, cursor: next }, signal);
|
|
25
|
+
// Discovery saw ZSet; a different type in the reader means it changed.
|
|
26
|
+
return read.state === 'unsupported' && read.metadata?.type !== 'zset' ? { ...read, state: 'changed', metadata: null } : read;
|
|
27
|
+
},
|
|
28
|
+
rows: page => page.entries,
|
|
29
|
+
output: (page, entries, next, first) => {
|
|
30
|
+
const errorMessage = page.error !== null ? connectionErrorMessages[page.error].zh : page.state === 'missing' || page.state === 'changed' ? keyStateMessages[page.state].zh : null;
|
|
31
|
+
return { readOnly: true, connection: this.connections.status(sessionId), source: selected === null ? 'explicit' : 'selection', key: identity, errorMessage,
|
|
32
|
+
value: { ...page, entries, cursor: next, scanComplete: page.state === 'page' && next === null,
|
|
33
|
+
complete: page.state === 'page' && first && pattern === '*' && next === null && String(entries.length) === page.metadata?.length } };
|
|
34
|
+
},
|
|
35
|
+
validate: error => { assertSelection(); this.connections.assertReadResult(sessionId, target, error); },
|
|
36
|
+
discard: () => this.zsets.disposeSession(sessionId),
|
|
37
|
+
oversize: '单个 ZSet 成员或其必要元信息超过 AI 单次结果预算,无法完整返回;请缩小匹配范围。',
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Display labels are lossy on purpose; only Base64 is used for identity. */
|
|
2
|
+
export function byteIdentity(raw) {
|
|
3
|
+
let label;
|
|
4
|
+
try {
|
|
5
|
+
const text = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(raw);
|
|
6
|
+
label = JSON.stringify(text.slice(0, 160)) + (text.length > 160 ? '…' : '');
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
label = '0x' + raw.subarray(0, 32).toString('hex') + (raw.length > 32 ? '…' : '');
|
|
10
|
+
}
|
|
11
|
+
return { base64: raw.toString('base64'), byteLength: raw.length, label };
|
|
12
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type RedisReply } from './resp.js';
|
|
2
|
+
/** CLIENT LIST cannot paginate server-side; discard addresses, names and command text. */
|
|
3
|
+
export declare function projectClients(reply: RedisReply): {
|
|
4
|
+
clients: {
|
|
5
|
+
id: string;
|
|
6
|
+
ageSeconds: string;
|
|
7
|
+
idleSeconds: string;
|
|
8
|
+
db: string;
|
|
9
|
+
flags: string;
|
|
10
|
+
}[];
|
|
11
|
+
totalCount: number;
|
|
12
|
+
complete: boolean;
|
|
13
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { RedisFrameError } from "./resp.js";
|
|
2
|
+
/** CLIENT LIST cannot paginate server-side; discard addresses, names and command text. */
|
|
3
|
+
export function projectClients(reply) {
|
|
4
|
+
if (!Buffer.isBuffer(reply))
|
|
5
|
+
throw new RedisFrameError('protocol');
|
|
6
|
+
let raw;
|
|
7
|
+
try {
|
|
8
|
+
raw = new TextDecoder('utf-8', { fatal: true }).decode(reply);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
throw new RedisFrameError('protocol');
|
|
12
|
+
}
|
|
13
|
+
const lines = raw.split('\n');
|
|
14
|
+
if (lines.at(-1) === '')
|
|
15
|
+
lines.pop();
|
|
16
|
+
const clients = lines.slice(0, 20).map(line => {
|
|
17
|
+
const values = new Map();
|
|
18
|
+
for (const token of line.trimEnd().split(' ')) {
|
|
19
|
+
const index = token.indexOf('=');
|
|
20
|
+
if (index <= 0)
|
|
21
|
+
throw new RedisFrameError('protocol');
|
|
22
|
+
const key = token.slice(0, index);
|
|
23
|
+
if (!['id', 'age', 'idle', 'db', 'flags'].includes(key))
|
|
24
|
+
continue;
|
|
25
|
+
if (values.has(key))
|
|
26
|
+
throw new RedisFrameError('protocol');
|
|
27
|
+
values.set(key, token.slice(index + 1));
|
|
28
|
+
}
|
|
29
|
+
const id = values.get('id'), age = values.get('age'), idle = values.get('idle'), db = values.get('db'), flags = values.get('flags');
|
|
30
|
+
if (id === undefined || age === undefined || idle === undefined || db === undefined || flags === undefined || ![id, age, idle, db].every(value => /^\d{1,20}$/u.test(value)) || !/^[A-Za-z]{1,32}$/u.test(flags))
|
|
31
|
+
throw new RedisFrameError('protocol');
|
|
32
|
+
return { id, ageSeconds: age, idleSeconds: idle, db, flags };
|
|
33
|
+
});
|
|
34
|
+
return { clients, totalCount: lines.length, complete: clients.length === lines.length };
|
|
35
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type RedisReadCommand } from './redis-transport.js';
|
|
2
|
+
import type { RedisReplaceRequest, RedisSampleRequest, RedisWriteResult } from './redis-write-transaction.js';
|
|
3
|
+
import { type RedisArgument, type RedisReply } from './resp.js';
|
|
4
|
+
import type { ConnectionErrorCode, ConnectionRequest, ConnectionTarget, ConnectionView, DatabaseTarget, SavedConnection } from '../domain.js';
|
|
5
|
+
import type { RedisProfiles } from './profile-store.js';
|
|
6
|
+
/** Small Host-owned transport surface; no Redis command strings cross the RPC. */
|
|
7
|
+
export interface RedisLink {
|
|
8
|
+
close(): void;
|
|
9
|
+
execute(command: RedisReadCommand, arguments_: readonly RedisArgument[], signal: AbortSignal, beforeDispatch: () => void, deadline?: number): Promise<RedisReply>;
|
|
10
|
+
onFailure(listener: (code: ConnectionErrorCode) => void): () => void;
|
|
11
|
+
}
|
|
12
|
+
/** A writable link has the fixed Host-only replace operation, not a command escape hatch. */
|
|
13
|
+
export interface RedisWritableLink extends RedisLink {
|
|
14
|
+
sampleExisting(request: RedisSampleRequest, signal: AbortSignal, guard: () => void): Promise<Buffer>;
|
|
15
|
+
replaceExisting(request: RedisReplaceRequest, signal: AbortSignal, guard: (submitted: boolean) => void, submitted: () => void, readback: (sampledAt: string) => void): Promise<RedisWriteResult>;
|
|
16
|
+
}
|
|
17
|
+
export type RedisOpener = (settings: ConnectionRequest, signal: AbortSignal) => Promise<RedisLink>;
|
|
18
|
+
export type CandidateConnectionResult = {
|
|
19
|
+
readonly state: 'connected' | 'changed';
|
|
20
|
+
readonly view: ConnectionView;
|
|
21
|
+
readonly previousRelease: 'closed' | 'uncertain';
|
|
22
|
+
} | {
|
|
23
|
+
readonly state: 'failed';
|
|
24
|
+
readonly error: ConnectionErrorCode;
|
|
25
|
+
};
|
|
26
|
+
type CandidateCommitGate = (signal: AbortSignal, commit: () => undefined) => Promise<boolean>;
|
|
27
|
+
export declare class ConnectionFailure extends Error {
|
|
28
|
+
readonly code: ConnectionErrorCode;
|
|
29
|
+
constructor(code: ConnectionErrorCode);
|
|
30
|
+
}
|
|
31
|
+
/** Map server messages to fixed public codes, never echoing host, URI or secrets. */
|
|
32
|
+
export declare function connectionErrorCode(error: unknown): ConnectionErrorCode;
|
|
33
|
+
/** Authentication, TLS and bounded replies share one owned socket; no reconnect. */
|
|
34
|
+
export declare const openRedis: RedisOpener;
|
|
35
|
+
/** One current connection per trusted session; hiding/reopening a pane does not open another. */
|
|
36
|
+
export declare class RedisConnections {
|
|
37
|
+
private readonly open;
|
|
38
|
+
private readonly sessions;
|
|
39
|
+
private readonly candidates;
|
|
40
|
+
private readonly unreleased;
|
|
41
|
+
private generation;
|
|
42
|
+
constructor(open?: RedisOpener);
|
|
43
|
+
private view;
|
|
44
|
+
private release;
|
|
45
|
+
private close;
|
|
46
|
+
private cancelCandidate;
|
|
47
|
+
/** Internal preparation only: the gate owns authorization and must call commit synchronously. */
|
|
48
|
+
connectCandidate(sessionId: string, settings: ConnectionRequest, signal: AbortSignal, gate: CandidateCommitGate, preflight: (signal: AbortSignal) => boolean | Promise<boolean>, beforeExchange?: () => void): Promise<CandidateConnectionResult>;
|
|
49
|
+
/** Bind candidate settings to the same normalized profile verified at commit. */
|
|
50
|
+
connectProfileCandidate(sessionId: string, profile: SavedConnection, password: string, signal: AbortSignal, profiles: RedisProfiles, beforeExchange?: () => void): Promise<CandidateConnectionResult>;
|
|
51
|
+
connect(sessionId: string, settings: ConnectionRequest, signal: AbortSignal): Promise<ConnectionView>;
|
|
52
|
+
status(sessionId: string): ConnectionView;
|
|
53
|
+
/** A completed operation may have been superseded before its caller resumes. */
|
|
54
|
+
isCurrentView(sessionId: string, view: ConnectionView): boolean;
|
|
55
|
+
private require;
|
|
56
|
+
assertTarget(sessionId: string, target: DatabaseTarget): void;
|
|
57
|
+
/** A failed read may still describe this exact connection after it has failed. */
|
|
58
|
+
isFailedTarget(sessionId: string, target: DatabaseTarget, error?: ConnectionErrorCode): boolean;
|
|
59
|
+
assertReadResult(sessionId: string, target: DatabaseTarget, error: ConnectionErrorCode | null): void;
|
|
60
|
+
/** Validate again after queueing and after receipt: SELECT cannot mix old DB results. */
|
|
61
|
+
read(sessionId: string, target: DatabaseTarget, command: Exclude<RedisReadCommand, 'SELECT'>, arguments_: readonly RedisArgument[], signal: AbortSignal, validate?: () => void, deadline?: number): Promise<RedisReply>;
|
|
62
|
+
/** Keep the original result even if the connection changes after submission. */
|
|
63
|
+
sampleExisting(sessionId: string, target: DatabaseTarget, request: RedisSampleRequest, signal: AbortSignal, validate: () => void): Promise<Buffer>;
|
|
64
|
+
/** Keep the original result even if the connection changes after submission. */
|
|
65
|
+
replaceExisting(sessionId: string, target: DatabaseTarget, request: RedisReplaceRequest, signal: AbortSignal, validate: () => void, submitted: () => void, readback: (sampledAt: string) => void): Promise<RedisWriteResult>;
|
|
66
|
+
selectDatabase(sessionId: string, target: DatabaseTarget, signal: AbortSignal, onInvalidate?: () => void): Promise<ConnectionView>;
|
|
67
|
+
disconnect(sessionId: string, target: ConnectionTarget): ConnectionView;
|
|
68
|
+
disposeSession(sessionId: string): void;
|
|
69
|
+
dispose(): void;
|
|
70
|
+
}
|
|
71
|
+
export {};
|