@thready-web/dsh-bot-plugin 0.1.0-beta.0 → 0.1.0-beta.10
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 +109 -1
- package/client.js +646 -621
- package/dist/approvals.js +160 -0
- package/dist/authorization.js +225 -0
- package/dist/cli.js +32 -12
- package/dist/context.js +59 -17
- package/dist/cron.js +1 -1
- package/dist/cua-access.js +79 -0
- package/dist/desk.js +314 -37
- package/dist/engine.js +178 -84
- package/dist/fs-safe.js +1 -1
- package/dist/host.js +1 -0
- package/dist/http-plugin.js +14 -4
- package/dist/http.js +383 -85
- package/dist/inbox.js +51 -40
- package/dist/index.js +190 -47
- package/dist/live-diff.js +3 -1
- package/dist/live-snapshot.js +2 -13
- package/dist/log/canonical.js +37 -0
- package/dist/log/plan.js +62 -0
- package/dist/log/routes.js +144 -0
- package/dist/log/snapshot-service.js +76 -0
- package/dist/log/source.js +173 -0
- package/dist/memory-catalog.js +798 -0
- package/dist/permissions.js +86 -0
- package/dist/provision.js +2 -0
- package/dist/questions.js +407 -0
- package/dist/runtime-registry.js +4 -4
- package/dist/session-files.js +2 -2
- package/dist/session-previews.js +5 -1
- package/dist/store.js +20 -5
- package/dist/tools.js +150 -32
- package/dist/triggers.js +83 -39
- package/dist/work-admit.js +222 -25
- package/dist/work-child.js +17 -9
- package/dist/work-http.js +33 -2
- package/dist/work-ledger.js +256 -16
- package/dist/work-mock-host.js +9 -6
- package/dist/work-mock.js +29 -19
- package/dist/work-tickets.js +423 -96
- package/dist/work-tools.js +43 -6
- package/dist/work-worker.js +1028 -207
- package/dist/workspace-files.js +319 -0
- package/dist/workspace-image.js +91 -0
- package/fixtures/mock-llm.mjs +17 -2
- package/package.json +15 -5
- package/public/assets/SourceEditor-BEQaScgR.js +669 -0
- package/public/assets/SourceEditor-CJwK846Z.css +1 -0
- package/public/assets/abap-CRCWOmpq.js +1 -0
- package/public/assets/apex-DnsZk_dE.js +1 -0
- package/public/assets/azcli-1IWB1ccx.js +1 -0
- package/public/assets/bat-DPkNLes8.js +1 -0
- package/public/assets/bicep-Corcdgou.js +2 -0
- package/public/assets/cameligo-CGrWLZr3.js +1 -0
- package/public/assets/clojure-D9WOWImG.js +1 -0
- package/public/assets/codicon-DCmgc-ay.ttf +0 -0
- package/public/assets/coffee-B7EJu28W.js +1 -0
- package/public/assets/cpp-SEyurbux.js +1 -0
- package/public/assets/csharp-BoL64M5l.js +1 -0
- package/public/assets/csp-C46ZqvIl.js +1 -0
- package/public/assets/css-DQU6DXDx.js +3 -0
- package/public/assets/css.worker-DYpkewsP.js +79 -0
- package/public/assets/cssMode-gpcjCFFS.js +4 -0
- package/public/assets/cypher-D84EuPTj.js +1 -0
- package/public/assets/dart-D8lhlL1r.js +1 -0
- package/public/assets/dockerfile-DLk6rpji.js +1 -0
- package/public/assets/ecl-BO6FnfXk.js +1 -0
- package/public/assets/editor.worker-B4pQIWZD.js +12 -0
- package/public/assets/elixir-BRjLKONM.js +1 -0
- package/public/assets/flow9-Cac8vKd7.js +1 -0
- package/public/assets/freemarker2-C1qemGIt.js +3 -0
- package/public/assets/fsharp-fd1GTHhf.js +1 -0
- package/public/assets/go-O9LJTZXk.js +1 -0
- package/public/assets/graphql-LQdxqEYJ.js +1 -0
- package/public/assets/handlebars-sZqIfAlP.js +1 -0
- package/public/assets/hcl-DxDQ3s82.js +1 -0
- package/public/assets/html-Bn1WXB8L.js +1 -0
- package/public/assets/html.worker-tnXziJOq.js +456 -0
- package/public/assets/htmlMode-CvQyXnaW.js +4 -0
- package/public/assets/index-DRNkUrer.css +1 -0
- package/public/assets/index-DhL5Kdlz.js +63 -0
- package/public/assets/ini-BvajGCUy.js +1 -0
- package/public/assets/java-SYsfObOQ.js +1 -0
- package/public/assets/javascript-Ox4Jg12j.js +1 -0
- package/public/assets/json.worker-aFakAbRR.js +44 -0
- package/public/assets/jsonMode-DMR7R2dQ.js +10 -0
- package/public/assets/julia-DQXNmw_w.js +1 -0
- package/public/assets/kotlin-qQ0MG-9I.js +1 -0
- package/public/assets/less-GGFNNJHn.js +2 -0
- package/public/assets/lexon-Canl7DCW.js +1 -0
- package/public/assets/liquid-B6HxtpBd.js +1 -0
- package/public/assets/lua-D28Ae8-K.js +1 -0
- package/public/assets/m3-DPitgjJI.js +1 -0
- package/public/assets/markdown-B811l8j2.js +1 -0
- package/public/assets/mdx--jDhmrXq.js +1 -0
- package/public/assets/mips-CdjsipkG.js +1 -0
- package/public/assets/msdax-CYqgjx_P.js +1 -0
- package/public/assets/mysql-BHd6q0vd.js +1 -0
- package/public/assets/objective-c-B1aVtJYH.js +1 -0
- package/public/assets/pascal-BhNW15KB.js +1 -0
- package/public/assets/pascaligo-5jv8CcQD.js +1 -0
- package/public/assets/perl-DlYyT36c.js +1 -0
- package/public/assets/pgsql-Dy0bjov7.js +1 -0
- package/public/assets/php-120yhfDK.js +1 -0
- package/public/assets/pla-CjnFlu4u.js +1 -0
- package/public/assets/postiats-CQpG440k.js +1 -0
- package/public/assets/powerquery-DdJtto1Z.js +1 -0
- package/public/assets/powershell-Bu_VLpJB.js +1 -0
- package/public/assets/protobuf-IBS6jZEB.js +2 -0
- package/public/assets/pug-kFxLfcjb.js +1 -0
- package/public/assets/python-YjG50skX.js +1 -0
- package/public/assets/qsharp-q7JyzKFN.js +1 -0
- package/public/assets/r-BIFz-_sK.js +1 -0
- package/public/assets/razor-CEcFE3UK.js +1 -0
- package/public/assets/redis-CHOsPHWR.js +1 -0
- package/public/assets/redshift-CBifECDb.js +1 -0
- package/public/assets/restructuredtext-CghPJEOS.js +1 -0
- package/public/assets/ruby-CYWGW-b1.js +1 -0
- package/public/assets/rust-DMDD0SHb.js +1 -0
- package/public/assets/sb-BYAiYHFx.js +1 -0
- package/public/assets/scala-Bqvq8jcR.js +1 -0
- package/public/assets/scheme-Dhb-2j9p.js +1 -0
- package/public/assets/scss-CTwUZ5N7.js +3 -0
- package/public/assets/shell-CsDZo4DB.js +1 -0
- package/public/assets/solidity-CME5AdoB.js +1 -0
- package/public/assets/sophia-RYC1BQQz.js +1 -0
- package/public/assets/sparql-KEyrF7De.js +1 -0
- package/public/assets/sql-BdTr02Mf.js +1 -0
- package/public/assets/st-C7iG7M4S.js +1 -0
- package/public/assets/swift-D7IUmUK8.js +1 -0
- package/public/assets/systemverilog-DgMryOEJ.js +1 -0
- package/public/assets/tcl-PloMZuKG.js +1 -0
- package/public/assets/ts.worker-Bij5J2kb.js +51316 -0
- package/public/assets/tsMode-g4kTQl09.js +11 -0
- package/public/assets/twig-BfRIq3la.js +1 -0
- package/public/assets/typescript-CnHeFmPW.js +1 -0
- package/public/assets/typespec-CzxlYoT_.js +1 -0
- package/public/assets/vb-BwAE3J76.js +1 -0
- package/public/assets/wgsl-B_1kOXbF.js +298 -0
- package/public/assets/xml-BFXMY9nb.js +1 -0
- package/public/assets/yaml-DNm87nSf.js +1 -0
- package/public/index.html +2 -2
- package/public/assets/index-Be82iGwe.css +0 -1
- package/public/assets/index-ByU9a3Lh.js +0 -61
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official session permission-preset surface for the desk settings UI.
|
|
3
|
+
*
|
|
4
|
+
* The write path is `ctx.permissionPresets.set(session, name)`. Auto is
|
|
5
|
+
* offered only when the live catalog includes it (the experimental package
|
|
6
|
+
* has registered it). This is a settings concern, not a transcript card.
|
|
7
|
+
*/
|
|
8
|
+
import { toolError } from "./errors.js";
|
|
9
|
+
function sessionOf(ctx, sessionId) {
|
|
10
|
+
const agent = ctx.agents?.get?.(sessionId);
|
|
11
|
+
if (agent?.session)
|
|
12
|
+
return agent.session;
|
|
13
|
+
const sessions = ctx.sessions ?? ctx.get?.('sessions');
|
|
14
|
+
return sessions?.get?.(sessionId);
|
|
15
|
+
}
|
|
16
|
+
function optionList(value) {
|
|
17
|
+
if (!Array.isArray(value))
|
|
18
|
+
return [];
|
|
19
|
+
return value.flatMap(item => {
|
|
20
|
+
if (!item || typeof item !== 'object')
|
|
21
|
+
return [];
|
|
22
|
+
const row = item;
|
|
23
|
+
if (typeof row.value !== 'string' || row.value === '')
|
|
24
|
+
return [];
|
|
25
|
+
const option = {
|
|
26
|
+
value: row.value,
|
|
27
|
+
name: typeof row.name === 'string' && row.name !== '' ? row.name : row.value,
|
|
28
|
+
};
|
|
29
|
+
if (typeof row.description === 'string' && row.description !== '')
|
|
30
|
+
option.description = row.description;
|
|
31
|
+
return [option];
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
export function permissionView(ctx, sessionId) {
|
|
35
|
+
const service = ctx.get?.('permissionPresets');
|
|
36
|
+
if (!service || typeof service.catalog !== 'function') {
|
|
37
|
+
return {
|
|
38
|
+
available: false,
|
|
39
|
+
currentValue: '',
|
|
40
|
+
options: [],
|
|
41
|
+
defaultOptions: [],
|
|
42
|
+
defaultPreset: '',
|
|
43
|
+
autoAvailable: false,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const catalog = service.catalog() ?? {};
|
|
47
|
+
const options = optionList(catalog.options);
|
|
48
|
+
const defaultOptions = optionList(catalog.defaultOptions);
|
|
49
|
+
const session = sessionId !== '' ? sessionOf(ctx, sessionId) : undefined;
|
|
50
|
+
const currentValue = session && typeof service.current === 'function'
|
|
51
|
+
? String(service.current(session) ?? '')
|
|
52
|
+
: typeof catalog.defaultPreset === 'string'
|
|
53
|
+
? catalog.defaultPreset
|
|
54
|
+
: '';
|
|
55
|
+
return {
|
|
56
|
+
available: true,
|
|
57
|
+
currentValue,
|
|
58
|
+
options,
|
|
59
|
+
defaultOptions,
|
|
60
|
+
defaultPreset: typeof catalog.defaultPreset === 'string' ? catalog.defaultPreset : '',
|
|
61
|
+
autoAvailable: options.some(option => option.value === 'auto'),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export function setPermission(ctx, sessionId, name) {
|
|
65
|
+
if (typeof sessionId !== 'string' || sessionId === '') {
|
|
66
|
+
return toolError('VALIDATION', 'sessionId is required.');
|
|
67
|
+
}
|
|
68
|
+
if (typeof name !== 'string' || name === '' || name === 'custom') {
|
|
69
|
+
return toolError('VALIDATION', 'A selectable preset name is required.');
|
|
70
|
+
}
|
|
71
|
+
const service = ctx.get?.('permissionPresets');
|
|
72
|
+
if (!service || typeof service.set !== 'function') {
|
|
73
|
+
return toolError('UNAVAILABLE', 'Official permission presets are not composed on this host.');
|
|
74
|
+
}
|
|
75
|
+
const session = sessionOf(ctx, sessionId);
|
|
76
|
+
if (!session)
|
|
77
|
+
return toolError('NOT_FOUND', 'No live session is available for this permission change.');
|
|
78
|
+
try {
|
|
79
|
+
service.set(session, name);
|
|
80
|
+
return { ok: true, ...permissionView(ctx, sessionId) };
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const message = error instanceof Error ? error.message : 'The permission preset could not be changed.';
|
|
84
|
+
return toolError('VALIDATION', message);
|
|
85
|
+
}
|
|
86
|
+
}
|
package/dist/provision.js
CHANGED
|
@@ -33,6 +33,8 @@ export async function provisionSession(engine, input) {
|
|
|
33
33
|
? input.idempotencyKey.trim()
|
|
34
34
|
: `provision:${sessionId}:${createHash('sha256').update(factText).digest('hex').slice(0, 16)}`;
|
|
35
35
|
const remember = toolDefinitions(engine).find(item => item.name === 'foundations_remember');
|
|
36
|
+
if (!remember)
|
|
37
|
+
throw new Error('foundations_remember is unavailable.');
|
|
36
38
|
const fact = await remember.execute({
|
|
37
39
|
target: 'fact',
|
|
38
40
|
content: factText,
|
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official ask_user_question answerer for the desk UI.
|
|
3
|
+
*
|
|
4
|
+
* Open calls can be completed only by returning from the
|
|
5
|
+
* `user-questions/request` waterfall. `userQuestions.answer` is only for
|
|
6
|
+
* continued (timed/pending) questions. This listener is prepended so the
|
|
7
|
+
* typpert `$events` remote answerer cannot intercept the request before the
|
|
8
|
+
* desk can submit it. The official blocking legacy tool does not set
|
|
9
|
+
* `wait.callId`; timed requests do.
|
|
10
|
+
*/
|
|
11
|
+
import { toolError } from "./errors.js";
|
|
12
|
+
export function questionRejection(code, message) {
|
|
13
|
+
const error = new Error(message);
|
|
14
|
+
error.code = code;
|
|
15
|
+
error.name = 'UserQuestionError';
|
|
16
|
+
return error;
|
|
17
|
+
}
|
|
18
|
+
const answerers = new Map();
|
|
19
|
+
export function publishQuestionAnswerer(dataDir, answerer) {
|
|
20
|
+
if (answerer)
|
|
21
|
+
answerers.set(dataDir, answerer);
|
|
22
|
+
else
|
|
23
|
+
answerers.delete(dataDir);
|
|
24
|
+
}
|
|
25
|
+
export function questionAnswererFor(dataDir) {
|
|
26
|
+
return answerers.get(dataDir) ?? null;
|
|
27
|
+
}
|
|
28
|
+
export function abortedQuestion(cause) {
|
|
29
|
+
const error = new Error('ask_user_question was aborted before the user answered', cause === undefined ? undefined : { cause });
|
|
30
|
+
error.code = 'ASK_ABORTED';
|
|
31
|
+
return error;
|
|
32
|
+
}
|
|
33
|
+
export function sessionIdOf(agent) {
|
|
34
|
+
if (!agent || typeof agent !== 'object')
|
|
35
|
+
return '';
|
|
36
|
+
if (typeof agent.session === 'string' && agent.session !== '')
|
|
37
|
+
return agent.session;
|
|
38
|
+
if (typeof agent.id === 'string' && agent.id !== '')
|
|
39
|
+
return agent.id;
|
|
40
|
+
return '';
|
|
41
|
+
}
|
|
42
|
+
export function callIdOf(request) {
|
|
43
|
+
const callId = request.wait?.callId;
|
|
44
|
+
return typeof callId === 'string' && callId !== '' ? callId : undefined;
|
|
45
|
+
}
|
|
46
|
+
function sameIds(questions, answers) {
|
|
47
|
+
if (questions.length !== answers.length)
|
|
48
|
+
return false;
|
|
49
|
+
const ids = new Set(answers.map(item => item.id));
|
|
50
|
+
return ids.size === answers.length && questions.every(item => ids.has(item.id));
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Build the official answer batch from a desk submission, preserving exact
|
|
54
|
+
* question ids and option labels. Does not invent answers.
|
|
55
|
+
*/
|
|
56
|
+
export function officialAnswerFrom(questions, submitted) {
|
|
57
|
+
if (!Array.isArray(questions) || questions.length === 0) {
|
|
58
|
+
return { ok: false, code: 'VALIDATION', message: 'A question request is required.' };
|
|
59
|
+
}
|
|
60
|
+
if (!Array.isArray(submitted)) {
|
|
61
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'The answer batch must name each question exactly once.' };
|
|
62
|
+
}
|
|
63
|
+
const answers = [];
|
|
64
|
+
const seen = new Set();
|
|
65
|
+
for (const item of submitted) {
|
|
66
|
+
if (!item || typeof item !== 'object') {
|
|
67
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'The answer batch must name each question exactly once.' };
|
|
68
|
+
}
|
|
69
|
+
if (typeof item.id !== 'string' || item.id === '' || seen.has(item.id)) {
|
|
70
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'The answer batch must name each question exactly once.' };
|
|
71
|
+
}
|
|
72
|
+
const question = questions.find(entry => entry.id === item.id);
|
|
73
|
+
if (!question) {
|
|
74
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'The answer batch must name each question exactly once.' };
|
|
75
|
+
}
|
|
76
|
+
if (!Array.isArray(item.selected) || item.selected.some(value => typeof value !== 'string')) {
|
|
77
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'Each answer must include a selected label array.' };
|
|
78
|
+
}
|
|
79
|
+
const options = question.options ?? [];
|
|
80
|
+
for (const label of item.selected) {
|
|
81
|
+
if (options.length === 0 || !options.some(option => option.label === label)) {
|
|
82
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'A selected label is not one of the offered options.' };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (question.multiSelect !== true && item.selected.length > 1) {
|
|
86
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'This question does not allow more than one selected option.' };
|
|
87
|
+
}
|
|
88
|
+
if (item.custom !== undefined && typeof item.custom !== 'string') {
|
|
89
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'A custom answer must be a string.' };
|
|
90
|
+
}
|
|
91
|
+
const custom = typeof item.custom === 'string' ? item.custom : undefined;
|
|
92
|
+
if (question.multiSelect !== true && custom !== undefined && custom !== '' && item.selected.length > 0) {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
code: 'BAD_ANSWER',
|
|
96
|
+
message: 'A single-select custom answer must leave selected empty.',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
seen.add(item.id);
|
|
100
|
+
const entry = { id: item.id, selected: [...item.selected] };
|
|
101
|
+
if (custom !== undefined && custom !== '')
|
|
102
|
+
entry.custom = custom;
|
|
103
|
+
answers.push(entry);
|
|
104
|
+
}
|
|
105
|
+
if (!sameIds(questions, answers)) {
|
|
106
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'The answer batch must name each question exactly once.' };
|
|
107
|
+
}
|
|
108
|
+
return { ok: true, answer: { answers } };
|
|
109
|
+
}
|
|
110
|
+
function matchPending(pending, sessionId, callId, questionIds) {
|
|
111
|
+
const sessionRows = [...pending].filter(entry => entry.sessionId === sessionId);
|
|
112
|
+
if (callId) {
|
|
113
|
+
const byCall = sessionRows.filter(entry => entry.callId === callId);
|
|
114
|
+
if (byCall.length === 1)
|
|
115
|
+
return { ok: true, entry: byCall[0] };
|
|
116
|
+
if (byCall.length > 1) {
|
|
117
|
+
return { ok: false, code: 'CONFLICT', message: 'Multiple open questions match this call.' };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const byQuestions = sessionRows.filter(entry => {
|
|
121
|
+
if (entry.callId && callId && entry.callId !== callId)
|
|
122
|
+
return false;
|
|
123
|
+
const ids = entry.request.questions.map(question => question.id);
|
|
124
|
+
return (ids.length === questionIds.length &&
|
|
125
|
+
ids.every((id, index) => id === questionIds[index] || questionIds.includes(id)) &&
|
|
126
|
+
questionIds.every(id => ids.includes(id)));
|
|
127
|
+
});
|
|
128
|
+
if (byQuestions.length === 1)
|
|
129
|
+
return { ok: true, entry: byQuestions[0] };
|
|
130
|
+
if (byQuestions.length > 1) {
|
|
131
|
+
return { ok: false, code: 'CONFLICT', message: 'Multiple open questions match this session.' };
|
|
132
|
+
}
|
|
133
|
+
if (questionIds.length === 0 && !callId && sessionRows.length === 1) {
|
|
134
|
+
return { ok: true, entry: sessionRows[0] };
|
|
135
|
+
}
|
|
136
|
+
if (questionIds.length === 0 && !callId && sessionRows.length > 1) {
|
|
137
|
+
return { ok: false, code: 'CONFLICT', message: 'Multiple open questions match this session.' };
|
|
138
|
+
}
|
|
139
|
+
return { ok: false, code: 'NOT_FOUND', message: 'No open user-questions request matches this session.' };
|
|
140
|
+
}
|
|
141
|
+
/** Structural official answer batch. Option membership is checked only against an open request. */
|
|
142
|
+
export function wellFormedAnswerBatch(submitted) {
|
|
143
|
+
if (!Array.isArray(submitted) || submitted.length === 0) {
|
|
144
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'The answer batch must name each question exactly once.' };
|
|
145
|
+
}
|
|
146
|
+
const answers = [];
|
|
147
|
+
const seen = new Set();
|
|
148
|
+
for (const item of submitted) {
|
|
149
|
+
if (!item || typeof item !== 'object' || typeof item.id !== 'string' || item.id === '' || seen.has(item.id)) {
|
|
150
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'The answer batch must name each question exactly once.' };
|
|
151
|
+
}
|
|
152
|
+
if (!Array.isArray(item.selected) || item.selected.some(value => typeof value !== 'string')) {
|
|
153
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'Each answer must include a selected label array.' };
|
|
154
|
+
}
|
|
155
|
+
if (item.custom !== undefined && typeof item.custom !== 'string') {
|
|
156
|
+
return { ok: false, code: 'BAD_ANSWER', message: 'A custom answer must be a string.' };
|
|
157
|
+
}
|
|
158
|
+
seen.add(item.id);
|
|
159
|
+
const entry = { id: item.id, selected: [...item.selected] };
|
|
160
|
+
if (typeof item.custom === 'string' && item.custom !== '')
|
|
161
|
+
entry.custom = item.custom;
|
|
162
|
+
answers.push(entry);
|
|
163
|
+
}
|
|
164
|
+
return { ok: true, answer: { answers } };
|
|
165
|
+
}
|
|
166
|
+
export function createQuestionAnswerer() {
|
|
167
|
+
const pending = new Set();
|
|
168
|
+
let host = null;
|
|
169
|
+
function detach(entry) {
|
|
170
|
+
if (!pending.has(entry))
|
|
171
|
+
return false;
|
|
172
|
+
pending.delete(entry);
|
|
173
|
+
if (entry.onAbort && entry.request.signal) {
|
|
174
|
+
entry.request.signal.removeEventListener('abort', entry.onAbort);
|
|
175
|
+
}
|
|
176
|
+
entry.waitAbort?.abort();
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
function attachTimedWait(entry) {
|
|
180
|
+
const timed = entry.request.wait?.timed === true;
|
|
181
|
+
const callId = entry.callId;
|
|
182
|
+
const attachWait = host?.userQuestions?.attachWait;
|
|
183
|
+
const agent = host?.agents?.get?.(entry.sessionId);
|
|
184
|
+
if (!timed || !callId || !attachWait || !agent)
|
|
185
|
+
return;
|
|
186
|
+
const waitAbort = new AbortController();
|
|
187
|
+
entry.waitAbort = waitAbort;
|
|
188
|
+
void (async () => {
|
|
189
|
+
try {
|
|
190
|
+
for await (const frame of attachWait(agent, callId, waitAbort.signal)) {
|
|
191
|
+
if (!pending.has(entry) || entry.takeTime)
|
|
192
|
+
return;
|
|
193
|
+
if (typeof frame?.remainingMs === 'number' && Number.isFinite(frame.remainingMs)) {
|
|
194
|
+
entry.remainingMs = Math.max(0, frame.remainingMs);
|
|
195
|
+
entry.deadline = Date.now() + entry.remainingMs;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
/* claim ended with the wait */
|
|
201
|
+
}
|
|
202
|
+
})();
|
|
203
|
+
}
|
|
204
|
+
function delegate(entry) {
|
|
205
|
+
if (!detach(entry))
|
|
206
|
+
return;
|
|
207
|
+
Promise.resolve().then(entry.next).then(entry.resolve, entry.reject);
|
|
208
|
+
}
|
|
209
|
+
function waitForAnswer(request, next) {
|
|
210
|
+
const sessionId = sessionIdOf(request.agent);
|
|
211
|
+
if (sessionId === '')
|
|
212
|
+
return next();
|
|
213
|
+
return new Promise((resolve, reject) => {
|
|
214
|
+
const entry = {
|
|
215
|
+
request,
|
|
216
|
+
sessionId,
|
|
217
|
+
callId: callIdOf(request),
|
|
218
|
+
next,
|
|
219
|
+
resolve,
|
|
220
|
+
reject,
|
|
221
|
+
};
|
|
222
|
+
const onAbort = () => {
|
|
223
|
+
if (!detach(entry))
|
|
224
|
+
return;
|
|
225
|
+
reject(abortedQuestion());
|
|
226
|
+
};
|
|
227
|
+
entry.onAbort = onAbort;
|
|
228
|
+
pending.add(entry);
|
|
229
|
+
if (request.signal?.aborted) {
|
|
230
|
+
onAbort();
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
request.signal?.addEventListener('abort', onAbort, { once: true });
|
|
234
|
+
attachTimedWait(entry);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
async function handle(request, next) {
|
|
238
|
+
return waitForAnswer(request, next);
|
|
239
|
+
}
|
|
240
|
+
function submitOpen(input) {
|
|
241
|
+
const questionIds = input.answers.map(item => item.id);
|
|
242
|
+
const matched = matchPending(pending, input.sessionId, input.callId, questionIds);
|
|
243
|
+
if (!matched.ok) {
|
|
244
|
+
if (matched.code === 'CONFLICT')
|
|
245
|
+
return toolError(matched.code, matched.message);
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
const built = officialAnswerFrom(matched.entry.request.questions, input.answers);
|
|
249
|
+
if (!built.ok)
|
|
250
|
+
return toolError(built.code, built.message);
|
|
251
|
+
if (!detach(matched.entry))
|
|
252
|
+
return toolError('CONFLICT', 'This question was already answered.');
|
|
253
|
+
matched.entry.resolve(built.answer);
|
|
254
|
+
return { ok: true, path: 'open', answer: built.answer };
|
|
255
|
+
}
|
|
256
|
+
function submitContinued(input) {
|
|
257
|
+
const userQuestions = host?.userQuestions;
|
|
258
|
+
const agents = host?.agents;
|
|
259
|
+
if (!userQuestions || !agents) {
|
|
260
|
+
return toolError('NOT_FOUND', 'No open user-questions request matches this session.');
|
|
261
|
+
}
|
|
262
|
+
const callId = input.callId;
|
|
263
|
+
if (typeof callId !== 'string' || callId === '') {
|
|
264
|
+
return toolError('VALIDATION', 'callId is required to answer a continued question.');
|
|
265
|
+
}
|
|
266
|
+
const shaped = wellFormedAnswerBatch(input.answers);
|
|
267
|
+
if (!shaped.ok)
|
|
268
|
+
return toolError(shaped.code, shaped.message);
|
|
269
|
+
const agent = agents.get(input.sessionId);
|
|
270
|
+
if (!agent)
|
|
271
|
+
return toolError('NOT_FOUND', 'No live agent is available for this session.');
|
|
272
|
+
const agentSession = sessionIdOf(agent);
|
|
273
|
+
if (agentSession !== '' && agentSession !== input.sessionId) {
|
|
274
|
+
return toolError('FORBIDDEN', 'The answer does not belong to this session.');
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
const accepted = userQuestions.answer(agent, callId, shaped.answer);
|
|
278
|
+
if (!accepted)
|
|
279
|
+
return toolError('NOT_FOUND', 'This question is not a continued user-questions call.');
|
|
280
|
+
return { ok: true, path: 'continued', answer: shaped.answer };
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
const code = error && typeof error === 'object' && 'code' in error && typeof error.code === 'string'
|
|
284
|
+
? error.code
|
|
285
|
+
: 'UNAVAILABLE';
|
|
286
|
+
const message = error instanceof Error ? error.message : 'The continued question could not be answered.';
|
|
287
|
+
if (code === 'BAD_ANSWER')
|
|
288
|
+
return toolError('BAD_ANSWER', message);
|
|
289
|
+
if (code === 'REPLY_QUEUED')
|
|
290
|
+
return toolError('CONFLICT', message);
|
|
291
|
+
if (code === 'CALLER_NOT_LIVE' || code === 'DELEGATED_CALLER')
|
|
292
|
+
return toolError('FORBIDDEN', message);
|
|
293
|
+
return toolError(code, message);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function submit(input) {
|
|
297
|
+
if (typeof input.sessionId !== 'string' || input.sessionId === '') {
|
|
298
|
+
return toolError('VALIDATION', 'sessionId is required.');
|
|
299
|
+
}
|
|
300
|
+
const sessionId = input.sessionId;
|
|
301
|
+
const callId = typeof input.callId === 'string' && input.callId !== '' ? input.callId : undefined;
|
|
302
|
+
const answers = Array.isArray(input.answers) ? input.answers : null;
|
|
303
|
+
if (!answers)
|
|
304
|
+
return toolError('VALIDATION', 'answers is required.');
|
|
305
|
+
const open = submitOpen({ sessionId, callId, answers });
|
|
306
|
+
if (open)
|
|
307
|
+
return open;
|
|
308
|
+
return submitContinued({ sessionId, callId, answers });
|
|
309
|
+
}
|
|
310
|
+
function matchOpen(sessionId, callId, questionIds) {
|
|
311
|
+
return matchPending(pending, sessionId, callId, questionIds ?? []);
|
|
312
|
+
}
|
|
313
|
+
function cancel(input) {
|
|
314
|
+
if (typeof input.sessionId !== 'string' || input.sessionId === '') {
|
|
315
|
+
return toolError('VALIDATION', 'sessionId is required.');
|
|
316
|
+
}
|
|
317
|
+
const questionIds = Array.isArray(input.answers) ? input.answers.map(item => item.id) : [];
|
|
318
|
+
const matched = matchOpen(input.sessionId, input.callId, questionIds);
|
|
319
|
+
if (!matched.ok)
|
|
320
|
+
return toolError(matched.code, matched.message);
|
|
321
|
+
if (!detach(matched.entry))
|
|
322
|
+
return toolError('CONFLICT', 'This question was already answered.');
|
|
323
|
+
matched.entry.reject(questionRejection('ASK_CANCELLED', 'The user requested changes or cancelled the question.'));
|
|
324
|
+
return { ok: true, path: 'cancelled' };
|
|
325
|
+
}
|
|
326
|
+
function timeout(input) {
|
|
327
|
+
if (typeof input.sessionId !== 'string' || input.sessionId === '') {
|
|
328
|
+
return toolError('VALIDATION', 'sessionId is required.');
|
|
329
|
+
}
|
|
330
|
+
const matched = matchOpen(input.sessionId, input.callId);
|
|
331
|
+
if (!matched.ok)
|
|
332
|
+
return toolError(matched.code, matched.message);
|
|
333
|
+
if (matched.entry.request.wait?.timed !== true) {
|
|
334
|
+
return toolError('VALIDATION', 'Only a timed question can be settled as ASK_TIMED_OUT.');
|
|
335
|
+
}
|
|
336
|
+
if (!detach(matched.entry))
|
|
337
|
+
return toolError('CONFLICT', 'This question was already answered.');
|
|
338
|
+
matched.entry.reject(questionRejection('ASK_TIMED_OUT', 'The foreground answer window closed.'));
|
|
339
|
+
return { ok: true, path: 'timed-out' };
|
|
340
|
+
}
|
|
341
|
+
function takeTime(input) {
|
|
342
|
+
if (typeof input.sessionId !== 'string' || input.sessionId === '') {
|
|
343
|
+
return toolError('VALIDATION', 'sessionId is required.');
|
|
344
|
+
}
|
|
345
|
+
const matched = matchOpen(input.sessionId, input.callId);
|
|
346
|
+
if (!matched.ok)
|
|
347
|
+
return toolError(matched.code, matched.message);
|
|
348
|
+
matched.entry.takeTime = true;
|
|
349
|
+
matched.entry.remainingMs = undefined;
|
|
350
|
+
matched.entry.deadline = undefined;
|
|
351
|
+
return { ok: true, path: 'waiting' };
|
|
352
|
+
}
|
|
353
|
+
function remainingOf(entry) {
|
|
354
|
+
if (entry.takeTime)
|
|
355
|
+
return undefined;
|
|
356
|
+
if (typeof entry.deadline === 'number')
|
|
357
|
+
return Math.max(0, entry.deadline - Date.now());
|
|
358
|
+
return entry.remainingMs;
|
|
359
|
+
}
|
|
360
|
+
function snapshot(sessionId) {
|
|
361
|
+
return [...pending]
|
|
362
|
+
.filter(entry => !sessionId || entry.sessionId === sessionId)
|
|
363
|
+
.map(entry => {
|
|
364
|
+
const timed = entry.request.wait?.timed === true;
|
|
365
|
+
const remainingMs = remainingOf(entry);
|
|
366
|
+
const waitState = !timed
|
|
367
|
+
? 'none'
|
|
368
|
+
: entry.takeTime
|
|
369
|
+
? 'waiting'
|
|
370
|
+
: typeof remainingMs === 'number'
|
|
371
|
+
? 'counting'
|
|
372
|
+
: 'waiting';
|
|
373
|
+
return {
|
|
374
|
+
sessionId: entry.sessionId,
|
|
375
|
+
callId: entry.callId,
|
|
376
|
+
timed,
|
|
377
|
+
waitState,
|
|
378
|
+
remainingMs,
|
|
379
|
+
questions: entry.request.questions,
|
|
380
|
+
};
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
function listen(ctx) {
|
|
384
|
+
host = ctx;
|
|
385
|
+
const dispose = ctx.on?.('user-questions/request', handle, true);
|
|
386
|
+
return () => {
|
|
387
|
+
if (typeof dispose === 'function')
|
|
388
|
+
dispose();
|
|
389
|
+
for (const entry of [...pending])
|
|
390
|
+
delegate(entry);
|
|
391
|
+
if (host === ctx)
|
|
392
|
+
host = null;
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
listen,
|
|
397
|
+
handle,
|
|
398
|
+
submit,
|
|
399
|
+
cancel,
|
|
400
|
+
timeout,
|
|
401
|
+
takeTime,
|
|
402
|
+
snapshot,
|
|
403
|
+
pendingCount() {
|
|
404
|
+
return pending.size;
|
|
405
|
+
},
|
|
406
|
+
};
|
|
407
|
+
}
|
package/dist/runtime-registry.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/** @type {Map<string,
|
|
1
|
+
/** @type {Map<string, any>} */
|
|
2
2
|
const runtimes = new Map();
|
|
3
3
|
/**
|
|
4
4
|
* @param {string} dataDir
|
|
5
|
-
* @param {
|
|
5
|
+
* @param {any} runtime
|
|
6
6
|
*/
|
|
7
7
|
export function publishRuntime(dataDir, runtime) {
|
|
8
8
|
if (runtime)
|
|
@@ -16,11 +16,11 @@ export function publishRuntime(dataDir, runtime) {
|
|
|
16
16
|
export function runtimeFor(dataDir) {
|
|
17
17
|
return runtimes.get(dataDir) ?? null;
|
|
18
18
|
}
|
|
19
|
-
/** @type {Map<string,
|
|
19
|
+
/** @type {Map<string, any>} */
|
|
20
20
|
const workRuntimes = new Map();
|
|
21
21
|
/**
|
|
22
22
|
* @param {string} dataDir
|
|
23
|
-
* @param {
|
|
23
|
+
* @param {any} runtime
|
|
24
24
|
*/
|
|
25
25
|
export function publishWorkRuntime(dataDir, runtime) {
|
|
26
26
|
if (runtime)
|
package/dist/session-files.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Remove one
|
|
1
|
+
/** Remove one session's on-disk log. DSH has no delete RPC; session/list rescans these directories. */
|
|
2
2
|
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { join, resolve, sep } from 'node:path';
|
|
4
4
|
const SESSION_ID = /^session-[A-Za-z0-9-]+$/;
|
|
@@ -94,7 +94,7 @@ export function forgetWorkspaceId(workspaceFile, sessionId) {
|
|
|
94
94
|
return;
|
|
95
95
|
const data = JSON.parse(readFileSync(workspaceFile, 'utf8'));
|
|
96
96
|
if (data?.global && Array.isArray(data.global.archivedSessionIds)) {
|
|
97
|
-
data.global.archivedSessionIds = data.global.archivedSessionIds.filter(id => id !== sessionId);
|
|
97
|
+
data.global.archivedSessionIds = data.global.archivedSessionIds.filter((id) => id !== sessionId);
|
|
98
98
|
}
|
|
99
99
|
const workspaces = data?.tables?.workspaces;
|
|
100
100
|
if (workspaces && typeof workspaces === 'object') {
|
package/dist/session-previews.js
CHANGED
|
@@ -107,5 +107,9 @@ function sanitize(parsed) {
|
|
|
107
107
|
function cleanPreview(value) {
|
|
108
108
|
if (typeof value !== 'string')
|
|
109
109
|
return '';
|
|
110
|
-
return value
|
|
110
|
+
return value
|
|
111
|
+
.replace(/[\u0000-\u001f]+/g, ' ')
|
|
112
|
+
.replace(/\s+/g, ' ')
|
|
113
|
+
.trim()
|
|
114
|
+
.slice(0, MAX_PREVIEW);
|
|
111
115
|
}
|
package/dist/store.js
CHANGED
|
@@ -346,7 +346,16 @@ CREATE TABLE IF NOT EXISTS user_deliveries (
|
|
|
346
346
|
*/
|
|
347
347
|
export function openStore(dataDir) {
|
|
348
348
|
const root = ensureDir(dataDir);
|
|
349
|
-
for (const dir of [
|
|
349
|
+
for (const dir of [
|
|
350
|
+
'blobs/persona',
|
|
351
|
+
'blobs/skills',
|
|
352
|
+
'src/soul',
|
|
353
|
+
'src/skills/public',
|
|
354
|
+
'src/skills/private',
|
|
355
|
+
'export',
|
|
356
|
+
'backups',
|
|
357
|
+
'secrets/webhooks',
|
|
358
|
+
]) {
|
|
350
359
|
mkdirSync(join(root, dir), { recursive: true, mode: 0o700 });
|
|
351
360
|
}
|
|
352
361
|
const db = new DatabaseSync(join(root, 'persona.sqlite'));
|
|
@@ -390,12 +399,16 @@ class KeyedLock {
|
|
|
390
399
|
async run(key, fn, timeoutMs) {
|
|
391
400
|
const prev = this.tails.get(key) ?? Promise.resolve();
|
|
392
401
|
let release = () => { };
|
|
393
|
-
const current = new Promise(resolve => {
|
|
402
|
+
const current = new Promise(resolve => {
|
|
403
|
+
release = () => resolve();
|
|
404
|
+
});
|
|
394
405
|
this.tails.set(key, current);
|
|
395
406
|
let timer;
|
|
396
407
|
const acquired = await Promise.race([
|
|
397
408
|
prev.then(() => 'ok'),
|
|
398
|
-
new Promise(resolve => {
|
|
409
|
+
new Promise(resolve => {
|
|
410
|
+
timer = setTimeout(() => resolve('busy'), timeoutMs);
|
|
411
|
+
}),
|
|
399
412
|
]);
|
|
400
413
|
clearTimeout(timer);
|
|
401
414
|
if (acquired !== 'ok') {
|
|
@@ -419,7 +432,7 @@ export class Store {
|
|
|
419
432
|
lock;
|
|
420
433
|
busyTimeoutMs;
|
|
421
434
|
/**
|
|
422
|
-
* @param {
|
|
435
|
+
* @param {SqlDatabase} db
|
|
423
436
|
* @param {string} root
|
|
424
437
|
*/
|
|
425
438
|
constructor(db, root) {
|
|
@@ -447,7 +460,9 @@ export class Store {
|
|
|
447
460
|
try {
|
|
448
461
|
this.db.exec('ROLLBACK');
|
|
449
462
|
}
|
|
450
|
-
catch {
|
|
463
|
+
catch {
|
|
464
|
+
/* already closed */
|
|
465
|
+
}
|
|
451
466
|
throw error;
|
|
452
467
|
}
|
|
453
468
|
}, this.busyTimeoutMs);
|