@uipath/uipath-extensions-sdk 0.1.22 → 0.1.23

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.
Files changed (43) hide show
  1. package/README.md +45 -8
  2. package/dist/adopt.d.ts +6 -0
  3. package/dist/adopt.js +459 -77
  4. package/dist/adopt.js.map +1 -1
  5. package/dist/cli.js +4 -2
  6. package/dist/cli.js.map +1 -1
  7. package/dist/commands/build-check.d.ts +2 -2
  8. package/dist/commands/build-check.js +43 -18
  9. package/dist/commands/build-check.js.map +1 -1
  10. package/dist/commands/new.js +9 -15
  11. package/dist/commands/new.js.map +1 -1
  12. package/dist/commands/pack.d.ts +12 -0
  13. package/dist/commands/pack.js +96 -30
  14. package/dist/commands/pack.js.map +1 -1
  15. package/dist/commands/submit.d.ts +15 -1
  16. package/dist/commands/submit.js +147 -35
  17. package/dist/commands/submit.js.map +1 -1
  18. package/dist/commands/upload.d.ts +4 -0
  19. package/dist/commands/upload.js +98 -26
  20. package/dist/commands/upload.js.map +1 -1
  21. package/dist/commands/validate-python.d.ts +15 -0
  22. package/dist/commands/validate-python.js +334 -0
  23. package/dist/commands/validate-python.js.map +1 -0
  24. package/dist/commands/validate.js +2 -1
  25. package/dist/commands/validate.js.map +1 -1
  26. package/dist/connector.js +14 -2
  27. package/dist/connector.js.map +1 -1
  28. package/dist/lib.d.ts +2 -1
  29. package/dist/lib.js +223 -21
  30. package/dist/lib.js.map +1 -1
  31. package/dist/pyproject.d.ts +1 -0
  32. package/dist/pyproject.js +16 -0
  33. package/dist/pyproject.js.map +1 -0
  34. package/dist/python-source.d.ts +25 -0
  35. package/dist/python-source.js +359 -0
  36. package/dist/python-source.js.map +1 -0
  37. package/dist/sourceWalk.d.ts +2 -1
  38. package/dist/sourceWalk.js +5 -3
  39. package/dist/sourceWalk.js.map +1 -1
  40. package/dist/verdict.d.ts +6 -0
  41. package/dist/verdict.js +13 -0
  42. package/dist/verdict.js.map +1 -0
  43. package/package.json +3 -2
package/dist/adopt.js CHANGED
@@ -1,21 +1,22 @@
1
1
  import { lstat, mkdir, readFile, writeFile } from "node:fs/promises";
2
2
  import { dirname, join } from "node:path";
3
- import { CONNECTION_HELPER_PATH, CONNECTION_HELPER_SOURCE, connectionInputNames, FIXTURES_DIR, fixtureProblem, ICON_PATH, isAllowedSourceDir, isAllowedSourcePath, isLocalOnlySource, MANIFEST_FILE, PackedArchiveError, packageNameProblem, packedName, parsePacked, readPackedEntries, } from "@uipath/uipath-extensions-gates";
3
+ import { isDeepStrictEqual } from "node:util";
4
+ import { assetsProblem, CONNECTION_HELPER_PATH, CONNECTION_HELPER_SOURCE, connectionInputNames, FIXTURES_DIR, fixtureProblem, ICON_PATH, ICON_PATH_PATTERN, iconPathOf, isAllowedSourceDir, isAllowedSourcePath, isLocalOnlySource, MANIFEST_FILE, PackedArchiveError, PYPROJECT, packageNameProblem, packedName, parsePacked, parseTomlText, readPackedEntries, readTomlFile, templateProblems, UV_LOCK, } from "@uipath/uipath-extensions-gates";
4
5
  import { strFromU8 } from "fflate";
5
6
  import { parse } from "yaml";
6
7
  import { labelOf, TYPES_NODE_PACKAGE, TYPES_NODE_VERSION, } from "./commands/new.js";
7
- import { PackError, withRegistrationExcluded } from "./commands/pack.js";
8
+ import { PACK_TIMEOUT_MS, PackError, projectKind, runUipPack, tail, } from "./commands/pack.js";
8
9
  import { DEFAULT_INDEX_REPO } from "./commands/submit.js";
9
10
  import { DEFAULT_ICON_BASE64 } from "./defaultIcon.js";
10
11
  import { exists } from "./exists.js";
11
12
  import { FIXTURE_PATH, starterFixtureText } from "./fixture.js";
12
13
  import { GitHubError, ghClient } from "./github.js";
14
+ import { setProjectField } from "./pyproject.js";
15
+ import { checkPythonAdoptFiles, isPythonFunctionProject, isPythonFunctionVersion, PythonFunctionError, packConfig, pythonFunctions, validPythonFunctionTarget, } from "./python-source.js";
13
16
  import { REGISTRATION_FILE } from "./registrationFile.js";
14
17
  import { excludedNames, isSkippedEntry, offListRemedy, walkSources, } from "./sourceWalk.js";
15
18
  import { symlinkProblem } from "./symlinkProblem.js";
16
19
  import { UipMissingError } from "./uip.js";
17
- const PACK_TIMEOUT_MS = 600_000;
18
- const MAX_OUTPUT_CHARS = 2000;
19
20
  const POLICY_FILE = "policy/gate-library.yaml";
20
21
  const SCHEMA_KEY = "template_schema";
21
22
  export const NAME_PREFIX = "uipath-extensions.";
@@ -69,7 +70,6 @@ const jsonWriter = (text) => {
69
70
  const newline = text.endsWith("\n") ? "\n" : "";
70
71
  return (value) => `${JSON.stringify(value, null, indent)}${newline}`;
71
72
  };
72
- const tail = (text) => text.length > MAX_OUTPUT_CHARS ? `...${text.slice(-MAX_OUTPUT_CHARS)}` : text;
73
73
  async function readText(path) {
74
74
  try {
75
75
  return await readFile(path, "utf8");
@@ -78,6 +78,26 @@ async function readText(path) {
78
78
  return undefined;
79
79
  }
80
80
  }
81
+ // A lossy read is unsafe when init may write back the same file.
82
+ async function readPythonText(dir, file) {
83
+ let bytes;
84
+ try {
85
+ bytes = await readFile(join(dir, file));
86
+ }
87
+ catch (error) {
88
+ return error.code === "ENOENT"
89
+ ? { text: undefined }
90
+ : { error: `${file}: file not readable` };
91
+ }
92
+ try {
93
+ return {
94
+ text: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes),
95
+ };
96
+ }
97
+ catch {
98
+ return { error: `${file} must be valid UTF-8` };
99
+ }
100
+ }
81
101
  function parseJson(text) {
82
102
  try {
83
103
  return JSON.parse(text);
@@ -105,31 +125,58 @@ function templateOperation(text) {
105
125
  ? first.name
106
126
  : undefined;
107
127
  }
108
- async function outsideSourceList(root, excluded) {
128
+ async function outsideSourceList(root, excluded, kind = "function") {
109
129
  const found = [];
130
+ const listed = [];
110
131
  await walkSources(root, async (entry, path, dir) => {
111
- if (isSkippedEntry(entry, dir, excluded) ||
112
- (dir === "" && isLocalOnlySource(entry.name)) ||
132
+ if (isSkippedEntry(entry, dir, excluded, kind) ||
133
+ (dir === "" && isLocalOnlySource(entry.name, kind)) ||
113
134
  (dir === "" && entry.isFile() && entry.name === REGISTRATION_FILE)) {
114
135
  return false;
115
136
  }
116
137
  if (entry.isDirectory()) {
117
- if (dir === "" && !isAllowedSourceDir(entry.name)) {
138
+ if (dir === "" && !isAllowedSourceDir(entry.name, kind)) {
118
139
  found.push(`${path}/`);
119
140
  return false;
120
141
  }
121
142
  return true;
122
143
  }
123
- if (!entry.isFile() || !isAllowedSourcePath(path))
144
+ if (!entry.isFile() || !isAllowedSourcePath(path, kind)) {
124
145
  found.push(path);
146
+ }
147
+ else {
148
+ listed.push(path);
149
+ }
125
150
  return false;
126
151
  });
127
- return found;
152
+ return { found, listed };
153
+ }
154
+ async function planSourceReports(plan, dir, excluded, kind = "function") {
155
+ const { found, listed } = await outsideSourceList(dir, excluded, kind);
156
+ for (const path of found) {
157
+ plan.reports.push({
158
+ path,
159
+ problem: `is not on the source list, ${offListRemedy(path)}`,
160
+ });
161
+ }
162
+ const assets = assetsProblem([
163
+ ...listed,
164
+ ...plan.writes.map((write) => write.path),
165
+ ]);
166
+ if (assets !== undefined) {
167
+ plan.reports.push({ path: "assets/", problem: `is refused, ${assets}` });
168
+ }
169
+ }
170
+ function templateIconPath(manifestText) {
171
+ const template = manifestText === undefined ? undefined : parseJson(manifestText);
172
+ const display = isRecord(template) ? template.display : undefined;
173
+ const icon = iconPathOf(isRecord(display) ? display : undefined);
174
+ return ICON_PATH_PATTERN.test(icon) ? icon : ICON_PATH;
128
175
  }
129
- async function junkNames(root, excluded) {
176
+ async function junkNames(root, excluded, kind = "function") {
130
177
  const found = new Set();
131
178
  await walkSources(root, async (entry, _path, dir) => {
132
- if (isSkippedEntry(entry, dir, excluded))
179
+ if (isSkippedEntry(entry, dir, excluded, kind))
133
180
  return false;
134
181
  const key = entry.isDirectory()
135
182
  ? "directoriesExcluded"
@@ -138,7 +185,8 @@ async function junkNames(root, excluded) {
138
185
  found.add(entry.name);
139
186
  return false;
140
187
  }
141
- return (entry.isDirectory() && (dir !== "" || isAllowedSourceDir(entry.name)));
188
+ return (entry.isDirectory() &&
189
+ (dir !== "" || isAllowedSourceDir(entry.name, kind)));
142
190
  });
143
191
  const keys = Object.keys(JUNK);
144
192
  return keys
@@ -274,36 +322,22 @@ function operationName(filePath) {
274
322
  return /^[A-Za-z]/.test(name) ? name : "run";
275
323
  }
276
324
  async function runPack(deps, dir, nupkgName) {
277
- let result;
278
325
  try {
279
- result = await withRegistrationExcluded(dir, () => deps.exec("uip", ["function", "pack"], {
280
- cwd: dir,
281
- timeout: PACK_TIMEOUT_MS,
282
- }));
326
+ return {
327
+ nupkg: await runUipPack({
328
+ projectDir: dir,
329
+ exec: deps.exec,
330
+ command: "uip function pack",
331
+ nupkgName,
332
+ }),
333
+ };
283
334
  }
284
335
  catch (error) {
285
- if (error instanceof PackError)
336
+ if (error instanceof PackError || error instanceof UipMissingError) {
286
337
  return { error: error.message };
287
- if (error.code === "ENOENT") {
288
- return { error: new UipMissingError().message };
289
338
  }
290
339
  throw error;
291
340
  }
292
- if (result.timedOut)
293
- return { error: "uip function pack timed out" };
294
- if (result.code !== 0) {
295
- return {
296
- error: `uip function pack exited with code ${result.code}: ${tail(result.stderr || result.stdout)}`,
297
- };
298
- }
299
- for (const where of [".uipath", ""]) {
300
- const candidate = join(dir, where, nupkgName);
301
- if (await exists(candidate))
302
- return { nupkg: candidate };
303
- }
304
- return {
305
- error: `uip function pack finished but ${nupkgName} was not created in the project or in .uipath`,
306
- };
307
341
  }
308
342
  function runtimeProblems(entries) {
309
343
  const bytes = entries[packedName(entries, "operate.json")];
@@ -331,8 +365,370 @@ function undescribedInputs(input) {
331
365
  property.description.trim() === "")
332
366
  .map(([name]) => name);
333
367
  }
368
+ export { isPythonFunctionProject } from "./python-source.js";
369
+ export async function pythonFunctionNeedsId(dir) {
370
+ if (!(await isPythonFunctionProject(dir)))
371
+ return false;
372
+ const result = await readTomlFile(join(dir, PYPROJECT));
373
+ const project = "value" in result && result.value.project;
374
+ return isRecord(project) && packageNameProblem(project.name) !== undefined;
375
+ }
376
+ function replaceProjectName(text, name) {
377
+ const lines = text.split("\n");
378
+ let inProject = false;
379
+ let changed = false;
380
+ for (const [i, line] of lines.entries()) {
381
+ if (/^\s*\[/.test(line)) {
382
+ inProject = /^\s*\[project\]\s*(?:#.*)?$/.test(line);
383
+ }
384
+ if (!inProject)
385
+ continue;
386
+ const match = /^(\s*name\s*=\s*)(?:"[^"\\]*"|'[^']*')(\s*(?:#.*)?)$/.exec(line);
387
+ if (!match)
388
+ continue;
389
+ if (changed)
390
+ return undefined;
391
+ lines[i] = `${match[1]}"${name}"${match[2]}`;
392
+ changed = true;
393
+ }
394
+ return changed ? lines.join("\n") : undefined;
395
+ }
396
+ async function planPythonFunction(opts, deps, uipath, uipathText, functions) {
397
+ const plan = { writes: [], reports: [], errors: [] };
398
+ const fail = (message) => ({
399
+ writes: [],
400
+ reports: [],
401
+ errors: [...plan.errors, message],
402
+ });
403
+ const entries = Object.entries(functions);
404
+ if (entries.length !== 1)
405
+ return fail("a node holds one function");
406
+ const [main, target] = entries[0];
407
+ if (!/^[A-Za-z][A-Za-z0-9_-]*$/.test(main) ||
408
+ !validPythonFunctionTarget(target)) {
409
+ return fail("uipath.json must map a function name to a Python source file and function");
410
+ }
411
+ const projectText = await readPythonText(opts.dir, PYPROJECT);
412
+ if ("error" in projectText)
413
+ return fail(projectText.error);
414
+ const before = projectText.text;
415
+ if (before === undefined)
416
+ return fail(`${PYPROJECT}: file not readable`);
417
+ const parsed = parseTomlText(before);
418
+ if ("error" in parsed)
419
+ return fail(`${PYPROJECT}: ${parsed.error}`);
420
+ const project = parsed.value.project;
421
+ if (!isRecord(project) ||
422
+ typeof project.name !== "string" ||
423
+ typeof project.version !== "string") {
424
+ return fail(`${PYPROJECT} must have a [project] name and version`);
425
+ }
426
+ if (!isPythonFunctionVersion(project.version))
427
+ return fail(`${PYPROJECT} needs a plain x.y.z package version`);
428
+ const iconExclusion = [...ICON_NAMES, ICON_PATH].find((entry) => excludedNames(uipath).has(entry));
429
+ if (iconExclusion !== undefined)
430
+ return fail(iconExcludedProblem(iconExclusion));
431
+ let hasIcon;
432
+ try {
433
+ packConfig(uipath);
434
+ hasIcon = await checkPythonAdoptFiles(opts.dir, target.split(":")[0]);
435
+ }
436
+ catch (error) {
437
+ if (error instanceof PythonFunctionError)
438
+ return fail(error.message);
439
+ throw error;
440
+ }
441
+ let name = project.name;
442
+ if (packageNameProblem(name)) {
443
+ if (opts.id === undefined)
444
+ return fail(MISSING_ID_ERROR);
445
+ name = `${NAME_PREFIX}${opts.id}`;
446
+ const problem = packageNameProblem(name);
447
+ if (problem !== undefined)
448
+ return fail(problem.replace("package.json name", "pyproject.toml [project].name"));
449
+ const after = replaceProjectName(before, name);
450
+ if (after === undefined) {
451
+ return fail('place the project name on one standalone name = "..." line in [project]');
452
+ }
453
+ const updated = parseTomlText(after);
454
+ const nextProject = "value" in updated ? updated.value.project : undefined;
455
+ if ("error" in updated ||
456
+ !isRecord(nextProject) ||
457
+ !isDeepStrictEqual({ ...updated.value, project: { ...nextProject } }, { ...parsed.value, project: { ...project, name } })) {
458
+ return fail(`renaming the package would change another field in ${PYPROJECT}`);
459
+ }
460
+ plan.writes.push({ path: PYPROJECT, before, after, identity: true });
461
+ if (await exists(join(opts.dir, "uv.lock"))) {
462
+ plan.reports.push({
463
+ path: "uv.lock",
464
+ problem: "run uv lock after the Python project is renamed",
465
+ });
466
+ }
467
+ }
468
+ const existingManifest = await readPythonText(opts.dir, MANIFEST_FILE);
469
+ if ("error" in existingManifest)
470
+ return fail(existingManifest.error);
471
+ const manifestText = existingManifest.text;
472
+ if (manifestText === undefined) {
473
+ if (project.license !== undefined &&
474
+ (typeof project.license !== "string" || project.license.trim() === "")) {
475
+ return fail(`${PYPROJECT} [project].license must be an SPDX string before generating the extension manifest; initialize with an explicit license instead of replacing the existing declaration`);
476
+ }
477
+ const found = opts.offline
478
+ ? { schema: 2 }
479
+ : await templateSchema(deps, opts.index ?? DEFAULT_INDEX_REPO);
480
+ if ("error" in found)
481
+ return fail(found.error);
482
+ const template = newTemplate(found.schema, project.version, name, operationName(main), hasIcon ? "own" : "default", typeof project.license === "string" ? project.license : undefined);
483
+ plan.writes.push({
484
+ path: MANIFEST_FILE,
485
+ before: null,
486
+ after: template.text,
487
+ });
488
+ plan.generated = template.generated;
489
+ if (!hasIcon)
490
+ plan.writes.push(defaultIconWrite());
491
+ }
492
+ else {
493
+ planExistingTemplateIcon(plan, {
494
+ text: manifestText,
495
+ hasIcon,
496
+ iconExclusion,
497
+ });
498
+ }
499
+ const plannedManifest = plan.writes.find(({ path }) => path === MANIFEST_FILE)?.after ??
500
+ manifestText;
501
+ const parsedManifest = plannedManifest === undefined ? undefined : parseJson(plannedManifest);
502
+ const problems = templateProblems(parsedManifest);
503
+ if (problems.length > 0)
504
+ return fail(`invalid ${MANIFEST_FILE}: ${problems.join("; ")}`);
505
+ const display = isRecord(parsedManifest) ? parsedManifest.display : undefined;
506
+ if (isRecord(display) && display.icon === ICON_PATH) {
507
+ const options = uipath.packOptions;
508
+ if (options !== undefined && !isRecord(options)) {
509
+ return fail("uipath.json packOptions must be an object");
510
+ }
511
+ const files = options?.filesIncluded ?? [];
512
+ if (!Array.isArray(files) ||
513
+ !files.every((entry) => typeof entry === "string")) {
514
+ return fail("uipath.json packOptions.filesIncluded must be a list of filenames");
515
+ }
516
+ if (!files.includes(ICON_PATH)) {
517
+ plan.writes.push({
518
+ path: "uipath.json",
519
+ before: uipathText,
520
+ after: jsonWriter(uipathText)({
521
+ ...uipath,
522
+ packOptions: { ...options, filesIncluded: [...files, ICON_PATH] },
523
+ }),
524
+ });
525
+ }
526
+ }
527
+ for (const { path } of plan.writes) {
528
+ const problem = await symlinkProblem(opts.dir, path, "adopt");
529
+ if (problem !== undefined)
530
+ plan.errors.push(problem);
531
+ }
532
+ return plan;
533
+ }
534
+ async function planTemplate(plan, opts, deps, node) {
535
+ const { manifestText, hasIcon, iconExclusion } = node;
536
+ if (manifestText !== undefined) {
537
+ planExistingTemplateIcon(plan, {
538
+ text: manifestText,
539
+ hasIcon,
540
+ iconExclusion,
541
+ });
542
+ return;
543
+ }
544
+ const found = opts.offline
545
+ ? { schema: 2 }
546
+ : await templateSchema(deps, opts.index ?? DEFAULT_INDEX_REPO);
547
+ if ("error" in found) {
548
+ plan.errors.push(found.error);
549
+ return;
550
+ }
551
+ const skipIcon = iconExclusion !== undefined && found.schema === undefined;
552
+ if (iconExclusion !== undefined && !skipIcon) {
553
+ plan.errors.push(iconExcludedProblem(iconExclusion));
554
+ }
555
+ const template = newTemplate(found.schema, node.version, node.packageName, node.operation, skipIcon ? "none" : hasIcon ? "own" : "default", node.license);
556
+ plan.writes.push({
557
+ path: MANIFEST_FILE,
558
+ before: null,
559
+ after: template.text,
560
+ });
561
+ plan.generated = template.generated;
562
+ if (!hasIcon && !skipIcon)
563
+ plan.writes.push(defaultIconWrite());
564
+ }
565
+ const AGENT_OPERATION = "run";
566
+ const ENTRY_POINTS_FILE = "entry-points.json";
567
+ export const AGENT_INIT = "uipath.json and entry-points.json are missing. Run init, which runs uip codedagent init to write them";
568
+ export const AGENT_LOCK = `${UV_LOCK} is missing. Run init, which runs uv lock to write it`;
569
+ const missing = async (dir, name) => (await readText(join(dir, name))) === undefined;
570
+ const needsCodedAgentInit = async (dir) => (await missing(dir, "uipath.json")) ||
571
+ (await missing(dir, ENTRY_POINTS_FILE));
572
+ export async function needsAgentInit(dir) {
573
+ return ((await projectKind(dir)) === "agent" &&
574
+ ((await needsCodedAgentInit(dir)) || (await missing(dir, UV_LOCK))));
575
+ }
576
+ async function runStep(exec, dir, command, args) {
577
+ const name = [command, ...args].join(" ");
578
+ let result;
579
+ try {
580
+ result = await exec(command, args, { cwd: dir, timeout: PACK_TIMEOUT_MS });
581
+ }
582
+ catch (error) {
583
+ if (error.code === "ENOENT") {
584
+ return command === "uip"
585
+ ? new UipMissingError().message
586
+ : `${command} was not found on the PATH. Install it, then run this command again.`;
587
+ }
588
+ throw error;
589
+ }
590
+ if (result.timedOut)
591
+ return `${name} timed out`;
592
+ return result.code === 0
593
+ ? undefined
594
+ : `${name} exited with code ${result.code}: ${tail(result.stderr || result.stdout)}`;
595
+ }
596
+ export async function runAgentInit(dir, exec) {
597
+ if (await needsCodedAgentInit(dir)) {
598
+ const failure = await runStep(exec, dir, "uip", ["codedagent", "init"]);
599
+ if (failure !== undefined)
600
+ return failure;
601
+ if (await needsCodedAgentInit(dir)) {
602
+ return "uip codedagent init finished but did not write uipath.json and entry-points.json";
603
+ }
604
+ }
605
+ if (await missing(dir, UV_LOCK)) {
606
+ const failure = await runStep(exec, dir, "uv", ["lock"]);
607
+ if (failure !== undefined)
608
+ return failure;
609
+ if (await missing(dir, UV_LOCK)) {
610
+ return `uv lock finished but did not write ${UV_LOCK}`;
611
+ }
612
+ }
613
+ return undefined;
614
+ }
615
+ function agentPackOptions(text, uipath, packsIcon, iconPath) {
616
+ const options = uipath.packOptions === undefined ? {} : uipath.packOptions;
617
+ if (!isRecord(options)) {
618
+ return { error: "uipath.json packOptions is not an object" };
619
+ }
620
+ const included = options.filesIncluded === undefined ? [] : options.filesIncluded;
621
+ if (!Array.isArray(included)) {
622
+ return { error: "uipath.json packOptions.filesIncluded is not a list" };
623
+ }
624
+ const listsIcon = !packsIcon || included.includes(iconPath);
625
+ if (listsIcon && options.includeUvLock === true)
626
+ return {};
627
+ return {
628
+ write: {
629
+ path: "uipath.json",
630
+ before: text,
631
+ after: jsonWriter(text)({
632
+ ...uipath,
633
+ packOptions: {
634
+ ...options,
635
+ ...(!listsIcon && { filesIncluded: [...included, iconPath] }),
636
+ includeUvLock: true,
637
+ },
638
+ }),
639
+ },
640
+ };
641
+ }
642
+ async function planAgentAdopt(opts, deps, plan) {
643
+ const fail = (message) => {
644
+ plan.errors.push(message);
645
+ return plan;
646
+ };
647
+ const uipathText = await readText(join(opts.dir, "uipath.json"));
648
+ const entryText = await readText(join(opts.dir, ENTRY_POINTS_FILE));
649
+ if (uipathText === undefined || entryText === undefined) {
650
+ return fail(AGENT_INIT);
651
+ }
652
+ if (await missing(opts.dir, UV_LOCK))
653
+ return fail(AGENT_LOCK);
654
+ const uipath = parseJson(uipathText);
655
+ if (!isRecord(uipath))
656
+ return fail("uipath.json is not a valid JSON object");
657
+ const entry = parseJson(entryText);
658
+ const entryPoints = isRecord(entry) ? entry.entryPoints : undefined;
659
+ if (!Array.isArray(entryPoints) || entryPoints.length !== 1) {
660
+ return fail(`${ENTRY_POINTS_FILE} must list one entry point, a node holds one agent`);
661
+ }
662
+ const pyprojectText = await readText(join(opts.dir, PYPROJECT));
663
+ const toml = await readTomlFile(join(opts.dir, PYPROJECT));
664
+ if (pyprojectText === undefined || "error" in toml) {
665
+ return fail(`${PYPROJECT}: ${"error" in toml ? toml.error : "file not found"}`);
666
+ }
667
+ const { project } = toml.value;
668
+ if (!isRecord(project) || typeof project.version !== "string") {
669
+ return fail(`${PYPROJECT} needs a [project] table with a version`);
670
+ }
671
+ let packageName = String(project.name);
672
+ if (packageNameProblem(project.name) !== undefined) {
673
+ if (opts.id === undefined) {
674
+ plan.errors.push(MISSING_ID_ERROR);
675
+ }
676
+ else {
677
+ const name = `${NAME_PREFIX}${opts.id}`;
678
+ const problem = packageNameProblem(name);
679
+ const after = problem === undefined
680
+ ? setProjectField(pyprojectText, "name", name)
681
+ : undefined;
682
+ if (problem !== undefined) {
683
+ plan.errors.push(problem);
684
+ }
685
+ else if (after === undefined) {
686
+ plan.errors.push(`${PYPROJECT} has no [project] name line to set`);
687
+ }
688
+ else {
689
+ packageName = name;
690
+ plan.writes.push({
691
+ path: PYPROJECT,
692
+ before: pyprojectText,
693
+ after,
694
+ identity: true,
695
+ });
696
+ }
697
+ }
698
+ }
699
+ const manifestText = await readText(join(opts.dir, MANIFEST_FILE));
700
+ const iconPath = templateIconPath(manifestText);
701
+ const hasIcon = await exists(join(opts.dir, iconPath));
702
+ const excluded = excludedNames(uipath);
703
+ await planTemplate(plan, opts, deps, {
704
+ manifestText,
705
+ version: project.version,
706
+ packageName,
707
+ operation: templateOperation(manifestText) ?? AGENT_OPERATION,
708
+ hasIcon,
709
+ iconExclusion: iconPath.split("/").find((name) => excluded.has(name)),
710
+ license: undefined,
711
+ });
712
+ const packOptions = agentPackOptions(uipathText, uipath, hasIcon || plan.writes.some((write) => write.path === ICON_PATH), iconPath);
713
+ if ("error" in packOptions) {
714
+ plan.errors.push(packOptions.error);
715
+ }
716
+ else if (packOptions.write) {
717
+ plan.writes.push(packOptions.write);
718
+ }
719
+ await planSourceReports(plan, opts.dir, excluded, "agent");
720
+ for (const { path } of plan.writes) {
721
+ const problem = await symlinkProblem(opts.dir, path, "adopt");
722
+ if (problem !== undefined)
723
+ plan.errors.push(problem);
724
+ }
725
+ return plan;
726
+ }
334
727
  export async function planAdopt(opts, deps) {
335
728
  const plan = { writes: [], reports: [], errors: [] };
729
+ if ((await projectKind(opts.dir)) === "agent") {
730
+ return planAgentAdopt(opts, deps, plan);
731
+ }
336
732
  const fail = (message) => {
337
733
  plan.errors.push(message);
338
734
  return plan;
@@ -345,8 +741,18 @@ export async function planAdopt(opts, deps) {
345
741
  if (!isRecord(uipath))
346
742
  return fail("uipath.json is not a valid JSON object");
347
743
  const packageText = await readText(join(opts.dir, "package.json"));
348
- if (packageText === undefined)
744
+ if (packageText === undefined) {
745
+ const functions = pythonFunctions(uipath);
746
+ if (functions !== undefined && (await isPythonFunctionProject(opts.dir))) {
747
+ const exact = await readPythonText(opts.dir, "uipath.json");
748
+ if ("error" in exact)
749
+ return fail(exact.error);
750
+ if (exact.text !== uipathText)
751
+ return fail("uipath.json changed while planning, run again");
752
+ return planPythonFunction(opts, deps, uipath, uipathText, functions);
753
+ }
349
754
  return fail("package.json not found");
755
+ }
350
756
  const pkg = parseJson(packageText);
351
757
  if (!isRecord(pkg))
352
758
  return fail("package.json is not a valid JSON object");
@@ -402,36 +808,15 @@ export async function planAdopt(opts, deps) {
402
808
  const iconExclusion = ICON_NAMES.find((name) => excluded.has(name));
403
809
  const manifestText = await readText(join(opts.dir, MANIFEST_FILE));
404
810
  const operation = templateOperation(manifestText) ?? operationName(entry.filePath);
405
- if (manifestText === undefined) {
406
- const found = opts.offline
407
- ? { schema: 2 }
408
- : await templateSchema(deps, opts.index ?? DEFAULT_INDEX_REPO);
409
- if ("error" in found) {
410
- plan.errors.push(found.error);
411
- }
412
- else {
413
- const skipIcon = iconExclusion !== undefined && found.schema === undefined;
414
- if (iconExclusion !== undefined && !skipIcon) {
415
- plan.errors.push(iconExcludedProblem(iconExclusion));
416
- }
417
- const template = newTemplate(found.schema, pkg.version, String(nextPkg.name), operation, skipIcon ? "none" : hasIcon ? "own" : "default", pkg.license);
418
- plan.writes.push({
419
- path: MANIFEST_FILE,
420
- before: null,
421
- after: template.text,
422
- });
423
- plan.generated = template.generated;
424
- if (!hasIcon && !skipIcon)
425
- plan.writes.push(defaultIconWrite());
426
- }
427
- }
428
- else {
429
- planExistingTemplateIcon(plan, {
430
- text: manifestText,
431
- hasIcon,
432
- iconExclusion,
433
- });
434
- }
811
+ await planTemplate(plan, opts, deps, {
812
+ manifestText,
813
+ version: pkg.version,
814
+ packageName: String(nextPkg.name),
815
+ operation,
816
+ hasIcon,
817
+ iconExclusion,
818
+ license: pkg.license,
819
+ });
435
820
  if ((await fixtureProblem(opts.dir)) !== undefined) {
436
821
  const blocked = await starterFixtureBlocked(opts.dir);
437
822
  if (blocked !== undefined) {
@@ -495,12 +880,7 @@ export async function planAdopt(opts, deps) {
495
880
  skipped = new Set([...excluded, ...junk.flatMap(([, names]) => names)]);
496
881
  }
497
882
  }
498
- for (const path of await outsideSourceList(opts.dir, skipped)) {
499
- plan.reports.push({
500
- path,
501
- problem: `is not on the source list, ${offListRemedy(path)}`,
502
- });
503
- }
883
+ await planSourceReports(plan, opts.dir, skipped);
504
884
  for (const name of undescribedInputs(entry.input)) {
505
885
  plan.reports.push({
506
886
  path: "entry-points.json",
@@ -519,11 +899,13 @@ export async function applyAdopt(plan, opts) {
519
899
  const problem = await symlinkProblem(opts.dir, path, "adopt");
520
900
  if (problem !== undefined)
521
901
  throw new Error(problem);
522
- if (before !== null && (await readText(join(opts.dir, path))) !== before) {
523
- throw new StaleAdoptPlanError(path);
902
+ if (before !== null) {
903
+ const actual = await readFile(join(opts.dir, path)).catch(() => undefined);
904
+ if (!actual?.equals(Buffer.from(before, "utf8")))
905
+ throw new StaleAdoptPlanError(path);
524
906
  }
525
907
  }
526
- const markers = new Set(["package.json", MANIFEST_FILE]);
908
+ const markers = new Set(["package.json", PYPROJECT, MANIFEST_FILE]);
527
909
  const ordered = [
528
910
  ...plan.writes.filter(({ path }) => !markers.has(path)),
529
911
  ...plan.writes.filter(({ path }) => markers.has(path)),