agents 0.14.3 → 0.14.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.
@@ -0,0 +1,40 @@
1
+ //#region src/skills/compile.d.ts
2
+ interface CompiledSkillScript {
3
+ /** Self-contained ESM source, ready to embed and run without a bundler. */
4
+ content: string;
5
+ /** Always `true`; mirrors the `precompiled` flag on skill resources. */
6
+ precompiled: true;
7
+ }
8
+ interface CompileSkillScriptOptions {
9
+ /**
10
+ * JavaScript target for the emitted bundle. Defaults to `es2022`, which the
11
+ * Workers runtime supports.
12
+ */
13
+ target?: string;
14
+ }
15
+ /**
16
+ * Whether a resource path is a skill script that should be compiled ahead of
17
+ * time (i.e. has a `.js`, `.mjs`, `.ts`, or `.tsx` extension).
18
+ */
19
+ declare function isCompilableSkillScript(path: string): boolean;
20
+ /**
21
+ * Compile a skill script file into a single self-contained ESM module.
22
+ *
23
+ * Resolves and inlines sibling imports relative to `entryPath`, strips
24
+ * TypeScript types, and emits ESM so the script can run in the Worker sandbox
25
+ * without an in-Worker bundler.
26
+ *
27
+ * @param entryPath Absolute path to the skill script file on disk.
28
+ */
29
+ declare function compileSkillScript(
30
+ entryPath: string,
31
+ options?: CompileSkillScriptOptions
32
+ ): Promise<CompiledSkillScript>;
33
+ //#endregion
34
+ export {
35
+ CompileSkillScriptOptions,
36
+ CompiledSkillScript,
37
+ compileSkillScript,
38
+ isCompilableSkillScript
39
+ };
40
+ //# sourceMappingURL=compile.d.ts.map
@@ -0,0 +1,65 @@
1
+ import { build } from "esbuild";
2
+ //#region src/skills/compile.ts
3
+ /**
4
+ * Build-time skill-script compiler.
5
+ *
6
+ * Skill scripts must be self-contained JavaScript modules before they can run:
7
+ * the Worker runtime executes them directly through the sandbox and does **not**
8
+ * ship an in-Worker bundler. This module compiles a skill script (TypeScript or
9
+ * multi-file JavaScript) into a single self-contained ESM bundle using esbuild.
10
+ *
11
+ * It runs in Node (build time) only — the Agents Vite plugin uses it to compile
12
+ * bundled skills, and skills served from R2 or other dynamic sources should be
13
+ * compiled with {@link compileSkillScript} before upload.
14
+ *
15
+ * @module
16
+ */
17
+ /** Skill-script extensions that can (and must) be compiled ahead of time. */
18
+ const COMPILABLE_SCRIPT_EXTENSIONS = new Set([
19
+ ".js",
20
+ ".mjs",
21
+ ".ts",
22
+ ".tsx"
23
+ ]);
24
+ function extensionOf(path) {
25
+ const file = path.split("/").at(-1) ?? path;
26
+ const index = file.lastIndexOf(".");
27
+ return index === -1 ? "" : file.slice(index).toLowerCase();
28
+ }
29
+ /**
30
+ * Whether a resource path is a skill script that should be compiled ahead of
31
+ * time (i.e. has a `.js`, `.mjs`, `.ts`, or `.tsx` extension).
32
+ */
33
+ function isCompilableSkillScript(path) {
34
+ return COMPILABLE_SCRIPT_EXTENSIONS.has(extensionOf(path));
35
+ }
36
+ /**
37
+ * Compile a skill script file into a single self-contained ESM module.
38
+ *
39
+ * Resolves and inlines sibling imports relative to `entryPath`, strips
40
+ * TypeScript types, and emits ESM so the script can run in the Worker sandbox
41
+ * without an in-Worker bundler.
42
+ *
43
+ * @param entryPath Absolute path to the skill script file on disk.
44
+ */
45
+ async function compileSkillScript(entryPath, options = {}) {
46
+ const output = (await build({
47
+ entryPoints: [entryPath],
48
+ bundle: true,
49
+ write: false,
50
+ format: "esm",
51
+ platform: "browser",
52
+ target: options.target ?? "es2022",
53
+ logLevel: "silent",
54
+ legalComments: "none"
55
+ })).outputFiles?.[0];
56
+ if (!output) throw new Error(`esbuild produced no output when compiling skill script "${entryPath}".`);
57
+ return {
58
+ content: output.text,
59
+ precompiled: true
60
+ };
61
+ }
62
+ //#endregion
63
+ export { compileSkillScript, isCompilableSkillScript };
64
+
65
+ //# sourceMappingURL=compile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile.js","names":[],"sources":["../../src/skills/compile.ts"],"sourcesContent":["import { build } from \"esbuild\";\n\n/**\n * Build-time skill-script compiler.\n *\n * Skill scripts must be self-contained JavaScript modules before they can run:\n * the Worker runtime executes them directly through the sandbox and does **not**\n * ship an in-Worker bundler. This module compiles a skill script (TypeScript or\n * multi-file JavaScript) into a single self-contained ESM bundle using esbuild.\n *\n * It runs in Node (build time) only — the Agents Vite plugin uses it to compile\n * bundled skills, and skills served from R2 or other dynamic sources should be\n * compiled with {@link compileSkillScript} before upload.\n *\n * @module\n */\n\n/** Skill-script extensions that can (and must) be compiled ahead of time. */\nconst COMPILABLE_SCRIPT_EXTENSIONS = new Set([\".js\", \".mjs\", \".ts\", \".tsx\"]);\n\nexport interface CompiledSkillScript {\n /** Self-contained ESM source, ready to embed and run without a bundler. */\n content: string;\n /** Always `true`; mirrors the `precompiled` flag on skill resources. */\n precompiled: true;\n}\n\nexport interface CompileSkillScriptOptions {\n /**\n * JavaScript target for the emitted bundle. Defaults to `es2022`, which the\n * Workers runtime supports.\n */\n target?: string;\n}\n\nfunction extensionOf(path: string): string {\n const file = path.split(\"/\").at(-1) ?? path;\n const index = file.lastIndexOf(\".\");\n return index === -1 ? \"\" : file.slice(index).toLowerCase();\n}\n\n/**\n * Whether a resource path is a skill script that should be compiled ahead of\n * time (i.e. has a `.js`, `.mjs`, `.ts`, or `.tsx` extension).\n */\nexport function isCompilableSkillScript(path: string): boolean {\n return COMPILABLE_SCRIPT_EXTENSIONS.has(extensionOf(path));\n}\n\n/**\n * Compile a skill script file into a single self-contained ESM module.\n *\n * Resolves and inlines sibling imports relative to `entryPath`, strips\n * TypeScript types, and emits ESM so the script can run in the Worker sandbox\n * without an in-Worker bundler.\n *\n * @param entryPath Absolute path to the skill script file on disk.\n */\nexport async function compileSkillScript(\n entryPath: string,\n options: CompileSkillScriptOptions = {}\n): Promise<CompiledSkillScript> {\n const result = await build({\n entryPoints: [entryPath],\n bundle: true,\n write: false,\n format: \"esm\",\n platform: \"browser\",\n target: options.target ?? \"es2022\",\n logLevel: \"silent\",\n legalComments: \"none\"\n });\n\n const output = result.outputFiles?.[0];\n if (!output) {\n throw new Error(\n `esbuild produced no output when compiling skill script \"${entryPath}\".`\n );\n }\n\n return { content: output.text, precompiled: true };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,MAAM,+BAA+B,IAAI,IAAI;CAAC;CAAO;CAAQ;CAAO;AAAM,CAAC;AAiB3E,SAAS,YAAY,MAAsB;CACzC,MAAM,OAAO,KAAK,MAAM,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK;CACvC,MAAM,QAAQ,KAAK,YAAY,GAAG;CAClC,OAAO,UAAU,KAAK,KAAK,KAAK,MAAM,KAAK,CAAC,CAAC,YAAY;AAC3D;;;;;AAMA,SAAgB,wBAAwB,MAAuB;CAC7D,OAAO,6BAA6B,IAAI,YAAY,IAAI,CAAC;AAC3D;;;;;;;;;;AAWA,eAAsB,mBACpB,WACA,UAAqC,CAAC,GACR;CAY9B,MAAM,UAAS,MAXM,MAAM;EACzB,aAAa,CAAC,SAAS;EACvB,QAAQ;EACR,OAAO;EACP,QAAQ;EACR,UAAU;EACV,QAAQ,QAAQ,UAAU;EAC1B,UAAU;EACV,eAAe;CACjB,CAAC,EAAA,CAEqB,cAAc;CACpC,IAAI,CAAC,QACH,MAAM,IAAI,MACR,2DAA2D,UAAU,GACvE;CAGF,OAAO;EAAE,SAAS,OAAO;EAAM,aAAa;CAAK;AACnD"}
@@ -38,6 +38,14 @@ interface SkillResourceDescriptor {
38
38
  size?: number;
39
39
  encoding?: "text" | "base64";
40
40
  mimeType?: string;
41
+ /**
42
+ * Set when a script resource was compiled to a self-contained JavaScript
43
+ * module ahead of time — by the Agents Vite plugin for bundled skills, or via
44
+ * `compileSkillScript` from `agents/skills/compile` for R2/dynamic skills. The
45
+ * runtime runs precompiled scripts directly; the runtime ships no in-Worker
46
+ * bundler, so non-precompiled TypeScript or multi-file scripts cannot run.
47
+ */
48
+ precompiled?: boolean;
41
49
  }
42
50
  interface SkillResource extends SkillResourceDescriptor {
43
51
  content: string;
@@ -505,45 +505,42 @@ function scriptModule(source, request) {
505
505
  "}"
506
506
  ].join("\n");
507
507
  }
508
- function moduleSource(module) {
509
- if (typeof module === "string") return module;
510
- return module?.js ?? module?.cjs ?? null;
508
+ /**
509
+ * Whether a script declares a default export, in either the raw author form
510
+ * (`export default ...`) or the bundled form esbuild emits
511
+ * (`export { run as default }`).
512
+ */
513
+ function hasDefaultExport(source) {
514
+ return /^\s*export\s+default\s+/m.test(source) || /export\s*\{[^}]*\bas\s+default\b[^}]*\}/m.test(source);
511
515
  }
512
516
  /**
513
- * Remove `export { ... }` blocks (illegal inside the function wrapper) and
514
- * rewrite a `export { X as default }` binding to the local `__skillRun`.
517
+ * Remove `export { ... }` blocks, which are illegal inside the function wrapper
518
+ * the runner builds around skill scripts.
515
519
  */
516
520
  function stripStrayExports(source) {
517
521
  return source.replace(/\n?export\s*\{[\s\S]*?\};?/g, "");
518
522
  }
519
523
  function rewriteBundledSource(source) {
520
- const defaultExport = source.match(/export\s*\{\s*([A-Za-z_$][\w$]*)\s+as\s+default\s*\};?/m);
521
- let out = source;
522
- if (defaultExport) out = out.replace(defaultExport[0], `const __skillRun = ${defaultExport[1]};`);
523
- return stripStrayExports(out);
524
+ const defaultBinding = source.match(/\bexport\s*\{[^}]*\b([A-Za-z_$][\w$]*)\s+as\s+default\b[^}]*\}/m);
525
+ const stripped = stripStrayExports(source);
526
+ if (defaultBinding) return `${stripped}\nconst __skillRun = ${defaultBinding[1]};`;
527
+ return stripped;
524
528
  }
525
529
  async function prepareJavaScriptSource(request, runtime) {
526
- const files = {};
530
+ if ((request.resources ?? []).some((resource) => resource.path === request.path && resource.precompiled === true)) return rewriteBundledSource(request.source);
531
+ let scriptFileCount = 1;
527
532
  for (const resource of request.resources ?? []) {
533
+ if (resource.path === request.path) continue;
528
534
  const extension = extensionOf(resource.path);
529
535
  if (resource.kind === "script" && (resource.encoding ?? "text") === "text" && [
530
536
  ".js",
531
537
  ".mjs",
532
538
  ".ts",
533
539
  ".tsx"
534
- ].includes(extension)) files[resource.path] = resource.content;
540
+ ].includes(extension)) scriptFileCount++;
535
541
  }
536
- files[request.path] = request.source;
537
- if (!(runtime === "typescript" || Object.keys(files).length > 1)) return request.source;
538
- const { createWorker } = await import("@cloudflare/worker-bundler");
539
- const result = await createWorker({
540
- files,
541
- entryPoint: request.path,
542
- bundle: true
543
- });
544
- const compiled = moduleSource(result.modules[result.mainModule]) ?? moduleSource(Object.values(result.modules)[0]);
545
- if (!compiled) throw new Error(`Failed to compile skill script: ${request.path}`);
546
- return rewriteBundledSource(compiled);
542
+ if (runtime === "javascript" && scriptFileCount === 1) return request.source;
543
+ throw new Error(`Skill script "${request.path}" must be compiled to a self-contained JavaScript module before it can run. Bundled skills are compiled automatically by the Agents Vite plugin. Skills served from R2 or other dynamic sources must be bundled ahead of time (e.g. with \`compileSkillScript\` from "agents/skills/compile") before upload.`);
547
544
  }
548
545
  async function executeToolFromSet(tools, name, input) {
549
546
  const target = tools?.[name];
@@ -1052,7 +1049,7 @@ async function runBashScript(request, options, bridge) {
1052
1049
  }
1053
1050
  }
1054
1051
  async function runJavaScriptScript(request, options, bridge, runtime) {
1055
- if (!/^\s*export\s+default\s+/m.test(request.source)) throw new Error("JS/TS skill scripts must `export default` an async run(input, ctx) function.");
1052
+ if (!hasDefaultExport(request.source)) throw new Error("JS/TS skill scripts must `export default` an async run(input, ctx) function.");
1056
1053
  const source = await prepareJavaScriptSource(request, runtime);
1057
1054
  const result = await new DynamicWorkerExecutor({
1058
1055
  loader: options.loader,