@wangnanshan/dsh-nanshan-session-manager 1.4.2 → 1.5.1
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/client.js +8 -0
- package/index.js +129 -0
- package/package.json +1 -1
package/client.js
CHANGED
|
@@ -229,6 +229,7 @@ window.__ModuleLoader__.load({
|
|
|
229
229
|
'rule.title': '规则列表',
|
|
230
230
|
'rule.titleMeta': '共 {total} 条 · {enabled} 启用',
|
|
231
231
|
'rule.new': '新建规则',
|
|
232
|
+
'rule.notice': '规则启用后会作为一条消息写入对话;停用不会撤回已写入的内容。只有之后新开启、且从未注入过该规则的对话,才不会受它影响。',
|
|
232
233
|
'rule.empty': '还没有任何规则',
|
|
233
234
|
'rule.emptyBody': '规则可以在全局、工作区、单个会话范围内约束 AI 助手的行为。',
|
|
234
235
|
'rule.emptyCta': '创建第一条规则',
|
|
@@ -449,6 +450,7 @@ window.__ModuleLoader__.load({
|
|
|
449
450
|
'rule.title': 'Rules',
|
|
450
451
|
'rule.titleMeta': '{total} rules · {enabled} active',
|
|
451
452
|
'rule.new': 'New Rule',
|
|
453
|
+
'rule.notice': 'Enabled rules are written into the conversation as a message and are not retracted when disabled. Only conversations opened afterwards that never received the rule will be unaffected.',
|
|
452
454
|
'rule.empty': 'No rules yet',
|
|
453
455
|
'rule.emptyBody': 'Rules constrain the AI assistant behaviour at global, workspace or session scope.',
|
|
454
456
|
'rule.emptyCta': 'Create your first rule',
|
|
@@ -777,6 +779,8 @@ window.__ModuleLoader__.load({
|
|
|
777
779
|
.nsm__rules-filter-btn { font-family: var(--nsm-font); font-size: 10.5px; font-weight: 500; padding: 4px 10px; border: none; border-radius: var(--nsm-radius-sm); background: transparent; color: var(--nsm-secondary); cursor: pointer; transition: background-color .12s ease, color .12s ease; }
|
|
778
780
|
.nsm__rules-filter-btn:hover { color: var(--nsm-text); }
|
|
779
781
|
.nsm__rules-filter-btn.is-active { background: var(--nsm-accent-soft); color: var(--nsm-accent); }
|
|
782
|
+
.nsm__rules-notice { padding: 10px 14px; border: 1px solid var(--nsm-border); border-left: 3px solid var(--nsm-accent); border-radius: 8px; background: var(--nsm-bg); }
|
|
783
|
+
.nsm__rules-notice p { margin: 0; font-size: 11.5px; line-height: 1.6; color: var(--nsm-secondary); }
|
|
780
784
|
.nsm__rules-list { display: flex; flex-direction: column; gap: 10px; }
|
|
781
785
|
|
|
782
786
|
/* ── v1.4 rule card ── */
|
|
@@ -2714,6 +2718,10 @@ window.__ModuleLoader__.load({
|
|
|
2714
2718
|
}, h(Glyph, { name: 'plus', size: 12 }), t('rule.new')),
|
|
2715
2719
|
));
|
|
2716
2720
|
|
|
2721
|
+
// ── Notice ──
|
|
2722
|
+
pane.push(h('div', { key: 'rule-notice', className: 'nsm__rules-notice' },
|
|
2723
|
+
h('p', null, t('rule.notice'))));
|
|
2724
|
+
|
|
2717
2725
|
// ── Rules list ──
|
|
2718
2726
|
if (rulesStatus === 'loading') {
|
|
2719
2727
|
pane.push(h('div', { key: 'rule-loading', className: 'nsm__loading' },
|
package/index.js
CHANGED
|
@@ -1491,6 +1491,135 @@ export function apply(ctx) {
|
|
|
1491
1491
|
() => ctx.webServer.register({ kind: 'prefix', path: API, handler }),
|
|
1492
1492
|
'nanshan-session-manager: session management api',
|
|
1493
1493
|
)
|
|
1494
|
+
|
|
1495
|
+
// Inject enabled rules into every agent step as durable instruction context.
|
|
1496
|
+
ctx.on('agent/pre-step', async (payload, next) => {
|
|
1497
|
+
const decision = await next()
|
|
1498
|
+
if (!decision || decision.kind === 'reject') return decision
|
|
1499
|
+
try {
|
|
1500
|
+
const agent = payload.agent
|
|
1501
|
+
const session = agent && agent.session
|
|
1502
|
+
const sessionId = session && session.id
|
|
1503
|
+
if (!sessionId) return decision
|
|
1504
|
+
|
|
1505
|
+
const rules = await loadRules()
|
|
1506
|
+
const enabled = Object.values(rules).filter((rule) => rule.enabled)
|
|
1507
|
+
if (enabled.length === 0) return decision
|
|
1508
|
+
|
|
1509
|
+
const cwd = await resolveAgentCwd(service, agent, payload.signal)
|
|
1510
|
+
const workspaceId = resolveWorkspaceId(service, cwd)
|
|
1511
|
+
const matched = enabled.filter((rule) => ruleMatches(rule, { sessionId, cwd, workspaceId }))
|
|
1512
|
+
if (matched.length === 0) return decision
|
|
1513
|
+
|
|
1514
|
+
const digest = ruleContextDigest(matched)
|
|
1515
|
+
if (sessionHasRuleContext(session, digest)) return decision
|
|
1516
|
+
|
|
1517
|
+
const batch = Array.isArray(decision.messages) ? decision.messages : []
|
|
1518
|
+
if (batch.some((message) => message && message.source
|
|
1519
|
+
&& message.source.kind === RULE_SOURCE_KIND
|
|
1520
|
+
&& message.source.digest === digest)) return decision
|
|
1521
|
+
|
|
1522
|
+
// An empty first entry owns a no-step turn; keep context pending for the next real prompt.
|
|
1523
|
+
if (payload.step === 1 && batch.length === 0) return decision
|
|
1524
|
+
|
|
1525
|
+
const message = {
|
|
1526
|
+
id: randomUUID(),
|
|
1527
|
+
role: 'user',
|
|
1528
|
+
content: [{ type: 'text', text: renderRuleContext(matched) }],
|
|
1529
|
+
source: { kind: RULE_SOURCE_KIND, digest },
|
|
1530
|
+
}
|
|
1531
|
+
return { ...decision, messages: [...batch, message] }
|
|
1532
|
+
} catch (error) {
|
|
1533
|
+
ctx.logger?.warn?.('nanshan-session-manager: rule injection failed: %o', error)
|
|
1534
|
+
return decision
|
|
1535
|
+
}
|
|
1536
|
+
})
|
|
1537
|
+
}
|
|
1538
|
+
|
|
1539
|
+
const RULE_SOURCE_KIND = 'nanshan-rule-context'
|
|
1540
|
+
|
|
1541
|
+
async function resolveAgentCwd(service, agent, signal) {
|
|
1542
|
+
try {
|
|
1543
|
+
const workingDirectory = service('workingDirectory')
|
|
1544
|
+
if (workingDirectory && typeof workingDirectory.ensure === 'function') {
|
|
1545
|
+
const cwd = await workingDirectory.ensure(agent, signal)
|
|
1546
|
+
if (typeof cwd === 'string' && cwd !== '') return cwd
|
|
1547
|
+
}
|
|
1548
|
+
} catch {
|
|
1549
|
+
// Fall through to the session header when the provider is unavailable.
|
|
1550
|
+
}
|
|
1551
|
+
const header = agent && agent.session && agent.session.header
|
|
1552
|
+
const fallback = header && (header.cwd || header.directory)
|
|
1553
|
+
return typeof fallback === 'string' && fallback !== '' ? fallback : null
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
function resolveWorkspaceId(service, cwd) {
|
|
1557
|
+
if (!cwd) return null
|
|
1558
|
+
try {
|
|
1559
|
+
const registry = service('workspaceRegistry')
|
|
1560
|
+
if (!registry || typeof registry.list !== 'function') return null
|
|
1561
|
+
const list = registry.list()
|
|
1562
|
+
let best = null
|
|
1563
|
+
for (const workspace of Array.isArray(list) ? list : []) {
|
|
1564
|
+
const path = workspace && (workspace.path || workspace.directory)
|
|
1565
|
+
if (typeof path !== 'string' || path === '') continue
|
|
1566
|
+
if (cwd === path || cwd.startsWith(path.endsWith('/') ? path : `${path}/`)) {
|
|
1567
|
+
if (!best || path.length > best.path.length) best = { id: workspace.id, path }
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
return best ? best.id : null
|
|
1571
|
+
} catch {
|
|
1572
|
+
return null
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
function ruleMatches(rule, { sessionId, cwd, workspaceId }) {
|
|
1577
|
+
const targets = Array.isArray(rule.targets) ? rule.targets : []
|
|
1578
|
+
if (rule.scope === 'global') return true
|
|
1579
|
+
if (rule.scope === 'session') return targets.includes(sessionId)
|
|
1580
|
+
if (rule.scope === 'workspace') {
|
|
1581
|
+
if (workspaceId && targets.includes(workspaceId)) return true
|
|
1582
|
+
if (cwd) {
|
|
1583
|
+
return targets.some((target) => {
|
|
1584
|
+
if (typeof target !== 'string' || target === '') return false
|
|
1585
|
+
return cwd === target || cwd.startsWith(target.endsWith('/') ? target : `${target}/`)
|
|
1586
|
+
})
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
return false
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
function ruleContextDigest(rules) {
|
|
1593
|
+
const payload = rules
|
|
1594
|
+
.map((rule) => `${rule.id}\u0000${rule.updatedAt}\u0000${rule.title}\u0000${rule.content}`)
|
|
1595
|
+
.join('\u0001')
|
|
1596
|
+
return createHash('sha256').update(payload).digest('hex').slice(0, 16)
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
function renderRuleContext(rules) {
|
|
1600
|
+
const body = rules.map((rule) => `## ${rule.title}\n\n${rule.content}`).join('\n\n')
|
|
1601
|
+
return [
|
|
1602
|
+
'<nanshan-rules>',
|
|
1603
|
+
'以下规则由用户在【南山】规则管理中启用,请在本会话的所有回复中遵守。',
|
|
1604
|
+
'若此前出现过 <nanshan-rules> 块,以本条为最新版本。',
|
|
1605
|
+
'',
|
|
1606
|
+
body,
|
|
1607
|
+
'</nanshan-rules>',
|
|
1608
|
+
].join('\n')
|
|
1609
|
+
}
|
|
1610
|
+
|
|
1611
|
+
function sessionHasRuleContext(session, digest) {
|
|
1612
|
+
if (!session) return false
|
|
1613
|
+
const surface = session.surface
|
|
1614
|
+
const nodes = surface && surface.nodes
|
|
1615
|
+
if (!nodes || typeof session.eventAt !== 'function') return false
|
|
1616
|
+
const seqs = Array.from(nodes)
|
|
1617
|
+
for (let index = seqs.length - 1; index >= 0; index -= 1) {
|
|
1618
|
+
const event = session.eventAt(seqs[index])
|
|
1619
|
+
const source = event && event.type === 'user/message' && event.data && event.data.source
|
|
1620
|
+
if (source && source.kind === RULE_SOURCE_KIND && source.digest === digest) return true
|
|
1621
|
+
}
|
|
1622
|
+
return false
|
|
1494
1623
|
}
|
|
1495
1624
|
|
|
1496
1625
|
function httpError(status, message) {
|