@dsh-ray/assistant-delivery 0.3.2 → 0.3.4
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 +5 -4
- package/lib/agent-runtime.d.ts +11 -3
- package/lib/agent-runtime.d.ts.map +1 -1
- package/lib/agent-runtime.js +273 -36
- package/lib/agent-runtime.js.map +1 -1
- package/lib/command-catalog.d.ts +42 -0
- package/lib/command-catalog.d.ts.map +1 -0
- package/lib/command-catalog.js +248 -0
- package/lib/command-catalog.js.map +1 -0
- package/lib/config-control.d.ts +158 -0
- package/lib/config-control.d.ts.map +1 -0
- package/lib/config-control.js +1406 -0
- package/lib/config-control.js.map +1 -0
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -1
- package/lib/index.js.map +1 -1
- package/lib/service.d.ts +10 -1
- package/lib/service.d.ts.map +1 -1
- package/lib/service.js +481 -3
- package/lib/service.js.map +1 -1
- package/lib/session-commands.d.ts +12 -0
- package/lib/session-commands.d.ts.map +1 -1
- package/lib/session-commands.js +57 -0
- package/lib/session-commands.js.map +1 -1
- package/lib/sqlite.d.ts.map +1 -1
- package/lib/sqlite.js +1 -0
- package/lib/sqlite.js.map +1 -1
- package/lib/store.d.ts +17 -1
- package/lib/store.d.ts.map +1 -1
- package/lib/store.js +333 -4
- package/lib/store.js.map +1 -1
- package/lib/types.d.ts +94 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +10 -10
package/lib/service.js
CHANGED
|
@@ -22,6 +22,7 @@ import { ASSISTANT_GROWTH_CONTRACT_VERSION, defaultRayPluginStatePath, growthObj
|
|
|
22
22
|
import { DeliveryAdapterRegistry, DeliveryCoordinator, InboundCoordinator, } from './coordinator.js';
|
|
23
23
|
import { DeliveryStore, DeliveryStoreError, } from './store.js';
|
|
24
24
|
import { DshDeliveryRuntime, redactOwnerLearningReply } from './agent-runtime.js';
|
|
25
|
+
import { listLarkSlashCommandSpecs } from './command-catalog.js';
|
|
25
26
|
import { parseOwnerVetoArgs } from './session-commands.js';
|
|
26
27
|
import { RayWorkerTicketRuntime } from './ray-worker-runtime.js';
|
|
27
28
|
import { DeliverySessionLeases, SessionLeaseUnavailable } from './session-lease-runtime.js';
|
|
@@ -41,6 +42,7 @@ import { collectDeliveryVersionReport, } from './version-query.js';
|
|
|
41
42
|
import { deliveryT1PreferenceKeys, deliveryT1PreferenceValues } from './learning-command.js';
|
|
42
43
|
import { assertReplyReplayAllowed, blockAgentRepliesForReplay as createReplyReplayBlock, } from './reply-replay.js';
|
|
43
44
|
import { feedbackSignalInput, naturalOwnerReplyWithoutQuotes, parseFeedbackCommand, parseNaturalObjectiveFeedback, parseNaturalOwnerReply, objectiveStateMessage, classifyNaturalPreferenceDirective, observedResponseLanguage, } from './feedback-command.js';
|
|
45
|
+
import { applyNonPreferenceConfigMutation, applyOwnerConsoleConversationMutation, buildConfigPickerSnapshot, OWNER_CONSOLE_PROCESS_LOGS_KEY, parseT1ConversationSelections, readOwnerConsoleShowProcessLogs, redactConfigSecret, T1_TEXT_KEY_ALIASES, validateOwnerConsoleConversationMutation, } from './config-control.js';
|
|
44
46
|
import { DELIVERY_PREFERENCE_PROJECTION_PROTOCOL, TRUSTED_DELIVERY_PRESENTATION_PRODUCER_PROTOCOL, } from './types.js';
|
|
45
47
|
const trustedDeliveryPreferenceProducers = new WeakSet();
|
|
46
48
|
const deliveryPreferenceProducerProbe = Symbol('assistant-delivery.preference-producer-probe');
|
|
@@ -349,6 +351,7 @@ const configSchema = Schema.object({
|
|
|
349
351
|
agentGoalContinuationTimeoutMs: Schema.number().step(1).min(0).max(300_000).default(0),
|
|
350
352
|
modelPickerTtlMs: Schema.number().step(1).min(60_000).max(86_400_000).default(900_000),
|
|
351
353
|
permissionPickerTtlMs: Schema.number().step(1).min(60_000).max(86_400_000).default(900_000),
|
|
354
|
+
configPickerTtlMs: Schema.number().step(1).min(60_000).max(86_400_000).default(900_000),
|
|
352
355
|
toolApprovalTtlMs: Schema.number().step(1).min(1_000).max(300_000).default(300_000),
|
|
353
356
|
toolApprovalPreparationTimeoutMs: Schema.number().step(1).min(100).max(30_000).default(10_000),
|
|
354
357
|
attentionAttachOverCap: Schema.boolean().default(true),
|
|
@@ -1257,6 +1260,9 @@ export class AssistantDeliveryService extends Service {
|
|
|
1257
1260
|
recordInterruptedTurnSummary: (inboxId, failureCode, summary) => this.deliveryStore.recordInterruptedTurnSummary({ inboxId, failureCode, summary }),
|
|
1258
1261
|
modelPickerTtlMs: config.modelPickerTtlMs,
|
|
1259
1262
|
permissionPickerTtlMs: config.permissionPickerTtlMs,
|
|
1263
|
+
configPickerTtlMs: config.configPickerTtlMs,
|
|
1264
|
+
buildConfigPicker: (binding, envelope, activeCategory) => this.buildConfigPickerForCommand(binding, envelope, activeCategory),
|
|
1265
|
+
applyConversationPreferenceMutation: (binding, envelope, pairs) => this.applyConversationPreferenceMutation(binding, envelope, pairs),
|
|
1260
1266
|
getModelSelection: conversation => this.deliveryStore.getModelSelection(conversation),
|
|
1261
1267
|
clearStaleModelReasoningEffort: (conversation, expected) => this.deliveryStore.clearStaleModelReasoningEffort({ conversation, expected }),
|
|
1262
1268
|
imageMaterializer,
|
|
@@ -1265,7 +1271,12 @@ export class AssistantDeliveryService extends Service {
|
|
|
1265
1271
|
isOwnerFeedbackController: (binding, envelope) => this.isOwnerFeedbackController(binding, envelope),
|
|
1266
1272
|
authorizeOwnerPreferenceFeedback: (binding, envelope, selections) => this.authorizeOwnerPreferenceFeedback(binding, envelope, selections),
|
|
1267
1273
|
dispatchPreferenceFeedback: events => this.dispatchPreferenceFeedback(events),
|
|
1268
|
-
replyCompletedPreferenceTurn: (agent, binding, envelope, reply) => this.replyCompletedPreferenceTurn(agent, binding, envelope,
|
|
1274
|
+
replyCompletedPreferenceTurn: (agent, binding, envelope, reply) => this.replyCompletedPreferenceTurn(agent, binding, envelope, {
|
|
1275
|
+
text: reply.text,
|
|
1276
|
+
...(reply.format === undefined ? {} : { format: reply.format === 'config-picker' ? 'plain' : reply.format }),
|
|
1277
|
+
...(reply.modelPicker === undefined ? {} : { modelPicker: reply.modelPicker }),
|
|
1278
|
+
...(reply.permissionPicker === undefined ? {} : { permissionPicker: reply.permissionPicker }),
|
|
1279
|
+
}),
|
|
1269
1280
|
dispatchObjectiveFeedback: (binding, envelope, objectiveStatus) => this.dispatchObjectiveFeedback(binding, envelope, objectiveStatus),
|
|
1270
1281
|
dispatchNaturalObjectiveFeedback: (binding, envelope) => this.dispatchNaturalObjectiveFeedback(binding, envelope),
|
|
1271
1282
|
dispatchWorkflowCommand: (binding, envelope, command) => this.dispatchWorkflowCommand(binding, envelope, command),
|
|
@@ -1300,6 +1311,7 @@ export class AssistantDeliveryService extends Service {
|
|
|
1300
1311
|
text: reply.text,
|
|
1301
1312
|
replyToEventId: eventId,
|
|
1302
1313
|
...(reply.format === undefined ? {} : { format: reply.format }),
|
|
1314
|
+
...(reply.configPicker === undefined ? {} : { configPicker: reply.configPicker }),
|
|
1303
1315
|
...(reply.modelPicker === undefined ? {} : { modelPicker: reply.modelPicker }),
|
|
1304
1316
|
...(reply.permissionPicker === undefined ? {} : { permissionPicker: reply.permissionPicker }),
|
|
1305
1317
|
});
|
|
@@ -1768,6 +1780,456 @@ export class AssistantDeliveryService extends Service {
|
|
|
1768
1780
|
const picker = record.intent.modelPicker;
|
|
1769
1781
|
return picker === undefined || Date.now() >= picker.expiresAt ? undefined : picker;
|
|
1770
1782
|
}
|
|
1783
|
+
configOperationId(conversation, eventId) {
|
|
1784
|
+
return `cfg:${createHash('sha256').update(JSON.stringify([conversation, eventId])).digest('hex').slice(0, 24)}`;
|
|
1785
|
+
}
|
|
1786
|
+
async readExplainReceiptForCallback(binding, operationId) {
|
|
1787
|
+
const sink = this.preferenceFeedbackSink;
|
|
1788
|
+
if (sink === undefined
|
|
1789
|
+
|| sink.registration.generation !== this.preferenceProducerGeneration
|
|
1790
|
+
|| !registrationOwnedByPreference(sink.registration)
|
|
1791
|
+
|| typeof sink.registration.control !== 'function') {
|
|
1792
|
+
return undefined;
|
|
1793
|
+
}
|
|
1794
|
+
const principalLineage = this.ownerLineageForBinding(binding);
|
|
1795
|
+
const admissionCursor = this.deliveryStore.latestAdmissionCursor();
|
|
1796
|
+
if (principalLineage === undefined || admissionCursor === undefined)
|
|
1797
|
+
return undefined;
|
|
1798
|
+
const request = Object.freeze({
|
|
1799
|
+
scope: Object.freeze({ workspace: binding.workspace, preset: binding.agentPreset }),
|
|
1800
|
+
principalId: externalPrincipalId(binding.principal),
|
|
1801
|
+
principalLineage,
|
|
1802
|
+
admissionCursor,
|
|
1803
|
+
action: 'explain',
|
|
1804
|
+
occurredAt: Date.now(),
|
|
1805
|
+
idempotencyKey: `config-explain:${createHash('sha256').update(JSON.stringify([
|
|
1806
|
+
binding.id, binding.version, operationId, admissionCursor.epoch, admissionCursor.sequence,
|
|
1807
|
+
])).digest('hex')}`,
|
|
1808
|
+
});
|
|
1809
|
+
try {
|
|
1810
|
+
await this.drainPreferenceProjections();
|
|
1811
|
+
if (this.deliveryStore.hasBlockingPreferenceProjectionBefore(request))
|
|
1812
|
+
return undefined;
|
|
1813
|
+
const receipt = await sink.registration.control(request);
|
|
1814
|
+
validateLearningControlReceipt(receipt, request);
|
|
1815
|
+
return receipt;
|
|
1816
|
+
}
|
|
1817
|
+
catch {
|
|
1818
|
+
return undefined;
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
async buildConfigPickerSnapshotForBinding(input) {
|
|
1822
|
+
const ctxAny = this.context;
|
|
1823
|
+
const principalLineage = this.ownerLineageForBinding(input.binding);
|
|
1824
|
+
const settingsSvc = ctxAny.get?.('settings');
|
|
1825
|
+
const credentialsSvc = ctxAny.get?.('credentials');
|
|
1826
|
+
const defaultModelSvc = ctxAny.get?.('agentDefaultModel');
|
|
1827
|
+
const llmSvc = ctxAny.get?.('llm');
|
|
1828
|
+
const prefSvc = ctxAny.get?.('assistantPreferenceLearning');
|
|
1829
|
+
return await buildConfigPickerSnapshot({
|
|
1830
|
+
operationId: input.operationId,
|
|
1831
|
+
issuedAt: input.issuedAt,
|
|
1832
|
+
expiresAt: input.expiresAt,
|
|
1833
|
+
bindingVersion: input.binding.version,
|
|
1834
|
+
sessionId: input.binding.sessionId,
|
|
1835
|
+
conversationKind: input.binding.conversation.kind,
|
|
1836
|
+
activeCategory: input.activeCategory,
|
|
1837
|
+
...(input.activeProviderId === undefined ? {} : { activeProviderId: input.activeProviderId }),
|
|
1838
|
+
workspace: input.binding.workspace,
|
|
1839
|
+
preset: input.binding.agentPreset,
|
|
1840
|
+
principalId: externalPrincipalId(input.binding.principal),
|
|
1841
|
+
...(principalLineage === undefined ? {} : { principalLineage }),
|
|
1842
|
+
...(settingsSvc === undefined ? {} : { settings: settingsSvc }),
|
|
1843
|
+
...(credentialsSvc === undefined ? {} : { credentials: credentialsSvc }),
|
|
1844
|
+
...(defaultModelSvc === undefined ? {} : { agentDefaultModel: defaultModelSvc }),
|
|
1845
|
+
...(llmSvc === undefined ? {} : { llm: llmSvc }),
|
|
1846
|
+
...(prefSvc === undefined ? {} : { preferenceLearning: prefSvc }),
|
|
1847
|
+
...(input.learningExplainReceipt === undefined ? {} : { learningExplainReceipt: input.learningExplainReceipt }),
|
|
1848
|
+
...(input.categoryNotices === undefined ? {} : { categoryNotices: input.categoryNotices }),
|
|
1849
|
+
});
|
|
1850
|
+
}
|
|
1851
|
+
async buildConfigPickerForCommand(binding, envelope, activeCategory) {
|
|
1852
|
+
const issuedAt = Date.now();
|
|
1853
|
+
const explainResult = await this.dispatchLearningCommand(binding, envelope, 'explain');
|
|
1854
|
+
const learningExplainReceipt = typeof explainResult === 'object' ? explainResult : undefined;
|
|
1855
|
+
return await this.buildConfigPickerSnapshotForBinding({
|
|
1856
|
+
binding,
|
|
1857
|
+
operationId: this.configOperationId(binding.conversation, envelope.eventId),
|
|
1858
|
+
issuedAt,
|
|
1859
|
+
expiresAt: issuedAt + this.config.configPickerTtlMs,
|
|
1860
|
+
activeCategory,
|
|
1861
|
+
...(learningExplainReceipt === undefined ? {} : { learningExplainReceipt }),
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
async applyConversationPreferenceMutation(binding, envelope, pairs) {
|
|
1865
|
+
const parsed = parseT1ConversationSelections(pairs);
|
|
1866
|
+
if (!parsed.ok) {
|
|
1867
|
+
return { ok: false, message: parsed.message };
|
|
1868
|
+
}
|
|
1869
|
+
const selections = parsed.selections.map(sel => ({
|
|
1870
|
+
preferenceKey: sel.preferenceKey,
|
|
1871
|
+
candidateValue: sel.candidateValue,
|
|
1872
|
+
interpretationTrust: 'explicit-selection',
|
|
1873
|
+
}));
|
|
1874
|
+
const attestation = this.authorizeOwnerPreferenceFeedback(binding, envelope, selections);
|
|
1875
|
+
if (attestation === undefined) {
|
|
1876
|
+
return { ok: false, message: '偏好修改授权未通过(owner 或策略限制)' };
|
|
1877
|
+
}
|
|
1878
|
+
const events = selections.map(selection => feedbackSignalInput(binding, envelope, selection, attestation));
|
|
1879
|
+
const dispatchResult = await this.dispatchPreferenceFeedback(Object.freeze(events));
|
|
1880
|
+
if (dispatchResult === 'unavailable') {
|
|
1881
|
+
return { ok: false, message: '偏好学习服务(preference-learning)尚未挂载,本次偏好未写入' };
|
|
1882
|
+
}
|
|
1883
|
+
if (dispatchResult === 'unknown') {
|
|
1884
|
+
return { ok: false, message: '偏好写入状态未确认,请稍后重试' };
|
|
1885
|
+
}
|
|
1886
|
+
const explainResult = await this.dispatchLearningCommand(binding, envelope, 'explain');
|
|
1887
|
+
if (typeof explainResult === 'object') {
|
|
1888
|
+
if (explainResult.outcome === 'stale') {
|
|
1889
|
+
return {
|
|
1890
|
+
ok: false,
|
|
1891
|
+
explainReceipt: explainResult,
|
|
1892
|
+
message: '检测到更新的偏好控制操作,本次偏好状态未覆盖',
|
|
1893
|
+
};
|
|
1894
|
+
}
|
|
1895
|
+
if (!explainResult.state.administrativelyEnabled) {
|
|
1896
|
+
return {
|
|
1897
|
+
ok: false,
|
|
1898
|
+
explainReceipt: explainResult,
|
|
1899
|
+
message: '偏好学习服务当前已禁用(enabled=false),已记录但暂不会激活生效',
|
|
1900
|
+
};
|
|
1901
|
+
}
|
|
1902
|
+
if (explainResult.state.collectionMode === 'paused') {
|
|
1903
|
+
return {
|
|
1904
|
+
ok: true,
|
|
1905
|
+
explainReceipt: explainResult,
|
|
1906
|
+
message: '偏好学习当前处于暂停状态(/learning resume 可恢复);选择已记录,恢复后激活',
|
|
1907
|
+
};
|
|
1908
|
+
}
|
|
1909
|
+
const activePairs = new Set((explainResult.explanation ?? [])
|
|
1910
|
+
.filter(item => item.state === 'active')
|
|
1911
|
+
.map(item => `${item.key}\0${item.value}`));
|
|
1912
|
+
const allActive = selections.every(sel => activePairs.has(`${sel.preferenceKey}\0${sel.candidateValue}`));
|
|
1913
|
+
if (allActive) {
|
|
1914
|
+
return {
|
|
1915
|
+
ok: true,
|
|
1916
|
+
explainReceipt: explainResult,
|
|
1917
|
+
message: `对话偏好已更新并立即激活(${selections.map(s => `${s.preferenceKey}=${s.candidateValue}`).join(',')})`,
|
|
1918
|
+
};
|
|
1919
|
+
}
|
|
1920
|
+
return {
|
|
1921
|
+
ok: true,
|
|
1922
|
+
explainReceipt: explainResult,
|
|
1923
|
+
message: '已记录偏好选择;当前未立即激活(可能受 autonomousT1Enabled=false 或激活上限限制)',
|
|
1924
|
+
};
|
|
1925
|
+
}
|
|
1926
|
+
return { ok: true, message: '已提交对话偏好选择' };
|
|
1927
|
+
}
|
|
1928
|
+
async settleConfigAction(input) {
|
|
1929
|
+
this.assertActive();
|
|
1930
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._@:-]{0,255}$/u.test(input.operationId)
|
|
1931
|
+
|| !/^[A-Za-z0-9][A-Za-z0-9._@:-]{0,255}$/u.test(input.callbackEventId)
|
|
1932
|
+
|| !/^[A-Za-z0-9][A-Za-z0-9._@/-]{0,511}$/u.test(input.cardMessageId)
|
|
1933
|
+
|| !/^[A-Za-z0-9][A-Za-z0-9._@:-]{0,255}$/u.test(input.bindingId)
|
|
1934
|
+
|| !/^[a-f0-9]{64}$/u.test(input.expectedStateHash)
|
|
1935
|
+
|| !Number.isSafeInteger(input.bindingVersion) || input.bindingVersion < 1
|
|
1936
|
+
|| !Number.isSafeInteger(input.issuedAt) || input.issuedAt < 1
|
|
1937
|
+
|| !Number.isSafeInteger(input.expiresAt) || input.expiresAt <= input.issuedAt) {
|
|
1938
|
+
throw new AssistantDeliveryError('missing-binding', 'config picker callback shape is invalid');
|
|
1939
|
+
}
|
|
1940
|
+
const binding = this.deliveryStore.getBinding(input.bindingId);
|
|
1941
|
+
const active = binding === undefined ? undefined : this.deliveryStore.getActiveBinding(binding.conversation);
|
|
1942
|
+
const principal = this.deliveryStore.getPrincipal(input.principal);
|
|
1943
|
+
if (binding?.status !== 'active' || active?.id !== binding.id
|
|
1944
|
+
|| principal?.status !== 'active' || principal.role !== 'owner'
|
|
1945
|
+
|| !this.ownsSession(binding.sessionId)
|
|
1946
|
+
|| binding.version !== input.bindingVersion
|
|
1947
|
+
|| binding.sessionId !== input.sessionId
|
|
1948
|
+
|| binding.conversation.kind !== input.conversationKind
|
|
1949
|
+
|| binding.conversation.chat !== input.callbackChatId
|
|
1950
|
+
|| externalPrincipalId(binding.principal) !== externalPrincipalId(input.principal)) {
|
|
1951
|
+
throw new AssistantDeliveryError('missing-binding', 'config picker callback principal or chat does not own the active binding');
|
|
1952
|
+
}
|
|
1953
|
+
const record = this.deliveryStore.getConfigPickerRecord(input.operationId, input.bindingId);
|
|
1954
|
+
const picker = record?.intent.configPicker;
|
|
1955
|
+
if (record === undefined || picker === undefined
|
|
1956
|
+
|| !['accepted', 'delivered', 'read'].includes(record.status)
|
|
1957
|
+
|| record.providerMessageId !== input.cardMessageId
|
|
1958
|
+
|| picker.operationId !== input.operationId
|
|
1959
|
+
|| picker.issuedAt !== input.issuedAt
|
|
1960
|
+
|| picker.expiresAt !== input.expiresAt
|
|
1961
|
+
|| Date.now() >= picker.expiresAt) {
|
|
1962
|
+
throw new AssistantDeliveryError('missing-binding', 'config picker card is expired or unavailable');
|
|
1963
|
+
}
|
|
1964
|
+
const accessDecision = this.policy.evaluate({
|
|
1965
|
+
subject: { kind: 'external', id: externalPrincipalId(principal.principal) },
|
|
1966
|
+
action: 'ingest',
|
|
1967
|
+
resource: { kind: 'message', id: `config:${binding.id}` },
|
|
1968
|
+
context: { initiator: 'external' },
|
|
1969
|
+
});
|
|
1970
|
+
if (accessDecision.effect !== 'allow') {
|
|
1971
|
+
throw policyDenied(accessDecision);
|
|
1972
|
+
}
|
|
1973
|
+
if ((input.category === 'providers' || input.action === 'switch_provider' || input.ephemeralSecret !== undefined)
|
|
1974
|
+
&& binding.conversation.kind !== 'dm') {
|
|
1975
|
+
return {
|
|
1976
|
+
ok: false,
|
|
1977
|
+
toastType: 'warning',
|
|
1978
|
+
toastMessage: '安全限制:模型 Provider 与 API Key 配置仅限在机器人私聊(DM)中操作',
|
|
1979
|
+
};
|
|
1980
|
+
}
|
|
1981
|
+
if (input.action === 'close') {
|
|
1982
|
+
return {
|
|
1983
|
+
ok: true,
|
|
1984
|
+
toastType: 'info',
|
|
1985
|
+
toastMessage: '配置面板已关闭',
|
|
1986
|
+
closed: true,
|
|
1987
|
+
};
|
|
1988
|
+
}
|
|
1989
|
+
if (input.action === 'switch_category' || input.action === 'switch_provider') {
|
|
1990
|
+
const explainReceipt = await this.readExplainReceiptForCallback(binding, input.operationId);
|
|
1991
|
+
const nextSnapshot = await this.buildConfigPickerSnapshotForBinding({
|
|
1992
|
+
binding,
|
|
1993
|
+
operationId: picker.operationId,
|
|
1994
|
+
issuedAt: picker.issuedAt,
|
|
1995
|
+
expiresAt: picker.expiresAt,
|
|
1996
|
+
activeCategory: input.category,
|
|
1997
|
+
...(input.providerId === undefined ? {} : { activeProviderId: input.providerId }),
|
|
1998
|
+
...(explainReceipt === undefined ? {} : { learningExplainReceipt: explainReceipt }),
|
|
1999
|
+
});
|
|
2000
|
+
return {
|
|
2001
|
+
ok: true,
|
|
2002
|
+
toastType: 'info',
|
|
2003
|
+
toastMessage: `已切换至「${nextSnapshot.categories.find(c => c.id === input.category)?.label ?? input.category}」`,
|
|
2004
|
+
configPicker: nextSnapshot,
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
2007
|
+
const currentExplain = input.category === 'conversation'
|
|
2008
|
+
? await this.readExplainReceiptForCallback(binding, input.operationId)
|
|
2009
|
+
: undefined;
|
|
2010
|
+
const currentSnapshot = await this.buildConfigPickerSnapshotForBinding({
|
|
2011
|
+
binding,
|
|
2012
|
+
operationId: picker.operationId,
|
|
2013
|
+
issuedAt: picker.issuedAt,
|
|
2014
|
+
expiresAt: picker.expiresAt,
|
|
2015
|
+
activeCategory: input.category,
|
|
2016
|
+
...(input.providerId === undefined ? {} : { activeProviderId: input.providerId }),
|
|
2017
|
+
...(currentExplain === undefined ? {} : { learningExplainReceipt: currentExplain }),
|
|
2018
|
+
});
|
|
2019
|
+
if (currentSnapshot.stateHash !== input.expectedStateHash) {
|
|
2020
|
+
return {
|
|
2021
|
+
ok: false,
|
|
2022
|
+
toastType: 'warning',
|
|
2023
|
+
toastMessage: '配置状态已变化,已为您刷新最新视图,请核对后再提交',
|
|
2024
|
+
configPicker: currentSnapshot,
|
|
2025
|
+
notice: '检测到配置在卡片打开期间已更新,已刷新最新值;本次未写入。',
|
|
2026
|
+
};
|
|
2027
|
+
}
|
|
2028
|
+
const mutateDecision = this.policy.authorize({
|
|
2029
|
+
subject: { kind: 'external', id: externalPrincipalId(principal.principal) },
|
|
2030
|
+
action: 'ingest',
|
|
2031
|
+
resource: { kind: 'message', id: `config-mutate:${binding.id}:${input.category}` },
|
|
2032
|
+
context: { initiator: 'external' },
|
|
2033
|
+
}, { idempotencyKey: `config-mutate:${input.operationId}:${input.callbackEventId}` });
|
|
2034
|
+
if (mutateDecision.effect !== 'allow') {
|
|
2035
|
+
throw policyDenied(mutateDecision);
|
|
2036
|
+
}
|
|
2037
|
+
if (input.category === 'conversation') {
|
|
2038
|
+
const safeValues = input.formValues ?? {};
|
|
2039
|
+
const textParts = Object.entries(safeValues).map(([k, v]) => `${k}=${v}`);
|
|
2040
|
+
const syntheticCommandText = `/config conversation set ${textParts.join(' ')}`.trim();
|
|
2041
|
+
return await this.runConversationTransition(binding.conversation, async () => {
|
|
2042
|
+
const envelope = {
|
|
2043
|
+
channel: binding.conversation.channel,
|
|
2044
|
+
account: binding.conversation.account,
|
|
2045
|
+
eventId: input.callbackEventId,
|
|
2046
|
+
occurredAt: Date.now(),
|
|
2047
|
+
principal: { ...principal.principal },
|
|
2048
|
+
conversation: { ...binding.conversation },
|
|
2049
|
+
kind: 'command',
|
|
2050
|
+
text: syntheticCommandText,
|
|
2051
|
+
metadata: {
|
|
2052
|
+
'config-picker-operation': picker.operationId,
|
|
2053
|
+
},
|
|
2054
|
+
};
|
|
2055
|
+
const claimed = this.deliveryStore.claimConfigCallbackInbox({
|
|
2056
|
+
envelope,
|
|
2057
|
+
bindingId: binding.id,
|
|
2058
|
+
ownerId: this.ownerId,
|
|
2059
|
+
leaseMs: this.config.leaseMs,
|
|
2060
|
+
});
|
|
2061
|
+
if (claimed.duplicate) {
|
|
2062
|
+
const explainReceipt = await this.readExplainReceiptForCallback(binding, input.operationId);
|
|
2063
|
+
const refreshed = await this.buildConfigPickerSnapshotForBinding({
|
|
2064
|
+
binding,
|
|
2065
|
+
operationId: picker.operationId,
|
|
2066
|
+
issuedAt: picker.issuedAt,
|
|
2067
|
+
expiresAt: picker.expiresAt,
|
|
2068
|
+
activeCategory: 'conversation',
|
|
2069
|
+
...(explainReceipt === undefined ? {} : { learningExplainReceipt: explainReceipt }),
|
|
2070
|
+
});
|
|
2071
|
+
return {
|
|
2072
|
+
ok: true,
|
|
2073
|
+
toastType: 'info',
|
|
2074
|
+
toastMessage: '该偏好提交已处理',
|
|
2075
|
+
configPicker: refreshed,
|
|
2076
|
+
};
|
|
2077
|
+
}
|
|
2078
|
+
let outcome;
|
|
2079
|
+
try {
|
|
2080
|
+
const t1Values = {};
|
|
2081
|
+
let showProcessLogsVal;
|
|
2082
|
+
let unknownKey;
|
|
2083
|
+
for (const [key, val] of Object.entries(safeValues)) {
|
|
2084
|
+
if (Object.hasOwn(T1_TEXT_KEY_ALIASES, key)) {
|
|
2085
|
+
t1Values[key] = val;
|
|
2086
|
+
}
|
|
2087
|
+
else if (key === OWNER_CONSOLE_PROCESS_LOGS_KEY) {
|
|
2088
|
+
showProcessLogsVal = val;
|
|
2089
|
+
}
|
|
2090
|
+
else {
|
|
2091
|
+
unknownKey = key;
|
|
2092
|
+
break;
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
if (unknownKey !== undefined) {
|
|
2096
|
+
outcome = { ok: false, message: `不支持的对话偏好字段:${unknownKey}` };
|
|
2097
|
+
}
|
|
2098
|
+
else {
|
|
2099
|
+
const ctxAny = this.context;
|
|
2100
|
+
const settingsSvc = ctxAny.get?.('settings');
|
|
2101
|
+
const currentShowProcessLogs = readOwnerConsoleShowProcessLogs(settingsSvc);
|
|
2102
|
+
const hasT1Edits = Object.keys(t1Values).length > 0;
|
|
2103
|
+
const normalizedShowVal = showProcessLogsVal?.trim().toLowerCase();
|
|
2104
|
+
const showUnchanged = currentShowProcessLogs !== undefined
|
|
2105
|
+
&& (normalizedShowVal === String(currentShowProcessLogs)
|
|
2106
|
+
|| (currentShowProcessLogs && (normalizedShowVal === '1' || normalizedShowVal === 'on' || normalizedShowVal === 'yes'))
|
|
2107
|
+
|| (!currentShowProcessLogs && (normalizedShowVal === '0' || normalizedShowVal === 'off' || normalizedShowVal === 'no')));
|
|
2108
|
+
const shouldMutateShow = showProcessLogsVal !== undefined && (!hasT1Edits || !showUnchanged);
|
|
2109
|
+
if (!hasT1Edits && !shouldMutateShow) {
|
|
2110
|
+
outcome = { ok: false, message: '未检测到可修改的对话偏好字段' };
|
|
2111
|
+
}
|
|
2112
|
+
else {
|
|
2113
|
+
const convCat = currentSnapshot.categories.find(cat => cat.id === 'conversation');
|
|
2114
|
+
const expectedRevisions = convCat?.namespace !== undefined && convCat?.revision !== undefined
|
|
2115
|
+
? { [convCat.namespace]: convCat.revision }
|
|
2116
|
+
: undefined;
|
|
2117
|
+
let preValidationError;
|
|
2118
|
+
if (hasT1Edits) {
|
|
2119
|
+
const t1Validation = parseT1ConversationSelections(t1Values);
|
|
2120
|
+
if (!t1Validation.ok)
|
|
2121
|
+
preValidationError = t1Validation.message;
|
|
2122
|
+
}
|
|
2123
|
+
if (preValidationError === undefined && shouldMutateShow && showProcessLogsVal !== undefined) {
|
|
2124
|
+
const showValidation = validateOwnerConsoleConversationMutation({
|
|
2125
|
+
values: { [OWNER_CONSOLE_PROCESS_LOGS_KEY]: showProcessLogsVal },
|
|
2126
|
+
...(expectedRevisions === undefined ? {} : { expectedRevisions }),
|
|
2127
|
+
...(settingsSvc === undefined ? {} : { settings: settingsSvc }),
|
|
2128
|
+
});
|
|
2129
|
+
if (!showValidation.ok)
|
|
2130
|
+
preValidationError = showValidation.message;
|
|
2131
|
+
}
|
|
2132
|
+
if (preValidationError !== undefined) {
|
|
2133
|
+
outcome = { ok: false, message: preValidationError };
|
|
2134
|
+
}
|
|
2135
|
+
else {
|
|
2136
|
+
const messages = [];
|
|
2137
|
+
let allOk = true;
|
|
2138
|
+
let explainReceipt;
|
|
2139
|
+
if (hasT1Edits) {
|
|
2140
|
+
const t1Res = await this.applyConversationPreferenceMutation(binding, envelope, t1Values);
|
|
2141
|
+
allOk = allOk && t1Res.ok;
|
|
2142
|
+
messages.push(t1Res.message);
|
|
2143
|
+
explainReceipt = t1Res.explainReceipt;
|
|
2144
|
+
}
|
|
2145
|
+
if (allOk && shouldMutateShow && showProcessLogsVal !== undefined) {
|
|
2146
|
+
const showRes = await applyOwnerConsoleConversationMutation({
|
|
2147
|
+
values: { [OWNER_CONSOLE_PROCESS_LOGS_KEY]: showProcessLogsVal },
|
|
2148
|
+
...(expectedRevisions === undefined ? {} : { expectedRevisions }),
|
|
2149
|
+
...(settingsSvc === undefined ? {} : { settings: settingsSvc }),
|
|
2150
|
+
});
|
|
2151
|
+
allOk = allOk && showRes.ok;
|
|
2152
|
+
messages.push(showRes.message);
|
|
2153
|
+
}
|
|
2154
|
+
outcome = {
|
|
2155
|
+
ok: allOk,
|
|
2156
|
+
message: messages.join(';'),
|
|
2157
|
+
...(explainReceipt === undefined ? {} : { explainReceipt }),
|
|
2158
|
+
};
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
this.deliveryStore.finishInbox({
|
|
2163
|
+
inboxId: claimed.record.id,
|
|
2164
|
+
ownerId: this.ownerId,
|
|
2165
|
+
fencingToken: claimed.fencingToken,
|
|
2166
|
+
outcome: 'processed',
|
|
2167
|
+
});
|
|
2168
|
+
}
|
|
2169
|
+
catch (error) {
|
|
2170
|
+
try {
|
|
2171
|
+
this.deliveryStore.finishInbox({
|
|
2172
|
+
inboxId: claimed.record.id,
|
|
2173
|
+
ownerId: this.ownerId,
|
|
2174
|
+
fencingToken: claimed.fencingToken,
|
|
2175
|
+
outcome: 'dead_letter',
|
|
2176
|
+
failureCode: 'config-callback-failed',
|
|
2177
|
+
});
|
|
2178
|
+
}
|
|
2179
|
+
catch { }
|
|
2180
|
+
throw error;
|
|
2181
|
+
}
|
|
2182
|
+
const refreshed = await this.buildConfigPickerSnapshotForBinding({
|
|
2183
|
+
binding,
|
|
2184
|
+
operationId: picker.operationId,
|
|
2185
|
+
issuedAt: picker.issuedAt,
|
|
2186
|
+
expiresAt: picker.expiresAt,
|
|
2187
|
+
activeCategory: 'conversation',
|
|
2188
|
+
...(outcome.explainReceipt === undefined ? {} : { learningExplainReceipt: outcome.explainReceipt }),
|
|
2189
|
+
categoryNotices: { conversation: outcome.message },
|
|
2190
|
+
});
|
|
2191
|
+
return {
|
|
2192
|
+
ok: outcome.ok,
|
|
2193
|
+
toastType: outcome.ok ? 'success' : 'warning',
|
|
2194
|
+
toastMessage: outcome.message,
|
|
2195
|
+
configPicker: refreshed,
|
|
2196
|
+
notice: outcome.message,
|
|
2197
|
+
};
|
|
2198
|
+
});
|
|
2199
|
+
}
|
|
2200
|
+
const ctxAny = this.context;
|
|
2201
|
+
const settingsSvc = ctxAny.get?.('settings');
|
|
2202
|
+
const credentialsSvc = ctxAny.get?.('credentials');
|
|
2203
|
+
const defaultModelSvc = ctxAny.get?.('agentDefaultModel');
|
|
2204
|
+
const outcome = await applyNonPreferenceConfigMutation({
|
|
2205
|
+
category: input.category,
|
|
2206
|
+
conversationKind: binding.conversation.kind,
|
|
2207
|
+
values: input.formValues ?? {},
|
|
2208
|
+
...(input.ephemeralSecret === undefined ? {} : { ephemeralSecret: input.ephemeralSecret }),
|
|
2209
|
+
...(input.providerId === undefined ? {} : { targetProviderId: input.providerId }),
|
|
2210
|
+
...(settingsSvc === undefined ? {} : { settings: settingsSvc }),
|
|
2211
|
+
...(credentialsSvc === undefined ? {} : { credentials: credentialsSvc }),
|
|
2212
|
+
...(defaultModelSvc === undefined ? {} : { agentDefaultModel: defaultModelSvc }),
|
|
2213
|
+
});
|
|
2214
|
+
const safeMessage = redactConfigSecret(outcome.message, input.ephemeralSecret);
|
|
2215
|
+
const nextProviderId = outcome.activeProviderId ?? input.providerId;
|
|
2216
|
+
const refreshed = await this.buildConfigPickerSnapshotForBinding({
|
|
2217
|
+
binding,
|
|
2218
|
+
operationId: picker.operationId,
|
|
2219
|
+
issuedAt: picker.issuedAt,
|
|
2220
|
+
expiresAt: picker.expiresAt,
|
|
2221
|
+
activeCategory: input.category,
|
|
2222
|
+
...(nextProviderId === undefined ? {} : { activeProviderId: nextProviderId }),
|
|
2223
|
+
categoryNotices: { [input.category]: safeMessage },
|
|
2224
|
+
});
|
|
2225
|
+
return {
|
|
2226
|
+
ok: outcome.ok,
|
|
2227
|
+
toastType: outcome.ok ? 'success' : 'warning',
|
|
2228
|
+
toastMessage: safeMessage,
|
|
2229
|
+
configPicker: refreshed,
|
|
2230
|
+
notice: safeMessage,
|
|
2231
|
+
};
|
|
2232
|
+
}
|
|
1771
2233
|
async settlePermissionSelection(input) {
|
|
1772
2234
|
this.assertActive();
|
|
1773
2235
|
if (!/^[A-Za-z0-9][A-Za-z0-9._@:-]{0,255}$/u.test(input.callbackEventId)
|
|
@@ -2397,6 +2859,19 @@ export class AssistantDeliveryService extends Service {
|
|
|
2397
2859
|
if (sink !== undefined)
|
|
2398
2860
|
await sink.registration.completed(handle);
|
|
2399
2861
|
}
|
|
2862
|
+
listSlashCommands() {
|
|
2863
|
+
let activeNativeCommandNames;
|
|
2864
|
+
try {
|
|
2865
|
+
const commands = this.ctx.get('commands');
|
|
2866
|
+
if (typeof commands?.list === 'function') {
|
|
2867
|
+
activeNativeCommandNames = commands.list(undefined).map(entry => entry.name);
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
catch {
|
|
2871
|
+
activeNativeCommandNames = undefined;
|
|
2872
|
+
}
|
|
2873
|
+
return listLarkSlashCommandSpecs(activeNativeCommandNames === undefined ? undefined : { activeNativeCommandNames });
|
|
2874
|
+
}
|
|
2400
2875
|
registerAdapter(adapter) {
|
|
2401
2876
|
this.assertActive();
|
|
2402
2877
|
return this.registry.register(adapter).then(unregister => {
|
|
@@ -7973,6 +8448,9 @@ export class AssistantDeliveryService extends Service {
|
|
|
7973
8448
|
target: { conversation: binding.conversation, principal: binding.principal },
|
|
7974
8449
|
text: input.text,
|
|
7975
8450
|
format,
|
|
8451
|
+
...(format !== 'config-picker' || input.configPicker === undefined
|
|
8452
|
+
? {}
|
|
8453
|
+
: { configPicker: input.configPicker }),
|
|
7976
8454
|
...(input.modelPicker === undefined ? {} : { modelPicker: input.modelPicker }),
|
|
7977
8455
|
...(format !== 'permission-picker' || input.permissionPicker === undefined
|
|
7978
8456
|
? {}
|
|
@@ -7990,9 +8468,9 @@ export class AssistantDeliveryService extends Service {
|
|
|
7990
8468
|
* degrade to plain text elsewhere, so a reply is never lost to a capability mismatch.
|
|
7991
8469
|
*/
|
|
7992
8470
|
replyFormat(conversation, requested) {
|
|
7993
|
-
if (requested === 'permission-picker') {
|
|
8471
|
+
if (requested === 'permission-picker' || requested === 'config-picker') {
|
|
7994
8472
|
const adapter = this.registry.get(conversation.channel, conversation.account);
|
|
7995
|
-
return adapter?.capabilities.formats.includes(
|
|
8473
|
+
return adapter?.capabilities.formats.includes(requested) === true ? requested : 'plain';
|
|
7996
8474
|
}
|
|
7997
8475
|
if (requested !== 'markdown')
|
|
7998
8476
|
return requested;
|