@inupedia/spotlight-client 0.3.5 → 0.4.0
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 +14 -123
- package/dist/clientTool.d.ts +42 -0
- package/dist/clientTool.d.ts.map +1 -0
- package/dist/clientTool.js +97 -0
- package/dist/index.d.ts +2 -19
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -18
- package/dist/node/capabilities/skillFileValidator.d.ts +0 -8
- package/dist/node/capabilities/skillFileValidator.d.ts.map +1 -1
- package/dist/node/capabilities/skillFileValidator.js +26 -46
- package/dist/node/index.d.ts +0 -10
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +0 -9
- package/dist/vite/index.d.ts +1 -7
- package/dist/vite/index.d.ts.map +1 -1
- package/dist/vite/index.js +1 -4
- package/dist/vite/spotlightClientTools.d.ts +27 -0
- package/dist/vite/spotlightClientTools.d.ts.map +1 -0
- package/dist/vite/spotlightClientTools.js +424 -0
- package/package.json +6 -20
- package/dist/capabilities/channel.d.ts +0 -28
- package/dist/capabilities/channel.d.ts.map +0 -1
- package/dist/capabilities/channel.js +0 -194
- package/dist/capabilities/errors.d.ts +0 -9
- package/dist/capabilities/errors.d.ts.map +0 -1
- package/dist/capabilities/errors.js +0 -19
- package/dist/capabilities/handshake.d.ts +0 -38
- package/dist/capabilities/handshake.d.ts.map +0 -1
- package/dist/capabilities/handshake.js +0 -139
- package/dist/capabilities/hostActionExecutor.d.ts +0 -22
- package/dist/capabilities/hostActionExecutor.d.ts.map +0 -1
- package/dist/capabilities/hostActionExecutor.js +0 -56
- package/dist/capabilities/jsonSchema.d.ts +0 -2
- package/dist/capabilities/jsonSchema.d.ts.map +0 -1
- package/dist/capabilities/jsonSchema.js +0 -73
- package/dist/capabilities/runtimeCapabilities.d.ts +0 -25
- package/dist/capabilities/runtimeCapabilities.d.ts.map +0 -1
- package/dist/capabilities/runtimeCapabilities.js +0 -53
- package/dist/node/capabilities/archivePath.d.ts +0 -4
- package/dist/node/capabilities/archivePath.d.ts.map +0 -1
- package/dist/node/capabilities/archivePath.js +0 -22
- package/dist/node/capabilities/canonicalJson.d.ts +0 -7
- package/dist/node/capabilities/canonicalJson.d.ts.map +0 -1
- package/dist/node/capabilities/canonicalJson.js +0 -122
- package/dist/node/capabilities/capabilityArtifact.d.ts +0 -15
- package/dist/node/capabilities/capabilityArtifact.d.ts.map +0 -1
- package/dist/node/capabilities/capabilityArtifact.js +0 -37
- package/dist/node/capabilities/capabilityArtifactError.d.ts +0 -7
- package/dist/node/capabilities/capabilityArtifactError.d.ts.map +0 -1
- package/dist/node/capabilities/capabilityArtifactError.js +0 -19
- package/dist/node/capabilities/capabilityArtifactTypes.d.ts +0 -32
- package/dist/node/capabilities/capabilityArtifactTypes.d.ts.map +0 -1
- package/dist/node/capabilities/capabilityArtifactTypes.js +0 -1
- package/dist/node/capabilities/capabilityFileMap.d.ts +0 -19
- package/dist/node/capabilities/capabilityFileMap.d.ts.map +0 -1
- package/dist/node/capabilities/capabilityFileMap.js +0 -234
- package/dist/node/capabilities/deterministicGzip.d.ts +0 -2
- package/dist/node/capabilities/deterministicGzip.d.ts.map +0 -1
- package/dist/node/capabilities/deterministicGzip.js +0 -51
- package/dist/node/capabilities/deterministicTar.d.ts +0 -6
- package/dist/node/capabilities/deterministicTar.d.ts.map +0 -1
- package/dist/node/capabilities/deterministicTar.js +0 -128
- package/dist/node/capabilities/skillDirectoryLoader.d.ts +0 -24
- package/dist/node/capabilities/skillDirectoryLoader.d.ts.map +0 -1
- package/dist/node/capabilities/skillDirectoryLoader.js +0 -308
- package/dist/node/capabilities/skillManifest.d.ts +0 -16
- package/dist/node/capabilities/skillManifest.d.ts.map +0 -1
- package/dist/node/capabilities/skillManifest.js +0 -22
- package/dist/node/capabilities/toolManifest.d.ts +0 -12
- package/dist/node/capabilities/toolManifest.d.ts.map +0 -1
- package/dist/node/capabilities/toolManifest.js +0 -111
- package/dist/node/capabilityPublishCli.d.ts +0 -2
- package/dist/node/capabilityPublishCli.d.ts.map +0 -1
- package/dist/node/capabilityPublishCli.js +0 -115
- package/dist/node/cli/spotlightCapabilitiesPublish.d.ts +0 -3
- package/dist/node/cli/spotlightCapabilitiesPublish.d.ts.map +0 -1
- package/dist/node/cli/spotlightCapabilitiesPublish.js +0 -3
- package/dist/node/publishCapabilitiesCli.d.ts +0 -3
- package/dist/node/publishCapabilitiesCli.d.ts.map +0 -1
- package/dist/node/publishCapabilitiesCli.js +0 -36
- package/dist/node/publishCapabilityArtifact.d.ts +0 -12
- package/dist/node/publishCapabilityArtifact.d.ts.map +0 -1
- package/dist/node/publishCapabilityArtifact.js +0 -25
- package/dist/runtimeId.d.ts +0 -3
- package/dist/runtimeId.d.ts.map +0 -1
- package/dist/runtimeId.js +0 -29
- package/dist/tools/frontendTool.d.ts +0 -39
- package/dist/tools/frontendTool.d.ts.map +0 -1
- package/dist/tools/frontendTool.js +0 -51
- package/dist/vite/capabilityBuild.d.ts +0 -9
- package/dist/vite/capabilityBuild.d.ts.map +0 -1
- package/dist/vite/capabilityBuild.js +0 -169
- package/dist/vite/capabilityBuildTypes.d.ts +0 -32
- package/dist/vite/capabilityBuildTypes.d.ts.map +0 -1
- package/dist/vite/capabilityBuildTypes.js +0 -1
- package/dist/vite/capabilityDevMiddleware.d.ts +0 -5
- package/dist/vite/capabilityDevMiddleware.d.ts.map +0 -1
- package/dist/vite/capabilityDevMiddleware.js +0 -42
- package/dist/vite/capabilityVirtualModule.d.ts +0 -11
- package/dist/vite/capabilityVirtualModule.d.ts.map +0 -1
- package/dist/vite/capabilityVirtualModule.js +0 -60
- package/dist/vite/capabilityVirtualModuleTypes.d.ts +0 -12
- package/dist/vite/capabilityVirtualModuleTypes.d.ts.map +0 -1
- package/dist/vite/capabilityVirtualModuleTypes.js +0 -1
- package/dist/vite/spotlightAgentIoc.d.ts +0 -11
- package/dist/vite/spotlightAgentIoc.d.ts.map +0 -1
- package/dist/vite/spotlightAgentIoc.js +0 -331
- package/dist/vite/spotlightCapabilities.d.ts +0 -4
- package/dist/vite/spotlightCapabilities.d.ts.map +0 -1
- package/dist/vite/spotlightCapabilities.js +0 -187
- package/src/agentDecorator.d.ts +0 -17
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { parse } from "@babel/parser";
|
|
2
|
+
import traverseImport from "@babel/traverse";
|
|
3
|
+
import * as t from "@babel/types";
|
|
4
|
+
import MagicString from "magic-string";
|
|
5
|
+
import { createHash } from "node:crypto";
|
|
6
|
+
const traverseAst = (traverseImport.default ??
|
|
7
|
+
traverseImport);
|
|
8
|
+
function literalSchema(node) {
|
|
9
|
+
const value = node.literal;
|
|
10
|
+
if (t.isStringLiteral(value))
|
|
11
|
+
return { type: "string", const: value.value };
|
|
12
|
+
if (t.isNumericLiteral(value))
|
|
13
|
+
return { type: "number", const: value.value };
|
|
14
|
+
if (t.isBooleanLiteral(value))
|
|
15
|
+
return { type: "boolean", const: value.value };
|
|
16
|
+
throw new Error("unsupported literal type");
|
|
17
|
+
}
|
|
18
|
+
function schemaFromType(node) {
|
|
19
|
+
if (t.isTSStringKeyword(node))
|
|
20
|
+
return { type: "string" };
|
|
21
|
+
if (t.isTSNumberKeyword(node))
|
|
22
|
+
return { type: "number" };
|
|
23
|
+
if (t.isTSBooleanKeyword(node))
|
|
24
|
+
return { type: "boolean" };
|
|
25
|
+
if (t.isTSNullKeyword(node) || t.isTSVoidKeyword(node))
|
|
26
|
+
return { type: "null" };
|
|
27
|
+
if (t.isTSUnknownKeyword(node) || t.isTSAnyKeyword(node))
|
|
28
|
+
return {};
|
|
29
|
+
if (t.isTSLiteralType(node))
|
|
30
|
+
return literalSchema(node);
|
|
31
|
+
if (t.isTSArrayType(node)) {
|
|
32
|
+
return { type: "array", items: schemaFromType(node.elementType) };
|
|
33
|
+
}
|
|
34
|
+
if (t.isTSUnionType(node)) {
|
|
35
|
+
const variants = node.types.map(schemaFromType);
|
|
36
|
+
const stringValues = variants
|
|
37
|
+
.map((variant) => variant.const)
|
|
38
|
+
.filter((value) => typeof value === "string");
|
|
39
|
+
if (stringValues.length === variants.length) {
|
|
40
|
+
return { type: "string", enum: stringValues };
|
|
41
|
+
}
|
|
42
|
+
return { anyOf: variants };
|
|
43
|
+
}
|
|
44
|
+
if (t.isTSTypeLiteral(node)) {
|
|
45
|
+
const properties = {};
|
|
46
|
+
const required = [];
|
|
47
|
+
for (const member of node.members) {
|
|
48
|
+
if (!t.isTSPropertySignature(member) || !member.typeAnnotation) {
|
|
49
|
+
throw new Error("only typed object properties are supported");
|
|
50
|
+
}
|
|
51
|
+
const key = t.isIdentifier(member.key)
|
|
52
|
+
? member.key.name
|
|
53
|
+
: t.isStringLiteral(member.key)
|
|
54
|
+
? member.key.value
|
|
55
|
+
: null;
|
|
56
|
+
if (!key)
|
|
57
|
+
throw new Error("computed property names are unsupported");
|
|
58
|
+
properties[key] = schemaFromType(member.typeAnnotation.typeAnnotation);
|
|
59
|
+
if (!member.optional)
|
|
60
|
+
required.push(key);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
type: "object",
|
|
64
|
+
properties,
|
|
65
|
+
additionalProperties: false,
|
|
66
|
+
...(required.length ? { required } : {}),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
throw new Error(`unsupported TypeScript type: ${node.type}`);
|
|
70
|
+
}
|
|
71
|
+
function handlerFunction(node) {
|
|
72
|
+
if (t.isArrowFunctionExpression(node) || t.isFunctionExpression(node))
|
|
73
|
+
return node;
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
function inputSchema(fn) {
|
|
77
|
+
if (fn.params.length === 0) {
|
|
78
|
+
return { type: "object", properties: {}, additionalProperties: false };
|
|
79
|
+
}
|
|
80
|
+
if (fn.params.length !== 1 ||
|
|
81
|
+
(!t.isIdentifier(fn.params[0]) && !t.isObjectPattern(fn.params[0]))) {
|
|
82
|
+
throw new Error("handler must have zero parameters or one typed parameter");
|
|
83
|
+
}
|
|
84
|
+
const paramAnnotation = fn.params[0].typeAnnotation;
|
|
85
|
+
const annotation = t.isTSTypeAnnotation(paramAnnotation)
|
|
86
|
+
? paramAnnotation.typeAnnotation
|
|
87
|
+
: undefined;
|
|
88
|
+
if (!annotation)
|
|
89
|
+
throw new Error("handler input parameter needs a TypeScript type");
|
|
90
|
+
return schemaFromType(annotation);
|
|
91
|
+
}
|
|
92
|
+
function hasValueReturn(fn) {
|
|
93
|
+
if (!t.isBlockStatement(fn.body))
|
|
94
|
+
return true;
|
|
95
|
+
let found = false;
|
|
96
|
+
traverseAst(fn.body, {
|
|
97
|
+
noScope: true,
|
|
98
|
+
ReturnStatement(path) {
|
|
99
|
+
if (path.node.argument)
|
|
100
|
+
found = true;
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
return found;
|
|
104
|
+
}
|
|
105
|
+
function outputSchema(fn) {
|
|
106
|
+
const annotation = t.isTSTypeAnnotation(fn.returnType)
|
|
107
|
+
? fn.returnType.typeAnnotation
|
|
108
|
+
: undefined;
|
|
109
|
+
if (annotation) {
|
|
110
|
+
if (t.isTSTypeReference(annotation) &&
|
|
111
|
+
t.isIdentifier(annotation.typeName, { name: "Promise" }) &&
|
|
112
|
+
annotation.typeParameters?.params.length === 1) {
|
|
113
|
+
return schemaFromType(annotation.typeParameters.params[0]);
|
|
114
|
+
}
|
|
115
|
+
return schemaFromType(annotation);
|
|
116
|
+
}
|
|
117
|
+
if (!hasValueReturn(fn))
|
|
118
|
+
return { type: "null" };
|
|
119
|
+
throw new Error("handler return type must be explicit when it returns a value");
|
|
120
|
+
}
|
|
121
|
+
function jsDoc(path) {
|
|
122
|
+
const declaration = path.parentPath?.node;
|
|
123
|
+
const exportNode = path.parentPath?.parentPath?.node;
|
|
124
|
+
const comments = [
|
|
125
|
+
...(declaration?.leadingComments ?? []),
|
|
126
|
+
...(exportNode?.leadingComments ?? []),
|
|
127
|
+
];
|
|
128
|
+
const comment = comments.reverse().find((item) => item.type === "CommentBlock");
|
|
129
|
+
if (!comment)
|
|
130
|
+
return "";
|
|
131
|
+
return comment.value
|
|
132
|
+
.split("\n")
|
|
133
|
+
.map((line) => line.replace(/^\s*\*\s?/, "").trim())
|
|
134
|
+
.filter((line) => line && !line.startsWith("@"))
|
|
135
|
+
.join(" ");
|
|
136
|
+
}
|
|
137
|
+
function staticJsonValue(node, path, seen = new Set()) {
|
|
138
|
+
if (t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isBooleanLiteral(node)) {
|
|
139
|
+
return node.value;
|
|
140
|
+
}
|
|
141
|
+
if (t.isNullLiteral(node))
|
|
142
|
+
return null;
|
|
143
|
+
if (t.isArrayExpression(node)) {
|
|
144
|
+
return node.elements.map((element) => {
|
|
145
|
+
if (!element || t.isSpreadElement(element)) {
|
|
146
|
+
throw new Error("schema arrays cannot contain holes or spreads");
|
|
147
|
+
}
|
|
148
|
+
return staticJsonValue(element, path, seen);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
if (t.isObjectExpression(node)) {
|
|
152
|
+
const result = {};
|
|
153
|
+
for (const property of node.properties) {
|
|
154
|
+
if (!t.isObjectProperty(property) || property.computed) {
|
|
155
|
+
throw new Error("schema objects cannot contain methods or spreads");
|
|
156
|
+
}
|
|
157
|
+
const key = t.isIdentifier(property.key)
|
|
158
|
+
? property.key.name
|
|
159
|
+
: t.isStringLiteral(property.key)
|
|
160
|
+
? property.key.value
|
|
161
|
+
: null;
|
|
162
|
+
if (!key)
|
|
163
|
+
throw new Error("schema keys must be identifiers or strings");
|
|
164
|
+
result[key] = staticJsonValue(property.value, path, seen);
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
168
|
+
if (t.isIdentifier(node)) {
|
|
169
|
+
if (seen.has(node.name))
|
|
170
|
+
throw new Error(`circular schema const: ${node.name}`);
|
|
171
|
+
const binding = path.scope.getBinding(node.name);
|
|
172
|
+
const declarator = binding?.path;
|
|
173
|
+
if (!declarator?.isVariableDeclarator() || !declarator.node.init) {
|
|
174
|
+
throw new Error(`schema identifier is not a local const: ${node.name}`);
|
|
175
|
+
}
|
|
176
|
+
const nextSeen = new Set(seen).add(node.name);
|
|
177
|
+
return staticJsonValue(declarator.node.init, path, nextSeen);
|
|
178
|
+
}
|
|
179
|
+
if (t.isTSAsExpression(node) || t.isTSSatisfiesExpression(node)) {
|
|
180
|
+
return staticJsonValue(node.expression, path, seen);
|
|
181
|
+
}
|
|
182
|
+
throw new Error(`schema contains dynamic expression: ${node.type}`);
|
|
183
|
+
}
|
|
184
|
+
function shouldInclude(id, include) {
|
|
185
|
+
const clean = id.split("?", 1)[0];
|
|
186
|
+
if (!/\.[cm]?[jt]sx?$/.test(clean) || clean.includes("node_modules"))
|
|
187
|
+
return false;
|
|
188
|
+
return typeof include === "string" ? clean.includes(include) : include.test(clean);
|
|
189
|
+
}
|
|
190
|
+
function staticToolOptions(options, path) {
|
|
191
|
+
if (!options)
|
|
192
|
+
return {};
|
|
193
|
+
const allowed = new Set([
|
|
194
|
+
"version",
|
|
195
|
+
"sideEffect",
|
|
196
|
+
"replayPolicy",
|
|
197
|
+
"riskLevel",
|
|
198
|
+
"requiresConfirmation",
|
|
199
|
+
"maxOutputBytes",
|
|
200
|
+
]);
|
|
201
|
+
const values = {};
|
|
202
|
+
for (const property of options.properties) {
|
|
203
|
+
if (!t.isObjectProperty(property) || property.computed)
|
|
204
|
+
continue;
|
|
205
|
+
const key = t.isIdentifier(property.key)
|
|
206
|
+
? property.key.name
|
|
207
|
+
: t.isStringLiteral(property.key)
|
|
208
|
+
? property.key.value
|
|
209
|
+
: "";
|
|
210
|
+
if (allowed.has(key))
|
|
211
|
+
values[key] = staticJsonValue(property.value, path);
|
|
212
|
+
}
|
|
213
|
+
if (values.version !== undefined && typeof values.version !== "string") {
|
|
214
|
+
throw new Error("version must be a string literal");
|
|
215
|
+
}
|
|
216
|
+
if (values.sideEffect !== undefined &&
|
|
217
|
+
!["none", "ui", "external"].includes(String(values.sideEffect))) {
|
|
218
|
+
throw new Error("sideEffect must be none, ui or external");
|
|
219
|
+
}
|
|
220
|
+
if (values.replayPolicy !== undefined &&
|
|
221
|
+
!["safe", "idempotency-key", "never"].includes(String(values.replayPolicy))) {
|
|
222
|
+
throw new Error("replayPolicy is invalid");
|
|
223
|
+
}
|
|
224
|
+
if (values.riskLevel !== undefined &&
|
|
225
|
+
!["low", "medium", "high"].includes(String(values.riskLevel))) {
|
|
226
|
+
throw new Error("riskLevel must be low, medium or high");
|
|
227
|
+
}
|
|
228
|
+
if (values.requiresConfirmation !== undefined &&
|
|
229
|
+
typeof values.requiresConfirmation !== "boolean") {
|
|
230
|
+
throw new Error("requiresConfirmation must be a boolean literal");
|
|
231
|
+
}
|
|
232
|
+
if (values.maxOutputBytes !== undefined &&
|
|
233
|
+
(typeof values.maxOutputBytes !== "number" || values.maxOutputBytes <= 0)) {
|
|
234
|
+
throw new Error("maxOutputBytes must be a positive number literal");
|
|
235
|
+
}
|
|
236
|
+
return values;
|
|
237
|
+
}
|
|
238
|
+
function stableJson(value) {
|
|
239
|
+
if (Array.isArray(value))
|
|
240
|
+
return `[${value.map(stableJson).join(",")}]`;
|
|
241
|
+
if (value && typeof value === "object") {
|
|
242
|
+
const record = value;
|
|
243
|
+
return `{${Object.keys(record)
|
|
244
|
+
.filter((key) => record[key] !== undefined)
|
|
245
|
+
.sort()
|
|
246
|
+
.map((key) => `${JSON.stringify(key)}:${stableJson(record[key])}`)
|
|
247
|
+
.join(",")}}`;
|
|
248
|
+
}
|
|
249
|
+
return JSON.stringify(value);
|
|
250
|
+
}
|
|
251
|
+
export function transformSpotlightClientTools(code, id, onDescriptor) {
|
|
252
|
+
if (!code.includes("defineClientTool"))
|
|
253
|
+
return null;
|
|
254
|
+
const ast = parse(code, {
|
|
255
|
+
sourceType: "module",
|
|
256
|
+
plugins: ["typescript", "jsx"],
|
|
257
|
+
sourceFilename: id,
|
|
258
|
+
});
|
|
259
|
+
const edits = [];
|
|
260
|
+
traverseAst(ast, {
|
|
261
|
+
VariableDeclarator(path) {
|
|
262
|
+
if (!t.isIdentifier(path.node.id) || !t.isCallExpression(path.node.init))
|
|
263
|
+
return;
|
|
264
|
+
const call = path.node.init;
|
|
265
|
+
if (!t.isIdentifier(call.callee, { name: "defineClientTool" }))
|
|
266
|
+
return;
|
|
267
|
+
if (call.arguments.length < 1 || call.arguments.length > 2) {
|
|
268
|
+
throw path.buildCodeFrameError("defineClientTool expects handler and optional options");
|
|
269
|
+
}
|
|
270
|
+
const fn = handlerFunction(call.arguments[0]);
|
|
271
|
+
if (!fn)
|
|
272
|
+
throw path.buildCodeFrameError("defineClientTool handler must be inline");
|
|
273
|
+
const description = jsDoc(path);
|
|
274
|
+
if (!description) {
|
|
275
|
+
throw path.buildCodeFrameError(`Client tool ${path.node.id.name} needs a JSDoc description`);
|
|
276
|
+
}
|
|
277
|
+
const optionArg = call.arguments[1];
|
|
278
|
+
if (optionArg && !t.isObjectExpression(optionArg)) {
|
|
279
|
+
throw path.buildCodeFrameError("Client tool options must be an object literal");
|
|
280
|
+
}
|
|
281
|
+
let descriptorOptions = {};
|
|
282
|
+
try {
|
|
283
|
+
descriptorOptions = staticToolOptions(optionArg, path);
|
|
284
|
+
}
|
|
285
|
+
catch (error) {
|
|
286
|
+
throw path.buildCodeFrameError(`Invalid static options for ${path.node.id.name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
287
|
+
}
|
|
288
|
+
const hasExplicitSchema = optionArg?.properties.some((property) => t.isObjectProperty(property) &&
|
|
289
|
+
t.isIdentifier(property.key, { name: "schema" }));
|
|
290
|
+
let schema;
|
|
291
|
+
if (!hasExplicitSchema) {
|
|
292
|
+
try {
|
|
293
|
+
schema = { input: inputSchema(fn), output: outputSchema(fn) };
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
throw path.buildCodeFrameError(`Cannot infer schema for ${path.node.id.name}: ${error instanceof Error ? error.message : String(error)}. Add { schema: { input, output } } as the second argument.`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const generated = [
|
|
300
|
+
`name:${JSON.stringify(path.node.id.name)}`,
|
|
301
|
+
`description:${JSON.stringify(description)}`,
|
|
302
|
+
...(schema ? [`schema:${JSON.stringify(schema)}`] : []),
|
|
303
|
+
].join(",");
|
|
304
|
+
if (schema) {
|
|
305
|
+
onDescriptor?.({
|
|
306
|
+
name: path.node.id.name,
|
|
307
|
+
version: "1.0.0",
|
|
308
|
+
description,
|
|
309
|
+
inputSchema: schema.input,
|
|
310
|
+
outputSchema: schema.output,
|
|
311
|
+
sideEffect: "ui",
|
|
312
|
+
replayPolicy: "never",
|
|
313
|
+
riskLevel: "low",
|
|
314
|
+
requiresConfirmation: false,
|
|
315
|
+
...descriptorOptions,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
else if (optionArg) {
|
|
319
|
+
const schemaProperty = optionArg.properties.find((property) => t.isObjectProperty(property) &&
|
|
320
|
+
t.isIdentifier(property.key, { name: "schema" }));
|
|
321
|
+
if (schemaProperty) {
|
|
322
|
+
try {
|
|
323
|
+
const parsed = staticJsonValue(schemaProperty.value, path);
|
|
324
|
+
if (!parsed?.input || typeof parsed.input !== "object") {
|
|
325
|
+
throw new Error("schema.input must be an object");
|
|
326
|
+
}
|
|
327
|
+
onDescriptor?.({
|
|
328
|
+
name: path.node.id.name,
|
|
329
|
+
version: "1.0.0",
|
|
330
|
+
description,
|
|
331
|
+
inputSchema: parsed.input,
|
|
332
|
+
outputSchema: parsed.output,
|
|
333
|
+
sideEffect: "ui",
|
|
334
|
+
replayPolicy: "never",
|
|
335
|
+
riskLevel: "low",
|
|
336
|
+
requiresConfirmation: false,
|
|
337
|
+
...descriptorOptions,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
catch (error) {
|
|
341
|
+
if (onDescriptor) {
|
|
342
|
+
throw path.buildCodeFrameError(`Cannot emit production schema for ${path.node.id.name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (optionArg) {
|
|
348
|
+
edits.push({
|
|
349
|
+
kind: "replace",
|
|
350
|
+
start: optionArg.start,
|
|
351
|
+
end: optionArg.end,
|
|
352
|
+
text: `{ ...${code.slice(optionArg.start, optionArg.end)}, ${generated} }`,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
const gapBeforeClose = code.slice(call.arguments[0].end, call.end - 1);
|
|
357
|
+
const separator = gapBeforeClose.includes(",") ? "" : ",";
|
|
358
|
+
edits.push({
|
|
359
|
+
kind: "insert",
|
|
360
|
+
position: call.end - 1,
|
|
361
|
+
text: `${separator} { ${generated} }`,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
if (!edits.length)
|
|
367
|
+
return null;
|
|
368
|
+
const magic = new MagicString(code);
|
|
369
|
+
const editPosition = (edit) => edit.kind === "insert" ? edit.position : edit.start;
|
|
370
|
+
for (const edit of edits.sort((a, b) => editPosition(b) - editPosition(a))) {
|
|
371
|
+
if (edit.kind === "insert")
|
|
372
|
+
magic.appendLeft(edit.position, edit.text);
|
|
373
|
+
else
|
|
374
|
+
magic.overwrite(edit.start, edit.end, edit.text);
|
|
375
|
+
}
|
|
376
|
+
return { code: magic.toString(), map: magic.generateMap({ hires: true }) };
|
|
377
|
+
}
|
|
378
|
+
export default function spotlightClientTools(options = {}) {
|
|
379
|
+
const include = options.include ?? "/src/";
|
|
380
|
+
const descriptors = new Map();
|
|
381
|
+
let productionBuild = false;
|
|
382
|
+
return {
|
|
383
|
+
name: "spotlight-client-tools",
|
|
384
|
+
enforce: "pre",
|
|
385
|
+
configResolved(config) {
|
|
386
|
+
productionBuild = config.command === "build" && config.mode === "production";
|
|
387
|
+
if (productionBuild &&
|
|
388
|
+
(!options.projectId?.trim() || !options.frontendBuildId?.trim())) {
|
|
389
|
+
throw new Error("spotlightClientTools: projectId and frontendBuildId are required for production builds");
|
|
390
|
+
}
|
|
391
|
+
},
|
|
392
|
+
transform(code, id) {
|
|
393
|
+
if (!shouldInclude(id, include))
|
|
394
|
+
return null;
|
|
395
|
+
return transformSpotlightClientTools(code, id, (descriptor) => {
|
|
396
|
+
descriptors.set(descriptor.name, descriptor);
|
|
397
|
+
});
|
|
398
|
+
},
|
|
399
|
+
generateBundle() {
|
|
400
|
+
if (!productionBuild)
|
|
401
|
+
return;
|
|
402
|
+
if (descriptors.size === 0) {
|
|
403
|
+
throw new Error("spotlightClientTools: production manifest has no tools");
|
|
404
|
+
}
|
|
405
|
+
const unsigned = {
|
|
406
|
+
protocolVersion: "spotlight.capabilities/1",
|
|
407
|
+
projectId: options.projectId.trim(),
|
|
408
|
+
frontendBuildId: options.frontendBuildId.trim(),
|
|
409
|
+
tools: [...descriptors.values()].sort((a, b) => a.name.localeCompare(b.name)),
|
|
410
|
+
};
|
|
411
|
+
const manifest = {
|
|
412
|
+
...unsigned,
|
|
413
|
+
manifestDigest: `sha256:${createHash("sha256")
|
|
414
|
+
.update(stableJson(unsigned))
|
|
415
|
+
.digest("hex")}`,
|
|
416
|
+
};
|
|
417
|
+
this.emitFile({
|
|
418
|
+
type: "asset",
|
|
419
|
+
fileName: options.manifestFileName ?? "spotlight-client-manifest.json",
|
|
420
|
+
source: `${JSON.stringify(manifest, null, 2)}\n`,
|
|
421
|
+
});
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inupedia/spotlight-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Build-described browser Client Tools and HTTP helpers for Inupedia Spotlight",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "inupedia",
|
|
@@ -17,37 +17,23 @@
|
|
|
17
17
|
"./node": {
|
|
18
18
|
"types": "./dist/node/index.d.ts",
|
|
19
19
|
"import": "./dist/node/index.js"
|
|
20
|
-
},
|
|
21
|
-
"./agent-decorator": {
|
|
22
|
-
"types": "./src/agentDecorator.d.ts"
|
|
23
|
-
},
|
|
24
|
-
"./agent-decorator-types": {
|
|
25
|
-
"types": "./dist/agentDecoratorTypes.d.ts",
|
|
26
|
-
"import": "./dist/agentDecoratorTypes.js"
|
|
27
20
|
}
|
|
28
21
|
},
|
|
29
22
|
"main": "./dist/index.js",
|
|
30
23
|
"types": "./dist/index.d.ts",
|
|
31
|
-
"bin": {
|
|
32
|
-
"spotlight-capabilities-publish": "./dist/node/cli/spotlightCapabilitiesPublish.js",
|
|
33
|
-
"spotlight-capabilities": "./dist/node/publishCapabilitiesCli.js"
|
|
34
|
-
},
|
|
35
24
|
"publishConfig": {
|
|
36
25
|
"access": "public"
|
|
37
26
|
},
|
|
38
27
|
"files": [
|
|
39
|
-
"dist"
|
|
40
|
-
"src/agentDecorator.d.ts"
|
|
28
|
+
"dist"
|
|
41
29
|
],
|
|
42
30
|
"dependencies": {
|
|
43
31
|
"@babel/parser": "^7.28.5",
|
|
44
32
|
"@babel/traverse": "^7.28.5",
|
|
45
33
|
"@babel/types": "^7.28.5",
|
|
46
|
-
"canonicalize": "3.0.0",
|
|
47
|
-
"fflate": "0.8.3",
|
|
48
34
|
"magic-string": "^0.30.21",
|
|
49
35
|
"yaml": "^2.8.4",
|
|
50
|
-
"@inupedia/spotlight-protocol": "0.
|
|
36
|
+
"@inupedia/spotlight-protocol": "0.4.0"
|
|
51
37
|
},
|
|
52
38
|
"peerDependencies": {
|
|
53
39
|
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
@@ -61,8 +47,8 @@
|
|
|
61
47
|
"@types/babel__traverse": "^7.28.0",
|
|
62
48
|
"@types/node": "^24.10.1",
|
|
63
49
|
"typescript": "^5.9.3",
|
|
64
|
-
"
|
|
65
|
-
"
|
|
50
|
+
"vitest": "^4.0.18",
|
|
51
|
+
"vite": "^7.3.1"
|
|
66
52
|
},
|
|
67
53
|
"scripts": {
|
|
68
54
|
"clean": "rm -rf dist",
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { type CapabilityHandshakeResultV1, type RenewCapabilityLeaseResultV1 } from "@inupedia/spotlight-protocol";
|
|
2
|
-
import type { FrontendToolRegistryV1 } from "../tools/frontendTool.js";
|
|
3
|
-
import { type CapabilityExecutionFenceV1 } from "./hostActionExecutor.js";
|
|
4
|
-
type AcceptedHandshake = Extract<CapabilityHandshakeResultV1, {
|
|
5
|
-
status: "accepted";
|
|
6
|
-
}>;
|
|
7
|
-
export interface CapabilityChannelV1 {
|
|
8
|
-
open(): void;
|
|
9
|
-
close(): void;
|
|
10
|
-
renew(): Promise<RenewCapabilityLeaseResultV1>;
|
|
11
|
-
reconnect(): Promise<void>;
|
|
12
|
-
fence(): Readonly<CapabilityExecutionFenceV1>;
|
|
13
|
-
}
|
|
14
|
-
export declare function createCapabilityChannel(options: {
|
|
15
|
-
endpoint: string;
|
|
16
|
-
handshake: AcceptedHandshake;
|
|
17
|
-
identity: {
|
|
18
|
-
sessionId: string;
|
|
19
|
-
browserInstanceId: string;
|
|
20
|
-
tabInstanceId: string;
|
|
21
|
-
signal?: AbortSignal;
|
|
22
|
-
};
|
|
23
|
-
registry: FrontendToolRegistryV1;
|
|
24
|
-
fetch: typeof globalThis.fetch;
|
|
25
|
-
createWebSocket?: (url: string) => WebSocket;
|
|
26
|
-
}): CapabilityChannelV1;
|
|
27
|
-
export {};
|
|
28
|
-
//# sourceMappingURL=channel.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../src/capabilities/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,2BAA2B,EAChC,KAAK,4BAA4B,EAElC,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAEvE,OAAO,EAA4B,KAAK,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AAEpG,KAAK,iBAAiB,GAAG,OAAO,CAAC,2BAA2B,EAAE;IAAE,MAAM,EAAE,UAAU,CAAA;CAAE,CAAC,CAAC;AAEtF,MAAM,WAAW,mBAAmB;IAClC,IAAI,IAAI,IAAI,CAAC;IACb,KAAK,IAAI,IAAI,CAAC;IACd,KAAK,IAAI,OAAO,CAAC,4BAA4B,CAAC,CAAC;IAC/C,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3B,KAAK,IAAI,QAAQ,CAAC,0BAA0B,CAAC,CAAC;CAC/C;AAED,wBAAgB,uBAAuB,CAAC,OAAO,EAAE;IAC/C,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,iBAAiB,CAAC;IAC7B,QAAQ,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,iBAAiB,EAAE,MAAM,CAAC;QAAC,aAAa,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,CAAC;IACxG,QAAQ,EAAE,sBAAsB,CAAC;IACjC,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAC/B,eAAe,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,SAAS,CAAC;CAC9C,GAAG,mBAAmB,CAyLtB"}
|