@oxmumu3/dsh-agent-twin 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -9
- package/client.js +503 -6
- package/examples/growth-bi-desk/README.md +2 -1
- package/examples/growth-bi-desk/mtn/growth-ska-graph.json +69 -0
- package/lib/channels.js +67 -9
- package/lib/fullblood-runner.js +33 -6
- package/lib/guest-server.js +614 -137
- package/lib/host.js +38 -3
- package/lib/mtn-provider.js +284 -0
- package/lib/official-connectors.js +5 -1
- package/lib/owner-notifier.js +123 -0
- package/lib/routes.js +53 -3
- package/lib/scanner.js +145 -14
- package/lib/service.js +48 -6
- package/lib/store.js +162 -16
- package/lib/twin-engine.js +956 -440
- package/package.json +3 -3
- package/shared/schema.js +63 -0
package/lib/twin-engine.js
CHANGED
|
@@ -8,16 +8,26 @@
|
|
|
8
8
|
import { randomUUID } from 'node:crypto';
|
|
9
9
|
import {
|
|
10
10
|
DEFAULT_DAILY_TURN_CAP,
|
|
11
|
+
DEFAULT_PUBLIC_GLOBAL_DAILY_LIMIT,
|
|
12
|
+
DEFAULT_PUBLIC_VISITOR_DAILY_LIMIT,
|
|
11
13
|
PRESENCE_YIELD_MS,
|
|
12
14
|
REDLINE_PATTERNS,
|
|
13
15
|
TWIN_PERSONA_PRESETS,
|
|
14
16
|
channelIdFromSelection,
|
|
15
17
|
channelSelectionLabel,
|
|
18
|
+
normalizePublicLobbyConfig,
|
|
16
19
|
redactSecrets,
|
|
17
20
|
sanitizeSurfaceText
|
|
18
21
|
} from '../shared/schema.js';
|
|
19
22
|
import { computePolicyDigest } from './fullblood-runner.js';
|
|
20
|
-
import {
|
|
23
|
+
import { makeApprovalShortCode } from './owner-notifier.js';
|
|
24
|
+
import {
|
|
25
|
+
detectProjectDelta,
|
|
26
|
+
materializeScopedWorkspace,
|
|
27
|
+
matchesScopePaths,
|
|
28
|
+
normalizeScopePaths,
|
|
29
|
+
scanProjectDirectory
|
|
30
|
+
} from './scanner.js';
|
|
21
31
|
import { todayKey } from './store.js';
|
|
22
32
|
|
|
23
33
|
function nowTimeLabel() {
|
|
@@ -64,16 +74,61 @@ export function createTwinEngine({
|
|
|
64
74
|
onDispatchOutbound,
|
|
65
75
|
llmResponder,
|
|
66
76
|
fullBloodRunner = null,
|
|
77
|
+
ownerNotifier = null,
|
|
67
78
|
knowledgeProviders = () => []
|
|
68
79
|
}) {
|
|
69
80
|
/** In-memory owner presence timestamps keyed by `<channelId>:<threadKey>` + mirrored to caller state. */
|
|
70
81
|
const ownerPresence = new Map();
|
|
71
82
|
const pendingScans = new Map();
|
|
72
83
|
const scopeQueues = new Map();
|
|
84
|
+
const activeTurns = new Map();
|
|
85
|
+
const MAX_CONCURRENT_PUBLIC_TURNS = 3;
|
|
86
|
+
const MAX_PUBLIC_PENDING_APPROVALS = 10;
|
|
87
|
+
const PUBLIC_PUSH_COOLDOWN_MS = 10 * 60 * 1000;
|
|
88
|
+
let activePublicTurns = 0;
|
|
89
|
+
let lastPublicOwnerPushAt = 0;
|
|
73
90
|
let extractTimer = null;
|
|
74
91
|
let stepChain = Promise.resolve();
|
|
75
92
|
let disposed = false;
|
|
76
93
|
|
|
94
|
+
async function enqueueApproval(item, { setDeskOnCold = false } = {}) {
|
|
95
|
+
const withCode = {
|
|
96
|
+
...item,
|
|
97
|
+
shortCode: item.shortCode || makeApprovalShortCode(item.id)
|
|
98
|
+
};
|
|
99
|
+
await store.updateState(st => {
|
|
100
|
+
if (withCode.isPublicLobby) {
|
|
101
|
+
let keptPublic = 0;
|
|
102
|
+
st.approvals = st.approvals.filter(existing => {
|
|
103
|
+
if (!existing?.isPublicLobby) return true;
|
|
104
|
+
if (keptPublic < MAX_PUBLIC_PENDING_APPROVALS - 1) {
|
|
105
|
+
keptPublic += 1;
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
st.approvals.unshift(withCode);
|
|
112
|
+
if (setDeskOnCold && st.lifecycleStage === 'cold') st.scene = 'desk';
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (ownerNotifier && typeof ownerNotifier.notifyPendingApproval === 'function') {
|
|
116
|
+
let allowPush = true;
|
|
117
|
+
if (withCode.isPublicLobby) {
|
|
118
|
+
const pubCfg = store.getState().publicLobby;
|
|
119
|
+
const now = Date.now();
|
|
120
|
+
allowPush = Boolean(pubCfg?.pushOwner) && (now - lastPublicOwnerPushAt >= PUBLIC_PUSH_COOLDOWN_MS);
|
|
121
|
+
if (allowPush) lastPublicOwnerPushAt = now;
|
|
122
|
+
}
|
|
123
|
+
if (allowPush) {
|
|
124
|
+
await ownerNotifier.notifyPendingApproval(withCode).catch(err =>
|
|
125
|
+
logger?.warn?.('[agent-twin] ownerNotifier push warning:', err?.message)
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return withCode;
|
|
130
|
+
}
|
|
131
|
+
|
|
77
132
|
function serializeScope(scopeKey, task) {
|
|
78
133
|
const key = String(scopeKey || 'default');
|
|
79
134
|
const prev = scopeQueues.get(key) || Promise.resolve();
|
|
@@ -108,6 +163,7 @@ export function createTwinEngine({
|
|
|
108
163
|
projectName: scan.projectName,
|
|
109
164
|
name: sanitizeSurfaceText(String(c.name || `${scan.projectName} · 能力 ${idx + 1}`)).slice(0, 48),
|
|
110
165
|
enabled: true,
|
|
166
|
+
scopePaths: normalizeScopePaths(c.scopePaths || []),
|
|
111
167
|
desc: sanitizeSurfaceText(String(c.desc || '')).slice(0, 180) || '依据已授权项目实料提炼的只读能力。',
|
|
112
168
|
basis: sanitizeSurfaceText(String(c.basis || `${scan.docCount} 份授权只读文档`)).slice(0, 80),
|
|
113
169
|
output: sanitizeSurfaceText(String(c.output || '先给结论,再说明依据来源')).slice(0, 80)
|
|
@@ -436,7 +492,13 @@ export function createTwinEngine({
|
|
|
436
492
|
for (const cap of scan.capabilities) {
|
|
437
493
|
const cIdx = st.capabilities.findIndex(c => c.id === cap.id);
|
|
438
494
|
if (cIdx === -1) st.capabilities.push(cap);
|
|
439
|
-
else
|
|
495
|
+
else {
|
|
496
|
+
st.capabilities[cIdx] = {
|
|
497
|
+
...cap,
|
|
498
|
+
enabled: st.capabilities[cIdx].enabled,
|
|
499
|
+
scopePaths: normalizeScopePaths(st.capabilities[cIdx].scopePaths || cap.scopePaths || [])
|
|
500
|
+
};
|
|
501
|
+
}
|
|
440
502
|
}
|
|
441
503
|
st.pendingDeltaByProject[projectId] = false;
|
|
442
504
|
});
|
|
@@ -445,24 +507,22 @@ export function createTwinEngine({
|
|
|
445
507
|
|
|
446
508
|
async function gatherAuthorizedSnippets(personCaps, question) {
|
|
447
509
|
const s = store.getState();
|
|
448
|
-
const
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
.map(c => c.id)
|
|
452
|
-
);
|
|
453
|
-
const projectIds = new Set(
|
|
454
|
-
s.capabilities
|
|
455
|
-
.filter(c => enabledCapIds.has(c.id))
|
|
456
|
-
.map(c => c.projectId)
|
|
457
|
-
);
|
|
510
|
+
const enabledCaps = s.capabilities.filter(c => c.enabled && (!Array.isArray(personCaps) || personCaps.includes(c.id)));
|
|
511
|
+
const enabledCapIds = new Set(enabledCaps.map(c => c.id));
|
|
512
|
+
const projectIds = new Set(enabledCaps.map(c => c.projectId));
|
|
458
513
|
const qTokens = tokenizeQuery(question);
|
|
459
514
|
const matches = [];
|
|
460
515
|
for (const pid of projectIds) {
|
|
461
516
|
const index = await store.readProjectIndex(pid);
|
|
462
517
|
if (!index || !Array.isArray(index.snippets)) continue;
|
|
518
|
+
const projCaps = enabledCaps.filter(c => c.projectId === pid);
|
|
519
|
+
const normalizedLists = projCaps.map(c => normalizeScopePaths(c.scopePaths));
|
|
520
|
+
const restrictByPaths = projCaps.length > 0 && normalizedLists.every(list => list.length > 0);
|
|
521
|
+
const unionPaths = restrictByPaths ? [...new Set(normalizedLists.flat())] : [];
|
|
463
522
|
for (const sn of index.snippets) {
|
|
464
523
|
const capMatch = !Array.isArray(sn.capIds) || sn.capIds.some(cid => enabledCapIds.has(cid));
|
|
465
524
|
if (!capMatch) continue;
|
|
525
|
+
if (restrictByPaths && sn.relPath && !matchesScopePaths(sn.relPath, unionPaths)) continue;
|
|
466
526
|
const score = scoreSnippetMatch(qTokens, sn);
|
|
467
527
|
if (score > 0) matches.push({ score, projectName: index.projectName, snippet: sn });
|
|
468
528
|
}
|
|
@@ -516,307 +576,718 @@ export function createTwinEngine({
|
|
|
516
576
|
return out;
|
|
517
577
|
}
|
|
518
578
|
|
|
579
|
+
async function recordTurnDelivery({
|
|
580
|
+
person,
|
|
581
|
+
isPublicVisitor,
|
|
582
|
+
visitorId,
|
|
583
|
+
cleanQ,
|
|
584
|
+
replyText,
|
|
585
|
+
capLabel,
|
|
586
|
+
statusLabel,
|
|
587
|
+
statusTone = 'ok',
|
|
588
|
+
basisHint,
|
|
589
|
+
channelId,
|
|
590
|
+
trace,
|
|
591
|
+
approvalId = '',
|
|
592
|
+
shortCode = '',
|
|
593
|
+
dshSessionId = '',
|
|
594
|
+
policyDigest = '',
|
|
595
|
+
countTurn = true
|
|
596
|
+
}) {
|
|
597
|
+
const turn = {
|
|
598
|
+
id: `th-${randomUUID().slice(0, 8)}`,
|
|
599
|
+
time: nowTimeLabel(),
|
|
600
|
+
capLabel,
|
|
601
|
+
question: cleanQ,
|
|
602
|
+
reply: replyText,
|
|
603
|
+
statusLabel,
|
|
604
|
+
statusTone,
|
|
605
|
+
basisHint,
|
|
606
|
+
approvalId,
|
|
607
|
+
shortCode,
|
|
608
|
+
trace
|
|
609
|
+
};
|
|
610
|
+
if (isPublicVisitor && visitorId) {
|
|
611
|
+
await store.appendVisitorTurn(visitorId, turn, {
|
|
612
|
+
countTurn,
|
|
613
|
+
dshSessionId,
|
|
614
|
+
lastPolicyDigest: policyDigest
|
|
615
|
+
});
|
|
616
|
+
await store.incrementPublicLobbyUsage();
|
|
617
|
+
} else if (person) {
|
|
618
|
+
await store.appendCallerTurn(person.id, turn, {
|
|
619
|
+
countTurn,
|
|
620
|
+
dshSessionId,
|
|
621
|
+
lastPolicyDigest: policyDigest
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
await store.updateState(st => {
|
|
626
|
+
if (!isPublicVisitor && person) {
|
|
627
|
+
const target = st.people.find(p => p.id === person.id);
|
|
628
|
+
if (target) {
|
|
629
|
+
target.servedCount = (target.servedCount || 0) + 1;
|
|
630
|
+
target.lastActive = '刚刚';
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
st.deliveries.unshift({
|
|
634
|
+
id: `del-${randomUUID().slice(0, 8)}`,
|
|
635
|
+
personId: person?.id || '',
|
|
636
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
637
|
+
time: nowTimeLabel(),
|
|
638
|
+
who: person?.name || '访客',
|
|
639
|
+
whoNote: person?.role || '公开扫码体验',
|
|
640
|
+
channel: channelSelectionLabel(channelId),
|
|
641
|
+
summary: replyText.slice(0, 52),
|
|
642
|
+
statusLabel,
|
|
643
|
+
statusTone,
|
|
644
|
+
trace
|
|
645
|
+
});
|
|
646
|
+
st.deliveries = st.deliveries.slice(0, 40);
|
|
647
|
+
});
|
|
648
|
+
return turn;
|
|
649
|
+
}
|
|
650
|
+
|
|
519
651
|
/**
|
|
520
|
-
*
|
|
521
|
-
* and owner self-test / caller preview sandboxes.
|
|
652
|
+
* Shared Scoped Twin execution kernel for private callers and fail-closed `/try` public visitors.
|
|
522
653
|
*/
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
654
|
+
function executeCoreTurn({
|
|
655
|
+
person = null,
|
|
656
|
+
twinProfile,
|
|
657
|
+
activeCaps,
|
|
658
|
+
cleanQ,
|
|
526
659
|
channelId = 'ch-web',
|
|
527
|
-
|
|
528
|
-
isGroup = false,
|
|
529
|
-
mentionsBot = true,
|
|
660
|
+
replyContext = {},
|
|
530
661
|
isOwnerSelfTest = false,
|
|
531
662
|
sandboxOnly = false,
|
|
532
|
-
|
|
533
|
-
|
|
663
|
+
forceDeterministic = false,
|
|
664
|
+
onProgress = null,
|
|
665
|
+
threadAdapter = null,
|
|
666
|
+
materializeScopeKey = 'selftest',
|
|
667
|
+
turnLockKey = 'selftest',
|
|
668
|
+
reuseSandbox = false,
|
|
669
|
+
isPublicVisitor = false,
|
|
670
|
+
visitorId = '',
|
|
671
|
+
dailyLimit = 0,
|
|
672
|
+
globalDailyLimit = 0
|
|
534
673
|
}) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
674
|
+
return serializeScope(turnLockKey, async () => {
|
|
675
|
+
const s = store.getState();
|
|
676
|
+
const secretVals = store.allSecretValues();
|
|
677
|
+
const latestThread = threadAdapter && !sandboxOnly && !isOwnerSelfTest
|
|
678
|
+
? await threadAdapter.readThread()
|
|
679
|
+
: null;
|
|
680
|
+
const currentTodayCount = latestThread?.dailyTurns?.date === todayKey()
|
|
681
|
+
? (latestThread.dailyTurns.count || 0)
|
|
682
|
+
: 0;
|
|
683
|
+
|
|
684
|
+
if (dailyLimit > 0 && currentTodayCount >= dailyLimit) {
|
|
685
|
+
return isPublicVisitor
|
|
686
|
+
? {
|
|
687
|
+
action: 'rate_limited',
|
|
688
|
+
limitReached: true,
|
|
689
|
+
remainingTurns: 0,
|
|
690
|
+
visitorDailyLimit: dailyLimit,
|
|
691
|
+
statusLabel: '你今日的体验次数已用完',
|
|
692
|
+
reply: `你今天在本公开大厅的 ${dailyLimit} 轮免费体验次数已用完(会话独立计数)。如果觉得效果不错,按下方「30 秒自建同款分身」提示在 DeepSeek Harness 中安装即可无限使用!`
|
|
693
|
+
}
|
|
694
|
+
: {
|
|
695
|
+
action: 'rate_limited',
|
|
696
|
+
statusLabel: '已达单日回复上限',
|
|
697
|
+
reply: '今天的分身自动化答疑已达到单日轮次上限,避免刷屏与过度消耗。如有紧急问题请直接联系项目负责人,本工具无任何付费升级限制。'
|
|
698
|
+
};
|
|
699
|
+
}
|
|
539
700
|
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
701
|
+
if (isPublicVisitor && globalDailyLimit > 0) {
|
|
702
|
+
const globalToday = s.publicLobbyUsage?.date === todayKey() ? (s.publicLobbyUsage.count || 0) : 0;
|
|
703
|
+
if (globalToday >= globalDailyLimit) {
|
|
704
|
+
return {
|
|
705
|
+
action: 'rate_limited',
|
|
706
|
+
limitReached: true,
|
|
707
|
+
remainingTurns: 0,
|
|
708
|
+
visitorDailyLimit: dailyLimit,
|
|
709
|
+
statusLabel: '公开体验今日总接待已满',
|
|
710
|
+
reply: `本机公开大厅今日 ${globalDailyLimit} 轮免费接待总上限已满(防刷熔断),明日自动恢复;也可按底部教程 30 秒把自己的工作区部署为同款 AI 分身。`
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
}
|
|
547
714
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
715
|
+
const turnTrackId = randomUUID().slice(0, 8);
|
|
716
|
+
const visitorDailyLimit = dailyLimit || DEFAULT_PUBLIC_VISITOR_DAILY_LIMIT;
|
|
717
|
+
const remainingAfterTurn = isPublicVisitor
|
|
718
|
+
? Math.max(0, visitorDailyLimit - (currentTodayCount + 1))
|
|
719
|
+
: undefined;
|
|
720
|
+
|
|
721
|
+
activeTurns.set(turnTrackId, {
|
|
722
|
+
id: turnTrackId,
|
|
723
|
+
callerId: person?.id || 'selftest',
|
|
724
|
+
personName: person?.name || '本机自测',
|
|
725
|
+
channelId,
|
|
726
|
+
question: cleanQ.slice(0, 64),
|
|
727
|
+
startedAt: Date.now(),
|
|
728
|
+
latestStep: null
|
|
729
|
+
});
|
|
553
730
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
if (isGroup && !mentionsBot) {
|
|
566
|
-
return { action: 'ignored', reason: 'group_not_mentioned' };
|
|
567
|
-
}
|
|
568
|
-
if (threadKey && isOwnerPresentRecently(channelId, threadKey)) {
|
|
569
|
-
const yieldNotice = {
|
|
570
|
-
id: `del-yield-${Date.now()}`,
|
|
571
|
-
personId: person.id,
|
|
572
|
-
time: nowTimeLabel(),
|
|
573
|
-
who: person.name,
|
|
574
|
-
whoNote: person.role,
|
|
575
|
-
channel: channelSelectionLabel(channelId),
|
|
576
|
-
summary: '检测到本人在原会话 15 分钟内发言,分身自动收声未抢答',
|
|
577
|
-
statusLabel: '本人在场自动收声',
|
|
578
|
-
statusTone: 'muted',
|
|
579
|
-
trace: {
|
|
580
|
-
intro: '15 分钟内检测到负责人在同一会话亲自回复过,触发本人在场收声保护。',
|
|
581
|
-
steps: [{ title: '检测最近真人互动', detail: '保持现场沟通顺畅,不做重复插话。' }],
|
|
582
|
-
techNote: '15 分钟计时窗口自动刷新。'
|
|
731
|
+
const emitProgress = step => {
|
|
732
|
+
if (!step) return;
|
|
733
|
+
const entry = activeTurns.get(turnTrackId);
|
|
734
|
+
if (entry) entry.latestStep = step;
|
|
735
|
+
if (typeof onProgress === 'function') {
|
|
736
|
+
try {
|
|
737
|
+
onProgress(step);
|
|
738
|
+
} catch {
|
|
739
|
+
// ignore streaming listener errors
|
|
583
740
|
}
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
try {
|
|
745
|
+
emitProgress({
|
|
746
|
+
phase: 'auth',
|
|
747
|
+
title: '核对身份与能力范围',
|
|
748
|
+
detail: person ? `${person.name} · 独立隔离会话` : '本机只读自测'
|
|
588
749
|
});
|
|
589
|
-
return { action: 'yielded', statusLabel: '本人在场自动收声', delivery: yieldNotice };
|
|
590
|
-
}
|
|
591
750
|
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
751
|
+
const externalKnowledge = await queryExternalProviders(cleanQ, person, activeCaps);
|
|
752
|
+
if (externalKnowledge.length > 0) {
|
|
753
|
+
emitProgress({
|
|
754
|
+
phase: 'mtn',
|
|
755
|
+
title: '命中语义知识图谱',
|
|
756
|
+
detail: `${externalKnowledge.length} 条关联口径`
|
|
757
|
+
});
|
|
758
|
+
}
|
|
602
759
|
|
|
603
|
-
|
|
604
|
-
|
|
760
|
+
// Path A: Full-Blooded Native DSH Agent Session + Thin Harness
|
|
761
|
+
if (!forceDeterministic && fullBloodRunner?.isAvailable?.()) {
|
|
762
|
+
try {
|
|
763
|
+
const startedAt = Date.now();
|
|
764
|
+
const runMaterialize = () => materializeScopedWorkspace({
|
|
765
|
+
rootDir: store.paths.rootDir,
|
|
766
|
+
scopeKey: materializeScopeKey,
|
|
767
|
+
mountedProjects: s.mountedProjects,
|
|
768
|
+
capabilities: s.capabilities,
|
|
769
|
+
allowedCapIds: activeCaps,
|
|
770
|
+
reuseIfUnchanged: reuseSandbox
|
|
771
|
+
});
|
|
772
|
+
const mat = reuseSandbox
|
|
773
|
+
? await serializeScope(`materialize-${materializeScopeKey}`, runMaterialize)
|
|
774
|
+
: await runMaterialize();
|
|
775
|
+
|
|
776
|
+
const digest = computePolicyDigest({
|
|
777
|
+
twinProfile,
|
|
778
|
+
rules: s.rules,
|
|
779
|
+
mode: person?.mode || 'direct',
|
|
780
|
+
note: person?.note || '',
|
|
781
|
+
activeCaps
|
|
782
|
+
});
|
|
783
|
+
const canReuseSession = Boolean(threadAdapter && !sandboxOnly && !isOwnerSelfTest && latestThread?.dshSessionId);
|
|
784
|
+
const includeFullPolicy = !canReuseSession || latestThread?.lastPolicyDigest !== digest;
|
|
785
|
+
|
|
786
|
+
const aiRes = await fullBloodRunner.runCallerTurn({
|
|
787
|
+
callerId: person?.id || 'selftest',
|
|
788
|
+
sessionId: canReuseSession ? latestThread.dshSessionId : '',
|
|
789
|
+
sandboxCwd: mat.sandboxDir,
|
|
790
|
+
twinProfile,
|
|
791
|
+
rules: s.rules,
|
|
792
|
+
person,
|
|
793
|
+
question: cleanQ,
|
|
794
|
+
externalKnowledge: externalKnowledge.map(k => k.snippet),
|
|
795
|
+
includeFullPolicy,
|
|
796
|
+
onProgress: emitProgress
|
|
797
|
+
});
|
|
605
798
|
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
const latestThread = person && !sandboxOnly && !isOwnerSelfTest
|
|
613
|
-
? await store.readCallerThread(person.id)
|
|
614
|
-
: callerThread;
|
|
615
|
-
const mat = await materializeScopedWorkspace({
|
|
616
|
-
rootDir: store.paths.rootDir,
|
|
617
|
-
scopeKey,
|
|
618
|
-
mountedProjects: s.mountedProjects,
|
|
619
|
-
capabilities: s.capabilities,
|
|
620
|
-
allowedCapIds: activeCaps
|
|
621
|
-
});
|
|
799
|
+
if (threadAdapter && (canReuseSession || (!sandboxOnly && !isOwnerSelfTest && aiRes.sessionId))) {
|
|
800
|
+
await threadAdapter.updateMeta({
|
|
801
|
+
dshSessionId: aiRes.sessionId || latestThread?.dshSessionId || '',
|
|
802
|
+
lastPolicyDigest: digest
|
|
803
|
+
});
|
|
804
|
+
}
|
|
622
805
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
806
|
+
const linkedProjects = mat.linkedProjects;
|
|
807
|
+
const policyDigest = digest;
|
|
808
|
+
|
|
809
|
+
const durationSec = `${Math.max(0.2, (Date.now() - startedAt) / 1000).toFixed(1)} 秒`;
|
|
810
|
+
const cleanReply = redactSecrets(sanitizeSurfaceText(aiRes.reply), secretVals);
|
|
811
|
+
const steps = Array.isArray(aiRes.traceSteps) && aiRes.traceSteps.length > 0
|
|
812
|
+
? aiRes.traceSteps
|
|
813
|
+
: [{ title: '只读核对授权沙箱', detail: linkedProjects.map(p => `${p.name}(${p.linkedCount}份)`).join('、') || '授权口径范围' }];
|
|
814
|
+
|
|
815
|
+
if (aiRes.decision === 'REDLINE') {
|
|
816
|
+
if (sandboxOnly) {
|
|
817
|
+
return {
|
|
818
|
+
action: 'redline_intercepted',
|
|
819
|
+
statusLabel: '待拍板 + 敏感拦截',
|
|
820
|
+
reply: cleanReply,
|
|
821
|
+
reason: aiRes.reason || '命中红线拦截'
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
const appItem = {
|
|
825
|
+
id: `app-red-${randomUUID().slice(0, 8)}`,
|
|
826
|
+
personId: person?.id || 'p-self',
|
|
827
|
+
personName: person?.name || '自测预演',
|
|
828
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
829
|
+
visitorId: isPublicVisitor ? visitorId : '',
|
|
830
|
+
channel: `${channelSelectionLabel(channelId)} · ${nowTimeLabel()}`,
|
|
831
|
+
tone: 'danger',
|
|
832
|
+
tag: '待拍板 + 敏感拦截',
|
|
833
|
+
headline: `${person?.name || '访客'} 触发红线拦截:${aiRes.reason || '涉敏或越界请求'}`,
|
|
834
|
+
gist: '满血分身核对规则后已自动拦截越权内容,并拟好安全替代回复等你确认。',
|
|
835
|
+
question: cleanQ,
|
|
836
|
+
draft: cleanReply,
|
|
837
|
+
primaryActionLabel: '批准发出安全回复',
|
|
838
|
+
allowRuleExtract: false,
|
|
839
|
+
basisHint: `规则与红线拦截(${aiRes.reason || '安全边界'})`,
|
|
840
|
+
replyContext,
|
|
841
|
+
trace: {
|
|
842
|
+
intro: `${person?.name || '访客'} 的消息经隔离分身语义审查命中工作台红线。`,
|
|
843
|
+
steps,
|
|
844
|
+
techNote: '隔离只读沙箱 + 模型外双层校验:敏感目录已物理剔除,拦截未外发。'
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
const savedRedline = await enqueueApproval(appItem, { setDeskOnCold: true });
|
|
848
|
+
const safeFallbackReply = isPublicVisitor
|
|
849
|
+
? `这个问题涉及敏感边界或未公开事项,已实时拦截并生成待拍板卡片 #${savedRedline.shortCode}。你可以在下方体验常规问题,或点击底部用 30 秒把自己的工作区部署成同款分身。`
|
|
850
|
+
: '这个问题涉及敏感边界或需本人拍板事项,已转呈负责人确认,稍后答复你。';
|
|
851
|
+
|
|
852
|
+
if (isPublicVisitor) {
|
|
853
|
+
await recordTurnDelivery({
|
|
854
|
+
person,
|
|
855
|
+
isPublicVisitor: true,
|
|
856
|
+
visitorId,
|
|
857
|
+
cleanQ,
|
|
858
|
+
replyText: safeFallbackReply,
|
|
859
|
+
capLabel: '敏感红线拦截',
|
|
860
|
+
statusLabel: `已拦下 · #${savedRedline.shortCode}`,
|
|
861
|
+
statusTone: 'warn',
|
|
862
|
+
basisHint: appItem.basisHint,
|
|
863
|
+
channelId,
|
|
864
|
+
trace: appItem.trace,
|
|
865
|
+
approvalId: savedRedline.id,
|
|
866
|
+
shortCode: savedRedline.shortCode,
|
|
867
|
+
dshSessionId: aiRes.sessionId || '',
|
|
868
|
+
policyDigest
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
return {
|
|
873
|
+
action: 'pending_approval',
|
|
874
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
875
|
+
approvalId: savedRedline.id,
|
|
876
|
+
shortCode: savedRedline.shortCode,
|
|
877
|
+
statusLabel: isPublicVisitor ? `已拦下 · 待拍板 #${savedRedline.shortCode}` : '已拦下并待本人拍板',
|
|
878
|
+
draft: cleanReply,
|
|
879
|
+
safeFallbackReply,
|
|
880
|
+
...(isPublicVisitor ? { reply: safeFallbackReply, remainingTurns: remainingAfterTurn, visitorDailyLimit } : {})
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
if (aiRes.decision === 'GAP') {
|
|
885
|
+
if (sandboxOnly) {
|
|
886
|
+
return {
|
|
887
|
+
action: 'knowledge_gap',
|
|
888
|
+
statusLabel: '触发待补口径(沙箱预览)',
|
|
889
|
+
reply: cleanReply
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
if (isOwnerSelfTest) {
|
|
893
|
+
const selfRes = {
|
|
894
|
+
question: cleanQ,
|
|
895
|
+
answer: cleanReply,
|
|
896
|
+
basis: '隔离工作区实查(未命中明确口径 · 已标待补)',
|
|
897
|
+
duration: durationSec
|
|
898
|
+
};
|
|
899
|
+
await store.updateState(st => { st.selfTestTurn = selfRes; });
|
|
900
|
+
return { action: 'replied', ...selfRes, reply: selfRes.answer };
|
|
901
|
+
}
|
|
902
|
+
const gapItem = {
|
|
903
|
+
id: `app-gap-${randomUUID().slice(0, 8)}`,
|
|
904
|
+
personId: person.id,
|
|
905
|
+
personName: person.name,
|
|
906
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
907
|
+
visitorId: isPublicVisitor ? visitorId : '',
|
|
908
|
+
channel: `${channelSelectionLabel(channelId)} · ${nowTimeLabel()}`,
|
|
909
|
+
tone: 'amber',
|
|
910
|
+
tag: '待补口径',
|
|
911
|
+
headline: `${person.name} 问到授权资料外问题,待你补充口径`,
|
|
912
|
+
gist: cleanReply.slice(0, 64),
|
|
913
|
+
question: cleanQ,
|
|
914
|
+
draft: cleanReply,
|
|
915
|
+
primaryActionLabel: '补好并回复对方',
|
|
916
|
+
allowRuleExtract: true,
|
|
917
|
+
basisHint: aiRes.reason || '授权资料核查未覆盖',
|
|
918
|
+
replyContext,
|
|
919
|
+
trace: {
|
|
920
|
+
intro: `${person.name} 的问题超出当前已授权项目文档覆盖范围。`,
|
|
921
|
+
steps,
|
|
922
|
+
techNote: '宁可转交负责人补充口径,绝不编造数字或越权读取未授权目录。'
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
const savedGap = await enqueueApproval(gapItem);
|
|
926
|
+
const gapHoldingReply = isPublicVisitor
|
|
927
|
+
? `这个问题暂未收录在当前公开体验工作区的授权资料中(已记入待补口径 #${savedGap.shortCode}),严禁编造答案。`
|
|
928
|
+
: '这个问题暂未在我当前掌握的公开口径文档中明确定义,我已转请负责人核实补充,确认后会第一时间给你答复。';
|
|
929
|
+
if (isPublicVisitor) {
|
|
930
|
+
await recordTurnDelivery({
|
|
931
|
+
person,
|
|
932
|
+
isPublicVisitor: true,
|
|
933
|
+
visitorId,
|
|
934
|
+
cleanQ,
|
|
935
|
+
replyText: gapHoldingReply,
|
|
936
|
+
capLabel: '待补口径边界',
|
|
937
|
+
statusLabel: `待补口径 · #${savedGap.shortCode}`,
|
|
938
|
+
statusTone: 'warn',
|
|
939
|
+
basisHint: gapItem.basisHint,
|
|
940
|
+
channelId,
|
|
941
|
+
trace: gapItem.trace,
|
|
942
|
+
approvalId: savedGap.id,
|
|
943
|
+
shortCode: savedGap.shortCode,
|
|
944
|
+
dshSessionId: aiRes.sessionId || '',
|
|
945
|
+
policyDigest
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
return {
|
|
949
|
+
action: 'knowledge_gap',
|
|
950
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
951
|
+
approvalId: savedGap.id,
|
|
952
|
+
shortCode: savedGap.shortCode,
|
|
953
|
+
statusLabel: isPublicVisitor ? `待补口径 · #${savedGap.shortCode}` : '待补口径已转服务台',
|
|
954
|
+
reply: gapHoldingReply,
|
|
955
|
+
...(isPublicVisitor ? { remainingTurns: remainingAfterTurn, visitorDailyLimit } : {})
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
if (sandboxOnly) {
|
|
960
|
+
return {
|
|
961
|
+
action: 'replied',
|
|
962
|
+
statusLabel: '满血沙箱试答(未写入外部记录)',
|
|
963
|
+
reply: cleanReply,
|
|
964
|
+
basis: linkedProjects.map(p => p.name).join('、') || '授权只读工作区'
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
if (isOwnerSelfTest) {
|
|
969
|
+
const selfRes = {
|
|
970
|
+
question: cleanQ,
|
|
971
|
+
answer: cleanReply,
|
|
972
|
+
basis: `${linkedProjects.map(p => p.name).join('、') || '授权工作区'}(满血只读实查 · ${steps.length} 步)`,
|
|
973
|
+
duration: durationSec
|
|
974
|
+
};
|
|
975
|
+
await store.updateState(st => { st.selfTestTurn = selfRes; });
|
|
976
|
+
return { action: 'replied', ...selfRes, reply: cleanReply };
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
const trace = {
|
|
980
|
+
intro: `收到 ${person.name} 通过「${channelSelectionLabel(channelId)}」的提问,已在独立只读工作区完成满血核查。`,
|
|
981
|
+
steps,
|
|
982
|
+
techNote: isPublicVisitor
|
|
983
|
+
? '公开体验会话按匿名 Cookie 隔离,与私有项目工作区物理分离。'
|
|
984
|
+
: '单人会话物理隔离与独立 DSH 会话上下文,不含他人历史或密钥文件。'
|
|
985
|
+
};
|
|
644
986
|
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
987
|
+
const shouldAutoReply = isPublicVisitor
|
|
988
|
+
? true
|
|
989
|
+
: (s.dutyMode === 'live' && (person.mode === 'direct' || person.mode === 'summary'));
|
|
990
|
+
if (shouldAutoReply) {
|
|
991
|
+
const statusLabel = isPublicVisitor ? '满血只读解答完成' : '通道回执已送达';
|
|
992
|
+
const turn = await recordTurnDelivery({
|
|
993
|
+
person,
|
|
994
|
+
isPublicVisitor,
|
|
995
|
+
visitorId,
|
|
996
|
+
cleanQ,
|
|
997
|
+
replyText: cleanReply,
|
|
998
|
+
capLabel: linkedProjects[0]?.name || '只读核查',
|
|
999
|
+
statusLabel,
|
|
1000
|
+
statusTone: 'ok',
|
|
1001
|
+
basisHint: `已实查「${linkedProjects.map(p => p.name).join('、') || '授权工作区'}」`,
|
|
1002
|
+
channelId,
|
|
1003
|
+
trace,
|
|
1004
|
+
dshSessionId: aiRes.sessionId || latestThread?.dshSessionId || '',
|
|
1005
|
+
policyDigest
|
|
649
1006
|
});
|
|
1007
|
+
return {
|
|
1008
|
+
action: 'replied',
|
|
1009
|
+
reply: cleanReply,
|
|
1010
|
+
statusLabel,
|
|
1011
|
+
turn,
|
|
1012
|
+
...(isPublicVisitor ? { remainingTurns: remainingAfterTurn, visitorDailyLimit } : {})
|
|
1013
|
+
};
|
|
650
1014
|
}
|
|
651
1015
|
|
|
652
|
-
|
|
653
|
-
|
|
1016
|
+
const draftApproval = {
|
|
1017
|
+
id: `app-${randomUUID().slice(0, 8)}`,
|
|
1018
|
+
personId: person.id,
|
|
1019
|
+
personName: person.name,
|
|
1020
|
+
channel: `${channelSelectionLabel(channelId)} · ${nowTimeLabel()}`,
|
|
1021
|
+
tone: 'amber',
|
|
1022
|
+
tag: '待过目草稿',
|
|
1023
|
+
headline: `${person.name} 提问:${cleanQ.slice(0, 26)}`,
|
|
1024
|
+
gist: cleanReply.slice(0, 56),
|
|
1025
|
+
question: cleanQ,
|
|
1026
|
+
draft: cleanReply,
|
|
1027
|
+
primaryActionLabel: '批准发出',
|
|
1028
|
+
allowRuleExtract: true,
|
|
1029
|
+
basisHint: `已实查「${linkedProjects.map(p => p.name).join('、') || '授权工作区'}」`,
|
|
1030
|
+
replyContext,
|
|
1031
|
+
trace
|
|
1032
|
+
};
|
|
1033
|
+
const savedDraft = await enqueueApproval(draftApproval);
|
|
1034
|
+
return {
|
|
1035
|
+
action: 'pending_approval',
|
|
1036
|
+
approvalId: savedDraft.id,
|
|
1037
|
+
shortCode: savedDraft.shortCode,
|
|
1038
|
+
statusLabel: '草稿待本人拍板',
|
|
1039
|
+
draft: cleanReply
|
|
1040
|
+
};
|
|
1041
|
+
} catch (err) {
|
|
1042
|
+
logger?.warn?.('[agent-twin] full-blood turn error, falling back to deterministic engine:', err?.message);
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
654
1045
|
|
|
655
|
-
|
|
1046
|
+
// Path B: Deterministic Offline Fallback
|
|
1047
|
+
const violation = detectRedlineViolation(cleanQ, person);
|
|
656
1048
|
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
1049
|
+
if (violation) {
|
|
1050
|
+
const draft = violation.kind === 'write'
|
|
1051
|
+
? '按只读安全红线,分身无权修改代码、写入数据库或执行变更脚本;我已将你的诉求转交给负责人本人评估处理。'
|
|
1052
|
+
: violation.kind === 'commitment'
|
|
1053
|
+
? '具体排期、商务报价或未公开策略需要由负责人本人核定;已将你的诉求转入待办,确认后第一时间回复你。'
|
|
1054
|
+
: '按只读隐私红线,不能提供逐笔客户手机号或原始流水明细;如需分析波动结构,我可以先为你整理脱敏汇总占比趋势。';
|
|
661
1055
|
|
|
662
|
-
if (aiRes.decision === 'REDLINE') {
|
|
663
1056
|
if (sandboxOnly) {
|
|
664
1057
|
return {
|
|
665
1058
|
action: 'redline_intercepted',
|
|
666
|
-
statusLabel:
|
|
667
|
-
reply:
|
|
668
|
-
reason:
|
|
1059
|
+
statusLabel: violation.label,
|
|
1060
|
+
reply: draft,
|
|
1061
|
+
reason: violation.reason
|
|
669
1062
|
};
|
|
670
1063
|
}
|
|
1064
|
+
|
|
671
1065
|
const appItem = {
|
|
672
1066
|
id: `app-red-${randomUUID().slice(0, 8)}`,
|
|
673
1067
|
personId: person?.id || 'p-self',
|
|
674
1068
|
personName: person?.name || '自测预演',
|
|
1069
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
1070
|
+
visitorId: isPublicVisitor ? visitorId : '',
|
|
675
1071
|
channel: `${channelSelectionLabel(channelId)} · ${nowTimeLabel()}`,
|
|
676
1072
|
tone: 'danger',
|
|
677
|
-
tag:
|
|
678
|
-
headline: `${person?.name || '访客'} 触发红线拦截:${
|
|
679
|
-
gist: '
|
|
1073
|
+
tag: violation.label,
|
|
1074
|
+
headline: `${person?.name || '访客'} 触发红线拦截:${violation.reason}`,
|
|
1075
|
+
gist: '已自动挡回越权明细或变更操作,并准备好安全替代回复等你确认。',
|
|
680
1076
|
question: cleanQ,
|
|
681
|
-
draft
|
|
1077
|
+
draft,
|
|
682
1078
|
primaryActionLabel: '批准发出安全回复',
|
|
683
1079
|
allowRuleExtract: false,
|
|
684
|
-
basisHint:
|
|
1080
|
+
basisHint: `红线规则触发(${violation.reason})`,
|
|
685
1081
|
replyContext,
|
|
686
1082
|
trace: {
|
|
687
|
-
intro: `${person?.name || '访客'}
|
|
688
|
-
steps
|
|
689
|
-
|
|
1083
|
+
intro: `${person?.name || '访客'} 的消息触发了工作台安全红线。`,
|
|
1084
|
+
steps: [
|
|
1085
|
+
{ title: '边界校验命中红线', detail: violation.reason },
|
|
1086
|
+
{ title: '生成脱敏替代答复', detail: '阻断原始字段或写操作,待本人审核发出。' }
|
|
1087
|
+
],
|
|
1088
|
+
techNote: '红线规则先于任何资料检索执行,零越权泄露风险。'
|
|
690
1089
|
}
|
|
691
1090
|
};
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
1091
|
+
|
|
1092
|
+
const savedViolation = await enqueueApproval(appItem, { setDeskOnCold: true });
|
|
1093
|
+
const safeFallbackReply = isPublicVisitor
|
|
1094
|
+
? `这个问题涉及敏感边界或未公开事项,已实时拦截并生成待拍板卡片 #${savedViolation.shortCode}。你可以在下方体验常规问题,或点击底部用 30 秒把自己的工作区部署成同款分身。`
|
|
1095
|
+
: '这个问题涉及敏感边界或需本人拍板事项,已转呈负责人确认,稍后答复你。';
|
|
1096
|
+
|
|
1097
|
+
if (isPublicVisitor) {
|
|
1098
|
+
await recordTurnDelivery({
|
|
1099
|
+
person,
|
|
1100
|
+
isPublicVisitor: true,
|
|
1101
|
+
visitorId,
|
|
1102
|
+
cleanQ,
|
|
1103
|
+
replyText: safeFallbackReply,
|
|
1104
|
+
capLabel: violation.label,
|
|
1105
|
+
statusLabel: `已拦下 · #${savedViolation.shortCode}`,
|
|
1106
|
+
statusTone: 'warn',
|
|
1107
|
+
basisHint: appItem.basisHint,
|
|
1108
|
+
channelId,
|
|
1109
|
+
trace: appItem.trace,
|
|
1110
|
+
approvalId: savedViolation.id,
|
|
1111
|
+
shortCode: savedViolation.shortCode
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
|
|
696
1115
|
return {
|
|
697
1116
|
action: 'pending_approval',
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
1117
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
1118
|
+
approvalId: savedViolation.id,
|
|
1119
|
+
shortCode: savedViolation.shortCode,
|
|
1120
|
+
statusLabel: isPublicVisitor ? `已拦下 · 待拍板 #${savedViolation.shortCode}` : '已拦下并待本人拍板',
|
|
1121
|
+
draft,
|
|
1122
|
+
safeFallbackReply,
|
|
1123
|
+
...(isPublicVisitor ? { reply: safeFallbackReply, remainingTurns: remainingAfterTurn, visitorDailyLimit } : {})
|
|
702
1124
|
};
|
|
703
1125
|
}
|
|
704
1126
|
|
|
705
|
-
|
|
1127
|
+
const localMatches = await gatherAuthorizedSnippets(activeCaps, cleanQ);
|
|
1128
|
+
const matches = [...externalKnowledge, ...localMatches].sort((a, b) => b.score - a.score);
|
|
1129
|
+
const hasCustomKnowledge = matches.length > 0 && matches[0].score >= 2;
|
|
1130
|
+
const isExplicitGapPrompt = /(未公开|内部底价|定价策略|内幕|保密|还没写)/.test(cleanQ);
|
|
1131
|
+
|
|
1132
|
+
if (!hasCustomKnowledge || isExplicitGapPrompt) {
|
|
1133
|
+
const gapDraft = '感谢提问,这块暂未写入当前已授权的项目口径文档,我已向负责人提交待补口径说明,确认准确边界后会补充回复你。';
|
|
706
1134
|
if (sandboxOnly) {
|
|
707
1135
|
return {
|
|
708
1136
|
action: 'knowledge_gap',
|
|
709
1137
|
statusLabel: '触发待补口径(沙箱预览)',
|
|
710
|
-
reply:
|
|
1138
|
+
reply: '这个问题涉及尚未写入授权资料库的口径细节,我先记下并转负责人「待补口径」补充,确认后第一时间答复你。'
|
|
711
1139
|
};
|
|
712
1140
|
}
|
|
1141
|
+
|
|
713
1142
|
if (isOwnerSelfTest) {
|
|
714
1143
|
const selfRes = {
|
|
715
1144
|
question: cleanQ,
|
|
716
|
-
answer:
|
|
717
|
-
basis:
|
|
718
|
-
duration:
|
|
1145
|
+
answer: '在当前授权的只读项目文档中未命中足够支撑(触发「待补口径」保护,未瞎编回答);若来自真实访客,会自动向你服务台发起口径补充卡片。',
|
|
1146
|
+
basis: '本地授权范围核查(未超出授权文件夹)',
|
|
1147
|
+
duration: '0.2 秒'
|
|
719
1148
|
};
|
|
720
1149
|
await store.updateState(st => { st.selfTestTurn = selfRes; });
|
|
721
1150
|
return { action: 'replied', ...selfRes, reply: selfRes.answer };
|
|
722
1151
|
}
|
|
1152
|
+
|
|
723
1153
|
const gapItem = {
|
|
724
1154
|
id: `app-gap-${randomUUID().slice(0, 8)}`,
|
|
725
1155
|
personId: person.id,
|
|
726
1156
|
personName: person.name,
|
|
1157
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
1158
|
+
visitorId: isPublicVisitor ? visitorId : '',
|
|
727
1159
|
channel: `${channelSelectionLabel(channelId)} · ${nowTimeLabel()}`,
|
|
728
1160
|
tone: 'amber',
|
|
729
1161
|
tag: '待补口径',
|
|
730
1162
|
headline: `${person.name} 问到授权资料外问题,待你补充口径`,
|
|
731
|
-
gist:
|
|
1163
|
+
gist: '已先礼貌告知正在核实(严禁胡编),等你填补结论后可顺手记成长期规矩。',
|
|
732
1164
|
question: cleanQ,
|
|
733
|
-
draft:
|
|
1165
|
+
draft: gapDraft,
|
|
734
1166
|
primaryActionLabel: '补好并回复对方',
|
|
735
1167
|
allowRuleExtract: true,
|
|
736
|
-
basisHint:
|
|
1168
|
+
basisHint: '知识缺口拦截 · 未发现公开授权依据',
|
|
737
1169
|
replyContext,
|
|
738
1170
|
trace: {
|
|
739
|
-
intro: `${person.name}
|
|
740
|
-
steps
|
|
741
|
-
|
|
1171
|
+
intro: `${person.name} 的问题超出当前已提取能力文档覆盖范围。`,
|
|
1172
|
+
steps: [
|
|
1173
|
+
{ title: '在授权文档索引中检索', detail: '未找到满足置信度的授权片段。' },
|
|
1174
|
+
{ title: '拒绝幻觉编造并升级待补口径', detail: '保留人工补充口供,确认后可一键沉淀为长期规矩。' }
|
|
1175
|
+
],
|
|
1176
|
+
techNote: '知识边界守卫拦截:宁可标待补口径,绝不编造数字或越权读取未授权目录。'
|
|
742
1177
|
}
|
|
743
1178
|
};
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
1179
|
+
|
|
1180
|
+
const savedGapB = await enqueueApproval(gapItem);
|
|
1181
|
+
const politeHoldingReply = isPublicVisitor
|
|
1182
|
+
? `这个问题暂未收录在当前公开体验工作区的授权资料中(已记入待补口径 #${savedGapB.shortCode}),严禁编造答案。`
|
|
1183
|
+
: '这个问题暂未在我当前掌握的公开口径文档中明确定义,我已转请负责人核实补充,确认后会第一时间给你答复。';
|
|
1184
|
+
|
|
1185
|
+
if (isPublicVisitor) {
|
|
1186
|
+
await recordTurnDelivery({
|
|
1187
|
+
person,
|
|
1188
|
+
isPublicVisitor: true,
|
|
1189
|
+
visitorId,
|
|
1190
|
+
cleanQ,
|
|
1191
|
+
replyText: politeHoldingReply,
|
|
1192
|
+
capLabel: '待补口径边界',
|
|
1193
|
+
statusLabel: `待补口径 · #${savedGapB.shortCode}`,
|
|
1194
|
+
statusTone: 'warn',
|
|
1195
|
+
basisHint: gapItem.basisHint,
|
|
1196
|
+
channelId,
|
|
1197
|
+
trace: gapItem.trace,
|
|
1198
|
+
approvalId: savedGapB.id,
|
|
1199
|
+
shortCode: savedGapB.shortCode
|
|
1200
|
+
});
|
|
1201
|
+
}
|
|
1202
|
+
|
|
747
1203
|
return {
|
|
748
1204
|
action: 'knowledge_gap',
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
1205
|
+
isPublicLobby: Boolean(isPublicVisitor),
|
|
1206
|
+
approvalId: savedGapB.id,
|
|
1207
|
+
shortCode: savedGapB.shortCode,
|
|
1208
|
+
statusLabel: isPublicVisitor ? `待补口径 · #${savedGapB.shortCode}` : '待补口径已转服务台',
|
|
1209
|
+
reply: politeHoldingReply,
|
|
1210
|
+
...(isPublicVisitor ? { remainingTurns: remainingAfterTurn, visitorDailyLimit } : {})
|
|
752
1211
|
};
|
|
753
1212
|
}
|
|
754
1213
|
|
|
1214
|
+
emitProgress({
|
|
1215
|
+
phase: 'doc',
|
|
1216
|
+
title: '核对授权文档口径',
|
|
1217
|
+
detail: `${matches[0].projectName} · ${matches[0].snippet.title}`
|
|
1218
|
+
});
|
|
1219
|
+
|
|
1220
|
+
const top = matches[0];
|
|
1221
|
+
let generatedReply = `根据「${top.projectName} · ${top.snippet.title}」的只读口径说明:${top.snippet.summary}`;
|
|
1222
|
+
if (typeof llmResponder === 'function') {
|
|
1223
|
+
try {
|
|
1224
|
+
const custom = await llmResponder({ question: cleanQ, matches: matches.slice(0, 3), rules: s.rules, person });
|
|
1225
|
+
if (typeof custom === 'string' && custom.trim()) {
|
|
1226
|
+
generatedReply = redactSecrets(sanitizeSurfaceText(custom.trim()), secretVals);
|
|
1227
|
+
}
|
|
1228
|
+
} catch (err) {
|
|
1229
|
+
logger?.warn?.('[agent-twin] llmResponder fallback used:', err?.message);
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
if (person?.note) {
|
|
1234
|
+
generatedReply += `(注:按「${sanitizeSurfaceText(person.note)}」约定整理)`;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
755
1237
|
if (sandboxOnly) {
|
|
756
1238
|
return {
|
|
757
1239
|
action: 'replied',
|
|
758
|
-
statusLabel: '
|
|
759
|
-
reply:
|
|
760
|
-
basis:
|
|
1240
|
+
statusLabel: '沙箱试答(未写入外部记录)',
|
|
1241
|
+
reply: generatedReply,
|
|
1242
|
+
basis: `${top.projectName} · ${top.snippet.title}`
|
|
761
1243
|
};
|
|
762
1244
|
}
|
|
763
1245
|
|
|
764
1246
|
if (isOwnerSelfTest) {
|
|
765
1247
|
const selfRes = {
|
|
766
1248
|
question: cleanQ,
|
|
767
|
-
answer:
|
|
768
|
-
basis: `${
|
|
769
|
-
duration:
|
|
1249
|
+
answer: generatedReply,
|
|
1250
|
+
basis: `${top.projectName} · ${top.snippet.title}(仅本地自测)`,
|
|
1251
|
+
duration: '0.4 秒'
|
|
770
1252
|
};
|
|
771
1253
|
await store.updateState(st => { st.selfTestTurn = selfRes; });
|
|
772
|
-
return { action: 'replied', ...selfRes, reply:
|
|
1254
|
+
return { action: 'replied', ...selfRes, reply: generatedReply };
|
|
773
1255
|
}
|
|
774
1256
|
|
|
775
1257
|
const trace = {
|
|
776
|
-
intro: `收到 ${person.name} 通过「${channelSelectionLabel(channelId)}
|
|
777
|
-
steps
|
|
778
|
-
|
|
1258
|
+
intro: `收到 ${person.name} 通过「${channelSelectionLabel(channelId)}」的提问,已在独立沙箱内完成只读核对。`,
|
|
1259
|
+
steps: [
|
|
1260
|
+
{ title: '核对白名单与能力范围', detail: `仅检索 ${person.name} 已获授权的 ${matches.length} 处片段。` },
|
|
1261
|
+
{ title: '依据授权文档生成结论', detail: `引用「${top.projectName} · ${top.snippet.title}」,过滤私密字段。` }
|
|
1262
|
+
],
|
|
1263
|
+
techNote: '单人会话物理隔离存档,不含其他访客历史或敏感文件路径。'
|
|
779
1264
|
};
|
|
780
1265
|
|
|
781
|
-
const shouldAutoReply =
|
|
1266
|
+
const shouldAutoReply = isPublicVisitor
|
|
1267
|
+
? true
|
|
1268
|
+
: (s.dutyMode === 'live' && (person.mode === 'direct' || person.mode === 'summary'));
|
|
782
1269
|
if (shouldAutoReply) {
|
|
783
|
-
const
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
1270
|
+
const statusLabel = isPublicVisitor ? '只读口径解答完成' : '通道回执已送达';
|
|
1271
|
+
const turn = await recordTurnDelivery({
|
|
1272
|
+
person,
|
|
1273
|
+
isPublicVisitor,
|
|
1274
|
+
visitorId,
|
|
1275
|
+
cleanQ,
|
|
1276
|
+
replyText: generatedReply,
|
|
1277
|
+
capLabel: top.snippet.title,
|
|
1278
|
+
statusLabel,
|
|
790
1279
|
statusTone: 'ok',
|
|
791
|
-
basisHint:
|
|
1280
|
+
basisHint: `已核对「${top.snippet.title}」`,
|
|
1281
|
+
channelId,
|
|
792
1282
|
trace
|
|
793
|
-
};
|
|
794
|
-
await store.appendCallerTurn(person.id, turn, {
|
|
795
|
-
countTurn: true,
|
|
796
|
-
dshSessionId: aiRes.sessionId || callerThread?.dshSessionId || '',
|
|
797
|
-
lastPolicyDigest: policyDigest
|
|
798
|
-
});
|
|
799
|
-
await store.updateState(st => {
|
|
800
|
-
const target = st.people.find(p => p.id === person.id);
|
|
801
|
-
if (target) {
|
|
802
|
-
target.servedCount = (target.servedCount || 0) + 1;
|
|
803
|
-
target.lastActive = '刚刚';
|
|
804
|
-
}
|
|
805
|
-
st.deliveries.unshift({
|
|
806
|
-
id: `del-${randomUUID().slice(0, 8)}`,
|
|
807
|
-
personId: person.id,
|
|
808
|
-
time: nowTimeLabel(),
|
|
809
|
-
who: person.name,
|
|
810
|
-
whoNote: person.role,
|
|
811
|
-
channel: channelSelectionLabel(channelId),
|
|
812
|
-
summary: cleanReply.slice(0, 52),
|
|
813
|
-
statusLabel: '通道回执已送达',
|
|
814
|
-
statusTone: 'ok',
|
|
815
|
-
trace
|
|
816
|
-
});
|
|
817
|
-
st.deliveries = st.deliveries.slice(0, 40);
|
|
818
1283
|
});
|
|
819
|
-
return {
|
|
1284
|
+
return {
|
|
1285
|
+
action: 'replied',
|
|
1286
|
+
reply: generatedReply,
|
|
1287
|
+
statusLabel,
|
|
1288
|
+
turn,
|
|
1289
|
+
...(isPublicVisitor ? { remainingTurns: remainingAfterTurn, visitorDailyLimit } : {})
|
|
1290
|
+
};
|
|
820
1291
|
}
|
|
821
1292
|
|
|
822
1293
|
const draftApproval = {
|
|
@@ -827,259 +1298,224 @@ export function createTwinEngine({
|
|
|
827
1298
|
tone: 'amber',
|
|
828
1299
|
tag: '待过目草稿',
|
|
829
1300
|
headline: `${person.name} 提问:${cleanQ.slice(0, 26)}`,
|
|
830
|
-
gist:
|
|
1301
|
+
gist: generatedReply.slice(0, 56),
|
|
831
1302
|
question: cleanQ,
|
|
832
|
-
draft:
|
|
1303
|
+
draft: generatedReply,
|
|
833
1304
|
primaryActionLabel: '批准发出',
|
|
834
1305
|
allowRuleExtract: true,
|
|
835
|
-
basisHint:
|
|
1306
|
+
basisHint: `已核对「${top.snippet.title}」`,
|
|
836
1307
|
replyContext,
|
|
837
1308
|
trace
|
|
838
1309
|
};
|
|
839
|
-
await
|
|
840
|
-
st.approvals.unshift(draftApproval);
|
|
841
|
-
});
|
|
1310
|
+
const savedDraftB = await enqueueApproval(draftApproval);
|
|
842
1311
|
return {
|
|
843
1312
|
action: 'pending_approval',
|
|
844
|
-
approvalId:
|
|
1313
|
+
approvalId: savedDraftB.id,
|
|
1314
|
+
shortCode: savedDraftB.shortCode,
|
|
845
1315
|
statusLabel: '草稿待本人拍板',
|
|
846
|
-
draft:
|
|
1316
|
+
draft: generatedReply
|
|
847
1317
|
};
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
}
|
|
1318
|
+
} finally {
|
|
1319
|
+
activeTurns.delete(turnTrackId);
|
|
851
1320
|
}
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
852
1323
|
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
id: `app-red-${randomUUID().slice(0, 8)}`,
|
|
874
|
-
personId: person?.id || 'p-self',
|
|
875
|
-
personName: person?.name || '自测预演',
|
|
876
|
-
channel: `${channelSelectionLabel(channelId)} · ${nowTimeLabel()}`,
|
|
877
|
-
tone: 'danger',
|
|
878
|
-
tag: violation.label,
|
|
879
|
-
headline: `${person?.name || '访客'} 触发红线拦截:${violation.reason}`,
|
|
880
|
-
gist: '已自动挡回越权明细或变更操作,并准备好安全替代回复等你确认。',
|
|
881
|
-
question: cleanQ,
|
|
882
|
-
draft,
|
|
883
|
-
primaryActionLabel: '批准发出安全回复',
|
|
884
|
-
allowRuleExtract: false,
|
|
885
|
-
basisHint: `红线规则触发(${violation.reason})`,
|
|
886
|
-
replyContext,
|
|
887
|
-
trace: {
|
|
888
|
-
intro: `${person?.name || '访客'} 的消息触发了工作台安全红线。`,
|
|
889
|
-
steps: [
|
|
890
|
-
{ title: '边界校验命中红线', detail: violation.reason },
|
|
891
|
-
{ title: '生成脱敏替代答复', detail: '阻断原始字段或写操作,待本人审核发出。' }
|
|
892
|
-
],
|
|
893
|
-
techNote: '红线规则先于任何资料检索执行,零越权泄露风险。'
|
|
894
|
-
}
|
|
895
|
-
};
|
|
896
|
-
|
|
897
|
-
await store.updateState(st => {
|
|
898
|
-
st.approvals.unshift(appItem);
|
|
899
|
-
if (st.lifecycleStage === 'cold') st.scene = 'desk';
|
|
900
|
-
});
|
|
1324
|
+
/**
|
|
1325
|
+
* Core Scoped Twin Session runtime handler for external named callers and self-tests.
|
|
1326
|
+
*/
|
|
1327
|
+
async function handleInboundMessage({
|
|
1328
|
+
personId,
|
|
1329
|
+
question,
|
|
1330
|
+
channelId = 'ch-web',
|
|
1331
|
+
threadKey = '',
|
|
1332
|
+
isGroup = false,
|
|
1333
|
+
mentionsBot = true,
|
|
1334
|
+
isOwnerSelfTest = false,
|
|
1335
|
+
sandboxOnly = false,
|
|
1336
|
+
replyContext = {},
|
|
1337
|
+
forceDeterministic = false,
|
|
1338
|
+
onProgress = null
|
|
1339
|
+
}) {
|
|
1340
|
+
const s = store.getState();
|
|
1341
|
+
const secretVals = store.allSecretValues();
|
|
1342
|
+
const cleanQ = redactSecrets(sanitizeSurfaceText(String(question || '').trim()), secretVals);
|
|
1343
|
+
if (!cleanQ) throw new Error('提问内容不能为空');
|
|
901
1344
|
|
|
1345
|
+
if (!isOwnerSelfTest && s.dutyMode === 'off') {
|
|
902
1346
|
return {
|
|
903
|
-
action: '
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
draft,
|
|
907
|
-
safeFallbackReply: '这个问题涉及敏感边界或需本人拍板事项,已转呈负责人确认,稍后答复你。'
|
|
1347
|
+
action: 'paused',
|
|
1348
|
+
statusLabel: '工作分身当前暂停对外值班',
|
|
1349
|
+
reply: '工作分身当前已暂停对外值班,暂时停止自动回复。工具内不设任何收费门槛,重要事项请直接联系项目负责人。'
|
|
908
1350
|
};
|
|
909
1351
|
}
|
|
910
1352
|
|
|
911
|
-
const
|
|
912
|
-
const matches = [...externalKnowledge, ...localMatches].sort((a, b) => b.score - a.score);
|
|
913
|
-
const hasCustomKnowledge = matches.length > 0 && matches[0].score >= 2;
|
|
914
|
-
const isExplicitGapPrompt = /(未公开|内部底价|定价策略|内幕|保密|还没写)/.test(cleanQ);
|
|
1353
|
+
const person = personId ? s.people.find(p => p.id === personId) : null;
|
|
915
1354
|
|
|
916
|
-
if (!
|
|
917
|
-
|
|
918
|
-
|
|
1355
|
+
if (!isOwnerSelfTest && !sandboxOnly) {
|
|
1356
|
+
if (!person) {
|
|
1357
|
+
return { action: 'ignored', reason: 'unknown_caller' };
|
|
1358
|
+
}
|
|
1359
|
+
if (!person.enabled || isPersonExpired(person)) {
|
|
919
1360
|
return {
|
|
920
|
-
action: '
|
|
921
|
-
statusLabel: '
|
|
922
|
-
reply:
|
|
1361
|
+
action: 'paused',
|
|
1362
|
+
statusLabel: '当前访问权限已暂停',
|
|
1363
|
+
reply: `你好 ${person.name},当前的分身试用或服务权限已暂停。本工具不收取任何费用,如需恢复请直接联系负责人打开开关。`
|
|
923
1364
|
};
|
|
924
1365
|
}
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
1366
|
+
if (isGroup && !mentionsBot) {
|
|
1367
|
+
return { action: 'ignored', reason: 'group_not_mentioned' };
|
|
1368
|
+
}
|
|
1369
|
+
if (threadKey && isOwnerPresentRecently(channelId, threadKey)) {
|
|
1370
|
+
const yieldNotice = {
|
|
1371
|
+
id: `del-yield-${Date.now()}`,
|
|
1372
|
+
personId: person.id,
|
|
1373
|
+
time: nowTimeLabel(),
|
|
1374
|
+
who: person.name,
|
|
1375
|
+
whoNote: person.role,
|
|
1376
|
+
channel: channelSelectionLabel(channelId),
|
|
1377
|
+
summary: '检测到本人在原会话 15 分钟内发言,分身自动收声未抢答',
|
|
1378
|
+
statusLabel: '本人在场自动收声',
|
|
1379
|
+
statusTone: 'muted',
|
|
1380
|
+
trace: {
|
|
1381
|
+
intro: '15 分钟内检测到负责人在同一会话亲自回复过,触发本人在场收声保护。',
|
|
1382
|
+
steps: [{ title: '检测最近真人互动', detail: '保持现场沟通顺畅,不做重复插话。' }],
|
|
1383
|
+
techNote: '15 分钟计时窗口自动刷新。'
|
|
1384
|
+
}
|
|
932
1385
|
};
|
|
933
|
-
await store.updateState(st => {
|
|
934
|
-
|
|
1386
|
+
await store.updateState(st => {
|
|
1387
|
+
st.deliveries.unshift(yieldNotice);
|
|
1388
|
+
st.deliveries = st.deliveries.slice(0, 40);
|
|
1389
|
+
});
|
|
1390
|
+
return { action: 'yielded', statusLabel: '本人在场自动收声', delivery: yieldNotice };
|
|
935
1391
|
}
|
|
1392
|
+
}
|
|
936
1393
|
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
1394
|
+
const activeCaps = person ? person.caps : s.capabilities.filter(c => c.enabled).map(c => c.id);
|
|
1395
|
+
const scopeKey = person && !isOwnerSelfTest ? `caller-${person.id}` : 'selftest';
|
|
1396
|
+
|
|
1397
|
+
return executeCoreTurn({
|
|
1398
|
+
person,
|
|
1399
|
+
twinProfile: s.twinProfile,
|
|
1400
|
+
activeCaps,
|
|
1401
|
+
cleanQ,
|
|
1402
|
+
channelId,
|
|
1403
|
+
replyContext,
|
|
1404
|
+
isOwnerSelfTest,
|
|
1405
|
+
sandboxOnly,
|
|
1406
|
+
forceDeterministic,
|
|
1407
|
+
onProgress,
|
|
1408
|
+
threadAdapter: person
|
|
1409
|
+
? {
|
|
1410
|
+
readThread: () => store.readCallerThread(person.id),
|
|
1411
|
+
updateMeta: patch => store.updateCallerMeta(person.id, patch)
|
|
1412
|
+
}
|
|
1413
|
+
: null,
|
|
1414
|
+
materializeScopeKey: scopeKey,
|
|
1415
|
+
turnLockKey: scopeKey,
|
|
1416
|
+
reuseSandbox: false,
|
|
1417
|
+
isPublicVisitor: false,
|
|
1418
|
+
dailyLimit: (!isOwnerSelfTest && !sandboxOnly) ? (s.dailyTurnCap || DEFAULT_DAILY_TURN_CAP) : 0
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
961
1421
|
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
1422
|
+
/**
|
|
1423
|
+
* Fail-closed handler for anonymous visitors on the `/try` Public Scan-to-Try Lobby.
|
|
1424
|
+
*/
|
|
1425
|
+
async function handlePublicVisitorMessage({
|
|
1426
|
+
visitorId,
|
|
1427
|
+
question,
|
|
1428
|
+
forceDeterministic = false,
|
|
1429
|
+
onProgress = null
|
|
1430
|
+
}) {
|
|
1431
|
+
const s = store.getState();
|
|
1432
|
+
const secretVals = store.allSecretValues();
|
|
1433
|
+
const cleanQ = redactSecrets(sanitizeSurfaceText(String(question || '').trim()), secretVals);
|
|
1434
|
+
if (!cleanQ) throw new Error('提问内容不能为空');
|
|
1435
|
+
|
|
1436
|
+
if (s.dutyMode === 'off') {
|
|
966
1437
|
return {
|
|
967
|
-
action: '
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
reply: politeHoldingReply
|
|
1438
|
+
action: 'paused',
|
|
1439
|
+
statusLabel: '工作分身当前暂停对外值班',
|
|
1440
|
+
reply: '本分身当前处于暂停对外值班状态,欢迎随时按底部指引 30 秒在 DeepSeek Harness 自建自己的隔离 AI 分身。'
|
|
971
1441
|
};
|
|
972
1442
|
}
|
|
973
1443
|
|
|
974
|
-
const
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
}
|
|
982
|
-
} catch (err) {
|
|
983
|
-
logger?.warn?.('[agent-twin] llmResponder fallback used:', err?.message);
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
if (person?.note) {
|
|
988
|
-
generatedReply += `(注:按「${sanitizeSurfaceText(person.note)}」约定整理)`;
|
|
1444
|
+
const publicCfg = normalizePublicLobbyConfig(s.publicLobby);
|
|
1445
|
+
if (!publicCfg.enabled) {
|
|
1446
|
+
return {
|
|
1447
|
+
action: 'paused',
|
|
1448
|
+
statusLabel: '公开体验大厅未开启',
|
|
1449
|
+
reply: '工作区负责人尚未开启公开扫码体验大厅,请先在工作台「能力与通道 -> 公开扫码真体验大厅」开启并勾选允许对外展示的只读能力。'
|
|
1450
|
+
};
|
|
989
1451
|
}
|
|
990
1452
|
|
|
991
|
-
|
|
1453
|
+
// Fail-Closed: NEVER fall back to all enabled capabilities when capIds is empty
|
|
1454
|
+
const configuredCapSet = new Set(publicCfg.capIds || []);
|
|
1455
|
+
const allowedCaps = s.capabilities.filter(c => c.enabled && configuredCapSet.has(c.id));
|
|
1456
|
+
const allowedCapIds = allowedCaps.map(c => c.id);
|
|
1457
|
+
if (allowedCapIds.length === 0) {
|
|
992
1458
|
return {
|
|
993
|
-
action: '
|
|
994
|
-
statusLabel: '
|
|
995
|
-
reply:
|
|
996
|
-
basis: `${top.projectName} · ${top.snippet.title}`
|
|
1459
|
+
action: 'paused',
|
|
1460
|
+
statusLabel: '暂未勾选公开体验能力',
|
|
1461
|
+
reply: '为严守 Fail-Closed 安全默认,负责人尚未在工作台勾选任何可对外体验的只读能力项,当前拒绝访问任何工作区文件。'
|
|
997
1462
|
};
|
|
998
1463
|
}
|
|
999
1464
|
|
|
1000
|
-
if (
|
|
1001
|
-
const
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
basis: `${top.projectName} · ${top.snippet.title}(仅本地自测)`,
|
|
1005
|
-
duration: '0.4 秒'
|
|
1006
|
-
};
|
|
1007
|
-
await store.updateState(st => { st.selfTestTurn = selfRes; });
|
|
1008
|
-
return { action: 'replied', ...selfRes, reply: generatedReply };
|
|
1465
|
+
if (activePublicTurns >= MAX_CONCURRENT_PUBLIC_TURNS) {
|
|
1466
|
+
const busyErr = new Error('当前同时在线体验人数较多,请稍等 10 秒后再试。');
|
|
1467
|
+
busyErr.statusCode = 429;
|
|
1468
|
+
throw busyErr;
|
|
1009
1469
|
}
|
|
1010
1470
|
|
|
1011
|
-
const
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1471
|
+
const visitorDailyLimit = Math.max(1, publicCfg.visitorDailyLimit || DEFAULT_PUBLIC_VISITOR_DAILY_LIMIT);
|
|
1472
|
+
const globalDailyLimit = Math.max(1, publicCfg.globalDailyLimit || DEFAULT_PUBLIC_GLOBAL_DAILY_LIMIT);
|
|
1473
|
+
const allowedProjectNames = [...new Set(allowedCaps.map(c => c.projectName).filter(Boolean))].join('、') || '公开授权工作区';
|
|
1474
|
+
// Strict Persona Isolation: never inherit private workspace title/intro (e.g. unrelated client projects)
|
|
1475
|
+
const effectiveTwinProfile = {
|
|
1476
|
+
personaId: 'advisor',
|
|
1477
|
+
name: publicCfg.title || `${allowedProjectNames} · 公开体验分身`,
|
|
1478
|
+
intro: publicCfg.intro || `由本机 DeepSeek Harness 只读驱动,仅可查「${allowedProjectNames}」已勾选能力,私有项目物理隔离不可见。`
|
|
1479
|
+
};
|
|
1480
|
+
const shortTag = String(visitorId).slice(-4).toUpperCase();
|
|
1481
|
+
const virtualPerson = {
|
|
1482
|
+
id: `vis-${String(visitorId).slice(0, 12)}`,
|
|
1483
|
+
name: `扫码访客 #${shortTag}`,
|
|
1484
|
+
role: '公开扫码体验',
|
|
1485
|
+
channel: '公开扫码体验',
|
|
1486
|
+
mode: publicCfg.mode === 'direct' ? 'direct' : 'summary',
|
|
1487
|
+
caps: allowedCapIds,
|
|
1488
|
+
enabled: true,
|
|
1489
|
+
isPublicVisitor: true
|
|
1018
1490
|
};
|
|
1019
1491
|
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
time: nowTimeLabel(),
|
|
1044
|
-
who: person.name,
|
|
1045
|
-
whoNote: person.role,
|
|
1046
|
-
channel: channelSelectionLabel(channelId),
|
|
1047
|
-
summary: generatedReply.slice(0, 48),
|
|
1048
|
-
statusLabel: '通道回执已送达',
|
|
1049
|
-
statusTone: 'ok',
|
|
1050
|
-
trace
|
|
1051
|
-
});
|
|
1052
|
-
st.deliveries = st.deliveries.slice(0, 40);
|
|
1492
|
+
activePublicTurns += 1;
|
|
1493
|
+
try {
|
|
1494
|
+
return await executeCoreTurn({
|
|
1495
|
+
person: virtualPerson,
|
|
1496
|
+
twinProfile: effectiveTwinProfile,
|
|
1497
|
+
activeCaps: allowedCapIds,
|
|
1498
|
+
cleanQ,
|
|
1499
|
+
channelId: 'ch-public-try',
|
|
1500
|
+
isOwnerSelfTest: false,
|
|
1501
|
+
sandboxOnly: false,
|
|
1502
|
+
forceDeterministic,
|
|
1503
|
+
onProgress,
|
|
1504
|
+
threadAdapter: {
|
|
1505
|
+
readThread: () => store.readVisitorThread(visitorId),
|
|
1506
|
+
updateMeta: patch => store.updateVisitorMeta(visitorId, patch)
|
|
1507
|
+
},
|
|
1508
|
+
materializeScopeKey: 'public-lobby',
|
|
1509
|
+
turnLockKey: `visitor-${visitorId}`,
|
|
1510
|
+
reuseSandbox: true,
|
|
1511
|
+
isPublicVisitor: true,
|
|
1512
|
+
visitorId,
|
|
1513
|
+
dailyLimit: visitorDailyLimit,
|
|
1514
|
+
globalDailyLimit
|
|
1053
1515
|
});
|
|
1054
|
-
|
|
1516
|
+
} finally {
|
|
1517
|
+
activePublicTurns = Math.max(0, activePublicTurns - 1);
|
|
1055
1518
|
}
|
|
1056
|
-
|
|
1057
|
-
const draftApproval = {
|
|
1058
|
-
id: `app-${randomUUID().slice(0, 8)}`,
|
|
1059
|
-
personId: person.id,
|
|
1060
|
-
personName: person.name,
|
|
1061
|
-
channel: `${channelSelectionLabel(channelId)} · ${nowTimeLabel()}`,
|
|
1062
|
-
tone: 'amber',
|
|
1063
|
-
tag: '待过目草稿',
|
|
1064
|
-
headline: `${person.name} 提问:${cleanQ.slice(0, 26)}`,
|
|
1065
|
-
gist: generatedReply.slice(0, 56),
|
|
1066
|
-
question: cleanQ,
|
|
1067
|
-
draft: generatedReply,
|
|
1068
|
-
primaryActionLabel: '批准发出',
|
|
1069
|
-
allowRuleExtract: true,
|
|
1070
|
-
basisHint: `已核对「${top.snippet.title}」`,
|
|
1071
|
-
replyContext,
|
|
1072
|
-
trace
|
|
1073
|
-
};
|
|
1074
|
-
await store.updateState(st => {
|
|
1075
|
-
st.approvals.unshift(draftApproval);
|
|
1076
|
-
});
|
|
1077
|
-
return {
|
|
1078
|
-
action: 'pending_approval',
|
|
1079
|
-
approvalId: draftApproval.id,
|
|
1080
|
-
statusLabel: '草稿待本人拍板',
|
|
1081
|
-
draft: generatedReply
|
|
1082
|
-
};
|
|
1083
1519
|
}
|
|
1084
1520
|
|
|
1085
1521
|
async function approveDraft(approvalId, customDraft = null) {
|
|
@@ -1092,7 +1528,7 @@ export function createTwinEngine({
|
|
|
1092
1528
|
);
|
|
1093
1529
|
|
|
1094
1530
|
const person = s.people.find(p => p.id === item.personId);
|
|
1095
|
-
let dispatchStatus = '已调用通道接口发出';
|
|
1531
|
+
let dispatchStatus = item.isPublicLobby ? '公开体验已审阅存档' : '已调用通道接口发出';
|
|
1096
1532
|
if (person && typeof onDispatchOutbound === 'function') {
|
|
1097
1533
|
try {
|
|
1098
1534
|
const res = await onDispatchOutbound(person, finalReply, item.replyContext || {});
|
|
@@ -1102,18 +1538,22 @@ export function createTwinEngine({
|
|
|
1102
1538
|
}
|
|
1103
1539
|
}
|
|
1104
1540
|
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1541
|
+
const approvedTurn = {
|
|
1542
|
+
id: `th-ap-${randomUUID().slice(0, 8)}`,
|
|
1543
|
+
time: nowTimeLabel(),
|
|
1544
|
+
capLabel: item.tag,
|
|
1545
|
+
question: item.question,
|
|
1546
|
+
reply: finalReply,
|
|
1547
|
+
statusLabel: dispatchStatus,
|
|
1548
|
+
statusTone: 'ok',
|
|
1549
|
+
basisHint: item.basisHint || '经负责人审核发出',
|
|
1550
|
+
trace: item.trace
|
|
1551
|
+
};
|
|
1552
|
+
|
|
1553
|
+
if (item.isPublicLobby && item.visitorId) {
|
|
1554
|
+
await store.appendVisitorTurn(item.visitorId, approvedTurn, { countTurn: false });
|
|
1555
|
+
} else if (person) {
|
|
1556
|
+
await store.appendCallerTurn(person.id, approvedTurn, { countTurn: true });
|
|
1117
1557
|
}
|
|
1118
1558
|
|
|
1119
1559
|
await store.updateState(st => {
|
|
@@ -1194,6 +1634,79 @@ export function createTwinEngine({
|
|
|
1194
1634
|
return { ok: true, kind: 'self_test', message: '已完成一轮本地只读试答', result: res };
|
|
1195
1635
|
}
|
|
1196
1636
|
|
|
1637
|
+
/**
|
|
1638
|
+
* Execute an owner mobile/DM approval command parsed by `parseOwnerMobileCommand`.
|
|
1639
|
+
*/
|
|
1640
|
+
async function resolveRemoteOwnerCommand(cmd) {
|
|
1641
|
+
if (!cmd || !cmd.kind) return '未识别的管理指令,可发送「待办」查看待拍板项。';
|
|
1642
|
+
const approvals = store.getState().approvals || [];
|
|
1643
|
+
|
|
1644
|
+
if (cmd.kind === 'list') {
|
|
1645
|
+
if (approvals.length === 0) {
|
|
1646
|
+
return '当前没有待你拍板的草稿或红线拦截。';
|
|
1647
|
+
}
|
|
1648
|
+
const lines = approvals.slice(0, 5).map(a => {
|
|
1649
|
+
const code = a.shortCode || makeApprovalShortCode(a.id);
|
|
1650
|
+
return `• #${code}|${a.personName}(${a.tag}):${sanitizeSurfaceText(a.question || '').slice(0, 36)}`;
|
|
1651
|
+
});
|
|
1652
|
+
lines.push('', '回复「批准 #码」或「改 #码:你的最终回复」或「忽略 #码」即可远程拍板。');
|
|
1653
|
+
return lines.join('\n');
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
if (approvals.length === 0) {
|
|
1657
|
+
return '当前没有待处理的拍板项。';
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
const codeQuery = String(cmd.code || '').trim().toUpperCase();
|
|
1661
|
+
const target = codeQuery
|
|
1662
|
+
? approvals.find(a =>
|
|
1663
|
+
(a.shortCode || makeApprovalShortCode(a.id)).toUpperCase() === codeQuery ||
|
|
1664
|
+
String(a.id || '').toUpperCase() === codeQuery
|
|
1665
|
+
)
|
|
1666
|
+
: approvals[0];
|
|
1667
|
+
|
|
1668
|
+
if (!target) {
|
|
1669
|
+
return `未找到编号 #${codeQuery} 对应的待拍板项,请发送「待办」查看当前有效编号。`;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
const resolvedCode = target.shortCode || makeApprovalShortCode(target.id);
|
|
1673
|
+
if (cmd.kind === 'dismiss') {
|
|
1674
|
+
await store.updateState(st => {
|
|
1675
|
+
st.approvals = st.approvals.filter(a => a.id !== target.id);
|
|
1676
|
+
}, { saveUndo: 'desk' });
|
|
1677
|
+
return `已忽略来自 ${target.personName} 的待办(#${resolvedCode}),不会对外发送。`;
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
if (cmd.kind === 'approve') {
|
|
1681
|
+
const res = await approveDraft(target.id, null);
|
|
1682
|
+
return `已拍板发出给 ${target.personName}(#${resolvedCode} · ${res.statusLabel})。`;
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
if (cmd.kind === 'edit') {
|
|
1686
|
+
const res = await approveDraft(target.id, cmd.text);
|
|
1687
|
+
return `已按你的修改发出给 ${target.personName}(#${resolvedCode} · ${res.statusLabel})。`;
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
if (cmd.kind === 'rule') {
|
|
1691
|
+
const cleanRule = sanitizeSurfaceText(String(cmd.text || '').trim());
|
|
1692
|
+
const res = await approveDraft(target.id, cleanRule);
|
|
1693
|
+
if (cleanRule) {
|
|
1694
|
+
await store.updateState(st => {
|
|
1695
|
+
if (!st.rules.some(r => r.text === cleanRule)) {
|
|
1696
|
+
st.rules.push({
|
|
1697
|
+
id: `r-mob-${randomUUID().slice(0, 6)}`,
|
|
1698
|
+
kind: '规矩',
|
|
1699
|
+
text: cleanRule
|
|
1700
|
+
});
|
|
1701
|
+
}
|
|
1702
|
+
}, { saveUndo: 'desk' });
|
|
1703
|
+
}
|
|
1704
|
+
return `已回复 ${target.personName} 并记为长期规矩(#${resolvedCode} · ${res.statusLabel})。`;
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
return '未识别的操作指令。';
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1197
1710
|
return {
|
|
1198
1711
|
startExtraction,
|
|
1199
1712
|
fastForwardExtraction,
|
|
@@ -1201,8 +1714,11 @@ export function createTwinEngine({
|
|
|
1201
1714
|
recheckProjectDelta,
|
|
1202
1715
|
confirmDeltaUpgrade,
|
|
1203
1716
|
handleInboundMessage,
|
|
1717
|
+
handlePublicVisitorMessage,
|
|
1204
1718
|
approveDraft,
|
|
1205
1719
|
executeOwnerCommand,
|
|
1720
|
+
resolveRemoteOwnerCommand,
|
|
1721
|
+
getActiveTurns: () => [...activeTurns.values()],
|
|
1206
1722
|
recordOwnerPresence,
|
|
1207
1723
|
isOwnerPresentRecently,
|
|
1208
1724
|
dispose,
|