@thready-web/dsh-bot-plugin 0.1.0-beta.1 → 0.1.0-beta.11
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/canvas-http.js +248 -0
- package/dist/canvas-skills.js +56 -0
- package/dist/canvas-store.js +794 -0
- package/dist/canvas-tools.js +211 -0
- package/dist/cli.js +32 -12
- package/dist/context.js +60 -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 +402 -85
- package/dist/inbox.js +51 -40
- package/dist/index.js +201 -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 +52 -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 +1033 -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 +16 -5
- package/public/assets/SourceEditor-CJwK846Z.css +1 -0
- package/public/assets/SourceEditor-uu0G7jsM.js +669 -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-C5DAFF5H.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-DExNwSiZ.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-BK7YAvpQ.js +1 -0
- package/public/assets/hcl-DxDQ3s82.js +1 -0
- package/public/assets/html-CdDm_zae.js +1 -0
- package/public/assets/html.worker-tnXziJOq.js +456 -0
- package/public/assets/htmlMode-CLdn9oa1.js +4 -0
- package/public/assets/index-BB-QDSf2.css +1 -0
- package/public/assets/index-CsX-_qbo.js +62 -0
- package/public/assets/ini-BvajGCUy.js +1 -0
- package/public/assets/java-SYsfObOQ.js +1 -0
- package/public/assets/javascript-mLl83_3W.js +1 -0
- package/public/assets/json.worker-aFakAbRR.js +44 -0
- package/public/assets/jsonMode-B1-bihFd.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-Cv8m0HUb.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-Q12b91rL.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-Kda811W0.js +1 -0
- package/public/assets/qsharp-q7JyzKFN.js +1 -0
- package/public/assets/r-BIFz-_sK.js +1 -0
- package/public/assets/razor-CRuCpdOW.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-BGHcz-M3.js +11 -0
- package/public/assets/twig-BfRIq3la.js +1 -0
- package/public/assets/typescript-Db1e0DCT.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-H3zzFmnd.js +1 -0
- package/public/assets/yaml-BQrKvmTF.js +1 -0
- package/public/index.html +2 -2
- package/skills/ui/baseline-ui/SKILL.md +85 -0
- package/skills/ui/bencium-innovative-ux-designer/SKILL.md +227 -0
- package/skills/ui/better-ui/SKILL.md +133 -0
- package/skills/ui/design-lab/SKILL.md +920 -0
- package/skills/ui/frontend-design/SKILL.md +71 -0
- package/skills/ui/impeccable/SKILL.md +93 -0
- package/skills/ui/interface-design/SKILL.md +320 -0
- package/skills/ui/sources.json +66 -0
- package/skills/ui/ui-design/SKILL.md +35 -0
- package/skills/ui/ui-skills-root/SKILL.md +59 -0
- package/skills/ui/visual-design-foundations/SKILL.md +318 -0
- package/skills/ui/web-design-guidelines/SKILL.md +39 -0
- package/public/assets/index-Be82iGwe.css +0 -1
- package/public/assets/index-ByU9a3Lh.js +0 -61
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official tool-permission answerer for the desk UI.
|
|
3
|
+
*
|
|
4
|
+
* `approval/request` is a distinct waterfall from user-questions. This
|
|
5
|
+
* listener is prepended so typtert `$events` cannot intercept first. The
|
|
6
|
+
* workbench claims every request it can show; anything it cannot identify
|
|
7
|
+
* fails closed as `unavailable`. There is no always-allow on this card.
|
|
8
|
+
*/
|
|
9
|
+
import { toolError } from "./errors.js";
|
|
10
|
+
const answerers = new Map();
|
|
11
|
+
export function publishApprovalAnswerer(dataDir, answerer) {
|
|
12
|
+
if (answerer)
|
|
13
|
+
answerers.set(dataDir, answerer);
|
|
14
|
+
else
|
|
15
|
+
answerers.delete(dataDir);
|
|
16
|
+
}
|
|
17
|
+
export function approvalAnswererFor(dataDir) {
|
|
18
|
+
return answerers.get(dataDir) ?? null;
|
|
19
|
+
}
|
|
20
|
+
export function approvalSessionIdOf(agent) {
|
|
21
|
+
if (!agent || typeof agent !== 'object')
|
|
22
|
+
return '';
|
|
23
|
+
if (typeof agent.session === 'string' && agent.session !== '')
|
|
24
|
+
return agent.session;
|
|
25
|
+
if (typeof agent.id === 'string' && agent.id !== '')
|
|
26
|
+
return agent.id;
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
function textOf(value) {
|
|
30
|
+
return typeof value === 'string' ? value : '';
|
|
31
|
+
}
|
|
32
|
+
function displayReasonOf(value) {
|
|
33
|
+
if (!value || typeof value !== 'object')
|
|
34
|
+
return undefined;
|
|
35
|
+
const en = textOf(value.en);
|
|
36
|
+
if (en === '')
|
|
37
|
+
return undefined;
|
|
38
|
+
const display = { en };
|
|
39
|
+
for (const [locale, message] of Object.entries(value)) {
|
|
40
|
+
if (typeof message === 'string' && message !== '')
|
|
41
|
+
display[locale] = message;
|
|
42
|
+
}
|
|
43
|
+
return display;
|
|
44
|
+
}
|
|
45
|
+
export function createApprovalAnswerer() {
|
|
46
|
+
const pending = new Set();
|
|
47
|
+
const settled = [];
|
|
48
|
+
let nextId = 1;
|
|
49
|
+
function detach(entry) {
|
|
50
|
+
if (!pending.has(entry))
|
|
51
|
+
return false;
|
|
52
|
+
pending.delete(entry);
|
|
53
|
+
if (entry.onAbort && entry.request.signal) {
|
|
54
|
+
entry.request.signal.removeEventListener('abort', entry.onAbort);
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
function remember(entry, outcome) {
|
|
59
|
+
settled.unshift({
|
|
60
|
+
requestId: entry.requestId,
|
|
61
|
+
sessionId: entry.sessionId,
|
|
62
|
+
toolName: entry.toolName,
|
|
63
|
+
callId: entry.callId,
|
|
64
|
+
reason: textOf(entry.request.reason),
|
|
65
|
+
displayReason: displayReasonOf(entry.request.displayReason),
|
|
66
|
+
state: outcome,
|
|
67
|
+
});
|
|
68
|
+
if (settled.length > 50)
|
|
69
|
+
settled.length = 50;
|
|
70
|
+
}
|
|
71
|
+
async function handle(request, _next) {
|
|
72
|
+
const sessionId = approvalSessionIdOf(request.agent);
|
|
73
|
+
const toolName = textOf(request.toolName);
|
|
74
|
+
if (sessionId === '' || toolName === '')
|
|
75
|
+
return 'unavailable';
|
|
76
|
+
return new Promise(resolve => {
|
|
77
|
+
const callId = textOf(request.callId);
|
|
78
|
+
const entry = {
|
|
79
|
+
requestId: `approval-${nextId++}`,
|
|
80
|
+
request,
|
|
81
|
+
sessionId,
|
|
82
|
+
toolName,
|
|
83
|
+
callId: callId === '' ? undefined : callId,
|
|
84
|
+
resolve,
|
|
85
|
+
};
|
|
86
|
+
const onAbort = () => {
|
|
87
|
+
if (!detach(entry))
|
|
88
|
+
return;
|
|
89
|
+
remember(entry, 'cancelled');
|
|
90
|
+
resolve('cancelled');
|
|
91
|
+
};
|
|
92
|
+
entry.onAbort = onAbort;
|
|
93
|
+
pending.add(entry);
|
|
94
|
+
if (request.signal?.aborted) {
|
|
95
|
+
onAbort();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
request.signal?.addEventListener('abort', onAbort, { once: true });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function submit(input) {
|
|
102
|
+
if (typeof input.sessionId !== 'string' || input.sessionId === '') {
|
|
103
|
+
return toolError('VALIDATION', 'sessionId is required.');
|
|
104
|
+
}
|
|
105
|
+
if (typeof input.requestId !== 'string' || input.requestId === '') {
|
|
106
|
+
return toolError('VALIDATION', 'requestId is required.');
|
|
107
|
+
}
|
|
108
|
+
if (input.outcome !== 'allowed-once' && input.outcome !== 'rejected') {
|
|
109
|
+
return toolError('VALIDATION', 'Tool approval only accepts allow-once or reject.');
|
|
110
|
+
}
|
|
111
|
+
const matches = [...pending].filter(entry => entry.sessionId === input.sessionId && entry.requestId === input.requestId);
|
|
112
|
+
if (matches.length > 1)
|
|
113
|
+
return toolError('CONFLICT', 'Multiple open approvals match this request.');
|
|
114
|
+
const entry = matches[0];
|
|
115
|
+
if (!entry)
|
|
116
|
+
return toolError('NOT_FOUND', 'No open tool-approval request matches this session.');
|
|
117
|
+
if (!detach(entry))
|
|
118
|
+
return toolError('CONFLICT', 'This approval was already answered.');
|
|
119
|
+
remember(entry, input.outcome);
|
|
120
|
+
entry.resolve(input.outcome);
|
|
121
|
+
return { ok: true, path: 'approval', outcome: input.outcome, requestId: entry.requestId };
|
|
122
|
+
}
|
|
123
|
+
function snapshot(sessionId) {
|
|
124
|
+
const open = [...pending]
|
|
125
|
+
.filter(entry => !sessionId || entry.sessionId === sessionId)
|
|
126
|
+
.map(entry => ({
|
|
127
|
+
requestId: entry.requestId,
|
|
128
|
+
sessionId: entry.sessionId,
|
|
129
|
+
toolName: entry.toolName,
|
|
130
|
+
callId: entry.callId,
|
|
131
|
+
reason: textOf(entry.request.reason),
|
|
132
|
+
displayReason: displayReasonOf(entry.request.displayReason),
|
|
133
|
+
state: 'open',
|
|
134
|
+
}));
|
|
135
|
+
const done = settled.filter(entry => !sessionId || entry.sessionId === sessionId);
|
|
136
|
+
return [...open, ...done];
|
|
137
|
+
}
|
|
138
|
+
function listen(ctx) {
|
|
139
|
+
const dispose = ctx.on?.('approval/request', handle, true);
|
|
140
|
+
return () => {
|
|
141
|
+
if (typeof dispose === 'function')
|
|
142
|
+
dispose();
|
|
143
|
+
for (const entry of [...pending]) {
|
|
144
|
+
if (!detach(entry))
|
|
145
|
+
continue;
|
|
146
|
+
remember(entry, 'unavailable');
|
|
147
|
+
entry.resolve('unavailable');
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
listen,
|
|
153
|
+
handle,
|
|
154
|
+
submit,
|
|
155
|
+
snapshot,
|
|
156
|
+
pendingCount() {
|
|
157
|
+
return pending.size;
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official `ctx.authorization` surface for the desk settings UI.
|
|
3
|
+
*
|
|
4
|
+
* Authorization is config-time, never part of the transcript or model
|
|
5
|
+
* context. The desk supplies the official interaction: notices, prompts,
|
|
6
|
+
* and cancel. Secret prompts are answered with the exact submitted value;
|
|
7
|
+
* the UI must not use a password input.
|
|
8
|
+
*/
|
|
9
|
+
import { toolError } from "./errors.js";
|
|
10
|
+
class DeclinedError extends Error {
|
|
11
|
+
code = 'DECLINED';
|
|
12
|
+
constructor(message = 'The authorization prompt was declined.') {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'AuthorizationDeclinedError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const controllers = new Map();
|
|
18
|
+
export function publishAuthorization(dataDir, controller) {
|
|
19
|
+
if (controller)
|
|
20
|
+
controllers.set(dataDir, controller);
|
|
21
|
+
else
|
|
22
|
+
controllers.delete(dataDir);
|
|
23
|
+
}
|
|
24
|
+
export function authorizationFor(dataDir) {
|
|
25
|
+
return controllers.get(dataDir) ?? null;
|
|
26
|
+
}
|
|
27
|
+
export function createAuthorizationController() {
|
|
28
|
+
const prompts = new Map();
|
|
29
|
+
const notices = [];
|
|
30
|
+
let host = null;
|
|
31
|
+
let nextId = 1;
|
|
32
|
+
let catalogRevision = 0;
|
|
33
|
+
const active = new Map();
|
|
34
|
+
function service() {
|
|
35
|
+
return host?.get?.('authorization');
|
|
36
|
+
}
|
|
37
|
+
function declined(message) {
|
|
38
|
+
const ctor = service()?.constructor?.AuthorizationDeclinedError;
|
|
39
|
+
if (typeof ctor === 'function') {
|
|
40
|
+
try {
|
|
41
|
+
return new ctor(message);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
/* fall through */
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const AuthorizationDeclinedError = service()?.AuthorizationDeclinedError;
|
|
48
|
+
if (typeof AuthorizationDeclinedError === 'function') {
|
|
49
|
+
try {
|
|
50
|
+
return new AuthorizationDeclinedError(message);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
/* fall through */
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return new DeclinedError(message);
|
|
57
|
+
}
|
|
58
|
+
function list() {
|
|
59
|
+
const entries = service()?.list?.();
|
|
60
|
+
if (!Array.isArray(entries))
|
|
61
|
+
return [];
|
|
62
|
+
return entries.flatMap(entry => {
|
|
63
|
+
if (!entry || typeof entry !== 'object')
|
|
64
|
+
return [];
|
|
65
|
+
const row = entry;
|
|
66
|
+
const methods = Array.isArray(row.methods)
|
|
67
|
+
? row.methods.flatMap(method => {
|
|
68
|
+
if (!method || typeof method !== 'object')
|
|
69
|
+
return [];
|
|
70
|
+
const item = method;
|
|
71
|
+
if (typeof item.id !== 'string' || item.id === '')
|
|
72
|
+
return [];
|
|
73
|
+
return [
|
|
74
|
+
{
|
|
75
|
+
id: item.id,
|
|
76
|
+
label: typeof item.label === 'string' && item.label !== '' ? item.label : item.id,
|
|
77
|
+
},
|
|
78
|
+
];
|
|
79
|
+
})
|
|
80
|
+
: [];
|
|
81
|
+
return [
|
|
82
|
+
{
|
|
83
|
+
key: row.key,
|
|
84
|
+
label: typeof row.label === 'string' ? row.label : '',
|
|
85
|
+
methods,
|
|
86
|
+
inFlight: row.inFlight === true,
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function snapshot(options = {}) {
|
|
92
|
+
return {
|
|
93
|
+
available: Boolean(service()),
|
|
94
|
+
...(options.catalog === false ? { catalogRevision, inFlight: [...active.values()] } : { entries: list() }),
|
|
95
|
+
notices: [...notices],
|
|
96
|
+
prompts: [...prompts.values()].map(entry => entry.prompt),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function begin(input) {
|
|
100
|
+
const auth = service();
|
|
101
|
+
if (!auth || typeof auth.begin !== 'function') {
|
|
102
|
+
return toolError('UNAVAILABLE', 'Official authorization is not composed on this host.');
|
|
103
|
+
}
|
|
104
|
+
if (input.key == null)
|
|
105
|
+
return toolError('VALIDATION', 'A credential key is required.');
|
|
106
|
+
const activeKey = Symbol();
|
|
107
|
+
active.set(activeKey, input.key);
|
|
108
|
+
catalogRevision++;
|
|
109
|
+
try {
|
|
110
|
+
const outcome = await auth.begin({
|
|
111
|
+
key: input.key,
|
|
112
|
+
method: typeof input.method === 'string' && input.method !== '' ? input.method : undefined,
|
|
113
|
+
interaction: {
|
|
114
|
+
notify(notice) {
|
|
115
|
+
const view = {
|
|
116
|
+
key: input.key,
|
|
117
|
+
message: typeof notice?.message === 'string' ? notice.message : '',
|
|
118
|
+
};
|
|
119
|
+
if (typeof notice?.url === 'string' && notice.url !== '')
|
|
120
|
+
view.url = notice.url;
|
|
121
|
+
if (typeof notice?.code === 'string' && notice.code !== '')
|
|
122
|
+
view.code = notice.code;
|
|
123
|
+
notices.push(view);
|
|
124
|
+
if (notices.length > 20)
|
|
125
|
+
notices.shift();
|
|
126
|
+
},
|
|
127
|
+
prompt(prompt) {
|
|
128
|
+
return new Promise((resolve, reject) => {
|
|
129
|
+
const kind = prompt?.kind === 'secret' || prompt?.kind === 'select' ? prompt.kind : 'text';
|
|
130
|
+
const view = {
|
|
131
|
+
promptId: `auth-${nextId++}`,
|
|
132
|
+
key: input.key,
|
|
133
|
+
kind,
|
|
134
|
+
message: typeof prompt?.message === 'string' ? prompt.message : '',
|
|
135
|
+
};
|
|
136
|
+
if (typeof prompt?.placeholder === 'string' && prompt.placeholder !== '') {
|
|
137
|
+
view.placeholder = prompt.placeholder;
|
|
138
|
+
}
|
|
139
|
+
if (kind === 'select' && Array.isArray(prompt?.options)) {
|
|
140
|
+
view.options = prompt.options.flatMap(option => {
|
|
141
|
+
if (!option || typeof option !== 'object')
|
|
142
|
+
return [];
|
|
143
|
+
const row = option;
|
|
144
|
+
if (typeof row.id !== 'string' || row.id === '')
|
|
145
|
+
return [];
|
|
146
|
+
const item = {
|
|
147
|
+
id: row.id,
|
|
148
|
+
label: typeof row.label === 'string' && row.label !== '' ? row.label : row.id,
|
|
149
|
+
};
|
|
150
|
+
if (typeof row.description === 'string' && row.description !== '')
|
|
151
|
+
item.description = row.description;
|
|
152
|
+
return [item];
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
prompts.set(view.promptId, { promptId: view.promptId, key: input.key, prompt: view, resolve, reject });
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
return { ok: true, outcome };
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
const code = error && typeof error === 'object' && 'code' in error && typeof error.code === 'string'
|
|
164
|
+
? error.code
|
|
165
|
+
: 'UNAVAILABLE';
|
|
166
|
+
const message = error instanceof Error ? error.message : 'Authorization could not be started.';
|
|
167
|
+
if (code === 'DECLINED')
|
|
168
|
+
return { ok: true, outcome: { status: 'cancelled' } };
|
|
169
|
+
return toolError(code === 'NO_FLOW' || code === 'UNKNOWN_METHOD' || code === 'ALREADY_IN_FLIGHT' ? code : 'UNAVAILABLE', message);
|
|
170
|
+
}
|
|
171
|
+
finally {
|
|
172
|
+
active.delete(activeKey);
|
|
173
|
+
catalogRevision++;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function answer(input) {
|
|
177
|
+
if (typeof input.promptId !== 'string' || input.promptId === '') {
|
|
178
|
+
return toolError('VALIDATION', 'promptId is required.');
|
|
179
|
+
}
|
|
180
|
+
const entry = prompts.get(input.promptId);
|
|
181
|
+
if (!entry)
|
|
182
|
+
return toolError('NOT_FOUND', 'No open authorization prompt matches this request.');
|
|
183
|
+
prompts.delete(input.promptId);
|
|
184
|
+
if (input.declined) {
|
|
185
|
+
entry.reject(declined());
|
|
186
|
+
return { ok: true, path: 'declined' };
|
|
187
|
+
}
|
|
188
|
+
if (typeof input.value !== 'string')
|
|
189
|
+
return toolError('VALIDATION', 'A prompt answer is required.');
|
|
190
|
+
entry.resolve(input.value);
|
|
191
|
+
return { ok: true, path: 'answered' };
|
|
192
|
+
}
|
|
193
|
+
function cancel(input) {
|
|
194
|
+
const auth = service();
|
|
195
|
+
if (!auth || typeof auth.cancel !== 'function') {
|
|
196
|
+
return toolError('UNAVAILABLE', 'Official authorization is not composed on this host.');
|
|
197
|
+
}
|
|
198
|
+
if (input.key == null)
|
|
199
|
+
return toolError('VALIDATION', 'A credential key is required.');
|
|
200
|
+
for (const [id, entry] of [...prompts]) {
|
|
201
|
+
if (entry.key === input.key || JSON.stringify(entry.key) === JSON.stringify(input.key)) {
|
|
202
|
+
prompts.delete(id);
|
|
203
|
+
entry.reject(declined());
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
catalogRevision++;
|
|
207
|
+
auth.cancel(input.key);
|
|
208
|
+
return { ok: true, path: 'cancelled' };
|
|
209
|
+
}
|
|
210
|
+
function bind(ctx) {
|
|
211
|
+
host = ctx;
|
|
212
|
+
catalogRevision++;
|
|
213
|
+
return () => {
|
|
214
|
+
if (host === ctx)
|
|
215
|
+
host = null;
|
|
216
|
+
catalogRevision++;
|
|
217
|
+
active.clear();
|
|
218
|
+
for (const entry of [...prompts.values()]) {
|
|
219
|
+
prompts.delete(entry.promptId);
|
|
220
|
+
entry.reject(declined());
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
return { bind, list, snapshot, begin, answer, cancel };
|
|
225
|
+
}
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { asStoreFailure, toolError } from "./errors.js";
|
|
2
|
+
import { CANVAS_MAX_JSON_BYTES, normalizeCanvasPath } from "./canvas-store.js";
|
|
3
|
+
const API = '/session-foundations/api/canvas';
|
|
4
|
+
const MEDIA = '/dsh-bot/canvas-media/';
|
|
5
|
+
const CANVAS_ID = /^cnv_[A-Za-z0-9_-]{16}$/;
|
|
6
|
+
/**
|
|
7
|
+
* Manual and test publish routes. They require the existing login gate.
|
|
8
|
+
* Model tools do not call them. Media routes are dispatched before that gate
|
|
9
|
+
* and accept only the capability token.
|
|
10
|
+
*/
|
|
11
|
+
export async function handleCanvasApi(req, path, url, canvas) {
|
|
12
|
+
try {
|
|
13
|
+
const method = req.method ?? 'GET';
|
|
14
|
+
if (method === 'POST' && path === API) {
|
|
15
|
+
const body = await readBody(req);
|
|
16
|
+
return outcome(canvas.create({ sessionId: body.sessionId, title: body.title, ticketId: body.ticketId, createdBy: 'user' }));
|
|
17
|
+
}
|
|
18
|
+
if (method === 'GET' && path === API)
|
|
19
|
+
return outcome(canvas.list(url.searchParams.get('sessionId') ?? ''));
|
|
20
|
+
const parts = path.slice(API.length + 1).split('/');
|
|
21
|
+
const id = parts[0] ?? '';
|
|
22
|
+
if (!CANVAS_ID.test(id))
|
|
23
|
+
return { status: 404, body: toolError('NOT_FOUND', 'No such canvas.') };
|
|
24
|
+
if (method === 'GET' && parts.length === 1)
|
|
25
|
+
return outcome(canvas.get(id));
|
|
26
|
+
if (method === 'GET' && parts[1] === 'revisions' && parts.length === 3) {
|
|
27
|
+
const revision = Number(parts[2]);
|
|
28
|
+
if (!/^[1-9]\d{0,8}$/.test(parts[2] ?? ''))
|
|
29
|
+
return { status: 404, body: toolError('NOT_FOUND', 'No such revision.') };
|
|
30
|
+
return outcome(canvas.manifest(id, revision));
|
|
31
|
+
}
|
|
32
|
+
if (method !== 'POST' || parts.length !== 2)
|
|
33
|
+
return { status: 404, body: toolError('NOT_FOUND', 'No such canvas route.') };
|
|
34
|
+
const body = await readBody(req);
|
|
35
|
+
if (parts[1] === 'snapshot' || parts[1] === 'delta') {
|
|
36
|
+
const files = decodeFiles(body.files);
|
|
37
|
+
if (!Array.isArray(files))
|
|
38
|
+
return { status: 400, body: files };
|
|
39
|
+
return outcome(await canvas.publish({
|
|
40
|
+
canvasId: id,
|
|
41
|
+
kind: parts[1],
|
|
42
|
+
baseRevision: typeof body.baseRevision === 'number' ? body.baseRevision : null,
|
|
43
|
+
entry: typeof body.entry === 'string' ? body.entry : '',
|
|
44
|
+
expectedFence: typeof body.expectedFence === 'number' ? body.expectedFence : -1,
|
|
45
|
+
files,
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
if (parts[1] === 'token') {
|
|
49
|
+
const revision = typeof body.revision === 'number' ? body.revision : null;
|
|
50
|
+
const minRevision = typeof body.minRevision === 'number' ? body.minRevision : revision;
|
|
51
|
+
const maxRevision = typeof body.maxRevision === 'number' ? body.maxRevision : revision;
|
|
52
|
+
if (minRevision == null || maxRevision == null)
|
|
53
|
+
return { status: 400, body: toolError('VALIDATION', 'revision is required.') };
|
|
54
|
+
return outcome(canvas.mint({ canvasId: id, minRevision, maxRevision }));
|
|
55
|
+
}
|
|
56
|
+
if (parts[1] === 'adopt')
|
|
57
|
+
return outcome(await canvas.adopt({ canvasId: id, revision: typeof body.revision === 'number' ? body.revision : 0 }));
|
|
58
|
+
if (parts[1] === 'revoke')
|
|
59
|
+
return outcome(canvas.revoke({ canvasId: id, tokenId: typeof body.tokenId === 'string' ? body.tokenId : '' }));
|
|
60
|
+
if (parts[1] === 'fail') {
|
|
61
|
+
return outcome(await canvas.fail({
|
|
62
|
+
canvasId: id,
|
|
63
|
+
expectedFence: typeof body.expectedFence === 'number' ? body.expectedFence : -1,
|
|
64
|
+
message: typeof body.message === 'string' ? body.message : '',
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
return { status: 404, body: toolError('NOT_FOUND', 'No such canvas route.') };
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
const code = error && typeof error === 'object' && 'code' in error ? String(error.code) : '';
|
|
71
|
+
if (code === 'VALIDATION')
|
|
72
|
+
return {
|
|
73
|
+
status: 400,
|
|
74
|
+
body: toolError('VALIDATION', error instanceof Error ? error.message : 'invalid canvas request'),
|
|
75
|
+
};
|
|
76
|
+
return { status: 500, body: asStoreFailure(error) };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Token-only media. A login cookie or Authorization header does not grant access.
|
|
81
|
+
* @param {string} requestUrl raw request target, before URL normalization
|
|
82
|
+
*/
|
|
83
|
+
export function serveCanvasMedia(req, res, requestUrl, canvas) {
|
|
84
|
+
if (req.method !== 'GET' && req.method !== 'HEAD') {
|
|
85
|
+
json(res, 405, toolError('VALIDATION', 'canvas media is read-only.'));
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const parsed = parseCanvasMedia(requestUrl);
|
|
89
|
+
if (!parsed) {
|
|
90
|
+
json(res, 400, toolError('VALIDATION', 'canvas media path is not allowed.'));
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (!canvas) {
|
|
94
|
+
json(res, 404, toolError('NOT_FOUND', 'No such canvas file.'));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const found = canvas.readMedia(parsed);
|
|
98
|
+
if (found.ok === false) {
|
|
99
|
+
json(res, found.code === 'NOT_FOUND' ? 404 : found.code === 'VALIDATION' ? 400 : 401, found);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const origin = requestOrigin(req);
|
|
103
|
+
if (origin === '') {
|
|
104
|
+
json(res, 400, toolError('VALIDATION', 'canvas media needs a host.'));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const headers = {
|
|
108
|
+
'content-type': found.type === 'text/html' || found.type === 'text/css' ? `${found.type}; charset=utf-8` : found.type,
|
|
109
|
+
'content-length': String(found.bytes.length),
|
|
110
|
+
'cache-control': 'private, no-store',
|
|
111
|
+
'x-content-type-options': 'nosniff',
|
|
112
|
+
'referrer-policy': 'no-referrer',
|
|
113
|
+
'content-security-policy': mediaPolicy(found.type, origin, parsed),
|
|
114
|
+
};
|
|
115
|
+
res.writeHead(200, headers);
|
|
116
|
+
res.end(req.method === 'HEAD' ? undefined : found.bytes);
|
|
117
|
+
}
|
|
118
|
+
/** Raw media target. Dot segments and any percent-encoding are rejected before resolution. */
|
|
119
|
+
export function parseCanvasMedia(requestUrl) {
|
|
120
|
+
const pathname = (requestUrl.split('?')[0] ?? '').replace(/\/+$/, '');
|
|
121
|
+
if (!pathname.startsWith(MEDIA))
|
|
122
|
+
return null;
|
|
123
|
+
if (pathname.includes('\\') || pathname.includes('%') || pathname.includes('\0'))
|
|
124
|
+
return null;
|
|
125
|
+
if (pathname.split('/').includes('..') || pathname.split('/').includes('.'))
|
|
126
|
+
return null;
|
|
127
|
+
const parts = pathname.slice(MEDIA.length).split('/');
|
|
128
|
+
if (parts.length < 3)
|
|
129
|
+
return null;
|
|
130
|
+
const [token, revision, ...rest] = parts;
|
|
131
|
+
if (!token || !/^[A-Za-z0-9_-]{20,128}$/.test(token))
|
|
132
|
+
return null;
|
|
133
|
+
if (!revision || !/^[1-9]\d{0,8}$/.test(revision))
|
|
134
|
+
return null;
|
|
135
|
+
const path = normalizeCanvasPath(rest.join('/'));
|
|
136
|
+
if (!path)
|
|
137
|
+
return null;
|
|
138
|
+
return { token, revision: Number(revision), path };
|
|
139
|
+
}
|
|
140
|
+
function mediaPolicy(type, origin, parsed) {
|
|
141
|
+
const sandbox = "sandbox; base-uri 'none'; form-action 'none'";
|
|
142
|
+
if (type !== 'text/html')
|
|
143
|
+
return `default-src 'none'; ${sandbox}`;
|
|
144
|
+
const prefix = `${origin}${MEDIA}${parsed.token}/${parsed.revision}/`;
|
|
145
|
+
// A design draft usually keeps CSS in a <style> block. External sheets still have to come from this prefix.
|
|
146
|
+
return [
|
|
147
|
+
"default-src 'none'",
|
|
148
|
+
`img-src ${prefix}`,
|
|
149
|
+
`style-src ${prefix} 'unsafe-inline'`,
|
|
150
|
+
`font-src ${prefix}`,
|
|
151
|
+
`frame-ancestors ${origin}`,
|
|
152
|
+
sandbox,
|
|
153
|
+
].join('; ');
|
|
154
|
+
}
|
|
155
|
+
function requestOrigin(req) {
|
|
156
|
+
const host = header(req, 'host');
|
|
157
|
+
if (!host || /[\s/\\]/.test(host))
|
|
158
|
+
return '';
|
|
159
|
+
const forwarded = header(req, 'x-forwarded-proto');
|
|
160
|
+
const proto = forwarded === 'https' || forwarded === 'http' ? forwarded : 'http';
|
|
161
|
+
return `${proto}://${host}`;
|
|
162
|
+
}
|
|
163
|
+
function header(req, name) {
|
|
164
|
+
const value = req.headers[name];
|
|
165
|
+
return Array.isArray(value) ? (value[0] ?? '') : (value ?? '');
|
|
166
|
+
}
|
|
167
|
+
function outcome(result) {
|
|
168
|
+
if (result.ok !== false)
|
|
169
|
+
return { status: 200, body: result };
|
|
170
|
+
const status = result.code === 'NOT_FOUND' ? 404 : result.code === 'CAS_CONFLICT' ? 409 : result.code === 'FORBIDDEN' ? 401 : 400;
|
|
171
|
+
return { status, body: result };
|
|
172
|
+
}
|
|
173
|
+
function decodeFiles(value) {
|
|
174
|
+
if (!Array.isArray(value))
|
|
175
|
+
return toolError('VALIDATION', 'files are required.');
|
|
176
|
+
const files = [];
|
|
177
|
+
for (const item of value) {
|
|
178
|
+
if (!item || typeof item !== 'object')
|
|
179
|
+
return toolError('VALIDATION', 'files are required.');
|
|
180
|
+
const record = item;
|
|
181
|
+
if (typeof record.path !== 'string' || typeof record.type !== 'string' || typeof record.body !== 'string')
|
|
182
|
+
return toolError('VALIDATION', 'each file needs path, type, and body.');
|
|
183
|
+
if (record.body.length > CANVAS_MAX_JSON_BYTES)
|
|
184
|
+
return toolError('VALIDATION', 'A canvas file is too large.');
|
|
185
|
+
const bytes = decodeBase64(record.body);
|
|
186
|
+
if (!bytes)
|
|
187
|
+
return toolError('VALIDATION', `body for ${record.path} is not base64.`);
|
|
188
|
+
files.push({ path: record.path, type: record.type, bytes });
|
|
189
|
+
}
|
|
190
|
+
return files;
|
|
191
|
+
}
|
|
192
|
+
function decodeBase64(value) {
|
|
193
|
+
if (value.length % 4 === 1 || /[^A-Za-z0-9+/=]/.test(value))
|
|
194
|
+
return null;
|
|
195
|
+
const bytes = Buffer.from(value, 'base64');
|
|
196
|
+
const canonical = bytes.toString('base64').replace(/=+$/u, '');
|
|
197
|
+
if (canonical !== value.replace(/=+$/u, ''))
|
|
198
|
+
return null;
|
|
199
|
+
return bytes;
|
|
200
|
+
}
|
|
201
|
+
function readBody(req) {
|
|
202
|
+
return new Promise((resolve, reject) => {
|
|
203
|
+
const chunks = [];
|
|
204
|
+
let total = 0;
|
|
205
|
+
let failed = false;
|
|
206
|
+
const fail = (error) => {
|
|
207
|
+
if (failed)
|
|
208
|
+
return;
|
|
209
|
+
failed = true;
|
|
210
|
+
reject(error);
|
|
211
|
+
};
|
|
212
|
+
req.on('data', (chunk) => {
|
|
213
|
+
total += chunk.length;
|
|
214
|
+
if (total > CANVAS_MAX_JSON_BYTES) {
|
|
215
|
+
fail(Object.assign(new Error('Canvas package is too large.'), { code: 'VALIDATION' }));
|
|
216
|
+
req.destroy();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
chunks.push(chunk);
|
|
220
|
+
});
|
|
221
|
+
req.on('end', () => {
|
|
222
|
+
if (failed)
|
|
223
|
+
return;
|
|
224
|
+
try {
|
|
225
|
+
const text = Buffer.concat(chunks).toString('utf8');
|
|
226
|
+
const parsed = text === '' ? {} : JSON.parse(text);
|
|
227
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
228
|
+
fail(Object.assign(new Error('canvas body must be an object.'), { code: 'VALIDATION' }));
|
|
229
|
+
else
|
|
230
|
+
resolve(parsed);
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
fail(Object.assign(new Error('invalid json'), { code: 'VALIDATION', cause: error }));
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
req.on('error', fail);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function json(res, status, body) {
|
|
240
|
+
const text = JSON.stringify(body);
|
|
241
|
+
res.writeHead(status, {
|
|
242
|
+
'content-type': 'application/json; charset=utf-8',
|
|
243
|
+
'content-length': Buffer.byteLength(text),
|
|
244
|
+
'cache-control': 'no-store',
|
|
245
|
+
'x-content-type-options': 'nosniff',
|
|
246
|
+
});
|
|
247
|
+
res.end(text);
|
|
248
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
const SKILL_ID = /^[a-z0-9][a-z0-9-]{0,64}$/;
|
|
5
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..', 'skills', 'ui');
|
|
6
|
+
/**
|
|
7
|
+
* Bundled canvas skills. `ui-design` is this plugin's contract.
|
|
8
|
+
* The other directories are a starter catalog from ui-skills.com.
|
|
9
|
+
*/
|
|
10
|
+
export function canvasSkillFiles() {
|
|
11
|
+
const entries = readdirSync(ROOT, { withFileTypes: true });
|
|
12
|
+
const skills = [];
|
|
13
|
+
for (const entry of entries) {
|
|
14
|
+
if (!entry.isDirectory() || !SKILL_ID.test(entry.name))
|
|
15
|
+
continue;
|
|
16
|
+
const bytes = readFileSync(join(ROOT, entry.name, 'SKILL.md'));
|
|
17
|
+
const summary = skillSummary(bytes.toString('utf8'), entry.name);
|
|
18
|
+
skills.push({
|
|
19
|
+
id: entry.name,
|
|
20
|
+
version: entry.name === 'ui-design' ? '1' : 'catalog',
|
|
21
|
+
summary,
|
|
22
|
+
bytes,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
skills.sort((left, right) => (left.id < right.id ? -1 : left.id > right.id ? 1 : 0));
|
|
26
|
+
return skills;
|
|
27
|
+
}
|
|
28
|
+
export function bindCanvasSkills(engine, sessionId) {
|
|
29
|
+
const bound = [];
|
|
30
|
+
const skipped = [];
|
|
31
|
+
for (const skill of canvasSkillFiles()) {
|
|
32
|
+
try {
|
|
33
|
+
engine.registerSkill(sessionId, 'public', skill.id, skill.version, skill.summary, skill.bytes);
|
|
34
|
+
bound.push(skill.id);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
skipped.push(skill.id);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { bound, skipped };
|
|
41
|
+
}
|
|
42
|
+
function skillSummary(text, fallback) {
|
|
43
|
+
const fence = text.match(/^---\r?\n([\s\S]*?)\r?\n---/);
|
|
44
|
+
const block = fence?.[1] ?? '';
|
|
45
|
+
const line = block.match(/^description:\s*(.*)$/m)?.[1]?.trim() ?? '';
|
|
46
|
+
const description = unquote(line).replace(/\s+/g, ' ').trim();
|
|
47
|
+
const summary = description || fallback;
|
|
48
|
+
return summary.slice(0, fallback === 'ui-design' ? 180 : 100);
|
|
49
|
+
}
|
|
50
|
+
function unquote(value) {
|
|
51
|
+
if ((value.startsWith('"') && value.endsWith('"') && value.length >= 2) ||
|
|
52
|
+
(value.startsWith("'") && value.endsWith("'") && value.length >= 2)) {
|
|
53
|
+
return value.slice(1, -1);
|
|
54
|
+
}
|
|
55
|
+
return value;
|
|
56
|
+
}
|