@jini-ai/agentic 0.4.3 → 0.4.5
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/CHANGELOG.md +4 -0
- package/dist/core/a2ui/json-pointer.js +94 -73
- package/dist/core/a2ui/resolve.js +45 -23
- package/dist/core/a2ui/tree.js +8 -5
- package/dist/core/capability.js +21 -12
- package/dist/core/dom/dom-page-driver.js +89 -74
- package/dist/core/gen-ui/encoder.js +31 -31
- package/dist/core/guards.js +13 -6
- package/dist/core/handle.d.ts +13 -0
- package/dist/core/handle.js +19 -0
- package/dist/core/index.d.ts +2 -1
- package/dist/core/index.js +2 -1
- package/dist/core/mcp-ui-apps.js +12 -6
- package/dist/core/page-executor.js +131 -111
- package/dist/core/site-guide/index.d.ts +4 -0
- package/dist/core/site-guide/index.js +4 -0
- package/dist/core/site-guide/site-guide.d.ts +18 -0
- package/dist/core/site-guide/site-guide.js +162 -0
- package/dist/core/site-guide/topic-schema.d.ts +16 -0
- package/dist/core/site-guide/topic-schema.js +68 -0
- package/dist/core/site-guide/types.d.ts +83 -0
- package/dist/core/site-guide/types.js +2 -0
- package/dist/skills/install/archive.js +42 -30
- package/dist/skills/install/github.js +102 -63
- package/dist/skills/install/index.d.ts +1 -0
- package/dist/skills/install/index.js +1 -0
- package/dist/skills/install/install-service.d.ts +20 -1
- package/dist/skills/install/install-service.js +23 -11
- package/dist/skills/install/layout.d.ts +8 -0
- package/dist/skills/install/layout.js +26 -1
- package/dist/skills/install/live-registration.js +1 -0
- package/dist/skills/install/node-filesystem.js +3 -1
- package/dist/skills/install/state.js +18 -7
- package/dist/skills/install/validation.d.ts +5 -1
- package/dist/skills/install/validation.js +85 -52
- package/dist/skills/install/yauzl-archive-reader.d.ts +29 -0
- package/dist/skills/install/yauzl-archive-reader.js +31 -0
- package/package.json +4 -4
- package/dist/a2ui/agent-to-renderer.d.ts +0 -613
- package/dist/a2ui/agent-to-renderer.js +0 -191
- package/dist/a2ui/catalog.d.ts +0 -73
- package/dist/a2ui/catalog.js +0 -304
- package/dist/a2ui/common-types.d.ts +0 -401
- package/dist/a2ui/common-types.js +0 -130
- package/dist/a2ui/index.d.ts +0 -16
- package/dist/a2ui/index.js +0 -16
- package/dist/a2ui/interpreter.d.ts +0 -71
- package/dist/a2ui/interpreter.js +0 -337
- package/dist/a2ui/json-pointer.d.ts +0 -48
- package/dist/a2ui/json-pointer.js +0 -174
- package/dist/a2ui/renderer-to-agent.d.ts +0 -342
- package/dist/a2ui/renderer-to-agent.js +0 -111
- package/dist/a2ui/resolve.d.ts +0 -28
- package/dist/a2ui/resolve.js +0 -144
- package/dist/a2ui/tree.d.ts +0 -66
- package/dist/a2ui/tree.js +0 -74
- package/dist/ag-ui.d.ts +0 -82
- package/dist/ag-ui.js +0 -85
- package/dist/agui/encoder.d.ts +0 -50
- package/dist/agui/encoder.js +0 -97
- package/dist/agui/events.d.ts +0 -59
- package/dist/agui/events.js +0 -15
- package/dist/capability.d.ts +0 -81
- package/dist/capability.js +0 -79
- package/dist/dom/dom-page-driver.d.ts +0 -36
- package/dist/dom/dom-page-driver.js +0 -601
- package/dist/dom/index.d.ts +0 -17
- package/dist/dom/index.js +0 -17
- package/dist/dom/model-context.d.ts +0 -69
- package/dist/dom/model-context.js +0 -44
- package/dist/element-handles.d.ts +0 -120
- package/dist/element-handles.js +0 -39
- package/dist/gen-ui/encoder.d.ts +0 -50
- package/dist/gen-ui/encoder.js +0 -97
- package/dist/gen-ui/events.d.ts +0 -59
- package/dist/gen-ui/events.js +0 -15
- package/dist/gen-ui/index.d.ts +0 -18
- package/dist/gen-ui/index.js +0 -17
- package/dist/guards.d.ts +0 -96
- package/dist/guards.js +0 -186
- package/dist/handle.d.ts +0 -41
- package/dist/handle.js +0 -41
- package/dist/mcp-ui-apps.d.ts +0 -223
- package/dist/mcp-ui-apps.js +0 -252
- package/dist/mcp-ui.d.ts +0 -133
- package/dist/mcp-ui.js +0 -134
- package/dist/page-capabilities.d.ts +0 -14
- package/dist/page-capabilities.js +0 -129
- package/dist/page-driver.d.ts +0 -120
- package/dist/page-driver.js +0 -2
- package/dist/page-executor.d.ts +0 -94
- package/dist/page-executor.js +0 -320
- package/dist/webmcp.d.ts +0 -202
- package/dist/webmcp.js +0 -173
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
const MINIMAL_TOPICS = [{
|
|
2
|
+
id: 'site', title: 'How this site works', summary: 'Learn the available building blocks before planning or building.',
|
|
3
|
+
explanation: 'Read the live facts, then consult the host topics and their tools. Missing or forbidden facts are unknown, not disabled capabilities.',
|
|
4
|
+
permissions: [], pointers: [], workflows: [],
|
|
5
|
+
}];
|
|
6
|
+
export class SiteGuideInputError extends Error {
|
|
7
|
+
constructor({ message }, _options = {}) { super(message); this.name = 'SiteGuideInputError'; }
|
|
8
|
+
}
|
|
9
|
+
/** Bound an injected read so an unavailable extension cannot hang learning. Always clear timers.
|
|
10
|
+
* @complexity O(1) beyond the supplied read; rejection text is kept internal to the shell.
|
|
11
|
+
*/
|
|
12
|
+
async function boundedRead(read, timeoutMs) {
|
|
13
|
+
let timer;
|
|
14
|
+
try {
|
|
15
|
+
return await Promise.race([Promise.resolve().then(read), new Promise((_resolve, reject) => {
|
|
16
|
+
timer = setTimeout(() => reject(new Error('site guide read timed out')), timeoutMs);
|
|
17
|
+
})]);
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
if (timer !== undefined)
|
|
21
|
+
clearTimeout(timer);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Compose by topic id. Core cards keep their owner; plugins add distinct, permission-gated topics.
|
|
25
|
+
* @complexity O(t) in bounded built-in and contributed topic counts, O(t) lookup space.
|
|
26
|
+
*/
|
|
27
|
+
function combineTopics(builtIns, contributions) {
|
|
28
|
+
const topics = new Map(builtIns.map(topic => [topic.id, { topic, source: 'core' }]));
|
|
29
|
+
const byId = new Map();
|
|
30
|
+
for (const contribution of contributions) {
|
|
31
|
+
for (const topic of contribution.topics) {
|
|
32
|
+
const candidates = byId.get(topic.id) ?? [];
|
|
33
|
+
candidates.push({ topic, pluginId: contribution.pluginId });
|
|
34
|
+
byId.set(topic.id, candidates);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
let conflicts = false;
|
|
38
|
+
for (const [id, candidates] of byId) {
|
|
39
|
+
// A contribution cannot replace a host's prose, pointers, workflows or security gates.
|
|
40
|
+
if (topics.has(id) || candidates.length !== 1) {
|
|
41
|
+
conflicts = true;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const candidate = candidates[0];
|
|
45
|
+
topics.set(id, { topic: candidate.topic, source: candidate.pluginId });
|
|
46
|
+
}
|
|
47
|
+
return { topics: [...topics.values()], conflicts };
|
|
48
|
+
}
|
|
49
|
+
/** Build a neutral read-only guide over host ports. No product state or plugin loader is ambient.
|
|
50
|
+
* Authorization and contributions are read fresh for each request. Errors from enrichment/facts
|
|
51
|
+
* yield explicit unavailable status without copying arbitrary error messages into model context.
|
|
52
|
+
* @param ports - Facts, authorization, optional enrichment, and host minimal capability cards.
|
|
53
|
+
* @returns One overview/explain service. Invalid input and hidden topics throw SiteGuideInputError.
|
|
54
|
+
* @complexity O(t + p) per request, topics plus distinct permissions; one bulk facts read.
|
|
55
|
+
* @example createSiteGuide({ facts, authorize, builtInTopics: cards }).run({ caller, operation: 'overview' });
|
|
56
|
+
*/
|
|
57
|
+
export function createSiteGuide(ports, options = {}) {
|
|
58
|
+
const timeoutMs = options.readTimeoutMs ?? 5_000;
|
|
59
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0)
|
|
60
|
+
throw new SiteGuideInputError({ message: 'readTimeoutMs must be a positive finite number' });
|
|
61
|
+
const builtIns = ports.builtInTopics ?? MINIMAL_TOPICS;
|
|
62
|
+
if (builtIns.length > 256 || new Set(builtIns.map(topic => topic.id)).size !== builtIns.length)
|
|
63
|
+
throw new SiteGuideInputError({ message: 'Built-in topics must have unique ids and contain at most 256 cards' });
|
|
64
|
+
return { async run(input, _options = {}) {
|
|
65
|
+
assertGuideInput(input);
|
|
66
|
+
const loaded = await loadContributions({ ports, caller: input.caller, timeoutMs });
|
|
67
|
+
const combined = combineTopics(builtIns, loaded.contributions);
|
|
68
|
+
const enrichment = combined.conflicts ? 'partial' : loaded.enrichment;
|
|
69
|
+
const gate = createPermissionGate({ ports, caller: input.caller, timeoutMs });
|
|
70
|
+
const views = await buildViews({ topics: combined.topics, input, permitted: gate.permitted });
|
|
71
|
+
if (input.operation === 'explain' && views.length === 0)
|
|
72
|
+
throw new SiteGuideInputError({ message: 'Topic unavailable. Call site_guide with operation overview for topics you may read.' });
|
|
73
|
+
// Overview is discovery only; read fresh domain facts when a visible topic is explained.
|
|
74
|
+
const facts = input.operation === 'explain' ? await readFacts({ ports, caller: input.caller, topicIds: views.map(view => view.id), timeoutMs }) : {};
|
|
75
|
+
return { operation: input.operation, topics: views, facts, callerPermissions: await gate.granted(), enrichment };
|
|
76
|
+
} };
|
|
77
|
+
}
|
|
78
|
+
/** Refuses unknown operations, a topic on overview, and explain without a well-formed topic id. */
|
|
79
|
+
function assertGuideInput(input) {
|
|
80
|
+
if (input.operation !== 'overview' && input.operation !== 'explain')
|
|
81
|
+
throw new SiteGuideInputError({ message: 'operation must be overview or explain' });
|
|
82
|
+
if (input.operation === 'overview' && input.topic !== undefined)
|
|
83
|
+
throw new SiteGuideInputError({ message: 'topic is only accepted with operation explain' });
|
|
84
|
+
if (input.operation === 'explain' && (typeof input.topic !== 'string' || !/^[a-z][a-z0-9-]{0,63}$/.test(input.topic))) {
|
|
85
|
+
throw new SiteGuideInputError({ message: 'explain requires a topic id from overview' });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Reads plugin contributions within the deadline; any failure or over-limit result degrades to none. */
|
|
89
|
+
async function loadContributions({ ports, caller, timeoutMs }) {
|
|
90
|
+
if (!ports.loadContributions)
|
|
91
|
+
return { contributions: [], enrichment: 'ok' };
|
|
92
|
+
try {
|
|
93
|
+
const loaded = await boundedRead(() => ports.loadContributions({ caller }), timeoutMs);
|
|
94
|
+
if (loaded.items.reduce((count, item) => count + item.topics.length, 0) > 256)
|
|
95
|
+
throw new SiteGuideInputError({ message: 'Too many contributed topics' });
|
|
96
|
+
return { contributions: loaded.items, enrichment: loaded.refusals.length ? 'partial' : 'ok' };
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
return { contributions: [], enrichment: 'unavailable' };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Per-request authorization cache: each permission is asked at most once, in first-use order. */
|
|
103
|
+
function createPermissionGate({ ports, caller, timeoutMs }) {
|
|
104
|
+
const decisions = new Map();
|
|
105
|
+
const allowed = (permission) => {
|
|
106
|
+
let decision = decisions.get(permission);
|
|
107
|
+
if (!decision) {
|
|
108
|
+
// Failed authorization is denial, never an excuse to expose a card or run its read.
|
|
109
|
+
decision = boundedRead(() => ports.authorize({ caller, permission }), timeoutMs).catch(() => false);
|
|
110
|
+
decisions.set(permission, decision);
|
|
111
|
+
}
|
|
112
|
+
return decision;
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
permitted: async (permissions) => (await Promise.all(permissions.map(allowed))).every(Boolean),
|
|
116
|
+
granted: async () => {
|
|
117
|
+
const callerPermissions = [];
|
|
118
|
+
for (const [permission, decision] of decisions)
|
|
119
|
+
if (await decision)
|
|
120
|
+
callerPermissions.push(permission);
|
|
121
|
+
return callerPermissions;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** Visible cards in topic order; explain narrows to the requested id and adds its permitted details. */
|
|
126
|
+
async function buildViews({ topics, input, permitted }) {
|
|
127
|
+
const views = [];
|
|
128
|
+
for (const entry of topics) {
|
|
129
|
+
if (input.operation === 'explain' && entry.topic.id !== input.topic)
|
|
130
|
+
continue;
|
|
131
|
+
if (!await permitted(entry.topic.permissions))
|
|
132
|
+
continue;
|
|
133
|
+
const view = { id: entry.topic.id, title: entry.topic.title, summary: entry.topic.summary, source: entry.source };
|
|
134
|
+
views.push(input.operation === 'explain' ? await explainView({ view, topic: entry.topic, permitted }) : view);
|
|
135
|
+
}
|
|
136
|
+
return views;
|
|
137
|
+
}
|
|
138
|
+
async function explainView({ view, topic, permitted }) {
|
|
139
|
+
const pointers = await withoutForbidden({ items: topic.pointers, permitted });
|
|
140
|
+
const workflows = await withoutForbidden({ items: topic.workflows, permitted });
|
|
141
|
+
return { ...view, explanation: topic.explanation, pointers, workflows };
|
|
142
|
+
}
|
|
143
|
+
/** Keeps items whose permissions are all granted, asked one item at a time, minus their gates. */
|
|
144
|
+
async function withoutForbidden({ items, permitted }) {
|
|
145
|
+
const kept = [];
|
|
146
|
+
for (const { permissions, ...item } of items)
|
|
147
|
+
if (await permitted(permissions))
|
|
148
|
+
kept.push(item);
|
|
149
|
+
return kept;
|
|
150
|
+
}
|
|
151
|
+
/** One bulk facts read for the visible topics; failure marks each of them unavailable. */
|
|
152
|
+
async function readFacts({ ports, caller, topicIds, timeoutMs }) {
|
|
153
|
+
try {
|
|
154
|
+
const read = await boundedRead(() => ports.facts.read({ caller, topics: topicIds }), timeoutMs);
|
|
155
|
+
// A facts adapter returning an unrelated section cannot disclose it through this tool.
|
|
156
|
+
return Object.fromEntries(topicIds.map(id => [id, read[id] ?? { status: 'unavailable' }]));
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return Object.fromEntries(topicIds.map(id => [id, { status: 'unavailable' }]));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=site-guide.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GuideTopic } from './types.js';
|
|
2
|
+
/** Parse bounded data-only enrichment. Invalid fields and duplicate ids refuse the whole file.
|
|
3
|
+
* @returns Validated topics or a fixed, data-free diagnostic; never quotes plugin bytes.
|
|
4
|
+
* @complexity O(n) characters, capped at 256 KiB of UTF-8, plus bounded shape traversal.
|
|
5
|
+
* @example parseGuideTopics({ raw: '{"version":1,"topics":[]}' });
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseGuideTopics({ raw }: {
|
|
8
|
+
readonly raw: string;
|
|
9
|
+
}, _options?: Record<string, never>): {
|
|
10
|
+
readonly ok: true;
|
|
11
|
+
readonly descriptors: readonly GuideTopic[];
|
|
12
|
+
} | {
|
|
13
|
+
readonly ok: false;
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=topic-schema.d.ts.map
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Native shape guards keep the core barrel usable without the optional A2UI/Zod peer. */
|
|
2
|
+
function record(value) {
|
|
3
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function text(value, maximum = 8_000) {
|
|
6
|
+
return typeof value === 'string' && value.length > 0 && value.length <= maximum;
|
|
7
|
+
}
|
|
8
|
+
function keys(value, permitted) {
|
|
9
|
+
return Object.keys(value).every(key => permitted.includes(key));
|
|
10
|
+
}
|
|
11
|
+
function list(value, maximum, guard) {
|
|
12
|
+
return Array.isArray(value) && value.length <= maximum && value.every(guard);
|
|
13
|
+
}
|
|
14
|
+
function permission(value) { return text(value, 128); }
|
|
15
|
+
function permissions(value) { return list(value, 32, permission); }
|
|
16
|
+
function step(value) { return text(value); }
|
|
17
|
+
/** Accept document/admin/HTTPS pointers, never script URLs or arbitrary local files. */
|
|
18
|
+
function href(value) {
|
|
19
|
+
return text(value, 512) && (value.startsWith('/admin/') || value.startsWith('development/') || value.startsWith('https://'));
|
|
20
|
+
}
|
|
21
|
+
function pointer(value) {
|
|
22
|
+
if (!record(value) || !keys(value, ['label', 'tool', 'href', 'permissions']))
|
|
23
|
+
return false;
|
|
24
|
+
if (!text(value.label) || !permissions(value.permissions))
|
|
25
|
+
return false;
|
|
26
|
+
if (value.tool !== undefined && !text(value.tool, 128))
|
|
27
|
+
return false;
|
|
28
|
+
if (value.href !== undefined && !href(value.href))
|
|
29
|
+
return false;
|
|
30
|
+
return value.tool !== undefined || value.href !== undefined;
|
|
31
|
+
}
|
|
32
|
+
function workflow(value) {
|
|
33
|
+
if (!record(value) || !keys(value, ['title', 'steps', 'permissions']))
|
|
34
|
+
return false;
|
|
35
|
+
return text(value.title) && permissions(value.permissions) && list(value.steps, 16, step) && value.steps.length > 0;
|
|
36
|
+
}
|
|
37
|
+
function topic(value) {
|
|
38
|
+
if (!record(value) || !keys(value, ['id', 'title', 'summary', 'explanation', 'permissions', 'pointers', 'workflows']))
|
|
39
|
+
return false;
|
|
40
|
+
if (!text(value.id, 64) || !/^[a-z][a-z0-9-]*$/.test(value.id))
|
|
41
|
+
return false;
|
|
42
|
+
return text(value.title, 128) && text(value.summary, 1_000) && text(value.explanation) && permissions(value.permissions)
|
|
43
|
+
&& list(value.pointers, 24, pointer) && list(value.workflows, 16, workflow);
|
|
44
|
+
}
|
|
45
|
+
/** Parse bounded data-only enrichment. Invalid fields and duplicate ids refuse the whole file.
|
|
46
|
+
* @returns Validated topics or a fixed, data-free diagnostic; never quotes plugin bytes.
|
|
47
|
+
* @complexity O(n) characters, capped at 256 KiB of UTF-8, plus bounded shape traversal.
|
|
48
|
+
* @example parseGuideTopics({ raw: '{"version":1,"topics":[]}' });
|
|
49
|
+
*/
|
|
50
|
+
export function parseGuideTopics({ raw }, _options = {}) {
|
|
51
|
+
if (raw.length > 262_144 || new TextEncoder().encode(raw).byteLength > 262_144)
|
|
52
|
+
return { ok: false, reason: 'site-guide manifest exceeds 256 KiB' };
|
|
53
|
+
let value;
|
|
54
|
+
try {
|
|
55
|
+
value = JSON.parse(raw);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return { ok: false, reason: 'site-guide manifest must be valid JSON' };
|
|
59
|
+
}
|
|
60
|
+
if (!record(value) || !keys(value, ['version', 'topics']) || value.version !== 1 || !list(value.topics, 64, topic)) {
|
|
61
|
+
return { ok: false, reason: 'site-guide manifest does not match version 1 topic schema' };
|
|
62
|
+
}
|
|
63
|
+
const ids = new Set(value.topics.map(item => item.id));
|
|
64
|
+
if (ids.size !== value.topics.length)
|
|
65
|
+
return { ok: false, reason: 'site-guide manifest contains duplicate topic ids' };
|
|
66
|
+
return { ok: true, descriptors: value.topics };
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=topic-schema.js.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** Read-only learning contracts. Hosts supply facts; plugins supply prose, never read effects. */
|
|
2
|
+
export interface GuideCaller {
|
|
3
|
+
readonly principalId: string;
|
|
4
|
+
}
|
|
5
|
+
export interface GuidePointer {
|
|
6
|
+
readonly label: string;
|
|
7
|
+
readonly tool?: string | undefined;
|
|
8
|
+
readonly href?: string | undefined;
|
|
9
|
+
readonly permissions: readonly string[];
|
|
10
|
+
}
|
|
11
|
+
export interface GuideWorkflow {
|
|
12
|
+
readonly title: string;
|
|
13
|
+
readonly steps: readonly string[];
|
|
14
|
+
readonly permissions: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
export interface GuideTopic {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly title: string;
|
|
19
|
+
readonly summary: string;
|
|
20
|
+
readonly explanation: string;
|
|
21
|
+
readonly permissions: readonly string[];
|
|
22
|
+
readonly pointers: readonly GuidePointer[];
|
|
23
|
+
readonly workflows: readonly GuideWorkflow[];
|
|
24
|
+
}
|
|
25
|
+
export interface GuideContribution {
|
|
26
|
+
readonly pluginId: string;
|
|
27
|
+
readonly topics: readonly GuideTopic[];
|
|
28
|
+
}
|
|
29
|
+
export interface GuideContributionResult {
|
|
30
|
+
readonly items: readonly GuideContribution[];
|
|
31
|
+
/** Host-local diagnostics; never expose arbitrary loader messages to the model. */
|
|
32
|
+
readonly refusals: readonly string[];
|
|
33
|
+
}
|
|
34
|
+
export type GuideFact = {
|
|
35
|
+
readonly status: 'ok';
|
|
36
|
+
readonly data: unknown;
|
|
37
|
+
readonly truncated?: boolean;
|
|
38
|
+
} | {
|
|
39
|
+
readonly status: 'forbidden' | 'unavailable';
|
|
40
|
+
};
|
|
41
|
+
/** Only a permission-scoped, explicitly projected DTO may leave the host adapter. */
|
|
42
|
+
export interface SiteFactsPort {
|
|
43
|
+
read(input: {
|
|
44
|
+
readonly caller: GuideCaller;
|
|
45
|
+
readonly topics: readonly string[];
|
|
46
|
+
}, options?: Record<string, never>): Promise<Readonly<Record<string, GuideFact>>>;
|
|
47
|
+
}
|
|
48
|
+
export interface SiteGuidePorts {
|
|
49
|
+
readonly facts: SiteFactsPort;
|
|
50
|
+
readonly authorize: (input: {
|
|
51
|
+
readonly caller: GuideCaller;
|
|
52
|
+
readonly permission: string;
|
|
53
|
+
}, options?: Record<string, never>) => Promise<boolean>;
|
|
54
|
+
readonly loadContributions?: (input: {
|
|
55
|
+
readonly caller: GuideCaller;
|
|
56
|
+
}, options?: Record<string, never>) => Promise<GuideContributionResult>;
|
|
57
|
+
readonly builtInTopics?: readonly GuideTopic[];
|
|
58
|
+
}
|
|
59
|
+
export interface GuideTopicView {
|
|
60
|
+
readonly id: string;
|
|
61
|
+
readonly title: string;
|
|
62
|
+
readonly summary: string;
|
|
63
|
+
readonly explanation?: string;
|
|
64
|
+
readonly pointers?: readonly Omit<GuidePointer, 'permissions'>[];
|
|
65
|
+
readonly workflows?: readonly Omit<GuideWorkflow, 'permissions'>[];
|
|
66
|
+
readonly source: string;
|
|
67
|
+
}
|
|
68
|
+
export interface GuideResult {
|
|
69
|
+
readonly operation: 'overview' | 'explain';
|
|
70
|
+
readonly topics: readonly GuideTopicView[];
|
|
71
|
+
readonly facts: Readonly<Record<string, GuideFact>>;
|
|
72
|
+
/** Granted permissions consulted for this result, rather than an inventory of the caller's roles. */
|
|
73
|
+
readonly callerPermissions: readonly string[];
|
|
74
|
+
readonly enrichment: 'ok' | 'unavailable' | 'partial';
|
|
75
|
+
}
|
|
76
|
+
export interface SiteGuide {
|
|
77
|
+
run(input: {
|
|
78
|
+
readonly caller: GuideCaller;
|
|
79
|
+
readonly operation: 'overview' | 'explain';
|
|
80
|
+
readonly topic?: string;
|
|
81
|
+
}, options?: Record<string, never>): Promise<GuideResult>;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -3,45 +3,57 @@ import { decodeSkillBase64, MAX_SKILL_BYTES, MAX_SKILL_FILES, MAX_SKILL_FILE_BYT
|
|
|
3
3
|
export async function readSkillArchive({ base64, archiveReader }, _optional = {}) {
|
|
4
4
|
try {
|
|
5
5
|
const reader = await archiveReader.open({ bytes: decodeSkillBase64({ value: base64 }) });
|
|
6
|
-
const files = [];
|
|
7
|
-
let total = 0;
|
|
8
|
-
let count = 0;
|
|
9
6
|
try {
|
|
10
|
-
|
|
11
|
-
if (++count > MAX_SKILL_FILES)
|
|
12
|
-
throw new SkillInputError({ message: "A skill archive may contain at most 256 entries." });
|
|
13
|
-
validateSkillPath({ filePath: entry.path.replace(/\/$/, "") });
|
|
14
|
-
if (entry.kind !== "file" && entry.kind !== "directory")
|
|
15
|
-
throw new SkillInputError({ message: "Skill archives may contain only regular files and directories." });
|
|
16
|
-
if (entry.kind === "directory")
|
|
17
|
-
continue;
|
|
18
|
-
if (!Number.isSafeInteger(entry.size) || entry.size < 0 || entry.size > MAX_SKILL_FILE_BYTES)
|
|
19
|
-
throw new SkillInputError({ message: "Skill archive file exceeds 1 MiB." });
|
|
20
|
-
const chunks = [];
|
|
21
|
-
let size = 0;
|
|
22
|
-
{
|
|
23
|
-
for await (const chunk of entry.read({})) {
|
|
24
|
-
size += chunk.length;
|
|
25
|
-
total += chunk.length;
|
|
26
|
-
if (size > MAX_SKILL_FILE_BYTES || total > MAX_SKILL_BYTES)
|
|
27
|
-
throw new SkillInputError({ message: "Skill archive exceeds its decompressed size limit." });
|
|
28
|
-
chunks.push(Buffer.from(chunk));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
if (size !== entry.size)
|
|
32
|
-
throw new SkillInputError({ message: "Skill archive file size does not match its metadata." });
|
|
33
|
-
files.push({ path: entry.path, contentBase64: Buffer.concat(chunks).toString("base64") });
|
|
34
|
-
}
|
|
35
|
-
return files;
|
|
7
|
+
return await collectArchiveFiles({ entries: reader.entries });
|
|
36
8
|
}
|
|
37
9
|
finally {
|
|
38
10
|
reader.close({});
|
|
39
11
|
}
|
|
40
12
|
}
|
|
41
13
|
catch (error) {
|
|
14
|
+
// Converts malformed archive parser failures into a client-actionable validation refusal.
|
|
42
15
|
if (error instanceof SkillInputError)
|
|
43
16
|
throw error;
|
|
44
|
-
throw new SkillInputError({ message: "Could not read skill ZIP. Use a valid ZIP with regular files only." });
|
|
17
|
+
throw new SkillInputError({ message: "Could not read skill ZIP. Use a valid ZIP with regular files only." }, { cause: error });
|
|
45
18
|
}
|
|
46
19
|
}
|
|
20
|
+
/** Walks every entry (directories count toward the entry limit) and reads the regular files.
|
|
21
|
+
* @complexity O(entries + decompressed bytes), bounded to 256 entries and 8 MiB.
|
|
22
|
+
*/
|
|
23
|
+
async function collectArchiveFiles({ entries }) {
|
|
24
|
+
const files = [];
|
|
25
|
+
const budget = { total: 0 };
|
|
26
|
+
let count = 0;
|
|
27
|
+
for await (const entry of entries) {
|
|
28
|
+
if (++count > MAX_SKILL_FILES)
|
|
29
|
+
throw new SkillInputError({ message: "A skill archive may contain at most 256 entries." });
|
|
30
|
+
if (isRegularFileEntry({ entry }))
|
|
31
|
+
files.push(await readArchiveFile({ entry, budget }));
|
|
32
|
+
}
|
|
33
|
+
return files;
|
|
34
|
+
}
|
|
35
|
+
/** Validates the entry path before its kind; refuses anything but files and directories. */
|
|
36
|
+
function isRegularFileEntry({ entry }) {
|
|
37
|
+
validateSkillPath({ filePath: entry.path.replace(/\/$/, "") });
|
|
38
|
+
if (entry.kind !== "file" && entry.kind !== "directory")
|
|
39
|
+
throw new SkillInputError({ message: "Skill archives may contain only regular files and directories." });
|
|
40
|
+
return entry.kind === "file";
|
|
41
|
+
}
|
|
42
|
+
/** Streams one file under its own 1 MiB cap and the archive-wide budget, then checks metadata. */
|
|
43
|
+
async function readArchiveFile({ entry, budget }) {
|
|
44
|
+
if (!Number.isSafeInteger(entry.size) || entry.size < 0 || entry.size > MAX_SKILL_FILE_BYTES)
|
|
45
|
+
throw new SkillInputError({ message: "Skill archive file exceeds 1 MiB." });
|
|
46
|
+
const chunks = [];
|
|
47
|
+
let size = 0;
|
|
48
|
+
for await (const chunk of entry.read({})) {
|
|
49
|
+
size += chunk.length;
|
|
50
|
+
budget.total += chunk.length;
|
|
51
|
+
if (size > MAX_SKILL_FILE_BYTES || budget.total > MAX_SKILL_BYTES)
|
|
52
|
+
throw new SkillInputError({ message: "Skill archive exceeds its decompressed size limit." });
|
|
53
|
+
chunks.push(Buffer.from(chunk));
|
|
54
|
+
}
|
|
55
|
+
if (size !== entry.size)
|
|
56
|
+
throw new SkillInputError({ message: "Skill archive file size does not match its metadata." });
|
|
57
|
+
return { path: entry.path, contentBase64: Buffer.concat(chunks).toString("base64") };
|
|
58
|
+
}
|
|
47
59
|
//# sourceMappingURL=archive.js.map
|
|
@@ -1,92 +1,131 @@
|
|
|
1
1
|
/** Commit-pinned public GitHub import with one request/body deadline, byte caps and refused redirects. */
|
|
2
2
|
import { MAX_SKILL_BYTES, MAX_SKILL_FILES, MAX_SKILL_FILE_BYTES, decodeSkillBase64, SkillInputError } from "./validation.js";
|
|
3
|
+
const URL_HELP = "Use an HTTPS GitHub repository URL, optionally ending in /tree/ref/skill-folder.";
|
|
4
|
+
const SHA_PATTERN = /^[a-f0-9]{40}$/;
|
|
5
|
+
const SEGMENT_PATTERN = /^[a-zA-Z0-9_.-]+$/;
|
|
6
|
+
const SKILL_FILE_PATTERN = /^(SKILL\.md|README\.md|LICENSE|(?:references|scripts|assets)\/.*)$/;
|
|
7
|
+
const REGULAR_FILE_MODES = ["100644", "100755"];
|
|
3
8
|
/** Restricts every request to api.github.com, disallows redirects, pins one immutable commit.
|
|
4
9
|
* @complexity O(files + downloaded bytes), at most 256 file requests and 8 MiB decoded content.
|
|
5
10
|
*/
|
|
6
11
|
export async function fetchGitHubSkill({ githubUrl, fetch: fetchImpl }, _optional = {}) {
|
|
12
|
+
const { owner, repo, ref, prefix } = parseGitHubSkillUrl({ githubUrl });
|
|
13
|
+
const base = `https://api.github.com/repos/${owner}/${repo}`;
|
|
14
|
+
// One deadline for the whole fetch, including streaming response bodies.
|
|
15
|
+
const json = createGitHubJsonReader({ fetch: fetchImpl, signal: AbortSignal.timeout(30_000) });
|
|
16
|
+
const commit = (await json(`${base}/commits/${encodeURIComponent(ref)}`)).sha;
|
|
17
|
+
if (typeof commit !== "string" || !SHA_PATTERN.test(commit))
|
|
18
|
+
throw new SkillInputError({ message: "GitHub returned an invalid commit." });
|
|
19
|
+
const tree = await json(`${base}/git/trees/${commit}?recursive=1`);
|
|
20
|
+
if (tree.truncated || !Array.isArray(tree.tree))
|
|
21
|
+
throw new SkillInputError({ message: "GitHub repository tree is incomplete. Choose a smaller skill repository." });
|
|
22
|
+
const files = await downloadSkillFiles({ rows: tree.tree, prefix, base, json });
|
|
23
|
+
return { files, source: { githubUrl, commit } };
|
|
24
|
+
}
|
|
25
|
+
/** Accepts only https://github.com/owner/repo[/tree/ref[/folder…]] with plain segments; refuses before any request. */
|
|
26
|
+
function parseGitHubSkillUrl({ githubUrl }) {
|
|
7
27
|
let url;
|
|
8
|
-
try {
|
|
9
|
-
url = new URL(githubUrl);
|
|
10
|
-
}
|
|
11
|
-
catch {
|
|
12
|
-
throw new SkillInputError({ message: "Use an HTTPS GitHub repository URL, optionally ending in /tree/ref/skill-folder." });
|
|
13
|
-
}
|
|
14
28
|
let parts;
|
|
15
29
|
try {
|
|
30
|
+
url = new URL(githubUrl);
|
|
16
31
|
parts = url.pathname.replace(/\/$/, "").split("/").slice(1).map(p => decodeURIComponent(p));
|
|
17
32
|
}
|
|
18
|
-
catch {
|
|
19
|
-
throw new SkillInputError({ message:
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw new SkillInputError({ message: URL_HELP }, { cause: error });
|
|
20
35
|
}
|
|
21
|
-
if (
|
|
22
|
-
throw new SkillInputError({ message:
|
|
23
|
-
const owner = parts[0];
|
|
36
|
+
if (!isGitHubOrigin({ url }) || !isSkillRepoPath({ parts }))
|
|
37
|
+
throw new SkillInputError({ message: URL_HELP });
|
|
24
38
|
const repo = parts[1].replace(/\.git$/, "");
|
|
25
39
|
if (!repo)
|
|
26
|
-
throw new SkillInputError({ message:
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
40
|
+
throw new SkillInputError({ message: URL_HELP });
|
|
41
|
+
return { owner: parts[0], repo, ref: parts[3] ?? "HEAD", prefix: parts.length > 4 ? parts.slice(4).join("/") + "/" : "" };
|
|
42
|
+
}
|
|
43
|
+
/** Plain https://github.com with no port, credentials, query or fragment. */
|
|
44
|
+
function isGitHubOrigin({ url }) {
|
|
45
|
+
return url.protocol === "https:" && url.hostname === "github.com" && !url.port && !url.username && !url.password && !url.search && !url.hash;
|
|
46
|
+
}
|
|
47
|
+
/** owner/repo, or owner/repo/tree/ref[/folder…]; every decoded segment is plain and never `.`/`..`. */
|
|
48
|
+
function isSkillRepoPath({ parts }) {
|
|
49
|
+
const shapeOk = parts.length === 2 || (parts.length >= 4 && parts[2] === "tree");
|
|
50
|
+
return shapeOk && parts.every(p => SEGMENT_PATTERN.test(p) && p !== "." && p !== "..");
|
|
51
|
+
}
|
|
52
|
+
/** One JSON endpoint reader sharing the redirect policy, accept header and deadline. */
|
|
53
|
+
function createGitHubJsonReader({ fetch: fetchImpl, signal }) {
|
|
54
|
+
return async (endpoint) => {
|
|
31
55
|
const response = await fetchImpl({ url: endpoint }, { redirect: "error", signal, headers: { Accept: "application/vnd.github+json" } });
|
|
32
56
|
if (!response.ok)
|
|
33
57
|
throw new SkillInputError({ message: `GitHub could not fetch this skill (HTTP ${response.status}). Use a public repository URL.` });
|
|
34
58
|
if (!response.body)
|
|
35
59
|
throw new SkillInputError({ message: "GitHub returned an empty response." });
|
|
36
|
-
const
|
|
37
|
-
let bytes = 0;
|
|
38
|
-
const chunks = [];
|
|
39
|
-
try {
|
|
40
|
-
for (;;) {
|
|
41
|
-
const part = await reader.read();
|
|
42
|
-
if (part.done)
|
|
43
|
-
break;
|
|
44
|
-
bytes += part.value.length;
|
|
45
|
-
if (bytes > MAX_SKILL_BYTES * 2)
|
|
46
|
-
throw new SkillInputError({ message: "GitHub skill response exceeds its size limit." });
|
|
47
|
-
chunks.push(part.value);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
finally {
|
|
51
|
-
await reader.cancel();
|
|
52
|
-
}
|
|
53
|
-
const result = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
60
|
+
const result = JSON.parse((await readBoundedBody({ body: response.body })).toString("utf8"));
|
|
54
61
|
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
55
62
|
throw new SkillInputError({ message: "GitHub returned an invalid skill response." });
|
|
56
63
|
return result;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** Streams a response body up to 16 MiB and always releases the reader. */
|
|
67
|
+
async function readBoundedBody({ body }) {
|
|
68
|
+
const reader = body.getReader();
|
|
69
|
+
let bytes = 0;
|
|
70
|
+
const chunks = [];
|
|
71
|
+
try {
|
|
72
|
+
for (;;) {
|
|
73
|
+
const part = await reader.read();
|
|
74
|
+
if (part.done)
|
|
75
|
+
break;
|
|
76
|
+
bytes += part.value.length;
|
|
77
|
+
if (bytes > MAX_SKILL_BYTES * 2)
|
|
78
|
+
throw new SkillInputError({ message: "GitHub skill response exceeds its size limit." });
|
|
79
|
+
chunks.push(part.value);
|
|
80
|
+
}
|
|
57
81
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
82
|
+
finally {
|
|
83
|
+
await reader.cancel();
|
|
84
|
+
}
|
|
85
|
+
return Buffer.concat(chunks);
|
|
86
|
+
}
|
|
87
|
+
/** Checks each selected row, then fetches its blob, in tree order, under declared and decoded budgets. */
|
|
88
|
+
async function downloadSkillFiles({ rows, prefix, base, json }) {
|
|
65
89
|
const files = [];
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (
|
|
90
|
+
const budget = { declared: 0, actual: 0 };
|
|
91
|
+
for (const row of rows) {
|
|
92
|
+
const relative = selectSkillRow({ row, prefix });
|
|
93
|
+
if (relative === undefined)
|
|
70
94
|
continue;
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (row.type !== "blob" || !["100644", "100755"].includes(String(row.mode)))
|
|
75
|
-
throw new SkillInputError({ message: "GitHub skills may contain only regular files." });
|
|
76
|
-
if (typeof row.size !== "number" || row.size < 0 || row.size > MAX_SKILL_FILE_BYTES || typeof row.sha !== "string" || !/^[a-f0-9]{40}$/.test(row.sha))
|
|
77
|
-
throw new SkillInputError({ message: "GitHub skill file exceeds its size limit or has invalid metadata." });
|
|
78
|
-
total += row.size;
|
|
79
|
-
if (total > MAX_SKILL_BYTES || files.length >= MAX_SKILL_FILES)
|
|
80
|
-
throw new SkillInputError({ message: "GitHub skill exceeds 8 MiB or 256 files." });
|
|
81
|
-
const blob = await json(`${base}/git/blobs/${row.sha}`);
|
|
82
|
-
if (blob.encoding !== "base64" || typeof blob.content !== "string")
|
|
83
|
-
throw new SkillInputError({ message: "GitHub returned an invalid skill file." });
|
|
84
|
-
const contentBase64 = blob.content.replace(/\s/g, "");
|
|
85
|
-
actualBytes += decodeSkillBase64({ value: contentBase64 }, { maxBytes: MAX_SKILL_FILE_BYTES }).length;
|
|
86
|
-
if (actualBytes > MAX_SKILL_BYTES)
|
|
95
|
+
const { size, sha } = readRegularBlobRow({ row });
|
|
96
|
+
budget.declared += size;
|
|
97
|
+
if (budget.declared > MAX_SKILL_BYTES || files.length >= MAX_SKILL_FILES)
|
|
87
98
|
throw new SkillInputError({ message: "GitHub skill exceeds 8 MiB or 256 files." });
|
|
88
|
-
files.push({ path: relative, contentBase64 });
|
|
99
|
+
files.push({ path: relative, contentBase64: await downloadBlob({ url: `${base}/git/blobs/${sha}`, json, budget }) });
|
|
89
100
|
}
|
|
90
|
-
return
|
|
101
|
+
return files;
|
|
102
|
+
}
|
|
103
|
+
/** Path relative to the skill folder when the row is an allowed skill file, else undefined (skipped). */
|
|
104
|
+
function selectSkillRow({ row, prefix }) {
|
|
105
|
+
if (typeof row.path !== "string" || !row.path.startsWith(prefix))
|
|
106
|
+
return undefined;
|
|
107
|
+
const relative = row.path.slice(prefix.length);
|
|
108
|
+
if (!SKILL_FILE_PATTERN.test(relative) || row.type === "tree")
|
|
109
|
+
return undefined;
|
|
110
|
+
return relative;
|
|
111
|
+
}
|
|
112
|
+
/** Refuses non-regular entries (submodules, symlinks) and missing or oversized metadata. */
|
|
113
|
+
function readRegularBlobRow({ row }) {
|
|
114
|
+
if (row.type !== "blob" || !REGULAR_FILE_MODES.includes(String(row.mode)))
|
|
115
|
+
throw new SkillInputError({ message: "GitHub skills may contain only regular files." });
|
|
116
|
+
if (typeof row.size !== "number" || row.size < 0 || row.size > MAX_SKILL_FILE_BYTES || typeof row.sha !== "string" || !SHA_PATTERN.test(row.sha))
|
|
117
|
+
throw new SkillInputError({ message: "GitHub skill file exceeds its size limit or has invalid metadata." });
|
|
118
|
+
return { size: row.size, sha: row.sha };
|
|
119
|
+
}
|
|
120
|
+
/** Fetches one base64 blob, enforcing the 1 MiB file cap and 8 MiB decoded total. */
|
|
121
|
+
async function downloadBlob({ url, json, budget }) {
|
|
122
|
+
const blob = await json(url);
|
|
123
|
+
if (blob.encoding !== "base64" || typeof blob.content !== "string")
|
|
124
|
+
throw new SkillInputError({ message: "GitHub returned an invalid skill file." });
|
|
125
|
+
const contentBase64 = blob.content.replace(/\s/g, "");
|
|
126
|
+
budget.actual += decodeSkillBase64({ value: contentBase64 }, { maxBytes: MAX_SKILL_FILE_BYTES }).length;
|
|
127
|
+
if (budget.actual > MAX_SKILL_BYTES)
|
|
128
|
+
throw new SkillInputError({ message: "GitHub skill exceeds 8 MiB or 256 files." });
|
|
129
|
+
return contentBase64;
|
|
91
130
|
}
|
|
92
131
|
//# sourceMappingURL=github.js.map
|